FIRMessagingTestUtilities.m 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 "FIRMessagingTestUtilities.h"
  17. #import <FirebaseAnalyticsInterop/FIRAnalyticsInterop.h>
  18. #import <FirebaseInstanceID/FirebaseInstanceID.h>
  19. #import <GoogleUtilities/GULUserDefaults.h>
  20. NS_ASSUME_NONNULL_BEGIN
  21. @interface FIRInstanceID (ExposedForTest)
  22. /// Private initializer to avoid singleton usage.
  23. - (FIRInstanceID *)initPrivately;
  24. /// Starts fetching and configuration of InstanceID. This is necessary after the `initPrivately`
  25. /// call.
  26. - (void)start;
  27. @end
  28. @interface FIRMessaging (ExposedForTest)
  29. /// Surface internal initializer to avoid singleton usage during tests.
  30. - (instancetype)initWithAnalytics:(nullable id<FIRAnalyticsInterop>)analytics
  31. withInstanceID:(FIRInstanceID *)instanceID
  32. withUserDefaults:(GULUserDefaults *)defaults;
  33. /// Kicks off required calls for some messaging tests.
  34. - (void)start;
  35. @end
  36. @implementation FIRMessagingTestUtilities
  37. + (FIRMessaging *)messagingForTestsWithUserDefaults:(GULUserDefaults *)userDefaults {
  38. // Create the messaging instance with all the necessary dependencies.
  39. FIRInstanceID *instanceID = [[FIRInstanceID alloc] initPrivately];
  40. [instanceID start];
  41. // Create the messaging instance and call `start`.
  42. FIRMessaging *messaging = [[FIRMessaging alloc] initWithAnalytics:nil
  43. withInstanceID:instanceID
  44. withUserDefaults:userDefaults];
  45. [messaging start];
  46. return messaging;
  47. }
  48. @end
  49. NS_ASSUME_NONNULL_END