Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: third_party/polymer/v1_0/components-chromium/iron-test-helpers/mock-interactions.js

Issue 1766433002: Roll Polymer to 1.3.1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /** 1 /**
2 * @license 2 * @license
3 * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. 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://polyme r.github.io/LICENSE.txt 4 * This code may only be used under the BSD style license found at http://polyme r.github.io/LICENSE.txt
5 * The complete set of authors may be found at http://polymer.github.io/AUTHORS. 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/CON TRIBUTORS.txt 6 * The complete set of contributors may be found at http://polymer.github.io/CON TRIBUTORS.txt
7 * Code distributed by Google as part of the polymer project is also 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/PA TENTS.txt 8 * subject to an additional IP rights grant found at http://polymer.github.io/PA TENTS.txt
9 */ 9 */
10 10
11 (function(global) { 11 (function(global) {
12 'use strict'; 12 'use strict';
13 13
14 var HAS_NEW_MOUSE = (function() { 14 var HAS_NEW_MOUSE = (function() {
15 var has = false; 15 var has = false;
16 try { 16 try {
17 has = Boolean(new MouseEvent('x')); 17 has = Boolean(new MouseEvent('x'));
18 } catch (_) {} 18 } catch (_) {}
19 return has; 19 return has;
20 })(); 20 })();
21 21
22 /*
23 * Returns the (x,y) coordinates representing the middle of a node.
24 *
25 * @param {HTMLElement} node An element.
26 */
22 function middleOfNode(node) { 27 function middleOfNode(node) {
23 var bcr = node.getBoundingClientRect(); 28 var bcr = node.getBoundingClientRect();
24 return { 29 return {
25 y: bcr.top + (bcr.height / 2), 30 y: bcr.top + (bcr.height / 2),
26 x: bcr.left + (bcr.width / 2) 31 x: bcr.left + (bcr.width / 2)
27 }; 32 };
28 } 33 }
29 34
35 /*
36 * Returns the (x,y) coordinates representing the top left corner of a node.
37 *
38 * @param {HTMLElement} node An element.
39 */
30 function topLeftOfNode(node) { 40 function topLeftOfNode(node) {
31 var bcr = node.getBoundingClientRect(); 41 var bcr = node.getBoundingClientRect();
32 return { 42 return {
33 y: bcr.top, 43 y: bcr.top,
34 x: bcr.left 44 x: bcr.left
35 }; 45 };
36 } 46 }
37 47
48 /*
49 * Fires a mouse event on a specific node, at a given set of coordinates.
50 * This event bubbles and is cancellable.
51 *
52 * @param {String} type The type of mouse event (such as 'tap' or 'down').
53 * @param {Object} xy The (x,y) coordinates the mouse event should be fired fr om.
54 * @param {HTMLElement} node The node to fire the event on.
55 */
38 function makeEvent(type, xy, node) { 56 function makeEvent(type, xy, node) {
39 var props = { 57 var props = {
40 bubbles: true, 58 bubbles: true,
41 cancelable: true, 59 cancelable: true,
42 clientX: xy.x, 60 clientX: xy.x,
43 clientY: xy.y, 61 clientY: xy.y,
44 // Make this a primary input. 62 // Make this a primary input.
45 buttons: 1 // http://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/b uttons 63 buttons: 1 // http://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/b uttons
46 }; 64 };
47 var e; 65 var e;
(...skipping 12 matching lines...) Expand all
60 false, /*ctrlKey */ 78 false, /*ctrlKey */
61 false, /*altKey */ 79 false, /*altKey */
62 false, /*shiftKey */ 80 false, /*shiftKey */
63 false, /*metaKey */ 81 false, /*metaKey */
64 0, /*button */ 82 0, /*button */
65 null /*relatedTarget*/); 83 null /*relatedTarget*/);
66 } 84 }
67 node.dispatchEvent(e); 85 node.dispatchEvent(e);
68 } 86 }
69 87
70 function down(node, xy) { 88 /*
71 xy = xy || middleOfNode(node); 89 * Simulates a mouse move action by firing a `move` mouse event on a
72 makeEvent('down', xy, node); 90 * specific node, between a set of coordinates.
73 } 91 *
74 92 * @param {HTMLElement} node The node to fire the event on.
93 * @param {Object} fromXY The (x,y) coordinates the dragging should start from .
94 * @param {Object} toXY The (x,y) coordinates the dragging should end at.
95 * @param {Object} steps Optional. The numbers of steps in the move motion.
96 * If not specified, the default is 5.
97 */
75 function move(node, fromXY, toXY, steps) { 98 function move(node, fromXY, toXY, steps) {
76 steps = steps || 5; 99 steps = steps || 5;
77 var dx = Math.round((fromXY.x - toXY.x) / steps); 100 var dx = Math.round((fromXY.x - toXY.x) / steps);
78 var dy = Math.round((fromXY.y - toXY.y) / steps); 101 var dy = Math.round((fromXY.y - toXY.y) / steps);
79 var xy = { 102 var xy = {
80 x: fromXY.x, 103 x: fromXY.x,
81 y: fromXY.y 104 y: fromXY.y
82 }; 105 };
83 for (var i = steps; i > 0; i--) { 106 for (var i = steps; i > 0; i--) {
84 makeEvent('move', xy, node); 107 makeEvent('move', xy, node);
85 xy.x += dx; 108 xy.x += dx;
86 xy.y += dy; 109 xy.y += dy;
87 } 110 }
88 makeEvent('move', { 111 makeEvent('move', {
89 x: toXY.x, 112 x: toXY.x,
90 y: toXY.y 113 y: toXY.y
91 }, node); 114 }, node);
92 } 115 }
93 116
117 /*
118 * Simulates a mouse dragging action originating in the middle of a specific n ode.
119 *
120 * @param {HTMLElement} target The node to fire the event on.
121 * @param {Number} dx The horizontal displacement.
122 * @param {Object} dy The vertical displacement
123 * @param {Object} steps Optional. The numbers of steps in the dragging motion .
124 * If not specified, the default is 5.
125 */
126 function track(target, dx, dy, steps) {
127 dx = dx | 0;
128 dy = dy | 0;
129 steps = steps || 5;
130 down(target);
131 var xy = middleOfNode(target);
132 var xy2 = {
133 x: xy.x + dx,
134 y: xy.y + dy
135 };
136 move(target, xy, xy2, steps);
137 up(target, xy2);
138 }
139
140 /*
141 * Fires a `down` mouse event on a specific node, at a given set of coordinate s.
142 * This event bubbles and is cancellable. If the (x,y) coordinates are
143 * not specified, the middle of the node will be used instead.
144 *
145 * @param {HTMLElement} node The node to fire the event on.
146 * @param {Object} xy Optional. The (x,y) coordinates the mouse event should b e fired from.
147 */
148 function down(node, xy) {
149 xy = xy || middleOfNode(node);
150 makeEvent('down', xy, node);
151 }
152
153 /*
154 * Fires an `up` mouse event on a specific node, at a given set of coordinates .
155 * This event bubbles and is cancellable. If the (x,y) coordinates are
156 * not specified, the middle of the node will be used instead.
157 *
158 * @param {HTMLElement} node The node to fire the event on.
159 * @param {Object} xy Optional. The (x,y) coordinates the mouse event should b e fired from.
160 */
94 function up(node, xy) { 161 function up(node, xy) {
95 xy = xy || middleOfNode(node); 162 xy = xy || middleOfNode(node);
96 makeEvent('up', xy, node); 163 makeEvent('up', xy, node);
97 } 164 }
98 165
166 /*
167 * Simulates a complete mouse click by firing a `down` mouse event, followed
168 * by an asynchronous `up` and `tap` events on a specific node. Calls the
169 *`callback` after the `tap` event is fired.
170 *
171 * @param {HTMLElement} target The node to fire the event on.
172 * @param {Object} callback Optional. The function to be called after the acti on ends.
173 */
174 function downAndUp(target, callback) {
175 down(target);
176 Polymer.Base.async(function() {
177 up(target);
178 tap(target);
179
180 callback && callback();
181 });
182 }
183
184 /*
185 * Fires a 'tap' mouse event on a specific node. This respects the pointer-eve nts
186 * set on the node, and will not fire on disabled nodes.
187 *
188 * @param {HTMLElement} node The node to fire the event on.
189 * @param {Object} xy Optional. The (x,y) coordinates the mouse event should b e fired from.
190 */
99 function tap(node) { 191 function tap(node) {
100 // Respect nodes that are disabled in the UI. 192 // Respect nodes that are disabled in the UI.
101 if (window.getComputedStyle(node)['pointer-events'] === 'none') 193 if (window.getComputedStyle(node)['pointer-events'] === 'none')
102 return; 194 return;
103 var xy = middleOfNode(node); 195 var xy = middleOfNode(node);
104 down(node, xy); 196 down(node, xy);
105 up(node, xy); 197 up(node, xy);
106 makeEvent('tap', xy, node); 198 makeEvent('tap', xy, node);
107 } 199 }
108 200
201 /*
202 * Focuses a node by firing a `focus` event. This event does not bubble.
203 *
204 * @param {HTMLElement} target The node to fire the event on.
205 */
109 function focus(target) { 206 function focus(target) {
110 Polymer.Base.fire('focus', {}, { 207 Polymer.Base.fire('focus', {}, {
111 bubbles: false, 208 bubbles: false,
112 node: target 209 node: target
113 }); 210 });
114 } 211 }
115 212
213 /*
214 * Blurs a node by firing a `blur` event. This event does not bubble.
215 *
216 * @param {HTMLElement} target The node to fire the event on.
217 */
116 function blur(target) { 218 function blur(target) {
117 Polymer.Base.fire('blur', {}, { 219 Polymer.Base.fire('blur', {}, {
118 bubbles: false, 220 bubbles: false,
119 node: target 221 node: target
120 }); 222 });
121 } 223 }
122 224
123 function downAndUp(target, callback) { 225 /*
124 down(target); 226 * Returns a keyboard event. This event bubbles and is cancellable.
125 Polymer.Base.async(function() { 227 *
126 up(target); 228 * @param {String} type The type of keyboard event (such as 'keyup' or 'keydow n').
127 tap(target); 229 * @param {Number} keyCode The keyCode for the event.
128 230 * @param {?String|[String]} modifiers The key modifiers for the event.
129 callback && callback(); 231 * Accepted values are shift, ctrl, alt, meta.
130 }); 232 */
131 } 233 function keyboardEventFor(type, keyCode, modifiers) {
132
133 function track(target, dx, dy, steps) {
134 dx = dx | 0;
135 dy = dy | 0;
136 steps = steps || 5;
137 down(target);
138 var xy = middleOfNode(target);
139 var xy2 = {
140 x: xy.x + dx,
141 y: xy.y + dy
142 };
143 move(target, xy, xy2, steps);
144 up(target, xy2);
145 }
146
147 function keyboardEventFor(type, keyCode) {
148 var event = new CustomEvent(type, { 234 var event = new CustomEvent(type, {
149 bubbles: true, 235 bubbles: true,
150 cancelable: true 236 cancelable: true
151 }); 237 });
152 238
153 event.keyCode = keyCode; 239 event.keyCode = keyCode;
154 event.code = keyCode; 240 event.code = keyCode;
155 241
242 modifiers = modifiers || [];
243 if (typeof modifiers === 'string') {
244 modifiers = [modifiers];
245 }
246 event.shiftKey = modifiers.indexOf('shift') !== -1;
247 event.altKey = modifiers.indexOf('alt') !== -1;
248 event.ctrlKey = modifiers.indexOf('ctrl') !== -1;
249 event.metaKey = modifiers.indexOf('meta') !== -1;
250
156 return event; 251 return event;
157 } 252 }
158 253
159 function keyEventOn(target, type, keyCode) { 254 /*
160 target.dispatchEvent(keyboardEventFor(type, keyCode)); 255 * Fires a keyboard event on a specific node. This event bubbles and is cancel lable.
256 *
257 * @param {HTMLElement} target The node to fire the event on.
258 * @param {String} type The type of keyboard event (such as 'keyup' or 'keydow n').
259 * @param {Number} keyCode The keyCode for the event.
260 * @param {?String|[String]} modifiers The key modifiers for the event.
261 * Accepted values are shift, ctrl, alt, meta.
262 */
263 function keyEventOn(target, type, keyCode, modifiers) {
264 target.dispatchEvent(keyboardEventFor(type, keyCode, modifiers));
161 } 265 }
162 266
163 function keyDownOn(target, keyCode) { 267 /*
164 keyEventOn(target, 'keydown', keyCode); 268 * Fires a 'keydown' event on a specific node. This event bubbles and is cance llable.
269 *
270 * @param {HTMLElement} target The node to fire the event on.
271 * @param {Number} keyCode The keyCode for the event.
272 * @param {?String|[String]} modifiers The key modifiers for the event.
273 * Accepted values are shift, ctrl, alt, meta.
274 */
275 function keyDownOn(target, keyCode, modifiers) {
276 keyEventOn(target, 'keydown', keyCode, modifiers);
165 } 277 }
166 278
167 function keyUpOn(target, keyCode) { 279 /*
168 keyEventOn(target, 'keyup', keyCode); 280 * Fires a 'keyup' event on a specific node. This event bubbles and is cancell able.
281 *
282 * @param {HTMLElement} target The node to fire the event on.
283 * @param {Number} keyCode The keyCode for the event.
284 * @param {?String|[String]} modifiers The key modifiers for the event.
285 * Accepted values are shift, ctrl, alt, meta.
286 */
287 function keyUpOn(target, keyCode, modifiers) {
288 keyEventOn(target, 'keyup', keyCode, modifiers);
169 } 289 }
170 290
171 function pressAndReleaseKeyOn(target, keyCode) { 291 /*
172 keyDownOn(target, keyCode); 292 * Simulates a complete key press by firing a `keydown` keyboard event, follow ed
293 * by an asynchronous `keyup` event on a specific node.
294 *
295 * @param {HTMLElement} target The node to fire the event on.
296 * @param {Number} keyCode The keyCode for the event.
297 * @param {?String|[String]} modifiers The key modifiers for the event.
298 * Accepted values are shift, ctrl, alt, meta.
299 */
300 function pressAndReleaseKeyOn(target, keyCode, modifiers) {
301 keyDownOn(target, keyCode, modifiers);
173 Polymer.Base.async(function() { 302 Polymer.Base.async(function() {
174 keyUpOn(target, keyCode); 303 keyUpOn(target, keyCode, modifiers);
175 }, 1); 304 }, 1);
176 } 305 }
177 306
307 /*
308 * Simulates a complete 'enter' key press by firing a `keydown` keyboard event ,
309 * followed by an asynchronous `keyup` event on a specific node.
310 *
311 * @param {HTMLElement} target The node to fire the event on.
312 */
178 function pressEnter(target) { 313 function pressEnter(target) {
179 pressAndReleaseKeyOn(target, 13); 314 pressAndReleaseKeyOn(target, 13);
180 } 315 }
181 316
317 /*
318 * Simulates a complete 'space' key press by firing a `keydown` keyboard event ,
319 * followed by an asynchronous `keyup` event on a specific node.
320 *
321 * @param {HTMLElement} target The node to fire the event on.
322 */
182 function pressSpace(target) { 323 function pressSpace(target) {
183 pressAndReleaseKeyOn(target, 32); 324 pressAndReleaseKeyOn(target, 32);
184 } 325 }
185 326
186 global.MockInteractions = { 327 global.MockInteractions = {
187 focus: focus, 328 focus: focus,
188 blur: blur, 329 blur: blur,
189 down: down, 330 down: down,
190 up: up, 331 up: up,
191 downAndUp: downAndUp, 332 downAndUp: downAndUp,
192 tap: tap, 333 tap: tap,
193 track: track, 334 track: track,
194 pressAndReleaseKeyOn: pressAndReleaseKeyOn, 335 pressAndReleaseKeyOn: pressAndReleaseKeyOn,
195 pressEnter: pressEnter, 336 pressEnter: pressEnter,
196 pressSpace: pressSpace, 337 pressSpace: pressSpace,
197 keyDownOn: keyDownOn, 338 keyDownOn: keyDownOn,
198 keyUpOn: keyUpOn, 339 keyUpOn: keyUpOn,
340 keyEventOn: keyEventOn,
199 middleOfNode: middleOfNode, 341 middleOfNode: middleOfNode,
200 topLeftOfNode: topLeftOfNode 342 topLeftOfNode: topLeftOfNode
201 }; 343 };
202 })(this); 344 })(this);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698