diff --git a/mock-interactions.js b/mock-interactions.js index 0c32c7b..c41f76f 100644 --- a/mock-interactions.js +++ b/mock-interactions.js @@ -1,27 +1,104 @@ +/** + * @license + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + (function(global) { 'use strict'; - function focus (target) { - Polymer.Base.fire.call(target, 'focus'); + var HAS_NEW_MOUSE = (function() { + var has = false; + try { + has = Boolean(new MouseEvent('x')); + } catch (_) {} + return has; + })(); + + function middleOfNode(node) { + var bcr = node.getBoundingClientRect(); + return { + y: bcr.top + (bcr.height / 2), + x: bcr.left + (bcr.width / 2) + }; } - function blur (target) { - Polymer.Base.fire.call(target, 'blur'); + function topLeftOfNode(node) { + var bcr = node.getBoundingClientRect(); + return { + y: bcr.top, + x: bcr.left + }; + } + + function makeEvent(type, xy, node) { + var props = { + bubbles: true, + cancelable: true, + clientX: xy.x, + clientY: xy.y + }; + var e; + var mousetype = type === 'tap' ? 'click' : 'mouse' + type; + if (HAS_NEW_MOUSE) { + e = new MouseEvent(mousetype, props); + } else { + e = document.createEvent('MouseEvent'); + e.initMouseEvent(mousetype, props.bubbles, props.cancelable, null, null, 0, 0, + props.clientX, props.clientY, false, false, false, false, 0, null); + } + node.dispatchEvent(e); } - function down (target) { - Polymer.Base.fire.call(target, 'mousedown'); + function down(node, xy) { + xy = xy || middleOfNode(node); + makeEvent('down', xy, node); } - function up (target) { - Polymer.Base.fire.call(target, 'mouseup'); + function move(node, fromXY, toXY, steps) { + steps = steps || 5; + var dx = Math.round((fromXY.x - toXY.x) / steps); + var dy = Math.round((fromXY.y - toXY.y) / steps); + var xy = { + x: fromXY.x, + y: fromXY.y + }; + for (var i = steps; i > 0; i--) { + makeEvent('move', xy, node); + xy.x += dx; + xy.y += dy; + } + makeEvent('move', { + x: toXY.x, + y: toXY.y + }, node); } - function tap(target) { - Polymer.Base.fire.call(target, 'tap'); + function up(node, xy) { + xy = xy || middleOfNode(node); + makeEvent('up', xy, node); } - function downAndUp (target, callback) { + function tap(node) { + var xy = middleOfNode(node); + down(node, xy); + up(node, xy); + makeEvent('tap', xy, node); + } + + function focus(target) { + Polymer.Base.fire.call(target, 'focus'); + } + + function blur(target) { + Polymer.Base.fire.call(target, 'blur'); + } + + function downAndUp(target, callback) { down(target); Polymer.Base.async(function() { up(target); @@ -31,7 +108,21 @@ }); } - function keyboardEventFor (type, keyCode) { + function track(target, dx, dy, steps) { + dx = dx | 0; + dy = dy | 0; + steps = steps || 5; + down(target); + var xy = middleOfNode(target); + var xy2 = { + x: xy.x + dx, + y: xy.y + dy + }; + move(target, xy, xy2); + up(target, xy2); + } + + function keyboardEventFor(type, keyCode) { var event = new CustomEvent(type); event.keyCode = keyCode; @@ -40,30 +131,30 @@ return event; } - function keyEventOn (target, type, keyCode) { + function keyEventOn(target, type, keyCode) { target.dispatchEvent(keyboardEventFor(type, keyCode)); } - function keyDownOn (target, keyCode) { + function keyDownOn(target, keyCode) { keyEventOn(target, 'keydown', keyCode); } - function keyUpOn (target, keyCode) { + function keyUpOn(target, keyCode) { keyEventOn(target, 'keyup', keyCode); } - function pressAndReleaseKeyOn (target, keyCode) { + function pressAndReleaseKeyOn(target, keyCode) { keyDownOn(target, keyCode); - Polymer.Base.async(function () { + Polymer.Base.async(function() { keyUpOn(target, keyCode); }, 1); } - function pressEnter (target) { + function pressEnter(target) { pressAndReleaseKeyOn(target, 13); } - function pressSpace (target) { + function pressSpace(target) { pressAndReleaseKeyOn(target, 32); } @@ -73,6 +164,8 @@ down: down, up: up, downAndUp: downAndUp, + tap: tap, + track: track, pressAndReleaseKeyOn: pressAndReleaseKeyOn, pressEnter: pressEnter, pressSpace: pressSpace