FirebaseOptionsTests.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 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.trackingID = Constants.Options.trackingID
  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 googleAppID = Constants.Options.googleAppID
  70. let gcmSenderID = Constants.Options.gcmSenderID
  71. let options = FirebaseOptions(googleAppID: googleAppID,
  72. gcmSenderID: gcmSenderID)
  73. let newGCMSenderID = "newgcmSenderID"
  74. options.gcmSenderID = newGCMSenderID
  75. XCTAssertEqual(options.gcmSenderID, newGCMSenderID)
  76. let newGoogleAppID = "newGoogleAppID"
  77. options.googleAppID = newGoogleAppID
  78. XCTAssertEqual(options.googleAppID, newGoogleAppID)
  79. XCTAssertNil(options.deepLinkURLScheme)
  80. options.deepLinkURLScheme = Constants.Options.deepLinkURLScheme
  81. XCTAssertEqual(options.deepLinkURLScheme, Constants.Options.deepLinkURLScheme)
  82. XCTAssertNil(options.androidClientID)
  83. options.androidClientID = Constants.Options.androidClientID
  84. XCTAssertEqual(options.androidClientID, Constants.Options.androidClientID)
  85. XCTAssertNil(options.appGroupID)
  86. options.appGroupID = Constants.Options.appGroupID
  87. XCTAssertEqual(options.appGroupID, Constants.Options.appGroupID)
  88. }
  89. func testCopyingProperties() {
  90. let googleAppID = Constants.Options.googleAppID
  91. let gcmSenderID = Constants.Options.gcmSenderID
  92. let options = FirebaseOptions(googleAppID: googleAppID,
  93. gcmSenderID: gcmSenderID)
  94. var apiKey = "123456789"
  95. options.apiKey = apiKey
  96. XCTAssertEqual(options.apiKey, apiKey)
  97. apiKey = "000000000"
  98. XCTAssertNotEqual(options.apiKey, apiKey)
  99. var deepLinkURLScheme = "comdeeplinkurl"
  100. options.deepLinkURLScheme = deepLinkURLScheme
  101. XCTAssertEqual(options.deepLinkURLScheme, deepLinkURLScheme)
  102. deepLinkURLScheme = "comlinkurl"
  103. XCTAssertNotEqual(options.deepLinkURLScheme, deepLinkURLScheme)
  104. }
  105. func testOptionsEquality() throws {
  106. let defaultOptions1 = try XCTUnwrap(
  107. FirebaseOptions.defaultOptions(),
  108. "Default options could not be unwrapped"
  109. )
  110. let defaultOptions2 = try XCTUnwrap(
  111. FirebaseOptions.defaultOptions(),
  112. "Default options could not be unwrapped"
  113. )
  114. XCTAssertEqual(defaultOptions1.hash, defaultOptions2.hash)
  115. XCTAssertTrue(defaultOptions1.isEqual(defaultOptions2))
  116. let plainOptions = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
  117. gcmSenderID: Constants.Options.gcmSenderID)
  118. XCTAssertFalse(plainOptions.isEqual(defaultOptions1))
  119. }
  120. // MARK: - Helpers
  121. private func assertOptionsMatchDefaultOptions(options: FirebaseOptions) {
  122. XCTAssertEqual(options.apiKey, Constants.Options.apiKey)
  123. XCTAssertEqual(options.bundleID, Constants.Options.bundleID)
  124. XCTAssertEqual(options.clientID, Constants.Options.clientID)
  125. XCTAssertEqual(options.trackingID, Constants.Options.trackingID)
  126. XCTAssertEqual(options.gcmSenderID, Constants.Options.gcmSenderID)
  127. XCTAssertEqual(options.projectID, Constants.Options.projectID)
  128. XCTAssertNil(options.androidClientID)
  129. XCTAssertEqual(options.googleAppID, Constants.Options.googleAppID)
  130. XCTAssertEqual(options.databaseURL, Constants.Options.databaseURL)
  131. XCTAssertNil(options.deepLinkURLScheme)
  132. XCTAssertEqual(options.storageBucket, Constants.Options.storageBucket)
  133. XCTAssertNil(options.appGroupID)
  134. }
  135. private func assertNullableOptionsAreEmpty(options: FirebaseOptions) {
  136. XCTAssertNil(options.apiKey)
  137. XCTAssertNil(options.clientID)
  138. XCTAssertNil(options.trackingID)
  139. XCTAssertNil(options.projectID)
  140. XCTAssertNil(options.androidClientID)
  141. XCTAssertNil(options.databaseURL)
  142. XCTAssertNil(options.deepLinkURLScheme)
  143. XCTAssertNil(options.storageBucket)
  144. XCTAssertNil(options.appGroupID)
  145. }
  146. }