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

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  NewTabUtils: "resource://gre/modules/NewTabUtils.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(this, {
  AboutNewTab: "resource:///modules/AboutNewTab.jsm",
});

const EN_US_TOPSITES =
  "https://www.youtube.com/,https://www.facebook.com/,https://www.amazon.com/,https://www.reddit.com/,https://www.wikipedia.org/,https://twitter.com/";

async function addTestVisits() {
  // Add some visits to a URL.
  for (let i = 0; i < 5; i++) {
    await PlacesTestUtils.addVisits("http://example.com/");
  }

  // Wait for example.com to be listed first.
  await updateTopSites(sites => {
    return sites && sites[0] && sites[0].url == "http://example.com/";
  });

  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: "https://www.youtube.com/",
    title: "YouTube",
  });
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.urlbar.suggest.topsites", true],
      ["browser.urlbar.suggest.quickactions", false],
      ["browser.newtabpage.activity-stream.default.sites", EN_US_TOPSITES],
    ],
  });

  await updateTopSites(
    sites => sites && sites.length == EN_US_TOPSITES.split(",").length
  );

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://example.com/"
  );

  registerCleanupFunction(() => {
    BrowserTestUtils.removeTab(tab);
  });
});

add_task(async function topSitesPrivateWindow() {
  // Top Sites should also be shown in private windows.
  let privateWin = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  await addTestVisits();
  let sites = AboutNewTab.getTopSites();
  Assert.equal(
    sites.length,
    7,
    "The test suite browser should have 7 Top Sites."
  );
  let urlbar = privateWin.gURLBar;
  await UrlbarTestUtils.promisePopupOpen(privateWin, () => {
    if (urlbar.getAttribute("pageproxystate") == "invalid") {
      urlbar.handleRevert();
    }
    EventUtils.synthesizeMouseAtCenter(urlbar.inputField, {}, privateWin);
  });
  Assert.ok(urlbar.view.isOpen, "UrlbarView should be open.");
  await UrlbarTestUtils.promiseSearchComplete(privateWin);

  Assert.equal(
    UrlbarTestUtils.getResultCount(privateWin),
    7,
    "The number of results should be the same as the number of Top Sites (7)."
  );

  // Top sites should also be shown in a private window if the search string
  // gets cleared.
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window: privateWin,
    value: "example",
  });
  urlbar.select();
  EventUtils.synthesizeKey("KEY_Backspace", {}, privateWin);
  Assert.ok(urlbar.view.isOpen, "UrlbarView should be open.");
  await UrlbarTestUtils.promiseSearchComplete(privateWin);
  Assert.equal(
    UrlbarTestUtils.getResultCount(privateWin),
    7,
    "The number of results should be the same as the number of Top Sites (7)."
  );

  await BrowserTestUtils.closeWindow(privateWin);

  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesUtils.history.clear();
  await UrlbarTestUtils.promisePopupClose(window, () => {
    gURLBar.blur();
  });
});

add_task(async function topSitesTabSwitch() {
  // Add some visits
  for (let i = 0; i < 5; i++) {
    await PlacesTestUtils.addVisits(["http://example.com/"]);
  }

  // Switch to the originating tab, to check for switch to the current tab.
  gBrowser.selectedTab = gBrowser.tabs[0];

  // Wait for the expected number of Top sites.
  await updateTopSites(sites => sites?.length == 7);
  Assert.equal(
    AboutNewTab.getTopSites().length,
    7,
    "The test suite browser should have 7 Top Sites."
  );

  async function checkResults(win, expectedResultType) {
    let resultCount = UrlbarTestUtils.getResultCount(win);
    let result;
    for (let i = 0; i < resultCount; ++i) {
      result = await UrlbarTestUtils.getDetailsOfResultAt(win, i);
      if (result.url == "http://example.com/") {
        break;
      }
    }
    Assert.equal(
      result.type,
      expectedResultType,
      `Should provide a result of type ${expectedResultType}.`
    );
  }

  info("Test in a non-private window");
  await UrlbarTestUtils.promisePopupOpen(window, () => {
    EventUtils.synthesizeMouseAtCenter(gURLBar.inputField, {});
  });
  Assert.ok(gURLBar.view.isOpen, "UrlbarView should be open.");
  await UrlbarTestUtils.promiseSearchComplete(window);
  await checkResults(window, UrlbarUtils.RESULT_TYPE.TAB_SWITCH);
  await UrlbarTestUtils.promisePopupClose(window);

  info("Test in a private window, switch to tab should not be offered");
  // Top Sites should also be shown in private windows.
  let privateWin = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });

  let urlbar = privateWin.gURLBar;
  await UrlbarTestUtils.promisePopupOpen(privateWin, () => {
    if (urlbar.getAttribute("pageproxystate") == "invalid") {
      urlbar.handleRevert();
    }
    EventUtils.synthesizeMouseAtCenter(urlbar.inputField, {}, privateWin);
  });

  Assert.ok(urlbar.view.isOpen, "UrlbarView should be open.");
  await UrlbarTestUtils.promiseSearchComplete(privateWin);
  await checkResults(privateWin, UrlbarUtils.RESULT_TYPE.URL);
  await UrlbarTestUtils.promisePopupClose(privateWin);
  await BrowserTestUtils.closeWindow(privateWin);

  await PlacesUtils.history.clear();
});