FIRMessagingServiceTest.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. [messaging subscribeToTopic:topicName];
  171. OCMVerifyAll(mockPubSub);
  172. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  173. messaging.pubsub = pubSub;
  174. }
  175. - (void)testSubscribeWithTopicPrefix {
  176. FIRMessaging *messaging = [FIRMessaging messaging];
  177. FIRMessagingPubSub *pubSub = messaging.pubsub;
  178. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  179. NSString *topicName = @"/topics/topicWithoutPrefix";
  180. messaging.pubsub = mockPubSub;
  181. messaging.defaultFcmToken = @"fake-default-token";
  182. OCMExpect([messaging.pubsub subscribeToTopic:[OCMArg isEqual:topicName]]);
  183. [messaging subscribeToTopic:topicName];
  184. OCMVerifyAll(mockPubSub);
  185. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  186. messaging.pubsub = pubSub;
  187. }
  188. - (void)testUnsubscribeWithNoTopicPrefix {
  189. FIRMessaging *messaging = [FIRMessaging messaging];
  190. FIRMessagingPubSub *pubSub = messaging.pubsub;
  191. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  192. NSString *topicName = @"topicWithoutPrefix";
  193. NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
  194. messaging.pubsub = mockPubSub;
  195. messaging.defaultFcmToken = @"fake-default-token";
  196. OCMExpect([messaging.pubsub unsubscribeFromTopic:[OCMArg isEqual:topicNameWithPrefix]]);
  197. [messaging unsubscribeFromTopic:topicName];
  198. OCMVerifyAll(mockPubSub);
  199. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  200. messaging.pubsub = pubSub;
  201. }
  202. - (void)testUnsubscribeWithTopicPrefix {
  203. FIRMessaging *messaging = [FIRMessaging messaging];
  204. FIRMessagingPubSub *pubSub = messaging.pubsub;
  205. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  206. NSString *topicName = @"/topics/topicWithPrefix";
  207. messaging.pubsub = mockPubSub;
  208. messaging.defaultFcmToken = @"fake-default-token";
  209. OCMExpect([messaging.pubsub unsubscribeFromTopic:[OCMArg isEqual:topicName]]);
  210. [messaging unsubscribeFromTopic:topicName];
  211. OCMVerifyAll(mockPubSub);
  212. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  213. messaging.pubsub = pubSub;
  214. }
  215. - (void)testFIRMessagingSDKVersionInFIRMessagingService {
  216. Class versionClass = NSClassFromString(kFIRMessagingSDKClassString);
  217. SEL versionSelector = NSSelectorFromString(kFIRMessagingSDKVersionSelectorString);
  218. if ([versionClass respondsToSelector:versionSelector]) {
  219. #pragma clang diagnostic push
  220. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  221. id versionString = [versionClass performSelector:versionSelector];
  222. #pragma clang diagnostic pop
  223. XCTAssertTrue([versionString isKindOfClass:[NSString class]]);
  224. } else {
  225. XCTFail("%@ does not respond to selector %@",
  226. kFIRMessagingSDKClassString, kFIRMessagingSDKVersionSelectorString);
  227. }
  228. }
  229. - (void)testFIRMessagingSDKLocaleInFIRMessagingService {
  230. Class klass = NSClassFromString(kFIRMessagingSDKClassString);
  231. SEL localeSelector = NSSelectorFromString(kFIRMessagingSDKLocaleSelectorString);
  232. if ([klass respondsToSelector:localeSelector]) {
  233. #pragma clang diagnostic push
  234. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  235. id locale = [klass performSelector:localeSelector];
  236. #pragma clang diagnostic pop
  237. XCTAssertTrue([locale isKindOfClass:[NSString class]]);
  238. XCTAssertNotNil(locale);
  239. } else {
  240. XCTFail("%@ does not respond to selector %@",
  241. kFIRMessagingSDKClassString, kFIRMessagingSDKLocaleSelectorString);
  242. }
  243. }
  244. @end