Bug 1888590 - Mark some subtests on trusted-types-event-handlers.html as failing...
[gecko.git] / docshell / test / browser / head.js
blob38f2528a2f9d5fafef5dca90d1b2e40f29225e8f
1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
4 /**
5  * Helper function for encoding override tests, loads URL, runs check1,
6  * forces encoding detection, runs check2.
7  */
8 function runCharsetTest(url, check1, check2) {
9   waitForExplicitFinish();
11   BrowserTestUtils.openNewForegroundTab(gBrowser, url, true).then(afterOpen);
13   function afterOpen() {
14     BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser).then(
15       afterChangeCharset
16     );
18     SpecialPowers.spawn(gBrowser.selectedBrowser, [], check1).then(() => {
19       BrowserForceEncodingDetection();
20     });
21   }
23   function afterChangeCharset() {
24     SpecialPowers.spawn(gBrowser.selectedBrowser, [], check2).then(() => {
25       gBrowser.removeCurrentTab();
26       finish();
27     });
28   }
31 /**
32  * Helper function for charset tests. It loads |url| in a new tab,
33  * runs |check|.
34  */
35 function runCharsetCheck(url, check) {
36   waitForExplicitFinish();
38   BrowserTestUtils.openNewForegroundTab(gBrowser, url, true).then(afterOpen);
40   function afterOpen() {
41     SpecialPowers.spawn(gBrowser.selectedBrowser, [], check).then(() => {
42       gBrowser.removeCurrentTab();
43       finish();
44     });
45   }
48 async function pushState(url, frameId) {
49   info(
50     `Doing a pushState, expecting to load ${url} ${
51       frameId ? "in an iframe" : ""
52     }`
53   );
54   let browser = gBrowser.selectedBrowser;
55   let bc = browser.browsingContext;
56   if (frameId) {
57     bc = await SpecialPowers.spawn(bc, [frameId], function (id) {
58       return content.document.getElementById(id).browsingContext;
59     });
60   }
61   let loaded = BrowserTestUtils.waitForLocationChange(gBrowser, url);
62   await SpecialPowers.spawn(bc, [url], function (url) {
63     content.history.pushState({}, "", url);
64   });
65   await loaded;
66   info(`Loaded ${url} ${frameId ? "in an iframe" : ""}`);
69 async function loadURI(url) {
70   info(`Doing a top-level loadURI, expecting to load ${url}`);
71   let browser = gBrowser.selectedBrowser;
72   let loaded = BrowserTestUtils.browserLoaded(browser, false, url);
73   BrowserTestUtils.startLoadingURIString(browser, url);
74   await loaded;
75   info(`Loaded ${url}`);
78 async function followLink(url, frameId) {
79   info(
80     `Creating and following a link to ${url} ${frameId ? "in an iframe" : ""}`
81   );
82   let browser = gBrowser.selectedBrowser;
83   let bc = browser.browsingContext;
84   if (frameId) {
85     bc = await SpecialPowers.spawn(bc, [frameId], function (id) {
86       return content.document.getElementById(id).browsingContext;
87     });
88   }
89   let loaded = BrowserTestUtils.browserLoaded(browser, !!frameId, url);
90   await SpecialPowers.spawn(bc, [url], function (url) {
91     let a = content.document.createElement("a");
92     a.href = url;
93     content.document.body.appendChild(a);
94     a.click();
95   });
96   await loaded;
97   info(`Loaded ${url} ${frameId ? "in an iframe" : ""}`);
100 async function goForward(url, useFrame = false) {
101   info(
102     `Clicking the forward button, expecting to load ${url} ${
103       useFrame ? "in an iframe" : ""
104     }`
105   );
106   let loaded = BrowserTestUtils.waitForLocationChange(gBrowser, url);
107   let forwardButton = document.getElementById("forward-button");
108   forwardButton.click();
109   await loaded;
110   info(`Loaded ${url} ${useFrame ? "in an iframe" : ""}`);
113 async function goBack(url, useFrame = false) {
114   info(
115     `Clicking the back button, expecting to load ${url} ${
116       useFrame ? "in an iframe" : ""
117     }`
118   );
119   let loaded = BrowserTestUtils.waitForLocationChange(gBrowser, url);
120   let backButton = document.getElementById("back-button");
121   backButton.click();
122   await loaded;
123   info(`Loaded ${url} ${useFrame ? "in an iframe" : ""}`);
126 function assertBackForwardState(canGoBack, canGoForward) {
127   let backButton = document.getElementById("back-button");
128   let forwardButton = document.getElementById("forward-button");
130   is(
131     backButton.disabled,
132     !canGoBack,
133     `${gBrowser.currentURI.spec}: back button is ${
134       canGoBack ? "not" : ""
135     } disabled`
136   );
137   is(
138     forwardButton.disabled,
139     !canGoForward,
140     `${gBrowser.currentURI.spec}: forward button is ${
141       canGoForward ? "not" : ""
142     } disabled`
143   );
146 class SHListener {
147   static NewEntry = 0;
148   static Reload = 1;
149   static GotoIndex = 2;
150   static Purge = 3;
151   static ReplaceEntry = 4;
152   static async waitForHistory(history, event) {
153     return new Promise(resolve => {
154       let listener = {
155         OnHistoryNewEntry: () => {},
156         OnHistoryReload: () => {
157           return true;
158         },
159         OnHistoryGotoIndex: () => {},
160         OnHistoryPurge: () => {},
161         OnHistoryReplaceEntry: () => {},
163         QueryInterface: ChromeUtils.generateQI([
164           "nsISHistoryListener",
165           "nsISupportsWeakReference",
166         ]),
167       };
169       function finish() {
170         history.removeSHistoryListener(listener);
171         resolve();
172       }
173       switch (event) {
174         case this.NewEntry:
175           listener.OnHistoryNewEntry = finish;
176           break;
177         case this.Reload:
178           listener.OnHistoryReload = () => {
179             finish();
180             return true;
181           };
182           break;
183         case this.GotoIndex:
184           listener.OnHistoryGotoIndex = finish;
185           break;
186         case this.Purge:
187           listener.OnHistoryPurge = finish;
188           break;
189         case this.ReplaceEntry:
190           listener.OnHistoryReplaceEntry = finish;
191           break;
192       }
194       history.addSHistoryListener(listener);
195     });
196   }