39e94eaf280d29f9c7518508f2a18f1107c087f7
[WebKit-https.git] / LayoutTests / http / tests / websocket / tests / hybi / send-file-blob.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../../../js-test-resources/js-test-pre.js"></script>
5 </head>
6 <body>
7 <div id="description"></div>
8 <div id="console"></div>
9 <script type="text/javascript">
10 description("WebSocket: Send a File.");
11
12 window.jsTestIsAsync = true;
13 if (window.testRunner)
14     testRunner.overridePreference("WebKitHixie76WebSocketProtocolEnabled", 0);
15
16 function startsWith(target, prefix)
17 {
18     return target.indexOf(prefix) === 0;
19 }
20
21 var fileSystemSize = 1024;
22 var fileName = "websocket-send-file-blob.txt";
23 var messageToWrite = "Hello, world!";
24
25 function runTest()
26 {
27     if (!window.webkitRequestFileSystem) {
28         testFailed("window.webkitRequestFileSystem is not available.");
29         finishJSTest();
30         return;
31     }
32
33     webkitRequestFileSystem(TEMPORARY, fileSystemSize, didGetFileSystem, didFail);
34 }
35
36 function didGetFileSystem(fileSystem)
37 {
38     debug("Got FileSystem object.");
39     fileSystem.root.getFile(fileName, {create: true}, didCreateFile, didFail);
40 }
41
42 var fileEntry;
43
44 function didCreateFile(entry)
45 {
46     debug("File created.");
47     fileEntry = entry;
48     shouldBeTrue("fileEntry.isFile");
49     fileEntry.createWriter(didGetFileWriter, didFail);
50 }
51
52 function didGetFileWriter(writer)
53 {
54     writer.truncate(0);
55     writer.onerror = function()
56     {
57         testFailed("FileWriter operation failed.");
58         endTest();
59     };
60     writer.onwrite = function()
61     {
62         writer.write(new Blob([messageToWrite]));
63         writer.onwrite = didWriteFile;
64     };
65 }
66
67 function didWriteFile()
68 {
69     debug("Wrote to file.");
70     fileEntry.file(didGetFile, didFail);
71 }
72
73 var closeEvent;
74
75 function didGetFile(file)
76 {
77     debug("Got File object.");
78     var ws = new WebSocket("ws://127.0.0.1:8880/websocket/tests/hybi/send-file-blob");
79     ws.onopen = function()
80     {
81         ws.send(file);
82     };
83     ws.onmessage = function(event)
84     {
85         var message = event.data;
86         if (startsWith(message, "PASS"))
87             testPassed(message);
88         else
89             testFailed(message);
90     };
91     ws.onclose = function(event)
92     {
93         closeEvent = event;
94         shouldBeTrue("closeEvent.wasClean");
95         endTest();
96     };
97 }
98
99 function didFail(fileError)
100 {
101     testFailed("FileSystem API operation failed: error code = " + fileError.code);
102     endTest();
103 }
104
105 function endTest()
106 {
107     if (fileEntry) {
108         debug("Deleting the file.");
109         fileEntry.remove(finishJSTest, finishJSTest);
110     } else
111         finishJSTest();
112 }
113
114 runTest();
115
116 </script>
117 <script src="../../../../js-test-resources/js-test-post.js"></script>
118 </body>
119 </html>