Chromium Code Reviews| 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..023ff7d545d8b4f4501cda792bb9b202b74d2be7 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) { |
| + var workerUrl = 'resources/empty_worker.js'; |
| + var workerScope = 'resources/scope/' + location.pathname; |
| + var swRegistration; |
|
Peter Beverloo
2016/10/14 14:36:30
no "var"
harkness
2016/10/14 15:13:46
Done.
|
| + |
| + 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}); |
| + }); |
| } |