Merge branch 'command-tests'

This commit is contained in:
Florian Bruhin 2015-08-16 22:48:07 +02:00
commit 6656e6aa9b
3 changed files with 195 additions and 39 deletions

View File

@ -0,0 +1,91 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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 <http://www.gnu.org/licenses/>.
"""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, 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'])
class FakeNetworkCache(QAbstractNetworkCache):
def cacheSize(self):
return 0
def data(self, _url):
return None
def insert(self, _dev):
pass
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 cookiejar_and_cache():
"""Fixture providing a fake cookie jar and cache."""
jar = QNetworkCookieJar()
cache = FakeNetworkCache()
objreg.register('cookie-jar', jar)
objreg.register('cache', cache)
yield
objreg.delete('cookie-jar')
objreg.delete('cache')
@pytest.yield_fixture
def objects(qtbot, default_config, key_config_stub, tab_registry,
host_blocker_stub):
"""Fixture providing a CommandDispatcher and a fake TabbedBrowser."""
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)
objreg.register('command-dispatcher', dispatcher, scope='window',
window=win_id)
yield ObjectsRet(tabbed_browser, dispatcher)
def test_openurl(objects):
objects.cd.openurl('localhost')

View File

@ -29,7 +29,7 @@ import pytest
import stubs as stubsmod
import logfail
from qutebrowser.config import configexc
from qutebrowser.config import config
from qutebrowser.utils import objreg, usertypes
@ -168,52 +168,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):
@ -286,3 +276,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)

View File

@ -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:
@ -291,3 +293,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()