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. 4 5 function startTestSoon() { 6 window.setTimeout(test, 0); 7 } 8 9 function test() { 10 try { 11 debug('Checking window.localStorage'); 12 sanityCheck(window.localStorage); 13 debug('Checking window.sessionStorage'); 14 sanityCheck(window.sessionStorage); 15 done(); 16 } catch(e) { 17 fail(e); 18 } 19 } 20 21 function sanityCheck(storage) { 22 storage.clear(); 23 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"); 30 31 storage.setItem("foo", "bar"); 32 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"); 39 40 storage.removeItem("foo"); 41 42 checkEqual(null, storage.getItem("foo"), 43 "getItem('foo') != null after removal"); 44 45 storage["foo"] = "baz"; 46 storage["name"] = "value"; 47 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"); 54 55 storage.clear(); 56 57 checkEqual(0, storage.length, 58 "storage.length != 0 after clear"); 59 60 var tooLarge = makeLargeString((5 * 1024 * 1024) + 1); 61 try { 62 storage.setItem("tooLarge", tooLarge); 63 throw "failed to throw exception for very large value"; 64 } catch(ex) { 65 checkEqual(ex.code, 22, 66 "ex.code != 22 for attempt to store a very large value"); 67 } 68 try { 69 storage.setItem(tooLarge, "key is too large"); 70 throw "failed to throw exception for very large key"; 71 } catch(ex) { 72 checkEqual(ex.code, 22, 73 "ex.code != 22 for attempt to store a very large key"); 74 } 75 } 76 77 function checkEqual(lhs, rhs, errorMessage) { 78 if (lhs !== rhs) 79 throw errorMessage; 80 } 81 82 function makeLargeString(minimumSize) { 83 return Array(minimumSize).join("X"); 84 } 85