summaryrefslogtreecommitdiff
path: root/tests/unit/browser/test_downloadview.py
blob: 84b50fad2e2e2bf83b93e25810105d6decf7ce20 (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
# 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/>.


import pytest

from PyQt5.QtCore import QUrl

from qutebrowser.browser import downloads, qtnetworkdownloads, downloadview


class FakeDownload(downloads.AbstractDownloadItem):

    # As this is used for tests, we only override what's actually needed.
    # pylint: disable=abstract-method

    def __init__(self, done: bool, successful: bool = False) -> None:
        super().__init__(manager=None)
        self.done = done
        self.successful = successful

    def url(self) -> QUrl:
        return QUrl('https://example.org/')


@pytest.fixture
def qtnetwork_manager(config_stub, cookiejar_and_cache):
    """A QtNetwork-based download manager."""
    return qtnetworkdownloads.DownloadManager()


@pytest.fixture
def model(qtnetwork_manager, qtmodeltester, qapp):
    """A simple DownloadModel."""
    model = downloads.DownloadModel(qtnetwork_manager)
    qtmodeltester.check(model)
    return model


@pytest.fixture
def view(model, qtbot):
    """A DownloadView."""
    dv = downloadview.DownloadView(model)
    qtbot.add_widget(dv)
    return dv


@pytest.mark.parametrize('can_clear', [True, False])
@pytest.mark.parametrize('item, expected', [
    # Clicking outside of a download item
    (None, []),

    # Clicking a in-progress item
    (FakeDownload(done=False), ["Cancel", "Copy URL"]),

    # Clicking a successful item
    (
        FakeDownload(done=True, successful=True),
        ["Open", "Open directory", "Remove", "Copy URL"],
    ),

    # Clicking an unsuccessful item
    (
        FakeDownload(done=True, successful=False),
        ["Retry", "Remove", "Copy URL"],
    ),
])
def test_get_menu_actions(can_clear, item, expected, view, qtnetwork_manager):
    if can_clear:
        qtnetwork_manager.downloads.append(FakeDownload(done=True))
        expected = expected.copy() + [None, "Remove all finished"]
    else:
        assert not qtnetwork_manager.downloads
        assert "Remove all finished" not in expected

    actions = view._get_menu_actions(item)
    texts = [action[0] for action in actions]
    assert texts == expected