diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 1877ac3a1..5472a5c2a 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -29,6 +29,7 @@ from PyQt5.QtNetwork import (QNetworkRequest, QAbstractNetworkCache, QNetworkCacheMetaData) from PyQt5.QtWidgets import QCommonStyle, QWidget, QLineEdit +from qutebrowser.browser import tab from qutebrowser.browser.webkit import webview, history from qutebrowser.config import configexc from qutebrowser.utils import usertypes @@ -224,24 +225,40 @@ def fake_qprocess(): return m -class FakeWebView(QWidget): +class FakeWebTabScroller(tab.AbstractScroller): - """Fake WebView which can be added to a tab.""" - - url_text_changed = pyqtSignal(str) - shutting_down = pyqtSignal() - - def __init__(self, url=FakeUrl(), title='', tab_id=0): + def __init__(self, pos_perc): super().__init__() - self.progress = 0 - self.scroll_pos = (-1, -1) - self.load_status = usertypes.LoadStatus.none - self.tab_id = tab_id - self.cur_url = url - self.title = title + self._pos_perc = pos_perc - def url(self): - return self.cur_url + def pos_perc(self): + return self._pos_perc + + +class FakeWebTab(tab.AbstractTab): + + """Fake AbstractTab to use in tests.""" + + def __init__(self, url=FakeUrl(), title='', tab_id=0, *, + scroll_pos_perc=(0, 0)): + super().__init__(win_id=0) + self._title = title + self._url = url + self.scroll = FakeWebTabScroller(scroll_pos_perc) + + @property + def cur_url(self): + return self._url + + def title(self): + return self._title + + def progress(self): + return 0 + + @property + def load_status(self): + return usertypes.LoadStatus.success class FakeSignal: @@ -537,7 +554,7 @@ class TabbedBrowserStub(QObject): return self.tabs[i] def page_title(self, i): - return self.tabs[i].title + return self.tabs[i].title() def on_tab_close_requested(self, idx): del self.tabs[idx] diff --git a/tests/unit/completion/test_models.py b/tests/unit/completion/test_models.py index 064ed9533..608f5cade 100644 --- a/tests/unit/completion/test_models.py +++ b/tests/unit/completion/test_models.py @@ -307,12 +307,12 @@ def test_session_completion(session_manager_stub): def test_tab_completion(stubs, qtbot, app_stub, win_registry, tabbed_browser_stubs): tabbed_browser_stubs[0].tabs = [ - stubs.FakeWebView(QUrl('https://github.com'), 'GitHub', 0), - stubs.FakeWebView(QUrl('https://wikipedia.org'), 'Wikipedia', 1), - stubs.FakeWebView(QUrl('https://duckduckgo.com'), 'DuckDuckGo', 2) + stubs.FakeWebTab(QUrl('https://github.com'), 'GitHub', 0), + stubs.FakeWebTab(QUrl('https://wikipedia.org'), 'Wikipedia', 1), + stubs.FakeWebTab(QUrl('https://duckduckgo.com'), 'DuckDuckGo', 2) ] tabbed_browser_stubs[1].tabs = [ - stubs.FakeWebView(QUrl('https://wiki.archlinux.org'), 'ArchWiki', 0), + stubs.FakeWebTab(QUrl('https://wiki.archlinux.org'), 'ArchWiki', 0), ] actual = _get_completions(miscmodels.TabCompletionModel()) assert actual == [ @@ -331,18 +331,18 @@ def test_tab_completion_delete(stubs, qtbot, app_stub, win_registry, tabbed_browser_stubs): """Verify closing a tab by deleting it from the completion widget.""" tabbed_browser_stubs[0].tabs = [ - stubs.FakeWebView(QUrl('https://github.com'), 'GitHub', 0), - stubs.FakeWebView(QUrl('https://wikipedia.org'), 'Wikipedia', 1), - stubs.FakeWebView(QUrl('https://duckduckgo.com'), 'DuckDuckGo', 2) + stubs.FakeWebTab(QUrl('https://github.com'), 'GitHub', 0), + stubs.FakeWebTab(QUrl('https://wikipedia.org'), 'Wikipedia', 1), + stubs.FakeWebTab(QUrl('https://duckduckgo.com'), 'DuckDuckGo', 2) ] tabbed_browser_stubs[1].tabs = [ - stubs.FakeWebView(QUrl('https://wiki.archlinux.org'), 'ArchWiki', 0), + stubs.FakeWebTab(QUrl('https://wiki.archlinux.org'), 'ArchWiki', 0), ] model = miscmodels.TabCompletionModel() view = _mock_view_index(model, 0, 1, qtbot) qtbot.add_widget(view) model.delete_cur_item(view) - actual = [tab.url() for tab in tabbed_browser_stubs[0].tabs] + actual = [tab.cur_url for tab in tabbed_browser_stubs[0].tabs] assert actual == [QUrl('https://github.com'), QUrl('https://duckduckgo.com')] diff --git a/tests/unit/mainwindow/statusbar/test_percentage.py b/tests/unit/mainwindow/statusbar/test_percentage.py index c40603188..aac4125c3 100644 --- a/tests/unit/mainwindow/statusbar/test_percentage.py +++ b/tests/unit/mainwindow/statusbar/test_percentage.py @@ -20,16 +20,11 @@ """Test Percentage widget.""" -import collections - import pytest from qutebrowser.mainwindow.statusbar.percentage import Percentage -FakeTab = collections.namedtuple('FakeTab', 'scroll_pos') - - @pytest.fixture def percentage(qtbot): """Fixture providing a Percentage widget.""" @@ -57,9 +52,9 @@ def test_percentage_text(percentage, y, expected): assert percentage.text() == expected -def test_tab_change(percentage): +def test_tab_change(percentage, stubs): """Make sure the percentage gets changed correctly when switching tabs.""" percentage.set_perc(x=None, y=10) - tab = FakeTab([0, 20]) + tab = stubs.FakeWebTab(scroll_pos_perc=(0, 20)) percentage.on_tab_changed(tab) assert percentage.text() == '[20%]' diff --git a/tests/unit/mainwindow/test_tabwidget.py b/tests/unit/mainwindow/test_tabwidget.py index e5d8554c7..5d76e2c53 100644 --- a/tests/unit/mainwindow/test_tabwidget.py +++ b/tests/unit/mainwindow/test_tabwidget.py @@ -69,7 +69,7 @@ class TestTabWidget: # Size taken from issue report pixmap = QPixmap(72, 1) icon = QIcon(pixmap) - page = stubs.FakeWebView() - widget.addTab(page, icon, 'foobar') + tab = stubs.FakeWebTab() + widget.addTab(tab, icon, 'foobar') widget.show() qtbot.waitForWindowShown(widget)