ConfigRealtimeTests.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2025 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 FirebaseInstallations
  16. @testable import FirebaseRemoteConfig
  17. import GoogleUtilities
  18. import XCTest
  19. typealias ConfigFetchCompletion = (RemoteConfigFetchStatus, RemoteConfigUpdate?, Error?) -> Void
  20. class ConfigRealtimeTests: XCTestCase {
  21. // Fake ConfigFetch - Simulates successful fetch with controllable version number
  22. private class FakeConfigFetch: ConfigFetch {
  23. var fakeTemplateVersionNumber: String = "0"
  24. var fetchCompletionHandler: ConfigFetchCompletion?
  25. override func realtimeFetchConfig(fetchAttemptNumber: Int,
  26. completionHandler: @escaping ConfigFetchCompletion) {
  27. fetchCompletionHandler = completionHandler
  28. }
  29. }
  30. public typealias FIRInstallationsTokenHandler = (InstallationsAuthTokenResult?, Error?) -> Void
  31. public typealias FIRInstallationsIDHandler = (String?, Error?) -> Void
  32. // Fake Installations - Simulates Installations token retrieval
  33. private class FakeInstallations: InstallationsProtocol {
  34. var fakeAuthToken: String?
  35. var fakeInstallationID: String?
  36. var authTokenCompletion: FIRInstallationsTokenHandler?
  37. var installationIDCompletion: FIRInstallationsIDHandler?
  38. func authToken(completion: @escaping FIRInstallationsTokenHandler) {
  39. authTokenCompletion = completion
  40. }
  41. func installationID(completion: @escaping FIRInstallationsIDHandler) {
  42. installationIDCompletion = completion
  43. }
  44. }
  45. var realtime: ConfigRealtime!
  46. private var fakeFetch: FakeConfigFetch!
  47. var fakeSettings: ConfigSettings!
  48. private var fakeInstallations: FakeInstallations!
  49. var options: FirebaseOptions!
  50. let namespace = "test_namespace:test_app"
  51. let expectationTimeout: TimeInterval = 2
  52. override func setUp() {
  53. super.setUp()
  54. options = FirebaseOptions(googleAppID: "1:1234567890:ios:abcdef1234567890",
  55. gcmSenderID: "1234567890")
  56. options.apiKey = "fake_api_key"
  57. fakeFetch = FakeConfigFetch(
  58. content: ConfigContent.sharedInstance,
  59. DBManager: ConfigDBManager.sharedInstance,
  60. settings: ConfigSettings(databaseManager: ConfigDBManager.sharedInstance,
  61. namespace: namespace, firebaseAppName: "test_app",
  62. googleAppID: options.googleAppID),
  63. analytics: nil,
  64. experiment: nil,
  65. queue: DispatchQueue.main,
  66. namespace: namespace,
  67. options: options
  68. )
  69. fakeSettings = ConfigSettings(databaseManager: ConfigDBManager.sharedInstance,
  70. namespace: namespace, firebaseAppName: "test_app",
  71. googleAppID: options.googleAppID)
  72. fakeInstallations = FakeInstallations()
  73. realtime = ConfigRealtime(configFetch: fakeFetch,
  74. settings: fakeSettings, namespace: namespace,
  75. options: options, installations: fakeInstallations)
  76. }
  77. override func tearDown() {
  78. realtime = nil
  79. fakeFetch = nil
  80. fakeSettings = nil
  81. options = nil
  82. fakeInstallations = nil
  83. super.tearDown()
  84. }
  85. private let fetchResponseHTTPStatusOK = 200
  86. private let fetchResponseHTTPStatusClientTimeout = 429
  87. private let fetchResponseHTTPStatusCodeBadGateway = 502
  88. private let fetchResponseHTTPStatusCodeServiceUnavailable = 503
  89. private let fetchResponseHTTPStatusCodeGatewayTimeout = 504
  90. func testIsStatusCodeRetryable() {
  91. XCTAssertTrue(realtime.isStatusCodeRetryable(fetchResponseHTTPStatusClientTimeout))
  92. XCTAssertTrue(realtime.isStatusCodeRetryable(fetchResponseHTTPStatusCodeServiceUnavailable))
  93. XCTAssertTrue(realtime.isStatusCodeRetryable(fetchResponseHTTPStatusCodeBadGateway))
  94. XCTAssertTrue(realtime.isStatusCodeRetryable(fetchResponseHTTPStatusCodeGatewayTimeout))
  95. XCTAssertFalse(realtime.isStatusCodeRetryable(fetchResponseHTTPStatusOK))
  96. XCTAssertFalse(realtime.isStatusCodeRetryable(400)) // Example non-retryable code
  97. }
  98. func testInBackground() {
  99. realtime.isInBackground = true // Set background state
  100. XCTAssertFalse(realtime.canMakeConnection()) // Should not be able to connect
  101. }
  102. func testRealtimeDisabled() {
  103. realtime.isRealtimeDisabled = true // Disable realtime updates
  104. XCTAssertFalse(realtime.canMakeConnection()) // Should not be able to connect
  105. }
  106. }