FIRMessagingTestUtilities.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "Example/Messaging/Tests/FIRMessagingTestUtilities.h"
  18. #import <FirebaseAnalyticsInterop/FIRAnalyticsInterop.h>
  19. #import <FirebaseInstanceID/FirebaseInstanceID.h>
  20. #import <GoogleUtilities/GULUserDefaults.h>
  21. #import <FirebaseInstallations/FIRInstallations.h>
  22. #import "Firebase/Messaging/FIRMessagingPubSub.h"
  23. #import "Firebase/Messaging/FIRMessagingRmqManager.h"
  24. NS_ASSUME_NONNULL_BEGIN
  25. static NSString *const kFIRMessagingDefaultsTestDomain = @"com.messaging.tests";
  26. @interface FIRInstanceID (ExposedForTest)
  27. /// Private initializer to avoid singleton usage.
  28. - (FIRInstanceID *)initPrivately;
  29. /// Starts fetching and configuration of InstanceID. This is necessary after the `initPrivately`
  30. /// call.
  31. - (void)start;
  32. @end
  33. @interface FIRMessaging (ExposedForTest)
  34. @property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
  35. @property(nonatomic, readwrite, strong) FIRMessagingRmqManager *rmq2Manager;
  36. /// Surface internal initializer to avoid singleton usage during tests.
  37. - (instancetype)initWithAnalytics:(nullable id<FIRAnalyticsInterop>)analytics
  38. withInstanceID:(FIRInstanceID *)instanceID
  39. withUserDefaults:(GULUserDefaults *)defaults;
  40. /// Kicks off required calls for some messaging tests.
  41. - (void)start;
  42. - (void)setupRmqManager;
  43. @end
  44. @interface FIRMessagingRmqManager (ExposedForTest)
  45. - (void)removeDatabase;
  46. @end
  47. @implementation FIRMessagingTestUtilities
  48. - (instancetype)initWithUserDefaults:(GULUserDefaults *)userDefaults withRMQManager:(BOOL)withRMQManager {
  49. self = [super init];
  50. if (self) {
  51. // `+[FIRInstallations installations]` supposed to be used on `-[FIRInstanceID start]` to get
  52. // `FIRInstallations` default instance. Need to stub it before.
  53. _mockInstallations = OCMClassMock([FIRInstallations class]);
  54. OCMStub([self.mockInstallations installations]).andReturn(self.mockInstallations);
  55. _instanceID = [[FIRInstanceID alloc] initPrivately];
  56. [_instanceID start];
  57. // Create the messaging instance and call `start`.
  58. _messaging = [[FIRMessaging alloc] initWithAnalytics:nil
  59. withInstanceID:_instanceID
  60. withUserDefaults:userDefaults];
  61. if (withRMQManager) {
  62. [_messaging start];
  63. }
  64. _mockMessaging = OCMPartialMock(_messaging);
  65. if (!withRMQManager) {
  66. OCMStub([_mockMessaging setupRmqManager]).andDo(nil);
  67. [(FIRMessaging *)_mockMessaging start];
  68. }
  69. _mockInstanceID = OCMPartialMock(_instanceID);
  70. _mockPubsub = OCMPartialMock(_messaging.pubsub);
  71. }
  72. return self;
  73. }
  74. - (void)cleanupAfterTest {
  75. [_messaging.rmq2Manager removeDatabase];
  76. [_messaging.messagingUserDefaults removePersistentDomainForName:kFIRMessagingDefaultsTestDomain];
  77. _messaging.shouldEstablishDirectChannel = NO;
  78. [_mockPubsub stopMocking];
  79. [_mockMessaging stopMocking];
  80. [_mockInstanceID stopMocking];
  81. }
  82. @end
  83. NS_ASSUME_NONNULL_END