tor-browser

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

test_sessionStorageBase.html (8206B)


      1 <html xmlns="http://www.w3.org/1999/xhtml">
      2 <head>
      3 <title>sessionStorage basic test</title>
      4 
      5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      7 
      8 <script type="text/javascript">
      9 
     10 var expectedEvents = [
     11  "empty,null,",
     12  "empty,,null",
     13  "key1,null,value1",
     14  "key1,value1,null",
     15  "key1,null,value1",
     16  "key2,null,value2",
     17  "key2,value2,value2-2",
     18  "key1,value1,value1-2",
     19  "key2,value2-2,null",
     20  "null,null,null"
     21 ];
     22 
     23 function setup() {
     24  sessionStorage.clear();
     25  SimpleTest.executeSoon(startTest);
     26 }
     27 
     28 function startTest()
     29 {
     30  // Initially check the sessionStorage is empty
     31  is(sessionStorage.length, 0, "The storage is empty [1]");
     32  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
     33  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
     34  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
     35  is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
     36  is(sessionStorage.nonexisting, undefined, "Nonexisting item is undefined (array access)");
     37  is(sessionStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)");
     38  sessionStorage.removeItem("nonexisting"); // Just check there is no exception
     39 
     40  is(typeof sessionStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object");
     41  is(typeof sessionStorage.nonexisting, "undefined", "['nonexisting'] is undefined");
     42  is(typeof sessionStorage.nonexisting, "undefined", "nonexisting is undefined");
     43  is(typeof sessionStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object");
     44  is(typeof sessionStorage.nonexisting2, "undefined", "['nonexisting2'] is undefined");
     45  is(typeof sessionStorage.nonexisting2, "undefined", "nonexisting2 is undefined");
     46 
     47  var sessionStorageCopy = sessionStorage;
     48 
     49  function onStorageChanged(e) {
     50    if (e.storageArea == sessionStorageCopy) {
     51      ok(expectedEvents.length, "Not more then expected events encountered");
     52      var receivedEvent = e.key + "," + e.oldValue + "," + e.newValue;
     53      is(receivedEvent, expectedEvents.shift(), "Expected event data: " + receivedEvent);
     54    }
     55  }
     56 
     57  // Listen for MozSessionStorageChanged
     58  SpecialPowers.addChromeEventListener("MozSessionStorageChanged", onStorageChanged, true);
     59 
     60  // add an empty-value key
     61  sessionStorage.setItem("empty", "");
     62  is(sessionStorage.getItem("empty"), "", "Empty value (getItem())");
     63  is(sessionStorage.empty, "", "Empty value (array access)");
     64  is(sessionStorage.empty, "", "Empty value (property access)");
     65  is(typeof sessionStorage.getItem("empty"), "string", "getItem('empty') is string");
     66  is(typeof sessionStorage.empty, "string", "['empty'] is string");
     67  is(typeof sessionStorage.empty, "string", "empty is string");
     68  sessionStorage.removeItem("empty");
     69  is(sessionStorage.length, 0, "The storage has no keys");
     70  is(sessionStorage.getItem("empty"), null, "empty item is null (getItem())");
     71  is(sessionStorage.empty, undefined, "empty item is undefined (array access)");
     72  is(sessionStorage.empty, undefined, "empty item is undefined (property access)");
     73  is(typeof sessionStorage.getItem("empty"), "object", "getItem('empty') is object");
     74  is(typeof sessionStorage.empty, "undefined", "['empty'] is undefined");
     75  is(typeof sessionStorage.empty, "undefined", "empty is undefined");
     76 
     77  // add one key, check it is there
     78  sessionStorage.setItem("key1", "value1");
     79  is(sessionStorage.length, 1, "The storage has one key-value pair");
     80  is(sessionStorage.key(0), "key1");
     81  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
     82  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
     83 
     84  // check all access method give the correct result
     85  // and are of the correct type
     86  is(sessionStorage.getItem("key1"), "value1", "getItem('key1') == value1");
     87  is(sessionStorage.key1, "value1", "['key1'] == value1");
     88  is(sessionStorage.key1, "value1", "key1 == value1");
     89 
     90  is(typeof sessionStorage.getItem("key1"), "string", "getItem('key1') is string");
     91  is(typeof sessionStorage.key1, "string", "['key1'] is string");
     92  is(typeof sessionStorage.key1, "string", "key1 is string");
     93 
     94  // remove the previously added key and check the storage is empty
     95  sessionStorage.removeItem("key1");
     96  is(sessionStorage.length, 0, "The storage is empty [2]");
     97  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
     98  is(sessionStorage.getItem("key1"), null, "\'key1\' removed");
     99 
    100  is(typeof sessionStorage.getItem("key1"), "object", "getItem('key1') is object");
    101  is(typeof sessionStorage.key1, "undefined", "['key1'] is undefined");
    102  is(typeof sessionStorage.key1, "undefined", "key1 is undefined");
    103 
    104  // add one key, check it is there
    105  sessionStorage.setItem("key1", "value1");
    106  is(sessionStorage.length, 1, "The storage has one key-value pair");
    107  is(sessionStorage.key(0), "key1");
    108  is(sessionStorage.getItem("key1"), "value1");
    109 
    110  // add a second key
    111  sessionStorage.setItem("key2", "value2");
    112  is(sessionStorage.length, 2, "The storage has two key-value pairs");
    113  is(sessionStorage.getItem("key1"), "value1");
    114  is(sessionStorage.getItem("key2"), "value2");
    115  var firstKey = sessionStorage.key(0);
    116  var secondKey = sessionStorage.key(1);
    117  ok((firstKey == 'key1' && secondKey == 'key2') ||
    118     (firstKey == 'key2' && secondKey == 'key1'),
    119     'key() API works.');
    120 
    121  // change the second key
    122  sessionStorage.setItem("key2", "value2-2");
    123  is(sessionStorage.length, 2, "The storage has two key-value pairs");
    124  is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
    125  is(sessionStorage.key(1), secondKey);
    126  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    127  is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
    128  is(sessionStorage.getItem("key1"), "value1");
    129  is(sessionStorage.getItem("key2"), "value2-2");
    130 
    131  // change the first key
    132  sessionStorage.setItem("key1", "value1-2");
    133  is(sessionStorage.length, 2, "The storage has two key-value pairs");
    134  is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
    135  is(sessionStorage.key(1), secondKey);
    136  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    137  is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
    138  is(sessionStorage.getItem("key1"), "value1-2");
    139  is(sessionStorage.getItem("key2"), "value2-2");
    140 
    141  // remove the second key
    142  sessionStorage.removeItem("key2");
    143  is(sessionStorage.length, 1, "The storage has one key-value pair");
    144  is(sessionStorage.key(0), "key1");
    145  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    146  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
    147  is(sessionStorage.getItem("key1"), "value1-2");
    148 
    149  // Clear the storage
    150  sessionStorage.clear();
    151  is(sessionStorage.length, 0, "The storage is empty [3]");
    152  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
    153  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    154  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
    155  is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null");
    156  is(sessionStorage.getItem("key1"), null, "key1 removed");
    157  is(sessionStorage.getItem("key2"), null, "key2 removed");
    158  sessionStorage.removeItem("nonexisting"); // Just check there is no exception
    159  sessionStorage.removeItem("key1"); // Just check there is no exception
    160  sessionStorage.removeItem("key2"); // Just check there is no exception
    161 
    162  SimpleTest.executeSoon(function () {
    163    SpecialPowers.removeChromeEventListener("MozSessionStorageChanged", onStorageChanged, true);
    164    is(expectedEvents.length, 0, "received the correct number of events");
    165 
    166    sessionStorage.clear();
    167    SimpleTest.finish();
    168  });
    169 }
    170 
    171 SimpleTest.waitForExplicitFinish();
    172 
    173 </script>
    174 
    175 </head>
    176 
    177 <body onload="setup();">
    178 
    179 </body>
    180 </html>