From e42c102493b477c44ae1ef36882b68befcdcbe3a Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Thu, 7 Jan 2016 18:47:33 +0200 Subject: [PATCH] Support contextmenu events using webpage.sendEvent('contextmenu') Phantomjs issue https://github.com/ariya/phantomjs/issues/11429 Fixes #11429 by allowing to send a "contextmenu" event. Intentionally does not map a "click" event using "right" button into contextmenu event because this would be inconsistent with how browsers work. Related issue in Ghostdriver: https://github.com/detro/ghostdriver/issues/125 --- src/webpage.cpp | 24 +++++++++++++++++ test/module/webpage/contextclick-event.js | 32 +++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 test/module/webpage/contextclick-event.js diff --git a/src/webpage.cpp b/src/webpage.cpp index 718f972708..026789cfed 100644 --- a/src/webpage.cpp +++ b/src/webpage.cpp @@ -33,6 +33,7 @@ #include #include +#include #include #include #include @@ -1514,6 +1515,29 @@ void WebPage::sendEvent(const QString& type, const QVariant& arg1, const QVarian return; } + // context click + if (type == "contextmenu") { + QContextMenuEvent::Reason reason = QContextMenuEvent::Mouse; + + // Gather coordinates + if (arg1.isValid() && arg2.isValid()) { + m_mousePos.setX(arg1.toInt()); + m_mousePos.setY(arg2.toInt()); + } + + // Prepare the context menu event + qDebug() << "Context Menu Event:" << eventType << "(" << reason << "," << m_mousePos << ")"; + QContextMenuEvent* event = new QContextMenuEvent(reason, m_mousePos, QCursor::pos(), keyboardModifiers); + + // Post and process events + // Send the context menu event directly to QWebPage::swallowContextMenuEvent which forwards it to JS + // If we fire the event using postEvent to m_customWebPage, it will end up in QWebPagePrivate::contextMenuEvent, + // which will not forward it to JS at all + m_customWebPage->swallowContextMenuEvent(event); + return; + } + + // mouse click events: Qt doesn't provide this as a separate events, // so we compose it with a mousedown/mouseup sequence // mouse doubleclick events: It is not enough to simply send a diff --git a/test/module/webpage/contextclick-event.js b/test/module/webpage/contextclick-event.js new file mode 100644 index 0000000000..d031746e92 --- /dev/null +++ b/test/module/webpage/contextclick-event.js @@ -0,0 +1,32 @@ +test(function () { + var page = require('webpage').create(); + + page.evaluate(function() { + window.addEventListener('contextmenu', function(event) { + window.loggedEvent = window.loggedEvent || {}; + window.loggedEvent.contextmenu = event; + }, false); + }); + page.sendEvent('contextmenu', 42, 217); + + var event = page.evaluate(function() { + return window.loggedEvent; + }); + assert_equals(event.contextmenu.clientX, 42); + assert_equals(event.contextmenu.clientY, 217); + + // click with modifier key + page.evaluate(function() { + window.addEventListener('contextmenu', function(event) { + window.loggedEvent = window.loggedEvent || {}; + window.loggedEvent.contextmenu = event; + }, false); + }); + page.sendEvent('contextmenu', 100, 100, 'left', page.event.modifier.shift); + + var event = page.evaluate(function() { + return window.loggedEvent.contextmenu; + }); + assert_is_true(event.shiftKey); + +}, "context click events");