| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- // Copyright 2020 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- import FirebaseCore
- @testable import FirebaseRemoteConfig
- import XCTest
- /// String constants used for testing.
- private enum Constants {
- static let key1 = "Key1"
- static let jedi = "Jedi"
- static let sith = "Sith_Lord"
- static let value1 = "Value1"
- static let obiwan = "Obi-Wan"
- static let yoda = "Yoda"
- static let darthSidious = "Darth Sidious"
- }
- class APITests: APITestBase {
- var console: RemoteConfigConsole!
- override func setUp() {
- super.setUp()
- if APITests.useFakeConfig {
- fakeConsole.config = [Constants.key1: Constants.value1]
- } else {
- console = RemoteConfigConsole()
- console.updateRemoteConfigValue(Constants.obiwan, forKey: Constants.jedi)
- }
- }
- override func tearDown() {
- super.tearDown()
- // If using RemoteConfigConsole, reset remote config values.
- if !APITests.useFakeConfig {
- console.removeRemoteConfigValue(forKey: Constants.sith)
- console.removeRemoteConfigValue(forKey: Constants.jedi)
- }
- }
- func testFetchThenActivate() {
- let expectation = self.expectation(description: #function)
- config.fetch { status, error in
- if let error = error {
- XCTFail("Fetch Error \(error)")
- }
- XCTAssertEqual(status, RemoteConfigFetchStatus.success)
- self.config.activate { _, error in
- XCTAssertNil(error)
- XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
- expectation.fulfill()
- }
- }
- waitForExpectations()
- }
- func testFetchWithExpirationThenActivate() {
- let expectation = self.expectation(description: #function)
- config.fetch(withExpirationDuration: 0) { status, error in
- if let error = error {
- XCTFail("Fetch Error \(error)")
- }
- XCTAssertEqual(status, RemoteConfigFetchStatus.success)
- self.config.activate { _, error in
- XCTAssertNil(error)
- XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
- expectation.fulfill()
- }
- }
- waitForExpectations()
- }
- func testFetchAndActivate() {
- let expectation = self.expectation(description: #function)
- config.fetchAndActivate { status, error in
- if let error = error {
- XCTFail("Fetch and Activate Error \(error)")
- }
- XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
- expectation.fulfill()
- }
- waitForExpectations()
- }
- // Test New API.
- // Contrast with testChangedActivateWillNotFlag in FakeConsole.swift.
- func testUnchangedActivateWillFlag() {
- let expectation = self.expectation(description: #function)
- config.fetch { status, error in
- if let error = error {
- XCTFail("Fetch Error \(error)")
- }
- XCTAssertEqual(status, RemoteConfigFetchStatus.success)
- self.config.activate { changed, error in
- XCTAssertTrue(!APITests.useFakeConfig || changed)
- XCTAssertNil(error)
- XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
- expectation.fulfill()
- }
- }
- waitForExpectations()
- let expectation2 = self.expectation(description: #function + "2")
- config.fetch { status, error in
- if let error = error {
- XCTFail("Fetch Error \(error)")
- }
- XCTAssertEqual(status, RemoteConfigFetchStatus.success)
- self.config.activate { changed, error in
- XCTAssertFalse(changed)
- XCTAssertNil(error)
- XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
- expectation2.fulfill()
- }
- }
- waitForExpectations()
- }
- func testFetchAndActivateUnchangedConfig() throws {
- guard APITests.useFakeConfig == false else { return }
- let expectation = self.expectation(description: #function)
- XCTAssertEqual(config.settings.minimumFetchInterval, 0)
- let serialQueue = DispatchQueue(label: "\(#function)Queue")
- let group = DispatchGroup()
- group.enter()
- serialQueue.async {
- // Represents pre-fetch occurring sometime in past.
- self.config.fetch { status, error in
- XCTAssertNil(error, "Fetch Error \(error!)")
- XCTAssertEqual(status, .success)
- group.leave()
- }
- }
- serialQueue.async {
- group.wait()
- group.enter()
- // Represents a `fetchAndActivate` being made to pull latest changes from Remote Config.
- self.config.fetchAndActivate { status, error in
- XCTAssertNil(error, "Fetch & Activate Error \(error!)")
- // Since no updates to remote config have occurred we use the `.successUsingPreFetchedData`.
- XCTAssertEqual(status, .successUsingPreFetchedData)
- // The `lastETagUpdateTime` should either be older or the same time as `lastFetchTime`.
- if let lastFetchTime = try? XCTUnwrap(self.config.lastFetchTime) {
- XCTAssertLessThanOrEqual(Double(self.config.settings.lastETagUpdateTime),
- Double(lastFetchTime.timeIntervalSince1970))
- } else {
- XCTFail("Could not unwrap lastFetchTime.")
- }
- expectation.fulfill()
- }
- }
- waitForExpectations()
- }
- // MARK: - RemoteConfigConsole Tests
- func testFetchConfigThenUpdateConsoleThenFetchAgain() {
- guard APITests.useFakeConfig == false else { return }
- let expectation = self.expectation(description: #function)
- config.fetchAndActivate { status, error in
- XCTAssertNil(error, "Fetch & Activate Error \(error!)")
- if let configValue = self.config.configValue(forKey: Constants.jedi).stringValue {
- XCTAssertEqual(configValue, Constants.obiwan)
- } else {
- XCTFail("Could not unwrap config value for key: \(Constants.jedi)")
- }
- expectation.fulfill()
- }
- waitForExpectations()
- // Synchronously update the console.
- console.updateRemoteConfigValue(Constants.yoda, forKey: Constants.jedi)
- let expectation2 = self.expectation(description: #function + "2")
- config.fetchAndActivate { status, error in
- XCTAssertNil(error, "Fetch & Activate Error \(error!)")
- if let configValue = self.config.configValue(forKey: Constants.jedi).stringValue {
- XCTAssertEqual(configValue, Constants.yoda)
- } else {
- XCTFail("Could not unwrap config value for key: \(Constants.jedi)")
- }
- expectation2.fulfill()
- }
- waitForExpectations()
- }
- func testFetchConfigThenAddValueOnConsoleThenFetchAgain() {
- guard APITests.useFakeConfig == false else { return }
- // Ensure no Sith Lord has been written to Remote Config yet.
- let expectation = self.expectation(description: #function)
- config.fetchAndActivate { status, error in
- XCTAssertNil(error, "Fetch & Activate Error \(error!)")
- XCTAssertTrue(self.config.configValue(forKey: Constants.sith).dataValue.isEmpty)
- expectation.fulfill()
- }
- waitForExpectations()
- // Synchronously update the console
- console.updateRemoteConfigValue(Constants.darthSidious, forKey: Constants.sith)
- // Verify the Sith Lord can now be fetched from Remote Config.
- let expectation2 = self.expectation(description: #function + "2")
- config.fetchAndActivate { status, error in
- XCTAssertNil(error, "Fetch & Activate Error \(error!)")
- if let configValue = self.config.configValue(forKey: Constants.sith).stringValue {
- XCTAssertEqual(configValue, Constants.darthSidious)
- } else {
- XCTFail("Could not unwrap config value for key: \(Constants.sith)")
- }
- expectation2.fulfill()
- }
- waitForExpectations()
- }
- func testFetchConfigThenDeleteValueOnConsoleThenFetchAgain() {
- guard APITests.useFakeConfig == false else { return }
- let expectation = self.expectation(description: #function)
- config.fetchAndActivate { status, error in
- XCTAssertNil(error, "Fetch & Activate Error \(error!)")
- if let configValue = self.config.configValue(forKey: Constants.jedi).stringValue {
- XCTAssertEqual(configValue, Constants.obiwan)
- } else {
- XCTFail("Could not unwrap config value for key: \(Constants.jedi)")
- }
- expectation.fulfill()
- }
- waitForExpectations()
- // Synchronously delete value on the console.
- console.removeRemoteConfigValue(forKey: Constants.jedi)
- let expectation2 = self.expectation(description: #function + "2")
- config.fetchAndActivate { status, error in
- XCTAssertNil(error, "Fetch & Activate Error \(error!)")
- XCTAssertTrue(self.config.configValue(forKey: Constants.jedi).dataValue.isEmpty,
- "Remote config should have been deleted.")
- expectation2.fulfill()
- }
- waitForExpectations()
- }
- // MARK: - Private Helpers
- private func waitForExpectations() {
- let kFIRStorageIntegrationTestTimeout = 10.0
- waitForExpectations(timeout: kFIRStorageIntegrationTestTimeout,
- handler: { (error) -> Void in
- if let error = error {
- print(error)
- }
- })
- }
- }
|