PageRenderTime 30ms CodeModel.GetById 2ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMNSObject+KeyValueObservingTest.m

http://macfuse.googlecode.com/
Objective C | 182 lines | 132 code | 24 blank | 26 comment | 0 complexity | ee95fe92ac4c45023dbcdc52e7ce1ae4 MD5 | raw file
  1//
  2//  GTMNSObject+KeyValueObservingTest.m
  3//
  4//  Copyright 2009 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//
 20//  Tester.m
 21//  MAKVONotificationCenter
 22//
 23//  Created by Michael Ash on 10/15/08.
 24//
 25
 26// This code is based on code by Michael Ash.
 27// See comment in header.
 28
 29#import "GTMSenTestCase.h"
 30#import "GTMNSObject+KeyValueObserving.h"
 31#import "GTMDefines.h"
 32#import "GTMUnitTestDevLog.h"
 33
 34@interface GTMNSObject_KeyValueObservingTest : GTMTestCase  {
 35  int32_t count_;
 36  NSMutableDictionary *dict_;
 37  __weak NSString *expectedValue_;
 38}
 39
 40- (void)observeValueChange:(GTMKeyValueChangeNotification *)notification;
 41
 42@end
 43
 44@implementation GTMNSObject_KeyValueObservingTest
 45- (void)setUp {
 46  dict_ = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
 47          @"foo", @"key",
 48          nil];
 49}
 50
 51- (void)tearDown {
 52  [dict_ release];
 53}
 54
 55- (void)testSingleChange {
 56  count_ = 0;
 57  [dict_ gtm_addObserver:self
 58             forKeyPath:@"key"
 59               selector:@selector(observeValueChange:)
 60               userInfo:@"userInfo"
 61                options:NSKeyValueObservingOptionNew];
 62  expectedValue_ = @"bar";
 63  [dict_ setObject:expectedValue_ forKey:@"key"];
 64  STAssertEquals(count_, (int32_t)1, nil);
 65  [dict_ gtm_removeObserver:self
 66                 forKeyPath:@"key"
 67                  selector:@selector(observeValueChange:)];
 68  [dict_ setObject:@"foo" forKey:@"key"];
 69  STAssertEquals(count_, (int32_t)1, nil);
 70}
 71
 72- (void)testStopObservingAllKeyPaths {
 73  count_ = 0;
 74  [dict_ gtm_addObserver:self
 75              forKeyPath:@"key"
 76                selector:@selector(observeValueChange:)
 77                userInfo:@"userInfo"
 78                 options:NSKeyValueObservingOptionNew];
 79  expectedValue_ = @"bar";
 80  [dict_ setObject:expectedValue_ forKey:@"key"];
 81  STAssertEquals(count_, (int32_t)1, nil);
 82  [self gtm_stopObservingAllKeyPaths];
 83  [dict_ setObject:@"foo" forKey:@"key"];
 84  STAssertEquals(count_, (int32_t)1, nil);
 85}
 86
 87
 88- (void)testRemoving {
 89  [GTMUnitTestDevLogDebug expectPattern:@"-\\[GTMNSObject_KeyValueObservingTest"
 90   @" testRemoving\\] was not observing.*"];
 91
 92  [dict_ gtm_removeObserver:self
 93                 forKeyPath:@"key"
 94                   selector:@selector(observeValueChange:)];
 95}
 96
 97- (void)testAdding {
 98  [dict_ gtm_addObserver:self
 99              forKeyPath:@"key"
100                selector:@selector(observeValueChange:)
101                userInfo:@"userInfo"
102                 options:NSKeyValueObservingOptionNew];
103  [GTMUnitTestDevLog expectPattern:@"-\\[GTMNSObject_KeyValueObservingTest"
104   @" testAdding\\] already observing.*"];
105  [dict_ gtm_addObserver:self
106              forKeyPath:@"key"
107                selector:@selector(observeValueChange:)
108                userInfo:@"userInfo"
109                 options:NSKeyValueObservingOptionNew];
110  [dict_ gtm_removeObserver:self
111                 forKeyPath:@"key"
112                   selector:@selector(observeValueChange:)];
113}
114
115- (void)observeValueChange:(GTMKeyValueChangeNotification *)notification {
116  STAssertEqualObjects([notification userInfo], @"userInfo", nil);
117  STAssertEqualObjects([notification keyPath], @"key", nil);
118  STAssertEqualObjects([notification object], dict_, nil);
119  NSDictionary *change = [notification change];
120  NSString *value = [change objectForKey:NSKeyValueChangeNewKey];
121  STAssertEqualObjects(value, expectedValue_, nil);
122  ++count_;
123
124  GTMKeyValueChangeNotification *copy = [[notification copy] autorelease];
125  STAssertEqualObjects(notification, copy, nil);
126  STAssertEquals([notification hash], [copy hash], nil);
127}
128
129@end
130
131#if GTM_PERFORM_KVO_CHECKS
132@interface GTMNSObject_KeyValueObservingChecksTest: GTMTestCase {
133 @private
134  id value_;
135  id _value2;
136  __weak NSArray *value3_;
137  __weak NSString *value4;
138}
139- (NSString *)value4;
140@end
141
142@implementation GTMNSObject_KeyValueObservingChecksTest
143
144- (void)setUp {
145  value_ = nil;
146  _value2 = nil;
147}
148
149- (void)testAddingObserver {
150  [GTMUnitTestDevLogDebug expectPattern:@"warning:.*"];
151  [self addObserver:self forKeyPath:@"value_" options:0 context:NULL];
152  [GTMUnitTestDevLogDebug expectPattern:@"warning:.*"];
153  [self addObserver:self forKeyPath:@"_value2" options:0 context:NULL];
154  value3_ = [NSArray arrayWithObject:@"foo"];
155  NSIndexSet *set = [NSIndexSet indexSetWithIndex:0];
156  [GTMUnitTestDevLogDebug expectPattern:@"warning:.*"];
157  [value3_ addObserver:self toObjectsAtIndexes:set forKeyPath:@"_fronttest"
158               options:0 context:NULL];
159  [GTMUnitTestDevLogDebug expectPattern:@"warning:.*"];
160  [value3_ addObserver:self toObjectsAtIndexes:set forKeyPath:@"backtest_"
161               options:0 context:NULL];
162#if DEBUG
163  // Should only throw in debug
164  STAssertThrows([self valueForKey:@"value_"], nil);
165#else
166  STAssertNoThrow([self valueForKey:@"value_"], nil);
167#endif
168  value4 = @"Hello";
169  STAssertEqualObjects([self valueForKey:@"value4"], @"Hello", nil);
170  [self removeObserver:self forKeyPath:@"value_"];
171  [self removeObserver:self forKeyPath:@"_value2"];
172  [value3_ removeObserver:self fromObjectsAtIndexes:set forKeyPath:@"_fronttest"];
173  [value3_ removeObserver:self fromObjectsAtIndexes:set forKeyPath:@"backtest_"];
174}
175
176- (NSString *)value4 {
177  return value4;
178}
179@end
180
181#endif  // GTM_PERFORM_KVO_CHECKS
182