90255f2b471e8c119e74969a56703de70b04be3a
[WebKit-https.git] / LayoutTests / fast / events / touch / emulate-touch-events.html
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script src="../../js/resources/js-test-pre.js"></script>
5 <div id="touchtarget" style="width: 100px; height: 100px; background-color: blue"></div>
6 <p id="description"></p>
7 <div id="console"></div>
8 <script>
9 var div = document.getElementById("touchtarget");
10 var lastEvent = null;
11 var touchEventsReceived = 0;
12 var EXPECTED_TOUCH_EVENTS_TOTAL = 3;
13
14 function touchEventCallback() {
15     if (window.eventSender) {
16         lastEvent = event;
17         verifyTouch(touchEventsReceived++);
18     } else
19         debug(event.type);
20
21     if (window.testRunner && touchEventsReceived == EXPECTED_TOUCH_EVENTS_TOTAL) {
22         window.internals.settings.setTouchEventEmulationEnabled(false);
23         finishJSTest();
24     }
25 }
26
27 function mouseMoveCallback(e) {
28     if (!lastEvent)
29         debug("Unexpected mousemove event received before touchstart");
30 }
31
32 div.addEventListener("touchstart", touchEventCallback, false);
33 div.addEventListener("touchmove", touchEventCallback, false);
34 div.addEventListener("touchend", touchEventCallback, false);
35 div.addEventListener("mousemove", mouseMoveCallback, false);
36
37 function verifyTouchEvent(type, totalTouchCount, changedTouchCount, targetTouchCount)
38 {
39     shouldBeEqualToString("lastEvent.type", type);
40     shouldBe("lastEvent.touches.length", totalTouchCount.toString());
41     shouldBe("lastEvent.changedTouches.length", changedTouchCount.toString());
42     shouldBe("lastEvent.targetTouches.length", targetTouchCount.toString());
43     shouldBe("lastEvent.pageX", "0");
44     shouldBe("lastEvent.pageY", "0");
45 }
46
47 function verifyTouchPoint(list, point, x, y, id)
48 {
49     shouldBe("lastEvent." + list + "[" + point + "].pageX", x.toString());
50     shouldBe("lastEvent." + list + "[" + point + "].pageY", y.toString());
51     shouldBe("lastEvent." + list + "[" + point + "].clientX", x.toString());
52     shouldBe("lastEvent." + list + "[" + point + "].clientY", y.toString());
53     shouldBe("lastEvent." + list + "[" + point + "].identifier", id.toString());
54 }
55
56 function verifyTouch(which) {
57     switch (which) {
58     case 0:
59         verifyTouchEvent("touchstart", 1, 1, 1);
60         shouldBe("lastEvent.shiftKey", "true");
61         shouldBe("lastEvent.altKey", "true");
62         shouldBe("lastEvent.ctrlKey", "false");
63         shouldBe("lastEvent.metaKey", "false");
64         shouldBeEqualToString("lastEvent.touches[0].target.id", "touchtarget");
65         verifyTouchPoint("touches", 0, 10, 10, 0);
66         verifyTouchPoint("changedTouches", 0, 10, 10, 0);
67         verifyTouchPoint("targetTouches", 0, 10, 10, 0);
68         break;
69     case 1:
70         verifyTouchEvent("touchmove", 1, 1, 1);
71         verifyTouchPoint("touches", 0, 20, 30, 0);
72         break;
73     case 2:
74         verifyTouchEvent("touchend", 0, 1, 0);
75         verifyTouchPoint("changedTouches", 0, 20, 30, 0);
76         shouldBe("lastEvent.shiftKey", "false");
77         shouldBe("lastEvent.altKey", "true");
78         shouldBe("lastEvent.ctrlKey", "true");
79         shouldBe("lastEvent.metaKey", "false");
80         break;
81     default:
82         testFailed("Wrong number of touch events! (" + which + ")");
83     }
84 }
85
86 function mouseEventSequence()
87 {
88     eventSender.mouseMoveTo(10, 10);
89     eventSender.mouseDown(0, ["shiftKey", "altKey"]);
90     eventSender.mouseMoveTo(20, 30);
91     eventSender.mouseUp(0, ["altKey", "ctrlKey"]);
92 }
93
94 if (window.eventSender && window.internals && window.internals.settings) {
95     description("This tests single touch event emulation using mouse events.");
96
97     window.eventSender.dragMode = false;
98     window.jsTestIsAsync = true;
99     window.internals.settings.setTouchEventEmulationEnabled(true);
100
101     shouldBe("'ontouchstart' in window", "true");
102     shouldBe("'ontouchend' in document", "true");
103
104     mouseEventSequence();
105 } else
106     debug("This test requires DumpRenderTree. Tap on the blue rect to log.");
107 </script>
108 <script src="../../js/resources/js-test-post.js"></script>
109 </body>
110 </html>