Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Messaging and IID singleton usage during tests. #2250

Merged
merged 5 commits into from
Jan 28, 2019
36 changes: 31 additions & 5 deletions Example/Messaging/Tests/FIRMessagingTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,37 @@

#import <FirebaseCore/FIRAppInternal.h>
#import <FirebaseInstanceID/FirebaseInstanceID.h>
#import <FirebaseAnalyticsInterop/FIRAnalyticsInterop.h>

#import "FIRMessaging.h"
#import "FIRMessaging_Private.h"

extern NSString *const kFIRMessagingFCMTokenFetchAPNSOption;

/// The NSUserDefaults domain for testing.
NSString *const kFIRMessagingDefaultsTestDomain = @"com.messaging.tests";

@interface FIRInstanceID (ExposedForTest)

+ (FIRInstanceID *)instanceIDForTests;
/// Private initializer to avoid singleton usage.
- (FIRInstanceID *)initPrivately;

/// Starts fetching and configuration of InstanceID. This is necessary after the `initPrivately`
/// call.
- (void)start;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will see if can put start inside initPrivately so most team can just call initPrivately only.


@end

@interface FIRMessaging ()
+ (FIRMessaging *)messagingForTests;

/// Surface internal initializer to avoid singleton usage during tests.
- (instancetype)initWithAnalytics:(nullable id<FIRAnalyticsInterop>)analytics
withInstanceID:(FIRInstanceID *)instanceID
withUserDefaults:(NSUserDefaults *)defaults;

@property(nonatomic, readwrite, strong) NSString *defaultFcmToken;
@property(nonatomic, readwrite, strong) NSData *apnsTokenData;
@property(nonatomic, readwrite, strong) FIRInstanceID *instanceID;
@property(nonatomic, readwrite, strong) NSUserDefaults *messagingUserDefaults;

// Direct Channel Methods
- (void)updateAutomaticClientConnection;
Expand All @@ -49,6 +61,7 @@ - (BOOL)shouldBeConnectedAutomatically;
@interface FIRMessagingTest : XCTestCase

@property(nonatomic, readonly, strong) FIRMessaging *messaging;
@property(nonatomic, strong) NSUserDefaults *messagingDefaults;
@property(nonatomic, readwrite, strong) id mockMessaging;
@property(nonatomic, readwrite, strong) id mockInstanceID;
@property(nonatomic, readwrite, strong) id mockFirebaseApp;
Expand All @@ -59,8 +72,19 @@ @implementation FIRMessagingTest

- (void)setUp {
[super setUp];
_messaging = [FIRMessaging messagingForTests];
charlotteliang marked this conversation as resolved.
Show resolved Hide resolved
_messaging.instanceID = [FIRInstanceID instanceIDForTests];

// Create the messaging instance with all the necessary dependencies.
FIRInstanceID *instanceID = [[FIRInstanceID alloc] initPrivately];
[instanceID start];

// Use custom defaults so we can empty it accordingly.
NSUserDefaults *defaults =
[[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingDefaultsTestDomain];
_messagingDefaults = defaults;
_messaging = [[FIRMessaging alloc] initWithAnalytics:nil
withInstanceID:instanceID
withUserDefaults:_messagingDefaults];

_mockFirebaseApp = OCMClassMock([FIRApp class]);
OCMStub([_mockFirebaseApp defaultApp]).andReturn(_mockFirebaseApp);
_mockInstanceID = OCMPartialMock(self.messaging.instanceID);
Expand All @@ -69,6 +93,8 @@ - (void)setUp {
}

- (void)tearDown {
[self.messagingDefaults removePersistentDomainForName:kFIRMessagingDefaultsTestDomain];
self.messagingDefaults = nil;
self.messaging.shouldEstablishDirectChannel = NO;
self.messaging.defaultFcmToken = nil;
self.messaging.apnsTokenData = nil;
Expand Down