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