FIRMessagingServiceTest.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. - (void)testSubscribeWithInvalidToken {
  116. FIRMessaging *messaging = [FIRMessaging messaging];
  117. XCTestExpectation *exceptionExpectation =
  118. [self expectationWithDescription:@"Should throw exception for invalid token"];
  119. @try {
  120. [messaging.pubsub subscribeWithToken:@""
  121. topic:@"/topics/hello-world"
  122. options:nil
  123. handler:
  124. ^(FIRMessagingTopicOperationResult result, NSError *error) {
  125. XCTFail(@"Should not invoke the handler");
  126. }];
  127. }
  128. @catch (NSException *exception) {
  129. [exceptionExpectation fulfill];
  130. }
  131. @finally {
  132. [self waitForExpectationsWithTimeout:0.1 handler:^(NSError *error) {
  133. XCTAssertNil(error);
  134. }];
  135. }
  136. }
  137. - (void)testUnsubscribeWithInvalidTopic {
  138. FIRMessaging *messaging = [FIRMessaging messaging];
  139. XCTestExpectation *exceptionExpectation =
  140. [self expectationWithDescription:@"Should throw exception for invalid token"];
  141. @try {
  142. [messaging.pubsub unsubscribeWithToken:@"abcdef1234"
  143. topic:nil
  144. options:nil
  145. handler:
  146. ^(FIRMessagingTopicOperationResult result, NSError *error) {
  147. XCTFail(@"Should not invoke the handler");
  148. }];
  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)testSubscribeWithNoTopicPrefix {
  160. FIRMessaging *messaging = [FIRMessaging messaging];
  161. FIRMessagingPubSub *pubSub = messaging.pubsub;
  162. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  163. NSString *topicName = @"topicWithoutPrefix";
  164. NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
  165. messaging.pubsub = mockPubSub;
  166. messaging.defaultFcmToken = @"fake-default-token";
  167. OCMExpect([messaging.pubsub subscribeToTopic:[OCMArg isEqual:topicNameWithPrefix]]);
  168. [messaging subscribeToTopic:topicName];
  169. OCMVerifyAll(mockPubSub);
  170. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  171. messaging.pubsub = pubSub;
  172. }
  173. - (void)testSubscribeWithTopicPrefix {
  174. FIRMessaging *messaging = [FIRMessaging messaging];
  175. FIRMessagingPubSub *pubSub = messaging.pubsub;
  176. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  177. NSString *topicName = @"/topics/topicWithoutPrefix";
  178. messaging.pubsub = mockPubSub;
  179. messaging.defaultFcmToken = @"fake-default-token";
  180. OCMExpect([messaging.pubsub subscribeToTopic:[OCMArg isEqual:topicName]]);
  181. [messaging subscribeToTopic:topicName];
  182. OCMVerifyAll(mockPubSub);
  183. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  184. messaging.pubsub = pubSub;
  185. }
  186. - (void)testUnsubscribeWithNoTopicPrefix {
  187. FIRMessaging *messaging = [FIRMessaging messaging];
  188. FIRMessagingPubSub *pubSub = messaging.pubsub;
  189. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  190. NSString *topicName = @"topicWithoutPrefix";
  191. NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
  192. messaging.pubsub = mockPubSub;
  193. messaging.defaultFcmToken = @"fake-default-token";
  194. OCMExpect([messaging.pubsub unsubscribeFromTopic:[OCMArg isEqual:topicNameWithPrefix]]);
  195. [messaging unsubscribeFromTopic:topicName];
  196. OCMVerifyAll(mockPubSub);
  197. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  198. messaging.pubsub = pubSub;
  199. }
  200. - (void)testUnsubscribeWithTopicPrefix {
  201. FIRMessaging *messaging = [FIRMessaging messaging];
  202. FIRMessagingPubSub *pubSub = messaging.pubsub;
  203. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  204. NSString *topicName = @"/topics/topicWithPrefix";
  205. messaging.pubsub = mockPubSub;
  206. messaging.defaultFcmToken = @"fake-default-token";
  207. OCMExpect([messaging.pubsub unsubscribeFromTopic:[OCMArg isEqual:topicName]]);
  208. [messaging unsubscribeFromTopic:topicName];
  209. OCMVerifyAll(mockPubSub);
  210. // Need to swap back since it's a singleton and hence will live beyond the scope of this test.
  211. messaging.pubsub = pubSub;
  212. }
  213. - (void)testFIRMessagingSDKVersionInFIRMessagingService {
  214. Class versionClass = NSClassFromString(kFIRMessagingSDKClassString);
  215. SEL versionSelector = NSSelectorFromString(kFIRMessagingSDKVersionSelectorString);
  216. if ([versionClass respondsToSelector:versionSelector]) {
  217. #pragma clang diagnostic push
  218. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  219. id versionString = [versionClass performSelector:versionSelector];
  220. #pragma clang diagnostic pop
  221. XCTAssertTrue([versionString isKindOfClass:[NSString class]]);
  222. } else {
  223. XCTFail("%@ does not respond to selector %@",
  224. kFIRMessagingSDKClassString, kFIRMessagingSDKVersionSelectorString);
  225. }
  226. }
  227. - (void)testFIRMessagingSDKLocaleInFIRMessagingService {
  228. Class klass = NSClassFromString(kFIRMessagingSDKClassString);
  229. SEL localeSelector = NSSelectorFromString(kFIRMessagingSDKLocaleSelectorString);
  230. if ([klass respondsToSelector:localeSelector]) {
  231. #pragma clang diagnostic push
  232. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  233. id locale = [klass performSelector:localeSelector];
  234. #pragma clang diagnostic pop
  235. XCTAssertTrue([locale isKindOfClass:[NSString class]]);
  236. XCTAssertNotNil(locale);
  237. } else {
  238. XCTFail("%@ does not respond to selector %@",
  239. kFIRMessagingSDKClassString, kFIRMessagingSDKLocaleSelectorString);
  240. }
  241. }
  242. @end