basic-multi-touch-events.js revision 692e5dbf12901edacf14812a6fae25462920af42
1var div = document.createElement("div"); 2div.id = "touchtarget"; 3div.style.width = "100px"; 4div.style.height = "100px"; 5div.style.backgroundColor = "blue"; 6 7var lastEvent = null; 8var touchEventsReceived = 0; 9var EXPECTED_TOUCH_EVENTS_TOTAL = 4; 10 11function touchEventCallback() { 12 if (window.eventSender) { 13 lastEvent = event; 14 verifyTouch(touchEventsReceived++); 15 } else { 16 debug(event.type); 17 } 18 19 if (window.layoutTestController && touchEventsReceived == EXPECTED_TOUCH_EVENTS_TOTAL) { 20 // If we've got here, we can safely say we were successfully parsed :) We need to 21 // call the isSucccessfullyParsed function to output the correct TEST COMPLETE 22 // footer message. 23 successfullyParsed = true; 24 isSuccessfullyParsed(); 25 layoutTestController.notifyDone(); 26 } 27} 28 29div.addEventListener("touchstart", touchEventCallback, false); 30div.addEventListener("touchmove", touchEventCallback, false); 31div.addEventListener("touchend", touchEventCallback, false); 32document.body.insertBefore(div, document.body.firstChild); 33 34function verifyTouchEvent(type, totalTouchCount, changedTouchCount, targetTouchCount) 35{ 36 shouldBeEqualToString("lastEvent.type", type); 37 shouldBe("lastEvent.touches.length", totalTouchCount.toString()); 38 shouldBe("lastEvent.changedTouches.length", changedTouchCount.toString()); 39 shouldBe("lastEvent.targetTouches.length", targetTouchCount.toString()); 40} 41 42function verifyTouchPoint(list, point, x, y, id) 43{ 44 shouldBe("lastEvent." + list + "[" + point + "].pageX", x.toString()); 45 shouldBe("lastEvent." + list + "[" + point + "].pageY", y.toString()); 46 shouldBe("lastEvent." + list + "[" + point + "].clientX", x.toString()); 47 shouldBe("lastEvent." + list + "[" + point + "].clientY", y.toString()); 48 shouldBe("lastEvent." + list + "[" + point + "].identifier", id.toString()); 49} 50 51function verifyTouch(which) { 52 switch (which) { 53 case 0: 54 verifyTouchEvent("touchstart", 2, 2, 2); 55 verifyTouchPoint("touches", 0, 10, 10, 0); 56 verifyTouchPoint("touches", 1, 20, 30, 1); 57 verifyTouchPoint("changedTouches", 0, 10, 10, 0); 58 verifyTouchPoint("changedTouches", 1, 20, 30, 1); 59 verifyTouchPoint("targetTouches", 0, 10, 10, 0); 60 verifyTouchPoint("targetTouches", 1, 20, 30, 1); 61 break; 62 case 1: 63 verifyTouchEvent("touchmove", 2, 1, 2); 64 verifyTouchPoint("touches", 0, 15, 15, 0); 65 verifyTouchPoint("changedTouches", 0, 15, 15, 0); 66 verifyTouchPoint("touches", 1, 20, 30, 1); 67 break; 68 case 2: 69 verifyTouchEvent("touchend", 1, 1, 1); 70 verifyTouchPoint("touches", 0, 20, 30, 1); 71 verifyTouchPoint("changedTouches", 0, 15, 15, 0); 72 verifyTouchPoint("targetTouches", 0, 20, 30, 1); 73 break; 74 case 3: 75 verifyTouchEvent("touchend", 0, 1, 0); 76 verifyTouchPoint("changedTouches", 0, 20, 30, 1); 77 break; 78 79 default: testFailed("Wrong number of touch events! (" + which + ")"); 80 } 81} 82 83function multiTouchSequence() 84{ 85 debug("multi touch sequence"); 86 87 debug("Two touchpoints pressed"); 88 eventSender.addTouchPoint(10, 10); 89 eventSender.addTouchPoint(20, 30); 90 eventSender.touchStart(); 91 92 debug("First touchpoint moved"); 93 eventSender.updateTouchPoint(0, 15, 15); 94 eventSender.touchMove(); 95 96 debug("First touchpoint is released"); 97 eventSender.releaseTouchPoint(0); 98 eventSender.touchEnd(); 99 100 debug("Last remaining touchpoint is released"); 101 eventSender.releaseTouchPoint(0); 102 eventSender.touchEnd(); 103} 104 105if (window.eventSender) { 106 description("This tests basic multi touch event support."); 107 108 lastEvent = null; 109 eventSender.clearTouchPoints(); 110 multiTouchSequence(); 111} else { 112 debug("This test requires DumpRenderTree. Tap on the blue rect to log.") 113} 114 115var successfullyParsed = true; 116