<!DOCTYPE html>
<html>
<head>
<script src="/js-test-resources/js-test.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script type="text/javascript">
description("WebSocket: Send a File.");
window.jsTestIsAsync = true;
function startsWith(target, prefix)
{
return target.indexOf(prefix) === 0;
}
var fileSystemSize = 1024;
var fileName = "websocket-send-file-blob.txt";
var messageToWrite = "Hello, world!";
function runTest()
{
if (!window.webkitRequestFileSystem) {
testFailed("window.webkitRequestFileSystem is not available.");
finishJSTest();
return;
}
webkitRequestFileSystem(TEMPORARY, fileSystemSize, didGetFileSystem, didFail);
}
function didGetFileSystem(fileSystem)
{
debug("Got FileSystem object.");
fileSystem.root.getFile(fileName, {create: true}, didCreateFile, didFail);
}
var fileEntry;
function didCreateFile(entry)
{
debug("File created.");
fileEntry = entry;
shouldBeTrue("fileEntry.isFile");
fileEntry.createWriter(didGetFileWriter, didFail);
}
function didGetFileWriter(writer)
{
writer.truncate(0);
writer.onerror = function()
{
testFailed("FileWriter operation failed.");
endTest();
};
writer.onwrite = function()
{
writer.write(new Blob([messageToWrite]));
writer.onwrite = didWriteFile;
};
}
function didWriteFile()
{
debug("Wrote to file.");
fileEntry.file(didGetFile, didFail);
}
var closeEvent;
function didGetFile(file)
{
debug("Got File object.");
var ws = new WebSocket("ws://127.0.0.1:8880/send-file-blob");
ws.onopen = function()
{
ws.send(file);
};
ws.onmessage = function(event)
{
var message = event.data;
if (startsWith(message, "PASS"))
testPassed(message);
else
testFailed(message);
};
ws.onclose = function(event)
{
closeEvent = event;
shouldBeTrue("closeEvent.wasClean");
endTest();
};
}
function didFail(fileError)
{
testFailed("FileSystem API operation failed: error code = " + fileError.code);
endTest();
}
function endTest()
{
if (fileEntry) {
debug("Deleting the file.");
fileEntry.remove(finishJSTest, finishJSTest);
} else
finishJSTest();
}
runTest();
</script>
</body>
</html>