FIRMessagingServiceTest.m 15 KB

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