@@ -53,10 +53,12 @@ + (nullable NSNumber *)readDataCollectionSwitchFromUserDefaultsForApp:(FIRApp *)
53
53
@interface FIRAppTest : FIRTestCase
54
54
55
55
@property (nonatomic ) id appClassMock;
56
- @property (nonatomic ) id observerMock;
57
56
@property (nonatomic ) id mockCoreDiagnosticsConnector;
58
57
@property (nonatomic ) NSNotificationCenter *notificationCenter;
59
58
59
+ // / If `YES` then throws when `logCoreTelemetryWithOptions:` method is called.
60
+ @property (nonatomic ) BOOL assertNoLogCoreTelemetry;
61
+
60
62
@end
61
63
62
64
@implementation FIRAppTest
@@ -67,13 +69,17 @@ - (void)setUp {
67
69
[FIRApp resetApps ];
68
70
// TODO: Don't mock the class we are testing.
69
71
_appClassMock = OCMClassMock ([FIRApp class ]);
70
- _observerMock = OCMObserverMock ();
71
72
_mockCoreDiagnosticsConnector = OCMClassMock ([FIRCoreDiagnosticsConnector class ]);
72
73
73
74
[FIROptionsMock mockFIROptions ];
74
75
76
+ self.assertNoLogCoreTelemetry = NO ;
75
77
OCMStub (ClassMethod ([self .mockCoreDiagnosticsConnector logCoreTelemetryWithOptions: [OCMArg any ]]))
76
- .andDo (^(NSInvocation *invocation){
78
+ .andDo (^(NSInvocation *invocation) {
79
+ if (self.assertNoLogCoreTelemetry ) {
80
+ XCTFail (@" Method `-[mockCoreDiagnosticsConnector logCoreTelemetryWithOptions:]` must not "
81
+ @" be called" );
82
+ }
77
83
});
78
84
79
85
// TODO: Remove all usages of defaultCenter in Core, then we can instantiate an instance here to
@@ -90,8 +96,6 @@ - (void)tearDown {
90
96
91
97
[_appClassMock stopMocking ];
92
98
_appClassMock = nil ;
93
- [_notificationCenter removeObserver: _observerMock];
94
- _observerMock = nil ;
95
99
_notificationCenter = nil ;
96
100
_mockCoreDiagnosticsConnector = nil ;
97
101
@@ -105,12 +109,12 @@ - (void)testConfigure {
105
109
106
110
NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName: kFIRDefaultAppName
107
111
isDefaultApp: YES ];
108
- [ self expectNotificationForObserver: self .observerMock
109
- notificationName :kFIRAppReadyToConfigureSDKNotification
110
- object: [FIRApp class ]
111
- userInfo: expectedUserInfo];
112
+ XCTestExpectation *notificationExpectation =
113
+ [ self expectNotificationNamed :kFIRAppReadyToConfigureSDKNotification
114
+ object: [FIRApp class ]
115
+ userInfo: expectedUserInfo];
112
116
XCTAssertNoThrow ([FIRApp configure ]);
113
- OCMVerifyAll ( self. observerMock ) ;
117
+ [ self waitForExpectations: @[ notificationExpectation ] timeout: 0.1 ] ;
114
118
115
119
FIRApp *app = [FIRApp defaultApp ];
116
120
XCTAssertNotNil (app);
@@ -140,17 +144,18 @@ - (void)testConfigureWithOptions {
140
144
141
145
NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName: kFIRDefaultAppName
142
146
isDefaultApp: YES ];
143
- [self expectNotificationForObserver: self .observerMock
144
- notificationName: kFIRAppReadyToConfigureSDKNotification
145
- object: [FIRApp class ]
146
- userInfo: expectedUserInfo];
147
+
148
+ XCTestExpectation *notificationExpectation =
149
+ [self expectNotificationNamed: kFIRAppReadyToConfigureSDKNotification
150
+ object: [FIRApp class ]
151
+ userInfo: expectedUserInfo];
147
152
148
153
// Use a valid instance of options.
149
154
FIROptions *options = [[FIROptions alloc ] initWithGoogleAppID: kGoogleAppID
150
155
GCMSenderID: kGCMSenderID ];
151
156
options.clientID = kClientID ;
152
157
XCTAssertNoThrow ([FIRApp configureWithOptions: options]);
153
- OCMVerifyAll ( self. observerMock ) ;
158
+ [ self waitForExpectations: @[ notificationExpectation ] timeout: 0.1 ] ;
154
159
155
160
// Verify the default app instance is created.
156
161
FIRApp *app = [FIRApp defaultApp ];
@@ -177,12 +182,12 @@ - (void)testConfigureWithNameAndOptions {
177
182
178
183
NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName: kFIRTestAppName1
179
184
isDefaultApp: NO ];
180
- [ self expectNotificationForObserver: self .observerMock
181
- notificationName :kFIRAppReadyToConfigureSDKNotification
182
- object: [FIRApp class ]
183
- userInfo: expectedUserInfo];
185
+ XCTestExpectation *notificationExpectation =
186
+ [ self expectNotificationNamed :kFIRAppReadyToConfigureSDKNotification
187
+ object: [FIRApp class ]
188
+ userInfo: expectedUserInfo];
184
189
XCTAssertNoThrow ([FIRApp configureWithName: kFIRTestAppName1 options: options]);
185
- OCMVerifyAll ( self. observerMock ) ;
190
+ [ self waitForExpectations: @[ notificationExpectation ] timeout: 0.1 ] ;
186
191
187
192
XCTAssertTrue ([FIRApp allApps ].count == 1 );
188
193
FIRApp *app = [FIRApp appNamed: kFIRTestAppName1 ];
@@ -199,16 +204,12 @@ - (void)testConfigureWithMultipleApps {
199
204
GCMSenderID: kGCMSenderID ];
200
205
options1.deepLinkURLScheme = kDeepLinkURLScheme ;
201
206
202
- // Set up notification center observer for verifying notifications.
203
- [self .notificationCenter addMockObserver: self .observerMock
204
- name: kFIRAppReadyToConfigureSDKNotification
205
- object: [FIRApp class ]];
206
-
207
207
NSDictionary *expectedUserInfo1 = [self expectedUserInfoWithAppName: kFIRTestAppName1
208
208
isDefaultApp: NO ];
209
- [[self .observerMock expect ] notificationWithName: kFIRAppReadyToConfigureSDKNotification
210
- object: [FIRApp class ]
211
- userInfo: expectedUserInfo1];
209
+ XCTestExpectation *configExpectation1 =
210
+ [self expectNotificationNamed: kFIRAppReadyToConfigureSDKNotification
211
+ object: [FIRApp class ]
212
+ userInfo: expectedUserInfo1];
212
213
XCTAssertNoThrow ([FIRApp configureWithName: kFIRTestAppName1 options: options1]);
213
214
XCTAssertTrue ([FIRApp allApps ].count == 1 );
214
215
@@ -220,13 +221,16 @@ - (void)testConfigureWithMultipleApps {
220
221
221
222
NSDictionary *expectedUserInfo2 = [self expectedUserInfoWithAppName: kFIRTestAppName2
222
223
isDefaultApp: NO ];
223
- [[self .observerMock expect ] notificationWithName: kFIRAppReadyToConfigureSDKNotification
224
- object: [FIRApp class ]
225
- userInfo: expectedUserInfo2];
224
+ XCTestExpectation *configExpectation2 =
225
+ [self expectNotificationNamed: kFIRAppReadyToConfigureSDKNotification
226
+ object: [FIRApp class ]
227
+ userInfo: expectedUserInfo2];
226
228
227
- [self .observerMock setExpectationOrderMatters: YES ];
228
229
XCTAssertNoThrow ([FIRApp configureWithName: kFIRTestAppName2 options: options2]);
229
- OCMVerifyAll (self.observerMock );
230
+
231
+ [self waitForExpectations: @[ configExpectation1, configExpectation2 ]
232
+ timeout: 0.1
233
+ enforceOrder: YES ];
230
234
231
235
XCTAssertTrue ([FIRApp allApps ].count == 2 );
232
236
FIRApp *app = [FIRApp appNamed: kFIRTestAppName2 ];
@@ -358,19 +362,21 @@ - (void)testDeleteApp {
358
362
XCTAssertNotNil (FIR_COMPONENT (FIRTestProtocolEagerCached, app.container ));
359
363
XCTAssertNil (FIR_COMPONENT (FIRTestProtocol, app.container ));
360
364
361
- [self expectNotificationForObserver: self .observerMock
362
- notificationName: kFIRAppDeleteNotification
363
- object: [FIRApp class ]
364
- userInfo: [OCMArg any ]];
365
- XCTestExpectation *expectation =
365
+ XCTestExpectation *notificationExpectation =
366
+ [self expectationForNotification: kFIRAppDeleteNotification
367
+ object: [FIRApp class ]
368
+ notificationCenter: self .notificationCenter
369
+ handler: nil ];
370
+
371
+ XCTestExpectation *deleteExpectation =
366
372
[self expectationWithDescription: @" Deleting the app should succeed." ];
367
373
[app deleteApp: ^(BOOL success) {
368
374
XCTAssertTrue (success);
369
- [expectation fulfill ];
375
+ [deleteExpectation fulfill ];
370
376
}];
371
377
372
- [self waitForExpectations: @[ expectation ] timeout: 1 ];
373
- OCMVerifyAll (self. observerMock );
378
+ [self waitForExpectations: @[ notificationExpectation, deleteExpectation ] timeout: 1 ];
379
+
374
380
XCTAssertTrue ([FIRApp allApps ].count == 0 );
375
381
376
382
// Check no new library instances created after the app delete.
@@ -725,24 +731,22 @@ - (void)testGlobalDataCollectionNoDiagnosticsSent {
725
731
FIRApp *app = [[FIRApp alloc ] initInstanceWithName: NSStringFromSelector (_cmd ) options: options];
726
732
app.dataCollectionDefaultEnabled = NO ;
727
733
728
- // Add an observer for the diagnostics notification. Currently no object is sent, but in the
729
- // future that could change so leave it as OCMOCK_ANY.
730
- [self .notificationCenter addMockObserver: self .observerMock
731
- name: kFIRAppDiagnosticsNotification
732
- object: OCMOCK_ANY];
733
-
734
734
// Stub out reading from user defaults since stubbing out the BOOL has issues. If the data
735
735
// collection switch is disabled, the `sendLogs` call should return immediately and not fire a
736
736
// notification.
737
737
OCMStub ([self .appClassMock readDataCollectionSwitchFromUserDefaultsForApp: OCMOCK_ANY])
738
738
.andReturn (@NO );
739
739
740
- // Ensure configure doesn 't fire a notification .
741
- [FIRApp configure ] ;
740
+ // Don 't expect the diagnostics data to be sent .
741
+ self. assertNoLogCoreTelemetry = YES ;
742
742
743
- // The observer mock is strict and will raise an exception when an unexpected notification is
744
- // received.
745
- OCMVerifyAll (self.observerMock );
743
+ // The diagnostics data is expected to be sent on `UIApplicationDidBecomeActiveNotification` when
744
+ // data collection is enabled.
745
+ [FIRApp configure ];
746
+ [self .notificationCenter postNotificationName: [self appDidBecomeActiveNotificationName ]
747
+ object: nil ];
748
+ // Wait for some time because diagnostics is logged asynchronously.
749
+ OCMVerifyAllWithDelay (self.mockCoreDiagnosticsConnector , 1 );
746
750
}
747
751
748
752
#pragma mark - Analytics Flag Tests
@@ -930,12 +934,18 @@ - (void)testCoreDiagnosticsLoggedWhenAppDidBecomeActive {
930
934
931
935
#pragma mark - private
932
936
933
- - (void )expectNotificationForObserver : (id )observer
934
- notificationName : (NSNotificationName )name
935
- object : (nullable id )object
936
- userInfo : (nullable NSDictionary *)userInfo {
937
- [self .notificationCenter addMockObserver: observer name: name object: object];
938
- [[observer expect ] notificationWithName: name object: object userInfo: userInfo];
937
+ - (XCTestExpectation *)expectNotificationNamed : (NSNotificationName )name
938
+ object : (nullable id )object
939
+ userInfo : (NSDictionary *)userInfo {
940
+ XCTestExpectation *notificationExpectation =
941
+ [self expectationForNotification: name
942
+ object: object
943
+ notificationCenter: self .notificationCenter
944
+ handler: ^BOOL (NSNotification *_Nonnull notification) {
945
+ return [userInfo isEqualToDictionary: notification.userInfo];
946
+ }];
947
+
948
+ return notificationExpectation;
939
949
}
940
950
941
951
- (NSDictionary <NSString *, NSObject *> *)expectedUserInfoWithAppName : (NSString *)name
0 commit comments