summaryrefslogtreecommitdiff
path: root/tests/unit/browser/test_urlmarks.py
blob: 29c1409091fe2e9a11bd184e34b3635bedbc9bdb (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
# Copyright 2018-2021 Ryan Roden-Corrent (rcorre) <ryan@rcorre.net>
#
# 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 bookmarks/quickmarks."""

import unittest.mock

import pytest
from qutebrowser.qt.core import QUrl

from qutebrowser.browser import urlmarks


@pytest.fixture
def bm_file(config_tmpdir):
    bm_dir = config_tmpdir / 'bookmarks'
    bm_dir.mkdir()
    bm_file = bm_dir / 'urls'
    return bm_file


def test_init(bm_file, fake_save_manager):
    bm_file.write('\n'.join([
        'http://example.com Example Site',
        'http://example.com/foo Foo',
        'http://example.com/bar Bar',
        'http://example.com/notitle',
    ]))

    bm = urlmarks.BookmarkManager()
    fake_save_manager.add_saveable.assert_called_once_with(
        'bookmark-manager',
        unittest.mock.ANY,
        unittest.mock.ANY,
        filename=str(bm_file),
    )

    assert list(bm.marks.items()) == [
        ('http://example.com', 'Example Site'),
        ('http://example.com/foo', 'Foo'),
        ('http://example.com/bar', 'Bar'),
        ('http://example.com/notitle', ''),
    ]


def test_add(bm_file, fake_save_manager, qtbot):
    bm = urlmarks.BookmarkManager()

    with qtbot.wait_signal(bm.changed):
        bm.add(QUrl('http://example.com'), 'Example Site')
    assert list(bm.marks.items()) == [
        ('http://example.com', 'Example Site'),
    ]

    with qtbot.wait_signal(bm.changed):
        bm.add(QUrl('http://example.com/notitle'), '')
    assert list(bm.marks.items()) == [
        ('http://example.com', 'Example Site'),
        ('http://example.com/notitle', ''),
    ]


def test_add_toggle(bm_file, fake_save_manager, qtbot):
    bm = urlmarks.BookmarkManager()

    with qtbot.wait_signal(bm.changed):
        bm.add(QUrl('http://example.com'), '', toggle=True)
    assert 'http://example.com' in bm.marks

    with qtbot.wait_signal(bm.changed):
        bm.add(QUrl('http://example.com'), '', toggle=True)
    assert 'http://example.com' not in bm.marks

    with qtbot.wait_signal(bm.changed):
        bm.add(QUrl('http://example.com'), '', toggle=True)
    assert 'http://example.com' in bm.marks


def test_add_dupe(bm_file, fake_save_manager, qtbot):
    bm = urlmarks.BookmarkManager()

    bm.add(QUrl('http://example.com'), '')
    with pytest.raises(urlmarks.AlreadyExistsError):
        bm.add(QUrl('http://example.com'), '')


def test_delete(bm_file, fake_save_manager, qtbot):
    bm = urlmarks.BookmarkManager()

    bm.add(QUrl('http://example.com/foo'), 'Foo')
    bm.add(QUrl('http://example.com/bar'), 'Bar')
    bm.add(QUrl('http://example.com/baz'), 'Baz')
    bm.save()

    with qtbot.wait_signal(bm.changed):
        bm.delete('http://example.com/bar')
    assert list(bm.marks.items()) == [
        ('http://example.com/foo', 'Foo'),
        ('http://example.com/baz', 'Baz'),
    ]


def test_save(bm_file, fake_save_manager, qtbot):
    bm = urlmarks.BookmarkManager()

    bm.add(QUrl('http://example.com'), 'Example Site')
    bm.add(QUrl('http://example.com/notitle'), '')
    bm.save()
    assert bm_file.read().splitlines() == [
        'http://example.com Example Site',
        'http://example.com/notitle ',
    ]