From 3b1c0e622094c831963e1f8f43ddff8d85b53d7e Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Thu, 11 Jun 2015 17:52:02 -0700 Subject: [PATCH] Bug 1169819 Add browser chrome test to validate SW force refresh. r=ehsan a=kwierso --- dom/workers/moz.build | 4 ++ dom/workers/test/serviceworkers/browser.ini | 8 +++ .../browser_base_force_refresh.html | 30 ++++++++++ .../browser_cached_force_refresh.html | 17 ++++++ .../serviceworkers/browser_force_refresh.js | 56 +++++++++++++++++++ .../browser_force_refresh_worker.js | 22 ++++++++ 6 files changed, 137 insertions(+) create mode 100644 dom/workers/test/serviceworkers/browser.ini create mode 100644 dom/workers/test/serviceworkers/browser_base_force_refresh.html create mode 100644 dom/workers/test/serviceworkers/browser_cached_force_refresh.html create mode 100644 dom/workers/test/serviceworkers/browser_force_refresh.js create mode 100644 dom/workers/test/serviceworkers/browser_force_refresh_worker.js diff --git a/dom/workers/moz.build b/dom/workers/moz.build index 77681acb4182..ce5b24bed84b 100644 --- a/dom/workers/moz.build +++ b/dom/workers/moz.build @@ -122,6 +122,10 @@ MOCHITEST_CHROME_MANIFESTS += [ 'test/serviceworkers/chrome.ini' ] +BROWSER_CHROME_MANIFESTS += [ + 'test/serviceworkers/browser.ini', +] + XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell/xpcshell.ini'] BROWSER_CHROME_MANIFESTS += ['test/browser.ini'] diff --git a/dom/workers/test/serviceworkers/browser.ini b/dom/workers/test/serviceworkers/browser.ini new file mode 100644 index 000000000000..c0ed8500b19c --- /dev/null +++ b/dom/workers/test/serviceworkers/browser.ini @@ -0,0 +1,8 @@ +[DEFAULT] +support-files = + browser_base_force_refresh.html + browser_cached_force_refresh.html + browser_force_refresh_worker.js + +[browser_force_refresh.js] +skip-if = e10s diff --git a/dom/workers/test/serviceworkers/browser_base_force_refresh.html b/dom/workers/test/serviceworkers/browser_base_force_refresh.html new file mode 100644 index 000000000000..e46489bac23d --- /dev/null +++ b/dom/workers/test/serviceworkers/browser_base_force_refresh.html @@ -0,0 +1,30 @@ + + + + + + + + + diff --git a/dom/workers/test/serviceworkers/browser_cached_force_refresh.html b/dom/workers/test/serviceworkers/browser_cached_force_refresh.html new file mode 100644 index 000000000000..4b550a3d23a3 --- /dev/null +++ b/dom/workers/test/serviceworkers/browser_cached_force_refresh.html @@ -0,0 +1,17 @@ + + + + + + + + + diff --git a/dom/workers/test/serviceworkers/browser_force_refresh.js b/dom/workers/test/serviceworkers/browser_force_refresh.js new file mode 100644 index 000000000000..f8b4d3dc9a8c --- /dev/null +++ b/dom/workers/test/serviceworkers/browser_force_refresh.js @@ -0,0 +1,56 @@ +/* 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', { ctrlKey: true }); +} + +function forceRefresh() { + EventUtils.synthesizeKey('R', { ctrlKey: 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; + + ok(true); + + 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); + }); +} diff --git a/dom/workers/test/serviceworkers/browser_force_refresh_worker.js b/dom/workers/test/serviceworkers/browser_force_refresh_worker.js new file mode 100644 index 000000000000..0fe91ed95a1b --- /dev/null +++ b/dom/workers/test/serviceworkers/browser_force_refresh_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); + }) + ); +});