/core/externals/update-engine/externals/google-toolbox-for-mac/UnitTesting/GTMUnitTestingBindingTest.m
Objective C | 116 lines | 74 code | 18 blank | 24 comment | 4 complexity | 19a6a6e90f2374cc79b4172fc4178a15 MD5 | raw file
1// 2// GTMUnitTestingBindingTest.m 3// 4// Copyright 2008 Google Inc. 5// 6// Licensed under the Apache License, Version 2.0 (the "License"); you may not 7// use this file except in compliance with the License. You may obtain a copy 8// of the License at 9// 10// http://www.apache.org/licenses/LICENSE-2.0 11// 12// Unless required by applicable law or agreed to in writing, software 13// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 14// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 15// License for the specific language governing permissions and limitations under 16// the License. 17// 18 19#import "GTMSenTestCase.h" 20#import "GTMUnitTestingTest.h" 21#import "GTMNSObject+BindingUnitTesting.h" 22 23@interface GTMUnitTestingBindingTest : GTMTestCase { 24 int expectedFailureCount_; 25} 26@end 27 28@interface GTMUnitTestingBindingBadClass : NSObject 29@end 30 31@implementation GTMUnitTestingBindingTest 32 33// Iterates through all of our subviews testing the exposed bindings 34- (void)doSubviewBindingTest:(NSView*)view { 35 NSArray *subviews = [view subviews]; 36 NSView *subview; 37 GTM_FOREACH_OBJECT(subview, subviews) { 38 GTMTestExposedBindings(subview, @"testing %@", subview); 39 [self doSubviewBindingTest:subview]; 40 } 41} 42 43- (void)testBindings { 44 // Get our window to work with and test it's bindings 45 GTMUnitTestingTestController *testWindowController 46 = [[GTMUnitTestingTestController alloc] initWithWindowNibName:@"GTMUnitTestingTest"]; 47 NSWindow *window = [testWindowController window]; 48 GTMTestExposedBindings(window, @"Window failed binding test"); 49 [self doSubviewBindingTest:[window contentView]]; 50 [window close]; 51 [testWindowController release]; 52 53 // Run a test against something with no bindings. 54 // We're expecting a failure here. 55 expectedFailureCount_ = 1; 56 GTMTestExposedBindings(@"foo", @"testing no bindings"); 57 STAssertEquals(expectedFailureCount_, 0, @"Didn't get expected failures testing bindings"); 58 59 // Run test against some with bad bindings. 60 // We're expecting failures here. 61 expectedFailureCount_ = 4; 62 GTMUnitTestingBindingBadClass *bad = [[[GTMUnitTestingBindingBadClass alloc] init] autorelease]; 63 GTMTestExposedBindings(bad, @"testing bad bindings"); 64 STAssertEquals(expectedFailureCount_, 0, @"Didn't get expected failures testing bad bindings"); 65} 66 67- (void)failWithException:(NSException *)anException { 68 if (expectedFailureCount_ > 0) { 69 expectedFailureCount_ -= 1; 70 } else { 71 [super failWithException:anException]; // COV_NF_LINE - not expecting exception 72 } 73} 74 75@end 76 77// Forces several error cases in our binding tests to test them 78@implementation GTMUnitTestingBindingBadClass 79 80NSString *const kGTMKeyWithNoClass = @"keyWithNoClass"; 81NSString *const kGTMKeyWithNoValue = @"keyWithNoValue"; 82NSString *const kGTMKeyWeCantSet = @"keyWeCantSet"; 83NSString *const kGTMKeyThatIsntEqual = @"keyThatIsntEqual"; 84 85- (NSArray *)exposedBindings { 86 return [NSArray arrayWithObjects:kGTMKeyWithNoClass, 87 kGTMKeyWithNoValue, 88 kGTMKeyWeCantSet, 89 kGTMKeyThatIsntEqual, 90 nil]; 91} 92 93- (NSArray*)gtm_unitTestExposedBindingsTestValues:(NSString*)binding { 94 GTMBindingUnitTestData *data 95 = [GTMBindingUnitTestData testWithIdentityValue:kGTMKeyThatIsntEqual]; 96 return [NSArray arrayWithObject:data]; 97} 98 99- (Class)valueClassForBinding:(NSString*)binding { 100 return [binding isEqualTo:kGTMKeyWithNoClass] ? nil : [NSString class]; 101} 102 103- (id)valueForKey:(NSString*)binding { 104 if ([binding isEqualTo:kGTMKeyWithNoValue]) { 105 [NSException raise:NSUndefinedKeyException format:@""]; 106 } 107 return @"foo"; 108} 109 110- (void)setValue:(id)value forKey:(NSString*)binding { 111 if ([binding isEqualTo:kGTMKeyWeCantSet]) { 112 [NSException raise:NSUndefinedKeyException format:@""]; 113 } 114} 115@end 116