browser_worker-03.js (1504B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Tests that the devtools/shared/worker can handle: 7 // returned primitives (or promise or Error) 8 // 9 // And tests `workerify` by doing so. 10 11 const { workerify } = ChromeUtils.importESModule( 12 "resource://devtools/shared/worker/worker.sys.mjs" 13 ); 14 function square(x) { 15 return x * x; 16 } 17 18 function squarePromise(x) { 19 return new Promise(resolve => resolve(x * x)); 20 } 21 22 function squareError() { 23 return new Error("Nope"); 24 } 25 26 function squarePromiseReject() { 27 return new Promise((_, reject) => reject("Nope")); 28 } 29 30 registerCleanupFunction(function () { 31 Services.prefs.clearUserPref("security.allow_parent_unrestricted_js_loads"); 32 }); 33 34 add_task(async function () { 35 // Needed for blob:null 36 Services.prefs.setBoolPref( 37 "security.allow_parent_unrestricted_js_loads", 38 true 39 ); 40 let fn = workerify(square); 41 is(await fn(5), 25, "return primitives successful"); 42 fn.destroy(); 43 44 fn = workerify(squarePromise); 45 is(await fn(5), 25, "promise primitives successful"); 46 fn.destroy(); 47 48 fn = workerify(squareError); 49 try { 50 await fn(5); 51 ok(false, "return error should reject"); 52 } catch (e) { 53 ok(true, "return error should reject"); 54 } 55 fn.destroy(); 56 57 fn = workerify(squarePromiseReject); 58 try { 59 await fn(5); 60 ok(false, "returned rejected promise rejects"); 61 } catch (e) { 62 ok(true, "returned rejected promise rejects"); 63 } 64 fn.destroy(); 65 });