Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Unified Diff: third_party/WebKit/LayoutTests/imported/wpt/dom/events/AddEventListenerOptions-once2.html

Issue 2245723002: Support "once" event listener option (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/imported/wpt/dom/events/AddEventListenerOptions-once2.html
diff --git a/third_party/WebKit/LayoutTests/imported/wpt/dom/events/AddEventListenerOptions-once2.html b/third_party/WebKit/LayoutTests/imported/wpt/dom/events/AddEventListenerOptions-once2.html
new file mode 100644
index 0000000000000000000000000000000000000000..b1bf3baa538ce25d6f951a8785279f13bd518234
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/imported/wpt/dom/events/AddEventListenerOptions-once2.html
@@ -0,0 +1,130 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<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
+<link rel="author" title="Anton Obzhirov" href="mailto:a.obzhirov@samsung.com">
+<!-- Rick Byers's AddEventListenerOptions-passive.html test was used as a template. -->
+<link rel="help" href="https://dom.spec.whatwg.org/#dom-addeventlisteneroptions-once">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<div id="log"></div>
+
+<script>
+
+test(function() {
+ var supportsOnce = false;
+ var query_options = {
+ get once() {
+ supportsOnce = true;
+ return false;
+ },
+ get dummy() {
+ assert_unreached("dummy value getter invoked");
+ return false;
+ }
+ };
+
+ document.addEventListener('test_event', null, query_options);
+ assert_true(supportsOnce, "addEventListener doesn't support the once option");
+
+ supportsOnce = false;
+ document.removeEventListener('test_event', null, query_options);
+ assert_false(supportsOnce, "removeEventListener supports the once option when it should not");
+}, "Supports once option on addEventListener only");
+
+function testOnceValue(optionsValue, expectedHandleCallCount) {
+ var handleCallCount = 0;
+ var handler = function() {
+ ++handleCallCount;
+ }
+
+ document.body.addEventListener('test', handler, optionsValue);
+ assert_equals(handleCallCount, 0, "Incorrect handleCallCount for the addEventListener step.");
+ document.body.dispatchEvent(new Event('test'));
+ assert_equals(handleCallCount, 1, "Incorrect handleCallCount for the first dispatchEvent step.");
+ document.body.dispatchEvent(new Event('test'));
+ assert_equals(handleCallCount, expectedHandleCallCount, "Incorrect handleCallCount for the second dispatchEvent step.");
+ document.removeEventListener('test', handler, optionsValue);
+}
+
+test(function() {
+ testOnceValue(undefined, 2);
+ testOnceValue({}, 2);
+ testOnceValue({once: false}, 2);
+ testOnceValue({once: true}, 1);
+ testOnceValue({once: 0}, 2);
+ testOnceValue({once: 1}, 1);
+}, "Event handler should be called only once if the 'once' option is true");
+
+test(function() {
+ var handleCallCount = 0;
+ var handler = function() {
+ ++handleCallCount;
+ document.body.dispatchEvent(new Event('test'));
+ assert_equals(handleCallCount, 1, "Incorrect handleCallCount for the second dispatchEvent step.");
+ }
+
+ document.body.addEventListener('test', handler, { 'once': true });
+ assert_equals(handleCallCount, 0, "Incorrect handleCallCount for the addEventListener step.");
+ document.body.dispatchEvent(new Event('test'));
+ assert_equals(handleCallCount, 1, "Incorrect handleCallCount for the first dispatchEvent step.");
+}, "Event handler shouldn't be called second time if the event is dispatched from 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
+
+function testOnceWithOtherHandlers(optionsValue, expectedHandleCallCount) {
+ var handleCallCount1 = 0;
+ var handler1 = function() {
+ ++handleCallCount1;
+ }
+ var handleCallCount2 = 0;
+ var handler2 = function() {
+ ++handleCallCount2;
+ }
+
+ document.body.addEventListener('test', handler1, optionsValue);
+ document.body.addEventListener('test', handler2);
+
+ document.body.dispatchEvent(new Event('test'));
+ assert_equals(handleCallCount1, 1, "Incorrect handleCallCount1 for the first dispatchEvent step.");
+ assert_equals(handleCallCount2, 1, "Incorrect handleCallCount2 for the first dispatchEvent step.");
+
+ document.body.dispatchEvent(new Event('test'));
+ assert_equals(handleCallCount1, expectedHandleCallCount, "Incorrect handleCallCount1 for the second dispatchEvent step.");
+ assert_equals(handleCallCount2, 2, "Incorrect handleCallCount2 for the second dispatchEvent step.");
+
+ document.removeEventListener('test', handler1);
+ document.removeEventListener('test', handler2);
+}
+
+test(function() {
+ testOnceWithOtherHandlers({}, 2);
+ testOnceWithOtherHandlers({once: false}, 2);
+ testOnceWithOtherHandlers({once: true}, 1);
+}, "'once' behavior of one listener should be unaffected by the presence of other 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
+
+function testOptionEquivalence(optionValue1, optionValue2, expectedEquality) {
+ var invocationCount = 0;
+ var handler = function handler(e) {
+ invocationCount++;
+ }
+ document.addEventListener('test', handler, optionValue1);
+ document.addEventListener('test', handler, optionValue2);
+ document.body.dispatchEvent(new Event('test', {bubbles: true}));
+ assert_equals(invocationCount, expectedEquality ? 1 : 2, "equivalence of options " +
+ JSON.stringify(optionValue1) + " and " + JSON.stringify(optionValue2));
+ document.removeEventListener('test', handler, optionValue1);
+ document.removeEventListener('test', handler, optionValue2);
+}
+
+test(function() {
+ // Sanity check options that should be treated as distinct handlers
+ testOptionEquivalence({capture:true}, {capture:false, once:false}, false);
+ testOptionEquivalence({capture:true}, {once:true}, false);
+
+ // Option values that should be treated as equivalent
+ testOptionEquivalence({}, {once:false}, true);
+ testOptionEquivalence({once:true}, {once:false}, true);
+ testOptionEquivalence(undefined, {once:false}, true);
+ testOptionEquivalence({capture: true, once: false}, {capture: true, once: true}, true);
+}, "Equivalence of option values");
+
+</script>
+

Powered by Google App Engine
This is Rietveld 408576698