summaryrefslogtreecommitdiff
path: root/tests/unit/mainwindow/statusbar/test_url.py
blob: dc3241cf1c58c35586c8282763ccc1ffd118229a (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2016-2021 Clayton Craft (craftyguy) <craftyguy@gmail.com>
#
# 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/>.


"""Test Statusbar url."""

import pytest

from qutebrowser.mainwindow.statusbar import url
from qutebrowser.qt import QtCore
from qutebrowser.utils import urlutils, usertypes


@pytest.fixture
def url_widget(qtbot, monkeypatch, config_stub):
    """Fixture providing a Url widget."""
    widget = url.UrlText()
    qtbot.add_widget(widget)
    assert not widget.isVisible()
    return widget


@pytest.mark.parametrize('url_text, expected', [
    ('http://example.com/foo/bar.html', 'http://example.com/foo/bar.html'),
    ('http://test.gr/%CE%B1%CE%B2%CE%B3%CE%B4.txt', 'http://test.gr/αβγδ.txt'),
    ('http://test.ru/%D0%B0%D0%B1%D0%B2%D0%B3.txt', 'http://test.ru/абвг.txt'),
    ('http://test.com/s%20p%20a%20c%20e.txt', 'http://test.com/s p a c e.txt'),
    ('http://test.com/%22quotes%22.html', 'http://test.com/%22quotes%22.html'),
    ('http://username:secret%20password@test.com', 'http://username@test.com'),
    ('http://example.com%5b/', '(invalid URL!) http://example.com%5b/'),
    # https://bugreports.qt.io/browse/QTBUG-60364
    ('http://www.xn--80ak6aa92e.com', 'http://www.xn--80ak6aa92e.com'),
    # IDN URL
    ('http://www.ä.com', '(www.xn--4ca.com) http://www.ä.com'),
    (None, ''),
])
@pytest.mark.parametrize('which', ['normal', 'hover'])
def test_set_url(url_widget, url_text, expected, which):
    """Test text when hovering over a percent encoded link."""
    if which == 'normal':
        if url_text is None:
            qurl = None
        else:
            qurl = QtCore.QUrl(url_text)
            if not qurl.isValid():
                # Special case for the invalid URL above
                expected = "Invalid URL!"
        url_widget.set_url(qurl)
    else:
        url_widget.set_hover_url(url_text)

    assert url_widget.text() == expected

    if which == 'hover' and expected:
        assert url_widget._urltype == url.UrlType.hover
    else:
        assert url_widget._urltype == url.UrlType.normal


@pytest.mark.parametrize('status, expected', [
    (usertypes.LoadStatus.success, url.UrlType.success),
    (usertypes.LoadStatus.success_https, url.UrlType.success_https),
    (usertypes.LoadStatus.error, url.UrlType.error),
    (usertypes.LoadStatus.warn, url.UrlType.warn),
    (usertypes.LoadStatus.loading, url.UrlType.normal),
    (usertypes.LoadStatus.none, url.UrlType.normal)
])
def test_on_load_status_changed(url_widget, status, expected):
    """Test text when status is changed."""
    url_widget.set_url(QtCore.QUrl('www.example.com'))
    url_widget.on_load_status_changed(status)
    assert url_widget._urltype == expected


@pytest.mark.parametrize(
    'load_status, qurl',
    [
        (
            usertypes.LoadStatus.success,
            QtCore.QUrl('http://abc123.com/this/awesome/url.html'),
        ),
        (usertypes.LoadStatus.success, QtCore.QUrl('http://reddit.com/r/linux')),
        (usertypes.LoadStatus.success, QtCore.QUrl('http://ä.com/')),
        (usertypes.LoadStatus.success_https, QtCore.QUrl('www.google.com')),
        (
            usertypes.LoadStatus.success_https,
            QtCore.QUrl('https://supersecret.gov/nsa/files.txt'),
        ),
        (
            usertypes.LoadStatus.warn,
            QtCore.QUrl('www.shadysite.org/some/file/with/issues.htm'),
        ),
        (usertypes.LoadStatus.error, QtCore.QUrl('invalid::/url')),
        (usertypes.LoadStatus.error, QtCore.QUrl()),
    ],
)
def test_on_tab_changed(url_widget, fake_web_tab, load_status, qurl):
    tab_widget = fake_web_tab(load_status=load_status, url=qurl)
    url_widget.on_tab_changed(tab_widget)

    assert url_widget._urltype.name == load_status.name
    if not qurl.isValid():
        expected = ''
    else:
        expected = urlutils.safe_display_string(qurl)
    assert url_widget.text() == expected


@pytest.mark.parametrize(
    'qurl, load_status, expected_status',
    [
        (
            QtCore.QUrl('http://abc123.com/this/awesome/url.html'),
            usertypes.LoadStatus.success,
            url.UrlType.success,
        ),
        (
            QtCore.QUrl('https://supersecret.gov/nsa/files.txt'),
            usertypes.LoadStatus.success_https,
            url.UrlType.success_https,
        ),
        (
            QtCore.QUrl('http://www.qutebrowser.org/CONTRIBUTING.html'),
            usertypes.LoadStatus.loading,
            url.UrlType.normal,
        ),
        (
            QtCore.QUrl('www.whatisthisurl.com'),
            usertypes.LoadStatus.warn,
            url.UrlType.warn,
        ),
    ],
)
def test_normal_url(url_widget, qurl, load_status, expected_status):
    url_widget.set_url(qurl)
    url_widget.on_load_status_changed(load_status)
    url_widget.set_hover_url(qurl.toDisplayString())
    url_widget.set_hover_url("")
    assert url_widget.text() == qurl.toDisplayString()
    assert url_widget._urltype == expected_status