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

"use strict";

var gOldCount = NewTabPagePreloading.MAX_COUNT;
registerCleanupFunction(() => {
  NewTabPagePreloading.MAX_COUNT = gOldCount;
});

async function openWinWithPreloadBrowser(options = {}) {
  let idleFinishedPromise = TestUtils.topicObserved(
    "browser-idle-startup-tasks-finished",
    w => {
      return w != window;
    }
  );
  let newWin = await BrowserTestUtils.openNewBrowserWindow(options);
  await idleFinishedPromise;
  await TestUtils.waitForCondition(() => newWin.gBrowser.preloadedBrowser);
  return newWin;
}

async function promiseNewTabLoadedInBrowser(browser) {
  let url = browser.ownerGlobal.BROWSER_NEW_TAB_URL;
  if (browser.currentURI.spec != url) {
    info(`Waiting for ${url} to be the location for the browser.`);
    await new Promise(resolve => {
      let progressListener = {
        onLocationChange(aWebProgress, aRequest, aLocationURI, aFlags) {
          if (!url || aLocationURI.spec == url) {
            browser.removeProgressListener(progressListener);
            resolve();
          }
        },
        QueryInterface: ChromeUtils.generateQI([
          Ci.nsISupportsWeakReference,
          Ci.nsIWebProgressListener2,
          Ci.nsIWebProgressListener,
        ]),
      };
      browser.addProgressListener(
        progressListener,
        Ci.nsIWebProgress.NOTIFY_ALL
      );
    });
  } else {
    info(`${url} already the current URI for the browser.`);
  }

  info(`Waiting for readyState complete in the browser`);
  await SpecialPowers.spawn(browser, [], function () {
    return ContentTaskUtils.waitForCondition(() => {
      return content.document.readyState == "complete";
    });
  });
}

/**
 * Verify that moving a preloaded browser's content from one window to the next
 * works correctly.
 */
add_task(async function moving_works() {
  NewTabPagePreloading.MAX_COUNT = 1;

  NewTabPagePreloading.removePreloadedBrowser(window);

  NewTabPagePreloading.maybeCreatePreloadedBrowser(window);
  isnot(gBrowser.preloadedBrowser, null, "Should have preloaded browser");

  let oldKey = gBrowser.preloadedBrowser.permanentKey;

  let newWin = await openWinWithPreloadBrowser();
  is(gBrowser.preloadedBrowser, null, "Preloaded browser should be gone");
  isnot(
    newWin.gBrowser.preloadedBrowser,
    null,
    "Should have moved the preload browser"
  );
  is(
    newWin.gBrowser.preloadedBrowser.permanentKey,
    oldKey,
    "Should have the same permanent key"
  );
  let browser = newWin.gBrowser.preloadedBrowser;
  let tab = BrowserTestUtils.addTab(
    newWin.gBrowser,
    newWin.BROWSER_NEW_TAB_URL
  );
  is(
    tab.linkedBrowser,
    browser,
    "Preloaded browser is usable when opening a new tab."
  );
  await promiseNewTabLoadedInBrowser(browser);
  ok(true, "Successfully loaded the tab.");

  tab = browser = null;
  await BrowserTestUtils.closeWindow(newWin);

  tab = BrowserTestUtils.addTab(gBrowser, BROWSER_NEW_TAB_URL);
  await promiseNewTabLoadedInBrowser(tab.linkedBrowser);

  ok(true, "Managed to open a tab in the original window still.");

  BrowserTestUtils.removeTab(tab);
});

add_task(async function moving_shouldnt_move_across_private_state() {
  NewTabPagePreloading.MAX_COUNT = 1;

  NewTabPagePreloading.removePreloadedBrowser(window);

  NewTabPagePreloading.maybeCreatePreloadedBrowser(window);
  isnot(gBrowser.preloadedBrowser, null, "Should have preloaded browser");

  let oldKey = gBrowser.preloadedBrowser.permanentKey;
  let newWin = await openWinWithPreloadBrowser({ private: true });

  isnot(
    gBrowser.preloadedBrowser,
    null,
    "Preloaded browser in original window should persist"
  );
  isnot(
    newWin.gBrowser.preloadedBrowser,
    null,
    "Should have created another preload browser"
  );
  isnot(
    newWin.gBrowser.preloadedBrowser.permanentKey,
    oldKey,
    "Should not have the same permanent key"
  );
  let browser = newWin.gBrowser.preloadedBrowser;
  let tab = BrowserTestUtils.addTab(
    newWin.gBrowser,
    newWin.BROWSER_NEW_TAB_URL
  );
  is(
    tab.linkedBrowser,
    browser,
    "Preloaded browser is usable when opening a new tab."
  );
  await promiseNewTabLoadedInBrowser(browser);
  ok(true, "Successfully loaded the tab.");

  tab = browser = null;
  await BrowserTestUtils.closeWindow(newWin);
});