From e5843ffcf62dfd8e07ebe912125da16a17ce26f9 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 25 May 2015 01:31:50 +0200 Subject: [PATCH 1/2] Command tests WIP --- tests/browser/test_commands.py | 99 ++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/browser/test_commands.py diff --git a/tests/browser/test_commands.py b/tests/browser/test_commands.py new file mode 100644 index 000000000..d527298ed --- /dev/null +++ b/tests/browser/test_commands.py @@ -0,0 +1,99 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Tests for qutebrowser.browser.commands.""" + +import collections + +import pytest +from PyQt5.QtGui import QFont, QColor +from PyQt5.QtWidgets import QTabBar, QTabWidget +from PyQt5.QtNetwork import QNetworkCookieJar + +from qutebrowser.browser import commands, cookies +from qutebrowser.mainwindow import tabbedbrowser +from qutebrowser.utils import objreg + + +ObjectsRet = collections.namedtuple('Dispatcher', ['tb', 'cd']) + +FakeWindow = collections.namedtuple('FakeWindow', ['registry']) + + +@pytest.yield_fixture +def win_registry(): + """Fixture providing a window registry for win_id 0.""" + registry = objreg.ObjectRegistry() + window = FakeWindow(registry) + objreg.window_registry[0] = window + yield registry + del objreg.window_registry[0] + + +@pytest.yield_fixture +def tab_registry(win_registry): + """Fixture providing a tab registry for win_id 0.""" + registry = objreg.ObjectRegistry() + objreg.register('tab-registry', registry, scope='window', window=0) + yield registry + objreg.delete('tab-registry', scope='window', window=0) + + +@pytest.yield_fixture(autouse=True) +def ram_cookiejar(): + jar = QNetworkCookieJar() + objreg.register('cookie-jar', jar) + yield jar + objreg.delete('cookie-jar') + + +@pytest.fixture +def objects(qtbot, config_stub, tab_registry): + """Fixture providing a CommandDispatcher and a fake TabbedBrowser.""" + config_stub.data = { + 'general': { + 'auto-search': False, + }, + 'fonts': { + 'tabbar': QFont('Courier'), + }, + 'colors': { + 'tabs.bg.bar': QColor('black'), + }, + 'tabs': { + 'movable': False, + 'position': QTabWidget.North, + 'select-on-remove': QTabBar.SelectRightTab, + 'tabs-are-windows': False, + }, + 'ui': { + 'zoom-levels': [100], + 'default-zoom': 100, + }, + } + win_id = 0 + tabbed_browser = tabbedbrowser.TabbedBrowser(win_id) + qtbot.add_widget(tabbed_browser) + dispatcher = commands.CommandDispatcher(win_id, tabbed_browser) + return ObjectsRet(tabbed_browser, dispatcher) + + +def test_openurl(objects): + objects.cd.openurl('http://www.heise.de') + #objects.tb_mock. From a02055414d7a4ba6ae39c164469f0029fc254053 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 20 Jul 2015 11:23:34 +0200 Subject: [PATCH 2/2] Make command tests work. --- tests/browser/test_commands.py | 82 ++++++++++++++----------------- tests/conftest.py | 88 +++++++++++++++++++--------------- tests/stubs.py | 55 +++++++++++++++++++++ 3 files changed, 141 insertions(+), 84 deletions(-) diff --git a/tests/browser/test_commands.py b/tests/browser/test_commands.py index d527298ed..8c82a5cf3 100644 --- a/tests/browser/test_commands.py +++ b/tests/browser/test_commands.py @@ -24,76 +24,68 @@ import collections import pytest from PyQt5.QtGui import QFont, QColor from PyQt5.QtWidgets import QTabBar, QTabWidget -from PyQt5.QtNetwork import QNetworkCookieJar +from PyQt5.QtNetwork import (QNetworkCookieJar, QAbstractNetworkCache, + QNetworkCacheMetaData) from qutebrowser.browser import commands, cookies from qutebrowser.mainwindow import tabbedbrowser from qutebrowser.utils import objreg +from qutebrowser.keyinput import modeman ObjectsRet = collections.namedtuple('Dispatcher', ['tb', 'cd']) -FakeWindow = collections.namedtuple('FakeWindow', ['registry']) +class FakeNetworkCache(QAbstractNetworkCache): + def cacheSize(self): + return 0 -@pytest.yield_fixture -def win_registry(): - """Fixture providing a window registry for win_id 0.""" - registry = objreg.ObjectRegistry() - window = FakeWindow(registry) - objreg.window_registry[0] = window - yield registry - del objreg.window_registry[0] + def data(self, _url): + return None + def insert(self, _dev): + pass -@pytest.yield_fixture -def tab_registry(win_registry): - """Fixture providing a tab registry for win_id 0.""" - registry = objreg.ObjectRegistry() - objreg.register('tab-registry', registry, scope='window', window=0) - yield registry - objreg.delete('tab-registry', scope='window', window=0) + def metaData(self, _url): + return QNetworkCacheMetaData() + + def prepare(self, _metadata): + return None + + def remove(self, _url): + return False + + def updateMetaData(self, _url): + pass @pytest.yield_fixture(autouse=True) -def ram_cookiejar(): +def cookiejar_and_cache(): + """Fixture providing a fake cookie jar and cache.""" jar = QNetworkCookieJar() + cache = FakeNetworkCache() objreg.register('cookie-jar', jar) - yield jar + objreg.register('cache', cache) + yield objreg.delete('cookie-jar') + objreg.delete('cache') -@pytest.fixture -def objects(qtbot, config_stub, tab_registry): +@pytest.yield_fixture +def objects(qtbot, default_config, key_config_stub, tab_registry, + host_blocker_stub): """Fixture providing a CommandDispatcher and a fake TabbedBrowser.""" - config_stub.data = { - 'general': { - 'auto-search': False, - }, - 'fonts': { - 'tabbar': QFont('Courier'), - }, - 'colors': { - 'tabs.bg.bar': QColor('black'), - }, - 'tabs': { - 'movable': False, - 'position': QTabWidget.North, - 'select-on-remove': QTabBar.SelectRightTab, - 'tabs-are-windows': False, - }, - 'ui': { - 'zoom-levels': [100], - 'default-zoom': 100, - }, - } win_id = 0 + modeman.init(win_id, parent=None) tabbed_browser = tabbedbrowser.TabbedBrowser(win_id) qtbot.add_widget(tabbed_browser) + objreg.register('tabbed-browser', tabbed_browser, scope='window', + window=win_id) dispatcher = commands.CommandDispatcher(win_id, tabbed_browser) - return ObjectsRet(tabbed_browser, dispatcher) + objreg.register('command-dispatcher', dispatcher, scope='window', + window=win_id) + yield ObjectsRet(tabbed_browser, dispatcher) def test_openurl(objects): - objects.cd.openurl('http://www.heise.de') - #objects.tb_mock. + objects.cd.openurl('localhost') diff --git a/tests/conftest.py b/tests/conftest.py index 6673d08dc..d11223970 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -27,7 +27,7 @@ import itertools import pytest import stubs as stubsmod -from qutebrowser.config import configexc +from qutebrowser.config import config from qutebrowser.utils import objreg, usertypes @@ -154,52 +154,42 @@ def cmdline_test(request): return request.param -class ConfigStub: - - """Stub for the config module. - - Attributes: - data: The config data to return. - """ - - def __init__(self, signal): - """Constructor. - - Args: - signal: The signal to use for self.changed. - """ - self.data = {} - self.changed = signal - - def section(self, name): - """Get a section from the config. - - Args: - name: The section name to get. - - Return: - The section as dict. - """ - return self.data[name] - - def get(self, sect, opt): - """Get a value from the config.""" - data = self.data[sect] - try: - return data[opt] - except KeyError: - raise configexc.NoOptionError(opt, sect) - - @pytest.yield_fixture def config_stub(stubs): """Fixture which provides a fake config object.""" - stub = ConfigStub(stubs.FakeSignal()) + stub = stubs.ConfigStub(signal=stubs.FakeSignal()) objreg.register('config', stub) yield stub objreg.delete('config') +@pytest.yield_fixture +def default_config(): + """Fixture that provides and registers an empty default config object.""" + config_obj = config.ConfigManager(configdir=None, fname=None, relaxed=True) + objreg.register('config', config_obj) + yield config_obj + objreg.delete('config') + + +@pytest.yield_fixture +def key_config_stub(stubs): + """Fixture which provides a fake key config object.""" + stub = stubs.KeyConfigStub() + objreg.register('key-config', stub) + yield stub + objreg.delete('key-config') + + +@pytest.yield_fixture +def host_blocker_stub(stubs): + """Fixture which provides a fake host blocker object.""" + stub = stubs.HostBlockerStub() + objreg.register('host-blocker', stub) + yield stub + objreg.delete('host-blocker') + + def pytest_runtest_setup(item): """Add some custom markers.""" if not isinstance(item, item.Function): @@ -272,3 +262,23 @@ class MessageMock: def message_mock(monkeypatch): """Fixture to get a MessageMock.""" return MessageMock(monkeypatch) + + +@pytest.yield_fixture +def win_registry(): + """Fixture providing a window registry for win_id 0.""" + FakeWindow = collections.namedtuple('FakeWindow', ['registry']) + registry = objreg.ObjectRegistry() + window = FakeWindow(registry) + objreg.window_registry[0] = window + yield registry + del objreg.window_registry[0] + + +@pytest.yield_fixture +def tab_registry(win_registry): + """Fixture providing a tab registry for win_id 0.""" + registry = objreg.ObjectRegistry() + objreg.register('tab-registry', registry, scope='window', window=0) + yield registry + objreg.delete('tab-registry', scope='window', window=0) diff --git a/tests/stubs.py b/tests/stubs.py index 4345a42f6..749eea842 100644 --- a/tests/stubs.py +++ b/tests/stubs.py @@ -29,6 +29,8 @@ from PyQt5.QtCore import pyqtSignal, QPoint, QProcess, QObject from PyQt5.QtNetwork import QNetworkRequest from PyQt5.QtWidgets import QCommonStyle +from qutebrowser.config import configexc + class FakeKeyEvent: @@ -274,3 +276,56 @@ class MessageModule: def info(self, _win_id, message, immediately=True): """Log an info message to the message logger.""" logging.getLogger('message').info(message) + + +class ConfigStub: + + """Stub for the config module. + + Attributes: + data: The config data to return. + """ + + def __init__(self, signal): + """Constructor. + + Args: + signal: The signal to use for self.changed. + """ + self.data = {} + self.changed = signal + + def section(self, name): + """Get a section from the config. + + Args: + name: The section name to get. + + Return: + The section as dict. + """ + return self.data[name] + + def get(self, sect, opt): + """Get a value from the config.""" + data = self.data[sect] + try: + return data[opt] + except KeyError: + raise configexc.NoOptionError(opt, sect) + + +class KeyConfigStub: + + """Stub for the key-config object.""" + + def get_bindings_for(self, _section): + return {} + + +class HostBlockerStub: + + """Stub for the host-blocker object.""" + + def __init__(self): + self.blocked_hosts = set()