Use qtbot.wait_callback

This commit is contained in:
Florian Bruhin 2018-09-26 11:39:52 +02:00
parent 73cba046e5
commit a27a8ada4d
7 changed files with 39 additions and 63 deletions

View File

@ -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."""

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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]