File: test_driver.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (127 lines) | stat: -rw-r--r-- 3,899 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
from textual import on
from textual.app import App
from textual.events import Click, MouseDown, MouseUp
from textual.widgets import Button


async def test_driver_mouse_down_up_click():
    """Mouse down and up should issue a click."""

    class MyApp(App):
        messages = []

        @on(Click)
        @on(MouseDown)
        @on(MouseUp)
        def handle(self, event):
            self.messages.append(event)

    app = MyApp()
    async with app.run_test() as pilot:
        app._driver.process_message(MouseDown(None, 0, 0, 0, 0, 1, False, False, False))
        app._driver.process_message(MouseUp(None, 0, 0, 0, 0, 1, False, False, False))
        await pilot.pause()
        assert len(app.messages) == 3
        assert isinstance(app.messages[0], MouseDown)
        assert isinstance(app.messages[1], MouseUp)
        assert isinstance(app.messages[2], Click)


async def test_driver_mouse_down_up_click_widget():
    """Mouse down and up should issue a click when they're on a widget."""

    class MyApp(App):
        messages = []

        def compose(self):
            yield Button()

        def on_button_pressed(self, event):
            self.messages.append(event)

    app = MyApp()
    async with app.run_test() as pilot:
        app._driver.process_message(MouseDown(None, 0, 0, 0, 0, 1, False, False, False))
        app._driver.process_message(MouseUp(None, 0, 0, 0, 0, 1, False, False, False))
        await pilot.pause()
        assert len(app.messages) == 1


async def test_driver_mouse_down_drag_inside_widget_up_click():
    """Mouse down and up should issue a click, even if the mouse moves but remains
    inside the same widget."""

    class MyApp(App):
        messages = []

        def compose(self):
            yield Button()

        def on_button_pressed(self, event):
            self.messages.append(event)

    app = MyApp()
    button_width = 16
    button_height = 3
    async with app.run_test() as pilot:
        # Sanity check
        width, height = app.query_one(Button).region.size
        assert (width, height) == (button_width, button_height)

        # Mouse down on the button, then move the mouse inside the button, then mouse up.
        app._driver.process_message(MouseDown(None, 0, 0, 0, 0, 1, False, False, False))
        app._driver.process_message(
            MouseUp(
                None,
                button_width - 1,
                button_height - 1,
                button_width - 1,
                button_height - 1,
                1,
                False,
                False,
                False,
            )
        )
        await pilot.pause()
        # A click should still be triggered.
        assert len(app.messages) == 1


async def test_driver_mouse_down_drag_outside_widget_up_click():
    """Mouse down and up don't issue a click if the mouse moves outside of the initial widget."""

    class MyApp(App):
        messages = []

        def compose(self):
            yield Button()

        def on_button_pressed(self, event):
            self.messages.append(event)

    app = MyApp()
    button_width = 16
    button_height = 3
    async with app.run_test() as pilot:
        # Sanity check
        width, height = app.query_one(Button).region.size
        assert (width, height) == (button_width, button_height)

        # Mouse down on the button, then move the mouse outside the button, then mouse up.
        app._driver.process_message(MouseDown(None, 0, 0, 0, 0, 1, False, False, False))
        app._driver.process_message(
            MouseUp(
                None,
                button_width + 1,
                button_height + 1,
                button_width + 1,
                button_height + 1,
                1,
                False,
                False,
                False,
            )
        )
        await pilot.pause()
        assert len(app.messages) == 0