1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/localstorage/test_lowDeviceStorage.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,76 @@ 1.4 +<html xmlns="http://www.w3.org/1999/xhtml"> 1.5 +<head> 1.6 +<title>Test localStorage usage while in a low device storage situation</title> 1.7 + 1.8 +<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.9 +<script type="text/javascript" src="localStorageCommon.js"></script> 1.10 +<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.11 + 1.12 +<script type="text/javascript"> 1.13 + 1.14 +/* 1.15 +This test does the following: 1.16 +- Stores an item in localStorage. 1.17 +- Checks the stored value. 1.18 +- Emulates a low device storage situation. 1.19 +- Gets the stored item again. 1.20 +- Removes the stored item. 1.21 +- Fails storing a new value. 1.22 +- Emulates recovering from a low device storage situation. 1.23 +- Stores a new value. 1.24 +- Checks the stored value. 1.25 +*/ 1.26 + 1.27 +function lowDeviceStorage(lowStorage) { 1.28 + var data = lowStorage ? "full" : "free"; 1.29 + os().notifyObservers(null, "disk-space-watcher", data); 1.30 +} 1.31 + 1.32 +function startTest() { 1.33 + // Add a test item. 1.34 + localStorage.setItem("item", "value"); 1.35 + is(localStorage.getItem("item"), "value", "getItem()"); 1.36 + 1.37 + // Emulates a low device storage situation. 1.38 + lowDeviceStorage(true); 1.39 + 1.40 + // Checks that we can still access to the stored item. 1.41 + is(localStorage.getItem("item"), "value", 1.42 + "getItem() during a device storage situation"); 1.43 + 1.44 + // Removes the stored item. 1.45 + localStorage.removeItem("item"); 1.46 + is(localStorage.getItem("item"), undefined, 1.47 + "getItem() after removing the item"); 1.48 + 1.49 + // Fails storing a new item. 1.50 + try { 1.51 + localStorage.setItem("newItem", "value"); 1.52 + ok(false, "Storing a new item is expected to fail"); 1.53 + } catch(e) { 1.54 + ok(true, "Got an expected exception " + e); 1.55 + } finally { 1.56 + is(localStorage.getItem("newItem"), undefined, 1.57 + "setItem while device storage is low"); 1.58 + } 1.59 + 1.60 + // Emulates recovering from a low device storage situation. 1.61 + lowDeviceStorage(false); 1.62 + 1.63 + // Add a test item after recovering from the low device storage situation. 1.64 + localStorage.setItem("newItem", "value"); 1.65 + is(localStorage.getItem("newItem"), "value", 1.66 + "getItem() with available storage"); 1.67 + 1.68 + SimpleTest.finish(); 1.69 +} 1.70 + 1.71 +SimpleTest.waitForExplicitFinish(); 1.72 + 1.73 +</script> 1.74 + 1.75 +</head> 1.76 + 1.77 +<body onload="startTest();"> 1.78 +</body> 1.79 +</html>