tor-browser

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

test_localStorageBase.html (10189B)


      1 <html xmlns="http://www.w3.org/1999/xhtml">
      2 <head>
      3 <title>localStorage 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  "testA,null,valueA",
     21  "testA,valueA,valueA2",
     22  "testB,null,valueB",
     23  "testB,valueB,valueB2",
     24  "testC,null,valueC",
     25  "testC,valueC,valueC2",
     26  "testC,valueC2,null",
     27  "testC,null,null",
     28  "testC,null,null",
     29  "null,null,test",
     30  "null,test,null",
     31  "null,null,test",
     32  "null,test,null",
     33  "null,null,null"
     34 ];
     35 
     36 function startTest()
     37 {
     38  // Initially check the localStorage is empty
     39  is(localStorage.length, 0, "The storage is empty [1]");
     40  is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
     41  is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
     42  is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
     43  is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
     44  is(localStorage.nonexisting, undefined, "Nonexisting item is undefined (array access)");
     45  is(localStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)");
     46  localStorage.removeItem("nonexisting"); // Just check there is no exception
     47 
     48  is(typeof localStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object");
     49  is(typeof localStorage.nonexisting, "undefined", "['nonexisting'] is undefined");
     50  is(typeof localStorage.nonexisting, "undefined", "nonexisting is undefined");
     51  is(typeof localStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object");
     52  is(typeof localStorage.nonexisting2, "undefined", "['nonexisting2'] is undefined");
     53  is(typeof localStorage.nonexisting2, "undefined", "nonexisting2 is undefined");
     54 
     55  var localStorageCopy = localStorage;
     56 
     57  function onStorageChanged(e) {
     58    if (e.storageArea == localStorageCopy) {
     59      ok(expectedEvents.length, "Not more then expected events encountered");
     60      var receivedEvent = e.key + "," + e.oldValue + "," + e.newValue;
     61      is(receivedEvent, expectedEvents.shift(), "Expected event data: " + receivedEvent);
     62    }
     63  }
     64 
     65  // Listen for MozLocalStorageChanged
     66  SpecialPowers.addChromeEventListener("MozLocalStorageChanged", onStorageChanged, true);
     67 
     68  // add an empty-value key
     69  localStorage.setItem("empty", "");
     70  is(localStorage.getItem("empty"), "", "Empty value (getItem())");
     71  is(localStorage.empty, "", "Empty value (array access)");
     72  is(localStorage.empty, "", "Empty value (property access)");
     73  is(typeof localStorage.getItem("empty"), "string", "getItem('empty') is string");
     74  is(typeof localStorage.empty, "string", "['empty'] is string");
     75  is(typeof localStorage.empty, "string", "empty is string");
     76  localStorage.removeItem("empty");
     77  is(localStorage.length, 0, "The storage has no keys");
     78  is(localStorage.getItem("empty"), null, "empty item is null (getItem())");
     79  is(localStorage.empty, undefined, "empty item is undefined (array access)");
     80  is(localStorage.empty, undefined, "empty item is undefined (property access)");
     81  is(typeof localStorage.getItem("empty"), "object", "getItem('empty') is object");
     82  is(typeof localStorage.empty, "undefined", "['empty'] is undefined");
     83  is(typeof localStorage.empty, "undefined", "empty is undefined");
     84 
     85  // add one key, check it is there
     86  localStorage.setItem("key1", "value1");
     87  is(localStorage.length, 1, "The storage has one key-value pair");
     88  is(localStorage.key(0), "key1");
     89  is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
     90  is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
     91 
     92  // check all access method give the correct result
     93  // and are of the correct type
     94  is(localStorage.getItem("key1"), "value1", "getItem('key1') == value1");
     95  is(localStorage.key1, "value1", "['key1'] == value1");
     96  is(localStorage.key1, "value1", "key1 == value1");
     97 
     98  is(typeof localStorage.getItem("key1"), "string", "getItem('key1') is string");
     99  is(typeof localStorage.key1, "string", "['key1'] is string");
    100  is(typeof localStorage.key1, "string", "key1 is string");
    101 
    102  // remove the previously added key and check the storage is empty
    103  localStorage.removeItem("key1");
    104  is(localStorage.length, 0, "The storage is empty [2]");
    105  is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
    106  is(localStorage.getItem("key1"), null, "\'key1\' removed");
    107 
    108  is(typeof localStorage.getItem("key1"), "object", "getItem('key1') is object");
    109  is(typeof localStorage.key1, "undefined", "['key1'] is object");
    110  is(typeof localStorage.key1, "undefined", "key1 is object");
    111 
    112  // add one key, check it is there
    113  localStorage.setItem("key1", "value1");
    114  is(localStorage.length, 1, "The storage has one key-value pair");
    115  is(localStorage.key(0), "key1");
    116  is(localStorage.getItem("key1"), "value1");
    117 
    118  // add a second key
    119  localStorage.setItem("key2", "value2");
    120  is(localStorage.length, 2, "The storage has two key-value pairs");
    121  is(localStorage.getItem("key1"), "value1");
    122  is(localStorage.getItem("key2"), "value2");
    123  var firstKey = localStorage.key(0);
    124  var secondKey = localStorage.key(1);
    125  ok((firstKey == 'key1' && secondKey == 'key2') ||
    126     (firstKey == 'key2' && secondKey == 'key1'),
    127     'key() API works.');
    128 
    129  // change the second key
    130  localStorage.setItem("key2", "value2-2");
    131  is(localStorage.length, 2, "The storage has two key-value pairs");
    132  is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
    133  is(localStorage.key(1), secondKey);
    134  is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
    135  is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
    136  is(localStorage.getItem("key1"), "value1");
    137  is(localStorage.getItem("key2"), "value2-2");
    138 
    139  // change the first key
    140  localStorage.setItem("key1", "value1-2");
    141  is(localStorage.length, 2, "The storage has two key-value pairs");
    142  is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
    143  is(localStorage.key(1), secondKey);
    144  is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
    145  is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
    146  is(localStorage.getItem("key1"), "value1-2");
    147  is(localStorage.getItem("key2"), "value2-2");
    148 
    149  // remove the second key
    150  localStorage.removeItem("key2");
    151  is(localStorage.length, 1, "The storage has one key-value pair");
    152  is(localStorage.key(0), "key1");
    153  is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
    154  is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
    155  is(localStorage.getItem("key1"), "value1-2");
    156 
    157  // JS property test
    158  localStorage.testA = "valueA";
    159  is(localStorage.testA, "valueA");
    160  is(localStorage.testA, "valueA");
    161  is(localStorage.getItem("testA"), "valueA");
    162 
    163  localStorage.testA = "valueA2";
    164  is(localStorage.testA, "valueA2");
    165  is(localStorage.testA, "valueA2");
    166  is(localStorage.getItem("testA"), "valueA2");
    167 
    168  localStorage.testB = "valueB";
    169  is(localStorage.testB, "valueB");
    170  is(localStorage.testB, "valueB");
    171  is(localStorage.getItem("testB"), "valueB");
    172 
    173  localStorage.testB = "valueB2";
    174  is(localStorage.testB, "valueB2");
    175  is(localStorage.testB, "valueB2");
    176  is(localStorage.getItem("testB"), "valueB2");
    177 
    178  localStorage.setItem("testC", "valueC");
    179  is(localStorage.testC, "valueC");
    180  is(localStorage.testC, "valueC");
    181  is(localStorage.getItem("testC"), "valueC");
    182 
    183  localStorage.setItem("testC", "valueC2");
    184  is(localStorage.testC, "valueC2");
    185  is(localStorage.testC, "valueC2");
    186  is(localStorage.getItem("testC"), "valueC2");
    187 
    188  localStorage.setItem("testC", null);
    189  is("testC" in localStorage, true);
    190  is(localStorage.getItem("testC"), "null");
    191  is(localStorage.testC, "null");
    192  is(localStorage.testC, "null");
    193 
    194  localStorage.removeItem("testC");
    195  localStorage.testC = null;
    196  is("testC" in localStorage, true);
    197  is(localStorage.getItem("testC"), "null");
    198  is(localStorage.testC, "null");
    199  is(localStorage.testC, "null");
    200 
    201  localStorage.setItem(null, "test");
    202  is("null" in localStorage, true);
    203  is(localStorage.getItem("null"), "test");
    204  is(localStorage.getItem(null), "test");
    205  is(localStorage.null, "test");
    206  localStorage.removeItem(null, "test");
    207  is("null" in localStorage, false);
    208 
    209  localStorage.setItem(null, "test");
    210  is("null" in localStorage, true);
    211  localStorage.removeItem("null", "test");
    212  is("null" in localStorage, false);
    213 
    214  // Clear the storage
    215  localStorage.clear();
    216  is("testB" in localStorage, false, "Keys are not in the JS scope of the storage");
    217  is("testC" in localStorage, false, "Keys are not in the JS scope of the storage");
    218  is(localStorage.length, 0, "The storage is empty [3]");
    219  is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
    220  is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
    221  is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
    222  is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null");
    223  is(localStorage.getItem("key1"), null, "key1 removed");
    224  is(localStorage.getItem("key2"), null, "key2 removed");
    225  localStorage.removeItem("nonexisting"); // Just check there is no exception
    226  localStorage.removeItem("key1"); // Just check there is no exception
    227  localStorage.removeItem("key2"); // Just check there is no exception
    228 
    229  SimpleTest.executeSoon(function () {
    230    SpecialPowers.removeChromeEventListener("MozLocalStorageChanged", onStorageChanged, true);
    231    is(expectedEvents.length, 0, "received the correct number of events");
    232 
    233    localStorage.clear();
    234    SimpleTest.finish();
    235  });
    236 }
    237 
    238 SimpleTest.waitForExplicitFinish();
    239 
    240 </script>
    241 
    242 </head>
    243 
    244 <body onload="startTest();">
    245 
    246 </body>
    247 </html>