-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathIntents.swift
233 lines (199 loc) · 9.17 KB
/
Intents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/a.out4 -swift-version 4 && %target-codesign %t/a.out4 && %target-run %t/a.out4
// RUN: %target-build-swift %s -o %t/a.out42 -swift-version 4.2 && %target-codesign %t/a.out42 && %target-run %t/a.out42
// REQUIRES: executable_test
// REQUIRES: objc_interop
// UNSUPPORTED: OS=tvos
import Intents
import StdlibUnittest
let IntentsTestSuite = TestSuite("Intents")
#if swift(>=4.2)
let swiftVersion = "4.2"
#else
let swiftVersion = "4"
#endif
#if !os(macOS)
if #available(iOS 10.0, watchOS 3.2, *) {
IntentsTestSuite.test("ErrorDomain/\(swiftVersion)") {
expectEqual("IntentsErrorDomain", INIntentErrorDomain)
}
IntentsTestSuite.test("extension/\(swiftVersion)") {
expectEqual("IntentsErrorDomain", INIntentError.errorDomain)
}
}
#endif
#if os(iOS)
if #available(iOS 11.0, *) {
IntentsTestSuite.test("INParameter KeyPath/\(swiftVersion)") {
let param = INParameter(keyPath: \INRequestRideIntent.pickupLocation)
expectEqual("pickupLocation", param?.parameterKeyPath)
if let typ = param?.parameterClass {
expectEqual(INRequestRideIntent.self, typ)
}
else {
expectUnreachable()
}
}
IntentsTestSuite.test("INSetProfileInCarIntent/defaultProfile availability/\(swiftVersion)") {
func f(profile: INSetProfileInCarIntent) {
var isDefaultProfile = profile.isDefaultProfile
expectType(Bool?.self, &isDefaultProfile)
}
}
}
#endif
#if os(iOS) || os(watchOS)
if #available(iOS 12.0, watchOS 5.0, *) {
// helper functions to build test data
func buildIntent(_ identifier: String) -> INIntent {
let person = INPerson(personHandle: INPersonHandle(value: "123-456-7890", type: .phoneNumber), nameComponents: nil, displayName: "test person \(identifier)", image: nil, contactIdentifier: nil, customIdentifier: "testPerson \(identifier)")
return INSendMessageIntent(recipients: [person], content: "test content \(identifier)", speakableGroupName: INSpeakableString(spokenPhrase: "test group \(identifier)"), conversationIdentifier: nil, serviceName: "test service \(identifier)", sender: person)
}
func buildUserActivity(_ identifier: String) -> NSUserActivity {
return NSUserActivity(activityType: "test activity \(identifier)")
}
IntentsTestSuite.test("INShortcutOverlay/is enum/\(swiftVersion)") {
// INIntent
let originalIntent = buildIntent("A")
let shortcutWithIntent: INShortcut = .intent(originalIntent)
switch shortcutWithIntent {
case .intent(let intent):
expectEqual(intent, originalIntent)
case .userActivity:
expectUnreachable()
}
// test convenince properties
expectEqual(shortcutWithIntent.intent, originalIntent)
expectEqual(shortcutWithIntent.userActivity, nil)
// test convenince init
expectEqual(INShortcut(intent: originalIntent), shortcutWithIntent)
// NSUserActivity
let originalUserActivity = buildUserActivity("A")
let shortcutWithNSUA: INShortcut = .userActivity(originalUserActivity)
switch shortcutWithNSUA {
case .intent:
expectUnreachable()
case .userActivity(let userActivity):
expectEqual(userActivity, originalUserActivity)
}
// test convenince properties
expectEqual(shortcutWithNSUA.intent, nil)
expectEqual(shortcutWithNSUA.userActivity, originalUserActivity)
// test convenince init
expectEqual(INShortcut(userActivity: originalUserActivity), shortcutWithNSUA)
}
IntentsTestSuite.test("INShortcutOverlay/conformances/\(swiftVersion)") {
let intentA = buildIntent("A")
let shortcutIntentA: INShortcut = .intent(intentA)
let shortcutIntentA2: INShortcut = .intent(intentA)
let shortcutIntentB: INShortcut = .intent(buildIntent("B"))
let userActivityA = buildUserActivity("A")
let shortcutUserActivityA: INShortcut = .userActivity(userActivityA)
let shortcutUserActivityA2: INShortcut = .userActivity(userActivityA)
let shortcutUserActivityB: INShortcut = .userActivity(buildUserActivity("B"))
// Equatable
expectEqual(shortcutIntentA, shortcutIntentA2)
expectNotEqual(shortcutIntentA, shortcutIntentB)
expectEqual(shortcutUserActivityA, shortcutUserActivityA2)
expectNotEqual(shortcutUserActivityA, shortcutUserActivityB)
expectNotEqual(shortcutIntentA, shortcutUserActivityA)
// Hashable
// expectEqual(shortcutIntentA.hashValue, shortcutIntentA.hashValue)
// expectEqual(shortcutUserActivityA.hashValue, shortcutUserActivityA.hashValue)
// Strings
let _: String = shortcutIntentA.description
let _: String = shortcutIntentA.debugDescription
}
// Make sure the shortcut property of INVoiceShortcut is imported as the overlay enum type
IntentsTestSuite.test("INShortcutOverlay/INVoiceShortcut propertyIsEnum/\(swiftVersion)") {
// NOTE: we can't actually run this one becuase we can't easily create an INVoiceShortcut, but at least type-check it
func f(voiceShortcut: INVoiceShortcut) {
switch voiceShortcut.shortcut {
case .intent(let intent):
print("got intent \(intent)")
case .userActivity(let userActivity):
print("got userActivity \(userActivity)")
}
}
}
}
#endif
#if os(iOS) || os(watchOS)
if #available(iOS 10.0, watchOS 3.2, *) {
IntentsTestSuite.test("INRideOption usesMeteredFare/\(swiftVersion)") {
func f(rideOption: inout INRideOption) {
#if swift(>=4)
rideOption.usesMeteredFare = true
expectType(Bool?.self, &rideOption.usesMeteredFare)
expectTrue(rideOption.usesMeteredFare ?? false)
#else
rideOption.usesMeteredFare = NSNumber(value: true)
expectType(NSNumber?.self, &rideOption.usesMeteredFare)
expectTrue(rideOption.usesMeteredFare?.boolValue ?? false)
#endif
}
}
}
#endif
#if os(iOS)
if #available(iOS 11.0, *) {
IntentsTestSuite.test("INSetProfileInCarIntent Initializers/\(swiftVersion)") {
#if swift(>=4)
_ = INSetProfileInCarIntent()
_ = INSetProfileInCarIntent(isDefaultProfile: nil)
_ = INSetProfileInCarIntent(profileName: nil)
_ = INSetProfileInCarIntent(profileName: nil, isDefaultProfile: nil)
_ = INSetProfileInCarIntent(profileNumber: nil)
_ = INSetProfileInCarIntent(profileNumber: nil, isDefaultProfile: nil)
_ = INSetProfileInCarIntent(profileNumber: nil, profileName: nil)
_ = INSetProfileInCarIntent(
profileNumber: nil, profileName: nil, isDefaultProfile: nil)
#else
_ = INSetProfileInCarIntent()
_ = INSetProfileInCarIntent(defaultProfile: nil)
_ = INSetProfileInCarIntent(profileName: nil)
_ = INSetProfileInCarIntent(profileName: nil, defaultProfile: nil)
_ = INSetProfileInCarIntent(profileNumber: nil)
_ = INSetProfileInCarIntent(profileNumber: nil, defaultProfile: nil)
_ = INSetProfileInCarIntent(profileNumber: nil, profileName: nil)
_ = INSetProfileInCarIntent(
profileNumber: nil, profileName: nil, defaultProfile: nil)
#endif
}
}
#endif
#if os(iOS) || os(watchOS)
if #available(iOS 12.0, watchOS 5.0, *) {
IntentsTestSuite.test("INPlayMediaIntent Initializer/\(swiftVersion)") {
let intent = INPlayMediaIntent(mediaItems: nil, mediaContainer: nil, playShuffled: false, playbackRepeatMode: .unknown, resumePlayback: true)
expectFalse(intent.playShuffled ?? true)
expectTrue(intent.resumePlayback ?? false)
}
}
#endif
#if os(iOS)
IntentsTestSuite.test("Car Commands Intents Initializer/\(swiftVersion)") {
if #available(iOS 12.0, *) {
_ = INSetProfileInCarIntent(profileNumber: nil, profileName: nil, isDefaultProfile: nil, carName: nil)
_ = INSetClimateSettingsInCarIntent(enableFan: nil, enableAirConditioner: nil, enableClimateControl: nil, enableAutoMode: nil, airCirculationMode: .unknown, fanSpeedIndex: nil, fanSpeedPercentage: nil, relativeFanSpeedSetting: .unknown, temperature: nil, relativeTemperatureSetting: .unknown, climateZone: .unknown, carName: nil)
_ = INSetDefrosterSettingsInCarIntent(enable: nil, defroster: .unknown, carName: nil)
_ = INSetSeatSettingsInCarIntent(enableHeating: nil, enableCooling: nil, enableMassage: nil, seat: .unknown, level: nil, relativeLevel: .unknown, carName: nil)
}
if #available(iOS 11.0, *) {
_ = INSetProfileInCarIntent(profileNumber: nil, profileName: nil, isDefaultProfile: nil)
_ = INSetClimateSettingsInCarIntent(enableFan: nil, enableAirConditioner: nil, enableClimateControl: nil, enableAutoMode: nil, airCirculationMode: .unknown, fanSpeedIndex: nil, fanSpeedPercentage: nil, relativeFanSpeedSetting: .unknown, temperature: nil, relativeTemperatureSetting: .unknown, climateZone: .unknown)
_ = INSetDefrosterSettingsInCarIntent(enable: nil, defroster: .unknown)
_ = INSetSeatSettingsInCarIntent(enableHeating: nil, enableCooling: nil, enableMassage: nil, seat: .unknown, level: nil, relativeLevel: .unknown)
}
}
#endif
#if os(iOS) || os(watchOS)
if #available(iOS 12.0, watchOS 5.0, *) {
IntentsTestSuite.test("INIntent Images/\(swiftVersion)") {
let intent = INSendPaymentIntent(payee: nil, currencyAmount: nil, note: nil)
intent.setImage(nil, forParameterNamed: \INSendPaymentIntent.payee)
intent.setImage(nil, forParameterNamed: \.payee)
}
}
#endif
runAllTests()