Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!DOCTYPE html> | |
| 2 <meta charset="utf-8"> | |
| 3 <title>EventListenerOptions.once</title> | |
|
dtapuska
2016/08/12 18:44:02
You shouldn't add a test to the imported directory
Anton Obzhirov
2016/08/15 22:18:00
OK, didn't know, thanks for pointing out. I guess
| |
| 4 <link rel="author" title="Anton Obzhirov" href="mailto:a.obzhirov@samsung.com"> | |
| 5 <!-- Rick Byers's AddEventListenerOptions-passive.html test was used as a templa te. --> | |
| 6 <link rel="help" href="https://dom.spec.whatwg.org/#dom-addeventlisteneroptions- once"> | |
| 7 <script src="/resources/testharness.js"></script> | |
| 8 <script src="/resources/testharnessreport.js"></script> | |
| 9 <div id="log"></div> | |
| 10 | |
| 11 <script> | |
| 12 | |
| 13 test(function() { | |
| 14 var supportsOnce = false; | |
| 15 var query_options = { | |
| 16 get once() { | |
| 17 supportsOnce = true; | |
| 18 return false; | |
| 19 }, | |
| 20 get dummy() { | |
| 21 assert_unreached("dummy value getter invoked"); | |
| 22 return false; | |
| 23 } | |
| 24 }; | |
| 25 | |
| 26 document.addEventListener('test_event', null, query_options); | |
| 27 assert_true(supportsOnce, "addEventListener doesn't support the once option"); | |
| 28 | |
| 29 supportsOnce = false; | |
| 30 document.removeEventListener('test_event', null, query_options); | |
| 31 assert_false(supportsOnce, "removeEventListener supports the once option when it should not"); | |
| 32 }, "Supports once option on addEventListener only"); | |
| 33 | |
| 34 function testOnceValue(optionsValue, expectedHandleCallCount) { | |
| 35 var handleCallCount = 0; | |
| 36 var handler = function() { | |
| 37 ++handleCallCount; | |
| 38 } | |
| 39 | |
| 40 document.body.addEventListener('test', handler, optionsValue); | |
| 41 assert_equals(handleCallCount, 0, "Incorrect handleCallCount for the addEventL istener step."); | |
| 42 document.body.dispatchEvent(new Event('test')); | |
| 43 assert_equals(handleCallCount, 1, "Incorrect handleCallCount for the first dis patchEvent step."); | |
| 44 document.body.dispatchEvent(new Event('test')); | |
| 45 assert_equals(handleCallCount, expectedHandleCallCount, "Incorrect handleCallC ount for the second dispatchEvent step."); | |
| 46 document.removeEventListener('test', handler, optionsValue); | |
| 47 } | |
| 48 | |
| 49 test(function() { | |
| 50 testOnceValue(undefined, 2); | |
| 51 testOnceValue({}, 2); | |
| 52 testOnceValue({once: false}, 2); | |
| 53 testOnceValue({once: true}, 1); | |
| 54 testOnceValue({once: 0}, 2); | |
| 55 testOnceValue({once: 1}, 1); | |
| 56 }, "Event handler should be called only once if the 'once' option is true"); | |
| 57 | |
| 58 test(function() { | |
| 59 var handleCallCount = 0; | |
| 60 var handler = function() { | |
| 61 ++handleCallCount; | |
| 62 document.body.dispatchEvent(new Event('test')); | |
| 63 assert_equals(handleCallCount, 1, "Incorrect handleCallCount for the secon d dispatchEvent step."); | |
| 64 } | |
| 65 | |
| 66 document.body.addEventListener('test', handler, { 'once': true }); | |
| 67 assert_equals(handleCallCount, 0, "Incorrect handleCallCount for the addEventL istener step."); | |
| 68 document.body.dispatchEvent(new Event('test')); | |
| 69 assert_equals(handleCallCount, 1, "Incorrect handleCallCount for the first dis patchEvent step."); | |
| 70 }, "Event handler shouldn't be called second time if the event is dispatched fro m the handler if the 'once' option is true"); | |
|
Xidorn Quan
2016/08/13 03:20:41
It seems the existing test for once already tests
Anton Obzhirov
2016/08/15 22:18:00
Yes, this one checks the same logic, I guess the w
| |
| 71 | |
| 72 function testOnceWithOtherHandlers(optionsValue, expectedHandleCallCount) { | |
| 73 var handleCallCount1 = 0; | |
| 74 var handler1 = function() { | |
| 75 ++handleCallCount1; | |
| 76 } | |
| 77 var handleCallCount2 = 0; | |
| 78 var handler2 = function() { | |
| 79 ++handleCallCount2; | |
| 80 } | |
| 81 | |
| 82 document.body.addEventListener('test', handler1, optionsValue); | |
| 83 document.body.addEventListener('test', handler2); | |
| 84 | |
| 85 document.body.dispatchEvent(new Event('test')); | |
| 86 assert_equals(handleCallCount1, 1, "Incorrect handleCallCount1 for the first d ispatchEvent step."); | |
| 87 assert_equals(handleCallCount2, 1, "Incorrect handleCallCount2 for the first d ispatchEvent step."); | |
| 88 | |
| 89 document.body.dispatchEvent(new Event('test')); | |
| 90 assert_equals(handleCallCount1, expectedHandleCallCount, "Incorrect handleCall Count1 for the second dispatchEvent step."); | |
| 91 assert_equals(handleCallCount2, 2, "Incorrect handleCallCount2 for the second dispatchEvent step."); | |
| 92 | |
| 93 document.removeEventListener('test', handler1); | |
| 94 document.removeEventListener('test', handler2); | |
| 95 } | |
| 96 | |
| 97 test(function() { | |
| 98 testOnceWithOtherHandlers({}, 2); | |
| 99 testOnceWithOtherHandlers({once: false}, 2); | |
| 100 testOnceWithOtherHandlers({once: true}, 1); | |
| 101 }, "'once' behavior of one listener should be unaffected by the presence of othe r listeners"); | |
|
Xidorn Quan
2016/08/13 03:20:41
This test looks like something the existing test a
Anton Obzhirov
2016/08/15 22:18:00
On 2016/08/13 03:20:41, Xidorn Quan wrote:
> This
| |
| 102 | |
| 103 function testOptionEquivalence(optionValue1, optionValue2, expectedEquality) { | |
| 104 var invocationCount = 0; | |
| 105 var handler = function handler(e) { | |
| 106 invocationCount++; | |
| 107 } | |
| 108 document.addEventListener('test', handler, optionValue1); | |
| 109 document.addEventListener('test', handler, optionValue2); | |
| 110 document.body.dispatchEvent(new Event('test', {bubbles: true})); | |
| 111 assert_equals(invocationCount, expectedEquality ? 1 : 2, "equivalence of optio ns " + | |
| 112 JSON.stringify(optionValue1) + " and " + JSON.stringify(optionValue2)); | |
| 113 document.removeEventListener('test', handler, optionValue1); | |
| 114 document.removeEventListener('test', handler, optionValue2); | |
| 115 } | |
| 116 | |
| 117 test(function() { | |
| 118 // Sanity check options that should be treated as distinct handlers | |
| 119 testOptionEquivalence({capture:true}, {capture:false, once:false}, false); | |
| 120 testOptionEquivalence({capture:true}, {once:true}, false); | |
| 121 | |
| 122 // Option values that should be treated as equivalent | |
| 123 testOptionEquivalence({}, {once:false}, true); | |
| 124 testOptionEquivalence({once:true}, {once:false}, true); | |
| 125 testOptionEquivalence(undefined, {once:false}, true); | |
| 126 testOptionEquivalence({capture: true, once: false}, {capture: true, once: true }, true); | |
| 127 }, "Equivalence of option values"); | |
| 128 | |
| 129 </script> | |
| 130 | |
| OLD | NEW |