summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/observable/tentative/observable-forEach.window.js
blob: 71c2e1730393807c7cb4ca2e55ef7099d9afa837 (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
async function loadIframeAndReturnContentWindow() {
   // Create and attach an iframe.
  const iframe = document.createElement('iframe');
  const iframeLoadPromise = new Promise((resolve, reject) => {
    iframe.onload = resolve;
    iframe.onerror = reject;
  });
  document.body.append(iframe);
  await iframeLoadPromise;
  return iframe.contentWindow;
}

promise_test(async t => {
  const contentWin = await loadIframeAndReturnContentWindow();

  window.results = [];

  contentWin.eval(`
    const parentResults = parent.results;

    const source = new Observable(subscriber => {
      window.frameElement.remove();

      // This invokes the forEach() operator's internal observer's next steps,
      // which at least in Chromium, must have a special "context is detached"
      // check to early-return, so as to not crash.
      subscriber.next(1);
    });

    source.forEach(value => {
      parentResults.push(value);
    });
  `);

  // If we got here, we didn't crash! Let's also check that `results` is empty.
  assert_array_equals(results, []);
}, "forEach()'s internal observer's next steps do not crash in a detached document");

promise_test(async t => {
  const contentWin = await loadIframeAndReturnContentWindow();

  window.results = [];

  contentWin.eval(`
    const parentResults = parent.results;

    const source = new Observable(subscriber => {
      subscriber.next(1);
    });

    source.forEach(value => {
      window.frameElement.remove();
      parentResults.push(value);
    });
  `);

  assert_array_equals(results, [1]);
}, "forEach()'s internal observer's next steps do not crash when visitor " +
   "callback detaches the document");