TestCase subclass: #LBZBezierTest instanceVariableNames: 'app model' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Bezier-Tests'! !LBZBezierTest methodsFor: 'tests'! setUp super setUp. app := LBZApp new. model := app model ! ! !LBZBezierTest methodsFor: 'tests'! tearDown app pause. app window ifNotNil: [app window delete]. super tearDown ! ! !LBZBezierTest methodsFor: 'tests'! click: label | button | button := app buttonNamed: label. button handleMouseDown: (MouseButtonEvent new setType: #mouseDown position: button center which: 4 buttons: 4 hand: World activeHand stamp: 1). button handleMouseUp: (MouseButtonEvent new setType: #mouseUp position: button center which: 4 buttons: 0 hand: World activeHand stamp: 2) ! ! !LBZBezierTest methodsFor: 'tests'! testDefaultArch self assert: model points = {60@280. 140@30. 460@30. 540@280}. self assert: model parameter = (1/2). self assert: model samples = 48 ! ! !LBZBezierTest methodsFor: 'tests'! testInterpolationExactEndpoints self assert: (model interpolate: 10@20 to: 90@60 at: 0) = (10@20). self assert: (model interpolate: 10@20 to: 90@60 at: 1) = (90@60) ! ! !LBZBezierTest methodsFor: 'tests'! testInterpolationMidpointAndQuarter self assert: (model interpolate: 10@20 to: 90@60 at: 1/2) = (50@40). self assert: (model interpolate: 10@20 to: 90@60 at: 1/4) = (30@30) ! ! !LBZBezierTest methodsFor: 'tests'! testQuadraticKnownPoint model setPoints: {0@0. 100@200. 200@0}. self assert: (model pointAt: 1/2) = (100@100). self assert: (model quadraticAt: 1/4) = ((50)@(75)) ! ! !LBZBezierTest methodsFor: 'tests'! testCubicKnownPoints self assert: (model pointAt: 0) = (60@280). self assert: (model pointAt: 1) = (540@280). self assert: (model pointAt: 1/2) = (300@(185/2)) ! ! !LBZBezierTest methodsFor: 'tests'! testLevelsSizes self assert: ((model levelsAt: 1/2) collect: [:row | row size]) = #(4 3 2 1) ! ! !LBZBezierTest methodsFor: 'tests'! testDegreeTwoAndThreeAndFour 2 to: 4 do: [:size | model setPoints: ({0@0. 100@100. 200@100. 300@0} first: size). self assert: (model levelsAt: 1/3) size = size. self assert: (model pointAt: 0) = model points first. self assert: (model pointAt: 1) = model points last] ! ! !LBZBezierTest methodsFor: 'tests'! testCubicMatchesBernstein | p expected t | p := model points. 0 to: 100 do: [:i | t := i/100. expected := (p first * ((1-t) raisedToInteger: 3)) + (p second * (3 * t * ((1-t) squared))) + (p third * (3 * t squared * (1-t))) + (p fourth * (t raisedToInteger: 3)). self assert: (model pointAt: t) = expected] ! ! !LBZBezierTest methodsFor: 'tests'! testConvexCoordinateBounds | p minX maxX minY maxY q | p := model points. minX := (p collect: [:v | v x]) min. maxX := (p collect: [:v | v x]) max. minY := (p collect: [:v | v y]) min. maxY := (p collect: [:v | v y]) max. 0 to: 100 do: [:i | q := model pointAt: i/100. self assert: (q x between: minX and: maxX). self assert: (q y between: minY and: maxY)] ! ! !LBZBezierTest methodsFor: 'tests'! testCoincidentPoints model setPoints: {123@45. 123@45. 123@45. 123@45}. 0 to: 100 do: [:i | self assert: (model pointAt: i/100) = (123@45)] ! ! !LBZBezierTest methodsFor: 'tests'! testCollinearControlPoints model preset: #line. 0 to: 100 do: [:i | self assert: (model pointAt: i/100) = (model interpolate: 60@280 to: 540@40 at: i/100)] ! ! !LBZBezierTest methodsFor: 'tests'! testClosedLoopEnds model preset: #loop. self assert: (model pointAt: 0) = (model pointAt: 1). self deny: (model pointAt: 1/2) = (model pointAt: 0) ! ! !LBZBezierTest methodsFor: 'tests'! testPointBoundsInclusive self assert: (model setPoints: {0@0. 600@360}). self deny: (model point: 1 put: -1@0). self deny: (model point: 2 put: 600@361) ! ! !LBZBezierTest methodsFor: 'tests'! testInvalidPointsAtomic | before | before := model points. {nil. #(). {0@0}. {0@0. 0@0. 0@0. 0@0. 0@0}. {0@0. 601@0}. {0@0. 1@ -1}. {0@0. nil}. {0@0. 1}. {0@0. Float infinity@0}} do: [:bad | self deny: (model setPoints: bad). self assert: model points = before] ! ! !LBZBezierTest methodsFor: 'tests'! testCopyOnInputAndOutput | values | values := {10@20. 50@60}. model setPoints: values. values at: 1 put: 99@99. model points at: 2 put: 88@88. self assert: model points = {10@20. 50@60} ! ! !LBZBezierTest methodsFor: 'tests'! testConstructionQueryIsolation | levels before | before := model points. levels := model levelsAt: 1/2. levels first at: 1 put: 0@0. self assert: model points = before. self assert: model parameter = (1/2) ! ! !LBZBezierTest methodsFor: 'tests'! testInvalidControlIndexAtomic | before | before := model points. {0. 5. nil. 1.5} do: [:bad | self deny: (model point: bad put: 3@4)]. self assert: model points = before ! ! !LBZBezierTest methodsFor: 'tests'! testParameterBounds self assert: (model parameter: 0). self assert: (model parameter: 1). {-1. 2. nil. '0.5'. Float infinity} do: [:bad | self deny: (model parameter: bad). self assert: model parameter = 1] ! ! !LBZBezierTest methodsFor: 'tests'! testSampleCountContract {2. 12. 48. 120} do: [:count | self assert: (model samples: count). self assert: model sampledPoints size = (count + 1)]. {0. 1. 121. 3.5. nil} do: [:bad | self deny: (model samples: bad). self assert: model samples = 120] ! ! !LBZBezierTest methodsFor: 'tests'! testSampleEndpointsAndMiddle | points | model samples: 48. points := model sampledPoints. self assert: points first = model points first. self assert: points last = model points last. self assert: (points at: 25) = (model pointAt: 1/2) ! ! !LBZBezierTest methodsFor: 'tests'! testSamplingDoesNotChangeConstruction | before t | before := model points. t := model parameter. model samples: 120. model sampledPoints. self assert: model points = before. self assert: model parameter = t ! ! !LBZBezierTest methodsFor: 'tests'! testIndependentModels | other | other := LBZModel new. model point: 1 put: 1@2. model parameter: 0. self assert: other points = {60@280. 540@80}. self assert: other parameter = (1/2) ! ! !LBZBezierTest methodsFor: 'tests'! testNativeParameterButtons app open. self click: '0%'. self assert: model parameter = 0. self click: '100%'. self assert: model parameter = 1. self click: '25%'. self assert: model parameter = (1/4) ! ! !LBZBezierTest methodsFor: 'tests'! testNativeSliderCallbacks app open. (app sliderAt: #t) setScaledValue: 37. self assert: model parameter = (37/100). (app sliderAt: #x) setScaledValue: 251. self assert: model points first x = 251. (app sliderAt: #y) setScaledValue: 89. self assert: model points first y = 89 ! ! !LBZBezierTest methodsFor: 'tests'! testRefreshDoesNotSendCallbacks app open. app play. app refresh. self assert: app running. app pause ! ! !LBZBezierTest methodsFor: 'tests'! testNativeSelectedControl app open. self click: 'P2'. (app sliderAt: #x) setScaledValue: 475. self assert: app selected = 3. self assert: model points third x = 475. self assert: model points first x = 60 ! ! !LBZBezierTest methodsFor: 'tests'! testNativeDragAndClamp | handle hand start finish | app open. hand := World activeHand. handle := app handleAt: 2. start := handle center. finish := app plotOrigin + (700@ -20). handle handleMouseDown: (MouseButtonEvent new setType: #mouseDown position: start which: 4 buttons: 4 hand: hand stamp: 1). self assert: hand mouseFocus == handle. handle handleMouseMove: (MouseMoveEvent new setType: #mouseMove startPoint: start endPoint: finish trail: #() buttons: 4 hand: hand stamp: 2). handle handleMouseUp: (MouseButtonEvent new setType: #mouseUp position: finish which: 4 buttons: 0 hand: hand stamp: 3). self assert: model points second = (600@0). self deny: hand mouseFocus == handle ! ! !LBZBezierTest methodsFor: 'tests'! testNativeSampleButtons app open. self click: '12 segments'. self assert: model sampledPoints size = 13. self click: '120 segments'. self assert: model sampledPoints size = 121 ! ! !LBZBezierTest methodsFor: 'tests'! testRepeatedPlayPause app open. 10 timesRepeat: [self click: 'Play'. self click: 'Play'. self assert: app running. self click: 'Pause'. self deny: app running]. self assert: model parameter = (1/2) ! ! !LBZBezierTest methodsFor: 'tests'! testClockBoundAndWrap app open. app play. app advanceMilliseconds: 10000. self assert: model parameter = (21/40). app advanceMilliseconds: -100. self assert: model parameter = (21/40). model parameter: 99/100. app advanceMilliseconds: 100. self assert: model parameter = (3/200). app pause ! ! !LBZBezierTest methodsFor: 'tests'! testPausedClockDoesNothing | before | before := model parameter. app advanceMilliseconds: 40. self assert: model parameter = before ! ! !LBZBezierTest methodsFor: 'tests'! testFocusLossPauses app open. app play. app window passivate. self deny: app running. app window activate. self deny: app running ! ! !LBZBezierTest methodsFor: 'tests'! testLookUnfocusedPauses app open. app play. app window lookUnfocused. self deny: app running ! ! !LBZBezierTest methodsFor: 'tests'! testClosedClockAndReopen | before old | app open. app play. before := model parameter. old := app window. old delete. self deny: app running. self assert: app window isNil. app step. app advanceMilliseconds: 100. self assert: model parameter = before. app open. self deny: app running. self deny: app window == old ! ! !LBZBezierTest methodsFor: 'tests'! testManualEditPauses app open. app play. (app sliderAt: #t) setScaledValue: 25. self deny: app running. self assert: model parameter = (1/4). app play. app moveControl: 2 to: 200@100. self deny: app running ! ! !LBZBezierTest methodsFor: 'tests'! testNativePresetsAndReset app open. self click: 'Loop'. self assert: model points first = model points last. self click: 'Line'. self assert: (model pointAt: 1/2) = (300@160). self click: '120 segments'. self click: 'P3'. self click: 'Play'. 2 timesRepeat: [self click: 'Reset']. self deny: app running. self assert: app selected = 1. self assert: model samples = 48. self assert: model parameter = (1/2). self assert: model points = {60@280. 140@30. 460@30. 540@280} ! ! !LBZBezierTest methodsFor: 'tests'! testInvalidPresetKeepsState | before | before := model points. self deny: (model preset: #unknown). self assert: model points = before ! ! !LBZBezierTest methodsFor: 'tests'! testDrawDoesNotMutateModel | before t count | before := model points. t := model parameter. count := model samples. app drawOn: (Form extent: 1100@800 depth: 32) getCanvas. self assert: model points = before. self assert: model parameter = t. self assert: model samples = count ! ! !LBZBezierTest methodsFor: 'tests'! testActualMorphicClockRegistration | before | app open. before := model parameter. app play. self assert: app isStepping. app instVarNamed: 'lastTick' put: Time millisecondClockValue - 80. app step. self assert: model parameter > before. self assert: model parameter <= (before + (1/40)). app pause. self deny: app isStepping ! ! !LBZBezierTest methodsFor: 'tests'! testDeleteRemovesScheduledClock app open. app play. self assert: app isStepping. app window delete. self deny: app isStepping. self deny: app running ! ! !LBZBezierTest methodsFor: 'tests'! testCollapseUnschedulesAndDoesNotResume | originalWorld | app open. originalWorld := app world. app play. self assert: (originalWorld isStepping: app). app window collapse. self deny: app running. self deny: (originalWorld isStepping: app). app pause. self deny: (originalWorld isStepping: app). app window expand. self deny: app running ! ! !LBZBezierTest methodsFor: 'tests'! testDetachedPauseUsesScheduledWorld | originalWorld | app open. originalWorld := app world. app play. app privateDelete. app pause. self deny: (originalWorld isStepping: app). self deny: app running ! ! !LBZBezierTest methodsFor: 'tests'! testCloseDuringDragReleasesFocus | handle hand | app open. hand := World activeHand. handle := app handleAt: 2. handle handleMouseDown: (MouseButtonEvent new setType: #mouseDown position: handle center which: 4 buttons: 4 hand: hand stamp: 1). self assert: hand mouseFocus == handle. app window delete. self deny: hand mouseFocus == handle ! ! !LBZBezierTest methodsFor: 'tests'! testResetDuringDragDoesNotStealOtherFocus | handle hand other | app open. hand := World activeHand. handle := app handleAt: 2. other := Morph new. handle handleMouseDown: (MouseButtonEvent new setType: #mouseDown position: handle center which: 4 buttons: 4 hand: hand stamp: 1). hand newMouseFocus: other. app reset. self assert: hand mouseFocus == other. hand releaseMouseFocus: other ! ! !LBZBezierTest methodsFor: 'tests'! testRepeatedDragMovesUntilRelease | handle hand start | app open. hand := World activeHand. handle := app handleAt: 2. start := handle center. handle handleMouseDown: (MouseButtonEvent new setType: #mouseDown position: start which: 4 buttons: 4 hand: hand stamp: 1). {180@90. 210@120. 240@150} do: [:p | handle handleMouseMove: (MouseMoveEvent new setType: #mouseMove startPoint: start endPoint: app plotOrigin + p trail: #() buttons: 4 hand: hand stamp: 2). self assert: model points second = p. self assert: hand mouseFocus == handle]. app pause. self deny: hand mouseFocus == handle ! ! !LBZBezierTest methodsFor: 'tests'! testPlayWhileCollapsedStaysPaused | originalWorld | app open. originalWorld := app world. app window collapse. app play. self deny: app running. self deny: (originalWorld isStepping: app) ! !