FirebaseOptionsTests.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 gcmSenderID = "custom_gcm_sender_id"
  44. let options = FirebaseOptions(googleAppID: googleAppID,
  45. gcmSenderID: gcmSenderID)
  46. XCTAssertEqual(options.googleAppID, googleAppID)
  47. XCTAssertEqual(options.gcmSenderID, gcmSenderID)
  48. let bundleID =
  49. try XCTUnwrap(Bundle.main.bundleIdentifier, "Could not retrieve bundle identifier")
  50. XCTAssertEqual(options.bundleID, bundleID)
  51. assertNullableOptionsAreEmpty(options: options)
  52. }
  53. func testCustomizedOptions() {
  54. let googleAppID = Constants.Options.googleAppID
  55. let gcmSenderID = Constants.Options.gcmSenderID
  56. let options = FirebaseOptions(googleAppID: googleAppID,
  57. gcmSenderID: gcmSenderID)
  58. options.bundleID = Constants.Options.bundleID
  59. options.apiKey = Constants.Options.apiKey
  60. options.clientID = Constants.Options.clientID
  61. options.projectID = Constants.Options.projectID
  62. options.databaseURL = Constants.Options.databaseURL
  63. options.storageBucket = Constants.Options.storageBucket
  64. options.appGroupID = Constants.Options.appGroupID
  65. assertOptionsMatchDefaultOptions(options: options)
  66. }
  67. func testEditingCustomOptions() {
  68. let googleAppID = Constants.Options.googleAppID
  69. let gcmSenderID = Constants.Options.gcmSenderID
  70. let options = FirebaseOptions(googleAppID: googleAppID,
  71. gcmSenderID: gcmSenderID)
  72. let newGCMSenderID = "newgcmSenderID"
  73. options.gcmSenderID = newGCMSenderID
  74. XCTAssertEqual(options.gcmSenderID, newGCMSenderID)
  75. let newGoogleAppID = "newGoogleAppID"
  76. options.googleAppID = newGoogleAppID
  77. XCTAssertEqual(options.googleAppID, newGoogleAppID)
  78. XCTAssertNil(options.deepLinkURLScheme)
  79. options.deepLinkURLScheme = Constants.Options.deepLinkURLScheme
  80. XCTAssertEqual(options.deepLinkURLScheme, Constants.Options.deepLinkURLScheme)
  81. XCTAssertNil(options.appGroupID)
  82. options.appGroupID = Constants.Options.appGroupID
  83. XCTAssertEqual(options.appGroupID, Constants.Options.appGroupID)
  84. }
  85. func testCopyingProperties() {
  86. let googleAppID = Constants.Options.googleAppID
  87. let gcmSenderID = Constants.Options.gcmSenderID
  88. let options = FirebaseOptions(googleAppID: googleAppID,
  89. gcmSenderID: gcmSenderID)
  90. var apiKey = "123456789"
  91. options.apiKey = apiKey
  92. XCTAssertEqual(options.apiKey, apiKey)
  93. apiKey = "000000000"
  94. XCTAssertNotEqual(options.apiKey, apiKey)
  95. var deepLinkURLScheme = "comdeeplinkurl"
  96. options.deepLinkURLScheme = deepLinkURLScheme
  97. XCTAssertEqual(options.deepLinkURLScheme, deepLinkURLScheme)
  98. deepLinkURLScheme = "comlinkurl"
  99. XCTAssertNotEqual(options.deepLinkURLScheme, deepLinkURLScheme)
  100. }
  101. func testOptionsEquality() throws {
  102. let defaultOptions1 = try XCTUnwrap(
  103. FirebaseOptions.defaultOptions(),
  104. "Default options could not be unwrapped"
  105. )
  106. let defaultOptions2 = try XCTUnwrap(
  107. FirebaseOptions.defaultOptions(),
  108. "Default options could not be unwrapped"
  109. )
  110. XCTAssertEqual(defaultOptions1.hash, defaultOptions2.hash)
  111. XCTAssertTrue(defaultOptions1.isEqual(defaultOptions2))
  112. let plainOptions = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  113. gcmSenderID: Constants.Options.gcmSenderID)
  114. XCTAssertFalse(plainOptions.isEqual(defaultOptions1))
  115. }
  116. // MARK: - Helpers
  117. private func assertOptionsMatchDefaultOptions(options: FirebaseOptions) {
  118. XCTAssertEqual(options.apiKey, Constants.Options.apiKey)
  119. XCTAssertEqual(options.bundleID, Constants.Options.bundleID)
  120. XCTAssertEqual(options.clientID, Constants.Options.clientID)
  121. XCTAssertEqual(options.gcmSenderID, Constants.Options.gcmSenderID)
  122. XCTAssertEqual(options.projectID, Constants.Options.projectID)
  123. XCTAssertEqual(options.googleAppID, Constants.Options.googleAppID)
  124. XCTAssertEqual(options.databaseURL, Constants.Options.databaseURL)
  125. XCTAssertNil(options.deepLinkURLScheme)
  126. XCTAssertEqual(options.storageBucket, Constants.Options.storageBucket)
  127. XCTAssertNil(options.appGroupID)
  128. }
  129. private func assertNullableOptionsAreEmpty(options: FirebaseOptions) {
  130. XCTAssertNil(options.apiKey)
  131. XCTAssertNil(options.clientID)
  132. XCTAssertNil(options.projectID)
  133. XCTAssertNil(options.databaseURL)
  134. XCTAssertNil(options.deepLinkURLScheme)
  135. XCTAssertNil(options.storageBucket)
  136. XCTAssertNil(options.appGroupID)
  137. }
  138. }