FIRMessagingTestUtilities.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <FirebaseInstanceID/FirebaseInstanceID.h>
  21. #import <GoogleUtilities/GULUserDefaults.h>
  22. #import <FirebaseInstallations/FIRInstallations.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 FIRMessagingTestUtilities
  49. - (instancetype)initWithUserDefaults:(GULUserDefaults *)userDefaults withRMQManager:(BOOL)withRMQManager {
  50. self = [super init];
  51. if (self) {
  52. // `+[FIRInstallations installations]` supposed to be used on `-[FIRInstanceID start]` to get
  53. // `FIRInstallations` default instance. Need to stub it before.
  54. _mockInstallations = OCMClassMock([FIRInstallations class]);
  55. OCMStub([self.mockInstallations installations]).andReturn(self.mockInstallations);
  56. _instanceID = [[FIRInstanceID alloc] initPrivately];
  57. [_instanceID start];
  58. // Create the messaging instance and call `start`.
  59. _messaging = [[FIRMessaging alloc] initWithAnalytics:nil
  60. withInstanceID:_instanceID
  61. withUserDefaults:userDefaults];
  62. if (withRMQManager) {
  63. [_messaging start];
  64. }
  65. _mockMessaging = OCMPartialMock(_messaging);
  66. if (!withRMQManager) {
  67. OCMStub([_mockMessaging setupRmqManager]).andDo(nil);
  68. [(FIRMessaging *)_mockMessaging start];
  69. }
  70. _mockInstanceID = OCMPartialMock(_instanceID);
  71. _mockPubsub = OCMPartialMock(_messaging.pubsub);
  72. }
  73. return self;
  74. }
  75. - (void)cleanupAfterTest:(XCTestCase *)testCase {
  76. [_messaging.rmq2Manager removeDatabase];
  77. [testCase waitForDrainDatabaseQueueForRmqManager:_messaging.rmq2Manager];
  78. [_messaging.messagingUserDefaults removePersistentDomainForName:kFIRMessagingDefaultsTestDomain];
  79. _messaging.shouldEstablishDirectChannel = NO;
  80. [_mockPubsub stopMocking];
  81. [_mockMessaging stopMocking];
  82. [_mockInstanceID stopMocking];
  83. }
  84. @end
  85. NS_ASSUME_NONNULL_END