Skip to content

Commit 3cd9695

Browse files
authored
GULUserDefaults clear for all archs (#3544)
1 parent b3bebbd commit 3cd9695

File tree

2 files changed

+4
-79
lines changed

2 files changed

+4
-79
lines changed

GoogleUtilities/Example/Tests/UserDefaults/GULUserDefaultsTests.m

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
/// The maximum time to wait for an expectation before failing.
2424
static const NSTimeInterval kGULTestCaseTimeoutInterval = 10;
2525

26-
@interface GULUserDefaults ()
27-
// Expose for testing.
28-
- (void)clearAllData;
29-
@end
30-
3126
@interface GULUserDefaultsThreadArgs : NSObject
3227

3328
/// The new user defaults to be tested on threads.
@@ -152,26 +147,6 @@ - (void)testNewUserDefaultsWithStandardUserDefaults {
152147
XCTAssertEqualObjects([newUserDefaults dictionaryForKey:key4], dictionary2);
153148
XCTAssertEqualObjects([userDefaults objectForKey:key4], dictionary2);
154149

155-
// Remove all of the objects from the new user defaults. The values from the NSUserDefaults must
156-
// also be cleared.
157-
[newUserDefaults clearAllData];
158-
XCTAssertNil([userDefaults objectForKey:key1]);
159-
XCTAssertNil([newUserDefaults objectForKey:key1]);
160-
XCTAssertNil([userDefaults objectForKey:key2]);
161-
XCTAssertNil([newUserDefaults objectForKey:key2]);
162-
XCTAssertNil([userDefaults objectForKey:key3]);
163-
XCTAssertNil([newUserDefaults objectForKey:key3]);
164-
XCTAssertNil([userDefaults objectForKey:key4]);
165-
XCTAssertNil([newUserDefaults objectForKey:key4]);
166-
XCTAssertNil([userDefaults objectForKey:key5]);
167-
XCTAssertNil([newUserDefaults objectForKey:key5]);
168-
XCTAssertNil([userDefaults objectForKey:key6]);
169-
XCTAssertNil([newUserDefaults objectForKey:key6]);
170-
XCTAssertNil([userDefaults objectForKey:key7]);
171-
XCTAssertNil([newUserDefaults objectForKey:key7]);
172-
XCTAssertNil([userDefaults objectForKey:key8]);
173-
XCTAssertNil([newUserDefaults objectForKey:key8]);
174-
175150
[self removePreferenceFileWithSuiteName:suiteName];
176151
}
177152

@@ -385,24 +360,6 @@ - (void)testNewSharedUserDefaultsWithStandardUserDefaults {
385360
XCTAssertEqualObjects([newUserDefaults objectForKey:key4], dictionary2);
386361
XCTAssertEqualObjects([userDefaults objectForKey:key4], dictionary2);
387362

388-
// Remove all of the objects from the new user defaults. The values from the NSUserDefaults must
389-
// also be cleared.
390-
[newUserDefaults clearAllData];
391-
XCTAssertNil([userDefaults objectForKey:key1]);
392-
XCTAssertNil([newUserDefaults objectForKey:key1]);
393-
XCTAssertNil([userDefaults objectForKey:key2]);
394-
XCTAssertNil([newUserDefaults objectForKey:key2]);
395-
XCTAssertNil([userDefaults objectForKey:key3]);
396-
XCTAssertNil([newUserDefaults objectForKey:key3]);
397-
XCTAssertNil([userDefaults objectForKey:key4]);
398-
XCTAssertNil([newUserDefaults objectForKey:key4]);
399-
XCTAssertNil([userDefaults objectForKey:key5]);
400-
XCTAssertNil([newUserDefaults objectForKey:key5]);
401-
XCTAssertNil([userDefaults objectForKey:key6]);
402-
XCTAssertNil([newUserDefaults objectForKey:key6]);
403-
XCTAssertNil([userDefaults objectForKey:key7]);
404-
XCTAssertNil([newUserDefaults objectForKey:key7]);
405-
406363
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
407364
}
408365

@@ -424,15 +381,14 @@ - (void)testUserDefaultNotifications {
424381
[newUserDefaults setObject:nil forKey:@"test-another"];
425382
XCTAssertNil([newUserDefaults objectForKey:@"test-another"]);
426383
[newUserDefaults synchronize];
427-
[newUserDefaults clearAllData];
428384
[[NSNotificationCenter defaultCenter] removeObserver:observer];
429385

430386
// Remove the underlying reference file.
431387
[self removePreferenceFileWithSuiteName:suiteName];
432388
}
433389

434390
- (void)testSynchronizeToDisk {
435-
#if TARGET_OS_OSX
391+
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
436392
// `NSFileManager` has trouble reading the files in `~/Library` even though the
437393
// `removeItemAtPath:` call works. Watching Finder while stepping through this test shows that the
438394
// file does get created and removed properly. When using LLDB to call `fileExistsAtPath:` the
@@ -458,7 +414,6 @@ - (void)testSynchronizeToDisk {
458414

459415
// Now get the file directly from disk.
460416
XCTAssertTrue([fileManager fileExistsAtPath:filePath]);
461-
[newUserDefaults clearAllData];
462417
[newUserDefaults synchronize];
463418

464419
[self removePreferenceFileWithSuiteName:suiteName];
@@ -500,7 +455,6 @@ - (void)testSetNilObject {
500455

501456
[newUserDefaults setObject:nil forKey:@"fine"];
502457
XCTAssertNil([newUserDefaults objectForKey:@"fine"]);
503-
[newUserDefaults clearAllData];
504458

505459
[self removePreferenceFileWithSuiteName:suiteName];
506460
}
@@ -513,7 +467,6 @@ - (void)testRemoveObject {
513467

514468
[newUserDefaults removeObjectForKey:@"fine"];
515469
XCTAssertNil([newUserDefaults objectForKey:@"fine"]);
516-
[newUserDefaults clearAllData];
517470

518471
[self removePreferenceFileWithSuiteName:suiteName];
519472
}
@@ -535,7 +488,6 @@ - (void)testNewUserDefaultsWithNSUserDefaultsFile {
535488
GULUserDefaults *newUserDefaults = [[GULUserDefaults alloc] initWithSuiteName:suiteName];
536489
XCTAssertEqualObjects([newUserDefaults objectForKey:@"key1"], @"value1");
537490
XCTAssertEqualObjects([newUserDefaults objectForKey:@"key2"], @"value2");
538-
[newUserDefaults clearAllData];
539491

540492
// Clean up.
541493
[self removePreferenceFileWithSuiteName:suiteName];
@@ -572,7 +524,6 @@ - (void)testNewUserDefaultsThreadSafeAddingObjects {
572524
XCTAssertEqualObjects([userDefaults objectForKey:key], @(i));
573525
}
574526

575-
[userDefaults clearAllData];
576527
[self removePreferenceFileWithSuiteName:suiteName];
577528
}
578529

@@ -616,7 +567,6 @@ - (void)testNewUserDefaultsRemovingObjects {
616567
XCTAssertNil([userDefaults objectForKey:key]);
617568
}
618569

619-
[userDefaults clearAllData];
620570
[self removePreferenceFileWithSuiteName:suiteName];
621571
}
622572

@@ -661,8 +611,6 @@ - (void)testNewUserDefaultsRemovingSomeObjects {
661611
XCTAssertEqualObjects([userDefaults objectForKey:key], @(i));
662612
}
663613
}
664-
[userDefaults clearAllData];
665-
666614
[self removePreferenceFileWithSuiteName:suiteName];
667615
}
668616

@@ -702,8 +650,6 @@ - (void)testBothUserDefaultsThreadSafeAddingObjects {
702650
XCTAssertEqualObjects([userDefaults objectForKey:key], @(i));
703651
}
704652
}
705-
706-
[newUserDefaults clearAllData];
707653
[self removePreferenceFileWithSuiteName:suiteName];
708654
}
709655

@@ -760,8 +706,6 @@ - (void)testBothUserDefaultsRemovingSomeObjects {
760706
}
761707
}
762708
}
763-
[userDefaults clearAllData];
764-
765709
[self removePreferenceFileWithSuiteName:suiteName];
766710
}
767711

@@ -835,6 +779,9 @@ - (NSString *)filePathForPreferencesName:(NSString *)preferencesName {
835779
}
836780

837781
- (void)removePreferenceFileWithSuiteName:(NSString *)suiteName {
782+
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:suiteName];
783+
[userDefaults removePersistentDomainForName:suiteName];
784+
838785
NSString *path = [self filePathForPreferencesName:suiteName];
839786
NSFileManager *fileManager = [NSFileManager defaultManager];
840787
if ([fileManager fileExistsAtPath:path]) {

GoogleUtilities/UserDefaults/GULUserDefaults.m

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -195,28 +195,6 @@ - (void)synchronize {
195195

196196
#pragma mark - Private methods
197197

198-
/// Removes all values from the search list entry specified by 'domainName', the current user, and
199-
/// any host. The change is persistent. Equivalent to -removePersistentDomainForName: of
200-
/// NSUserDefaults.
201-
- (void)clearAllData {
202-
// On macOS, using `kCFPreferencesCurrentHost` will not set all the keys necessary to match
203-
// `NSUserDefaults`.
204-
#if TARGET_OS_OSX
205-
CFStringRef host = kCFPreferencesAnyHost;
206-
#else
207-
CFStringRef host = kCFPreferencesCurrentHost;
208-
#endif // TARGET_OS_OSX
209-
210-
CFArrayRef keyList = CFPreferencesCopyKeyList(_appNameRef, kCFPreferencesCurrentUser, host);
211-
if (!keyList) {
212-
return;
213-
}
214-
215-
CFPreferencesSetMultiple(NULL, keyList, _appNameRef, kCFPreferencesCurrentUser, host);
216-
CFRelease(keyList);
217-
[self scheduleSynchronize];
218-
}
219-
220198
- (void)scheduleSynchronize {
221199
// Synchronize data using a timer so that multiple set... calls can be coalesced under one
222200
// synchronize.

0 commit comments

Comments
 (0)