summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webstorage/set.window.js
blob: 8e671d2dedd353793da72bdc9aebed269d0d9d4d (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
["localStorage", "sessionStorage"].forEach(function(name) {
    [9, "x"].forEach(function(key) {
        test(function() {
            var expected = "value for " + this.name;
            var value = expected;

            var storage = window[name];
            storage.clear();

            assert_equals(storage[key], undefined);
            assert_equals(storage.getItem(key), null);
            assert_equals(storage[key] = value, value);
            assert_equals(storage[key], expected);
            assert_equals(storage.getItem(key), expected);
        }, "Setting property for key " + key + " on " + name);

        test(function() {
            var expected = "value for " + this.name;
            var value = {
                toString: function() { return expected; }
            };

            var storage = window[name];
            storage.clear();

            assert_equals(storage[key], undefined);
            assert_equals(storage.getItem(key), null);
            assert_equals(storage[key] = value, value);
            assert_equals(storage[key], expected);
            assert_equals(storage.getItem(key), expected);
        }, "Setting property with toString for key " + key + " on " + name);

        test(function() {
            var proto = "proto for " + this.name;
            Storage.prototype[key] = proto;
            this.add_cleanup(function() { delete Storage.prototype[key]; });

            var value = "value for " + this.name;

            var storage = window[name];
            storage.clear();

            assert_equals(storage[key], proto);
            assert_equals(storage.getItem(key), null);
            assert_equals(storage[key] = value, value);
            // Hidden because no [LegacyOverrideBuiltIns].
            assert_equals(storage[key], proto);
            assert_equals(Object.getOwnPropertyDescriptor(storage, key), undefined);
            assert_equals(storage.getItem(key), value);
        }, "Setting property for key " + key + " on " + name + " with data property on prototype");

        test(function() {
            var proto = "proto for " + this.name;
            Storage.prototype[key] = proto;
            this.add_cleanup(function() { delete Storage.prototype[key]; });

            var value = "value for " + this.name;
            var existing = "existing for " + this.name;

            var storage = window[name];
            storage.clear();

            storage.setItem(key, existing);

            // Hidden because no [LegacyOverrideBuiltIns].
            assert_equals(storage[key], proto);
            assert_equals(Object.getOwnPropertyDescriptor(storage, key), undefined);
            assert_equals(storage.getItem(key), existing);
            assert_equals(storage[key] = value, value);
            assert_equals(storage[key], proto);
            assert_equals(Object.getOwnPropertyDescriptor(storage, key), undefined);
            assert_equals(storage.getItem(key), value);
        }, "Setting property for key " + key + " on " + name + " with data property on prototype and existing item");

        test(function() {
            var storage = window[name];
            storage.clear();

            var proto = "proto getter for " + this.name;
            Object.defineProperty(Storage.prototype, key, {
                "get": function() { return proto; },
                "set": this.unreached_func("Should not call [[Set]] on prototype"),
                "configurable": true,
            });
            this.add_cleanup(function() {
                delete Storage.prototype[key];
                delete storage[key];
                assert_false(key in storage);
            });

            var value = "value for " + this.name;

            assert_equals(storage[key], proto);
            assert_equals(storage.getItem(key), null);
            assert_equals(storage[key] = value, value);
            // Property is hidden because no [LegacyOverrideBuiltIns].
            assert_equals(storage[key], proto);
            assert_equals(Object.getOwnPropertyDescriptor(storage, key), undefined);
            assert_equals(storage.getItem(key), value);
        }, "Setting property for key " + key + " on " + name + " with accessor property on prototype");
    });
});