summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_tabMatchesInAwesomebar_perwindowpb.js
blob: b7b13eecf85d45a99cc9a42f686e298ccc58eb60 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * This test ensures that we don't switch between tabs from normal window to
 * private browsing window or opposite.
 */

const TEST_URL = `${TEST_BASE_URL}dummy_page.html`;

add_task(async function () {
  let normalWindow = await BrowserTestUtils.openNewBrowserWindow();
  let privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  await runTest(normalWindow, privateWindow, false);
  await BrowserTestUtils.closeWindow(normalWindow);
  await BrowserTestUtils.closeWindow(privateWindow);

  normalWindow = await BrowserTestUtils.openNewBrowserWindow();
  privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  await runTest(privateWindow, normalWindow, false);
  await BrowserTestUtils.closeWindow(normalWindow);
  await BrowserTestUtils.closeWindow(privateWindow);

  privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  await runTest(privateWindow, privateWindow, true);
  await BrowserTestUtils.closeWindow(privateWindow);

  normalWindow = await BrowserTestUtils.openNewBrowserWindow();
  await runTest(normalWindow, normalWindow, true);
  await BrowserTestUtils.closeWindow(normalWindow);
});

async function runTest(aSourceWindow, aDestWindow, aExpectSwitch, aCallback) {
  await BrowserTestUtils.openNewForegroundTab(aSourceWindow.gBrowser, TEST_URL);
  let testTab = await BrowserTestUtils.openNewForegroundTab(
    aDestWindow.gBrowser
  );

  info("waiting for focus on the window");
  await SimpleTest.promiseFocus(aDestWindow);
  info("got focus on the window");

  // Select the testTab
  aDestWindow.gBrowser.selectedTab = testTab;

  // Ensure that this tab has no history entries
  let sessionHistoryCount = await new Promise(resolve => {
    SessionStore.getSessionHistory(
      gBrowser.selectedTab,
      function (sessionHistory) {
        resolve(sessionHistory.entries.length);
      }
    );
  });

  ok(
    sessionHistoryCount < 2,
    `The test tab has 1 or fewer history entries. sessionHistoryCount=${sessionHistoryCount}`
  );
  // Ensure that this tab is on about:blank
  is(
    testTab.linkedBrowser.currentURI.spec,
    "about:blank",
    "The test tab is on about:blank"
  );
  // Ensure that this tab's document has no child nodes
  await SpecialPowers.spawn(testTab.linkedBrowser, [], async function () {
    ok(
      !content.document.body.hasChildNodes(),
      "The test tab has no child nodes"
    );
  });
  ok(
    !testTab.hasAttribute("busy"),
    "The test tab doesn't have the busy attribute"
  );

  // Wait for the Awesomebar popup to appear.
  let searchString = TEST_URL;
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window: aDestWindow,
    value: searchString,
  });

  info(`awesomebar popup appeared. aExpectSwitch: ${aExpectSwitch}`);
  // Make sure the last match is selected.
  while (
    UrlbarTestUtils.getSelectedRowIndex(aDestWindow) <
    UrlbarTestUtils.getResultCount(aDestWindow) - 1
  ) {
    info("handling key navigation for DOM_VK_DOWN key");
    EventUtils.synthesizeKey("KEY_ArrowDown", {}, aDestWindow);
  }

  let awaitTabSwitch;
  if (aExpectSwitch) {
    awaitTabSwitch = BrowserTestUtils.waitForTabClosing(testTab);
  }

  // Execute the selected action.
  EventUtils.synthesizeKey("KEY_Enter", {}, aDestWindow);
  info("sent Enter command to the controller");

  if (aExpectSwitch) {
    // If we expect a tab switch then the current tab
    // will be closed and we switch to the other tab.
    await awaitTabSwitch;
  } else {
    // If we don't expect a tab switch then wait for the tab to load.
    await BrowserTestUtils.browserLoaded(testTab.linkedBrowser);
  }
}