Unit test tab (buffer) completion.

This involved adding a few stubs as well as expanding the FakeWebView.
This commit is contained in:
Ryan Roden-Corrent 2016-06-16 23:12:21 -04:00
parent 610f9b7068
commit 16f043034f
3 changed files with 77 additions and 3 deletions

View File

@ -234,6 +234,24 @@ def session_manager_stub(stubs):
objreg.delete('session-manager')
@pytest.yield_fixture
def tabbed_browser_stub(stubs):
"""Fixture which provides a fake tabbed-browser object."""
stub = stubs.TabbedBrowserStub()
objreg.register('tabbed-browser', stub, scope='window', window=0)
yield stub
objreg.delete('tabbed-browser', scope='window', window=0)
@pytest.yield_fixture
def app_stub(stubs):
"""Fixture which provides a fake app object."""
stub = stubs.ApplicationStub()
objreg.register('app', stub)
yield stub
objreg.delete('app')
@pytest.fixture(scope='session')
def stubs():
"""Provide access to stub objects useful for testing."""

View File

@ -31,6 +31,7 @@ from PyQt5.QtWidgets import QCommonStyle, QWidget
from qutebrowser.browser.webkit import webview
from qutebrowser.config import configexc
from qutebrowser.mainwindow import mainwindow
class FakeNetworkCache(QAbstractNetworkCache):
@ -219,13 +220,20 @@ class FakeWebView(QWidget):
"""Fake WebView which can be added to a tab."""
def __init__(self):
url_text_changed = pyqtSignal(str)
shutting_down = pyqtSignal()
def __init__(self, url=FakeUrl(), title='', tab_id=0):
super().__init__()
self.progress = 0
self.scroll_pos = (-1, -1)
self.load_status = webview.LoadStatus.none
self.tab_id = 0
self.cur_url = FakeUrl()
self.tab_id = tab_id
self.cur_url = url
self.title = title
def url(self):
return self.cur_url
class FakeSignal:
@ -480,3 +488,34 @@ class SessionManagerStub:
def list_sessions(self):
return self.sessions
class TabbedBrowserStub(QObject):
"""Stub for the tabbed-browser object."""
new_tab = pyqtSignal(webview.WebView, int)
def __init__(self, parent=None):
super().__init__(parent)
self.tabs = []
self.shutting_down = False
def count(self):
return len(self.tabs)
def widget(self, i):
return self.tabs[i]
def page_title(self, i):
return self.tabs[i].title
class ApplicationStub(QObject):
"""Stub to insert as the app object in objreg."""
new_window = pyqtSignal(mainwindow.MainWindow)
def __init__(self):
super().__init__()

View File

@ -184,6 +184,23 @@ def test_session_completion(session_manager_stub):
]
def test_tab_completion(stubs, qtbot, app_stub, win_registry,
tabbed_browser_stub):
tabbed_browser_stub.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)
]
actual = _get_completions(miscmodels.TabCompletionModel())
assert actual == [
('0', [
('0/1', 'https://github.com', 'GitHub'),
('0/2', 'https://wikipedia.org', 'Wikipedia'),
('0/3', 'https://duckduckgo.com', 'DuckDuckGo')
])
]
def _get_completions(model):
"""Collect all the completion entries of a model, organized by category.