common_temporaryFileBlob.js (2883B)
1 // This file expects next() to be defined in the scope it is imported into. 2 /* global next */ 3 var data = new Array(256).join("1234567890ABCDEF"); 4 5 function createXHR() { 6 var xhr = new XMLHttpRequest(); 7 xhr.open("POST", "temporaryFileBlob.sjs"); 8 xhr.responseType = "blob"; 9 xhr.send({ 10 toString() { 11 return data; 12 }, 13 }); 14 return xhr; 15 } 16 17 function test_simple() { 18 info("Simple test"); 19 20 var xhr = createXHR(); 21 22 xhr.onloadend = function () { 23 ok(xhr.response instanceof Blob, "We have a blob!"); 24 ok(!(xhr.response instanceof File), "Our blob is not a file!"); 25 if ("SpecialPowers" in self) { 26 is( 27 SpecialPowers.wrap(xhr.response).blobImplType, 28 "StreamBlobImpl[TemporaryFileBlobImpl]", 29 "We have a blob stored into a stream file" 30 ); 31 } 32 is(xhr.response.size, data.length, "Data length matches"); 33 34 var fr = new FileReader(); 35 fr.readAsText(xhr.response); 36 fr.onload = function () { 37 is(fr.result, data, "Data content matches"); 38 next(); 39 }; 40 }; 41 } 42 43 function test_abort() { 44 info("Aborting during onloading"); 45 46 var xhr = createXHR(); 47 48 xhr.onprogress = function () { 49 xhr.abort(); 50 }; 51 52 xhr.onloadend = function () { 53 ok(!xhr.response, "We should not have a Blob!"); 54 next(); 55 }; 56 } 57 58 function test_reuse() { 59 info("Reuse test"); 60 61 var xhr = createXHR(); 62 63 var count = 0; 64 xhr.onloadend = function () { 65 ok(xhr.response instanceof Blob, "We have a blob!"); 66 ok(!(xhr.response instanceof File), "Our blob is not a file!"); 67 if ("SpecialPowers" in self) { 68 is( 69 SpecialPowers.wrap(xhr.response).blobImplType, 70 "StreamBlobImpl[TemporaryFileBlobImpl]", 71 "We have a blob stored into a stream file" 72 ); 73 } 74 is(xhr.response.size, data.length, "Data length matches"); 75 76 var fr = new FileReader(); 77 fr.readAsText(xhr.response); 78 fr.onload = function () { 79 is(fr.result, data, "Data content matches"); 80 if (++count > 2) { 81 next(); 82 return; 83 } 84 85 xhr.open("POST", "temporaryFileBlob.sjs"); 86 xhr.responseType = "blob"; 87 xhr.send({ 88 toString() { 89 return data; 90 }, 91 }); 92 }; 93 }; 94 } 95 96 function test_worker_generic(test) { 97 var w = new Worker("worker_temporaryFileBlob.js"); 98 w.onmessage = function (e) { 99 if (e.data.type == "info") { 100 info(e.data.msg); 101 } else if (e.data.type == "check") { 102 ok(e.data.what, e.data.msg); 103 } else if (e.data.type == "finish") { 104 next(); 105 } else { 106 ok(false, "Something wrong happened"); 107 } 108 }; 109 110 w.postMessage(test); 111 } 112 113 function test_worker() { 114 info("XHR in workers"); 115 test_worker_generic("simple"); 116 } 117 118 function test_worker_abort() { 119 info("XHR in workers"); 120 test_worker_generic("abort"); 121 } 122 123 function test_worker_reuse() { 124 info("XHR in workers"); 125 test_worker_generic("reuse"); 126 }