mobridge-mcp 0.6.0 → 0.8.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.
@@ -1,319 +0,0 @@
1
- import Foundation
2
- import XCTest
3
- import Darwin
4
- import ObjectiveC
5
-
6
- // The XCTest Runner (WDA-style): a long-running UI test that binds a TCP socket on 127.0.0.1:8500 and
7
- // services length-prefixed JSON-RPC requests by doing REAL XCUIApplication/XCUIElement work. TS stays
8
- // the source of truth for NodeRef resolution + VerificationPolicy; this Runner only captures fresh
9
- // snapshots, double-checks the candidate count, performs the native action, and returns before/after.
10
- //
11
- // Framing (frozen, matches PROTOCOL.md + bridgeTransport.ts): 4-byte big-endian length + UTF-8 JSON.
12
- final class BridgeServer: XCTestCase {
13
-
14
- private static let port: UInt16 = 8500
15
- private let runnerInstanceId = UUID().uuidString
16
- private var sessionToken = ""
17
- private var app: XCUIApplication!
18
- // The bundle id of the app currently under automation. Default = SpringBoard (the home screen), since
19
- // this is a standalone UI-test Runner with NO host app of its own. app.open with a `bundleId` switches
20
- // this to any already-installed app (XCUIApplication(bundleIdentifier:)), so CMS can read/act on App
21
- // Store / system apps without re-signing them; only this Runner is signed.
22
- private static let springboard = "com.apple.springboard"
23
- private var targetBundleId: String? = nil
24
-
25
- // PERF (the single biggest iOS speedup): XCUITest blocks EVERY query/gesture until the app reaches a
26
- // quiescent state (animations + main-runloop idle). A media feed (TikTok, a game, any animation) never
27
- // idles, so each call waits the full ~animation timeout and appears to hang / times out. Neutralize the
28
- // private `waitForQuiescenceIncludingAnimationsIdle:` to a no-op — the same technique WebDriverAgent /
29
- // Appium use. Guarded: if Apple renames it, we simply keep the default (slow but correct) behavior.
30
- private static func disableQuiescenceWaiting() {
31
- guard let cls = NSClassFromString("XCUIApplicationProcess") else { return }
32
- let sel = NSSelectorFromString("waitForQuiescenceIncludingAnimationsIdle:")
33
- guard let method = class_getInstanceMethod(cls, sel) else { return }
34
- let noop: @convention(block) (AnyObject, Bool) -> Void = { _, _ in }
35
- method_setImplementation(method, imp_implementationWithBlock(noop))
36
- FileHandle.standardError.write("[runner] quiescence wait disabled (fast mode)\n".data(using: .utf8)!)
37
- }
38
-
39
- func testServe() throws {
40
- continueAfterFailure = true
41
- BridgeServer.disableQuiescenceWaiting()
42
- // Standalone Runner (no fixture host app): default to SpringBoard, which is always running, so a
43
- // screen.get before any app.open reads the home screen instead of launching a nonexistent host.
44
- app = XCUIApplication(bundleIdentifier: BridgeServer.springboard)
45
-
46
- let listenFd = try openListener(port: BridgeServer.port)
47
- defer { close(listenFd) }
48
- FileHandle.standardError.write("[runner] listening on 127.0.0.1:\(BridgeServer.port)\n".data(using: .utf8)!)
49
-
50
- while true {
51
- let fd = accept(listenFd, nil, nil)
52
- if fd < 0 { continue }
53
- var shutdown = false
54
- while !shutdown, let req = readFrame(fd) {
55
- let (resp, stop) = handle(req)
56
- writeFrame(fd, resp)
57
- shutdown = stop
58
- }
59
- close(fd)
60
- if shutdown { break }
61
- }
62
- }
63
-
64
- // MARK: dispatch
65
-
66
- /// Every method this Runner serves. Keep in lockstep with the switch in the dispatcher below.
67
- static let servedMethods = [
68
- "HELLO", "PING", "SHUTDOWN", "bridge.methods",
69
- "device.connect", "app.open", "app.terminate", "screen.get", "screen.act", "gesture.perform", "system.action",
70
- ]
71
-
72
- private func handle(_ req: [String: Any]) -> ([String: Any], Bool) {
73
- let requestId = req["requestId"] as? Int ?? 0
74
- let method = req["method"] as? String ?? ""
75
- let params = req["params"] as? [String: Any] ?? [:]
76
-
77
- func ok(_ result: [String: Any]) -> ([String: Any], Bool) {
78
- ([ "requestId": requestId, "runnerInstanceId": runnerInstanceId, "sessionToken": sessionToken, "result": result ], false)
79
- }
80
- func err(_ code: String, _ msg: String, _ rec: [String] = []) -> ([String: Any], Bool) {
81
- ([ "requestId": requestId, "runnerInstanceId": runnerInstanceId, "sessionToken": sessionToken,
82
- "error": ["error": code, "message": msg, "recovery": rec] ], false)
83
- }
84
-
85
- switch method {
86
- case "HELLO":
87
- sessionToken = params["sessionToken"] as? String ?? UUID().uuidString
88
- return ok(["protocolVersion": 1, "runnerInstanceId": runnerInstanceId, "sessionToken": sessionToken, "maxPayloadBytes": 16 * 1024 * 1024, "runnerState": "READY"])
89
- case "PING":
90
- return ok(["pong": true])
91
- case "bridge.methods":
92
- // The Runner's OWN list of served methods — the single source of truth the capability
93
- // manifest is checked against (I10). Hand-maintaining the manifest is what let it claim
94
- // FULL for methods that were never implemented; this list cannot drift from the switch
95
- // below without someone editing it, and the parity gate fails loudly when it does.
96
- return ok(["methods": Self.servedMethods])
97
- case "SHUTDOWN":
98
- return ([ "requestId": requestId, "result": ["bye": true] ], true)
99
- case "device.connect":
100
- return ok(["deviceId": params["deviceId"] as? String ?? "ios-sim", "sessionId": runnerInstanceId, "capabilities": ["screen.get", "screen.find", "screen.act", "screen.wait", "screen.diff"]])
101
- case "app.open":
102
- return appOpen(params, ok: ok, err: err)
103
- case "app.terminate":
104
- // The real terminate for a PHYSICAL device. The host path shelled out to `xcrun simctl
105
- // terminate`, which is Simulator-only — it could never have worked on hardware. XCUITest
106
- // owns the app process here, so this is both correct and the only thing available.
107
- let bid = params["bundleId"] as? String ?? ""
108
- guard !bid.isEmpty else {
109
- return err("ACTION_REJECTED", "app.terminate needs a bundleId.", ["Pass the target app's bundle identifier."])
110
- }
111
- let target = XCUIApplication(bundleIdentifier: bid)
112
- let wasRunning = target.state != .notRunning
113
- target.terminate()
114
- return ok(["terminated": true, "bundleId": bid, "wasRunning": wasRunning])
115
- case "screen.get":
116
- return ok(snapshot())
117
- case "screen.act":
118
- return screenAct(params, ok: ok, err: err)
119
- case "gesture.perform":
120
- return gesturePerform(params, ok: ok, err: err)
121
- case "system.action":
122
- let a = params["action"] as? String ?? ""
123
- // rc.2: DISPATCH + evidence only. HOME is business-level with no intrinsic predicate; the
124
- // Runner never self-reports a verdict — TS is the sole verifier.
125
- if a == "HOME" {
126
- let before = snapshot()
127
- XCUIDevice.shared.press(.home)
128
- return ok(["dispatch": ["status": "DISPATCHED", "method": "GLOBAL_KEY"], "before": before, "after": snapshot()])
129
- }
130
- return err("ACTION_UNSUPPORTED", "system.action '\(a)' is not supported on iOS.", ["iOS BACK/RECENTS are not global actions."])
131
- default:
132
- return err("ACTION_UNSUPPORTED", "Unknown bridge method '\(method)'.", ["See PROTOCOL.md."])
133
- }
134
- }
135
-
136
- // Coordinate gesture (mobile.gesture.perform): TAP / LONG_PRESS / SWIPE / DRAG by absolute screen
137
- // points, for surfaces with no semantic node to act on (media feeds, canvas, maps). Points map onto
138
- // the app window via a (0,0)-normalized coordinate + a point offset. PINCH is not expressible via a
139
- // single XCUICoordinate, so it is honestly rejected.
140
- private func gesturePerform(_ p: [String: Any], ok: ([String: Any]) -> ([String: Any], Bool), err: (String, String, [String]) -> ([String: Any], Bool)) -> ([String: Any], Bool) {
141
- func num(_ v: Any?) -> Double? { if let d = v as? Double { return d }; if let i = v as? Int { return Double(i) }; if let s = v as? String { return Double(s) }; return nil }
142
- let kind = (p["kind"] as? String ?? "TAP").uppercased()
143
- guard let x = num(p["x"]), let y = num(p["y"]) else {
144
- return err("ACTION_REJECTED", "gesture.perform needs numeric x,y.", ["Pass x,y (and x2,y2 for SWIPE/DRAG) in screen points."])
145
- }
146
- // Blind coordinate gesture: NO before/after snapshot (a heavy media feed's tree is slow to capture
147
- // and needs no semantic verification here — the caller does an explicit screen.get if it wants one).
148
- // PRIMARY path is MSHID: synthesized HID touch events that bypass the app process entirely, so a
149
- // swipe on TikTok is injected in ~ms instead of ~1.2s via XCUICoordinate (which waits on the app).
150
- // XCUICoordinate stays as a fallback only if the private HID API is unavailable on this OS.
151
- let start = CGPoint(x: x, y: y)
152
- var endPoint = start
153
- if kind == "SWIPE" || kind == "DRAG" {
154
- guard let x2 = num(p["x2"]), let y2 = num(p["y2"]) else {
155
- return err("ACTION_REJECTED", "\(kind) needs x2,y2 (end point).", ["Pass x2,y2 in screen points."])
156
- }
157
- endPoint = CGPoint(x: x2, y: y2)
158
- }
159
- guard ["TAP", "LONG_PRESS", "SWIPE", "DRAG"].contains(kind) else {
160
- return err("ACTION_UNSUPPORTED", "gesture kind '\(kind)' is not supported on iOS.", ["Use TAP, LONG_PRESS, SWIPE, or DRAG."])
161
- }
162
- // PRIMARY: HID injection (MODERN_HID iOS15+ → LEGACY_HID iOS14), which bypasses the app process so
163
- // a swipe on a heavy/animated app lands in ~ms. MSHID returns the injector name, or nil → fall back
164
- // to XCUICoordinate (slow, but always available) so the gesture is never lost.
165
- var method: String?
166
- switch kind {
167
- case "TAP": method = MSHID.tap(at: start)
168
- case "LONG_PRESS": method = MSHID.longPress(at: start, duration: 0.7)
169
- case "SWIPE": method = MSHID.swipe(from: start, to: endPoint, duration: 0.12)
170
- case "DRAG": method = MSHID.drag(from: start, to: endPoint, duration: 0.6)
171
- default: break
172
- }
173
- if method == nil {
174
- let origin = app.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0))
175
- let s = origin.withOffset(CGVector(dx: x, dy: y))
176
- switch kind {
177
- case "TAP": s.tap()
178
- case "LONG_PRESS": s.press(forDuration: 0.7)
179
- default: s.press(forDuration: kind == "DRAG" ? 0.6 : 0.08, thenDragTo: origin.withOffset(CGVector(dx: endPoint.x, dy: endPoint.y)))
180
- }
181
- method = "COORDINATE"
182
- }
183
- return ok(["dispatch": ["status": "DISPATCHED", "method": method!, "fallbackType": "GESTURE"], "screenChanged": true])
184
- }
185
-
186
- private func isValidBundleId(_ b: String) -> Bool {
187
- // reverse-DNS-ish: segments of [A-Za-z0-9-] joined by dots; guards against argv/format injection.
188
- guard b.count <= 255 else { return false }
189
- return b.range(of: "^[A-Za-z0-9][A-Za-z0-9-]*(\\.[A-Za-z0-9][A-Za-z0-9-]*)+$", options: .regularExpression) != nil
190
- }
191
-
192
- private func snapshot() -> [String: Any] {
193
- let bundle = targetBundleId ?? BridgeServer.springboard
194
- if let snap = try? app.snapshot() {
195
- return Xcui.toScreenSnapshot(snap, bundleId: bundle, deviceId: "ios-sim")
196
- }
197
- return ["schemaVersion": 1, "platform": "IOS", "nodes": [], "treeHash": "0", "packageName": bundle, "appId": bundle, "snapshotId": "s_empty", "deviceId": "ios-sim", "windowId": 0, "orientation": "portrait", "screenStateId": "ios:0", "compact": true, "timestamp": Int(Date().timeIntervalSince1970 * 1000)]
198
- }
199
-
200
- private func appOpen(_ p: [String: Any], ok: ([String: Any]) -> ([String: Any], Bool), err: (String, String, [String]) -> ([String: Any], Bool)) -> ([String: Any], Bool) {
201
- let before = snapshot()
202
- let action = p["appAction"] as? String ?? "APP_OPEN"
203
- // Optional target switch: drive any already-installed app by bundle id (no re-signing of the target).
204
- if let bundleId = (p["bundleId"] as? String), !bundleId.isEmpty {
205
- guard isValidBundleId(bundleId) else {
206
- return err("ACTION_REJECTED", "Invalid bundleId '\(bundleId)'.", ["Pass a valid iOS bundle identifier, e.g. com.apple.Preferences."])
207
- }
208
- if bundleId != targetBundleId {
209
- app = XCUIApplication(bundleIdentifier: bundleId)
210
- targetBundleId = bundleId
211
- }
212
- }
213
- switch action {
214
- case "APP_OPEN", "APP_BRING_TO_FRONT": app.activate() // launches the target if not already running
215
- case "APP_CLOSE_UI": XCUIDevice.shared.press(.home)
216
- default: break
217
- }
218
- // rc.2: DISPATCH + before/after only. TS applies the intrinsic PACKAGE_CHANGED postcondition.
219
- return ok(["dispatch": ["status": "DISPATCHED", "method": "ACCESSIBILITY_ACTION"], "before": before, "after": snapshot()])
220
- }
221
-
222
- // TS has already resolved the NodeRef; the Runner re-queries by the resolved locator, double-checks
223
- // the count (defence in depth), performs the verb, and returns before/after snapshots.
224
- private func screenAct(_ p: [String: Any], ok: ([String: Any]) -> ([String: Any], Bool), err: (String, String, [String]) -> ([String: Any], Bool)) -> ([String: Any], Bool) {
225
- let before = snapshot()
226
- let verb = p["verb"] as? String ?? "CLICK"
227
- // Narrow the live re-query to the element TYPE TS resolved, not just its label: a table row and
228
- // the static text inside it share a label, so a label-only query counted 2 and this defence
229
- // refused every list tap. `nativeType` is this Runner's own "XCUIElementType<raw>" string coming
230
- // back, so no role mapping is invented here; absent or unparsable, the scope stays .any and the
231
- // behaviour is exactly what it was.
232
- let scope: XCUIElement.ElementType = (p["nativeType"] as? String)
233
- .flatMap { UInt( $0.replacingOccurrences(of: "XCUIElementType", with: "")) }
234
- .flatMap { XCUIElement.ElementType(rawValue: $0) } ?? .any
235
- let query: XCUIElementQuery
236
- if let aid = p["automationId"] as? String, !aid.isEmpty {
237
- query = app.descendants(matching: scope).matching(identifier: aid)
238
- } else if let label = p["label"] as? String, !label.isEmpty {
239
- query = app.descendants(matching: scope).matching(NSPredicate(format: "label == %@", label))
240
- } else {
241
- return err("NODE_NOT_FOUND", "screen.act needs automationId or label.", ["Resolve a NodeRef first."])
242
- }
243
- let count = query.count
244
- if count == 0 { return err("NODE_NOT_FOUND", "No element matched on the live screen.", ["Re-read screen.get."]) }
245
- if count > 1 { return err("AMBIGUOUS_MATCH", "\(count) elements matched; refusing to guess.", ["Use a stronger locator (automationId)."]) }
246
- let el = query.element
247
- guard el.waitForExistence(timeout: 3) else { return err("NODE_NOT_FOUND", "Element did not become resolvable.", []) }
248
- switch verb {
249
- case "SET_TEXT":
250
- el.tap()
251
- // Clear any existing content first so SET_TEXT is a replace, not an append.
252
- if let cur = el.value as? String, !cur.isEmpty {
253
- el.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: cur.count))
254
- }
255
- el.typeText(p["value"] as? String ?? "")
256
- case "CLEAR_TEXT":
257
- el.tap()
258
- if let cur = el.value as? String, !cur.isEmpty {
259
- el.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: cur.count))
260
- }
261
- default:
262
- el.tap()
263
- }
264
- // Let the UI settle (SwiftUI re-render) before the after-snapshot, mirroring Android waitStable.
265
- Thread.sleep(forTimeInterval: 0.4)
266
- return ok(["before": before, "after": snapshot(), "evidence": ["screenChanged": true]])
267
- }
268
-
269
- // MARK: sockets (length-prefixed framing)
270
-
271
- private func openListener(port: UInt16) throws -> Int32 {
272
- let fd = socket(AF_INET, SOCK_STREAM, 0)
273
- guard fd >= 0 else { throw NSError(domain: "runner", code: 1) }
274
- var yes: Int32 = 1
275
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, socklen_t(MemoryLayout<Int32>.size))
276
- var addr = sockaddr_in()
277
- addr.sin_family = sa_family_t(AF_INET)
278
- addr.sin_port = port.bigEndian
279
- addr.sin_addr.s_addr = inet_addr("127.0.0.1")
280
- let bound = withUnsafePointer(to: &addr) { $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { bind(fd, $0, socklen_t(MemoryLayout<sockaddr_in>.size)) } }
281
- guard bound == 0 else { close(fd); throw NSError(domain: "runner", code: 2) }
282
- guard listen(fd, 1) == 0 else { close(fd); throw NSError(domain: "runner", code: 3) }
283
- return fd
284
- }
285
-
286
- private func readN(_ fd: Int32, _ n: Int) -> Data? {
287
- var out = Data(); out.reserveCapacity(n)
288
- var buf = [UInt8](repeating: 0, count: 65536)
289
- while out.count < n {
290
- let want = min(n - out.count, buf.count)
291
- let r = recv(fd, &buf, want, 0)
292
- if r <= 0 { return nil }
293
- out.append(contentsOf: buf[0..<r])
294
- }
295
- return out
296
- }
297
-
298
- private func readFrame(_ fd: Int32) -> [String: Any]? {
299
- guard let lenData = readN(fd, 4) else { return nil }
300
- let len = lenData.withUnsafeBytes { $0.load(as: UInt32.self).bigEndian }
301
- guard len > 0, len <= 16 * 1024 * 1024, let body = readN(fd, Int(len)) else { return nil }
302
- return (try? JSONSerialization.jsonObject(with: body)) as? [String: Any]
303
- }
304
-
305
- private func writeFrame(_ fd: Int32, _ obj: [String: Any]) {
306
- guard let body = try? JSONSerialization.data(withJSONObject: obj) else { return }
307
- var len = UInt32(body.count).bigEndian
308
- var frame = Data(bytes: &len, count: 4); frame.append(body)
309
- frame.withUnsafeBytes { raw in
310
- var off = 0
311
- let base = raw.bindMemory(to: UInt8.self).baseAddress!
312
- while off < frame.count {
313
- let w = send(fd, base + off, frame.count - off, 0)
314
- if w <= 0 { break }
315
- off += w
316
- }
317
- }
318
- }
319
- }
@@ -1,25 +0,0 @@
1
- // MSHID — HID-level gesture injection facade (Appium/WebDriverAgent technique). Synthesizes touch
2
- // events WITHOUT querying the app's accessibility tree and WITHOUT waiting on the app process, so a
3
- // TAP/SWIPE/DRAG/LONG_PRESS lands in ~ms regardless of how heavy/animated the foreground app is.
4
- //
5
- // Version-durable: a GestureInjector chain tries the MODERN path first
6
- // (XCSynthesizedEventRecord + XCPointerEventPath via XCUIDevice.eventSynthesizer, iOS 15+/current),
7
- // then the LEGACY path (XCEventGenerator, iOS 14). Each method returns the injector name that
8
- // succeeded ("MODERN_HID" / "LEGACY_HID") or nil, so the Swift caller can fall back to XCUICoordinate
9
- // and report which path ran. All coordinates are ABSOLUTE screen points.
10
- #import <Foundation/Foundation.h>
11
- #import <CoreGraphics/CoreGraphics.h>
12
-
13
- NS_ASSUME_NONNULL_BEGIN
14
-
15
- @interface MSHID : NSObject
16
- /// True when any HID injector is usable on this OS (else the caller uses XCUICoordinate).
17
- + (BOOL)available;
18
- /// Each returns the method name that injected the gesture ("MODERN_HID"/"LEGACY_HID"), or nil on failure.
19
- + (nullable NSString *)tapAt:(CGPoint)point;
20
- + (nullable NSString *)longPressAt:(CGPoint)point duration:(double)duration;
21
- + (nullable NSString *)swipeFrom:(CGPoint)from to:(CGPoint)to duration:(double)duration;
22
- + (nullable NSString *)dragFrom:(CGPoint)from to:(CGPoint)to duration:(double)duration;
23
- @end
24
-
25
- NS_ASSUME_NONNULL_END
@@ -1,155 +0,0 @@
1
- #import "MSHID.h"
2
- #import <XCTest/XCTest.h>
3
-
4
- // ---- Private XCTest event APIs (signatures from appium/WebDriverAgent PrivateHeaders) --------------
5
-
6
- // MODERN (iOS 15+/current): build a pointer path, wrap it in a synthesized event, dispatch via the
7
- // device's event synthesizer. This is what WDA switched to when Apple reworked XCTest internals.
8
- @interface XCPointerEventPath : NSObject
9
- - (instancetype)initForTouchAtPoint:(CGPoint)point offset:(double)offset;
10
- - (void)moveToPoint:(CGPoint)point atOffset:(double)offset;
11
- - (void)liftUpAtOffset:(double)offset;
12
- @end
13
-
14
- @interface XCSynthesizedEventRecord : NSObject
15
- - (instancetype)initWithName:(NSString *)name interfaceOrientation:(long long)orientation;
16
- - (void)addPointerEventPath:(XCPointerEventPath *)path;
17
- @end
18
-
19
- @interface NSObject (MSEventSynthesizer)
20
- - (id)eventSynthesizer; // on XCUIDevice
21
- - (void)synthesizeEvent:(id)event completion:(void (^)(BOOL result, NSError *error))completion; // on the synthesizer
22
- @end
23
-
24
- // LEGACY (iOS 14): the older singleton generator.
25
- @interface XCEventGenerator : NSObject
26
- + (instancetype)sharedGenerator;
27
- - (double)pressAtPoint:(CGPoint)point forDuration:(double)duration liftAtPoint:(CGPoint)liftPoint velocity:(double)velocity orientation:(long long)orientation name:(NSString *)name handler:(void (^)(id record, NSError *error))handler;
28
- - (double)pressAtPoint:(CGPoint)point forDuration:(double)duration orientation:(long long)orientation handler:(void (^)(id record, NSError *error))handler;
29
- @end
30
-
31
- // Portrait; the CMS coordinates are already in portrait screen points.
32
- static const long long kPortrait = 1;
33
-
34
- // Run an async injector and wait (bounded) for its completion. The Runner's socket loop is NOT the main
35
- // thread, so a plain semaphore wait here does not deadlock a completion delivered on the main queue.
36
- static BOOL MSAwait(double timeout, void (^inject)(dispatch_semaphore_t sem)) {
37
- dispatch_semaphore_t sem = dispatch_semaphore_create(0);
38
- @try { inject(sem); }
39
- @catch (NSException *e) { return NO; }
40
- return dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))) == 0;
41
- }
42
-
43
- // ---- Injector protocol + two implementations ------------------------------------------------------
44
-
45
- @protocol MSGestureInjector <NSObject>
46
- - (BOOL)available;
47
- - (NSString *)name;
48
- - (BOOL)tapAt:(CGPoint)p;
49
- - (BOOL)longPressAt:(CGPoint)p duration:(double)dur;
50
- - (BOOL)swipeFrom:(CGPoint)from to:(CGPoint)to duration:(double)dur;
51
- - (BOOL)dragFrom:(CGPoint)from to:(CGPoint)to duration:(double)dur;
52
- @end
53
-
54
- // MODERN — XCSynthesizedEventRecord + XCPointerEventPath via XCUIDevice.eventSynthesizer.
55
- @interface MSModernInjector : NSObject <MSGestureInjector>
56
- @end
57
- @implementation MSModernInjector
58
- - (NSString *)name { return @"MODERN_HID"; }
59
- - (id)synthesizer {
60
- XCUIDevice *dev = [XCUIDevice sharedDevice];
61
- if (![dev respondsToSelector:@selector(eventSynthesizer)]) return nil;
62
- return [dev eventSynthesizer];
63
- }
64
- - (BOOL)available {
65
- return NSClassFromString(@"XCSynthesizedEventRecord") && NSClassFromString(@"XCPointerEventPath") && [self synthesizer];
66
- }
67
- // Build a single-finger path: touch-down at `from`, optional hold, move to `to`, lift. A tap is from==to.
68
- - (BOOL)injectPathFrom:(CGPoint)from to:(CGPoint)to hold:(double)hold move:(double)move name:(NSString *)name {
69
- id synth = [self synthesizer]; if (!synth) return NO;
70
- Class pathC = NSClassFromString(@"XCPointerEventPath");
71
- Class recC = NSClassFromString(@"XCSynthesizedEventRecord");
72
- if (!pathC || !recC) return NO;
73
- XCPointerEventPath *path = [[pathC alloc] initForTouchAtPoint:from offset:0.0];
74
- double t = 0.0;
75
- if (hold > 0) { [path moveToPoint:from atOffset:(t += hold)]; }
76
- if (!CGPointEqualToPoint(from, to)) { [path moveToPoint:to atOffset:(t += MAX(move, 0.01))]; }
77
- else { t += 0.05; }
78
- [path liftUpAtOffset:t];
79
- XCSynthesizedEventRecord *rec = [[recC alloc] initWithName:name interfaceOrientation:kPortrait];
80
- [rec addPointerEventPath:path];
81
- __block BOOL ok = NO;
82
- BOOL fired = MSAwait(t + 5.0, ^(dispatch_semaphore_t sem) {
83
- [synth synthesizeEvent:rec completion:^(BOOL result, NSError *error) { ok = (error == nil); dispatch_semaphore_signal(sem); }];
84
- });
85
- return fired && ok;
86
- }
87
- - (BOOL)tapAt:(CGPoint)p { return [self injectPathFrom:p to:p hold:0 move:0 name:@"tap"]; }
88
- - (BOOL)longPressAt:(CGPoint)p duration:(double)dur { return [self injectPathFrom:p to:p hold:dur move:0 name:@"longpress"]; }
89
- - (BOOL)swipeFrom:(CGPoint)f to:(CGPoint)t duration:(double)dur { return [self injectPathFrom:f to:t hold:0 move:dur name:@"swipe"]; }
90
- - (BOOL)dragFrom:(CGPoint)f to:(CGPoint)t duration:(double)dur { return [self injectPathFrom:f to:t hold:0.5 move:dur name:@"drag"]; }
91
- @end
92
-
93
- // LEGACY — XCEventGenerator (iOS 14).
94
- @interface MSLegacyInjector : NSObject <MSGestureInjector>
95
- @end
96
- @implementation MSLegacyInjector
97
- - (NSString *)name { return @"LEGACY_HID"; }
98
- - (nullable XCEventGenerator *)gen {
99
- Class cls = NSClassFromString(@"XCEventGenerator");
100
- if (!cls) return nil;
101
- @try { return [cls sharedGenerator]; } @catch (NSException *e) { return nil; }
102
- }
103
- - (BOOL)available { return [self gen] != nil; }
104
- - (BOOL)tapAt:(CGPoint)p {
105
- XCEventGenerator *g = [self gen]; if (!g) return NO;
106
- return MSAwait(5.0, ^(dispatch_semaphore_t sem) {
107
- [g pressAtPoint:p forDuration:0.0 liftAtPoint:p velocity:0 orientation:kPortrait name:@"tap" handler:^(id r, NSError *e) { dispatch_semaphore_signal(sem); }];
108
- });
109
- }
110
- - (BOOL)longPressAt:(CGPoint)p duration:(double)dur {
111
- XCEventGenerator *g = [self gen]; if (!g) return NO;
112
- return MSAwait(dur + 5.0, ^(dispatch_semaphore_t sem) {
113
- [g pressAtPoint:p forDuration:dur orientation:kPortrait handler:^(id r, NSError *e) { dispatch_semaphore_signal(sem); }];
114
- });
115
- }
116
- - (BOOL)swipeFrom:(CGPoint)f to:(CGPoint)t duration:(double)dur { return [self dragFrom:f to:t duration:dur press:0.0]; }
117
- - (BOOL)dragFrom:(CGPoint)f to:(CGPoint)t duration:(double)dur { return [self dragFrom:f to:t duration:dur press:0.5]; }
118
- - (BOOL)dragFrom:(CGPoint)f to:(CGPoint)t duration:(double)dur press:(double)press {
119
- XCEventGenerator *g = [self gen]; if (!g) return NO;
120
- double vel = hypot(t.x - f.x, t.y - f.y) / MAX(dur, 0.05);
121
- return MSAwait(dur + press + 5.0, ^(dispatch_semaphore_t sem) {
122
- [g pressAtPoint:f forDuration:press liftAtPoint:t velocity:vel orientation:kPortrait name:@"swipe" handler:^(id r, NSError *e) { dispatch_semaphore_signal(sem); }];
123
- });
124
- }
125
- @end
126
-
127
- // ---- Facade: try MODERN then LEGACY; return the name that ran (or nil → caller uses XCUICoordinate) --
128
-
129
- @implementation MSHID
130
-
131
- + (NSArray<id<MSGestureInjector>> *)chain {
132
- static NSArray *chain; static dispatch_once_t once;
133
- dispatch_once(&once, ^{ chain = @[[MSModernInjector new], [MSLegacyInjector new]]; });
134
- return chain;
135
- }
136
-
137
- + (BOOL)available {
138
- for (id<MSGestureInjector> inj in [self chain]) { if ([inj available]) return YES; }
139
- return NO;
140
- }
141
-
142
- + (nullable NSString *)run:(BOOL (^)(id<MSGestureInjector> inj))op {
143
- for (id<MSGestureInjector> inj in [self chain]) {
144
- if (![inj available]) continue;
145
- if (op(inj)) return [inj name];
146
- }
147
- return nil;
148
- }
149
-
150
- + (nullable NSString *)tapAt:(CGPoint)p { return [self run:^BOOL(id<MSGestureInjector> i) { return [i tapAt:p]; }]; }
151
- + (nullable NSString *)longPressAt:(CGPoint)p duration:(double)d { return [self run:^BOOL(id<MSGestureInjector> i) { return [i longPressAt:p duration:d]; }]; }
152
- + (nullable NSString *)swipeFrom:(CGPoint)f to:(CGPoint)t duration:(double)d { return [self run:^BOOL(id<MSGestureInjector> i) { return [i swipeFrom:f to:t duration:d]; }]; }
153
- + (nullable NSString *)dragFrom:(CGPoint)f to:(CGPoint)t duration:(double)d { return [self run:^BOOL(id<MSGestureInjector> i) { return [i dragFrom:f to:t duration:d]; }]; }
154
-
155
- @end
@@ -1,2 +0,0 @@
1
- // Swift ↔ ObjC bridging header for the UITests target: exposes MSHID (HID gesture injection) to Swift.
2
- #import "MSHID.h"
@@ -1,90 +0,0 @@
1
- import Foundation
2
- import XCTest
3
-
4
- // Real XCUIElementSnapshot -> platform-neutral ScreenSnapshot (protocol/schemas). This is the
5
- // production mapper (the Sources/MsmSupervisor/SnapshotModel.swift copy is the no-XCTest stand-in).
6
- enum Xcui {
7
-
8
- static func role(for type: XCUIElement.ElementType) -> String {
9
- switch type {
10
- case .button: return "BUTTON"
11
- case .staticText: return "TEXT"
12
- case .textField, .secureTextField, .searchField: return "INPUT"
13
- case .image: return "IMAGE"
14
- case .checkBox: return "CHECKBOX"
15
- case .radioButton: return "RADIO"
16
- case .switch, .toggle: return "SWITCH"
17
- case .tab, .tabBar: return "TAB"
18
- case .link: return "LINK"
19
- case .slider: return "SLIDER"
20
- case .progressIndicator: return "PROGRESS"
21
- case .table, .collectionView: return "LIST"
22
- case .cell: return "LIST_ITEM"
23
- case .webView: return "WEB_VIEW"
24
- case .window, .application: return "SCREEN"
25
- case .sheet, .alert, .dialog: return "DIALOG"
26
- default: return "UNKNOWN"
27
- }
28
- }
29
-
30
- /// FNV-1a over the ordered node signatures — parity with the Android collector's treeHash.
31
- static func treeHash(_ nodes: [[String: Any]]) -> String {
32
- let sig = nodes.map { n in
33
- [n["role"] as? String ?? "", n["label"] as? String ?? "", n["value"] as? String ?? "",
34
- n["automationId"] as? String ?? "", (n["bounds"] as? [Int])?.map(String.init).joined(separator: ",") ?? ""].joined(separator: "|")
35
- }.joined(separator: "\n")
36
- var h: UInt32 = 2166136261
37
- for b in sig.utf8 { h = (h ^ UInt32(b)) &* 16777619 }
38
- return String(format: "%08x", h)
39
- }
40
-
41
- static func toScreenSnapshot(_ root: XCUIElementSnapshot, bundleId: String, deviceId: String) -> [String: Any] {
42
- var nodes: [[String: Any]] = []
43
- var counter = 0
44
- var truncated = false
45
- // PERF (C): bound the tree on heavy apps (a TikTok/feed hierarchy is huge and slow to serialize on
46
- // a 2GB device). Cap depth + total nodes; env-overridable. Truncation is reported, never silent.
47
- let env = ProcessInfo.processInfo.environment
48
- let maxDepth = Int(env["MSM_IOS_MAX_DEPTH"] ?? "") ?? 60
49
- let maxNodes = Int(env["MSM_IOS_MAX_NODES"] ?? "") ?? 3000
50
- func walk(_ el: XCUIElementSnapshot, parentId: String?, depth: Int) {
51
- if nodes.count >= maxNodes || depth > maxDepth { truncated = true; return }
52
- let f = el.frame
53
- // PERF: drop zero-area layout artifacts (they cannot be seen or tapped), but keep descending so
54
- // any visible children still surface — this trims the tree on dense apps without losing a
55
- // real target. The child keeps the current parentId so the hierarchy stays connected.
56
- if f.width <= 0 || f.height <= 0 {
57
- for c in el.children { walk(c, parentId: parentId, depth: depth + 1) }
58
- return
59
- }
60
- let id = "n\(counter)"; counter += 1
61
- var node: [String: Any] = [
62
- "nodeId": id,
63
- "role": role(for: el.elementType),
64
- "nativeType": "XCUIElementType\(el.elementType.rawValue)",
65
- ]
66
- if let p = parentId { node["parentId"] = p } else { node["parentId"] = NSNull() }
67
- let ident = el.identifier
68
- if !ident.isEmpty { node["automationId"] = ident; node["resourceId"] = ident }
69
- if !el.label.isEmpty { node["label"] = el.label }
70
- if let v = el.value as? String, !v.isEmpty { node["value"] = v }
71
- node["bounds"] = [Int(f.minX), Int(f.minY), Int(f.maxX), Int(f.maxY)]
72
- let isSecure = el.elementType == .secureTextField
73
- node["state"] = ["enabled": el.isEnabled, "selected": el.isSelected, "password": isSecure]
74
- nodes.append(node)
75
- for c in el.children { walk(c, parentId: id, depth: depth + 1) }
76
- }
77
- walk(root, parentId: nil, depth: 0)
78
- let th = treeHash(nodes)
79
- return [
80
- "schemaVersion": 1, "platform": "IOS", "deviceId": deviceId,
81
- "snapshotId": "s_\(UUID().uuidString.prefix(8))",
82
- // Epoch MILLISECONDS, per common.schema.json#/$defs/timestampMs. An ISO8601 string here
83
- // made every iOS screen.get fail a strict client's output-schema check.
84
- "timestamp": Int(Date().timeIntervalSince1970 * 1000),
85
- "packageName": bundleId, "appId": bundleId, "windowId": 0, "orientation": "portrait",
86
- "screenStateId": "ios:\(th.prefix(4))", "compact": true, "nodes": nodes, "treeHash": th,
87
- "truncated": truncated,
88
- ]
89
- }
90
- }
@@ -1,60 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Generate MobileSemantic.xcodeproj — ONE target: the standalone MobileSemanticUITests UI-test bundle
3
- # (WebDriverAgent style: no host application). The bundle's BridgeServer IS the device driver; it drives
4
- # arbitrary apps via XCUIApplication(bundleIdentifier:), so it needs no fixture/host app of its own.
5
- # No Xcode GUI, no xcodegen. Run: `ruby gen_project.rb`.
6
- require 'xcodeproj'
7
-
8
- here = File.dirname(File.expand_path(__FILE__))
9
- proj_path = File.join(here, 'MobileSemantic.xcodeproj')
10
- proj = Xcodeproj::Project.new(proj_path)
11
-
12
- DEPLOY = '14.0'
13
- # Physical-device signing (M5). Simulator builds ignore this; device builds use automatic signing
14
- # with this development team. Override with MSM_DEV_TEAM to use a different Apple account.
15
- DEV_TEAM = ENV['MSM_DEV_TEAM'] || 'A2KNU2X4F7'
16
-
17
- # --- UI-test bundle target (the ONLY target: the driver) ---------------------
18
- # A target-less ui_test_bundle: xcodebuild auto-generates the MobileSemanticUITests-Runner host app,
19
- # exactly like WebDriverAgent. No TEST_TARGET_NAME / no app dependency, so there is no second app to
20
- # install or confuse with the driver.
21
- uitest = proj.new_target(:ui_test_bundle, 'MobileSemanticUITests', :ios, DEPLOY)
22
- ui_group = proj.new_group('UITests', 'UITests')
23
- # Swift sources + the ObjC HID shim (MSHID.m). A bridging header (set below) exposes MSHID to Swift.
24
- Dir.glob(File.join(here, 'UITests', '*.{swift,m}')).sort.each do |f|
25
- ref = ui_group.new_file(f)
26
- uitest.source_build_phase.add_file_reference(ref)
27
- end
28
-
29
- # --- build settings ----------------------------------------------------------
30
- common = {
31
- 'IPHONEOS_DEPLOYMENT_TARGET' => DEPLOY,
32
- 'SWIFT_VERSION' => '5.0',
33
- # Automatic signing so a device build works; the simulator SDK skips signing regardless.
34
- 'CODE_SIGN_STYLE' => 'Automatic',
35
- 'DEVELOPMENT_TEAM' => DEV_TEAM,
36
- 'CODE_SIGN_IDENTITY[sdk=iphoneos*]' => 'Apple Development',
37
- 'CODE_SIGNING_ALLOWED[sdk=iphonesimulator*]' => 'NO',
38
- 'GENERATE_INFOPLIST_FILE' => 'YES',
39
- 'CURRENT_PROJECT_VERSION' => '1',
40
- 'MARKETING_VERSION' => '1.0',
41
- 'ALWAYS_SEARCH_USER_PATHS' => 'NO',
42
- }
43
- uitest.build_configurations.each do |c|
44
- c.build_settings.merge!(common)
45
- c.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = 'com.msm.MobileSemanticUITests'
46
- # Bridging header so Swift can call the ObjC HID shim (MSHID); enable ObjC modules.
47
- c.build_settings['SWIFT_OBJC_BRIDGING_HEADER'] = 'UITests/MobileSemanticUITests-Bridging.h'
48
- c.build_settings['CLANG_ENABLE_MODULES'] = 'YES'
49
- c.build_settings['SWIFT_INSTALL_OBJC_HEADER'] = 'NO'
50
- end
51
-
52
- # --- shared scheme so `xcodebuild -scheme MobileSemanticUITests test` works ---
53
- proj.save
54
-
55
- scheme = Xcodeproj::XCScheme.new
56
- scheme.add_build_target(uitest)
57
- scheme.add_test_target(uitest)
58
- scheme.save_as(proj_path, 'MobileSemanticUITests', true)
59
-
60
- puts "generated #{proj_path} (standalone UITests-Runner, no fixture app)"