FIRMessagingTest.m 8.6 KB

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