File: dialog-requestclose.html

package info (click to toggle)
thunderbird 1%3A140.4.0esr-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 4,609,436 kB
  • sloc: cpp: 7,672,442; javascript: 5,901,613; ansic: 3,898,954; python: 1,413,343; xml: 653,997; asm: 462,286; java: 180,927; sh: 113,489; makefile: 20,463; perl: 14,288; objc: 13,059; yacc: 4,583; pascal: 3,352; lex: 1,720; ruby: 1,222; exp: 762; sql: 715; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (215 lines) | stat: -rw-r--r-- 8,224 bytes parent folder | download | duplicates (8)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!doctype html>
<meta charset="utf-8">
<link rel=help href="https://html.spec.whatwg.org/multipage/interactive-elements.html#dom-dialog-request-close">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>

<dialog>Dialog</dialog>

<script>
function waitForRender() {
  return new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
}

const dialog = document.querySelector('dialog');
function openDialog(openMethod) {
  assert_false(dialog.open);
  assert_false(dialog.matches(':open'));
  switch (openMethod) {
    case 'modeless':
      dialog.show();
      break;
    case 'modal':
      dialog.showModal();
      break;
    case 'open':
      dialog.open = true;
      break;
    default:
      assert_unreached('Unknown open method');
  }
  assert_true(dialog.open);
  assert_true(dialog.matches(':open'));
  assert_equals(dialog.matches(':modal'),openMethod === 'modal');
}

function setup(t,closedby) {
  t.add_cleanup(() => {
    dialog.close();
    dialog.removeAttribute('closedby');
    dialog.returnValue = '';
  });
  assert_false(dialog.hasAttribute('closedby'));
  if (closedby) {
    dialog.setAttribute('closedby',closedby);
  }
  return t.get_signal();
}

['modeless','modal','open'].forEach(openMethod => {
  [null,'any','closedrequest','none'].forEach(closedby => {
    const testDescription = `for ${openMethod} dialog with closedby=${closedby}`;
    promise_test(async (t) => {
      setup(t,closedby);
      openDialog(openMethod);
      dialog.requestClose();
      assert_false(dialog.open);
      assert_false(dialog.matches(':open'));
    },`requestClose basic behavior ${testDescription}`);

    promise_test(async (t) => {
      const signal = setup(t,closedby);
      let events = [];
      const { promise: untilFullyClosed, resolve: hasClosed } = Promise.withResolvers();
      dialog.addEventListener('cancel',() => events.push('cancel'),{signal});
      dialog.addEventListener('close',() => {
        events.push('close');
        hasClosed();
      },{signal});
      openDialog(openMethod);
      assert_array_equals(events,[]);
      dialog.requestClose();
      assert_false(dialog.open);
      assert_false(dialog.matches(':open'));
      assert_array_equals(events,['cancel'],'close is scheduled');
      await untilFullyClosed;
      assert_array_equals(events,['cancel','close']);
    },`requestClose fires both cancel and close ${testDescription}`);

    promise_test(async (t) => {
      const signal = setup(t,'none');
      let events = [];
      const { promise: untilFullyClosed, resolve: hasClosed } = Promise.withResolvers();
      const { promise: untilFullyClosedAgain, resolve: hasClosedAgain } = Promise.withResolvers();
      const closeResolvers = [hasClosed, hasClosedAgain];
      dialog.addEventListener('cancel',() => events.push('cancel'),{signal});
      dialog.addEventListener('close',() => {
        events.push('close');
        closeResolvers.shift()();
      },{signal});
      openDialog(openMethod);
      dialog.setAttribute('closedby',closedby);
      assert_array_equals(events,[]);
      dialog.requestClose();
      assert_false(dialog.open,'Adding closedby after dialog is open');
      assert_false(dialog.matches(':open'));
      assert_array_equals(events,['cancel']);
      await untilFullyClosed;
      events=[];
      openDialog(openMethod);
      dialog.removeAttribute('closedby');
      assert_array_equals(events,[]);
      dialog.requestClose();
      assert_false(dialog.open,'Removing closedby after dialog is open');
      assert_array_equals(events,['cancel']);
      await untilFullyClosedAgain;
      assert_array_equals(events,['cancel', 'close']);
    },`closedby has no effect on dialog.requestClose() ${testDescription}`);

    if (closedby != "none") {
      promise_test(async (t) => {
        const signal = setup(t,closedby);
        let shouldPreventDefault = true;
        dialog.addEventListener('cancel',(e) => {
          if (shouldPreventDefault) {
            e.preventDefault();
          }
        },{signal});
        openDialog(openMethod);
        dialog.requestClose();
        assert_true(dialog.open,'cancel event was cancelled - dialog shouldn\'t close');
        assert_true(dialog.matches(':open'));
        shouldPreventDefault = false;
        dialog.requestClose();
        assert_false(dialog.open,'cancel event was not cancelled - dialog should now close');
        assert_false(dialog.matches(':open'));
      },`requestClose can be cancelled ${testDescription}`);

      promise_test(async (t) => {
        const signal = setup(t,closedby);
        dialog.addEventListener('cancel',(e) => e.preventDefault(),{signal});
        openDialog(openMethod);
        // No user activation here.
        dialog.requestClose();
        dialog.requestClose();
        dialog.requestClose();
        assert_true(dialog.open,'cancel event was cancelled - dialog shouldn\'t close');
        assert_true(dialog.matches(':open'));
      },`requestClose avoids abuse prevention logic ${testDescription}`);

      promise_test(async (t) => {
        setup(t,closedby);
        openDialog(openMethod);
        assert_equals(dialog.returnValue,'','Return value starts out empty');
        const returnValue = 'The return value';
        dialog.requestClose(returnValue);
        assert_false(dialog.open);
        assert_false(dialog.matches(':open'));
        assert_equals(dialog.returnValue,returnValue,'Return value should be set');
        dialog.show();
        dialog.close();
        assert_equals(dialog.returnValue,returnValue,'Return value should not be changed by close()');
        dialog.show();
        dialog.close('another');
        assert_equals(dialog.returnValue,'another','Return value changes via close(value)');
      },`requestClose(returnValue) passes along the return value ${testDescription}`);

      promise_test(async (t) => {
        setup(t,closedby);
        dialog.addEventListener('cancel',(e) => e.preventDefault(),{once:true});
        openDialog(openMethod);
        dialog.returnValue = 'foo';
        assert_equals(dialog.returnValue,'foo');
        dialog.requestClose('This should not get saved');
        assert_true(dialog.open,'cancelled');
        assert_true(dialog.matches(':open'));
        assert_equals(dialog.returnValue,'foo','Return value should not be changed');
      },`requestClose(returnValue) doesn't change returnvalue when cancelled ${testDescription}`);
    }
  });
});

promise_test(async (t) => {
  setup(t);
  dialog.open = true;
  dialog.requestClose();
  assert_false(dialog.open);
},`requestClose basic behavior when dialog is open via attribute`);

promise_test(async (t) => {
  const signal = setup(t);
  let events = [];
  const { promise: untilFullyClosed, resolve: hasClosed } = Promise.withResolvers();
  dialog.addEventListener('cancel',() => events.push('cancel'),{signal});
  dialog.addEventListener('close',() => {
    events.push('close')
    hasClosed();
  },{signal});
  dialog.open = true;
  assert_array_equals(events,[]);
  dialog.requestClose();
  assert_false(dialog.open);
  assert_array_equals(events,['cancel'],'close is scheduled');
  await untilFullyClosed;
  assert_array_equals(events,['cancel','close']);
},`requestClose fires cancel and close when dialog is open via attribute`);

promise_test(async (t) => {
  await setup(t);
  dialog.open = true;
  assert_equals(dialog.returnValue,'','Return value starts out empty');
  const returnValue = 'The return value';
  dialog.requestClose(returnValue);
  assert_false(dialog.open);
  assert_equals(dialog.returnValue,returnValue,'Return value should be set');
  dialog.show();
  dialog.close();
  assert_equals(dialog.returnValue,returnValue,'Return value should not be changed by close()');
  dialog.show();
  dialog.close('another');
  assert_equals(dialog.returnValue,'another','Return value changes via close(value)');
},`requestClose(returnValue) passes along the return value when dialog is open via attribute`);
</script>