9 background-color: rgb(
255,
0,
0);
12 <input type=text id=target
>
13 <script src=
"../../../../resources/js-test.js"></script>
15 description("Verifies that a GestureTap triggers focus and selectionchange if and only if mousedown isn't prevented.");
17 function logEvent(e
, opt_extra
) {
18 var msg
= 'Received ' + e
.type
+ ' on ' + (e
.target
.id
|| e
.target
.nodeName
);
20 msg
+= ' ' + opt_extra
;
24 var target
= document
.getElementById('target');
25 var eventsToLog
= ['mousemove', 'mousedown', 'mouseover', 'mouseup', 'mouseout', 'click'];
26 for (var i
= 0; i
< eventsToLog
.length
; i
++) {
27 target
.addEventListener(eventsToLog
[i
], logEvent
);
29 document
.addEventListener('selectionchange', function(e
) {
30 var selection
= getSelection();
31 var selectionNode
= selection
.anchorNode
;
34 anchor
= (selectionNode
.id
|| selectionNode
.nodeName
) + '[' + selection
.anchorOffset
+ ']';
35 logEvent(e
, 'anchor=' + anchor
);
39 function doTap(targetNode
, expectHandled
) {
40 var rect
= targetNode
.getBoundingClientRect();
41 var targetX
= rect
.left
+ 2;
42 var targetY
= rect
.top
+ 2;
44 return new Promise(function(resolve
, reject
) {
45 if (!('eventSender' in window
)) {
46 reject(Error('test requires eventSender'));
50 debug('Sending GestureTapDown');
51 eventSender
.gestureTapDown(targetX
, targetY
, 30, 30);
52 debug('Sending GestureShowPress');
53 eventSender
.gestureShowPress(targetX
, targetY
, 30, 30);
54 debug('Sending GestureTap');
55 tapHandled
= eventSender
.gestureTap(targetX
, targetY
, 1, 30, 30);
56 shouldBe('tapHandled', expectHandled
? 'true' : 'false');
58 // Run any pending tasks before resolving the promise.
59 setTimeout(resolve
, 0);
63 function consumeEvent(e
) {
67 function isFocused(element
) {
68 var style
= getComputedStyle(element
);
69 return style
.backgroundColor
== 'rgb(255, 0, 0)';
73 return new Promise(function(resolve
, reject
) {
74 debug('Tap on input field but consume mousedown');
75 target
.addEventListener('mousedown', consumeEvent
);
76 doTap(target
, true).then(function() {
77 shouldBeFalse('isFocused(target)');
79 }).then(resolve
, reject
);
84 return new Promise(function(resolve
, reject
) {
85 debug('Tap on input field without consuming mousedown');
86 target
.removeEventListener('mousedown', consumeEvent
);
87 doTap(target
, false).then(function() {
88 shouldBeTrue('isFocused(target)');
90 }).then(resolve
, reject
);
96 return new Promise(function(resolve
, reject
) {
97 debug ('Tap elsewhere to clear focus');
98 doTap(document
.getElementById('description'), false).then(function() {
99 shouldBeFalse('isFocused(target)');
101 }).then(resolve
, reject
);
105 jsTestIsAsync
= true;
106 onload = function() {
110 .catch(function(err
) {
111 testFailed("Promise rejected: " + err
.message
);
112 }).then(finishJSTest
);