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

Unified Diff: third_party/WebKit/LayoutTests/http/tests/push_messaging/resources/test-helpers.js

Issue 2411733002: Check the format of an applicationServerKey when used to register a push subscription. (Closed)
Patch Set: More formatting Created 4 years, 2 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/http/tests/push_messaging/resources/test-helpers.js
diff --git a/third_party/WebKit/LayoutTests/http/tests/push_messaging/resources/test-helpers.js b/third_party/WebKit/LayoutTests/http/tests/push_messaging/resources/test-helpers.js
index 32ea3dc5abad3c6b75ec950326b219ed7363be8f..01a820eff389f01da0f30d197366bec0c1f215eb 100644
--- a/third_party/WebKit/LayoutTests/http/tests/push_messaging/resources/test-helpers.js
+++ b/third_party/WebKit/LayoutTests/http/tests/push_messaging/resources/test-helpers.js
@@ -1,43 +1,76 @@
"use strict";
-// Subscribes and unsubscribes to push once so that the manifest details are stored in service
-// worker storage. After this, subscribe can succeed from inside the service worker.
-// This triggers an infobar unless a permission decision was previously set.
+// Subscribes and unsubscribes to push once so that the manifest details are
+// stored in service worker storage. After this, subscribe can succeed from
+// inside the service worker. This triggers an infobar unless a permission
+// decision was previously set.
function subscribeAndUnsubscribePush(registration) {
- return new Promise(function(resolve, reject) {
- // 1. Call subscribe in document context. The manifest details are stored in the service
- // worker storage for later use in a service worker context where there is no manifest.
- registration.pushManager.subscribe()
- .then(function(subscription) {
- // 2. Call unsubscribe so we can subscribe again later inside a service worker.
- return subscription.unsubscribe();
- })
- .then(function(unsubscription_result) {
- resolve();
- })
- .catch(function(e) {
- reject(e);
- });
+ return new Promise(function(resolve, reject) {
+ // 1. Call subscribe in document context. The manifest details are stored
+ // in the service worker storage for later use in a service worker context
+ // where there is no manifest.
+ registration.pushManager.subscribe().then(function(subscription) {
+ // 2. Call unsubscribe so we can subscribe again later inside a
+ // service worker.
+ return subscription.unsubscribe();
+ })
+ .then(function(unsubscription_result) {
+ resolve();
+ })
+ .catch(function(e) {
+ reject(e);
});
+ });
}
-// Runs |command| in the service worker connected to |port|. Returns a promise that will be resolved
-// with the response data of the command.
+// Registers a service worker and subscribes to push using the given string
+// as an applicationServerKey.
+function registerAndSubscribePushWithString(test, serverKeyString) {
+ return registerAndSubscribePush(test,
+ new TextEncoder().encode(serverKeyString));
+}
+
+// Subscribes to push with the given application server key. serverKey should be
+// a Uint8Array.
+function registerAndSubscribePush(test, serverKey) {
+ const workerUrl = 'resources/empty_worker.js';
+ const workerScope = 'resources/scope/' + location.pathname;
+ let swRegistration;
+
+ return service_worker_unregister_and_register(test, workerUrl, workerScope)
+ .then(function(serviceWorkerRegistration) {
+ swRegistration = serviceWorkerRegistration;
+ return wait_for_state(test, swRegistration.installing, 'activated');
+ })
+ .then(function() {
+ if (window.testRunner) {
+ testRunner.setPermission('push-messaging', 'granted', location.origin,
+ location.origin);
+ }
+ return swRegistration.pushManager.subscribe({
+ userVisibleOnly: true,
+ applicationServerKey: serverKey
+ });
+ });
+}
+
+// Runs |command| in the service worker connected to |port|. Returns a promise
+// that will be resolved with the response data of the command.
function runCommandInServiceWorker(port, command) {
- return new Promise(function(resolve, reject) {
- port.addEventListener('message', function listener(event) {
- // To make this listener a oneshot, remove it the first time it runs.
- port.removeEventListener('message', listener);
+ return new Promise(function(resolve, reject) {
+ port.addEventListener('message', function listener(event) {
+ // To make this listener a oneshot, remove it the first time it runs.
+ port.removeEventListener('message', listener);
- if (typeof event.data != 'object' || !event.data.command)
- assert_unreached('Invalid message from the service worker');
+ if (typeof event.data != 'object' || !event.data.command)
+ assert_unreached('Invalid message from the service worker');
- assert_equals(event.data.command, command);
- if (event.data.success)
- resolve(event.data);
- else
- reject(new Error(event.data.errorMessage));
- });
- port.postMessage({command: command});
+ assert_equals(event.data.command, command);
+ if (event.data.success)
+ resolve(event.data);
+ else
+ reject(new Error(event.data.errorMessage));
});
+ port.postMessage({command: command});
+ });
}

Powered by Google App Engine
This is Rietveld 408576698