summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webstorage/storage_key.html
blob: c70b536fb07dfe9b31c3bf3964e89ecb4c209ad7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>WebStorage Test: Storage - key(index)</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
["localStorage", "sessionStorage"].forEach(function(name) {
    test(function() {
        var storage = window[name];
        storage.clear();

        storage.setItem("name", "user1");
        storage.setItem("age", "20");
        storage.setItem("a", "1");
        storage.setItem("b", "2");

        var keys = ["name", "age", "a", "b"];
        function doTest(index) {
            test(function() {
                var key = storage.key(index);
                assert_not_equals(key, null);
                assert_true(keys.indexOf(key) >= 0,
                            "Unexpected key " + key + " found.");
            }, name + ".key(" + index + ") should return the right thing.");
        }
        for (var i = 0; i < keys.length; ++i) {
            doTest(i);
            doTest(i + 0x100000000);
        }

        test(function() {
            assert_equals(storage.key(-1), null, "storage.key(-1)");
            assert_equals(storage.key(4), null, "storage.key(4)");
        }, name + ".key() should return null for out-of-range arguments.");
    }, name + ".key");

    test(function() {
        var get_keys = function(s) {
            var keys = [];
            for (var i = 0; i < s.length; ++i) {
                keys.push(s.key(i));
            }
            return keys;
        };
        var storage = window[name];
        storage.clear();

        storage.setItem("name", "user1");
        storage.setItem("age", "20");
        storage.setItem("a", "1");
        storage.setItem("b", "2");

        var expected_keys = get_keys(storage);
        storage.setItem("name", "user2");
        assert_array_equals(get_keys(storage), expected_keys);
    }, name + ".key with value changes");
});
</script>