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
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2015-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 browser.signalfilter."""
import logging
import dataclasses
import pytest
from qutebrowser.qt.core import pyqtSignal, pyqtSlot, QObject
from qutebrowser.browser import signalfilter
class Signaller(QObject):
signal = pyqtSignal(str)
link_hovered = pyqtSignal(str)
filtered_signal = pyqtSignal(str)
cur_link_hovered = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.filtered_signal_arg = None
self.filtered_signal.connect(self.filtered_signal_slot)
@pyqtSlot(str)
def filtered_signal_slot(self, s):
self.filtered_signal_arg = s
@dataclasses.dataclass
class Objects:
signal_filter: signalfilter.SignalFilter
signaller: Signaller
@pytest.fixture
def objects():
signal_filter = signalfilter.SignalFilter(0)
tab = None
signaller = Signaller()
signaller.signal.connect(
signal_filter.create(signaller.filtered_signal, tab))
signaller.link_hovered.connect(
signal_filter.create(signaller.cur_link_hovered, tab))
return Objects(signal_filter=signal_filter, signaller=signaller)
@pytest.mark.parametrize('index_of, emitted', [(0, True), (1, False)])
def test_filtering(objects, tabbed_browser_stubs, index_of, emitted):
browser = tabbed_browser_stubs[0]
browser.widget.current_index = 0
browser.widget.index_of = index_of
objects.signaller.signal.emit('foo')
if emitted:
assert objects.signaller.filtered_signal_arg == 'foo'
else:
assert objects.signaller.filtered_signal_arg is None
@pytest.mark.parametrize('index_of, verb', [(0, 'emitting'), (1, 'ignoring')])
def test_logging(caplog, objects, tabbed_browser_stubs, index_of, verb):
browser = tabbed_browser_stubs[0]
browser.widget.current_index = 0
browser.widget.index_of = index_of
with caplog.at_level(logging.DEBUG, logger='signals'):
objects.signaller.signal.emit('foo')
expected_msg = "{}: filtered_signal('foo') (tab {})".format(verb, index_of)
assert caplog.messages == [expected_msg]
@pytest.mark.parametrize('index_of', [0, 1])
def test_no_logging(caplog, objects, tabbed_browser_stubs, index_of):
browser = tabbed_browser_stubs[0]
browser.widget.current_index = 0
browser.widget.index_of = index_of
with caplog.at_level(logging.DEBUG, logger='signals'):
objects.signaller.link_hovered.emit('foo')
assert not caplog.records
def test_runtime_error(objects, tabbed_browser_stubs):
"""Test that there's no crash if indexOf() raises RuntimeError."""
browser = tabbed_browser_stubs[0]
browser.widget.current_index = 0
browser.widget.index_of = RuntimeError
objects.signaller.signal.emit('foo')
assert objects.signaller.filtered_signal_arg is None
|