2015-10-21 22:05:41 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2016-01-04 07:12:39 +01:00
|
|
|
# Copyright 2015-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2015-10-21 22:05:41 +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/>.
|
|
|
|
|
|
|
|
"""Test the quteproc fixture used for tests."""
|
|
|
|
|
2015-11-09 19:34:34 +01:00
|
|
|
import logging
|
|
|
|
import datetime
|
|
|
|
|
2015-10-21 22:05:41 +02:00
|
|
|
import pytest
|
|
|
|
|
2015-12-01 22:45:59 +01:00
|
|
|
import quteprocess
|
|
|
|
import testprocess
|
2015-11-09 19:34:34 +01:00
|
|
|
from qutebrowser.utils import log
|
|
|
|
|
2015-10-21 22:05:41 +02:00
|
|
|
|
2016-01-14 06:54:21 +01:00
|
|
|
@pytest.mark.parametrize('cmd', [
|
|
|
|
':message-error test',
|
|
|
|
':jseval console.log("[FAIL] test");'
|
|
|
|
])
|
|
|
|
def test_quteproc_error_message(qtbot, quteproc, cmd):
|
2015-10-21 22:05:41 +02:00
|
|
|
"""Make sure the test fails with an unexpected error message."""
|
2016-01-08 09:49:06 +01:00
|
|
|
with qtbot.waitSignal(quteproc.got_error):
|
2016-01-14 06:54:21 +01:00
|
|
|
quteproc.send_cmd(cmd)
|
2015-10-21 22:05:41 +02:00
|
|
|
# Usually we wouldn't call this from inside a test, but here we force the
|
|
|
|
# error to occur during the test rather than at teardown time.
|
|
|
|
with pytest.raises(pytest.fail.Exception):
|
2016-02-04 06:43:14 +01:00
|
|
|
quteproc.after_test(did_fail=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_quteproc_error_message_did_fail(qtbot, quteproc):
|
|
|
|
"""Make sure the test does not fail on teardown if the main test failed."""
|
|
|
|
with qtbot.waitSignal(quteproc.got_error):
|
|
|
|
quteproc.send_cmd(':message-error test')
|
|
|
|
# Usually we wouldn't call this from inside a test, but here we force the
|
|
|
|
# error to occur during the test rather than at teardown time.
|
|
|
|
quteproc.after_test(did_fail=True)
|
2015-11-02 06:19:19 +01:00
|
|
|
|
|
|
|
|
2016-01-14 20:32:17 +01:00
|
|
|
def test_quteproc_skip_via_js(qtbot, quteproc):
|
2016-01-14 18:50:36 +01:00
|
|
|
with pytest.raises(pytest.skip.Exception) as excinfo:
|
2016-01-14 20:32:17 +01:00
|
|
|
quteproc.send_cmd(':jseval console.log("[SKIP] test");')
|
|
|
|
quteproc.wait_for_js('[SKIP] test')
|
|
|
|
|
|
|
|
# Usually we wouldn't call this from inside a test, but here we force
|
|
|
|
# the error to occur during the test rather than at teardown time.
|
2016-02-04 06:43:14 +01:00
|
|
|
quteproc.after_test(did_fail=False)
|
2016-01-14 18:50:36 +01:00
|
|
|
|
2016-01-14 20:32:17 +01:00
|
|
|
assert str(excinfo.value) == 'test'
|
|
|
|
|
|
|
|
|
|
|
|
def test_quteproc_skip_and_wait_for(qtbot, quteproc):
|
|
|
|
"""This test will skip *again* during teardown, but we don't care."""
|
|
|
|
with pytest.raises(pytest.skip.Exception):
|
|
|
|
quteproc.send_cmd(':jseval console.log("[SKIP] foo");')
|
|
|
|
quteproc.wait_for_js("[SKIP] foo")
|
|
|
|
quteproc.wait_for(message='This will not match')
|
2016-01-14 18:50:36 +01:00
|
|
|
|
|
|
|
|
2015-11-02 06:19:19 +01:00
|
|
|
def test_qt_log_ignore(qtbot, quteproc):
|
|
|
|
"""Make sure the test passes when logging a qt_log_ignore message."""
|
2016-01-08 09:49:06 +01:00
|
|
|
with qtbot.waitSignal(quteproc.got_error):
|
2015-11-02 06:19:19 +01:00
|
|
|
quteproc.send_cmd(':message-error "SpellCheck: test"')
|
2015-11-09 19:34:34 +01:00
|
|
|
|
|
|
|
|
2016-01-17 20:46:55 +01:00
|
|
|
def test_quteprocess_quitting(qtbot, quteproc_process):
|
|
|
|
"""When qutebrowser quits, after_test should fail."""
|
2016-01-18 23:00:41 +01:00
|
|
|
with qtbot.waitSignal(quteproc_process.proc.finished, timeout=15000):
|
2016-01-17 20:46:55 +01:00
|
|
|
quteproc_process.send_cmd(':quit')
|
|
|
|
with pytest.raises(testprocess.ProcessExited):
|
2016-02-04 06:43:14 +01:00
|
|
|
quteproc_process.after_test(did_fail=False)
|
2016-01-17 20:46:55 +01:00
|
|
|
|
|
|
|
|
2015-11-09 19:34:34 +01:00
|
|
|
@pytest.mark.parametrize('data, attrs', [
|
|
|
|
(
|
|
|
|
# Normal message
|
|
|
|
'01:02:03 DEBUG init earlyinit:init_log:280 Log initialized.',
|
|
|
|
{
|
|
|
|
'timestamp': datetime.datetime(year=1900, month=1, day=1,
|
|
|
|
hour=1, minute=2, second=3),
|
|
|
|
'loglevel': logging.DEBUG,
|
|
|
|
'category': 'init',
|
|
|
|
'module': 'earlyinit',
|
|
|
|
'function': 'init_log',
|
2016-01-21 18:07:56 +01:00
|
|
|
'line': 280,
|
2015-11-09 19:34:34 +01:00
|
|
|
'message': 'Log initialized.',
|
|
|
|
'expected': False,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
(
|
|
|
|
# VDEBUG
|
|
|
|
'00:00:00 VDEBUG foo foo:foo:0 test',
|
|
|
|
{'loglevel': log.VDEBUG_LEVEL}
|
|
|
|
),
|
|
|
|
(
|
|
|
|
# Unknown module
|
|
|
|
'00:00:00 WARNING qt Unknown module:none:0 test',
|
|
|
|
{'module': None, 'function': None, 'line': None},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
# Expected message
|
|
|
|
'00:00:00 VDEBUG foo foo:foo:0 SpellCheck: test',
|
|
|
|
{'expected': True},
|
2015-11-09 19:55:05 +01:00
|
|
|
),
|
|
|
|
(
|
|
|
|
# Weird Qt location
|
|
|
|
'00:00:00 DEBUG qt qnetworkreplyhttpimpl:"void '
|
|
|
|
'QNetworkReplyHttpImplPrivate::error(QNetworkReply::NetworkError, '
|
|
|
|
'const QString&)":1929 QNetworkReplyImplPrivate::error: Internal '
|
|
|
|
'problem, this method must only be called once.',
|
|
|
|
{
|
|
|
|
'module': 'qnetworkreplyhttpimpl',
|
|
|
|
'function': 'void QNetworkReplyHttpImplPrivate::error('
|
|
|
|
'QNetworkReply::NetworkError, const QString&)',
|
|
|
|
'line': 1929
|
|
|
|
}
|
|
|
|
),
|
|
|
|
(
|
2016-01-21 18:07:56 +01:00
|
|
|
'00:00:00 WARNING qt qxcbxsettings:"QXcbXSettings::'
|
|
|
|
'QXcbXSettings(QXcbScreen*)":233 '
|
|
|
|
'QXcbXSettings::QXcbXSettings(QXcbScreen*) Failed to get selection '
|
|
|
|
'owner for XSETTINGS_S atom ',
|
2015-11-09 19:55:05 +01:00
|
|
|
{
|
|
|
|
'module': 'qxcbxsettings',
|
|
|
|
'function': 'QXcbXSettings::QXcbXSettings(QXcbScreen*)',
|
|
|
|
'line': 233,
|
|
|
|
}
|
|
|
|
),
|
2015-11-10 09:23:18 +01:00
|
|
|
(
|
|
|
|
# With [2s ago] marker
|
|
|
|
'00:00:00 DEBUG foo foo:foo:0 [2s ago] test',
|
|
|
|
{'prefix': '2s ago', 'message': 'test'}
|
|
|
|
),
|
2015-12-16 22:21:29 +01:00
|
|
|
], ids=['normal', 'vdebug', 'unknown module', 'expected message',
|
|
|
|
'weird Qt location', 'QXcbXSettings', '2s ago marker'])
|
2015-11-09 19:34:34 +01:00
|
|
|
def test_log_line_parse(data, attrs):
|
|
|
|
line = quteprocess.LogLine(data)
|
|
|
|
for name, expected in attrs.items():
|
|
|
|
actual = getattr(line, name)
|
|
|
|
assert actual == expected, name
|
|
|
|
|
|
|
|
|
|
|
|
def test_log_line_no_match():
|
2015-11-16 23:14:24 +01:00
|
|
|
with pytest.raises(testprocess.InvalidLine):
|
2015-11-09 19:34:34 +01:00
|
|
|
quteprocess.LogLine("Hello World!")
|
2016-03-29 20:45:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestClickElement:
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def open_page(self, quteproc):
|
|
|
|
quteproc.open_path('data/click_element.html')
|
|
|
|
quteproc.wait_for_load_finished('data/click_element.html')
|
|
|
|
|
|
|
|
def test_click_element(self, quteproc):
|
|
|
|
quteproc.click_element('Test Element')
|
|
|
|
quteproc.wait_for_js('click_element clicked')
|
|
|
|
|
|
|
|
def test_click_special_chars(self, quteproc):
|
|
|
|
quteproc.click_element('"Don\'t", he shouted')
|
|
|
|
quteproc.wait_for_js('click_element special chars')
|
|
|
|
|
|
|
|
def test_duplicate(self, quteproc):
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
quteproc.click_element('Duplicate')
|
|
|
|
assert 'not unique' in str(excinfo.value)
|
|
|
|
|
|
|
|
def test_nonexistent(self, quteproc):
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
quteproc.click_element('no element exists with this text')
|
|
|
|
assert 'No element' in str(excinfo.value)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('string, expected', [
|
|
|
|
('Test', "'Test'"),
|
|
|
|
("Don't", '"Don\'t"'),
|
|
|
|
# This is some serious string escaping madness
|
2016-03-29 21:02:54 +02:00
|
|
|
('"Don\'t", he said',
|
|
|
|
"concat('\"', 'Don', \"'\", 't', '\"', ', he said')"),
|
2016-03-29 20:45:15 +02:00
|
|
|
])
|
|
|
|
def test_xpath_escape(string, expected):
|
|
|
|
assert quteprocess._xpath_escape(string) == expected
|
2016-04-06 08:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('value', [
|
|
|
|
'foo',
|
|
|
|
'foo"bar', # Make sure a " is preserved
|
|
|
|
])
|
|
|
|
def test_set(quteproc, value):
|
|
|
|
quteproc.set_setting('network', 'accept-language', value)
|
|
|
|
read_back = quteproc.get_setting('network', 'accept-language')
|
|
|
|
assert read_back == value
|