FIRMessagingTest.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import <GoogleUtilities/GULUserDefaults.h>
  19. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  20. #import "FirebaseMessaging/Sources/FIRMessaging_Private.h"
  21. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  22. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenManager.h"
  23. #import "FirebaseMessaging/Tests/UnitTests/FIRMessagingTestUtilities.h"
  24. #import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
  25. extern NSString *const kFIRMessagingFCMTokenFetchAPNSOption;
  26. @interface FIRMessaging ()
  27. @property(nonatomic, readwrite, strong) NSString *defaultFcmToken;
  28. @property(nonatomic, readwrite, strong) NSData *apnsTokenData;
  29. @property(nonatomic, readwrite, strong) FIRMessagingTokenManager *tokenManager;
  30. // Expose autoInitEnabled static method for IID.
  31. + (BOOL)isAutoInitEnabledWithUserDefaults:(NSUserDefaults *)userDefaults;
  32. // Direct Channel Methods
  33. - (void)updateAutomaticClientConnection;
  34. - (BOOL)shouldBeConnectedAutomatically;
  35. @end
  36. @interface FIRMessagingTest : XCTestCase
  37. @property(nonatomic, readonly, strong) FIRMessaging *messaging;
  38. @property(nonatomic, readwrite, strong) id mockMessaging;
  39. @property(nonatomic, readwrite, strong) id mockInstanceID;
  40. @property(nonatomic, readwrite, strong) id mockFirebaseApp;
  41. @property(nonatomic, readwrite, strong) id mockTokenManager;
  42. @property(nonatomic, strong) FIRMessagingTestUtilities *testUtil;
  43. @end
  44. @implementation FIRMessagingTest
  45. - (void)setUp {
  46. [super setUp];
  47. // Create the messaging instance with all the necessary dependencies.
  48. NSUserDefaults *defaults =
  49. [[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingDefaultsTestDomain];
  50. _testUtil = [[FIRMessagingTestUtilities alloc] initWithUserDefaults:defaults withRMQManager:NO];
  51. _mockMessaging = _testUtil.mockMessaging;
  52. _messaging = _testUtil.messaging;
  53. _mockTokenManager = _testUtil.mockTokenManager;
  54. _mockFirebaseApp = OCMClassMock([FIRApp class]);
  55. OCMStub([_mockFirebaseApp defaultApp]).andReturn(_mockFirebaseApp);
  56. [[NSUserDefaults standardUserDefaults]
  57. removePersistentDomainForName:[NSBundle mainBundle].bundleIdentifier];
  58. }
  59. - (void)tearDown {
  60. [_testUtil cleanupAfterTest:self];
  61. [_mockFirebaseApp stopMocking];
  62. _messaging = nil;
  63. [[[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingDefaultsTestDomain]
  64. removePersistentDomainForName:kFIRMessagingDefaultsTestDomain];
  65. [super tearDown];
  66. }
  67. - (void)testAutoInitEnableFlag {
  68. // Should read from Info.plist
  69. XCTAssertFalse(_messaging.isAutoInitEnabled);
  70. // Now set the flag should overwrite Info.plist value.
  71. _messaging.autoInitEnabled = YES;
  72. XCTAssertTrue(_messaging.isAutoInitEnabled);
  73. }
  74. - (void)testAutoInitEnableFlagOverrideGlobalTrue {
  75. OCMStub([_mockFirebaseApp isDataCollectionDefaultEnabled]).andReturn(YES);
  76. id bundleMock = OCMPartialMock([NSBundle mainBundle]);
  77. OCMStub([bundleMock objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled]).andReturn(nil);
  78. XCTAssertTrue(self.messaging.isAutoInitEnabled);
  79. self.messaging.autoInitEnabled = NO;
  80. XCTAssertFalse(self.messaging.isAutoInitEnabled);
  81. [bundleMock stopMocking];
  82. }
  83. - (void)testAutoInitEnableFlagOverrideGlobalFalse {
  84. OCMStub([_mockFirebaseApp isDataCollectionDefaultEnabled]).andReturn(YES);
  85. id bundleMock = OCMPartialMock([NSBundle mainBundle]);
  86. OCMStub([bundleMock objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled]).andReturn(nil);
  87. XCTAssertTrue(self.messaging.isAutoInitEnabled);
  88. self.messaging.autoInitEnabled = NO;
  89. XCTAssertFalse(self.messaging.isAutoInitEnabled);
  90. [bundleMock stopMocking];
  91. }
  92. - (void)testAutoInitEnableGlobalDefaultTrue {
  93. OCMStub([_mockFirebaseApp isDataCollectionDefaultEnabled]).andReturn(YES);
  94. id bundleMock = OCMPartialMock([NSBundle mainBundle]);
  95. OCMStub([bundleMock objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled]).andReturn(nil);
  96. XCTAssertTrue(self.messaging.isAutoInitEnabled);
  97. [bundleMock stopMocking];
  98. }
  99. - (void)testAutoInitEnableGlobalDefaultFalse {
  100. OCMStub([_mockFirebaseApp isDataCollectionDefaultEnabled]).andReturn(NO);
  101. id bundleMock = OCMPartialMock([NSBundle mainBundle]);
  102. OCMStub([bundleMock objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled]).andReturn(nil);
  103. XCTAssertFalse(self.messaging.isAutoInitEnabled);
  104. [bundleMock stopMocking];
  105. }
  106. - (void)testAutoInitEnabledMatchesStaticMethod {
  107. // Flag is set to YES in user defaults.
  108. NSUserDefaults *defaults = self.messaging.messagingUserDefaults;
  109. [defaults setObject:@YES forKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled];
  110. XCTAssertTrue(self.messaging.isAutoInitEnabled);
  111. XCTAssertEqual(self.messaging.isAutoInitEnabled,
  112. [FIRMessaging isAutoInitEnabledWithUserDefaults:defaults]);
  113. }
  114. - (void)testAutoInitDisabledMatchesStaticMethod {
  115. // Flag is set to NO in user defaults.
  116. NSUserDefaults *defaults = self.messaging.messagingUserDefaults;
  117. [defaults setObject:@NO forKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled];
  118. XCTAssertFalse(self.messaging.isAutoInitEnabled);
  119. XCTAssertEqual(self.messaging.isAutoInitEnabled,
  120. [FIRMessaging isAutoInitEnabledWithUserDefaults:defaults]);
  121. }
  122. #pragma mark - FCM Token Fetching and Deleting
  123. // TODO(chliang) mock tokenManager
  124. - (void)x_testAPNSTokenIncludedInOptionsIfAvailableDuringTokenFetch {
  125. self.messaging.apnsTokenData =
  126. [@"PRETENDING_TO_BE_A_DEVICE_TOKEN" dataUsingEncoding:NSUTF8StringEncoding];
  127. XCTestExpectation *expectation =
  128. [self expectationWithDescription:@"Included APNS Token data in options dict."];
  129. // Inspect the 'options' dictionary to tell whether our expectation was fulfilled
  130. [[[self.mockInstanceID stub] andDo:^(NSInvocation *invocation) {
  131. __unsafe_unretained NSDictionary *options;
  132. [invocation getArgument:&options atIndex:4];
  133. if (options[@"apns_token"] != nil) {
  134. [expectation fulfill];
  135. }
  136. }] tokenWithAuthorizedEntity:OCMOCK_ANY scope:OCMOCK_ANY options:OCMOCK_ANY handler:OCMOCK_ANY];
  137. [self.messaging
  138. retrieveFCMTokenForSenderID:@"123456"
  139. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error){
  140. }];
  141. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  142. }
  143. - (void)x_testAPNSTokenNotIncludedIfUnavailableDuringTokenFetch {
  144. XCTestExpectation *expectation =
  145. [self expectationWithDescription:@"Included APNS Token data not included in options dict."];
  146. // Inspect the 'options' dictionary to tell whether our expectation was fulfilled
  147. [[[self.mockInstanceID stub] andDo:^(NSInvocation *invocation) {
  148. __unsafe_unretained NSDictionary *options;
  149. [invocation getArgument:&options atIndex:4];
  150. if (options[@"apns_token"] == nil) {
  151. [expectation fulfill];
  152. }
  153. }] tokenWithAuthorizedEntity:OCMOCK_ANY scope:OCMOCK_ANY options:OCMOCK_ANY handler:OCMOCK_ANY];
  154. [self.messaging
  155. retrieveFCMTokenForSenderID:@"123456"
  156. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error){
  157. }];
  158. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  159. }
  160. - (void)testReturnsErrorWhenFetchingTokenWithoutSenderID {
  161. XCTestExpectation *expectation =
  162. [self expectationWithDescription:@"Returned an error fetching token without Sender ID"];
  163. #pragma clang diagnostic push
  164. #pragma clang diagnostic ignored "-Wnonnull"
  165. [self.messaging
  166. retrieveFCMTokenForSenderID:nil
  167. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  168. if (error != nil) {
  169. [expectation fulfill];
  170. }
  171. }];
  172. #pragma clang diagnostic pop
  173. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  174. }
  175. - (void)testReturnsErrorWhenFetchingTokenWithoutAPNSToken {
  176. XCTestExpectation *expectation =
  177. [self expectationWithDescription:@"Returned an error fetching token without APNS Token"];
  178. #pragma clang diagnostic push
  179. #pragma clang diagnostic ignored "-Wnonnull"
  180. [self.messaging
  181. retrieveFCMTokenForSenderID:@"12345"
  182. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  183. if (error != nil) {
  184. [expectation fulfill];
  185. }
  186. }];
  187. #pragma clang diagnostic pop
  188. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  189. }
  190. - (void)testReturnsErrorWhenFetchingTokenWithEmptySenderID {
  191. XCTestExpectation *expectation =
  192. [self expectationWithDescription:@"Returned an error fetching token with empty Sender ID"];
  193. [self.messaging
  194. retrieveFCMTokenForSenderID:@""
  195. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  196. if (error != nil) {
  197. [expectation fulfill];
  198. }
  199. }];
  200. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  201. }
  202. - (void)testReturnsErrorWhenDeletingTokenWithoutSenderID {
  203. XCTestExpectation *expectation =
  204. [self expectationWithDescription:@"Returned an error deleting token without Sender ID"];
  205. #pragma clang diagnostic push
  206. #pragma clang diagnostic ignored "-Wnonnull"
  207. [self.messaging deleteFCMTokenForSenderID:nil
  208. completion:^(NSError *_Nullable error) {
  209. if (error != nil) {
  210. [expectation fulfill];
  211. }
  212. }];
  213. #pragma clang diagnostic pop
  214. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  215. }
  216. - (void)testReturnsErrorWhenDeletingTokenWithEmptySenderID {
  217. XCTestExpectation *expectation =
  218. [self expectationWithDescription:@"Returned an error deleting token with empty Sender ID"];
  219. [self.messaging deleteFCMTokenForSenderID:@""
  220. completion:^(NSError *_Nullable error) {
  221. if (error != nil) {
  222. [expectation fulfill];
  223. }
  224. }];
  225. [self waitForExpectationsWithTimeout:0.1 handler:nil];
  226. }
  227. @end