|
9 | 9 | function Actions(defaultTickDuration=16) {
|
10 | 10 | this.sourceTypes = new Map([["key", KeySource],
|
11 | 11 | ["pointer", PointerSource],
|
| 12 | + ["wheel", WheelSource], |
12 | 13 | ["none", GeneralSource]]);
|
13 | 14 | this.sources = new Map();
|
14 | 15 | this.sourceOrder = [];
|
|
22 | 23 | this.createSource("none");
|
23 | 24 | this.tickIdx = 0;
|
24 | 25 | this.defaultTickDuration = defaultTickDuration;
|
| 26 | + this.context = null; |
25 | 27 | }
|
26 | 28 |
|
27 | 29 | Actions.prototype = {
|
|
65 | 67 | } catch(e) {
|
66 | 68 | return Promise.reject(e);
|
67 | 69 | }
|
68 |
| - return test_driver.action_sequence(actions); |
| 70 | + return test_driver.action_sequence(actions, this.context); |
| 71 | + }, |
| 72 | + |
| 73 | + /** |
| 74 | + * Set the context for the actions |
| 75 | + * |
| 76 | + * @param {WindowProxy} context - Context in which to run the action sequence |
| 77 | + */ |
| 78 | + setContext: function(context) { |
| 79 | + this.context = context; |
| 80 | + return this; |
69 | 81 | },
|
70 | 82 |
|
71 | 83 | /**
|
72 | 84 | * Get the action source with a particular source type and name.
|
73 | 85 | * If no name is passed, a new source with the given type is
|
74 | 86 | * created.
|
75 | 87 | *
|
76 |
| - * @param {String} type - Source type ('none', 'key', or 'pointer') |
| 88 | + * @param {String} type - Source type ('none', 'key', 'pointer', or 'wheel') |
77 | 89 | * @param {String?} name - Name of the source
|
78 | 90 | * @returns {Source} Source object for that source.
|
79 | 91 | */
|
|
154 | 166 | return this;
|
155 | 167 | },
|
156 | 168 |
|
| 169 | + /** |
| 170 | + * Add a new wheel input source with the given name |
| 171 | + * |
| 172 | + * @param {String} type - Name of the wheel source |
| 173 | + * @param {Bool} set - Set source as the default wheel source |
| 174 | + * @returns {Actions} |
| 175 | + */ |
| 176 | + addWheel: function(name, set=true) { |
| 177 | + this.createSource("wheel", name); |
| 178 | + if (set) { |
| 179 | + this.setWheel(name); |
| 180 | + } |
| 181 | + return this; |
| 182 | + }, |
| 183 | + |
| 184 | + /** |
| 185 | + * Set the current default wheel source |
| 186 | + * |
| 187 | + * @param {String} name - Name of the wheel source |
| 188 | + * @returns {Actions} |
| 189 | + */ |
| 190 | + setWheel: function(name) { |
| 191 | + this.setSource("wheel", name); |
| 192 | + return this; |
| 193 | + }, |
| 194 | + |
157 | 195 | createSource: function(type, name, parameters={}) {
|
158 | 196 | if (!this.sources.has(type)) {
|
159 | 197 | throw new Error(`${type} is not a valid action type`);
|
|
196 | 234 | *
|
197 | 235 | * @param {Number?} duration - Minimum length of the tick in ms.
|
198 | 236 | * @param {String} sourceType - source type
|
199 |
| - * @param {String?} sourceName - Named key or pointer source to use or null for the default |
200 |
| - * key or pointer source |
| 237 | + * @param {String?} sourceName - Named key, pointer or wheel source to use |
| 238 | + * or null for the default key, pointer or |
| 239 | + * wheel source |
201 | 240 | * @returns {Actions}
|
202 | 241 | */
|
203 | 242 | pause: function(duration=0, sourceType="none", {sourceName=null}={}) {
|
|
280 | 319 | source.pointerMove(this, x, y, duration, origin);
|
281 | 320 | return this;
|
282 | 321 | },
|
| 322 | + |
| 323 | + /** |
| 324 | + * Create a scroll event for the current default wheel source |
| 325 | + * |
| 326 | + * @param {Number} x - mouse cursor x coordinate |
| 327 | + * @param {Number} y - mouse cursor y coordinate |
| 328 | + * @param {Number} deltaX - scroll delta value along the x-axis in pixels |
| 329 | + * @param {Number} deltaY - scroll delta value along the y-axis in pixels |
| 330 | + * @param {String|Element} origin - Origin of the coordinate system. |
| 331 | + * Either "viewport" or an Element |
| 332 | + * @param {Number?} duration - Time in ms for the scroll |
| 333 | + * @param {String?} sourceName - Named wheel source to use or null for the |
| 334 | + * default wheel source |
| 335 | + * @returns {Actions} |
| 336 | + */ |
| 337 | + scroll: function(x, y, deltaX, deltaY, |
| 338 | + {origin="viewport", duration, sourceName=null}={}) { |
| 339 | + let source = this.getSource("wheel", sourceName); |
| 340 | + source.scroll(this, x, y, deltaX, deltaY, duration, origin); |
| 341 | + return this; |
| 342 | + }, |
283 | 343 | };
|
284 | 344 |
|
285 | 345 | function GeneralSource() {
|
|
417 | 477 | },
|
418 | 478 | };
|
419 | 479 |
|
| 480 | + function WheelSource() { |
| 481 | + this.actions = new Map(); |
| 482 | + } |
| 483 | + |
| 484 | + WheelSource.prototype = { |
| 485 | + serialize: function(tickCount) { |
| 486 | + if (!this.actions.size) { |
| 487 | + return undefined; |
| 488 | + } |
| 489 | + let actions = []; |
| 490 | + let data = {"type": "wheel", "actions": actions}; |
| 491 | + for (let i=0; i<tickCount; i++) { |
| 492 | + if (this.actions.has(i)) { |
| 493 | + actions.push(this.actions.get(i)); |
| 494 | + } else { |
| 495 | + actions.push({"type": "pause"}); |
| 496 | + } |
| 497 | + } |
| 498 | + return data; |
| 499 | + }, |
| 500 | + |
| 501 | + scroll: function(actions, x, y, deltaX, deltaY, duration, origin) { |
| 502 | + let tick = actions.tickIdx; |
| 503 | + if (this.actions.has(tick)) { |
| 504 | + tick = actions.addTick().tickIdx; |
| 505 | + } |
| 506 | + this.actions.set(tick, {type: "scroll", x, y, deltaX, deltaY, origin}); |
| 507 | + if (duration) { |
| 508 | + this.actions.get(tick).duration = duration; |
| 509 | + } |
| 510 | + }, |
| 511 | + |
| 512 | + addPause: function(actions, duration) { |
| 513 | + let tick = actions.tickIdx; |
| 514 | + if (this.actions.has(tick)) { |
| 515 | + tick = actions.addTick().tickIdx; |
| 516 | + } |
| 517 | + this.actions.set(tick, {type: "pause", duration: duration}); |
| 518 | + }, |
| 519 | + }; |
| 520 | + |
420 | 521 | test_driver.Actions = Actions;
|
421 | 522 | })();
|
0 commit comments