App/Tests/FoldSessionTests.swift
370 lines · 12591 bytes
35 symbols in this file
cupincrementingMovesToTheNextTenthdecrementingMovesToTheTenthBelowAndStopsAtZeroincrementingToTheEndCompletesTheStepincrementingOnTheLastStepStaysAtTheEndzoomIsClampedAndOrbitAndZoomLeaveTheFoldAlonecompletingAStepResetsTheCameraToTheNextViewpointtranslationcupInViewanOrbitDuringADragRestoresTAndTheDragDoesNotCompleteaDragThatStartsDuringAnOrbitIsSuspendedaDragWithoutAnOrbitCompletesAtTheEndtheReleasePositionIsAppliedBeforeTheLiftaReleaseShortOfTheEndLeavesTWhereTheFingerLiftedaTurnOverReleasedPastTheModelsWidthCompletesanOrbitAfterACancelledDragLeavesTAloneopeningAStepStartsItFromTheFrameBeforeItopeningAnEarlierStepGoesBackToItsStartopeningTheCurrentStepLeavesItAloneaDragBackTakesTheFoldBackToItsStartopeningAStepFromTheStartOfAnotherShowsTheNewStepopeningAStepDuringADragLeavesTheNewStepAloneanOrbitAfterOpeningAStepDuringADragLeavesTAlonefinishedCupfinishingTheLastStepFinishesAndLocksTheModeladjustingToTheEndOfTheLastStepFinishesTheModeldragsAndAdjustingAreIgnoredWhileLockedunlockingThenDraggingBackUnfoldsAndLeavesTheFinishedStatefoldingTheLastStepAgainFinishesAgainliftingAtTheEndWhileFinishedAndUnlockedDoesNotFinishAgainopeningAnotherStepLeavesTheFinishedStatetheLockDoesNothingBeforeTheModelIsFinishedorbitAndZoomStillChangeTheCameraWhileLockedanOrbitDuringADragBackLeavesTheModelFinishedlockingDuringADragBackReturnsTheModelToTheEnd
1import FoldFormat
2import Models
3import Testing
4import simd
5@testable import Fold
6
7@MainActor
8private func cup(step: Int = 1) throws -> FoldSession {
9 let model = try #require(try ModelLibrary.all().first { $0.fileTitle == "Cup" })
10 return try FoldSession(model: model, step: step)
11}
12
13@MainActor @Test func incrementingMovesToTheNextTenth() throws {
14 let session = try cup()
15 session.adjust(1)
16 #expect(session.t == 0.1)
17 session.adjust(1); session.adjust(1)
18 #expect(session.t == 0.3)
19 session.t = 0.37
20 session.adjust(1)
21 #expect(session.t == 0.4)
22}
23
24@MainActor @Test func decrementingMovesToTheTenthBelowAndStopsAtZero() throws {
25 let session = try cup()
26 session.adjust(-1)
27 #expect(session.t == 0)
28 session.t = 0.37
29 session.adjust(-1)
30 #expect(session.t == 0.3)
31}
32
33@MainActor @Test func incrementingToTheEndCompletesTheStep() throws {
34 let session = try cup()
35 for _ in 0..<9 { session.adjust(1) }
36 #expect(session.step.index == 1 && session.t == 0.9)
37 session.adjust(1)
38 #expect(session.step.index == 2)
39 #expect(session.t == 0)
40}
41
42@MainActor @Test func incrementingOnTheLastStepStaysAtTheEnd() throws {
43 let session = try cup(step: 6)
44 for _ in 0..<12 { session.adjust(1) }
45 #expect(session.step.index == 6)
46 #expect(session.t == 1)
47}
48
49@MainActor @Test func zoomIsClampedAndOrbitAndZoomLeaveTheFoldAlone() throws {
50 let session = try cup()
51 session.t = 0.5
52 session.zoom(by: 100)
53 #expect(session.fieldOfView == FoldSession.fieldOfViewRange.lowerBound)
54 session.zoom(by: 0.001)
55 #expect(session.fieldOfView == FoldSession.fieldOfViewRange.upperBound)
56 session.orbit(by: SIMD2(0.4, -0.7))
57 #expect(session.t == 0.5)
58 #expect(session.step.index == 1)
59}
60
61@MainActor @Test func completingAStepResetsTheCameraToTheNextViewpoint() throws {
62 let session = try cup()
63 let start = session.camera
64 session.orbit(by: SIMD2(0.4, -0.7))
65 session.zoom(by: 2)
66 session.t = 1
67 session.completeStep()
68 #expect(session.step.index == 2)
69 #expect(session.eyeDirection == SIMD3(0, 0, 1))
70 #expect(session.camera.fieldOfView == start.fieldOfView)
71}
72
73/// The translation that takes a fold drag begun at `start` to `t` along the session's path, and
74/// 30 points past its end when `t` is 1.
75@MainActor
76private func translation(_ session: FoldSession, from start: Double = 0, to t: Double) -> SIMD2<Double> {
77 let m = session.mapping
78 let past = t == 1 ? simd_normalize(m.point(at: 1) - m.point(at: 127.0 / 128)) * 30 : .zero
79 return m.point(at: t) - m.point(at: start) + past
80}
81
82@MainActor
83private func cupInView(step: Int = 1) throws -> FoldSession {
84 let session = try cup(step: step)
85 session.viewportSize = SIMD2(400, 400)
86 return session
87}
88
89@MainActor @Test func anOrbitDuringADragRestoresTAndTheDragDoesNotComplete() throws {
90 let session = try cupInView()
91 session.t = 0.2
92 session.beginFoldDrag()
93 session.drag(translation: translation(session, from: 0.2, to: 1))
94 #expect(session.t == 1)
95 session.beginOrbit()
96 #expect(session.t == 0.2)
97 #expect(session.isDragSuspended)
98 session.endOrbit()
99 #expect(session.isDragSuspended)
100 session.endDrag(translation: translation(session, from: 0.2, to: 1))
101 #expect(session.step.index == 1)
102 #expect(session.t == 0.2)
103 #expect(!session.isDragSuspended)
104}
105
106@MainActor @Test func aDragThatStartsDuringAnOrbitIsSuspended() throws {
107 let session = try cupInView()
108 session.beginOrbit()
109 session.beginFoldDrag()
110 #expect(session.isDragSuspended)
111 session.endOrbit()
112 session.endDrag(translation: translation(session, to: 1))
113 #expect(session.step.index == 1)
114 #expect(session.t == 0)
115}
116
117@MainActor @Test func aDragWithoutAnOrbitCompletesAtTheEnd() throws {
118 let session = try cupInView()
119 session.beginFoldDrag()
120 session.drag(translation: translation(session, to: 1))
121 session.endDrag(translation: translation(session, to: 1))
122 #expect(session.step.index == 2)
123}
124
125@MainActor @Test func theReleasePositionIsAppliedBeforeTheLift() throws {
126 // the last movement can arrive only with the lift
127 let session = try cupInView()
128 session.beginFoldDrag()
129 session.drag(translation: translation(session, to: 0.5))
130 #expect(abs(session.t - 0.5) < 0.01)
131 session.endDrag(translation: translation(session, to: 1))
132 #expect(session.step.index == 2)
133 #expect(session.t == 0)
134}
135
136@MainActor @Test func aReleaseShortOfTheEndLeavesTWhereTheFingerLifted() throws {
137 let session = try cupInView()
138 session.beginFoldDrag()
139 session.endDrag(translation: translation(session, to: 0.5))
140 #expect(session.step.index == 1)
141 #expect(abs(session.t - 0.5) < 0.01)
142}
143
144@MainActor @Test func aTurnOverReleasedPastTheModelsWidthCompletes() throws {
145 let session = try cupInView(step: 5)
146 #expect(session.step.kind == .turnOver)
147 session.beginReorientDrag()
148 session.endDrag(translation: SIMD2(1000, 0))
149 #expect(session.step.index == 6)
150}
151
152@MainActor @Test func anOrbitAfterACancelledDragLeavesTAlone() throws {
153 let session = try cupInView()
154 session.t = 0.2
155 session.beginFoldDrag()
156 session.drag(translation: translation(session, from: 0.2, to: 0.6))
157 session.cancelDrag()
158 session.beginOrbit()
159 #expect(abs(session.t - 0.6) < 0.01)
160 #expect(!session.isDragSuspended)
161 session.endOrbit()
162}
163
164@MainActor @Test func openingAStepStartsItFromTheFrameBeforeIt() throws {
165 let session = try cup()
166 session.orbit(by: SIMD2(0.4, -0.7))
167 session.zoom(by: 2)
168 session.t = 0.6
169 session.open(step: 4)
170 let fresh = try cup(step: 4)
171 #expect(session.step.index == 4)
172 #expect(session.t == 0)
173 #expect(session.camera.eye == fresh.camera.eye)
174 #expect(session.camera.up == fresh.camera.up)
175 #expect(session.camera.fieldOfView == fresh.camera.fieldOfView)
176 #expect(session.diagram == fresh.diagram)
177 #expect(session.meshPositions == fresh.meshPositions)
178}
179
180@MainActor @Test func openingAnEarlierStepGoesBackToItsStart() throws {
181 let session = try cup(step: 4)
182 session.t = 1
183 session.completeStep()
184 session.open(step: 2)
185 #expect(session.step.index == 2)
186 #expect(session.t == 0)
187 #expect(session.meshPositions == (try cup(step: 2)).meshPositions)
188}
189
190@MainActor @Test func openingTheCurrentStepLeavesItAlone() throws {
191 let session = try cup(step: 3)
192 session.orbit(by: SIMD2(0.4, -0.7))
193 session.t = 0.4
194 let camera = session.camera
195 session.open(step: 3)
196 #expect(session.step.index == 3)
197 #expect(session.t == 0.4)
198 #expect(session.camera.eye == camera.eye)
199}
200
201@MainActor @Test func aDragBackTakesTheFoldBackToItsStart() throws {
202 let session = try cupInView()
203 session.beginFoldDrag()
204 session.drag(translation: translation(session, to: 0.7))
205 session.drag(translation: translation(session, to: 0.3))
206 #expect(abs(session.t - 0.3) < 0.01)
207 session.endDrag(translation: .zero)
208 #expect(session.step.index == 1)
209 #expect(session.t == 0)
210}
211
212@MainActor @Test func openingAStepFromTheStartOfAnotherShowsTheNewStep() throws {
213 let session = try cup()
214 session.open(step: 4)
215 #expect(session.meshPositions == (try cup(step: 4)).meshPositions)
216}
217
218@MainActor @Test func openingAStepDuringADragLeavesTheNewStepAlone() throws {
219 let session = try cupInView()
220 session.beginFoldDrag()
221 let end = translation(session, to: 1)
222 session.drag(translation: translation(session, to: 0.5))
223 session.open(step: 4)
224 session.drag(translation: end)
225 session.endDrag(translation: end)
226 #expect(session.step.index == 4)
227 #expect(session.t == 0)
228}
229
230@MainActor @Test func anOrbitAfterOpeningAStepDuringADragLeavesTAlone() throws {
231 let session = try cupInView()
232 session.t = 0.2
233 session.beginFoldDrag()
234 session.drag(translation: translation(session, from: 0.2, to: 0.6))
235 session.open(step: 4)
236 session.beginOrbit()
237 #expect(session.t == 0)
238 #expect(!session.isDragSuspended)
239 session.endOrbit()
240}
241
242/// The cup at its last step, finished by a drag to the end and a lift.
243@MainActor
244private func finishedCup() throws -> FoldSession {
245 let session = try cupInView(step: 6)
246 session.beginFoldDrag()
247 session.endDrag(translation: translation(session, to: 1))
248 return session
249}
250
251@MainActor @Test func finishingTheLastStepFinishesAndLocksTheModel() throws {
252 let session = try finishedCup()
253 #expect(session.step.index == 6)
254 #expect(session.t == 1)
255 #expect(session.isFinished)
256 #expect(session.isLocked)
257 #expect(session.finishCount == 1)
258}
259
260@MainActor @Test func adjustingToTheEndOfTheLastStepFinishesTheModel() throws {
261 let session = try cup(step: 6)
262 for _ in 0..<9 { session.adjust(1) }
263 #expect(!session.isFinished)
264 session.adjust(1)
265 #expect(session.isFinished && session.isLocked)
266}
267
268@MainActor @Test func dragsAndAdjustingAreIgnoredWhileLocked() throws {
269 let session = try finishedCup()
270 session.beginFoldDrag()
271 session.drag(translation: translation(session, from: 1, to: 0.5))
272 #expect(session.t == 1)
273 session.endDrag(translation: translation(session, from: 1, to: 0.5))
274 session.beginReorientDrag()
275 session.endDrag(translation: SIMD2(-1000, 0))
276 session.adjust(-1)
277 #expect(session.t == 1)
278 #expect(session.isLocked)
279 #expect(session.finishCount == 1)
280}
281
282@MainActor @Test func unlockingThenDraggingBackUnfoldsAndLeavesTheFinishedState() throws {
283 let session = try finishedCup()
284 session.toggleLock()
285 #expect(!session.isLocked)
286 #expect(session.isFinished)
287 session.beginFoldDrag()
288 session.drag(translation: translation(session, from: 1, to: 0.5))
289 #expect(abs(session.t - 0.5) < 0.01)
290 session.endDrag(translation: translation(session, from: 1, to: 0.5))
291 #expect(abs(session.t - 0.5) < 0.01)
292 #expect(!session.isFinished)
293 #expect(!session.isLocked)
294}
295
296@MainActor @Test func foldingTheLastStepAgainFinishesAgain() throws {
297 let session = try finishedCup()
298 session.toggleLock()
299 session.adjust(-1)
300 #expect(session.t == 0.9)
301 #expect(!session.isFinished)
302 session.beginFoldDrag()
303 session.endDrag(translation: translation(session, from: 0.9, to: 1))
304 #expect(session.isFinished && session.isLocked)
305 #expect(session.finishCount == 2)
306}
307
308@MainActor @Test func liftingAtTheEndWhileFinishedAndUnlockedDoesNotFinishAgain() throws {
309 let session = try finishedCup()
310 session.toggleLock()
311 session.beginFoldDrag()
312 session.endDrag(translation: translation(session, from: 1, to: 1))
313 #expect(session.isFinished && !session.isLocked)
314 #expect(session.finishCount == 1)
315}
316
317@MainActor @Test func openingAnotherStepLeavesTheFinishedState() throws {
318 let session = try finishedCup()
319 session.open(step: 2)
320 #expect(session.step.index == 2)
321 #expect(session.t == 0)
322 #expect(!session.isFinished && !session.isLocked)
323 session.toggleLock()
324 #expect(!session.isLocked)
325}
326
327@MainActor @Test func theLockDoesNothingBeforeTheModelIsFinished() throws {
328 let session = try cup(step: 6)
329 session.t = 1
330 session.toggleLock()
331 #expect(!session.isLocked && !session.isFinished)
332}
333
334@MainActor @Test func orbitAndZoomStillChangeTheCameraWhileLocked() throws {
335 let session = try finishedCup()
336 let camera = session.camera
337 session.beginOrbit()
338 session.orbit(by: SIMD2(0.4, -0.7))
339 session.zoom(by: 2)
340 session.endOrbit()
341 #expect(session.camera.eye != camera.eye)
342 #expect(session.camera.fieldOfView != camera.fieldOfView)
343 #expect(session.t == 1 && session.isLocked)
344}
345
346@MainActor @Test func anOrbitDuringADragBackLeavesTheModelFinished() throws {
347 let session = try finishedCup()
348 session.toggleLock()
349 session.beginFoldDrag()
350 session.drag(translation: translation(session, from: 1, to: 0.7))
351 session.beginOrbit()
352 #expect(session.t == 1)
353 session.endOrbit()
354 session.endDrag(translation: translation(session, from: 1, to: 0.7))
355 #expect(session.t == 1)
356 #expect(session.isFinished && !session.isLocked)
357 #expect(session.finishCount == 1)
358}
359
360@MainActor @Test func lockingDuringADragBackReturnsTheModelToTheEnd() throws {
361 let session = try finishedCup()
362 session.toggleLock()
363 session.beginFoldDrag()
364 session.drag(translation: translation(session, from: 1, to: 0.7))
365 session.toggleLock()
366 #expect(session.t == 1)
367 #expect(session.isFinished && session.isLocked)
368 session.endDrag(translation: translation(session, from: 1, to: 0.7))
369 #expect(session.t == 1 && session.isLocked)
370}