FIRMessagingServiceTest.m 12 KB

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