1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
"""
Tests for the switch toggle animation, which is considered a basic animation.
(An animation that also plays on the level BASIC.)
"""
from textual.app import App, ComposeResult
from textual.widgets import Switch
class SwitchApp(App[None]):
def compose(self) -> ComposeResult:
yield Switch()
async def test_switch_animates_on_full() -> None:
app = SwitchApp()
app.animation_level = "full"
async with app.run_test() as pilot:
switch = app.query_one(Switch)
animator = app.animator
# Freeze time at 0 before triggering the animation.
animator._get_time = lambda *_: 0
switch.action_toggle_switch()
await pilot.pause()
# Freeze time after the animation start and before animation end.
animator._get_time = lambda *_: 0.01
# Move to the next frame.
animator()
# The animation should still be running.
assert app.animator.is_being_animated(switch, "_slider_position")
async def test_switch_animates_on_basic() -> None:
app = SwitchApp()
app.animation_level = "basic"
async with app.run_test() as pilot:
switch = app.query_one(Switch)
animator = app.animator
# Freeze time at 0 before triggering the animation.
animator._get_time = lambda *_: 0
switch.action_toggle_switch()
await pilot.pause()
# Freeze time after the animation start and before animation end.
animator._get_time = lambda *_: 0.01
# Move to the next frame.
animator()
# The animation should still be running.
assert app.animator.is_being_animated(switch, "_slider_position")
async def test_switch_does_not_animate_on_none() -> None:
app = SwitchApp()
app.animation_level = "none"
async with app.run_test() as pilot:
switch = app.query_one(Switch)
animator = app.animator
# Freeze time at 0 before triggering the animation.
animator._get_time = lambda *_: 0
switch.action_toggle_switch()
await pilot.pause()
# Freeze time after the animation start and before animation end.
animator._get_time = lambda *_: 0.01
# Move to the next frame.
animator()
# The animation should still be running.
assert not app.animator.is_being_animated(switch, "_slider_position")
|