From c8ff5bd14bb74277b6c15472e5f719c549c98381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Tue, 24 Jan 2023 18:30:24 +0000 Subject: [PATCH] Add tests. --- tests/test_animation.py | 122 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 1 deletion(-) diff --git a/tests/test_animation.py b/tests/test_animation.py index 0498e7693..14d57f6fc 100644 --- a/tests/test_animation.py +++ b/tests/test_animation.py @@ -9,7 +9,7 @@ class AnimApp(App): #foo { height: 1; } - + """ def compose(self) -> ComposeResult: @@ -37,3 +37,123 @@ async def test_animate_height() -> None: assert elapsed >= 0.5 # Check the height reached the maximum assert static.styles.height.value == 100 + + +async def test_scheduling_animation() -> None: + """Test that scheduling an animation works.""" + + app = AnimApp() + delay = 0.1 + + async with app.run_test() as pilot: + styles = app.query_one(Static).styles + styles.background = "black" + + styles.animate("background", "white", delay=delay, duration=0) + + await pilot.pause(0.9 * delay) + assert styles.background.rgb == (0, 0, 0) # Still black + + await pilot.wait_for_scheduled_animations() + assert styles.background.rgb == (255, 255, 255) + + +async def test_wait_for_current_animations() -> None: + """Test that we can wait only for the current animations taking place.""" + + app = AnimApp() + + delay = 10 + + async with app.run_test() as pilot: + styles = app.query_one(Static).styles + styles.animate("height", 100, duration=0.1) + start = perf_counter() + styles.animate("height", 200, duration=0.1, delay=delay) + + # Wait for the first animation to finish + await pilot.wait_for_animation() + elapsed = perf_counter() - start + assert elapsed < (delay / 2) + + +async def test_wait_for_current_and_scheduled_animations() -> None: + """Test that we can wait for current and scheduled animations.""" + + app = AnimApp() + + async with app.run_test() as pilot: + styles = app.query_one(Static).styles + + start = perf_counter() + styles.animate("height", 50, duration=0.01) + styles.animate("background", "black", duration=0.01, delay=0.05) + + await pilot.wait_for_scheduled_animations() + elapsed = perf_counter() - start + assert elapsed >= 0.06 + assert styles.background.rgb == (0, 0, 0) + + +async def test_reverse_animations() -> None: + """Test that you can create reverse animations. + + Regression test for #1372 https://github.com/Textualize/textual/issues/1372 + """ + + app = AnimApp() + + async with app.run_test() as pilot: + static = app.query_one(Static) + styles = static.styles + + # Starting point. + styles.background = "black" + assert styles.background.rgb == (0, 0, 0) + + # First, make sure we can go from black to white and back, step by step. + styles.animate("background", "white", duration=0.01) + await pilot.wait_for_animation() + assert styles.background.rgb == (255, 255, 255) + + styles.animate("background", "black", duration=0.01) + await pilot.wait_for_animation() + assert styles.background.rgb == (0, 0, 0) + + # Now, the actual test is to make sure we go back to black if scheduling both at once. + styles.animate("background", "white", duration=0.01) + styles.animate("background", "black", duration=0.01) + await pilot.wait_for_animation() + assert styles.background.rgb == (0, 0, 0) + + +async def test_schedule_reverse_animations() -> None: + """Test that you can schedule reverse animations. + + Regression test for #1372 https://github.com/Textualize/textual/issues/1372 + """ + + app = AnimApp() + + async with app.run_test() as pilot: + static = app.query_one(Static) + styles = static.styles + + # Starting point. + styles.background = "black" + assert styles.background.rgb == (0, 0, 0) + + # First, make sure we can go from black to white and back, step by step. + styles.animate("background", "white", delay=0.01, duration=0.01) + await pilot.wait_for_scheduled_animations() + assert styles.background.rgb == (255, 255, 255) + + styles.animate("background", "black", delay=0.01, duration=0.01) + await pilot.wait_for_scheduled_animations() + assert styles.background.rgb == (0, 0, 0) + + # Now, the actual test is to make sure we go back to black if scheduling both at once. + styles.animate("background", "white", delay=0.01, duration=0.01) + styles.animate("background", "black", delay=0.01, duration=0.01) + await pilot.wait_for_scheduled_animations() + assert styles.background.rgb == (0, 0, 0)