FIRMessagingTestUtilities.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "Firebase/Messaging/FIRMessagingPubSub.h"
  22. #import "Firebase/Messaging/FIRMessagingRmqManager.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. static NSString *const kFIRMessagingDefaultsTestDomain = @"com.messaging.tests";
  25. @interface FIRInstanceID (ExposedForTest)
  26. /// Private initializer to avoid singleton usage.
  27. - (FIRInstanceID *)initPrivately;
  28. /// Starts fetching and configuration of InstanceID. This is necessary after the `initPrivately`
  29. /// call.
  30. - (void)start;
  31. @end
  32. @interface FIRMessaging (ExposedForTest)
  33. @property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
  34. @property(nonatomic, readwrite, strong) FIRMessagingRmqManager *rmq2Manager;
  35. /// Surface internal initializer to avoid singleton usage during tests.
  36. - (instancetype)initWithAnalytics:(nullable id<FIRAnalyticsInterop>)analytics
  37. withInstanceID:(FIRInstanceID *)instanceID
  38. withUserDefaults:(GULUserDefaults *)defaults;
  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 FIRMessagingTestUtilities
  47. - (instancetype)initWithUserDefaults:(GULUserDefaults *)userDefaults withRMQManager:(BOOL)withRMQManager {
  48. self = [super init];
  49. if (self) {
  50. _instanceID = [[FIRInstanceID alloc] initPrivately];
  51. [_instanceID start];
  52. // Create the messaging instance and call `start`.
  53. _messaging = [[FIRMessaging alloc] initWithAnalytics:nil
  54. withInstanceID:_instanceID
  55. withUserDefaults:userDefaults];
  56. if (withRMQManager) {
  57. [_messaging start];
  58. }
  59. _mockMessaging = OCMPartialMock(_messaging);
  60. if (!withRMQManager) {
  61. OCMStub([_mockMessaging setupRmqManager]).andDo(nil);
  62. [(FIRMessaging *)_mockMessaging start];
  63. }
  64. _mockInstanceID = OCMPartialMock(_instanceID);
  65. _mockPubsub = OCMPartialMock(_messaging.pubsub);
  66. }
  67. return self;
  68. }
  69. - (void)cleanupAfterTest {
  70. [_messaging.rmq2Manager removeDatabase];
  71. [_messaging.messagingUserDefaults removePersistentDomainForName:kFIRMessagingDefaultsTestDomain];
  72. _messaging.shouldEstablishDirectChannel = NO;
  73. [_mockPubsub stopMocking];
  74. [_mockMessaging stopMocking];
  75. [_mockInstanceID stopMocking];
  76. }
  77. @end
  78. NS_ASSUME_NONNULL_END