1 // Supports test-runner control messages being send over |messagePort|, which enable
2 // workers to have limited access to TestRunner methods.
3 function supportTestRunnerMessagesOnPort(messagePort)
5 if (!window.testRunner)
8 messagePort.addEventListener('message', function(message) {
9 if (message.data.type == 'simulateWebNotificationClick')
10 testRunner.simulateWebNotificationClick(message.data.title);
14 // Starts |script| as a dedicated worker and executes the testharness tests defined
15 // within it. The Notification-related testRunner methods will be made available.
16 function dedicatedWorkerTest(script)
18 var worker = new Worker(script);
19 supportTestRunnerMessagesOnPort(worker);
21 fetch_tests_from_worker(worker);
24 // Starts |script| as a shared worker and executes the testharness tests defined
25 // within it. The Notification-related testRunner methods will be made available.
26 function sharedWorkerTest(script)
28 var worker = new SharedWorker(script, 'Notification API LayoutTest worker');
29 supportTestRunnerMessagesOnPort(worker.port);
31 fetch_tests_from_worker(worker);
34 // Used by tests to announce that all tests have been registered when they're
35 // being ran from a dedicated or shared worker. Not necessary for document tests.
36 function isDedicatedOrSharedWorker()
41 // Unregisters, then registers the Service Worker in |script| using |scope|, waits
42 // for it to activate and then inserts a message port. Returns a Promise which will
43 // be resolved with an object having the worker's registration and port. The
44 // Service Worker will be automatically unregistered once the test has completed.
45 function getActiveServiceWorkerWithMessagePort(test, script, scope)
47 var registration = null;
48 return service_worker_unregister_and_register(test, script, scope).then(function(swRegistration) {
49 registration = swRegistration;
50 add_completion_callback(function() {
51 registration.unregister();
54 return wait_for_state(test, registration.installing, 'activated');
56 assert_not_equals(registration.active, null, 'The Service Worker needs to be activated.');
57 return new Promise(function(resolve) {
58 var messageChannel = new MessageChannel();
59 messageChannel.port1.addEventListener('message', function(event) {
60 if (event.data == 'ready')
61 resolve({ registration: registration, port: messageChannel.port1 });
64 registration.active.postMessage(messageChannel.port2, [messageChannel.port2]);
65 messageChannel.port1.start();