OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Service Worker: Navigate a Window</title> |
| 3 <script src="/resources/testharness.js"></script> |
| 4 <script src="/resources/testharnessreport.js"></script> |
| 5 <script src="resources/get-host-info.sub.js"></script> |
| 6 <script src="resources/test-helpers.sub.js"></script> |
| 7 <body> |
| 8 <script> |
| 9 var host_info = get_host_info(); |
| 10 var BASE_URL = host_info['HTTPS_ORIGIN'] + base_path(); |
| 11 |
| 12 function wait_for_message(msg) { |
| 13 return new Promise(function(resolve, reject) { |
| 14 window.addEventListener('message', function onMsg(evt) { |
| 15 if (evt.data.type === msg) { |
| 16 resolve(); |
| 17 } |
| 18 }); |
| 19 }); |
| 20 } |
| 21 |
| 22 function with_window(url) { |
| 23 var win = window.open(url); |
| 24 return wait_for_message('LOADED').then(_ => win); |
| 25 } |
| 26 |
| 27 function navigate_window(win, url) { |
| 28 win.location = url; |
| 29 return wait_for_message('LOADED').then(_ => win); |
| 30 } |
| 31 |
| 32 function reload_window(win) { |
| 33 win.location.reload(); |
| 34 return wait_for_message('LOADED').then(_ => win); |
| 35 } |
| 36 |
| 37 function go_back(win) { |
| 38 win.history.back(); |
| 39 return wait_for_message('PAGESHOW').then(_ => win); |
| 40 } |
| 41 |
| 42 function go_forward(win) { |
| 43 win.history.forward(); |
| 44 return wait_for_message('PAGESHOW').then(_ => win); |
| 45 } |
| 46 |
| 47 function get_clients(win, sw, opts) { |
| 48 return new Promise((resolve, reject) => { |
| 49 win.navigator.serviceWorker.addEventListener('message', function onMsg(evt)
{ |
| 50 win.navigator.serviceWorker.removeEventListener('message', onMsg); |
| 51 if (evt.data.type === 'success') { |
| 52 resolve(evt.data.detail); |
| 53 } else { |
| 54 reject(evt.data.detail); |
| 55 } |
| 56 }); |
| 57 sw.postMessage({ type: 'GET_CLIENTS', opts: (opts || {}) }); |
| 58 }); |
| 59 } |
| 60 |
| 61 function validate_window(win, url, opts) { |
| 62 return win.navigator.serviceWorker.getRegistration(url) |
| 63 .then(reg => { |
| 64 // In order to compare service worker instances we need to |
| 65 // make sure the DOM object is owned by the same global; the |
| 66 // opened window in this case. |
| 67 assert_equals(win.navigator.serviceWorker.controller, reg.active, |
| 68 'window should be controlled by service worker'); |
| 69 return get_clients(win, reg.active, opts); |
| 70 }) |
| 71 .then(resultList => { |
| 72 // We should always see our controlled window. |
| 73 var expected = [ |
| 74 { url: url, frameType: 'auxiliary' } |
| 75 ]; |
| 76 // If we are including uncontrolled windows, then we might see the |
| 77 // test window itself and the test harness. |
| 78 if (opts.includeUncontrolled) { |
| 79 expected.push({ url: BASE_URL + 'navigate-window.https.html', |
| 80 frameType: 'auxiliary' }); |
| 81 expected.push({ url: host_info['HTTPS_ORIGIN'] + '/testharness_runner.
html', |
| 82 frameType: 'top-level' }); |
| 83 } |
| 84 assert_equals(resultList.length, expected.length, |
| 85 'expected number of clients'); |
| 86 for (var i = 0; i < resultList.length; ++i) { |
| 87 assert_equals(resultList[i].url, expected[i].url, |
| 88 'client should have expected url'); |
| 89 assert_equals(resultList[i].frameType, expected[i].frameType, |
| 90 ' client should have expected frame type'); |
| 91 } |
| 92 return win; |
| 93 }) |
| 94 } |
| 95 |
| 96 promise_test(function(t) { |
| 97 var worker = BASE_URL + 'resources/navigate-window-worker.js'; |
| 98 var scope = BASE_URL + 'resources/loaded.html?navigate-window-controlled'; |
| 99 var url1 = scope + '&q=1'; |
| 100 var url2 = scope + '&q=2'; |
| 101 return service_worker_unregister_and_register(t, worker, scope) |
| 102 .then(reg => wait_for_state(t, reg.installing, 'activated') ) |
| 103 .then(___ => with_window(url1)) |
| 104 .then(win => validate_window(win, url1, { includeUncontrolled: false })) |
| 105 .then(win => navigate_window(win, url2)) |
| 106 .then(win => validate_window(win, url2, { includeUncontrolled: false })) |
| 107 .then(win => go_back(win)) |
| 108 .then(win => validate_window(win, url1, { includeUncontrolled: false })) |
| 109 .then(win => go_forward(win)) |
| 110 .then(win => validate_window(win, url2, { includeUncontrolled: false })) |
| 111 .then(win => reload_window(win)) |
| 112 .then(win => validate_window(win, url2, { includeUncontrolled: false })) |
| 113 .then(win => win.close()) |
| 114 .catch(unreached_rejection(t)) |
| 115 .then(___ => service_worker_unregister(t, scope)) |
| 116 }, 'Clients.matchAll() should not show an old window as controlled after ' + |
| 117 'it navigates.'); |
| 118 |
| 119 promise_test(function(t) { |
| 120 var worker = BASE_URL + 'resources/navigate-window-worker.js'; |
| 121 var scope = BASE_URL + 'resources/loaded.html?navigate-window-uncontrolled'; |
| 122 var url1 = scope + '&q=1'; |
| 123 var url2 = scope + '&q=2'; |
| 124 return service_worker_unregister_and_register(t, worker, scope) |
| 125 .then(reg => wait_for_state(t, reg.installing, 'activated') ) |
| 126 .then(___ => with_window(url1)) |
| 127 .then(win => validate_window(win, url1, { includeUncontrolled: true })) |
| 128 .then(win => navigate_window(win, url2)) |
| 129 .then(win => validate_window(win, url2, { includeUncontrolled: true })) |
| 130 .then(win => go_back(win)) |
| 131 .then(win => validate_window(win, url1, { includeUncontrolled: true })) |
| 132 .then(win => go_forward(win)) |
| 133 .then(win => validate_window(win, url2, { includeUncontrolled: true })) |
| 134 .then(win => reload_window(win)) |
| 135 .then(win => validate_window(win, url2, { includeUncontrolled: true })) |
| 136 .then(win => win.close()) |
| 137 .catch(unreached_rejection(t)) |
| 138 .then(___ => service_worker_unregister(t, scope)) |
| 139 }, 'Clients.matchAll() should not show an old window after it navigates.'); |
| 140 </script> |
| 141 </body> |
OLD | NEW |