import XCTest final class ReorientGestureTests: XCTestCase { override func setUp() { continueAfterFailure = false } func testDraggingAcrossTheModelCompletesATurnOverStep() { let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() app.buttons["Cup"].tap() advanceToFirstTurnOverStep(app) let before = app.staticTexts["step.index"].label dragAcross(app.otherElements["fold.paper"]) XCTAssertNotEqual(before, app.staticTexts["step.index"].label) } func testFoldDragDoesNothingOnATurnOverStep() { let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() app.buttons["Cup"].tap() advanceToFirstTurnOverStep(app) let before = app.staticTexts["step.index"].label let paper = app.otherElements["fold.paper"] // a downward drag longer than the fold drag's fallback throw, at most 160 points drag(paper, from: CGVector(dx: 0.5, dy: 0.05), to: CGVector(dx: 0.5, dy: 0.95)) XCTAssertEqual(app.staticTexts["step.index"].label, before) XCTAssertEqual(paper.value as? String, "0%") } func testReorientDragDoesNothingOnAFoldStep() { let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() app.buttons["Cup"].tap() // step 1 folds the bottom corner up; this drag is across that path dragAcross(app.otherElements["fold.paper"]) XCTAssertEqual(app.staticTexts["step.index"].label, "1") } func testTurnOverReorientsTheModelNotTheCamera() { let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() app.buttons["Cup"].tap() advanceToFirstTurnOverStep(app) let paper = app.otherElements["fold.paper"] let diagram = app.descendants(matching: .any)["diagram"] let camera = app.descendants(matching: .any)["paper.camera"] XCTAssertTrue(camera.waitForExistence(timeout: 5)) let step = app.staticTexts["step.index"].label let diagramBefore = diagram.screenshot().pngRepresentation let cameraBefore = camera.value as? String let paperBefore = paper.screenshot().pngRepresentation drag(paper, from: CGVector(dx: 0.3, dy: 0.5), to: CGVector(dx: 0.5, dy: 0.5)) let midway = paper.value as? String XCTAssertNotEqual(midway, "0%") XCTAssertNotEqual(midway, "100%") XCTAssertEqual(app.staticTexts["step.index"].label, step) XCTAssertEqual(diagram.screenshot().pngRepresentation, diagramBefore) XCTAssertEqual(camera.value as? String, cameraBefore) dragAcross(paper) XCTAssertNotEqual(app.staticTexts["step.index"].label, step) XCTAssertNotEqual(paper.screenshot().pngRepresentation, paperBefore) } } /// Completes the current model's fold steps up to its first turn-over. The app must be launched /// with `-UITesting`. func advanceToFirstTurnOverStep(_ app: XCUIApplication) { while stepKind(app) != "turnOver" { let step = app.staticTexts["step.index"].label completeCurrentFold(app) XCTAssertNotEqual(app.staticTexts["step.index"].label, step) } } /// The current step's kind, as in `krz:step`. The app must be launched with `-UITesting`. func stepKind(_ app: XCUIApplication) -> String { let kind = app.descendants(matching: .any)["step.kind"] XCTAssertTrue(kind.waitForExistence(timeout: 5)) return kind.value as? String ?? "" } /// A rightward drag across most of the element's width, at its middle. It starts clear of the /// screen's left edge, where a drag would go back in the navigation stack. func dragAcross(_ element: XCUIElement) { drag(element, from: CGVector(dx: 0.1, dy: 0.5), to: CGVector(dx: 0.9, dy: 0.5)) } /// A straight drag between two points given as fractions of the element's frame, then a lift. func drag(_ element: XCUIElement, from start: CGVector, to end: CGVector) { element.coordinate(withNormalizedOffset: start) .press(forDuration: 0.1, thenDragTo: element.coordinate(withNormalizedOffset: end)) }