FIRMessagingServiceTest.m 12 KB

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