Open History to see the last commit
1import XCTest
2@testable import ExpeditionCore
3
4final class WindowFeatureTests: XCTestCase {
5 let viewport = WorldRect(x: 0, y: 0, width: 1000, height: 800)
6 let window = DesktopWindow(id: 7, frame: .init(x: 100, y: 100, width: 600, height: 600))
7 func pixels(scroll: Int = 0, blank: Bool = false) -> TerrainPixels {
8 let w = 240, h = 240
9 var rgb = [UInt8](repeating: 235, count: w * h * 3)
10 if !blank {
11 for y in 0..<h {
12 for x in 0..<w {
13 // Sustained two-colour block, separate hairline, vertical crack.
14 let block = y >= 60 + scroll && y < 105 + scroll && x >= 20 && x < 210
15 let line = y == 155 + scroll && x >= 20 && x < 210
16 let vertical = (x == 145 || x == 146) && y >= 170 + scroll && y < 225 + scroll
17 let text = y == 20 && x % 12 < 5
18 if block || line || vertical || text {
19 let i = (y * w + x) * 3
20 rgb[i] = 30; rgb[i + 1] = 45; rgb[i + 2] = 65
21 }
22 }
23 }
24 }
25 return .init(width: w, height: h, rgb: rgb, pointWidth: 600, pointHeight: 600)
26 }
27 func feature(_ id: Int = 1, _ edge: TerrainEdge = .ledge, y: Double = 300) -> WindowFeature {
28 .init(id: id, windowID: 7, edge: edge, start: .init(x: 50, y: y), end: .init(x: 500, y: y))
29 }
30 func terrain(_ features: [WindowFeature], windows: [DesktopWindow]? = nil) -> TerrainSnapshot {
31 .init(windows: windows ?? [window], viewport: viewport, features: features)
32 }
33 func explorer(at point: WorldPoint, terrain: TerrainSnapshot) -> ExplorerInteraction {
34 var e = ExplorerInteraction(position: point, bounds: .init(minimum: .init(x: 24, y: 0), maximum: .init(x: 976, y: 724)))
35 e.autonomous = true; e.updateTerrain(terrain); e.setKeyboardControl(true)
36 return e
37 }
38 func advance(_ e: inout ExplorerInteraction, _ seconds: Double) {
39 for _ in 0..<Int(seconds * 120) { e.advance(by: 1.0 / 120) }
40 }
41 func testDetectorSeparatesRegionsFromHairlinesAndRejectsText() {
42 let features = WindowFeatureDetector.detect(pixels(), windowID: 7)
43 XCTAssertTrue(features.contains { $0.edge == .ledge && abs($0.start.y - 450) < 4 })
44 XCTAssertTrue(features.contains { $0.edge == .horizontalCrack && abs($0.start.y - 212.5) < 4 })
45 XCTAssertTrue(features.contains { $0.edge == .verticalCrack })
46 XCTAssertTrue(features.contains { $0.edge == .verticalEdge })
47 XCTAssertFalse(features.contains { $0.start.y > 530 }, "Short repeated glyph strokes cannot become ledges")
48 }
49 func testFaintSinglePixelRulesAndLowContrastColourBoundaries() {
50 let w = 300, h = 220
51 var rgb = [UInt8](repeating: 240, count: w * h * 3)
52 for y in 0..<h {
53 for x in 15..<285 {
54 let value: UInt8 = y >= 45 && y < 85 ? 218 : (y == 130 ? 220 : 240)
55 for c in 0..<3 { rgb[(y * w + x) * 3 + c] = value }
56 }
57 }
58 let image = TerrainPixels(width: w, height: h, rgb: rgb, pointWidth: 600, pointHeight: 440)
59 let features = WindowFeatureDetector.detect(image, windowID: 7)
60 XCTAssertTrue(features.contains { $0.edge == .ledge && abs($0.start.y - 350) <= 3 })
61 XCTAssertTrue(features.contains { $0.edge == .horizontalCrack && abs($0.start.y - 180) <= 3 })
62 }
63 func testTextRegionsRejectLongUnderlinesWithoutHidingNearbyDivider() {
64 let w = 300, h = 220
65 var rgb = [UInt8](repeating: 240, count: w * h * 3)
66 for y in [80, 105] {
67 for x in 20..<280 {
68 for c in 0..<3 { rgb[(y * w + x) * 3 + c] = 80 }
69 }
70 }
71 let unfiltered = TerrainPixels(width: w, height: h, rgb: rgb, pointWidth: 600, pointHeight: 440)
72 XCTAssertTrue(WindowFeatureDetector.detect(unfiltered, windowID: 7).contains { abs($0.start.y - 280) < 2 })
73 let filtered = TerrainPixels(width: w, height: h, rgb: rgb, pointWidth: 600, pointHeight: 440,
74 textRegions: [.init(x: 35, y: 275, width: 530, height: 32)])
75 let features = WindowFeatureDetector.detect(filtered, windowID: 7)
76 XCTAssertFalse(features.contains { abs($0.start.y - 280) < 2 }, "A long text underline must not become a crack")
77 XCTAssertTrue(features.contains { abs($0.start.y - 230) < 2 }, "The independent divider below the text must survive")
78 var tracker = WindowFeatureTracker()
79 tracker.update(unfiltered, windowID: 7, at: 1); tracker.confirmUnchanged(at: 1.05)
80 let oldTextID = tracker.features(at: 1.05).first { abs($0.start.y - 280) < 2 }!.id
81 tracker.update(filtered, windowID: 7, at: 1.1)
82 XCTAssertFalse(tracker.features(at: 1.1).contains { $0.id == oldTextID }, "Recognised text must not retain an old hold through missed-frame grace")
83 }
84 func testGentleGradientDoesNotBecomeManyLedges() {
85 let w = 120, h = 120
86 var rgb = [UInt8](repeating: 0, count: w * h * 3)
87 for y in 0..<h {
88 for x in 0..<w {
89 for c in 0..<3 { rgb[(y * w + x) * 3 + c] = UInt8(100 + y / 2) }
90 }
91 }
92 XCTAssertTrue(WindowFeatureDetector.detect(.init(width: w, height: h, rgb: rgb, pointWidth: 360, pointHeight: 360), windowID: 7).isEmpty)
93 }
94 func testTrackerKeepsIdentityAcrossFastScrollAndExpires() {
95 var tracker = WindowFeatureTracker()
96 tracker.update(pixels(), windowID: 7, at: 1)
97 XCTAssertTrue(tracker.features(at: 1).isEmpty)
98 tracker.confirmUnchanged(at: 1.05)
99 let old = tracker.features(at: 1.05).first { $0.edge == .horizontalCrack }!
100 tracker.update(pixels(scroll: -24), windowID: 7, at: 1.1)
101 let moved = tracker.features(at: 1.1).first { $0.id == old.id }
102 XCTAssertNotNil(moved)
103 XCTAssertEqual(moved!.start.y - old.start.y, 60, accuracy: 3)
104 XCTAssertTrue(tracker.features(at: 1.5).isEmpty)
105 tracker.update(pixels(blank: true), windowID: 7, at: 1.15)
106 XCTAssertFalse(tracker.features(at: 1.15).contains { $0.id == old.id })
107 }
108 func testInternalLedgeSweptLandingTracksScrollAndWindowMove() {
109 let f = feature()
110 let t = terrain([f])
111 let landing = TerrainSupport.landing(from: .init(x: 300, y: 450), to: .init(x: 300, y: 350), in: t)!
112 XCTAssertEqual(landing.0.edge, .ledge)
113 var e = explorer(at: landing.1, terrain: t)
114 var shifted = f; shifted.start.y += 35; shifted.end.y += 35
115 e.updateTerrain(terrain([shifted]), sampleElapsed: 0.05)
116 XCTAssertEqual(e.position.y, 435)
117 let moved = DesktopWindow(id: 7, frame: .init(x: 140, y: 130, width: 600, height: 600))
118 e.updateTerrain(terrain([shifted], windows: [moved]), sampleElapsed: 0.05)
119 XCTAssertEqual(e.position, .init(x: 340, y: 465))
120 e.updateTerrain(terrain([]))
121 XCTAssertNil(e.attachment)
122 XCTAssertEqual(e.state, .recovering)
123 }
124 func testCrackCanBeGrabbedTraversedAndFollowsScroll() {
125 let f = feature(2, .horizontalCrack)
126 let t = terrain([f])
127 var e = explorer(at: .init(x: 300, y: 342), terrain: t)
128 XCTAssertTrue(e.transferToWindowHold())
129 advance(&e, 0.25)
130 XCTAssertEqual(e.attachment?.featureID, 2)
131 e.setControlDirection(horizontal: 1, vertical: 0)
132 advance(&e, 1)
133 XCTAssertEqual(e.position.x, 330, accuracy: 0.5)
134 XCTAssertEqual(e.position.y, 342)
135 XCTAssertNotNil(e.internalClimbingPose)
136 var scrolled = f; scrolled.start.y += 40; scrolled.end.y += 40
137 e.updateTerrain(terrain([scrolled]), sampleElapsed: 0.05)
138 XCTAssertEqual(e.position.y, 382)
139 e.setControlDirection(horizontal: 0, vertical: -1)
140 advance(&e, 0.1)
141 XCTAssertEqual(e.state, .recovering)
142 XCTAssertNil(e.attachment)
143 }
144 func testDownFromMiddleOfWideInternalLedgeDoesNotRecatchIt() {
145 let t = terrain([feature()])
146 var e = explorer(at: .init(x: 350, y: 400), terrain: t)
147 XCTAssertEqual(e.attachment?.edge, .ledge)
148 e.setControlDirection(horizontal: 0, vertical: -1)
149 advance(&e, 1.3)
150 XCTAssertEqual(e.state, .recovering)
151 XCTAssertLessThan(e.position.y, 392)
152 XCTAssertNil(e.attachment)
153 }
154 func testVerticalRouteClimbsBothWaysAndConnectsToCrack() {
155 let vertical = WindowFeature(id: 3, windowID: 7, edge: .verticalCrack, start: .init(x: 200, y: 60), end: .init(x: 200, y: 450))
156 let t = terrain([vertical, feature(4, .horizontalCrack, y: 300), feature(5, .ledge, y: 450)])
157 var e = explorer(at: .init(x: 288, y: 250), terrain: t)
158 XCTAssertTrue(e.transferToWindowHold())
159 advance(&e, 0.25)
160 e.setControlDirection(horizontal: 0, vertical: 1); advance(&e, 1)
161 let high = e.position.y
162 XCTAssertGreaterThan(high, 250)
163 e.setControlDirection(horizontal: 0, vertical: -1); advance(&e, 1)
164 XCTAssertLessThan(e.position.y, high)
165 let graph = NavigationGraph(terrain: t)
166 XCTAssertTrue(graph.steps.contains { $0.kind == .transferring && $0.from.featureID == 3 && $0.to.featureID == 4 })
167 XCTAssertTrue(graph.steps.filter { $0.from.featureID == 3 && $0.to.featureID == 3 }.allSatisfy { $0.kind == .climbing })
168 e.setControlDirection(horizontal: 0, vertical: 1); advance(&e, 20)
169 XCTAssertEqual(e.attachment?.edge, .ledge, "Up at the end of a vertical route pulls onto the adjoining ledge")
170 XCTAssertEqual(e.position.y, 550)
171
172 }
173 func testForegroundWindowOccludesInternalHoldsAndBody() {
174 let front = DesktopWindow(id: 8, frame: .init(x: 250, y: 300, width: 120, height: 240))
175 let t = terrain([feature()], windows: [front, window])
176 XCTAssertNil(TerrainSupport.grip(at: .init(x: 300, y: 400), in: t))
177 XCTAssertNotNil(TerrainSupport.grip(at: .init(x: 450, y: 400), in: t))
178 }
179 func testRopeCanTransferToMovingCrackWithoutReturningToRoof() {
180 let crack = feature(6, .horizontalCrack, y: 300)
181 let t = terrain([crack])
182 var e = explorer(at: .init(x: 350, y: 0), terrain: t)
183 XCTAssertTrue(e.fireGrapple(toward: .init(x: 350, y: 700)))
184 advance(&e, 2)
185 e.setControlDirection(horizontal: 0, vertical: 1)
186 for _ in 0..<2400 {
187 e.advance(by: 1.0 / 120)
188 if e.attachment?.featureID == 6 && e.state != .transferringHold { break }
189 }
190 XCTAssertEqual(e.attachment?.featureID, 6)
191 XCTAssertNil(e.ropeSession)
192 XCTAssertEqual(e.position.y, 342, accuracy: 24)
193 }
194}