App/UITests/ReorientGestureTests.swift

00ea029d0652da12d0f4237769e7020b72cfa7f3
fold/App/UITests/ReorientGestureTests.swift history · blame · raw

91 lines · 4140 bytes

10 symbols in this file
 1import XCTest
 2
 3final class ReorientGestureTests: XCTestCase {
 4    override func setUp() {
 5        continueAfterFailure = false
 6    }
 7
 8    func testDraggingAcrossTheModelCompletesATurnOverStep() {
 9        let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch()
10        app.buttons["Cup"].tap()
11        advanceToFirstTurnOverStep(app)
12        let before = app.staticTexts["step.index"].label
13        dragAcross(app.otherElements["fold.paper"])
14        XCTAssertNotEqual(before, app.staticTexts["step.index"].label)
15    }
16
17    func testFoldDragDoesNothingOnATurnOverStep() {
18        let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch()
19        app.buttons["Cup"].tap()
20        advanceToFirstTurnOverStep(app)
21        let before = app.staticTexts["step.index"].label
22        let paper = app.otherElements["fold.paper"]
23        // a downward drag longer than the fold drag's fallback throw, at most 160 points
24        drag(paper, from: CGVector(dx: 0.5, dy: 0.05), to: CGVector(dx: 0.5, dy: 0.95))
25        XCTAssertEqual(app.staticTexts["step.index"].label, before)
26        XCTAssertEqual(paper.value as? String, "0%")
27    }
28
29    func testReorientDragDoesNothingOnAFoldStep() {
30        let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch()
31        app.buttons["Cup"].tap()
32        // step 1 folds the bottom corner up; this drag is across that path
33        dragAcross(app.otherElements["fold.paper"])
34        XCTAssertEqual(app.staticTexts["step.index"].label, "1")
35    }
36
37    func testTurnOverReorientsTheModelNotTheCamera() {
38        let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch()
39        app.buttons["Cup"].tap()
40        advanceToFirstTurnOverStep(app)
41        let paper = app.otherElements["fold.paper"]
42        let diagram = app.descendants(matching: .any)["diagram"]
43        let camera = app.descendants(matching: .any)["paper.camera"]
44        XCTAssertTrue(camera.waitForExistence(timeout: 5))
45        let step = app.staticTexts["step.index"].label
46        let diagramBefore = diagram.screenshot().pngRepresentation
47        let cameraBefore = camera.value as? String
48        let paperBefore = paper.screenshot().pngRepresentation
49
50        drag(paper, from: CGVector(dx: 0.3, dy: 0.5), to: CGVector(dx: 0.5, dy: 0.5))
51        let midway = paper.value as? String
52        XCTAssertNotEqual(midway, "0%")
53        XCTAssertNotEqual(midway, "100%")
54        XCTAssertEqual(app.staticTexts["step.index"].label, step)
55        XCTAssertEqual(diagram.screenshot().pngRepresentation, diagramBefore)
56        XCTAssertEqual(camera.value as? String, cameraBefore)
57
58        dragAcross(paper)
59        XCTAssertNotEqual(app.staticTexts["step.index"].label, step)
60        XCTAssertNotEqual(paper.screenshot().pngRepresentation, paperBefore)
61    }
62}
63
64/// Completes the current model's fold steps up to its first turn-over. The app must be launched
65/// with `-UITesting`.
66func advanceToFirstTurnOverStep(_ app: XCUIApplication) {
67    while stepKind(app) != "turnOver" {
68        let step = app.staticTexts["step.index"].label
69        completeCurrentFold(app)
70        XCTAssertNotEqual(app.staticTexts["step.index"].label, step)
71    }
72}
73
74/// The current step's kind, as in `krz:step`. The app must be launched with `-UITesting`.
75func stepKind(_ app: XCUIApplication) -> String {
76    let kind = app.descendants(matching: .any)["step.kind"]
77    XCTAssertTrue(kind.waitForExistence(timeout: 5))
78    return kind.value as? String ?? ""
79}
80
81/// A rightward drag across most of the element's width, at its middle. It starts clear of the
82/// screen's left edge, where a drag would go back in the navigation stack.
83func dragAcross(_ element: XCUIElement) {
84    drag(element, from: CGVector(dx: 0.1, dy: 0.5), to: CGVector(dx: 0.9, dy: 0.5))
85}
86
87/// A straight drag between two points given as fractions of the element's frame, then a lift.
88func drag(_ element: XCUIElement, from start: CGVector, to end: CGVector) {
89    element.coordinate(withNormalizedOffset: start)
90        .press(forDuration: 0.1, thenDragTo: element.coordinate(withNormalizedOffset: end))
91}