FIRMessagingPubSubTest.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import <GoogleUtilities/GULReachabilityChecker.h>
  19. #import "Example/Messaging/Tests/FIRMessagingTestUtilities.h"
  20. #import "Firebase/Messaging/FIRMessagingClient.h"
  21. #import "Firebase/Messaging/FIRMessagingPendingTopicsList.h"
  22. #import "Firebase/Messaging/FIRMessagingPubSub.h"
  23. #import "Firebase/Messaging/FIRMessagingRmqManager.h"
  24. @interface FIRMessagingPubSub (ExposedForTest)
  25. @property(nonatomic, readwrite, strong) FIRMessagingPendingTopicsList *pendingTopicUpdates;
  26. - (void)archivePendingTopicsList:(FIRMessagingPendingTopicsList *)topicsList;
  27. - (void)restorePendingTopicsList;
  28. @end
  29. @interface FIRMessagingPendingTopicsList (ExposedForTest)
  30. @property(nonatomic, strong) NSMutableArray<FIRMessagingTopicBatch *> *topicBatches;
  31. @end
  32. @interface FIRMessagingPubSubTest : XCTestCase
  33. @end
  34. @implementation FIRMessagingPubSubTest
  35. static NSString *const kTopicName = @"topic-Name";
  36. #pragma mark - topicMatchForSender tests
  37. /// Tests that an empty topic name is an invalid topic.
  38. - (void)testTopicMatchForEmptyTopicPrefix {
  39. XCTAssertFalse([FIRMessagingPubSub isValidTopicWithPrefix:@""]);
  40. }
  41. /// Tests that a topic with an invalid prefix is not a valid topic name.
  42. - (void)testTopicMatchWithInvalidTopicPrefix {
  43. XCTAssertFalse([FIRMessagingPubSub isValidTopicWithPrefix:@"/topics+abcdef/"]);
  44. }
  45. /// Tests that a topic with a valid prefix but invalid name is not a valid topic name.
  46. - (void)testTopicMatchWithValidTopicPrefixButInvalidName {
  47. XCTAssertFalse([FIRMessagingPubSub isValidTopicWithPrefix:@"/topics/aaaaaa/topics/lala"]);
  48. }
  49. /// Tests that multiple backslashes in topics is an invalid topic name.
  50. - (void)testTopicMatchForInvalidTopicPrefix_multipleBackslash {
  51. XCTAssertFalse([FIRMessagingPubSub isValidTopicWithPrefix:@"/topics//abc"]);
  52. }
  53. /// Tests a topic name with a valid prefix and name.
  54. - (void)testTopicMatchForValidTopicSender {
  55. NSString *topic = [NSString stringWithFormat:@"/topics/%@", kTopicName];
  56. XCTAssertTrue([FIRMessagingPubSub isValidTopicWithPrefix:topic]);
  57. }
  58. /// Tests topic prefix for topics with no prefix.
  59. - (void)testTopicHasNoTopicPrefix {
  60. XCTAssertFalse([FIRMessagingPubSub hasTopicsPrefix:@""]);
  61. }
  62. /// Tests topic prefix for valid prefix.
  63. - (void)testTopicHasValidToicsPrefix {
  64. XCTAssertTrue([FIRMessagingPubSub hasTopicsPrefix:@"/topics/"]);
  65. }
  66. /// Tests topic prefix wih no prefix.
  67. - (void)testAddTopicPrefix_withNoPrefix {
  68. NSString *topic = [FIRMessagingPubSub addPrefixToTopic:@""];
  69. XCTAssertTrue([FIRMessagingPubSub hasTopicsPrefix:topic]);
  70. XCTAssertFalse([FIRMessagingPubSub isValidTopicWithPrefix:topic]);
  71. }
  72. /// Tests adding the "/topics/" prefix for topic name which already has a prefix.
  73. - (void)testAddTopicPrefix_withPrefix {
  74. NSString *topic = [NSString stringWithFormat:@"/topics/%@", kTopicName];
  75. topic = [FIRMessagingPubSub addPrefixToTopic:topic];
  76. XCTAssertTrue([FIRMessagingPubSub hasTopicsPrefix:topic]);
  77. XCTAssertTrue([FIRMessagingPubSub isValidTopicWithPrefix:topic]);
  78. }
  79. - (void)testRemoveTopicPrefix {
  80. NSString *topic = [NSString stringWithFormat:@"/topics/%@", kTopicName];
  81. topic = [FIRMessagingPubSub removePrefixFromTopic:topic];
  82. XCTAssertEqualObjects(topic, kTopicName);
  83. // if the topic doesn't have the prefix, should return topic itself.
  84. topic = [FIRMessagingPubSub removePrefixFromTopic:kTopicName];
  85. XCTAssertEqualObjects(topic, kTopicName);
  86. }
  87. - (void)testTopicListArchive {
  88. MockPendingTopicsListDelegate *notReadyDelegate = [[MockPendingTopicsListDelegate alloc] init];
  89. notReadyDelegate.isReady = NO;
  90. FIRMessagingPendingTopicsList *topicList = [[FIRMessagingPendingTopicsList alloc] init];
  91. topicList.delegate = notReadyDelegate;
  92. // There should be 3 batches as actions are different than the last ones.
  93. [topicList addOperationForTopic:@"/topics/0"
  94. withAction:FIRMessagingTopicActionSubscribe
  95. completion:nil];
  96. [topicList addOperationForTopic:@"/topics/1"
  97. withAction:FIRMessagingTopicActionUnsubscribe
  98. completion:nil];
  99. [topicList addOperationForTopic:@"/topics/2"
  100. withAction:FIRMessagingTopicActionSubscribe
  101. completion:nil];
  102. XCTAssertEqual(topicList.numberOfBatches, 3);
  103. id mockClientDelegate = OCMStrictProtocolMock(@protocol(FIRMessagingClientDelegate));
  104. id mockReachability = OCMClassMock([GULReachabilityChecker class]);
  105. id mockRmqManager = OCMClassMock([FIRMessagingRmqManager class]);
  106. FIRMessagingClient *client = [[FIRMessagingClient alloc] initWithDelegate:mockClientDelegate
  107. reachability:mockReachability
  108. rmq2Manager:mockRmqManager];
  109. FIRMessagingPubSub *pubSub = [[FIRMessagingPubSub alloc] initWithClient:client];
  110. [pubSub archivePendingTopicsList:topicList];
  111. [pubSub restorePendingTopicsList];
  112. XCTAssertEqual(pubSub.pendingTopicUpdates.numberOfBatches, 3);
  113. NSMutableArray<FIRMessagingTopicBatch *> *topicBatches = pubSub.pendingTopicUpdates.topicBatches;
  114. XCTAssertTrue([topicBatches[0].topics containsObject:@"/topics/0"]);
  115. XCTAssertTrue([topicBatches[1].topics containsObject:@"/topics/1"]);
  116. XCTAssertTrue([topicBatches[2].topics containsObject:@"/topics/2"]);
  117. }
  118. @end