FirebaseRemoteConfigSwift_APIBuildTests.swift 8.7 KB

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