summaryrefslogtreecommitdiffstats
path: root/dom/file/tests/test_mozfiledataurl.html
blob: 68c88fa28c215a56bec6d20aea5e3839c0f2e301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE HTML>
<html>
<head>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
  <title>Test for File urls</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="start()">
<p id="display">
<iframe id=inner></iframe>
<iframe id=iframe></iframe>
<img id=img onload="gen.next(event);">
<audio id=audio onloadeddata="gen.next(event);">
</p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script class="testbody" type="application/javascript">

try {
  URL.createObjectURL(undefined);
} catch(e) { }

window.addEventListener("message", function(e) {
  gen.next(JSON.parse(e.data));
});

const innerSameSiteURI = "file_mozfiledataurl_inner.html";
const innerCrossSiteURI = "http://example.com/tests/dom/file/tests/file_mozfiledataurl_inner.html"

var fileNames = ["file_mozfiledataurl_img.jpg",
                 "file_mozfiledataurl_audio.ogg",
                 "file_mozfiledataurl_doc.html",
                 "file_mozfiledataurl_text.txt"];

function start() {
  let xhr = new XMLHttpRequest;
  xhr.open("GET", "/dynamic/getMyDirectory.sjs", false);
  xhr.send();
  let basePath = xhr.responseText;

  let fullFileNames = [];
  for (let name of fileNames) {
    fullFileNames.push(basePath + name);
  }

  var script = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL("create_file_objects.js"));

  script.addMessageListener("created-file-objects", function handler(files) {
    script.removeMessageListener("created-file-objects", handler);
    gen = runTest(files);
    gen.next();
  });

  script.sendAsyncMessage("create-file-objects", {fileNames: fullFileNames});
};

SimpleTest.waitForExplicitFinish();
SimpleTest.expectAssertions(0, 3);

function* runTest([imgFile, audioFile, docFile, xhrFile]) {
  inner = document.getElementById('inner');
  img = document.getElementById('img');
  audio = document.getElementById('audio');
  iframe = document.getElementById('iframe');
  inner.onload = function() { gen.next("inner loaded"); };

  // Attempt to load a image in this document
  var fileurl = URL.createObjectURL(imgFile);
  img.src = fileurl;
  var e = (yield);
  is(e.type, "load", "loaded successfully");
  is(img.width, 120, "correct width");
  is(img.height, 90, "correct height");

  // Revoke url and attempt to load a image in this document
  img.src = "file_mozfiledataurl_img.jpg";
  is((yield).type, "load", "successfull reset image");
  URL.revokeObjectURL(fileurl);
  todo(false, "urls need to act like 404s, not fail to parse");
/*  img.src = fileurl;
  var e = (yield);
  is(e.type, "error", "failed successfully");
  isnot(img.width, 120, "correct error width");
  isnot(img.height, 90, "correct error height");
*/
  // Generate new fileurl and make sure it's different from the old
  var oldFileurl = fileurl;
  fileurl = URL.createObjectURL(imgFile);
  isnot(fileurl, oldFileurl, "URL.createObjectURL generated the same url twice");

  // Attempt to load an image in a different same-origin document
  inner.src = innerSameSiteURI;
  yield undefined;
  inner.contentWindow.postMessage(JSON.stringify({img:fileurl}), "*");
  var res = (yield);
  is(res.type, "load", "loaded successfully");
  is(res.width, 120, "correct width");
  is(res.height, 90, "correct height");

  // Attempt to load an image in a different cross-origin document
  inner.src = innerCrossSiteURI;
  yield undefined;
  inner.contentWindow.postMessage(JSON.stringify({img:fileurl}), "*");
  var res = (yield);
  is(res.type, "error", "failed successfully");
  isnot(res.width, 120, "correct error width");
  isnot(res.height, 90, "correct error height");

  // Attempt to load an audio in this document
  fileurl = URL.createObjectURL(audioFile);
  audio.src = fileurl;
  var e = (yield);
  is(e.type, "loadeddata", "loaded successfully");

  // Revoke url and attempt to load a audio in this document
  audio.src = "file_mozfiledataurl_audio.ogg";
  is((yield).type, "loadeddata", "successfully reset audio");
  URL.revokeObjectURL(fileurl);
  todo(false, "urls need to act like 404s, not fail to parse");
/*  img.src = fileurl;
  var e = (yield);
  is(e.type, "error", "failed successfully");
  isnot(img.width, 120, "correct error width");
  isnot(img.height, 90, "correct error height");
*/
  // Generate new fileurl and make sure it's different from the old
  var oldFileurl = fileurl;
  fileurl = URL.createObjectURL(audioFile);
  isnot(fileurl, oldFileurl, "URL.createObjectURL generated the same url twice");

  // Attempt to load an audio in a different same-origin document
  inner.src = innerSameSiteURI;
  yield undefined;
  inner.contentWindow.postMessage(JSON.stringify({audio:fileurl}), "*");
  var res = (yield);
  is(res.type, "loadeddata", "loaded successfully");

  // Attempt to load an audio in a different cross-origin document
  inner.src = innerCrossSiteURI;
  yield undefined;
  inner.contentWindow.postMessage(JSON.stringify({audio:fileurl}), "*");
  var res = (yield);
  is(res.type, "error", "failed successfully");

  // Attempt to load a HTML document in an iframe in this document
  iframe.onload = function() { gen.next(); };
  iframe.src = "file_mozfiledataurl_doc.html";
  yield undefined;
  is(iframe.contentDocument.getElementsByTagName("p")[0].textContent,
     "This here is a document!",
     "iframe loaded successfully");
  is(iframe.contentDocument.getElementById("img").width, 120,
     "image in iframe width");
  is(iframe.contentDocument.getElementById("img").height, 90,
     "image in iframe height");

  // Attempt to load a HTML document in an iframe in this document, using file url
  fileurl = URL.createObjectURL(docFile);
  iframe.src = fileurl;
  yield undefined;
  is(iframe.contentDocument.getElementsByTagName("p")[0].textContent,
     "This here is a document!",
     "iframe loaded successfully");
  isnot(iframe.contentDocument.getElementById("img").width, 120,
        "failed image in iframe width");
  isnot(iframe.contentDocument.getElementById("img").height, 90,
        "failed image in iframe height");

  // Attempt to load a HTML document in an iframe in inner document
  inner.src = innerSameSiteURI;
  is((yield), "inner loaded", "correct gen.next()");
  inner.contentWindow.postMessage(JSON.stringify({iframe:"file_mozfiledataurl_doc.html"}), "*");
  var res = (yield);
  is(res.type, "load", "loaded successfully");
  is(res.text, "This here is a document!", "loaded successfully");
  is(res.imgWidth, 120, "correct width");

  // Attempt to load a HTML document in an iframe in inner document, using file url
  inner.contentWindow.postMessage(JSON.stringify({iframe:fileurl}), "*");
  var res = (yield);
  is(res.type, "load", "loaded successfully");
  is(res.text, "This here is a document!", "loaded successfully");
  isnot(res.imgWidth, 120, "correct width");

  // Attempt to load a HTML document in an iframe in inner cross-site document, using file url
  inner.src = innerCrossSiteURI;
  is((yield), "inner loaded", "correct gen.next()");
  inner.contentWindow.postMessage(JSON.stringify({iframe:fileurl}), "*");
  var res = (yield);
  is(res.type, "error", "load failed successfully");

  // Attempt to load file url using XHR
  fileurl = URL.createObjectURL(xhrFile);
  xhr = new XMLHttpRequest;
  xhr.onload = function() { gen.next("XHR finished"); };
  xhr.open("GET", fileurl);
  xhr.send();
  is((yield), "XHR finished", "correct gen.next()");
  xhr.responseText == "Yarr, here be plaintext file, ya landlubber\n";

  // Attempt to load file url using XHR in inner document
  inner.src = innerSameSiteURI;
  is((yield), "inner loaded", "correct gen.next()");
  inner.contentWindow.postMessage(JSON.stringify({xhr:fileurl}), "*");
  var res = (yield);
  is(res.didThrow, undefined, "load successful");
  is(res.text, "Yarr, here be plaintext file, ya landlubber\n", "load successful");

  // Attempt to load file url using XHR
  inner.src = innerCrossSiteURI;
  is((yield), "inner loaded", "correct gen.next()");
  inner.contentWindow.postMessage(JSON.stringify({xhr:fileurl}), "*");
  var res = (yield);
  is(res.didError, true, "load failed successfully");

  SimpleTest.finish();
}
</script>
</pre>
</body>
</html>