From 782561462bb03345bc09164d513f33bc3816642b Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 8 Jul 2016 10:09:03 +0200 Subject: [PATCH] Use stubs.FakeWebTab to fake tabs This also fixes the cur_url access. --- tests/helpers/stubs.py | 14 +++++----- .../mainwindow/statusbar/test_percentage.py | 2 +- .../mainwindow/statusbar/test_progress.py | 26 +++++++++---------- tests/unit/mainwindow/statusbar/test_url.py | 13 +++------- 4 files changed, 24 insertions(+), 31 deletions(-) diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 522dbd209..d0c894e9c 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -242,25 +242,27 @@ class FakeWebTab(tab.AbstractTab): """Fake AbstractTab to use in tests.""" def __init__(self, url=FakeUrl(), title='', tab_id=0, *, - scroll_pos_perc=(0, 0)): + scroll_pos_perc=(0, 0), + load_status=usertypes.LoadStatus.success, + progress=0): super().__init__(win_id=0) + self._load_status = load_status self._title = title self._url = url + self._progress = progress self.scroll = FakeWebTabScroller(scroll_pos_perc) - @property - def cur_url(self): + def url(self): return self._url def title(self): return self._title def progress(self): - return 0 + return self._progress - @property def load_status(self): - return usertypes.LoadStatus.success + return self._load_status class FakeSignal: diff --git a/tests/unit/mainwindow/statusbar/test_percentage.py b/tests/unit/mainwindow/statusbar/test_percentage.py index bec3f06bf..0e1388244 100644 --- a/tests/unit/mainwindow/statusbar/test_percentage.py +++ b/tests/unit/mainwindow/statusbar/test_percentage.py @@ -53,7 +53,7 @@ def test_percentage_text(percentage, y, expected): assert percentage.text() == expected -def test_tab_change(percentage, stubs): +def test_tab_change(percentage, stubs, qapp): """Make sure the percentage gets changed correctly when switching tabs.""" percentage.set_perc(x=None, y=10) tab = stubs.FakeWebTab(scroll_pos_perc=(0, 20)) diff --git a/tests/unit/mainwindow/statusbar/test_progress.py b/tests/unit/mainwindow/statusbar/test_progress.py index 8a12379f3..1cb5ca143 100644 --- a/tests/unit/mainwindow/statusbar/test_progress.py +++ b/tests/unit/mainwindow/statusbar/test_progress.py @@ -27,6 +27,8 @@ import pytest from qutebrowser.mainwindow.statusbar.progress import Progress from qutebrowser.utils import usertypes +from tests.helpers import stubs + @pytest.fixture def progress_widget(qtbot, monkeypatch, config_stub): @@ -55,28 +57,24 @@ def test_load_started(progress_widget): assert progress_widget.isVisible() -# mock tab object -Tab = namedtuple('Tab', 'progress load_status') - - -@pytest.mark.parametrize('tab, expected_visible', [ - (Tab(15, usertypes.LoadStatus.loading), True), - (Tab(100, usertypes.LoadStatus.success), False), - (Tab(100, usertypes.LoadStatus.error), False), - (Tab(100, usertypes.LoadStatus.warn), False), - (Tab(100, usertypes.LoadStatus.none), False), +@pytest.mark.parametrize('progress, load_status, expected_visible', [ + (15, usertypes.LoadStatus.loading, True), + (100, usertypes.LoadStatus.success, False), + (100, usertypes.LoadStatus.error, False), + (100, usertypes.LoadStatus.warn, False), + (100, usertypes.LoadStatus.none, False), ]) -def test_tab_changed(progress_widget, tab, expected_visible): +def test_tab_changed(qapp, stubs, progress_widget, progress, load_status, + expected_visible): """Test that progress widget value and visibility state match expectations. - This uses a dummy Tab object. - Args: progress_widget: Progress widget that will be tested. """ + tab = stubs.FakeWebTab(progress=progress, load_status=load_status) progress_widget.on_tab_changed(tab) actual = progress_widget.value(), progress_widget.isVisible() - expected = tab.progress, expected_visible + expected = tab.progress(), expected_visible assert actual == expected diff --git a/tests/unit/mainwindow/statusbar/test_url.py b/tests/unit/mainwindow/statusbar/test_url.py index d53a97b3d..bc4365b0a 100644 --- a/tests/unit/mainwindow/statusbar/test_url.py +++ b/tests/unit/mainwindow/statusbar/test_url.py @@ -26,13 +26,7 @@ import collections from qutebrowser.utils import usertypes from qutebrowser.mainwindow.statusbar import url - -@pytest.fixture -def tab_widget(): - """Fixture providing a fake tab widget.""" - tab = collections.namedtuple('Tab', 'cur_url load_status') - tab.cur_url = collections.namedtuple('cur_url', 'toDisplayString') - return tab +from PyQt5.QtCore import QUrl @pytest.fixture @@ -128,9 +122,8 @@ def test_on_load_status_changed(url_widget, status, expected): (url.UrlType.error, 'Th1$ i$ n0t @ n0rm@L uRL! P@n1c! <-->'), (url.UrlType.error, None) ]) -def test_on_tab_changed(url_widget, tab_widget, load_status, url_text): - tab_widget.load_status = load_status - tab_widget.cur_url.toDisplayString = lambda: url_text +def test_on_tab_changed(url_widget, stubs, qapp, load_status, url_text): + tab_widget = stubs.FakeWebTab(load_status=load_status, url=QUrl(url_text)) url_widget.on_tab_changed(tab_widget) if url_text is not None: assert url_widget._urltype == load_status