3 * Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6 * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7 * Code distributed by Google as part of the polymer project is also
8 * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
14 var HAS_NEW_MOUSE = (function() {
17 has = Boolean(new MouseEvent('x'));
22 function middleOfNode(node) {
23 var bcr = node.getBoundingClientRect();
25 y: bcr.top + (bcr.height / 2),
26 x: bcr.left + (bcr.width / 2)
30 function topLeftOfNode(node) {
31 var bcr = node.getBoundingClientRect();
38 function makeEvent(type, xy, node) {
46 var mousetype = type === 'tap' ? 'click' : 'mouse' + type;
48 e = new MouseEvent(mousetype, props);
50 e = document.createEvent('MouseEvent');
51 e.initMouseEvent(mousetype, props.bubbles, props.cancelable, null, null, 0, 0,
52 props.clientX, props.clientY, false, false, false, false, 0, null);
54 node.dispatchEvent(e);
57 function down(node, xy) {
58 xy = xy || middleOfNode(node);
59 makeEvent('down', xy, node);
62 function move(node, fromXY, toXY, steps) {
64 var dx = Math.round((fromXY.x - toXY.x) / steps);
65 var dy = Math.round((fromXY.y - toXY.y) / steps);
70 for (var i = steps; i > 0; i--) {
71 makeEvent('move', xy, node);
81 function up(node, xy) {
82 xy = xy || middleOfNode(node);
83 makeEvent('up', xy, node);
87 // Respect nodes that are disabled in the UI.
88 if (window.getComputedStyle(node)['pointer-events'] === 'none')
90 var xy = middleOfNode(node);
93 makeEvent('tap', xy, node);
96 function focus(target) {
97 Polymer.Base.fire.call(target, 'focus', {}, {
102 function blur(target) {
103 Polymer.Base.fire.call(target, 'blur', {}, {
108 function downAndUp(target, callback) {
110 Polymer.Base.async(function() {
114 callback && callback();
118 function track(target, dx, dy, steps) {
123 var xy = middleOfNode(target);
128 move(target, xy, xy2, steps);
132 function keyboardEventFor(type, keyCode) {
133 var event = new CustomEvent(type);
135 event.keyCode = keyCode;
136 event.code = keyCode;
141 function keyEventOn(target, type, keyCode) {
142 target.dispatchEvent(keyboardEventFor(type, keyCode));
145 function keyDownOn(target, keyCode) {
146 keyEventOn(target, 'keydown', keyCode);
149 function keyUpOn(target, keyCode) {
150 keyEventOn(target, 'keyup', keyCode);
153 function pressAndReleaseKeyOn(target, keyCode) {
154 keyDownOn(target, keyCode);
155 Polymer.Base.async(function() {
156 keyUpOn(target, keyCode);
160 function pressEnter(target) {
161 pressAndReleaseKeyOn(target, 13);
164 function pressSpace(target) {
165 pressAndReleaseKeyOn(target, 32);
168 global.MockInteractions = {
173 downAndUp: downAndUp,
176 pressAndReleaseKeyOn: pressAndReleaseKeyOn,
177 pressEnter: pressEnter,
178 pressSpace: pressSpace,
179 keyDownOn: keyDownOn,
181 middleOfNode: middleOfNode,
182 topLeftOfNode: topLeftOfNode