tor-browser

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

browser_461634.js (3694B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 add_task(async function testClosedTabData() {
      6  /** Test for Bug 461634 */
      7 
      8  const REMEMBER = Date.now(),
      9    FORGET = Math.random();
     10  let test_state = {
     11    windows: [
     12      {
     13        tabs: [{ entries: [] }],
     14        _closedTabs: [
     15          {
     16            state: { entries: [{ url: "http://www.example.net/" }] },
     17            title: FORGET,
     18          },
     19          {
     20            state: { entries: [{ url: "http://www.example.net/" }] },
     21            title: REMEMBER,
     22          },
     23          {
     24            state: { entries: [{ url: "http://www.example.net/" }] },
     25            title: FORGET,
     26          },
     27          {
     28            state: { entries: [{ url: "http://www.example.net/" }] },
     29            title: REMEMBER,
     30          },
     31        ],
     32      },
     33    ],
     34  };
     35  let remember_count = 2;
     36 
     37  function countByTitle(aClosedTabList, aTitle) {
     38    return aClosedTabList.filter(aData => aData.title == aTitle).length;
     39  }
     40 
     41  function testForError(aFunction) {
     42    try {
     43      aFunction();
     44      return false;
     45    } catch (ex) {
     46      return ex.name == "NS_ERROR_ILLEGAL_VALUE";
     47    }
     48  }
     49 
     50  // Open a window and add the above closed tab list.
     51  let newWin = openDialog(location, "", "chrome,all,dialog=no");
     52  await promiseWindowLoaded(newWin);
     53 
     54  Services.prefs.setIntPref(
     55    "browser.sessionstore.max_tabs_undo",
     56    test_state.windows[0]._closedTabs.length
     57  );
     58  await setWindowState(newWin, test_state, true);
     59 
     60  let closedTabs = SessionStore.getClosedTabDataForWindow(newWin);
     61 
     62  // Verify that non JSON serialized data is the same as JSON serialized data.
     63  is(
     64    JSON.stringify(closedTabs),
     65    JSON.stringify(SessionStore.getClosedTabDataForWindow(newWin)),
     66    "Non-serialized data is the same as serialized data"
     67  );
     68 
     69  is(
     70    closedTabs.length,
     71    test_state.windows[0]._closedTabs.length,
     72    "Closed tab list has the expected length"
     73  );
     74  is(
     75    countByTitle(closedTabs, FORGET),
     76    test_state.windows[0]._closedTabs.length - remember_count,
     77    "The correct amout of tabs are to be forgotten"
     78  );
     79  is(
     80    countByTitle(closedTabs, REMEMBER),
     81    remember_count,
     82    "Everything is set up"
     83  );
     84 
     85  // All of the following calls with illegal arguments should throw NS_ERROR_ILLEGAL_VALUE.
     86  ok(
     87    testForError(() => ss.forgetClosedTab({}, 0)),
     88    "Invalid window for forgetClosedTab throws"
     89  );
     90  ok(
     91    testForError(() => ss.forgetClosedTab(newWin, -1)),
     92    "Invalid tab for forgetClosedTab throws"
     93  );
     94  ok(
     95    testForError(() =>
     96      ss.forgetClosedTab(newWin, test_state.windows[0]._closedTabs.length + 1)
     97    ),
     98    "Invalid tab for forgetClosedTab throws"
     99  );
    100 
    101  // Remove third tab, then first tab.
    102  ss.forgetClosedTab(newWin, 2);
    103  ss.forgetClosedTab(newWin, null);
    104 
    105  closedTabs = SessionStore.getClosedTabDataForWindow(newWin);
    106 
    107  // Verify that non JSON serialized data is the same as JSON serialized data.
    108  is(
    109    JSON.stringify(closedTabs),
    110    JSON.stringify(SessionStore.getClosedTabDataForWindow(newWin)),
    111    "Non-serialized data is the same as serialized data"
    112  );
    113 
    114  is(
    115    closedTabs.length,
    116    remember_count,
    117    "The correct amout of tabs was removed"
    118  );
    119  is(
    120    countByTitle(closedTabs, FORGET),
    121    0,
    122    "All tabs specifically forgotten were indeed removed"
    123  );
    124  is(
    125    countByTitle(closedTabs, REMEMBER),
    126    remember_count,
    127    "... and tabs not specifically forgetten weren't"
    128  );
    129 
    130  // Clean up.
    131  Services.prefs.clearUserPref("browser.sessionstore.max_tabs_undo");
    132  await BrowserTestUtils.closeWindow(newWin);
    133 });