dom/tests/mochitest/sessionstorage/test_sessionStorageBase.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/sessionstorage/test_sessionStorageBase.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,181 @@
     1.4 +<html xmlns="http://www.w3.org/1999/xhtml">
     1.5 +<head>
     1.6 +<title>sessionStorage basic test</title>
     1.7 +
     1.8 +<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.10 +
    1.11 +<script type="text/javascript">
    1.12 +
    1.13 +var expectedEvents = [
    1.14 +  "empty,null,",
    1.15 +  "empty,,null",
    1.16 +  "key1,null,value1",
    1.17 +  "key1,value1,null",
    1.18 +  "key1,null,value1",
    1.19 +  "key2,null,value2",
    1.20 +  "key2,value2,value2-2",
    1.21 +  "key1,value1,value1-2",
    1.22 +  "key2,value2-2,null",
    1.23 +  "null,null,null"
    1.24 +];
    1.25 +
    1.26 +function setup() {
    1.27 +  sessionStorage.clear();
    1.28 +  SimpleTest.executeSoon(startTest);
    1.29 +}
    1.30 +
    1.31 +function startTest()
    1.32 +{
    1.33 +  // Initially check the sessionStorage is empty
    1.34 +  is(sessionStorage.length, 0, "The storage is empty [1]");
    1.35 +  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
    1.36 +  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    1.37 +  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
    1.38 +  is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
    1.39 +  is(sessionStorage["nonexisting"], undefined, "Nonexisting item is undefined (array access)");
    1.40 +  is(sessionStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)");
    1.41 +  sessionStorage.removeItem("nonexisting"); // Just check there is no exception
    1.42 +
    1.43 +  is(typeof sessionStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object");
    1.44 +  is(typeof sessionStorage["nonexisting"], "undefined", "['nonexisting'] is undefined");
    1.45 +  is(typeof sessionStorage.nonexisting, "undefined", "nonexisting is undefined");
    1.46 +  is(typeof sessionStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object");
    1.47 +  is(typeof sessionStorage["nonexisting2"], "undefined", "['nonexisting2'] is undefined");
    1.48 +  is(typeof sessionStorage.nonexisting2, "undefined", "nonexisting2 is undefined");
    1.49 +
    1.50 +  var mozStorageChangedReceived = 0;
    1.51 +  var sessionStorageCopy = sessionStorage;
    1.52 +
    1.53 +  function onStorageChanged(e) {
    1.54 +    if (e.storageArea == sessionStorageCopy) {
    1.55 +      ok(expectedEvents.length > 0, "Not more then expected events encountered");
    1.56 +      var receivedEvent = e.key + "," + e.oldValue + "," + e.newValue;
    1.57 +      is(receivedEvent, expectedEvents.shift(), "Expected event data: " + receivedEvent);
    1.58 +    }
    1.59 +  }
    1.60 +
    1.61 +  // Listen for MozStorageChanged
    1.62 +  SpecialPowers.addChromeEventListener("MozStorageChanged", onStorageChanged, false);
    1.63 +
    1.64 +  // add an empty-value key
    1.65 +  sessionStorage.setItem("empty", "");
    1.66 +  is(sessionStorage.getItem("empty"), "", "Empty value (getItem())");
    1.67 +  is(sessionStorage["empty"], "", "Empty value (array access)");
    1.68 +  is(sessionStorage.empty, "", "Empty value (property access)");
    1.69 +  is(typeof sessionStorage.getItem("empty"), "string", "getItem('empty') is string");
    1.70 +  is(typeof sessionStorage["empty"], "string", "['empty'] is string");
    1.71 +  is(typeof sessionStorage.empty, "string", "empty is string");
    1.72 +  sessionStorage.removeItem("empty");
    1.73 +  is(sessionStorage.length, 0, "The storage has no keys");
    1.74 +  is(sessionStorage.getItem("empty"), null, "empty item is null (getItem())");
    1.75 +  is(sessionStorage["empty"], undefined, "empty item is undefined (array access)");
    1.76 +  is(sessionStorage.empty, undefined, "empty item is undefined (property access)");
    1.77 +  is(typeof sessionStorage.getItem("empty"), "object", "getItem('empty') is object");
    1.78 +  is(typeof sessionStorage["empty"], "undefined", "['empty'] is undefined");
    1.79 +  is(typeof sessionStorage.empty, "undefined", "empty is undefined");
    1.80 +
    1.81 +  // add one key, check it is there
    1.82 +  sessionStorage.setItem("key1", "value1");
    1.83 +  is(sessionStorage.length, 1, "The storage has one key-value pair");
    1.84 +  is(sessionStorage.key(0), "key1");
    1.85 +  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
    1.86 +  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
    1.87 +
    1.88 +  // check all access method give the correct result
    1.89 +  // and are of the correct type
    1.90 +  is(sessionStorage.getItem("key1"), "value1", "getItem('key1') == value1");
    1.91 +  is(sessionStorage["key1"], "value1", "['key1'] == value1");
    1.92 +  is(sessionStorage.key1, "value1", "key1 == value1");
    1.93 +
    1.94 +  is(typeof sessionStorage.getItem("key1"), "string", "getItem('key1') is string");
    1.95 +  is(typeof sessionStorage["key1"], "string", "['key1'] is string");
    1.96 +  is(typeof sessionStorage.key1, "string", "key1 is string");
    1.97 +
    1.98 +  // remove the previously added key and check the storage is empty
    1.99 +  sessionStorage.removeItem("key1");
   1.100 +  is(sessionStorage.length, 0, "The storage is empty [2]");
   1.101 +  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
   1.102 +  is(sessionStorage.getItem("key1"), null, "\'key1\' removed");
   1.103 +
   1.104 +  is(typeof sessionStorage.getItem("key1"), "object", "getItem('key1') is object");
   1.105 +  is(typeof sessionStorage["key1"], "undefined", "['key1'] is undefined");
   1.106 +  is(typeof sessionStorage.key1, "undefined", "key1 is undefined");
   1.107 +
   1.108 +  // add one key, check it is there
   1.109 +  sessionStorage.setItem("key1", "value1");
   1.110 +  is(sessionStorage.length, 1, "The storage has one key-value pair");
   1.111 +  is(sessionStorage.key(0), "key1");
   1.112 +  is(sessionStorage.getItem("key1"), "value1");
   1.113 +
   1.114 +  // add a second key
   1.115 +  sessionStorage.setItem("key2", "value2");
   1.116 +  is(sessionStorage.length, 2, "The storage has two key-value pairs");
   1.117 +  is(sessionStorage.getItem("key1"), "value1");
   1.118 +  is(sessionStorage.getItem("key2"), "value2");
   1.119 +  var firstKey = sessionStorage.key(0);
   1.120 +  var secondKey = sessionStorage.key(1);
   1.121 +  ok((firstKey == 'key1' && secondKey == 'key2') ||
   1.122 +     (firstKey == 'key2' && secondKey == 'key1'),
   1.123 +     'key() API works.');
   1.124 +
   1.125 +  // change the second key
   1.126 +  sessionStorage.setItem("key2", "value2-2");
   1.127 +  is(sessionStorage.length, 2, "The storage has two key-value pairs");
   1.128 +  is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
   1.129 +  is(sessionStorage.key(1), secondKey);
   1.130 +  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
   1.131 +  is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
   1.132 +  is(sessionStorage.getItem("key1"), "value1");
   1.133 +  is(sessionStorage.getItem("key2"), "value2-2");
   1.134 +
   1.135 +  // change the first key
   1.136 +  sessionStorage.setItem("key1", "value1-2");
   1.137 +  is(sessionStorage.length, 2, "The storage has two key-value pairs");
   1.138 +  is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
   1.139 +  is(sessionStorage.key(1), secondKey);
   1.140 +  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
   1.141 +  is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
   1.142 +  is(sessionStorage.getItem("key1"), "value1-2");
   1.143 +  is(sessionStorage.getItem("key2"), "value2-2");
   1.144 +
   1.145 +  // remove the second key
   1.146 +  sessionStorage.removeItem("key2");
   1.147 +  is(sessionStorage.length, 1, "The storage has one key-value pair");
   1.148 +  is(sessionStorage.key(0), "key1");
   1.149 +  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
   1.150 +  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
   1.151 +  is(sessionStorage.getItem("key1"), "value1-2");
   1.152 +
   1.153 +  // Clear the storage
   1.154 +  sessionStorage.clear();
   1.155 +  is(sessionStorage.length, 0, "The storage is empty [3]");
   1.156 +  is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
   1.157 +  is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
   1.158 +  is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
   1.159 +  is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null");
   1.160 +  is(sessionStorage.getItem("key1"), null, "key1 removed");
   1.161 +  is(sessionStorage.getItem("key2"), null, "key2 removed");
   1.162 +  sessionStorage.removeItem("nonexisting"); // Just check there is no exception
   1.163 +  sessionStorage.removeItem("key1"); // Just check there is no exception
   1.164 +  sessionStorage.removeItem("key2"); // Just check there is no exception
   1.165 +
   1.166 +  SimpleTest.executeSoon(function () {
   1.167 +    SpecialPowers.removeChromeEventListener("MozStorageChanged", onStorageChanged, false);
   1.168 +    is(expectedEvents.length, 0, "received the correct number of events");
   1.169 +
   1.170 +    sessionStorage.clear();
   1.171 +    SimpleTest.finish();
   1.172 +  });
   1.173 +}
   1.174 +
   1.175 +SimpleTest.waitForExplicitFinish();
   1.176 +
   1.177 +</script>
   1.178 +
   1.179 +</head>
   1.180 +
   1.181 +<body onload="setup();">
   1.182 +
   1.183 +</body>
   1.184 +</html>

mercurial