AsyncAwaitTests.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #if compiler(>=5.5.2) && canImport(_Concurrency)
  18. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  19. class AsyncAwaitTests: APITestBase {
  20. func testFetchThenActivate() async throws {
  21. let status = try await config.fetch()
  22. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  23. let success = try await config.activate()
  24. XCTAssertTrue(success)
  25. }
  26. func testFetchWithExpirationThenActivate() async throws {
  27. let status = try await config.fetch(withExpirationDuration: 0)
  28. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  29. _ = try await config.activate()
  30. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  31. }
  32. func testFetchAndActivate() async throws {
  33. let status = try await config.fetchAndActivate()
  34. XCTAssertEqual(status, .successFetchedFromRemote)
  35. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  36. }
  37. func testFetchAndActivateGenericValue() async throws {
  38. let status = try await config.fetchAndActivate()
  39. XCTAssertEqual(status, .successFetchedFromRemote)
  40. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  41. }
  42. // Contrast with testChangedActivateWillNotFlag in FakeConsole.swift.
  43. func testUnchangedActivateWillFlag() async throws {
  44. let status = try await config.fetch()
  45. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  46. let changed = try await config.activate()
  47. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  48. XCTAssertTrue(!APITests.useFakeConfig || changed)
  49. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  50. }
  51. func testFetchAndActivateUnchangedConfig() async throws {
  52. guard APITests.useFakeConfig == false else { return }
  53. XCTAssertEqual(config.settings.minimumFetchInterval, 0)
  54. // Represents pre-fetch occurring sometime in past.
  55. let status = try await config.fetch()
  56. XCTAssertEqual(status, .success)
  57. // Represents a `fetchAndActivate` being made to pull latest changes from Remote Config.
  58. let status2 = try await config.fetchAndActivate()
  59. // Since no updates to remote config have occurred we use the `.successUsingPreFetchedData`.
  60. // The behavior of the next test changed in Firebase 7.0.0.
  61. // It's an open question which is correct, but it should only
  62. // be changed in a major release.
  63. // See https://github.com/firebase/firebase-ios-sdk/pull/8788
  64. // XCTAssertEqual(status, .successUsingPreFetchedData)
  65. XCTAssertEqual(status2, .successFetchedFromRemote)
  66. // The `lastETagUpdateTime` should either be older or the same time as `lastFetchTime`.
  67. if let lastFetchTime = try? XCTUnwrap(config.lastFetchTime) {
  68. XCTAssertLessThanOrEqual(Double(config.settings.lastETagUpdateTime),
  69. Double(lastFetchTime.timeIntervalSince1970))
  70. } else {
  71. XCTFail("Could not unwrap lastFetchTime.")
  72. }
  73. }
  74. // MARK: - RemoteConfigConsole Tests
  75. func testFetchConfigThenUpdateConsoleThenFetchAgain() async throws {
  76. guard APITests.useFakeConfig == false else { return }
  77. _ = try await config.fetchAndActivate()
  78. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  79. XCTAssertEqual(configValue, Constants.obiwan)
  80. // Synchronously update the console.
  81. console.updateRemoteConfigValue(Constants.yoda, forKey: Constants.jedi)
  82. _ = try await config.fetchAndActivate()
  83. let configValue2 = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  84. XCTAssertEqual(configValue2, Constants.yoda)
  85. }
  86. func testFetchConfigThenAddValueOnConsoleThenFetchAgain() async throws {
  87. guard APITests.useFakeConfig == false else { return }
  88. // Ensure no Sith Lord has been written to Remote Config yet.
  89. _ = try await config.fetchAndActivate()
  90. XCTAssertTrue(config.configValue(forKey: Constants.sith).dataValue.isEmpty)
  91. // Synchronously update the console
  92. console.updateRemoteConfigValue(Constants.darthSidious, forKey: Constants.sith)
  93. // Verify the Sith Lord can now be fetched from Remote Config
  94. _ = try await config.fetchAndActivate()
  95. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.sith).stringValue)
  96. XCTAssertEqual(configValue, Constants.darthSidious)
  97. }
  98. func testFetchConfigThenDeleteValueOnConsoleThenFetchAgain() async throws {
  99. guard APITests.useFakeConfig == false else { return }
  100. _ = try await config.fetchAndActivate()
  101. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  102. XCTAssertEqual(configValue, Constants.obiwan)
  103. // Synchronously delete value on the console.
  104. console.removeRemoteConfigValue(forKey: Constants.jedi)
  105. _ = try await config.fetchAndActivate()
  106. XCTAssertTrue(config.configValue(forKey: Constants.jedi).dataValue.isEmpty,
  107. "Remote config should have been deleted.")
  108. }
  109. }
  110. #endif