From bc45aa33e0ac58e8ccd54508b36dc0b463c4c3c3 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 14 Sep 2018 20:18:33 +0200 Subject: [PATCH] Make sure we wait until follow_selected is done --- qutebrowser/browser/browsertab.py | 2 ++ qutebrowser/browser/webengine/webenginetab.py | 6 +++++- qutebrowser/browser/webkit/webkittab.py | 5 +++++ tests/unit/browser/test_caret.py | 10 ++++++---- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index 087834e53..0eb9b6493 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -395,9 +395,11 @@ class AbstractCaret(QObject): Signals: selection_toggled: Emitted when the selection was toggled. arg: Whether the selection is now active. + follow_selected_done: Emitted when a follow_selection action is done. """ selection_toggled = pyqtSignal(bool) + follow_selected_done = pyqtSignal() def __init__(self, tab, mode_manager, parent=None): super().__init__(parent) diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 3eec5669c..78913e92e 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -344,10 +344,13 @@ class WebEngineCaret(browsertab.AbstractCaret): tab: Open in a new tab. """ if js_elem is None: + self.follow_selected_done.emit() return + if js_elem == "focused": # we had a focused element, not a selected one. Just send self._follow_enter(tab) + self.follow_selected_done.emit() return assert isinstance(js_elem, dict), js_elem @@ -365,7 +368,8 @@ class WebEngineCaret(browsertab.AbstractCaret): elem.click(click_type) except webelem.Error as e: message.error(str(e)) - return + + self.follow_selected_done.emit() def follow_selected(self, *, tab=False): if self._tab.search.search_displayed: diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index 7b7ad0c7d..041e058b1 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -369,11 +369,13 @@ class WebKitCaret(browsertab.AbstractCaret): # https://github.com/annulen/webkit/commit/0e75f3272d149bc64899c161f150eb341a2417af # TODO find a way to check if something is focused self._follow_enter(tab) + self.follow_selected_done.emit() return try: selected_element = xml.etree.ElementTree.fromstring( '{}'.format(selection)).find('a') except xml.etree.ElementTree.ParseError: + self.follow_selected_done.emit() raise browsertab.WebTabError('Could not parse selected ' 'element!') @@ -381,6 +383,7 @@ class WebKitCaret(browsertab.AbstractCaret): try: url = selected_element.attrib['href'] except KeyError: + self.follow_selected_done.emit() raise browsertab.WebTabError('Anchor element without ' 'href!') url = self._tab.url().resolved(QUrl(url)) @@ -389,6 +392,8 @@ class WebKitCaret(browsertab.AbstractCaret): else: self._tab.openurl(url) + self.follow_selected_done.emit() + class WebKitZoom(browsertab.AbstractZoom): diff --git a/tests/unit/browser/test_caret.py b/tests/unit/browser/test_caret.py index 2ec81da9c..273fd6881 100644 --- a/tests/unit/browser/test_caret.py +++ b/tests/unit/browser/test_caret.py @@ -331,14 +331,16 @@ class TestSearch: class TestFollowSelected: - def test_follow_selected_without_a_selection(self, caret, selection): - caret.follow_selected() + def test_follow_selected_without_a_selection(self, qtbot, caret, selection): + with qtbot.wait_signal(caret.follow_selected_done): + caret.follow_selected() - def test_follow_selected_with_text(self, caret, selection): + def test_follow_selected_with_text(self, qtbot, caret, selection): caret.move_to_next_word() selection.toggle() caret.move_to_end_of_word() - caret.follow_selected() + with qtbot.wait_signal(caret.follow_selected_done): + caret.follow_selected() @pytest.mark.parametrize('with_js', [True, False]) def test_follow_selected_with_link(self, caret, selection, config_stub,