summaryrefslogtreecommitdiffstats
path: root/docshell/test/browser/browser_browsingContext-webProgress.js
blob: 225242be6061d90cc9d1a989f3d5eadf7995e25e (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function () {
  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:blank"
  );
  const browser = tab.linkedBrowser;
  const aboutBlankBrowsingContext = browser.browsingContext;
  const { webProgress } = aboutBlankBrowsingContext;
  ok(
    webProgress,
    "Got a WebProgress interface on BrowsingContext in the parent process"
  );
  is(
    webProgress.browsingContext,
    browser.browsingContext,
    "WebProgress.browsingContext refers to the right BrowsingContext"
  );

  const onLocationChanged = waitForNextLocationChange(webProgress);
  const newLocation = "data:text/html;charset=utf-8,first-page";
  let loaded = BrowserTestUtils.browserLoaded(browser);
  BrowserTestUtils.startLoadingURIString(browser, newLocation);
  await loaded;

  const firstPageBrowsingContext = browser.browsingContext;
  const isBfcacheInParentEnabled =
    SpecialPowers.Services.appinfo.sessionHistoryInParent &&
    SpecialPowers.Services.prefs.getBoolPref("fission.bfcacheInParent");
  if (isBfcacheInParentEnabled) {
    isnot(
      aboutBlankBrowsingContext,
      firstPageBrowsingContext,
      "With bfcache in parent, navigations spawn a new BrowsingContext"
    );
  } else {
    is(
      aboutBlankBrowsingContext,
      firstPageBrowsingContext,
      "Without bfcache in parent, navigations reuse the same BrowsingContext"
    );
  }

  info("Wait for onLocationChange to be fired");
  {
    const { browsingContext, location, request, flags } =
      await onLocationChanged;
    is(
      browsingContext,
      firstPageBrowsingContext,
      "Location change fires on the new BrowsingContext"
    );
    ok(location instanceof Ci.nsIURI);
    is(location.spec, newLocation);
    ok(request instanceof Ci.nsIChannel);
    is(request.URI.spec, newLocation);
    is(flags, 0);
  }

  const onIframeLocationChanged = waitForNextLocationChange(webProgress);
  const iframeLocation = "data:text/html;charset=utf-8,iframe";
  const iframeBC = await SpecialPowers.spawn(
    browser,
    [iframeLocation],
    async url => {
      const iframe = content.document.createElement("iframe");
      await new Promise(resolve => {
        iframe.addEventListener("load", resolve, { once: true });
        iframe.src = url;
        content.document.body.appendChild(iframe);
      });

      return iframe.browsingContext;
    }
  );
  ok(
    iframeBC.webProgress,
    "The iframe BrowsingContext also exposes a WebProgress"
  );
  {
    const { browsingContext, location, request, flags } =
      await onIframeLocationChanged;
    is(
      browsingContext,
      iframeBC,
      "Iframe location change fires on the iframe BrowsingContext"
    );
    ok(location instanceof Ci.nsIURI);
    is(location.spec, iframeLocation);
    ok(request instanceof Ci.nsIChannel);
    is(request.URI.spec, iframeLocation);
    is(flags, 0);
  }

  const onSecondLocationChanged = waitForNextLocationChange(webProgress);
  const onSecondPageDocumentStart = waitForNextDocumentStart(webProgress);
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  const secondLocation = "http://example.com/document-builder.sjs?html=com";
  loaded = BrowserTestUtils.browserLoaded(browser);
  BrowserTestUtils.startLoadingURIString(browser, secondLocation);
  await loaded;

  const secondPageBrowsingContext = browser.browsingContext;
  if (isBfcacheInParentEnabled) {
    isnot(
      firstPageBrowsingContext,
      secondPageBrowsingContext,
      "With bfcache in parent, navigations spawn a new BrowsingContext"
    );
  } else {
    is(
      firstPageBrowsingContext,
      secondPageBrowsingContext,
      "Without bfcache in parent, navigations reuse the same BrowsingContext"
    );
  }
  {
    const { browsingContext, location, request, flags } =
      await onSecondLocationChanged;
    is(
      browsingContext,
      secondPageBrowsingContext,
      "Second location change fires on the new BrowsingContext"
    );
    ok(location instanceof Ci.nsIURI);
    is(location.spec, secondLocation);
    ok(request instanceof Ci.nsIChannel);
    is(request.URI.spec, secondLocation);
    is(flags, 0);
  }
  {
    const { browsingContext, request } = await onSecondPageDocumentStart;
    is(
      browsingContext,
      firstPageBrowsingContext,
      "STATE_START, when navigating to another process, fires on the BrowsingContext we navigate *from*"
    );
    ok(request instanceof Ci.nsIChannel);
    is(request.URI.spec, secondLocation);
  }

  const onBackLocationChanged = waitForNextLocationChange(webProgress, true);
  const onBackDocumentStart = waitForNextDocumentStart(webProgress);
  browser.goBack();

  {
    const { browsingContext, location, request, flags } =
      await onBackLocationChanged;
    is(
      browsingContext,
      firstPageBrowsingContext,
      "location change, when navigating back, fires on the BrowsingContext we navigate *to*"
    );
    ok(location instanceof Ci.nsIURI);
    is(location.spec, newLocation);
    ok(request instanceof Ci.nsIChannel);
    is(request.URI.spec, newLocation);
    is(flags, 0);
  }
  {
    const { browsingContext, request } = await onBackDocumentStart;
    is(
      browsingContext,
      secondPageBrowsingContext,
      "STATE_START, when navigating back, fires on the BrowsingContext we navigate *from*"
    );
    ok(request instanceof Ci.nsIChannel);
    is(request.URI.spec, newLocation);
  }

  BrowserTestUtils.removeTab(tab);
});

function waitForNextLocationChange(webProgress, onlyTopLevel = false) {
  return new Promise(resolve => {
    const wpl = {
      QueryInterface: ChromeUtils.generateQI([
        "nsIWebProgressListener",
        "nsISupportsWeakReference",
      ]),
      onLocationChange(progress, request, location, flags) {
        if (onlyTopLevel && progress.browsingContext.parent) {
          // Ignore non-toplevel.
          return;
        }
        webProgress.removeProgressListener(wpl, Ci.nsIWebProgress.NOTIFY_ALL);
        resolve({
          browsingContext: progress.browsingContext,
          location,
          request,
          flags,
        });
      },
    };
    // Add a strong reference to the progress listener.
    resolve.wpl = wpl;
    webProgress.addProgressListener(wpl, Ci.nsIWebProgress.NOTIFY_ALL);
  });
}

function waitForNextDocumentStart(webProgress) {
  return new Promise(resolve => {
    const wpl = {
      QueryInterface: ChromeUtils.generateQI([
        "nsIWebProgressListener",
        "nsISupportsWeakReference",
      ]),
      onStateChange(progress, request, flags, status) {
        if (
          flags & Ci.nsIWebProgressListener.STATE_IS_DOCUMENT &&
          flags & Ci.nsIWebProgressListener.STATE_START
        ) {
          webProgress.removeProgressListener(wpl, Ci.nsIWebProgress.NOTIFY_ALL);
          resolve({ browsingContext: progress.browsingContext, request });
        }
      },
    };
    // Add a strong reference to the progress listener.
    resolve.wpl = wpl;
    webProgress.addProgressListener(wpl, Ci.nsIWebProgress.NOTIFY_ALL);
  });
}