From 16f043034faa65ee55e781caf899bc0dce1e170d Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Thu, 16 Jun 2016 23:12:21 -0400 Subject: [PATCH] Unit test tab (buffer) completion. This involved adding a few stubs as well as expanding the FakeWebView. --- tests/helpers/fixtures.py | 18 +++++++++++ tests/helpers/stubs.py | 45 ++++++++++++++++++++++++++-- tests/unit/completion/test_models.py | 17 +++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 0fcec6ab2..819dda15d 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -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.""" diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 2d27406d4..f338d64ec 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -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__() diff --git a/tests/unit/completion/test_models.py b/tests/unit/completion/test_models.py index 6d0de25be..07b8dcc52 100644 --- a/tests/unit/completion/test_models.py +++ b/tests/unit/completion/test_models.py @@ -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.