FIRMessagingServiceTest.m 14 KB

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