summaryrefslogtreecommitdiff
path: root/tests/unit/browser/test_caret.py
blob: 86014040db1512214f313b4ee59bef5ee844ffd0 (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2018-2021 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser.  If not, see <https://www.gnu.org/licenses/>.

"""Tests for caret browsing mode."""

import textwrap

import pytest
from PyQt5.QtCore import QUrl

from qutebrowser.utils import usertypes
from qutebrowser.browser import browsertab


@pytest.fixture
def caret(web_tab, qtbot, mode_manager):
    web_tab.container.expose()

    with qtbot.wait_signal(web_tab.load_finished, timeout=10000):
        web_tab.load_url(QUrl('qute://testdata/data/caret.html'))

    with qtbot.wait_signal(web_tab.caret.selection_toggled):
        mode_manager.enter(usertypes.KeyMode.caret)

    return web_tab.caret


class Selection:

    """Helper to interact with the caret selection."""

    def __init__(self, qtbot, caret):
        self._qtbot = qtbot
        self._caret = caret

    def check(self, expected, *, strip=False):
        """Check whether we got the expected selection.

        Since (especially on Windows) the selection is empty if we're checking
        too quickly, we try to read it multiple times.
        """
        for _ in range(10):
            with self._qtbot.wait_callback() as callback:
                self._caret.selection(callback)

            selection = callback.args[0]
            if selection:
                if strip:
                    selection = selection.strip()
                assert selection == expected
                return
            elif not selection and not expected:
                return

            self._qtbot.wait(50)

        pytest.fail('Failed to get selection!')

    def check_multiline(self, expected, *, strip=False):
        self.check(textwrap.dedent(expected).strip(), strip=strip)

    def toggle(self, *, line=False):
        """Toggle the selection and return the new selection state."""
        with self._qtbot.wait_signal(self._caret.selection_toggled) as blocker:
            self._caret.toggle_selection(line=line)
        return blocker.args[0]


@pytest.fixture
def selection(qtbot, caret):
    return Selection(qtbot, caret)


def test_toggle(caret, selection, qtbot):
    """Make sure calling toggleSelection produces the correct callback values.

    This also makes sure that the SelectionState enum in JS lines up with the
    Python browsertab.SelectionState enum.
    """
    assert selection.toggle() == browsertab.SelectionState.normal
    assert selection.toggle(line=True) == browsertab.SelectionState.line
    assert selection.toggle() == browsertab.SelectionState.normal
    assert selection.toggle() == browsertab.SelectionState.none


def test_selection_callback_wrong_mode(qtbot, caplog,
                                       webengine_tab, mode_manager):
    """Test what calling the selection callback outside of caret mode.

    It should be ignored, as something could have left caret mode while the
    async callback was happening, so we don't want to mess with the status bar.
    """
    assert mode_manager.mode == usertypes.KeyMode.normal
    with qtbot.assert_not_emitted(webengine_tab.caret.selection_toggled):
        webengine_tab.caret._toggle_sel_translate('normal')

    msg = 'Ignoring caret selection callback in KeyMode.normal'
    assert caplog.messages == [msg]


class TestDocument:

    def test_selecting_entire_document(self, caret, selection):
        selection.toggle()
        caret.move_to_end_of_document()
        selection.check_multiline("""
            one two three
            eins zwei drei

            four five six
            vier fünf sechs
        """, strip=True)

    def test_moving_to_end_and_start(self, caret, selection):
        caret.move_to_end_of_document()
        caret.move_to_start_of_document()
        selection.toggle()
        caret.move_to_end_of_word()
        selection.check("one")

    def test_moving_to_end_and_start_with_selection(self, caret, selection):
        caret.move_to_end_of_document()
        selection.toggle()
        caret.move_to_start_of_document()
        selection.check_multiline("""
            one two three
            eins zwei drei

            four five six
            vier fünf sechs
        """, strip=True)


class TestBlock:

    def test_selecting_block(self, caret, selection):
        selection.toggle()
        caret.move_to_end_of_next_block()
        selection.check_multiline("""
            one two three
            eins zwei drei
        """)

    def test_moving_back_to_the_end_of_prev_block_with_sel(self, caret, selection):
        caret.move_to_end_of_next_block(2)
        selection.toggle()
        caret.move_to_end_of_prev_block()
        caret.move_to_prev_word()
        selection.check_multiline("""
            drei

            four five six
        """)

    def test_moving_back_to_the_end_of_prev_block(self, caret, selection):
        caret.move_to_end_of_next_block(2)
        caret.move_to_end_of_prev_block()
        selection.toggle()
        caret.move_to_prev_word()
        selection.check("drei")

    def test_moving_back_to_the_start_of_prev_block_with_sel(self, caret, selection):
        caret.move_to_end_of_next_block(2)
        selection.toggle()
        caret.move_to_start_of_prev_block()
        selection.check_multiline("""
            eins zwei drei

            four five six
        """)

    def test_moving_back_to_the_start_of_prev_block(self, caret, selection):
        caret.move_to_end_of_next_block(2)
        caret.move_to_start_of_prev_block()
        selection.toggle()
        caret.move_to_next_word()
        selection.check("eins ")

    def test_moving_to_the_start_of_next_block_with_sel(self, caret, selection):
        selection.toggle()
        caret.move_to_start_of_next_block()
        selection.check("one two three\n")

    def test_moving_to_the_start_of_next_block(self, caret, selection):
        caret.move_to_start_of_next_block()
        selection.toggle()
        caret.move_to_end_of_word()
        selection.check("eins")


class TestLine:

    def test_selecting_a_line(self, caret, selection):
        selection.toggle()
        caret.move_to_end_of_line()
        selection.check("one two three")

    def test_moving_and_selecting_a_line(self, caret, selection):
        caret.move_to_next_line()
        selection.toggle()
        caret.move_to_end_of_line()
        selection.check("eins zwei drei")

    def test_selecting_next_line(self, caret, selection):
        selection.toggle()
        caret.move_to_next_line()
        selection.check("one two three\n")

    def test_moving_to_end_and_to_start_of_line(self, caret, selection):
        caret.move_to_end_of_line()
        caret.move_to_start_of_line()
        selection.toggle()
        caret.move_to_end_of_word()
        selection.check("one")

    def test_selecting_a_line_backwards(self, caret, selection):
        caret.move_to_end_of_line()
        selection.toggle()
        caret.move_to_start_of_line()
        selection.check("one two three")

    def test_selecting_previous_line(self, caret, selection):
        caret.move_to_next_line()
        selection.toggle()
        caret.move_to_prev_line()
        selection.check("one two three\n")

    def test_moving_to_previous_line(self, caret, selection):
        caret.move_to_next_line()
        caret.move_to_prev_line()
        selection.toggle()
        caret.move_to_next_line()
        selection.check("one two three\n")


class TestWord:

    def test_selecting_a_word(self, caret, selection):
        selection.toggle()
        caret.move_to_end_of_word()
        selection.check("one")

    def test_moving_to_end_and_selecting_a_word(self, caret, selection):
        caret.move_to_end_of_word()
        selection.toggle()
        caret.move_to_end_of_word()
        selection.check(" two")

    def test_moving_to_next_word_and_selecting_a_word(self, caret, selection):
        caret.move_to_next_word()
        selection.toggle()
        caret.move_to_end_of_word()
        selection.check("two")

    def test_moving_to_next_word_and_selecting_until_next_word(self, caret, selection):
        caret.move_to_next_word()
        selection.toggle()
        caret.move_to_next_word()
        selection.check("two ")

    def test_moving_to_previous_word_and_selecting_a_word(self, caret, selection):
        caret.move_to_end_of_word()
        selection.toggle()
        caret.move_to_prev_word()
        selection.check("one")

    def test_moving_to_previous_word(self, caret, selection):
        caret.move_to_end_of_word()
        caret.move_to_prev_word()
        selection.toggle()
        caret.move_to_end_of_word()
        selection.check("one")


class TestChar:

    def test_selecting_a_char(self, caret, selection):
        selection.toggle()
        caret.move_to_next_char()
        selection.check("o")

    def test_moving_and_selecting_a_char(self, caret, selection):
        caret.move_to_next_char()
        selection.toggle()
        caret.move_to_next_char()
        selection.check("n")

    def test_selecting_previous_char(self, caret, selection):
        caret.move_to_end_of_word()
        selection.toggle()
        caret.move_to_prev_char()
        selection.check("e")

    def test_moving_to_previous_char(self, caret, selection):
        caret.move_to_end_of_word()
        caret.move_to_prev_char()
        selection.toggle()
        caret.move_to_end_of_word()
        selection.check("e")


def test_drop_selection(caret, selection):
    selection.toggle()
    caret.move_to_end_of_word()
    caret.drop_selection()
    selection.check("")


class TestSearch:

    @pytest.mark.no_xvfb
    def test_yanking_a_searched_line(self, caret, selection, mode_manager, web_tab, qtbot):
        mode_manager.leave(usertypes.KeyMode.caret)

        with qtbot.wait_callback() as callback:
            web_tab.search.search('fiv', result_cb=callback)
        callback.assert_called_with(True)

        mode_manager.enter(usertypes.KeyMode.caret)
        caret.move_to_end_of_line()
        selection.check('five six')

    @pytest.mark.no_xvfb
    def test_yanking_a_searched_line_with_multiple_matches(self, caret, selection, mode_manager, web_tab, qtbot):
        mode_manager.leave(usertypes.KeyMode.caret)

        with qtbot.wait_callback() as callback:
            web_tab.search.search('w', result_cb=callback)
        callback.assert_called_with(True)

        with qtbot.wait_callback() as callback:
            web_tab.search.next_result(result_cb=callback)
        callback.assert_called_with(True)

        mode_manager.enter(usertypes.KeyMode.caret)

        caret.move_to_end_of_line()
        selection.check('wei drei')


class TestFollowSelected:

    LOAD_STARTED_DELAY = 50

    @pytest.fixture(params=[True, False], autouse=True)
    def toggle_js(self, request, config_stub):
        config_stub.val.content.javascript.enabled = request.param

    def test_follow_selected_without_a_selection(self, qtbot, caret, selection, web_tab,
                                                 mode_manager):
        caret.move_to_next_word()  # Move cursor away from the link
        mode_manager.leave(usertypes.KeyMode.caret)
        with qtbot.wait_signal(caret.follow_selected_done):
            with qtbot.assert_not_emitted(web_tab.load_started,
                                          wait=self.LOAD_STARTED_DELAY):
                caret.follow_selected()

    def test_follow_selected_with_text(self, qtbot, caret, selection, web_tab):
        caret.move_to_next_word()
        selection.toggle()
        caret.move_to_end_of_word()
        with qtbot.wait_signal(caret.follow_selected_done):
            with qtbot.assert_not_emitted(web_tab.load_started,
                                          wait=self.LOAD_STARTED_DELAY):
                caret.follow_selected()

    def test_follow_selected_with_link(self, caret, selection, config_stub,
                                       qtbot, web_tab):
        selection.toggle()
        caret.move_to_end_of_word()
        with qtbot.wait_signal(web_tab.load_finished):
            with qtbot.wait_signal(caret.follow_selected_done):
                caret.follow_selected()
        assert web_tab.url().path() == '/data/hello.txt'


class TestReverse:

    def test_does_not_change_selection(self, caret, selection):
        selection.toggle()
        caret.reverse_selection()
        selection.check("")

    def test_repetition_of_movement_results_in_empty_selection(self, caret, selection):
        selection.toggle()
        caret.move_to_end_of_word()
        caret.reverse_selection()
        caret.move_to_end_of_word()
        selection.check("")

    def test_reverse(self, caret, selection):
        selection.toggle()
        caret.move_to_end_of_word()
        caret.reverse_selection()
        caret.move_to_next_char()
        selection.check("ne")
        caret.reverse_selection()
        caret.move_to_next_char()
        selection.check("ne ")
        caret.move_to_end_of_line()
        selection.check("ne two three")
        caret.reverse_selection()
        caret.move_to_start_of_line()
        selection.check("one two three")


class TestLineSelection:

    def test_toggle(self, caret, selection):
        selection.toggle(line=True)
        selection.check("one two three")

    def test_toggle_untoggle(self, caret, selection):
        selection.toggle()
        selection.check("")
        selection.toggle(line=True)
        selection.check("one two three")
        selection.toggle()
        selection.check("one two three")

    def test_from_center(self, caret, selection):
        caret.move_to_next_char(4)
        selection.toggle(line=True)
        selection.check("one two three")

    def test_more_lines(self, caret, selection):
        selection.toggle(line=True)
        caret.move_to_next_line(2)
        selection.check_multiline("""
            one two three
            eins zwei drei

            four five six
        """, strip=True)

    def test_not_selecting_char(self, caret, selection):
        selection.toggle(line=True)
        caret.move_to_next_char()
        selection.check("one two three")
        caret.move_to_prev_char()
        selection.check("one two three")

    def test_selecting_prev_next_word(self, caret, selection):
        selection.toggle(line=True)
        caret.move_to_next_word()
        selection.check("one two three")
        caret.move_to_prev_word()
        selection.check("one two three")

    def test_selecting_end_word(self, caret, selection):
        selection.toggle(line=True)
        caret.move_to_end_of_word()
        selection.check("one two three")

    def test_selecting_prev_next_line(self, caret, selection):
        selection.toggle(line=True)
        caret.move_to_next_line()
        selection.check_multiline("""
            one two three
            eins zwei drei
        """, strip=True)
        caret.move_to_prev_line()
        selection.check("one two three")

    def test_not_selecting_start_end_line(self, caret, selection):
        selection.toggle(line=True)
        caret.move_to_end_of_line()
        selection.check("one two three")
        caret.move_to_start_of_line()
        selection.check("one two three")

    def test_selecting_block(self, caret, selection):
        selection.toggle(line=True)
        caret.move_to_end_of_next_block()
        selection.check_multiline("""
            one two three
            eins zwei drei
        """, strip=True)

    @pytest.mark.not_mac(
        reason='https://github.com/qutebrowser/qutebrowser/issues/5459')
    def test_selecting_start_end_document(self, caret, selection):
        selection.toggle(line=True)
        caret.move_to_end_of_document()
        selection.check_multiline("""
            one two three
            eins zwei drei

            four five six
            vier fünf sechs
        """, strip=True)

        caret.move_to_start_of_document()
        selection.check("one two three")