File size: 12,157 Bytes
2c3c408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
from __future__ import annotations
from typing import cast, List, Sequence

import pytest
from rich.console import RenderableType
from rich.text import Text

from tests.utilities.test_app import AppTest
from textual.app import ComposeResult
from textual.css.types import EdgeType
from textual.geometry import Size
from textual.widget import Widget
from textual.widgets import Placeholder

pytestmark = pytest.mark.integration_test

# Let's allow ourselves some abbreviated names for those tests,
# in order to make the test cases a bit easier to read :-)
SCREEN_W = 100  # width of our Screens
SCREEN_H = 8  # height of our Screens
SCREEN_SIZE = Size(SCREEN_W, SCREEN_H)
PLACEHOLDERS_DEFAULT_H = 3  # the default height for our Placeholder widgets

# As per Widget's CSS property, by default Widgets have a horizontal scrollbar of size 1
# and a vertical scrollbar of size 2:
SCROLL_H_SIZE = 1
SCROLL_V_SIZE = 2


@pytest.mark.asyncio
@pytest.mark.parametrize(
    (
        "placeholders_count",
        "root_container_style",
        "placeholders_style",
        "expected_root_widget_virtual_size",
        "expected_placeholders_size",
        "expected_placeholders_offset_x",
    ),
    (
        *[
            [
                1,
                f"border: {invisible_border_edge};",  # #root has no visible border
                "",  # no specific placeholder style
                # #root's virtual size=screen size
                (SCREEN_W, SCREEN_H),
                # placeholders width=same than screen :: height=default height
                (SCREEN_W, PLACEHOLDERS_DEFAULT_H),
                # placeholders should be at offset 0
                0,
            ]
            for invisible_border_edge in ("", "none", "hidden")
        ],
        [
            1,
            "border: solid white;",  # #root has a visible border
            "",  # no specific placeholder style
            # #root's virtual size is smaller because of its borders
            (SCREEN_W - 2, SCREEN_H - 2),
            # placeholders width=same than screen, minus 2 borders :: height=default height minus 2 borders
            (SCREEN_W - 2, PLACEHOLDERS_DEFAULT_H),
            # placeholders should be at offset 1 because of #root's border
            1,
        ],
        [
            4,
            "border: solid white;",  # #root has a visible border
            "",  # no specific placeholder style
            # #root's virtual height should be as high as its stacked content
            (SCREEN_W - 2 - 1, PLACEHOLDERS_DEFAULT_H * 4),
            # placeholders width=same than screen, minus 2 borders, minus scrollbar :: height=default height minus 2 borders
            (SCREEN_W - 2 - SCROLL_V_SIZE, PLACEHOLDERS_DEFAULT_H),
            # placeholders should be at offset 1 because of #root's border
            1,
        ],
        [
            1,
            "border: solid white;",  # #root has a visible border
            "align: center top;",  # placeholders are centered horizontally
            # #root's virtual size=screen size
            (SCREEN_W, SCREEN_H),
            # placeholders width=same than screen, minus 2 borders :: height=default height
            (SCREEN_W - 2, PLACEHOLDERS_DEFAULT_H),
            # placeholders should be at offset 1 because of #root's border
            1,
        ],
        [
            4,
            "border: solid white;",  # #root has a visible border
            "align: center top;",  # placeholders are centered horizontally
            # #root's virtual height should be as high as its stacked content
            (
                SCREEN_W - 2 - SCROLL_V_SIZE,
                PLACEHOLDERS_DEFAULT_H * 4,
            ),
            # placeholders width=same than screen, minus 2 borders, minus scrollbar :: height=default height
            (SCREEN_W - 2 - SCROLL_V_SIZE, PLACEHOLDERS_DEFAULT_H),
            # placeholders should be at offset 1 because of #root's border
            1,
        ],
    ),
)
async def test_composition_of_vertical_container_with_children(
    placeholders_count: int,
    root_container_style: str,
    placeholders_style: str,
    expected_placeholders_size: tuple[int, int],
    expected_root_widget_virtual_size: tuple[int, int],
    expected_placeholders_offset_x: int,
):
    class VerticalContainer(Widget):
        DEFAULT_CSS = (
            """
        VerticalContainer {
            layout: vertical;
            overflow: hidden auto;
            ${root_container_style}
        }

        VerticalContainer Placeholder {
            height: ${placeholders_height};
            ${placeholders_style}
        }
        """.replace(
                "${root_container_style}", root_container_style
            )
            .replace("${placeholders_height}", str(PLACEHOLDERS_DEFAULT_H))
            .replace("${placeholders_style}", placeholders_style)
        )

    class MyTestApp(AppTest):
        def compose(self) -> ComposeResult:
            placeholders = [
                Placeholder(id=f"placeholder_{i}", name=f"Placeholder #{i}")
                for i in range(placeholders_count)
            ]

            yield VerticalContainer(*placeholders, id="root")

    app = MyTestApp(size=SCREEN_SIZE, test_name="compositor")

    expected_screen_size = SCREEN_SIZE

    async with app.in_running_state():
        # root widget checks:
        root_widget = cast(Widget, app.get_child("root"))
        assert root_widget.outer_size == expected_screen_size
        root_widget_region = app.screen.find_widget(root_widget).region
        assert root_widget_region == (
            0,
            0,
            expected_screen_size.width,
            expected_screen_size.height,
        )

        app_placeholders = cast(List[Widget], app.query("Placeholder"))
        assert len(app_placeholders) == placeholders_count

    # placeholder widgets checks:
    for placeholder in app_placeholders:
        assert placeholder.outer_size == expected_placeholders_size
        assert placeholder.styles.offset.x.value == 0.0
        assert app.screen.get_offset(placeholder).x == expected_placeholders_offset_x


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "edge_type,expected_box_inner_size,expected_box_size,expected_top_left_edge_color,expects_visible_char_at_top_left_edge",
    (
        # These first 3 types of border edge types are synonyms, and display no borders:
        ["", Size(SCREEN_W, 1), Size(SCREEN_W, 1), "black", False],
        ["none", Size(SCREEN_W, 1), Size(SCREEN_W, 1), "black", False],
        ["hidden", Size(SCREEN_W, 1), Size(SCREEN_W, 1), "black", False],
        # Let's transition to "blank": we still see no visible border, but the size is increased
        # as the gutter space is reserved the same way it would be with a border:
        ["blank", Size(SCREEN_W - 2, 1), Size(SCREEN_W, 3), "#ffffff", False],
        # And now for the "normally visible" border edge types:
        # --> we see a visible border, and the size is increased:
        *[
            [edge_style, Size(SCREEN_W - 2, 1), Size(SCREEN_W, 3), "#ffffff", True]
            for edge_style in [
                "round",
                "solid",
                "double",
                "dashed",
                "heavy",
                "inner",
                "outer",
                "hkey",
                "vkey",
            ]
        ],
    ),
)
async def test_border_edge_types_impact_on_widget_size(
    edge_type: EdgeType,
    expected_box_inner_size: Size,
    expected_box_size: Size,
    expected_top_left_edge_color: str,
    expects_visible_char_at_top_left_edge: bool,
):
    class BorderTarget(Widget):
        def render(self) -> RenderableType:
            return Text("border target", style="black on yellow", justify="center")

    border_target = BorderTarget()
    border_target.styles.height = "auto"
    border_target.styles.border = (edge_type, "white")

    class MyTestApp(AppTest):
        def compose(self) -> ComposeResult:
            yield border_target

    app = MyTestApp(size=SCREEN_SIZE, test_name="border_edge_types")

    await app.boot_and_shutdown()

    box_inner_size = Size(
        border_target.content_region.width,
        border_target.content_region.height,
    )
    assert box_inner_size == expected_box_inner_size

    assert border_target.outer_size == expected_box_size

    top_left_edge_style = app.screen.get_style_at(0, 0)
    top_left_edge_color = top_left_edge_style.color.name
    assert top_left_edge_color.upper() == expected_top_left_edge_color.upper()

    top_left_edge_char = app.get_char_at(0, 0)
    top_left_edge_char_is_a_visible_one = top_left_edge_char != " "
    assert top_left_edge_char_is_a_visible_one == expects_visible_char_at_top_left_edge


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "large_widget_size,container_style,expected_large_widget_visible_region_size",
    (
        # In these tests we're going to insert a "large widget"
        # into a container with size (20,20).
        # ---------------- let's start!
        # no overflow/scrollbar instructions: no scrollbars
        [Size(30, 30), "color: red", Size(20, 20)],
        # explicit hiding of the overflow: no scrollbars either
        [Size(30, 30), "overflow: hidden", Size(20, 20)],
        # scrollbar for both directions
        [
            Size(30, 30),
            "overflow: auto",
            Size(
                20 - SCROLL_V_SIZE,
                20 - SCROLL_H_SIZE,
            ),
        ],
        # horizontal scrollbar
        [Size(30, 30), "overflow-x: auto", Size(20, 20 - SCROLL_H_SIZE)],
        # vertical scrollbar
        [Size(30, 30), "overflow-y: auto", Size(20 - SCROLL_V_SIZE, 20)],
        # scrollbar for both directions, custom scrollbar size
        [Size(30, 30), ("overflow: auto", "scrollbar-size: 3 5"), Size(20 - 5, 20 - 3)],
        # scrollbar for both directions, custom vertical scrollbar size
        [
            Size(30, 30),
            ("overflow: auto", "scrollbar-size-vertical: 3"),
            Size(20 - 3, 20 - SCROLL_H_SIZE),
        ],
        # scrollbar for both directions, custom horizontal scrollbar size
        [
            Size(30, 30),
            ("overflow: auto", "scrollbar-size-horizontal: 3"),
            Size(20 - SCROLL_V_SIZE, 20 - 3),
        ],
        # scrollbar needed only horizontally, custom scrollbar size
        [
            Size(30, 20),
            ("overflow: auto", "scrollbar-size: 3 3"),
            Size(20, 20 - 3),
        ],
    ),
)
async def test_scrollbar_size_impact_on_the_layout(
    large_widget_size: Size,
    container_style: str | Sequence[str],
    expected_large_widget_visible_region_size: Size,
):
    class LargeWidget(Widget):
        def on_mount(self):
            self.styles.width = large_widget_size[0]
            self.styles.height = large_widget_size[1]

    container_style_rules = (
        [container_style] if isinstance(container_style, str) else container_style
    )

    class LargeWidgetContainer(Widget):
        # TODO: Once textual#581 ("Default versus User CSS") is solved the following CSS should just use the
        #  "LargeWidgetContainer" selector, without having to use a more specific one to be able to override Widget's CSS:
        DEFAULT_CSS = """
        #large-widget-container {
            width: 20;
            height: 20;
            ${container_style};
        }
        """.replace(
            "${container_style}",
            ";\n".join(container_style_rules),
        )

    large_widget = LargeWidget()
    large_widget.expand = False
    container = LargeWidgetContainer(large_widget, id="large-widget-container")

    class MyTestApp(AppTest):
        def compose(self) -> ComposeResult:
            yield container

    app = MyTestApp(size=Size(40, 40), test_name="scrollbar_size_impact_on_the_layout")

    await app.boot_and_shutdown()

    compositor = app.screen._compositor
    widgets_map = compositor.map
    large_widget_visible_region_size = widgets_map[large_widget].visible_region.size
    assert large_widget_visible_region_size == expected_large_widget_visible_region_size