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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
from textual.app import App, ComposeResult
from textual.containers import Container, Vertical
from textual.widgets import Button, Label, Static
async def test_remove_single_widget():
"""It should be possible to the only widget on a screen."""
async with App().run_test() as pilot:
widget = Static()
assert not widget.is_attached
await pilot.app.mount(widget)
assert widget.is_attached
assert len(pilot.app.screen._nodes) == 1
await pilot.app.query_one(Static).remove()
assert not widget.is_attached
assert len(pilot.app.screen._nodes) == 0
async def test_many_remove_all_widgets():
"""It should be possible to remove all widgets on a multi-widget screen."""
async with App().run_test() as pilot:
await pilot.app.mount(*[Static() for _ in range(10)])
assert len(pilot.app.screen._nodes) == 10
await pilot.app.query(Static).remove()
assert len(pilot.app.screen._nodes) == 0
async def test_many_remove_some_widgets():
"""It should be possible to remove some widgets on a multi-widget screen."""
async with App().run_test() as pilot:
await pilot.app.mount(*[Static(classes=f"is-{n % 2}") for n in range(10)])
assert len(pilot.app.screen._nodes) == 10
await pilot.app.query(".is-0").remove()
assert len(pilot.app.screen._nodes) == 5
async def test_remove_branch():
"""It should be possible to remove a whole branch in the DOM."""
async with App().run_test() as pilot:
await pilot.app.mount(
Container(Container(Container(Container(Container(Static()))))),
Static(),
Container(Container(Container(Container(Container(Static()))))),
)
assert len(pilot.app.screen.walk_children(with_self=False)) == 13
await pilot.app.screen._nodes[0].remove()
assert len(pilot.app.screen.walk_children(with_self=False)) == 7
async def test_remove_overlap():
"""It should be possible to remove an overlapping collection of widgets."""
async with App().run_test() as pilot:
await pilot.app.mount(
Container(Container(Container(Container(Container(Static()))))),
Static(),
Container(Container(Container(Container(Container(Static()))))),
)
assert len(pilot.app.screen.walk_children(with_self=False)) == 13
await pilot.app.query(Container).remove()
assert len(pilot.app.screen.walk_children(with_self=False)) == 1
async def test_remove_move_focus():
"""Removing a focused widget should settle focus elsewhere."""
async with App().run_test() as pilot:
buttons = [Button(str(n)) for n in range(10)]
await pilot.app.mount(Container(*buttons[:5]), Container(*buttons[5:]))
assert len(pilot.app.screen._nodes) == 2
assert len(pilot.app.screen.walk_children(with_self=False)) == 12
assert pilot.app.focused is None
await pilot.press("tab")
assert pilot.app.focused is not None
assert pilot.app.focused == buttons[0]
await pilot.app.screen._nodes[0].remove()
assert len(pilot.app.screen._nodes) == 1
assert len(pilot.app.screen.walk_children(with_self=False)) == 6
assert pilot.app.focused is not None
assert pilot.app.focused == buttons[9]
async def test_widget_remove_order() -> None:
"""A Widget.remove of a top-level widget should cause bottom-first removal."""
removals: list[str] = []
class Removable(Container):
def on_unmount(self, _):
removals.append(self.id if self.id is not None else "unknown")
async with App().run_test() as pilot:
await pilot.app.mount(
Removable(Removable(Removable(id="grandchild"), id="child"), id="parent")
)
assert len(pilot.app.screen.walk_children(with_self=False)) == 3
await pilot.app.screen._nodes[0].remove()
assert len(pilot.app.screen.walk_children(with_self=False)) == 0
assert removals == ["grandchild", "child", "parent"]
async def test_query_remove_order():
"""A DOMQuery.remove of a top-level widget should cause bottom-first removal."""
removals: list[str] = []
class Removable(Container):
def on_unmount(self, _):
removals.append(self.id if self.id is not None else "unknown")
async with App().run_test() as pilot:
await pilot.app.mount(
Removable(Removable(Removable(id="grandchild"), id="child"), id="parent")
)
assert len(pilot.app.screen.walk_children(with_self=False)) == 3
await pilot.app.query(Removable).remove()
assert len(pilot.app.screen.walk_children(with_self=False)) == 0
assert removals == ["grandchild", "child", "parent"]
class ExampleApp(App):
def compose(self) -> ComposeResult:
yield Button("ABC")
yield Label("Outside of vertical.")
with Vertical():
for index in range(5):
yield Label(str(index))
async def test_widget_remove_children_container():
app = ExampleApp()
async with app.run_test():
container = app.query_one(Vertical)
# 6 labels in total, with 5 of them inside the container.
assert len(app.query(Label)) == 6
assert len(container.children) == 5
await container.remove_children()
# The labels inside the container are gone, and the 1 outside remains.
assert len(app.query(Label)) == 1
assert len(container.children) == 0
async def test_widget_remove_children_with_star_selector():
app = ExampleApp()
async with app.run_test():
container = app.query_one(Vertical)
# 6 labels in total, with 5 of them inside the container.
assert len(app.query(Label)) == 6
assert len(container.children) == 5
await container.remove_children("*")
# The labels inside the container are gone, and the 1 outside remains.
assert len(app.query(Label)) == 1
assert len(container.children) == 0
async def test_widget_remove_children_with_string_selector():
app = ExampleApp()
async with app.run_test():
container = app.query_one(Vertical)
# 6 labels in total, with 5 of them inside the container.
assert len(app.query(Label)) == 6
assert len(container.children) == 5
await app.screen.remove_children("Label")
# Only the Screen > Label widget is gone, everything else remains.
assert len(app.query(Button)) == 1
assert len(app.query(Vertical)) == 1
assert len(app.query(Label)) == 5
async def test_widget_remove_children_with_type_selector():
app = ExampleApp()
async with app.run_test():
assert len(app.query(Button)) == 1 # Sanity check.
await app.screen.remove_children(Button)
assert len(app.query(Button)) == 0
async def test_widget_remove_children_with_selector_does_not_leak():
app = ExampleApp()
async with app.run_test():
container = app.query_one(Vertical)
# 6 labels in total, with 5 of them inside the container.
assert len(app.query(Label)) == 6
assert len(container.children) == 5
await container.remove_children("Label")
# The labels inside the container are gone, and the 1 outside remains.
assert len(app.query(Label)) == 1
assert len(container.children) == 0
async def test_widget_remove_children_no_children():
app = ExampleApp()
async with app.run_test():
button = app.query_one(Button)
count_before = len(app.query("*"))
await button.remove_children()
count_after = len(app.query("*"))
assert len(app.query(Button)) == 1 # The button still remains.
assert (
count_before == count_after
) # No widgets have been removed, since Button has no children.
async def test_widget_remove_children_no_children_match_selector():
app = ExampleApp()
async with app.run_test():
container = app.query_one(Vertical)
assert len(container.query("Button")) == 0 # Sanity check.
count_before = len(app.query("*"))
container_children_before = list(container.children)
await container.remove_children("Button")
assert count_before == len(app.query("*"))
assert container_children_before == list(container.children)
|