Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap diagnostics notification in collection flag check. #1979

Merged
merged 3 commits into from Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 25 additions & 2 deletions Example/Core/Tests/FIRAppTest.m
Expand Up @@ -690,8 +690,6 @@ - (void)testGlobalDataCollectionClearedAfterDelete {
}

- (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
Expand All @@ -707,6 +705,9 @@ - (void)testGlobalDataCollectionNoDiagnosticsSent {
OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
.andReturn(@NO);

// Ensure configure doesn't fire a notification.
[FIRApp configure];

NSError *error = [NSError errorWithDomain:@"com.firebase" code:42 userInfo:nil];
[[FIRApp defaultApp] sendLogsWithServiceName:@"Service" version:@"Version" error:error];

Expand All @@ -715,6 +716,28 @@ - (void)testGlobalDataCollectionNoDiagnosticsSent {
OCMVerifyAll(self.observerMock);
}

- (void)testGlobalDataCollectionNoDiagnosticsSentForNoOptions {
// 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];
Copy link
Member

Choose a reason for hiding this comment

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

Does OCMOCK_ANY include nil?

Copy link
Member Author

Choose a reason for hiding this comment

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

Will test to see if that's the case!

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call, it does include nil. I'll make sure we don't duplicate this elsewhere in a different PR (grabbed this from a different location).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.


OCMStub([self.appClassMock isDataCollectionDefaultEnabled]).andReturn(@NO);

// Ensure there is no FIROptions instance, and call configure which should throw an exception and
// attempt to log a diagnostics notification.
self.optionsInstanceMock = nil;
XCTAssertThrows([FIRApp configure]);

// The observer mock is strict and will raise an exception when an unexpected notification is
// received.
OCMVerifyAll(self.observerMock);
}

#pragma mark - Analytics Flag Tests

- (void)testAnalyticsSetByGlobalDataCollectionSwitch {
Expand Down
38 changes: 23 additions & 15 deletions Firebase/Core/FIRApp.m
Expand Up @@ -107,13 +107,19 @@ @implementation FIRApp
+ (void)configure {
FIROptions *options = [FIROptions defaultOptions];
if (!options) {
[[NSNotificationCenter defaultCenter]
postNotificationName:kFIRAppDiagnosticsNotification
object:nil
userInfo:@{
kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore),
kFIRAppDiagnosticsErrorKey : [FIRApp errorForMissingOptions]
}];
// Read the Info.plist to see if the flag is set. At this point we can't check any user defaults
// since the app isn't configured at all, so only rely on the Info.plist value.
NSNumber *collectionEnabledPlistValue = [[self class] readDataCollectionSwitchFromPlist];
if (!collectionEnabledPlistValue || [collectionEnabledPlistValue boolValue]) {
[[NSNotificationCenter defaultCenter]
postNotificationName:kFIRAppDiagnosticsNotification
object:nil
userInfo:@{
kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore),
kFIRAppDiagnosticsErrorKey : [FIRApp errorForMissingOptions]
}];
}

[NSException raise:kFirebaseCoreErrorDomain
format:
@"`[FIRApp configure];` (`FirebaseApp.configure()` in Swift) could not find "
Expand Down Expand Up @@ -274,13 +280,15 @@ + (void)addAppToAppDictionary:(FIRApp *)app {
}
if ([app configureCore]) {
sAllApps[app.name] = app;
[[NSNotificationCenter defaultCenter]
postNotificationName:kFIRAppDiagnosticsNotification
object:nil
userInfo:@{
kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore),
kFIRAppDiagnosticsFIRAppKey : app
}];
if ([app isDataCollectionDefaultEnabled]) {
Copy link
Member

Choose a reason for hiding this comment

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

It seems like this should only be done in configureCore - but that could be a separate refactor?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll try to refactor it now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

[[NSNotificationCenter defaultCenter]
postNotificationName:kFIRAppDiagnosticsNotification
object:nil
userInfo:@{
kFIRAppDiagnosticsConfigurationTypeKey : @(FIRConfigTypeCore),
kFIRAppDiagnosticsFIRAppKey : app
}];
}
} else {
[NSException raise:kFirebaseCoreErrorDomain
format:
Expand Down Expand Up @@ -317,7 +325,7 @@ - (void)getTokenForcingRefresh:(BOOL)forceRefresh withCallback:(FIRTokenCallback
- (BOOL)configureCore {
[self checkExpectedBundleID];
if (![self isAppIDValid]) {
if (_options.usingOptionsFromDefaultPlist) {
if (_options.usingOptionsFromDefaultPlist && [self isDataCollectionDefaultEnabled]) {
[[NSNotificationCenter defaultCenter]
postNotificationName:kFIRAppDiagnosticsNotification
object:nil
Expand Down