1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 function startTestSoon() {
6 window.setTimeout(test, 0);
11 debug('Checking window.localStorage');
12 sanityCheck(window.localStorage);
13 debug('Checking window.sessionStorage');
14 sanityCheck(window.sessionStorage);
21 function sanityCheck(storage) {
24 checkEqual(0, storage.length,
25 "storage.length != 0 at start");
26 checkEqual(null, storage.getItem("foo"),
27 "getItem('foo') != null prior to addition");
28 checkEqual(null, storage.key(0),
29 "key(0) != null prior to addition");
31 storage.setItem("foo", "bar");
33 checkEqual(1, storage.length,
34 "storage.length != 1 after addition");
35 checkEqual("bar", storage.getItem("foo"),
36 "getItem('foo') != 'bar' after addition");
37 checkEqual("foo", storage.key(0),
38 "key(0) != 'foo' after addition");
40 storage.removeItem("foo");
42 checkEqual(null, storage.getItem("foo"),
43 "getItem('foo') != null after removal");
45 storage["foo"] = "baz";
46 storage["name"] = "value";
48 checkEqual(2, storage.length,
49 "storage.length != 2 after 2 additions");
50 checkEqual("baz", storage["foo"],
51 "storage['foo'] != 'baz' after addition");
52 checkEqual("value", storage["name"],
53 "storage['name'] != 'value' after addition");
57 checkEqual(0, storage.length,
58 "storage.length != 0 after clear");
60 var tooLarge = makeLargeString((5 * 1024 * 1024) + 1);
62 storage.setItem("tooLarge", tooLarge);
63 throw "failed to throw exception for very large value";
65 checkEqual(ex.code, 22,
66 "ex.code != 22 for attempt to store a very large value");
69 storage.setItem(tooLarge, "key is too large");
70 throw "failed to throw exception for very large key";
72 checkEqual(ex.code, 22,
73 "ex.code != 22 for attempt to store a very large key");
77 function checkEqual(lhs, rhs, errorMessage) {
82 function makeLargeString(minimumSize) {
83 return Array(minimumSize).join("X");