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/UIKit.h>
  17. #import <XCTest/XCTest.h>
  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:^(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:^(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]
  106. subscribeWithToken:@"abcdef1234"
  107. topic:@"/topics/hello-world"
  108. options:nil
  109. handler:^(NSError *error) {
  110. XCTAssertNil(error);
  111. XCTAssertEqual(kFIRMessagingErrorCodePubSubFIRMessagingNotSetup, error.code);
  112. }];
  113. }
  114. // TODO(chliangGoogle) Investigate why invalid token can't throw assertion but the rest can under
  115. // release build.
  116. - (void)testSubscribeWithInvalidTopic {
  117. FIRMessaging *messaging = [FIRMessaging messaging];
  118. XCTestExpectation *exceptionExpectation =
  119. [self expectationWithDescription:@"Should throw exception for invalid token"];
  120. @try {
  121. [messaging.pubsub subscribeWithToken:@"abcdef1234"
  122. topic:nil
  123. options:nil
  124. handler:^(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:^(NSError *error) {
  146. XCTFail(@"Should not invoke the handler");
  147. }];
  148. }
  149. @catch (NSException *exception) {
  150. [exceptionExpectation fulfill];
  151. }
  152. @finally {
  153. [self waitForExpectationsWithTimeout:0.1 handler:^(NSError *error) {
  154. XCTAssertNil(error);
  155. }];
  156. }
  157. }
  158. - (void)testSubscribeWithNoTopicPrefix {
  159. FIRMessaging *messaging = [FIRMessaging messaging];
  160. FIRMessagingPubSub *pubSub = messaging.pubsub;
  161. id mockPubSub = OCMClassMock([FIRMessagingPubSub class]);
  162. NSString *topicName = @"topicWithoutPrefix";
  163. NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
  164. messaging.pubsub = mockPubSub;
  165. messaging.defaultFcmToken = @"fake-default-token";
  166. OCMExpect([messaging.pubsub subscribeToTopic:[OCMArg isEqual:topicNameWithPrefix]
  167. handler:[OCMArg any]]);
  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] handler:[OCMArg any]]);
  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. handler:[OCMArg any]]);
  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. handler:[OCMArg any]]);
  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