FIRIAMSDKModeManagerTests.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2017 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 <GoogleUtilities/GULUserDefaults.h>
  17. #import <OCMock/OCMock.h>
  18. #import <XCTest/XCTest.h>
  19. #import "FirebaseInAppMessaging/Sources/Private/Runtime/FIRIAMSDKModeManager.h"
  20. @interface FIRIAMSDKModeManagerTests : XCTestCase
  21. @property(nonatomic) GULUserDefaults *mockUserDefaults;
  22. @property(nonatomic) id<FIRIAMTestingModeListener> mockTestingModeListener;
  23. @end
  24. @implementation FIRIAMSDKModeManagerTests
  25. - (void)setUp {
  26. [super setUp];
  27. self.mockUserDefaults = OCMClassMock(GULUserDefaults.class);
  28. self.mockTestingModeListener = OCMStrictProtocolMock(@protocol(FIRIAMTestingModeListener));
  29. }
  30. - (void)tearDown {
  31. // Put teardown code here. This method is called after the invocation of each test method in the
  32. // class.
  33. [super tearDown];
  34. }
  35. - (void)testFirstRunFromInstall_ok {
  36. // mode entry not existing from a fresh install
  37. OCMStub([self.mockUserDefaults objectForKey:[OCMArg isEqual:kFIRIAMUserDefaultKeyForSDKMode]])
  38. .andReturn(nil);
  39. FIRIAMSDKModeManager *sdkManager =
  40. [[FIRIAMSDKModeManager alloc] initWithUserDefaults:self.mockUserDefaults
  41. testingModeListener:self.mockTestingModeListener];
  42. XCTAssertEqual(FIRIAMSDKModeNewlyInstalled, [sdkManager currentMode]);
  43. // verify that we setting the mode into use defaults
  44. OCMVerify([self.mockUserDefaults
  45. setObject:[OCMArg isEqual:[NSNumber numberWithInt:FIRIAMSDKModeNewlyInstalled]]
  46. forKey:[OCMArg isEqual:kFIRIAMUserDefaultKeyForSDKMode]]);
  47. // verify that we are initializing fetch count as 0 by writing into user defaults
  48. OCMVerify([self.mockUserDefaults
  49. setInteger:0
  50. forKey:[OCMArg isEqual:kFIRIAMUserDefaultKeyForServerFetchCount]]);
  51. }
  52. - (void)testGoingIntoRegularFromNewlyInstalledMode {
  53. // mode entry not existing from a fresh install
  54. OCMStub([self.mockUserDefaults objectForKey:[OCMArg isEqual:kFIRIAMUserDefaultKeyForSDKMode]])
  55. .andReturn(nil);
  56. FIRIAMSDKModeManager *sdkManager =
  57. [[FIRIAMSDKModeManager alloc] initWithUserDefaults:self.mockUserDefaults
  58. testingModeListener:self.mockTestingModeListener];
  59. XCTAssertEqual(FIRIAMSDKModeNewlyInstalled, [sdkManager currentMode]);
  60. // now we register up to kFIRIAMMaxFetchInNewlyInstalledMode - 1 fetches and it still stay
  61. // in Newly Installed mode
  62. for (int i = 0; i < kFIRIAMMaxFetchInNewlyInstalledMode - 1; i++) {
  63. [sdkManager registerOneMoreFetch];
  64. }
  65. XCTAssertEqual(FIRIAMSDKModeNewlyInstalled, [sdkManager currentMode]);
  66. // now one more fetch would turn it into regular mode
  67. [sdkManager registerOneMoreFetch];
  68. XCTAssertEqual(FIRIAMSDKModeRegular, [sdkManager currentMode]);
  69. }
  70. - (void)testIncrementCountForFetchRegistrationFromNewlyInstalledMode {
  71. // put sdk into regular mode
  72. OCMStub([self.mockUserDefaults objectForKey:[OCMArg isEqual:kFIRIAMUserDefaultKeyForSDKMode]])
  73. .andReturn([NSNumber numberWithInt:FIRIAMSDKModeNewlyInstalled]);
  74. int currentFetchCount = 3;
  75. OCMStub([self.mockUserDefaults
  76. integerForKey:[OCMArg isEqual:kFIRIAMUserDefaultKeyForServerFetchCount]])
  77. .andReturn(currentFetchCount);
  78. FIRIAMSDKModeManager *sdkManager =
  79. [[FIRIAMSDKModeManager alloc] initWithUserDefaults:self.mockUserDefaults
  80. testingModeListener:self.mockTestingModeListener];
  81. // now we do new fetch registration
  82. [sdkManager registerOneMoreFetch];
  83. // verify that we are writing currentFetchCount+1 into user defaults
  84. OCMVerify([self.mockUserDefaults
  85. setInteger:currentFetchCount + 1
  86. forKey:[OCMArg isEqual:kFIRIAMUserDefaultKeyForServerFetchCount]]);
  87. }
  88. - (void)testNoUpdateForFetchRegistrationFromRegularMode {
  89. // put sdk into regular mode
  90. OCMStub([self.mockUserDefaults objectForKey:[OCMArg isEqual:kFIRIAMUserDefaultKeyForSDKMode]])
  91. .andReturn([NSNumber numberWithInt:FIRIAMSDKModeRegular]);
  92. int currentFetchCount = 3;
  93. OCMStub([self.mockUserDefaults
  94. integerForKey:[OCMArg isEqual:kFIRIAMUserDefaultKeyForServerFetchCount]])
  95. .andReturn(currentFetchCount);
  96. FIRIAMSDKModeManager *sdkManager =
  97. [[FIRIAMSDKModeManager alloc] initWithUserDefaults:self.mockUserDefaults
  98. testingModeListener:self.mockTestingModeListener];
  99. // now we do new fetch registration, but no more fetch count or mode updates in user defaults
  100. [sdkManager registerOneMoreFetch];
  101. XCTAssertEqual(FIRIAMSDKModeRegular, [sdkManager currentMode]);
  102. OCMReject([self.mockUserDefaults setInteger:currentFetchCount + 1 forKey:[OCMArg any]]);
  103. }
  104. - (void)testGoingIntoTestingDeviceMode {
  105. // mode entry not existing from a fresh install
  106. OCMStub([self.mockUserDefaults objectForKey:[OCMArg isEqual:kFIRIAMUserDefaultKeyForSDKMode]])
  107. .andReturn(nil);
  108. OCMExpect([self.mockTestingModeListener testingModeSwitchedOn]);
  109. FIRIAMSDKModeManager *sdkManager =
  110. [[FIRIAMSDKModeManager alloc] initWithUserDefaults:self.mockUserDefaults
  111. testingModeListener:self.mockTestingModeListener];
  112. [sdkManager becomeTestingInstance];
  113. XCTAssertEqual(FIRIAMSDKModeTesting, [sdkManager currentMode]);
  114. OCMVerify([self.mockTestingModeListener testingModeSwitchedOn]);
  115. }
  116. @end