summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/close-watcher/abortsignal.html
blob: ec360f483d43a1ae5e02ec17d5c8b750a9211734 (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
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="resources/helpers.js"></script>

<body>
<script>
// TODO(domenic): maybe update createRecordingCloseWatcher() to allow passing args and use it?

test(() => {
  let watcher = new CloseWatcher({ signal: AbortSignal.abort() });
  let oncancel_called = false;
  let onclose_called = false;
  watcher.oncancel = () => oncancel_called = true;
  watcher.onclose = () => onclose_called = true;

  watcher.requestClose();

  assert_false(oncancel_called);
  assert_false(onclose_called);
}, "already-aborted AbortSignal then requestClose() fires no events");

test(() => {
  let controller = new AbortController();
  let watcher = new CloseWatcher({ signal: controller.signal });
  let oncancel_called = false;
  let onclose_called = false;
  watcher.oncancel = () => oncancel_called = true;
  watcher.onclose = () => onclose_called = true;

  controller.abort();
  watcher.requestClose();

  assert_false(oncancel_called);
  assert_false(onclose_called);
}, "abortController.abort() then requestClose() fires no events");

test(() => {
  let controller = new AbortController();
  let watcher = new CloseWatcher({ signal: controller.signal });
  let oncancel_call_count_ = 0;
  let onclose_call_count_ = 0;
  watcher.oncancel = () => oncancel_call_count_++;
  watcher.onclose = () => onclose_call_count_++;

  watcher.requestClose();
  controller.abort();

  assert_equals(oncancel_call_count_, 1);
  assert_equals(onclose_call_count_, 1);
}, "requestClose() then abortController.abort() fires only one close event");

promise_test(async () => {
  let watcher = new CloseWatcher({ signal: AbortSignal.abort() });
  let oncancel_called = false;
  let onclose_called = false;
  watcher.oncancel = () => oncancel_called = true;
  watcher.onclose = () => onclose_called = true;

  await sendCloseRequest();

  assert_false(oncancel_called);
  assert_false(onclose_called);
}, "already-aborted AbortSignal then Esc key fires no events");

promise_test(async t => {
  let controller = new AbortController();
  let watcher = new CloseWatcher({ signal: controller.signal });
  let oncancel_called = false;
  let onclose_called = false;
  watcher.oncancel = () => oncancel_called = true;
  watcher.onclose = () => onclose_called = true;

  controller.abort();
  await sendCloseRequest();

  assert_false(oncancel_called);
  assert_false(onclose_called);
}, "abortController.abort() then close via Esc key fires no events");

promise_test(async t => {
  let controller = new AbortController();
  let watcher = new CloseWatcher({ signal: controller.signal });
  let oncancel_call_count_ = 0;
  let onclose_call_count_ = 0;
  watcher.oncancel = () => oncancel_call_count_++;
  watcher.onclose = () => onclose_call_count_++;

  await sendCloseRequest();
  controller.abort();

  assert_equals(oncancel_call_count_, 1);
  assert_equals(onclose_call_count_, 1);
}, "Esc key then abortController.abort() fires only one close event");

test(t => {
  let controller = new AbortController();
  let watcher = new CloseWatcher({ signal: controller.signal });
  controller.abort();
  let watcher2 = new CloseWatcher();
  t.add_cleanup(() => watcher2.destroy());
}, "abortController.abort()ing a free CloseWatcher allows a new one to be created without a user activation");

promise_test(async t => {
  let controller = new AbortController();
  let watcher = new CloseWatcher({ signal: controller.signal });
  watcher.oncancel = () => { controller.abort(); }
  watcher.onclose = t.unreached_func("onclose");
  await test_driver.bless("give user activation so that cancel will fire", () => {
    watcher.requestClose();
  });
}, "abortController.abort() inside oncancel");

test(t => {
  let controller = new AbortController();
  let watcher = new CloseWatcher({ signal: controller.signal });
  watcher.onclose = () => { controller.abort(); }
  watcher.requestClose();
}, "abortController.abort() inside onclose is benign");
</script>