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