AsyncAwaitTests.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseCore
  15. @testable import FirebaseRemoteConfig
  16. import XCTest
  17. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  18. class AsyncAwaitTests: APITestBase {
  19. func testFetchThenActivate() async throws {
  20. let status = try await config.fetch()
  21. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  22. let success = try await config.activate()
  23. XCTAssertTrue(success)
  24. }
  25. func testFetchWithExpirationThenActivate() async throws {
  26. let status = try await config.fetch(withExpirationDuration: 0)
  27. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  28. _ = try await config.activate()
  29. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  30. }
  31. func testFetchAndActivate() async throws {
  32. let status = try await config.fetchAndActivate()
  33. XCTAssertEqual(status, .successFetchedFromRemote)
  34. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  35. }
  36. func testFetchAndActivateGenericValue() async throws {
  37. let status = try await config.fetchAndActivate()
  38. XCTAssertEqual(status, .successFetchedFromRemote)
  39. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  40. }
  41. // Contrast with testChangedActivateWillNotFlag in FakeConsole.swift.
  42. func testUnchangedActivateWillFlag() async throws {
  43. let status = try await config.fetch()
  44. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  45. let changed = try await config.activate()
  46. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  47. XCTAssertTrue(!APITests.useFakeConfig || changed)
  48. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  49. }
  50. func testFetchAndActivateUnchangedConfig() async throws {
  51. guard APITests.useFakeConfig == false else { return }
  52. XCTAssertEqual(config.settings.minimumFetchInterval, 0)
  53. // Represents pre-fetch occurring sometime in past.
  54. let status = try await config.fetch()
  55. XCTAssertEqual(status, .success)
  56. // Represents a `fetchAndActivate` being made to pull latest changes from Remote Config.
  57. let status2 = try await config.fetchAndActivate()
  58. // Since no updates to remote config have occurred we use the `.successUsingPreFetchedData`.
  59. // The behavior of the next test changed in Firebase 7.0.0.
  60. // It's an open question which is correct, but it should only
  61. // be changed in a major release.
  62. // See https://github.com/firebase/firebase-ios-sdk/pull/8788
  63. // XCTAssertEqual(status, .successUsingPreFetchedData)
  64. XCTAssertEqual(status2, .successFetchedFromRemote)
  65. // The `lastETagUpdateTime` should either be older or the same time as `lastFetchTime`.
  66. if let lastFetchTime = try? XCTUnwrap(config.lastFetchTime) {
  67. XCTAssertLessThanOrEqual(Double(config.settings.lastETagUpdateTime),
  68. Double(lastFetchTime.timeIntervalSince1970))
  69. } else {
  70. XCTFail("Could not unwrap lastFetchTime.")
  71. }
  72. }
  73. // MARK: - RemoteConfigConsole Tests
  74. func testFetchConfigThenUpdateConsoleThenFetchAgain() async throws {
  75. guard APITests.useFakeConfig == false else { return }
  76. _ = try await config.fetchAndActivate()
  77. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  78. XCTAssertEqual(configValue, Constants.obiwan)
  79. // Synchronously update the console.
  80. console.updateRemoteConfigValue(Constants.yoda, forKey: Constants.jedi)
  81. _ = try await config.fetchAndActivate()
  82. let configValue2 = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  83. XCTAssertEqual(configValue2, Constants.yoda)
  84. }
  85. func testFetchConfigThenAddValueOnConsoleThenFetchAgain() async throws {
  86. guard APITests.useFakeConfig == false else { return }
  87. // Ensure no Sith Lord has been written to Remote Config yet.
  88. _ = try await config.fetchAndActivate()
  89. XCTAssertTrue(config.configValue(forKey: Constants.sith).dataValue.isEmpty)
  90. // Synchronously update the console
  91. console.updateRemoteConfigValue(Constants.darthSidious, forKey: Constants.sith)
  92. // Verify the Sith Lord can now be fetched from Remote Config
  93. _ = try await config.fetchAndActivate()
  94. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.sith).stringValue)
  95. XCTAssertEqual(configValue, Constants.darthSidious)
  96. }
  97. func testFetchConfigThenDeleteValueOnConsoleThenFetchAgain() async throws {
  98. guard APITests.useFakeConfig == false else { return }
  99. _ = try await config.fetchAndActivate()
  100. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  101. XCTAssertEqual(configValue, Constants.obiwan)
  102. // Synchronously delete value on the console.
  103. console.removeRemoteConfigValue(forKey: Constants.jedi)
  104. _ = try await config.fetchAndActivate()
  105. XCTAssertTrue(config.configValue(forKey: Constants.jedi).dataValue.isEmpty,
  106. "Remote config should have been deleted.")
  107. }
  108. }