FIRMessagingServiceTest.m 10 KB

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