|
| 1 | +<!DOCTYPE html> |
| 2 | +<title>Service Worker: Clients.matchAll</title> |
| 3 | +<script src="/resources/testharness.js"></script> |
| 4 | +<script src="/resources/testharnessreport.js"></script> |
| 5 | +<script src="resources/test-helpers.sub.js"></script> |
| 6 | +<script> |
| 7 | +var scope = 'resources/clients-frame-freeze.html'; |
| 8 | +var windows = []; |
| 9 | +var expected_window_1 = |
| 10 | + {visibilityState: 'visible', focused: false, lifecycleState: "frozen", url: new URL(scope + '#1', location).toString(), type: 'window', frameType: 'top-level'}; |
| 11 | +var expected_window_2 = |
| 12 | + {visibilityState: 'visible', focused: false, lifecycleState: "active", url: new URL(scope + '#2', location).toString(), type: 'window', frameType: 'top-level'}; |
| 13 | +function with_window(url, name) { |
| 14 | + return new Promise(function(resolve) { |
| 15 | + var child = window.open(url, name); |
| 16 | + window.onmessage = () => {resolve(child)}; |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +promise_test(function(t) { |
| 21 | + return service_worker_unregister_and_register( |
| 22 | + t, 'resources/clients-matchall-worker.js', scope) |
| 23 | + .then(function(registration) { |
| 24 | + t.add_cleanup(function() { |
| 25 | + return service_worker_unregister(t, scope); |
| 26 | + }); |
| 27 | + |
| 28 | + return wait_for_state(t, registration.installing, 'activated'); |
| 29 | + }) |
| 30 | + .then(function() { return with_window(scope + '#1', 'Child 1'); }) |
| 31 | + .then(function(window1) { |
| 32 | + windows.push(window1); |
| 33 | + return with_window(scope + '#2', 'Child 2'); |
| 34 | + }) |
| 35 | + .then(function(window2) { |
| 36 | + windows.push(window2); |
| 37 | + return new Promise(function(resolve) { |
| 38 | + window.onmessage = resolve; |
| 39 | + windows[0].postMessage('freeze'); |
| 40 | + }); |
| 41 | + }) |
| 42 | + .then(function() { |
| 43 | + var channel = new MessageChannel(); |
| 44 | + |
| 45 | + return new Promise(function(resolve) { |
| 46 | + channel.port1.onmessage = resolve; |
| 47 | + windows[1].navigator.serviceWorker.controller.postMessage( |
| 48 | + {port:channel.port2, includeLifecycleState: true}, [channel.port2]); |
| 49 | + }); |
| 50 | + }) |
| 51 | + .then(function(e) { |
| 52 | + assert_equals(e.data.length, 1); |
| 53 | + assert_object_equals(e.data[0], expected_window_2); |
| 54 | + }) |
| 55 | + .then(function() { |
| 56 | + var channel = new MessageChannel(); |
| 57 | + |
| 58 | + return new Promise(function(resolve) { |
| 59 | + channel.port1.onmessage = resolve; |
| 60 | + windows[1].navigator.serviceWorker.controller.postMessage( |
| 61 | + {port:channel.port2, options: {lifecycleState: "all"}, includeLifecycleState: true}, [channel.port2]); |
| 62 | + }); |
| 63 | + }) |
| 64 | + .then(function(e) { |
| 65 | + assert_equals(e.data.length, 2); |
| 66 | + // No specific order is required, so support inversion. |
| 67 | + if (e.data[0][3] == new URL(scope + '#2', location)) { |
| 68 | + assert_object_equals(e.data[0], expected_window_2); |
| 69 | + assert_object_equals(e.data[1], expected_window_1); |
| 70 | + } else { |
| 71 | + assert_object_equals(e.data[0], expected_window_1); |
| 72 | + assert_object_equals(e.data[1], expected_window_2); |
| 73 | + } |
| 74 | + }) |
| 75 | + .then(function() { |
| 76 | + var channel = new MessageChannel(); |
| 77 | + |
| 78 | + return new Promise(function(resolve) { |
| 79 | + channel.port1.onmessage = resolve; |
| 80 | + windows[1].navigator.serviceWorker.controller.postMessage( |
| 81 | + {port:channel.port2, options: {lifecycleState: "frozen"}, includeLifecycleState: true}, [channel.port2]); |
| 82 | + }); |
| 83 | + }) |
| 84 | + .then(function(e) { |
| 85 | + assert_equals(e.data.length, 1); |
| 86 | + assert_object_equals(e.data[0], expected_window_1); |
| 87 | + }) |
| 88 | + .then(function() { |
| 89 | + var channel = new MessageChannel(); |
| 90 | + |
| 91 | + return new Promise(function(resolve) { |
| 92 | + channel.port1.onmessage = resolve; |
| 93 | + windows[1].navigator.serviceWorker.controller.postMessage( |
| 94 | + {port:channel.port2, options: {lifecycleState: "active"}, includeLifecycleState: true}, [channel.port2]); |
| 95 | + }); |
| 96 | + }) |
| 97 | + .then(function(e) { |
| 98 | + assert_equals(e.data.length, 1); |
| 99 | + assert_object_equals(e.data[0], expected_window_2); |
| 100 | + }); |
| 101 | +}, 'Test Clients.matchAll()'); |
| 102 | + |
| 103 | +</script> |
0 commit comments