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