summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_relatedTabs_reset.js
blob: 531a9e723dec7ee45e52c9f143a226fb7871c393 (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
/* 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/. */

add_task(async function () {
  is(gBrowser.tabs.length, 1, "one tab is open initially");

  const TestPage =
    "http://mochi.test:8888/browser/browser/base/content/test/general/dummy_page.html";

  // Add several new tabs in sequence
  let tabs = [];
  let ReferrerInfo = Components.Constructor(
    "@mozilla.org/referrer-info;1",
    "nsIReferrerInfo",
    "init"
  );

  function getPrincipal(url, attrs) {
    let uri = Services.io.newURI(url);
    if (!attrs) {
      attrs = {};
    }
    return Services.scriptSecurityManager.createContentPrincipal(uri, attrs);
  }

  function addTab(aURL, aReferrer) {
    let referrerInfo = new ReferrerInfo(
      Ci.nsIReferrerInfo.EMPTY,
      true,
      aReferrer
    );
    let triggeringPrincipal = getPrincipal(aURL);
    let tab = BrowserTestUtils.addTab(gBrowser, aURL, {
      referrerInfo,
      triggeringPrincipal,
    });
    tabs.push(tab);
    return BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  }

  function loadTab(tab, url) {
    let loaded = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    info("Loading page: " + url);
    BrowserTestUtils.loadURIString(tab.linkedBrowser, url);
    return loaded;
  }

  function testPosition(tabNum, expectedPosition, msg) {
    is(
      Array.prototype.indexOf.call(gBrowser.tabs, tabs[tabNum]),
      expectedPosition,
      msg
    );
  }

  // Initial selected tab
  await addTab("http://mochi.test:8888/#0");
  testPosition(0, 1, "Initial tab opened in position 1");
  gBrowser.selectedTab = tabs[0];

  // Related tabs
  await addTab("http://mochi.test:8888/#1", gBrowser.currentURI);
  testPosition(1, 2, "Related tab was opened to the far right");

  await addTab("http://mochi.test:8888/#2", gBrowser.currentURI);
  testPosition(2, 3, "Related tab was opened to the far right");

  // Load a new page
  await loadTab(tabs[0], TestPage);

  // Add a new related tab after the page load
  await addTab("http://mochi.test:8888/#3", gBrowser.currentURI);
  testPosition(
    3,
    2,
    "Tab opened to the right of initial tab after system navigation"
  );

  tabs.forEach(gBrowser.removeTab, gBrowser);
});