Bug 1287706 part 8 - Add web-platform-test for once option. r=smaug
authorXidorn Quan <me@upsuper.org>
Thu, 28 Jul 2016 12:00:06 +1000
changeset 332047 63b876fe012a5311643eb6d0b568e0c250ab18cc
parent 332046 dc5c362afc7cd6d580eda2f937e50e2257c6b7de
child 332048 343a4eced34bdb2c610da8d439da348f1e159eaa
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1287706
milestone50.0a1
Bug 1287706 part 8 - Add web-platform-test for once option. r=smaug MozReview-Commit-ID: t5f34n2taN
testing/web-platform/meta/MANIFEST.json
testing/web-platform/tests/dom/events/AddEventListenerOptions-once.html
--- a/testing/web-platform/meta/MANIFEST.json
+++ b/testing/web-platform/meta/MANIFEST.json
@@ -36378,16 +36378,22 @@
           }
         ],
         "XMLHttpRequest/xmlhttprequest-sync-not-hang-scriptloader.html": [
           {
             "path": "XMLHttpRequest/xmlhttprequest-sync-not-hang-scriptloader.html",
             "url": "/XMLHttpRequest/xmlhttprequest-sync-not-hang-scriptloader.html"
           }
         ],
+        "dom/events/AddEventListenerOptions-once.html": [
+          {
+            "path": "dom/events/AddEventListenerOptions-once.html",
+            "url": "/dom/events/AddEventListenerOptions-once.html"
+          }
+        ],
         "editing/run/justifycenter.html": [
           {
             "path": "editing/run/justifycenter.html",
             "timeout": "long",
             "url": "/editing/run/justifycenter.html"
           }
         ],
         "editing/run/multitest.html": [
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/dom/events/AddEventListenerOptions-once.html
@@ -0,0 +1,81 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>AddEventListenerOptions.once</title>
+<link rel="author" title="Xidorn Quan" href="https://www.upsuper.org">
+<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 invoked_once = false;
+  var invoked_normal = false;
+  function handler_once() {
+    invoked_once = true;
+  }
+  function handler_normal() {
+    invoked_normal = true;
+  }
+
+  document.addEventListener('test', handler_once, {once: true});
+  document.addEventListener('test', handler_normal);
+  document.dispatchEvent(new Event('test'));
+  assert_equals(invoked_once, true, "Once handler should be invoked");
+  assert_equals(invoked_normal, true, "Normal handler should be invoked");
+
+  invoked_once = false;
+  invoked_normal = false;
+  document.dispatchEvent(new Event('test'));
+  assert_equals(invoked_once, false, "Once handler shouldn't be invoked again");
+  assert_equals(invoked_normal, true, "Normal handler should be invoked again");
+  document.removeEventListener('test', handler_normal);
+}, "Once listener should be invoked only once");
+
+test(function() {
+  var invoked_count = 0;
+  function handler() {
+    invoked_count++;
+    if (invoked_count == 1)
+      document.dispatchEvent(new Event('test'));
+  }
+  document.addEventListener('test', handler, {once: true});
+  document.dispatchEvent(new Event('test'));
+  assert_equals(invoked_count, 1, "Once handler should only be invoked once");
+
+  invoked_count = 0;
+  function handler2() {
+    invoked_count++;
+    if (invoked_count == 1)
+      document.addEventListener('test', handler2, {once: true});
+    if (invoked_count <= 2)
+      document.dispatchEvent(new Event('test'));
+  }
+  document.addEventListener('test', handler2, {once: true});
+  document.dispatchEvent(new Event('test'));
+  assert_equals(invoked_count, 2, "Once handler should only be invoked once after each adding");
+}, "Once listener should be invoked only once even if the event is nested");
+
+test(function() {
+  var invoked_count = 0;
+  function handler() {
+    invoked_count++;
+  }
+
+  document.addEventListener('test', handler, {once: true});
+  document.addEventListener('test', handler);
+  document.dispatchEvent(new Event('test'));
+  assert_equals(invoked_count, 1, "The handler should only be added once");
+
+  invoked_count = 0;
+  document.dispatchEvent(new Event('test'));
+  assert_equals(invoked_count, 0, "The handler was added as a once listener");
+
+  invoked_count = 0;
+  document.addEventListener('test', handler, {once: true});
+  document.removeEventListener('test', handler);
+  document.dispatchEvent(new Event('test'));
+  assert_equals(invoked_count, 0, "The handler should have been removed");
+}, "Once listener should be added / removed like normal listeners");
+
+</script>