FIRMessagingTest.m 10 KB

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