1description("Test dom storage with many different types of keys (as opposed to values)"); 2 3function test(storageString) 4{ 5 storage = eval(storageString); 6 if (!storage) { 7 testFailed(storageString + " DOES NOT exist"); 8 return; 9 } 10 11 debug("Testing " + storageString); 12 13 evalAndLog("storage.clear()"); 14 shouldBe("storage.length", "0"); 15 16 debug(""); 17 shouldBeNull("storage.getItem('FOO')"); 18 evalAndLog("storage.setItem('FOO', 'BAR')"); 19 shouldBe("storage.length", "1"); 20 21 shouldBeEqualToString("storage.getItem('FOO')", "BAR"); 22 shouldBeNull("storage.getItem('foo')"); 23 shouldBeUndefined("storage.foo"); 24 shouldBeUndefined("storage['foo']"); 25 26 evalAndLog("storage.foo = 'x'"); 27 shouldBeEqualToString("storage.foo", "x"); 28 shouldBeEqualToString("storage['foo']", "x"); 29 shouldBeEqualToString("storage.getItem('foo')", "x"); 30 evalAndLog("storage['foo'] = 'y'"); 31 shouldBeEqualToString("storage.foo", "y"); 32 shouldBeEqualToString("storage['foo']", "y"); 33 shouldBeEqualToString("storage.getItem('foo')", "y"); 34 evalAndLog("storage.setItem('foo', 'z')"); 35 shouldBeEqualToString("storage.foo", "z"); 36 shouldBeEqualToString("storage['foo']", "z"); 37 shouldBeEqualToString("storage.getItem('foo')", "z"); 38 shouldBe("storage.length", "2"); 39 40 debug(""); 41 debug("Testing a null key"); 42 evalAndLog("storage.setItem(null, 'asdf')"); 43 shouldBeEqualToString("storage.getItem('null')", "asdf"); 44 shouldBeEqualToString("storage.getItem(null)", "asdf"); 45 shouldBeEqualToString("storage['null']", "asdf"); 46 shouldBeEqualToString("storage[null]", "asdf"); 47 shouldBe("storage.length", "3"); 48 49 evalAndLog("storage[null] = 1"); 50 shouldBeEqualToString("storage.getItem(null)", "1"); 51 evalAndLog("storage['null'] = 2"); 52 shouldBeEqualToString("storage.getItem(null)", "2"); 53 evalAndLog("storage.setItem('null', 3)"); 54 shouldBeEqualToString("storage.getItem(null)", "3"); 55 shouldBe("storage.length", "3"); 56 57 debug(""); 58 debug("Testing an undefined key"); 59 evalAndLog("storage[undefined] = 'xyz'"); 60 shouldBeEqualToString("storage.getItem('undefined')", "xyz"); 61 shouldBeEqualToString("storage.getItem(undefined)", "xyz"); 62 shouldBeEqualToString("storage['undefined']", "xyz"); 63 shouldBeEqualToString("storage[undefined]", "xyz"); 64 shouldBe("storage.length", "4"); 65 66 evalAndLog("storage['undefined'] = 4"); 67 shouldBeEqualToString("storage.getItem(undefined)", "4"); 68 evalAndLog("storage.setItem(undefined, 5)"); 69 shouldBeEqualToString("storage.getItem(undefined)", "5"); 70 evalAndLog("storage.setItem('undefined', 6)"); 71 shouldBeEqualToString("storage.getItem(undefined)", "6"); 72 shouldBe("storage.length", "4"); 73 74 debug(""); 75 debug("Testing a numeric key"); 76 evalAndLog("storage['2'] = 'ppp'"); 77 shouldBeEqualToString("storage.getItem('2')", "ppp"); 78 shouldBeEqualToString("storage.getItem(2)", "ppp"); 79 shouldBeEqualToString("storage['2']", "ppp"); 80 shouldBeEqualToString("storage[2]", "ppp"); 81 shouldBe("storage.length", "5"); 82 83 evalAndLog("storage[2] = 7"); 84 shouldBeEqualToString("storage.getItem(2)", "7"); 85 evalAndLog("storage.setItem(2, 8)"); 86 shouldBeEqualToString("storage.getItem(2)", "8"); 87 evalAndLog("storage.setItem('2', 9)"); 88 shouldBeEqualToString("storage.getItem(2)", "9"); 89 shouldBe("storage.length", "5"); 90 91 debug(""); 92 debug("Setting a non-ascii string to foo"); 93 k = String.fromCharCode(255425) + String.fromCharCode(255) + String.fromCharCode(2554252321) + String.fromCharCode(0) + 'hello'; 94 evalAndLog("storage[k] = 'hello'"); 95 shouldBeEqualToString("storage.getItem(k)", "hello"); 96 shouldBeEqualToString("storage[k]", "hello"); 97 shouldBe("storage.length", "6"); 98 99 debug(""); 100 debug("Testing case differences"); 101 evalAndLog("storage.foo1 = 'lower1'"); 102 evalAndLog("storage.FOO1 = 'UPPER1'"); 103 evalAndLog("storage['foo2'] = 'lower2'"); 104 evalAndLog("storage['FOO2'] = 'UPPER2'"); 105 evalAndLog("storage.setItem('foo3', 'lower3')"); 106 evalAndLog("storage.setItem('FOO3', 'UPPER3')"); 107 shouldBeEqualToString("storage.foo1", "lower1"); 108 shouldBeEqualToString("storage.FOO1", "UPPER1"); 109 shouldBeEqualToString("storage['foo2']", "lower2"); 110 shouldBeEqualToString("storage['FOO2']", "UPPER2"); 111 shouldBeEqualToString("storage.getItem('foo3')", "lower3"); 112 shouldBeEqualToString("storage.getItem('FOO3')", "UPPER3"); 113 shouldBe("storage.length", "12"); 114 115 116 debug(""); 117 debug("Testing overriding length"); 118 shouldBe("storage.length", "12"); 119 shouldBe("storage['length']", "12"); 120 shouldBeNull("storage.getItem('length')"); 121 122 evalAndLog("storage.length = 0"); 123 shouldBe("storage.length", "12"); 124 shouldBe("storage['length']", "12"); 125 shouldBeNull("storage.getItem('length')"); 126 127 evalAndLog("storage['length'] = 0"); 128 shouldBe("storage.length", "12"); 129 shouldBe("storage['length']", "12"); 130 shouldBeNull("storage.getItem('length')"); 131 132 evalAndLog("storage.setItem('length', 0)"); 133 shouldBe("storage.length", "13"); 134 shouldBe("storage['length']", "13"); 135 shouldBeEqualToString("storage.getItem('length')", "0"); 136 137 evalAndLog("storage.removeItem('length')"); 138 shouldBe("storage.length", "12"); 139 shouldBe("storage['length']", "12"); 140 shouldBeNull("storage.getItem('length')"); 141 142 evalAndLog("storage.setItem('length', 0)"); 143 shouldBe("storage.length", "13"); 144} 145 146test("sessionStorage"); 147debug(""); 148debug(""); 149test("localStorage"); 150 151window.successfullyParsed = true; 152isSuccessfullyParsed(); 153