import XCTest final class FoldGestureTests: XCTestCase { override func setUp() { continueAfterFailure = false } func testCompletingAStepAdvancesToTheNext() { let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() app.buttons["Cup"].tap() completeCurrentFold(app) // drag along the moving facet's path to t = 1, then lift XCTAssertEqual(app.staticTexts["step.index"].label, "2") } func testReleasingPartwayLeavesTheStepWhereItIs() { let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() app.buttons["Cup"].tap() dragCurrentFold(app, to: 0.5) let released = app.otherElements["fold.paper"].value as? String sleep(2) XCTAssertEqual(app.staticTexts["step.index"].label, "1") XCTAssertEqual(app.otherElements["fold.paper"].value as? String, released) XCTAssertNotEqual(released, "0%") } } /// Drags the current step's moving faces along their path to its end, then lifts. func completeCurrentFold(_ app: XCUIApplication) { dragCurrentFold(app, to: 1) } /// Drags the current step's moving faces along their path, read from `fold.path` (the app must be /// launched with `-UITesting`), to `fraction` of it, then lifts. Each drag is one straight /// press-and-drag through every 32nd sample and every turn, with short ones merged so each moves /// at least `minimumChord` points: a DragGesture does not begin under 10. A turn is where the path /// goes back the way it came, at a fold-and-unfold's peak or at a non-rigid step's phase /// boundaries (t = ⅓ and ⅔); a drag to a turn continues 6 points past it, since the drag after it /// starts back along the way it came, and a drag to the end continues 30 points past it. The app /// reads only a drag's translation, so a drag from a point outside the paper view starts at its /// centre instead. Each holds before lifting, so its last position arrives before the lift. func dragCurrentFold(_ app: XCUIApplication, to fraction: Double) { let paper = app.otherElements["fold.paper"] let marker = app.descendants(matching: .any)["fold.path"] XCTAssertTrue(marker.waitForExistence(timeout: 5)) let samples = (marker.value as? String ?? "").split(separator: " ").map { pair in let xy = pair.split(separator: ",").map { Double($0)! } return CGVector(dx: xy[0], dy: xy[1]) } XCTAssertFalse(samples.isEmpty) let size = paper.frame.size func chord(_ a: Int, _ b: Int) -> CGVector { CGVector(dx: (samples[b].dx - samples[a].dx) * size.width, dy: (samples[b].dy - samples[a].dy) * size.height) } func length(_ v: CGVector) -> Double { (v.dx * v.dx + v.dy * v.dy).squareRoot() } func dot(_ u: CGVector, _ v: CGVector) -> Double { u.dx * v.dx + u.dy * v.dy } let last = Int((fraction * Double(samples.count - 1)).rounded()) // a turn: the path arrives along one direction and leaves against it; its sample is the one // furthest along the arrival within two of the boundary's let boundaries = stepKind(app) == "foldAndUnfold" ? [64] : [43, 85] var turns: [Int] = [] for c in boundaries where c + 2 < last { let arriving = chord(c - 8, c), leaving = chord(c, min(c + 24, last)) guard length(arriving) > 2, length(leaving) > 2, dot(arriving, leaving) < 0 else { continue } turns.append(((c - 2)...(c + 2)).max { dot(chord(0, $0), arriving) < dot(chord(0, $1), arriving) }!) } var stops = [0] let regular = stride(from: 32, to: last, by: 32).filter { i in !turns.contains { abs($0 - i) < 8 } } for i in (regular + turns + [last]).sorted() { if !turns.contains(i) && i != last && length(chord(stops.last!, i)) < minimumChord { continue } while stops.count > 1 && !turns.contains(stops.last!) && length(chord(stops.last!, i)) < minimumChord { stops.removeLast() } stops.append(i) } for (a, b) in zip(stops, stops.dropFirst()) { var move = chord(a, b) let l = max(length(move), 1), past = b == samples.count - 1 ? 30.0 : turns.contains(b) ? 6.0 : 0.0 move = CGVector(dx: move.dx + move.dx / l * past, dy: move.dy + move.dy / l * past) let inside = (0.02...0.98).contains(samples[a].dx) && (0.02...0.98).contains(samples[a].dy) let start = paper.coordinate(withNormalizedOffset: inside ? samples[a] : CGVector(dx: 0.5, dy: 0.5)) start.press(forDuration: 0.1, thenDragTo: start.withOffset(move), withVelocity: .default, thenHoldForDuration: 0.3) } } /// The shortest straight drag `dragCurrentFold` makes, in points. private let minimumChord = 12.0