1#if DEBUG
2import UIKit
3
4private var scriptedPage = false
5
6extension CanvasView {
7 /// Replays `SNOWBOUND_SCRIPT` through the calls touch and the keyboard make, to check
8 /// input without a finger: steps joined by `|`, such as
9 /// `tap:120,300|type:hi|mark:かな|unmark|return|delete|select:2,9|scroll:0,600|zoom:1.5|shot:a`.
10 /// `tap` and `doubletap` take points from the view's corner, `select` text offsets,
11 /// `scroll` a content offset; `format:N` applies `sb_view_apply` formatting, `find:word`
12 /// selects a match, `title` edits the title, `picture` inserts a drawn picture, `selectall`
13 /// is the Select All command; `pencil:x,y;x,y;…` draws through points as the Pencil does,
14 /// `tool:pen:N`, `tool:shape:N`, `tool:eraser` or `tool:lasso` gives it a tool, `picker` shows or hides the
15 /// drawing tools, `pencildoubletap` is the Pencil's double tap, and `deleteink` deletes
16 /// what the lasso picked; `rotate:landscape` or `rotate:portrait` turns the device; `done`
17 /// ends editing, `reading` turns the reading view on or off, `record:audio` or
18 /// `record:video` records, `pauserecording` pauses or resumes and `stoprecording` stops it,
19 /// `wait:N` waits N seconds more before the next step, `tree` saves the view hierarchy to
20 /// Documents/tree.txt, and `shot:a` the window to Documents/a.png, as a device has no
21 /// screenshot command; `snap:a` only logs, for a simulator screenshot with the keyboard;
22 /// `activate:Label` activates the element of any window so named, as VoiceOver would,
23 /// opening a button's menu or picking a menu's item or a list's row (`Row/Button` for a
24 /// button in a row), `back` goes back, and `audit` logs whether the format bar and tag grid
25 /// name every button.
26 func runScript() {
27 // Only the first page shown runs it, not every page opened afterwards.
28 guard !scriptedPage, let script = ProcessInfo.processInfo.environment["SNOWBOUND_SCRIPT"] else { return }
29 scriptedPage = true
30 // UIKit's accessibility, as VoiceOver or a UI test loads it, for `activate` and `audit`.
31 if let library = dlopen("/usr/lib/libAccessibility.dylib", RTLD_NOW),
32 let enable = dlsym(library, "_AXSSetAutomationEnabled")
33 {
34 unsafeBitCast(enable, to: (@convention(c) (Int32) -> Void).self)(1)
35 }
36 var delay = 1.0
37 for step in script.split(separator: "|") {
38 if step.hasPrefix("wait:") { delay += Double(step.dropFirst(5)) ?? 0 }
39 // Held, so that steps after the page goes, as Delete Page takes it, still run.
40 DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [self] in perform(String(step)) }
41 delay += 0.4
42 }
43 }
44
45 private func perform(_ step: String) {
46 let (command, argument) = step.firstIndex(of: ":").map {
47 (String(step[..<$0]), String(step[step.index(after: $0)...]))
48 } ?? (step, "")
49 NSLog("script \(command) \(argument)")
50 switch command {
51 case "tap":
52 let values = argument.split(separator: ",").compactMap { Double($0) }
53 tap(at: CGPoint(x: values[0] + contentOffset.x, y: values[1] + contentOffset.y))
54 case "pencil":
55 // Points as `x,y`, or `x,y,pressure` for the Pencil's pressure from 0 to 1.
56 let points = argument.split(separator: ";").map {
57 let values = $0.split(separator: ",").compactMap { Double($0) }
58 let point = CGPoint(x: values[0] + contentOffset.x, y: values[1] + contentOffset.y)
59 return (point, values.count > 2 ? Float(values[2]) : -1) as InkSample
60 }
61 inkPressed(points[0])
62 inkMoved(points.dropFirst())
63 inkReleased()
64 case "tool":
65 let parts = argument.split(separator: ":")
66 switch parts.first {
67 case "pen": setInkTool(.pen(UInt8(parts.last ?? "") ?? 0))
68 case "eraser": setInkTool(.eraser)
69 case "lasso": setInkTool(.lasso)
70 case "shape": setInkTool(.shape(UInt8(parts.last ?? "") ?? 0))
71 default: break
72 }
73 case "picker": showPicker(!pickerShown)
74 case "pencildoubletap": pencilInteractionDidTap(UIPencilInteraction())
75 case "deleteink": deleteInk()
76 case "type": insertText(argument)
77 case "return": insertText("\n")
78 case "delete": deleteBackward()
79 case "mark": setMarkedText(argument, selectedRange: NSRange(location: argument.utf16.count, length: 0))
80 case "unmark": unmarkText()
81 case "select":
82 let values = argument.split(separator: ",").compactMap { Int($0) }
83 // As a gesture would, the change reaches the system's text interaction.
84 inputDelegate?.selectionWillChange(self)
85 defer { inputDelegate?.selectionDidChange(self) }
86 selectedTextRange = textRange(
87 from: position(from: beginningOfDocument, offset: values[0])!,
88 to: position(from: beginningOfDocument, offset: values[1])!)
89 case "scroll":
90 // `scroll:,Y` keeps the horizontal offset.
91 let values = argument.split(separator: ",", omittingEmptySubsequences: false).map { Double($0) }
92 setContentOffset(CGPoint(x: values[0] ?? contentOffset.x, y: values[1] ?? contentOffset.y), animated: true)
93 case "zoom": setZoomScale(Double(argument) ?? 1, animated: false)
94 case "doubletap":
95 let values = argument.split(separator: ",").compactMap { Double($0) }
96 zoom(at: CGPoint(x: values[0] + contentOffset.x, y: values[1] + contentOffset.y))
97 case "rotate":
98 let orientation: UIInterfaceOrientationMask = argument == "portrait" ? .portrait : .landscapeRight
99 window?.windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation))
100 case "menu":
101 guard let range = selectedTextRange,
102 let menu = interactions.lazy.compactMap({ $0 as? UIEditMenuInteraction }).first(where: { $0.delegate === self })
103 else { break }
104 let caret = caretRect(for: range.start)
105 menu.presentEditMenu(with: UIEditMenuConfiguration(identifier: nil, sourcePoint: CGPoint(x: caret.midX, y: caret.minY)))
106 case "done": _ = resignFirstResponder()
107 case "back":
108 // The innermost navigation stack shown, as its Back button pops it.
109 func stacks(_ controller: UIViewController) -> [UINavigationController] {
110 controller.children.flatMap(stacks) + [controller as? UINavigationController].compactMap { $0 }
111 }
112 let shown = Self.windows.compactMap(\.rootViewController).flatMap(stacks)
113 shown.first { $0.viewControllers.count > 1 && $0.viewIfLoaded?.window != nil }?.popViewController(animated: true)
114 case "wait", "snap": break
115 case "activate":
116 // An alert's button, which takes no accessibility activation outside VoiceOver.
117 let alert = Self.windows.lazy.compactMap { window in
118 sequence(first: window.rootViewController, next: { $0?.presentedViewController }).compactMap { $0 }.last
119 }.compactMap { $0 as? UIAlertController }.first
120 if let alert, let action = alert.actions.first(where: { $0.title == argument }),
121 let handler = action.value(forKey: "handler")
122 {
123 alert.dismiss(animated: true) {
124 unsafeBitCast(handler as AnyObject, to: (@convention(block) (UIAlertAction) -> Void).self)(action)
125 }
126 break
127 }
128 // `Row/Button` names a button within a row.
129 let names = argument.split(separator: "/").map(String.init)
130 var found: NSObject? = Self.windows.reversed().lazy.compactMap { Self.element(names[0], in: $0) }.first
131 for name in names.dropFirst() {
132 found = found.flatMap { Self.children($0).lazy.compactMap { Self.element(name, in: $0) }.first }
133 }
134 guard let found else {
135 NSLog("script activate: no \(argument)")
136 break
137 }
138 let chain = (found as? UIView).map { Array(sequence(first: $0, next: \.superview)) } ?? []
139 let named = { (name: String) in chain.first { NSStringFromClass(type(of: $0)) == name } }
140 if let button = found as? UIControl, button.showsMenuAsPrimaryAction, let menu = button.contextMenuInteraction {
141 let middle = CGPoint(x: button.bounds.midX, y: button.bounds.midY)
142 menu.perform(Selector(("_presentMenuAtLocation:")), with: NSValue(cgPoint: middle))
143 } else if let cell = chain.lazy.compactMap({ $0 as? UICollectionViewCell }).first,
144 let list = chain.lazy.compactMap({ $0 as? UICollectionView }).first, let index = list.indexPath(for: cell)
145 {
146 // A menu's item or a list's row, as a tap picks it.
147 if let items = named("_UIContextMenuListView"), let menu = named("_UIContextMenuView"),
148 let item = items.perform(Selector(("elementAtIndexPath:")), with: index)?.takeUnretainedValue()
149 {
150 menu.perform(Selector(("_handleSelectionForElement:")), with: item)
151 } else {
152 list.selectItem(at: index, animated: false, scrollPosition: [])
153 list.delegate?.collectionView?(list, didSelectItemAt: index)
154 }
155 } else if let cell = found as? UITableViewCell, let table = chain.lazy.compactMap({ $0 as? UITableView }).first,
156 let index = table.indexPath(for: cell)
157 {
158 table.selectRow(at: index, animated: false, scrollPosition: .none)
159 table.delegate?.tableView?(table, didSelectRowAt: index)
160 } else if !found.accessibilityActivate(), let control = found as? UIControl {
161 control.sendActions(for: .primaryActionTriggered)
162 }
163 case "audit": audit()
164 case "record", "stoprecording":
165 let page = sequence(first: self as UIResponder, next: \.next).lazy.compactMap { $0 as? PageViewController }.first
166 if command == "record" { page?.record(video: argument == "video") } else { page?.stopRecording() }
167 case "pauserecording": Recorder.current.map { $0.pause(!($0.elapsed?.paused ?? false)) }
168 case "selectall": selectAll(nil)
169 case "reading": showReading(!readingShown)
170 case "format": apply(UInt8(argument) ?? 0)
171 case "find": _ = find(argument)
172 case "title": _ = focusTitle()
173 case "picture":
174 let image = UIGraphicsImageRenderer(size: CGSize(width: 320, height: 200)).image { context in
175 UIColor.systemTeal.setFill()
176 context.fill(CGRect(x: 0, y: 0, width: 320, height: 200))
177 UIColor.systemYellow.setFill()
178 context.cgContext.fillEllipse(in: CGRect(x: 110, y: 50, width: 100, height: 100))
179 }
180 if let data = image.jpegData(compressionQuality: 0.9) { insertPicture(data, size: CGSize(width: 240, height: 150)) }
181 case "tree":
182 let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
183 try? "\(value(forKey: "recursiveDescription") ?? "")".write(
184 to: documents.appendingPathComponent("tree.txt"), atomically: true, encoding: .utf8)
185 case "shot":
186 guard let window else { break }
187 let image = UIGraphicsImageRenderer(bounds: window.bounds).image { _ in
188 window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
189 }
190 let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
191 try? image.pngData()?.write(to: documents.appendingPathComponent("\(argument).png"))
192 default: NSLog("script: unknown step \(step)")
193 }
194 if let range = selectedTextRange, let all = textRange(from: beginningOfDocument, to: endOfDocument) {
195 NSLog("script selection \(offset(from: beginningOfDocument, to: range.start))..\(offset(from: beginningOfDocument, to: range.end)) text \(text(in: all) ?? "nil")")
196 }
197 }
198}
199
200extension CanvasView {
201 /// The scene's windows, menus' and alerts' among them.
202 fileprivate static var windows: [UIWindow] {
203 UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.flatMap(\.windows)
204 }
205
206 /// The frontmost view or accessibility element under `root` VoiceOver names `label`.
207 fileprivate static func element(_ label: String, in root: NSObject) -> NSObject? {
208 if let view = root as? UIView, view.isHidden { return nil }
209 if root.isAccessibilityElement, root.accessibilityLabel == label { return root }
210 // A loop, as a lazy `first` evaluates each match twice, doubling at every level.
211 for child in children(root) {
212 if let found = element(label, in: child) { return found }
213 }
214 return nil
215 }
216
217 /// `root`'s subviews, frontmost first, then its accessibility elements.
218 fileprivate static func children(_ root: NSObject) -> [NSObject] {
219 ((root as? UIView)?.subviews ?? []).reversed() + (root.accessibilityElements as? [NSObject] ?? [])
220 }
221
222 /// Logs `audit ok`, or each button of the format bar and tag grid VoiceOver can't name.
223 fileprivate func audit() {
224 func buttons(_ view: UIView) -> [UIButton] {
225 (view as? UIButton).map { [$0] } ?? view.subviews.flatMap(buttons)
226 }
227 var problems = buttons(formatBar).filter { ($0.accessibilityLabel ?? "").isEmpty }.map { "unnamed \($0)" }
228 if let grid = TagGrid.shown {
229 let named = buttons(grid).filter {
230 $0.isAccessibilityElement && $0.accessibilityTraits.contains(.button) && !($0.accessibilityLabel ?? "").isEmpty
231 }.compactMap(\.accessibilityLabel)
232 let expected = Tags.all.prefix(Tags.keyed).map(\.name) + ["More Tags…", "Remove Tag"]
233 if named != expected { problems.append("grid names \(named)") }
234 if !grid.accessibilityViewIsModal { problems.append("grid not modal") }
235 }
236 NSLog(problems.isEmpty ? "audit ok, grid \(TagGrid.shown != nil)" : "audit FAIL \(problems)")
237 }
238}
239
240/// Logs `presenter changed` whenever a coordinated write reaches `SNOWBOUND_PRESENTER`, a
241/// file, to check that saving tells file providers about it.
242final class Presenter: NSObject, NSFilePresenter {
243 private static var watching: Presenter?
244 let presentedItemURL: URL?
245 let presentedItemOperationQueue = OperationQueue()
246
247 private init(_ url: URL) { presentedItemURL = url }
248
249 static func watch() {
250 guard let path = ProcessInfo.processInfo.environment["SNOWBOUND_PRESENTER"] else { return }
251 let presenter = Presenter(URL(fileURLWithPath: path))
252 NSFileCoordinator.addFilePresenter(presenter)
253 watching = presenter
254 }
255
256 func presentedItemDidChange() { NSLog("presenter changed") }
257}
258#endif