FIRMessagingServiceTest.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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;
  17. @import XCTest;
  18. #import <OCMock/OCMock.h>
  19. #import "FIRMessaging.h"
  20. #import "FIRMessagingClient.h"
  21. #import "FIRMessagingPubSub.h"
  22. #import "FIRMessagingTopicsCommon.h"
  23. #import "InternalHeaders/FIRMessagingInternalUtilities.h"
  24. #import "NSError+FIRMessaging.h"
  25. @interface FIRMessaging () <FIRMessagingClientDelegate>
  26. @property(nonatomic, readwrite, strong) FIRMessagingClient *client;
  27. @property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
  28. @property(nonatomic, readwrite, strong) NSString *defaultFcmToken;
  29. @end
  30. @interface FIRMessagingPubSub ()
  31. @property(nonatomic, readwrite, strong) FIRMessagingClient *client;
  32. @end
  33. @interface FIRMessagingServiceTest : XCTestCase
  34. @end
  35. @implementation FIRMessagingServiceTest
  36. - (void)testSubscribe {
  37. id mockClient = OCMClassMock([FIRMessagingClient class]);
  38. FIRMessaging *service = [FIRMessaging messaging];
  39. [service setClient:mockClient];
  40. [service.pubsub setClient:mockClient];
  41. XCTestExpectation *subscribeExpectation =
  42. [self expectationWithDescription:@"Should call subscribe on FIRMessagingClient"];
  43. NSString *token = @"abcdefghijklmn";
  44. NSString *topic = @"/topics/some-random-topic";
  45. [[[mockClient stub]
  46. andDo:^(NSInvocation *invocation) {
  47. [subscribeExpectation fulfill];
  48. }]
  49. updateSubscriptionWithToken:token
  50. topic:topic
  51. options:OCMOCK_ANY
  52. shouldDelete:NO
  53. handler:OCMOCK_ANY];
  54. [service.pubsub subscribeWithToken:token
  55. topic:topic
  56. options:nil
  57. handler:^(FIRMessagingTopicOperationResult result, NSError *error) {
  58. // not a nil block
  59. }];
  60. // should call updateSubscription
  61. [self waitForExpectationsWithTimeout:0.1
  62. handler:^(NSError *error) {
  63. XCTAssertNil(error);
  64. [mockClient verify];
  65. }];
  66. }
  67. - (void)testUnsubscribe {
  68. id mockClient = OCMClassMock([FIRMessagingClient class]);
  69. FIRMessaging *messaging = [FIRMessaging messaging];
  70. [messaging setClient:mockClient];
  71. [messaging.pubsub setClient:mockClient];
  72. XCTestExpectation *subscribeExpectation =
  73. [self expectationWithDescription:@"Should call unsubscribe on FIRMessagingClient"];
  74. NSString *token = @"abcdefghijklmn";
  75. NSString *topic = @"/topics/some-random-topic";
  76. [[[mockClient stub] andDo:^(NSInvocation *invocation) {
  77. [subscribeExpectation fulfill];
  78. }]
  79. updateSubscriptionWithToken:[OCMArg isEqual:token]
  80. topic:[OCMArg isEqual:topic]
  81. options:[OCMArg checkWithBlock:^BOOL(id obj) {
  82. if ([obj isKindOfClass:[NSDictionary class]]) {
  83. return [(NSDictionary *)obj count] == 0;
  84. }
  85. return NO;
  86. }]
  87. shouldDelete:YES
  88. handler:OCMOCK_ANY];
  89. [messaging.pubsub unsubscribeWithToken:token
  90. topic:topic
  91. options:nil
  92. handler:^(FIRMessagingTopicOperationResult result, NSError *error){
  93. }];
  94. // should call updateSubscription
  95. [self waitForExpectationsWithTimeout:0.1
  96. handler:^(NSError *error) {
  97. XCTAssertNil(error);
  98. [mockClient verify];
  99. }];
  100. }
  101. /**
  102. * Test using PubSub without explicitly starting FIRMessagingService.
  103. */
  104. - (void)testSubscribeWithoutStart {
  105. [[[FIRMessaging messaging] pubsub] subscribeWithToken:@"abcdef1234"
  106. topic:@"/topics/hello-world"
  107. options:nil
  108. handler:
  109. ^(FIRMessagingTopicOperationResult result, NSError *error) {
  110. XCTAssertNil(error);
  111. XCTAssertEqual(kFIRMessagingErrorCodePubSubFIRMessagingNotSetup,
  112. error.code);
  113. }];
  114. }
  115. // TODO(chliangGoogle) Investigate why invalid token can't throw assertion but the rest can under
  116. // release build.
  117. - (void)testSubscribeWithInvalidTopic {
  118. FIRMessaging *messaging = [FIRMessaging messaging];
  119. XCTestExpectation *exceptionExpectation =
  120. [self expectationWithDescription:@"Should throw exception for invalid token"];
  121. @try {
  122. [messaging.pubsub subscribeWithToken:@"abcdef1234"
  123. topic:nil
  124. options:nil
  125. handler:
  126. ^(FIRMessagingTopicOperationResult result, NSError *error) {
  127. XCTFail(@"Should not invoke the handler");
  128. }];
  129. }
  130. @catch (NSException *exception) {
  131. [exceptionExpectation fulfill];
  132. }
  133. @finally {
  134. [self waitForExpectationsWithTimeout:0.1 handler:^(NSError *error) {
  135. XCTAssertNil(error);
  136. }];
  137. }
  138. }
  139. - (void)testUnsubscribeWithInvalidTopic {
  140. FIRMessaging *messaging = [FIRMessaging messaging];
  141. XCTestExpectation *exceptionExpectation =
  142. [self expectationWithDescription:@"Should throw exception for invalid token"];
  143. @try {
  144. [messaging.pubsub unsubscribeWithToken:@"abcdef1234"
  145. topic:nil
  146. options:nil
  147. handler:
  148. ^(FIRMessagingTopicOperationResult result, NSError *error) {
  149. XCTFail(@"Should not invoke the handler");
  150. }];
  151. }
  152. @catch (NSException *exception) {
  153. [exceptionExpectation fulfill];
  154. }
  155. @finally {
  156. [self waitForExpectationsWithTimeout:0.1 handler:^(NSError *error) {
  157. XCTAssertNil(error);
  158. }];
  159. }
  160. }
  161. - (void)testSubscribeWithNoTopicPrefix {
  162. FIRMessaging *messaging = [FIRMessaging messaging];
  163. FIRMessagingPubSub *pubSub = messaging.pubsub;
  164. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  165. NSString *topicName = @"topicWithoutPrefix";
  166. NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
  167. messaging.pubsub = mockPubSub;
  168. messaging.defaultFcmToken = @"fake-default-token";
  169. OCMExpect([messaging.pubsub subscribeToTopic:[OCMArg isEqual:topicNameWithPrefix]
  170. handler:[OCMArg any]]);
  171. [messaging subscribeToTopic:topicName];
  172. OCMVerifyAll(mockPubSub);
  173. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  174. messaging.pubsub = pubSub;
  175. }
  176. - (void)testSubscribeWithTopicPrefix {
  177. FIRMessaging *messaging = [FIRMessaging messaging];
  178. FIRMessagingPubSub *pubSub = messaging.pubsub;
  179. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  180. NSString *topicName = @"/topics/topicWithoutPrefix";
  181. messaging.pubsub = mockPubSub;
  182. messaging.defaultFcmToken = @"fake-default-token";
  183. OCMExpect([messaging.pubsub subscribeToTopic:[OCMArg isEqual:topicName] handler:[OCMArg any]]);
  184. [messaging subscribeToTopic:topicName];
  185. OCMVerifyAll(mockPubSub);
  186. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  187. messaging.pubsub = pubSub;
  188. }
  189. - (void)testUnsubscribeWithNoTopicPrefix {
  190. FIRMessaging *messaging = [FIRMessaging messaging];
  191. FIRMessagingPubSub *pubSub = messaging.pubsub;
  192. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  193. NSString *topicName = @"topicWithoutPrefix";
  194. NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
  195. messaging.pubsub = mockPubSub;
  196. messaging.defaultFcmToken = @"fake-default-token";
  197. OCMExpect([messaging.pubsub unsubscribeFromTopic:[OCMArg isEqual:topicNameWithPrefix]
  198. handler:[OCMArg any]]);
  199. [messaging unsubscribeFromTopic:topicName];
  200. OCMVerifyAll(mockPubSub);
  201. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  202. messaging.pubsub = pubSub;
  203. }
  204. - (void)testUnsubscribeWithTopicPrefix {
  205. FIRMessaging *messaging = [FIRMessaging messaging];
  206. FIRMessagingPubSub *pubSub = messaging.pubsub;
  207. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  208. NSString *topicName = @"/topics/topicWithPrefix";
  209. messaging.pubsub = mockPubSub;
  210. messaging.defaultFcmToken = @"fake-default-token";
  211. OCMExpect([messaging.pubsub unsubscribeFromTopic:[OCMArg isEqual:topicName]
  212. handler:[OCMArg any]]);
  213. [messaging unsubscribeFromTopic:topicName];
  214. OCMVerifyAll(mockPubSub);
  215. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  216. messaging.pubsub = pubSub;
  217. }
  218. - (void)testFIRMessagingSDKVersionInFIRMessagingService {
  219. Class versionClass = NSClassFromString(kFIRMessagingSDKClassString);
  220. SEL versionSelector = NSSelectorFromString(kFIRMessagingSDKVersionSelectorString);
  221. if ([versionClass respondsToSelector:versionSelector]) {
  222. #pragma clang diagnostic push
  223. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  224. id versionString = [versionClass performSelector:versionSelector];
  225. #pragma clang diagnostic pop
  226. XCTAssertTrue([versionString isKindOfClass:[NSString class]]);
  227. } else {
  228. XCTFail("%@ does not respond to selector %@",
  229. kFIRMessagingSDKClassString, kFIRMessagingSDKVersionSelectorString);
  230. }
  231. }
  232. - (void)testFIRMessagingSDKLocaleInFIRMessagingService {
  233. Class klass = NSClassFromString(kFIRMessagingSDKClassString);
  234. SEL localeSelector = NSSelectorFromString(kFIRMessagingSDKLocaleSelectorString);
  235. if ([klass respondsToSelector:localeSelector]) {
  236. #pragma clang diagnostic push
  237. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  238. id locale = [klass performSelector:localeSelector];
  239. #pragma clang diagnostic pop
  240. XCTAssertTrue([locale isKindOfClass:[NSString class]]);
  241. XCTAssertNotNil(locale);
  242. } else {
  243. XCTFail("%@ does not respond to selector %@",
  244. kFIRMessagingSDKClassString, kFIRMessagingSDKLocaleSelectorString);
  245. }
  246. }
  247. @end