<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<style>
iframe {
  width: 300px;
  height: 300px;
  top: 100px;
  left: 100px;
  border: 0;
  position: absolute;
  background: green;
}
#outerFrame {
  width: 500px;
  height: 500px;
  background: blue;
}
</style>
<body id="outerFrame body" onload="run()">
<div id='outerFrame'>
<iframe id='innerFrameElement' src="resources/pointerevent_mouse_pointercapture-iframe.html"></iframe>
</div>
</body>
<script>
var receivedEventList = [];
function handleEvent(event) {
  receivedEventList.push(event.target.id + ' received ' + event.type);

  if (event.type == 'pointerdown') {
    if (document.setPointerCaptureOnPointerDown) {
      event.target.setPointerCapture(event.pointerId);
    }
  }

  if (event.type == "pointermove") {
    if (document.releasePointerCaptureOnFirstMove && event.target.hasPointerCapture(event.pointerId))
        event.target.releasePointerCapture(event.pointerId);
  }
};

document.testEventList = ['pointerup', 'pointerdown', 'pointermove', 'gotpointercapture', 'lostpointercapture'];
document.testEventList.forEach(function(eventName) {
  document.getElementById('outerFrame').addEventListener(eventName, handleEvent);
});

document.setPointerCaptureOnPointerDown = false;
document.releasePointerCaptureOnFirstMove = false;

function run() {
    promise_test (async() => {
        document.setPointerCaptureOnPointerDown = true;
        receivedEventList = [];
        expectedEventList = ["innerFrame received pointermove",
                             "innerFrame received pointerdown",
                             "innerFrame received gotpointercapture",
                             "innerFrame received pointermove",
                             "innerFrame received pointermove",
                             "innerFrame received pointerup",
                             "innerFrame received lostpointercapture"];
        await new test_driver.Actions()
                             .pointerMove(200, 200)
                             .pointerDown()
                             .pointerMove(150, 150)
                             .pointerMove(50, 50)
                             .pointerUp()
                             .send();
        assert_array_equals(receivedEventList, expectedEventList, "Received events: " + receivedEventList);
        document.setPointerCaptureOnPointerDown = false;
    }, "Test pointer capture event route across the same-origin frame: Mouse down at inner frame and set pointer capture.");


    promise_test (async() => {
        document.setPointerCaptureOnPointerDown = true;
        receivedEventList = [];
        expectedEventList = ["outerFrame received pointermove",
                             "outerFrame received pointerdown",
                             "outerFrame received gotpointercapture",
                             "outerFrame received pointermove",
                             "outerFrame received pointerup",
                             "outerFrame received lostpointercapture"];
        await new test_driver.Actions()
                             .pointerMove(25, 25)
                             .pointerDown()
                             .pointerMove(200, 200)
                             .pointerUp()
                             .send();
        assert_array_equals(receivedEventList, expectedEventList, "Received events: " + receivedEventList);
        document.setPointerCaptureOnPointerDown = false;
    }, "Test pointer capture event route across the same-origin frame: Mouse down at outer frame body and set pointer capture.");


    promise_test (async() => {
        document.setPointerCaptureOnPointerDown = true;
        document.releasePointerCaptureOnFirstMove = true;
        receivedEventList = [];
        expectedEventList = ["innerFrame received pointermove",
                             "innerFrame received pointerdown",
                             "innerFrame received gotpointercapture",
                             "innerFrame received pointermove",
                             "innerFrame received lostpointercapture",
                             "innerFrameDocument received pointermove",
                             "innerFrameDocument received pointerup",];
        await new test_driver.Actions()
                             .pointerMove(200, 200)
                             .pointerDown()
                             .pointerMove(150, 150)
                             .pointerMove(50, 50)
                             .pointerUp()
                             .send();
        assert_array_equals(receivedEventList, expectedEventList, "Received events: " + receivedEventList);
        document.releasePointerCaptureOnFirstMove = false;
        document.setPointerCaptureOnPointerDown = false;
    }, "Test pointer capture event route across the same-origin frame: Mouse down with set capture at inner frame, then release on next mouse move.");


    promise_test (async() => {
        document.setPointerCaptureOnPointerDown = true;
        document.releasePointerCaptureOnFirstMove = true;
        receivedEventList = [];
        expectedEventList = ["outerFrame received pointermove",
                             "outerFrame received pointerdown",
                             "outerFrame received gotpointercapture",
                             "outerFrame received pointermove",
                             "outerFrame received lostpointercapture",
                             "innerFrame received pointermove",
                             "innerFrame received pointerup"];
        await new test_driver.Actions()
                             .pointerMove(50, 50)
                             .pointerDown()
                             .pointerMove(200, 200)
                             .pointerMove(250, 250)
                             .pointerUp()
                             .send();
        assert_array_equals(receivedEventList, expectedEventList, "Received events: " + receivedEventList);
        document.releasePointerCaptureOnFirstMove = false;
        document.setPointerCaptureOnPointerDown = false;
    }, "Test pointercapture event route across the same-origin frame: Mouse down with set capture at outer frame, then release on next mouse move.");
}
</script>

