FIRInstanceIDWithFCMTest.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <XCTest/XCTest.h>
  17. #import <FirebaseCore/FIRAppInternal.h>
  18. #import <FirebaseInstanceID/FirebaseInstanceID.h>
  19. #import <OCMock/OCMock.h>
  20. #import "FIRMessaging_Private.h"
  21. #import "FIRMessaging.h"
  22. #import "FIRMessagingTestUtilities.h"
  23. @interface FIRInstanceID (ExposedForTest)
  24. - (BOOL)isFCMAutoInitEnabled;
  25. - (instancetype)initPrivately;
  26. - (void)start;
  27. @end
  28. @interface FIRMessaging ()
  29. + (FIRMessaging *)messagingForTests;
  30. @end
  31. @interface FIRInstanceIDTest : XCTestCase
  32. @property(nonatomic, readwrite, strong) FIRInstanceID *instanceID;
  33. @property(nonatomic, readwrite, strong) id mockFirebaseApp;
  34. @end
  35. @implementation FIRInstanceIDTest
  36. - (void)setUp {
  37. [super setUp];
  38. _instanceID = [[FIRInstanceID alloc] initPrivately];
  39. [_instanceID start];
  40. _mockFirebaseApp = OCMClassMock([FIRApp class]);
  41. OCMStub([_mockFirebaseApp defaultApp]).andReturn(_mockFirebaseApp);
  42. }
  43. - (void)tearDown {
  44. self.instanceID = nil;
  45. [_mockFirebaseApp stopMocking];
  46. [super tearDown];
  47. }
  48. - (void)testFCMAutoInitEnabled {
  49. NSString *const kFIRMessagingTestsAutoInit = @"com.messaging.test_autoInit";
  50. NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingTestsAutoInit];
  51. FIRMessaging *messaging = [FIRMessagingTestUtilities messagingForTestsWithUserDefaults:defaults];
  52. id classMock = OCMClassMock([FIRMessaging class]);
  53. OCMStub([classMock messaging]).andReturn(messaging);
  54. OCMStub([_mockFirebaseApp isDataCollectionDefaultEnabled]).andReturn(YES);
  55. messaging.autoInitEnabled = YES;
  56. XCTAssertTrue(
  57. [_instanceID isFCMAutoInitEnabled],
  58. @"When FCM is available, FCM Auto Init Enabled should be FCM's autoInitEnable property.");
  59. messaging.autoInitEnabled = NO;
  60. XCTAssertFalse(
  61. [_instanceID isFCMAutoInitEnabled],
  62. @"When FCM is available, FCM Auto Init Enabled should be FCM's autoInitEnable property.");
  63. messaging.autoInitEnabled = YES;
  64. XCTAssertTrue([_instanceID isFCMAutoInitEnabled]);
  65. [classMock stopMocking];
  66. }
  67. @end