Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Support contextmenu events using webpage.sendEvent('contextmenu')
Browse files Browse the repository at this point in the history
Phantomjs issue
#10688

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
  • Loading branch information
Artur- committed Jan 9, 2016
1 parent 14d53ce commit d7680b6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/webpage.cpp
Expand Up @@ -33,6 +33,7 @@
#include <math.h>

#include <QApplication>
#include <QContextMenuEvent>
#include <QDesktopServices>
#include <QDateTime>
#include <QDir>
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions 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");

0 comments on commit d7680b6

Please sign in to comment.