App/UITests/FoldGestureTests.swift
89 lines · 4726 bytes
9 symbols in this file
1import XCTest
2
3final class FoldGestureTests: XCTestCase {
4 override func setUp() {
5 continueAfterFailure = false
6 }
7
8 func testCompletingAStepAdvancesToTheNext() {
9 let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch()
10 app.buttons["Cup"].tap()
11 completeCurrentFold(app) // drag along the moving facet's path to t = 1, then lift
12 XCTAssertEqual(app.staticTexts["step.index"].label, "2")
13 }
14
15 func testReleasingPartwayLeavesTheStepWhereItIs() {
16 let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch()
17 app.buttons["Cup"].tap()
18 dragCurrentFold(app, to: 0.5)
19 let released = app.otherElements["fold.paper"].value as? String
20 sleep(2)
21 XCTAssertEqual(app.staticTexts["step.index"].label, "1")
22 XCTAssertEqual(app.otherElements["fold.paper"].value as? String, released)
23 XCTAssertNotEqual(released, "0%")
24 }
25}
26
27/// Drags the current step's moving faces along their path to its end, then lifts.
28func completeCurrentFold(_ app: XCUIApplication) {
29 dragCurrentFold(app, to: 1)
30}
31
32/// Drags the current step's moving faces along their path, read from `fold.path` (the app must be
33/// launched with `-UITesting`), to `fraction` of it, then lifts. Each drag is one straight
34/// press-and-drag through every 32nd sample and every turn, with short ones merged so each moves
35/// at least `minimumChord` points: a DragGesture does not begin under 10. A turn is where the path
36/// goes back the way it came, at a fold-and-unfold's peak or at a non-rigid step's phase
37/// boundaries (t = ⅓ and ⅔); a drag to a turn continues 6 points past it, since the drag after it
38/// starts back along the way it came, and a drag to the end continues 30 points past it. The app
39/// reads only a drag's translation, so a drag from a point outside the paper view starts at its
40/// centre instead. Each holds before lifting, so its last position arrives before the lift.
41func dragCurrentFold(_ app: XCUIApplication, to fraction: Double) {
42 let paper = app.otherElements["fold.paper"]
43 let marker = app.descendants(matching: .any)["fold.path"]
44 XCTAssertTrue(marker.waitForExistence(timeout: 5))
45 let samples = (marker.value as? String ?? "").split(separator: " ").map { pair in
46 let xy = pair.split(separator: ",").map { Double($0)! }
47 return CGVector(dx: xy[0], dy: xy[1])
48 }
49 XCTAssertFalse(samples.isEmpty)
50 let size = paper.frame.size
51 func chord(_ a: Int, _ b: Int) -> CGVector {
52 CGVector(dx: (samples[b].dx - samples[a].dx) * size.width, dy: (samples[b].dy - samples[a].dy) * size.height)
53 }
54 func length(_ v: CGVector) -> Double { (v.dx * v.dx + v.dy * v.dy).squareRoot() }
55 func dot(_ u: CGVector, _ v: CGVector) -> Double { u.dx * v.dx + u.dy * v.dy }
56 let last = Int((fraction * Double(samples.count - 1)).rounded())
57
58 // a turn: the path arrives along one direction and leaves against it; its sample is the one
59 // furthest along the arrival within two of the boundary's
60 let boundaries = stepKind(app) == "foldAndUnfold" ? [64] : [43, 85]
61 var turns: [Int] = []
62 for c in boundaries where c + 2 < last {
63 let arriving = chord(c - 8, c), leaving = chord(c, min(c + 24, last))
64 guard length(arriving) > 2, length(leaving) > 2, dot(arriving, leaving) < 0 else { continue }
65 turns.append(((c - 2)...(c + 2)).max { dot(chord(0, $0), arriving) < dot(chord(0, $1), arriving) }!)
66 }
67 var stops = [0]
68 let regular = stride(from: 32, to: last, by: 32).filter { i in !turns.contains { abs($0 - i) < 8 } }
69 for i in (regular + turns + [last]).sorted() {
70 if !turns.contains(i) && i != last && length(chord(stops.last!, i)) < minimumChord { continue }
71 while stops.count > 1 && !turns.contains(stops.last!) && length(chord(stops.last!, i)) < minimumChord {
72 stops.removeLast()
73 }
74 stops.append(i)
75 }
76
77 for (a, b) in zip(stops, stops.dropFirst()) {
78 var move = chord(a, b)
79 let l = max(length(move), 1), past = b == samples.count - 1 ? 30.0 : turns.contains(b) ? 6.0 : 0.0
80 move = CGVector(dx: move.dx + move.dx / l * past, dy: move.dy + move.dy / l * past)
81 let inside = (0.02...0.98).contains(samples[a].dx) && (0.02...0.98).contains(samples[a].dy)
82 let start = paper.coordinate(withNormalizedOffset: inside ? samples[a] : CGVector(dx: 0.5, dy: 0.5))
83 start.press(forDuration: 0.1, thenDragTo: start.withOffset(move), withVelocity: .default,
84 thenHoldForDuration: 0.3)
85 }
86}
87
88/// The shortest straight drag `dragCurrentFold` makes, in points.
89private let minimumChord = 12.0