OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE HTML> |
| 2 <meta charset="utf-8"> |
| 3 <title>EventListenerOptions.passive</title> |
| 4 <link rel="author" title="Rick Byers" href="mailto:rbyers@chromium.org"> |
| 5 <link rel="help" href="https://dom.spec.whatwg.org/#dom-addeventlisteneroptions-
passive"> |
| 6 <script src="/resources/testharness.js"></script> |
| 7 <script src="/resources/testharnessreport.js"></script> |
| 8 <div id="log"></div> |
| 9 |
| 10 <script> |
| 11 |
| 12 test(function() { |
| 13 var supportsPassive = false; |
| 14 var query_options = { |
| 15 get passive() { |
| 16 supportsPassive = true; |
| 17 return false; |
| 18 }, |
| 19 get dummy() { |
| 20 assert_unreached("dummy value getter invoked"); |
| 21 return false; |
| 22 } |
| 23 }; |
| 24 |
| 25 document.addEventListener('test_event', null, query_options); |
| 26 assert_true(supportsPassive, "addEventListener doesn't support the passive opt
ion"); |
| 27 |
| 28 supportsPassive = false; |
| 29 document.removeEventListener('test_event', null, query_options); |
| 30 assert_false(supportsPassive, "removeEventListener supports the passive option
when it should not"); |
| 31 }, "Supports passive option on addEventListener only"); |
| 32 |
| 33 function testPassiveValue(optionsValue, expectedDefaultPrevented) { |
| 34 var defaultPrevented = undefined; |
| 35 var handler = function handler(e) { |
| 36 assert_false(e.defaultPrevented, "Event prematurely marked defaultPrevented"
); |
| 37 e.preventDefault(); |
| 38 defaultPrevented = e.defaultPrevented; |
| 39 } |
| 40 document.addEventListener('test', handler, optionsValue); |
| 41 var uncanceled = document.body.dispatchEvent(new Event('test', {bubbles: true,
cancelable: true})); |
| 42 |
| 43 assert_equals(defaultPrevented, expectedDefaultPrevented, "Incorrect defaultPr
evented for options: " + JSON.stringify(optionsValue)); |
| 44 assert_equals(uncanceled, !expectedDefaultPrevented, "Incorrect return value f
rom dispatchEvent"); |
| 45 |
| 46 document.removeEventListener('test', handler, optionsValue); |
| 47 } |
| 48 |
| 49 test(function() { |
| 50 testPassiveValue(undefined, true); |
| 51 testPassiveValue({}, true); |
| 52 testPassiveValue({passive: false}, true); |
| 53 testPassiveValue({passive: true}, false); |
| 54 testPassiveValue({passive: 0}, true); |
| 55 testPassiveValue({passive: 1}, false); |
| 56 }, "preventDefault should be ignored if-and-only-if the passive option is true")
; |
| 57 |
| 58 function testPassiveWithOtherHandlers(optionsValue, expectedDefaultPrevented) { |
| 59 var handlerInvoked1 = false; |
| 60 var dummyHandler1 = function() { |
| 61 handlerInvoked1 = true; |
| 62 }; |
| 63 var handlerInvoked2 = false; |
| 64 var dummyHandler2 = function() { |
| 65 handlerInvoked2 = true; |
| 66 }; |
| 67 |
| 68 document.addEventListener('test', dummyHandler1, {passive:true}); |
| 69 document.addEventListener('test', dummyHandler2); |
| 70 |
| 71 testPassiveValue(optionsValue, expectedDefaultPrevented); |
| 72 |
| 73 assert_true(handlerInvoked1, "Extra passive handler not invoked"); |
| 74 assert_true(handlerInvoked2, "Extra non-passive handler not invoked"); |
| 75 |
| 76 document.removeEventListener('test', dummyHandler1); |
| 77 document.removeEventListener('test', dummyHandler2); |
| 78 } |
| 79 |
| 80 test(function() { |
| 81 testPassiveWithOtherHandlers({}, true); |
| 82 testPassiveWithOtherHandlers({passive: false}, true); |
| 83 testPassiveWithOtherHandlers({passive: true}, false); |
| 84 }, "passive behavior of one listener should be unaffeted by the presence of othe
r listeners"); |
| 85 |
| 86 function testOptionEquivalence(optionValue1, optionValue2, expectedEquality) { |
| 87 var invocationCount = 0; |
| 88 var handler = function handler(e) { |
| 89 invocationCount++; |
| 90 } |
| 91 document.addEventListener('test', handler, optionValue1); |
| 92 document.addEventListener('test', handler, optionValue2); |
| 93 document.body.dispatchEvent(new Event('test', {bubbles: true})); |
| 94 assert_equals(invocationCount, expectedEquality ? 1 : 2, "equivalence of optio
ns " + |
| 95 JSON.stringify(optionValue1) + " and " + JSON.stringify(optionValue2)); |
| 96 document.removeEventListener('test', handler, optionValue1); |
| 97 document.removeEventListener('test', handler, optionValue2); |
| 98 } |
| 99 |
| 100 test(function() { |
| 101 // Sanity check options that should be treated as distinct handlers |
| 102 testOptionEquivalence({capture:true}, {capture:false, passive:false}, false); |
| 103 testOptionEquivalence({capture:true}, {passive:true}, false); |
| 104 |
| 105 // Option values that should be treated as equivalent |
| 106 testOptionEquivalence({}, {passive:false}, true); |
| 107 testOptionEquivalence({passive:true}, {passive:false}, true); |
| 108 testOptionEquivalence(undefined, {passive:true}, true); |
| 109 testOptionEquivalence({capture: true, passive: false}, {capture: true, passive
: true}, true); |
| 110 |
| 111 }, "Equivalence of option values"); |
| 112 |
| 113 </script> |
OLD | NEW |