Home | History | Annotate | Download | only in script-tests
      1 var div = document.createElement("div");
      2 div.id = "touchtarget";
      3 div.style.width = "100px";
      4 div.style.height = "100px";
      5 div.style.backgroundColor = "blue";
      6 
      7 var lastEvent = null;
      8 var touchEventsReceived = 0;
      9 var EXPECTED_TOUCH_EVENTS_TOTAL = 4;
     10 
     11 function 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 
     29 div.addEventListener("touchstart", touchEventCallback, false);
     30 div.addEventListener("touchmove", touchEventCallback, false);
     31 div.addEventListener("touchend", touchEventCallback, false);
     32 document.body.insertBefore(div, document.body.firstChild);
     33 
     34 function 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     shouldBe("lastEvent.pageX", "0");
     41     shouldBe("lastEvent.pageY", "0");
     42 }
     43 
     44 function verifyTouchPoint(list, point, x, y, id)
     45 {
     46     shouldBe("lastEvent." + list + "[" + point + "].pageX", x.toString());
     47     shouldBe("lastEvent." + list + "[" + point + "].pageY", y.toString());
     48     shouldBe("lastEvent." + list + "[" + point + "].clientX", x.toString());
     49     shouldBe("lastEvent." + list + "[" + point + "].clientY", y.toString());
     50     shouldBe("lastEvent." + list + "[" + point + "].identifier", id.toString());
     51 }
     52 
     53 function verifyTouch(which) {
     54     switch (which) {
     55         case 0:
     56             verifyTouchEvent("touchstart", 2, 2, 2);
     57             verifyTouchPoint("touches", 0, 10, 10, 0);
     58             verifyTouchPoint("touches", 1, 20, 30, 1);
     59             verifyTouchPoint("changedTouches", 0, 10, 10, 0);
     60             verifyTouchPoint("changedTouches", 1, 20, 30, 1);
     61             verifyTouchPoint("targetTouches", 0, 10, 10, 0);
     62             verifyTouchPoint("targetTouches", 1, 20, 30, 1);
     63         break;
     64         case 1:
     65             verifyTouchEvent("touchmove", 2, 1, 2);
     66             verifyTouchPoint("touches", 0, 15, 15, 0);
     67             verifyTouchPoint("changedTouches", 0, 15, 15, 0);
     68             verifyTouchPoint("touches", 1, 20, 30, 1);
     69         break;
     70         case 2:
     71             verifyTouchEvent("touchend", 1, 1, 1);
     72             verifyTouchPoint("touches", 0, 20, 30, 1);
     73             verifyTouchPoint("changedTouches", 0, 15, 15, 0);
     74             verifyTouchPoint("targetTouches", 0, 20, 30, 1);
     75         break;
     76         case 3:
     77             verifyTouchEvent("touchend", 0, 1, 0);
     78             verifyTouchPoint("changedTouches", 0, 20, 30, 1);
     79         break;
     80 
     81         default: testFailed("Wrong number of touch events! (" + which + ")");
     82     }
     83 }
     84 
     85 function multiTouchSequence()
     86 {
     87     debug("multi touch sequence");
     88 
     89     debug("Two touchpoints pressed");
     90     eventSender.addTouchPoint(10, 10);
     91     eventSender.addTouchPoint(20, 30);
     92     eventSender.touchStart();
     93 
     94     debug("First touchpoint moved");
     95     eventSender.updateTouchPoint(0, 15, 15);
     96     eventSender.touchMove();
     97 
     98     debug("First touchpoint is released");
     99     eventSender.releaseTouchPoint(0);
    100     eventSender.touchEnd();
    101 
    102     debug("Last remaining touchpoint is released");
    103     eventSender.releaseTouchPoint(0);
    104     eventSender.touchEnd();
    105 }
    106 
    107 if (window.eventSender) {
    108     description("This tests basic multi touch event support.");
    109 
    110     lastEvent = null;
    111     eventSender.clearTouchPoints();
    112     multiTouchSequence();
    113 } else {
    114     debug("This test requires DumpRenderTree.  Tap on the blue rect to log.")
    115 }
    116 
    117 var successfullyParsed = true;
    118