tor-browser

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

browser_history_persist.js (4049B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Ensure that history entries that should not be persisted are restored in the
      8 * same state.
      9 */
     10 add_task(async function check_history_not_persisted() {
     11  // Create an about:blank tab
     12  let tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
     13  let browser = tab.linkedBrowser;
     14  await BrowserTestUtils.browserLoaded(browser, { wantLoad: "about:blank" });
     15 
     16  // Retrieve the tab state.
     17  await TabStateFlusher.flush(browser);
     18  let state = JSON.parse(ss.getTabState(tab));
     19  is(
     20    state.entries[0].transient,
     21    true,
     22    "Should have collected the transient state"
     23  );
     24  BrowserTestUtils.removeTab(tab);
     25  browser = null;
     26 
     27  // Open a new tab to restore into.
     28  tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
     29  browser = tab.linkedBrowser;
     30 
     31  let sessionHistory = browser.browsingContext.sessionHistory;
     32 
     33  // addTab sends a message to the child to load about:blank, which sends a
     34  // message to the parent to add the SH entry.
     35  // promiseTabState modifies the SH syncronously, so ensure it is settled.
     36  await BrowserTestUtils.browserLoaded(browser, { wantLoad: "about:blank" });
     37  is(sessionHistory.count, 1, "Should have initial entry");
     38 
     39  info("New about:blank loaded, restoring state");
     40  await promiseTabState(tab, state);
     41 
     42  is(sessionHistory.count, 1, "Should be a single history entry");
     43  is(
     44    sessionHistory.getEntryAtIndex(0).URI.spec,
     45    "about:blank",
     46    "Should be the right URL"
     47  );
     48 
     49  // Load a new URL into the tab, it should replace the about:blank history entry
     50  BrowserTestUtils.startLoadingURIString(browser, "about:robots");
     51  await BrowserTestUtils.browserLoaded(browser, { wantLoad: "about:robots" });
     52 
     53  sessionHistory = browser.browsingContext.sessionHistory;
     54 
     55  is(sessionHistory.count, 1, "Should be a single history entry");
     56  is(
     57    sessionHistory.getEntryAtIndex(0).URI.spec,
     58    "about:robots",
     59    "Should be the right URL"
     60  );
     61 
     62  // Cleanup.
     63  BrowserTestUtils.removeTab(tab);
     64 });
     65 
     66 /**
     67 * Check that entries default to being persisted when the attribute doesn't
     68 * exist
     69 */
     70 add_task(async function check_history_default_persisted() {
     71  // Create an about:blank tab
     72  let tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
     73  let browser = tab.linkedBrowser;
     74  await BrowserTestUtils.browserLoaded(browser, { wantLoad: "about:blank" });
     75 
     76  // Retrieve the tab state.
     77  await TabStateFlusher.flush(browser);
     78  let state = JSON.parse(ss.getTabState(tab));
     79  delete state.entries[0].transient;
     80  BrowserTestUtils.removeTab(tab);
     81  browser = null;
     82 
     83  // Open a new tab to restore into.
     84  tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
     85  browser = tab.linkedBrowser;
     86 
     87  let sessionHistory = browser.browsingContext.sessionHistory;
     88 
     89  // addTab sends a message to the child to load about:blank, which sends a
     90  // message to the parent to add the SH entry.
     91  // promiseTabState modifies the SH syncronously, so ensure it is settled.
     92  await BrowserTestUtils.browserLoaded(browser, { wantLoad: "about:blank" });
     93  is(sessionHistory.count, 1, "Should have initial entry");
     94 
     95  info("New about:blank loaded, restoring state");
     96  await promiseTabState(tab, state);
     97 
     98  is(sessionHistory.count, 1, "Should be a single history entry");
     99  is(
    100    sessionHistory.getEntryAtIndex(0).URI.spec,
    101    "about:blank",
    102    "Should be the right URL"
    103  );
    104 
    105  // Load a new URL into the tab, it should NOT replace the about:blank history entry
    106  BrowserTestUtils.startLoadingURIString(browser, "about:robots");
    107  await promiseBrowserLoaded(browser, false, "about:robots");
    108 
    109  sessionHistory = browser.browsingContext.sessionHistory;
    110 
    111  is(sessionHistory.count, 2, "Should be two history entries");
    112  is(
    113    sessionHistory.getEntryAtIndex(0).URI.spec,
    114    "about:blank",
    115    "Should be the right URL"
    116  );
    117  is(
    118    sessionHistory.getEntryAtIndex(1).URI.spec,
    119    "about:robots",
    120    "Should be the right URL"
    121  );
    122 
    123  // Cleanup.
    124  BrowserTestUtils.removeTab(tab);
    125 });