1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.file;
16
17 import java.io.File;
18 import java.net.URL;
19 import java.nio.charset.Charset;
20 import java.nio.charset.StandardCharsets;
21
22 import org.apache.commons.io.FileUtils;
23 import org.htmlunit.WebDriverTestCase;
24 import org.htmlunit.junit.annotation.Alerts;
25 import org.junit.jupiter.api.Test;
26 import org.openqa.selenium.By;
27 import org.openqa.selenium.WebDriver;
28
29
30
31
32
33
34
35 public class FileReaderTest extends WebDriverTestCase {
36
37
38
39
40 @Test
41 @Alerts("0")
42 public void ctorReadyState() throws Exception {
43 final String html = DOCTYPE_HTML
44 + "<html>\n"
45 + "<head>\n"
46 + " <script>\n"
47 + LOG_TITLE_FUNCTION
48 + " function test() {\n"
49 + " var reader = new FileReader();\n"
50 + " log(reader.readyState);\n"
51 + " }\n"
52 + " </script>\n"
53 + "<head>\n"
54 + "<body>\n"
55 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
56 + "</body>\n"
57 + "</html>";
58
59 final WebDriver driver = loadPage2(html);
60 driver.findElement(By.id("testBtn")).click();
61 verifyTitle2(driver, getExpectedAlerts());
62 }
63
64
65
66
67 @Test
68 @Alerts("data:text/plain;base64,SHRtbFVuaXQ=")
69 public void readAsDataURL_file() throws Exception {
70 final String html = DOCTYPE_HTML
71 + "<html>\n"
72 + "<head>\n"
73 + " <script>\n"
74 + LOG_TITLE_FUNCTION
75 + " function test() {\n"
76 + " var files = document.testForm.fileupload.files;\n"
77 + " var reader = new FileReader();\n"
78 + " reader.onload = function() {\n"
79 + " var dataURL = reader.result;\n"
80 + " log(dataURL);\n"
81 + " };\n"
82 + " reader.readAsDataURL(files[0]);\n"
83 + " }\n"
84 + " </script>\n"
85 + "<head>\n"
86 + "<body>\n"
87 + " <form name='testForm'>\n"
88 + " <input type='file' id='fileupload' name='fileupload'>\n"
89 + " </form>\n"
90 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
91 + "</body>\n"
92 + "</html>";
93
94 final WebDriver driver = loadPage2(html);
95
96 final File tstFile = File.createTempFile("HtmlUnitReadAsDataURLTest", ".txt");
97 try {
98 FileUtils.write(tstFile, "HtmlUnit", StandardCharsets.UTF_8);
99
100 final String path = tstFile.getCanonicalPath();
101 driver.findElement(By.name("fileupload")).sendKeys(path);
102
103 driver.findElement(By.id("testBtn")).click();
104
105 verifyTitle2(driver, getExpectedAlerts());
106 }
107 finally {
108 FileUtils.deleteQuietly(tstFile);
109 }
110 }
111
112
113
114
115 @Test
116 @Alerts("data:application/octet-stream;base64,SHRtbFVuaXRpcyBncmVhdA==")
117 public void readAsDataURL_inMemoryFile() throws Exception {
118 final String html = DOCTYPE_HTML
119 + "<html>\n"
120 + "<head>\n"
121 + "<head>\n"
122 + "<body>\n"
123 + " <script>\n"
124 + LOG_TITLE_FUNCTION
125 + " let file = new File(['Html', 'Unit', 'is great'], 'htMluniT.txt');\n"
126 + " var reader = new FileReader();\n"
127 + " reader.onload = function() {\n"
128 + " var dataURL = reader.result;\n"
129 + " log(dataURL);\n"
130 + " };\n"
131 + " reader.readAsDataURL(file);\n"
132 + " </script>\n"
133 + "</body>\n"
134 + "</html>";
135
136 loadPageVerifyTitle2(html);
137 }
138
139
140
141
142 @Test
143 @Alerts("data:application/octet-stream;base64,SHRtbFVuaXRpcyBncmVhdA==")
144 public void readAsDataURL_blob() throws Exception {
145 final String html = DOCTYPE_HTML
146 + "<html>\n"
147 + "<head>\n"
148 + " <script>\n"
149 + LOG_TITLE_FUNCTION
150 + " function test() {\n"
151 + " var blob = new Blob(['Html', 'Unit', 'is great']);\n"
152 + " var reader = new FileReader();\n"
153 + " reader.onload = function() {\n"
154 + " var dataURL = reader.result;\n"
155 + " log(dataURL);\n"
156 + " };\n"
157 + " reader.readAsDataURL(blob);\n"
158 + " }\n"
159 + " </script>\n"
160 + "<head>\n"
161 + "<body onload='test()'>\n"
162 + "</body>\n"
163 + "</html>";
164
165 loadPageVerifyTitle2(html);
166 }
167
168
169
170
171 @Test
172 @Alerts("data:text/plain;base64,SHRtbFVuaXRpcyBncmVhdA==")
173 public void readAsDataURL_blobMimeType() throws Exception {
174 final String html = DOCTYPE_HTML
175 + "<html>\n"
176 + "<head>\n"
177 + " <script>\n"
178 + LOG_TITLE_FUNCTION
179 + " function test() {\n"
180 + " var blob = new Blob(['Html', 'Unit', 'is great'], {type : 'text/plain'});\n"
181 + " var reader = new FileReader();\n"
182 + " reader.onload = function() {\n"
183 + " var dataURL = reader.result;\n"
184 + " log(dataURL);\n"
185 + " };\n"
186 + " reader.readAsDataURL(blob);\n"
187 + " }\n"
188 + " </script>\n"
189 + "<head>\n"
190 + "<body onload='test()'>\n"
191 + "</body>\n"
192 + "</html>";
193
194 loadPageVerifyTitle2(html);
195 }
196
197
198
199
200 @Test
201 @Alerts("data:unknown;base64,SHRtbFVuaXRpcyBncmVhdA==")
202 public void readAsDataURL_blobMimeTypeUnknown() throws Exception {
203 final String html = DOCTYPE_HTML
204 + "<html>\n"
205 + "<head>\n"
206 + " <script>\n"
207 + LOG_TITLE_FUNCTION
208 + " function test() {\n"
209 + " var blob = new Blob(['Html', 'Unit', 'is great'], {type : 'unKNown'});\n"
210 + " var reader = new FileReader();\n"
211 + " reader.onload = function() {\n"
212 + " var dataURL = reader.result;\n"
213 + " log(dataURL);\n"
214 + " };\n"
215 + " reader.readAsDataURL(blob);\n"
216 + " }\n"
217 + " </script>\n"
218 + "<head>\n"
219 + "<body onload='test()'>\n"
220 + "</body>\n"
221 + "</html>";
222
223 loadPageVerifyTitle2(html);
224 }
225
226
227
228
229
230
231
232
233 @Alerts("data:application/octet-stream;base64,"
234 + "Niii65mOV9yO6adjkXdWd+zTIXFcOWwumIGlIFRqQ05seTG+J2dx0KcD")
235 private void readAsDataURLUnknown() throws Exception {
236 final String html = DOCTYPE_HTML
237 + "<html>\n"
238 + "<head>\n"
239 + "<script>\n"
240 + LOG_TITLE_FUNCTION
241 + " function previewFile() {\n"
242 + " var file = document.querySelector('input[type=file]').files[0];\n"
243 + " var reader = new FileReader();\n"
244 + " reader.addEventListener('load', function () {\n"
245 + " log(reader.result);\n"
246 + " }, false);\n"
247 + "\n"
248 + " if (file) {\n"
249 + " reader.readAsDataURL(file);\n"
250 + " }\n"
251 + " }\n"
252 + "</script>\n"
253 + "</head>\n"
254 + "<body>\n"
255 + " <input type='file' onchange='previewFile()'>\n"
256 + "</body>\n"
257 + "</html>";
258
259 final WebDriver driver = loadPage2(html);
260
261 final String filename = "testfiles/random.bytes";
262 final URL fileURL = getClass().getClassLoader().getResource(filename);
263 assertNotNull("Resource '" + filename + "' not found", fileURL);
264 final File file = new File(fileURL.toURI());
265 assertTrue("File '" + file.getAbsolutePath() + "' does not exist", file.exists());
266
267 driver.findElement(By.tagName("input")).sendKeys(file.getAbsolutePath());
268 verifyTitle2(driver, getExpectedAlerts());
269 }
270
271
272
273
274 @Test
275 @Alerts("#data:image/png;base64,")
276 public void readAsDataURLEmptyImage() throws Exception {
277 final String html = DOCTYPE_HTML
278 + "<html>\n"
279 + "<head>\n"
280 + "<script>\n"
281 + LOG_TITLE_FUNCTION
282 + " function previewFile() {\n"
283 + " var file = document.querySelector('input[type=file]').files[0];\n"
284 + " var reader = new FileReader();\n"
285 + " reader.addEventListener('load', function () {\n"
286 + " log('#' + reader.result);\n"
287 + " }, false);\n"
288
289 + " if (file) {\n"
290 + " reader.readAsDataURL(file);\n"
291 + " }\n"
292 + " }\n"
293 + "</script>\n"
294 + "</head>\n"
295 + "<body>\n"
296 + " <input type='file' onchange='previewFile()'>\n"
297 + "</body>\n"
298 + "</html>";
299
300 final WebDriver driver = loadPage2(html);
301
302 final String filename = "testfiles/empty.png";
303 final URL fileURL = getClass().getClassLoader().getResource(filename);
304 assertNotNull("Resource '" + filename + "' not found", fileURL);
305 final File file = new File(fileURL.toURI());
306 assertTrue("File '" + file.getAbsolutePath() + "' does not exist", file.exists());
307
308 driver.findElement(By.tagName("input")).sendKeys(file.getAbsolutePath());
309 verifyTitle2(driver, getExpectedAlerts());
310 }
311
312
313
314
315 @Test
316 @Alerts({"[object ArrayBuffer]", "8"})
317 public void readAsArrayBuffer() throws Exception {
318 final String html = DOCTYPE_HTML
319 + "<html>\n"
320 + "<head>\n"
321 + " <script>\n"
322 + LOG_TITLE_FUNCTION
323 + " function test() {\n"
324 + " var files = document.testForm.fileupload.files;\n"
325 + " var reader = new FileReader();\n"
326 + " reader.onload = function() {\n"
327 + " log(reader.result);\n"
328 + " log(reader.result.byteLength);\n"
329 + " };\n"
330 + " reader.readAsArrayBuffer(files[0]);\n"
331 + " }\n"
332 + " </script>\n"
333 + "<head>\n"
334 + "<body>\n"
335 + " <form name='testForm'>\n"
336 + " <input type='file' id='fileupload' name='fileupload'>\n"
337 + " </form>\n"
338 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
339 + "</body>\n"
340 + "</html>";
341
342 final WebDriver driver = loadPage2(html);
343
344 final File tstFile = File.createTempFile("HtmlUnitReadAsArrayBufferTest", ".txt");
345 try {
346 FileUtils.write(tstFile, "HtmlUnit", StandardCharsets.UTF_8);
347
348 final String path = tstFile.getCanonicalPath();
349 driver.findElement(By.name("fileupload")).sendKeys(path);
350
351 driver.findElement(By.id("testBtn")).click();
352
353 verifyTitle2(driver, getExpectedAlerts());
354 }
355 finally {
356 FileUtils.deleteQuietly(tstFile);
357 }
358 }
359
360
361
362
363 @Test
364 @Alerts({"[object ArrayBuffer]", "128"})
365 public void readAsArrayBufferUnknown() throws Exception {
366 final String html = DOCTYPE_HTML
367 + "<html>\n"
368 + "<head>\n"
369 + "<script>\n"
370 + LOG_TITLE_FUNCTION
371 + " function previewFile() {\n"
372 + " var file = document.querySelector('input[type=file]').files[0];\n"
373 + " var reader = new FileReader();\n"
374 + " reader.addEventListener('load', function () {\n"
375 + " log(reader.result);\n"
376 + " log(reader.result.byteLength);\n"
377 + " }, false);\n"
378 + "\n"
379 + " if (file) {\n"
380 + " reader.readAsArrayBuffer(file);\n"
381 + " }\n"
382 + " }\n"
383 + "</script>\n"
384 + "</head>\n"
385 + "<body>\n"
386 + " <input type='file' onchange='previewFile()'>\n"
387 + "</body>\n"
388 + "</html>";
389
390 final WebDriver driver = loadPage2(html);
391
392 final String filename = "testfiles/tiny-png.img";
393 final URL fileURL = getClass().getClassLoader().getResource(filename);
394 assertNotNull("Resource '" + filename + "' not found", fileURL);
395 final File file = new File(fileURL.toURI());
396 assertTrue("File '" + file.getAbsolutePath() + "' does not exist", file.exists());
397
398 driver.findElement(By.tagName("input")).sendKeys(file.getAbsolutePath());
399 verifyTitle2(driver, getExpectedAlerts());
400 }
401
402
403
404
405 @Test
406 @Alerts({"[object ArrayBuffer]", "0"})
407 public void readAsArrayBufferEmptyImage() throws Exception {
408 final String html = DOCTYPE_HTML
409 + "<html>\n"
410 + "<head>\n"
411 + "<script>\n"
412 + LOG_TITLE_FUNCTION
413 + " function previewFile() {\n"
414 + " var file = document.querySelector('input[type=file]').files[0];\n"
415 + " var reader = new FileReader();\n"
416 + " reader.addEventListener('load', function () {\n"
417 + " log(reader.result);\n"
418 + " log(reader.result.byteLength);\n"
419 + " }, false);\n"
420 + "\n"
421 + " if (file) {\n"
422 + " reader.readAsArrayBuffer(file);\n"
423 + " }\n"
424 + " }\n"
425 + "</script>\n"
426 + "</head>\n"
427 + "<body>\n"
428 + " <input type='file' onchange='previewFile()'>\n"
429 + "</body>\n"
430 + "</html>";
431
432 final WebDriver driver = loadPage2(html);
433
434 final String filename = "testfiles/empty.png";
435 final URL fileURL = getClass().getClassLoader().getResource(filename);
436 assertNotNull("Resource '" + filename + "' not found", fileURL);
437 final File file = new File(fileURL.toURI());
438 assertTrue("File '" + file.getAbsolutePath() + "' does not exist", file.exists());
439
440 driver.findElement(By.tagName("input")).sendKeys(file.getAbsolutePath());
441 verifyTitle2(driver, getExpectedAlerts());
442 }
443
444
445
446
447 @Test
448 @Alerts("Html \u00dcnit")
449 public void readAsText() throws Exception {
450 final String html = DOCTYPE_HTML
451 + "<html>\n"
452 + "<head>\n"
453 + " <script>\n"
454 + LOG_TITLE_FUNCTION
455 + " function test() {\n"
456 + " var files = document.testForm.fileupload.files;\n"
457 + " var reader = new FileReader();\n"
458 + " reader.onload = function() {\n"
459 + " log(reader.result);\n"
460 + " };\n"
461 + " reader.readAsText(files[0]);\n"
462 + " }\n"
463 + " </script>\n"
464 + "<head>\n"
465 + "<body>\n"
466 + " <form name='testForm'>\n"
467 + " <input type='file' id='fileupload' name='fileupload'>\n"
468 + " </form>\n"
469 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
470 + "</body>\n"
471 + "</html>";
472
473 final WebDriver driver = loadPage2(html);
474
475 final File tstFile = File.createTempFile("HtmlUnitReadAsTextTest", ".txt");
476 try {
477 FileUtils.write(tstFile, "Html \u00dcnit", StandardCharsets.UTF_8);
478
479 final String path = tstFile.getCanonicalPath();
480 driver.findElement(By.name("fileupload")).sendKeys(path);
481
482 driver.findElement(By.id("testBtn")).click();
483
484 verifyTitle2(driver, getExpectedAlerts());
485 }
486 finally {
487 FileUtils.deleteQuietly(tstFile);
488 }
489 }
490
491
492
493
494 @Test
495 @Alerts("Html \u00dcnit")
496 public void readAsTextEncodingNull() throws Exception {
497 final String html = DOCTYPE_HTML
498 + "<html>\n"
499 + "<head>\n"
500 + " <script>\n"
501 + LOG_TITLE_FUNCTION
502 + " function test() {\n"
503 + " var files = document.testForm.fileupload.files;\n"
504 + " var reader = new FileReader();\n"
505 + " reader.onload = function() {\n"
506 + " log(reader.result);\n"
507 + " };\n"
508 + " reader.readAsText(files[0], null);\n"
509 + " }\n"
510 + " </script>\n"
511 + "<head>\n"
512 + "<body>\n"
513 + " <form name='testForm'>\n"
514 + " <input type='file' id='fileupload' name='fileupload'>\n"
515 + " </form>\n"
516 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
517 + "</body>\n"
518 + "</html>";
519
520 final WebDriver driver = loadPage2(html);
521
522 final File tstFile = File.createTempFile("HtmlUnitReadAsTextTest", ".txt");
523 try {
524 FileUtils.write(tstFile, "Html \u00dcnit", StandardCharsets.UTF_8);
525
526 final String path = tstFile.getCanonicalPath();
527 driver.findElement(By.name("fileupload")).sendKeys(path);
528
529 driver.findElement(By.id("testBtn")).click();
530
531 verifyTitle2(driver, getExpectedAlerts());
532 }
533 finally {
534 FileUtils.deleteQuietly(tstFile);
535 }
536 }
537
538
539
540
541 @Test
542 @Alerts("Html \u00dcnit")
543 public void readAsTextEncodingUndefined() throws Exception {
544 final String html = DOCTYPE_HTML
545 + "<html>\n"
546 + "<head>\n"
547 + " <script>\n"
548 + LOG_TITLE_FUNCTION
549 + " function test() {\n"
550 + " var files = document.testForm.fileupload.files;\n"
551 + " var reader = new FileReader();\n"
552 + " reader.onload = function() {\n"
553 + " log(reader.result);\n"
554 + " };\n"
555 + " reader.readAsText(files[0], undefined);\n"
556 + " }\n"
557 + " </script>\n"
558 + "<head>\n"
559 + "<body>\n"
560 + " <form name='testForm'>\n"
561 + " <input type='file' id='fileupload' name='fileupload'>\n"
562 + " </form>\n"
563 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
564 + "</body>\n"
565 + "</html>";
566
567 final WebDriver driver = loadPage2(html);
568
569 final File tstFile = File.createTempFile("HtmlUnitReadAsTextTest", ".txt");
570 try {
571 FileUtils.write(tstFile, "Html \u00dcnit", StandardCharsets.UTF_8);
572
573 final String path = tstFile.getCanonicalPath();
574 driver.findElement(By.name("fileupload")).sendKeys(path);
575
576 driver.findElement(By.id("testBtn")).click();
577
578 verifyTitle2(driver, getExpectedAlerts());
579 }
580 finally {
581 FileUtils.deleteQuietly(tstFile);
582 }
583 }
584
585
586
587
588 @Test
589 @Alerts("Html \u00dcnit")
590 public void readAsTextEncodingUnknown() throws Exception {
591 final String html = DOCTYPE_HTML
592 + "<html>\n"
593 + "<head>\n"
594 + " <script>\n"
595 + LOG_TITLE_FUNCTION
596 + " function test() {\n"
597 + " var files = document.testForm.fileupload.files;\n"
598 + " var reader = new FileReader();\n"
599 + " reader.onload = function() {\n"
600 + " log(reader.result);\n"
601 + " };\n"
602 + " reader.readAsText(files[0], 'Unknown');\n"
603 + " }\n"
604 + " </script>\n"
605 + "<head>\n"
606 + "<body>\n"
607 + " <form name='testForm'>\n"
608 + " <input type='file' id='fileupload' name='fileupload'>\n"
609 + " </form>\n"
610 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
611 + "</body>\n"
612 + "</html>";
613
614 final WebDriver driver = loadPage2(html);
615
616 final File tstFile = File.createTempFile("HtmlUnitReadAsTextTest", ".txt");
617 try {
618 FileUtils.write(tstFile, "Html \u00dcnit", StandardCharsets.UTF_8);
619
620 final String path = tstFile.getCanonicalPath();
621 driver.findElement(By.name("fileupload")).sendKeys(path);
622
623 driver.findElement(By.id("testBtn")).click();
624
625 verifyTitle2(driver, getExpectedAlerts());
626 }
627 finally {
628 FileUtils.deleteQuietly(tstFile);
629 }
630 }
631
632
633
634
635 @Test
636 @Alerts("Html \u00dcnit")
637 public void readAsTextUtf8() throws Exception {
638 final String html = DOCTYPE_HTML
639 + "<html>\n"
640 + "<head>\n"
641 + " <script>\n"
642 + LOG_TITLE_FUNCTION
643 + " function test() {\n"
644 + " var files = document.testForm.fileupload.files;\n"
645 + " var reader = new FileReader();\n"
646 + " reader.onload = function() {\n"
647 + " log(reader.result);\n"
648 + " };\n"
649 + " reader.readAsText(files[0], 'utf-8');\n"
650 + " }\n"
651 + " </script>\n"
652 + "<head>\n"
653 + "<body>\n"
654 + " <form name='testForm'>\n"
655 + " <input type='file' id='fileupload' name='fileupload'>\n"
656 + " </form>\n"
657 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
658 + "</body>\n"
659 + "</html>";
660
661 final WebDriver driver = loadPage2(html);
662
663 final File tstFile = File.createTempFile("HtmlUnitReadAsTextTest", ".txt");
664 try {
665 FileUtils.write(tstFile, "Html \u00dcnit", StandardCharsets.UTF_8);
666
667 final String path = tstFile.getCanonicalPath();
668 driver.findElement(By.name("fileupload")).sendKeys(path);
669
670 driver.findElement(By.id("testBtn")).click();
671
672 verifyTitle2(driver, getExpectedAlerts());
673 }
674 finally {
675 FileUtils.deleteQuietly(tstFile);
676 }
677 }
678
679
680
681
682 @Test
683 @Alerts("Html \u00dcnit")
684 public void readAsTextIso88591() throws Exception {
685 final String html = DOCTYPE_HTML
686 + "<html>\n"
687 + "<head>\n"
688 + " <script>\n"
689 + LOG_TITLE_FUNCTION
690 + " function test() {\n"
691 + " var files = document.testForm.fileupload.files;\n"
692 + " var reader = new FileReader();\n"
693 + " reader.onload = function() {\n"
694 + " log(reader.result);\n"
695 + " };\n"
696 + " reader.readAsText(files[0], 'iso-8859-1');\n"
697 + " }\n"
698 + " </script>\n"
699 + "<head>\n"
700 + "<body>\n"
701 + " <form name='testForm'>\n"
702 + " <input type='file' id='fileupload' name='fileupload'>\n"
703 + " </form>\n"
704 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
705 + "</body>\n"
706 + "</html>";
707
708 final WebDriver driver = loadPage2(html);
709
710 final File tstFile = File.createTempFile("HtmlUnitReadAsTextTest", ".txt");
711 try {
712 FileUtils.write(tstFile, "Html \u00dcnit", Charset.forName("ISO-8859-1"));
713
714 final String path = tstFile.getCanonicalPath();
715 driver.findElement(By.name("fileupload")).sendKeys(path);
716
717 driver.findElement(By.id("testBtn")).click();
718
719 verifyTitle2(driver, getExpectedAlerts());
720 }
721 finally {
722 FileUtils.deleteQuietly(tstFile);
723 }
724 }
725
726
727
728
729 @Test
730 @Alerts({"", "0"})
731 public void readAsTextEmpty() throws Exception {
732 final String html = DOCTYPE_HTML
733 + "<html>\n"
734 + "<head>\n"
735 + " <script>\n"
736 + LOG_TITLE_FUNCTION
737 + " function test() {\n"
738 + " var files = document.testForm.fileupload.files;\n"
739 + " var reader = new FileReader();\n"
740 + " reader.onload = function() {\n"
741 + " log(reader.result);\n"
742 + " log(reader.result.length);\n"
743 + " };\n"
744 + " reader.readAsText(files[0]);\n"
745 + " }\n"
746 + " </script>\n"
747 + "<head>\n"
748 + "<body>\n"
749 + " <form name='testForm'>\n"
750 + " <input type='file' id='fileupload' name='fileupload'>\n"
751 + " </form>\n"
752 + " <button id='testBtn' onclick='test()'>Tester</button>\n"
753 + "</body>\n"
754 + "</html>";
755
756 final WebDriver driver = loadPage2(html);
757
758 final File tstFile = File.createTempFile("HtmlUnitReadAsTextTest", ".txt");
759 try {
760 FileUtils.write(tstFile, "", StandardCharsets.UTF_8);
761
762 final String path = tstFile.getCanonicalPath();
763 driver.findElement(By.name("fileupload")).sendKeys(path);
764
765 driver.findElement(By.id("testBtn")).click();
766
767 verifyTitle2(driver, getExpectedAlerts());
768 }
769 finally {
770 FileUtils.deleteQuietly(tstFile);
771 }
772 }
773
774
775
776
777 @Test
778 @Alerts("HtmlUnit")
779 public void readAsText_blob() throws Exception {
780 final String html = DOCTYPE_HTML
781 + "<html>\n"
782 + "<head>\n"
783 + " <script>\n"
784 + LOG_TITLE_FUNCTION
785 + " function test() {\n"
786 + " var blob = new Blob(['HtmlUnit'], {type : 'text/html'});\n"
787 + " var reader = new FileReader();\n"
788 + " reader.onload = function() {\n"
789 + " log(reader.result);\n"
790 + " };\n"
791 + " reader.readAsText(blob);\n"
792 + " }\n"
793 + " </script>\n"
794 + "<head>\n"
795 + "<body onload='test()'>\n"
796 + "</body>\n"
797 + "</html>";
798
799 loadPageVerifyTitle2(html);
800 }
801
802
803
804
805 @Test
806 @Alerts({"[object ArrayBuffer]", "8"})
807 public void readAsArrayBuffer_blob() throws Exception {
808 final String html = DOCTYPE_HTML
809 + "<html>\n"
810 + "<head>\n"
811 + " <script>\n"
812 + LOG_TITLE_FUNCTION
813 + " function test() {\n"
814 + " var blob = new Blob(['HtmlUnit'], {type : 'text/html'});\n"
815 + " var reader = new FileReader();\n"
816 + " reader.onload = function() {\n"
817 + " log(reader.result);\n"
818 + " log(reader.result.byteLength);\n"
819 + " };\n"
820 + " reader.readAsArrayBuffer(blob);\n"
821 + " }\n"
822 + " </script>\n"
823 + "</head>\n"
824 + "<body onload='test()'>\n"
825 + "</body>\n"
826 + "</html>";
827
828 loadPageVerifyTitle2(html);
829 }
830 }