<script src="../../../resources/js-test-pre.js"></script>
<div id='touchtarget' style='width: 50; height: 50'></div>
<script>
description("This test checks that we correctly update the touch event handler count as event handlers are added and removed");

debug("Test addEventListener/removeEventListener on the document.");
(function() {
    var listener = function() { }

    shouldBe('window.internals.touchEventHandlerCount()', '0');
    document.addEventListener('touchstart', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');
    document.addEventListener('touchmove', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '2');
    document.addEventListener('touchstart', listener, false);
    shouldBe('window.internals.touchEventHandlerCount()', '3');
    document.removeEventListener('touchmove', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '2');
    document.removeEventListener('touchstart', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');

    // Try removing the capturing listener again.
    document.removeEventListener('touchstart', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');
    document.removeEventListener('touchmove', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');

    document.removeEventListener('touchstart', listener, false);
    shouldBe('window.internals.touchEventHandlerCount()', '0');
})();

debug("Test setting touch handlers on the document.");
(function() {
    shouldBe('window.internals.touchEventHandlerCount()', '0');
    document.ontouchstart = function() { }
    document.ontouchmove = function() {}
    document.ontouchend = function() {}
    document.ontouchcancel = function() {}
    shouldBe('window.internals.touchEventHandlerCount()', '4');
    document.ontouchstart = function() { }
    shouldBe('window.internals.touchEventHandlerCount()', '4');
    document.ontouchstart = null;
    shouldBe('window.internals.touchEventHandlerCount()', '3');
    document.ontouchstart = null;
    shouldBe('window.internals.touchEventHandlerCount()', '3');
    document.ontouchmove = null;
    document.ontouchend = null;
    document.ontouchcancel = null;
    shouldBe('window.internals.touchEventHandlerCount()', '0');
})();

debug("Test addEventListener/removeEventListener on the window.");
(function() {
    var listener = function() { }

    shouldBe('window.internals.touchEventHandlerCount()', '0');
    window.addEventListener('touchstart', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');
    window.addEventListener('touchmove', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '2');
    window.addEventListener('touchstart', listener, false);
    shouldBe('window.internals.touchEventHandlerCount()', '3');
    window.removeEventListener('touchmove', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '2');
    window.removeEventListener('touchstart', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');

    // Try removing the capturing listener again.
    window.removeEventListener('touchstart', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');
    window.removeEventListener('touchmove', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');

    window.removeEventListener('touchstart', listener, false);
    shouldBe('window.internals.touchEventHandlerCount()', '0');
})();

debug("Test setting touch handlers on the window.");
(function() {
    shouldBe('window.internals.touchEventHandlerCount()', '0');
    window.ontouchstart = function() { }
    window.ontouchmove = function() {}
    window.ontouchend = function() {}
    window.ontouchcancel = function() {}
    shouldBe('window.internals.touchEventHandlerCount()', '4');
    window.ontouchstart = function() { }
    shouldBe('window.internals.touchEventHandlerCount()', '4');
    window.ontouchstart = null;
    shouldBe('window.internals.touchEventHandlerCount()', '3');
    window.ontouchstart = null;
    shouldBe('window.internals.touchEventHandlerCount()', '3');
    window.ontouchmove = null;
    window.ontouchend = null;
    window.ontouchcancel = null;
    shouldBe('window.internals.touchEventHandlerCount()', '0');
})();

debug("Test addEventListener/removeEventListener on a div.");
(function() {
    var listener = function() { }
    var div = document.getElementById('touchtarget');

    shouldBe('window.internals.touchEventHandlerCount()', '0');
    div.addEventListener('touchstart', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');
    div.addEventListener('touchmove', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '2');
    div.addEventListener('touchstart', listener, false);
    shouldBe('window.internals.touchEventHandlerCount()', '3');
    div.removeEventListener('touchmove', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '2');
    div.removeEventListener('touchstart', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');

    // Try removing the capturing listener again.
    div.removeEventListener('touchstart', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');
    div.removeEventListener('touchmove', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '1');

    div.removeEventListener('touchstart', listener, false);
    shouldBe('window.internals.touchEventHandlerCount()', '0');
})();

debug("Test setting touch handlers on a div.");
(function() {
    var div = document.getElementById('touchtarget');

    shouldBe('window.internals.touchEventHandlerCount()', '0');
    div.ontouchstart = function() { }
    div.ontouchmove = function() { }
    div.ontouchend = function() { }
    div.ontouchcancel = function() { }
    shouldBe('window.internals.touchEventHandlerCount()', '4');
    div.ontouchstart = function() { }
    shouldBe('window.internals.touchEventHandlerCount()', '4');
    div.ontouchstart = null;
    shouldBe('window.internals.touchEventHandlerCount()', '3');
    div.ontouchstart = null;
    shouldBe('window.internals.touchEventHandlerCount()', '3');
    div.ontouchmove = null;
    div.ontouchend = null;
    div.ontouchcancel = null;
    shouldBe('window.internals.touchEventHandlerCount()', '0');
})();

debug("Test addEventListener/removeEventListener on a new div.");
(function() {
    var div = document.createElement('div');
    var touchtarget = document.getElementById('touchtarget');
    var listener = function() { }

    shouldBe('window.internals.touchEventHandlerCount()', '0');

    div.addEventListener('touchstart', listener, true);
    div.addEventListener('touchmove', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '2');

    touchtarget.appendChild(div);
    shouldBe('window.internals.touchEventHandlerCount()', '2');

    div.addEventListener('touchend', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '3');

    div.removeEventListener('touchstart', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '2');

    touchtarget.removeChild(div);
    shouldBe('window.internals.touchEventHandlerCount()', '2');

    div.removeEventListener('touchmove', listener, false);
    shouldBe('window.internals.touchEventHandlerCount()', '2');

    div.removeEventListener('touchmove', listener, true);
    div.removeEventListener('touchend', listener, true);
    shouldBe('window.internals.touchEventHandlerCount()', '0');
})();

debug("Test setting touch handlers on a new div.");
(function() {
    var div = document.createElement('div');
    var touchtarget = document.getElementById('touchtarget');

    shouldBe('window.internals.touchEventHandlerCount()', '0');

    div.ontouchstart = function() { }
    div.ontouchmove = function() { }
    shouldBe('window.internals.touchEventHandlerCount()', '2');

    touchtarget.appendChild(div);
    shouldBe('window.internals.touchEventHandlerCount()', '2');

    div.ontouchend = function() { }
    shouldBe('window.internals.touchEventHandlerCount()', '3');

    div.ontouchstart = null;
    shouldBe('window.internals.touchEventHandlerCount()', '2');

    touchtarget.removeChild(div);
    shouldBe('window.internals.touchEventHandlerCount()', '2');

    div.ontouchend = null;
    div.ontouchmove = null;
    shouldBe('window.internals.touchEventHandlerCount()', '0');
})();

debug("Test that nested Documents' touch handlers are properly removed from their parent Document.");
(function() {
    var iframe = document.createElement('iframe');
    var touchtarget = document.getElementById('touchtarget');

    shouldBe('window.internals.touchEventHandlerCount()', '0');

    touchtarget.appendChild(iframe);

    var nestedDocument = iframe.contentWindow.document;
    nestedDocument.open('text/html', 'replace');
    nestedDocument.write("<!DOCTYPE html>\n<html><body onload=\"window.ontouchstart = function() { };\"></body>");
    nestedDocument.close();

    shouldBe('window.internals.touchEventHandlerCount()', '1');

    touchtarget.removeChild(iframe);
    shouldBe('window.internals.touchEventHandlerCount()', '0');
})();
</script>
</body>
