summaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/qmltests/data/tst_contextMenu.qml
blob: 58e27b8ba6db0de0f52b47e52d9857a59bc0719e (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
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import QtQuick
import QtTest
import QtWebEngine
import "../mock-delegates/TestParams"

TestWebEngineView {
    id: webEngineView
    width: 400
    height: 400

    property string linkText: ""
    property var mediaType: null
    property string selectedText: ""

    onContextMenuRequested: function (request) {
        linkText = request.linkText;
        mediaType = request.mediaType;
        selectedText = request.selectedText;
    }

    SignalSpy {
        id: contextMenuRequestedSpy
        target: webEngineView
        signalName: "contextMenuRequested"
    }

    TestCase {
        id: testCase
        name: "WebEngineViewContextMenu"
        when: windowShown

        function init() {
            MenuParams.isMenuOpened = false;
        }

        function cleanup() {
            contextMenuRequestedSpy.clear();
        }

        function test_contextMenuRequest_data() {
            return [
                   { tag: "defaultContextMenu", userHandled: false, accepted: false },
                   { tag: "defaultContextMenuWithConnect", userHandled: true, accepted: false },
                   { tag: "dontShowDefaultContextMenu", userHandled: true, accepted: true },
            ];
        }

        function test_contextMenuRequest(row) {
            function contextMenuHandler(request) {
                request.accepted = row.accepted;
            }

            if (row.userHandled) {
                webEngineView.contextMenuRequested.connect(contextMenuHandler);
            }
            webEngineView.loadHtml("<html></html>");
            verify(webEngineView.waitForLoadSucceeded());

            mouseClick(webEngineView, 20, 20, Qt.RightButton);
            contextMenuRequestedSpy.wait();
            compare(contextMenuRequestedSpy.count, 1);
            tryCompare(MenuParams, "isMenuOpened", !row.accepted);

            webEngineView.contextMenuRequested.disconnect(contextMenuHandler);
        }

        function test_contextMenuLinkAndSelectedText() {
            webEngineView.loadHtml("<html><body>" +
                                   "<span id='text'>Text </span>" +
                                   "<a id='link' href='test1.html'>Link</a>" +
                                   "</body></html>");
            verify(webEngineView.waitForLoadSucceeded());

            // 1. Nothing is selected, right click on the link
            var linkCenter = getElementCenter("link");
            mouseClick(webEngineView, linkCenter.x, linkCenter.y, Qt.RightButton);
            contextMenuRequestedSpy.wait();
            compare(contextMenuRequestedSpy.count, 1);

            compare(linkText, "Link");
            compare(mediaType, ContextMenuRequest.MediaTypeNone);
            compare(selectedText, "");

            verify(webEngineView.action(WebEngineView.OpenLinkInNewTab).enabled);
            verify(webEngineView.action(WebEngineView.OpenLinkInNewWindow).enabled);
            verify(webEngineView.action(WebEngineView.DownloadLinkToDisk).enabled);
            verify(webEngineView.action(WebEngineView.CopyLinkToClipboard).enabled);

            contextMenuRequestedSpy.clear();

            // 2. Everything is selected, right click on the link
            webEngineView.triggerWebAction(WebEngineView.SelectAll);
            tryVerify(function() { return getTextSelection() == "Text Link" });

            mouseClick(webEngineView, linkCenter.x, linkCenter.y, Qt.RightButton);
            contextMenuRequestedSpy.wait();
            compare(contextMenuRequestedSpy.count, 1);

            compare(linkText, "Link");
            compare(mediaType, ContextMenuRequest.MediaTypeNone);
            compare(selectedText, "Text Link");

            contextMenuRequestedSpy.clear();

            // 3. Everything is selected, right click on the text
            var textCenter = getElementCenter("text");
            mouseClick(webEngineView, textCenter.x, textCenter.y, Qt.RightButton);
            contextMenuRequestedSpy.wait();
            compare(contextMenuRequestedSpy.count, 1);

            compare(linkText, "");
            compare(mediaType, ContextMenuRequest.MediaTypeNone);
            compare(selectedText, "Text Link");
        }

        function test_contextMenuMediaType() {
            webEngineView.url = Qt.resolvedUrl("favicon.html");
            verify(webEngineView.waitForLoadSucceeded());
            // 1. Right click on the image
            var center = getElementCenter("image");
            mouseClick(webEngineView, center.x, center.y, Qt.RightButton);
            contextMenuRequestedSpy.wait();
            compare(contextMenuRequestedSpy.count, 1);

            compare(linkText, "");
            compare(mediaType, ContextMenuRequest.MediaTypeImage);
            compare(selectedText, "");
            contextMenuRequestedSpy.clear();

            // 2. Right click out of the image
            mouseClick(webEngineView, center.x + 30, center.y, Qt.RightButton);
            contextMenuRequestedSpy.wait();
            compare(contextMenuRequestedSpy.count, 1);

            compare(linkText, "");
            compare(mediaType, ContextMenuRequest.MediaTypeNone);
            compare(selectedText, "");
        }
    }
}