FirebaseOptionsTests.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. @testable import FirebaseCore
  15. import XCTest
  16. class FirebaseOptionsTests: XCTestCase {
  17. func testDefaultOptions() throws {
  18. let options = try XCTUnwrap(
  19. FirebaseOptions.defaultOptions(),
  20. "Default options could not be unwrapped"
  21. )
  22. assertOptionsMatchDefaultOptions(options: options)
  23. }
  24. func testInitWithContentsOfFile() throws {
  25. let bundle = try XCTUnwrap(
  26. Bundle(for: type(of: self)),
  27. "Could not find bundle"
  28. )
  29. let path = try XCTUnwrap(
  30. bundle.path(forResource: "GoogleService-Info", ofType: "plist"),
  31. "Could not find path for file"
  32. )
  33. let options = FirebaseOptions(contentsOfFile: path)
  34. XCTAssertNotNil(options)
  35. }
  36. func testInitWithInvalidSourceFile() {
  37. let invalidPath = "path/to/non-existing/plist"
  38. let options = FirebaseOptions(contentsOfFile: invalidPath)
  39. XCTAssertNil(options)
  40. }
  41. func testInitWithCustomFields() throws {
  42. let googleAppID = "5:678:ios:678def"
  43. let projectNumber = "custom_gcm_sender_id"
  44. let options = FirebaseOptions(
  45. appID: googleAppID,
  46. projectNumber: projectNumber,
  47. projectID: Constants.Options.projectID,
  48. apiKey: Constants.Options.apiKey
  49. )
  50. XCTAssertEqual(options.googleAppID, googleAppID)
  51. XCTAssertEqual(options.projectNumber, projectNumber)
  52. let bundleID =
  53. try XCTUnwrap(Bundle.main.bundleIdentifier, "Could not retrieve bundle identifier")
  54. XCTAssertEqual(options.bundleID, bundleID)
  55. assertNullableOptionsAreEmpty(options: options)
  56. }
  57. func testCustomizedOptions() {
  58. let options = appOptions()
  59. options.bundleID = Constants.Options.bundleID
  60. options.apiKey = Constants.Options.apiKey
  61. options.clientID = Constants.Options.clientID
  62. options.projectID = Constants.Options.projectID
  63. options.databaseURL = Constants.Options.databaseURL
  64. options.storageBucket = Constants.Options.storageBucket
  65. options.appGroupID = Constants.Options.appGroupID
  66. assertOptionsMatchDefaultOptions(options: options)
  67. }
  68. func testEditingCustomOptions() {
  69. let options = appOptions()
  70. let newprojectNumber = "newprojectNumber"
  71. options.projectNumber = newprojectNumber
  72. XCTAssertEqual(options.projectNumber, newprojectNumber)
  73. let newGoogleAppID = "newGoogleAppID"
  74. options.googleAppID = newGoogleAppID
  75. XCTAssertEqual(options.googleAppID, newGoogleAppID)
  76. XCTAssertNil(options.deepLinkURLScheme)
  77. options.deepLinkURLScheme = Constants.Options.deepLinkURLScheme
  78. XCTAssertEqual(options.deepLinkURLScheme, Constants.Options.deepLinkURLScheme)
  79. XCTAssertNil(options.appGroupID)
  80. options.appGroupID = Constants.Options.appGroupID
  81. XCTAssertEqual(options.appGroupID, Constants.Options.appGroupID)
  82. }
  83. func testCopyingProperties() {
  84. let options = appOptions()
  85. var apiKey = "123456789"
  86. options.apiKey = apiKey
  87. XCTAssertEqual(options.apiKey, apiKey)
  88. apiKey = "000000000"
  89. XCTAssertNotEqual(options.apiKey, apiKey)
  90. var deepLinkURLScheme = "comdeeplinkurl"
  91. options.deepLinkURLScheme = deepLinkURLScheme
  92. XCTAssertEqual(options.deepLinkURLScheme, deepLinkURLScheme)
  93. deepLinkURLScheme = "comlinkurl"
  94. XCTAssertNotEqual(options.deepLinkURLScheme, deepLinkURLScheme)
  95. }
  96. func testOptionsEquality() throws {
  97. let defaultOptions1 = try XCTUnwrap(
  98. FirebaseOptions.defaultOptions(),
  99. "Default options could not be unwrapped"
  100. )
  101. let defaultOptions2 = try XCTUnwrap(
  102. FirebaseOptions.defaultOptions(),
  103. "Default options could not be unwrapped"
  104. )
  105. XCTAssertEqual(defaultOptions1.hash, defaultOptions2.hash)
  106. XCTAssertTrue(defaultOptions1.isEqual(defaultOptions2))
  107. let plainOptions = appOptions()
  108. XCTAssertFalse(plainOptions.isEqual(defaultOptions1))
  109. }
  110. // MARK: - Helpers
  111. private func assertOptionsMatchDefaultOptions(options: FirebaseOptions) {
  112. XCTAssertEqual(options.apiKey, Constants.Options.apiKey)
  113. XCTAssertEqual(options.bundleID, Constants.Options.bundleID)
  114. XCTAssertEqual(options.clientID, Constants.Options.clientID)
  115. XCTAssertEqual(options.projectNumber, Constants.Options.projectNumber)
  116. XCTAssertEqual(options.projectID, Constants.Options.projectID)
  117. XCTAssertEqual(options.googleAppID, Constants.Options.googleAppID)
  118. XCTAssertEqual(options.databaseURL, Constants.Options.databaseURL)
  119. XCTAssertNil(options.deepLinkURLScheme)
  120. XCTAssertEqual(options.storageBucket, Constants.Options.storageBucket)
  121. XCTAssertNil(options.appGroupID)
  122. }
  123. private func assertNullableOptionsAreEmpty(options: FirebaseOptions) {
  124. XCTAssertNil(options.clientID)
  125. XCTAssertNil(options.databaseURL)
  126. XCTAssertNil(options.deepLinkURLScheme)
  127. XCTAssertNil(options.storageBucket)
  128. XCTAssertNil(options.appGroupID)
  129. }
  130. private func appOptions() -> FirebaseOptions {
  131. return FirebaseOptions(
  132. appID: Constants.Options.googleAppID,
  133. projectNumber: Constants.Options.projectNumber,
  134. projectID: Constants.Options.projectID,
  135. apiKey: Constants.Options.apiKey
  136. )
  137. }
  138. }