Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / forms / file / input-file-entries.html
blob629c9bdc0a29d61e71a97e9f9135a6573e0736c6
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <input type="file" name="files" id="files">
5 <pre id="console"></pre>
6 <script src="../../../resources/js-test.js"></script>
7 <script>
8 description('Tests the webkitEntries attribute of &lt;input type="file"&gt;');
10 var testCases = [
11 // Single file.
12 ['dir1/UTF8.txt'],
13 // Single directory.
14 ['dir1'],
15 // Multiple files.
16 ['apple.gif', 'dir1/UTF8.txt', 'dir2/green.jpg'],
17 // Multiple directories.
18 ['dir1', 'dir2'],
19 // Mixed case.
20 ['dir1', 'dir1/UTF8.txt', 'test.txt', 'dir2']
23 // The expected directories/files info in the actual resources directory.
24 var expectedFileAttributes = {
25 'dir1': { 'directory': true, 'files':[ '/dir1/UTF8.txt', '/dir1/UTF8-2.txt' ] },
26 'dir2': { 'directory': true, 'files':[ '/dir2/green.jpg' ] },
27 'apple.gif': { 'directory': false, 'size': 3340 },
28 'test.txt': { 'directory': false, 'size': 5 },
29 'dir1/UTF8.txt': { 'directory': false, 'size': 5 },
30 'dir1/UTF8-2.txt': { 'directory': false, 'size': 9 },
31 'dir2/green.jpg': { 'directory': false, 'size': 764 }
34 var droppedEntries, verifyingEntry, returnedMetadata, returnedEntries;
36 var jsTestIsAsync = true;
37 if (window.testRunner) {
38 testRunner.waitUntilDone();
39 doTest(0);
42 function doTest(index)
44 if (index >= testCases.length) {
45 finishJSTest();
46 return;
49 debug('* Testing testCases[' + index + ']');
50 var paths = testCases[index];
51 var input = document.getElementById('files');
52 input.onchange = function() { onInputFileChange(index); };
53 if (paths.length > 1)
54 input.setAttribute('multiple', 'multiple')
55 else
56 input.removeAttribute('multiple')
57 var testPaths = paths.map(function(path) { return '../resources/' + path });
58 eventSender.beginDragWithFiles(testPaths);
59 var centerX = input.offsetLeft + input.offsetWidth / 2;
60 var centerY = input.offsetTop + input.offsetHeight / 2;
61 eventSender.mouseMoveTo(centerX, centerY);
62 eventSender.mouseUp();
65 function onInputFileChange(index)
67 droppedEntries = document.getElementById('files').webkitEntries;
68 shouldEvaluateTo('droppedEntries.length', testCases[index].length);
69 verifyEntry(index, 0);
72 function verifyEntry(testIndex, entryIndex)
74 if (entryIndex == testCases[testIndex].length) {
75 doTest(testIndex + 1);
76 return;
79 var entry = verifyingEntry = droppedEntries[entryIndex];
80 debug('Verifying entry (' + entryIndex + '/' + testCases[testIndex].length + '):' + entry.fullPath);
82 var path = testCases[testIndex][entryIndex];
83 var expected = expectedFileAttributes[path];
84 var expectedPath = '/' + getBaseName(path);
86 shouldBeEqualToString('verifyingEntry.fullPath', expectedPath);
87 shouldEvaluateTo('verifyingEntry.isDirectory', expected.directory);
89 var callback = function() { verifyEntry(testIndex, entryIndex + 1); };
90 if (entry.isFile) {
91 entry.getMetadata(verifyFile.bind(this, expected, callback), onError);
92 } else {
93 var reader = entry.createReader();
94 reader.readEntries(verifyDirectory.bind(this, expected, callback), onError);
98 function verifyFile(expected, callback, metadata)
100 returnedMetadata = metadata;
101 shouldEvaluateTo('returnedMetadata.size', expected.size);
102 callback();
105 function verifyDirectory(expected, callback, entries)
107 returnedEntries = [];
108 for (var i = 0; i < entries.length; i++) {
109 // Skip the entries that start with '.' (so that we do not get '.svn' etc)
110 if (entries[i].name.indexOf('.') == 0)
111 continue;
112 returnedEntries.push(entries[i].fullPath);
114 returnedEntries.sort();
115 shouldEvaluateTo('returnedEntries.length', expected.files.length);
116 for (var i = 0; i < returnedEntries.length; i++)
117 debug(returnedEntries[i]);
118 callback();
121 function getBaseName(path)
123 var components = path.split('/');
124 return components[components.length - 1];
127 function onError(error)
129 testFailed('Test finished with unexpected error: ' + error.code);
130 finishJSTest();
133 </script>
134 </body>
135 </html>