summaryrefslogtreecommitdiff
path: root/tests/unit/browser/webkit/test_tabhistory.py
blob: 047454e25d48894e854fb175eebbe68ab4c24e79 (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
# Copyright 2015-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/>.

# FIXME:qt6 (lint)
# pylint: disable=no-name-in-module

"""Tests for webelement.tabhistory."""

import dataclasses
from typing import Any

import pytest
pytest.importorskip('qutebrowser.qt.webkit')
from qutebrowser.qt.core import QUrl, QPoint
from qutebrowser.qt.webkit import QWebHistory

from qutebrowser.browser.webkit import tabhistory
from qutebrowser.misc.sessions import TabHistoryItem as Item
from qutebrowser.utils import qtutils


pytestmark = pytest.mark.qt_log_ignore('QIODevice::read.*: device not open')


ITEMS = [
    Item(QUrl('https://www.heise.de/'), 'heise'),
    Item(QUrl('about:blank'), 'blank', active=True),
    Item(QUrl('http://example.com/%E2%80%A6'), 'percent'),
    Item(QUrl('http://example.com/?foo=bar'), 'arg',
         original_url=QUrl('http://original.url.example.com/'),
         user_data={'foo': 23, 'bar': 42}),
    # From https://github.com/OtterBrowser/otter-browser/issues/709#issuecomment-74749471
    Item(QUrl('http://github.com/OtterBrowser/24/134/2344/otter-browser/'
              'issues/709/'),
         'Page not found | github',
         user_data={'zoom': 149, 'scroll-pos': QPoint(0, 0)}),
    Item(QUrl('https://mail.google.com/mail/u/0/#label/some+label/'
              '234lkjsd0932lkjf884jqwerdf4'),
         '"some label" - email@gmail.com - Gmail"',
         user_data={'zoom': 120, 'scroll-pos': QPoint(0, 0)}),
]


@dataclasses.dataclass
class Objects:

    history: QWebHistory
    user_data: Any


@pytest.fixture
def empty_history(webpage):
    """Fixture providing an empty QWebHistory."""
    hist = webpage.history()
    assert hist.count() == 0
    return hist


@pytest.fixture
def objects(empty_history):
    """Fixture providing a history (and userdata) filled with example data."""
    stream, _data, user_data = tabhistory.serialize(ITEMS)
    qtutils.deserialize_stream(stream, empty_history)
    return Objects(history=empty_history, user_data=user_data)


def test_count(objects):
    """Check if the history's count was loaded correctly."""
    assert objects.history.count() == len(ITEMS)


@pytest.mark.parametrize('i', range(len(ITEMS)))
def test_valid(objects, i):
    """Check if all items are valid."""
    assert objects.history.itemAt(i).isValid()


@pytest.mark.parametrize('i', range(len(ITEMS)))
def test_no_userdata(objects, i):
    """Check if all items have no user data."""
    assert objects.history.itemAt(i).userData() is None


def test_userdata(objects):
    """Check if all user data has been restored to user_data."""
    userdata_items = [item.user_data for item in ITEMS]
    assert userdata_items == objects.user_data


def test_currentitem(objects):
    """Check if the current item index was loaded correctly."""
    assert objects.history.currentItemIndex() == 1


@pytest.mark.parametrize('i, item', enumerate(ITEMS))
def test_urls(objects, i, item):
    """Check if the URLs were loaded correctly."""
    assert objects.history.itemAt(i).url() == item.url


@pytest.mark.parametrize('i, item', enumerate(ITEMS))
def test_original_urls(objects, i, item):
    """Check if the original URLs were loaded correctly."""
    assert objects.history.itemAt(i).originalUrl() == item.original_url


@pytest.mark.parametrize('i, item', enumerate(ITEMS))
def test_titles(objects, i, item):
    """Check if the titles were loaded correctly."""
    assert objects.history.itemAt(i).title() == item.title


def test_no_active_item():
    """Check tabhistory.serialize with no active item."""
    items = [Item(QUrl(), '')]
    with pytest.raises(ValueError):
        tabhistory.serialize(items)


def test_two_active_items():
    """Check tabhistory.serialize with two active items."""
    items = [Item(QUrl(), '', active=True),
             Item(QUrl(), ''),
             Item(QUrl(), '', active=True)]
    with pytest.raises(ValueError):
        tabhistory.serialize(items)


def test_empty(empty_history):
    """Check tabhistory.serialize with no items."""
    items = []
    stream, _data, user_data = tabhistory.serialize(items)
    qtutils.deserialize_stream(stream, empty_history)
    assert empty_history.count() == 0
    assert empty_history.currentItemIndex() == 0
    assert not user_data