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 = 5;
     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", 1, 1, 1);
     57            shouldBe("lastEvent.shiftKey", "false");
     58            shouldBeEqualToString("lastEvent.touches[0].target.id", "touchtarget");
     59            verifyTouchPoint("touches", 0, 10, 10, 0);
     60            verifyTouchPoint("changedTouches", 0, 10, 10, 0);
     61            verifyTouchPoint("targetTouches", 0, 10, 10, 0);
     62         break;
     63         case 1:
     64            verifyTouchEvent("touchmove", 1, 1, 1);
     65            verifyTouchPoint("touches", 0, 50, 50, 0);
     66            shouldBe("lastEvent.shiftKey", "true");
     67            shouldBe("lastEvent.altKey", "true");
     68            shouldBe("lastEvent.ctrlKey", "false");
     69            shouldBe("lastEvent.metaKey", "false");
     70         break;
     71         case 2:
     72             verifyTouchEvent("touchend", 0, 1, 0);
     73             verifyTouchPoint("changedTouches", 0, 50, 50, 0);
     74             shouldBe("lastEvent.shiftKey", "false");
     75             shouldBe("lastEvent.altKey", "false");
     76         break;
     77         case 3:
     78             verifyTouchEvent("touchstart", 1, 1, 1);
     79             shouldBeEqualToString("lastEvent.targetTouches[0].target.tagName", "DIV");
     80         break;
     81         case 4:
     82             verifyTouchEvent("touchmove", 1, 1, 1);
     83             shouldBeEqualToString("lastEvent.touches[0].target.tagName", "DIV");
     84         break;
     85 
     86         default: testFailed("Wrong number of touch events! (" + which + ")");
     87     }
     88 }
     89 
     90 function singleTouchSequence()
     91 {
     92     eventSender.addTouchPoint(10, 10);
     93     eventSender.touchStart();
     94 
     95     eventSender.updateTouchPoint(0, 50, 50);
     96     eventSender.setTouchModifier("shift", true);
     97     eventSender.setTouchModifier("alt", true);
     98     eventSender.touchMove();
     99 
    100     eventSender.setTouchModifier("shift", false);
    101     eventSender.setTouchModifier("alt", false);
    102 
    103     eventSender.releaseTouchPoint(0);
    104     eventSender.touchEnd();
    105 }
    106 
    107 function touchTargets()
    108 {
    109     eventSender.addTouchPoint(20, 20);
    110     eventSender.touchStart();
    111 
    112     eventSender.updateTouchPoint(0, 1000, 1000);
    113     eventSender.touchMove();
    114 }
    115 
    116 if (window.layoutTestController)
    117     layoutTestController.waitUntilDone();
    118 
    119 if (window.eventSender) {
    120     description("This tests basic single touch event support.");
    121 
    122     lastEvent = null;
    123     eventSender.clearTouchPoints();
    124     singleTouchSequence();
    125 
    126     lastEvent = null;
    127     eventSender.clearTouchPoints();
    128     touchTargets();
    129 
    130 } else {
    131     debug("This test requires DumpRenderTree.  Tap on the blue rect to log.")
    132 }
    133 
    134 
    135