FIRMessagingTestUtilities.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "XCTestCase+FIRMessagingRmqManagerTests.h"
  18. #import "Example/Messaging/Tests/FIRMessagingTestUtilities.h"
  19. #import <FirebaseAnalyticsInterop/FIRAnalyticsInterop.h>
  20. #import <FirebaseInstallations/FIRInstallations.h>
  21. #import <FirebaseInstanceID/FirebaseInstanceID.h>
  22. #import <GoogleUtilities/GULUserDefaults.h>
  23. #import "Firebase/Messaging/FIRMessagingPubSub.h"
  24. #import "Firebase/Messaging/FIRMessagingRmqManager.h"
  25. NS_ASSUME_NONNULL_BEGIN
  26. static NSString *const kFIRMessagingDefaultsTestDomain = @"com.messaging.tests";
  27. @interface FIRInstanceID (ExposedForTest)
  28. /// Private initializer to avoid singleton usage.
  29. - (FIRInstanceID *)initPrivately;
  30. /// Starts fetching and configuration of InstanceID. This is necessary after the `initPrivately`
  31. /// call.
  32. - (void)start;
  33. @end
  34. @interface FIRMessaging (ExposedForTest)
  35. @property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
  36. @property(nonatomic, readwrite, strong) FIRMessagingRmqManager *rmq2Manager;
  37. /// Surface internal initializer to avoid singleton usage during tests.
  38. - (instancetype)initWithAnalytics:(nullable id<FIRAnalyticsInterop>)analytics
  39. withInstanceID:(FIRInstanceID *)instanceID
  40. withUserDefaults:(GULUserDefaults *)defaults;
  41. /// Kicks off required calls for some messaging tests.
  42. - (void)start;
  43. - (void)setupRmqManager;
  44. @end
  45. @interface FIRMessagingRmqManager (ExposedForTest)
  46. - (void)removeDatabase;
  47. @end
  48. @implementation MockPendingTopicsListDelegate
  49. - (BOOL)pendingTopicsListCanRequestTopicUpdates:(FIRMessagingPendingTopicsList *)list {
  50. return self.isReady;
  51. }
  52. - (void)pendingTopicsList:(FIRMessagingPendingTopicsList *)list
  53. requestedUpdateForTopic:(NSString *)topic
  54. action:(FIRMessagingTopicAction)action
  55. completion:(FIRMessagingTopicOperationCompletion)completion {
  56. if (self.subscriptionHandler) {
  57. self.subscriptionHandler(topic, action, completion);
  58. }
  59. }
  60. - (void)pendingTopicsListDidUpdate:(FIRMessagingPendingTopicsList *)list {
  61. if (self.updateHandler) {
  62. self.updateHandler();
  63. }
  64. }
  65. @end
  66. @implementation FIRMessagingTestUtilities
  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. _instanceID = [[FIRInstanceID alloc] initPrivately];
  76. [_instanceID start];
  77. // Create the messaging instance and call `start`.
  78. _messaging = [[FIRMessaging alloc] initWithAnalytics:nil
  79. withInstanceID:_instanceID
  80. withUserDefaults:userDefaults];
  81. if (withRMQManager) {
  82. [_messaging start];
  83. }
  84. _mockMessaging = OCMPartialMock(_messaging);
  85. if (!withRMQManager) {
  86. OCMStub([_mockMessaging setupRmqManager]).andDo(nil);
  87. [(FIRMessaging *)_mockMessaging start];
  88. }
  89. _mockInstanceID = OCMPartialMock(_instanceID);
  90. _mockPubsub = OCMPartialMock(_messaging.pubsub);
  91. }
  92. return self;
  93. }
  94. - (void)cleanupAfterTest:(XCTestCase *)testCase {
  95. [_messaging.rmq2Manager removeDatabase];
  96. [testCase waitForDrainDatabaseQueueForRmqManager:_messaging.rmq2Manager];
  97. [_messaging.messagingUserDefaults removePersistentDomainForName:kFIRMessagingDefaultsTestDomain];
  98. #pragma clang diagnostic push
  99. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  100. _messaging.shouldEstablishDirectChannel = NO;
  101. #pragma clang diagnostic pop
  102. [_mockPubsub stopMocking];
  103. [_mockMessaging stopMocking];
  104. [_mockInstanceID stopMocking];
  105. }
  106. @end
  107. NS_ASSUME_NONNULL_END