Bug 1169819 Add browser chrome test to validate SW force refresh. r=ehsan
authorBen Kelly <ben@wanderview.com>
Fri, 12 Jun 2015 18:59:01 -0700
changeset 248686 6f9177ab99d1df9b28fc985969b4879f3237d265
parent 248685 b9eaa0ee5ced104b1d57ca84f96b2bdc0ff8aa1a
child 248687 e7f489150f45e06d182c8f026b07eac95eb82a6d
push id61035
push userbkelly@mozilla.com
push dateSat, 13 Jun 2015 01:59:01 +0000
treeherdermozilla-inbound@e7f489150f45 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1169819
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 1169819 Add browser chrome test to validate SW force refresh. r=ehsan
dom/workers/moz.build
dom/workers/test/serviceworkers/browser.ini
dom/workers/test/serviceworkers/browser_base_force_refresh.html
dom/workers/test/serviceworkers/browser_cached_force_refresh.html
dom/workers/test/serviceworkers/browser_force_refresh.js
dom/workers/test/serviceworkers/force_refresh_browser_worker.js
--- a/dom/workers/moz.build
+++ b/dom/workers/moz.build
@@ -117,13 +117,17 @@ MOCHITEST_MANIFESTS += [
     'test/serviceworkers/mochitest.ini',
 ]
 
 MOCHITEST_CHROME_MANIFESTS += [
     'test/chrome.ini',
     'test/serviceworkers/chrome.ini'
 ]
 
+BROWSER_CHROME_MANIFESTS += [
+    'test/serviceworkers/browser.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/browser.ini
@@ -0,0 +1,7 @@
+[DEFAULT]
+support-files =
+  browser_base_force_refresh.html
+  browser_cached_force_refresh.html
+  force_refresh_browser_worker.js
+
+[browser_force_refresh.js]
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/browser_base_force_refresh.html
@@ -0,0 +1,30 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+</head>
+<body>
+<script type="text/javascript">
+addEventListener('load', function(event) {
+  navigator.serviceWorker.register('force_refresh_browser_worker.js').then(function(swr) {
+    if (!swr) {
+      return;
+    }
+    var custom = new Event('base-register', { bubbles: true });
+    document.dispatchEvent(custom);
+  });
+
+  navigator.serviceWorker.ready.then(function() {
+    var custom = new Event('base-sw-ready', { bubbles: true });
+    document.dispatchEvent(custom);
+  });
+
+  var custom = new Event('base-load', { bubbles: true });
+  document.dispatchEvent(custom);
+});
+</script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/browser_cached_force_refresh.html
@@ -0,0 +1,17 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+</head>
+<body>
+<script type="text/javascript">
+addEventListener('load', function(event) {
+  var custom = new Event('cached-load', { bubbles: true });
+  document.dispatchEvent(custom);
+});
+</script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/browser_force_refresh.js
@@ -0,0 +1,54 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+var gTestRoot = getRootDirectory(gTestPath).replace("chrome://mochitests/content/",
+                                                    "http://mochi.test:8888/")
+
+function refresh() {
+  EventUtils.synthesizeKey('R', { accelKey: true });
+}
+
+function forceRefresh() {
+  EventUtils.synthesizeKey('R', { accelKey: true, shiftKey: true });
+}
+
+function test() {
+  waitForExplicitFinish();
+  SpecialPowers.pushPrefEnv({'set': [['dom.serviceWorkers.enabled', true],
+                                     ['dom.serviceWorkers.exemptFromPerDomainMax', true],
+                                     ['dom.serviceWorkers.testing.enabled', true],
+                                     ['dom.caches.enabled', true]]},
+                            function() {
+    var url = gTestRoot + 'browser_base_force_refresh.html';
+    var tab = gBrowser.addTab(url);
+    gBrowser.selectedTab = tab;
+
+    var cachedLoad = false;
+
+    function eventHandler(event) {
+      if (event.type === 'base-load') {
+        if (cachedLoad) {
+          gBrowser.removeTab(tab);
+          executeSoon(finish);
+        }
+      } else if (event.type === 'base-register') {
+        ok(!cachedLoad, 'cached load should not occur before base register');
+        refresh();
+      } else if (event.type === 'base-sw-ready') {
+        ok(!cachedLoad, 'cached load should not occur before base ready');
+        refresh();
+      } else if (event.type === 'cached-load') {
+        ok(!cachedLoad, 'cached load should not occur twice');
+        cachedLoad = true;
+        forceRefresh();
+      }
+
+      return;
+    }
+
+    addEventListener('base-load', eventHandler, true, true);
+    addEventListener('base-register', eventHandler, true, true);
+    addEventListener('base-sw-ready', eventHandler, true, true);
+    addEventListener('cached-load', eventHandler, true, true);
+  });
+}
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/force_refresh_browser_worker.js
@@ -0,0 +1,22 @@
+var name = 'browserRefresherCache';
+
+self.addEventListener('install', function(event) {
+  event.waitUntil(
+    Promise.all([caches.open(name),
+                 fetch('./browser_cached_force_refresh.html')]).then(function(results) {
+      var cache = results[0];
+      var response = results[1];
+      return cache.put('./browser_base_force_refresh.html', response);
+    })
+  );
+});
+
+self.addEventListener('fetch', function (event) {
+  event.respondWith(
+    caches.open(name).then(function(cache) {
+      return cache.match(event.request);
+    }).then(function(response) {
+      return response || fetch(event.request);
+    })
+  );
+});