File: test_tree_messages.py

package info (click to toggle)
textual 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,084 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (254 lines) | stat: -rw-r--r-- 8,875 bytes parent folder | download | duplicates (2)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from __future__ import annotations

from typing import Any

from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Button, Tree


class MyTree(Tree[None]):
    pass


class TreeApp(App[None]):
    """Test tree app."""

    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)
        self.messages: list[tuple[str, str]] = []

    def compose(self) -> ComposeResult:
        """Compose the child widgets."""
        yield MyTree("Root", id="test-tree")

    def on_mount(self) -> None:
        self.query_one(MyTree).root.add("Child")
        self.query_one(MyTree).focus()

    def record(
        self,
        event: (
            Tree.NodeSelected[None]
            | Tree.NodeExpanded[None]
            | Tree.NodeCollapsed[None]
            | Tree.NodeHighlighted[None]
        ),
    ) -> None:
        self.messages.append(
            (event.__class__.__name__, event.node.tree.id or "Unknown")
        )

    def on_tree_node_selected(self, event: Tree.NodeSelected[None]) -> None:
        self.record(event)

    def on_tree_node_expanded(self, event: Tree.NodeExpanded[None]) -> None:
        self.record(event)

    def on_tree_node_collapsed(self, event: Tree.NodeCollapsed[None]) -> None:
        self.record(event)

    def on_tree_node_highlighted(self, event: Tree.NodeHighlighted[None]) -> None:
        self.record(event)


async def test_tree_node_selected_message() -> None:
    """Selecting a node should result in a selected message being emitted."""
    async with TreeApp().run_test() as pilot:
        await pilot.press("enter")
        await pilot.pause()
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeSelected", "test-tree"),
            ("NodeExpanded", "test-tree"),
        ]


async def test_tree_node_selected_message_no_auto() -> None:
    """Selecting a node should result in only a selected message being emitted."""
    async with TreeApp().run_test() as pilot:
        pilot.app.query_one(MyTree).auto_expand = False
        await pilot.press("enter")
        await pilot.pause()
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeSelected", "test-tree"),
        ]


async def test_tree_node_expanded_message() -> None:
    """Expanding a node should result in an expanded message being emitted."""
    async with TreeApp().run_test() as pilot:
        await pilot.press("space")
        await pilot.pause()
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeExpanded", "test-tree"),
        ]


async def tree_node_expanded_by_code_message() -> None:
    """Expanding a node via the API should result in an expanded message being posted."""
    async with TreeApp().run_test() as pilot:
        pilot.app.query_one(Tree).root.children[0].expand()
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeExpanded", "test-tree"),
        ]


async def tree_node_all_expanded_by_code_message() -> None:
    """Expanding all nodes via the API should result in expanded messages being posted."""
    async with TreeApp().run_test() as pilot:
        pilot.app.query_one(Tree).root.children[0].expand_all()
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeExpanded", "test-tree"),
        ]


async def test_tree_node_collapsed_message() -> None:
    """Collapsing a node should result in a collapsed message being emitted."""
    async with TreeApp().run_test() as pilot:
        await pilot.press("space", "space")
        await pilot.pause()
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeExpanded", "test-tree"),
            ("NodeCollapsed", "test-tree"),
        ]


async def tree_node_collapsed_by_code_message() -> None:
    """Collapsing a node via the API should result in a collapsed message being posted."""
    async with TreeApp().run_test() as pilot:
        pilot.app.query_one(Tree).root.children[0].expand().collapse()
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeExpanded", "test-tree"),
            ("NodeCollapsed", "test-tree"),
        ]


async def tree_node_all_collapsed_by_code_message() -> None:
    """Collapsing all nodes via the API should result in collapsed messages being posted."""
    async with TreeApp().run_test() as pilot:
        pilot.app.query_one(Tree).root.children[0].expand_all().collapse_all()
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeExpanded", "test-tree"),
            ("NodeCollapsed", "test-tree"),
        ]


async def tree_node_toggled_by_code_message() -> None:
    """Toggling a node twice via the API should result in expanded and collapsed messages."""
    async with TreeApp().run_test() as pilot:
        pilot.app.query_one(Tree).root.children[0].toggle().toggle()
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeExpanded", "test-tree"),
            ("NodeCollapsed", "test-tree"),
        ]


async def tree_node_all_toggled_by_code_message() -> None:
    """Toggling all nodes twice via the API should result in expanded and collapsed messages."""
    async with TreeApp().run_test() as pilot:
        pilot.app.query_one(Tree).root.children[0].toggle_all().toggle_all()
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeExpanded", "test-tree"),
            ("NodeCollapsed", "test-tree"),
        ]


async def test_tree_node_highlighted_message() -> None:
    """Highlighting a node should result in a highlighted message being emitted."""
    async with TreeApp().run_test() as pilot:
        await pilot.press("enter", "down")
        await pilot.pause()
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeSelected", "test-tree"),
            ("NodeExpanded", "test-tree"),
            ("NodeHighlighted", "test-tree"),
        ]


class TreeWrapper(Vertical):
    """Testing widget related to https://github.com/Textualize/textual/issues/3869"""

    def __init__(self, auto_expand: bool) -> None:
        super().__init__()
        self._auto_expand = auto_expand

    def compose(self) -> ComposeResult:
        """Compose the child widgets."""
        yield Button(id="expander")
        yield Button(id="collapser")
        yield MyTree("Root", id="test-tree")

    def on_mount(self) -> None:
        self.query_one(MyTree).auto_expand = self._auto_expand
        self.query_one(MyTree).root.add("Child")

    def on_button_pressed(self, event: Button.Pressed) -> None:
        if event.button.id == "expander":
            self.query_one(Tree).root.expand()
        elif event.button.id == "collapser":
            self.query_one(Tree).root.collapse()


class TreeViaCodeApp(App[None]):
    """Testing app related to https://github.com/Textualize/textual/issues/3869"""

    def __init__(self, auto_expand: bool, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)
        self.messages: list[tuple[str, str]] = []
        self._auto_expand = auto_expand

    def compose(self) -> ComposeResult:
        """Compose the child widgets."""
        yield TreeWrapper(self._auto_expand)

    def record(
        self,
        event: (
            Tree.NodeExpanded[None]
            | Tree.NodeCollapsed[None]
            | Tree.NodeHighlighted[None]
        ),
    ) -> None:
        self.messages.append(
            (event.__class__.__name__, event.node.tree.id or "Unknown")
        )

    def on_tree_node_expanded(self, event: Tree.NodeExpanded[None]) -> None:
        self.record(event)

    def on_tree_node_collapsed(self, event: Tree.NodeCollapsed[None]) -> None:
        self.record(event)

    def on_tree_node_highlighted(self, event: Tree.NodeHighlighted[None]) -> None:
        self.record(event)


async def test_expand_node_from_code() -> None:
    """Expanding a node from code should result in the appropriate message."""
    async with TreeViaCodeApp(False).run_test() as pilot:
        await pilot.click("#expander")
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeExpanded", "test-tree"),
        ]


async def test_collapse_node_from_code() -> None:
    """Collapsing a node from code should result in the appropriate message."""
    async with TreeViaCodeApp(True).run_test() as pilot:
        await pilot.click("#collapser")
        assert pilot.app.messages == [
            ("NodeHighlighted", "test-tree"),
            ("NodeCollapsed", "test-tree"),
        ]