<html>
<script src="../../../resources/js-test-pre.js"></script>
<style>
iframe {
  width: 100px;
  height: 100px;
  margin: 0;
  padding: 0;
  border: 1px solid black;
}

#iframe1
{
position:absolute;
left:50px;
top:50px;
}

#iframe2
{
position:absolute;
left:200px;
top:50px;
}

#output
{
position:absolute;
top:150px;
}

</style>
<body onload="runTest()">
<iframe id="iframe1" src="./resources/multi-touch-inside-iframes2.html"></iframe>
<iframe id="iframe2" src="./resources/multi-touch-inside-iframes2.html"></iframe>
<div id="output">
<p id="description"></p>
<div id="console"></div>
</div>
<script type="text/javascript">
description('Test multi-touch is sent to the same iframe.');

var touchEventCount = 0;
var title = 0;

function onTouch(event, receiver)
{
    ev = event;
    touchreceiver = receiver;
    switch (touchEventCount) {
        case 0:
            shouldBeEqualToString('ev.type', 'touchstart');
            shouldBeEqualToString('touchreceiver', 'iframe1');
            shouldBe("ev.changedTouches.length", "1");
            shouldBe("ev.touches.length", "1");
            shouldBe("ev.targetTouches.length", "1");
            shouldBe("ev.touches[0].pageX", "50");
            shouldBe("ev.touches[0].pageY", "50");
            shouldBeEqualToString('ev.touches[0].target.ownerDocument.title', 'iframe1');
            shouldBeEqualToString('ev.changedTouches[0].target.ownerDocument.title', 'iframe1');
            shouldBeEqualToString('ev.targetTouches[0].target.ownerDocument.title', 'iframe1');
            break;
        case 1:
            shouldBeEqualToString('ev.type', 'touchmove');
            shouldBeEqualToString('touchreceiver', 'iframe1');
            shouldBe("ev.changedTouches.length", "1");
            shouldBe("ev.touches.length", "1");
            shouldBe("ev.targetTouches.length", "1");
            shouldBe("ev.touches[0].pageX", "150");
            shouldBe("ev.touches[0].pageY", "150");
            shouldBeEqualToString('ev.touches[0].target.ownerDocument.title', 'iframe1');
            shouldBeEqualToString('ev.changedTouches[0].target.ownerDocument.title', 'iframe1');
            shouldBeEqualToString('ev.targetTouches[0].target.ownerDocument.title', 'iframe1');
            break;
        case 2:
            shouldBeEqualToString('ev.type', 'touchend');
            shouldBeEqualToString('touchreceiver', 'iframe1');
            shouldBe("ev.changedTouches.length", "1");
            shouldBe("ev.touches.length", "0");
            shouldBe("ev.targetTouches.length", "0");
            shouldBeEqualToString('ev.changedTouches[0].target.ownerDocument.title', 'iframe1');
            break;
        case 3:
            shouldBeEqualToString('ev.type', 'touchstart');
            shouldBeEqualToString('touchreceiver', 'iframe2');
            shouldBe("ev.changedTouches.length", "1");
            shouldBe("ev.touches.length", "1");
            shouldBe("ev.targetTouches.length", "1");
            shouldBe("ev.touches[0].pageX", "50");
            shouldBe("ev.touches[0].pageY", "50");
            shouldBeEqualToString('ev.touches[0].target.ownerDocument.title', 'iframe2');
            shouldBeEqualToString('ev.changedTouches[0].target.ownerDocument.title', 'iframe2');
            shouldBeEqualToString('ev.targetTouches[0].target.ownerDocument.title', 'iframe2');
            break;
        case 4:
            shouldBeEqualToString('ev.type', 'touchend');
            shouldBeEqualToString('touchreceiver', 'iframe2');
            shouldBe("ev.changedTouches.length", "1");
            shouldBe("ev.touches.length", "0");
            shouldBe("ev.targetTouches.length", "0");
            shouldBeEqualToString('ev.changedTouches[0].target.ownerDocument.title', 'iframe2');
            isSuccessfullyParsed();
            testRunner.notifyDone();
    }
    touchEventCount++;
}

function runTest() {
    document.getElementById("iframe1").contentWindow.document.title = "iframe1";
    document.getElementById("iframe2").contentWindow.document.title = "iframe2";
    if (window.eventSender) {
        eventSender.clearTouchPoints();
        // Touch the center of iframe1. 50px is offset to iframe1 in main frame,
        // 1px for the iframe border, 50px to get to centre.
        debug('First touch is on iframe1.');
        shouldBeEqualToString('document.elementFromPoint(101, 101).id', 'iframe1');
        eventSender.addTouchPoint(101, 101);
        eventSender.touchStart();
        debug('');

        debug('Second touch is on iframe2, nothing should happen.');
        shouldBeEqualToString('document.elementFromPoint(251, 101).id', 'iframe2');
        eventSender.addTouchPoint(251, 101);
        eventSender.touchStart();
        debug('');

        debug('Moves the first touch outside iframe1.');
        eventSender.updateTouchPoint(0, 201, 201);
        eventSender.touchMove();
        debug('');

        debug('Release the first touch.');
        eventSender.releaseTouchPoint(0);
        eventSender.touchEnd();
        debug('');

        debug('Third touch is on iframe2, nothing should happen.');
        shouldBeEqualToString('document.elementFromPoint(261, 101).id', 'iframe2');
        eventSender.addTouchPoint(261, 101);
        eventSender.touchStart();
        debug('');

        debug('Release all touches on iframe2, and touch iframe2 again.');
        eventSender.releaseTouchPoint(0);
        eventSender.releaseTouchPoint(1);
        eventSender.touchEnd();
        eventSender.addTouchPoint(251, 101);
        eventSender.touchStart();
        debug('');

        debug('Touch iframe1, nothing shoud happen.');
        eventSender.addTouchPoint(101, 101);
        eventSender.touchStart();
        debug('');

        debug('Release all touches.');
        eventSender.releaseTouchPoint(0);
        eventSender.releaseTouchPoint(1);
        eventSender.touchEnd();
    } else {
       debug('This test requires DRT.');
    }
}

if (window.testRunner) {
    testRunner.waitUntilDone();
}
</script>
</body>
</html>
