tor-browser

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

browser_491577.js (5231B)


      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 test_deleteClosedWindow() {
      6  /** Test for Bug 491577 */
      7 
      8  const REMEMBER = Date.now(),
      9    FORGET = Math.random();
     10  let test_state = {
     11    windows: [
     12      {
     13        tabs: [
     14          {
     15            entries: [
     16              { url: "http://example.com/", triggeringPrincipal_base64 },
     17            ],
     18          },
     19        ],
     20        selected: 1,
     21      },
     22    ],
     23    _closedWindows: [
     24      // _closedWindows[0]
     25      {
     26        tabs: [
     27          {
     28            entries: [
     29              {
     30                url: "http://example.com/",
     31                triggeringPrincipal_base64,
     32                title: "title",
     33              },
     34            ],
     35          },
     36          {
     37            entries: [
     38              {
     39                url: "http://mozilla.org/",
     40                triggeringPrincipal_base64,
     41                title: "title",
     42              },
     43            ],
     44          },
     45        ],
     46        selected: 2,
     47        title: FORGET,
     48        _closedTabs: [],
     49      },
     50      // _closedWindows[1]
     51      {
     52        tabs: [
     53          {
     54            entries: [
     55              {
     56                url: "http://mozilla.org/",
     57                triggeringPrincipal_base64,
     58                title: "title",
     59              },
     60            ],
     61          },
     62          {
     63            entries: [
     64              {
     65                url: "http://example.com/",
     66                triggeringPrincipal_base64,
     67                title: "title",
     68              },
     69            ],
     70          },
     71          {
     72            entries: [
     73              {
     74                url: "http://mozilla.org/",
     75                triggeringPrincipal_base64,
     76                title: "title",
     77              },
     78            ],
     79          },
     80        ],
     81        selected: 3,
     82        title: REMEMBER,
     83        _closedTabs: [],
     84      },
     85      // _closedWindows[2]
     86      {
     87        tabs: [
     88          {
     89            entries: [
     90              {
     91                url: "http://example.com/",
     92                triggeringPrincipal_base64,
     93                title: "title",
     94              },
     95            ],
     96          },
     97        ],
     98        selected: 1,
     99        title: FORGET,
    100        _closedTabs: [
    101          {
    102            state: {
    103              entries: [
    104                {
    105                  url: "http://mozilla.org/",
    106                  triggeringPrincipal_base64,
    107                  title: "title",
    108                },
    109                {
    110                  url: "http://mozilla.org/again",
    111                  triggeringPrincipal_base64,
    112                  title: "title",
    113                },
    114              ],
    115            },
    116            pos: 1,
    117            title: "title",
    118          },
    119          {
    120            state: {
    121              entries: [
    122                {
    123                  url: "http://example.com",
    124                  triggeringPrincipal_base64,
    125                  title: "title",
    126                },
    127              ],
    128            },
    129            title: "title",
    130          },
    131        ],
    132      },
    133    ],
    134  };
    135  let remember_count = 1;
    136 
    137  function countByTitle(aClosedWindowList, aTitle) {
    138    return aClosedWindowList.filter(aData => aData.title == aTitle).length;
    139  }
    140 
    141  function testForError(aFunction) {
    142    try {
    143      aFunction();
    144      return false;
    145    } catch (ex) {
    146      return ex.name == "NS_ERROR_ILLEGAL_VALUE";
    147    }
    148  }
    149 
    150  // open a window and add the above closed window list
    151  let newWin = openDialog(location, "_blank", "chrome,all,dialog=no");
    152  await promiseWindowLoaded(newWin);
    153  Services.prefs.setIntPref(
    154    "browser.sessionstore.max_windows_undo",
    155    test_state._closedWindows.length
    156  );
    157  await setWindowState(newWin, test_state, true);
    158 
    159  let closedWindows = ss.getClosedWindowData();
    160  is(
    161    closedWindows.length,
    162    test_state._closedWindows.length,
    163    "Closed window list has the expected length"
    164  );
    165  is(
    166    countByTitle(closedWindows, FORGET),
    167    test_state._closedWindows.length - remember_count,
    168    "The correct amount of windows are to be forgotten"
    169  );
    170  is(
    171    countByTitle(closedWindows, REMEMBER),
    172    remember_count,
    173    "Everything is set up."
    174  );
    175 
    176  // all of the following calls with illegal arguments should throw NS_ERROR_ILLEGAL_VALUE
    177  ok(
    178    testForError(() => ss.forgetClosedWindow(-1)),
    179    "Invalid window for forgetClosedWindow throws"
    180  );
    181  ok(
    182    testForError(() =>
    183      ss.forgetClosedWindow(test_state._closedWindows.length + 1)
    184    ),
    185    "Invalid window for forgetClosedWindow throws"
    186  );
    187 
    188  // Remove third window, then first window
    189  ss.forgetClosedWindow(2);
    190  ss.forgetClosedWindow(null);
    191 
    192  closedWindows = ss.getClosedWindowData();
    193  is(
    194    closedWindows.length,
    195    remember_count,
    196    "The correct amount of windows were removed"
    197  );
    198  is(
    199    countByTitle(closedWindows, FORGET),
    200    0,
    201    "All windows specifically forgotten were indeed removed"
    202  );
    203  is(
    204    countByTitle(closedWindows, REMEMBER),
    205    remember_count,
    206    "... and windows not specifically forgetten weren't."
    207  );
    208 
    209  // clean up
    210  Services.prefs.clearUserPref("browser.sessionstore.max_windows_undo");
    211  await BrowserTestUtils.closeWindow(newWin);
    212 });