Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(741)

Unified Diff: third_party/WebKit/LayoutTests/external/wpt/service-workers/service-worker/resources/windowclient-navigate-worker.js

Issue 2906963002: Upstream service wrkr "window client" test to WPT (Closed)
Patch Set: Incorporate review feedback Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/external/wpt/service-workers/service-worker/resources/windowclient-navigate-worker.js
diff --git a/third_party/WebKit/LayoutTests/external/wpt/service-workers/service-worker/resources/windowclient-navigate-worker.js b/third_party/WebKit/LayoutTests/external/wpt/service-workers/service-worker/resources/windowclient-navigate-worker.js
new file mode 100644
index 0000000000000000000000000000000000000000..8c2c2c66d758dc8bb726c9b0f73cec5fbc3fe6e9
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/external/wpt/service-workers/service-worker/resources/windowclient-navigate-worker.js
@@ -0,0 +1,66 @@
+function match_query(query_string) {
+ return self.location.search.substr(1) == query_string;
+}
+
+function navigate_test(e) {
+ var port = e.data.port;
+ var url = e.data.url;
+
+ return clients.matchAll({ includeUncontrolled : true })
+ .then(function(client_list) {
+ for (var i = 0; i < client_list.length; i++) {
+ var client = client_list[i];
+ if (client.frameType == 'nested') {
+ return client.navigate(url);
+ }
+ }
+ port.postMessage('Could not locate window client.');
+ })
+ .then(function(new_client) {
+ if (new_client === null)
+ port.postMessage(new_client);
+ else
+ port.postMessage(new_client.url);
+ })
+ .catch(function(error) {
+ port.postMessage(error.name);
+ });
+}
+
+function getTestClient() {
+ return clients.matchAll({ includeUncontrolled: true })
+ .then(function(client_list) {
+ for (var i = 0; i < client_list.length; i++) {
+ var client = client_list[i];
+
+ if (/windowclient-navigate\.https\.html/.test(client.url)) {
+ return client;
+ }
+ }
+
+ throw new Error('Service worker was unable to locate test client.');
+ });
+}
+
+function waitForMessage(client) {
+ var channel = new MessageChannel();
+ client.postMessage({ port: channel.port2 }, [channel.port2]);
+
+ return new Promise(function(resolve) {
+ channel.port1.onmessage = resolve;
+ });
+}
+
+// The worker must remain in the "installing" state for the duration of some
+// sub-tests. In order to achieve this coordination without relying on global
+// state, the worker must create a message channel with the client from within
+// the "install" event handler.
+if (match_query('installing')) {
+ self.addEventListener('install', function(e) {
+ e.waitUntil(getTestClient().then(waitForMessage));
+ });
+}
+
+self.addEventListener('message', function(e) {
+ e.waitUntil(navigate_test(e));
+ });

Powered by Google App Engine
This is Rietveld 408576698