FirebaseRemoteConfigSwift_APIBuildTests.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright 2023 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 XCTest
  15. import FirebaseCore
  16. import FirebaseRemoteConfig
  17. import FirebaseRemoteConfigInterop
  18. final class FirebaseRemoteConfig_APIBuildTests: XCTestCase {
  19. func usage(code: FirebaseRemoteConfig.RemoteConfigError,
  20. updateErrorCode: FirebaseRemoteConfig.RemoteConfigUpdateError) throws {
  21. // MARK: - FirebaseRemoteConfig
  22. // TODO(ncooke3): These global constants should be lowercase.
  23. let _: String = FirebaseRemoteConfig.NamespaceGoogleMobilePlatform
  24. let _: String = FirebaseRemoteConfig.RemoteConfigThrottledEndTimeInSecondsKey
  25. func testRemoveListener(registration: ConfigUpdateListenerRegistration) {
  26. registration.remove()
  27. }
  28. let fetchStatus: FirebaseRemoteConfig.RemoteConfigFetchStatus? = nil
  29. switch fetchStatus! {
  30. case .noFetchYet: break
  31. case .success: break
  32. case .failure: break
  33. case .throttled: break
  34. @unknown default: break
  35. }
  36. let fetchAndActivateStatus: FirebaseRemoteConfig.RemoteConfigFetchAndActivateStatus? = nil
  37. switch fetchAndActivateStatus! {
  38. case .successFetchedFromRemote: break
  39. case .successUsingPreFetchedData: break
  40. case .error: break
  41. @unknown default: break
  42. }
  43. // Used to pass into the initializers for the custom errors below.
  44. let nsError = NSError(domain: "", code: 0, userInfo: nil)
  45. // TODO(ncooke3): Global constants should be lowercase.
  46. // TODO(paulb777): Decide if ok to break and add release note.
  47. // let _: String = FirebaseRemoteConfig.RemoteConfigErrorDomain
  48. // let _ = FirebaseRemoteConfig.RemoteConfigError(_nsError: nsError)
  49. // let _: FirebaseRemoteConfig.RemoteConfigError.Code._ErrorType = FirebaseRemoteConfig
  50. // .RemoteConfigError(_nsError: nsError)
  51. let _: String = FirebaseRemoteConfig.RemoteConfigError.errorDomain
  52. switch code {
  53. case .unknown: break
  54. case .throttled: break
  55. case .internalError: break
  56. @unknown default: break
  57. }
  58. _ = FirebaseRemoteConfig.RemoteConfigError.unknown
  59. _ = FirebaseRemoteConfig.RemoteConfigError.throttled
  60. _ = FirebaseRemoteConfig.RemoteConfigError.internalError
  61. // TODO(ncooke3): Global constants should be lowercase.
  62. // TODO(paulb777): Decide if ok to break and add release note.
  63. // let _: String = FirebaseRemoteConfig.RemoteConfigUpdateErrorDomain
  64. // let _ = FirebaseRemoteConfig.RemoteConfigUpdateError(_nsError: nsError)
  65. // let _: FirebaseRemoteConfig.RemoteConfigUpdateError.Code._ErrorType = FirebaseRemoteConfig
  66. // .RemoteConfigUpdateError(_nsError: nsError)
  67. let _: String = FirebaseRemoteConfig.RemoteConfigUpdateError.errorDomain
  68. switch updateErrorCode {
  69. case .streamError: break
  70. case .notFetched: break
  71. case .messageInvalid: break
  72. case .unavailable: break
  73. @unknown default: break
  74. }
  75. _ = FirebaseRemoteConfig.RemoteConfigUpdateError.streamError
  76. _ = FirebaseRemoteConfig.RemoteConfigUpdateError.notFetched
  77. _ = FirebaseRemoteConfig.RemoteConfigUpdateError.messageInvalid
  78. _ = FirebaseRemoteConfig.RemoteConfigUpdateError.unavailable
  79. // This is only initializable for internal clients - fire-perf.
  80. let value = FirebaseRemoteConfig.RemoteConfigValue(data: Data(), source: .default)
  81. let _: String? = value.stringValue
  82. // TODO(ncooke3): Returns an Objective-C reference type.
  83. let _: NSNumber = value.numberValue
  84. let _: Data = value.dataValue
  85. let _: Bool = value.boolValue
  86. let _: Any? = value.jsonValue
  87. let source: FirebaseRemoteConfig.RemoteConfigSource = value.source
  88. switch source {
  89. case .remote: break
  90. case .default: break
  91. case .static: break
  92. @unknown default: break
  93. }
  94. let settings = FirebaseRemoteConfig.RemoteConfigSettings()
  95. settings.minimumFetchInterval = TimeInterval(100)
  96. settings.fetchTimeout = TimeInterval(100)
  97. // TODO(ncooke3): This should probably not be initializable.
  98. let update = FirebaseRemoteConfig.RemoteConfigUpdate()
  99. let _: Set<String> = update.updatedKeys
  100. let _ = FirebaseRemoteConfig.RemoteConfig.remoteConfig()
  101. let config = FirebaseRemoteConfig.RemoteConfig
  102. .remoteConfig(app: FirebaseCore.FirebaseApp.app()!)
  103. let _: Date? = config.lastFetchTime
  104. let _: FirebaseRemoteConfig.RemoteConfigFetchStatus = config.lastFetchStatus
  105. let _: FirebaseRemoteConfig.RemoteConfigSettings = config.configSettings
  106. config.ensureInitialized(completionHandler: { (error: Error?) in })
  107. config.fetch(completionHandler: { (status: FirebaseRemoteConfig.RemoteConfigFetchStatus,
  108. error: Error?) in })
  109. config.fetch()
  110. config.fetch(
  111. withExpirationDuration: TimeInterval(100),
  112. completionHandler: { (status: FirebaseRemoteConfig.RemoteConfigFetchStatus, error: Error?) in
  113. }
  114. )
  115. config.fetch(withExpirationDuration: TimeInterval(100))
  116. config
  117. .fetchAndActivate(
  118. completionHandler: { (status: FirebaseRemoteConfig.RemoteConfigFetchAndActivateStatus,
  119. error: Error?) in }
  120. )
  121. config.fetchAndActivate()
  122. config.activate(completion: { (success: Bool, error: Error?) in })
  123. config.activate()
  124. if #available(iOS 13.0, *) {
  125. Task {
  126. let _: Void = try await config.ensureInitialized()
  127. let _: FirebaseRemoteConfig.RemoteConfigFetchStatus = try await config.fetch()
  128. let _: FirebaseRemoteConfig.RemoteConfigFetchStatus = try await config
  129. .fetch(withExpirationDuration: TimeInterval(100))
  130. let _: FirebaseRemoteConfig.RemoteConfigFetchAndActivateStatus = try await config
  131. .fetchAndActivate()
  132. let _: Bool = try await config.activate()
  133. }
  134. }
  135. let _: FirebaseRemoteConfig.RemoteConfigValue = config["key"]
  136. let _: FirebaseRemoteConfig.RemoteConfigValue = config.configValue(forKey: "key")
  137. // TODO(ncooke3): Should `nil` be acceptable here in a Swift context?
  138. let _: FirebaseRemoteConfig.RemoteConfigValue = config.configValue(forKey: "key")
  139. let _: FirebaseRemoteConfig.RemoteConfigValue = config.configValue(
  140. forKey: "key",
  141. source: source
  142. )
  143. // TODO(ncooke3): Should `nil` be acceptable here in a Swift context?
  144. let _: FirebaseRemoteConfig.RemoteConfigValue = config.configValue(
  145. forKey: "key",
  146. source: source
  147. )
  148. let _: [String] = config.allKeys(from: source)
  149. let _: Set<String> = config.keys(withPrefix: "")
  150. // TODO(ncooke3): Should `nil` be acceptable here in a Swift context?
  151. let _: Set<String> = config.keys(withPrefix: nil)
  152. let defaults: [String: NSObject]? = [:]
  153. config.setDefaults(defaults)
  154. // TODO(ncooke3): Should `nil` be acceptable here in a Swift context?
  155. config.setDefaults(nil)
  156. config.setDefaults(fromPlist: "")
  157. // TODO(ncooke3): Should `nil` be acceptable here in a Swift context?
  158. config.setDefaults(fromPlist: nil)
  159. let _: FirebaseRemoteConfig.RemoteConfigValue? = config.defaultValue(forKey: "")
  160. let _: FirebaseRemoteConfig.RemoteConfigValue? = config.defaultValue(forKey: "key")
  161. let _: FirebaseRemoteConfig.ConfigUpdateListenerRegistration = config
  162. .addOnConfigUpdateListener(
  163. remoteConfigUpdateCompletion: { (update: FirebaseRemoteConfig.RemoteConfigUpdate?,
  164. error: Error?) in
  165. }
  166. )
  167. let valueError: FirebaseRemoteConfig
  168. .RemoteConfigValueCodableError = .unsupportedType("foo")
  169. switch valueError {
  170. case .unsupportedType: break
  171. }
  172. let error: FirebaseRemoteConfig.RemoteConfigCodableError = .invalidSetDefaultsInput("foo")
  173. switch error {
  174. case .invalidSetDefaultsInput: break
  175. }
  176. @available(iOS 14.0, macOS 11.0, macCatalyst 14.0, tvOS 14.0, watchOS 7.0, *)
  177. struct PropertyWrapperTester {
  178. @FirebaseRemoteConfig.RemoteConfigProperty(key: "", fallback: "")
  179. var stringValue: String!
  180. }
  181. struct MyDecodableValue: Decodable {}
  182. let _: MyDecodableValue? = config[decodedValue: ""]
  183. let _: [String: AnyHashable]? = config[jsonValue: ""]
  184. let _: MyDecodableValue = try value.decoded()
  185. let _: MyDecodableValue = try value.decoded(asType: MyDecodableValue.self)
  186. let _: MyDecodableValue? = try config.decoded()
  187. let _: MyDecodableValue? = try config.decoded(asType: MyDecodableValue.self)
  188. struct MyEncodableValue: Encodable {}
  189. let _: Void = try config.setDefaults(from: MyEncodableValue())
  190. Task {
  191. let signals: [String: CustomSignalValue?] = [
  192. "signal_1": .integer(5),
  193. "signal_2": .string("enable_feature"),
  194. "signal_3": 5,
  195. "signal_4": "enable_feature",
  196. "signal_5": "enable_feature_\("secret")",
  197. "signal_6": .double(3.14),
  198. "signal_7": 3.14159,
  199. "signal_8": nil, // Used to delete the custom signal for a given key.
  200. ]
  201. try await config.setCustomSignals(signals)
  202. }
  203. }
  204. }