OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>Service Worker: Clients.get with window and worker clients</title> | 2 <title>Service Worker: Clients.get with window and worker clients</title> |
3 <script src="/resources/testharness.js"></script> | 3 <script src="/resources/testharness.js"></script> |
4 <script src="/resources/testharnessreport.js"></script> | 4 <script src="/resources/testharnessreport.js"></script> |
5 <script src="resources/test-helpers.sub.js"></script> | 5 <script src="resources/test-helpers.sub.js"></script> |
6 <script> | 6 <script> |
7 var scope = 'resources/clients-get-client-types'; | 7 var scope = 'resources/clients-get-client-types'; |
8 var frame_url = scope + '-frame.html'; | 8 var frame_url = scope + '-frame.html'; |
9 var shared_worker_url = scope + '-shared-worker.js'; | 9 var shared_worker_url = scope + '-shared-worker.js'; |
| 10 var worker_url = scope + '-worker.js'; |
10 var client_ids = []; | 11 var client_ids = []; |
11 var frame; | 12 var frame; |
12 promise_test(function(t) { | 13 promise_test(function(t) { |
13 return service_worker_unregister_and_register( | 14 return service_worker_unregister_and_register( |
14 t, 'resources/clients-get-worker.js', scope) | 15 t, 'resources/clients-get-worker.js', scope) |
15 .then(function(registration) { | 16 .then(function(registration) { |
16 add_completion_callback(function() { registration.unregister(); }); | 17 add_completion_callback(function() { registration.unregister(); }); |
17 return wait_for_state(t, registration.installing, 'activated'); | 18 return wait_for_state(t, registration.installing, 'activated'); |
18 }) | 19 }) |
19 .then(function() { | 20 .then(function() { |
(...skipping 10 matching lines...) Expand all Loading... |
30 return new Promise(function(resolve) { | 31 return new Promise(function(resolve) { |
31 var w = new SharedWorker(shared_worker_url); | 32 var w = new SharedWorker(shared_worker_url); |
32 w.port.onmessage = function(e) { | 33 w.port.onmessage = function(e) { |
33 resolve(e.data.clientId); | 34 resolve(e.data.clientId); |
34 }; | 35 }; |
35 }); | 36 }); |
36 }) | 37 }) |
37 .then(function(client_id) { | 38 .then(function(client_id) { |
38 client_ids.push(client_id); | 39 client_ids.push(client_id); |
39 var channel = new MessageChannel(); | 40 var channel = new MessageChannel(); |
| 41 var w = new Worker(worker_url); |
| 42 w.postMessage({cmd:'GetClientId', port:channel.port2}, |
| 43 [channel.port2]); |
| 44 return new Promise(function(resolve) { |
| 45 channel.port1.onmessage = function(e) { |
| 46 resolve(e.data.clientId); |
| 47 }; |
| 48 }); |
| 49 }) |
| 50 .then(function(client_id) { |
| 51 client_ids.push(client_id); |
| 52 var channel = new MessageChannel(); |
| 53 frame.contentWindow.postMessage('StartWorker', '*', [channel.port2]); |
| 54 return new Promise(function(resolve) { |
| 55 channel.port1.onmessage = function(e) { |
| 56 resolve(e.data.clientId); |
| 57 }; |
| 58 }); |
| 59 }) |
| 60 .then(function(client_id) { |
| 61 client_ids.push(client_id); |
| 62 var channel = new MessageChannel(); |
40 var saw_message = new Promise(function(resolve) { | 63 var saw_message = new Promise(function(resolve) { |
41 channel.port1.onmessage = resolve; | 64 channel.port1.onmessage = resolve; |
42 }); | 65 }); |
43 frame.contentWindow.navigator.serviceWorker.controller.postMessage( | 66 frame.contentWindow.navigator.serviceWorker.controller.postMessage( |
44 {port: channel.port2, clientIds: client_ids}, [channel.port2]); | 67 {port: channel.port2, clientIds: client_ids}, [channel.port2]); |
45 return saw_message; | 68 return saw_message; |
46 }) | 69 }) |
47 .then(function(e) { | 70 .then(function(e) { |
48 assert_equals(e.data.length, 2); | 71 assert_equals(e.data.length, expected.length); |
49 assert_array_equals(e.data[0], expected[0]); | 72 // We use these assert_not_equals because assert_array_equals doesn't |
50 assert_array_equals(e.data[1], expected[1]); | 73 // print the error description when passed an undefined value. |
| 74 assert_not_equals(e.data[0], undefined, |
| 75 'Window client should not be undefined'); |
| 76 assert_array_equals(e.data[0], expected[0], 'Window client'); |
| 77 assert_not_equals(e.data[1], undefined, |
| 78 'Shared worker client should not be undefined'); |
| 79 assert_array_equals(e.data[1], expected[1], 'Shared worker client'); |
| 80 assert_not_equals(e.data[2], undefined, |
| 81 'Worker(Started by main frame) client should not be undefined'); |
| 82 assert_array_equals(e.data[2], expected[2], |
| 83 'Worker(Started by main frame) client'); |
| 84 assert_not_equals(e.data[3], undefined, |
| 85 'Worker(Started by sub frame) client should not be undefined'); |
| 86 assert_array_equals(e.data[3], expected[3], |
| 87 'Worker(Started by sub frame) client'); |
51 }); | 88 }); |
52 }, 'Test Clients.get() with window and worker clients'); | 89 }, 'Test Clients.get() with window and worker clients'); |
53 | 90 |
54 function wait_for_clientId() { | 91 function wait_for_clientId() { |
55 return new Promise(function(resolve) { | 92 return new Promise(function(resolve) { |
56 function get_client_id(e) { | 93 function get_client_id(e) { |
57 window.removeEventListener('message', get_client_id); | 94 window.removeEventListener('message', get_client_id); |
58 resolve(e.data.clientId); | 95 resolve(e.data.clientId); |
59 } | 96 } |
60 window.addEventListener('message', get_client_id, false); | 97 window.addEventListener('message', get_client_id, false); |
61 }); | 98 }); |
62 } | 99 } |
63 | 100 |
64 var expected = [ | 101 var expected = [ |
65 // visibilityState, focused, url, type, frameType | 102 // visibilityState, focused, url, type, frameType |
66 ['visible', true, normalizeURL(scope) + '-frame.html', 'window', 'nested'], | 103 ['visible', true, normalizeURL(scope) + '-frame.html', 'window', 'nested'], |
67 [undefined, undefined, normalizeURL(scope) + '-shared-worker.js', 'sharedwor
ker', 'none'] | 104 [undefined, undefined, normalizeURL(scope) + '-shared-worker.js', 'sharedwor
ker', 'none'], |
| 105 [undefined, undefined, normalizeURL(scope) + '-worker.js', 'worker', 'none']
, |
| 106 [undefined, undefined, normalizeURL(scope) + '-frame-worker.js', 'worker', '
none'] |
68 ]; | 107 ]; |
69 </script> | 108 </script> |
OLD | NEW |