storage_enumerate.window.js (2157B)
1 ["localStorage", "sessionStorage"].forEach(function(name) { 2 test(function() { 3 assert_true(name in window, name + " exist"); 4 5 var storage = window[name]; 6 storage.clear(); 7 8 Storage.prototype.prototypeTestKey = "prototypeTestValue"; 9 storage.foo = "bar"; 10 storage.fu = "baz"; 11 storage.batman = "bin suparman"; 12 storage.bar = "foo"; 13 storage.alpha = "beta"; 14 storage.zeta = "gamma"; 15 16 const enumeratedArray = Object.keys(storage); 17 enumeratedArray.sort(); // Storage order is implementation-defined. 18 19 const expectArray = ["alpha", "bar", "batman", "foo", "fu", "zeta"]; 20 assert_array_equals(enumeratedArray, expectArray); 21 22 // 'prototypeTestKey' is not an actual storage key, it is just a 23 // property set on Storage's prototype object. 24 assert_equals(storage.length, 6); 25 assert_equals(storage.getItem("prototypeTestKey"), null); 26 assert_equals(storage.prototypeTestKey, "prototypeTestValue"); 27 }, name + ": enumerate a Storage object and get only the keys as a result and the built-in properties of the Storage object should be ignored"); 28 29 test(function() { 30 const storage = window[name]; 31 storage.clear(); 32 33 storage.setItem("foo", "bar"); 34 storage.baz = "quux"; 35 storage.setItem(0, "alpha"); 36 storage[42] = "beta"; 37 38 for (let prop in storage) { 39 if (!storage.hasOwnProperty(prop)) 40 continue; 41 const desc = Object.getOwnPropertyDescriptor(storage, prop); 42 assert_true(desc.configurable); 43 assert_true(desc.enumerable); 44 assert_true(desc.writable); 45 } 46 47 const keys = Object.keys(storage); 48 keys.sort(); // Storage order is implementation-defined. 49 assert_array_equals(keys, ["0", "42", "baz", "foo"]); 50 51 const values = Object.values(storage); 52 values.sort(); // Storage order is implementation-defined. 53 assert_array_equals(values, ["alpha", "bar", "beta", "quux"]); 54 }, name + ": test enumeration of numeric and non-numeric keys"); 55 });