summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/shadow-dom/imperative-slot-api-slotchange.html
blob: 236aaea18a14dfb4f0f62948a5ea2df38e3ca4f1 (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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!DOCTYPE html>
<title>Shadow DOM: Imperative Slot API slotchange event</title>
<meta name="author" title="Yu Han" href="mailto:yuzhehan@chromium.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://dom.spec.whatwg.org/#signaling-slot-change">
<script src="resources/shadow-dom.js"></script>

<div id="test_slotchange">
  <div id="host">
    <template id="shadow_root" data-mode="open" data-slot-assignment="manual">
      <slot id="s1"><div id="fb">fallback</div></slot>
      <slot id="s2"></slot>
      <div>
        <slot id="s2.5"></slot>
      </div>
      <slot id="s3"></slot>
    </template>
    <div id="c1"></div>
    <div id="c2"></div>
  </div>
  <div id="c4"></div>
</div>

<script>
function getDataCollection() {
  return {
    s1EventCount: 0,
    s2EventCount: 0,
    s3EventCount: 0,
    s1ResolveFn: null,
    s2ResolveFn: null,
    s3ResolveFn: null,
  }
}

function setupShadowDOM(id, test, data) {
  let tTree = createTestTree(id);
  tTree.s1.addEventListener('slotchange', (event) => {
    if (!event.isFakeEvent) {
      test.step(function () {
          assert_equals(event.type, 'slotchange', 'slotchange event\'s type must be "slotchange"');
          assert_equals(event.target, tTree.s1, 'slotchange event\'s target must be the slot element');
          assert_equals(event.relatedTarget, undefined, 'slotchange must not set relatedTarget');
      });
      data.s1EventCount++;
    }
    data.s1ResolveFn();
  });
  tTree.s2.addEventListener('slotchange', (event) => {
    if (!event.isFakeEvent) {
      test.step(function () {
          assert_equals(event.type, 'slotchange', 'slotchange event\'s type must be "slotchange"');
          assert_equals(event.target, tTree.s2, 'slotchange event\'s target must be the slot element');
          assert_equals(event.relatedTarget, undefined, 'slotchange must not set relatedTarget');
      });
      data.s2EventCount++;
    }
    data.s2ResolveFn();
  });
  tTree.s3.addEventListener('slotchange', (event) => {
    if (!event.isFakeEvent) {
      test.step(function () {
        assert_equals(event.type, 'slotchange', 'slotchange event\'s type must be "slotchange"');
        // listen to bubbling events.
        assert_equals(event.relatedTarget, undefined, 'slotchange must not set relatedTarget');
      });
      data.s3EventCount++;
    }
    data.s3ResolveFn();
  });
  return tTree;
}

function monitorSlots(data) {
    const s1Promise = new Promise((resolve, reject) => {
      data.s1ResolveFn = resolve;
    });
    const s2Promise = new Promise((resolve, reject) => {
      data.s2ResolveFn = resolve;
    });
    const s3Promise = new Promise((resolve, reject) => {
      data.s3ResolveFn = resolve;
    });
    return [s1Promise, s2Promise, s3Promise];
}
</script>

<script>
// Tests:
async_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_slotchange, test, data);
  let [s1Promise, s2Promise] = monitorSlots(data);

  tTree.s1.assign(tTree.c1);
  tTree.s2.assign(tTree.c2);

  assert_equals(data.s1EventCount, 0, 'slotchange event must not be fired synchronously');
  assert_equals(data.s2EventCount, 0);

  Promise.all([s1Promise, s2Promise]).then(test.step_func_done(() => {
    assert_equals(data.s1EventCount, 1);
    assert_equals(data.s2EventCount, 1);
  }));
}, 'slotchange event must not fire synchronously.');

async_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_slotchange, test, data);
  let [s1Promise, s2Promise] = monitorSlots(data);

  tTree.s1.assign();;
  tTree.s2.assign();
  tTree.host.insertBefore(tTree.c4, tTree.c1);

  Promise.all([s1Promise, s2Promise]).then(test.step_func_done(() => {
    assert_equals(data.s1EventCount, 0);
    assert_equals(data.s2EventCount, 0);
  }));

  // use fake event to trigger event handler.
  let fakeEvent = new Event('slotchange');
  fakeEvent.isFakeEvent = true;
  tTree.s1.dispatchEvent(fakeEvent);
  tTree.s2.dispatchEvent(fakeEvent);
}, 'slotchange event should not fire when assignments do not change assignedNodes.');

async_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_slotchange,test, data);
  let [s1Promise] = monitorSlots(data);

  tTree.s1.assign(tTree.c1, tTree.c2);

  s1Promise.then(test.step_func(() => {
    assert_equals(data.s1EventCount, 1);

    [s1Promise] = monitorSlots(data);
    tTree.s1.assign(tTree.c1, tTree.c2);
    tTree.s1.assign(tTree.c1, tTree.c2, tTree.c1, tTree.c2, tTree.c2);

    s1Promise.then(test.step_func_done(() => {
      assert_equals(data.s1EventCount, 1);
    }));

    let fakeEvent = new Event('slotchange');
    fakeEvent.isFakeEvent = true;
    tTree.s1.dispatchEvent(fakeEvent);
  }));

}, 'slotchange event should not fire when same node is assigned.');

async_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_slotchange, test, data);
  let [s1Promise, s2Promise] = monitorSlots(data);

  tTree.s1.assign(tTree.c1);
  tTree.s2.assign(tTree.c2);

  Promise.all([s1Promise, s2Promise]).then(test.step_func_done(() => {
    assert_equals(data.s1EventCount, 1);
    assert_equals(data.s2EventCount, 1);
  }));
}, "Fire slotchange event when slot's assigned nodes changes.");

async_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_slotchange, test, data);
  let [s1Promise, s2Promise] = monitorSlots(data);

  tTree.s1.assign(tTree.c1);

  s1Promise.then(test.step_func(() => {
    assert_equals(data.s1EventCount, 1);

    [s1Promise, s2Promise] = monitorSlots(data);
    tTree.s2.assign(tTree.c1);

    Promise.all([s1Promise, s2Promise]).then(test.step_func_done(() => {
      assert_equals(data.s1EventCount, 2);
      assert_equals(data.s2EventCount, 1);
    }));
  }));
}, "Fire slotchange event on previous slot and new slot when node is reassigned.");

async_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_slotchange, test, data);
  let [s1Promise] = monitorSlots(data);

  tTree.s1.assign(tTree.c1);

  s1Promise.then(test.step_func(() => {
    assert_equals(data.s1EventCount, 1);

    [s1Promise] = monitorSlots(data);
    tTree.s1.assign();

    s1Promise.then(test.step_func_done(() => {
      assert_equals(data.s1EventCount, 2);
    }));
  }));
}, "Fire slotchange event on node assignment and when assigned node is removed.");

async_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_slotchange, test, data);
  let [s1Promise] = monitorSlots(data);

  tTree.s1.assign(tTree.c1, tTree.c2);

  s1Promise.then(test.step_func(() => {
    assert_equals(data.s1EventCount, 1);

    [s1Promise] = monitorSlots(data);
    tTree.s1.assign(tTree.c2, tTree.c1);

    s1Promise.then(test.step_func_done(() => {
      assert_equals(data.s1EventCount, 2);
    }));
  }));
}, "Fire slotchange event when order of assigned nodes changes.");

promise_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_slotchange, test, data);
  let [s1Promise] = monitorSlots(data);

  tTree.s1.assign(tTree.c1);

  return s1Promise.then(test.step_func(() => {
    assert_equals(data.s1EventCount, 1);

    [s1Promise] = monitorSlots(data);
    tTree.c1.remove();

    return s1Promise;
  }))
  .then(test.step_func(() => {
      assert_equals(data.s1EventCount, 2);
  }));
}, "Fire slotchange event when assigned node is removed.");

promise_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_slotchange, test, data);
  [s1Promise] = monitorSlots(data);

  tTree.s1.assign(tTree.c1);

  return s1Promise.then(test.step_func(() => {
    assert_equals(data.s1EventCount, 1);

    [s1Promise] = monitorSlots(data);
    tTree.s1.remove();

    return s1Promise;
  }))
  .then(test.step_func(() => {
      assert_equals(data.s1EventCount, 2);
  }));
}, "Fire slotchange event when removing a slot from Shadows Root that changes its assigned nodes.");

async_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_slotchange, test, data);
  let [s1Promise] = monitorSlots(data);

  tTree.s1.remove();

  let fakeEvent = new Event('slotchange');
  fakeEvent.isFakeEvent = true;
  tTree.s1.dispatchEvent(fakeEvent);

  s1Promise.then(test.step_func(() => {
    assert_equals(data.s2EventCount, 0);

    [s1Promise, s2Promise] = monitorSlots(data);
    tTree.shadow_root.insertBefore(tTree.s1, tTree.s2);

    tTree.s1.dispatchEvent(fakeEvent);
    tTree.s2.dispatchEvent(fakeEvent);

    Promise.all([s1Promise, s2Promise]).then(test.step_func_done(() => {
      assert_equals(data.s1EventCount, 0);
      assert_equals(data.s2EventCount, 0);
    }));
  }));

}, "No slotchange event when adding or removing an empty slot.");

async_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_slotchange, test, data);
  let [s1Promise, s2Promise] = monitorSlots(data);

  tTree.host.appendChild(document.createElement("div"));

  let fakeEvent = new Event('slotchange');
  fakeEvent.isFakeEvent = true;
  tTree.s1.dispatchEvent(fakeEvent);
  tTree.s2.dispatchEvent(fakeEvent);

  Promise.all([s1Promise, s2Promise]).then(test.step_func(() => {
    assert_equals(data.s1EventCount, 0);
    assert_equals(data.s2EventCount, 0);

    [s1Promise, s2Promise] = monitorSlots(data);
    tTree.shadow_root.insertBefore(document.createElement("div"), tTree.s2);

    tTree.s1.dispatchEvent(fakeEvent);
    tTree.s2.dispatchEvent(fakeEvent);

    Promise.all([s1Promise, s2Promise]).then(test.step_func_done(() => {
      assert_equals(data.s1EventCount, 0);
      assert_equals(data.s2EventCount, 0);
    }));
  }));

}, "No slotchange event when adding another slotable.");

</script>

<div id="test_nested_slotchange">
  <div>
    <template data-mode="open" data-slot-assignment="manual">
      <div>
        <template data-mode="open" data-slot-assignment="manual">
          <slot id="s2"></slot>
          <slot id="s3"></slot>
        </template>
        <slot id="s1"></slot>
      </div>
    </template>
    <div id="c1"></div>
  </div>
</div>

<script>
async_test((test) => {
  const data = getDataCollection();
  let tTree = setupShadowDOM(test_nested_slotchange, test, data);
  let [s1Promise, s2Promise, s3Promise] = monitorSlots(data);

  tTree.s3.assign(tTree.s1);

  s3Promise.then(test.step_func(() => {
    assert_equals(data.s3EventCount, 1);
    [s1Promise, s2Promise, s3Promise] = monitorSlots(data);

    tTree.s1.assign(tTree.c1);

    Promise.all([s1Promise, s3Promise]).then(test.step_func_done(() => {
      assert_equals(data.s1EventCount, 1);
      assert_equals(data.s3EventCount, 2);
    }));
  }));
}, "Fire slotchange event when assign node to nested slot, ensure event bubbles ups.");

promise_test(async t => {
  async function mutationObserversRun() {
    return new Promise(r => {
      t.step_timeout(r, 0);
    });
  }
  let tTree = createTestTree(test_slotchange);

  tTree.s1.assign(tTree.c1);
  tTree["s2.5"].assign(tTree.c2);

  let slotChangedOrder = [];

  // Clears out pending mutation observers
  await mutationObserversRun();

  tTree.s1.addEventListener("slotchange", function() {
    slotChangedOrder.push("s1");
  });

  tTree.s3.addEventListener("slotchange", function() {
    slotChangedOrder.push("s3");
  });

  tTree["s2.5"].addEventListener("slotchange", function() {
    slotChangedOrder.push("s2.5");
  });

  tTree.s3.assign(tTree.c2, tTree.c1);
  await mutationObserversRun();
  assert_array_equals(slotChangedOrder, ["s1", "s2.5", "s3"]);
}, 'Signal a slot change should be done in tree order.');
</script>