ApplicationImpl cleanup, part 1:
[chromium-blink-merge.git] / third_party / polymer / v1_0 / components / iron-test-helpers / mock-interactions.js
blob887b9bee1aa7663f50e45b02955a8d757aad8519
1 /**
2 * @license
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
9 */
11 (function(global) {
12 'use strict';
14 var HAS_NEW_MOUSE = (function() {
15 var has = false;
16 try {
17 has = Boolean(new MouseEvent('x'));
18 } catch (_) {}
19 return has;
20 })();
22 function middleOfNode(node) {
23 var bcr = node.getBoundingClientRect();
24 return {
25 y: bcr.top + (bcr.height / 2),
26 x: bcr.left + (bcr.width / 2)
30 function topLeftOfNode(node) {
31 var bcr = node.getBoundingClientRect();
32 return {
33 y: bcr.top,
34 x: bcr.left
38 function makeEvent(type, xy, node) {
39 var props = {
40 bubbles: true,
41 cancelable: true,
42 clientX: xy.x,
43 clientY: xy.y
45 var e;
46 var mousetype = type === 'tap' ? 'click' : 'mouse' + type;
47 if (HAS_NEW_MOUSE) {
48 e = new MouseEvent(mousetype, props);
49 } else {
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) {
63 steps = steps || 5;
64 var dx = Math.round((fromXY.x - toXY.x) / steps);
65 var dy = Math.round((fromXY.y - toXY.y) / steps);
66 var xy = {
67 x: fromXY.x,
68 y: fromXY.y
70 for (var i = steps; i > 0; i--) {
71 makeEvent('move', xy, node);
72 xy.x += dx;
73 xy.y += dy;
75 makeEvent('move', {
76 x: toXY.x,
77 y: toXY.y
78 }, node);
81 function up(node, xy) {
82 xy = xy || middleOfNode(node);
83 makeEvent('up', xy, node);
86 function tap(node) {
87 var xy = middleOfNode(node);
88 down(node, xy);
89 up(node, xy);
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) {
102 down(target);
103 Polymer.Base.async(function() {
104 up(target);
105 tap(target);
107 callback && callback();
111 function track(target, dx, dy, steps) {
112 dx = dx | 0;
113 dy = dy | 0;
114 steps = steps || 5;
115 down(target);
116 var xy = middleOfNode(target);
117 var xy2 = {
118 x: xy.x + dx,
119 y: xy.y + dy
121 move(target, xy, xy2, steps);
122 up(target, xy2);
125 function keyboardEventFor(type, keyCode) {
126 var event = new CustomEvent(type);
128 event.keyCode = keyCode;
129 event.code = keyCode;
131 return event;
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);
150 }, 1);
153 function pressEnter(target) {
154 pressAndReleaseKeyOn(target, 13);
157 function pressSpace(target) {
158 pressAndReleaseKeyOn(target, 32);
161 global.MockInteractions = {
162 focus: focus,
163 blur: blur,
164 down: down,
165 up: up,
166 downAndUp: downAndUp,
167 tap: tap,
168 track: track,
169 pressAndReleaseKeyOn: pressAndReleaseKeyOn,
170 pressEnter: pressEnter,
171 pressSpace: pressSpace,
172 keyDownOn: keyDownOn,
173 keyUpOn: keyUpOn,
174 middleOfNode: middleOfNode,
175 topLeftOfNode: topLeftOfNode
177 })(this);