File: helpers.js

package info (click to toggle)
thunderbird 1%3A144.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,725,312 kB
  • sloc: cpp: 7,869,225; javascript: 5,974,276; ansic: 3,946,747; python: 1,421,062; xml: 654,642; asm: 474,045; java: 183,117; sh: 110,973; makefile: 20,398; perl: 14,362; objc: 13,086; yacc: 4,583; pascal: 3,448; lex: 1,720; ruby: 999; exp: 762; sql: 731; awk: 580; php: 436; lisp: 430; sed: 69; csh: 10
file content (121 lines) | stat: -rw-r--r-- 4,459 bytes parent folder | download | duplicates (20)
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
// Returns a promise that asserts the "load" and "pageshow" events are not
// fired on |target|.
function assertNoLoadAndPageshowEvent(t, target) {
  target.addEventListener("load", t.unreached_func("load should not be fired"));
  target.addEventListener("pageshow", t.unreached_func("pageshow should not be fired"));
  return new Promise(resolve => {
    // Wait 50ms to ensure events fired after asynchronous navigations are
    // also captured.
    setTimeout(resolve, 50);
  });
}

const url204 = "/common/blank.html?pipe=status(204)";
const postMessageToOpenerOnLoad = `
    window.onload = () => {
      window.opener.postMessage("loaded", "*")
    }
  `;

// -- Start of helpers for iframe initial empty document tests.

// Creates an iframe with an unset src and appends it to the document.
window.insertIframe = (t) => {
  const iframe = document.createElement("iframe");
  t.add_cleanup(() => iframe.remove());
  document.body.append(iframe);
  return iframe;
};

// Creates an iframe with src set to a URL that doesn't commit a new document
// (results in a HTTP 204 response) and appends it to the document.
window.insertIframeWith204Src = (t) => {
  const iframe = document.createElement("iframe");
  iframe.src = url204;
  t.add_cleanup(() => iframe.remove());
  document.body.append(iframe);
  return iframe;
};

// Creates an iframe with src="about:blank" and appends it to the document.
window.insertIframeWithAboutBlankSrc = (t) => {
  const iframe = document.createElement("iframe");
  t.add_cleanup(() => iframe.remove());
  iframe.src = "about:blank";
  document.body.append(iframe);
  return iframe;
};

// Creates an iframe with src="about:blank", appends it to the document, and
// waits for the non-initial about:blank document finished loading.
window.insertIframeWithAboutBlankSrcWaitForLoad = async (t) => {
  const iframe = insertIframeWithAboutBlankSrc(t);
  const aboutBlankLoad = new Promise(resolve => {
    // In some browsers, the non-initial about:blank navigation commits
    // asynchronously, while in other browsers, it would commit synchronously.
    // This means we can't wait for the "load" event as it might have already
    // ran. Instead, just wait for 100ms before resolving, as the non-initial
    // about:blank navigation will most likely take less than 100 ms to commit.
    t.step_timeout(resolve, 100);
  });
  await aboutBlankLoad;
  return iframe;
};

// Waits for the "load" event for |urlRelativeToThisDocument| to run on
// |iframe|.
window.waitForLoad = (t, iframe, urlRelativeToThisDocument) => {
  return new Promise(resolve => {
    iframe.addEventListener("load", t.step_func(() => {
      assert_equals(iframe.contentWindow.location.href, (new URL(urlRelativeToThisDocument, location.href)).href);

      // Wait a bit longer to ensure all history stuff has settled, e.g. the document is "completely loaded"
      // (which happens from a queued task).
      setTimeout(resolve, 0);
    }), { once: true });
  });
};

// -- End of helpers for iframe initial empty document tests.

// -- Start of helpers for opened windows' initial empty document tests.

// window.open() to a URL that doesn't load a new document (results in a HTTP
// 204 response). This should create a new window that stays on the initial
// empty document.
window.windowOpen204 = (t) => {
  const openedWindow = window.open(url204);
  t.add_cleanup(() => openedWindow.close());
  return openedWindow;
};

// window.open() (no URL set). This should create a new window that stays on
// the initial empty document as it won't trigger a non-initial about:blank
// navigation.
window.windowOpenNoURL = (t) => {
  const openedWindow = window.open();
  t.add_cleanup(() => openedWindow.close());
  return openedWindow;
};

// window.open("about:blank"). This should create a new window that stays on
// the initial empty document as it won't trigger a non-initial about:blank
// navigation.
window.windowOpenAboutBlank = (t) => {
  const openedWindow = window.open("about:blank");
  t.add_cleanup(() => openedWindow.close());
  return openedWindow;
};

// Waits for a postMessage with data set to |message| is received on the current
// window.
window.waitForMessage = (t, message) => {
  return new Promise(resolve => {
    window.addEventListener("message", t.step_func((event) => {
      if (event.data == message)
        resolve();
    }));
  });
};

// -- End of helpers for opened windows' initial empty document tests.