summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/events/event-global.html
blob: f70606fb65496a689515f0e01719a86a98c8b371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<title>window.event tests</title>
<link rel="author" title="Mike Taylor" href="mailto:miketaylr@gmail.com">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
setup({allow_uncaught_exception: true});

test(t => {
  assert_own_property(window, "event");
  assert_equals(window.event, undefined);
}, "event exists on window, which is initially set to undefined");

async_test(t => {
  let target = document.createElement("div");
  assert_equals(window.event, undefined, "undefined before dispatch");

  let clickEvent = new Event("click");
  target.addEventListener("click", t.step_func_done(e => {
    assert_equals(window.event, clickEvent, "window.event set to current event during dispatch");
  }));

  target.dispatchEvent(clickEvent);
  assert_equals(window.event, undefined, "undefined after dispatch");
}, "window.event is only defined during dispatch");

async_test(t => {
  let parent = document.createElement("div");
  let root = parent.attachShadow({mode: "closed"});
  let span = document.createElement("span");
  root.appendChild(span);

  span.addEventListener("test", t.step_func(e => {
    assert_equals(window.event, undefined);
    assert_not_equals(window.event, e);
  }));

  parent.addEventListener("test", t.step_func_done(e => {
    assert_equals(window.event, e);
    assert_not_equals(window.event, undefined);
  }));

  parent.dispatchEvent(new Event("test", {composed: true}));
}, "window.event is undefined if the target is in a shadow tree (event dispatched outside shadow tree)");

async_test(t => {
  let parent = document.createElement("div");
  let root = parent.attachShadow({mode: "closed"});
  let span = document.createElement("span");
  root.appendChild(span);
  let shadowNode = root.firstElementChild;

  shadowNode.addEventListener("test", t.step_func((e) => {
    assert_not_equals(window.event, e);
    assert_equals(window.event, undefined);
  }));

  parent.addEventListener("test", t.step_func_done(e => {
    assert_equals(window.event, e);
    assert_not_equals(window.event, undefined);
  }));

  shadowNode.dispatchEvent(new Event("test", {composed: true, bubbles: true}));
}, "window.event is undefined if the target is in a shadow tree (event dispatched inside shadow tree)");

async_test(t => {
  let parent = document.createElement("div");
  let root = parent.attachShadow({mode: "open"});
  document.body.append(parent)
  let span = document.createElement("span");
  root.append(span);
  let shadowNode = root.firstElementChild;

  shadowNode.addEventListener("error", t.step_func(e => {
    assert_not_equals(window.event, e);
    assert_equals(window.event, undefined);
  }));

  let windowOnErrorCalled = false;
  window.onerror = t.step_func_done(() => {
    windowOnErrorCalled = true;
    assert_equals(typeof window.event, "object");
    assert_equals(window.event.type, "error");
  });

  shadowNode.dispatchEvent(new ErrorEvent("error", {composed: true, bubbles: true}));
  assert_true(windowOnErrorCalled);
}, "window.event is undefined inside window.onerror if the target is in a shadow tree (ErrorEvent dispatched inside shadow tree)");

async_test(t => {
  let target1 = document.createElement("div");
  let target2 = document.createElement("div");

  target2.addEventListener("dude", t.step_func(() => {
    assert_equals(window.event.type, "dude");
  }));

  target1.addEventListener("cool", t.step_func_done(() => {
    assert_equals(window.event.type, "cool", "got expected event from global event during dispatch");
    target2.dispatchEvent(new Event("dude"));
    assert_equals(window.event.type, "cool", "got expected event from global event after handling a different event handler callback");
  }));

  target1.dispatchEvent(new Event("cool"));
}, "window.event is set to the current event during dispatch");

async_test(t => {
  let target = document.createElement("div");

  target.addEventListener("click", t.step_func_done(e => {
    assert_equals(e, window.event);
  }));

  target.dispatchEvent(new Event("click"));
}, "window.event is set to the current event, which is the event passed to dispatch");

async_test(t => {
  let target = new XMLHttpRequest();

  target.onload = t.step_func_done(e => {
    assert_equals(e, window.event);
  });

  target.dispatchEvent(new Event("load"));
}, "window.event is set to the current event, which is the event passed to dispatch (2)");
</script>