FirebaseAppTests.swift 13 KB

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