@@ -42,6 +42,9 @@ + (BOOL)validateAppID:(NSString *)appID;
4242+ (BOOL )validateAppIDFormat : (NSString *)appID withVersion : (NSString *)version ;
4343+ (BOOL )validateAppIDFingerprint : (NSString *)appID withVersion : (NSString *)version ;
4444
45+ + (nullable NSNumber *)readDataCollectionSwitchFromPlist ;
46+ + (nullable NSNumber *)readDataCollectionSwitchFromUserDefaultsForApp : (FIRApp *)app ;
47+
4548@end
4649
4750@interface FIRAppTest : FIRTestCase
@@ -552,6 +555,133 @@ - (void)testAppIDFingerprintInvalid {
552555 [FIRApp validateAppIDFingerprint: @" 1:1337:ios:deadbeef:ab" withVersion: kGoodVersionV1 ]);
553556}
554557
558+ #pragma mark - Automatic Data Collection Tests
559+
560+ - (void )testGlobalDataCollectionNoFlags {
561+ // Test: No flags set.
562+ [FIRApp configure ];
563+ OCMStub ([self .appClassMock readDataCollectionSwitchFromPlist ]).andReturn (nil );
564+ OCMStub ([self .appClassMock readDataCollectionSwitchFromUserDefaultsForApp: OCMOCK_ANY])
565+ .andReturn (nil );
566+
567+ XCTAssertTrue ([FIRApp defaultApp ].isAutomaticDataCollectionEnabled );
568+ }
569+
570+ - (void )testGlobalDataCollectionPlistSetEnabled {
571+ // Test: Plist set to enabled, no override.
572+ [FIRApp configure ];
573+ OCMStub ([self .appClassMock readDataCollectionSwitchFromPlist ]).andReturn (@YES );
574+ OCMStub ([self .appClassMock readDataCollectionSwitchFromUserDefaultsForApp: OCMOCK_ANY])
575+ .andReturn (nil );
576+
577+ XCTAssertTrue ([FIRApp defaultApp ].isAutomaticDataCollectionEnabled );
578+ }
579+
580+ - (void )testGlobalDataCollectionPlistSetDisabled {
581+ // Test: Plist set to disabled, no override.
582+ [FIRApp configure ];
583+ OCMStub ([self .appClassMock readDataCollectionSwitchFromPlist ]).andReturn (@NO );
584+ OCMStub ([self .appClassMock readDataCollectionSwitchFromUserDefaultsForApp: OCMOCK_ANY])
585+ .andReturn (nil );
586+
587+ XCTAssertFalse ([FIRApp defaultApp ].isAutomaticDataCollectionEnabled );
588+ }
589+
590+ - (void )testGlobalDataCollectionUserSpecifiedEnabled {
591+ // Test: User specified as enabled, no plist value.
592+ [FIRApp configure ];
593+ OCMStub ([self .appClassMock readDataCollectionSwitchFromPlist ]).andReturn (nil );
594+ OCMStub ([self .appClassMock readDataCollectionSwitchFromUserDefaultsForApp: OCMOCK_ANY])
595+ .andReturn (@YES );
596+
597+ XCTAssertTrue ([FIRApp defaultApp ].isAutomaticDataCollectionEnabled );
598+ }
599+
600+ - (void )testGlobalDataCollectionUserSpecifiedDisabled {
601+ // Test: User specified as disabled, no plist value.
602+ [FIRApp configure ];
603+ OCMStub ([self .appClassMock readDataCollectionSwitchFromPlist ]).andReturn (nil );
604+ OCMStub ([self .appClassMock readDataCollectionSwitchFromUserDefaultsForApp: OCMOCK_ANY])
605+ .andReturn (@NO );
606+
607+ XCTAssertFalse ([FIRApp defaultApp ].isAutomaticDataCollectionEnabled );
608+ }
609+
610+ - (void )testGlobalDataCollectionUserOverriddenEnabled {
611+ // Test: User specified as enabled, with plist set as disabled.
612+ [FIRApp configure ];
613+ OCMStub ([self .appClassMock readDataCollectionSwitchFromPlist ]).andReturn (@NO );
614+ OCMStub ([self .appClassMock readDataCollectionSwitchFromUserDefaultsForApp: OCMOCK_ANY])
615+ .andReturn (@YES );
616+
617+ XCTAssertTrue ([FIRApp defaultApp ].isAutomaticDataCollectionEnabled );
618+ }
619+
620+ - (void )testGlobalDataCollectionUserOverriddenDisabled {
621+ // Test: User specified as disabled, with plist set as enabled.
622+ [FIRApp configure ];
623+ OCMStub ([self .appClassMock readDataCollectionSwitchFromPlist ]).andReturn (@YES );
624+ OCMStub ([self .appClassMock readDataCollectionSwitchFromUserDefaultsForApp: OCMOCK_ANY])
625+ .andReturn (@NO );
626+
627+ XCTAssertFalse ([FIRApp defaultApp ].isAutomaticDataCollectionEnabled );
628+ }
629+
630+ - (void )testGlobalDataCollectionWriteToDefaults {
631+ id defaultsMock = OCMPartialMock ([NSUserDefaults standardUserDefaults ]);
632+ [FIRApp configure ];
633+
634+ FIRApp *app = [FIRApp defaultApp ];
635+ app.automaticDataCollectionEnabled = YES ;
636+ NSString *key =
637+ [NSString stringWithFormat: kFIRGlobalAppDataCollectionEnabledDefaultsKeyFormat , app.name];
638+ OCMVerify ([defaultsMock setObject: @YES forKey: key]);
639+
640+ [FIRApp defaultApp ].automaticDataCollectionEnabled = NO ;
641+ OCMVerify ([defaultsMock setObject: @NO forKey: key]);
642+
643+ [defaultsMock stopMocking ];
644+ }
645+
646+ - (void )testGlobalDataCollectionClearedAfterDelete {
647+ // Configure and disable data collection for the default FIRApp.
648+ [FIRApp configure ];
649+ FIRApp *app = [FIRApp defaultApp ];
650+ app.automaticDataCollectionEnabled = NO ;
651+ XCTAssertFalse (app.isAutomaticDataCollectionEnabled );
652+
653+ // Delete the app, and verify that the switch was reset.
654+ XCTestExpectation *deleteFinished =
655+ [self expectationWithDescription: @" The app should successfully delete." ];
656+ [app deleteApp: ^(BOOL success) {
657+ if (success) {
658+ [deleteFinished fulfill ];
659+ }
660+ }];
661+
662+ // Wait for the delete to complete.
663+ [self waitForExpectations: @[ deleteFinished ] timeout: 1 ];
664+
665+ // Set up the default app again, and check the data collection flag.
666+ [FIRApp configure ];
667+ XCTAssertTrue ([FIRApp defaultApp ].isAutomaticDataCollectionEnabled );
668+ }
669+
670+ - (void )testGlobalDataCollectionNoDiagnosticsSent {
671+ [FIRApp configure ];
672+
673+ // Stub out reading from user defaults since stubbing out the BOOL has issues. If the data
674+ // collection switch is disabled, the `sendLogs` call should return immediately and not fire a
675+ // notification.
676+ OCMStub ([self .appClassMock readDataCollectionSwitchFromUserDefaultsForApp: OCMOCK_ANY])
677+ .andReturn (@NO );
678+ OCMReject ([self .notificationCenterMock postNotificationName: kFIRAppDiagnosticsNotification
679+ object: OCMOCK_ANY
680+ userInfo: OCMOCK_ANY]);
681+ NSError *error = [NSError errorWithDomain: @" com.firebase" code: 42 userInfo: nil ];
682+ [[FIRApp defaultApp ] sendLogsWithServiceName: @" Service" version: @" Version" error: error];
683+ }
684+
555685#pragma mark - Internal Methods
556686
557687- (void )testAuthGetUID {
0 commit comments