noracu 0.2.6 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +489 -0
- package/README.md +75 -16
- package/README.zh-CN.md +75 -16
- package/SECURITY.md +31 -11
- package/docs/architecture.md +24 -5
- package/docs/ios-runner-protocol.md +156 -0
- package/docs/tools.md +36 -19
- package/docs/usage.md +98 -5
- package/imctl/pyproject.toml +1 -1
- package/imctl/src/imctl/__init__.py +1 -1
- package/imctl/src/imctl/cli.py +315 -23
- package/imctl/src/imctl/gestures.py +169 -13
- package/imctl/src/imctl/input.py +112 -23
- package/imctl/src/imctl/pointer.py +81 -0
- package/imctl/src/imctl/screenrect.py +167 -0
- package/imctl/src/imctl/verify.py +68 -4
- package/imctl/src/imctl/window.py +39 -6
- package/ios-runner/LICENSE +21 -0
- package/ios-runner/NoracuRunner.xcodeproj/project.pbxproj +228 -0
- package/ios-runner/NoracuRunner.xcodeproj/xcshareddata/xcschemes/NoracuRunner.xcscheme +69 -0
- package/ios-runner/NoracuRunnerUITests/NoracuRunner.swift +172 -0
- package/ios-runner/NoracuRunnerUITests/RunnerCommands.swift +567 -0
- package/ios-runner/NoracuRunnerUITests/RunnerServer.swift +187 -0
- package/ios-runner/README.md +59 -0
- package/package.json +12 -6
- package/src/approval.js +177 -0
- package/src/bg-inject.js +154 -0
- package/src/ios-session.js +650 -0
- package/src/ios-setup.js +155 -0
- package/src/ios-xcuitest.js +373 -0
- package/src/mac-ax.js +22 -5
- package/src/server.mjs +1375 -173
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// RunnerCommands.swift — protocol v1 command implementations (snapshot, gestures, typing).
|
|
3
|
+
// All entry points run on the main thread. MIT licensed, see LICENSE.
|
|
4
|
+
|
|
5
|
+
import XCTest
|
|
6
|
+
import Foundation
|
|
7
|
+
|
|
8
|
+
enum RunnerCommands {
|
|
9
|
+
// MARK: helpers
|
|
10
|
+
|
|
11
|
+
static func fail(_ id: Int, _ error: String, _ message: String, sent: Any? = nil) -> [String: Any] {
|
|
12
|
+
var r: [String: Any] = ["id": id, "ok": false, "error": error, "message": message]
|
|
13
|
+
if let sent { r["sent"] = sent }
|
|
14
|
+
return r
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static func ok(_ id: Int, _ result: [String: Any]) -> [String: Any] {
|
|
18
|
+
["id": id, "ok": true, "result": result]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static let bundleIdRegex = "^[A-Za-z0-9][A-Za-z0-9.-]{0,154}$"
|
|
22
|
+
|
|
23
|
+
static func validBundleId(_ s: String) -> Bool {
|
|
24
|
+
s.range(of: bundleIdRegex, options: .regularExpression) != nil
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static func screen() -> CGRect { XCUIApplication(bundleIdentifier: "com.apple.springboard").frame }
|
|
28
|
+
|
|
29
|
+
static func app(_ bundleId: String) -> XCUIApplication {
|
|
30
|
+
XCUIApplication(bundleIdentifier: bundleId)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static func msSince(_ t0: Date) -> Int { Int(Date().timeIntervalSince(t0) * 1000) }
|
|
34
|
+
|
|
35
|
+
static let inputOps: Set<String> = ["tap", "press", "drag", "type", "keys", "home"]
|
|
36
|
+
|
|
37
|
+
static func isBool(_ v: Any) -> Bool { CFGetTypeID(v as CFTypeRef) == CFBooleanGetTypeID() }
|
|
38
|
+
|
|
39
|
+
// a number field; a JSON true / false is not a number
|
|
40
|
+
static func num(_ v: Any?) -> Double? {
|
|
41
|
+
guard let v, !isBool(v) else { return nil }
|
|
42
|
+
return v as? Double
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// an optional whole-number field: absent → the default; present but not a whole number in range → nil,
|
|
46
|
+
// and the caller refuses the command (a field of the wrong type is never read as "not given")
|
|
47
|
+
static func optInt(_ args: [String: Any], _ key: String, _ range: ClosedRange<Int>, _ dflt: Int) -> Int? {
|
|
48
|
+
guard let raw = args[key] else { return dflt }
|
|
49
|
+
guard !isBool(raw), let v = raw as? Int, range.contains(v) else { return nil }
|
|
50
|
+
return v
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// MARK: dispatch
|
|
54
|
+
|
|
55
|
+
@MainActor
|
|
56
|
+
static func dispatch(op: String, args: [String: Any], box: RunnerBox) -> [String: Any] {
|
|
57
|
+
let id = 0 // id is re-attached by the server wrapper below
|
|
58
|
+
switch op {
|
|
59
|
+
case "ping":
|
|
60
|
+
return ok(id, ["v": 1, "uptime_ms": msSince(box.started), "served": box.served])
|
|
61
|
+
case "app_state":
|
|
62
|
+
return appState(args)
|
|
63
|
+
case "snapshot":
|
|
64
|
+
return snapshot(args)
|
|
65
|
+
case "tap":
|
|
66
|
+
return input(args) { a, x, y in tapCmd(a, x, y, args) }
|
|
67
|
+
case "press":
|
|
68
|
+
return input(args, press: true) { a, x, y in pressCmd(a, x, y, args) }
|
|
69
|
+
case "drag":
|
|
70
|
+
return dragCmd(args)
|
|
71
|
+
case "type":
|
|
72
|
+
return textCmd(args) { a in typeCmd(a, args) }
|
|
73
|
+
case "keys":
|
|
74
|
+
return textCmd(args) { a in keysCmd(a, args) }
|
|
75
|
+
case "home":
|
|
76
|
+
let t0 = Date()
|
|
77
|
+
XCUIDevice.shared.press(.home)
|
|
78
|
+
return ok(0, ["ms": msSince(t0)])
|
|
79
|
+
case "screenshot":
|
|
80
|
+
return screenshotCmd()
|
|
81
|
+
case "front":
|
|
82
|
+
return frontCmd()
|
|
83
|
+
case "selftest_issue":
|
|
84
|
+
// test hook, answered only when the runner was started with NORACU_SELFTEST=1
|
|
85
|
+
guard box.selftest else { return fail(0, "unknown_op", "unknown op \(op)") }
|
|
86
|
+
if (args["late"] as? Bool) == true {
|
|
87
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { XCTFail("noracu selftest late issue") }
|
|
88
|
+
return ok(0, [:])
|
|
89
|
+
}
|
|
90
|
+
XCTFail("noracu selftest issue")
|
|
91
|
+
return ok(0, [:])
|
|
92
|
+
case "quit":
|
|
93
|
+
box.done = true
|
|
94
|
+
return ok(0, [:])
|
|
95
|
+
default:
|
|
96
|
+
return fail(0, "unknown_op", "unknown op \(op)")
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// MARK: app_state
|
|
101
|
+
|
|
102
|
+
@MainActor
|
|
103
|
+
static func appState(_ args: [String: Any]) -> [String: Any] {
|
|
104
|
+
guard let bundle = args["bundle_id"] as? String, validBundleId(bundle) else {
|
|
105
|
+
return fail(0, "invalid_args", "bad bundle_id")
|
|
106
|
+
}
|
|
107
|
+
let a = app(bundle)
|
|
108
|
+
let raw = a.state.rawValue
|
|
109
|
+
let name: String
|
|
110
|
+
switch a.state {
|
|
111
|
+
case .unknown: name = "unknown"
|
|
112
|
+
case .notRunning: name = "notRunning"
|
|
113
|
+
case .runningBackgroundSuspended: name = "runningBackgroundSuspended"
|
|
114
|
+
case .runningBackground: name = "runningBackground"
|
|
115
|
+
case .runningForeground: name = "runningForeground"
|
|
116
|
+
@unknown default: name = "unknown"
|
|
117
|
+
}
|
|
118
|
+
return ok(0, ["state": raw, "name": name])
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// MARK: text-command gate (type / keys: no coordinates)
|
|
122
|
+
|
|
123
|
+
@MainActor
|
|
124
|
+
static func textCmd(_ args: [String: Any], _ body: (XCUIApplication) -> [String: Any]) -> [String: Any] {
|
|
125
|
+
guard let bundle = args["bundle_id"] as? String, validBundleId(bundle) else {
|
|
126
|
+
return fail(0, "invalid_args", "bad bundle_id", sent: false)
|
|
127
|
+
}
|
|
128
|
+
let a = app(bundle)
|
|
129
|
+
let st = a.state
|
|
130
|
+
if st == .notRunning || st == .unknown {
|
|
131
|
+
return fail(0, "app_not_running", "state \(st.rawValue)", sent: false)
|
|
132
|
+
}
|
|
133
|
+
if bundle != "com.apple.springboard" && st != .runningForeground {
|
|
134
|
+
return fail(0, "app_not_foreground", "state \(st.rawValue)", sent: false)
|
|
135
|
+
}
|
|
136
|
+
if bundle == "com.apple.springboard", let no = springboardRefusal(args, .text) { return no }
|
|
137
|
+
if bundle != "com.apple.springboard", let no = alertRefusal() { return no }
|
|
138
|
+
return body(a)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// MARK: events without XCTest's idle wait
|
|
142
|
+
|
|
143
|
+
// XCTest waits for the app to go idle before and after each synthesized event. An app that never does held a tap
|
|
144
|
+
// past noracu's 20 s limit unsent: the context menu of a long-pressed note in Notes (iPhone 16 Pro, iOS 27.2.1,
|
|
145
|
+
// device run 2026-11-14). Taps, presses and drags go out without that wait, through XCUIApplication's
|
|
146
|
+
// _performWithInteractionOptions:block: with SkipPreEventQuiescence | SkipPostEventQuiescence (bits 0 and 1, as
|
|
147
|
+
// XCUIAutomation's own shouldSkip…Quiescence read them). Where that method is missing the event goes out the usual
|
|
148
|
+
// way. noracu reports these inputs as unverified and reads the screen again before relying on what they did.
|
|
149
|
+
@MainActor
|
|
150
|
+
static func withoutIdleWait(_ a: XCUIApplication, _ body: @escaping () -> Void) {
|
|
151
|
+
let sel = NSSelectorFromString("_performWithInteractionOptions:block:")
|
|
152
|
+
guard a.responds(to: sel), let m = class_getInstanceMethod(type(of: a), sel) else { body(); return }
|
|
153
|
+
typealias Perform = @convention(c) (AnyObject, Selector, UInt, @convention(block) () -> Void) -> Void
|
|
154
|
+
let perform = unsafeBitCast(method_getImplementation(m), to: Perform.self)
|
|
155
|
+
perform(a, sel, 0b11, body)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// MARK: input-command gate
|
|
159
|
+
|
|
160
|
+
@MainActor
|
|
161
|
+
static func input(_ args: [String: Any], press: Bool = false, _ body: (XCUIApplication, CGFloat, CGFloat) -> [String: Any]) -> [String: Any] {
|
|
162
|
+
guard let bundle = args["bundle_id"] as? String, validBundleId(bundle) else {
|
|
163
|
+
return fail(0, "invalid_args", "bad bundle_id", sent: false)
|
|
164
|
+
}
|
|
165
|
+
guard let x = num(args["x"]), let y = num(args["y"]) else {
|
|
166
|
+
return fail(0, "invalid_args", "x/y required", sent: false)
|
|
167
|
+
}
|
|
168
|
+
let scr = screen()
|
|
169
|
+
guard x >= 0, y >= 0, x <= scr.width, y <= scr.height else {
|
|
170
|
+
return fail(0, "invalid_args", "point outside screen", sent: false)
|
|
171
|
+
}
|
|
172
|
+
let a = app(bundle)
|
|
173
|
+
let st = a.state
|
|
174
|
+
if st == .notRunning || st == .unknown {
|
|
175
|
+
return fail(0, "app_not_running", "state \(st.rawValue)", sent: false)
|
|
176
|
+
}
|
|
177
|
+
if bundle != "com.apple.springboard" && st != .runningForeground {
|
|
178
|
+
return fail(0, "app_not_foreground", "state \(st.rawValue)", sent: false)
|
|
179
|
+
}
|
|
180
|
+
if bundle == "com.apple.springboard", let no = springboardRefusal(args, press ? .press : .tap, at: CGPoint(x: x, y: y)) { return no }
|
|
181
|
+
if bundle != "com.apple.springboard", let no = alertRefusal() { return no }
|
|
182
|
+
return body(a, CGFloat(x), CGFloat(y))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
@MainActor
|
|
186
|
+
static func tapCmd(_ a: XCUIApplication, _ x: CGFloat, _ y: CGFloat, _ args: [String: Any]) -> [String: Any] {
|
|
187
|
+
guard let count = optInt(args, "count", 1...3, 1) else { return fail(0, "invalid_args", "count must be a whole number 1–3", sent: false) }
|
|
188
|
+
let t0 = Date()
|
|
189
|
+
let coord = a.coordinate(withNormalizedOffset: .zero).withOffset(CGVector(dx: x, dy: y))
|
|
190
|
+
withoutIdleWait(a) {
|
|
191
|
+
if count == 2 {
|
|
192
|
+
coord.doubleTap()
|
|
193
|
+
} else if count == 3 {
|
|
194
|
+
for _ in 0..<3 { coord.tap() }
|
|
195
|
+
} else {
|
|
196
|
+
coord.tap()
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return ok(0, ["ms": msSince(t0)])
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
@MainActor
|
|
203
|
+
static func pressCmd(_ a: XCUIApplication, _ x: CGFloat, _ y: CGFloat, _ args: [String: Any]) -> [String: Any] {
|
|
204
|
+
guard let rawD = args["duration_ms"], !isBool(rawD), let d = rawD as? Int, (100...5000).contains(d) else {
|
|
205
|
+
return fail(0, "invalid_args", "duration_ms out of range", sent: false)
|
|
206
|
+
}
|
|
207
|
+
let t0 = Date()
|
|
208
|
+
let coord = a.coordinate(withNormalizedOffset: .zero).withOffset(CGVector(dx: x, dy: y))
|
|
209
|
+
withoutIdleWait(a) { coord.press(forDuration: TimeInterval(d) / 1000.0) }
|
|
210
|
+
return ok(0, ["ms": msSince(t0)])
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
@MainActor
|
|
214
|
+
static func dragCmd(_ args: [String: Any]) -> [String: Any] {
|
|
215
|
+
guard let bundle = args["bundle_id"] as? String, validBundleId(bundle) else {
|
|
216
|
+
return fail(0, "invalid_args", "bad bundle_id", sent: false)
|
|
217
|
+
}
|
|
218
|
+
guard let from = args["from"] as? [Any], from.count == 2,
|
|
219
|
+
let fx = num(from[0]), let fy = num(from[1]),
|
|
220
|
+
let to = args["to"] as? [Any], to.count == 2,
|
|
221
|
+
let tx = num(to[0]), let ty = num(to[1]) else {
|
|
222
|
+
return fail(0, "invalid_args", "from/to required", sent: false)
|
|
223
|
+
}
|
|
224
|
+
guard let holdMs = optInt(args, "hold_ms", 0...2000, 50) else { return fail(0, "invalid_args", "hold_ms must be a whole number 0–2000", sent: false) }
|
|
225
|
+
guard let endHoldMs = optInt(args, "end_hold_ms", 0...2000, 300) else { return fail(0, "invalid_args", "end_hold_ms must be a whole number 0–2000", sent: false) }
|
|
226
|
+
var velocity: XCUIGestureVelocity = .slow
|
|
227
|
+
if let raw = args["velocity"] {
|
|
228
|
+
switch raw as? String {
|
|
229
|
+
case "slow": velocity = .slow
|
|
230
|
+
case "default": velocity = .default
|
|
231
|
+
case "fast": velocity = .fast
|
|
232
|
+
default: return fail(0, "invalid_args", "bad velocity", sent: false)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
let scr = screen()
|
|
236
|
+
for p in [CGPoint(x: fx, y: fy), CGPoint(x: tx, y: ty)] {
|
|
237
|
+
guard p.x >= 0, p.y >= 0, p.x <= scr.width, p.y <= scr.height else {
|
|
238
|
+
return fail(0, "invalid_args", "point outside screen", sent: false)
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
let a = app(bundle)
|
|
242
|
+
let st = a.state
|
|
243
|
+
if st == .notRunning || st == .unknown {
|
|
244
|
+
return fail(0, "app_not_running", "state \(st.rawValue)", sent: false)
|
|
245
|
+
}
|
|
246
|
+
if bundle != "com.apple.springboard" && st != .runningForeground {
|
|
247
|
+
return fail(0, "app_not_foreground", "state \(st.rawValue)", sent: false)
|
|
248
|
+
}
|
|
249
|
+
if bundle == "com.apple.springboard", let no = springboardRefusal(args, .drag) { return no }
|
|
250
|
+
if bundle != "com.apple.springboard", let no = alertRefusal() { return no }
|
|
251
|
+
let t0 = Date()
|
|
252
|
+
let fromC = a.coordinate(withNormalizedOffset: .zero).withOffset(CGVector(dx: fx, dy: fy))
|
|
253
|
+
let toC = a.coordinate(withNormalizedOffset: .zero).withOffset(CGVector(dx: tx, dy: ty))
|
|
254
|
+
withoutIdleWait(a) {
|
|
255
|
+
fromC.press(forDuration: TimeInterval(holdMs) / 1000.0, thenDragTo: toC, withVelocity: velocity, thenHoldForDuration: TimeInterval(endHoldMs) / 1000.0)
|
|
256
|
+
}
|
|
257
|
+
return ok(0, ["ms": msSince(t0)])
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
@MainActor
|
|
261
|
+
static func typeCmd(_ a: XCUIApplication, _ args: [String: Any]) -> [String: Any] {
|
|
262
|
+
guard let text = args["text"] as? String, (1...2000).contains(text.count) else {
|
|
263
|
+
return fail(0, "invalid_args", "text out of range", sent: false)
|
|
264
|
+
}
|
|
265
|
+
guard a.keyboards.firstMatch.exists else {
|
|
266
|
+
return fail(0, "no_keyboard", "no keyboard", sent: false)
|
|
267
|
+
}
|
|
268
|
+
let t0 = Date()
|
|
269
|
+
a.typeText(text)
|
|
270
|
+
return ok(0, ["ms": msSince(t0)])
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
@MainActor
|
|
274
|
+
static func keysCmd(_ a: XCUIApplication, _ args: [String: Any]) -> [String: Any] {
|
|
275
|
+
guard let key = args["key"] as? String, key == "delete" || key == "return" else {
|
|
276
|
+
return fail(0, "invalid_args", "bad key", sent: false)
|
|
277
|
+
}
|
|
278
|
+
guard let count = optInt(args, "count", 1...500, 1) else { return fail(0, "invalid_args", "count must be a whole number 1–500", sent: false) }
|
|
279
|
+
guard a.keyboards.firstMatch.exists else {
|
|
280
|
+
return fail(0, "no_keyboard", "no keyboard", sent: false)
|
|
281
|
+
}
|
|
282
|
+
let t0 = Date()
|
|
283
|
+
let raw = key == "delete" ? XCUIKeyboardKey.delete.rawValue : XCUIKeyboardKey.return.rawValue
|
|
284
|
+
a.typeText(String(repeating: raw, count: count))
|
|
285
|
+
return ok(0, ["ms": msSince(t0)])
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// MARK: screenshot
|
|
289
|
+
|
|
290
|
+
@MainActor
|
|
291
|
+
static func screenshotCmd() -> [String: Any] {
|
|
292
|
+
let t0 = Date()
|
|
293
|
+
guard let png = XCUIScreen.main.screenshot().pngRepresentation as Data? else {
|
|
294
|
+
return fail(0, "screenshot_failed", "no png")
|
|
295
|
+
}
|
|
296
|
+
let scr = screen()
|
|
297
|
+
return ok(0, ["png_base64": png.base64EncodedString(), "w": Int(scr.width), "h": Int(scr.height), "ms": msSince(t0)])
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// MARK: front — what SpringBoard itself is showing
|
|
301
|
+
//
|
|
302
|
+
// Public XCUITest cannot name the app in front. It can tell whether SpringBoard's own Home Screen is on
|
|
303
|
+
// screen (its icons are) and where SpringBoard's alerts are.
|
|
304
|
+
|
|
305
|
+
struct SBAlert {
|
|
306
|
+
let frame: CGRect
|
|
307
|
+
let buttons: [CGRect]
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
@MainActor
|
|
311
|
+
static func springboardFront() -> (icons: Int, alerts: [SBAlert])? {
|
|
312
|
+
let snap: XCUIElementSnapshot
|
|
313
|
+
do { snap = try app("com.apple.springboard").snapshot() } catch { return nil }
|
|
314
|
+
let scr = screen()
|
|
315
|
+
var icons = 0
|
|
316
|
+
var alerts: [SBAlert] = []
|
|
317
|
+
func shown(_ s: XCUIElementSnapshot) -> Bool { s.frame.width > 0 && s.frame.height > 0 && s.frame.intersects(scr) }
|
|
318
|
+
func buttons(_ s: XCUIElementSnapshot, _ out: inout [CGRect]) {
|
|
319
|
+
if s.elementType == .button && shown(s) { out.append(s.frame) }
|
|
320
|
+
for c in s.children { buttons(c, &out) }
|
|
321
|
+
}
|
|
322
|
+
func walk(_ s: XCUIElementSnapshot) {
|
|
323
|
+
if shown(s) && s.elementType == .icon { icons += 1 }
|
|
324
|
+
// an alert only: a sheet's frame can cover the whole screen (pre-review P3)
|
|
325
|
+
if shown(s) && s.elementType == .alert {
|
|
326
|
+
var b: [CGRect] = []
|
|
327
|
+
buttons(s, &b)
|
|
328
|
+
alerts.append(SBAlert(frame: s.frame, buttons: b))
|
|
329
|
+
}
|
|
330
|
+
for c in s.children { walk(c) }
|
|
331
|
+
}
|
|
332
|
+
walk(snap)
|
|
333
|
+
return (icons, alerts)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
enum SBInput { case tap, press, drag, text }
|
|
337
|
+
|
|
338
|
+
// review S4A-s8-g-1: a system alert (a permission prompt, say) sits over the app in front while the app still reads
|
|
339
|
+
// "in front", and the app's own snapshot does not show it. Input named for an app is not sent while one is up: a tap
|
|
340
|
+
// at the app's point would press the alert's button, and text would go to the alert's field. SpringBoard that
|
|
341
|
+
// cannot be read fails closed. Returns the refusal, or nil.
|
|
342
|
+
@MainActor
|
|
343
|
+
static func alertRefusal() -> [String: Any]? {
|
|
344
|
+
guard let f = springboardFront() else { return fail(0, "system_alert", "com.apple.springboard could not be read, so a system alert could not be ruled out", sent: false) }
|
|
345
|
+
if !f.alerts.isEmpty { return fail(0, "system_alert", "a system alert is up over the app", sent: false) }
|
|
346
|
+
return nil
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// com.apple.springboard always reads "running in the foreground", whatever is on screen, so its state says
|
|
350
|
+
// nothing about where a point lands — and on the Home Screen itself a tap on an icon opens an app no rule
|
|
351
|
+
// was asked about. With springboard_only: true a command named for it is sent in one case only: a single
|
|
352
|
+
// tap whose point lies on a button of one of SpringBoard's alerts (a system prompt). A sheet does not count
|
|
353
|
+
// (its frame can cover the screen), and nothing else is sent: no long press, no drag, no text and no key —
|
|
354
|
+
// text goes wherever the keyboard focus is, which the public API does not show, and a second Return would
|
|
355
|
+
// land on whatever the alert was covering (pre-review P3). Returns the refusal, or nil.
|
|
356
|
+
@MainActor
|
|
357
|
+
static func springboardRefusal(_ args: [String: Any], _ kind: SBInput, at point: CGPoint? = nil) -> [String: Any]? {
|
|
358
|
+
guard let raw = args["springboard_only"] else { return nil }
|
|
359
|
+
guard isBool(raw), let only = raw as? Bool else { return fail(0, "invalid_args", "springboard_only must be true or false", sent: false) }
|
|
360
|
+
if !only { return nil }
|
|
361
|
+
switch kind {
|
|
362
|
+
case .drag: return fail(0, "app_not_foreground", "a drag named for com.apple.springboard is not sent", sent: false)
|
|
363
|
+
case .press: return fail(0, "app_not_foreground", "a long press named for com.apple.springboard is not sent", sent: false)
|
|
364
|
+
case .text: return fail(0, "app_not_foreground", "text and keys named for com.apple.springboard are not sent", sent: false)
|
|
365
|
+
case .tap: break
|
|
366
|
+
}
|
|
367
|
+
if let c = args["count"] {
|
|
368
|
+
guard !isBool(c), let n = c as? Int, (1...3).contains(n) else { return fail(0, "invalid_args", "count must be a whole number 1–3", sent: false) }
|
|
369
|
+
if n != 1 { return fail(0, "app_not_foreground", "more than one tap named for com.apple.springboard is not sent", sent: false) }
|
|
370
|
+
}
|
|
371
|
+
guard let p = point, let f = springboardFront() else { return fail(0, "app_not_foreground", "com.apple.springboard could not be read", sent: false) }
|
|
372
|
+
if f.alerts.contains(where: { a in a.frame.contains(p) && a.buttons.contains(where: { $0.contains(p) }) }) { return nil }
|
|
373
|
+
return fail(0, "app_not_foreground", "the point is not on a button of one of SpringBoard's alerts", sent: false)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
@MainActor
|
|
377
|
+
static func frontCmd() -> [String: Any] {
|
|
378
|
+
let t0 = Date()
|
|
379
|
+
guard let f = springboardFront() else { return fail(0, "snapshot_failed", "com.apple.springboard could not be read") }
|
|
380
|
+
return ok(0, [
|
|
381
|
+
"home": f.icons > 0,
|
|
382
|
+
"icons": f.icons,
|
|
383
|
+
"alerts": f.alerts.map { a in [a.frame.minX, a.frame.minY, a.frame.width, a.frame.height].map { Double(round($0 * 10) / 10) } },
|
|
384
|
+
"alert_buttons": f.alerts.reduce(0) { $0 + $1.buttons.count },
|
|
385
|
+
"ms": msSince(t0),
|
|
386
|
+
])
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// MARK: snapshot
|
|
390
|
+
|
|
391
|
+
static let interactiveTypes: Set<XCUIElement.ElementType> = [
|
|
392
|
+
.button, .cell, .textField, .secureTextField, .searchField, .textView, .switch,
|
|
393
|
+
.slider, .link, .tab, .key, .menuItem, .segmentedControl, .pickerWheel, .stepper, .toggle,
|
|
394
|
+
]
|
|
395
|
+
|
|
396
|
+
static func typeName(_ t: XCUIElement.ElementType) -> String {
|
|
397
|
+
switch t {
|
|
398
|
+
case .any: return "any"
|
|
399
|
+
case .application: return "application"
|
|
400
|
+
case .activityIndicator: return "activityIndicator"
|
|
401
|
+
case .alert: return "alert"
|
|
402
|
+
case .browser: return "browser"
|
|
403
|
+
case .button: return "button"
|
|
404
|
+
case .cell: return "cell"
|
|
405
|
+
case .checkBox: return "checkBox"
|
|
406
|
+
case .collectionView: return "collectionView"
|
|
407
|
+
case .comboBox: return "comboBox"
|
|
408
|
+
case .datePicker: return "datePicker"
|
|
409
|
+
case .decrementArrow: return "decrementArrow"
|
|
410
|
+
case .dialog: return "dialog"
|
|
411
|
+
case .image: return "image"
|
|
412
|
+
case .incrementArrow: return "incrementArrow"
|
|
413
|
+
case .key: return "key"
|
|
414
|
+
case .keyboard: return "keyboard"
|
|
415
|
+
case .link: return "link"
|
|
416
|
+
case .map: return "map"
|
|
417
|
+
case .menu: return "menu"
|
|
418
|
+
case .menuBar: return "menuBar"
|
|
419
|
+
case .menuItem: return "menuItem"
|
|
420
|
+
case .navigationBar: return "navigationBar"
|
|
421
|
+
case .other: return "other"
|
|
422
|
+
case .pageIndicator: return "pageIndicator"
|
|
423
|
+
case .picker: return "picker"
|
|
424
|
+
case .pickerWheel: return "pickerWheel"
|
|
425
|
+
case .popUpButton: return "popUpButton"
|
|
426
|
+
case .progressIndicator: return "progressIndicator"
|
|
427
|
+
case .radioButton: return "radioButton"
|
|
428
|
+
case .radioGroup: return "radioGroup"
|
|
429
|
+
case .scrollBar: return "scrollBar"
|
|
430
|
+
case .scrollView: return "scrollView"
|
|
431
|
+
case .icon: return "icon"
|
|
432
|
+
case .searchField: return "searchField"
|
|
433
|
+
case .segmentedControl: return "segmentedControl"
|
|
434
|
+
case .sheet: return "sheet"
|
|
435
|
+
case .slider: return "slider"
|
|
436
|
+
case .splitGroup: return "splitGroup"
|
|
437
|
+
case .staticText: return "staticText"
|
|
438
|
+
case .statusBar: return "statusBar"
|
|
439
|
+
case .stepper: return "stepper"
|
|
440
|
+
case .switch: return "switch"
|
|
441
|
+
case .tab: return "tab"
|
|
442
|
+
case .tabBar: return "tabBar"
|
|
443
|
+
case .table: return "table"
|
|
444
|
+
case .textField: return "textField"
|
|
445
|
+
case .secureTextField: return "secureTextField" // review S4A-s8-reviewer-1: it fell to "other"
|
|
446
|
+
case .textView: return "textView"
|
|
447
|
+
case .toggle: return "toggle"
|
|
448
|
+
case .toolbar: return "toolbar"
|
|
449
|
+
case .webView: return "webView"
|
|
450
|
+
case .window: return "window"
|
|
451
|
+
case .touchBar: return "touchBar"
|
|
452
|
+
case .statusItem: return "statusItem"
|
|
453
|
+
case .layoutArea: return "layoutArea"
|
|
454
|
+
case .layoutItem: return "layoutItem"
|
|
455
|
+
case .handle: return "handle"
|
|
456
|
+
case .grid: return "grid"
|
|
457
|
+
case .tableRow: return "tableRow"
|
|
458
|
+
case .tableColumn: return "tableColumn"
|
|
459
|
+
case .outline: return "outline"
|
|
460
|
+
case .colorWell: return "colorWell"
|
|
461
|
+
case .dockItem: return "dockItem"
|
|
462
|
+
case .tabGroup: return "tabGroup"
|
|
463
|
+
case .levelIndicator: return "levelIndicator"
|
|
464
|
+
case .valueIndicator: return "valueIndicator"
|
|
465
|
+
@unknown default: return "other"
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
struct FlatNode {
|
|
470
|
+
let type: XCUIElement.ElementType
|
|
471
|
+
let label: String
|
|
472
|
+
let ident: String
|
|
473
|
+
let value: String
|
|
474
|
+
let placeholder: String
|
|
475
|
+
let frame: CGRect
|
|
476
|
+
let enabled: Bool
|
|
477
|
+
let selected: Bool
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
static func flatten(_ s: XCUIElementSnapshot) -> [FlatNode] {
|
|
481
|
+
var out: [FlatNode] = []
|
|
482
|
+
// a row's label: its own, or the labels inside it in reading order, joined (cut at 200 characters by the caller)
|
|
483
|
+
func rowText(_ s: XCUIElementSnapshot) -> String {
|
|
484
|
+
if !s.label.isEmpty { return s.label }
|
|
485
|
+
var parts: [String] = []
|
|
486
|
+
var length = 0
|
|
487
|
+
func collect(_ n: XCUIElementSnapshot) {
|
|
488
|
+
for c in n.children {
|
|
489
|
+
if length > 200 { return }
|
|
490
|
+
if c.label.isEmpty { collect(c) } else if parts.last != c.label { parts.append(c.label); length += c.label.count + 2 }
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
collect(s)
|
|
494
|
+
return parts.joined(separator: ", ")
|
|
495
|
+
}
|
|
496
|
+
func walk(_ s: XCUIElementSnapshot) {
|
|
497
|
+
let isRow = s.elementType == .cell
|
|
498
|
+
out.append(FlatNode(
|
|
499
|
+
type: s.elementType,
|
|
500
|
+
label: isRow ? rowText(s) : s.label,
|
|
501
|
+
ident: s.identifier,
|
|
502
|
+
value: (s.value as? String) ?? "",
|
|
503
|
+
placeholder: s.placeholderValue ?? "",
|
|
504
|
+
frame: s.frame,
|
|
505
|
+
enabled: s.isEnabled,
|
|
506
|
+
selected: s.isSelected))
|
|
507
|
+
for c in s.children { walk(c) }
|
|
508
|
+
}
|
|
509
|
+
walk(s)
|
|
510
|
+
return out
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
@MainActor
|
|
514
|
+
static func snapshot(_ args: [String: Any]) -> [String: Any] {
|
|
515
|
+
guard let bundle = args["bundle_id"] as? String, validBundleId(bundle) else {
|
|
516
|
+
return fail(0, "invalid_args", "bad bundle_id")
|
|
517
|
+
}
|
|
518
|
+
guard let maxNodes = optInt(args, "max_nodes", 1...1000, 400) else { return fail(0, "invalid_args", "max_nodes must be a whole number 1–1000") }
|
|
519
|
+
let a = app(bundle)
|
|
520
|
+
let st = a.state
|
|
521
|
+
if st == .notRunning || st == .unknown {
|
|
522
|
+
return fail(0, "app_not_running", "state \(st.rawValue)")
|
|
523
|
+
}
|
|
524
|
+
let t0 = Date()
|
|
525
|
+
let snap: XCUIElementSnapshot
|
|
526
|
+
do { snap = try a.snapshot() } catch {
|
|
527
|
+
return fail(0, "snapshot_failed", error.localizedDescription)
|
|
528
|
+
}
|
|
529
|
+
let scr = screen()
|
|
530
|
+
var nodes: [[String: Any]] = []
|
|
531
|
+
var truncated = false
|
|
532
|
+
for n in flatten(snap) {
|
|
533
|
+
guard n.frame.width > 0, n.frame.height > 0 else { continue }
|
|
534
|
+
guard n.frame.intersects(scr) else { continue }
|
|
535
|
+
guard !n.label.isEmpty || interactiveTypes.contains(n.type) else { continue }
|
|
536
|
+
if nodes.count >= maxNodes { truncated = true; break }
|
|
537
|
+
var obj: [String: Any] = [
|
|
538
|
+
"i": nodes.count,
|
|
539
|
+
"type": typeName(n.type),
|
|
540
|
+
"label": String(n.label.prefix(200)),
|
|
541
|
+
"id": String(n.ident.prefix(80)),
|
|
542
|
+
"frame": [n.frame.minX, n.frame.minY, n.frame.width, n.frame.height].map { Double(round($0 * 10) / 10) },
|
|
543
|
+
"enabled": n.enabled,
|
|
544
|
+
"selected": n.selected,
|
|
545
|
+
]
|
|
546
|
+
if n.type == .textField || n.type == .searchField || n.type == .textView || n.type == .switch || n.type == .slider {
|
|
547
|
+
// an empty text field reports its placeholder as its value: that is not content
|
|
548
|
+
var v = n.value
|
|
549
|
+
if !n.placeholder.isEmpty {
|
|
550
|
+
obj["placeholder"] = String(n.placeholder.prefix(200))
|
|
551
|
+
if v == n.placeholder { v = "" }
|
|
552
|
+
}
|
|
553
|
+
obj["value"] = String(v.prefix(4000))
|
|
554
|
+
if v.count > 4000 { obj["value_truncated"] = true }
|
|
555
|
+
}
|
|
556
|
+
nodes.append(obj)
|
|
557
|
+
}
|
|
558
|
+
return ok(0, [
|
|
559
|
+
"bundle_id": bundle,
|
|
560
|
+
"state": st.rawValue,
|
|
561
|
+
"screen": ["w": scr.width, "h": scr.height],
|
|
562
|
+
"nodes": nodes,
|
|
563
|
+
"truncated": truncated,
|
|
564
|
+
"ms": msSince(t0),
|
|
565
|
+
])
|
|
566
|
+
}
|
|
567
|
+
}
|