Files
firebase-ios-sdk/Example/Messaging/Tests/FIRInstanceIDWithFCMTest.m
T
Ryan Wilson 56b37d2543 Fix container instantiation timing, IID startup. (#4030)
* Fix container instantiation timing, IID startup.

The container instantiation could happen at the wrong time, before all
components have been added to the container. This fixes it, and also
moves IID to use the proper instantiation timing instead of relying on
the `configureWithApp:` call. This is part continuation of #3147, where
I'll be fixing the rest of the SDKs in follow up PRs.

* Typo

* Further work on instantiation timing.

This moves the instantiation of components to after the FirebaseApp is
completely configured and assigned, as it should be. This also exposed
a recursive issue with IID calling the Messaging singleton, which in
turn called IID and instantiated multiple instances.

A change was made to break the cycle: the Messaging `isAutoInitEnabled`
call was changed to a static method vs an instance method, allowing IID
to call it during initialization without instantiating the Messaging
instance (since it doesn't have a direct dependency on it).

* Revert unintentional formatting of entire FIRMessaging file.

* Fix tests.

* Format InstanceID.m

* Remove unused import.

* Fix ComponentContainer test.

* Minor PR feedback.
2019-10-11 17:20:46 -04:00

82 lines
2.5 KiB
Objective-C

/*
* Copyright 2019 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>
#import <FirebaseCore/FIRAppInternal.h>
#import <FirebaseInstanceID/FirebaseInstanceID.h>
#import <FirebaseMessaging/FIRMessaging.h>
#import "Firebase/Messaging/FIRMessaging_Private.h"
#import "Example/Messaging/Tests/FIRMessagingTestUtilities.h"
@interface FIRInstanceID (ExposedForTest)
- (BOOL)isFCMAutoInitEnabled;
- (instancetype)initPrivately;
- (void)start;
@end
@interface FIRMessaging ()
+ (FIRMessaging *)messagingForTests;
@end
@interface FIRInstanceIDTest : XCTestCase
@property(nonatomic, readwrite, strong) FIRInstanceID *instanceID;
@property(nonatomic, readwrite, strong) id mockFirebaseApp;
@end
@implementation FIRInstanceIDTest
- (void)setUp {
[super setUp];
_instanceID = [[FIRInstanceID alloc] initPrivately];
[_instanceID start];
_mockFirebaseApp = OCMClassMock([FIRApp class]);
OCMStub([_mockFirebaseApp defaultApp]).andReturn(_mockFirebaseApp);
}
- (void)tearDown {
self.instanceID = nil;
[_mockFirebaseApp stopMocking];
[super tearDown];
}
- (void)testFCMAutoInitEnabled {
// Use the standardUserDefaults since that's what IID expects and depends on.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
FIRMessaging *messaging = [FIRMessagingTestUtilities messagingForTestsWithUserDefaults:defaults];
id classMock = OCMClassMock([FIRMessaging class]);
OCMStub([_mockFirebaseApp isDataCollectionDefaultEnabled]).andReturn(YES);
messaging.autoInitEnabled = YES;
XCTAssertTrue(
[_instanceID isFCMAutoInitEnabled],
@"When FCM is available, FCM Auto Init Enabled should be FCM's autoInitEnable property.");
messaging.autoInitEnabled = NO;
XCTAssertFalse(
[_instanceID isFCMAutoInitEnabled],
@"When FCM is available, FCM Auto Init Enabled should be FCM's autoInitEnable property.");
messaging.autoInitEnabled = YES;
XCTAssertTrue([_instanceID isFCMAutoInitEnabled]);
[classMock stopMocking];
}
@end