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

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

async function addBookmark(bookmark) {
  info("Creating bookmark and keyword");
  let bm = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: bookmark.url,
    title: bookmark.title,
  });
  if (bookmark.keyword) {
    await PlacesUtils.keywords.insert({
      keyword: bookmark.keyword,
      url: bookmark.url,
    });
  }

  registerCleanupFunction(async function () {
    if (bookmark.keyword) {
      await PlacesUtils.keywords.remove(bookmark.keyword);
    }
    await PlacesUtils.bookmarks.remove(bm);
  });
}

/**
 * Check that if the user hits enter and ctrl-t at the same time, we open the
 * URL in the right tab.
 */
add_task(async function hitEnterLoadInRightTab() {
  await addBookmark({
    title: "Test for keyword bookmark and URL",
    url: TEST_URL,
    keyword: "urlbarkeyword",
  });

  info("Opening a tab");
  let oldTabOpenPromise = BrowserTestUtils.waitForEvent(
    gBrowser.tabContainer,
    "TabOpen"
  );
  BrowserOpenTab();
  let oldTab = (await oldTabOpenPromise).target;
  let oldTabLoadedPromise = BrowserTestUtils.browserLoaded(
    oldTab.linkedBrowser,
    false,
    TEST_URL
  ).then(() => info("Old tab loaded"));

  info("Filling URL bar, sending <return> and opening a tab");
  let tabOpenPromise = BrowserTestUtils.waitForEvent(
    gBrowser.tabContainer,
    "TabOpen"
  );
  gURLBar.value = "urlbarkeyword";
  gURLBar.focus();
  gURLBar.select();
  EventUtils.sendKey("return");

  info("Immediately open a second tab");
  BrowserOpenTab();
  let newTab = (await tabOpenPromise).target;

  info("Created new tab; waiting for tabs to load");
  let newTabLoadedPromise = BrowserTestUtils.browserLoaded(
    newTab.linkedBrowser,
    false,
    "about:newtab"
  ).then(() => info("New tab loaded"));
  // If one of the tabs loads the wrong page, this will timeout, and that
  // indicates we regressed this bug fix.
  await Promise.all([newTabLoadedPromise, oldTabLoadedPromise]);
  // These are not particularly useful, but the test must contain some checks.
  is(
    newTab.linkedBrowser.currentURI.spec,
    "about:newtab",
    "New tab loaded about:newtab"
  );
  is(oldTab.linkedBrowser.currentURI.spec, TEST_URL, "Old tab loaded URL");

  info("Closing tabs");
  BrowserTestUtils.removeTab(newTab);
  BrowserTestUtils.removeTab(oldTab);
});