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