Skip to content

Commit 8e3c0a7

Browse files
authored
Added Core app swift tests (#5678)
* Added Core app swift tests * Resolved feedback * Resolved feedback 2.0 * Resolved feedback 3.0 * Resolved feedback 4.0 * Skip travis pod lib lint tests for xcode 10.3 * Fixed attempt to skip tests * Undid change to core.yml * Reordered constants and removed an unneccesary one * FirebaseOptions keys now match Constants.Options * Resolved minor nits
1 parent 6ff9949 commit 8e3c0a7

File tree

7 files changed

+445
-10
lines changed

7 files changed

+445
-10
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
env:
3030
- PROJECT=Core METHOD=pod-lib-lint
3131
script:
32-
- travis_retry ./scripts/if_changed.sh ./scripts/pod_lib_lint.rb FirebaseCore.podspec --platforms=ios
32+
- travis_retry ./scripts/if_changed.sh ./scripts/pod_lib_lint.rb FirebaseCore.podspec --skip-tests --platforms=ios
3333
- travis_retry ./scripts/if_changed.sh ./scripts/pod_lib_lint.rb FirebaseCoreDiagnostics.podspec --platforms=ios
3434

3535
- stage: test

FirebaseCore.podspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ Firebase Core includes FIRApp and FIROptions which provide central configuration
5555
s.test_spec 'swift-unit' do |swift_unit_tests|
5656
swift_unit_tests.platforms = {:ios => '8.0', :osx => '10.11', :tvos => '10.0'}
5757
swift_unit_tests.source_files = 'FirebaseCore/Tests/Unit/Swift/**/*.swift',
58-
'FirebaseCore/Tests/Unit/Swift/**/*.h'
58+
'FirebaseCore/Tests/Unit/Swift/**/*.h',
59+
'FirebaseCore/Tests/Unit/Swift/SwiftTestingUtilities/*'
60+
swift_unit_tests.resources = 'FirebaseCore/Tests/Unit/Resources/GoogleService-Info.plist'
5961
swift_unit_tests.pod_target_xcconfig = {
6062
'SWIFT_OBJC_BRIDGING_HEADER' => '$(PODS_TARGET_SRCROOT)/FirebaseCore/Tests/Unit/Swift/FirebaseCore-unit-Bridging-Header.h'
6163
}
Lines changed: 350 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,350 @@
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+
private extension Constants {
19+
static let testAppName1 = "test_app_name_1"
20+
static let testAppName2 = "test_app_name_2"
21+
}
22+
23+
class FirebaseAppTests: XCTestCase {
24+
override func setUp() {
25+
super.setUp()
26+
}
27+
28+
override func tearDown() {
29+
super.tearDown()
30+
FirebaseApp.resetApps()
31+
}
32+
33+
func testSwiftFlagWithSwift() {
34+
XCTAssertTrue(FirebaseApp.firebaseUserAgent().contains("swift"))
35+
}
36+
37+
func testConfigure() throws {
38+
expectAppConfigurationNotification(appName: Constants.App.defaultName, isDefaultApp: true)
39+
40+
let configurationAttempt = {
41+
try ExceptionCatcher.catchException {
42+
FirebaseApp.configure()
43+
}
44+
}
45+
XCTAssertNoThrow(try configurationAttempt())
46+
47+
let app = try XCTUnwrap(FirebaseApp.app(), "Failed to unwrap default app")
48+
XCTAssertEqual(app.name, Constants.App.defaultName)
49+
XCTAssertEqual(app.options.clientID, Constants.Options.clientID)
50+
XCTAssertEqual(FirebaseApp.allApps?.count, 1)
51+
52+
// TODO: check registered libraries instances available
53+
54+
waitForExpectations(timeout: 1)
55+
}
56+
57+
func testIsDefaultAppConfigured() {
58+
XCTAssertFalse(FirebaseApp.isDefaultAppConfigured())
59+
60+
expectAppConfigurationNotification(appName: Constants.App.defaultName, isDefaultApp: true)
61+
62+
let configurationAttempt = {
63+
try ExceptionCatcher.catchException {
64+
FirebaseApp.configure()
65+
}
66+
}
67+
XCTAssertNoThrow(try configurationAttempt())
68+
XCTAssertTrue(FirebaseApp.isDefaultAppConfigured())
69+
70+
waitForExpectations(timeout: 1)
71+
}
72+
73+
func testConfigureDefaultAppTwice() {
74+
let firstConfigurationAttempt = {
75+
try ExceptionCatcher.catchException {
76+
FirebaseApp.configure()
77+
}
78+
}
79+
XCTAssertNoThrow(try firstConfigurationAttempt())
80+
81+
let secondConfigurationAttempt = {
82+
try ExceptionCatcher.catchException {
83+
FirebaseApp.configure()
84+
}
85+
}
86+
XCTAssertThrowsError(try secondConfigurationAttempt())
87+
}
88+
89+
func testConfigureWithOptions() throws {
90+
expectAppConfigurationNotification(appName: Constants.App.defaultName, isDefaultApp: true)
91+
92+
let options = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
93+
gcmSenderID: Constants.Options.gcmSenderID)
94+
options.clientID = Constants.Options.clientID
95+
let configurationAttempt = {
96+
try ExceptionCatcher.catchException {
97+
FirebaseApp.configure(options: options)
98+
}
99+
}
100+
XCTAssertNoThrow(try configurationAttempt())
101+
102+
let app = try XCTUnwrap(FirebaseApp.app(), "Failed to unwrap default app")
103+
XCTAssertEqual(app.name, Constants.App.defaultName)
104+
XCTAssertEqual(app.options.googleAppID, Constants.Options.googleAppID)
105+
XCTAssertEqual(app.options.gcmSenderID, Constants.Options.gcmSenderID)
106+
XCTAssertEqual(app.options.clientID, Constants.Options.clientID)
107+
XCTAssertTrue(FirebaseApp.allApps?.count == 1)
108+
109+
waitForExpectations(timeout: 1)
110+
}
111+
112+
func testConfigureWithNameAndOptions() throws {
113+
expectAppConfigurationNotification(appName: Constants.testAppName1, isDefaultApp: false)
114+
115+
let options = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
116+
gcmSenderID: Constants.Options.gcmSenderID)
117+
options.clientID = Constants.Options.clientID
118+
119+
let configurationAttempt = {
120+
try ExceptionCatcher.catchException {
121+
FirebaseApp.configure(name: Constants.testAppName1, options: options)
122+
}
123+
}
124+
XCTAssertNoThrow(try configurationAttempt())
125+
126+
let app = try XCTUnwrap(
127+
FirebaseApp.app(name: Constants.testAppName1),
128+
"Failed to unwrap custom named app"
129+
)
130+
XCTAssertEqual(app.name, Constants.testAppName1)
131+
XCTAssertEqual(app.options.googleAppID, Constants.Options.googleAppID)
132+
XCTAssertEqual(app.options.gcmSenderID, Constants.Options.gcmSenderID)
133+
XCTAssertEqual(app.options.clientID, Constants.Options.clientID)
134+
XCTAssertTrue(FirebaseApp.allApps?.count == 1)
135+
136+
let configureAppAgain = {
137+
try ExceptionCatcher.catchException {
138+
FirebaseApp.configure(name: Constants.testAppName1, options: options)
139+
}
140+
}
141+
142+
XCTAssertThrowsError(try configureAppAgain())
143+
144+
waitForExpectations(timeout: 1)
145+
}
146+
147+
func testConfigureMultipleApps() throws {
148+
let options1 = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
149+
gcmSenderID: Constants.Options.gcmSenderID)
150+
options1.deepLinkURLScheme = Constants.Options.deepLinkURLScheme
151+
152+
expectAppConfigurationNotification(appName: Constants.testAppName1, isDefaultApp: false)
153+
154+
XCTAssertNoThrow(FirebaseApp.configure(name: Constants.testAppName1, options: options1))
155+
156+
let app1 = try XCTUnwrap(FirebaseApp.app(name: Constants.testAppName1), "Failed to unwrap app1")
157+
XCTAssertEqual(app1.name, Constants.testAppName1)
158+
XCTAssertEqual(app1.options.googleAppID, Constants.Options.googleAppID)
159+
XCTAssertEqual(app1.options.gcmSenderID, Constants.Options.gcmSenderID)
160+
XCTAssertEqual(app1.options.deepLinkURLScheme, Constants.Options.deepLinkURLScheme)
161+
XCTAssertTrue(FirebaseApp.allApps?.count == 1)
162+
163+
// Configure a different app with valid customized options.
164+
let options2 = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
165+
gcmSenderID: Constants.Options.gcmSenderID)
166+
options2.bundleID = Constants.Options.bundleID
167+
options2.apiKey = Constants.Options.apiKey
168+
169+
expectAppConfigurationNotification(appName: Constants.testAppName2, isDefaultApp: false)
170+
171+
let configureApp2Attempt = {
172+
try ExceptionCatcher.catchException {
173+
FirebaseApp.configure(name: Constants.testAppName2, options: options2)
174+
}
175+
}
176+
XCTAssertNoThrow(try configureApp2Attempt())
177+
178+
let app2 = try XCTUnwrap(FirebaseApp.app(name: Constants.testAppName2), "Failed to unwrap app2")
179+
XCTAssertEqual(app2.name, Constants.testAppName2)
180+
XCTAssertEqual(app2.options.googleAppID, Constants.Options.googleAppID)
181+
XCTAssertEqual(app2.options.gcmSenderID, Constants.Options.gcmSenderID)
182+
XCTAssertEqual(app2.options.bundleID, Constants.Options.bundleID)
183+
XCTAssertEqual(app2.options.apiKey, Constants.Options.apiKey)
184+
XCTAssertTrue(FirebaseApp.allApps?.count == 2)
185+
186+
waitForExpectations(timeout: 1)
187+
}
188+
189+
func testGetUnitializedDefaultApp() {
190+
let app = FirebaseApp.app()
191+
XCTAssertNil(app)
192+
}
193+
194+
func testGetInitializedDefaultApp() {
195+
FirebaseApp.configure()
196+
let app = FirebaseApp.app()
197+
XCTAssertNotNil(app)
198+
}
199+
200+
func testGetExistingAppWithName() throws {
201+
// Configure a different app with valid customized options.
202+
let options = try XCTUnwrap(FirebaseOptions.defaultOptions(), "Could not load default options")
203+
FirebaseApp.configure(name: Constants.testAppName1, options: options)
204+
let app = FirebaseApp.app(name: Constants.testAppName1)
205+
XCTAssertNotNil(app, "Failed to get app")
206+
}
207+
208+
func testAttemptToGetNonExistingAppWithName() {
209+
let unknownAppName = "The Missing App"
210+
let app = FirebaseApp.app(name: unknownAppName)
211+
XCTAssertNil(app)
212+
}
213+
214+
func testAllApps() throws {
215+
XCTAssertNil(FirebaseApp.allApps)
216+
217+
let options1 = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
218+
gcmSenderID: Constants.Options.gcmSenderID)
219+
FirebaseApp.configure(name: Constants.testAppName1, options: options1)
220+
let app1 = try XCTUnwrap(
221+
FirebaseApp.app(name: Constants.testAppName1),
222+
"App1 could not be unwrapped"
223+
)
224+
225+
let options2 = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
226+
gcmSenderID: Constants.Options.gcmSenderID)
227+
FirebaseApp.configure(name: Constants.testAppName2, options: options2)
228+
let app2 = try XCTUnwrap(
229+
FirebaseApp.app(name: Constants.testAppName2),
230+
"App2 could not be unwrapped"
231+
)
232+
233+
let apps = try XCTUnwrap(FirebaseApp.allApps, "Could not retrieve apps")
234+
235+
XCTAssertEqual(apps.count, 2)
236+
XCTAssertTrue(apps.keys.contains(Constants.testAppName1))
237+
XCTAssertEqual(apps[Constants.testAppName1], app1)
238+
XCTAssertTrue(apps.keys.contains(Constants.testAppName2))
239+
XCTAssertEqual(apps[Constants.testAppName2], app2)
240+
}
241+
242+
func testDeleteApp() throws {
243+
XCTAssertNil(FirebaseApp.app(name: Constants.testAppName1))
244+
XCTAssertNil(FirebaseApp.allApps)
245+
246+
expectAppConfigurationNotification(appName: Constants.testAppName1, isDefaultApp: false)
247+
248+
let options = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
249+
gcmSenderID: Constants.Options.gcmSenderID)
250+
FirebaseApp.configure(name: Constants.testAppName1, options: options)
251+
252+
let app = try XCTUnwrap(FirebaseApp.app(name: Constants.testAppName1), "Could not unwrap app")
253+
let apps = try XCTUnwrap(FirebaseApp.allApps, "Could not retrieve app dictionary")
254+
XCTAssertTrue(apps.keys.contains(app.name))
255+
let appDeletedExpectation = expectation(description: #function)
256+
app.delete { success in
257+
XCTAssertTrue(success)
258+
XCTAssertFalse(FirebaseApp.allApps?.keys.contains(Constants.testAppName1) ?? false)
259+
appDeletedExpectation.fulfill()
260+
}
261+
262+
waitForExpectations(timeout: 1)
263+
}
264+
265+
func testGetNameOfDefaultApp() throws {
266+
FirebaseApp.configure()
267+
268+
let defaultApp = try XCTUnwrap(FirebaseApp.app(), "Could not unwrap default app")
269+
XCTAssertEqual(defaultApp.name, Constants.App.defaultName)
270+
}
271+
272+
func testGetNameOfApp() throws {
273+
XCTAssertNil(FirebaseApp.app(name: Constants.testAppName1))
274+
275+
let options = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
276+
gcmSenderID: Constants.Options.gcmSenderID)
277+
FirebaseApp.configure(name: Constants.testAppName1, options: options)
278+
279+
let app = try XCTUnwrap(
280+
FirebaseApp.app(name: Constants.testAppName1),
281+
"Failed to unwrap custom named app"
282+
)
283+
XCTAssertEqual(app.name, Constants.testAppName1)
284+
}
285+
286+
func testOptionsForApp() throws {
287+
FirebaseApp.configure()
288+
let defaultApp = try XCTUnwrap(FirebaseApp.app(), "Could not unwrap default app")
289+
let defaultOptions = FirebaseOptions.defaultOptions()
290+
XCTAssertEqual(defaultApp.options, defaultOptions)
291+
292+
let options = FirebaseOptions(googleAppID: Constants.Options.googleAppID,
293+
gcmSenderID: Constants.Options.gcmSenderID)
294+
let superSecretURLScheme = "com.supersecret.googledeeplinkurl"
295+
options.deepLinkURLScheme = superSecretURLScheme
296+
FirebaseApp.configure(name: Constants.testAppName1, options: options)
297+
298+
let app = try XCTUnwrap(
299+
FirebaseApp.app(name: Constants.testAppName1),
300+
"Could not unwrap custom named app"
301+
)
302+
XCTAssertEqual(app.name, Constants.testAppName1)
303+
XCTAssertEqual(app.options.googleAppID, Constants.Options.googleAppID)
304+
XCTAssertEqual(app.options.gcmSenderID, Constants.Options.gcmSenderID)
305+
XCTAssertEqual(app.options.deepLinkURLScheme, superSecretURLScheme)
306+
XCTAssertNil(app.options.androidClientID)
307+
}
308+
309+
func testFirebaseDataCollectionDefaultEnabled() throws {
310+
let app = FirebaseApp(instanceWithName: "emptyApp", options: FirebaseOptions())
311+
312+
// defaults to true unless otherwise set to no in app's Info.plist
313+
XCTAssertTrue(app.isDataCollectionDefaultEnabled)
314+
315+
app.isDataCollectionDefaultEnabled = false
316+
XCTAssertFalse(app.isDataCollectionDefaultEnabled)
317+
318+
// Cleanup
319+
app.isDataCollectionDefaultEnabled = true
320+
321+
let expecation = expectation(description: #function)
322+
app.delete { success in
323+
expecation.fulfill()
324+
}
325+
326+
waitForExpectations(timeout: 1)
327+
}
328+
329+
// MARK: - Helpers
330+
331+
private func expectAppConfigurationNotification(appName: String, isDefaultApp: Bool) {
332+
let expectedUserInfo: NSDictionary = [
333+
"FIRAppNameKey": appName,
334+
"FIRAppIsDefaultAppKey": NSNumber(value: isDefaultApp),
335+
"FIRGoogleAppIDKey": Constants.Options.googleAppID,
336+
]
337+
338+
expectation(forNotification: NSNotification.Name.firAppReadyToConfigureSDK,
339+
object: FirebaseApp.self, handler: { (notification) -> Bool in
340+
if let userInfo = notification.userInfo {
341+
if expectedUserInfo.isEqual(to: userInfo) {
342+
return true
343+
}
344+
} else {
345+
XCTFail("Failed to unwrap notification user info")
346+
}
347+
return false
348+
})
349+
}
350+
}

FirebaseCore/Tests/Unit/Swift/FirebaseCore-unit-Bridging-Header.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
// limitations under the License.
1414

1515
#import <FirebaseCore/FIRAppInternal.h>
16+
#import "ExceptionCatcher.h"

0 commit comments

Comments
 (0)