FIRMessagingTest.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "FIRMessagingConfig.h"
  20. #import "FIRMessagingInstanceIDProxy.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. - (instancetype)initWithConfig:(FIRMessagingConfig *)config;
  27. // Direct Channel Methods
  28. - (void)updateAutomaticClientConnection;
  29. - (BOOL)shouldBeConnectedAutomatically;
  30. @end
  31. @interface FIRMessagingTest : XCTestCase
  32. @property(nonatomic, readonly, strong) FIRMessaging *messaging;
  33. @property(nonatomic, readwrite, strong) id mockMessaging;
  34. @property(nonatomic, readwrite, strong) id mockInstanceIDProxy;
  35. @end
  36. @implementation FIRMessagingTest
  37. - (void)setUp {
  38. [super setUp];
  39. FIRMessagingConfig *config = [FIRMessagingConfig defaultConfig];
  40. _messaging = [[FIRMessaging alloc] initWithConfig:config];
  41. _mockMessaging = OCMPartialMock(self.messaging);
  42. _mockInstanceIDProxy = OCMPartialMock(self.messaging.instanceIDProxy);
  43. self.messaging.instanceIDProxy = _mockInstanceIDProxy;
  44. }
  45. - (void)tearDown {
  46. _messaging = nil;
  47. _mockMessaging = nil;
  48. [super tearDown];
  49. }
  50. #pragma mark - Direct Channel Establishment Testing
  51. // Should connect with valid token and application in foreground
  52. - (void)testDoesAutomaticallyConnectIfTokenAvailableAndForegrounded {
  53. // Disable actually attempting a connection
  54. [[[_mockMessaging stub] andDo:^(NSInvocation *invocation) {
  55. // Doing nothing on purpose, when -updateAutomaticClientConnection is called
  56. }] updateAutomaticClientConnection];
  57. // Set direct channel to be established after disabling connection attempt
  58. self.messaging.shouldEstablishDirectChannel = YES;
  59. // Set a "valid" token (i.e. not nil or empty)
  60. self.messaging.defaultFcmToken = @"1234567";
  61. // Swizzle application state to return UIApplicationStateActive
  62. UIApplication *app = [UIApplication sharedApplication];
  63. id mockApp = OCMPartialMock(app);
  64. [[[mockApp stub] andReturnValue:@(UIApplicationStateActive)] applicationState];
  65. BOOL shouldBeConnected = [_mockMessaging shouldBeConnectedAutomatically];
  66. XCTAssertTrue(shouldBeConnected);
  67. }
  68. // Should not connect if application is active, but token is empty
  69. - (void)testDoesNotAutomaticallyConnectIfTokenIsEmpty {
  70. // Disable actually attempting a connection
  71. [[[_mockMessaging stub] andDo:^(NSInvocation *invocation) {
  72. // Doing nothing on purpose, when -updateAutomaticClientConnection is called
  73. }] updateAutomaticClientConnection];
  74. // Set direct channel to be established after disabling connection attempt
  75. self.messaging.shouldEstablishDirectChannel = YES;
  76. // By default, there should be no fcmToken
  77. // Swizzle application state to return UIApplicationStateActive
  78. UIApplication *app = [UIApplication sharedApplication];
  79. id mockApp = OCMPartialMock(app);
  80. [[[mockApp stub] andReturnValue:@(UIApplicationStateActive)] applicationState];
  81. BOOL shouldBeConnected = [_mockMessaging shouldBeConnectedAutomatically];
  82. XCTAssertFalse(shouldBeConnected);
  83. }
  84. // Should not connect if token valid but application isn't active
  85. - (void)testDoesNotAutomaticallyConnectIfApplicationNotActive {
  86. // Disable actually attempting a connection
  87. [[[_mockMessaging stub] andDo:^(NSInvocation *invocation) {
  88. // Doing nothing on purpose, when -updateAutomaticClientConnection is called
  89. }] updateAutomaticClientConnection];
  90. // Set direct channel to be established after disabling connection attempt
  91. self.messaging.shouldEstablishDirectChannel = YES;
  92. // Set a "valid" token (i.e. not nil or empty)
  93. self.messaging.defaultFcmToken = @"abcd1234";
  94. // Swizzle application state to return UIApplicationStateActive
  95. UIApplication *app = [UIApplication sharedApplication];
  96. id mockApp = OCMPartialMock(app);
  97. [[[mockApp stub] andReturnValue:@(UIApplicationStateBackground)] applicationState];
  98. BOOL shouldBeConnected = [_mockMessaging shouldBeConnectedAutomatically];
  99. XCTAssertFalse(shouldBeConnected);
  100. }
  101. #pragma mark - FCM Token Fetching and Deleting
  102. #ifdef NEED_WORKAROUND_FOR_PRIVATE_OCMOCK_getArgumentAtIndexAsObject
  103. - (void)testAPNSTokenIncludedInOptionsIfAvailableDuringTokenFetch {
  104. self.messaging.apnsTokenData =
  105. [@"PRETENDING_TO_BE_A_DEVICE_TOKEN" dataUsingEncoding:NSUTF8StringEncoding];
  106. XCTestExpectation *expectation =
  107. [self expectationWithDescription:@"Included APNS Token data in options dict."];
  108. // Inspect the 'options' dictionary to tell whether our expectation was fulfilled
  109. [[[self.mockInstanceIDProxy stub] andDo:^(NSInvocation *invocation) {
  110. // Calling getArgument:atIndex: directly leads to an EXC_BAD_ACCESS; use OCMock's wrapper.
  111. NSDictionary *options = [invocation getArgumentAtIndexAsObject:4];
  112. if (options[@"apns_token"] != nil) {
  113. [expectation fulfill];
  114. }
  115. }] tokenWithAuthorizedEntity:OCMOCK_ANY scope:OCMOCK_ANY options:OCMOCK_ANY handler:OCMOCK_ANY];
  116. [self.messaging retrieveFCMTokenForSenderID:@"123456"
  117. completion:^(NSString * _Nullable FCMToken,
  118. NSError * _Nullable error) {}];
  119. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  120. }
  121. - (void)testAPNSTokenNotIncludedIfUnavailableDuringTokenFetch {
  122. XCTestExpectation *expectation =
  123. [self expectationWithDescription:@"Included APNS Token data not included in options dict."];
  124. // Inspect the 'options' dictionary to tell whether our expectation was fulfilled
  125. [[[self.mockInstanceIDProxy stub] andDo:^(NSInvocation *invocation) {
  126. // Calling getArgument:atIndex: directly leads to an EXC_BAD_ACCESS; use OCMock's wrapper.
  127. NSDictionary *options = [invocation getArgumentAtIndexAsObject:4];
  128. if (options[@"apns_token"] == nil) {
  129. [expectation fulfill];
  130. }
  131. }] tokenWithAuthorizedEntity:OCMOCK_ANY scope:OCMOCK_ANY options:OCMOCK_ANY handler:OCMOCK_ANY];
  132. [self.messaging retrieveFCMTokenForSenderID:@"123456"
  133. completion:^(NSString * _Nullable FCMToken,
  134. NSError * _Nullable error) {}];
  135. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  136. }
  137. #endif
  138. - (void)testReturnsErrorWhenFetchingTokenWithoutSenderID {
  139. XCTestExpectation *expectation =
  140. [self expectationWithDescription:@"Returned an error fetching token without Sender ID"];
  141. #pragma clang diagnostic push
  142. #pragma clang diagnostic ignored "-Wnonnull"
  143. [self.messaging retrieveFCMTokenForSenderID:nil
  144. completion:
  145. ^(NSString * _Nullable FCMToken, NSError * _Nullable error) {
  146. if (error != nil) {
  147. [expectation fulfill];
  148. }
  149. }];
  150. #pragma clang diagnostic pop
  151. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  152. }
  153. - (void)testReturnsErrorWhenFetchingTokenWithEmptySenderID {
  154. XCTestExpectation *expectation =
  155. [self expectationWithDescription:@"Returned an error fetching token with empty Sender ID"];
  156. [self.messaging retrieveFCMTokenForSenderID:@""
  157. completion:
  158. ^(NSString * _Nullable FCMToken, NSError * _Nullable error) {
  159. if (error != nil) {
  160. [expectation fulfill];
  161. }
  162. }];
  163. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  164. }
  165. - (void)testReturnsErrorWhenDeletingTokenWithoutSenderID {
  166. XCTestExpectation *expectation =
  167. [self expectationWithDescription:@"Returned an error deleting token without Sender ID"];
  168. #pragma clang diagnostic push
  169. #pragma clang diagnostic ignored "-Wnonnull"
  170. [self.messaging deleteFCMTokenForSenderID:nil completion:^(NSError * _Nullable error) {
  171. if (error != nil) {
  172. [expectation fulfill];
  173. }
  174. }];
  175. #pragma clang diagnostic pop
  176. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  177. }
  178. - (void)testReturnsErrorWhenDeletingTokenWithEmptySenderID {
  179. XCTestExpectation *expectation =
  180. [self expectationWithDescription:@"Returned an error deleting token with empty Sender ID"];
  181. [self.messaging deleteFCMTokenForSenderID:@"" completion:^(NSError * _Nullable error) {
  182. if (error != nil) {
  183. [expectation fulfill];
  184. }
  185. }];
  186. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  187. }
  188. @end