summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/urlbar/browser_tabMatchesInAwesomebar.js
blob: d207092d45e910d7b13d89dd44ed160565bb683d (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
 * vim:set ts=2 sw=2 sts=2 et:
 * 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/. */

requestLongerTimeout(2);

const TEST_URL_BASES = [
  "http://example.org/browser/browser/base/content/test/urlbar/dummy_page.html#tabmatch",
  "http://example.org/browser/browser/base/content/test/urlbar/moz.png#tabmatch"
];

var gController = Cc["@mozilla.org/autocomplete/controller;1"].
                  getService(Ci.nsIAutoCompleteController);

var gTabCounter = 0;

add_task(function* step_1() {
  info("Running step 1");
  let maxResults = Services.prefs.getIntPref("browser.urlbar.maxRichResults");
  let promises = [];
  for (let i = 0; i < maxResults - 1; i++) {
    let tab = gBrowser.addTab();
    promises.push(loadTab(tab, TEST_URL_BASES[0] + (++gTabCounter)));
  }

  yield Promise.all(promises);
  yield ensure_opentabs_match_db();
});

add_task(function* step_2() {
  info("Running step 2");
  gBrowser.selectTabAtIndex(1);
  gBrowser.removeCurrentTab();
  gBrowser.selectTabAtIndex(1);
  gBrowser.removeCurrentTab();

  let promises = [];
  for (let i = 1; i < gBrowser.tabs.length; i++)
    promises.push(loadTab(gBrowser.tabs[i], TEST_URL_BASES[1] + (++gTabCounter)));

  yield Promise.all(promises);
  yield ensure_opentabs_match_db();
});

add_task(function* step_3() {
  info("Running step 3");
  let promises = [];
  for (let i = 1; i < gBrowser.tabs.length; i++)
    promises.push(loadTab(gBrowser.tabs[i], TEST_URL_BASES[0] + gTabCounter));

  yield Promise.all(promises);
  yield ensure_opentabs_match_db();
});

add_task(function* step_4() {
  info("Running step 4 - ensure we don't register subframes as open pages");
  let tab = gBrowser.addTab();
  tab.linkedBrowser.loadURI('data:text/html,<body><iframe src=""></iframe></body>');
  yield BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  yield ContentTask.spawn(tab.linkedBrowser, null, function* () {
    let iframe_loaded = ContentTaskUtils.waitForEvent(content.document, "load", true);
    content.document.querySelector("iframe").src = "http://test2.example.org/";
    yield iframe_loaded;
  });

  yield ensure_opentabs_match_db();
});

add_task(function* step_5() {
  info("Running step 5 - remove tab immediately");
  let tab = gBrowser.addTab("about:logo");
  yield BrowserTestUtils.removeTab(tab);
  yield ensure_opentabs_match_db();
});

add_task(function* step_6() {
  info("Running step 6 - check swapBrowsersAndCloseOther preserves registered switch-to-tab result");
  let tabToKeep = gBrowser.addTab();
  let tab = gBrowser.addTab();
  tab.linkedBrowser.loadURI("about:mozilla");
  yield BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  gBrowser.updateBrowserRemoteness(tabToKeep.linkedBrowser, tab.linkedBrowser.isRemoteBrowser);
  gBrowser.swapBrowsersAndCloseOther(tabToKeep, tab);

  yield ensure_opentabs_match_db()

  yield BrowserTestUtils.removeTab(tabToKeep);

  yield ensure_opentabs_match_db();
});

add_task(function* step_7() {
  info("Running step 7 - close all tabs");

  Services.prefs.clearUserPref("browser.sessionstore.restore_on_demand");

  gBrowser.addTab("about:blank", {skipAnimation: true});
  while (gBrowser.tabs.length > 1) {
    info("Removing tab: " + gBrowser.tabs[0].linkedBrowser.currentURI.spec);
    gBrowser.selectTabAtIndex(0);
    gBrowser.removeCurrentTab();
  }

  yield ensure_opentabs_match_db();
});

add_task(function* cleanup() {
  info("Cleaning up");

  yield PlacesTestUtils.clearHistory();
});

function loadTab(tab, url) {
  // Because adding visits is async, we will not be notified immediately.
  let loaded = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  let visited = new Promise(resolve => {
    Services.obs.addObserver(
      function observer(aSubject, aTopic, aData) {
        if (url != aSubject.QueryInterface(Ci.nsIURI).spec)
          return;
        Services.obs.removeObserver(observer, aTopic);
        resolve();
      },
      "uri-visit-saved",
      false
    );
  });

  info("Loading page: " + url);
  tab.linkedBrowser.loadURI(url);
  return Promise.all([ loaded, visited ]);
}

function ensure_opentabs_match_db() {
  var tabs = {};

  var winEnum = Services.wm.getEnumerator("navigator:browser");
  while (winEnum.hasMoreElements()) {
    let browserWin = winEnum.getNext();
    // skip closed-but-not-destroyed windows
    if (browserWin.closed)
      continue;

    for (let i = 0; i < browserWin.gBrowser.tabContainer.childElementCount; i++) {
      let browser = browserWin.gBrowser.getBrowserAtIndex(i);
      let url = browser.currentURI.spec;
      if (browserWin.isBlankPageURL(url))
        continue;
      if (!(url in tabs))
        tabs[url] = 1;
      else
        tabs[url]++;
    }
  }

  return new Promise(resolve => {
    checkAutocompleteResults(tabs, resolve);
  });
}

function checkAutocompleteResults(aExpected, aCallback)
{
  gController.input = {
    timeout: 10,
    textValue: "",
    searches: ["unifiedcomplete"],
    searchParam: "enable-actions",
    popupOpen: false,
    minResultsForPopup: 0,
    invalidate: function() {},
    disableAutoComplete: false,
    completeDefaultIndex: false,
    get popup() { return this; },
    onSearchBegin: function() {},
    onSearchComplete:  function ()
    {
      info("Found " + gController.matchCount + " matches.");
      // Check to see the expected uris and titles match up (in any order)
      for (let i = 0; i < gController.matchCount; i++) {
        if (gController.getStyleAt(i).includes("heuristic")) {
          info("Skip heuristic match");
          continue;
        }
        let action = gURLBar.popup.input._parseActionUrl(gController.getValueAt(i));
        let uri = action.params.url;

        info("Search for '" + uri + "' in open tabs.");
        let expected = uri in aExpected;
        ok(expected, uri + " was found in autocomplete, was " + (expected ? "" : "not ") + "expected");
        // Remove the found entry from expected results.
        delete aExpected[uri];
      }

      // Make sure there is no reported open page that is not open.
      for (let entry in aExpected) {
        ok(false, "'" + entry + "' should be found in autocomplete");
      }

      executeSoon(aCallback);
    },
    setSelectedIndex: function() {},
    get searchCount() { return this.searches.length; },
    getSearchAt: function(aIndex) { return this.searches[aIndex]; },
    QueryInterface: XPCOMUtils.generateQI([
      Ci.nsIAutoCompleteInput,
      Ci.nsIAutoCompletePopup,
    ])
  };

  info("Searching open pages.");
  gController.startSearch(Services.prefs.getCharPref("browser.urlbar.restrict.openpage"));
}