1'use strict';
2
3const common = require('../common');
4
5// Manually converted from https://github.com/web-platform-tests/wpt/blob/master/dom/events/AddEventListenerOptions-passive.html
6// in order to define the `document` ourselves
7
8const {
9  fail,
10  ok,
11  strictEqual
12} = require('assert');
13
14{
15  const document = new EventTarget();
16  let supportsPassive = false;
17  const query_options = {
18    get passive() {
19      supportsPassive = true;
20      return false;
21    },
22    get dummy() {
23      fail('dummy value getter invoked');
24      return false;
25    }
26  };
27
28  document.addEventListener('test_event', null, query_options);
29  ok(supportsPassive);
30
31  supportsPassive = false;
32  document.removeEventListener('test_event', null, query_options);
33  strictEqual(supportsPassive, false);
34}
35{
36  function testPassiveValue(optionsValue, expectedDefaultPrevented) {
37    const document = new EventTarget();
38    let defaultPrevented;
39    function handler(e) {
40      if (e.defaultPrevented) {
41        fail('Event prematurely marked defaultPrevented');
42      }
43      e.preventDefault();
44      defaultPrevented = e.defaultPrevented;
45    }
46    document.addEventListener('test', handler, optionsValue);
47    // TODO the WHATWG test is more extensive here and tests dispatching on
48    // document.body, if we ever support getParent we should amend this
49    const ev = new Event('test', { bubbles: true, cancelable: true });
50    const uncanceled = document.dispatchEvent(ev);
51
52    strictEqual(defaultPrevented, expectedDefaultPrevented);
53    strictEqual(uncanceled, !expectedDefaultPrevented);
54
55    document.removeEventListener('test', handler, optionsValue);
56  }
57  testPassiveValue(undefined, true);
58  testPassiveValue({}, true);
59  testPassiveValue({ passive: false }, true);
60
61  common.skip('TODO: passive listeners is still broken');
62  testPassiveValue({ passive: 1 }, false);
63  testPassiveValue({ passive: true }, false);
64  testPassiveValue({ passive: 0 }, true);
65}
66