tor-browser

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

test_sessionDataHelpers.js (2900B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 /**
      5 * Test SessionDataHelpers.
      6 */
      7 
      8 "use strict";
      9 
     10 const { SessionDataHelpers } = ChromeUtils.importESModule(
     11  "resource://devtools/server/actors/watcher/SessionDataHelpers.sys.mjs",
     12  { global: "contextual" }
     13 );
     14 const { SUPPORTED_DATA } = SessionDataHelpers;
     15 const { TARGETS } = SUPPORTED_DATA;
     16 
     17 function run_test() {
     18  const sessionData = {
     19    [TARGETS]: [],
     20  };
     21 
     22  info("Test adding a new entry");
     23  SessionDataHelpers.addOrSetSessionDataEntry(
     24    sessionData,
     25    TARGETS,
     26    ["frame", "worker"],
     27    "add"
     28  );
     29  deepEqual(
     30    sessionData[TARGETS],
     31    ["frame", "worker"],
     32    "the two elements were added"
     33  );
     34 
     35  info("Test adding a duplicated entry");
     36  SessionDataHelpers.addOrSetSessionDataEntry(
     37    sessionData,
     38    TARGETS,
     39    ["frame"],
     40    "add"
     41  );
     42  deepEqual(
     43    sessionData[TARGETS],
     44    ["frame", "worker"],
     45    "addOrSetSessionDataEntry ignore duplicates"
     46  );
     47 
     48  SessionDataHelpers.addOrSetSessionDataEntry(
     49    sessionData,
     50    TARGETS,
     51    ["process"],
     52    "add"
     53  );
     54  deepEqual(
     55    sessionData[TARGETS],
     56    ["frame", "worker", "process"],
     57    "the third element is added"
     58  );
     59 
     60  info("Test removing an existing entry");
     61  let removed = SessionDataHelpers.removeSessionDataEntry(
     62    sessionData,
     63    TARGETS,
     64    ["process"]
     65  );
     66  ok(removed, "removedSessionDataEntry returned true as it removed an element");
     67  deepEqual(
     68    sessionData[TARGETS],
     69    ["frame", "worker"],
     70    "the element has been remove"
     71  );
     72 
     73  info("Test removing non-existing entry");
     74  removed = SessionDataHelpers.removeSessionDataEntry(sessionData, TARGETS, [
     75    "not-existing",
     76  ]);
     77  ok(
     78    !removed,
     79    "removedSessionDataEntry returned false as no element has been removed"
     80  );
     81  deepEqual(
     82    sessionData[TARGETS],
     83    ["frame", "worker"],
     84    "no change made to the array"
     85  );
     86 
     87  removed = SessionDataHelpers.removeSessionDataEntry(sessionData, TARGETS, [
     88    "frame",
     89    "worker",
     90  ]);
     91  ok(
     92    removed,
     93    "removedSessionDataEntry returned true as elements have been removed"
     94  );
     95  deepEqual(sessionData[TARGETS], [], "all elements were removed");
     96 
     97  info("Test settting instead of adding data entries");
     98  SessionDataHelpers.addOrSetSessionDataEntry(
     99    sessionData,
    100    TARGETS,
    101    ["frame"],
    102    "add"
    103  );
    104  deepEqual(sessionData[TARGETS], ["frame"], "frame was re-added");
    105 
    106  SessionDataHelpers.addOrSetSessionDataEntry(
    107    sessionData,
    108    TARGETS,
    109    ["process", "worker"],
    110    "set"
    111  );
    112  deepEqual(
    113    sessionData[TARGETS],
    114    ["process", "worker"],
    115    "frame was replaced by process and worker"
    116  );
    117 
    118  info("Test setting an empty array");
    119  SessionDataHelpers.addOrSetSessionDataEntry(sessionData, TARGETS, [], "set");
    120  deepEqual(
    121    sessionData[TARGETS],
    122    [],
    123    "Setting an empty array of entries clears the data entry"
    124  );
    125 }