<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 930348 - test stub Navigator ServiceWorker utilities.</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">

  function simpleRegister() {
    var p = navigator.serviceWorker.register("worker.js", { scope: "simpleregister/" });
    ok(p instanceof Promise, "register() should return a Promise");
    return Promise.resolve();
  }

  function sameOriginWorker() {
    p = navigator.serviceWorker.register("http://some-other-origin/worker.js");
    return p.then(function(w) {
      ok(false, "Worker from different origin should fail");
    }, function(e) {
      ok(e.name === "SecurityError", "Should fail with a SecurityError");
    });
  }

  function sameOriginScope() {
    p = navigator.serviceWorker.register("worker.js", { scope: "http://www.example.com/" });
    return p.then(function(w) {
      ok(false, "Worker controlling scope for different origin should fail");
    }, function(e) {
      ok(e.name === "SecurityError", "Should fail with a SecurityError");
    });
  }

  function httpsOnly() {
    return SpecialPowers.pushPrefEnv({'set': [["dom.serviceWorkers.testing.enabled", false]] })
    .then(function() {
      return navigator.serviceWorker.register("/worker.js");
    }).then(function(w) {
      ok(false, "non-HTTPS pages cannot register ServiceWorkers");
    }, function(e) {
      ok(e.name === "TypeError", "navigator.serviceWorker should be undefined");
    }).then(function() {
      return SpecialPowers.popPrefEnv();
    });
  }

  function realWorker() {
    var p = navigator.serviceWorker.register("worker.js", { scope: "realworker" });
    return p.then(function(wr) {
      ok(wr instanceof ServiceWorkerRegistration, "Register a ServiceWorker");

      info(wr.scope);
      ok(wr.scope == (new URL("realworker", document.baseURI)).href, "Scope should match");
      // active, waiting, installing should return valid worker instances
      // because the registration is for the realworker scope, so the workers
      // should be obtained for that scope and not for
      // test_installation_simple.html
      var worker = wr.installing;
      ok(worker && wr.scope.match(/realworker$/) &&
         worker.scriptURL.match(/worker.js$/), "Valid worker instance should be available.");
      return wr.unregister().then(function(success) {
        ok(success, "The worker should be unregistered successfully");
      }, function(e) {
        dump("Error unregistering the worker: " + e + "\n");
      });
    }, function(e) {
      info("Error: " + e.name);
      ok(false, "realWorker Registration should have succeeded!");
    });
  }

  function networkError404() {
    return navigator.serviceWorker.register("404.js", { scope: "network_error/"}).then(function(w) {
        ok(false, "404 response should fail with TypeError");
      }, function(e) {
        ok(e.name === "TypeError", "404 response should fail with TypeError");
      });
  }

  function redirectError() {
    return navigator.serviceWorker.register("redirect_serviceworker.sjs", { scope: "redirect_error/" }).then(function(swr) {
        ok(false, "redirection should fail");
      }, function (e) {
        ok(e.name === "SecurityError", "redirection should fail with SecurityError");
      });
  }

  function parseError() {
    var p = navigator.serviceWorker.register("parse_error_worker.js", { scope: "parse_error/" });
    return p.then(function(wr) {
      ok(false, "Registration should fail with parse error");
      return navigator.serviceWorker.getRegistration("parse_error/").then(function(swr) {
        // See https://github.com/slightlyoff/ServiceWorker/issues/547
        is(swr, undefined, "A failed registration for a scope with no prior controllers should clear itself");
      });
    }, function(e) {
      ok(e instanceof Error, "Registration should fail with parse error");
    });
  }

  // FIXME(nsm): test for parse error when Update step doesn't happen (directly from register).

  function updatefound() {
    var frame = document.createElement("iframe");
    frame.setAttribute("id", "simpleregister-frame");
    frame.setAttribute("src", new URL("simpleregister/index.html", document.baseURI).href);
    document.body.appendChild(frame);
    var resolve, reject;
    var p = new Promise(function(res, rej) {
      resolve = res;
      reject = rej;
    });

    var regPromise;
    function continueTest() {
      regPromise = navigator.serviceWorker.register(
        "worker2.js", { scope: "simpleregister/" });
    }

    window.onmessage = function(e) {
      if (e.data.type == "ready") {
        continueTest();
      } else if (e.data.type == "finish") {
        window.onmessage = null;
        // We have to make frame navigate away, otherwise it will call
        // MaybeStopControlling() when this document is unloaded. At that point
        // the pref has been disabled, so the ServiceWorkerManager is not available.
        frame.setAttribute("src", new URL("about:blank").href);
        regPromise.then(function(reg) {
          reg.unregister().then(function(success) {
            ok(success, "The worker should be unregistered successfully");
            resolve();
          }, function(error) {
            dump("Error unregistering the worker: " + error + "\n");
          });
        });
      } else if (e.data.type == "check") {
        ok(e.data.status, e.data.msg);
      }
    }
    return p;
  }

  var readyPromiseResolved = false;

  function readyPromise() {
    var frame = document.createElement("iframe");
    frame.setAttribute("id", "simpleregister-frame-ready");
    frame.setAttribute("src", new URL("simpleregister/ready.html", document.baseURI).href);
    document.body.appendChild(frame);

    var channel = new MessageChannel();
    frame.addEventListener('load', function() {
      frame.contentWindow.postMessage('your port!', '*', [channel.port2]);
    });

    channel.port1.onmessage = function() {
      readyPromiseResolved = true;
    }

    return Promise.resolve();
  }

  function checkReadyPromise() {
    ok(readyPromiseResolved, "The ready promise has been resolved!");
    return Promise.resolve();
  }

  function runTest() {
    simpleRegister()
      .then(sameOriginWorker)
      .then(sameOriginScope)
      .then(httpsOnly)
      .then(readyPromise)
      .then(realWorker)
      .then(networkError404)
      .then(redirectError)
      .then(parseError)
      .then(updatefound)
      .then(checkReadyPromise)
      // put more tests here.
      .then(function() {
        SimpleTest.finish();
      }).catch(function(e) {
        ok(false, "Some test failed with error " + e);
        SimpleTest.finish();
      });
  }

  SimpleTest.waitForExplicitFinish();
  SpecialPowers.pushPrefEnv({"set": [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["dom.caches.testing.enabled", true],
  ]}, runTest);
</script>
</pre>
</body>
</html>
