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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
"""
Test that generic animations can be disabled.
"""
from textual.app import App, ComposeResult
from textual.color import Color
from textual.widgets import Label
class SingleLabelApp(App[None]):
"""Single label whose background colour we'll animate."""
CSS = """
Label {
background: red;
}
"""
def compose(self) -> ComposeResult:
yield Label()
async def test_style_animations_via_animate_work_on_full() -> None:
app = SingleLabelApp()
app.animation_level = "full"
async with app.run_test():
label = app.query_one(Label)
# Sanity check.
assert label.styles.background == Color.parse("red")
animator = app.animator
# Freeze time at 0 before triggering the animation.
animator._get_time = lambda *_: 0
label.styles.animate("background", "blue", duration=1)
assert len(animator._animations) > 0 # Sanity check.
# Freeze time around the animation midpoint.
animator._get_time = lambda *_: 0.5
# Move to the next frame.
animator()
# The animation shouldn't have completed.
assert label.styles.background != Color.parse("red")
assert label.styles.background != Color.parse("blue")
async def test_style_animations_via_animate_are_disabled_on_basic() -> None:
app = SingleLabelApp()
app.animation_level = "basic"
async with app.run_test():
label = app.query_one(Label)
# Sanity check.
assert label.styles.background == Color.parse("red")
animator = app.animator
# Freeze time at 0 before triggering the animation.
animator._get_time = lambda *_: 0
label.styles.animate("background", "blue", duration=1)
assert len(animator._animations) > 0 # Sanity check.
# 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 have completed.
assert label.styles.background == Color.parse("blue")
async def test_style_animations_via_animate_are_disabled_on_none() -> None:
app = SingleLabelApp()
app.animation_level = "none"
async with app.run_test():
label = app.query_one(Label)
# Sanity check.
assert label.styles.background == Color.parse("red")
animator = app.animator
# Freeze time at 0 before triggering the animation.
animator._get_time = lambda *_: 0
label.styles.animate("background", "blue", duration=1)
assert len(animator._animations) > 0 # Sanity check.
# 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 have completed.
assert label.styles.background == Color.parse("blue")
class LabelWithTransitionsApp(App[None]):
"""Single label whose background is set to animate with TCSS."""
CSS = """
Label {
background: red;
transition: background 1s;
}
Label.blue-bg {
background: blue;
}
"""
def compose(self) -> ComposeResult:
yield Label()
async def test_style_animations_via_transition_work_on_full() -> None:
app = LabelWithTransitionsApp()
app.animation_level = "full"
async with app.run_test():
label = app.query_one(Label)
# Sanity check.
assert label.styles.background == Color.parse("red")
animator = app.animator
# Free time at 0 before triggering the animation.
animator._get_time = lambda *_: 0
label.add_class("blue-bg")
assert len(animator._animations) > 0 # Sanity check.
# Freeze time in the middle of the animation.
animator._get_time = lambda *_: 0.5
animator()
# The animation should be undergoing.
assert label.styles.background != Color.parse("red")
assert label.styles.background != Color.parse("blue")
async def test_style_animations_via_transition_are_disabled_on_basic() -> None:
app = LabelWithTransitionsApp()
app.animation_level = "basic"
async with app.run_test():
label = app.query_one(Label)
# Sanity check.
assert label.styles.background == Color.parse("red")
animator = app.animator
# Free time at 0 before triggering the animation.
animator._get_time = lambda *_: 0
label.add_class("blue-bg")
assert len(animator._animations) > 0 # Sanity check.
# Freeze time after the animation start and before animation end.
animator._get_time = lambda *_: 0.01
animator()
# The animation should have completed.
assert label.styles.background == Color.parse("blue")
async def test_style_animations_via_transition_are_disabled_on_none() -> None:
app = LabelWithTransitionsApp()
app.animation_level = "none"
async with app.run_test():
label = app.query_one(Label)
# Sanity check.
assert label.styles.background == Color.parse("red")
animator = app.animator
# Free time at 0 before triggering the animation.
animator._get_time = lambda *_: 0
label.add_class("blue-bg")
assert len(animator._animations) > 0 # Sanity check.
# Freeze time after the animation start and before animation end.
animator._get_time = lambda *_: 0.01
animator()
# The animation should have completed.
assert label.styles.background == Color.parse("blue")
|