summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_switchToTab_closed_tab.js
blob: 85b428db61be753a4fce76d53cffa963da37ac1f (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
/* 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/. */

/**
 * This tests that the "switch to tab" result in the urlbar
 * will still load the relevant URL if the tab being referred
 * to does not exist.
 */

"use strict";

const { UrlbarProviderOpenTabs } = ChromeUtils.importESModule(
  "resource:///modules/UrlbarProviderOpenTabs.sys.mjs"
);

async function openPagesCount() {
  let conn = await PlacesUtils.promiseLargeCacheDBConnection();
  let res = await conn.executeCached(
    "SELECT COUNT(*) AS count FROM moz_openpages_temp;"
  );
  return res[0].getResultByName("count");
}

add_task(async function test_switchToTab_tab_closed() {
  let testURL =
    "https://example.org/browser/browser/components/urlbar/tests/browser/dummy_page.html";

  // Open a blank tab to start the test from.
  let testTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "https://example.org"
  );

  // Check how many currently open pages are registered
  let pagesCount = await openPagesCount();

  // Register an open tab that does not exist, this simulates a tab being
  // opened but not properly unregistered.
  await UrlbarProviderOpenTabs.registerOpenTab(
    testURL,
    gBrowser.contentPrincipal.userContextId,
    false
  );

  Assert.equal(
    await openPagesCount(),
    pagesCount + 1,
    "We registered a new open page"
  );

  let tabOpenPromise = BrowserTestUtils.waitForEvent(
    gBrowser.tabContainer,
    "TabOpen",
    false
  );

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: testURL,
  });

  // The first result is the heuristic, the second will be the switch to tab.
  let element = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 1);
  EventUtils.synthesizeMouseAtCenter(element, {}, window);

  await tabOpenPromise;
  await BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    false,
    testURL
  );

  Assert.equal(
    gBrowser.selectedBrowser.currentURI.spec,
    testURL,
    "We opened a new tab with the URL"
  );

  gBrowser.removeTab(gBrowser.selectedTab);

  Assert.equal(
    await openPagesCount(),
    pagesCount,
    "We unregistered the orphaned open tab"
  );

  gBrowser.removeTab(testTab);
  await PlacesUtils.history.clear();
});