workerStorageAllowed.js (3276B)
1 // Unfortunately, workers can't share the code from storagePermissionsUtils. 2 // These are basic mechanisms for communicating to the test runner. 3 4 function ok(condition, text) { 5 if (!condition) { 6 self.postMessage("FAILURE: " + text); 7 } else { 8 self.postMessage(text); 9 } 10 } 11 12 function finishTest() { 13 self.postMessage("done"); 14 self.close(); 15 } 16 17 // Make sure that we can access indexedDB 18 function idbTest() { 19 try { 20 indexedDB; 21 22 const idbcycle = new Promise((resolve, reject) => { 23 const begin = indexedDB.open("door"); 24 begin.onerror = e => { 25 reject(e); 26 }; 27 begin.onsuccess = () => { 28 indexedDB 29 .databases() 30 .then(dbs => { 31 ok( 32 dbs.some(elem => elem.name === "door"), 33 "WORKER just created database should be found" 34 ); 35 const end = indexedDB.deleteDatabase("door"); 36 end.onerror = e => { 37 reject(e); 38 }; 39 end.onsuccess = () => { 40 resolve(); 41 }; 42 }) 43 .catch(err => { 44 reject(err); 45 }); 46 }; 47 }); 48 49 idbcycle.then( 50 () => { 51 ok(true, "WORKER getting indexedDB didn't throw"); 52 cacheTest(); 53 }, 54 e => { 55 ok(false, "WORKER getting indexedDB threw " + e.message); 56 cacheTest(); 57 } 58 ); 59 } catch (e) { 60 ok(false, "WORKER getting indexedDB should not throw"); 61 cacheTest(); 62 } 63 } 64 65 // Make sure that we can access caches 66 function cacheTest() { 67 try { 68 var promise = caches.keys(); 69 ok(true, "WORKER getting caches didn't throw"); 70 71 promise.then( 72 function () { 73 ok( 74 location.protocol == "https:", 75 "WORKER The promise was not rejected" 76 ); 77 workerTest(); 78 }, 79 function () { 80 ok( 81 location.protocol !== "https:", 82 "WORKER The promise should not have been rejected" 83 ); 84 workerTest(); 85 } 86 ); 87 } catch (e) { 88 ok( 89 location.protocol !== "https:", 90 "WORKER getting caches should not have thrown" 91 ); 92 workerTest(); 93 } 94 } 95 96 // Try to spawn an inner worker, and make sure that it can also access storage 97 function workerTest() { 98 if (location.hash != "#outer") { 99 // Don't recurse infinitely, if we are the inner worker, don't spawn another 100 finishTest(); 101 return; 102 } 103 // Create the inner worker, and listen for test messages from it 104 var worker = new Worker("workerStorageAllowed.js#inner"); 105 worker.addEventListener("message", function (e) { 106 if (e.data == "done") { 107 finishTest(); 108 return; 109 } 110 111 ok( 112 !e.data.match(/^FAILURE/), 113 e.data + " (WORKER = workerStorageAllowed.js#inner)" 114 ); 115 }); 116 117 worker.addEventListener("error", function (e) { 118 ok(false, e.data + " (WORKER = workerStorageAllowed.js#inner)"); 119 120 finishTest(); 121 }); 122 } 123 124 try { 125 // Workers don't have access to localstorage or sessionstorage 126 ok( 127 typeof self.localStorage == "undefined", 128 "localStorage should be undefined" 129 ); 130 ok( 131 typeof self.sessionStorage == "undefined", 132 "sessionStorage should be undefined" 133 ); 134 135 idbTest(); 136 } catch (e) { 137 ok(false, "WORKER Unwelcome exception received"); 138 finishTest(); 139 }