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;
  17. #import <OCMock/OCMock.h>
  18. #import "FIRMessaging.h"
  19. #import "FIRMessagingInstanceIDProxy.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) FIRMessagingInstanceIDProxy *instanceIDProxy;
  26. @property(nonatomic, readwrite, strong) NSUserDefaults *messagingUserDefaults;
  27. - (instancetype)initPrivately;
  28. // Direct Channel Methods
  29. - (void)updateAutomaticClientConnection;
  30. - (BOOL)shouldBeConnectedAutomatically;
  31. @end
  32. @interface FIRMessagingTest : XCTestCase
  33. @property(nonatomic, readonly, strong) FIRMessaging *messaging;
  34. @property(nonatomic, readwrite, strong) id mockMessaging;
  35. @property(nonatomic, readwrite, strong) id mockInstanceIDProxy;
  36. @end
  37. @implementation FIRMessagingTest
  38. - (void)setUp {
  39. [super setUp];
  40. _messaging = [[FIRMessaging alloc] initPrivately];
  41. _mockMessaging = OCMPartialMock(self.messaging);
  42. _mockInstanceIDProxy = OCMPartialMock(self.messaging.instanceIDProxy);
  43. self.messaging.instanceIDProxy = _mockInstanceIDProxy;
  44. [[NSUserDefaults standardUserDefaults]
  45. removePersistentDomainForName:[NSBundle mainBundle].bundleIdentifier];
  46. }
  47. - (void)tearDown {
  48. _messaging = nil;
  49. _mockMessaging = nil;
  50. [super tearDown];
  51. }
  52. - (void)testAutoInitEnableFlag {
  53. // Should read from Info.plist
  54. XCTAssertFalse(_messaging.isAutoInitEnabled);
  55. // Now set the flag should overwrite Info.plist value.
  56. _messaging.autoInitEnabled = YES;
  57. XCTAssertTrue(_messaging.isAutoInitEnabled);
  58. }
  59. #pragma mark - Direct Channel Establishment Testing
  60. // Should connect with valid token and application in foreground
  61. - (void)testDoesAutomaticallyConnectIfTokenAvailableAndForegrounded {
  62. // Disable actually attempting a connection
  63. [[[_mockMessaging stub] andDo:^(NSInvocation *invocation) {
  64. // Doing nothing on purpose, when -updateAutomaticClientConnection is called
  65. }] updateAutomaticClientConnection];
  66. // Set direct channel to be established after disabling connection attempt
  67. self.messaging.shouldEstablishDirectChannel = YES;
  68. // Set a "valid" token (i.e. not nil or empty)
  69. self.messaging.defaultFcmToken = @"1234567";
  70. // Swizzle application state to return UIApplicationStateActive
  71. UIApplication *app = [UIApplication sharedApplication];
  72. id mockApp = OCMPartialMock(app);
  73. [[[mockApp stub] andReturnValue:@(UIApplicationStateActive)] applicationState];
  74. BOOL shouldBeConnected = [_mockMessaging shouldBeConnectedAutomatically];
  75. XCTAssertTrue(shouldBeConnected);
  76. }
  77. // Should not connect if application is active, but token is empty
  78. - (void)testDoesNotAutomaticallyConnectIfTokenIsEmpty {
  79. // Disable actually attempting a connection
  80. [[[_mockMessaging stub] andDo:^(NSInvocation *invocation) {
  81. // Doing nothing on purpose, when -updateAutomaticClientConnection is called
  82. }] updateAutomaticClientConnection];
  83. // Set direct channel to be established after disabling connection attempt
  84. self.messaging.shouldEstablishDirectChannel = YES;
  85. // By default, there should be no fcmToken
  86. // Swizzle application state to return UIApplicationStateActive
  87. UIApplication *app = [UIApplication sharedApplication];
  88. id mockApp = OCMPartialMock(app);
  89. [[[mockApp stub] andReturnValue:@(UIApplicationStateActive)] applicationState];
  90. BOOL shouldBeConnected = [_mockMessaging shouldBeConnectedAutomatically];
  91. XCTAssertFalse(shouldBeConnected);
  92. }
  93. // Should not connect if token valid but application isn't active
  94. - (void)testDoesNotAutomaticallyConnectIfApplicationNotActive {
  95. // Disable actually attempting a connection
  96. [[[_mockMessaging stub] andDo:^(NSInvocation *invocation) {
  97. // Doing nothing on purpose, when -updateAutomaticClientConnection is called
  98. }] updateAutomaticClientConnection];
  99. // Set direct channel to be established after disabling connection attempt
  100. self.messaging.shouldEstablishDirectChannel = YES;
  101. // Set a "valid" token (i.e. not nil or empty)
  102. self.messaging.defaultFcmToken = @"abcd1234";
  103. // Swizzle application state to return UIApplicationStateActive
  104. UIApplication *app = [UIApplication sharedApplication];
  105. id mockApp = OCMPartialMock(app);
  106. [[[mockApp stub] andReturnValue:@(UIApplicationStateBackground)] applicationState];
  107. BOOL shouldBeConnected = [_mockMessaging shouldBeConnectedAutomatically];
  108. XCTAssertFalse(shouldBeConnected);
  109. }
  110. #pragma mark - FCM Token Fetching and Deleting
  111. #ifdef NEED_WORKAROUND_FOR_PRIVATE_OCMOCK_getArgumentAtIndexAsObject
  112. - (void)testAPNSTokenIncludedInOptionsIfAvailableDuringTokenFetch {
  113. self.messaging.apnsTokenData =
  114. [@"PRETENDING_TO_BE_A_DEVICE_TOKEN" dataUsingEncoding:NSUTF8StringEncoding];
  115. XCTestExpectation *expectation =
  116. [self expectationWithDescription:@"Included APNS Token data in options dict."];
  117. // Inspect the 'options' dictionary to tell whether our expectation was fulfilled
  118. [[[self.mockInstanceIDProxy stub] andDo:^(NSInvocation *invocation) {
  119. // Calling getArgument:atIndex: directly leads to an EXC_BAD_ACCESS; use OCMock's wrapper.
  120. NSDictionary *options = [invocation getArgumentAtIndexAsObject:4];
  121. if (options[@"apns_token"] != nil) {
  122. [expectation fulfill];
  123. }
  124. }] tokenWithAuthorizedEntity:OCMOCK_ANY scope:OCMOCK_ANY options:OCMOCK_ANY handler:OCMOCK_ANY];
  125. [self.messaging retrieveFCMTokenForSenderID:@"123456"
  126. completion:^(NSString * _Nullable FCMToken,
  127. NSError * _Nullable error) {}];
  128. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  129. }
  130. - (void)testAPNSTokenNotIncludedIfUnavailableDuringTokenFetch {
  131. XCTestExpectation *expectation =
  132. [self expectationWithDescription:@"Included APNS Token data not included in options dict."];
  133. // Inspect the 'options' dictionary to tell whether our expectation was fulfilled
  134. [[[self.mockInstanceIDProxy stub] andDo:^(NSInvocation *invocation) {
  135. // Calling getArgument:atIndex: directly leads to an EXC_BAD_ACCESS; use OCMock's wrapper.
  136. NSDictionary *options = [invocation getArgumentAtIndexAsObject:4];
  137. if (options[@"apns_token"] == nil) {
  138. [expectation fulfill];
  139. }
  140. }] tokenWithAuthorizedEntity:OCMOCK_ANY scope:OCMOCK_ANY options:OCMOCK_ANY handler:OCMOCK_ANY];
  141. [self.messaging retrieveFCMTokenForSenderID:@"123456"
  142. completion:^(NSString * _Nullable FCMToken,
  143. NSError * _Nullable error) {}];
  144. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  145. }
  146. #endif
  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