2009-05-18 01:12:14 -07:00
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
|
|
<head>
|
|
|
|
<title>localStorage basic test, while in sesison only mode</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()
|
|
|
|
{
|
2013-09-16 08:15:22 -07:00
|
|
|
SpecialPowers.pushPermissions([{'type': 'cookie', 'allow': SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION, 'context': document}], test1);
|
|
|
|
}
|
2009-05-18 01:12:14 -07:00
|
|
|
|
2013-09-16 08:15:22 -07:00
|
|
|
function test1() {
|
2009-05-18 01:12:14 -07:00
|
|
|
// Initially check the localStorage is empty
|
|
|
|
is(localStorage.length, 0, "The storage is empty [1]");
|
2012-03-29 14:09:09 -07:00
|
|
|
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
|
2009-05-18 01:12:14 -07:00
|
|
|
is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
|
2012-04-03 00:25:39 -07:00
|
|
|
is(localStorage["nonexisting"], undefined, "Nonexisting item is undefined (array access)");
|
|
|
|
is(localStorage.nonexisting, undefined, "Nonexisting item is undefined (property access)");
|
2009-05-18 01:12:14 -07:00
|
|
|
localStorage.removeItem("nonexisting"); // Just check there is no exception
|
|
|
|
|
|
|
|
is(typeof localStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object");
|
2012-04-03 00:25:39 -07:00
|
|
|
is(typeof localStorage["nonexisting"], "undefined", "['nonexisting'] is undefined");
|
|
|
|
is(typeof localStorage.nonexisting, "undefined", "nonexisting is undefined");
|
2009-05-18 01:12:14 -07:00
|
|
|
is(typeof localStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object");
|
2012-04-03 00:25:39 -07:00
|
|
|
is(typeof localStorage["nonexisting2"], "undefined", "['nonexisting2'] is undefined");
|
|
|
|
is(typeof localStorage.nonexisting2, "undefined", "nonexisting2 is undefined");
|
2009-05-18 01:12:14 -07:00
|
|
|
|
|
|
|
// add an empty-value key
|
|
|
|
localStorage.setItem("empty", "");
|
|
|
|
is(localStorage.getItem("empty"), "", "Empty value (getItem())");
|
|
|
|
is(localStorage["empty"], "", "Empty value (array access)");
|
|
|
|
is(localStorage.empty, "", "Empty value (property access)");
|
|
|
|
is(typeof localStorage.getItem("empty"), "string", "getItem('empty') is string");
|
|
|
|
is(typeof localStorage["empty"], "string", "['empty'] is string");
|
|
|
|
is(typeof localStorage.empty, "string", "empty is string");
|
|
|
|
localStorage.removeItem("empty");
|
|
|
|
is(localStorage.length, 0, "The storage has no keys");
|
|
|
|
is(localStorage.getItem("empty"), null, "empty item is null (getItem())");
|
2012-04-03 00:25:39 -07:00
|
|
|
is(localStorage["empty"], undefined, "empty item is undefined (array access)");
|
|
|
|
is(localStorage.empty, undefined, "empty item is undefined (property access)");
|
2009-05-18 01:12:14 -07:00
|
|
|
is(typeof localStorage.getItem("empty"), "object", "getItem('empty') is object");
|
2012-04-03 00:25:39 -07:00
|
|
|
is(typeof localStorage["empty"], "undefined", "['empty'] is undefined");
|
|
|
|
is(typeof localStorage.empty, "undefined", "empty is undefined");
|
2009-05-18 01:12:14 -07:00
|
|
|
|
|
|
|
// add one key, check it is there
|
|
|
|
localStorage.setItem("key1", "value1");
|
|
|
|
is(localStorage.length, 1, "The storage has one key-value pair");
|
|
|
|
is(localStorage.key(0), "key1");
|
2012-03-29 14:09:09 -07:00
|
|
|
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
|
2009-05-18 01:12:14 -07:00
|
|
|
|
|
|
|
// check all access method give the correct result
|
|
|
|
// and are of the correct type
|
|
|
|
is(localStorage.getItem("key1"), "value1", "getItem('key1') == value1");
|
|
|
|
is(localStorage["key1"], "value1", "['key1'] == value1");
|
|
|
|
is(localStorage.key1, "value1", "key1 == value1");
|
|
|
|
|
|
|
|
is(typeof localStorage.getItem("key1"), "string", "getItem('key1') is string");
|
|
|
|
is(typeof localStorage["key1"], "string", "['key1'] is string");
|
|
|
|
is(typeof localStorage.key1, "string", "key1 is string");
|
|
|
|
|
|
|
|
// remove the previously added key and check the storage is empty
|
|
|
|
localStorage.removeItem("key1");
|
|
|
|
is(localStorage.length, 0, "The storage is empty [2]");
|
2012-03-29 14:09:09 -07:00
|
|
|
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
|
2009-05-18 01:12:14 -07:00
|
|
|
is(localStorage.getItem("key1"), null, "\'key1\' removed");
|
|
|
|
|
|
|
|
is(typeof localStorage.getItem("key1"), "object", "getItem('key1') is object");
|
2012-04-03 00:25:39 -07:00
|
|
|
is(typeof localStorage["key1"], "undefined", "['key1'] is undefined");
|
|
|
|
is(typeof localStorage.key1, "undefined", "key1 is undefined");
|
2009-05-18 01:12:14 -07:00
|
|
|
|
|
|
|
// add one key, check it is there
|
|
|
|
localStorage.setItem("key1", "value1");
|
|
|
|
is(localStorage.length, 1, "The storage has one key-value pair");
|
|
|
|
is(localStorage.key(0), "key1");
|
|
|
|
is(localStorage.getItem("key1"), "value1");
|
|
|
|
|
|
|
|
// add a second key
|
|
|
|
localStorage.setItem("key2", "value2");
|
|
|
|
is(localStorage.length, 2, "The storage has two key-value pairs");
|
|
|
|
is(localStorage.getItem("key1"), "value1");
|
|
|
|
is(localStorage.getItem("key2"), "value2");
|
2012-03-02 15:43:50 -08:00
|
|
|
var firstKey = localStorage.key(0);
|
|
|
|
var secondKey = localStorage.key(1);
|
|
|
|
ok((firstKey == 'key1' && secondKey == 'key2') ||
|
|
|
|
(firstKey == 'key2' && secondKey == 'key1'),
|
2012-03-04 12:57:35 -08:00
|
|
|
'key() API works.');
|
2009-05-18 01:12:14 -07:00
|
|
|
|
|
|
|
// change the second key
|
|
|
|
localStorage.setItem("key2", "value2-2");
|
|
|
|
is(localStorage.length, 2, "The storage has two key-value pairs");
|
2012-03-02 15:43:50 -08:00
|
|
|
is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
|
|
|
|
is(localStorage.key(1), secondKey);
|
2012-03-29 14:09:09 -07:00
|
|
|
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
|
2009-05-18 01:12:14 -07:00
|
|
|
is(localStorage.getItem("key1"), "value1");
|
|
|
|
is(localStorage.getItem("key2"), "value2-2");
|
|
|
|
|
|
|
|
// change the first key
|
|
|
|
localStorage.setItem("key1", "value1-2");
|
|
|
|
is(localStorage.length, 2, "The storage has two key-value pairs");
|
2012-03-02 15:43:50 -08:00
|
|
|
is(localStorage.key(0), firstKey); // After key value changes the order must be preserved
|
|
|
|
is(localStorage.key(1), secondKey);
|
2012-03-29 14:09:09 -07:00
|
|
|
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(localStorage.key(2), null, "key() should return null for out-of-bounds access");
|
2009-05-18 01:12:14 -07:00
|
|
|
is(localStorage.getItem("key1"), "value1-2");
|
|
|
|
is(localStorage.getItem("key2"), "value2-2");
|
|
|
|
|
|
|
|
// remove the second key
|
|
|
|
localStorage.removeItem("key2");
|
|
|
|
is(localStorage.length, 1, "The storage has one key-value pair");
|
|
|
|
is(localStorage.key(0), "key1");
|
2012-03-29 14:09:09 -07:00
|
|
|
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
|
2009-05-18 01:12:14 -07:00
|
|
|
is(localStorage.getItem("key1"), "value1-2");
|
|
|
|
|
|
|
|
// JS property test
|
|
|
|
localStorage.testA = "valueA";
|
|
|
|
is(localStorage.testA, "valueA");
|
|
|
|
is(localStorage["testA"], "valueA");
|
|
|
|
is(localStorage.getItem("testA"), "valueA");
|
|
|
|
|
|
|
|
localStorage.testA = "valueA2";
|
|
|
|
is(localStorage.testA, "valueA2");
|
|
|
|
is(localStorage["testA"], "valueA2");
|
|
|
|
is(localStorage.getItem("testA"), "valueA2");
|
|
|
|
|
|
|
|
localStorage["testB"] = "valueB";
|
|
|
|
is(localStorage.testB, "valueB");
|
|
|
|
is(localStorage["testB"], "valueB");
|
|
|
|
is(localStorage.getItem("testB"), "valueB");
|
|
|
|
|
|
|
|
localStorage["testB"] = "valueB2";
|
|
|
|
is(localStorage.testB, "valueB2");
|
|
|
|
is(localStorage["testB"], "valueB2");
|
|
|
|
is(localStorage.getItem("testB"), "valueB2");
|
|
|
|
|
|
|
|
localStorage.setItem("testC", "valueC");
|
|
|
|
is(localStorage.testC, "valueC");
|
|
|
|
is(localStorage["testC"], "valueC");
|
|
|
|
is(localStorage.getItem("testC"), "valueC");
|
|
|
|
|
|
|
|
localStorage.setItem("testC", "valueC2");
|
|
|
|
is(localStorage.testC, "valueC2");
|
|
|
|
is(localStorage["testC"], "valueC2");
|
|
|
|
is(localStorage.getItem("testC"), "valueC2");
|
|
|
|
|
2012-04-03 00:25:39 -07:00
|
|
|
localStorage.setItem("testC", null);
|
|
|
|
is("testC" in localStorage, true);
|
|
|
|
is(localStorage.getItem("testC"), "null");
|
|
|
|
is(localStorage["testC"], "null");
|
|
|
|
is(localStorage.testC, "null");
|
|
|
|
|
|
|
|
localStorage.removeItem("testC");
|
|
|
|
localStorage["testC"] = null;
|
|
|
|
is("testC" in localStorage, true);
|
|
|
|
is(localStorage.getItem("testC"), "null");
|
|
|
|
is(localStorage["testC"], "null");
|
|
|
|
is(localStorage.testC, "null");
|
|
|
|
|
|
|
|
localStorage.setItem(null, "test");
|
|
|
|
is("null" in localStorage, true);
|
|
|
|
is(localStorage.getItem("null"), "test");
|
|
|
|
is(localStorage.getItem(null), "test");
|
|
|
|
is(localStorage["null"], "test");
|
|
|
|
localStorage.removeItem(null, "test");
|
|
|
|
// bug 350023
|
|
|
|
todo_is("null" in localStorage, false);
|
|
|
|
|
|
|
|
localStorage.setItem(null, "test");
|
|
|
|
is("null" in localStorage, true);
|
|
|
|
localStorage.removeItem("null", "test");
|
|
|
|
// bug 350023
|
|
|
|
todo_is("null" in localStorage, false);
|
|
|
|
|
2009-05-18 01:12:14 -07:00
|
|
|
// Clear the storage
|
|
|
|
localStorage.clear();
|
|
|
|
is(localStorage.length, 0, "The storage is empty [3]");
|
2012-03-29 14:09:09 -07:00
|
|
|
is(localStorage.key(0), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(localStorage.key(-1), null, "key() should return null for out-of-bounds access");
|
|
|
|
is(localStorage.key(1), null, "key() should return null for out-of-bounds access");
|
2009-05-18 01:12:14 -07:00
|
|
|
is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null");
|
|
|
|
is(localStorage.getItem("key1"), null, "key1 removed");
|
|
|
|
is(localStorage.getItem("key2"), null, "key2 removed");
|
|
|
|
localStorage.removeItem("nonexisting"); // Just check there is no exception
|
|
|
|
localStorage.removeItem("key1"); // Just check there is no exception
|
|
|
|
localStorage.removeItem("key2"); // Just check there is no exception
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
SimpleTest.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body onload="startTest();">
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|