Skip to content

Commit 1997f76

Browse files
authored
Installations API build tests (#8353)
* Installations API build tests * Fix class name
1 parent 6444730 commit 1997f76

File tree

2 files changed

+161
-1
lines changed

2 files changed

+161
-1
lines changed

FirebaseInstallations.podspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ Pod::Spec.new do |s|
6060
:tvos => tvos_deployment_target
6161
}
6262
unit_tests.source_files = base_dir + 'Tests/Unit/*.[mh]',
63-
base_dir + 'Tests/Utils/*.[mh]'
63+
base_dir + 'Tests/Utils/*.[mh]',
64+
base_dir + 'Tests/Unit/Swift/*'
6465
unit_tests.resources = base_dir + 'Tests/Fixture/**/*'
6566
unit_tests.requires_app_host = true
6667
unit_tests.dependency 'OCMock'
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
//
2+
// Copyright 2021 Google LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
// MARK: This file is used to evaluate the experience of using Firebase APIs in Swift.
18+
19+
import Foundation
20+
21+
import FirebaseCore
22+
import FirebaseInstallations
23+
24+
final class InstallationsAPITests {
25+
func usage() {
26+
// MARK: - Installations
27+
28+
// `InstallationIDDidChange` & associated notification keys
29+
_ = NotificationCenter.default
30+
.addObserver(
31+
forName: .InstallationIDDidChange,
32+
object: nil,
33+
queue: .main
34+
) { notification in
35+
_ = notification.userInfo?[InstallationIDDidChangeAppNameKey]
36+
}
37+
38+
// Retrieving an Installations instance
39+
_ = Installations.installations()
40+
41+
if let app = FirebaseApp.app() {
42+
_ = Installations.installations(app: app)
43+
}
44+
45+
// Create or retrieve an installations ID
46+
Installations.installations().installationID { id, error in
47+
if let _ /* id */ = id {
48+
// ...
49+
} else if let _ /* error */ = error {
50+
// ...
51+
}
52+
}
53+
54+
#if swift(>=5.5)
55+
if #available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
56+
// async/await is a Swift 5.5+ feature available on iOS 15+
57+
async {
58+
do {
59+
try await Installations.installations().installationID()
60+
} catch {
61+
// ...
62+
}
63+
}
64+
}
65+
#endif // swift(>=5.5)
66+
67+
// Retrieves an installation auth token
68+
Installations.installations().authToken { result, error in
69+
if let _ /* result */ = result {
70+
// ...
71+
} else if let _ /* error */ = error {
72+
// ...
73+
}
74+
}
75+
76+
#if swift(>=5.5)
77+
if #available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
78+
// async/await is a Swift 5.5+ feature available on iOS 15+
79+
async {
80+
do {
81+
_ = try await Installations.installations().authToken()
82+
} catch {
83+
// ...
84+
}
85+
}
86+
}
87+
#endif // swift(>=5.5)
88+
89+
// Retrieves an installation auth token with forcing refresh parameter
90+
Installations.installations().authTokenForcingRefresh(true) { result, error in
91+
if let _ /* result */ = result {
92+
// ...
93+
} else if let _ /* error */ = error {
94+
// ...
95+
}
96+
}
97+
98+
#if swift(>=5.5)
99+
if #available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
100+
// async/await is a Swift 5.5+ feature available on iOS 15+
101+
async {
102+
do {
103+
_ = try await Installations.installations().authTokenForcingRefresh(true)
104+
} catch {
105+
// ...
106+
}
107+
}
108+
}
109+
#endif // swift(>=5.5)
110+
111+
// Delete installation data
112+
Installations.installations().delete { error in
113+
if let _ /* error */ = error {
114+
// ...
115+
}
116+
}
117+
118+
#if swift(>=5.5)
119+
if #available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) {
120+
// async/await is a Swift 5.5+ feature available on iOS 15+
121+
async {
122+
do {
123+
_ = try await Installations.installations().delete()
124+
} catch {
125+
// ...
126+
}
127+
}
128+
}
129+
#endif // swift(>=5.5)
130+
131+
// MARK: - InstallationsAuthTokenResult
132+
133+
Installations.installations().authToken { result, _ in
134+
if let result = result {
135+
_ = result.expirationDate
136+
_ = result.authToken
137+
}
138+
}
139+
140+
// MARK: - InstallationsErrorCode
141+
142+
Installations.installations().authToken { _, error in
143+
if let error = error {
144+
switch (error as NSError).code {
145+
case Int(InstallationsErrorCode.unknown.rawValue):
146+
break
147+
case Int(InstallationsErrorCode.keychain.rawValue):
148+
break
149+
case Int(InstallationsErrorCode.serverUnreachable.rawValue):
150+
break
151+
case Int(InstallationsErrorCode.invalidConfiguration.rawValue):
152+
break
153+
default:
154+
break
155+
}
156+
}
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)