FIRMessagingPubSubTest.m 5.3 KB

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