serviceworker_driver.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Any copyright is dedicated to the Public Domain.
  2. // http://creativecommons.org/publicdomain/zero/1.0/
  3. function serviceWorkerTestExec(testFile) {
  4. var isB2G = !navigator.userAgent.includes("Android") &&
  5. /Mobile|Tablet/.test(navigator.userAgent);
  6. if (isB2G) {
  7. // TODO B2G doesn't support running service workers for now due to bug 1137683.
  8. dump("Skipping running the test in SW until bug 1137683 gets fixed.\n");
  9. return Promise.resolve();
  10. }
  11. return new Promise(function(resolve, reject) {
  12. function setupSW(registration) {
  13. var worker = registration.waiting ||
  14. registration.active;
  15. window.addEventListener("message",function onMessage(event) {
  16. if (event.data.context != "ServiceWorker") {
  17. return;
  18. }
  19. if (event.data.type == 'finish') {
  20. window.removeEventListener("message", onMessage);
  21. registration.unregister()
  22. .then(resolve)
  23. .catch(reject);
  24. } else if (event.data.type == 'status') {
  25. ok(event.data.status, event.data.context + ": " + event.data.msg);
  26. }
  27. }, false);
  28. worker.onerror = reject;
  29. var iframe = document.createElement("iframe");
  30. iframe.src = "message_receiver.html";
  31. iframe.onload = function() {
  32. worker.postMessage({ script: testFile });
  33. };
  34. document.body.appendChild(iframe);
  35. }
  36. navigator.serviceWorker.ready.then(setupSW);
  37. navigator.serviceWorker.register("worker_wrapper.js", {scope: "."});
  38. });
  39. }