Bug 1169249 - Unregister service worker registration when uninstalling a service-worker-enabled application. Tests. r=baku
authorFernando Jimenez <ferjmoreno@gmail.com>
Thu, 11 Jun 2015 19:42:38 +0200
changeset 248373 3257bc1b54987843b019009412bf7ea6722dfecb
parent 248372 82951c8ba8c53a3d7b1707bdff3508594e8391a3
child 248374 536bd9910bc2bb0dfa2997a16eaf5ba80a4148c4
push id28893
push userkwierso@gmail.com
push dateFri, 12 Jun 2015 00:02:58 +0000
treeherderautoland@8cf9d3e497f9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1169249
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1169249 - Unregister service worker registration when uninstalling a service-worker-enabled application. Tests. r=baku
dom/workers/moz.build
dom/workers/test/serviceworkers/app/index.html
dom/workers/test/serviceworkers/app/manifest.webapp
dom/workers/test/serviceworkers/app/manifest.webapp^headers^
dom/workers/test/serviceworkers/app/sw.js
dom/workers/test/serviceworkers/chrome.ini
dom/workers/test/serviceworkers/test_app_installation.html
--- a/dom/workers/moz.build
+++ b/dom/workers/moz.build
@@ -112,15 +112,18 @@ TEST_DIRS += [
     'test/extensions/traditional',
 ]
 
 MOCHITEST_MANIFESTS += [
     'test/mochitest.ini',
     'test/serviceworkers/mochitest.ini',
 ]
 
-MOCHITEST_CHROME_MANIFESTS += ['test/chrome.ini']
+MOCHITEST_CHROME_MANIFESTS += [
+    'test/chrome.ini',
+    'test/serviceworkers/chrome.ini'
+]
 
 XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell/xpcshell.ini']
 
 BROWSER_CHROME_MANIFESTS += ['test/browser.ini']
 
 TEST_DIRS += ['test/gtest']
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/app/index.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>Test app for bug 1169249</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/workers/test/serviceworkers/app/manifest.webapp
@@ -0,0 +1,5 @@
+{
+  "name": "App",
+  "launch_path": "/index.html",
+  "description": "Test app for bug 1169249"
+}
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/app/manifest.webapp^headers^
@@ -0,0 +1,1 @@
+Content-Type: application/manifest+json
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/app/sw.js
@@ -0,0 +1,1 @@
+// Useless service worker.
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/chrome.ini
@@ -0,0 +1,6 @@
+[DEFAULT]
+skip-if = buildapp == 'b2g'
+support-files =
+  app/*
+
+[test_app_installation.html]
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/test_app_installation.html
@@ -0,0 +1,167 @@
+<!DOCTYPE html>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id={1169249}
+-->
+<head>
+  <title>Test for Bug {1169249}</title>
+  <script type="application/javascript"
+          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript"
+          src="chrome://mochikit/content/chrome-harness.js"></script>
+  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
+</head>
+<body>
+
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id={1169249}">Mozilla Bug {1169249}</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="application/javascript;version=1.7">
+
+const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+XPCOMUtils.defineLazyServiceGetter(this, "gServiceWorkerManager",
+                                   "@mozilla.org/serviceworkers/manager;1",
+                                   "nsIServiceWorkerManager");
+
+XPCOMUtils.defineLazyServiceGetter(this, "gAppsService",
+                                   "@mozilla.org/AppsService;1",
+                                   "nsIAppsService");
+SimpleTest.waitForExplicitFinish();
+
+const gOrigin = 'http://test/chrome/dom/workers/test/serviceworkers/app';
+const appManifestURL = gOrigin + '/manifest.webapp';
+let gApp;
+
+addLoadEvent(go);
+
+function setup() {
+  info('Setting up');
+  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]
+    ]}, () => {
+      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 checkSwRegistration(aExpectedRegistrations) {
+  return new Promise((resolve, reject) => {
+    let registrations = [];
+    registrations = gServiceWorkerManager.getAllRegistrations();
+    is(registrations.length, aExpectedRegistrations.length,
+       "There should be " + aExpectedRegistrations.length + " registrations");
+
+    for (let i = 0; i < registrations.length; i++) {
+      let registration = registrations.queryElementAt(i, Ci.nsIServiceWorkerInfo);
+      if (!registration) {
+        reject();
+        return;
+      }
+      is(registration.principal.origin, aExpectedRegistrations[i].origin,
+         "Registration principal should be as expected");
+    }
+    resolve();
+  });
+}
+
+
+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 (/READY/.exec(message)) {
+        ok(true, 'Message from app: ' + message);
+        resolve();
+      }
+    }, false);
+    let domParent = document.getElementById('container');
+    domParent.appendChild(iframe);
+    ok(true, "origin " +  gOrigin + gApp.manifest.launch_path);
+    SpecialPowers.wrap(iframe.contentWindow).location =
+      gOrigin + gApp.manifest.launch_path;
+  });
+}
+
+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 go() {
+  setup()
+    .then(installApp)
+    .then(launchApp)
+    .then(() => {
+      app = gAppsService.getAppByManifestURL(gApp.manifestURL);
+      return checkSwRegistration([{
+        origin: app.principal.origin
+      }]);
+    })
+    .then(uninstallApp)
+    .then(() => {
+      return checkSwRegistration([]);
+    })
+    .then(SimpleTest.finish)
+    .catch((e) => {
+      ok(false, 'Unexpected error ' + e);
+      SimpleTest.finish();
+    });
+}
+</script>
+</pre>
+<div id="container"></div>
+</body>
+</html>