content/base/test/test_mozfiledataurl.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_mozfiledataurl.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,221 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
     1.8 +  <title>Test for File urls</title>
     1.9 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +</head>
    1.12 +<body onload="gen.next()">
    1.13 +<p id="display">
    1.14 +<iframe id=inner></iframe>
    1.15 +<iframe id=iframe></iframe>
    1.16 +<img id=img onload="gen.send(event);">
    1.17 +<audio id=audio onloadeddata="gen.send(event);">
    1.18 +<input type=file id=fileList>
    1.19 +</p>
    1.20 +<div id="content" style="display: none">
    1.21 +  
    1.22 +</div>
    1.23 +<pre id="test">
    1.24 +<script class="testbody" type="application/javascript;version=1.8">
    1.25 +
    1.26 +try {
    1.27 +  URL.createObjectURL(undefined);
    1.28 +} catch(e) { }
    1.29 +
    1.30 +window.addEventListener("message", function(e) {
    1.31 +  gen.send(JSON.parse(e.data));
    1.32 +}, false);
    1.33 +
    1.34 +const innerSameSiteURI = "file_mozfiledataurl_inner.html";
    1.35 +const innerCrossSiteURI = "http://example.com/tests/content/base/test/file_mozfiledataurl_inner.html"
    1.36 +
    1.37 +gen = runTest();
    1.38 +SimpleTest.waitForExplicitFinish();
    1.39 +
    1.40 +function runTest() {
    1.41 +  inner = document.getElementById('inner');
    1.42 +  img = document.getElementById('img');
    1.43 +  audio = document.getElementById('audio');
    1.44 +  iframe = document.getElementById('iframe');
    1.45 +  inner.onload = function() { gen.send("inner loaded"); };
    1.46 +
    1.47 +  // Attempt to load a image in this document
    1.48 +  var file = getFile("file_mozfiledataurl_img.jpg");
    1.49 +  var fileurl = URL.createObjectURL(file);
    1.50 +  img.src = fileurl;
    1.51 +  var e = (yield);
    1.52 +  is(e.type, "load", "loaded successfully");
    1.53 +  is(img.width, 120, "correct width");
    1.54 +  is(img.height, 90, "correct height");
    1.55 +
    1.56 +  // Revoke url and attempt to load a image in this document
    1.57 +  img.src = "file_mozfiledataurl_img.jpg";
    1.58 +  is((yield).type, "load", "successfull reset image");
    1.59 +  URL.revokeObjectURL(fileurl);
    1.60 +  todo(false, "urls need to act like 404s, not fail to parse");
    1.61 +/*  img.src = fileurl;
    1.62 +  var e = (yield);
    1.63 +  is(e.type, "error", "failed successfully");
    1.64 +  isnot(img.width, 120, "correct error width");
    1.65 +  isnot(img.height, 90, "correct error height");
    1.66 +*/
    1.67 +  // Generate new fileurl and make sure it's different from the old
    1.68 +  var oldFileurl = fileurl;
    1.69 +  var fileurl = URL.createObjectURL(file);
    1.70 +  isnot(fileurl, oldFileurl, "URL.createObjectURL generated the same url twice");
    1.71 +
    1.72 +  // Attempt to load an image in a different same-origin document
    1.73 +  inner.src = innerSameSiteURI;
    1.74 +  yield undefined;
    1.75 +  inner.contentWindow.postMessage(JSON.stringify({img:fileurl}), "*");
    1.76 +  var res = (yield);
    1.77 +  is(res.type, "load", "loaded successfully");
    1.78 +  is(res.width, 120, "correct width");
    1.79 +  is(res.height, 90, "correct height");
    1.80 +  
    1.81 +  // Attempt to load an image in a different cross-origin document
    1.82 +  inner.src = innerCrossSiteURI;
    1.83 +  yield undefined;
    1.84 +  inner.contentWindow.postMessage(JSON.stringify({img:fileurl}), "*");
    1.85 +  var res = (yield);
    1.86 +  is(res.type, "error", "failed successfully");
    1.87 +  isnot(res.width, 120, "correct error width");
    1.88 +  isnot(res.height, 90, "correct error height");
    1.89 +
    1.90 +  // Attempt to load an audio in this document
    1.91 +  var file = getFile("file_mozfiledataurl_audio.ogg");
    1.92 +  var fileurl = URL.createObjectURL(file);
    1.93 +  audio.src = fileurl;
    1.94 +  var e = (yield);
    1.95 +  is(e.type, "loadeddata", "loaded successfully");
    1.96 +
    1.97 +  // Revoke url and attempt to load a audio in this document
    1.98 +  audio.src = "file_mozfiledataurl_audio.ogg";
    1.99 +  is((yield).type, "loadeddata", "successfully reset audio");
   1.100 +  URL.revokeObjectURL(fileurl);
   1.101 +  todo(false, "urls need to act like 404s, not fail to parse");
   1.102 +/*  img.src = fileurl;
   1.103 +  var e = (yield);
   1.104 +  is(e.type, "error", "failed successfully");
   1.105 +  isnot(img.width, 120, "correct error width");
   1.106 +  isnot(img.height, 90, "correct error height");
   1.107 +*/
   1.108 +  // Generate new fileurl and make sure it's different from the old
   1.109 +  var oldFileurl = fileurl;
   1.110 +  var fileurl = URL.createObjectURL(file);
   1.111 +  isnot(fileurl, oldFileurl, "URL.createObjectURL generated the same url twice");
   1.112 +
   1.113 +  // Attempt to load an audio in a different same-origin document
   1.114 +  inner.src = innerSameSiteURI;
   1.115 +  yield undefined;
   1.116 +  inner.contentWindow.postMessage(JSON.stringify({audio:fileurl}), "*");
   1.117 +  var res = (yield);
   1.118 +  is(res.type, "loadeddata", "loaded successfully");
   1.119 +  
   1.120 +  // Attempt to load an audio in a different cross-origin document
   1.121 +  inner.src = innerCrossSiteURI;
   1.122 +  yield undefined;
   1.123 +  inner.contentWindow.postMessage(JSON.stringify({audio:fileurl}), "*");
   1.124 +  var res = (yield);
   1.125 +  is(res.type, "error", "failed successfully");
   1.126 +
   1.127 +  // Attempt to load a HTML document in an iframe in this document
   1.128 +  iframe.onload = function() { gen.next(); };
   1.129 +  iframe.src = "file_mozfiledataurl_doc.html";
   1.130 +  yield undefined;
   1.131 +  is(iframe.contentDocument.getElementsByTagName("p")[0].textContent,
   1.132 +     "This here is a document!",
   1.133 +     "iframe loaded successfully");
   1.134 +  is(iframe.contentDocument.getElementById("img").width, 120,
   1.135 +     "image in iframe width");
   1.136 +  is(iframe.contentDocument.getElementById("img").height, 90,
   1.137 +     "image in iframe height");
   1.138 +
   1.139 +  // Attempt to load a HTML document in an iframe in this document, using file url
   1.140 +  file = getFile("file_mozfiledataurl_doc.html");
   1.141 +  fileurl = URL.createObjectURL(file);
   1.142 +  iframe.src = fileurl;
   1.143 +  yield undefined;
   1.144 +  is(iframe.contentDocument.getElementsByTagName("p")[0].textContent,
   1.145 +     "This here is a document!",
   1.146 +     "iframe loaded successfully");
   1.147 +  isnot(iframe.contentDocument.getElementById("img").width, 120,
   1.148 +        "failed image in iframe width");
   1.149 +  isnot(iframe.contentDocument.getElementById("img").height, 90,
   1.150 +        "failed image in iframe height");
   1.151 +
   1.152 +  // Attempt to load a HTML document in an iframe in inner document
   1.153 +  inner.src = innerSameSiteURI;
   1.154 +  is((yield), "inner loaded", "correct gen.next()");
   1.155 +  inner.contentWindow.postMessage(JSON.stringify({iframe:"file_mozfiledataurl_doc.html"}), "*");
   1.156 +  var res = (yield);
   1.157 +  is(res.type, "load", "loaded successfully");
   1.158 +  is(res.text, "This here is a document!", "loaded successfully");
   1.159 +  is(res.imgWidth, 120, "correct width");
   1.160 +
   1.161 +  // Attempt to load a HTML document in an iframe in inner document, using file url
   1.162 +  inner.contentWindow.postMessage(JSON.stringify({iframe:fileurl}), "*");
   1.163 +  var res = (yield);
   1.164 +  is(res.type, "load", "loaded successfully");
   1.165 +  is(res.text, "This here is a document!", "loaded successfully");
   1.166 +  isnot(res.imgWidth, 120, "correct width");
   1.167 +
   1.168 +  // Attempt to load a HTML document in an iframe in inner cross-site document, using file url
   1.169 +  inner.src = innerCrossSiteURI;
   1.170 +  is((yield), "inner loaded", "correct gen.next()");
   1.171 +  inner.contentWindow.postMessage(JSON.stringify({iframe:fileurl}), "*");
   1.172 +  var res = (yield);
   1.173 +  is(res.type, "error", "load failed successfully");
   1.174 +
   1.175 +  // Attempt to load file url using XHR
   1.176 +  file = getFile("file_mozfiledataurl_text.txt");
   1.177 +  fileurl = URL.createObjectURL(file);
   1.178 +  xhr = new XMLHttpRequest;
   1.179 +  xhr.onload = function() { gen.send("XHR finished"); };
   1.180 +  xhr.open("GET", fileurl);
   1.181 +  xhr.send();
   1.182 +  is((yield), "XHR finished", "correct gen.next()");
   1.183 +  xhr.responseText == "Yarr, here be plaintext file, ya landlubber\n";
   1.184 +
   1.185 +  // Attempt to load file url using XHR in inner document
   1.186 +  inner.src = innerSameSiteURI;
   1.187 +  is((yield), "inner loaded", "correct gen.next()");
   1.188 +  inner.contentWindow.postMessage(JSON.stringify({xhr:fileurl}), "*");
   1.189 +  var res = (yield);
   1.190 +  is(res.didThrow, undefined, "load successful");
   1.191 +  is(res.text, "Yarr, here be plaintext file, ya landlubber\n", "load successful");
   1.192 +
   1.193 +  // Attempt to load file url using XHR
   1.194 +  inner.src = innerCrossSiteURI;
   1.195 +  is((yield), "inner loaded", "correct gen.next()");
   1.196 +  inner.contentWindow.postMessage(JSON.stringify({xhr:fileurl}), "*");
   1.197 +  var res = (yield);
   1.198 +  is(res.didThrow, true, "load failed successfully");
   1.199 +
   1.200 +  SimpleTest.finish();
   1.201 +
   1.202 +  yield undefined;
   1.203 +}
   1.204 +
   1.205 +
   1.206 +var basePath = "";
   1.207 +function getFile(name) {
   1.208 +  if (!basePath) {
   1.209 +    let xhr = new XMLHttpRequest;
   1.210 +    xhr.open("GET", "/dynamic/getMyDirectory.sjs", false);
   1.211 +    xhr.send();
   1.212 +    basePath = xhr.responseText;
   1.213 +  }
   1.214 +
   1.215 +  var fileList = document.getElementById('fileList');
   1.216 +  SpecialPowers.wrap(fileList).value = basePath + name;
   1.217 +
   1.218 +  return fileList.files[0];
   1.219 +}
   1.220 +
   1.221 +</script>
   1.222 +</pre>
   1.223 +</body>
   1.224 +</html>

mercurial