gecko/dom/tests/mochitest/localstorage/test_localStorageBase.html

191 lines
7.9 KiB
HTML
Raw Normal View History

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>localStorage basic test</title>
<script type="text/javascript" src="/MochiKit/packed.js"></script>
<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">
var INDEX_SIZE_ERR = 1;
function checkException(func, exc)
{
var exceptionThrew = false;
try {
func();
}
catch (ex) {
exceptionThrew = true;
is(ex.code, exc, "Expected "+exc+" exception");
}
ok(exceptionThrew, "Exception "+exc+" threw");
}
function startTest()
{
// Initially check the localStorage is empty
is(localStorage.length, 0, "The storage is empty [1]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
is(localStorage.getItem("nonexisting"), null, "Nonexisting item is null (getItem())");
is(localStorage["nonexisting"], null, "Nonexisting item is null (array access)");
is(localStorage.nonexisting, null, "Nonexisting item is null (property access)");
localStorage.removeItem("nonexisting"); // Just check there is no exception
is(typeof localStorage.getItem("nonexisting"), "object", "getItem('nonexisting') is object");
is(typeof localStorage["nonexisting"], "object", "['nonexisting'] is object");
is(typeof localStorage.nonexisting, "object", "nonexisting is object");
is(typeof localStorage.getItem("nonexisting2"), "object", "getItem('nonexisting2') is object");
is(typeof localStorage["nonexisting2"], "object", "['nonexisting2'] is object");
is(typeof localStorage.nonexisting2, "object", "nonexisting2 is object");
// 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())");
is(localStorage["empty"], null, "empty item is null (array access)");
is(localStorage.empty, null, "empty item is null (property access)");
is(typeof localStorage.getItem("empty"), "object", "getItem('empty') is object");
is(typeof localStorage["empty"], "object", "['empty'] is object");
is(typeof localStorage.empty, "object", "empty is object");
// 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");
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
// 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]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR);
is(localStorage.getItem("key1"), null, "\'key1\' removed");
is(typeof localStorage.getItem("key1"), "object", "getItem('key1') is object");
is(typeof localStorage["key1"], "object", "['key1'] is object");
is(typeof localStorage.key1, "object", "key1 is object");
// 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.key(1), "key1"); // This test might not be accurate because order is not preserved
is(localStorage.key(0), "key2");
is(localStorage.getItem("key1"), "value1");
is(localStorage.getItem("key2"), "value2");
// change the second key
localStorage.setItem("key2", "value2-2");
is(localStorage.length, 2, "The storage has two key-value pairs");
is(localStorage.key(1), "key1"); // After key value changes the order must be preserved
is(localStorage.key(0), "key2");
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(2);}, INDEX_SIZE_ERR);
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");
is(localStorage.key(1), "key1"); // After key value changes the order must be preserved
is(localStorage.key(0), "key2");
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(2);}, INDEX_SIZE_ERR);
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");
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
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");
// Clear the storage
localStorage.clear();
is("testB" in localStorage, false, "Keys are not in the JS scope of the storage");
is("testC" in localStorage, false, "Keys are not in the JS scope of the storage");
is(localStorage.length, 0, "The storage is empty [3]");
checkException(function() {localStorage.key(0);}, INDEX_SIZE_ERR); // this is unspecified!
checkException(function() {localStorage.key(-1);}, INDEX_SIZE_ERR);
checkException(function() {localStorage.key(1);}, INDEX_SIZE_ERR);
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>