FirebaseAppTests.swift 13 KB

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