Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Example/Messaging/Tests/FIRMessagingClientTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

#import <XCTest/XCTest.h>

#import <FirebaseInstanceID/FIRInstanceID_Private.h>
#import <OCMock/OCMock.h>

#import "Protos/GtalkCore.pbobjc.h"

#import "FIRMessagingCheckinService.h"
#import "FIRMessagingClient.h"
#import "FIRMessagingConnection.h"
#import "FIRMessagingDataMessageManager.h"
Expand All @@ -44,9 +44,9 @@
static NSString *const kFIRMessagingAppIDToken = @"1234xyzdef56789";
static NSString *const kTopicToSubscribeTo = @"/topics/abcdef/hello-world";

@interface FIRMessagingRegistrar ()
@interface FIRInstanceID (exposedForTests)

@property(nonatomic, readwrite, strong) FIRMessagingCheckinService *checkinService;
+ (FIRInstanceID *)instanceIDForTests;

@end

Expand Down Expand Up @@ -91,6 +91,8 @@ @interface FIRMessagingClientTest : XCTestCase
@property(nonatomic, readwrite, strong) id mockClientDelegate;
@property(nonatomic, readwrite, strong) id mockDataMessageManager;
@property(nonatomic, readwrite, strong) id mockRegistrar;
@property(nonatomic, readwrite, strong) id mockInstanceID;


// argument callback blocks
@property(nonatomic, readwrite, copy) FIRMessagingConnectCompletionHandler connectCompletion;
Expand Down Expand Up @@ -131,6 +133,7 @@ - (void)tearDown {
- (void)tearDownMocksAndHandlers {
self.connectCompletion = nil;
self.subscribeCompletion = nil;
[self.mockInstanceID stopMocking];
}

- (void)setupConnectionWithFakeLoginResult:(BOOL)loginResult
Expand Down Expand Up @@ -296,12 +299,10 @@ - (void)setupFakeConnectionWithClass:(Class)connectionClass
}

- (void)addFIRMessagingPreferenceKeysToUserDefaults {
id mockCheckinService = OCMClassMock([FIRMessagingCheckinService class]);
[[[mockCheckinService stub] andReturn:kDeviceAuthId] deviceAuthID];
[[[mockCheckinService stub] andReturn:kSecretToken] secretToken];
[[[mockCheckinService stub] andReturnValue:@YES] hasValidCheckinInfo];

[[[self.mockRegistrar stub] andReturn:mockCheckinService] checkinService];
self.mockInstanceID = OCMPartialMock([FIRInstanceID instanceIDForTests]);
OCMStub([self.mockInstanceID tryToLoadValidCheckinInfo]).andReturn(YES);
OCMStub([self.mockInstanceID deviceAuthID]).andReturn(kDeviceAuthId);
OCMStub([self.mockInstanceID secretToken]).andReturn(kSecretToken);
}

@end
Expand Down
31 changes: 0 additions & 31 deletions Example/Messaging/Tests/FIRMessagingRegistrarTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#import <OCMock/OCMock.h>

#import "FIRMessagingCheckinService.h"
#import "FIRMessagingPubSubRegistrar.h"
#import "FIRMessagingRegistrar.h"
#import "FIRMessagingUtilities.h"
Expand All @@ -38,15 +37,13 @@
@interface FIRMessagingRegistrar ()

@property(nonatomic, readwrite, strong) FIRMessagingPubSubRegistrar *pubsubRegistrar;
@property(nonatomic, readwrite, strong) FIRMessagingCheckinService *checkinService;

@end

@interface FIRMessagingRegistrarTest : XCTestCase

@property(nonatomic, readwrite, strong) FIRMessagingRegistrar *registrar;
@property(nonatomic, readwrite, strong) id mockRegistrar;
@property(nonatomic, readwrite, strong) id mockCheckin;
@property(nonatomic, readwrite, strong) id mockPubsubRegistrar;

@end
Expand All @@ -57,15 +54,11 @@ - (void)setUp {
[super setUp];
_registrar = [[FIRMessagingRegistrar alloc] init];
_mockRegistrar = OCMPartialMock(_registrar);
_mockCheckin = OCMPartialMock(_registrar.checkinService);
_registrar.checkinService = _mockCheckin;
_registrar.pubsubRegistrar = OCMClassMock([FIRMessagingPubSubRegistrar class]);
_mockPubsubRegistrar = _registrar.pubsubRegistrar;
}

- (void)testUpdateSubscriptionWithValidCheckinData {
[self stubCheckinService];

[self.registrar updateSubscriptionToTopic:kTopicToSubscribeTo
withToken:kFIRMessagingAppIDToken
options:nil
Expand All @@ -81,8 +74,6 @@ - (void)testUpdateSubscriptionWithValidCheckinData {
}

- (void)testUpdateSubscription {
[self stubCheckinService];

__block FIRMessagingTopicOperationCompletion pubsubCompletion;
[[[self.mockPubsubRegistrar stub] andDo:^(NSInvocation *invocation) {
pubsubCompletion(nil);
Expand All @@ -102,26 +93,4 @@ - (void)testUpdateSubscription {
XCTAssertNil(error);
}];
}

- (void)testFailedUpdateSubscriptionWithNoCheckin {
// Mock checkin service to always return NO for hasValidCheckinInfo
[[[self.mockCheckin stub] andReturnValue:@NO] hasValidCheckinInfo];
// This should not create a network request since we don't have checkin info
[self.registrar updateSubscriptionToTopic:kTopicToSubscribeTo
withToken:kFIRMessagingAppIDToken
options:nil
shouldDelete:NO
handler:^(NSError *error) {
XCTAssertNotNil(error);
}];
}

#pragma mark - Private Helpers

- (void)stubCheckinService {
[[[self.mockCheckin stub] andReturn:kDeviceAuthId] deviceAuthID];
[[[self.mockCheckin stub] andReturn:kSecretToken] secretToken];
[[[self.mockCheckin stub] andReturnValue:@YES] hasValidCheckinInfo];
}

@end
7 changes: 0 additions & 7 deletions Firebase/InstanceID/FIRInstanceID+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
// TODO(chliangGoogle) Rename this to Internal.
@interface FIRInstanceID (Private)

/**
* Return the cached checkin preferences on the disk. This is used internally only by Messaging.
*
* @return The cached checkin preferences on the client.
*/
- (nullable FIRInstanceIDCheckinPreferences *)cachedCheckinPreferences;

/**
* Fetches checkin info for the app. If the app has valid cached checkin preferences
* they are returned instead of making a network request.
Expand Down
4 changes: 0 additions & 4 deletions Firebase/InstanceID/FIRInstanceID+Private.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ @interface FIRInstanceID ()

@implementation FIRInstanceID (Private)

- (FIRInstanceIDCheckinPreferences *)cachedCheckinPreferences {
return [self.tokenManager.authService checkinPreferences];
}

// This method just wraps our pre-configured auth service to make the request.
// This method is only needed by first-party users, like Remote Config.
- (void)fetchCheckinInfoWithHandler:(FIRInstanceIDDeviceCheckinCompletion)handler {
Expand Down
21 changes: 21 additions & 0 deletions Firebase/InstanceID/FIRInstanceID.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import <GoogleUtilities/GULAppEnvironmentUtil.h>
#import "FIRInstanceID+Private.h"
#import "FIRInstanceIDAuthService.h"
#import "FIRInstanceIDCheckinPreferences.h"
#import "FIRInstanceIDCombinedHandler.h"
#import "FIRInstanceIDConstants.h"
#import "FIRInstanceIDDefines.h"
Expand Down Expand Up @@ -611,6 +612,26 @@ - (void)deleteIdentityWithHandler:(FIRInstanceIDDeleteHandler)handler {
}];
}

#pragma mark - Checkin

- (BOOL)tryToLoadValidCheckinInfo {
FIRInstanceIDCheckinPreferences *checkinPreferences =
[self.tokenManager.authService checkinPreferences];
return [checkinPreferences hasValidCheckinInfo];
}

- (NSString *)deviceAuthID {
return [self.tokenManager.authService checkinPreferences].deviceID;
}

- (NSString *)secretToken {
return [self.tokenManager.authService checkinPreferences].secretToken;
}

- (NSString *)versionInfo {
return [self.tokenManager.authService checkinPreferences].versionInfo;
}

#pragma mark - Config

+ (void)load {
Expand Down
1 change: 0 additions & 1 deletion Firebase/InstanceID/FIRInstanceIDCheckinPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
@property(nonatomic, readonly, copy) NSString *deviceDataVersion;
@property(nonatomic, readonly, copy) NSString *digest;
@property(nonatomic, readonly, copy) NSString *versionInfo;
@property(nonatomic, readonly, strong) NSMutableDictionary *gServicesData;
@property(nonatomic, readonly, assign) int64_t lastCheckinTimestampMillis;

/**
Expand Down
32 changes: 31 additions & 1 deletion Firebase/InstanceID/Private/FIRInstanceID_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@

#import <FirebaseInstanceID/FIRInstanceID.h>

NS_ASSUME_NONNULL_BEGIN

@class FIRInstanceIDCheckinPreferences;
/**
* Private API used by other Firebase SDKs.
*/
@interface FIRInstanceID ()

@property(nonatomic, readonly, strong) NSString *deviceAuthID;
@property(nonatomic, readonly, strong) NSString *secretToken;
@property(nonatomic, readonly, strong) NSString *versionInfo;

/**
* Private initializer.
*/
- (nonnull instancetype)initPrivately;
- (instancetype)initPrivately;

/**
* Returns a Firebase Messaging scoped token for the firebase app.
Expand All @@ -34,4 +41,27 @@
*/
- (nullable NSString *)token;

/**
* Verify if valid checkin preferences have been loaded in memory.
*
* @return YES if valid checkin preferences exist in memory else NO.
*/
- (BOOL)hasValidCheckinInfo;

/**
* Try to load prefetched checkin preferences from the cache. This supports the use case where
* InstanceID library has already obtained a valid checkin and we should be using that.
*
* This should be used as a last gasp effort to retreive any cached checkin preferences before
* hitting the FIRMessaging backend to retrieve new preferences.
*
* Note this is only required because InstanceID and FIRMessaging both require checkin preferences
* which need to be synced with each other.
*
* @return YES if successfully loaded cached checkin preferences into memory else NO.
*/
- (BOOL)tryToLoadValidCheckinInfo;

@end

NS_ASSUME_NONNULL_END
53 changes: 0 additions & 53 deletions Firebase/Messaging/FIRMessagingCheckinService.h

This file was deleted.

Loading