FirebaseAppTests.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 XCTest
  15. @testable import FirebaseCore
  16. private extension Constants {
  17. static let testAppName1 = "test_app_name_1"
  18. static let testAppName2 = "test_app_name_2"
  19. }
  20. class FirebaseAppTests: XCTestCase {
  21. override func setUp() {
  22. super.setUp()
  23. FIROptionsMock.mockFIROptions()
  24. }
  25. override func tearDown() {
  26. super.tearDown()
  27. FirebaseApp.resetApps()
  28. }
  29. func testSwiftFlagWithSwift() {
  30. XCTAssertTrue(FirebaseApp.firebaseUserAgent().contains("swift"))
  31. }
  32. func testConfigure() throws {
  33. expectAppConfigurationNotification(appName: Constants.App.defaultName, isDefaultApp: true)
  34. let configurationAttempt = {
  35. try ExceptionCatcher.catchException {
  36. FirebaseApp.configure()
  37. }
  38. }
  39. XCTAssertNoThrow(try configurationAttempt())
  40. let app = try XCTUnwrap(FirebaseApp.app(), "Failed to unwrap default app")
  41. XCTAssertEqual(app.name, Constants.App.defaultName)
  42. XCTAssertEqual(app.options.clientID, Constants.Options.clientID)
  43. XCTAssertEqual(FirebaseApp.allApps?.count, 1)
  44. // TODO: check registered libraries instances available
  45. waitForExpectations(timeout: 1)
  46. }
  47. func testIsDefaultAppConfigured() {
  48. XCTAssertFalse(FirebaseApp.isDefaultAppConfigured())
  49. expectAppConfigurationNotification(appName: Constants.App.defaultName, isDefaultApp: true)
  50. let configurationAttempt = {
  51. try ExceptionCatcher.catchException {
  52. FirebaseApp.configure()
  53. }
  54. }
  55. XCTAssertNoThrow(try configurationAttempt())
  56. XCTAssertTrue(FirebaseApp.isDefaultAppConfigured())
  57. waitForExpectations(timeout: 1)
  58. }
  59. func testConfigureDefaultAppTwice() {
  60. let firstConfigurationAttempt = {
  61. try ExceptionCatcher.catchException {
  62. FirebaseApp.configure()
  63. }
  64. }
  65. XCTAssertNoThrow(try firstConfigurationAttempt())
  66. let secondConfigurationAttempt = {
  67. try ExceptionCatcher.catchException {
  68. FirebaseApp.configure()
  69. }
  70. }
  71. XCTAssertThrowsError(try secondConfigurationAttempt())
  72. }
  73. func testConfigureWithOptions() throws {
  74. expectAppConfigurationNotification(appName: Constants.App.defaultName, isDefaultApp: true)
  75. let options = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  76. gcmSenderID: Constants.Options.gcmSenderID)
  77. options.clientID = Constants.Options.clientID
  78. let configurationAttempt = {
  79. try ExceptionCatcher.catchException {
  80. FirebaseApp.configure(options: options)
  81. }
  82. }
  83. XCTAssertNoThrow(try configurationAttempt())
  84. let app = try XCTUnwrap(FirebaseApp.app(), "Failed to unwrap default app")
  85. XCTAssertEqual(app.name, Constants.App.defaultName)
  86. XCTAssertEqual(app.options.googleAppID, Constants.Options.googleAppID)
  87. XCTAssertEqual(app.options.gcmSenderID, Constants.Options.gcmSenderID)
  88. XCTAssertEqual(app.options.clientID, Constants.Options.clientID)
  89. XCTAssertTrue(FirebaseApp.allApps?.count == 1)
  90. waitForExpectations(timeout: 1)
  91. }
  92. func testConfigureWithNameAndOptions() throws {
  93. expectAppConfigurationNotification(appName: Constants.testAppName1, isDefaultApp: false)
  94. let options = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  95. gcmSenderID: Constants.Options.gcmSenderID)
  96. options.clientID = Constants.Options.clientID
  97. let configurationAttempt = {
  98. try ExceptionCatcher.catchException {
  99. FirebaseApp.configure(name: Constants.testAppName1, options: options)
  100. }
  101. }
  102. XCTAssertNoThrow(try configurationAttempt())
  103. let app = try XCTUnwrap(
  104. FirebaseApp.app(name: Constants.testAppName1),
  105. "Failed to unwrap custom named app"
  106. )
  107. XCTAssertEqual(app.name, Constants.testAppName1)
  108. XCTAssertEqual(app.options.googleAppID, Constants.Options.googleAppID)
  109. XCTAssertEqual(app.options.gcmSenderID, Constants.Options.gcmSenderID)
  110. XCTAssertEqual(app.options.clientID, Constants.Options.clientID)
  111. XCTAssertTrue(FirebaseApp.allApps?.count == 1)
  112. let configureAppAgain = {
  113. try ExceptionCatcher.catchException {
  114. FirebaseApp.configure(name: Constants.testAppName1, options: options)
  115. }
  116. }
  117. XCTAssertThrowsError(try configureAppAgain())
  118. waitForExpectations(timeout: 1)
  119. }
  120. func testConfigureMultipleApps() throws {
  121. let options1 = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  122. gcmSenderID: Constants.Options.gcmSenderID)
  123. options1.deepLinkURLScheme = Constants.Options.deepLinkURLScheme
  124. expectAppConfigurationNotification(appName: Constants.testAppName1, isDefaultApp: false)
  125. XCTAssertNoThrow(FirebaseApp.configure(name: Constants.testAppName1, options: options1))
  126. let app1 = try XCTUnwrap(FirebaseApp.app(name: Constants.testAppName1), "Failed to unwrap app1")
  127. XCTAssertEqual(app1.name, Constants.testAppName1)
  128. XCTAssertEqual(app1.options.googleAppID, Constants.Options.googleAppID)
  129. XCTAssertEqual(app1.options.gcmSenderID, Constants.Options.gcmSenderID)
  130. XCTAssertEqual(app1.options.deepLinkURLScheme, Constants.Options.deepLinkURLScheme)
  131. XCTAssertTrue(FirebaseApp.allApps?.count == 1)
  132. // Configure a different app with valid customized options.
  133. let options2 = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  134. gcmSenderID: Constants.Options.gcmSenderID)
  135. options2.bundleID = Constants.Options.bundleID
  136. options2.apiKey = Constants.Options.apiKey
  137. expectAppConfigurationNotification(appName: Constants.testAppName2, isDefaultApp: false)
  138. let configureApp2Attempt = {
  139. try ExceptionCatcher.catchException {
  140. FirebaseApp.configure(name: Constants.testAppName2, options: options2)
  141. }
  142. }
  143. XCTAssertNoThrow(try configureApp2Attempt())
  144. let app2 = try XCTUnwrap(FirebaseApp.app(name: Constants.testAppName2), "Failed to unwrap app2")
  145. XCTAssertEqual(app2.name, Constants.testAppName2)
  146. XCTAssertEqual(app2.options.googleAppID, Constants.Options.googleAppID)
  147. XCTAssertEqual(app2.options.gcmSenderID, Constants.Options.gcmSenderID)
  148. XCTAssertEqual(app2.options.bundleID, Constants.Options.bundleID)
  149. XCTAssertEqual(app2.options.apiKey, Constants.Options.apiKey)
  150. XCTAssertTrue(FirebaseApp.allApps?.count == 2)
  151. waitForExpectations(timeout: 1)
  152. }
  153. func testGetUnitializedDefaultApp() {
  154. let app = FirebaseApp.app()
  155. XCTAssertNil(app)
  156. }
  157. func testGetInitializedDefaultApp() {
  158. FirebaseApp.configure()
  159. let app = FirebaseApp.app()
  160. XCTAssertNotNil(app)
  161. }
  162. func testGetExistingAppWithName() throws {
  163. // Configure a different app with valid customized options.
  164. let options = try XCTUnwrap(FirebaseOptions.defaultOptions(), "Could not load default options")
  165. FirebaseApp.configure(name: Constants.testAppName1, options: options)
  166. let app = FirebaseApp.app(name: Constants.testAppName1)
  167. XCTAssertNotNil(app, "Failed to get app")
  168. }
  169. func testAttemptToGetNonExistingAppWithName() {
  170. let unknownAppName = "The Missing App"
  171. let app = FirebaseApp.app(name: unknownAppName)
  172. XCTAssertNil(app)
  173. }
  174. func testAllApps() throws {
  175. XCTAssertNil(FirebaseApp.allApps)
  176. let options1 = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  177. gcmSenderID: Constants.Options.gcmSenderID)
  178. FirebaseApp.configure(name: Constants.testAppName1, options: options1)
  179. let app1 = try XCTUnwrap(
  180. FirebaseApp.app(name: Constants.testAppName1),
  181. "App1 could not be unwrapped"
  182. )
  183. let options2 = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  184. gcmSenderID: Constants.Options.gcmSenderID)
  185. FirebaseApp.configure(name: Constants.testAppName2, options: options2)
  186. let app2 = try XCTUnwrap(
  187. FirebaseApp.app(name: Constants.testAppName2),
  188. "App2 could not be unwrapped"
  189. )
  190. let apps = try XCTUnwrap(FirebaseApp.allApps, "Could not retrieve apps")
  191. XCTAssertEqual(apps.count, 2)
  192. XCTAssertTrue(apps.keys.contains(Constants.testAppName1))
  193. XCTAssertEqual(apps[Constants.testAppName1], app1)
  194. XCTAssertTrue(apps.keys.contains(Constants.testAppName2))
  195. XCTAssertEqual(apps[Constants.testAppName2], app2)
  196. }
  197. func testDeleteApp() throws {
  198. XCTAssertNil(FirebaseApp.app(name: Constants.testAppName1))
  199. XCTAssertNil(FirebaseApp.allApps)
  200. expectAppConfigurationNotification(appName: Constants.testAppName1, isDefaultApp: false)
  201. let options = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  202. gcmSenderID: Constants.Options.gcmSenderID)
  203. FirebaseApp.configure(name: Constants.testAppName1, options: options)
  204. let app = try XCTUnwrap(FirebaseApp.app(name: Constants.testAppName1), "Could not unwrap app")
  205. let apps = try XCTUnwrap(FirebaseApp.allApps, "Could not retrieve app dictionary")
  206. XCTAssertTrue(apps.keys.contains(app.name))
  207. let appDeletedExpectation = expectation(description: #function)
  208. app.delete { success in
  209. XCTAssertTrue(success)
  210. XCTAssertFalse(FirebaseApp.allApps?.keys.contains(Constants.testAppName1) ?? false)
  211. appDeletedExpectation.fulfill()
  212. }
  213. waitForExpectations(timeout: 1)
  214. }
  215. func testGetNameOfDefaultApp() throws {
  216. FirebaseApp.configure()
  217. let defaultApp = try XCTUnwrap(FirebaseApp.app(), "Could not unwrap default app")
  218. XCTAssertEqual(defaultApp.name, Constants.App.defaultName)
  219. }
  220. func testGetNameOfApp() throws {
  221. XCTAssertNil(FirebaseApp.app(name: Constants.testAppName1))
  222. let options = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  223. gcmSenderID: Constants.Options.gcmSenderID)
  224. FirebaseApp.configure(name: Constants.testAppName1, options: options)
  225. let app = try XCTUnwrap(
  226. FirebaseApp.app(name: Constants.testAppName1),
  227. "Failed to unwrap custom named app"
  228. )
  229. XCTAssertEqual(app.name, Constants.testAppName1)
  230. }
  231. func testOptionsForApp() throws {
  232. FirebaseApp.configure()
  233. let defaultApp = try XCTUnwrap(FirebaseApp.app(), "Could not unwrap default app")
  234. let defaultOptions = FirebaseOptions.defaultOptions()
  235. XCTAssertEqual(defaultApp.options, defaultOptions)
  236. let options = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  237. gcmSenderID: Constants.Options.gcmSenderID)
  238. let superSecretURLScheme = "com.supersecret.googledeeplinkurl"
  239. options.deepLinkURLScheme = superSecretURLScheme
  240. FirebaseApp.configure(name: Constants.testAppName1, options: options)
  241. let app = try XCTUnwrap(
  242. FirebaseApp.app(name: Constants.testAppName1),
  243. "Could not unwrap custom named app"
  244. )
  245. XCTAssertEqual(app.name, Constants.testAppName1)
  246. XCTAssertEqual(app.options.googleAppID, Constants.Options.googleAppID)
  247. XCTAssertEqual(app.options.gcmSenderID, Constants.Options.gcmSenderID)
  248. XCTAssertEqual(app.options.deepLinkURLScheme, superSecretURLScheme)
  249. XCTAssertNil(app.options.androidClientID)
  250. }
  251. func testFirebaseDataCollectionDefaultEnabled() throws {
  252. let app = FirebaseApp(instanceWithName: "emptyApp",
  253. options: FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  254. gcmSenderID: Constants.Options.gcmSenderID))
  255. // defaults to true unless otherwise set to no in app's Info.plist
  256. XCTAssertTrue(app.isDataCollectionDefaultEnabled)
  257. app.isDataCollectionDefaultEnabled = false
  258. XCTAssertFalse(app.isDataCollectionDefaultEnabled)
  259. // Cleanup
  260. app.isDataCollectionDefaultEnabled = true
  261. let expecation = expectation(description: #function)
  262. app.delete { success in
  263. expecation.fulfill()
  264. }
  265. waitForExpectations(timeout: 1)
  266. }
  267. // MARK: - Firebase User Agent
  268. func testFirebaseUserAgent_SwiftRuntime() {
  269. XCTAssertTrue(FirebaseApp.firebaseUserAgent().contains("swift/true"))
  270. }
  271. // MARK: - Helpers
  272. private func expectAppConfigurationNotification(appName: String, isDefaultApp: Bool) {
  273. let expectedUserInfo: NSDictionary = [
  274. "FIRAppNameKey": appName,
  275. "FIRAppIsDefaultAppKey": NSNumber(value: isDefaultApp),
  276. "FIRGoogleAppIDKey": Constants.Options.googleAppID,
  277. ]
  278. expectation(forNotification: NSNotification.Name.firAppReadyToConfigureSDK,
  279. object: FirebaseApp.self, handler: { (notification) -> Bool in
  280. if let userInfo = notification.userInfo {
  281. if expectedUserInfo.isEqual(to: userInfo) {
  282. return true
  283. }
  284. } else {
  285. XCTFail("Failed to unwrap notification user info")
  286. }
  287. return false
  288. })
  289. }
  290. }