summaryrefslogtreecommitdiff
path: root/tests/unit/javascript/test_js_quirks.py
blob: da05a74a1162fb5339fa64e8953599fb4f5f23ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2021 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 <https://www.gnu.org/licenses/>.

"""Tests for QtWebEngine JavaScript quirks.

This tests JS functionality which is missing in older QtWebEngine releases, but we have
polyfills for. They should either pass because the polyfill is active, or pass because
the native functionality exists.
"""

import pathlib

import pytest
from qutebrowser.qt.core import QUrl

import qutebrowser
from qutebrowser.utils import usertypes
from qutebrowser.config import configdata


@pytest.mark.parametrize('base_url, source, expected', [
    pytest.param(
        QUrl(),
        '"This is a test".replaceAll("test", "fest")',
        "This is a fest",
        id='replace-all',
    ),
    pytest.param(
        QUrl(),
        '"This is a test".replaceAll(/[tr]est/g, "fest")',
        "This is a fest",
        id='replace-all-regex',
    ),
    pytest.param(
        QUrl(),
        '"This is a [test[".replaceAll("[", "<")',
        "This is a <test<",
        id='replace-all-reserved-string',
    ),
    pytest.param(
        QUrl("https://test.qutebrowser.org/linkedin"),
        '[1, 2, 3].at(1)',
        2,
        id='array-at',
    ),
])
def test_js_quirks(config_stub, js_tester_webengine, base_url, source, expected):
    config_stub.val.content.site_specific_quirks.skip = []
    js_tester_webengine.tab._scripts._inject_site_specific_quirks()
    js_tester_webengine.load('base.html', base_url=base_url)
    js_tester_webengine.run(source, expected, world=usertypes.JsWorld.main)


def test_js_quirks_match_files(webengine_tab):
    quirks_path = pathlib.Path(qutebrowser.__file__).parent / "javascript" / "quirks"
    suffix = ".user.js"
    quirks_files = {p.name[:-len(suffix)] for p in quirks_path.glob(f"*{suffix}")}
    quirks_code = {q.filename for q in webengine_tab._scripts._get_quirks()}
    assert quirks_code == quirks_files


def test_js_quirks_match_settings(webengine_tab, configdata_init):
    opt = configdata.DATA["content.site_specific_quirks.skip"]
    prefix = "js-"
    valid_values = opt.typ.get_valid_values()
    assert valid_values is not None
    quirks_config = {
        val[len(prefix):].replace("-", "_")
        for val in valid_values
        if val.startswith(prefix)
    }

    quirks_code = {q.filename for q in webengine_tab._scripts._get_quirks()}
    quirks_code -= {"googledocs"}  # special case, UA quirk

    assert quirks_code == quirks_config