OLD | NEW |
(Empty) | |
| 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://polyme
r.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/CON
TRIBUTORS.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/PA
TENTS.txt |
| 9 */ |
| 10 |
| 11 (function(global) { |
| 12 'use strict'; |
| 13 |
| 14 var HAS_NEW_MOUSE = (function() { |
| 15 var has = false; |
| 16 try { |
| 17 has = Boolean(new MouseEvent('x')); |
| 18 } catch (_) {} |
| 19 return has; |
| 20 })(); |
| 21 |
| 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) |
| 27 }; |
| 28 } |
| 29 |
| 30 function topLeftOfNode(node) { |
| 31 var bcr = node.getBoundingClientRect(); |
| 32 return { |
| 33 y: bcr.top, |
| 34 x: bcr.left |
| 35 }; |
| 36 } |
| 37 |
| 38 function makeEvent(type, xy, node) { |
| 39 var props = { |
| 40 bubbles: true, |
| 41 cancelable: true, |
| 42 clientX: xy.x, |
| 43 clientY: xy.y |
| 44 }; |
| 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); |
| 53 } |
| 54 node.dispatchEvent(e); |
| 55 } |
| 56 |
| 57 function down(node, xy) { |
| 58 xy = xy || middleOfNode(node); |
| 59 makeEvent('down', xy, node); |
| 60 } |
| 61 |
| 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 |
| 69 }; |
| 70 for (var i = steps; i > 0; i--) { |
| 71 makeEvent('move', xy, node); |
| 72 xy.x += dx; |
| 73 xy.y += dy; |
| 74 } |
| 75 makeEvent('move', { |
| 76 x: toXY.x, |
| 77 y: toXY.y |
| 78 }, node); |
| 79 } |
| 80 |
| 81 function up(node, xy) { |
| 82 xy = xy || middleOfNode(node); |
| 83 makeEvent('up', xy, node); |
| 84 } |
| 85 |
| 86 function tap(node) { |
| 87 var xy = middleOfNode(node); |
| 88 down(node, xy); |
| 89 up(node, xy); |
| 90 makeEvent('tap', xy, node); |
| 91 } |
| 92 |
| 93 function focus(target) { |
| 94 Polymer.Base.fire.call(target, 'focus'); |
| 95 } |
| 96 |
| 97 function blur(target) { |
| 98 Polymer.Base.fire.call(target, 'blur'); |
| 99 } |
| 100 |
| 101 function downAndUp(target, callback) { |
| 102 down(target); |
| 103 Polymer.Base.async(function() { |
| 104 up(target); |
| 105 tap(target); |
| 106 |
| 107 callback && callback(); |
| 108 }); |
| 109 } |
| 110 |
| 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 |
| 120 }; |
| 121 move(target, xy, xy2, steps); |
| 122 up(target, xy2); |
| 123 } |
| 124 |
| 125 function keyboardEventFor(type, keyCode) { |
| 126 var event = new CustomEvent(type); |
| 127 |
| 128 event.keyCode = keyCode; |
| 129 event.code = keyCode; |
| 130 |
| 131 return event; |
| 132 } |
| 133 |
| 134 function keyEventOn(target, type, keyCode) { |
| 135 target.dispatchEvent(keyboardEventFor(type, keyCode)); |
| 136 } |
| 137 |
| 138 function keyDownOn(target, keyCode) { |
| 139 keyEventOn(target, 'keydown', keyCode); |
| 140 } |
| 141 |
| 142 function keyUpOn(target, keyCode) { |
| 143 keyEventOn(target, 'keyup', keyCode); |
| 144 } |
| 145 |
| 146 function pressAndReleaseKeyOn(target, keyCode) { |
| 147 keyDownOn(target, keyCode); |
| 148 Polymer.Base.async(function() { |
| 149 keyUpOn(target, keyCode); |
| 150 }, 1); |
| 151 } |
| 152 |
| 153 function pressEnter(target) { |
| 154 pressAndReleaseKeyOn(target, 13); |
| 155 } |
| 156 |
| 157 function pressSpace(target) { |
| 158 pressAndReleaseKeyOn(target, 32); |
| 159 } |
| 160 |
| 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 |
| 176 }; |
| 177 })(this); |
OLD | NEW |