3 <script src="../../../resources/js-test.js"></script>
4 <script src="resources/file-drag-common.js"></script>
7 <form id="form" method="post" enctype="multipart/form-data">
8 <input id="text-input" type="text" name="posted-text">
9 <input id="file-input" type="file" name="posted-file">
10 <input id="multiple-file-input" type="file" multiple="multiple" name="posted-files">
12 <div id="manual-instructions">
13 To run this test manually:
15 <li>Enter something to the text field and choose files with the files choosers.
16 <li>Click <u><a onclick="navigateAway()">here</a></u> to navigate away from the page.
17 <li>Click <u><a onclick="navigateBack()">here</a></u> to navigate back to the page.
18 <li>Check that the state of the form was restored.
21 <div id="console"></div>
23 var jsTestIsAsync = true;
25 description("Test that the state of a file chooser is recovered when navigating away and navigating back.");
27 // Structure of the test:
28 // Start of the test (document.location.search == "")
30 // - Navigate to a different location.
31 // On the different page (document.location.search == "?differentpage")
33 // Back on the original page (document.location.search == "" && window.name == "wentback")
34 // - Check the form data.
37 if (!window.testRunner)
39 if (document.location.search == "" && window.name == "") {
40 // Start of the test. Set the values to the input elements.
41 document.getElementById("text-input").value = "text to be posted";
42 dragFilesOntoInput(document.getElementById("file-input"), ["../resources/test.txt"]);
43 dragFilesOntoInput(document.getElementById("multiple-file-input"),
44 ["../resources/test.txt", "../resources/test2.txt"]);
46 } else if (document.location.search == "?differentpage") {
47 // We have navigated to a different page. Now navigate back.
49 } else if (document.location.search == "" && window.name == "wentback") {
50 // We have navigated back. Now check the form values.
51 shouldBeEqualToString("document.getElementById('text-input').value", "text to be posted");
53 // The real file paths cannot be read from the file chooser element, but
54 // we can verify that the path was restored properly by reading the
56 expectFileContents(document.getElementById("file-input").files[0], "Hello");
57 expectFileContents(document.getElementById("multiple-file-input").files[0], "Hello");
58 expectFileContents(document.getElementById("multiple-file-input").files[1], "Another");
62 function navigateAway() {
63 // Navigate away; do it with a timeout so that it will create a history entry.
64 setTimeout(function() {document.location = document.location + "?differentpage";}, 0);
67 function navigateBack() {
68 window.name = "wentback";
74 function checkFileContents(expected, evt) {
75 document.readFileContents = evt.target.result;
76 shouldBeEqualToString("document.readFileContents", expected);
83 function expectFileContents(file, contents) {
84 var reader = new FileReader();
85 reader.readAsText(file, "UTF-8");
86 reader.onload = checkFileContents.bind(undefined, contents);