File.lastModifiedDate must return null if the modified time info is not available
[WebKit-https.git] / LayoutTests / http / tests / local / fileapi / script-tests / send-sliced-dragged-file.js
1 description("Test for slicing a dragged file and sending it via XMLHttpRequest.");
2
3 var tempFileContent = "1234567890";
4 var tempFileName = "send-slice-dragged-file.tmp";
5 var subfile;
6
7 function uploadFile(file, filePath, start, length, expectedException)
8 {
9     var xhr = new XMLHttpRequest();
10     xhr.open("POST", "http://127.0.0.1:8000/resources/post-and-verify.cgi?path=" + filePath + "&start=" + start + "&length=" + length, false);
11
12     var passed;
13     var message;
14     try {
15         xhr.send(file);
16         if (expectedException) {
17             passed = false;
18             message = "Unexpected response data received: " + xhr.responseText + ". Expecting exception thrown";
19         } else {
20             if (xhr.responseText == "OK") {
21                 passed = true;
22                 message = "Expected response data received: " + xhr.responseText;
23             } else {
24                 passed = false;
25                 message = "Unexpected response data received: " + xhr.responseText;
26             }
27         }
28     } catch (ex) {
29         if (expectedException) {
30             passed = true;
31             message = "Expected exception thrown: " + ex;
32         } else {
33             passed = false;
34             message = "Unexpected exception thrown: " + ex;
35         }
36     }
37
38     if (passed)
39         testPassed(message);
40     else
41         testFailed(message);
42 }
43
44 function computeExpectedLength(fileLength, start, length)
45 {
46     var expectedLength = length;
47     if (start + length > fileLength) {
48         if (start >= fileLength)
49             expectedLength = 0;
50         else
51             expectedLength = fileLength - start;
52     }
53     return expectedLength;
54 }
55
56 function onStableFileDrop(file, filePath, fileLength, start, length, contentType)
57 {
58     // Slice the file.
59     subfile = file.webkitSlice(start, start + length, contentType);
60     shouldEvaluateTo("subfile.size", computeExpectedLength(fileLength, start, length));
61     shouldBe("subfile.type", (contentType != undefined && contentType != null) ? '"' + contentType + '"' : '""');
62
63     // Upload the sliced file.
64     uploadFile(subfile, filePath, start, length, false);
65 }
66
67 function dragAndSliceStableFile(filePath, fileLength, start, length, contentType)
68 {
69     setFileInputDropCallback(function(file) { onStableFileDrop(file, "../local/fileapi/" + filePath, fileLength, start, length, contentType); });
70     eventSender.beginDragWithFiles([filePath]);
71     moveMouseToCenterOfElement(fileInput);
72     eventSender.mouseUp();
73 }
74
75 function onUnstableFileDrop(file, filePath, fileLength, start, length, contentType)
76 {
77     // Slice the file.
78     subfile = file.webkitSlice(start, start + length, contentType);
79     shouldEvaluateTo("subfile.size", computeExpectedLength(fileLength, start, length));
80     shouldBe("subfile.type", (contentType != undefined && contentType != null) ? '"' + contentType + '"' : '""');
81   
82     // Upload the sliced file.
83     uploadFile(subfile, filePath, start, length, false);
84
85     // Touch the underlying temp file.
86     touchTempFile(tempFileName);
87     
88     // Upload the sliced file. We should receive an exception since the file has been changed.
89     uploadFile(subfile, filePath, start, length, true);
90
91     // Remove the temp file.
92     removeTempFile(tempFileName);
93 }
94
95 function dragAndSliceUnstableFile(start, length, contentType)
96 {
97     var tempFilePath = createTempFile(tempFileName, tempFileContent);
98     if (tempFilePath.length == 0)
99         return;
100
101     setFileInputDropCallback(function(file) { onUnstableFileDrop(file, tempFilePath, tempFileContent.length, start, length, contentType); });
102     eventSender.beginDragWithFiles([tempFilePath]);
103     moveMouseToCenterOfElement(fileInput);
104     eventSender.mouseUp();
105 }
106
107 function runTest()
108 {
109     debug("Test slicing and sending an empty file.");
110     dragAndSliceStableFile("../resources/empty.txt", 0, 0, 10);
111
112     debug("Test slicing and sending a small file.");
113     dragAndSliceStableFile("../resources/file-for-drag-to-send.txt", 10, 2, 4, null);
114     dragAndSliceStableFile("../resources/file-for-drag-to-send.txt", 10, 2, 20, "type/foo");
115     dragAndSliceStableFile("../resources/file-for-drag-to-send.txt", 10, 15, 20, "type/bar");
116
117     // This is to test a file that exceeds the read buffer limit (2K in Mac).
118     debug("Test slicing and sending a big file.");
119     dragAndSliceStableFile("../resources/abe.png", 12242, 10, 40);
120     dragAndSliceStableFile("../resources/abe.png", 12242, 10, 3000);
121     dragAndSliceStableFile("../resources/abe.png", 12242, 3000, 15000);
122
123     debug("Test slicing and sending a file that has been changed right before sending.");
124     dragAndSliceUnstableFile(3, 5);
125 }
126
127 if (window.eventSender) {
128     runTest();
129     // Clean up after ourselves
130     removeFileInputElement();
131 } else {
132     testFailed("This test is not interactive, please run using DumpRenderTree");
133 }
134
135 var successfullyParsed = true;