Make command tests work.

This commit is contained in:
Florian Bruhin 2015-07-20 11:23:34 +02:00
parent e5843ffcf6
commit a02055414d
3 changed files with 141 additions and 84 deletions

View File

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

View File

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

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