From 78cc35d0705ef9f7b3e7785f031f77595ed71217 Mon Sep 17 00:00:00 2001 From: Valentin Fokin Date: Mon, 13 Nov 2017 10:09:13 +0100 Subject: Add QML autotests for ContextMenu Change-Id: If18bb163744b9064c0e4d97a9476851b6dee39ad Reviewed-by: Qt CI Bot Reviewed-by: Peter Varga --- .../auto/quick/qmltests/data/TestWebEngineView.qml | 20 +- tests/auto/quick/qmltests/data/favicon.html | 2 +- tests/auto/quick/qmltests/data/tst_contextMenu.qml | 224 +++++++++++++++++++++ tests/auto/quick/qmltests/data/tst_mouseClick.qml | 18 -- .../QtWebEngine/Controls1Delegates/Menu.qml | 57 ++++++ .../QtWebEngine/Controls1Delegates/MenuItem.qml | 44 ++++ tests/auto/quick/qmltests/qmltests.pro | 3 + 7 files changed, 348 insertions(+), 20 deletions(-) create mode 100644 tests/auto/quick/qmltests/data/tst_contextMenu.qml create mode 100644 tests/auto/quick/qmltests/mock-delegates/QtWebEngine/Controls1Delegates/Menu.qml create mode 100644 tests/auto/quick/qmltests/mock-delegates/QtWebEngine/Controls1Delegates/MenuItem.qml (limited to 'tests/auto/quick/qmltests') diff --git a/tests/auto/quick/qmltests/data/TestWebEngineView.qml b/tests/auto/quick/qmltests/data/TestWebEngineView.qml index 8304a993a..2ce8f3026 100644 --- a/tests/auto/quick/qmltests/data/TestWebEngineView.qml +++ b/tests/auto/quick/qmltests/data/TestWebEngineView.qml @@ -28,7 +28,7 @@ import QtQuick 2.0 import QtTest 1.1 -import QtWebEngine 1.3 +import QtWebEngine 1.6 WebEngineView { property var loadStatus: null @@ -82,6 +82,24 @@ WebEngineView { verifyElementHasFocus(element); } + function getElementCenter(element) { + var center; + runJavaScript("(function() {" + + " var elem = document.getElementById('" + element + "');" + + " var rect = elem.getBoundingClientRect();" + + " return { 'x': (rect.left + rect.right) / 2, 'y': (rect.top + rect.bottom) / 2 };" + + "})();", function(result) { center = result } ); + testCase.tryVerify(function() { return center !== undefined; }); + return center; + } + + function getTextSelection() { + var textSelection; + runJavaScript("window.getSelection().toString()", function(result) { textSelection = result }); + testCase.tryVerify(function() { return textSelection !== undefined; }); + return textSelection; + } + TestResult { id: testResult } TestCase { id: testCase } diff --git a/tests/auto/quick/qmltests/data/favicon.html b/tests/auto/quick/qmltests/data/favicon.html index 9823fa323..7a81194e4 100644 --- a/tests/auto/quick/qmltests/data/favicon.html +++ b/tests/auto/quick/qmltests/data/favicon.html @@ -5,6 +5,6 @@

It's expected that you see a favicon displayed for this page when you open it as a local file.

The favicon looks like this:

- + diff --git a/tests/auto/quick/qmltests/data/tst_contextMenu.qml b/tests/auto/quick/qmltests/data/tst_contextMenu.qml new file mode 100644 index 000000000..58947beaf --- /dev/null +++ b/tests/auto/quick/qmltests/data/tst_contextMenu.qml @@ -0,0 +1,224 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import QtQuick.Controls 1.4 +import QtTest 1.0 +import QtWebEngine 1.6 + +TestWebEngineView { + id: webEngineView + width: 400 + height: 400 + + property string linkText: "" + property var mediaType: null + property string selectedText: "" + + onContextMenuRequested: { + linkText = request.linkText; + mediaType = request.mediaType; + selectedText = request.selectedText; + } + + SignalSpy { + id: contextMenuRequestedSpy + target: webEngineView + signalName: "contextMenuRequested" + } + + function getContextMenus() { + var data = webEngineView.data; + var contextMenus = []; + + for (var i = 0; i < data.length; i++) { + if (data[i].type == MenuItemType.Menu) { + contextMenus.push(data[i]); + } + } + return contextMenus; + } + + function destroyContextMenu() { + contextMenuTest.keyPress(Qt.Key_Escape); + return getContextMenus().length == 0; + } + + TestCase { + id: contextMenuTest + name: "WebEngineViewContextMenu" + when: windowShown + + function init() { + var contextMenus = getContextMenus(); + compare(contextMenus.length, 0); + } + + function cleanup() { + contextMenuRequestedSpy.clear(); + } + + function test_contextMenu_data() { + return [ + { tag: "defaultContextMenu", userHandled: false, accepted: false }, + { tag: "defaultContextMenuWithConnect", userHandled: true, accepted: false }, + { tag: "dontShowDefaultContextMenu", userHandled: true, accepted: true }, + ]; + } + + function test_contextMenu(row) { + if (Qt.platform.os == "osx") { + skip("When the menu pops up on macOS, it does not return and the test fails after time out."); + } + + function contextMenuHandler(request) { + request.accepted = row.accepted; + } + + if (row.userHandled) { + webEngineView.contextMenuRequested.connect(contextMenuHandler); + } + + mouseClick(webEngineView, 20, 20, Qt.RightButton); + contextMenuRequestedSpy.wait(); + compare(contextMenuRequestedSpy.count, 1); + + // There should be maximum one ContextMenu present at a time + var contextMenus = getContextMenus(); + verify(contextMenus.length <= 1); + compare(contextMenus[0] != null, !row.accepted); + + // FIXME: Sometimes the keyPress(Qt.Key_Escape) event isn't caught so we keep trying + tryVerify(destroyContextMenu); + webEngineView.contextMenuRequested.disconnect(contextMenuHandler); + } + + function test_contextMenuLinkAndSelectedText() { + if (Qt.platform.os == "osx") { + skip("When the menu pops up on macOS, it does not return and the test fails after time out."); + } + + webEngineView.loadHtml("" + + "Text " + + "Link" + + ""); + 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); + + var contextMenus = getContextMenus(); + compare(contextMenus.length, 1); + verify(contextMenus[0]); + compare(linkText, "Link"); + compare(mediaType, ContextMenuRequest.MediaTypeNone); + compare(selectedText, ""); + + contextMenuRequestedSpy.clear(); + // FIXME: Sometimes the keyPress(Qt.Key_Escape) event isn't caught so we keep trying + tryVerify(destroyContextMenu); + + // 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); + + contextMenus = getContextMenus(); + compare(contextMenus.length, 1); + verify(contextMenus[0]); + compare(linkText, "Link"); + compare(mediaType, ContextMenuRequest.MediaTypeNone); + compare(selectedText, "Text Link"); + + contextMenuRequestedSpy.clear(); + // FIXME: Sometimes the keyPress(Qt.Key_Escape) event isn't caught so we keep trying + tryVerify(destroyContextMenu); + + // 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); + + contextMenus = getContextMenus(); + compare(contextMenus.length, 1); + verify(contextMenus[0]); + compare(linkText, ""); + compare(mediaType, ContextMenuRequest.MediaTypeNone); + compare(selectedText, "Text Link"); + + // FIXME: Sometimes the keyPress(Qt.Key_Escape) event isn't caught so we keep trying + tryVerify(destroyContextMenu); + } + + function test_contextMenuMediaType() { + if (Qt.platform.os == "osx") { + skip("When the menu pops up on macOS, it does not return and the test fails after time out."); + } + + 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); + + var contextMenus = getContextMenus(); + compare(contextMenus.length, 1); + verify(contextMenus[0]); + compare(linkText, ""); + compare(mediaType, ContextMenuRequest.MediaTypeImage); + compare(selectedText, ""); + contextMenuRequestedSpy.clear(); + // FIXME: Sometimes the keyPress(Qt.Key_Escape) event isn't caught so we keep trying + tryVerify(destroyContextMenu); + + // 2. Right click out of the image + mouseClick(webEngineView, center.x + 30, center.y, Qt.RightButton); + contextMenuRequestedSpy.wait(); + compare(contextMenuRequestedSpy.count, 1); + + contextMenus = getContextMenus(); + compare(contextMenus.length, 1); + verify(contextMenus[0]); + compare(linkText, ""); + compare(mediaType, ContextMenuRequest.MediaTypeNone); + compare(selectedText, ""); + + // FIXME: Sometimes the keyPress(Qt.Key_Escape) event isn't caught so we keep trying + tryVerify(destroyContextMenu); + } + } +} diff --git a/tests/auto/quick/qmltests/data/tst_mouseClick.qml b/tests/auto/quick/qmltests/data/tst_mouseClick.qml index 527102f59..bd6625a90 100644 --- a/tests/auto/quick/qmltests/data/tst_mouseClick.qml +++ b/tests/auto/quick/qmltests/data/tst_mouseClick.qml @@ -64,24 +64,6 @@ TestWebEngineView { name: "WebEngineViewMouseClick" when: windowShown - function getElementCenter(element) { - var center; - runJavaScript("(function() {" + - " var elem = document.getElementById('" + element + "');" + - " var rect = elem.getBoundingClientRect();" + - " return { 'x': (rect.left + rect.right) / 2, 'y': (rect.top + rect.bottom) / 2 };" + - "})();", function(result) { center = result } ); - tryVerify(function() { return center != undefined; }); - return center; - } - - function getTextSelection() { - var textSelection; - runJavaScript("window.getSelection().toString()", function(result) { textSelection = result }); - tryVerify(function() { return textSelection != undefined; }); - return textSelection; - } - function test_singleClick() { webEngineView.settings.focusOnNavigationEnabled = false; webEngineView.loadHtml("" + diff --git a/tests/auto/quick/qmltests/mock-delegates/QtWebEngine/Controls1Delegates/Menu.qml b/tests/auto/quick/qmltests/mock-delegates/QtWebEngine/Controls1Delegates/Menu.qml new file mode 100644 index 000000000..36efa7680 --- /dev/null +++ b/tests/auto/quick/qmltests/mock-delegates/QtWebEngine/Controls1Delegates/Menu.qml @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.5 +import QtQuick.Controls 1.4 as Controls + +Controls.Menu { + id: menu + signal done() + + // Use private API for now + onAboutToHide: doneTimer.start() + + // WORKAROUND On Mac the Menu may be destroyed before the MenuItem + // is actually triggered (see qtbase commit 08cc9b9991ae9ab51) + Timer { + id: doneTimer + interval: 100 + onTriggered: menu.done() + } +} diff --git a/tests/auto/quick/qmltests/mock-delegates/QtWebEngine/Controls1Delegates/MenuItem.qml b/tests/auto/quick/qmltests/mock-delegates/QtWebEngine/Controls1Delegates/MenuItem.qml new file mode 100644 index 000000000..e61f4c230 --- /dev/null +++ b/tests/auto/quick/qmltests/mock-delegates/QtWebEngine/Controls1Delegates/MenuItem.qml @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.5 +import QtQuick.Controls 1.4 as Controls + +Controls.MenuItem { } + diff --git a/tests/auto/quick/qmltests/qmltests.pro b/tests/auto/quick/qmltests/qmltests.pro index 5014fd6e3..16ecbc1bc 100644 --- a/tests/auto/quick/qmltests/qmltests.pro +++ b/tests/auto/quick/qmltests/qmltests.pro @@ -43,6 +43,7 @@ OTHER_FILES += \ $$PWD/data/keyboardEvents.html \ $$PWD/data/titleupdate.js \ $$PWD/data/tst_activeFocusOnPress.qml \ + $$PWD/data/tst_contextMenu.qml \ $$PWD/data/tst_desktopBehaviorLoadHtml.qml \ $$PWD/data/tst_download.qml \ $$PWD/data/tst_favicon.qml \ @@ -92,6 +93,8 @@ OTHER_FILES += \ $$PWD/mock-delegates/QtWebEngine/Controls1Delegates/AlertDialog.qml \ $$PWD/mock-delegates/QtWebEngine/Controls1Delegates/ConfirmDialog.qml \ $$PWD/mock-delegates/QtWebEngine/Controls1Delegates/FilePicker.qml \ + $$PWD/mock-delegates/QtWebEngine/Controls1Delegates/Menu.qml \ + $$PWD/mock-delegates/QtWebEngine/Controls1Delegates/MenuItem.qml \ $$PWD/mock-delegates/QtWebEngine/Controls1Delegates/PromptDialog.qml \ $$PWD/mock-delegates/QtWebEngine/Controls1Delegates/qmldir \ $$PWD/mock-delegates/TestParams/FilePickerParams.qml \ -- cgit v1.2.3