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