From 08f447cd3fffae80748239daada493fcc19c111f Mon Sep 17 00:00:00 2001 From: Ryan Wilson Date: Mon, 14 May 2018 10:02:25 -0700 Subject: Add global data collection switch. (#1219) * Addition of global data collection switch. * Added Messaging conformance to data switch. Also formatted code. * Move data collection flag internal until all SDKs conform to it. * Formatting in response to code review. --- Example/Core/Tests/FIRAppTest.m | 130 +++++++++++++++++++++++++++++ Example/Messaging/Tests/FIRMessagingTest.m | 40 +++++++++ 2 files changed, 170 insertions(+) (limited to 'Example') diff --git a/Example/Core/Tests/FIRAppTest.m b/Example/Core/Tests/FIRAppTest.m index 6825e6a..abf1d38 100644 --- a/Example/Core/Tests/FIRAppTest.m +++ b/Example/Core/Tests/FIRAppTest.m @@ -42,6 +42,9 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; + (BOOL)validateAppIDFormat:(NSString *)appID withVersion:(NSString *)version; + (BOOL)validateAppIDFingerprint:(NSString *)appID withVersion:(NSString *)version; ++ (nullable NSNumber *)readDataCollectionSwitchFromPlist; ++ (nullable NSNumber *)readDataCollectionSwitchFromUserDefaultsForApp:(FIRApp *)app; + @end @interface FIRAppTest : FIRTestCase @@ -552,6 +555,133 @@ NSString *const kFIRTestAppName2 = @"test-app-name-2"; [FIRApp validateAppIDFingerprint:@"1:1337:ios:deadbeef:ab" withVersion:kGoodVersionV1]); } +#pragma mark - Automatic Data Collection Tests + +- (void)testGlobalDataCollectionNoFlags { + // Test: No flags set. + [FIRApp configure]; + OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil); + OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY]) + .andReturn(nil); + + XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled); +} + +- (void)testGlobalDataCollectionPlistSetEnabled { + // Test: Plist set to enabled, no override. + [FIRApp configure]; + OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES); + OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY]) + .andReturn(nil); + + XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled); +} + +- (void)testGlobalDataCollectionPlistSetDisabled { + // Test: Plist set to disabled, no override. + [FIRApp configure]; + OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO); + OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY]) + .andReturn(nil); + + XCTAssertFalse([FIRApp defaultApp].isAutomaticDataCollectionEnabled); +} + +- (void)testGlobalDataCollectionUserSpecifiedEnabled { + // Test: User specified as enabled, no plist value. + [FIRApp configure]; + OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil); + OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY]) + .andReturn(@YES); + + XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled); +} + +- (void)testGlobalDataCollectionUserSpecifiedDisabled { + // Test: User specified as disabled, no plist value. + [FIRApp configure]; + OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil); + OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY]) + .andReturn(@NO); + + XCTAssertFalse([FIRApp defaultApp].isAutomaticDataCollectionEnabled); +} + +- (void)testGlobalDataCollectionUserOverriddenEnabled { + // Test: User specified as enabled, with plist set as disabled. + [FIRApp configure]; + OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO); + OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY]) + .andReturn(@YES); + + XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled); +} + +- (void)testGlobalDataCollectionUserOverriddenDisabled { + // Test: User specified as disabled, with plist set as enabled. + [FIRApp configure]; + OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES); + OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY]) + .andReturn(@NO); + + XCTAssertFalse([FIRApp defaultApp].isAutomaticDataCollectionEnabled); +} + +- (void)testGlobalDataCollectionWriteToDefaults { + id defaultsMock = OCMPartialMock([NSUserDefaults standardUserDefaults]); + [FIRApp configure]; + + FIRApp *app = [FIRApp defaultApp]; + app.automaticDataCollectionEnabled = YES; + NSString *key = + [NSString stringWithFormat:kFIRGlobalAppDataCollectionEnabledDefaultsKeyFormat, app.name]; + OCMVerify([defaultsMock setObject:@YES forKey:key]); + + [FIRApp defaultApp].automaticDataCollectionEnabled = NO; + OCMVerify([defaultsMock setObject:@NO forKey:key]); + + [defaultsMock stopMocking]; +} + +- (void)testGlobalDataCollectionClearedAfterDelete { + // Configure and disable data collection for the default FIRApp. + [FIRApp configure]; + FIRApp *app = [FIRApp defaultApp]; + app.automaticDataCollectionEnabled = NO; + XCTAssertFalse(app.isAutomaticDataCollectionEnabled); + + // Delete the app, and verify that the switch was reset. + XCTestExpectation *deleteFinished = + [self expectationWithDescription:@"The app should successfully delete."]; + [app deleteApp:^(BOOL success) { + if (success) { + [deleteFinished fulfill]; + } + }]; + + // Wait for the delete to complete. + [self waitForExpectations:@[ deleteFinished ] timeout:1]; + + // Set up the default app again, and check the data collection flag. + [FIRApp configure]; + XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled); +} + +- (void)testGlobalDataCollectionNoDiagnosticsSent { + [FIRApp configure]; + + // 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]; +} + #pragma mark - Internal Methods - (void)testAuthGetUID { diff --git a/Example/Messaging/Tests/FIRMessagingTest.m b/Example/Messaging/Tests/FIRMessagingTest.m index adc830d..61ff136 100644 --- a/Example/Messaging/Tests/FIRMessagingTest.m +++ b/Example/Messaging/Tests/FIRMessagingTest.m @@ -75,6 +75,46 @@ extern NSString *const kFIRMessagingFCMTokenFetchAPNSOption; XCTAssertTrue(_messaging.isAutoInitEnabled); } +- (void)testAutoInitEnableFlagOverrideGlobalTrue { + OCMStub([self.mockMessaging isGlobalAutomaticDataCollectionEnabled]).andReturn(YES); + id bundleMock = OCMPartialMock([NSBundle mainBundle]); + OCMStub([bundleMock objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled]).andReturn(nil); + XCTAssertTrue(self.messaging.isAutoInitEnabled); + + self.messaging.autoInitEnabled = NO; + XCTAssertFalse(self.messaging.isAutoInitEnabled); + [bundleMock stopMocking]; +} + +- (void)testAutoInitEnableFlagOverrideGlobalFalse { + OCMStub([self.mockMessaging isGlobalAutomaticDataCollectionEnabled]).andReturn(YES); + id bundleMock = OCMPartialMock([NSBundle mainBundle]); + OCMStub([bundleMock objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled]).andReturn(nil); + XCTAssertTrue(self.messaging.isAutoInitEnabled); + + self.messaging.autoInitEnabled = NO; + XCTAssertFalse(self.messaging.isAutoInitEnabled); + [bundleMock stopMocking]; +} + +- (void)testAutoInitEnableGlobalDefaultTrue { + OCMStub([self.mockMessaging isGlobalAutomaticDataCollectionEnabled]).andReturn(YES); + id bundleMock = OCMPartialMock([NSBundle mainBundle]); + OCMStub([bundleMock objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled]).andReturn(nil); + + XCTAssertTrue(self.messaging.isAutoInitEnabled); + [bundleMock stopMocking]; +} + +- (void)testAutoInitEnableGlobalDefaultFalse { + OCMStub([self.mockMessaging isGlobalAutomaticDataCollectionEnabled]).andReturn(NO); + id bundleMock = OCMPartialMock([NSBundle mainBundle]); + OCMStub([bundleMock objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled]).andReturn(nil); + + XCTAssertFalse(self.messaging.isAutoInitEnabled); + [bundleMock stopMocking]; +} + #pragma mark - Direct Channel Establishment Testing // Should connect with valid token and application in foreground -- cgit v1.2.3