@@ -52,8 +52,9 @@ @interface FIRAppTest : FIRTestCase
52
52
53
53
@property (nonatomic ) id appClassMock;
54
54
@property (nonatomic ) id optionsInstanceMock;
55
- @property (nonatomic ) id notificationCenterMock ;
55
+ @property (nonatomic ) id observerMock ;
56
56
@property (nonatomic ) FIRApp *app;
57
+ @property (nonatomic ) NSNotificationCenter *notificationCenter;
57
58
58
59
@end
59
60
@@ -65,25 +66,32 @@ - (void)setUp {
65
66
[FIRApp resetApps ];
66
67
_appClassMock = OCMClassMock ([FIRApp class ]);
67
68
_optionsInstanceMock = OCMPartialMock ([FIROptions defaultOptions ]);
68
- _notificationCenterMock = OCMPartialMock ([NSNotificationCenter defaultCenter ]);
69
+ _observerMock = OCMObserverMock ();
70
+
71
+ // TODO: Remove all usages of defaultCenter in Core, then we can instantiate an instance here to
72
+ // inject instead of using defaultCenter.
73
+ _notificationCenter = [NSNotificationCenter defaultCenter ];
69
74
}
70
75
71
76
- (void )tearDown {
72
77
[_appClassMock stopMocking ];
73
78
[_optionsInstanceMock stopMocking ];
74
- [_notificationCenterMock stopMocking ];
79
+ [_notificationCenter removeObserver: _observerMock];
80
+ _observerMock = nil ;
81
+ _notificationCenter = nil ;
75
82
76
83
[super tearDown ];
77
84
}
78
85
79
86
- (void )testConfigure {
80
87
NSDictionary *expectedUserInfo =
81
88
[self expectedUserInfoWithAppName: kFIRDefaultAppName isDefaultApp: YES ];
82
- OCMExpect ([self .notificationCenterMock postNotificationName: kFIRAppReadyToConfigureSDKNotification
83
- object: [FIRApp class ]
84
- userInfo: expectedUserInfo]);
89
+ [self expectNotificationForObserver: self .observerMock
90
+ notificationName: kFIRAppReadyToConfigureSDKNotification
91
+ object: [FIRApp class ]
92
+ userInfo: expectedUserInfo];
85
93
XCTAssertNoThrow ([FIRApp configure ]);
86
- OCMVerifyAll (self.notificationCenterMock );
94
+ OCMVerifyAll (self.observerMock );
87
95
88
96
self.app = [FIRApp defaultApp ];
89
97
XCTAssertNotNil (self.app );
@@ -108,12 +116,13 @@ - (void)testConfigureWithOptions {
108
116
109
117
NSDictionary *expectedUserInfo =
110
118
[self expectedUserInfoWithAppName: kFIRDefaultAppName isDefaultApp: YES ];
111
- OCMExpect ([self .notificationCenterMock postNotificationName: kFIRAppReadyToConfigureSDKNotification
112
- object: [FIRApp class ]
113
- userInfo: expectedUserInfo]);
119
+ [self expectNotificationForObserver: self .observerMock
120
+ notificationName: kFIRAppReadyToConfigureSDKNotification
121
+ object: [FIRApp class ]
122
+ userInfo: expectedUserInfo];
114
123
// default options
115
124
XCTAssertNoThrow ([FIRApp configureWithOptions: [FIROptions defaultOptions ]]);
116
- OCMVerifyAll (self.notificationCenterMock );
125
+ OCMVerifyAll (self.observerMock );
117
126
118
127
self.app = [FIRApp defaultApp ];
119
128
XCTAssertNotNil (self.app );
@@ -130,12 +139,13 @@ - (void)testConfigureWithCustomizedOptions {
130
139
options.APIKey = kCustomizedAPIKey ;
131
140
NSDictionary *expectedUserInfo =
132
141
[self expectedUserInfoWithAppName: kFIRDefaultAppName isDefaultApp: YES ];
133
- OCMExpect ([self .notificationCenterMock postNotificationName: kFIRAppReadyToConfigureSDKNotification
134
- object: [FIRApp class ]
135
- userInfo: expectedUserInfo]);
142
+ [self expectNotificationForObserver: self .observerMock
143
+ notificationName: kFIRAppReadyToConfigureSDKNotification
144
+ object: [FIRApp class ]
145
+ userInfo: expectedUserInfo];
136
146
137
147
XCTAssertNoThrow ([FIRApp configureWithOptions: options]);
138
- OCMVerifyAll (self.notificationCenterMock );
148
+ OCMVerifyAll (self.observerMock );
139
149
140
150
self.app = [FIRApp defaultApp ];
141
151
XCTAssertNotNil (self.app );
@@ -158,11 +168,12 @@ - (void)testConfigureWithNameAndOptions {
158
168
159
169
NSDictionary *expectedUserInfo =
160
170
[self expectedUserInfoWithAppName: kFIRTestAppName1 isDefaultApp: NO ];
161
- OCMExpect ([self .notificationCenterMock postNotificationName: kFIRAppReadyToConfigureSDKNotification
162
- object: [FIRApp class ]
163
- userInfo: expectedUserInfo]);
171
+ [self expectNotificationForObserver: self .observerMock
172
+ notificationName: kFIRAppReadyToConfigureSDKNotification
173
+ object: [FIRApp class ]
174
+ userInfo: expectedUserInfo];
164
175
XCTAssertNoThrow ([FIRApp configureWithName: kFIRTestAppName1 options: [FIROptions defaultOptions ]]);
165
- OCMVerifyAll (self.notificationCenterMock );
176
+ OCMVerifyAll (self.observerMock );
166
177
167
178
XCTAssertTrue ([FIRApp allApps ].count == 1 );
168
179
self.app = [FIRApp appNamed: kFIRTestAppName1 ];
@@ -179,11 +190,16 @@ - (void)testConfigureWithNameAndCustomizedOptions {
179
190
FIROptions *newOptions = [options copy ];
180
191
newOptions.deepLinkURLScheme = kDeepLinkURLScheme ;
181
192
193
+ // Set up notification center observer for verifying notifications.
194
+ [self .notificationCenter addMockObserver: self .observerMock
195
+ name: kFIRAppReadyToConfigureSDKNotification
196
+ object: [FIRApp class ]];
197
+
182
198
NSDictionary *expectedUserInfo1 =
183
199
[self expectedUserInfoWithAppName: kFIRTestAppName1 isDefaultApp: NO ];
184
- OCMExpect ([ self .notificationCenterMock postNotificationName :kFIRAppReadyToConfigureSDKNotification
185
- object: [FIRApp class ]
186
- userInfo: expectedUserInfo1]) ;
200
+ [[ self .observerMock expect ] notificationWithName :kFIRAppReadyToConfigureSDKNotification
201
+ object: [FIRApp class ]
202
+ userInfo: expectedUserInfo1];
187
203
XCTAssertNoThrow ([FIRApp configureWithName: kFIRTestAppName1 options: newOptions]);
188
204
XCTAssertTrue ([FIRApp allApps ].count == 1 );
189
205
self.app = [FIRApp appNamed: kFIRTestAppName1 ];
@@ -196,11 +212,13 @@ - (void)testConfigureWithNameAndCustomizedOptions {
196
212
197
213
NSDictionary *expectedUserInfo2 =
198
214
[self expectedUserInfoWithAppName: kFIRTestAppName2 isDefaultApp: NO ];
199
- OCMExpect ([self .notificationCenterMock postNotificationName: kFIRAppReadyToConfigureSDKNotification
200
- object: [FIRApp class ]
201
- userInfo: expectedUserInfo2]);
215
+ [[self .observerMock expect ] notificationWithName: kFIRAppReadyToConfigureSDKNotification
216
+ object: [FIRApp class ]
217
+ userInfo: expectedUserInfo2];
218
+
219
+ [self .observerMock setExpectationOrderMatters: YES ];
202
220
XCTAssertNoThrow ([FIRApp configureWithName: kFIRTestAppName2 options: customizedOptions]);
203
- OCMVerifyAll (self.notificationCenterMock );
221
+ OCMVerifyAll (self.observerMock );
204
222
205
223
XCTAssertTrue ([FIRApp allApps ].count == 2 );
206
224
self.app = [FIRApp appNamed: kFIRTestAppName2 ];
@@ -241,12 +259,15 @@ - (void)testDeleteApp {
241
259
[FIRApp configure ];
242
260
self.app = [FIRApp defaultApp ];
243
261
XCTAssertTrue ([FIRApp allApps ].count == 1 );
262
+ [self expectNotificationForObserver: self .observerMock
263
+ notificationName: kFIRAppDeleteNotification
264
+ object: [FIRApp class ]
265
+ userInfo: [OCMArg any ]];
244
266
[self .app deleteApp: ^(BOOL success) {
245
267
XCTAssertTrue (success);
246
268
}];
247
- OCMVerify ([self .notificationCenterMock postNotificationName: kFIRAppDeleteNotification
248
- object: [FIRApp class ]
249
- userInfo: [OCMArg any ]]);
269
+
270
+ OCMVerifyAll (self.observerMock );
250
271
XCTAssertTrue (self.app .alreadySentDeleteNotification );
251
272
XCTAssertTrue ([FIRApp allApps ].count == 0 );
252
273
}
@@ -671,16 +692,27 @@ - (void)testGlobalDataCollectionClearedAfterDelete {
671
692
- (void )testGlobalDataCollectionNoDiagnosticsSent {
672
693
[FIRApp configure ];
673
694
695
+ // Add an observer for the diagnostics notification - both with and without an object to ensure it
696
+ // catches it either way. Currently no object is sent, but in the future that could change.
697
+ [self .notificationCenter addMockObserver: self .observerMock
698
+ name: kFIRAppDiagnosticsNotification
699
+ object: nil ];
700
+ [self .notificationCenter addMockObserver: self .observerMock
701
+ name: kFIRAppDiagnosticsNotification
702
+ object: OCMOCK_ANY];
703
+
674
704
// Stub out reading from user defaults since stubbing out the BOOL has issues. If the data
675
705
// collection switch is disabled, the `sendLogs` call should return immediately and not fire a
676
706
// notification.
677
707
OCMStub ([self .appClassMock readDataCollectionSwitchFromUserDefaultsForApp: OCMOCK_ANY])
678
708
.andReturn (@NO );
679
- OCMReject ([self .notificationCenterMock postNotificationName: kFIRAppDiagnosticsNotification
680
- object: OCMOCK_ANY
681
- userInfo: OCMOCK_ANY]);
709
+
682
710
NSError *error = [NSError errorWithDomain: @" com.firebase" code: 42 userInfo: nil ];
683
711
[[FIRApp defaultApp ] sendLogsWithServiceName: @" Service" version: @" Version" error: error];
712
+
713
+ // The observer mock is strict and will raise an exception when an unexpected notification is
714
+ // received.
715
+ OCMVerifyAll (self.observerMock );
684
716
}
685
717
686
718
#pragma mark - Analytics Flag Tests
@@ -767,6 +799,14 @@ - (void)testMultipleLibraries {
767
799
768
800
#pragma mark - private
769
801
802
+ - (void )expectNotificationForObserver : (id )observer
803
+ notificationName : (NSNotificationName )name
804
+ object : (nullable id )object
805
+ userInfo : (nullable NSDictionary *)userInfo {
806
+ [self .notificationCenter addMockObserver: observer name: name object: object];
807
+ [[observer expect ] notificationWithName: name object: object userInfo: userInfo];
808
+ }
809
+
770
810
- (NSDictionary <NSString *, NSObject *> *)expectedUserInfoWithAppName : (NSString *)name
771
811
isDefaultApp : (BOOL )isDefaultApp {
772
812
return @{
0 commit comments