FIRMessagingServiceTest.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "FirebaseMessaging/Sources/FIRMessagingPubSub.h"
  21. #import "FirebaseMessaging/Sources/FIRMessagingTopicsCommon.h"
  22. #import "FirebaseMessaging/Sources/NSError+FIRMessaging.h"
  23. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  24. #import "FirebaseMessaging/Tests/UnitTests/FIRMessagingTestUtilities.h"
  25. static NSString *const kFakeToken =
  26. @"fE1e1PZJFSQ:APA91bFAOjp1ahBWn9rTlbjArwBEm_"
  27. @"yUTTzK6dhIvLqzqqCSabaa4TQVM0pGTmF6r7tmMHPe6VYiGMHuCwJFgj5v97xl78sUNMLwuPPhoci8z_"
  28. @"QGlCrTbxCFGzEUfvA3fGpGgIVQU2W6";
  29. static NSString *const kFakeID = @"fE1e1PZJFSQ";
  30. static NSString *const kFIRMessagingSDKClassString = @"FIRMessaging";
  31. static NSString *const kFIRMessagingSDKVersionSelectorString = @"FIRMessagingSDKVersion";
  32. static NSString *const kFIRMessagingSDKLocaleSelectorString = @"FIRMessagingSDKCurrentLocale";
  33. static NSString *const kFIRMessagingTestsServiceSuiteName = @"com.messaging.test_serviceTest";
  34. @interface FIRMessaging ()
  35. @property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
  36. @property(nonatomic, readwrite, strong) NSString *defaultFcmToken;
  37. #pragma clang diagnostic push
  38. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  39. @property(nonatomic, readwrite, strong) FIRInstanceID *instanceID;
  40. @end
  41. @interface FIRMessagingPubSub (ExposedForTest)
  42. - (void)updateSubscriptionWithToken:(NSString *)token
  43. topic:(NSString *)topic
  44. options:(NSDictionary *)options
  45. shouldDelete:(BOOL)shouldDelete
  46. handler:(FIRMessagingTopicOperationCompletion)handler;
  47. @end
  48. @interface FIRInstanceIDResult (ExposedForTest)
  49. @property(nonatomic, readwrite, copy) NSString *instanceID;
  50. @property(nonatomic, readwrite, copy) NSString *token;
  51. @end
  52. @interface FIRMessagingServiceTest : XCTestCase {
  53. FIRMessaging *_messaging;
  54. FIRInstanceIDResult *_result;
  55. id _mockInstanceID;
  56. id _mockMessaging;
  57. id _mockPubSub;
  58. FIRMessagingTestUtilities *_testUtil;
  59. }
  60. @end
  61. @implementation FIRMessagingServiceTest
  62. - (void)setUp {
  63. [super setUp];
  64. NSUserDefaults *defaults =
  65. [[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingTestsServiceSuiteName];
  66. _testUtil = [[FIRMessagingTestUtilities alloc] initWithUserDefaults:defaults withRMQManager:NO];
  67. _mockMessaging = _testUtil.mockMessaging;
  68. _messaging = _testUtil.messaging;
  69. OCMStub([_mockMessaging defaultFcmToken]).andReturn(kFakeToken);
  70. _mockPubSub = _testUtil.mockPubsub;
  71. _mockInstanceID = _testUtil.mockInstanceID;
  72. _result = [[FIRInstanceIDResult alloc] init];
  73. _result.token = kFakeToken;
  74. _result.instanceID = kFakeID;
  75. }
  76. - (void)tearDown {
  77. [_testUtil cleanupAfterTest:self];
  78. _messaging = nil;
  79. [_mockPubSub stopMocking];
  80. [[[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingTestsServiceSuiteName]
  81. removePersistentDomainForName:kFIRMessagingTestsServiceSuiteName];
  82. [super tearDown];
  83. }
  84. - (void)testSubscribe {
  85. XCTestExpectation *subscribeExpectation =
  86. [self expectationWithDescription:@"Should call subscribe on Pubsub"];
  87. NSString *token = kFakeToken;
  88. NSString *topic = @"/topics/some-random-topic";
  89. [[[_mockPubSub stub] andDo:^(NSInvocation *invocation) {
  90. [subscribeExpectation fulfill];
  91. }] updateSubscriptionWithToken:token
  92. topic:topic
  93. options:OCMOCK_ANY
  94. shouldDelete:NO
  95. handler:OCMOCK_ANY];
  96. [_mockPubSub subscribeWithToken:token
  97. topic:topic
  98. options:nil
  99. handler:^(NSError *error){
  100. // not a nil block
  101. }];
  102. // should call updateSubscription
  103. [self waitForExpectationsWithTimeout:0.1
  104. handler:^(NSError *error) {
  105. XCTAssertNil(error);
  106. [_mockPubSub verify];
  107. }];
  108. }
  109. - (void)testUnsubscribe {
  110. XCTestExpectation *subscribeExpectation =
  111. [self expectationWithDescription:@"Should call unsubscribe on Pubsub"];
  112. NSString *token = kFakeToken;
  113. NSString *topic = @"/topics/some-random-topic";
  114. [[[_mockPubSub stub] andDo:^(NSInvocation *invocation) {
  115. [subscribeExpectation fulfill];
  116. }] updateSubscriptionWithToken:[OCMArg isEqual:token]
  117. topic:[OCMArg isEqual:topic]
  118. options:[OCMArg checkWithBlock:^BOOL(id obj) {
  119. if ([obj isKindOfClass:[NSDictionary class]]) {
  120. return [(NSDictionary *)obj count] == 0;
  121. }
  122. return NO;
  123. }]
  124. shouldDelete:YES
  125. handler:OCMOCK_ANY];
  126. [_mockPubSub unsubscribeWithToken:token
  127. topic:topic
  128. options:nil
  129. handler:^(NSError *error){
  130. }];
  131. // should call updateSubscription
  132. [self waitForExpectationsWithTimeout:0.1
  133. handler:^(NSError *error) {
  134. XCTAssertNil(error);
  135. [_mockPubSub verify];
  136. }];
  137. }
  138. - (void)testSubscribeWithNoTopicPrefix {
  139. OCMStub([_mockInstanceID
  140. instanceIDWithHandler:([OCMArg invokeBlockWithArgs:_result, [NSNull null], nil])]);
  141. NSString *topicName = @"topicWithoutPrefix";
  142. NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
  143. OCMExpect([_mockPubSub subscribeToTopic:[OCMArg isEqual:topicNameWithPrefix]
  144. handler:[OCMArg any]]);
  145. [_messaging subscribeToTopic:topicName];
  146. OCMVerifyAll(_mockPubSub);
  147. }
  148. - (void)testSubscribeWithTopicPrefix {
  149. OCMStub([_mockInstanceID
  150. instanceIDWithHandler:([OCMArg invokeBlockWithArgs:_result, [NSNull null], nil])]);
  151. NSString *topicName = @"/topics/topicWithoutPrefix";
  152. OCMExpect([_mockPubSub subscribeToTopic:[OCMArg isEqual:topicName] handler:[OCMArg any]]);
  153. [_messaging subscribeToTopic:topicName];
  154. OCMVerifyAll(_mockPubSub);
  155. }
  156. - (void)testUnsubscribeWithNoTopicPrefix {
  157. OCMStub([_mockInstanceID
  158. instanceIDWithHandler:([OCMArg invokeBlockWithArgs:_result, [NSNull null], nil])]);
  159. NSString *topicName = @"topicWithoutPrefix";
  160. NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
  161. OCMExpect([_mockPubSub unsubscribeFromTopic:[OCMArg isEqual:topicNameWithPrefix]
  162. handler:[OCMArg any]]);
  163. [_messaging unsubscribeFromTopic:topicName];
  164. OCMVerifyAll(_mockPubSub);
  165. }
  166. - (void)testUnsubscribeWithTopicPrefix {
  167. OCMStub([_mockInstanceID
  168. instanceIDWithHandler:([OCMArg invokeBlockWithArgs:_result, [NSNull null], nil])]);
  169. NSString *topicName = @"/topics/topicWithPrefix";
  170. OCMExpect([_mockPubSub unsubscribeFromTopic:[OCMArg isEqual:topicName] handler:[OCMArg any]]);
  171. [_messaging unsubscribeFromTopic:topicName];
  172. OCMVerifyAll(_mockPubSub);
  173. }
  174. - (void)testSubscriptionCompletionHandlerWithSuccess {
  175. OCMStub([_mockInstanceID
  176. instanceIDWithHandler:([OCMArg invokeBlockWithArgs:_result, [NSNull null], nil])]);
  177. OCMStub([_mockPubSub subscribeToTopic:[OCMArg any]
  178. handler:([OCMArg invokeBlockWithArgs:[NSNull null], nil])]);
  179. XCTestExpectation *subscriptionCompletionExpectation =
  180. [self expectationWithDescription:@"Subscription is complete"];
  181. [_messaging subscribeToTopic:@"news"
  182. completion:^(NSError *error) {
  183. XCTAssertNil(error);
  184. [subscriptionCompletionExpectation fulfill];
  185. }];
  186. [self waitForExpectationsWithTimeout:0.2
  187. handler:^(NSError *_Nullable error){
  188. }];
  189. }
  190. - (void)testUnsubscribeCompletionHandlerWithSuccess {
  191. OCMStub([_mockInstanceID
  192. instanceIDWithHandler:([OCMArg invokeBlockWithArgs:_result, [NSNull null], nil])]);
  193. OCMStub([_mockPubSub unsubscribeFromTopic:[OCMArg any]
  194. handler:([OCMArg invokeBlockWithArgs:[NSNull null], nil])]);
  195. XCTestExpectation *unsubscriptionCompletionExpectation =
  196. [self expectationWithDescription:@"Unsubscription is complete"];
  197. [_messaging unsubscribeFromTopic:@"news"
  198. completion:^(NSError *_Nullable error) {
  199. XCTAssertNil(error);
  200. [unsubscriptionCompletionExpectation fulfill];
  201. }];
  202. [self waitForExpectationsWithTimeout:0.2
  203. handler:^(NSError *_Nullable error){
  204. }];
  205. }
  206. - (void)testSubscriptionCompletionHandlerWithInvalidTopicName {
  207. OCMStub([_mockInstanceID
  208. instanceIDWithHandler:([OCMArg invokeBlockWithArgs:_result, [NSNull null], nil])]);
  209. XCTestExpectation *subscriptionCompletionExpectation =
  210. [self expectationWithDescription:@"Subscription is complete"];
  211. [_messaging subscribeToTopic:@"!@#$%^&*()"
  212. completion:^(NSError *_Nullable error) {
  213. XCTAssertNotNil(error);
  214. XCTAssertEqual(error.code, FIRMessagingErrorInvalidTopicName);
  215. [subscriptionCompletionExpectation fulfill];
  216. }];
  217. [self waitForExpectationsWithTimeout:0.2
  218. handler:^(NSError *_Nullable error){
  219. }];
  220. }
  221. - (void)testUnsubscribeCompletionHandlerWithInvalidTopicName {
  222. OCMStub([_mockInstanceID
  223. instanceIDWithHandler:([OCMArg invokeBlockWithArgs:_result, [NSNull null], nil])]);
  224. XCTestExpectation *unsubscriptionCompletionExpectation =
  225. [self expectationWithDescription:@"Unsubscription is complete"];
  226. [_messaging unsubscribeFromTopic:@"!@#$%^&*()"
  227. completion:^(NSError *error) {
  228. XCTAssertNotNil(error);
  229. XCTAssertEqual(error.code, FIRMessagingErrorInvalidTopicName);
  230. [unsubscriptionCompletionExpectation fulfill];
  231. }];
  232. [self waitForExpectationsWithTimeout:0.2
  233. handler:^(NSError *_Nullable error){
  234. }];
  235. }
  236. - (void)testFIRMessagingSDKVersionInFIRMessagingService {
  237. Class versionClass = NSClassFromString(kFIRMessagingSDKClassString);
  238. SEL versionSelector = NSSelectorFromString(kFIRMessagingSDKVersionSelectorString);
  239. if ([versionClass respondsToSelector:versionSelector]) {
  240. #pragma clang diagnostic push
  241. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  242. id versionString = [versionClass performSelector:versionSelector];
  243. #pragma clang diagnostic pop
  244. XCTAssertTrue([versionString isKindOfClass:[NSString class]]);
  245. } else {
  246. XCTFail("%@ does not respond to selector %@", kFIRMessagingSDKClassString,
  247. kFIRMessagingSDKVersionSelectorString);
  248. }
  249. }
  250. - (void)testFIRMessagingSDKLocaleInFIRMessagingService {
  251. Class klass = NSClassFromString(kFIRMessagingSDKClassString);
  252. SEL localeSelector = NSSelectorFromString(kFIRMessagingSDKLocaleSelectorString);
  253. if ([klass respondsToSelector:localeSelector]) {
  254. #pragma clang diagnostic push
  255. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  256. id locale = [klass performSelector:localeSelector];
  257. #pragma clang diagnostic pop
  258. XCTAssertTrue([locale isKindOfClass:[NSString class]]);
  259. XCTAssertNotNil(locale);
  260. } else {
  261. XCTFail("%@ does not respond to selector %@", kFIRMessagingSDKClassString,
  262. kFIRMessagingSDKLocaleSelectorString);
  263. }
  264. }
  265. - (void)testSubscribeFailedWithInvalidToken {
  266. // Mock get token is failed with FIRMessagingErrorUnknown error.
  267. XCTestExpectation *subscriptionCompletionExpectation =
  268. [self expectationWithDescription:@"Subscription is complete"];
  269. NSString *failureReason = @"Invalid token.";
  270. OCMStub([_mockInstanceID
  271. instanceIDWithHandler:
  272. ([OCMArg invokeBlockWithArgs:[NSNull null],
  273. [NSError messagingErrorWithCode:kFIRMessagingErrorCodeUnknown
  274. failureReason:failureReason],
  275. nil])]);
  276. [_messaging subscribeToTopic:@"Apple"
  277. completion:^(NSError *_Nullable error) {
  278. XCTAssertNotNil(error);
  279. XCTAssertEqual(error.code, kFIRMessagingErrorCodeUnknown);
  280. XCTAssertEqualObjects(failureReason, error.localizedFailureReason);
  281. [subscriptionCompletionExpectation fulfill];
  282. }];
  283. [self waitForExpectationsWithTimeout:0.2 handler:nil];
  284. }
  285. - (void)testUnsubscribeFailedWithInvalidToken {
  286. NSString *failureReason = @"Invalid token.";
  287. OCMStub([_mockInstanceID
  288. instanceIDWithHandler:
  289. ([OCMArg invokeBlockWithArgs:[NSNull null],
  290. [NSError messagingErrorWithCode:kFIRMessagingErrorCodeUnknown
  291. failureReason:failureReason],
  292. nil])]);
  293. XCTestExpectation *unsubscriptionCompletionExpectation =
  294. [self expectationWithDescription:@"Unsubscription is complete"];
  295. [_messaging unsubscribeFromTopic:@"news"
  296. completion:^(NSError *_Nullable error) {
  297. XCTAssertNotNil(error);
  298. XCTAssertEqual(error.code, kFIRMessagingErrorCodeUnknown);
  299. XCTAssertEqualObjects(failureReason, error.localizedFailureReason);
  300. [unsubscriptionCompletionExpectation fulfill];
  301. }];
  302. [self waitForExpectationsWithTimeout:0.2 handler:nil];
  303. }
  304. #pragma clang diagnostic pop
  305. @end