File: test_base.py

package info (click to toggle)
qtile 0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,004 kB
  • sloc: python: 49,959; ansic: 4,371; xml: 324; sh: 260; makefile: 218
file content (256 lines) | stat: -rw-r--r-- 8,733 bytes parent folder | download
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
import pytest

import libqtile.bar
import libqtile.config
from libqtile.command.base import expose_command
from libqtile.widget import Spacer, TextBox
from libqtile.widget.base import BackgroundPoll, _Widget
from test.helpers import BareConfig, Retry


class TimerWidget(_Widget):
    @expose_command()
    def set_timer1(self):
        self.timer1 = self.timeout_add(10, self.set_timer1)

    @expose_command()
    def cancel_timer1(self):
        self.timer1.cancel()

    @expose_command()
    def set_timer2(self):
        self.timer2 = self.timeout_add(10, self.set_timer2)

    @expose_command()
    def cancel_timer2(self):
        self.timer2.cancel()

    @expose_command()
    def get_active_timers(self):
        active = [x for x in self._futures if getattr(x, "_scheduled", False)]
        return len(active)


class PollingWidget(BackgroundPoll):
    poll_count = 0

    def poll(self):
        self.poll_count += 1
        return f"Poll count: {self.poll_count}"


def test_multiple_timers(minimal_conf_noscreen, manager_nospawn):
    config = minimal_conf_noscreen
    config.screens = [libqtile.config.Screen(top=libqtile.bar.Bar([TimerWidget(10)], 10))]

    # Start manager and check no active timers
    manager_nospawn.start(config)
    assert manager_nospawn.c.widget["timerwidget"].get_active_timers() == 0

    # Start both timers and confirm both are active
    manager_nospawn.c.widget["timerwidget"].set_timer1()
    manager_nospawn.c.widget["timerwidget"].set_timer2()
    assert manager_nospawn.c.widget["timerwidget"].get_active_timers() == 2

    # Cancel timer1
    manager_nospawn.c.widget["timerwidget"].cancel_timer1()
    assert manager_nospawn.c.widget["timerwidget"].get_active_timers() == 1

    # Cancel timer2
    manager_nospawn.c.widget["timerwidget"].cancel_timer2()
    assert manager_nospawn.c.widget["timerwidget"].get_active_timers() == 0

    # Restart both timers
    manager_nospawn.c.widget["timerwidget"].set_timer1()
    manager_nospawn.c.widget["timerwidget"].set_timer2()
    assert manager_nospawn.c.widget["timerwidget"].get_active_timers() == 2

    # Verify that `finalize()` cancels all timers.
    manager_nospawn.c.widget["timerwidget"].eval("self.finalize()")
    assert manager_nospawn.c.widget["timerwidget"].get_active_timers() == 0


def test_mirrors_same_bar(minimal_conf_noscreen, manager_nospawn):
    """Verify that mirror created when widget reused in same bar."""
    config = minimal_conf_noscreen
    tbox = TextBox("Testing Mirrors")
    config.screens = [libqtile.config.Screen(top=libqtile.bar.Bar([tbox, tbox], 10))]

    manager_nospawn.start(config)
    info = manager_nospawn.c.bar["top"].info()["widgets"]

    # First instance is retained, second is replaced by mirror
    assert len(info) == 2
    assert [w["name"] for w in info] == ["textbox", "mirror"]


def test_mirrors_different_bar(minimal_conf_noscreen, manager_nospawn):
    """Verify that mirror created when widget reused in different bar."""
    config = minimal_conf_noscreen
    tbox = TextBox("Testing Mirrors")
    config.fake_screens = [
        libqtile.config.Screen(top=libqtile.bar.Bar([tbox], 10), x=0, y=0, width=400, height=600),
        libqtile.config.Screen(
            top=libqtile.bar.Bar([tbox], 10), x=400, y=0, width=400, height=600
        ),
    ]

    manager_nospawn.start(config)
    screen0 = manager_nospawn.c.screen[0].bar["top"].info()["widgets"]
    screen1 = manager_nospawn.c.screen[1].bar["top"].info()["widgets"]

    # Original widget should be in the first screen
    assert len(screen0) == 1
    assert [w["name"] for w in screen0] == ["textbox"]

    # Widget is replaced with a mirror on the second screen
    assert len(screen1) == 1
    assert [w["name"] for w in screen1] == ["mirror"]


def test_mirrors_stretch(minimal_conf_noscreen, manager_nospawn):
    """Verify that mirror widgets stretch according to their own bar"""
    config = minimal_conf_noscreen
    tbox = TextBox("Testing Mirrors")
    stretch = Spacer()
    config.fake_screens = [
        libqtile.config.Screen(
            top=libqtile.bar.Bar([stretch, tbox], 10), x=0, y=0, width=600, height=600
        ),
        libqtile.config.Screen(
            top=libqtile.bar.Bar([stretch, tbox], 10), x=600, y=0, width=200, height=600
        ),
    ]

    manager_nospawn.start(config)
    screen0 = manager_nospawn.c.screen[0].bar["top"].info()["widgets"]
    screen1 = manager_nospawn.c.screen[1].bar["top"].info()["widgets"]

    # Spacer is the first widget in each bar. This should be stretched according to its own bar
    # so check its length is equal to the bar length minus the length of the text box.
    assert screen0[0]["length"] == 600 - screen0[1]["length"]
    assert screen1[0]["length"] == 200 - screen1[1]["length"]


def test_threadpolltext_force_update(minimal_conf_noscreen, manager_nospawn):
    """Check that widget can be polled instantly via command interface."""
    config = minimal_conf_noscreen
    tpoll = PollingWidget("Not polled")
    config.screens = [libqtile.config.Screen(top=libqtile.bar.Bar([tpoll], 10))]

    manager_nospawn.start(config)
    widget = manager_nospawn.c.widget["pollingwidget"]

    # Widget is polled immediately when configured
    assert widget.info()["text"] == "Poll count: 1"

    # Default update_interval is 600 seconds so the widget won't poll during test unless forced
    widget.force_update()
    assert widget.info()["text"] == "Poll count: 2"


def test_threadpolltext_update_interval_none(minimal_conf_noscreen, manager_nospawn):
    """Check that widget will be polled only once if update_interval == None"""
    config = minimal_conf_noscreen
    tpoll = PollingWidget("Not polled", update_interval=None)
    config.screens = [libqtile.config.Screen(top=libqtile.bar.Bar([tpoll], 10))]

    manager_nospawn.start(config)
    widget = manager_nospawn.c.widget["pollingwidget"]

    # Widget is polled immediately when configured
    assert widget.info()["text"] == "Poll count: 1"


class ScrollingTextConfig(BareConfig):
    screens = [
        libqtile.config.Screen(
            top=libqtile.bar.Bar(
                [
                    TextBox("NoWidth", name="no_width", scroll=True),
                    TextBox("ShortText", name="short_text", width=100, scroll=True),
                    TextBox("Longer text " * 5, name="longer_text", width=100, scroll=True),
                    TextBox(
                        "ShortFixedWidth",
                        name="fixed_width",
                        width=200,
                        scroll=True,
                        scroll_fixed_width=True,
                    ),
                ],
                32,
            )
        )
    ]


scrolling_text_config = pytest.mark.parametrize("manager", [ScrollingTextConfig], indirect=True)


@scrolling_text_config
def test_text_scroll_no_width(manager):
    """
    Scrolling text needs a fixed width. If this is not set a warning is provided and
    scrolling is disabled.
    """
    logs = manager.get_log_buffer()
    assert "WARNING - no_width: You must specify a width when enabling scrolling." in logs
    _, output = manager.c.widget["no_width"].eval("self.scroll")
    assert output == "False"


@scrolling_text_config
def test_text_scroll_short_text(manager):
    """
    When scrolling is enabled, width is a "max_width" setting.
    Shorter text will reslult in widget shrinking.
    """
    widget = manager.c.widget["short_text"]

    # Width is shorter than max width
    assert widget.info()["width"] < 100

    # Scrolling is still enabled (but won't do anything)
    _, output = widget.eval("self.scroll")
    assert output == "True"

    _, output = widget.eval("self._should_scroll")
    assert output == "False"


@scrolling_text_config
def test_text_scroll_long_text(manager):
    """
    Longer text scrolls by incrementing an offset counter.
    """

    @Retry(ignore_exceptions=(AssertionError,))
    def wait_for_scroll(widget):
        _, scroll_count = widget.eval("self._scroll_offset")
        assert int(scroll_count) > 5

    widget = manager.c.widget["longer_text"]

    # Width is fixed at set width
    assert widget.info()["width"] == 100

    # Scrolling is still enabled
    _, output = widget.eval("self.scroll")
    assert output == "True"

    _, output = widget.eval("self._should_scroll")
    assert output == "True"

    # Check actually scrolling
    wait_for_scroll(widget)


@scrolling_text_config
def test_scroll_fixed_width(manager):
    widget = manager.c.widget["fixed_width"]

    _, layout = widget.eval("self.layout.width")
    assert int(layout) < 200

    # Widget width is fixed at set width
    assert widget.info()["width"] == 200