Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Modifiers To Mouse Events #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions mock-interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
* @param {{ x: number, y: number }} xy The (x,y) coordinates the mouse event should be fired from.
* @param {!HTMLElement} node The node to fire the event on.
*/
function makeMouseEvent(type, xy, node) {
function makeMouseEvent(type, xy, node, options) {
var props = {
bubbles: true,
cancelable: true,
Expand All @@ -117,6 +117,13 @@
// Make this a primary input.
buttons: 1 // http://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
};
if (options) {
props.ctrlKey = options.ctrlKey || false;
props.altKey = options.altKey || false;
props.shiftKey = options.shiftKey || false;
props.metaKey = options.metaKey || false;
}

var e;
if (HAS_NEW_MOUSE) {
e = new MouseEvent(type, props);
Expand All @@ -129,10 +136,10 @@
0, /* screenX */
0, /* screenY */
props.clientX, props.clientY,
false, /*ctrlKey */
false, /*altKey */
false, /*shiftKey */
false, /*metaKey */
options.ctrlKey || false, /*ctrlKey */
options.altKey || false, /*altKey */
options.shiftKey || false, /*shiftKey */
options.metaKey || false, /*metaKey */
0, /*button */
null /*relatedTarget*/);
}
Expand Down Expand Up @@ -199,9 +206,9 @@
* @param {!HTMLElement} node The node to fire the event on.
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the mouse event should be fired from.
*/
function down(node, xy) {
function down(node, xy, options) {
xy = xy || middleOfNode(node);
makeMouseEvent('mousedown', xy, node);
makeMouseEvent('mousedown', xy, node, options);
}

/**
Expand All @@ -212,9 +219,9 @@
* @param {!HTMLElement} node The node to fire the event on.
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the mouse event should be fired from.
*/
function up(node, xy) {
function up(node, xy, options) {
xy = xy || middleOfNode(node);
makeMouseEvent('mouseup', xy, node);
makeMouseEvent('mouseup', xy, node, options);
}

/**
Expand All @@ -223,9 +230,9 @@
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the mouse event should
* be fired from.
*/
function click(node, xy) {
function click(node, xy, options) {
xy = xy || middleOfNode(node);
makeMouseEvent('click', xy, node);
makeMouseEvent('click', xy, node, options);
}

/**
Expand Down Expand Up @@ -268,10 +275,10 @@
touchend(target);
}

down(target);
down(target, options);
Polymer.Base.async(function() {
up(target);
click(target);
up(target, options);
click(target, options);
callback && callback();
});
}
Expand All @@ -297,9 +304,9 @@
touchend(node, xy);
}

down(node, xy);
up(node, xy);
click(node, xy);
down(node, xy, options);
up(node, xy, options);
click(node, xy, options);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions test/mock-interactions.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,47 @@
expect(tapSpy.callCount).to.be.eql(1);
});

test('dispatches tap event with shift pressed', function(done) {
var tapSpy = function(e) {
button.removeEventListener('click', tapSpy);
expect(e.shiftKey).to.be.true;
done();
};
button.addEventListener('click', tapSpy);
MockInteractions.tap(button, {shiftKey: true});
});

test('dispatches tap event with ctrl pressed', function(done) {
var tapSpy = function(e) {
button.removeEventListener('click', tapSpy);
expect(e.ctrlKey).to.be.true;
done();
};
button.addEventListener('click', tapSpy);
MockInteractions.tap(button, {ctrlKey: true});
});

test('dispatches tap event with alt pressed', function(done) {
var tapSpy = function(e) {
button.removeEventListener('click', tapSpy);
expect(e.altKey).to.be.true;
done();
};
button.addEventListener('click', tapSpy);
MockInteractions.tap(button, {altKey: true});
});

test('dispatches tap event with the meta-key pressed', function(done) {
var tapSpy = function(e) {
button.removeEventListener('click', tapSpy);
expect(e.metaKey).to.be.true;
done();
};
button.addEventListener('click', tapSpy);
MockInteractions.tap(button, {metaKey: true});
});


suite('with touch emulation', function() {
test('only dispatches one tap event', function() {
var tapSpy = sinon.spy();
Expand Down