Open History to see the last commit
1import XCTest
2@testable import ExpeditionCore
3
4final class ExplorerInteractionTests: XCTestCase {
5 private typealias Point = ExplorerInteraction.Point
6
7 private func airborne() -> ExplorerInteraction {
8 ExplorerInteraction(position: .init(x: 200, y: 800), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 1124)))
9 }
10
11 private var emptySky: TerrainSnapshot { .init(windows: [], viewport: .init(x: 0, y: 0, width: 1000, height: 1200)) }
12
13 func testSidewaysReleaseKeepsMomentumThenAirResistanceReducesIt() {
14 var subject = airborne()
15 subject.beginDrag(at: subject.position, time: 0)
16 subject.drag(to: .init(x: 250, y: 800), time: 0.05)
17 subject.endDrag(on: emptySky)
18 let released = subject.position.x
19 subject.advance(by: 0.1)
20 let early = subject.position.x - released
21 XCTAssertGreaterThan(early, 0)
22 subject.advance(by: 0.9)
23 let before = subject.position.x
24 subject.advance(by: 0.1)
25 XCTAssertGreaterThan(subject.position.x, before)
26 XCTAssertLessThan(subject.position.x - before, early)
27 }
28
29 func testUpwardThrowRisesBeforeFallingBackDown() {
30 var subject = airborne()
31 subject.beginDrag(at: subject.position, time: 0)
32 subject.drag(to: .init(x: 200, y: 850), time: 0.05)
33 subject.endDrag(on: emptySky)
34 subject.advance(by: 0.1)
35 XCTAssertGreaterThan(subject.position.y, 850)
36 subject.advance(by: 2)
37 XCTAssertLessThan(subject.position.y, 850)
38 }
39
40 func testHoldingStillBeforeReleaseDoesNotReuseOldThrowVelocity() {
41 var subject = airborne()
42 subject.beginDrag(at: subject.position, time: 0)
43 subject.drag(to: .init(x: 250, y: 800), time: 0.05)
44 subject.drag(to: subject.position, time: 0.4)
45 subject.endDrag(on: emptySky)
46 subject.advance(by: 0.1)
47 XCTAssertEqual(subject.position.x, 250)
48 }
49
50 func testGrabbingAgainCancelsThePreviousFlightMomentum() {
51 var subject = airborne()
52 subject.beginDrag(at: subject.position, time: 0)
53 subject.drag(to: .init(x: 250, y: 800), time: 0.05)
54 subject.endDrag(on: emptySky)
55 subject.advance(by: 0.1)
56 subject.beginDrag(at: subject.position, time: 1)
57 let x = subject.position.x
58 subject.drag(to: subject.position, time: 1.3)
59 subject.endDrag(on: emptySky)
60 subject.advance(by: 0.1)
61 XCTAssertEqual(subject.position.x, x)
62 }
63
64 private func makeInteraction() -> ExplorerInteraction {
65 ExplorerInteraction(position: Point(x: 100, y: 100), bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 776, y: 524)))
66 }
67
68 func testGrabOffsetIsPreservedInsteadOfSnappingToPointer() {
69 var subject = makeInteraction()
70 subject.beginDrag(at: Point(x: 108, y: 148))
71 subject.drag(to: Point(x: 308, y: 348))
72 XCTAssertEqual(subject.position, Point(x: 300, y: 300))
73 XCTAssertEqual(subject.state, .held)
74 }
75
76 func testPointerCanLeaveCharacterAndDisplayWhileDragStaysCaptured() {
77 var subject = makeInteraction()
78 subject.beginDrag(at: Point(x: 100, y: 100))
79 subject.drag(to: Point(x: -500, y: 4000))
80 XCTAssertEqual(subject.position, Point(x: 24, y: 524))
81 subject.drag(to: Point(x: 300, y: 200))
82 XCTAssertEqual(subject.position, Point(x: 300, y: 200))
83 subject.endDrag()
84 subject.drag(to: .zero)
85 XCTAssertEqual(subject.position, Point(x: 300, y: 200))
86 }
87
88 func testReleaseSettlesBeforeReturningToIdleAndDuplicateReleaseDoesNotRestartIt() {
89 var subject = makeInteraction()
90 subject.beginDrag(at: subject.position)
91 subject.endDrag()
92 subject.advance(by: 0.5)
93 XCTAssertEqual(subject.state, .settling)
94 subject.endDrag()
95 subject.advance(by: 0.31)
96 XCTAssertEqual(subject.state, .idle)
97 }
98
99 func testNewGrabInterruptsSettling() {
100 var subject = makeInteraction()
101 subject.beginDrag(at: subject.position)
102 subject.endDrag()
103 subject.beginDrag(at: Point(x: 105, y: 120))
104 subject.advance(by: 10)
105 subject.drag(to: Point(x: 205, y: 220))
106 XCTAssertEqual(subject.state, .held)
107 XCTAssertEqual(subject.position, Point(x: 200, y: 200))
108 }
109
110 func testDisplayChangeCancelsDragAndClampsPosition() {
111 var subject = makeInteraction()
112 subject.beginDrag(at: subject.position)
113 subject.updateBounds(.init(minimum: .init(x: 10, y: 10), maximum: .init(x: 60, y: 60)))
114 XCTAssertEqual(subject.state, .settling)
115 XCTAssertEqual(subject.position, Point(x: 60, y: 60))
116 subject.drag(to: Point(x: 500, y: 500))
117 XCTAssertEqual(subject.position, Point(x: 60, y: 60))
118 }
119
120 func testInvalidTimeCannotEndSettling() {
121 var subject = makeInteraction()
122 subject.beginDrag(at: subject.position)
123 subject.endDrag()
124 subject.advance(by: -.infinity)
125 subject.advance(by: .nan)
126 subject.advance(by: -1)
127 XCTAssertEqual(subject.state, .settling)
128 }
129}