summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2018-09-26 11:39:52 +0200
committerFlorian Bruhin <me@the-compiler.org>2018-09-26 11:45:45 +0200
commita27a8ada4d19844b23d11ea02d4ff2aeb857cf70 (patch)
tree4e54cacef70d6a0f43169521d530137402982bd6
parent73cba046e5c54c38fcff4682f181b6b7577f2968 (diff)
downloadqutebrowser-a27a8ada4d19844b23d11ea02d4ff2aeb857cf70.tar.gz
qutebrowser-a27a8ada4d19844b23d11ea02d4ff2aeb857cf70.zip
Use qtbot.wait_callback
-rw-r--r--tests/helpers/fixtures.py5
-rw-r--r--tests/helpers/utils.py26
-rw-r--r--tests/unit/browser/test_caret.py33
-rw-r--r--tests/unit/javascript/conftest.py6
-rw-r--r--tests/unit/javascript/position_caret/test_position_caret.py15
-rw-r--r--tests/unit/javascript/test_greasemonkey.py8
-rw-r--r--tests/unit/javascript/test_js_execution.py9
7 files changed, 39 insertions, 63 deletions
diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py
index 5332d1433..f10c9213f 100644
--- a/tests/helpers/fixtures.py
+++ b/tests/helpers/fixtures.py
@@ -84,11 +84,6 @@ class WinRegistryHelper:
del objreg.window_registry[win_id]
-@pytest.fixture
-def callback_checker(qtbot):
- return helpers.utils.CallbackChecker(qtbot)
-
-
class FakeStatusBar(QWidget):
"""Fake statusbar to test progressbar sizing."""
diff --git a/tests/helpers/utils.py b/tests/helpers/utils.py
index 66b3f8133..f5350dfee 100644
--- a/tests/helpers/utils.py
+++ b/tests/helpers/utils.py
@@ -182,29 +182,3 @@ def abs_datapath():
@contextlib.contextmanager
def nop_contextmanager():
yield
-
-
-class CallbackChecker(QObject):
-
- """Check if a value provided by a callback is the expected one."""
-
- got_result = pyqtSignal(object)
- UNSET = object()
-
- def __init__(self, qtbot, parent=None):
- super().__init__(parent)
- self._qtbot = qtbot
- self._result = self.UNSET
-
- def callback(self, result):
- """Callback which can be passed to runJavaScript."""
- self._result = result
- self.got_result.emit(result)
-
- def check(self, expected):
- """Wait until the JS result arrived and compare it."""
- __tracebackhide__ = True
- if self._result is self.UNSET:
- with self._qtbot.waitSignal(self.got_result, timeout=2000):
- pass
- assert self._result == expected
diff --git a/tests/unit/browser/test_caret.py b/tests/unit/browser/test_caret.py
index 97c614f17..da73ca162 100644
--- a/tests/unit/browser/test_caret.py
+++ b/tests/unit/browser/test_caret.py
@@ -45,7 +45,6 @@ class Selection:
def __init__(self, qtbot, caret):
self._qtbot = qtbot
self._caret = caret
- self._callback_checker = utils.CallbackChecker(qtbot)
def check(self, expected, *, strip=False):
"""Check whether we got the expected selection.
@@ -54,11 +53,10 @@ class Selection:
too quickly, we try to read it multiple times.
"""
for _ in range(10):
- with self._qtbot.wait_signal(
- self._callback_checker.got_result) as blocker:
- self._caret.selection(self._callback_checker.callback)
+ with self._qtbot.wait_callback() as callback:
+ self._caret.selection(callback)
- selection = blocker.args[0]
+ selection = callback.args[0]
if selection:
if strip:
selection = selection.strip()
@@ -76,7 +74,7 @@ class Selection:
@pytest.fixture
-def selection(qtbot, caret, callback_checker):
+def selection(qtbot, caret):
return Selection(qtbot, caret)
@@ -294,12 +292,13 @@ class TestSearch:
@pytest.mark.qtbug60673
@pytest.mark.no_xvfb
- def test_yanking_a_searched_line(self, caret, selection, mode_manager, callback_checker, web_tab, qtbot):
+ def test_yanking_a_searched_line(self, caret, selection, mode_manager, web_tab, qtbot):
web_tab.show()
mode_manager.leave(usertypes.KeyMode.caret)
- web_tab.search.search('fiv', result_cb=callback_checker.callback)
- callback_checker.check(True)
+ with qtbot.wait_callback() as callback:
+ web_tab.search.search('fiv', result_cb=callback)
+ assert callback.args == [True]
mode_manager.enter(usertypes.KeyMode.caret)
caret.move_to_end_of_line()
@@ -307,15 +306,17 @@ class TestSearch:
@pytest.mark.qtbug60673
@pytest.mark.no_xvfb
- def test_yanking_a_searched_line_with_multiple_matches(self, caret, selection, mode_manager, callback_checker, web_tab, qtbot):
+ def test_yanking_a_searched_line_with_multiple_matches(self, caret, selection, mode_manager, web_tab, qtbot):
web_tab.show()
mode_manager.leave(usertypes.KeyMode.caret)
- web_tab.search.search('w', result_cb=callback_checker.callback)
- callback_checker.check(True)
+ with qtbot.wait_callback() as callback:
+ web_tab.search.search('w', result_cb=callback)
+ assert callback.args == [True]
- web_tab.search.next_result(result_cb=callback_checker.callback)
- callback_checker.check(True)
+ with qtbot.wait_callback() as callback:
+ web_tab.search.next_result(result_cb=callback)
+ assert callback.args == [True]
mode_manager.enter(usertypes.KeyMode.caret)
@@ -337,7 +338,7 @@ class TestFollowSelected:
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):
+ wait=self.LOAD_STARTED_DELAY):
caret.follow_selected()
def test_follow_selected_with_text(self, qtbot, caret, selection, web_tab):
@@ -346,7 +347,7 @@ class TestFollowSelected:
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):
+ wait=self.LOAD_STARTED_DELAY):
caret.follow_selected()
def test_follow_selected_with_link(self, caret, selection, config_stub,
diff --git a/tests/unit/javascript/conftest.py b/tests/unit/javascript/conftest.py
index 89db706e5..b2a5924da 100644
--- a/tests/unit/javascript/conftest.py
+++ b/tests/unit/javascript/conftest.py
@@ -110,9 +110,9 @@ class JSTester:
expected: The value expected return from the javascript execution
world: The scope the javascript will run in
"""
- callback_checker = helpers.utils.CallbackChecker(self.qtbot)
- self.tab.run_js_async(source, callback_checker.callback, world=world)
- callback_checker.check(expected)
+ with self.qtbot.wait_callback() as callback:
+ self.tab.run_js_async(source, callback, world=world)
+ assert callback.args == [expected]
@pytest.fixture
diff --git a/tests/unit/javascript/position_caret/test_position_caret.py b/tests/unit/javascript/position_caret/test_position_caret.py
index 34b92dc66..eb5a05ad4 100644
--- a/tests/unit/javascript/position_caret/test_position_caret.py
+++ b/tests/unit/javascript/position_caret/test_position_caret.py
@@ -43,10 +43,12 @@ class CaretTester:
Attributes:
js: The js_tester fixture.
+ _qtbot: The qtbot fixture.
"""
- def __init__(self, js_tester):
+ def __init__(self, js_tester, qtbot):
self.js = js_tester
+ self._qtbot = qtbot
def check(self):
"""Check whether the caret is before the MARKER text."""
@@ -54,10 +56,9 @@ class CaretTester:
self.js.tab.caret.toggle_selection()
self.js.tab.caret.move_to_next_word()
- callback_checker = helpers.utils.CallbackChecker(self.js.qtbot)
- self.js.tab.caret.selection(lambda text:
- callback_checker.callback(text.rstrip()))
- callback_checker.check('MARKER')
+ with self._qtbot.wait_callback() as callback:
+ self.js.tab.caret.selection(lambda text: callback(text.rstrip()))
+ assert callback.args == ['MARKER']
def check_scrolled(self):
"""Check if the page is scrolled down."""
@@ -65,9 +66,9 @@ class CaretTester:
@pytest.fixture
-def caret_tester(js_tester_webkit):
+def caret_tester(js_tester_webkit, qtbot):
"""Helper fixture to test caret browsing positions."""
- caret_tester = CaretTester(js_tester_webkit)
+ caret_tester = CaretTester(js_tester_webkit, qtbot)
# Showing webview here is necessary for test_scrolled_down_img to
# succeed in some cases, see #1988
caret_tester.js.tab.show()
diff --git a/tests/unit/javascript/test_greasemonkey.py b/tests/unit/javascript/test_greasemonkey.py
index 79983a5c9..9870352d4 100644
--- a/tests/unit/javascript/test_greasemonkey.py
+++ b/tests/unit/javascript/test_greasemonkey.py
@@ -234,11 +234,13 @@ class TestWindowIsolation:
"global", "global"]
return ret
- def test_webengine(self, callback_checker, webengineview, setup):
+ def test_webengine(self, qtbot, webengineview, setup):
page = webengineview.page()
page.runJavaScript(setup.setup_script)
- page.runJavaScript(setup.test_script, callback_checker.callback)
- callback_checker.check(setup.expected)
+
+ with qtbot.wait_callback() as callback:
+ page.runJavaScript(setup.test_script, callback)
+ assert callback.args == [setup.expected]
# The JSCore in 602.1 doesn't fully support Proxy.
@pytest.mark.qtwebkit6021_skip
diff --git a/tests/unit/javascript/test_js_execution.py b/tests/unit/javascript/test_js_execution.py
index 81f999962..4d47e62c5 100644
--- a/tests/unit/javascript/test_js_execution.py
+++ b/tests/unit/javascript/test_js_execution.py
@@ -58,7 +58,7 @@ def test_element_js_webkit(webview, js_enabled, expected):
(True, 2, 2.0),
(False, 2, 2.0),
])
-def test_simple_js_webengine(callback_checker, webengineview, qapp,
+def test_simple_js_webengine(qtbot, webengineview, qapp,
js_enabled, world, expected):
"""With QtWebEngine, runJavaScript works even when JS is off."""
# If we get there (because of the webengineview fixture) we can be certain
@@ -74,5 +74,8 @@ def test_simple_js_webengine(callback_checker, webengineview, qapp,
qapp.processEvents()
page = webengineview.page()
- page.runJavaScript('1 + 1', world, callback_checker.callback)
- callback_checker.check(expected)
+
+ with qtbot.wait_callback() as callback:
+ page.runJavaScript('1 + 1', world, callback)
+
+ assert callback.args == [expected]