FIRMessagingTestUtilities.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2019 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 <GoogleUtilities/GULUserDefaults.h>
  18. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  19. #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
  20. #import "FirebaseMessaging/Sources/FIRMessagingPubSub.h"
  21. #import "FirebaseMessaging/Sources/FIRMessagingRmqManager.h"
  22. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenManager.h"
  23. #import "FirebaseMessaging/Tests/UnitTests/FIRMessagingTestUtilities.h"
  24. #import "FirebaseMessaging/Tests/UnitTests/XCTestCase+FIRMessagingRmqManagerTests.h"
  25. #import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
  26. NS_ASSUME_NONNULL_BEGIN
  27. NSString *const kFIRMessagingDefaultsTestDomain = @"com.messaging.tests";
  28. static NSString *const kFakeSenderID = @"123456789123";
  29. @interface FIRMessaging (ExposedForTest)
  30. @property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
  31. @property(nonatomic, readwrite, strong) FIRMessagingRmqManager *rmq2Manager;
  32. @property(nonatomic, readwrite, strong) FIRMessagingTokenManager *tokenManager;
  33. /// Surface internal initializer to avoid singleton usage during tests.
  34. #pragma clang diagnostic push
  35. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  36. - (instancetype)initWithAnalytics:(nullable id<FIRAnalyticsInterop>)analytics
  37. withUserDefaults:(GULUserDefaults *)defaults;
  38. #pragma clang diagnostic pop
  39. /// Kicks off required calls for some messaging tests.
  40. - (void)start;
  41. - (void)setupRmqManager;
  42. @end
  43. @interface FIRMessagingRmqManager (ExposedForTest)
  44. - (void)removeDatabase;
  45. @end
  46. @implementation MockPendingTopicsListDelegate
  47. - (BOOL)pendingTopicsListCanRequestTopicUpdates:(FIRMessagingPendingTopicsList *)list {
  48. return self.isReady;
  49. }
  50. - (void)pendingTopicsList:(FIRMessagingPendingTopicsList *)list
  51. requestedUpdateForTopic:(NSString *)topic
  52. action:(FIRMessagingTopicAction)action
  53. completion:(FIRMessagingTopicOperationCompletion)completion {
  54. if (self.subscriptionHandler) {
  55. self.subscriptionHandler(topic, action, completion);
  56. }
  57. }
  58. - (void)pendingTopicsListDidUpdate:(FIRMessagingPendingTopicsList *)list {
  59. if (self.updateHandler) {
  60. self.updateHandler();
  61. }
  62. }
  63. @end
  64. @implementation FIRMessagingTestUtilities {
  65. id _mockMessagingClass;
  66. }
  67. - (instancetype)initWithUserDefaults:(GULUserDefaults *)userDefaults
  68. withRMQManager:(BOOL)withRMQManager {
  69. self = [super init];
  70. if (self) {
  71. // `+[FIRInstallations installations]` supposed to be used on `-[FIRInstanceID start]` to get
  72. // `FIRInstallations` default instance. Need to stub it before.
  73. _mockInstallations = OCMClassMock([FIRInstallations class]);
  74. OCMStub([self.mockInstallations installations]).andReturn(self.mockInstallations);
  75. // Create the messaging instance and call `start`.
  76. _messaging = [[FIRMessaging alloc] initWithAnalytics:nil withUserDefaults:userDefaults];
  77. if (withRMQManager) {
  78. [_messaging start];
  79. }
  80. _mockMessaging = OCMPartialMock(_messaging);
  81. if (!withRMQManager) {
  82. OCMStub([_mockMessaging setupRmqManager]).andDo(nil);
  83. [(FIRMessaging *)_mockMessaging start];
  84. }
  85. _mockPubsub = OCMPartialMock(_messaging.pubsub);
  86. _mockTokenManager = OCMPartialMock(_messaging.tokenManager);
  87. _mockMessagingClass = OCMClassMock([FIRMessaging class]);
  88. OCMStub([_mockMessagingClass messaging]).andReturn(_mockMessaging);
  89. OCMStub([_mockTokenManager fcmSenderID]).andReturn(kFakeSenderID);
  90. }
  91. return self;
  92. }
  93. - (void)cleanupAfterTest:(XCTestCase *)testCase {
  94. [_messaging.rmq2Manager removeDatabase];
  95. [testCase waitForDrainDatabaseQueueForRmqManager:_messaging.rmq2Manager];
  96. [_messaging.messagingUserDefaults removePersistentDomainForName:kFIRMessagingDefaultsTestDomain];
  97. [_mockPubsub stopMocking];
  98. [_mockMessaging stopMocking];
  99. [_mockMessagingClass stopMocking];
  100. [_mockTokenManager stopMocking];
  101. [_mockInstallations stopMocking];
  102. }
  103. @end
  104. NS_ASSUME_NONNULL_END