Bug 1218148 - Web Platform tests for WindowClient.navigate() r?baku draft
authorAndreas Farre <farre@mozilla.com>
Mon, 06 Jun 2016 15:57:25 +0200
changeset 384126 61f428f57d2f9703cc945ff2c096b8ee4d33ed3c
parent 384125 d115291bd0b8a78cdecae1e6c5643ef047d7217d
child 524613 2ee62df9e93f14882110738500e6cc299ca53a5f
push id22172
push userbmo:afarre@mozilla.com
push dateTue, 05 Jul 2016 16:13:00 +0000
reviewersbaku
bugs1218148
milestone50.0a1
Bug 1218148 - Web Platform tests for WindowClient.navigate() r?baku MozReview-Commit-ID: 4oXwKr29AhI
testing/web-platform/meta/MANIFEST.json
testing/web-platform/tests/service-workers/service-worker/client-navigate.https.html
testing/web-platform/tests/service-workers/service-worker/resources/client-navigate-frame.html
testing/web-platform/tests/service-workers/service-worker/resources/client-navigate-worker.js
testing/web-platform/tests/service-workers/service-worker/resources/client-navigated-frame.html
--- a/testing/web-platform/meta/MANIFEST.json
+++ b/testing/web-platform/meta/MANIFEST.json
@@ -36398,16 +36398,22 @@
           }
         ],
         "web-animations/animation-model/keyframe-effects/spacing-keyframes.html": [
           {
             "path": "web-animations/animation-model/keyframe-effects/spacing-keyframes.html",
             "url": "/web-animations/animation-model/keyframe-effects/spacing-keyframes.html"
           }
         ],
+        "service-workers/service-worker/client-navigate.https.html": [
+          {
+            "path": "service-workers/service-worker/client-navigate.https.html",
+            "url": "/service-workers/service-worker/client-navigate.https.html"
+          }
+        ],
         "web-animations/interfaces/DocumentTimeline/constructor.html": [
           {
             "path": "web-animations/interfaces/DocumentTimeline/constructor.html",
             "url": "/web-animations/interfaces/DocumentTimeline/constructor.html"
           }
         ],
         "web-animations/interfaces/DocumentTimeline/idlharness.html": [
           {
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/client-navigate.https.html
@@ -0,0 +1,117 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>Service Worker: WindowClient.navigate</title>
+<script src=/resources/testharness.js></script>
+<script src=/resources/testharnessreport.js></script>
+<script src="resources/get-host-info.sub.js"></script>
+<script src="resources/test-helpers.sub.js"></script>
+<script>
+  function wait_for_message(msg) {
+    return new Promise(function(resolve, reject) {
+      var get_message_data = function get_message_data(e) {
+        window.removeEventListener("message", get_message_data);
+        resolve(e.data);
+      }
+      window.addEventListener("message", get_message_data, false);
+    });
+  }
+
+  function run_test(controller, clientId, test) {
+    return new Promise(function(resolve, reject) {
+      var channel = new MessageChannel();
+      channel.port1.onmessage = function(e) {
+        resolve(e.data);
+      };
+      var message = {
+        port: channel.port2,
+        test: test,
+        clientId: clientId,
+      };
+      controller.postMessage(
+        message, [channel.port2]);
+    });
+  }
+
+  promise_test(function(t) {
+    var worker = "resources/client-navigate-worker.js";
+    var scope = "resources/client-navigate-frame.html";
+    var controller, frame, clientId;
+
+    return service_worker_unregister_and_register(t, worker, scope)
+      .then(reg => wait_for_state(t, reg.installing, "activated"))
+      .then(___ => with_iframe(scope))
+      .then(f => {
+        frame = f;
+        controller = frame.contentWindow.navigator.serviceWorker.controller;
+        fetch_tests_from_worker(controller);
+        return wait_for_message()
+      })
+      .then(({id}) => clientId = id)
+      .then(___ => run_test(controller, clientId, "test_client_navigate_success"))
+      .then(({result, url}) => {
+        assert_equals(result, "test_client_navigate_success");
+        assert_equals(
+          url, new URL("resources/client-navigated-frame.html",
+                       location).toString());
+        assert_equals(
+          frame.contentWindow.location.href,
+          new URL("resources/client-navigated-frame.html",
+                  location).toString());
+      })
+      .catch(unreached_rejection(t))
+      .then(___ => service_worker_unregister(t, scope));
+  }, "Frame location should update on successful navigation");
+
+  promise_test(function(t) {
+    var worker = "resources/client-navigate-worker.js";
+    var scope = "resources/client-navigate-frame.html";
+    var controller, frame, clientId;
+
+    return service_worker_unregister_and_register(t, worker, scope)
+      .then(reg => wait_for_state(t, reg.installing, "activated"))
+      .then(___ => with_iframe(scope))
+      .then(f => {
+        frame = f;
+        controller = frame.contentWindow.navigator.serviceWorker.controller;
+        fetch_tests_from_worker(controller);
+        return wait_for_message()
+      })
+      .then(({id}) => clientId = id)
+      .then(___ => run_test(controller, clientId, "test_client_navigate_redirect"))
+      .then(({result, url}) => {
+        assert_equals(result, "test_client_navigate_redirect");
+        assert_equals(url, "");
+        assert_throws(null, function() { return frame.contentWindow.location.href });
+      })
+      .catch(unreached_rejection(t))
+      .then(___ => service_worker_unregister(t, scope));
+  }, "Frame location should not be accessible after redirect");
+
+  promise_test(function(t) {
+    var worker = "resources/client-navigate-worker.js";
+    var scope = "resources/client-navigate-frame.html";
+    var controller, frame, clientId;
+
+    return service_worker_unregister_and_register(t, worker, scope)
+      .then(reg => wait_for_state(t, reg.installing, "activated"))
+      .then(___ => with_iframe(scope))
+      .then(f => {
+        frame = f;
+        controller = frame.contentWindow.navigator.serviceWorker.controller;
+        fetch_tests_from_worker(controller);
+        return wait_for_message()
+      })
+      .then(({id}) => clientId = id)
+      .then(___ => run_test(controller, clientId, "test_client_navigate_failure"))
+      .then(({result, url}) => {
+        assert_equals(result, "test_client_navigate_failure");
+        assert_equals(
+          frame.contentWindow.location.href,
+          new URL("resources/client-navigate-frame.html",
+                  location).toString());
+        frame.contentWindow.document.body.style = "background-color: green"
+      })
+      .catch(unreached_rejection(t))
+      .then(___ => service_worker_unregister(t, scope));
+  }, "Frame location should not update on failed navigation");
+</script>
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/client-navigate-frame.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<meta charset=utf-8>
+<script>
+  fetch("clientId")
+    .then(function(response) {
+      return response.text();
+    })
+    .then(function(text) {
+      parent.postMessage({id: text}, "*");
+    });
+</script>
+<body style="background-color: red;"></body>
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/client-navigate-worker.js
@@ -0,0 +1,71 @@
+importScripts("worker-testharness.js");
+importScripts("test-helpers.sub.js");
+importScripts("get-host-info.sub.js")
+importScripts("testharness-helpers.js")
+
+self.onfetch = function(e) {
+  if (e.request.url.indexOf("client-navigate-frame.html") >= 0) {
+    if (e.clientId === null) {
+      e.respondWith(fetch(e.request));
+    } else {
+      e.respondWith(Response.error());
+    }
+    return;
+  }
+  e.respondWith(new Response(e.clientId));
+};
+
+function pass(test, url) {
+  return { result: test,
+           url: url };
+}
+
+function fail(test, reason) {
+  return { result: "FAILED " + test + " " + reason }
+}
+
+self.onmessage = function(e) {
+  var port = e.data.port;
+  var test = e.data.test;
+  var clientId = e.data.clientId;
+  var clientUrl = "";
+  if (test === "test_client_navigate_success") {
+    promise_test(function(t) {
+      this.add_cleanup(() => port.postMessage(pass(test, clientUrl)));
+      return self.clients.get(clientId)
+                 .then(client => client.navigate("client-navigated-frame.html"))
+                 .then(client => {
+                   clientUrl = client.url;
+                   assert_true(client instanceof WindowClient);
+                 })
+                 .catch(unreached_rejection(t));
+    }, "Return value should be instance of WindowClient");
+  } else if (test === "test_client_navigate_failure") {
+    promise_test(function(t) {
+      return self.clients.get(clientId)
+                 .then(client => assert_promise_rejects(client.navigate("http://example.com")))
+                 .catch(unreached_rejection(t));
+    }, "Navigating to different origin should reject");
+
+    promise_test(function(t) {
+      this.add_cleanup(function() { port.postMessage(pass(test, "")); });
+      return self.clients.get(clientId)
+                 .then(client => promise_rejects(t, new TypeError(), client.navigate("about:blank")))
+                 .catch(unreached_rejection(t));
+    }, "Navigating to about:blank should reject with TypeError")
+  } else if (test === "test_client_navigate_redirect") {
+    var host_info = get_host_info();
+    var url = new URL(host_info['HTTPS_REMOTE_ORIGIN']).toString() +
+              new URL("client-navigated-frame.html", location).pathname.substring(1);
+    promise_test(function(t) {
+      this.add_cleanup(() => port.postMessage(pass(test, clientUrl)));
+      return self.clients.get(clientId)
+                 .then(client => client.navigate("redirect.py?Redirect=" + url))
+                 .then(client => {
+                   clientUrl = (client && client.url) || ""
+                   assert_true(client === null);
+                 })
+                 .catch(unreached_rejection(t));
+    }, "Redirecting to another origin should resolve with null");
+  }
+};
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/client-navigated-frame.html
@@ -0,0 +1,3 @@
+<!DOCTYPE html>
+<meta charset=utf-8>
+<body style="background-color: green;"></body>