Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 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(undefined, storage.getItem("foo"), | |
| 27 "getItem('foo') != undefined prior to addition"); | |
| 28 checkEqual(undefined, storage.key(0), | |
| 29 "key(0) != undefined 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(undefined, storage.getItem("foo"), | |
| 43 "getItem('foo') != undefined 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 try { | |
| 61 storage.setItem("tooLarge", makeLargeString((5 * 1024 * 1024) + 1)); | |
| 62 throw "failed to throw execption for very large value"; | |
| 63 } catch(ex) { | |
| 64 checkEqual(ex.code, 22, | |
| 65 "ex.code != 22 for attempt to store a very large value"); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 function checkEqual(lhs, rhs, errorMessage) { | |
| 70 if (lhs != rhs) | |
|
jsbell
2012/04/04 18:37:56
Nit: I'd prefer the stricter !== comparison, which
michaeln
2012/04/04 19:03:21
Right, some of my checkEquals should be testing ag
| |
| 71 throw errorMessage; | |
| 72 } | |
| 73 | |
| 74 function makeLargeString(minimumSize) { | |
| 75 var x = "a large string of gibberish_"; | |
| 76 while (x.length < minimumSize) | |
|
jsbell
2012/04/04 18:37:56
Per dgrogan's advice to me, watch out for this sty
michaeln
2012/04/04 19:03:21
At the sizes i'm working with, i haven't noticed t
| |
| 77 x = x.concat(x); | |
| 78 return x; | |
| 79 } | |
| OLD | NEW |