1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSString.h>
#import <Foundation/NSKeyValueObserving.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSNull.h>
#import "ObjectTesting.h"
@interface Observer : NSObject
{
NSInteger count;
NSInteger kvoCount;
}
- (NSInteger)count;
- (NSInteger)kvoCount;
- (void)notified:(NSNotification *)n;
@end
@implementation Observer
- (NSInteger)count
{
return count;
}
- (NSInteger)kvoCount
{
return kvoCount;
}
- (void)notified:(NSNotification *)n
{
count++;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
id old = [change objectForKey:NSKeyValueChangeOldKey];
id new = [ change objectForKey : NSKeyValueChangeNewKey ];
NSKeyValueChange kind =
[[change objectForKey:NSKeyValueChangeKindKey] intValue];
id isPrior = [change objectForKey:NSKeyValueChangeNotificationIsPriorKey];
NSLog(@"KVO: %@: old = %@, new = %@, kind = %ld, isPrior = %@",
keyPath, old, new, kind, isPrior);
if ([keyPath isEqualToString:@"Test Suite Bool"])
{
switch (kvoCount)
{
case 0: // Initial
{
PASS_EQUAL(
new, [NSNull null],
"KVO: Initial setting of 'Test Suite Bool' has new = null");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Bool' is of kind "
"NSKeyValueChangeSetting (initial)");
break;
}
case 3: // Prior to [defs setBool:YES forKey:@"Test Suite Bool"];
{
PASS_EQUAL(
old, [NSNull null],
"KVO: First setting of 'Test Suite Bool' has old = null (prior)");
PASS(new == nil,
"KVO: First setting of 'Test Suite Bool' has no new (prior)");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Bool' is of kind "
"NSKeyValueChangeSetting (prior)");
PASS_EQUAL(isPrior, [NSNumber numberWithBool:YES],
"KVO: notification for 'Test Suite Bool' is prior");
break;
}
case 4: // [defs setBool:YES forKey:@"Test Suite Bool"];
{
PASS_EQUAL(
old, [NSNull null],
"KVO: First setting of 'Test Suite Bool' has old = null");
PASS([new isKindOfClass:[ NSNumber class ]],
"KVO: New value for 'Test Suite Bool' has NSNumber");
PASS(YES == [new boolValue],
"KVO: new value for 'Test Suite Bool' is YES");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Bool' is of kind "
"NSKeyValueChangeSetting");
break;
}
case 9: // Prior to [defs removeObjectForKey:@"Test Suite Bool"];
{
PASS([old isKindOfClass:[NSNumber class]],
"KVO: First setting of 'Test Suite Bool' has old NSNumber");
PASS(YES == [old boolValue],
"KVO: old value for 'Test Suite Bool' is YES");
PASS(new == nil,
"KVO: First setting of 'Test Suite Bool' has no new");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Bool' is of kind "
"NSKeyValueChangeSetting");
PASS_EQUAL(isPrior, [NSNumber numberWithBool:YES],
"KVO: notification for 'Test Suite Bool' is prior");
break;
}
case 10: // [defs removeObjectForKey:@"Test Suite Bool"];
{
PASS([old isKindOfClass:[NSNumber class]],
"KVO: First setting of 'Test Suite Bool' has old NSNumber");
PASS(YES == [old boolValue],
"KVO: old value for 'Test Suite Bool' is YES");
PASS_EQUAL(
new, [NSNull null],
"KVO: First setting of 'Test Suite Bool' has new = null");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Bool' is of kind "
"NSKeyValueChangeSetting");
break;
}
default: {
PASS(NO, "KVO: unexpected count for 'Test Suite Bool'");
break;
}
}
}
else if ([keyPath isEqualToString:@"Test Suite Int"])
{
switch (kvoCount)
{
case 1: // Initial
{
PASS_EQUAL(
new, [NSNull null],
"KVO: Initial setting of 'Test Suite Int' has new = null");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Int' is of kind "
"NSKeyValueChangeSetting (initial)");
break;
}
case 5: // Prior to [defs setInteger:34 forKey:@"Test
// Suite Int"];
{
PASS_EQUAL(old, [NSNull null],
"KVO: First setting of 'Test Suite Int' has old = null");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Int' is of kind "
"NSKeyValueChangeSetting");
PASS_EQUAL(isPrior, [NSNumber numberWithBool:YES],
"KVO: notification for 'Test Suite Int' is prior");
break;
}
case 6: // [defs setInteger:34 forKey:@"Test Suite Int"];
{
PASS_EQUAL(
old, [NSNull null],
"KVO: Second setting of 'Test Suite Int' has old = null");
PASS([new isKindOfClass:[ NSNumber class ]],
"KVO: New value for 'Test Suite Int' has NSNumber");
PASS(34 == [new intValue],
"KVO: new value for 'Test Suite Int' is 34");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Int' is of kind "
"NSKeyValueChangeSetting");
break;
}
case 11: // Prior to [defs setObject:nil
// forKey:@"Test Suite Int"];
{
PASS([old isKindOfClass:[NSNumber class]],
"KVO: First setting of 'Test Suite Int' has old NSNumber");
PASS(34 == [old intValue],
"KVO: old value for 'Test Suite Int' is 34");
PASS(new == nil,
"KVO: First setting of 'Test Suite Int' has no new");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Int' is of kind "
"NSKeyValueChangeSetting");
PASS_EQUAL(isPrior, [NSNumber numberWithBool:YES],
"KVO: notification for 'Test Suite Int' is prior");
break;
}
case 12: // [defs setObject:nil forKey:@"Test Suite Int"];
{
PASS([old isKindOfClass:[NSNumber class]],
"KVO: First setting of 'Test Suite Int' has old NSNumber");
PASS(34 == [old intValue],
"KVO: old value for 'Test Suite Int' is 34");
PASS_EQUAL(new, [NSNull null],
"KVO: First setting of 'Test Suite Int' has new = null");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Int' is of kind "
"NSKeyValueChangeSetting");
break;
}
default: {
PASS(NO, "KVO: unexpected count for 'Test Suite Int'");
break;
}
}
}
else if ([keyPath isEqualToString:@"Test Suite Str"])
{
switch (kvoCount)
{
case 2: // Initial
{
PASS_EQUAL(
new, [NSNull null],
"KVO: Initial setting of 'Test Suite Str' has new = null");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Str' is of kind "
"NSKeyValueChangeSetting (initial)");
break;
}
case 7: // Prior to [defs setObject:@"SetString"
// forKey:@"Test Suite Str"];
{
PASS_EQUAL(old, [NSNull null],
"KVO: First setting of 'Test Suite Str' has old = null");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Str' is of kind "
"NSKeyValueChangeSetting");
PASS_EQUAL(isPrior, [NSNumber numberWithBool:YES],
"KVO: notification for 'Test Suite Str' is prior");
break;
}
case 8: // [defs setObject:@"SetString"
// forKey:@"Test Suite Str"];
{
PASS_EQUAL(
old, [NSNull null],
"KVO: Second setting of 'Test Suite Str' has old = null");
PASS([new isKindOfClass:[ NSString class ]],
"KVO: New value for 'Test Suite Str' has NSString");
PASS([new isEqual:@"SetString"],
"KVO: new value for 'Test Suite Str' is 'SetString'");
PASS(kind == NSKeyValueChangeSetting,
"KVO: notification for 'Test Suite Str' is of kind "
"NSKeyValueChangeSetting");
break;
}
default: {
PASS(NO, "KVO: unexpected count for 'Test Suite Str'");
break;
}
}
}
kvoCount++;
}
@end
int
main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
Observer *obs = [[Observer new] autorelease];
NSUserDefaults *defs;
defs = [NSUserDefaults standardUserDefaults];
PASS(defs != nil && [defs isKindOfClass:[NSUserDefaults class]],
"NSUserDefaults understands +standardUserDefaults");
/* Reset the defaults */
[defs removeObjectForKey:@"Test Suite Bool"];
[defs removeObjectForKey:@"Test Suite Int"];
[defs removeObjectForKey:@"Test Suite Str"];
[[NSNotificationCenter defaultCenter]
addObserver:obs
selector:@selector(notified:)
name:NSUserDefaultsDidChangeNotification
object:nil];
[defs addObserver:obs
forKeyPath:@"Test Suite Bool"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
| NSKeyValueObservingOptionPrior
| NSKeyValueObservingOptionInitial
context:NULL];
[defs addObserver:obs
forKeyPath:@"Test Suite Int"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
| NSKeyValueObservingOptionPrior
| NSKeyValueObservingOptionInitial
context:NULL];
[defs addObserver:obs
forKeyPath:@"Test Suite Str"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
| NSKeyValueObservingOptionPrior
| NSKeyValueObservingOptionInitial
context:NULL];
PASS([obs kvoCount] == 3, "KVO: initial count is 3");
[defs setBool:YES forKey:@"Test Suite Bool"];
PASS([defs boolForKey:@"Test Suite Bool"],
"NSUserDefaults can set/get a BOOL");
PASS([[defs objectForKey:@"Test Suite Bool"] isKindOfClass:[NSNumber class]],
"NSUserDefaults returns NSNumber for a BOOL");
PASS([obs count] == 1, "setting a boolean causes notification");
PASS([obs kvoCount] == 5, "KVO: setting boolean caused 2 notifications");
[defs setInteger:34 forKey:@"Test Suite Int"];
PASS([defs integerForKey:@"Test Suite Int"] == 34,
"NSUserDefaults can set/get an int");
PASS([[defs objectForKey:@"Test Suite Int"] isKindOfClass:[NSNumber class]],
"NSUserDefaults returns NSNumber for an int");
PASS([obs count] == 2, "setting an integer causes notification");
PASS([obs kvoCount] == 7, "KVO: setting integer caused 2 notifications");
[defs setObject:@"SetString" forKey:@"Test Suite Str"];
PASS([[defs stringForKey:@"Test Suite Str"] isEqual:@"SetString"],
"NSUserDefaults can set/get a string");
PASS([[defs objectForKey:@"Test Suite Str"] isKindOfClass:[NSString class]],
"NSUserDefaults returns NSString for a string");
PASS([obs count] == 3, "setting a string causes notification");
PASS([obs kvoCount] == 9, "KVO: setting integer caused 2 notifications");
[defs removeObjectForKey:@"Test Suite Bool"];
PASS(nil == [defs objectForKey:@"Test Suite Bool"],
"NSUserDefaults can use -removeObjectForKey: to remove a bool");
PASS([obs count] == 4, "removing a key causes notification");
PASS([obs kvoCount] == 11, "KVO: removing bool caused 2 notifications");
[defs setObject:nil forKey:@"Test Suite Int"];
PASS(nil == [defs objectForKey:@"Test Suite Int"],
"NSUserDefaults can use -setObject:forKey: to remove an int");
PASS([obs count] == 5, "setting nil object causes notification");
PASS([obs kvoCount] == 13, "KVO: removing int caused 2 notifications");
[defs setObject:@"SetString" forKey:@"Test Suite Str"];
PASS([[defs objectForKey:@"Test Suite Str"] isKindOfClass:[NSString class]],
"NSUserDefaults returns NSString for an updated string");
PASS([obs count] == 6, "setting a string causes notification");
[defs setObject:nil forKey:@"Test Suite Int"];
PASS([obs count] == 7, "setting nil object twice causes notification");
[defs removeObserver:obs forKeyPath:@"Test Suite Bool" context:NULL];
[defs removeObserver:obs forKeyPath:@"Test Suite Int" context:NULL];
[defs removeObserver:obs forKeyPath:@"Test Suite Str" context:NULL];
[arp release];
arp = nil;
return 0;
}
|