tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_bug343515.js (8649B)


      1 // Test for bug 343515 - Need API for tabbrowsers to tell docshells they're visible/hidden
      2 
      3 // Globals
      4 var testPath = "http://mochi.test:8888/browser/docshell/test/navigation/";
      5 var ctx = {};
      6 
      7 add_setup(async function () {
      8  await SpecialPowers.pushPrefEnv({
      9    set: [["test.wait300msAfterTabSwitch", true]],
     10  });
     11 });
     12 
     13 add_task(async function () {
     14  // Step 1.
     15 
     16  // Get a handle on the initial tab
     17  ctx.tab0 = gBrowser.selectedTab;
     18  ctx.tab0Browser = gBrowser.getBrowserForTab(ctx.tab0);
     19 
     20  await BrowserTestUtils.waitForCondition(
     21    () => ctx.tab0Browser.docShellIsActive,
     22    "Timed out waiting for initial tab to be active."
     23  );
     24 
     25  // Open a New Tab
     26  ctx.tab1 = BrowserTestUtils.addTab(gBrowser, testPath + "bug343515_pg1.html");
     27  ctx.tab1Browser = gBrowser.getBrowserForTab(ctx.tab1);
     28  await BrowserTestUtils.browserLoaded(ctx.tab1Browser);
     29 
     30  // Step 2.
     31  is(
     32    testPath + "bug343515_pg1.html",
     33    ctx.tab1Browser.currentURI.spec,
     34    "Got expected tab 1 url in step 2"
     35  );
     36 
     37  // Our current tab should still be active
     38  ok(ctx.tab0Browser.docShellIsActive, "Tab 0 should still be active");
     39  ok(!ctx.tab1Browser.docShellIsActive, "Tab 1 should not be active");
     40 
     41  // Switch to tab 1
     42  await BrowserTestUtils.switchTab(gBrowser, ctx.tab1);
     43 
     44  // Tab 1 should now be active
     45  ok(!ctx.tab0Browser.docShellIsActive, "Tab 0 should be inactive");
     46  ok(ctx.tab1Browser.docShellIsActive, "Tab 1 should be active");
     47 
     48  // Open another tab
     49  ctx.tab2 = BrowserTestUtils.addTab(gBrowser, testPath + "bug343515_pg2.html");
     50  ctx.tab2Browser = gBrowser.getBrowserForTab(ctx.tab2);
     51 
     52  await BrowserTestUtils.browserLoaded(ctx.tab2Browser);
     53 
     54  // Step 3.
     55  is(
     56    testPath + "bug343515_pg2.html",
     57    ctx.tab2Browser.currentURI.spec,
     58    "Got expected tab 2 url in step 3"
     59  );
     60 
     61  // Tab 0 should be inactive, Tab 1 should be active
     62  ok(!ctx.tab0Browser.docShellIsActive, "Tab 0 should be inactive");
     63  ok(ctx.tab1Browser.docShellIsActive, "Tab 1 should be active");
     64 
     65  // Tab 2's window _and_ its iframes should be inactive
     66  ok(!ctx.tab2Browser.docShellIsActive, "Tab 2 should be inactive");
     67 
     68  await SpecialPowers.spawn(ctx.tab2Browser, [], async function () {
     69    Assert.equal(content.frames.length, 2, "Tab 2 should have 2 iframes");
     70    for (var i = 0; i < content.frames.length; i++) {
     71      info("step 3, frame " + i + " info: " + content.frames[i].location);
     72      let bc = content.frames[i].browsingContext;
     73      Assert.ok(!bc.isActive, `Tab2 iframe ${i} should be inactive`);
     74    }
     75  });
     76 
     77  // Navigate tab 2 to a different page
     78  BrowserTestUtils.startLoadingURIString(
     79    ctx.tab2Browser,
     80    testPath + "bug343515_pg3.html"
     81  );
     82 
     83  await BrowserTestUtils.browserLoaded(ctx.tab2Browser);
     84 
     85  // Step 4.
     86 
     87  async function checkTab2Active(outerExpected) {
     88    await SpecialPowers.spawn(
     89      ctx.tab2Browser,
     90      [outerExpected],
     91      async function (expected) {
     92        function isActive(aWindow) {
     93          var docshell = aWindow.docShell;
     94          info(`checking ${docshell.browsingContext.id}`);
     95          return docshell.browsingContext.isActive;
     96        }
     97 
     98        let active = expected ? "active" : "inactive";
     99        Assert.equal(content.frames.length, 2, "Tab 2 should have 2 iframes");
    100        for (var i = 0; i < content.frames.length; i++) {
    101          info("step 4, frame " + i + " info: " + content.frames[i].location);
    102        }
    103        Assert.equal(
    104          content.frames[0].frames.length,
    105          1,
    106          "Tab 2 iframe 0 should have 1 iframes"
    107        );
    108        Assert.equal(
    109          isActive(content.frames[0]),
    110          expected,
    111          `Tab2 iframe 0 should be ${active}`
    112        );
    113        Assert.equal(
    114          isActive(content.frames[0].frames[0]),
    115          expected,
    116          `Tab2 iframe 0 subiframe 0 should be ${active}`
    117        );
    118        Assert.equal(
    119          isActive(content.frames[1]),
    120          expected,
    121          `Tab2 iframe 1 should be ${active}`
    122        );
    123      }
    124    );
    125  }
    126 
    127  is(
    128    testPath + "bug343515_pg3.html",
    129    ctx.tab2Browser.currentURI.spec,
    130    "Got expected tab 2 url in step 4"
    131  );
    132 
    133  // Tab 0 should be inactive, Tab 1 should be active
    134  ok(!ctx.tab0Browser.docShellIsActive, "Tab 0 should be inactive");
    135  ok(ctx.tab1Browser.docShellIsActive, "Tab 1 should be active");
    136 
    137  // Tab2 and all descendants should be inactive
    138  await checkTab2Active(false);
    139 
    140  // Switch to Tab 2
    141  await BrowserTestUtils.switchTab(gBrowser, ctx.tab2);
    142 
    143  // Check everything
    144  ok(!ctx.tab0Browser.docShellIsActive, "Tab 0 should be inactive");
    145  ok(!ctx.tab1Browser.docShellIsActive, "Tab 1 should be inactive");
    146  ok(ctx.tab2Browser.docShellIsActive, "Tab 2 should be active");
    147 
    148  await checkTab2Active(true);
    149 
    150  // Go back
    151  let backDone = BrowserTestUtils.waitForLocationChange(
    152    gBrowser,
    153    testPath + "bug343515_pg2.html"
    154  );
    155  ctx.tab2Browser.goBack();
    156  await backDone;
    157 
    158  // Step 5.
    159 
    160  // Check everything
    161  ok(!ctx.tab0Browser.docShellIsActive, "Tab 0 should be inactive");
    162  ok(!ctx.tab1Browser.docShellIsActive, "Tab 1 should be inactive");
    163  ok(ctx.tab2Browser.docShellIsActive, "Tab 2 should be active");
    164  is(
    165    testPath + "bug343515_pg2.html",
    166    ctx.tab2Browser.currentURI.spec,
    167    "Got expected tab 2 url in step 5"
    168  );
    169 
    170  await SpecialPowers.spawn(ctx.tab2Browser, [], async function () {
    171    for (var i = 0; i < content.frames.length; i++) {
    172      let bc = content.frames[i].browsingContext;
    173      Assert.ok(bc.isActive, `Tab2 iframe ${i} should be active`);
    174    }
    175  });
    176 
    177  // Switch to tab 1
    178  await BrowserTestUtils.switchTab(gBrowser, ctx.tab1);
    179 
    180  // Navigate to page 3
    181  BrowserTestUtils.startLoadingURIString(
    182    ctx.tab1Browser,
    183    testPath + "bug343515_pg3.html"
    184  );
    185 
    186  await BrowserTestUtils.browserLoaded(ctx.tab1Browser);
    187 
    188  // Step 6.
    189 
    190  // Check everything
    191  ok(!ctx.tab0Browser.docShellIsActive, "Tab 0 should be inactive");
    192  ok(ctx.tab1Browser.docShellIsActive, "Tab 1 should be active");
    193  is(
    194    testPath + "bug343515_pg3.html",
    195    ctx.tab1Browser.currentURI.spec,
    196    "Got expected tab 1 url in step 6"
    197  );
    198 
    199  await SpecialPowers.spawn(ctx.tab1Browser, [], async function () {
    200    function isActive(aWindow) {
    201      var docshell = aWindow.docShell;
    202      info(`checking ${docshell.browsingContext.id}`);
    203      return docshell.browsingContext.isActive;
    204    }
    205 
    206    Assert.ok(isActive(content.frames[0]), "Tab1 iframe 0 should be active");
    207    Assert.ok(
    208      isActive(content.frames[0].frames[0]),
    209      "Tab1 iframe 0 subiframe 0 should be active"
    210    );
    211    Assert.ok(isActive(content.frames[1]), "Tab1 iframe 1 should be active");
    212  });
    213 
    214  ok(!ctx.tab2Browser.docShellIsActive, "Tab 2 should be inactive");
    215 
    216  await SpecialPowers.spawn(ctx.tab2Browser, [], async function () {
    217    for (var i = 0; i < content.frames.length; i++) {
    218      let bc = content.frames[i].browsingContext;
    219      Assert.ok(!bc.isActive, `Tab2 iframe ${i} should be inactive`);
    220    }
    221  });
    222 
    223  // Go forward on tab 2
    224  let forwardDone = BrowserTestUtils.waitForLocationChange(
    225    gBrowser,
    226    testPath + "bug343515_pg3.html"
    227  );
    228  ctx.tab2Browser.goForward();
    229  await forwardDone;
    230 
    231  // Step 7.
    232 
    233  async function checkBrowser(browser, outerTabNum, outerActive) {
    234    let data = { tabNum: outerTabNum, active: outerActive };
    235    await SpecialPowers.spawn(
    236      browser,
    237      [data],
    238      async function ({ tabNum, active }) {
    239        function isActive(aWindow) {
    240          var docshell = aWindow.docShell;
    241          info(`checking ${docshell.browsingContext.id}`);
    242          return docshell.browsingContext.isActive;
    243        }
    244 
    245        let activestr = active ? "active" : "inactive";
    246        Assert.equal(
    247          isActive(content.frames[0]),
    248          active,
    249          `Tab${tabNum} iframe 0 should be ${activestr}`
    250        );
    251        Assert.equal(
    252          isActive(content.frames[0].frames[0]),
    253          active,
    254          `Tab${tabNum} iframe 0 subiframe 0 should be ${activestr}`
    255        );
    256        Assert.equal(
    257          isActive(content.frames[1]),
    258          active,
    259          `Tab${tabNum} iframe 1 should be ${activestr}`
    260        );
    261      }
    262    );
    263  }
    264 
    265  // Check everything
    266  ok(!ctx.tab0Browser.docShellIsActive, "Tab 0 should be inactive");
    267  ok(ctx.tab1Browser.docShellIsActive, "Tab 1 should be active");
    268  is(
    269    testPath + "bug343515_pg3.html",
    270    ctx.tab2Browser.currentURI.spec,
    271    "Got expected tab 2 url in step 7"
    272  );
    273 
    274  await checkBrowser(ctx.tab1Browser, 1, true);
    275 
    276  ok(!ctx.tab2Browser.docShellIsActive, "Tab 2 should be inactive");
    277  await checkBrowser(ctx.tab2Browser, 2, false);
    278 
    279  // Close the tabs we made
    280  BrowserTestUtils.removeTab(ctx.tab1);
    281  BrowserTestUtils.removeTab(ctx.tab2);
    282 });