content/base/test/test_mozfiledataurl.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
michael@0 5 <title>Test for File urls</title>
michael@0 6 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 8 </head>
michael@0 9 <body onload="gen.next()">
michael@0 10 <p id="display">
michael@0 11 <iframe id=inner></iframe>
michael@0 12 <iframe id=iframe></iframe>
michael@0 13 <img id=img onload="gen.send(event);">
michael@0 14 <audio id=audio onloadeddata="gen.send(event);">
michael@0 15 <input type=file id=fileList>
michael@0 16 </p>
michael@0 17 <div id="content" style="display: none">
michael@0 18
michael@0 19 </div>
michael@0 20 <pre id="test">
michael@0 21 <script class="testbody" type="application/javascript;version=1.8">
michael@0 22
michael@0 23 try {
michael@0 24 URL.createObjectURL(undefined);
michael@0 25 } catch(e) { }
michael@0 26
michael@0 27 window.addEventListener("message", function(e) {
michael@0 28 gen.send(JSON.parse(e.data));
michael@0 29 }, false);
michael@0 30
michael@0 31 const innerSameSiteURI = "file_mozfiledataurl_inner.html";
michael@0 32 const innerCrossSiteURI = "http://example.com/tests/content/base/test/file_mozfiledataurl_inner.html"
michael@0 33
michael@0 34 gen = runTest();
michael@0 35 SimpleTest.waitForExplicitFinish();
michael@0 36
michael@0 37 function runTest() {
michael@0 38 inner = document.getElementById('inner');
michael@0 39 img = document.getElementById('img');
michael@0 40 audio = document.getElementById('audio');
michael@0 41 iframe = document.getElementById('iframe');
michael@0 42 inner.onload = function() { gen.send("inner loaded"); };
michael@0 43
michael@0 44 // Attempt to load a image in this document
michael@0 45 var file = getFile("file_mozfiledataurl_img.jpg");
michael@0 46 var fileurl = URL.createObjectURL(file);
michael@0 47 img.src = fileurl;
michael@0 48 var e = (yield);
michael@0 49 is(e.type, "load", "loaded successfully");
michael@0 50 is(img.width, 120, "correct width");
michael@0 51 is(img.height, 90, "correct height");
michael@0 52
michael@0 53 // Revoke url and attempt to load a image in this document
michael@0 54 img.src = "file_mozfiledataurl_img.jpg";
michael@0 55 is((yield).type, "load", "successfull reset image");
michael@0 56 URL.revokeObjectURL(fileurl);
michael@0 57 todo(false, "urls need to act like 404s, not fail to parse");
michael@0 58 /* img.src = fileurl;
michael@0 59 var e = (yield);
michael@0 60 is(e.type, "error", "failed successfully");
michael@0 61 isnot(img.width, 120, "correct error width");
michael@0 62 isnot(img.height, 90, "correct error height");
michael@0 63 */
michael@0 64 // Generate new fileurl and make sure it's different from the old
michael@0 65 var oldFileurl = fileurl;
michael@0 66 var fileurl = URL.createObjectURL(file);
michael@0 67 isnot(fileurl, oldFileurl, "URL.createObjectURL generated the same url twice");
michael@0 68
michael@0 69 // Attempt to load an image in a different same-origin document
michael@0 70 inner.src = innerSameSiteURI;
michael@0 71 yield undefined;
michael@0 72 inner.contentWindow.postMessage(JSON.stringify({img:fileurl}), "*");
michael@0 73 var res = (yield);
michael@0 74 is(res.type, "load", "loaded successfully");
michael@0 75 is(res.width, 120, "correct width");
michael@0 76 is(res.height, 90, "correct height");
michael@0 77
michael@0 78 // Attempt to load an image in a different cross-origin document
michael@0 79 inner.src = innerCrossSiteURI;
michael@0 80 yield undefined;
michael@0 81 inner.contentWindow.postMessage(JSON.stringify({img:fileurl}), "*");
michael@0 82 var res = (yield);
michael@0 83 is(res.type, "error", "failed successfully");
michael@0 84 isnot(res.width, 120, "correct error width");
michael@0 85 isnot(res.height, 90, "correct error height");
michael@0 86
michael@0 87 // Attempt to load an audio in this document
michael@0 88 var file = getFile("file_mozfiledataurl_audio.ogg");
michael@0 89 var fileurl = URL.createObjectURL(file);
michael@0 90 audio.src = fileurl;
michael@0 91 var e = (yield);
michael@0 92 is(e.type, "loadeddata", "loaded successfully");
michael@0 93
michael@0 94 // Revoke url and attempt to load a audio in this document
michael@0 95 audio.src = "file_mozfiledataurl_audio.ogg";
michael@0 96 is((yield).type, "loadeddata", "successfully reset audio");
michael@0 97 URL.revokeObjectURL(fileurl);
michael@0 98 todo(false, "urls need to act like 404s, not fail to parse");
michael@0 99 /* img.src = fileurl;
michael@0 100 var e = (yield);
michael@0 101 is(e.type, "error", "failed successfully");
michael@0 102 isnot(img.width, 120, "correct error width");
michael@0 103 isnot(img.height, 90, "correct error height");
michael@0 104 */
michael@0 105 // Generate new fileurl and make sure it's different from the old
michael@0 106 var oldFileurl = fileurl;
michael@0 107 var fileurl = URL.createObjectURL(file);
michael@0 108 isnot(fileurl, oldFileurl, "URL.createObjectURL generated the same url twice");
michael@0 109
michael@0 110 // Attempt to load an audio in a different same-origin document
michael@0 111 inner.src = innerSameSiteURI;
michael@0 112 yield undefined;
michael@0 113 inner.contentWindow.postMessage(JSON.stringify({audio:fileurl}), "*");
michael@0 114 var res = (yield);
michael@0 115 is(res.type, "loadeddata", "loaded successfully");
michael@0 116
michael@0 117 // Attempt to load an audio in a different cross-origin document
michael@0 118 inner.src = innerCrossSiteURI;
michael@0 119 yield undefined;
michael@0 120 inner.contentWindow.postMessage(JSON.stringify({audio:fileurl}), "*");
michael@0 121 var res = (yield);
michael@0 122 is(res.type, "error", "failed successfully");
michael@0 123
michael@0 124 // Attempt to load a HTML document in an iframe in this document
michael@0 125 iframe.onload = function() { gen.next(); };
michael@0 126 iframe.src = "file_mozfiledataurl_doc.html";
michael@0 127 yield undefined;
michael@0 128 is(iframe.contentDocument.getElementsByTagName("p")[0].textContent,
michael@0 129 "This here is a document!",
michael@0 130 "iframe loaded successfully");
michael@0 131 is(iframe.contentDocument.getElementById("img").width, 120,
michael@0 132 "image in iframe width");
michael@0 133 is(iframe.contentDocument.getElementById("img").height, 90,
michael@0 134 "image in iframe height");
michael@0 135
michael@0 136 // Attempt to load a HTML document in an iframe in this document, using file url
michael@0 137 file = getFile("file_mozfiledataurl_doc.html");
michael@0 138 fileurl = URL.createObjectURL(file);
michael@0 139 iframe.src = fileurl;
michael@0 140 yield undefined;
michael@0 141 is(iframe.contentDocument.getElementsByTagName("p")[0].textContent,
michael@0 142 "This here is a document!",
michael@0 143 "iframe loaded successfully");
michael@0 144 isnot(iframe.contentDocument.getElementById("img").width, 120,
michael@0 145 "failed image in iframe width");
michael@0 146 isnot(iframe.contentDocument.getElementById("img").height, 90,
michael@0 147 "failed image in iframe height");
michael@0 148
michael@0 149 // Attempt to load a HTML document in an iframe in inner document
michael@0 150 inner.src = innerSameSiteURI;
michael@0 151 is((yield), "inner loaded", "correct gen.next()");
michael@0 152 inner.contentWindow.postMessage(JSON.stringify({iframe:"file_mozfiledataurl_doc.html"}), "*");
michael@0 153 var res = (yield);
michael@0 154 is(res.type, "load", "loaded successfully");
michael@0 155 is(res.text, "This here is a document!", "loaded successfully");
michael@0 156 is(res.imgWidth, 120, "correct width");
michael@0 157
michael@0 158 // Attempt to load a HTML document in an iframe in inner document, using file url
michael@0 159 inner.contentWindow.postMessage(JSON.stringify({iframe:fileurl}), "*");
michael@0 160 var res = (yield);
michael@0 161 is(res.type, "load", "loaded successfully");
michael@0 162 is(res.text, "This here is a document!", "loaded successfully");
michael@0 163 isnot(res.imgWidth, 120, "correct width");
michael@0 164
michael@0 165 // Attempt to load a HTML document in an iframe in inner cross-site document, using file url
michael@0 166 inner.src = innerCrossSiteURI;
michael@0 167 is((yield), "inner loaded", "correct gen.next()");
michael@0 168 inner.contentWindow.postMessage(JSON.stringify({iframe:fileurl}), "*");
michael@0 169 var res = (yield);
michael@0 170 is(res.type, "error", "load failed successfully");
michael@0 171
michael@0 172 // Attempt to load file url using XHR
michael@0 173 file = getFile("file_mozfiledataurl_text.txt");
michael@0 174 fileurl = URL.createObjectURL(file);
michael@0 175 xhr = new XMLHttpRequest;
michael@0 176 xhr.onload = function() { gen.send("XHR finished"); };
michael@0 177 xhr.open("GET", fileurl);
michael@0 178 xhr.send();
michael@0 179 is((yield), "XHR finished", "correct gen.next()");
michael@0 180 xhr.responseText == "Yarr, here be plaintext file, ya landlubber\n";
michael@0 181
michael@0 182 // Attempt to load file url using XHR in inner document
michael@0 183 inner.src = innerSameSiteURI;
michael@0 184 is((yield), "inner loaded", "correct gen.next()");
michael@0 185 inner.contentWindow.postMessage(JSON.stringify({xhr:fileurl}), "*");
michael@0 186 var res = (yield);
michael@0 187 is(res.didThrow, undefined, "load successful");
michael@0 188 is(res.text, "Yarr, here be plaintext file, ya landlubber\n", "load successful");
michael@0 189
michael@0 190 // Attempt to load file url using XHR
michael@0 191 inner.src = innerCrossSiteURI;
michael@0 192 is((yield), "inner loaded", "correct gen.next()");
michael@0 193 inner.contentWindow.postMessage(JSON.stringify({xhr:fileurl}), "*");
michael@0 194 var res = (yield);
michael@0 195 is(res.didThrow, true, "load failed successfully");
michael@0 196
michael@0 197 SimpleTest.finish();
michael@0 198
michael@0 199 yield undefined;
michael@0 200 }
michael@0 201
michael@0 202
michael@0 203 var basePath = "";
michael@0 204 function getFile(name) {
michael@0 205 if (!basePath) {
michael@0 206 let xhr = new XMLHttpRequest;
michael@0 207 xhr.open("GET", "/dynamic/getMyDirectory.sjs", false);
michael@0 208 xhr.send();
michael@0 209 basePath = xhr.responseText;
michael@0 210 }
michael@0 211
michael@0 212 var fileList = document.getElementById('fileList');
michael@0 213 SpecialPowers.wrap(fileList).value = basePath + name;
michael@0 214
michael@0 215 return fileList.files[0];
michael@0 216 }
michael@0 217
michael@0 218 </script>
michael@0 219 </pre>
michael@0 220 </body>
michael@0 221 </html>

mercurial