FirebaseOptionsTests.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.appGroupID)
  79. options.appGroupID = Constants.Options.appGroupID
  80. XCTAssertEqual(options.appGroupID, Constants.Options.appGroupID)
  81. }
  82. func testCopyingProperties() {
  83. let googleAppID = Constants.Options.googleAppID
  84. let gcmSenderID = Constants.Options.gcmSenderID
  85. let options = FirebaseOptions(googleAppID: googleAppID,
  86. gcmSenderID: gcmSenderID)
  87. var apiKey = "123456789"
  88. options.apiKey = apiKey
  89. XCTAssertEqual(options.apiKey, apiKey)
  90. apiKey = "000000000"
  91. XCTAssertNotEqual(options.apiKey, apiKey)
  92. }
  93. func testOptionsEquality() throws {
  94. let defaultOptions1 = try XCTUnwrap(
  95. FirebaseOptions.defaultOptions(),
  96. "Default options could not be unwrapped"
  97. )
  98. let defaultOptions2 = try XCTUnwrap(
  99. FirebaseOptions.defaultOptions(),
  100. "Default options could not be unwrapped"
  101. )
  102. XCTAssertEqual(defaultOptions1.hash, defaultOptions2.hash)
  103. XCTAssertTrue(defaultOptions1.isEqual(defaultOptions2))
  104. let plainOptions = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  105. gcmSenderID: Constants.Options.gcmSenderID)
  106. XCTAssertFalse(plainOptions.isEqual(defaultOptions1))
  107. }
  108. // MARK: - Helpers
  109. private func assertOptionsMatchDefaultOptions(options: FirebaseOptions) {
  110. XCTAssertEqual(options.apiKey, Constants.Options.apiKey)
  111. XCTAssertEqual(options.bundleID, Constants.Options.bundleID)
  112. XCTAssertEqual(options.clientID, Constants.Options.clientID)
  113. XCTAssertEqual(options.gcmSenderID, Constants.Options.gcmSenderID)
  114. XCTAssertEqual(options.projectID, Constants.Options.projectID)
  115. XCTAssertEqual(options.googleAppID, Constants.Options.googleAppID)
  116. XCTAssertEqual(options.databaseURL, Constants.Options.databaseURL)
  117. XCTAssertEqual(options.storageBucket, Constants.Options.storageBucket)
  118. XCTAssertNil(options.appGroupID)
  119. }
  120. private func assertNullableOptionsAreEmpty(options: FirebaseOptions) {
  121. XCTAssertNil(options.apiKey)
  122. XCTAssertNil(options.clientID)
  123. XCTAssertNil(options.projectID)
  124. XCTAssertNil(options.databaseURL)
  125. XCTAssertNil(options.storageBucket)
  126. XCTAssertNil(options.appGroupID)
  127. }
  128. }