Skip to content

Commit 497d08b

Browse files
Core: fix and cleanup notification center tests (#7244)
* Fix log diagnostics tests and remove unused notification * Replace deprecated OCMock methods with notification expectations * Bump Core and CoreDiagnostics versions * Defer Core version update until release Co-authored-by: Paul Beusterien <[email protected]>
1 parent dc05755 commit 497d08b

File tree

7 files changed

+70
-82
lines changed

7 files changed

+70
-82
lines changed

Example/CoreDiagnostics/Tests/FIRCoreDiagnosticsTest.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
#import "Firebase/CoreDiagnostics/FIRCDLibrary/Protogen/nanopb/firebasecore.nanopb.h"
3333

34-
extern NSString *const kFIRAppDiagnosticsNotification;
3534
extern NSString *const kFIRLastCheckinDateKey;
3635

3736
static NSString *const kGoogleAppID = @"1:123:ios:123abc";

FirebaseCore.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Firebase Core includes FIRApp and FIROptions which provide central configuration
4747
s.tvos.framework = 'UIKit'
4848
s.dependency 'GoogleUtilities/Environment', '~> 7.0'
4949
s.dependency 'GoogleUtilities/Logger', '~> 7.0'
50-
s.dependency 'FirebaseCoreDiagnostics', '~> 7.0'
50+
s.dependency 'FirebaseCoreDiagnostics', '~> 7.4'
5151

5252
s.pod_target_xcconfig = {
5353
'GCC_C_LANGUAGE_STANDARD' => 'c99',

FirebaseCore/Sources/FIRApp.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@
7373
NSString *const kFIRGlobalAppDataCollectionEnabledPlistKey =
7474
@"FirebaseDataCollectionDefaultEnabled";
7575

76-
NSString *const kFIRAppDiagnosticsNotification = @"FIRAppDiagnosticsNotification";
77-
7876
NSString *const kFIRAppDiagnosticsConfigurationTypeKey = @"ConfigType";
7977
NSString *const kFIRAppDiagnosticsErrorKey = @"Error";
8078
NSString *const kFIRAppDiagnosticsFIRAppKey = @"FIRApp";

FirebaseCore/Sources/Private/FIRAppInternal.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ extern NSString *const kFIRGlobalAppDataCollectionEnabledDefaultsKeyFormat;
5252
*/
5353
extern NSString *const kFIRGlobalAppDataCollectionEnabledPlistKey;
5454

55-
/**
56-
* A notification fired containing diagnostic information when SDK errors occur.
57-
*/
58-
extern NSString *const kFIRAppDiagnosticsNotification;
59-
6055
/** @var FIRAuthStateDidChangeInternalNotification
6156
@brief The name of the @c NSNotificationCenter notification which is posted when the auth state
6257
changes (e.g. a new token has been produced, a user logs in or out). The object parameter of

FirebaseCore/Tests/Unit/FIRAnalyticsConfigurationTest.m

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#import "FirebaseCore/Sources/FIRAnalyticsConfiguration.h"
2020

2121
@interface FIRAnalyticsConfigurationTest : FIRTestCase
22-
/// An observer for NSNotificationCenter.
23-
@property(nonatomic, strong) id observerMock;
2422

2523
@property(nonatomic, strong) NSNotificationCenter *notificationCenter;
2624
@end
@@ -30,12 +28,10 @@ @implementation FIRAnalyticsConfigurationTest
3028
- (void)setUp {
3129
[super setUp];
3230

33-
_observerMock = OCMObserverMock();
3431
_notificationCenter = [NSNotificationCenter defaultCenter];
3532
}
3633

3734
- (void)tearDown {
38-
_observerMock = nil;
3935
_notificationCenter = nil;
4036

4137
[super tearDown];
@@ -72,14 +68,4 @@ - (void)testSettingAnalyticsCollectionPersistence {
7268
[userDefaultsMock stopMocking];
7369
}
7470

75-
#pragma mark - Private Test Helpers
76-
77-
- (void)expectNotificationForObserver:(id)observer
78-
notificationName:(NSNotificationName)name
79-
object:(nullable id)object
80-
userInfo:(nullable NSDictionary *)userInfo {
81-
[self.notificationCenter addMockObserver:self.observerMock name:name object:object];
82-
[[observer expect] notificationWithName:name object:object userInfo:userInfo];
83-
}
84-
8571
@end

FirebaseCore/Tests/Unit/FIRAppTest.m

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ + (nullable NSNumber *)readDataCollectionSwitchFromUserDefaultsForApp:(FIRApp *)
5353
@interface FIRAppTest : FIRTestCase
5454

5555
@property(nonatomic) id appClassMock;
56-
@property(nonatomic) id observerMock;
5756
@property(nonatomic) id mockCoreDiagnosticsConnector;
5857
@property(nonatomic) NSNotificationCenter *notificationCenter;
5958

59+
/// If `YES` then throws when `logCoreTelemetryWithOptions:` method is called.
60+
@property(nonatomic) BOOL assertNoLogCoreTelemetry;
61+
6062
@end
6163

6264
@implementation FIRAppTest
@@ -67,13 +69,17 @@ - (void)setUp {
6769
[FIRApp resetApps];
6870
// TODO: Don't mock the class we are testing.
6971
_appClassMock = OCMClassMock([FIRApp class]);
70-
_observerMock = OCMObserverMock();
7172
_mockCoreDiagnosticsConnector = OCMClassMock([FIRCoreDiagnosticsConnector class]);
7273

7374
[FIROptionsMock mockFIROptions];
7475

76+
self.assertNoLogCoreTelemetry = NO;
7577
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+
}
7783
});
7884

7985
// TODO: Remove all usages of defaultCenter in Core, then we can instantiate an instance here to
@@ -90,8 +96,6 @@ - (void)tearDown {
9096

9197
[_appClassMock stopMocking];
9298
_appClassMock = nil;
93-
[_notificationCenter removeObserver:_observerMock];
94-
_observerMock = nil;
9599
_notificationCenter = nil;
96100
_mockCoreDiagnosticsConnector = nil;
97101

@@ -105,12 +109,12 @@ - (void)testConfigure {
105109

106110
NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName
107111
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];
112116
XCTAssertNoThrow([FIRApp configure]);
113-
OCMVerifyAll(self.observerMock);
117+
[self waitForExpectations:@[ notificationExpectation ] timeout:0.1];
114118

115119
FIRApp *app = [FIRApp defaultApp];
116120
XCTAssertNotNil(app);
@@ -140,17 +144,18 @@ - (void)testConfigureWithOptions {
140144

141145
NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName
142146
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];
147152

148153
// Use a valid instance of options.
149154
FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
150155
GCMSenderID:kGCMSenderID];
151156
options.clientID = kClientID;
152157
XCTAssertNoThrow([FIRApp configureWithOptions:options]);
153-
OCMVerifyAll(self.observerMock);
158+
[self waitForExpectations:@[ notificationExpectation ] timeout:0.1];
154159

155160
// Verify the default app instance is created.
156161
FIRApp *app = [FIRApp defaultApp];
@@ -177,12 +182,12 @@ - (void)testConfigureWithNameAndOptions {
177182

178183
NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRTestAppName1
179184
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];
184189
XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:options]);
185-
OCMVerifyAll(self.observerMock);
190+
[self waitForExpectations:@[ notificationExpectation ] timeout:0.1];
186191

187192
XCTAssertTrue([FIRApp allApps].count == 1);
188193
FIRApp *app = [FIRApp appNamed:kFIRTestAppName1];
@@ -199,16 +204,12 @@ - (void)testConfigureWithMultipleApps {
199204
GCMSenderID:kGCMSenderID];
200205
options1.deepLinkURLScheme = kDeepLinkURLScheme;
201206

202-
// Set up notification center observer for verifying notifications.
203-
[self.notificationCenter addMockObserver:self.observerMock
204-
name:kFIRAppReadyToConfigureSDKNotification
205-
object:[FIRApp class]];
206-
207207
NSDictionary *expectedUserInfo1 = [self expectedUserInfoWithAppName:kFIRTestAppName1
208208
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];
212213
XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:options1]);
213214
XCTAssertTrue([FIRApp allApps].count == 1);
214215

@@ -220,13 +221,16 @@ - (void)testConfigureWithMultipleApps {
220221

221222
NSDictionary *expectedUserInfo2 = [self expectedUserInfoWithAppName:kFIRTestAppName2
222223
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];
226228

227-
[self.observerMock setExpectationOrderMatters:YES];
228229
XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName2 options:options2]);
229-
OCMVerifyAll(self.observerMock);
230+
231+
[self waitForExpectations:@[ configExpectation1, configExpectation2 ]
232+
timeout:0.1
233+
enforceOrder:YES];
230234

231235
XCTAssertTrue([FIRApp allApps].count == 2);
232236
FIRApp *app = [FIRApp appNamed:kFIRTestAppName2];
@@ -358,19 +362,21 @@ - (void)testDeleteApp {
358362
XCTAssertNotNil(FIR_COMPONENT(FIRTestProtocolEagerCached, app.container));
359363
XCTAssertNil(FIR_COMPONENT(FIRTestProtocol, app.container));
360364

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 =
366372
[self expectationWithDescription:@"Deleting the app should succeed."];
367373
[app deleteApp:^(BOOL success) {
368374
XCTAssertTrue(success);
369-
[expectation fulfill];
375+
[deleteExpectation fulfill];
370376
}];
371377

372-
[self waitForExpectations:@[ expectation ] timeout:1];
373-
OCMVerifyAll(self.observerMock);
378+
[self waitForExpectations:@[ notificationExpectation, deleteExpectation ] timeout:1];
379+
374380
XCTAssertTrue([FIRApp allApps].count == 0);
375381

376382
// Check no new library instances created after the app delete.
@@ -725,24 +731,22 @@ - (void)testGlobalDataCollectionNoDiagnosticsSent {
725731
FIRApp *app = [[FIRApp alloc] initInstanceWithName:NSStringFromSelector(_cmd) options:options];
726732
app.dataCollectionDefaultEnabled = NO;
727733

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-
734734
// Stub out reading from user defaults since stubbing out the BOOL has issues. If the data
735735
// collection switch is disabled, the `sendLogs` call should return immediately and not fire a
736736
// notification.
737737
OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
738738
.andReturn(@NO);
739739

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;
742742

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);
746750
}
747751

748752
#pragma mark - Analytics Flag Tests
@@ -930,12 +934,18 @@ - (void)testCoreDiagnosticsLoggedWhenAppDidBecomeActive {
930934

931935
#pragma mark - private
932936

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;
939949
}
940950

941951
- (NSDictionary<NSString *, NSObject *> *)expectedUserInfoWithAppName:(NSString *)name

FirebaseCoreDiagnostics.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'FirebaseCoreDiagnostics'
3-
s.version = '7.3.0'
3+
s.version = '7.4.0'
44
s.summary = 'Firebase Core Diagnostics'
55

66
s.description = <<-DESC

0 commit comments

Comments
 (0)