add-listener-from-callback.html 1.53 KB
Newer Older
1 2
<html>
<head>
3
<script src="../resources/js-test-pre.js"></script>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
</head>
<body>
<script>
description('Tests that adding a new event listener from a callback works as expected.');

var firedEvent = {value: 2.2, min: 1.1, max: 3.3};

var deviceProximityEvent;
function checkProximity(event) {
    deviceProximityEvent = event;
    shouldBe('deviceProximityEvent.value', 'firedEvent.value');
    shouldBe('deviceProximityEvent.min', 'firedEvent.min');
    shouldBe('deviceProximityEvent.max', 'firedEvent.max');
}

var firstListenerEvents = 0;
function firstListener(event) {
    checkProximity(event);
    if (++firstListenerEvents == 1) {
        window.addEventListener('webkitdeviceproximity', secondListener);
24
        internals.setDeviceProximity('webkitdeviceproximity', firedEvent.value, firedEvent.min, firedEvent.max);
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    }
    else if (firstListenerEvents > 2)
        testFailed('Too many events for first listener.');
    maybeFinishTest();
}

var secondListenerEvents = 0;
function secondListener(event) {
    checkProximity(event);
    if (++secondListenerEvents > 1)
        testFailed('Too many events for second listener.');
    maybeFinishTest();
}

function maybeFinishTest() {
    if (firstListenerEvents == 2 && secondListenerEvents == 1)
        finishJSTest();
}

window.addEventListener('webkitdeviceproximity', firstListener);
45
internals.setDeviceProximity('webkitdeviceproximity', firedEvent.value, firedEvent.min, firedEvent.max);
46 47
window.jsTestIsAsync = true;
</script>
48
<script src="../resources/js-test-post.js"></script>
49 50
</body>
</html>