FIRMessagingTest.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import <OCMock/OCMock.h>
  18. #import <FirebaseInstanceID/FirebaseInstanceID.h>
  19. #import "FIRMessaging.h"
  20. #import "FIRMessaging_Private.h"
  21. extern NSString *const kFIRMessagingFCMTokenFetchAPNSOption;
  22. @interface FIRMessaging ()
  23. @property(nonatomic, readwrite, strong) NSString *defaultFcmToken;
  24. @property(nonatomic, readwrite, strong) NSData *apnsTokenData;
  25. @property(nonatomic, readwrite, strong) FIRInstanceID *instanceID;
  26. @property(nonatomic, readwrite, strong) NSUserDefaults *messagingUserDefaults;
  27. - (instancetype)initWithInstanceID:(FIRInstanceID *)instanceID
  28. userDefaults:(NSUserDefaults *)defaults;
  29. // Direct Channel Methods
  30. - (void)updateAutomaticClientConnection;
  31. - (BOOL)shouldBeConnectedAutomatically;
  32. @end
  33. @interface FIRMessagingTest : XCTestCase
  34. @property(nonatomic, readonly, strong) FIRMessaging *messaging;
  35. @property(nonatomic, readwrite, strong) id mockMessaging;
  36. @property(nonatomic, readwrite, strong) id mockInstanceID;
  37. @end
  38. @implementation FIRMessagingTest
  39. - (void)setUp {
  40. [super setUp];
  41. _messaging = [[FIRMessaging alloc] initWithInstanceID:[FIRInstanceID instanceID]
  42. userDefaults:[NSUserDefaults standardUserDefaults]];
  43. _mockMessaging = OCMPartialMock(self.messaging);
  44. _mockInstanceID = OCMPartialMock(self.messaging.instanceID);
  45. self.messaging.instanceID = _mockInstanceID;
  46. [[NSUserDefaults standardUserDefaults]
  47. removePersistentDomainForName:[NSBundle mainBundle].bundleIdentifier];
  48. }
  49. - (void)tearDown {
  50. [_mockMessaging stopMocking];
  51. [_mockInstanceID stopMocking];
  52. [super tearDown];
  53. }
  54. - (void)testAutoInitEnableFlag {
  55. // Should read from Info.plist
  56. XCTAssertFalse(_messaging.isAutoInitEnabled);
  57. // Now set the flag should overwrite Info.plist value.
  58. _messaging.autoInitEnabled = YES;
  59. XCTAssertTrue(_messaging.isAutoInitEnabled);
  60. }
  61. #pragma mark - Direct Channel Establishment Testing
  62. // Should connect with valid token and application in foreground
  63. - (void)testDoesAutomaticallyConnectIfTokenAvailableAndForegrounded {
  64. // Disable actually attempting a connection
  65. [[[_mockMessaging stub] andDo:^(NSInvocation *invocation) {
  66. // Doing nothing on purpose, when -updateAutomaticClientConnection is called
  67. }] updateAutomaticClientConnection];
  68. // Set direct channel to be established after disabling connection attempt
  69. self.messaging.shouldEstablishDirectChannel = YES;
  70. // Set a "valid" token (i.e. not nil or empty)
  71. self.messaging.defaultFcmToken = @"1234567";
  72. // Swizzle application state to return UIApplicationStateActive
  73. UIApplication *app = [UIApplication sharedApplication];
  74. id mockApp = OCMPartialMock(app);
  75. [[[mockApp stub] andReturnValue:@(UIApplicationStateActive)] applicationState];
  76. BOOL shouldBeConnected = [_mockMessaging shouldBeConnectedAutomatically];
  77. XCTAssertTrue(shouldBeConnected);
  78. }
  79. // Should not connect if application is active, but token is empty
  80. - (void)testDoesNotAutomaticallyConnectIfTokenIsEmpty {
  81. // Disable actually attempting a connection
  82. [[[_mockMessaging stub] andDo:^(NSInvocation *invocation) {
  83. // Doing nothing on purpose, when -updateAutomaticClientConnection is called
  84. }] updateAutomaticClientConnection];
  85. // Set direct channel to be established after disabling connection attempt
  86. self.messaging.shouldEstablishDirectChannel = YES;
  87. // By default, there should be no fcmToken
  88. // Swizzle application state to return UIApplicationStateActive
  89. UIApplication *app = [UIApplication sharedApplication];
  90. id mockApp = OCMPartialMock(app);
  91. [[[mockApp stub] andReturnValue:@(UIApplicationStateActive)] applicationState];
  92. BOOL shouldBeConnected = [_mockMessaging shouldBeConnectedAutomatically];
  93. XCTAssertFalse(shouldBeConnected);
  94. }
  95. // Should not connect if token valid but application isn't active
  96. - (void)testDoesNotAutomaticallyConnectIfApplicationNotActive {
  97. // Disable actually attempting a connection
  98. [[[_mockMessaging stub] andDo:^(NSInvocation *invocation) {
  99. // Doing nothing on purpose, when -updateAutomaticClientConnection is called
  100. }] updateAutomaticClientConnection];
  101. // Set direct channel to be established after disabling connection attempt
  102. self.messaging.shouldEstablishDirectChannel = YES;
  103. // Set a "valid" token (i.e. not nil or empty)
  104. self.messaging.defaultFcmToken = @"abcd1234";
  105. // Swizzle application state to return UIApplicationStateActive
  106. UIApplication *app = [UIApplication sharedApplication];
  107. id mockApp = OCMPartialMock(app);
  108. [[[mockApp stub] andReturnValue:@(UIApplicationStateBackground)] applicationState];
  109. BOOL shouldBeConnected = [_mockMessaging shouldBeConnectedAutomatically];
  110. XCTAssertFalse(shouldBeConnected);
  111. }
  112. #pragma mark - FCM Token Fetching and Deleting
  113. - (void)testAPNSTokenIncludedInOptionsIfAvailableDuringTokenFetch {
  114. self.messaging.apnsTokenData =
  115. [@"PRETENDING_TO_BE_A_DEVICE_TOKEN" dataUsingEncoding:NSUTF8StringEncoding];
  116. XCTestExpectation *expectation =
  117. [self expectationWithDescription:@"Included APNS Token data in options dict."];
  118. // Inspect the 'options' dictionary to tell whether our expectation was fulfilled
  119. [[[self.mockInstanceID stub] andDo:^(NSInvocation *invocation) {
  120. NSDictionary *options;
  121. [invocation getArgument:&options atIndex:4];
  122. if (options[@"apns_token"] != nil) {
  123. [expectation fulfill];
  124. }
  125. }] tokenWithAuthorizedEntity:OCMOCK_ANY scope:OCMOCK_ANY options:OCMOCK_ANY handler:OCMOCK_ANY];
  126. [self.messaging retrieveFCMTokenForSenderID:@"123456"
  127. completion:^(NSString * _Nullable FCMToken,
  128. NSError * _Nullable error) {}];
  129. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  130. }
  131. - (void)testAPNSTokenNotIncludedIfUnavailableDuringTokenFetch {
  132. XCTestExpectation *expectation =
  133. [self expectationWithDescription:@"Included APNS Token data not included in options dict."];
  134. // Inspect the 'options' dictionary to tell whether our expectation was fulfilled
  135. [[[self.mockInstanceID stub] andDo:^(NSInvocation *invocation) {
  136. NSDictionary *options;
  137. [invocation getArgument:&options atIndex:4];
  138. if (options[@"apns_token"] == nil) {
  139. [expectation fulfill];
  140. }
  141. }] tokenWithAuthorizedEntity:OCMOCK_ANY scope:OCMOCK_ANY options:OCMOCK_ANY handler:OCMOCK_ANY];
  142. [self.messaging retrieveFCMTokenForSenderID:@"123456"
  143. completion:^(NSString * _Nullable FCMToken,
  144. NSError * _Nullable error) {}];
  145. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  146. }
  147. - (void)testReturnsErrorWhenFetchingTokenWithoutSenderID {
  148. XCTestExpectation *expectation =
  149. [self expectationWithDescription:@"Returned an error fetching token without Sender ID"];
  150. #pragma clang diagnostic push
  151. #pragma clang diagnostic ignored "-Wnonnull"
  152. [self.messaging retrieveFCMTokenForSenderID:nil
  153. completion:
  154. ^(NSString * _Nullable FCMToken, NSError * _Nullable error) {
  155. if (error != nil) {
  156. [expectation fulfill];
  157. }
  158. }];
  159. #pragma clang diagnostic pop
  160. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  161. }
  162. - (void)testReturnsErrorWhenFetchingTokenWithEmptySenderID {
  163. XCTestExpectation *expectation =
  164. [self expectationWithDescription:@"Returned an error fetching token with empty Sender ID"];
  165. [self.messaging retrieveFCMTokenForSenderID:@""
  166. completion:
  167. ^(NSString * _Nullable FCMToken, NSError * _Nullable error) {
  168. if (error != nil) {
  169. [expectation fulfill];
  170. }
  171. }];
  172. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  173. }
  174. - (void)testReturnsErrorWhenDeletingTokenWithoutSenderID {
  175. XCTestExpectation *expectation =
  176. [self expectationWithDescription:@"Returned an error deleting token without Sender ID"];
  177. #pragma clang diagnostic push
  178. #pragma clang diagnostic ignored "-Wnonnull"
  179. [self.messaging deleteFCMTokenForSenderID:nil completion:^(NSError * _Nullable error) {
  180. if (error != nil) {
  181. [expectation fulfill];
  182. }
  183. }];
  184. #pragma clang diagnostic pop
  185. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  186. }
  187. - (void)testReturnsErrorWhenDeletingTokenWithEmptySenderID {
  188. XCTestExpectation *expectation =
  189. [self expectationWithDescription:@"Returned an error deleting token with empty Sender ID"];
  190. [self.messaging deleteFCMTokenForSenderID:@"" completion:^(NSError * _Nullable error) {
  191. if (error != nil) {
  192. [expectation fulfill];
  193. }
  194. }];
  195. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  196. }
  197. @end