Use stubs.FakeWebTab to fake tabs

This also fixes the cur_url access.
This commit is contained in:
Florian Bruhin 2016-07-08 10:09:03 +02:00
parent 09f4c2199e
commit 782561462b
4 changed files with 24 additions and 31 deletions

View File

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

View File

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

View File

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

View File

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