APITests.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. import FirebaseRemoteConfig
  16. import XCTest
  17. class APITests: XCTestCase {
  18. var app: FirebaseApp!
  19. var config: RemoteConfig!
  20. override class func setUp() {
  21. FirebaseApp.configure()
  22. }
  23. override func setUp() {
  24. super.setUp()
  25. app = FirebaseApp.app()
  26. config = RemoteConfig.remoteConfig(app: app!)
  27. let settings = RemoteConfigSettings()
  28. settings.minimumFetchInterval = 0
  29. config.configSettings = settings
  30. FirebaseConfiguration.shared.setLoggerLevel(FirebaseLoggerLevel.debug)
  31. }
  32. override func tearDown() {
  33. app = nil
  34. config = nil
  35. super.tearDown()
  36. }
  37. func testFetchThenActivate() {
  38. let expectation = self.expectation(description: #function)
  39. config.fetch { status, error in
  40. if let error = error {
  41. XCTFail("Fetch Error \(error)")
  42. }
  43. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  44. self.config.activate { error in
  45. if let error = error {
  46. // This API returns an error if the config was unchanged.
  47. //
  48. print("Activate Error \(error)")
  49. }
  50. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  51. expectation.fulfill()
  52. }
  53. }
  54. waitForExpectations()
  55. }
  56. func testFetchWithExpirationThenActivate() {
  57. let expectation = self.expectation(description: #function)
  58. config.fetch(withExpirationDuration: 0) { status, error in
  59. if let error = error {
  60. XCTFail("Fetch Error \(error)")
  61. }
  62. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  63. self.config.activate { error in
  64. if let error = error {
  65. // This API returns an error if the config was unchanged.
  66. //
  67. print("Activate Error \(error)")
  68. }
  69. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  70. expectation.fulfill()
  71. }
  72. }
  73. waitForExpectations()
  74. }
  75. func testFetchAndActivate() {
  76. let expectation = self.expectation(description: #function)
  77. config.fetchAndActivate { status, error in
  78. if let error = error {
  79. XCTFail("Fetch and Activate Error \(error)")
  80. }
  81. XCTAssertEqual(self.config["Key1"].stringValue, "Value1")
  82. expectation.fulfill()
  83. }
  84. waitForExpectations()
  85. }
  86. private func waitForExpectations() {
  87. let kFIRStorageIntegrationTestTimeout = 10.0
  88. waitForExpectations(timeout: kFIRStorageIntegrationTestTimeout,
  89. handler: { (error) -> Void in
  90. if let error = error {
  91. print(error)
  92. }
  93. })
  94. }
  95. }