2009-05-20 15:27:31 -07:00
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
|
|
<head>
|
|
|
|
<title>sessionStorage basic test</title>
|
|
|
|
|
|
|
|
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
|
|
|
function startTest()
|
|
|
|
{
|
|
|
|
sessionStorage.clear();
|
|
|
|
|
|
|
|
// Initially check the sessionStorage is empty
|
|
|
|
is(sessionStorage.length, 0, "The storage is empty [1]");
|
2012-03-29 14:09:09 -07:00
|
|
|
is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
|
2009-05-20 15:27:31 -07:00
|
|
|
is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
|
|
|
|
is(sessionStorage["nonexisting"], null, "Nonexisting item is null (array access)");
|
|
|
|
is(sessionStorage.nonexisting, null, "Nonexisting item is null (property access)");
|
|
|
|
sessionStorage.removeItem("nonexisting"); // Just check there is no exception
|
|
|
|
|
|
|
|
is(typeof sessionStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object");
|
|
|
|
is(typeof sessionStorage["nonexisting"], "object", "['nonexisting'] is object");
|
|
|
|
is(typeof sessionStorage.nonexisting, "object", "nonexisting is object");
|
|
|
|
is(typeof sessionStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object");
|
|
|
|
is(typeof sessionStorage["nonexisting2"], "object", "['nonexisting2'] is object");
|
|
|
|
is(typeof sessionStorage.nonexisting2, "object", "nonexisting2 is object");
|
|
|
|
|
|
|
|
// add an empty-value key
|
|
|
|
sessionStorage.setItem("empty", "");
|
|
|
|
is(sessionStorage.getItem("empty"), "", "Empty value (getItem())");
|
|
|
|
is(sessionStorage["empty"], "", "Empty value (array access)");
|
|
|
|
is(sessionStorage.empty, "", "Empty value (property access)");
|
|
|
|
is(typeof sessionStorage.getItem("empty"), "string", "getItem('empty') is string");
|
|
|
|
is(typeof sessionStorage["empty"], "string", "['empty'] is string");
|
|
|
|
is(typeof sessionStorage.empty, "string", "empty is string");
|
|
|
|
sessionStorage.removeItem("empty");
|
|
|
|
is(sessionStorage.length, 0, "The storage has no keys");
|
|
|
|
is(sessionStorage.getItem("empty"), null, "empty item is null (getItem())");
|
|
|
|
is(sessionStorage["empty"], null, "empty item is null (array access)");
|
|
|
|
is(sessionStorage.empty, null, "empty item is null (property access)");
|
|
|
|
is(typeof sessionStorage.getItem("empty"), "object", "getItem('empty') is object");
|
|
|
|
is(typeof sessionStorage["empty"], "object", "['empty'] is object");
|
|
|
|
is(typeof sessionStorage.empty, "object", "empty is object");
|
|
|
|
|
|
|
|
// add one key, check it is there
|
|
|
|
sessionStorage.setItem("key1", "value1");
|
|
|
|
is(sessionStorage.length, 1, "The storage has one key-value pair");
|
|
|
|
is(sessionStorage.key(0), "key1");
|
2012-03-29 14:09:09 -07:00
|
|
|
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
|
2009-05-20 15:27:31 -07:00
|
|
|
|
|
|
|
// check all access method give the correct result
|
|
|
|
// and are of the correct type
|
|
|
|
is(sessionStorage.getItem("key1"), "value1", "getItem('key1') == value1");
|
|
|
|
is(sessionStorage["key1"], "value1", "['key1'] == value1");
|
|
|
|
is(sessionStorage.key1, "value1", "key1 == value1");
|
|
|
|
|
|
|
|
is(typeof sessionStorage.getItem("key1"), "string", "getItem('key1') is string");
|
|
|
|
is(typeof sessionStorage["key1"], "string", "['key1'] is string");
|
|
|
|
is(typeof sessionStorage.key1, "string", "key1 is string");
|
|
|
|
|
|
|
|
// remove the previously added key and check the storage is empty
|
|
|
|
sessionStorage.removeItem("key1");
|
|
|
|
is(sessionStorage.length, 0, "The storage is empty [2]");
|
2012-03-29 14:09:09 -07:00
|
|
|
is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
|
2009-05-20 15:27:31 -07:00
|
|
|
is(sessionStorage.getItem("key1"), null, "\'key1\' removed");
|
|
|
|
|
|
|
|
is(typeof sessionStorage.getItem("key1"), "object", "getItem('key1') is object");
|
|
|
|
is(typeof sessionStorage["key1"], "object", "['key1'] is object");
|
|
|
|
is(typeof sessionStorage.key1, "object", "key1 is object");
|
|
|
|
|
|
|
|
// add one key, check it is there
|
|
|
|
sessionStorage.setItem("key1", "value1");
|
|
|
|
is(sessionStorage.length, 1, "The storage has one key-value pair");
|
|
|
|
is(sessionStorage.key(0), "key1");
|
|
|
|
is(sessionStorage.getItem("key1"), "value1");
|
|
|
|
|
|
|
|
// add a second key
|
|
|
|
sessionStorage.setItem("key2", "value2");
|
|
|
|
is(sessionStorage.length, 2, "The storage has two key-value pairs");
|
|
|
|
is(sessionStorage.getItem("key1"), "value1");
|
|
|
|
is(sessionStorage.getItem("key2"), "value2");
|
2012-03-02 15:43:50 -08:00
|
|
|
var firstKey = sessionStorage.key(0);
|
|
|
|
var secondKey = sessionStorage.key(1);
|
|
|
|
ok((firstKey == 'key1' && secondKey == 'key2') ||
|
|
|
|
(firstKey == 'key2' && secondKey == 'key1'),
|
2012-03-04 12:57:35 -08:00
|
|
|
'key() API works.');
|
2009-05-20 15:27:31 -07:00
|
|
|
|
|
|
|
// change the second key
|
|
|
|
sessionStorage.setItem("key2", "value2-2");
|
|
|
|
is(sessionStorage.length, 2, "The storage has two key-value pairs");
|
2012-03-02 15:43:50 -08:00
|
|
|
is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
|
|
|
|
is(sessionStorage.key(1), secondKey);
|
2012-03-29 14:09:09 -07:00
|
|
|
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
|
2009-05-20 15:27:31 -07:00
|
|
|
is(sessionStorage.getItem("key1"), "value1");
|
|
|
|
is(sessionStorage.getItem("key2"), "value2-2");
|
|
|
|
|
|
|
|
// change the first key
|
|
|
|
sessionStorage.setItem("key1", "value1-2");
|
|
|
|
is(sessionStorage.length, 2, "The storage has two key-value pairs");
|
2012-03-02 15:43:50 -08:00
|
|
|
is(sessionStorage.key(0), firstKey); // After key value changes the order must be preserved
|
|
|
|
is(sessionStorage.key(1), secondKey);
|
2012-03-29 14:09:09 -07:00
|
|
|
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(sessionStorage.key(2), null, "key() should return null for out-of-bounds access");
|
2009-05-20 15:27:31 -07:00
|
|
|
is(sessionStorage.getItem("key1"), "value1-2");
|
|
|
|
is(sessionStorage.getItem("key2"), "value2-2");
|
|
|
|
|
|
|
|
// remove the second key
|
|
|
|
sessionStorage.removeItem("key2");
|
|
|
|
is(sessionStorage.length, 1, "The storage has one key-value pair");
|
|
|
|
is(sessionStorage.key(0), "key1");
|
2012-03-29 14:09:09 -07:00
|
|
|
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
|
2009-05-20 15:27:31 -07:00
|
|
|
is(sessionStorage.getItem("key1"), "value1-2");
|
|
|
|
|
|
|
|
// Clear the storage
|
|
|
|
sessionStorage.clear();
|
|
|
|
is(sessionStorage.length, 0, "The storage is empty [3]");
|
2012-03-29 14:09:09 -07:00
|
|
|
is(sessionStorage.key(0), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(sessionStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(sessionStorage.key(1), null, "key() should return null for out-of-bounds access");
|
2009-05-20 15:27:31 -07:00
|
|
|
is(sessionStorage.getItem("nonexisting"), null, "Nonexisting item is null");
|
|
|
|
is(sessionStorage.getItem("key1"), null, "key1 removed");
|
|
|
|
is(sessionStorage.getItem("key2"), null, "key2 removed");
|
|
|
|
sessionStorage.removeItem("nonexisting"); // Just check there is no exception
|
|
|
|
sessionStorage.removeItem("key1"); // Just check there is no exception
|
|
|
|
sessionStorage.removeItem("key2"); // Just check there is no exception
|
|
|
|
|
|
|
|
SimpleTest.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body onload="startTest();">
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|