Re-sync web-platform-tests/dom/events from upstream
[WebKit-https.git] / LayoutTests / imported / w3c / web-platform-tests / dom / events / event-global-extra.window.js
1 const otherWindow = document.body.appendChild(document.createElement("iframe")).contentWindow;
2
3 ["EventTarget", "XMLHttpRequest"].forEach(constructorName => {
4   async_test(t => {
5     const eventTarget = new otherWindow[constructorName]();
6     eventTarget.addEventListener("hi", t.step_func_done(e => {
7       assert_equals(otherWindow.event, undefined);
8       assert_equals(e, window.event);
9     }));
10     eventTarget.dispatchEvent(new Event("hi"));
11   }, "window.event for constructors from another global: " + constructorName);
12 });
13
14 // XXX: It would be good to test a subclass of EventTarget once we sort out
15 // https://github.com/heycam/webidl/issues/540
16
17 async_test(t => {
18   const element = document.body.appendChild(otherWindow.document.createElement("meh"));
19   element.addEventListener("yo", t.step_func_done(e => {
20     assert_equals(e, window.event);
21   }));
22   element.dispatchEvent(new Event("yo"));
23 }, "window.event and element from another document");
24
25 async_test(t => {
26   const doc = otherWindow.document,
27         element = doc.body.appendChild(doc.createElement("meh")),
28         child = element.appendChild(doc.createElement("bleh"));
29   element.addEventListener("yoyo", t.step_func(e => {
30     document.body.appendChild(element);
31     assert_equals(element.ownerDocument, document);
32     assert_equals(window.event, e);
33     assert_equals(otherWindow.event, undefined);
34   }), true);
35   element.addEventListener("yoyo", t.step_func(e => {
36     assert_equals(element.ownerDocument, document);
37     assert_equals(window.event, e);
38     assert_equals(otherWindow.event, undefined);
39   }), true);
40   child.addEventListener("yoyo", t.step_func_done(e => {
41     assert_equals(child.ownerDocument, document);
42     assert_equals(window.event, e);
43     assert_equals(otherWindow.event, undefined);
44   }));
45   child.dispatchEvent(new Event("yoyo"));
46 }, "window.event and moving an element post-dispatch");
47
48 test(t => {
49   const host = document.createElement("div"),
50         shadow = host.attachShadow({ mode: "open" }),
51         child = shadow.appendChild(document.createElement("trala")),
52         furtherChild = child.appendChild(document.createElement("waddup"));
53   let counter = 0;
54   host.addEventListener("hi", t.step_func(e => {
55     assert_equals(window.event, e);
56     assert_equals(counter++, 3);
57   }));
58   child.addEventListener("hi", t.step_func(e => {
59     assert_equals(window.event, undefined);
60     assert_equals(counter++, 2);
61   }));
62   furtherChild.addEventListener("hi", t.step_func(e => {
63     host.appendChild(child);
64     assert_equals(window.event, undefined);
65     assert_equals(counter++, 0);
66   }));
67   furtherChild.addEventListener("hi", t.step_func(e => {
68     assert_equals(window.event, undefined);
69     assert_equals(counter++, 1);
70   }));
71   furtherChild.dispatchEvent(new Event("hi", { composed: true, bubbles: true }));
72   assert_equals(counter, 4);
73 }, "window.event should not be affected by nodes moving post-dispatch");
74
75 async_test(t => {
76   const frame = document.body.appendChild(document.createElement("iframe"));
77   frame.src = "resources/event-global-extra-frame.html";
78   frame.onload = t.step_func_done((load_event) => {
79     const event = new Event("hi");
80     document.addEventListener("hi", frame.contentWindow.listener); // listener intentionally not wrapped in t.step_func
81     document.addEventListener("hi", t.step_func(e => {
82       assert_equals(event, e);
83       assert_equals(window.event, e);
84     }));
85     document.dispatchEvent(event);
86     assert_equals(frameState.event, event);
87     assert_equals(frameState.windowEvent, event);
88     assert_equals(frameState.parentEvent, load_event);
89   });
90 }, "Listener from a different global");