2014-06-23 19:44:21 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2019-02-23 06:34:17 +01:00
|
|
|
# Copyright 2014-2019 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2014-06-23 19:44:21 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2016-07-05 20:11:42 +02:00
|
|
|
# pylint: disable=invalid-name,abstract-method
|
2014-06-23 19:44:21 +02:00
|
|
|
|
|
|
|
"""Fake objects/stubs."""
|
2015-04-05 20:30:31 +02:00
|
|
|
|
2014-08-26 19:10:14 +02:00
|
|
|
from unittest import mock
|
2018-03-03 01:00:27 +01:00
|
|
|
import contextlib
|
|
|
|
import shutil
|
2014-06-23 19:44:21 +02:00
|
|
|
|
2017-09-19 22:18:02 +02:00
|
|
|
import attr
|
2017-12-11 07:10:17 +01:00
|
|
|
from PyQt5.QtCore import pyqtSignal, QPoint, QProcess, QObject, QUrl
|
2018-03-28 09:33:27 +02:00
|
|
|
from PyQt5.QtGui import QIcon
|
2015-08-30 23:11:23 +02:00
|
|
|
from PyQt5.QtNetwork import (QNetworkRequest, QAbstractNetworkCache,
|
|
|
|
QNetworkCacheMetaData)
|
2016-11-30 13:35:08 +01:00
|
|
|
from PyQt5.QtWidgets import QCommonStyle, QLineEdit, QWidget, QTabBar
|
2014-06-23 19:44:21 +02:00
|
|
|
|
2018-03-03 01:00:27 +01:00
|
|
|
from qutebrowser.browser import browsertab, downloads
|
2017-08-08 16:55:03 +02:00
|
|
|
from qutebrowser.utils import usertypes
|
2016-06-17 05:12:21 +02:00
|
|
|
from qutebrowser.mainwindow import mainwindow
|
2015-07-20 11:23:34 +02:00
|
|
|
|
2014-06-23 19:44:21 +02:00
|
|
|
|
2015-08-30 23:11:23 +02:00
|
|
|
class FakeNetworkCache(QAbstractNetworkCache):
|
|
|
|
|
2015-08-31 08:00:21 +02:00
|
|
|
"""Fake cache with no data."""
|
|
|
|
|
2015-08-30 23:11:23 +02:00
|
|
|
def cacheSize(self):
|
|
|
|
return 0
|
|
|
|
|
2018-08-19 10:49:46 +02:00
|
|
|
def data(self, _url):
|
2015-08-30 23:11:23 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def insert(self, _dev):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def metaData(self, _url):
|
|
|
|
return QNetworkCacheMetaData()
|
|
|
|
|
2018-08-19 10:49:46 +02:00
|
|
|
def prepare(self, _metadata):
|
2015-08-30 23:11:23 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def remove(self, _url):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def updateMetaData(self, _url):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-06-23 19:44:21 +02:00
|
|
|
class FakeKeyEvent:
|
|
|
|
|
|
|
|
"""Fake QKeyPressEvent stub."""
|
|
|
|
|
|
|
|
def __init__(self, key, modifiers=0, text=''):
|
2014-08-26 19:10:14 +02:00
|
|
|
self.key = mock.Mock(return_value=key)
|
|
|
|
self.text = mock.Mock(return_value=text)
|
|
|
|
self.modifiers = mock.Mock(return_value=modifiers)
|
2014-06-23 19:44:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FakeWebFrame:
|
|
|
|
|
2016-09-07 11:24:28 +02:00
|
|
|
"""A stub for QWebFrame."""
|
2014-06-23 19:44:21 +02:00
|
|
|
|
2015-12-21 08:26:02 +01:00
|
|
|
def __init__(self, geometry=None, *, scroll=None, plaintext=None,
|
2016-08-16 17:15:44 +02:00
|
|
|
html=None, parent=None, zoom=1.0):
|
2014-06-23 19:44:21 +02:00
|
|
|
"""Constructor.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
geometry: The geometry of the frame as QRect.
|
|
|
|
scroll: The scroll position as QPoint.
|
2015-12-21 08:26:02 +01:00
|
|
|
plaintext: Return value of toPlainText
|
|
|
|
html: Return value of tohtml.
|
2016-06-06 11:56:15 +02:00
|
|
|
zoom: The zoom factor.
|
2014-06-23 19:44:21 +02:00
|
|
|
parent: The parent frame.
|
|
|
|
"""
|
|
|
|
if scroll is None:
|
|
|
|
scroll = QPoint(0, 0)
|
2014-08-26 19:10:14 +02:00
|
|
|
self.geometry = mock.Mock(return_value=geometry)
|
|
|
|
self.scrollPosition = mock.Mock(return_value=scroll)
|
|
|
|
self.parentFrame = mock.Mock(return_value=parent)
|
2015-12-21 08:26:02 +01:00
|
|
|
self.toPlainText = mock.Mock(return_value=plaintext)
|
|
|
|
self.toHtml = mock.Mock(return_value=html)
|
2016-06-06 11:56:15 +02:00
|
|
|
self.zoomFactor = mock.Mock(return_value=zoom)
|
2015-08-10 19:37:16 +02:00
|
|
|
|
2014-06-23 19:44:21 +02:00
|
|
|
|
|
|
|
class FakeChildrenFrame:
|
|
|
|
|
|
|
|
"""A stub for QWebFrame to test get_child_frames."""
|
|
|
|
|
|
|
|
def __init__(self, children=None):
|
|
|
|
if children is None:
|
|
|
|
children = []
|
2014-08-26 19:10:14 +02:00
|
|
|
self.childFrames = mock.Mock(return_value=children)
|
2014-06-23 19:44:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FakeQApplication:
|
|
|
|
|
|
|
|
"""Stub to insert as QApplication module."""
|
|
|
|
|
2016-07-06 23:47:59 +02:00
|
|
|
UNSET = object()
|
|
|
|
|
|
|
|
def __init__(self, style=None, all_widgets=None, active_window=None,
|
|
|
|
instance=UNSET):
|
|
|
|
|
|
|
|
if instance is self.UNSET:
|
|
|
|
self.instance = mock.Mock(return_value=self)
|
|
|
|
else:
|
|
|
|
self.instance = mock.Mock(return_value=instance)
|
2015-08-11 18:08:11 +02:00
|
|
|
|
2015-06-26 22:37:00 +02:00
|
|
|
self.style = mock.Mock(spec=QCommonStyle)
|
|
|
|
self.style().metaObject().className.return_value = style
|
2014-06-23 19:44:21 +02:00
|
|
|
|
2015-08-11 18:08:11 +02:00
|
|
|
self.allWidgets = lambda: all_widgets
|
2015-08-23 21:55:25 +02:00
|
|
|
self.activeWindow = lambda: active_window
|
2015-08-11 18:08:11 +02:00
|
|
|
|
2014-06-23 19:44:21 +02:00
|
|
|
|
|
|
|
class FakeNetworkReply:
|
|
|
|
|
|
|
|
"""QNetworkReply stub which provides a Content-Disposition header."""
|
|
|
|
|
2014-08-12 09:03:38 +02:00
|
|
|
KNOWN_HEADERS = {
|
|
|
|
QNetworkRequest.ContentTypeHeader: 'Content-Type',
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, headers=None, url=None):
|
2014-06-23 19:44:21 +02:00
|
|
|
if url is None:
|
2017-12-11 07:10:17 +01:00
|
|
|
url = QUrl()
|
2014-08-12 09:03:38 +02:00
|
|
|
if headers is None:
|
|
|
|
self.headers = {}
|
|
|
|
else:
|
|
|
|
self.headers = headers
|
2014-08-26 19:10:14 +02:00
|
|
|
self.url = mock.Mock(return_value=url)
|
2014-06-23 19:44:21 +02:00
|
|
|
|
|
|
|
def hasRawHeader(self, name):
|
|
|
|
"""Check if the reply has a certain header.
|
|
|
|
|
|
|
|
Args:
|
2015-02-05 07:12:36 +01:00
|
|
|
name: The name of the header as ISO-8859-1 encoded bytes object.
|
2014-06-23 19:44:21 +02:00
|
|
|
|
|
|
|
Return:
|
|
|
|
True if the header is present, False if not.
|
|
|
|
"""
|
2015-02-05 07:12:36 +01:00
|
|
|
return name.decode('iso-8859-1') in self.headers
|
2014-06-23 19:44:21 +02:00
|
|
|
|
|
|
|
def rawHeader(self, name):
|
|
|
|
"""Get the raw header data of a header.
|
|
|
|
|
|
|
|
Args:
|
2015-02-05 07:12:36 +01:00
|
|
|
name: The name of the header as ISO-8859-1 encoded bytes object.
|
2014-06-23 19:44:21 +02:00
|
|
|
|
|
|
|
Return:
|
|
|
|
The header data, as ISO-8859-1 encoded bytes() object.
|
2014-08-12 09:03:38 +02:00
|
|
|
"""
|
2015-02-05 07:12:36 +01:00
|
|
|
name = name.decode('iso-8859-1')
|
2014-08-12 09:03:38 +02:00
|
|
|
return self.headers[name].encode('iso-8859-1')
|
2014-06-23 19:44:21 +02:00
|
|
|
|
2014-08-12 09:03:38 +02:00
|
|
|
def header(self, known_header):
|
|
|
|
"""Get a known header.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
known_header: A QNetworkRequest::KnownHeaders member.
|
|
|
|
"""
|
|
|
|
key = self.KNOWN_HEADERS[known_header]
|
|
|
|
try:
|
|
|
|
return self.headers[key]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def setHeader(self, known_header, value):
|
|
|
|
"""Set a known header.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
known_header: A QNetworkRequest::KnownHeaders member.
|
|
|
|
value: The value to set.
|
2014-06-23 19:44:21 +02:00
|
|
|
"""
|
2014-08-12 09:03:38 +02:00
|
|
|
key = self.KNOWN_HEADERS[known_header]
|
|
|
|
self.headers[key] = value
|
2014-06-23 19:44:21 +02:00
|
|
|
|
|
|
|
|
2015-06-11 23:02:18 +02:00
|
|
|
def fake_qprocess():
|
|
|
|
"""Factory for a QProcess mock which has the QProcess enum values."""
|
|
|
|
m = mock.Mock(spec=QProcess)
|
2017-09-19 22:18:02 +02:00
|
|
|
for name in ['NormalExit', 'CrashExit', 'FailedToStart', 'Crashed',
|
2015-06-11 23:02:18 +02:00
|
|
|
'Timedout', 'WriteError', 'ReadError', 'UnknownError']:
|
2017-09-19 22:18:02 +02:00
|
|
|
setattr(m, name, getattr(QProcess, name))
|
2015-06-11 23:02:18 +02:00
|
|
|
return m
|
2014-06-23 19:44:21 +02:00
|
|
|
|
|
|
|
|
2016-07-10 17:23:08 +02:00
|
|
|
class FakeWebTabScroller(browsertab.AbstractScroller):
|
2015-10-23 14:01:22 +02:00
|
|
|
|
2016-07-05 20:11:42 +02:00
|
|
|
"""Fake AbstractScroller to use in tests."""
|
|
|
|
|
2016-07-13 13:44:10 +02:00
|
|
|
def __init__(self, tab, pos_perc):
|
|
|
|
super().__init__(tab)
|
2016-07-05 17:46:08 +02:00
|
|
|
self._pos_perc = pos_perc
|
2015-10-23 14:01:22 +02:00
|
|
|
|
2016-07-05 17:46:08 +02:00
|
|
|
def pos_perc(self):
|
|
|
|
return self._pos_perc
|
2016-06-17 05:12:21 +02:00
|
|
|
|
|
|
|
|
2017-06-18 15:04:14 +02:00
|
|
|
class FakeWebTabHistory(browsertab.AbstractHistory):
|
2017-07-09 23:27:34 +02:00
|
|
|
|
2017-06-18 15:04:14 +02:00
|
|
|
"""Fake for Web{Kit,Engine}History."""
|
|
|
|
|
|
|
|
def __init__(self, tab, *, can_go_back, can_go_forward):
|
|
|
|
super().__init__(tab)
|
|
|
|
self._can_go_back = can_go_back
|
|
|
|
self._can_go_forward = can_go_forward
|
|
|
|
|
|
|
|
def can_go_back(self):
|
|
|
|
assert self._can_go_back is not None
|
|
|
|
return self._can_go_back
|
|
|
|
|
|
|
|
def can_go_forward(self):
|
|
|
|
assert self._can_go_forward is not None
|
|
|
|
return self._can_go_forward
|
|
|
|
|
|
|
|
|
2018-06-11 12:41:55 +02:00
|
|
|
class FakeWebTabAudio(browsertab.AbstractAudio):
|
|
|
|
|
|
|
|
def is_muted(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_recently_audible(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2018-11-28 22:19:07 +01:00
|
|
|
class FakeWebTabPrivate(browsertab.AbstractTabPrivate):
|
2018-11-28 17:59:27 +01:00
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-07-10 17:23:08 +02:00
|
|
|
class FakeWebTab(browsertab.AbstractTab):
|
2016-07-05 17:46:08 +02:00
|
|
|
|
|
|
|
"""Fake AbstractTab to use in tests."""
|
|
|
|
|
2017-12-11 07:10:17 +01:00
|
|
|
def __init__(self, url=QUrl(), title='', tab_id=0, *,
|
2016-07-08 10:09:03 +02:00
|
|
|
scroll_pos_perc=(0, 0),
|
|
|
|
load_status=usertypes.LoadStatus.success,
|
2017-06-18 15:04:14 +02:00
|
|
|
progress=0, can_go_back=None, can_go_forward=None):
|
2018-11-28 22:19:07 +01:00
|
|
|
super().__init__(win_id=0, private=False)
|
2016-07-08 10:09:03 +02:00
|
|
|
self._load_status = load_status
|
2016-07-05 17:46:08 +02:00
|
|
|
self._title = title
|
|
|
|
self._url = url
|
2016-07-08 10:09:03 +02:00
|
|
|
self._progress = progress
|
2017-06-18 15:04:14 +02:00
|
|
|
self.history = FakeWebTabHistory(self, can_go_back=can_go_back,
|
|
|
|
can_go_forward=can_go_forward)
|
2016-07-26 13:19:07 +02:00
|
|
|
self.scroller = FakeWebTabScroller(self, scroll_pos_perc)
|
2018-09-07 21:44:17 +02:00
|
|
|
self.audio = FakeWebTabAudio(self)
|
2018-11-28 22:19:07 +01:00
|
|
|
self.private_api = FakeWebTabPrivate(tab=self, mode_manager=None)
|
2016-08-04 18:35:35 +02:00
|
|
|
wrapped = QWidget()
|
|
|
|
self._layout.wrap(self, wrapped)
|
2016-07-05 17:46:08 +02:00
|
|
|
|
2018-11-29 19:23:14 +01:00
|
|
|
def url(self, *, requested=False):
|
2016-08-10 13:54:52 +02:00
|
|
|
assert not requested
|
2016-07-05 17:46:08 +02:00
|
|
|
return self._url
|
|
|
|
|
|
|
|
def title(self):
|
|
|
|
return self._title
|
|
|
|
|
|
|
|
def progress(self):
|
2016-07-08 10:09:03 +02:00
|
|
|
return self._progress
|
2016-07-05 17:46:08 +02:00
|
|
|
|
|
|
|
def load_status(self):
|
2016-07-08 10:09:03 +02:00
|
|
|
return self._load_status
|
2015-10-23 14:01:22 +02:00
|
|
|
|
2018-03-06 22:46:54 +01:00
|
|
|
def icon(self):
|
2018-03-28 09:33:27 +02:00
|
|
|
return QIcon()
|
2018-03-06 22:46:54 +01:00
|
|
|
|
2015-10-23 14:01:22 +02:00
|
|
|
|
2014-06-23 19:44:21 +02:00
|
|
|
class FakeSignal:
|
|
|
|
|
2015-09-02 20:40:19 +02:00
|
|
|
"""Fake pyqtSignal stub which does nothing.
|
2014-06-23 19:44:21 +02:00
|
|
|
|
2015-09-02 20:40:19 +02:00
|
|
|
Attributes:
|
|
|
|
signal: The name of the signal, like pyqtSignal.
|
|
|
|
_func: The function to be invoked when the signal gets called.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name='fake', func=None):
|
2014-06-23 19:44:21 +02:00
|
|
|
self.signal = '2{}(int, int)'.format(name)
|
2015-09-02 20:40:19 +02:00
|
|
|
self._func = func
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
if self._func is None:
|
|
|
|
raise TypeError("'FakeSignal' object is not callable")
|
|
|
|
else:
|
|
|
|
return self._func()
|
2014-08-05 14:14:16 +02:00
|
|
|
|
2015-05-07 07:58:22 +02:00
|
|
|
def connect(self, slot):
|
|
|
|
"""Connect the signal to a slot.
|
|
|
|
|
|
|
|
Currently does nothing, but could be improved to do some sanity
|
|
|
|
checking on the slot.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def disconnect(self, slot=None):
|
|
|
|
"""Disconnect the signal from a slot.
|
|
|
|
|
|
|
|
Currently does nothing, but could be improved to do some sanity
|
|
|
|
checking on the slot and see if it actually got connected.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def emit(self, *args):
|
|
|
|
"""Emit the signal.
|
|
|
|
|
|
|
|
Currently does nothing, but could be improved to do type checking based
|
|
|
|
on a signature given to __init__.
|
|
|
|
"""
|
|
|
|
|
2014-08-05 14:14:16 +02:00
|
|
|
|
2017-09-19 22:18:02 +02:00
|
|
|
@attr.s(frozen=True)
|
2014-08-05 14:14:16 +02:00
|
|
|
class FakeCommand:
|
|
|
|
|
|
|
|
"""A simple command stub which has a description."""
|
|
|
|
|
2017-09-19 22:18:02 +02:00
|
|
|
name = attr.ib('')
|
|
|
|
desc = attr.ib('')
|
|
|
|
hide = attr.ib(False)
|
|
|
|
debug = attr.ib(False)
|
|
|
|
deprecated = attr.ib(False)
|
|
|
|
completion = attr.ib(None)
|
|
|
|
maxsplit = attr.ib(None)
|
2017-10-02 18:01:53 +02:00
|
|
|
takes_count = attr.ib(lambda: False)
|
2017-11-13 07:59:54 +01:00
|
|
|
modes = attr.ib((usertypes.KeyMode.normal, ))
|
2015-02-27 12:43:54 +01:00
|
|
|
|
|
|
|
|
2015-03-01 21:29:27 +01:00
|
|
|
class FakeTimer(QObject):
|
2015-02-27 12:43:54 +01:00
|
|
|
|
|
|
|
"""Stub for a usertypes.Timer."""
|
|
|
|
|
2015-03-01 21:29:27 +01:00
|
|
|
timeout_signal = pyqtSignal()
|
|
|
|
|
2015-02-27 12:43:54 +01:00
|
|
|
def __init__(self, parent=None, name=None):
|
2015-03-01 21:29:27 +01:00
|
|
|
super().__init__(parent)
|
|
|
|
self.timeout = mock.Mock(spec=['connect', 'disconnect', 'emit'])
|
|
|
|
self.timeout.connect.side_effect = self.timeout_signal.connect
|
|
|
|
self.timeout.disconnect.side_effect = self.timeout_signal.disconnect
|
|
|
|
self.timeout.emit.side_effect = self._emit
|
|
|
|
self._started = False
|
|
|
|
self._singleshot = False
|
|
|
|
self._interval = 0
|
|
|
|
self._name = name
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<{} name={!r}>'.format(self.__class__.__name__, self._name)
|
|
|
|
|
|
|
|
def _emit(self):
|
|
|
|
"""Called when the timeout "signal" gets emitted."""
|
|
|
|
if self._singleshot:
|
|
|
|
self._started = False
|
|
|
|
self.timeout_signal.emit()
|
|
|
|
|
|
|
|
def setInterval(self, interval):
|
|
|
|
self._interval = interval
|
|
|
|
|
|
|
|
def interval(self):
|
|
|
|
return self._interval
|
|
|
|
|
|
|
|
def setSingleShot(self, singleshot):
|
|
|
|
self._singleshot = singleshot
|
|
|
|
|
2015-04-04 17:29:17 +02:00
|
|
|
def isSingleShot(self):
|
2015-03-01 21:29:27 +01:00
|
|
|
return self._singleshot
|
|
|
|
|
2017-10-09 16:23:45 +02:00
|
|
|
def start(self, interval=None):
|
|
|
|
if interval:
|
|
|
|
self._interval = interval
|
2015-03-01 21:29:27 +01:00
|
|
|
self._started = True
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self._started = False
|
|
|
|
|
|
|
|
def isActive(self):
|
|
|
|
return self._started
|
2015-04-04 18:24:26 +02:00
|
|
|
|
|
|
|
|
2016-07-12 02:54:16 +02:00
|
|
|
class InstaTimer(QObject):
|
|
|
|
|
|
|
|
"""Stub for a QTimer that fires instantly on start().
|
|
|
|
|
|
|
|
Useful to test a time-based event without inserting an artificial delay.
|
|
|
|
"""
|
|
|
|
|
|
|
|
timeout = pyqtSignal()
|
|
|
|
|
2017-10-09 16:23:45 +02:00
|
|
|
def start(self, interval=None):
|
2016-07-12 02:54:16 +02:00
|
|
|
self.timeout.emit()
|
|
|
|
|
|
|
|
def setSingleShot(self, yes):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setInterval(self, interval):
|
|
|
|
pass
|
|
|
|
|
2017-06-07 14:10:44 +02:00
|
|
|
@staticmethod
|
|
|
|
def singleShot(_interval, fun):
|
|
|
|
fun()
|
|
|
|
|
2016-07-12 02:54:16 +02:00
|
|
|
|
2016-07-12 18:47:28 +02:00
|
|
|
class StatusBarCommandStub(QLineEdit):
|
2016-06-29 04:55:16 +02:00
|
|
|
|
|
|
|
"""Stub for the statusbar command prompt."""
|
|
|
|
|
|
|
|
got_cmd = pyqtSignal(str)
|
|
|
|
clear_completion_selection = pyqtSignal()
|
|
|
|
hide_completion = pyqtSignal()
|
|
|
|
update_completion = pyqtSignal()
|
|
|
|
show_cmd = pyqtSignal()
|
|
|
|
hide_cmd = pyqtSignal()
|
|
|
|
|
2016-07-03 19:03:30 +02:00
|
|
|
def prefix(self):
|
2016-07-04 05:06:36 +02:00
|
|
|
return self.text()[0]
|
2016-06-29 04:55:16 +02:00
|
|
|
|
|
|
|
|
2016-06-16 05:41:14 +02:00
|
|
|
class UrlMarkManagerStub(QObject):
|
|
|
|
|
|
|
|
"""Stub for the quickmark-manager or bookmark-manager object."""
|
|
|
|
|
|
|
|
added = pyqtSignal(str, str)
|
|
|
|
removed = pyqtSignal(str)
|
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
|
|
|
self.marks = {}
|
|
|
|
|
2016-06-22 05:04:03 +02:00
|
|
|
def delete(self, key):
|
|
|
|
del self.marks[key]
|
|
|
|
self.removed.emit(key)
|
|
|
|
|
|
|
|
|
|
|
|
class BookmarkManagerStub(UrlMarkManagerStub):
|
2016-06-22 05:23:24 +02:00
|
|
|
|
|
|
|
"""Stub for the bookmark-manager object."""
|
|
|
|
|
2016-06-22 05:04:03 +02:00
|
|
|
|
|
|
|
class QuickmarkManagerStub(UrlMarkManagerStub):
|
2016-06-22 05:23:24 +02:00
|
|
|
|
|
|
|
"""Stub for the quickmark-manager object."""
|
|
|
|
|
2016-06-22 05:04:03 +02:00
|
|
|
def quickmark_del(self, key):
|
|
|
|
self.delete(key)
|
|
|
|
|
2016-06-16 05:41:14 +02:00
|
|
|
|
2016-06-17 05:12:04 +02:00
|
|
|
class SessionManagerStub:
|
|
|
|
|
|
|
|
"""Stub for the session-manager object."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.sessions = []
|
|
|
|
|
|
|
|
def list_sessions(self):
|
|
|
|
return self.sessions
|
2016-06-17 05:12:21 +02:00
|
|
|
|
2018-03-19 18:18:21 +01:00
|
|
|
def save_autosave(self):
|
|
|
|
pass
|
|
|
|
|
2018-03-13 08:47:41 +01:00
|
|
|
|
2016-06-17 05:12:21 +02:00
|
|
|
class TabbedBrowserStub(QObject):
|
|
|
|
|
|
|
|
"""Stub for the tabbed-browser object."""
|
|
|
|
|
2018-02-15 12:02:42 +01:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
|
|
|
self.widget = TabWidgetStub()
|
|
|
|
self.shutting_down = False
|
2018-11-30 08:31:03 +01:00
|
|
|
self.loaded_url = None
|
2018-10-16 11:00:05 +02:00
|
|
|
self.cur_url = None
|
2018-02-15 12:02:42 +01:00
|
|
|
|
|
|
|
def on_tab_close_requested(self, idx):
|
|
|
|
del self.widget.tabs[idx]
|
|
|
|
|
|
|
|
def widgets(self):
|
|
|
|
return self.widget.tabs
|
|
|
|
|
|
|
|
def tabopen(self, url):
|
2018-11-30 08:31:03 +01:00
|
|
|
self.loaded_url = url
|
2018-02-15 12:02:42 +01:00
|
|
|
|
2018-11-30 08:31:03 +01:00
|
|
|
def load_url(self, url, *, newtab):
|
|
|
|
self.loaded_url = url
|
2018-02-15 12:02:42 +01:00
|
|
|
|
2018-10-16 11:00:05 +02:00
|
|
|
def current_url(self):
|
|
|
|
if self.current_url is None:
|
|
|
|
raise ValueError("current_url got called with cur_url None!")
|
|
|
|
return self.cur_url
|
|
|
|
|
2018-03-13 08:47:41 +01:00
|
|
|
|
2018-02-15 12:02:42 +01:00
|
|
|
class TabWidgetStub(QObject):
|
|
|
|
|
|
|
|
"""Stub for the tab-widget object."""
|
|
|
|
|
2016-07-10 17:23:08 +02:00
|
|
|
new_tab = pyqtSignal(browsertab.AbstractTab, int)
|
2016-06-17 05:12:21 +02:00
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
|
|
|
self.tabs = []
|
2016-11-30 13:35:08 +01:00
|
|
|
self._qtabbar = QTabBar()
|
2017-06-20 15:19:53 +02:00
|
|
|
self.index_of = None
|
|
|
|
self.current_index = None
|
2016-06-17 05:12:21 +02:00
|
|
|
|
|
|
|
def count(self):
|
|
|
|
return len(self.tabs)
|
|
|
|
|
|
|
|
def widget(self, i):
|
|
|
|
return self.tabs[i]
|
|
|
|
|
|
|
|
def page_title(self, i):
|
2016-07-05 17:46:08 +02:00
|
|
|
return self.tabs[i].title()
|
2016-06-17 05:12:21 +02:00
|
|
|
|
2016-11-30 13:35:08 +01:00
|
|
|
def tabBar(self):
|
2016-11-30 15:23:35 +01:00
|
|
|
return self._qtabbar
|
2016-11-30 13:35:08 +01:00
|
|
|
|
2017-06-20 15:19:53 +02:00
|
|
|
def indexOf(self, _tab):
|
|
|
|
if self.index_of is None:
|
|
|
|
raise ValueError("indexOf got called with index_of None!")
|
|
|
|
elif self.index_of is RuntimeError:
|
|
|
|
raise RuntimeError
|
|
|
|
else:
|
|
|
|
return self.index_of
|
|
|
|
|
|
|
|
def currentIndex(self):
|
|
|
|
if self.current_index is None:
|
|
|
|
raise ValueError("currentIndex got called with current_index "
|
|
|
|
"None!")
|
|
|
|
return self.current_index
|
|
|
|
|
|
|
|
def currentWidget(self):
|
2017-07-10 08:00:09 +02:00
|
|
|
idx = self.currentIndex()
|
|
|
|
if idx == -1:
|
|
|
|
return None
|
|
|
|
return self.tabs[idx - 1]
|
2017-06-20 15:19:53 +02:00
|
|
|
|
2018-03-13 08:47:41 +01:00
|
|
|
|
2016-06-17 05:12:21 +02:00
|
|
|
class ApplicationStub(QObject):
|
|
|
|
|
|
|
|
"""Stub to insert as the app object in objreg."""
|
|
|
|
|
|
|
|
new_window = pyqtSignal(mainwindow.MainWindow)
|
2018-02-13 19:38:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HTTPPostStub(QObject):
|
|
|
|
|
|
|
|
"""A stub class for HTTPClient.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
url: the last url send by post()
|
|
|
|
data: the last data send by post()
|
|
|
|
"""
|
|
|
|
|
|
|
|
success = pyqtSignal(str)
|
|
|
|
error = pyqtSignal(str)
|
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
|
|
|
self.url = None
|
|
|
|
self.data = None
|
|
|
|
|
|
|
|
def post(self, url, data=None):
|
|
|
|
self.url = url
|
|
|
|
self.data = data
|
2018-03-03 01:00:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class FakeDownloadItem(QObject):
|
|
|
|
|
|
|
|
"""Mock browser.downloads.DownloadItem."""
|
|
|
|
|
|
|
|
finished = pyqtSignal()
|
|
|
|
|
|
|
|
def __init__(self, fileobj, name, parent=None):
|
|
|
|
super().__init__(parent)
|
|
|
|
self.fileobj = fileobj
|
|
|
|
self.name = name
|
2018-01-21 03:37:22 +01:00
|
|
|
self.successful = False
|
2018-03-03 01:00:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class FakeDownloadManager:
|
|
|
|
|
|
|
|
"""Mock browser.downloads.DownloadManager."""
|
|
|
|
|
|
|
|
def __init__(self, tmpdir):
|
|
|
|
self._tmpdir = tmpdir
|
2018-01-21 03:37:22 +01:00
|
|
|
self.downloads = []
|
2018-03-03 01:00:27 +01:00
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def _open_fileobj(self, target):
|
|
|
|
"""Ensure a DownloadTarget's fileobj attribute is available."""
|
|
|
|
if isinstance(target, downloads.FileDownloadTarget):
|
|
|
|
target.fileobj = open(target.filename, 'wb')
|
|
|
|
try:
|
|
|
|
yield target.fileobj
|
|
|
|
finally:
|
|
|
|
target.fileobj.close()
|
|
|
|
else:
|
|
|
|
yield target.fileobj
|
|
|
|
|
|
|
|
def get(self, url, target, **kwargs):
|
|
|
|
"""Return a FakeDownloadItem instance with a fileobj.
|
|
|
|
|
|
|
|
The content is copied from the file the given url links to.
|
|
|
|
"""
|
|
|
|
with self._open_fileobj(target):
|
|
|
|
download_item = FakeDownloadItem(target.fileobj, name=url.path())
|
|
|
|
with (self._tmpdir / url.path()).open('rb') as fake_url_file:
|
|
|
|
shutil.copyfileobj(fake_url_file, download_item.fileobj)
|
2018-01-21 03:37:22 +01:00
|
|
|
self.downloads.append(download_item)
|
2018-03-03 01:00:27 +01:00
|
|
|
return download_item
|
2018-09-06 20:04:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FakeHistoryProgress:
|
|
|
|
|
|
|
|
"""Fake for a WebHistoryProgress object."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._started = False
|
|
|
|
self._finished = False
|
|
|
|
self._value = 0
|
|
|
|
|
|
|
|
def start(self, _text, _maximum):
|
|
|
|
self._started = True
|
|
|
|
|
|
|
|
def tick(self):
|
|
|
|
self._value += 1
|
|
|
|
|
|
|
|
def finish(self):
|
|
|
|
self._finished = True
|