diff options
Diffstat (limited to 'Example/Core/Tests/FIRAppTest.m')
-rw-r--r-- | Example/Core/Tests/FIRAppTest.m | 104 |
1 files changed, 72 insertions, 32 deletions
diff --git a/Example/Core/Tests/FIRAppTest.m b/Example/Core/Tests/FIRAppTest.m index 549c1ab..656f046 100644 --- a/Example/Core/Tests/FIRAppTest.m +++ b/Example/Core/Tests/FIRAppTest.m @@ -52,8 +52,9 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; @property(nonatomic) id appClassMock; @property(nonatomic) id optionsInstanceMock; -@property(nonatomic) id notificationCenterMock; +@property(nonatomic) id observerMock; @property(nonatomic) FIRApp *app; +@property(nonatomic) NSNotificationCenter *notificationCenter; @end @@ -65,13 +66,19 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; [FIRApp resetApps]; _appClassMock = OCMClassMock([FIRApp class]); _optionsInstanceMock = OCMPartialMock([FIROptions defaultOptions]); - _notificationCenterMock = OCMPartialMock([NSNotificationCenter defaultCenter]); + _observerMock = OCMObserverMock(); + + // TODO: Remove all usages of defaultCenter in Core, then we can instantiate an instance here to + // inject instead of using defaultCenter. + _notificationCenter = [NSNotificationCenter defaultCenter]; } - (void)tearDown { [_appClassMock stopMocking]; [_optionsInstanceMock stopMocking]; - [_notificationCenterMock stopMocking]; + [_notificationCenter removeObserver:_observerMock]; + _observerMock = nil; + _notificationCenter = nil; [super tearDown]; } @@ -79,11 +86,12 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; - (void)testConfigure { NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName isDefaultApp:YES]; - OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification - object:[FIRApp class] - userInfo:expectedUserInfo]); + [self expectNotificationForObserver:self.observerMock + notificationName:kFIRAppReadyToConfigureSDKNotification + object:[FIRApp class] + userInfo:expectedUserInfo]; XCTAssertNoThrow([FIRApp configure]); - OCMVerifyAll(self.notificationCenterMock); + OCMVerifyAll(self.observerMock); self.app = [FIRApp defaultApp]; XCTAssertNotNil(self.app); @@ -108,12 +116,13 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName isDefaultApp:YES]; - OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification - object:[FIRApp class] - userInfo:expectedUserInfo]); + [self expectNotificationForObserver:self.observerMock + notificationName:kFIRAppReadyToConfigureSDKNotification + object:[FIRApp class] + userInfo:expectedUserInfo]; // default options XCTAssertNoThrow([FIRApp configureWithOptions:[FIROptions defaultOptions]]); - OCMVerifyAll(self.notificationCenterMock); + OCMVerifyAll(self.observerMock); self.app = [FIRApp defaultApp]; XCTAssertNotNil(self.app); @@ -130,12 +139,13 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; options.APIKey = kCustomizedAPIKey; NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName isDefaultApp:YES]; - OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification - object:[FIRApp class] - userInfo:expectedUserInfo]); + [self expectNotificationForObserver:self.observerMock + notificationName:kFIRAppReadyToConfigureSDKNotification + object:[FIRApp class] + userInfo:expectedUserInfo]; XCTAssertNoThrow([FIRApp configureWithOptions:options]); - OCMVerifyAll(self.notificationCenterMock); + OCMVerifyAll(self.observerMock); self.app = [FIRApp defaultApp]; XCTAssertNotNil(self.app); @@ -158,11 +168,12 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRTestAppName1 isDefaultApp:NO]; - OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification - object:[FIRApp class] - userInfo:expectedUserInfo]); + [self expectNotificationForObserver:self.observerMock + notificationName:kFIRAppReadyToConfigureSDKNotification + object:[FIRApp class] + userInfo:expectedUserInfo]; XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]]); - OCMVerifyAll(self.notificationCenterMock); + OCMVerifyAll(self.observerMock); XCTAssertTrue([FIRApp allApps].count == 1); self.app = [FIRApp appNamed:kFIRTestAppName1]; @@ -179,11 +190,16 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; FIROptions *newOptions = [options copy]; newOptions.deepLinkURLScheme = kDeepLinkURLScheme; + // Set up notification center observer for verifying notifications. + [self.notificationCenter addMockObserver:self.observerMock + name:kFIRAppReadyToConfigureSDKNotification + object:[FIRApp class]]; + NSDictionary *expectedUserInfo1 = [self expectedUserInfoWithAppName:kFIRTestAppName1 isDefaultApp:NO]; - OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification - object:[FIRApp class] - userInfo:expectedUserInfo1]); + [[self.observerMock expect] notificationWithName:kFIRAppReadyToConfigureSDKNotification + object:[FIRApp class] + userInfo:expectedUserInfo1]; XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:newOptions]); XCTAssertTrue([FIRApp allApps].count == 1); self.app = [FIRApp appNamed:kFIRTestAppName1]; @@ -196,11 +212,13 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; NSDictionary *expectedUserInfo2 = [self expectedUserInfoWithAppName:kFIRTestAppName2 isDefaultApp:NO]; - OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification - object:[FIRApp class] - userInfo:expectedUserInfo2]); + [[self.observerMock expect] notificationWithName:kFIRAppReadyToConfigureSDKNotification + object:[FIRApp class] + userInfo:expectedUserInfo2]; + + [self.observerMock setExpectationOrderMatters:YES]; XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName2 options:customizedOptions]); - OCMVerifyAll(self.notificationCenterMock); + OCMVerifyAll(self.observerMock); XCTAssertTrue([FIRApp allApps].count == 2); self.app = [FIRApp appNamed:kFIRTestAppName2]; @@ -241,12 +259,15 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; [FIRApp configure]; self.app = [FIRApp defaultApp]; XCTAssertTrue([FIRApp allApps].count == 1); + [self expectNotificationForObserver:self.observerMock + notificationName:kFIRAppDeleteNotification + object:[FIRApp class] + userInfo:[OCMArg any]]; [self.app deleteApp:^(BOOL success) { XCTAssertTrue(success); }]; - OCMVerify([self.notificationCenterMock postNotificationName:kFIRAppDeleteNotification - object:[FIRApp class] - userInfo:[OCMArg any]]); + + OCMVerifyAll(self.observerMock); XCTAssertTrue(self.app.alreadySentDeleteNotification); XCTAssertTrue([FIRApp allApps].count == 0); } @@ -671,16 +692,27 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; - (void)testGlobalDataCollectionNoDiagnosticsSent { [FIRApp configure]; + // Add an observer for the diagnostics notification - both with and without an object to ensure it + // catches it either way. Currently no object is sent, but in the future that could change. + [self.notificationCenter addMockObserver:self.observerMock + name:kFIRAppDiagnosticsNotification + object:nil]; + [self.notificationCenter addMockObserver:self.observerMock + name:kFIRAppDiagnosticsNotification + object:OCMOCK_ANY]; + // Stub out reading from user defaults since stubbing out the BOOL has issues. If the data // collection switch is disabled, the `sendLogs` call should return immediately and not fire a // notification. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY]) .andReturn(@NO); - OCMReject([self.notificationCenterMock postNotificationName:kFIRAppDiagnosticsNotification - object:OCMOCK_ANY - userInfo:OCMOCK_ANY]); + NSError *error = [NSError errorWithDomain:@"com.firebase" code:42 userInfo:nil]; [[FIRApp defaultApp] sendLogsWithServiceName:@"Service" version:@"Version" error:error]; + + // The observer mock is strict and will raise an exception when an unexpected notification is + // received. + OCMVerifyAll(self.observerMock); } #pragma mark - Analytics Flag Tests @@ -767,6 +799,14 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; #pragma mark - private +- (void)expectNotificationForObserver:(id)observer + notificationName:(NSNotificationName)name + object:(nullable id)object + userInfo:(nullable NSDictionary *)userInfo { + [self.notificationCenter addMockObserver:observer name:name object:object]; + [[observer expect] notificationWithName:name object:object userInfo:userInfo]; +} + - (NSDictionary<NSString *, NSObject *> *)expectedUserInfoWithAppName:(NSString *)name isDefaultApp:(BOOL)isDefaultApp { return @{ |