APITests.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 APITests: APITestBase {
  18. override func setUp() {
  19. super.setUp()
  20. if APITests.useFakeConfig {
  21. fakeConsole.config = ["Key1": "Value1"]
  22. }
  23. }
  24. func testFetchThenActivate() {
  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. XCTAssertNil(error)
  33. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  34. expectation.fulfill()
  35. }
  36. }
  37. waitForExpectations()
  38. }
  39. func testFetchWithExpirationThenActivate() {
  40. let expectation = self.expectation(description: #function)
  41. config.fetch(withExpirationDuration: 0) { status, error in
  42. if let error = error {
  43. XCTFail("Fetch Error \(error)")
  44. }
  45. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  46. self.config.activate { _, error in
  47. XCTAssertNil(error)
  48. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  49. expectation.fulfill()
  50. }
  51. }
  52. waitForExpectations()
  53. }
  54. func testFetchAndActivate() {
  55. let expectation = self.expectation(description: #function)
  56. config.fetchAndActivate { status, error in
  57. if let error = error {
  58. XCTFail("Fetch and Activate Error \(error)")
  59. }
  60. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  61. expectation.fulfill()
  62. }
  63. waitForExpectations()
  64. }
  65. // Test old API.
  66. // Contrast with testChangedActivateWillNotError in FakeConsole.swift.
  67. func testUnchangedActivateWillError() {
  68. let expectation = self.expectation(description: #function)
  69. config.fetch { status, error in
  70. if let error = error {
  71. XCTFail("Fetch Error \(error)")
  72. }
  73. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  74. self.config.activate { error in
  75. if let error = error {
  76. print("Activate Error \(error)")
  77. }
  78. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  79. expectation.fulfill()
  80. }
  81. }
  82. waitForExpectations()
  83. let expectation2 = self.expectation(description: #function + "2")
  84. config.fetch { status, error in
  85. if let error = error {
  86. XCTFail("Fetch Error \(error)")
  87. }
  88. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  89. self.config.activate { error in
  90. XCTAssertNotNil(error)
  91. if let error = error {
  92. XCTAssertEqual((error as NSError).code, RemoteConfigError.internalError.rawValue)
  93. }
  94. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  95. expectation2.fulfill()
  96. }
  97. }
  98. waitForExpectations()
  99. }
  100. // Test New API.
  101. // Contrast with testChangedActivateWillNotFlag in FakeConsole.swift.
  102. func testUnchangedActivateWillFlag() {
  103. let expectation = self.expectation(description: #function)
  104. config.fetch { status, error in
  105. if let error = error {
  106. XCTFail("Fetch Error \(error)")
  107. }
  108. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  109. self.config.activate { changed, error in
  110. XCTAssertTrue(!APITests.useFakeConfig || changed)
  111. XCTAssertNil(error)
  112. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  113. expectation.fulfill()
  114. }
  115. }
  116. waitForExpectations()
  117. let expectation2 = self.expectation(description: #function + "2")
  118. config.fetch { status, error in
  119. if let error = error {
  120. XCTFail("Fetch Error \(error)")
  121. }
  122. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  123. self.config.activate { changed, error in
  124. XCTAssertFalse(changed)
  125. XCTAssertNil(error)
  126. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  127. expectation2.fulfill()
  128. }
  129. }
  130. waitForExpectations()
  131. }
  132. private func waitForExpectations() {
  133. let kFIRStorageIntegrationTestTimeout = 10.0
  134. waitForExpectations(timeout: kFIRStorageIntegrationTestTimeout,
  135. handler: { (error) -> Void in
  136. if let error = error {
  137. print(error)
  138. }
  139. })
  140. }
  141. }