tests: Use FakeWebTab for stubbing

This commit is contained in:
Florian Bruhin 2016-07-05 17:46:08 +02:00
parent 5107a87291
commit 4e5a7a891e
4 changed files with 46 additions and 34 deletions

View File

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

View File

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

View File

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

View File

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