forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouchscreen.js
387 lines (332 loc) · 12.8 KB
/
touchscreen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright 2011 WebDriver committers
// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview The file contains an abstraction of a touch screen
* for simulating atomic touchscreen actions.
*/
goog.provide('bot.Touchscreen');
goog.require('bot');
goog.require('bot.Device');
goog.require('bot.Error');
goog.require('bot.ErrorCode');
goog.require('bot.dom');
goog.require('bot.events.EventType');
goog.require('goog.math.Coordinate');
/**
* A TouchScreen that provides atomic touch actions. The metaphor
* for this abstraction is a finger moving above the touchscreen that
* can press and then release the touchscreen when specified.
*
* The touchscreen supports three actions: press, release, and move.
*
* @constructor
* @extends {bot.Device}
*/
bot.Touchscreen = function() {
goog.base(this);
/** @private {!goog.math.Coordinate} */
this.clientXY_ = new goog.math.Coordinate(0, 0);
/** @private {!goog.math.Coordinate} */
this.clientXY2_ = new goog.math.Coordinate(0, 0);
};
goog.inherits(bot.Touchscreen, bot.Device);
/** @private {boolean} */
bot.Touchscreen.prototype.hasMovedAfterPress_ = false;
/** @private {boolean} */
bot.Touchscreen.prototype.cancelled_ = false;
/** @private {number} */
bot.Touchscreen.prototype.touchIdentifier_ = 0;
/** @private {number} */
bot.Touchscreen.prototype.touchIdentifier2_ = 0;
/** @private {number} */
bot.Touchscreen.prototype.touchCounter_ = 2;
/**
* Press the touch screen. Pressing before moving results in an exception.
* Pressing while already pressed also results in an exception.
*
* @param {boolean=} opt_press2 Whether or not press the second finger during
* the press. If not defined or false, only the primary finger will be
* pressed.
*/
bot.Touchscreen.prototype.press = function(opt_press2) {
if (this.isPressed()) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'Cannot press touchscreen when already pressed.');
}
this.hasMovedAfterPress_ = false;
this.touchIdentifier_ = this.touchCounter_++;
if (opt_press2) {
this.touchIdentifier2_ = this.touchCounter_++;
}
if (bot.userAgent.IE_DOC_10) {
this.firePointerEvents_(bot.Touchscreen.fireSinglePressPointer_);
} else {
this.fireTouchEvent_(bot.events.EventType.TOUCHSTART);
}
};
/**
* Releases an element on a touchscreen. Releasing an element that is not
* pressed results in an exception.
*/
bot.Touchscreen.prototype.release = function() {
if (!this.isPressed()) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'Cannot release touchscreen when not already pressed.');
}
if (!bot.userAgent.IE_DOC_10) {
this.fireTouchReleaseEvents_();
} else if (!this.cancelled_) {
this.firePointerEvents_(bot.Touchscreen.fireSingleReleasePointer_);
}
bot.Device.clearPointerMap();
this.touchIdentifier_ = 0;
this.touchIdentifier2_ = 0;
this.cancelled_ = false;
};
/**
* Moves finger along the touchscreen.
*
* @param {!Element} element Element that is being pressed.
* @param {!goog.math.Coordinate} coords Coordinates relative to
* currentElement.
* @param {goog.math.Coordinate=} opt_coords2 Coordinates relative to
* currentElement.
*/
bot.Touchscreen.prototype.move = function(element, coords, opt_coords2) {
// The target element for touch actions is the original element. Hence, the
// element is set only when the touchscreen is not currently being pressed.
// The exception is IE10 which fire events on the moved to element.
var originalElement = this.getElement();
if (!this.isPressed() || bot.userAgent.IE_DOC_10) {
this.setElement(element);
}
var rect = bot.dom.getClientRect(element);
this.clientXY_.x = coords.x + rect.left;
this.clientXY_.y = coords.y + rect.top;
if (goog.isDef(opt_coords2)) {
this.clientXY2_.x = opt_coords2.x + rect.left;
this.clientXY2_.y = opt_coords2.y + rect.top;
}
if (this.isPressed()) {
if (!bot.userAgent.IE_DOC_10) {
this.hasMovedAfterPress_ = true;
this.fireTouchEvent_(bot.events.EventType.TOUCHMOVE);
} else if (!this.cancelled_) {
if (element != originalElement) {
this.hasMovedAfterPress_ = true;
}
if (bot.Touchscreen.hasMsTouchActionsEnabled_(element)) {
this.firePointerEvents_(bot.Touchscreen.fireSingleMovePointer_);
} else {
this.fireMSPointerEvent(bot.events.EventType.MSPOINTEROUT, coords, -1,
this.touchIdentifier_, MSPointerEvent.MSPOINTER_TYPE_TOUCH, true);
this.fireMouseEvent(bot.events.EventType.MOUSEOUT, coords, 0);
this.fireMSPointerEvent(bot.events.EventType.MSPOINTERCANCEL, coords, 0,
this.touchIdentifier_, MSPointerEvent.MSPOINTER_TYPE_TOUCH, true);
this.cancelled_ = true;
bot.Device.clearPointerMap();
}
}
}
};
/**
* Returns whether the touchscreen is currently pressed.
*
* @return {boolean} Whether the touchscreen is pressed.
*/
bot.Touchscreen.prototype.isPressed = function() {
return !!this.touchIdentifier_;
};
/**
* A helper function to fire touch events.
*
* @param {bot.events.EventType} type Event type.
* @private
*/
bot.Touchscreen.prototype.fireTouchEvent_ = function(type) {
if (!this.isPressed()) {
throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR,
'Should never fire event when touchscreen is not pressed.');
}
var touchIdentifier2;
var coords2;
if (this.touchIdentifier2_) {
touchIdentifier2 = this.touchIdentifier2_;
coords2 = this.clientXY2_;
}
this.fireTouchEvent(type, this.touchIdentifier_, this.clientXY_,
touchIdentifier2, coords2);
};
/**
* A helper function to fire touch events that occur on a release.
*
* @private
*/
bot.Touchscreen.prototype.fireTouchReleaseEvents_ = function() {
this.fireTouchEvent_(bot.events.EventType.TOUCHEND);
// If no movement occurred since press, TouchScreen.Release will fire the
// legacy mouse events: mousemove, mousedown, mouseup, and click
// after the touch events have been fired. The click button should be zero
// and only one mousemove should fire.
if (!this.hasMovedAfterPress_) {
this.fireMouseEvent(bot.events.EventType.MOUSEMOVE, this.clientXY_, 0);
var performFocus = this.fireMouseEvent(bot.events.EventType.MOUSEDOWN,
this.clientXY_, 0);
// Element gets focus after the mousedown event only if the mousedown was
// not cancelled.
if (performFocus) {
this.focusOnElement();
}
this.maybeToggleOption();
this.fireMouseEvent(bot.events.EventType.MOUSEUP, this.clientXY_, 0);
// Special click logic to follow links and to perform form actions.
if (!(bot.userAgent.WINDOWS_PHONE &&
bot.dom.isElement(this.getElement(), goog.dom.TagName.OPTION))) {
this.clickElement(this.clientXY_, /* button value */ 0);
}
}
};
/**
* A helper function to fire a sequence of Pointer events.
* @param {function(!bot.Touchscreen, !Element, !goog.math.Coordinate, number,
* boolean)} fireSinglePointer A function that fires a set of events for one
* finger.
* @private
*/
bot.Touchscreen.prototype.firePointerEvents_ = function(fireSinglePointer) {
fireSinglePointer(this, this.getElement(), this.clientXY_,
this.touchIdentifier_, true);
if (this.touchIdentifier2_ &&
bot.Touchscreen.hasMsTouchActionsEnabled_(this.getElement())) {
fireSinglePointer(this, this.getElement(),
this.clientXY2_, this.touchIdentifier2_, false);
}
};
/**
* A helper function to fire Pointer events related to a press.
*
* @param {!bot.Touchscreen} ts A touchscreen object.
* @param {!Element} element Element that is being pressed.
* @param {!goog.math.Coordinate} coords Coordinates relative to
* currentElement.
* @param {number} id The touch identifier.
* @param {boolean} isPrimary Whether the pointer represents the primary point
* of contact.
* @private
*/
bot.Touchscreen.fireSinglePressPointer_ = function(ts, element, coords, id,
isPrimary) {
// Fire a mousemove event.
ts.fireMouseEvent(bot.events.EventType.MOUSEMOVE, coords, 0);
// Fire a MSPointerOver and mouseover events.
ts.fireMSPointerEvent(bot.events.EventType.MSPOINTEROVER, coords, 0, id,
MSPointerEvent.MSPOINTER_TYPE_TOUCH, isPrimary);
ts.fireMouseEvent(bot.events.EventType.MOUSEOVER, coords, 0);
// Fire a MSPointerDown and mousedown events.
ts.fireMSPointerEvent(bot.events.EventType.MSPOINTERDOWN, coords, 0, id,
MSPointerEvent.MSPOINTER_TYPE_TOUCH, isPrimary);
// Element gets focus after the mousedown event.
if (ts.fireMouseEvent(bot.events.EventType.MOUSEDOWN, coords, 0)) {
// For selectable elements, IE 10 fires a MSGotPointerCapture event.
if (bot.dom.isSelectable(element)) {
ts.fireMSPointerEvent(bot.events.EventType.MSGOTPOINTERCAPTURE, coords, 0,
id, MSPointerEvent.MSPOINTER_TYPE_TOUCH, isPrimary);
}
ts.focusOnElement();
}
};
/**
* A helper function to fire Pointer events related to a release.
*
* @param {!bot.Touchscreen} ts A touchscreen object.
* @param {!Element} element Element that is being released.
* @param {!goog.math.Coordinate} coords Coordinates relative to
* currentElement.
* @param {number} id The touch identifier.
* @param {boolean} isPrimary Whether the pointer represents the primary point
* of contact.
* @private
*/
bot.Touchscreen.fireSingleReleasePointer_ = function(ts, element, coords, id,
isPrimary) {
// Fire a MSPointerUp and mouseup events.
ts.fireMSPointerEvent(bot.events.EventType.MSPOINTERUP, coords, 0, id,
MSPointerEvent.MSPOINTER_TYPE_TOUCH, isPrimary);
ts.fireMouseEvent(bot.events.EventType.MOUSEUP, coords, 0, null, 0, false,
id);
// Fire a click.
if (!ts.hasMovedAfterPress_) {
ts.maybeToggleOption();
if (!(bot.userAgent.WINDOWS_PHONE &&
bot.dom.isElement(element, goog.dom.TagName.OPTION))) {
ts.clickElement(ts.clientXY_, 0, id);
}
}
if (bot.dom.isSelectable(element)) {
// For selectable elements, IE 10 fires a MSLostPointerCapture event.
ts.fireMSPointerEvent(bot.events.EventType.MSLOSTPOINTERCAPTURE,
new goog.math.Coordinate(0, 0), 0, id,
MSPointerEvent.MSPOINTER_TYPE_TOUCH, false);
}
// Fire a MSPointerOut and mouseout events.
ts.fireMSPointerEvent(bot.events.EventType.MSPOINTEROUT, coords, -1, id,
MSPointerEvent.MSPOINTER_TYPE_TOUCH, isPrimary);
ts.fireMouseEvent(bot.events.EventType.MOUSEOUT, coords, 0, null, 0, false,
id);
};
/**
* A helper function to fire Pointer events related to a move.
*
* @param {!bot.Touchscreen} ts A touchscreen object.
* @param {!Element} element Element that is being moved.
* @param {!goog.math.Coordinate} coords Coordinates relative to
* currentElement.
* @param {number} id The touch identifier.
* @param {boolean} isPrimary Whether the pointer represents the primary point
* of contact.
* @private
*/
bot.Touchscreen.fireSingleMovePointer_ = function(ts, element, coords, id,
isPrimary) {
// Fire a MSPointerMove and mousemove events.
ts.fireMSPointerEvent(bot.events.EventType.MSPOINTERMOVE, coords, -1, id,
MSPointerEvent.MSPOINTER_TYPE_TOUCH, isPrimary);
ts.fireMouseEvent(bot.events.EventType.MOUSEMOVE, coords, 0, null, 0, false,
id);
};
/**
* A function that determines whether an element can be manipulated by the user.
* The msTouchAction style is queried and an element can be manipulated if the
* style value is none. If an element cannot be manipulated, then move gestures
* will result in a cancellation and multi-touch events will be prevented. Tap
* gestures will still be allowed. If not on IE 10, the function returns true.
*
* @param {!Element} element The element being manipulated.
* @return {boolean} Whether the element can be manipulated.
* @private
*/
bot.Touchscreen.hasMsTouchActionsEnabled_ = function(element) {
if (!bot.userAgent.IE_DOC_10) {
throw new Error('hasMsTouchActionsEnable should only be called from IE 10');
}
// Although this particular element may have a style indicating that it cannot
// receive javascript events, its parent may indicate otherwise.
if (bot.dom.getEffectiveStyle(element, 'ms-touch-action') == 'none') {
return true;
} else {
var parent = bot.dom.getParentElement(element);
return !!parent && bot.Touchscreen.hasMsTouchActionsEnabled_(parent);
}
};