Bug 1265795 P2 Add a web-platform-test for the window navigation case. r=bz
authorBen Kelly <ben@wanderview.com>
Thu, 21 Apr 2016 16:10:40 -0700
changeset 332420 7ee57ef9f2904d052215b6974acc3d0db9fc97d0
parent 332419 51a007dd9060d9e95d9023f437807ecb1bbbe95d
child 332421 f3bc58b4de70cd7e4ef0ca161d8149545023b294
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs1265795
milestone48.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 1265795 P2 Add a web-platform-test for the window navigation case. r=bz
testing/web-platform/meta/MANIFEST.json
testing/web-platform/tests/service-workers/service-worker/navigate-window.https.html
testing/web-platform/tests/service-workers/service-worker/resources/loaded.html
testing/web-platform/tests/service-workers/service-worker/resources/navigate-window-worker.js
--- a/testing/web-platform/meta/MANIFEST.json
+++ b/testing/web-platform/meta/MANIFEST.json
@@ -35271,16 +35271,22 @@
           }
         ],
         "html/webappapis/scripting/processing-model-2/window-onerror-with-cross-frame-event-listeners-4.html": [
           {
             "path": "html/webappapis/scripting/processing-model-2/window-onerror-with-cross-frame-event-listeners-4.html",
             "url": "/html/webappapis/scripting/processing-model-2/window-onerror-with-cross-frame-event-listeners-4.html"
           }
         ],
+        "service-workers/service-worker/navigate-window.https.html": [
+          {
+            "path": "service-workers/service-worker/navigate-window.https.html",
+            "url": "/service-workers/service-worker/navigate-window.https.html"
+          }
+        ],
         "web-animations/timing-model/active-time.html": [
           {
             "path": "web-animations/timing-model/active-time.html",
             "url": "/web-animations/timing-model/active-time.html"
           }
         ],
         "web-animations/timing-model/current-iteration.html": [
           {
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/navigate-window.https.html
@@ -0,0 +1,97 @@
+<!DOCTYPE html>
+<title>Service Worker: Navigate a Window</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>
+<body>
+<script>
+var host_info = get_host_info();
+var BASE_URL = host_info['HTTPS_ORIGIN'] + base_path();
+
+function wait_for_message(msg) {
+  return new Promise(function(resolve, reject) {
+    window.addEventListener('message', function onMsg(evt) {
+      if (evt.data.type === msg) {
+        resolve();
+      }
+    });
+  });
+}
+
+function with_window(url) {
+  var win = window.open(url);
+  return wait_for_message('LOADED').then(_ => win);
+}
+
+function navigate_window(win, url) {
+  win.location = url;
+  return wait_for_message('LOADED').then(_ => win);
+}
+
+function go_back(win) {
+  win.history.back();
+  return wait_for_message('PAGESHOW').then(_ => win);
+}
+
+function go_forward(win) {
+  win.history.forward();
+  return wait_for_message('PAGESHOW').then(_ => win);
+}
+
+function get_clients(win, sw, opts) {
+  return new Promise((resolve, reject) => {
+    win.navigator.serviceWorker.addEventListener('message', function onMsg(evt) {
+      win.navigator.serviceWorker.removeEventListener('message', onMsg);
+      if (evt.data.type === 'success') {
+        resolve(evt.data.detail);
+      } else {
+        reject(evt.data.detail);
+      }
+    });
+    sw.postMessage({ type: 'GET_CLIENTS', opts: (opts || {}) });
+  });
+}
+
+function validate_window(win, url, opts) {
+  return win.navigator.serviceWorker.getRegistration(url)
+    .then(reg => {
+        // In order to compare service worker instances we need to
+        // make sure the DOM object is owned by the same global; the
+        // opened window in this case.
+        assert_equals(win.navigator.serviceWorker.controller, reg.active,
+                      'window should be controlled by service worker');
+        return get_clients(win, reg.active);
+      })
+    .then(resultList => {
+        assert_equals(resultList.length, 1, 'there should only be one client');
+        assert_equals(resultList[0].url, url,
+                      'client should be our opened window');
+        assert_equals(resultList[0].frameType, 'auxiliary',
+                      'window.open() should create a client with an auxiliary frame type');
+        return win;
+      })
+}
+
+async_test(function(t) {
+    var worker = BASE_URL + 'resources/navigate-window-worker.js';
+    var scope = BASE_URL + 'resources/loaded.html?navigate-window';
+    var url1 = scope + '&q=1';
+    var url2 = scope + '&q=2';
+    service_worker_unregister_and_register(t, worker, scope)
+      .then(reg => wait_for_state(t, reg.installing, 'activated') )
+      .then(___ => with_window(url1))
+      .then(win => validate_window(win, url1, { includeUncontrolled: false }))
+      .then(win => navigate_window(win, url2))
+      .then(win => validate_window(win, url2, { includeUncontrolled: false }))
+      .then(win => go_back(win))
+      .then(win => validate_window(win, url1, { includeUncontrolled: false }))
+      .then(win => go_forward(win))
+      .then(win => validate_window(win, url2, { includeUncontrolled: false }))
+      .then(win => win.close())
+      .catch(unreached_rejection(t))
+      .then(___ => service_worker_unregister_and_done(t, scope))
+  }, 'Clients.matchAll() should not show an old window as controlled after ' +
+     'it navigates.');
+</script>
+</body>
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/loaded.html
@@ -0,0 +1,9 @@
+<script>
+addEventListener('load', function() {
+  opener.postMessage({ type: 'LOADED' }, '*');
+});
+
+addEventListener('pageshow', function() {
+  opener.postMessage({ type: 'PAGESHOW' }, '*');
+});
+</script>
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/service-workers/service-worker/resources/navigate-window-worker.js
@@ -0,0 +1,21 @@
+addEventListener('message', function(evt) {
+  if (evt.data.type === 'GET_CLIENTS') {
+    clients.matchAll(evt.data.opts).then(function(clientList) {
+      var resultList = clientList.map(function(c) {
+        return { url: c.url, frameType: c.frameType, id: c.id };
+      });
+      evt.source.postMessage({ type: 'success', detail: resultList });
+    }).catch(function(err) {
+      evt.source.postMessage({
+        type: 'failure',
+        detail: 'matchAll() rejected with "' + err + '"'
+      });
+    });
+    return;
+  }
+
+  evt.source.postMessage({
+    type: 'failure',
+    detail: 'Unexpected message type "' + evt.data.type + '"'
+  });
+});