Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions Crashlytics/UnitTests/FIRCLSSettingsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,33 @@ - (BOOL)writeSettings:(const NSString *)settings
- (void)cacheSettingsWithGoogleAppID:(NSString *)googleAppID
currentTimestamp:(NSTimeInterval)currentTimestamp
expectedRemoveCount:(NSInteger)expectedRemoveCount {
self.fileManager.removeExpectation = [[XCTestExpectation alloc]
initWithDescription:@"FIRCLSMockFileManager.removeExpectation.cache"];
self.fileManager.removeCount = 0;
self.fileManager.expectedRemoveCount = expectedRemoveCount;

XCTestExpectation *expectation =
[self expectationForNotification:FIRCLSMockFileManagerDidRemoveItemNotification
object:nil
handler:nil];
expectation.expectedFulfillmentCount = expectedRemoveCount;

[self.settings cacheSettingsWithGoogleAppID:googleAppID currentTimestamp:currentTimestamp];

[self waitForExpectations:@[ self.fileManager.removeExpectation ] timeout:1];
[self waitForExpectations:@[ expectation ] timeout:5.0];
}

- (void)reloadFromCacheWithGoogleAppID:(NSString *)googleAppID
currentTimestamp:(NSTimeInterval)currentTimestamp
expectedRemoveCount:(NSInteger)expectedRemoveCount {
self.fileManager.removeExpectation = [[XCTestExpectation alloc]
initWithDescription:@"FIRCLSMockFileManager.removeExpectation.reload"];
self.fileManager.removeCount = 0;
self.fileManager.expectedRemoveCount = expectedRemoveCount;

XCTestExpectation *expectation =
[self expectationForNotification:FIRCLSMockFileManagerDidRemoveItemNotification
object:nil
handler:nil];
expectation.expectedFulfillmentCount = expectedRemoveCount;

[self.settings reloadFromCacheWithGoogleAppID:googleAppID currentTimestamp:currentTimestamp];

[self waitForExpectations:@[ self.fileManager.removeExpectation ] timeout:5.0];
[self waitForExpectations:@[ expectation ] timeout:10.0];
}

- (void)testActivatedSettingsCached {
Expand Down Expand Up @@ -205,10 +211,6 @@ - (void)testCacheExpiredFromTTL {
[self writeSettings:FIRCLSTestSettingsActivated error:&error];
XCTAssertNil(error, "%@", error);

// 1 delete for clearing the cache key, plus 2 for the deletes from reloading and clearing the
// cache and cache key
self.fileManager.expectedRemoveCount = 3;

NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
[self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];

Expand Down Expand Up @@ -238,10 +240,6 @@ - (void)testCacheExpiredFromBuildInstanceID {
[self writeSettings:FIRCLSTestSettingsActivated error:&error];
XCTAssertNil(error, "%@", error);

// 1 delete for clearing the cache key, plus 2 for the deletes from reloading and clearing the
// cache and cache key
self.fileManager.expectedRemoveCount = 3;

NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
[self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];

Expand Down Expand Up @@ -272,10 +270,6 @@ - (void)testCacheExpiredFromAppVersion {
[self writeSettings:FIRCLSTestSettingsActivated error:&error];
XCTAssertNil(error, "%@", error);

// 1 delete for clearing the cache key, plus 2 for the deletes from reloading and clearing the
// cache and cache key
self.fileManager.expectedRemoveCount = 3;

NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
[self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];

Expand Down Expand Up @@ -387,7 +381,7 @@ - (void)testCorruptCache {
// Cache them, and reload. Since it's corrupted we should delete it all
[self cacheSettingsWithGoogleAppID:TestGoogleAppID
currentTimestamp:currentTimestamp
expectedRemoveCount:2];
expectedRemoveCount:3];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we changed the expected count here without change any actual code logic?


// Should have default values because we deleted the cache and settingsDictionary
XCTAssertEqual(self.settings.isCacheExpired, YES);
Expand Down
12 changes: 3 additions & 9 deletions Crashlytics/UnitTests/Mocks/FIRCLSMockFileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@

#import <XCTest/XCTest.h>

@interface FIRCLSMockFileManager : FIRCLSFileManager
// Notification posted when an item is removed via `removeItemAtPath`.
extern NSNotificationName const FIRCLSMockFileManagerDidRemoveItemNotification;

// Number of calls to removeItemAtPath are expected for the unit test
@property(nonatomic) NSInteger expectedRemoveCount;
@interface FIRCLSMockFileManager : FIRCLSFileManager

// Incremented when a remove happens with removeItemAtPath
@property(nonatomic) NSInteger removeCount;

// Will be fulfilled when the expected number of removes have happened
// using removeItemAtPath
//
// Users should initialize this in their test.
@property(nonatomic, strong) XCTestExpectation *removeExpectation;

@end
11 changes: 6 additions & 5 deletions Crashlytics/UnitTests/Mocks/FIRCLSMockFileManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#import "Crashlytics/UnitTests/Mocks/FIRCLSMockFileManager.h"

NSNotificationName const FIRCLSMockFileManagerDidRemoveItemNotification =
@"FIRCLSMockFileManagerDidRemoveItemNotification";

@interface FIRCLSMockFileManager ()

@property(nonatomic) NSMutableDictionary<NSString *, NSData *> *fileSystemDict;
Expand All @@ -38,11 +41,9 @@ - (BOOL)removeItemAtPath:(NSString *)path {

self.removeCount += 1;

// If we set up the expectation, and we went over the expected count or removes, fulfill the
// expectation
if (self.removeExpectation && self.removeCount >= self.expectedRemoveCount) {
[self.removeExpectation fulfill];
}
[[NSNotificationCenter defaultCenter]
postNotificationName:FIRCLSMockFileManagerDidRemoveItemNotification
object:self];

return YES;
}
Expand Down
Loading