FakeConsoleTests.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2020 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. class FakeConsoleTests: APITestBase {
  18. override func setUp() {
  19. super.setUp()
  20. fakeConsole.config = ["Key1": "Value1"]
  21. }
  22. // Test old API.
  23. // Contrast with testUnchangedActivateWillError in APITests.swift.
  24. func testChangedActivateWillNotError() {
  25. let expectation = self.expectation(description: #function)
  26. config.fetch { status, error in
  27. if let error = error {
  28. XCTFail("Fetch Error \(error)")
  29. }
  30. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  31. self.config.activate { error in
  32. if let error = error {
  33. print("Activate Error \(error)")
  34. }
  35. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  36. expectation.fulfill()
  37. }
  38. }
  39. waitForExpectations()
  40. // Simulate updating console.
  41. fakeConsole.config = ["Key1": "Value2"]
  42. let expectation2 = self.expectation(description: #function + "2")
  43. config.fetch { status, error in
  44. if let error = error {
  45. XCTFail("Fetch Error \(error)")
  46. }
  47. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  48. self.config.activate { error in
  49. XCTAssertNil(error)
  50. XCTAssertEqual(self.config["Key1"].stringValue, "Value2")
  51. expectation2.fulfill()
  52. }
  53. }
  54. waitForExpectations()
  55. }
  56. // Test New API.
  57. // Contrast with testUnchangedActivateWillFlag in APITests.swift.
  58. func testChangedActivateWillNotFlag() {
  59. let expectation = self.expectation(description: #function)
  60. config.fetch { status, error in
  61. if let error = error {
  62. XCTFail("Fetch Error \(error)")
  63. }
  64. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  65. self.config.activate { changed, error in
  66. XCTAssertNil(error)
  67. XCTAssertTrue(changed)
  68. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  69. expectation.fulfill()
  70. }
  71. }
  72. waitForExpectations()
  73. // Simulate updating console.
  74. fakeConsole.config = ["Key1": "Value2"]
  75. let expectation2 = self.expectation(description: #function + "2")
  76. config.fetch { status, error in
  77. if let error = error {
  78. XCTFail("Fetch Error \(error)")
  79. }
  80. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  81. self.config.activate { changed, error in
  82. XCTAssertNil(error)
  83. XCTAssert(changed)
  84. XCTAssertEqual(self.config["Key1"].stringValue, "Value2")
  85. expectation2.fulfill()
  86. }
  87. }
  88. waitForExpectations()
  89. }
  90. private func waitForExpectations() {
  91. let kFIRStorageIntegrationTestTimeout = 10.0
  92. waitForExpectations(timeout: kFIRStorageIntegrationTestTimeout,
  93. handler: { (error) -> Void in
  94. if let error = error {
  95. print(error)
  96. }
  97. })
  98. }
  99. }