Bug 1172562 - Clear QuotaManager storage when uninstalling an app. Test. r=bkelly
authorFernando Jimenez <ferjmoreno@gmail.com>
Thu, 02 Jul 2015 11:17:01 +0200
changeset 251133 51dc428a5250672e97231d7b36c937eeb7664bba
parent 251132 7f3f04e7425279b89fcc1a844f14a1845cd56413
child 251134 65c580e2cb9baba37cca94f701742b442d77b0b0
push id13833
push userryanvm@gmail.com
push dateThu, 02 Jul 2015 20:00:55 +0000
treeherderfx-team@98101796b275 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1172562
milestone42.0a1
Bug 1172562 - Clear QuotaManager storage when uninstalling an app. Test. r=bkelly
dom/cache/test/mochitest/app/after_clear.html
dom/cache/test/mochitest/app/before_clear.html
dom/cache/test/mochitest/app/index.html
dom/cache/test/mochitest/app/manifest.webapp
dom/cache/test/mochitest/app/manifest.webapp^headers^
dom/cache/test/mochitest/app/sw.js
dom/cache/test/mochitest/app/test.js
dom/cache/test/mochitest/mochitest.ini
dom/cache/test/mochitest/test_cache_clear_on_app_uninstall.html
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/app/after_clear.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>Test app for bug 1172562</title>
+    <script src='test.js'></script>
+    <script type='application/javascript;version=1.7'>
+
+function runTests() {
+  return Promise.resolve()
+    .then(() => { return navigator.serviceWorker.ready })
+    .then((registration) => {
+      return new Promise((resolve) => {
+        var worker = registration.waiting || registration.active;
+        worker.postMessage('read');
+        navigator.serviceWorker.onmessage = (message) => {
+          if (message.data.type == 'done') {
+            ok(!message.data.cached, 'No cached data');
+            resolve();
+          }
+        };
+      });
+    })
+    .then(done);
+}
+  </script>
+  </head>
+  <body onload='runTests()'>
+  </body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/app/before_clear.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>Test app for bug 1172562</title>
+    <script src='test.js'></script>
+    <script type='application/javascript;version=1.7'>
+
+function runTests() {
+  return Promise.resolve()
+    .then(() => { return navigator.serviceWorker.ready })
+    .then((registration) => {
+      return new Promise((resolve) => {
+        var worker = registration.waiting || registration.active;
+        worker.postMessage('write');
+        navigator.serviceWorker.onmessage = (message) => {
+          if (message.data.type == 'written') {
+            worker.postMessage('read');
+          } else if (message.data.type == 'done') {
+            ok(message.data.cached, 'Write success');
+            resolve();
+          }
+        };
+      });
+    })
+    .then(done);
+}
+  </script>
+  </head>
+  <body onload='runTests()'>
+  </body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/app/index.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>Test app for bug 1172562</title>
+    <script type='application/javascript;version=1.7'>
+function ok(aCondition, aMessage) {
+  if (aCondition) {
+    alert('OK: ' + aMessage);
+  } else {
+    alert('KO: ' + aMessage);
+  }
+}
+
+function ready() {
+  alert('READY');
+}
+
+function registerServiceWorker() {
+  return new Promise((resolve, reject) => {
+    navigator.serviceWorker.ready.then(() => {
+      ready();
+      resolve();
+    });
+    navigator.serviceWorker.register('sw.js', {scope: '.'})
+    .then(registration => {
+      ok(true, 'service worker registered');
+    })
+    .catch(reject);
+  });
+}
+
+function runTests() {
+  return Promise.resolve()
+    .then(registerServiceWorker)
+    .then(ready)
+}
+  </script>
+  </head>
+  <body onload='runTests()'>
+  </body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/app/manifest.webapp
@@ -0,0 +1,5 @@
+{
+  "name": "App",
+  "launch_path": "/index.html",
+  "description": "Test app for bug 1172562"
+}
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/app/manifest.webapp^headers^
@@ -0,0 +1,1 @@
+Content-Type: application/manifest+json
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/app/sw.js
@@ -0,0 +1,18 @@
+self.addEventListener('message', (message) => {
+  caches.open('acache').then((cache) => {
+    if(message.data == 'write') {
+      cache.add('aurl').then(() => {
+        message.source.postMessage({
+          type: 'written'
+        });
+      });
+    } else if (message.data == 'read') {
+      cache.match('aurl').then((result) => {
+        message.source.postMessage({
+          type: 'done',
+          cached: !!result
+        });
+      });
+    }
+  });
+});
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/app/test.js
@@ -0,0 +1,15 @@
+function ok(aCondition, aMessage) {
+  if (aCondition) {
+    alert('OK: ' + aMessage);
+  } else {
+    alert('KO: ' + aMessage);
+  }
+}
+
+function ready() {
+  alert('READY');
+}
+
+function done() {
+  alert('DONE');
+}
--- a/dom/cache/test/mochitest/mochitest.ini
+++ b/dom/cache/test/mochitest/mochitest.ini
@@ -18,16 +18,17 @@ support-files =
   test_cache_keys.js
   test_cache_put.js
   test_cache_requestCache.js
   test_cache_delete.js
   test_cache_put_reorder.js
   test_cache_https.js
   large_url_list.js
   empty.html
+  app/*
 
 [test_cache.html]
 [test_cache_add.html]
 [test_cache_match_request.html]
 [test_cache_matchAll_request.html]
 [test_cache_overwrite.html]
 [test_cache_match_vary.html]
 [test_caches.html]
@@ -35,11 +36,13 @@ support-files =
 [test_cache_put.html]
 [test_cache_requestCache.html]
 [test_cache_delete.html]
 [test_cache_put_reorder.html]
 [test_cache_https.html]
   skip-if = buildapp == 'b2g' # bug 1162353
 [test_cache_restart.html]
 [test_cache_shrink.html]
+[test_cache_clear_on_app_uninstall.html]
+  skip-if = e10s || buildapp == 'b2g' # bug 1178685
 [test_cache_orphaned_cache.html]
 [test_cache_orphaned_body.html]
 [test_cache_untrusted.html]
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/test_cache_clear_on_app_uninstall.html
@@ -0,0 +1,163 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Bug 1172562 - Clear QuotaManager storage when uninstalling an app</title>
+  <script type='text/javascript' src='/tests/SimpleTest/SimpleTest.js'></script>
+  <link rel='stylesheet' type='text/css' href='/tests/SimpleTest/test.css' />
+</head>
+<body onload='runTests()'>
+<p id='display'></p>
+<div id='content' style='display: none'></div>
+<pre id='test'></pre>
+<script class='testbody' type='application/javascript;version=1.7'>
+
+SimpleTest.waitForExplicitFinish();
+
+const gOrigin = 'http://mochi.test:8888/tests/dom/cache/test/mochitest/app';
+const appManifestURL = gOrigin + '/manifest.webapp';
+let gApp;
+
+function setup() {
+  return new Promise((resolve, reject) => {
+    SpecialPowers.setAllAppsLaunchable(true);
+    SpecialPowers.pushPrefEnv({'set': [
+      ['dom.mozBrowserFramesEnabled', true],
+      ['dom.serviceWorkers.exemptFromPerDomainMax', true],
+      ['dom.serviceWorkers.enabled', true],
+      ['dom.serviceWorkers.testing.enabled', true],
+      ['dom.caches.enabled', true],
+    ]}, () => {
+      SpecialPowers.pushPermissions([
+        { 'type': 'webapps-manage', 'allow': 1, 'context': document },
+        { 'type': 'browser', 'allow': 1, 'context': document },
+        { 'type': 'embed-apps', 'allow': 1, 'context': document }
+      ], () => {
+        SpecialPowers.autoConfirmAppInstall(() => {
+          SpecialPowers.autoConfirmAppUninstall(resolve);
+        });
+      });
+    });
+  });
+}
+
+function installApp() {
+  return new Promise((resolve, reject) => {
+    let req = navigator.mozApps.install(appManifestURL);
+    req.onsuccess = function() {
+      gApp = req.result;
+      is(req.result.manifestURL, appManifestURL, 'app installed');
+      if (req.result.installState == 'installed') {
+        is(req.result.installState, 'installed', 'app downloaded');
+        resolve()
+      } else {
+        req.result.ondownloadapplied = function() {
+          is(req.result.installState, 'installed', 'app downloaded');
+          resolve();
+        }
+      }
+    }
+    req.onerror = reject;
+  });
+}
+
+function launchApp() {
+  if (!gApp) {
+    ok(false, 'No test application to launch');
+    return Promise.reject();
+  }
+  return new Promise((resolve, reject) => {
+    let iframe = document.createElement('iframe');
+    iframe.setAttribute('mozbrowser', 'true');
+    iframe.setAttribute('mozapp', gApp.manifestURL);
+    iframe.addEventListener('mozbrowsershowmodalprompt', function listener(e) {
+      let message = e.detail.message;
+      if (/OK/.exec(message)) {
+        ok(true, "Message from app: " + message);
+      } else if (/KO/.exec(message)) {
+        ok(false, "Message from app: " + message);
+      } else if (/READY/.exec(message)) {
+        ok(true, "Message from app: " + message);
+        resolve();
+      } else {
+        ok(false, "Unexpected message received: " + message);
+      }
+    }, false);
+    let domParent = document.getElementById('container');
+    domParent.appendChild(iframe);
+    SpecialPowers.wrap(iframe.contentWindow).location =
+      gOrigin + gApp.manifest.launch_path;
+  });
+}
+
+function loadControlled(aUrl) {
+  return new Promise((resolve, reject) => {
+    let iframe = document.createElement('iframe');
+    iframe.setAttribute('mozbrowser', 'true');
+    iframe.setAttribute('mozapp', gApp.manifestURL);
+    iframe.addEventListener('mozbrowsershowmodalprompt', function listener(e) {
+      let message = e.detail.message;
+      if (/OK/.exec(message)) {
+        ok(true, "Message from app: " + message);
+      } else if (/KO/.exec(message)) {
+        ok(false, "Message from app: " + message);
+      } else if (/DONE/.exec(message)) {
+        ok(true, "Messaging from app complete");
+        iframe.removeEventListener('mozbrowsershowmodalprompt', listener);
+        let domParent = document.getElementById('container');
+        domParent.removeChild(iframe);
+        resolve();
+      } else {
+        ok(false, "Unexpected message received: " + message);
+      }
+      }, false);
+    let domParent = document.getElementById('container');
+    domParent.appendChild(iframe);
+    SpecialPowers.wrap(iframe.contentWindow).location =
+      gOrigin + aUrl;
+  });
+}
+
+function loadBeforeClear() {
+  return loadControlled('/before_clear.html');
+}
+
+function loadAfterClear() {
+  return loadControlled('/after_clear.html');
+}
+
+function uninstallApp() {
+  return new Promise((resolve, reject) => {
+    if (!gApp) {
+      return reject();
+    }
+    let req = navigator.mozApps.mgmt.uninstall(gApp);
+    req.onsuccess = resolve;
+    req.onerror = reject;
+  });
+}
+
+function runTests() {
+  setup()
+    .then(installApp)
+    .then(launchApp)
+    .then(loadBeforeClear)
+    .then(uninstallApp)
+    .then(installApp)
+    .then(launchApp)
+    .then(loadAfterClear)
+    .then(uninstallApp)
+    .then(SimpleTest.finish)
+    .catch((e) => {
+      ok(false, 'Unexpected error ' + e.target.error.name);
+      SimpleTest.finish();
+    });
+}
+
+</script>
+<div id='container'></div>
+</body>
+</html>