FIRMessagingTest.m 10 KB

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