Bug 861894 - Avoid apps to schedule new offline cache downloads while device free space is low. Tests. r=honzab
authorFernando Jiménez <ferjmoreno@gmail.com>
Fri, 10 May 2013 16:16:56 +0200
changeset 138447 6380de7328874e88b380b54c9cb8f1861360cf56
parent 138446 044d554846ff020bebc1503a5e00fdd6c21fd97e
child 138448 feabd8f7ae0fbd3361d0bb4a1c85322e4f007ec1
push idunknown
push userunknown
push dateunknown
reviewershonzab
bugs861894
milestone23.0a1
Bug 861894 - Avoid apps to schedule new offline cache downloads while device free space is low. Tests. r=honzab
dom/tests/mochitest/ajax/offline/Makefile.in
dom/tests/mochitest/ajax/offline/test_lowDeviceStorage.html
dom/tests/mochitest/ajax/offline/test_lowDeviceStorageDuringUpdate.html
testing/mochitest/b2g.json
--- a/dom/tests/mochitest/ajax/offline/Makefile.in
+++ b/dom/tests/mochitest/ajax/offline/Makefile.in
@@ -90,13 +90,15 @@ MOCHITEST_FILES	= \
 	changing1Hour.sjs \
 	changingManifest.sjs \
 	offlineChild.html \
 	test_xhtmlManifest.xhtml \
 	test_missingManifest.html \
 	missing.html \
 	jupiter.jpg \
 	test_cancelOfflineCache.html \
+	test_lowDeviceStorage.html \
+	test_lowDeviceStorageDuringUpdate.html \
 	$(NULL)
 
 # test_offlineMode.html disabled due to bug 656943
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/dom/tests/mochitest/ajax/offline/test_lowDeviceStorage.html
@@ -0,0 +1,91 @@
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<title>Low device storage</title>
+
+<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+<script type="text/javascript" src="/tests/dom/tests/mochitest/ajax/offline/offlineTests.js"></script>
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+
+<script type="text/javascript">
+
+/**
+ * This test checks that an offline cache update scheduled *after* a low device
+ * storage situation appears is canceled. It basically does:
+ *
+ * 1. Notifies to the offline cache update service about a fake
+ *    low device storage situation.
+ * 2. Schedules an update and observes for its notifications.
+ * 3. We are supposed to recieve an error event notifying about the cancelation
+ *    of the update because of the low storage situation.
+ * 4. Notifies to the offline cache update service that we've recovered from
+ *    the low storage situation.
+ */
+
+var updateService = SpecialPowers.Cc['@mozilla.org/offlinecacheupdate-service;1']
+                                 .getService(Ci.nsIOfflineCacheUpdateService);
+
+var obs = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
+                       .getService(SpecialPowers.Ci.nsIObserverService);
+
+var errorReceived = false;
+
+function finish() {
+  obs.notifyObservers(updateService, "disk-space-watcher", "free");
+
+  OfflineTest.teardown();
+  OfflineTest.finish();
+}
+
+if (OfflineTest.setup()) {
+  obs.notifyObservers(updateService, "disk-space-watcher", "full");
+
+  var updateObserver = {
+    updateStateChanged: function (aUpdate, aState) {
+      switch(aState) {
+        case Ci.nsIOfflineCacheUpdateObserver.STATE_ERROR:
+          aUpdate.removeObserver(this);
+          errorReceived = true;
+          OfflineTest.ok(true, "Expected error. Update canceled");
+          finish();
+        break;
+        case Ci.nsIOfflineCacheUpdateObserver.STATE_FINISHED:
+          OfflineTest.ok(errorReceived,
+                         "Finished after receiving the expected error");
+          finish();
+        break;
+        case Ci.nsIOfflineCacheUpdateObserver.STATE_NOUPDATE:
+          aUpdate.removeObserver(this);
+          OfflineTest.ok(false, "No update");
+          finish();
+          break;
+        case Ci.nsIOfflineCacheUpdateObserver.STATE_DOWNLOADING:
+        case Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMSTARTED:
+        case Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMPROGRESS:
+          aUpdate.removeObserver(this);
+          OfflineTest.ok(false, "The update was supposed to be canceled");
+          finish();
+          break;
+      }
+    },
+    applicationCacheAvailable: function() {}
+  };
+
+  var manifest = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/simpleManifest.cacheManifest";
+  var ioService = Cc["@mozilla.org/network/io-service;1"]
+                  .getService(Ci.nsIIOService);
+  var manifestURI = ioService.newURI(manifest, null, null);
+  var documentURI = ioService.newURI(document.documentURI, null, null);
+  var update = updateService.scheduleUpdate(manifestURI, documentURI, window);
+  update.addObserver(updateObserver, false);
+}
+
+SimpleTest.waitForExplicitFinish();
+
+</script>
+
+</head>
+
+<body>
+
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/tests/mochitest/ajax/offline/test_lowDeviceStorageDuringUpdate.html
@@ -0,0 +1,59 @@
+<html xmlns="http://www.w3.org/1999/xhtml" manifest="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/simpleManifest.cacheManifest">
+<head>
+<title>Low device storage during update</title>
+
+<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+<script type="text/javascript" src="/tests/dom/tests/mochitest/ajax/offline/offlineTests.js"></script>
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+
+<script type="text/javascript">
+
+/**
+ * This test checks that an offline cache update is canceled when a low device
+ * storage condition is detected during the update.
+ */
+
+var updateService = Cc['@mozilla.org/offlinecacheupdate-service;1']
+                    .getService(Ci.nsIOfflineCacheUpdateService);
+
+var obs = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
+                       .getService(SpecialPowers.Ci.nsIObserverService);
+
+function finish() {
+  obs.notifyObservers(updateService, "disk-space-watcher", "free");
+
+  OfflineTest.teardown();
+  OfflineTest.finish();
+}
+
+function onError() {
+  OfflineTest.ok(true, "Expected error: Update canceled");
+  finish();
+}
+
+function onUnexpectedEvent() {
+  OfflineTest.ok(false, "The update was supposed to be canceled");
+  finish();
+}
+
+function onChecking() {
+  obs.notifyObservers(updateService, "disk-space-watcher", "full");
+}
+
+if (OfflineTest.setup()) {
+  applicationCache.onerror = OfflineTest.priv(onError);
+  applicationCache.onprogress = OfflineTest.priv(onUnexpectedEvent);
+  applicationCache.oncached = OfflineTest.priv(onUnexpectedEvent);
+  applicationCache.onchecking = OfflineTest.priv(onChecking);
+}
+
+SimpleTest.waitForExplicitFinish();
+
+</script>
+
+</head>
+
+<body>
+
+</body>
+</html>
--- a/testing/mochitest/b2g.json
+++ b/testing/mochitest/b2g.json
@@ -220,16 +220,18 @@
     "dom/tests/mochitest/ajax/offline/test_bug744719.html":"",
     "dom/tests/mochitest/ajax/offline/test_bug765203.html":"",
     "dom/tests/mochitest/ajax/offline/test_bypass.html":"",
     "dom/tests/mochitest/ajax/offline/test_cancelOfflineCache.html":"",
     "dom/tests/mochitest/ajax/offline/test_changingManifest.html":"",
     "dom/tests/mochitest/ajax/offline/test_fallback.html":"",
     "dom/tests/mochitest/ajax/offline/test_foreign.html":"",
     "dom/tests/mochitest/ajax/offline/test_identicalManifest.html":"",
+    "dom/tests/mochitest/ajax/offline/test_lowDeviceStorage.html":"",
+    "dom/tests/mochitest/ajax/offline/test_lowDeviceStorageDuringUpdate.html":"",
     "dom/tests/mochitest/ajax/offline/test_missingFile.html":"",
     "dom/tests/mochitest/ajax/offline/test_missingManifest.html":"",
     "dom/tests/mochitest/ajax/offline/test_obsolete.html":"",
     "dom/tests/mochitest/ajax/offline/test_offlineIFrame.html":"",
     "dom/tests/mochitest/ajax/offline/test_overlap.html":"",
     "dom/tests/mochitest/ajax/offline/test_redirectManifest.html":"",
     "dom/tests/mochitest/ajax/offline/test_redirectUpdateItem.html":"",
     "dom/tests/mochitest/ajax/offline/test_refetchManifest.html":"",