openWindow.serviceworker.js (5147B)
1 // the worker won't shut down between events because we increased 2 // the timeout values. 3 var client; 4 var window_count = 0; 5 var expected_window_count = 9; 6 var isolated_window_count = 0; 7 var expected_isolated_window_count = 2; 8 var resolve_got_all_windows = null; 9 var got_all_windows = new Promise(res => { 10 resolve_got_all_windows = res; 11 }); 12 13 // |expected_window_count| needs to be updated for every new call that's 14 // expected to actually open a new window regardless of what |clients.openWindow| 15 // returns. 16 function testForUrl(url, throwType, clientProperties, resultsArray) { 17 return clients 18 .openWindow(url) 19 .then(function (e) { 20 if (throwType != null) { 21 resultsArray.push({ 22 result: false, 23 message: "openWindow should throw " + throwType, 24 }); 25 } else if (clientProperties) { 26 resultsArray.push({ 27 result: e instanceof WindowClient, 28 message: `openWindow should resolve to a WindowClient for url ${url}, got ${e}`, 29 }); 30 resultsArray.push({ 31 result: e.url == clientProperties.url, 32 message: "Client url should be " + clientProperties.url, 33 }); 34 // Add more properties 35 } else { 36 resultsArray.push({ 37 result: e == null, 38 message: "Open window should resolve to null. Got: " + e, 39 }); 40 } 41 }) 42 .catch(function (err) { 43 if (throwType == null) { 44 resultsArray.push({ 45 result: false, 46 message: "Unexpected throw: " + err, 47 }); 48 } else { 49 resultsArray.push({ 50 result: err.toString().includes(throwType), 51 message: "openWindow should throw: " + err, 52 }); 53 } 54 }); 55 } 56 57 onmessage = function (event) { 58 if (event.data == "testNoPopup") { 59 client = event.source; 60 61 var results = []; 62 var promises = []; 63 promises.push(testForUrl("about:blank", "TypeError", null, results)); 64 promises.push( 65 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 66 testForUrl("http://example.com", "InvalidAccessError", null, results) 67 ); 68 promises.push( 69 testForUrl("_._*`InvalidURL", "InvalidAccessError", null, results) 70 ); 71 event.waitUntil( 72 Promise.all(promises).then(() => { 73 client.postMessage(results); 74 }) 75 ); 76 } 77 78 if (event.data == "NEW_WINDOW" || event.data == "NEW_ISOLATED_WINDOW") { 79 window_count += 1; 80 if (event.data == "NEW_ISOLATED_WINDOW") { 81 isolated_window_count += 1; 82 } 83 if (window_count == expected_window_count) { 84 resolve_got_all_windows(); 85 } 86 } 87 88 if (event.data == "CHECK_NUMBER_OF_WINDOWS") { 89 event.waitUntil( 90 got_all_windows 91 .then(function () { 92 return clients.matchAll(); 93 }) 94 .then(function (cl) { 95 event.source.postMessage([ 96 { 97 result: cl.length == expected_window_count, 98 message: `The number of windows is correct. ${cl.length} == ${expected_window_count}`, 99 }, 100 { 101 result: isolated_window_count == expected_isolated_window_count, 102 message: `The number of isolated windows is correct. ${isolated_window_count} == ${expected_isolated_window_count}`, 103 }, 104 ]); 105 for (const windowClient of cl) { 106 windowClient.postMessage("CLOSE"); 107 } 108 }) 109 ); 110 } 111 }; 112 113 onnotificationclick = function (e) { 114 var results = []; 115 var promises = []; 116 117 var redirect = 118 "https://example.com/tests/dom/notification/test/mochitest/redirect.sjs?"; 119 var redirect_xorigin = 120 "https://example.org/tests/dom/notification/test/mochitest/redirect.sjs?"; 121 var same_origin = 122 "https://example.com/tests/dom/notification/test/mochitest/open_window/client.sjs"; 123 var different_origin = 124 "https://example.org/tests/dom/notification/test/mochitest/open_window/client.sjs"; 125 126 promises.push(testForUrl("about:blank", "TypeError", null, results)); 127 promises.push(testForUrl(different_origin, null, null, results)); 128 promises.push(testForUrl(same_origin, null, { url: same_origin }, results)); 129 promises.push( 130 testForUrl("open_window/client.sjs", null, { url: same_origin }, results) 131 ); 132 133 // redirect tests 134 promises.push( 135 testForUrl( 136 redirect + "open_window/client.sjs", 137 null, 138 { url: same_origin }, 139 results 140 ) 141 ); 142 promises.push(testForUrl(redirect + different_origin, null, null, results)); 143 144 promises.push( 145 testForUrl(redirect_xorigin + "open_window/client.sjs", null, null, results) 146 ); 147 promises.push( 148 testForUrl( 149 redirect_xorigin + same_origin, 150 null, 151 { url: same_origin }, 152 results 153 ) 154 ); 155 156 // coop+coep tests 157 promises.push( 158 testForUrl( 159 same_origin + "?crossOriginIsolated=true", 160 null, 161 { url: same_origin + "?crossOriginIsolated=true" }, 162 results 163 ) 164 ); 165 promises.push( 166 testForUrl( 167 different_origin + "?crossOriginIsolated=true", 168 null, 169 null, 170 results 171 ) 172 ); 173 174 e.waitUntil( 175 Promise.all(promises).then(function () { 176 client.postMessage(results); 177 }) 178 ); 179 };