From b023d4e02e0a91c99847da4a6429c008ae3ec398 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: Mon, 30 Jan 2023 11:50:02 +0000 Subject: [PATCH] Improve tests. --- tests/css/test_programmatic_style_changes.py | 107 +++++++----- .../__snapshots__/test_snapshots.ambr | 155 ++++++++++++++++++ .../programmatic_scrollbar_gutter_change.py | 29 ++++ tests/snapshot_tests/test_snapshots.py | 10 ++ 4 files changed, 264 insertions(+), 37 deletions(-) create mode 100644 tests/snapshot_tests/snapshot_apps/programmatic_scrollbar_gutter_change.py diff --git a/tests/css/test_programmatic_style_changes.py b/tests/css/test_programmatic_style_changes.py index b30f34ba7..f15ecdd55 100644 --- a/tests/css/test_programmatic_style_changes.py +++ b/tests/css/test_programmatic_style_changes.py @@ -6,57 +6,90 @@ from textual.screen import Screen from textual.widgets import Label -updates = 0 - - -class _Label(Label): - """Label widget that keeps track of its own updates.""" - - def refresh(self, *args, **kwargs): - global updates - updates += 1 - return super().refresh(*args, **kwargs) - - @pytest.mark.parametrize( "style, value", [ - ("grid_size_rows", 2), - ("grid_size_columns", 2), - ("grid_gutter_vertical", 2), - ("grid_gutter_horizontal", 1), + ("grid_size_rows", 3), + ("grid_size_columns", 3), + ("grid_gutter_vertical", 4), + ("grid_gutter_horizontal", 4), ("grid_rows", "1fr 3fr"), ("grid_columns", "1fr 3fr"), - ("scrollbar_gutter", "stable"), - ("align_horizontal", "right"), - ("align_vertical", "bottom"), - ("align", ("right", "bottom")), ], ) -def test_programmatic_style_change_refreshes_children_layout(style: str, value): +async def test_programmatic_style_change_updates_children(style: str, value: object): """Regression test for #1607 https://github.com/Textualize/textual/issues/1607 Some programmatic style changes to a widget were not updating the layout of the children widgets, which seemed to be happening when the style change did not affect the size of the widget but did affect the layout of the children. + + This test, in particular, checks the attributes that _should_ affect the size of the + children widgets. """ - global updates + class MyApp(App[None]): + CSS = """ + Grid { grid-size: 2 2; } + Label { width: 100%; height: 100%; } + """ - app = App() - app.DEFAULT_CSS = "Grid { grid-size: 1 1; }" - app._set_active() - app.push_screen(Screen()) + def compose(self): + yield Grid( + Label("one"), + Label("two"), + Label("three"), + Label("four"), + ) - grid = Grid( - _Label("one"), - _Label("two"), - _Label("three"), - _Label("four"), - ) - app.screen._add_children(grid) + app = MyApp() - update_count = updates - setattr(grid.styles, style, value) - print(updates, update_count) - assert updates > update_count + async with app.run_test() as pilot: + sizes = [(lbl.size.width, lbl.size.height) for lbl in app.screen.query(Label)] + + setattr(app.query_one(Grid).styles, style, value) + await pilot.pause() + + assert sizes != [ + (lbl.size.width, lbl.size.height) for lbl in app.screen.query(Label) + ] + + +@pytest.mark.parametrize( + "style, value", + [ + ("align_horizontal", "right"), + ("align_vertical", "bottom"), + ("align", ("right", "bottom")), + ], +) +async def test_programmatic_align_change_updates_children_position( + style: str, value: str +): + """Regression test for #1607 for the align(_xxx) styles. + + See https://github.com/Textualize/textual/issues/1607. + """ + + class MyApp(App[None]): + CSS = "Grid { grid-size: 2 2; }" + + def compose(self): + yield Grid( + Label("one"), + Label("two"), + Label("three"), + Label("four"), + ) + + app = MyApp() + + async with app.run_test() as pilot: + offsets = [(lbl.region.x, lbl.region.y) for lbl in app.screen.query(Label)] + + setattr(app.query_one(Grid).styles, style, value) + await pilot.pause() + + assert offsets != [ + (lbl.region.x, lbl.region.y) for lbl in app.screen.query(Label) + ] diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr index 52b617982..27dfb81aa 100644 --- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr +++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr @@ -13651,6 +13651,161 @@ ''' # --- +# name: test_programmatic_scrollbar_gutter_change + ''' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ProgrammaticScrollbarGutterChange + + + + + + + + + + onetwo + + + + + + + + + + + + threefour + + + + + + + + + + + + + + + + ''' +# --- # name: test_textlog_max_lines ''' diff --git a/tests/snapshot_tests/snapshot_apps/programmatic_scrollbar_gutter_change.py b/tests/snapshot_tests/snapshot_apps/programmatic_scrollbar_gutter_change.py new file mode 100644 index 000000000..d1a7fba0d --- /dev/null +++ b/tests/snapshot_tests/snapshot_apps/programmatic_scrollbar_gutter_change.py @@ -0,0 +1,29 @@ +from textual.app import App +from textual.containers import Grid +from textual.widgets import Label + + +class ProgrammaticScrollbarGutterChange(App[None]): + CSS = """ + Grid { grid-size: 2 2; scrollbar-size: 5 5; } + Label { width: 100%; height: 100%; background: red; } + """ + + def compose(self): + yield Grid( + Label("one"), + Label("two"), + Label("three"), + Label("four"), + ) + + def on_key(self, event): + if event.key == "s": + self.query_one(Grid).styles.scrollbar_gutter = "stable" + + +app = ProgrammaticScrollbarGutterChange() + + +if __name__ == "__main__": + app().run() diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 868bd91aa..7a6fb9324 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -179,6 +179,16 @@ def test_nested_auto_heights(snap_compare): assert snap_compare("snapshot_apps/nested_auto_heights.py", press=["1", "2", "_"]) +def test_programmatic_scrollbar_gutter_change(snap_compare): + """Regression test for #1607 https://github.com/Textualize/textual/issues/1607 + + See also tests/css/test_programmatic_style_changes.py for other related regression tests. + """ + assert snap_compare( + "snapshot_apps/programmatic_scrollbar_gutter_change.py", press=["s"] + ) + + # --- Other ---