test_blob_worker_xhr_read.html (3250B)
1 <!-- 2 Any copyright is dedicated to the Public Domain. 3 http://creativecommons.org/publicdomain/zero/1.0/ 4 --> 5 <html> 6 <head> 7 <title>Indexed Database Blob Read From Worker</title> 8 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 11 12 <script type="text/javascript"> 13 /** 14 * Create an IndexedDB-backed Blob, send it to the worker, try and read the 15 * contents of the Blob from the worker using an XHR. Ideally, we don't 16 * deadlock the main thread. 17 */ 18 function* testSteps() 19 { 20 const BLOB_DATA = ["Green"]; 21 const BLOB_TYPE = "text/plain"; 22 const BLOB_SIZE = BLOB_DATA.join("").length; 23 24 info("Setting up"); 25 26 let request = indexedDB.open(window.location.pathname, 1); 27 request.onerror = errorHandler; 28 request.onupgradeneeded = grabEventAndContinueHandler; 29 request.onsuccess = unexpectedSuccessHandler; 30 let event = yield undefined; 31 32 let db = event.target.result; 33 db.onerror = errorHandler; 34 35 ok(db, "Created database"); 36 37 info("Creating objectStore"); 38 39 let objectStore = db.createObjectStore("foo", { autoIncrement: true }); 40 41 request.onupgradeneeded = unexpectedSuccessHandler; 42 request.onsuccess = grabEventAndContinueHandler; 43 event = yield undefined; 44 45 ok(true, "Opened database"); 46 47 let blob = new Blob(BLOB_DATA, { type: BLOB_TYPE }); 48 49 info("Adding blob to database"); 50 51 objectStore = db.transaction("foo", "readwrite").objectStore("foo"); 52 objectStore.add(blob).onsuccess = grabEventAndContinueHandler; 53 event = yield undefined; 54 55 let blobKey = event.target.result; 56 ok(blobKey, "Got a key for the blob"); 57 58 info("Getting blob from the database"); 59 60 objectStore = db.transaction("foo").objectStore("foo"); 61 objectStore.get(blobKey).onsuccess = grabEventAndContinueHandler; 62 event = yield undefined; 63 64 blob = event.target.result; 65 66 ok(blob instanceof Blob, "Got a blob"); 67 is(blob.size, BLOB_SIZE, "Correct size"); 68 is(blob.type, BLOB_TYPE, "Correct type"); 69 70 info("Sending blob to a worker"); 71 72 function workerScript() { 73 /* eslint-env worker */ 74 onmessage = function(event) { 75 var blob = event.data; 76 var blobUrl = URL.createObjectURL(blob); 77 var xhr = new XMLHttpRequest(); 78 xhr.open("GET", blobUrl, true); 79 xhr.responseType = "text"; 80 xhr.onload = function() { 81 postMessage({ data: xhr.response }); 82 URL.revokeObjectURL(blobUrl); 83 }; 84 xhr.onerror = function() { 85 postMessage({ data: null }); 86 URL.revokeObjectURL(blobUrl); 87 }; 88 xhr.send(); 89 }; 90 } 91 92 let workerScriptUrl = 93 URL.createObjectURL(new Blob(["(", workerScript.toString(), ")()"])); 94 95 let xhrWorker = new Worker(workerScriptUrl); 96 xhrWorker.postMessage(blob); 97 xhrWorker.onmessage = grabEventAndContinueHandler; 98 event = yield undefined; 99 100 is(event.data.data, "Green", "XHR returned expected payload."); 101 xhrWorker.terminate(); 102 103 URL.revokeObjectURL(workerScriptUrl); 104 105 finishTest(); 106 } 107 </script> 108 <script type="text/javascript" src="helpers.js"></script> 109 110 </head> 111 112 <body onload="runTest();"></body> 113 114 </html>