Skip to content

Commit f2536cf

Browse files
FirebaseApp: send Core Diagnostics data on appDidBecomeActive (#3437)
* FIRApp: send CoreDiagnostics data on appDidBecomeActive. * ./scripts/style.sh * Renaming. * FIRAppTest: stub FIRCoreDiagnosticsConnector to avoid invoking FIRCoreDiagnostics methods in the tests. * Run ./scripts/style.sh * Rename to `logCoreTelemetry`
1 parent 50103de commit f2536cf

File tree

4 files changed

+106
-11
lines changed

4 files changed

+106
-11
lines changed

Example/Core/Tests/FIRAppTest.m

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
#import "FIRTestCase.h"
1616
#import "FIRTestComponents.h"
1717

18-
#import <FirebaseCoreDiagnosticsInterop/FIRCoreDiagnosticsData.h>
19-
#import <FirebaseCoreDiagnosticsInterop/FIRCoreDiagnosticsInterop.h>
20-
2118
#import <FirebaseCore/FIRAnalyticsConfiguration.h>
2219
#import <FirebaseCore/FIRAppInternal.h>
20+
#import <FirebaseCore/FIRCoreDiagnosticsConnector.h>
2321
#import <FirebaseCore/FIROptionsInternal.h>
2422

2523
NSString *const kFIRTestAppName1 = @"test_app_name_1";
@@ -53,6 +51,7 @@ @interface FIRAppTest : FIRTestCase
5351

5452
@property(nonatomic) id appClassMock;
5553
@property(nonatomic) id observerMock;
54+
@property(nonatomic) id mockCoreDiagnosticsConnector;
5655
@property(nonatomic) NSNotificationCenter *notificationCenter;
5756

5857
@end
@@ -65,6 +64,11 @@ - (void)setUp {
6564
[FIRApp resetApps];
6665
_appClassMock = OCMClassMock([FIRApp class]);
6766
_observerMock = OCMObserverMock();
67+
_mockCoreDiagnosticsConnector = OCMClassMock([FIRCoreDiagnosticsConnector class]);
68+
69+
OCMStub(ClassMethod([self.mockCoreDiagnosticsConnector logCoreTelemetryWithOptions:[OCMArg any]]))
70+
.andDo(^(NSInvocation *invocation){
71+
});
6872

6973
// TODO: Remove all usages of defaultCenter in Core, then we can instantiate an instance here to
7074
// inject instead of using defaultCenter.
@@ -76,6 +80,7 @@ - (void)tearDown {
7680
[_notificationCenter removeObserver:_observerMock];
7781
_observerMock = nil;
7882
_notificationCenter = nil;
83+
_mockCoreDiagnosticsConnector = nil;
7984

8085
[super tearDown];
8186
}
@@ -730,6 +735,24 @@ - (void)testRegisteringNonConformingLibrary {
730735
XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"InvalidLibrary`/1.0.0"]);
731736
}
732737

738+
#pragma mark - Core Diagnostics
739+
740+
- (void)testCoreDiagnosticsLoggedWhenFIRAppIsConfigured {
741+
[self expectCoreDiagnosticsDataLogWithOptions:[self appOptions]];
742+
[self createConfiguredAppWithName:NSStringFromSelector(_cmd)];
743+
OCMVerifyAll(self.mockCoreDiagnosticsConnector);
744+
}
745+
746+
- (void)testCoreDiagnosticsLoggedWhenAppDidBecomeActive {
747+
FIRApp *app = [self createConfiguredAppWithName:NSStringFromSelector(_cmd)];
748+
[self expectCoreDiagnosticsDataLogWithOptions:app.options];
749+
750+
[self.notificationCenter postNotificationName:[self appDidBecomeActiveNotificationName]
751+
object:nil];
752+
753+
OCMVerifyAll(self.mockCoreDiagnosticsConnector);
754+
}
755+
733756
#pragma mark - private
734757

735758
- (void)expectNotificationForObserver:(id)observer
@@ -749,4 +772,39 @@ - (void)expectNotificationForObserver:(id)observer
749772
};
750773
}
751774

775+
- (void)expectCoreDiagnosticsDataLogWithOptions:(nullable FIROptions *)expectedOptions {
776+
[self.mockCoreDiagnosticsConnector stopMocking];
777+
self.mockCoreDiagnosticsConnector = nil;
778+
self.mockCoreDiagnosticsConnector = OCMClassMock([FIRCoreDiagnosticsConnector class]);
779+
780+
OCMExpect(ClassMethod([self.mockCoreDiagnosticsConnector
781+
logCoreTelemetryWithOptions:[OCMArg checkWithBlock:^BOOL(FIROptions *options) {
782+
if (!expectedOptions) {
783+
return YES;
784+
}
785+
return [options.googleAppID isEqualToString:expectedOptions.googleAppID] &&
786+
[options.GCMSenderID isEqualToString:expectedOptions.GCMSenderID];
787+
}]]));
788+
}
789+
790+
- (NSNotificationName)appDidBecomeActiveNotificationName {
791+
#if TARGET_OS_IOS || TARGET_OS_TV
792+
return UIApplicationDidBecomeActiveNotification;
793+
#endif
794+
795+
#if TARGET_OS_OSX
796+
return NSApplicationDidBecomeActiveNotification;
797+
#endif
798+
}
799+
800+
- (FIRApp *)createConfiguredAppWithName:(NSString *)name {
801+
FIROptions *options = [self appOptions];
802+
[FIRApp configureWithName:name options:options];
803+
return [FIRApp appNamed:name];
804+
}
805+
806+
- (FIROptions *)appOptions {
807+
return [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
808+
}
809+
752810
@end

Firebase/Core/FIRApp.m

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@
1414

1515
#include <sys/utsname.h>
1616

17-
#import "FIRApp.h"
17+
#if __has_include(<UIKit/UIKit.h>)
18+
#import <UIKit/UIKit.h>
19+
#endif
20+
21+
#if __has_include(<AppKit/AppKit.h>)
22+
#import <AppKit/AppKit.h>
23+
#endif
1824

19-
#import <FirebaseCoreDiagnosticsInterop/FIRCoreDiagnosticsData.h>
25+
#import "FIRApp.h"
2026

2127
#import "Private/FIRAnalyticsConfiguration.h"
2228
#import "Private/FIRAppInternal.h"
@@ -283,15 +289,17 @@ - (instancetype)initInstanceWithName:(NSString *)name options:(FIROptions *)opti
283289
return self;
284290
}
285291

292+
- (void)dealloc {
293+
[[NSNotificationCenter defaultCenter] removeObserver:self];
294+
}
295+
286296
- (BOOL)configureCore {
287297
[self checkExpectedBundleID];
288298
if (![self isAppIDValid]) {
289299
return NO;
290300
}
291301

292-
if ([self isDataCollectionDefaultEnabled]) {
293-
[FIRCoreDiagnosticsConnector logConfigureCoreWithOptions:_options];
294-
}
302+
[self logCoreTelemetryIfEnabled];
295303

296304
#if TARGET_OS_IOS
297305
// Initialize the Analytics once there is a valid options under default app. Analytics should
@@ -316,6 +324,8 @@ - (BOOL)configureCore {
316324
}
317325
#endif
318326

327+
[self subscribeForAppDidBecomeActiveNotifications];
328+
319329
return YES;
320330
}
321331

@@ -796,4 +806,31 @@ - (void)sendLogsWithServiceName:(NSString *)serviceName
796806
}
797807
#pragma clang diagnostic pop
798808

809+
#pragma mark - App Life Cycle
810+
811+
- (void)subscribeForAppDidBecomeActiveNotifications {
812+
#if TARGET_OS_IOS || TARGET_OS_TV
813+
NSNotificationName notificationName = UIApplicationDidBecomeActiveNotification;
814+
#endif
815+
816+
#if TARGET_OS_OSX
817+
NSNotificationName notificationName = NSApplicationDidBecomeActiveNotification;
818+
#endif
819+
820+
[[NSNotificationCenter defaultCenter] addObserver:self
821+
selector:@selector(appDidBecomeActive:)
822+
name:notificationName
823+
object:nil];
824+
}
825+
826+
- (void)appDidBecomeActive:(NSNotification *)notification {
827+
[self logCoreTelemetryIfEnabled];
828+
}
829+
830+
- (void)logCoreTelemetryIfEnabled {
831+
if ([self isDataCollectionDefaultEnabled]) {
832+
[FIRCoreDiagnosticsConnector logCoreTelemetryWithOptions:_options];
833+
}
834+
}
835+
799836
@end

Firebase/Core/FIRCoreDiagnosticsConnector.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ + (void)initialize {
4343
}
4444
}
4545

46-
+ (void)logConfigureCoreWithOptions:(FIROptions *)options {
46+
+ (void)logCoreTelemetryWithOptions:(FIROptions *)options {
4747
if (FIRCoreDiagnosticsImplementation) {
4848
FIRDiagnosticsData *diagnosticsData = [[FIRDiagnosticsData alloc] init];
4949
[diagnosticsData insertValue:@(YES) forKey:kFIRCDIsDataCollectionDefaultEnabledKey];

Firebase/Core/Private/FIRCoreDiagnosticsConnector.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ NS_ASSUME_NONNULL_BEGIN
2424
/** Connects FIRCore with the CoreDiagnostics library. */
2525
@interface FIRCoreDiagnosticsConnector : NSObject
2626

27-
/** Logs data related to a -configureCore call.
27+
/** Logs FirebaseCore related data.
2828
*
2929
* @param options The options object containing data to log.
3030
*/
31-
+ (void)logConfigureCoreWithOptions:(FIROptions *)options;
31+
+ (void)logCoreTelemetryWithOptions:(FIROptions *)options;
3232

3333
@end
3434

0 commit comments

Comments
 (0)