|
1 <html xmlns="http://www.w3.org/1999/xhtml"> |
|
2 <head> |
|
3 <title>Test localStorage usage while in a low device storage situation</title> |
|
4 |
|
5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
6 <script type="text/javascript" src="localStorageCommon.js"></script> |
|
7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
8 |
|
9 <script type="text/javascript"> |
|
10 |
|
11 /* |
|
12 This test does the following: |
|
13 - Stores an item in localStorage. |
|
14 - Checks the stored value. |
|
15 - Emulates a low device storage situation. |
|
16 - Gets the stored item again. |
|
17 - Removes the stored item. |
|
18 - Fails storing a new value. |
|
19 - Emulates recovering from a low device storage situation. |
|
20 - Stores a new value. |
|
21 - Checks the stored value. |
|
22 */ |
|
23 |
|
24 function lowDeviceStorage(lowStorage) { |
|
25 var data = lowStorage ? "full" : "free"; |
|
26 os().notifyObservers(null, "disk-space-watcher", data); |
|
27 } |
|
28 |
|
29 function startTest() { |
|
30 // Add a test item. |
|
31 localStorage.setItem("item", "value"); |
|
32 is(localStorage.getItem("item"), "value", "getItem()"); |
|
33 |
|
34 // Emulates a low device storage situation. |
|
35 lowDeviceStorage(true); |
|
36 |
|
37 // Checks that we can still access to the stored item. |
|
38 is(localStorage.getItem("item"), "value", |
|
39 "getItem() during a device storage situation"); |
|
40 |
|
41 // Removes the stored item. |
|
42 localStorage.removeItem("item"); |
|
43 is(localStorage.getItem("item"), undefined, |
|
44 "getItem() after removing the item"); |
|
45 |
|
46 // Fails storing a new item. |
|
47 try { |
|
48 localStorage.setItem("newItem", "value"); |
|
49 ok(false, "Storing a new item is expected to fail"); |
|
50 } catch(e) { |
|
51 ok(true, "Got an expected exception " + e); |
|
52 } finally { |
|
53 is(localStorage.getItem("newItem"), undefined, |
|
54 "setItem while device storage is low"); |
|
55 } |
|
56 |
|
57 // Emulates recovering from a low device storage situation. |
|
58 lowDeviceStorage(false); |
|
59 |
|
60 // Add a test item after recovering from the low device storage situation. |
|
61 localStorage.setItem("newItem", "value"); |
|
62 is(localStorage.getItem("newItem"), "value", |
|
63 "getItem() with available storage"); |
|
64 |
|
65 SimpleTest.finish(); |
|
66 } |
|
67 |
|
68 SimpleTest.waitForExplicitFinish(); |
|
69 |
|
70 </script> |
|
71 |
|
72 </head> |
|
73 |
|
74 <body onload="startTest();"> |
|
75 </body> |
|
76 </html> |