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 var xy = middleOfNode(node);
90 makeEvent('tap', xy, node);
93 function focus(target) {
94 Polymer.Base.fire.call(target, 'focus');
97 function blur(target) {
98 Polymer.Base.fire.call(target, 'blur');
101 function downAndUp(target, callback) {
103 Polymer.Base.async(function() {
107 callback && callback();
111 function track(target, dx, dy, steps) {
116 var xy = middleOfNode(target);
121 move(target, xy, xy2, steps);
125 function keyboardEventFor(type, keyCode) {
126 var event = new CustomEvent(type);
128 event.keyCode = keyCode;
129 event.code = keyCode;
134 function keyEventOn(target, type, keyCode) {
135 target.dispatchEvent(keyboardEventFor(type, keyCode));
138 function keyDownOn(target, keyCode) {
139 keyEventOn(target, 'keydown', keyCode);
142 function keyUpOn(target, keyCode) {
143 keyEventOn(target, 'keyup', keyCode);
146 function pressAndReleaseKeyOn(target, keyCode) {
147 keyDownOn(target, keyCode);
148 Polymer.Base.async(function() {
149 keyUpOn(target, keyCode);
153 function pressEnter(target) {
154 pressAndReleaseKeyOn(target, 13);
157 function pressSpace(target) {
158 pressAndReleaseKeyOn(target, 32);
161 global.MockInteractions = {
166 downAndUp: downAndUp,
169 pressAndReleaseKeyOn: pressAndReleaseKeyOn,
170 pressEnter: pressEnter,
171 pressSpace: pressSpace,
172 keyDownOn: keyDownOn,
174 middleOfNode: middleOfNode,
175 topLeftOfNode: topLeftOfNode