summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webstorage/event_basic.js
blob: fe8446cec6a23f37f974032d7d26d62dd6125b94 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
testStorages(function(storageString) {
    async_test(function(t) {
        assert_true(storageString in window, storageString + " exist");
        var storage = window[storageString];
        t.add_cleanup(function() { storage.clear() });

        clearStorage(storageString, t.step_func(step1));
        assert_equals(storage.length, 0, "storage.length");

        function step1(msg)
        {
            storage.setItem('FOO', 'BAR');

            runAfterNStorageEvents(t.step_func(step2), 1);
        }

        function step2(msg)
        {
            if(msg != undefined) {
                assert_unreached(msg);
            }
            assert_equals(storageEventList.length, 1);
            assert_equals(storageEventList[0].key, "FOO");
            assert_equals(storageEventList[0].oldValue, null);
            assert_equals(storageEventList[0].newValue, "BAR");

            storage.setItem('FU', 'BAR');
            storage.setItem('a', '1');
            storage.setItem('b', '2');
            storage.setItem('b', '3');

            runAfterNStorageEvents(t.step_func(step3), 5);
        }

        function step3(msg)
        {
            if(msg != undefined) {
                assert_unreached(msg);
            }
            assert_equals(storageEventList.length, 5);
            assert_equals(storageEventList[1].key, "FU");
            assert_equals(storageEventList[1].oldValue, null);
            assert_equals(storageEventList[1].newValue, "BAR");

            assert_equals(storageEventList[2].key, "a");
            assert_equals(storageEventList[2].oldValue, null);
            assert_equals(storageEventList[2].newValue, "1");

            assert_equals(storageEventList[3].key, "b");
            assert_equals(storageEventList[3].oldValue, null);
            assert_equals(storageEventList[3].newValue, "2");

            assert_equals(storageEventList[4].key, "b");
            assert_equals(storageEventList[4].oldValue, "2");
            assert_equals(storageEventList[4].newValue, "3");

            storage.removeItem('FOO');

            runAfterNStorageEvents(t.step_func(step4), 6);
        }

        function step4(msg)
        {
            if(msg != undefined) {
                assert_unreached(msg);
            }
            assert_equals(storageEventList.length, 6);
            assert_equals(storageEventList[5].key, "FOO");
            assert_equals(storageEventList[5].oldValue, "BAR");
            assert_equals(storageEventList[5].newValue, null);

            storage.removeItem('FU');

            runAfterNStorageEvents(t.step_func(step5), 7);
        }

        function step5(msg)
        {
            if(msg != undefined) {
                assert_unreached(msg);
            }
            assert_equals(storageEventList.length, 7);
            assert_equals(storageEventList[6].key, "FU");
            assert_equals(storageEventList[6].oldValue, "BAR");
            assert_equals(storageEventList[6].newValue, null);

            storage.clear();

            runAfterNStorageEvents(t.step_func(step6), 8);
        }

        function step6(msg)
        {
            if(msg != undefined) {
                assert_unreached(msg);
            }
            assert_equals(storageEventList.length, 8);
            assert_equals(storageEventList[7].key, null);
            assert_equals(storageEventList[7].oldValue, null);
            assert_equals(storageEventList[7].newValue, null);

            t.done();
        }

    }, storageString + " mutations fire StorageEvents that are caught by the event listener set via window.onstorage.");
});