From 81d76f7619f65c08306c53eaf3c105967da79882 Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Wed, 13 May 2015 16:52:39 -0700 Subject: [PATCH 1/3] Port a simplified gesture faker from polymer-gestures 0.5 --- fake.js | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 fake.js diff --git a/fake.js b/fake.js new file mode 100644 index 0000000..868e623 --- /dev/null +++ b/fake.js @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + +(function(scope) { + 'use strict'; + + var HAS_NEW_MOUSE = (function() { + var has = false; + try { + has = Boolean(new MouseEvent('x')); + } catch (_) {} + return has; + })(); + + function Fake() { + } + + Fake.prototype = { + middleOfNode: function(node) { + var bcr = node.getBoundingClientRect(); + return { + y: bcr.top + (bcr.height / 2), + x: bcr.left + (bcr.width / 2) + }; + }, + topLeftOfNode: function(node) { + var bcr = node.getBoundingClientRect(); + return { + y: bcr.top, + x: bcr.left + }; + }, + makeEvent: function(type, xy, node) { + var props = { + bubbles: true, + cancelable: true, + clientX: xy.x, + clientY: xy.y + }; + var e; + var mousetype = type === 'tap' ? 'click' : 'mouse' + type; + if (HAS_NEW_MOUSE) { + e = new MouseEvent(mousetype, props); + } else { + e = document.createEvent('MouseEvent'); + e.initMouseEvent(mousetype, props.bubbles, props.cancelable, null, null, 0, 0, + props.clientX, props.clientY, false, false, false, false, 0, null); + } + node.dispatchEvent(e); + }, + down: function(node, xy) { + xy = xy || this.middleOfNode(node); + this.makeEvent('down', xy, node); + }, + move: function(node, fromXY, toXY, steps) { + steps = steps || 5; + var dx = Math.round((fromXY.x - toXY.x) / steps); + var dy = Math.round((fromXY.y - toXY.y) / steps); + var xy = { + x: fromXY.x, + y: fromXY.y + }; + for (var i = steps; i > 0; i--) { + this.makeEvent('move', xy, node); + xy.x += dx; + xy.y += dy; + } + this.makeEvent('move', { + x: toXY.x, + y: toXY.y + }, node); + }, + up: function(node, xy) { + xy = xy || this.middleOfNode(node); + this.makeEvent('up', xy, node); + }, + tap: function(node) { + var xy = this.middleOfNode(node); + this.down(node, xy); + this.up(node, xy); + this.makeEvent('tap', xy, node); + } + }; + + scope.Fake = Fake; +})(window); From e9233825c4ea919f86afa6330ed623b6d3b4983f Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Wed, 13 May 2015 17:28:14 -0700 Subject: [PATCH 2/3] Integrate fake into mock interactions Replace MockInteractions down/up/tap with fake versions Add track --- fake.js | 92 ------------------------------ mock-interactions.js | 131 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 112 insertions(+), 111 deletions(-) delete mode 100644 fake.js diff --git a/fake.js b/fake.js deleted file mode 100644 index 868e623..0000000 --- a/fake.js +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. - * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt - * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt - * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt - * Code distributed by Google as part of the polymer project is also - * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt - */ - -(function(scope) { - 'use strict'; - - var HAS_NEW_MOUSE = (function() { - var has = false; - try { - has = Boolean(new MouseEvent('x')); - } catch (_) {} - return has; - })(); - - function Fake() { - } - - Fake.prototype = { - middleOfNode: function(node) { - var bcr = node.getBoundingClientRect(); - return { - y: bcr.top + (bcr.height / 2), - x: bcr.left + (bcr.width / 2) - }; - }, - topLeftOfNode: function(node) { - var bcr = node.getBoundingClientRect(); - return { - y: bcr.top, - x: bcr.left - }; - }, - makeEvent: function(type, xy, node) { - var props = { - bubbles: true, - cancelable: true, - clientX: xy.x, - clientY: xy.y - }; - var e; - var mousetype = type === 'tap' ? 'click' : 'mouse' + type; - if (HAS_NEW_MOUSE) { - e = new MouseEvent(mousetype, props); - } else { - e = document.createEvent('MouseEvent'); - e.initMouseEvent(mousetype, props.bubbles, props.cancelable, null, null, 0, 0, - props.clientX, props.clientY, false, false, false, false, 0, null); - } - node.dispatchEvent(e); - }, - down: function(node, xy) { - xy = xy || this.middleOfNode(node); - this.makeEvent('down', xy, node); - }, - move: function(node, fromXY, toXY, steps) { - steps = steps || 5; - var dx = Math.round((fromXY.x - toXY.x) / steps); - var dy = Math.round((fromXY.y - toXY.y) / steps); - var xy = { - x: fromXY.x, - y: fromXY.y - }; - for (var i = steps; i > 0; i--) { - this.makeEvent('move', xy, node); - xy.x += dx; - xy.y += dy; - } - this.makeEvent('move', { - x: toXY.x, - y: toXY.y - }, node); - }, - up: function(node, xy) { - xy = xy || this.middleOfNode(node); - this.makeEvent('up', xy, node); - }, - tap: function(node) { - var xy = this.middleOfNode(node); - this.down(node, xy); - this.up(node, xy); - this.makeEvent('tap', xy, node); - } - }; - - scope.Fake = Fake; -})(window); diff --git a/mock-interactions.js b/mock-interactions.js index 0c32c7b..3263e0f 100644 --- a/mock-interactions.js +++ b/mock-interactions.js @@ -1,27 +1,104 @@ +/** + * @license + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + (function(global) { 'use strict'; - function focus (target) { - Polymer.Base.fire.call(target, 'focus'); + var HAS_NEW_MOUSE = (function() { + var has = false; + try { + has = Boolean(new MouseEvent('x')); + } catch (_) {} + return has; + })(); + + function middleOfNode(node) { + var bcr = node.getBoundingClientRect(); + return { + y: bcr.top + (bcr.height / 2), + x: bcr.left + (bcr.width / 2) + }; } - function blur (target) { - Polymer.Base.fire.call(target, 'blur'); + function topLeftOfNode(node) { + var bcr = node.getBoundingClientRect(); + return { + y: bcr.top, + x: bcr.left + }; + } + + function makeEvent(type, xy, node) { + var props = { + bubbles: true, + cancelable: true, + clientX: xy.x, + clientY: xy.y + }; + var e; + var mousetype = type === 'tap' ? 'click' : 'mouse' + type; + if (HAS_NEW_MOUSE) { + e = new MouseEvent(mousetype, props); + } else { + e = document.createEvent('MouseEvent'); + e.initMouseEvent(mousetype, props.bubbles, props.cancelable, null, null, 0, 0, + props.clientX, props.clientY, false, false, false, false, 0, null); + } + node.dispatchEvent(e); } - function down (target) { - Polymer.Base.fire.call(target, 'mousedown'); + function down(node, xy) { + xy = xy || middleOfNode(node); + makeEvent('down', xy, node); } - function up (target) { - Polymer.Base.fire.call(target, 'mouseup'); + function move(node, fromXY, toXY, steps) { + steps = steps || 5; + var dx = Math.round((fromXY.x - toXY.x) / steps); + var dy = Math.round((fromXY.y - toXY.y) / steps); + var xy = { + x: fromXY.x, + y: fromXY.y + }; + for (var i = steps; i > 0; i--) { + makeEvent('move', xy, node); + xy.x += dx; + xy.y += dy; + } + makeEvent('move', { + x: toXY.x, + y: toXY.y + }, node); } - function tap(target) { - Polymer.Base.fire.call(target, 'tap'); + function up(node, xy) { + xy = xy || middleOfNode(node); + makeEvent('up', xy, node); } - function downAndUp (target, callback) { + function tap(node) { + var xy = middleOfNode(node); + down(node, xy); + up(node, xy); + makeEvent('tap', xy, node); + } + + function focus(target) { + Polymer.Base.fire.call(target, 'focus'); + } + + function blur(target) { + Polymer.Base.fire.call(target, 'blur'); + } + + function downAndUp(target, callback) { down(target); Polymer.Base.async(function() { up(target); @@ -31,7 +108,21 @@ }); } - function keyboardEventFor (type, keyCode) { + function track(target, dx, dy, steps) { + moveX = moveX | 0; + moveY = moveY | 0; + steps = steps || 5; + down(target); + var xy = middleOfNode(target); + var xy2 = { + x: xy.x + dx, + y: xy.y + dy + }; + move(target, xy, xy2); + up(target, xy2); + } + + function keyboardEventFor(type, keyCode) { var event = new CustomEvent(type); event.keyCode = keyCode; @@ -40,30 +131,30 @@ return event; } - function keyEventOn (target, type, keyCode) { + function keyEventOn(target, type, keyCode) { target.dispatchEvent(keyboardEventFor(type, keyCode)); } - function keyDownOn (target, keyCode) { + function keyDownOn(target, keyCode) { keyEventOn(target, 'keydown', keyCode); } - function keyUpOn (target, keyCode) { + function keyUpOn(target, keyCode) { keyEventOn(target, 'keyup', keyCode); } - function pressAndReleaseKeyOn (target, keyCode) { + function pressAndReleaseKeyOn(target, keyCode) { keyDownOn(target, keyCode); - Polymer.Base.async(function () { + Polymer.Base.async(function() { keyUpOn(target, keyCode); }, 1); } - function pressEnter (target) { + function pressEnter(target) { pressAndReleaseKeyOn(target, 13); } - function pressSpace (target) { + function pressSpace(target) { pressAndReleaseKeyOn(target, 32); } @@ -73,6 +164,8 @@ down: down, up: up, downAndUp: downAndUp, + tap: tap, + track: track, pressAndReleaseKeyOn: pressAndReleaseKeyOn, pressEnter: pressEnter, pressSpace: pressSpace From 8f3a5f7c24c0679fb58ab7c49b7b7e948171887d Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Wed, 13 May 2015 17:38:50 -0700 Subject: [PATCH 3/3] fix variables in move --- mock-interactions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mock-interactions.js b/mock-interactions.js index 3263e0f..c41f76f 100644 --- a/mock-interactions.js +++ b/mock-interactions.js @@ -109,8 +109,8 @@ } function track(target, dx, dy, steps) { - moveX = moveX | 0; - moveY = moveY | 0; + dx = dx | 0; + dy = dy | 0; steps = steps || 5; down(target); var xy = middleOfNode(target);