summaryrefslogtreecommitdiffstats
path: root/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_history_shift_click.js
blob: 793bcd1a5d2c8fd477562fd41f2d32fbcbc2b735 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

add_task(async function () {
  await testShiftClickOpensNewWindow("back-button");
});

add_task(async function () {
  await testShiftClickOpensNewWindow("forward-button");
});

// Create new private browser, open new tab and set history state, then return the window
async function createPrivateWindow() {
  const privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  await BrowserTestUtils.openNewForegroundTab(
    privateWindow.gBrowser,
    "http://example.com"
  );
  await SpecialPowers.spawn(
    privateWindow.gBrowser.selectedBrowser,
    [],
    async function () {
      content.history.pushState({}, "first item", "first-item.html");
      content.history.pushState({}, "second item", "second-item.html");
      content.history.pushState({}, "third item", "third-item.html");
      content.history.back();
    }
  );
  await TestUtils.topicObserved("sessionstore-state-write-complete");

  // Wait for the session data to be flushed before continuing the test
  await new Promise(resolve =>
    SessionStore.getSessionHistory(privateWindow.gBrowser.selectedTab, resolve)
  );

  info("Private window created");

  return privateWindow;
}

async function testShiftClickOpensNewWindow(buttonId) {
  const privateWindow = await createPrivateWindow();

  const button = privateWindow.document.getElementById(buttonId);
  // Wait for the new private window to be created after click
  const newPrivateWindowPromise = BrowserTestUtils.waitForNewWindow();

  EventUtils.synthesizeMouseAtCenter(button, { shiftKey: true }, privateWindow);

  info("Waiting for new private browser to open");

  const newPrivateWindow = await newPrivateWindowPromise;

  ok(
    PrivateBrowsingUtils.isBrowserPrivate(newPrivateWindow.gBrowser),
    "New window is private"
  );

  // Cleanup
  await Promise.all([
    BrowserTestUtils.closeWindow(privateWindow),
    BrowserTestUtils.closeWindow(newPrivateWindow),
  ]);

  info("Closed all windows");
}