tor-browser

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

browser_test_offline_tab.js (2063B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_task(async function test_set_tab_offline() {
      7  await BrowserTestUtils.withNewTab("https://example.com", async browser => {
      8    // Set the tab to offline
      9    gBrowser.selectedBrowser.browsingContext.forceOffline = true;
     10 
     11    await SpecialPowers.spawn(browser, [], async () => {
     12      try {
     13        await content.fetch("https://example.com/empty.html");
     14        ok(false, "Should not load since tab is offline");
     15      } catch (err) {
     16        is(err.name, "TypeError", "Should fail since tab is offline");
     17      }
     18    });
     19  });
     20 });
     21 
     22 add_task(async function test_set_tab_online() {
     23  await BrowserTestUtils.withNewTab("https://example.com", async browser => {
     24    // Set the tab to online
     25    gBrowser.selectedBrowser.browsingContext.forceOffline = false;
     26 
     27    await SpecialPowers.spawn(browser, [], async () => {
     28      try {
     29        await content.fetch("https://example.com/empty.html");
     30        ok(true, "Should load since tab is online");
     31      } catch (err) {
     32        ok(false, "Should not fail since tab is online");
     33      }
     34    });
     35  });
     36 });
     37 
     38 add_task(async function test_set_tab_offline_events() {
     39  await BrowserTestUtils.withNewTab("https://example.com", async browser => {
     40    await SpecialPowers.spawn(browser, [], async () => {
     41      content.events = [];
     42      content.window.addEventListener("offline", () =>
     43        content.events.push("offline")
     44      );
     45      content.window.addEventListener("online", () =>
     46        content.events.push("online")
     47      );
     48    });
     49    gBrowser.selectedBrowser.browsingContext.forceOffline = true;
     50    Assert.deepEqual(
     51      await SpecialPowers.spawn(browser, [], async () => content.events),
     52      ["offline"],
     53      "events should be fired"
     54    );
     55    gBrowser.selectedBrowser.browsingContext.forceOffline = false;
     56    Assert.deepEqual(
     57      await SpecialPowers.spawn(browser, [], async () => content.events),
     58      ["offline", "online"],
     59      "events should be fired"
     60    );
     61  });
     62 });