File: beforeunload-canceling.html

package info (click to toggle)
firefox-esr 68.10.0esr-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,143,932 kB
  • sloc: cpp: 5,227,879; javascript: 4,315,531; ansic: 2,467,042; python: 794,975; java: 349,993; asm: 232,034; xml: 228,320; sh: 82,008; lisp: 41,202; makefile: 22,347; perl: 15,555; objc: 5,277; cs: 4,725; yacc: 1,778; ada: 1,681; pascal: 1,673; lex: 1,417; exp: 527; php: 436; ruby: 225; awk: 162; sed: 53; csh: 44
file content (181 lines) | stat: -rw-r--r-- 5,337 bytes parent folder | download | duplicates (12)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<meta charset="utf-8">
<title>beforeunload return value cancelation behavior</title>
<link rel="help" href="https://html.spec.whatwg.org/multipage/webappapis.html#the-event-handler-processing-algorithm">
<link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<div id="log"></div>

<script>
"use strict";

promise_test(t => {
  let onbeforeunloadHappened = false;
  window.onbeforeunload = t.step_func(() => {
    onbeforeunloadHappened = true;
    return "cancel me";
  });

  const eventWatcher = new EventWatcher(t, window, "beforeunload");
  const promise = eventWatcher.wait_for("beforeunload").then(e => {
    assert_true(onbeforeunloadHappened, "CustomEvent must be able to trigger the event handler");
    assert_false(e.defaultPrevented, "The event must not have been canceled");
    window.onbeforeunload = null;
  });

  window.dispatchEvent(new CustomEvent("beforeunload"));

  return promise;
}, "Returning a string must not cancel the event: CustomEvent, non-cancelable");

promise_test(t => {
  let onbeforeunloadHappened = false;
  window.onbeforeunload = t.step_func(() => {
    onbeforeunloadHappened = true;
    return "cancel me";
  });

  const eventWatcher = new EventWatcher(t, window, "beforeunload");
  const promise = eventWatcher.wait_for("beforeunload").then(e => {
    assert_true(onbeforeunloadHappened, "CustomEvent must be able to trigger the event handler");
    assert_false(e.defaultPrevented, "The event must not have been canceled");
    window.onbeforeunload = null;
    t.done();
  });

  window.dispatchEvent(new CustomEvent("beforeunload", { cancelable: true }));

  return promise;
}, "Returning a string must not cancel the event: CustomEvent, cancelable");

promise_test(t => {
  let onbeforeunloadHappened = false;
  window.onbeforeunload = t.step_func(() => {
    onbeforeunloadHappened = true;
    return false;
  });

  const eventWatcher = new EventWatcher(t, window, "beforeunload");
  const promise = eventWatcher.wait_for("beforeunload").then(e => {
    assert_true(onbeforeunloadHappened, "CustomEvent must be able to trigger the event handler");
    assert_false(e.defaultPrevented, "The event must not have been canceled");
    window.onbeforeunload = null;
    t.done();
  });

  window.dispatchEvent(new CustomEvent("beforeunload", { cancelable: true }));

  return promise;
}, "Returning false must not cancel the event, because it's coerced to the DOMString \"false\" which does not cancel " +
   "CustomEvents: CustomEvent, cancelable");

// This test can be removed if we update the DOM Standard to disallow createEvent("BeforeUnloadEvent"). Browser support
// is inconsistent. https://github.com/whatwg/dom/issues/362
promise_test(t => {
  const eventWatcher = new EventWatcher(t, window, "click");
  const promise = eventWatcher.wait_for("click").then(e => {
    assert_false(e.defaultPrevented, "The event must not have been canceled");
    window.onbeforeunload = null;
    t.done();
  });

  const ev = document.createEvent("BeforeUnloadEvent");
  ev.initEvent("click", false, true);
  window.dispatchEvent(ev);

  return promise;
}, "Returning a string must not cancel the event: BeforeUnloadEvent with type \"click\", cancelable");

const testCases = [
  {
    valueToReturn: null,
    expectCancelation: false,
    expectedReturnValue: ""
  },
  {
    valueToReturn: undefined,
    expectCancelation: false,
    expectedReturnValue: ""
  },
  {
    valueToReturn: "",
    expectCancelation: true,
    expectedReturnValue: ""
  },
  {
    valueToReturn: false,
    expectCancelation: true,
    expectedReturnValue: "false"
  },
  {
    valueToReturn: true,
    expectCancelation: true,
    expectedReturnValue: "true"
  },
  {
    valueToReturn: 0,
    expectCancelation: true,
    expectedReturnValue: "0"
  },
  {
    valueToReturn: null,
    expectCancelation: false,
    setReturnValue: "foo",
    expectedReturnValue: "foo"
  },
  {
    valueToReturn: undefined,
    expectCancelation: false,
    setReturnValue: "foo",
    expectedReturnValue: "foo"
  },
  {
    valueToReturn: "",
    expectCancelation: true,
    setReturnValue: "foo",
    expectedReturnValue: "foo"
  },
  {
    valueToReturn: false,
    expectCancelation: true,
    setReturnValue: "foo",
    expectedReturnValue: "foo"
  },
  {
    valueToReturn: true,
    expectCancelation: true,
    setReturnValue: "foo",
    expectedReturnValue: "foo"
  },
  {
    valueToReturn: 0,
    expectCancelation: true,
    setReturnValue: "foo",
    expectedReturnValue: "foo"
  }
];

var testCaseIndex = 0;
function runNextTest() {
  const testCase = testCases[testCaseIndex];

  const labelAboutReturnValue = testCase.setReturnValue === undefined ? "" :
    `; setting returnValue to ${testCase.setReturnValue}`;

  async_test(t => {
    const iframe = document.createElement("iframe");
    iframe.onload = t.step_func(() => {
      iframe.contentWindow.runTest(t, testCase);
      if (++testCaseIndex < testCases.length)
        runNextTest();
    });

    iframe.src = "beforeunload-canceling-1.html";
    document.body.appendChild(iframe);
  }, `Returning ${testCase.valueToReturn} with a real iframe unloading${labelAboutReturnValue}`);
}

runNextTest();
</script>