1<!DOCTYPE HTML>
2<html>
3<head>
4<title>File Writer test</title>
5<link rel="stylesheet" href="../js/resources/js-test-style.css">
6<script src="../js/resources/js-test-pre.js"></script>
7<script>
8 var fileEntry;
9
10 function cleanUp() {
11 var needToCallFinish = true;
12 try {
13 if (fileEntry) {
14 fileEntry.remove(finishJSTest, finishJSTest);
15 needToCallFinish = false;
16 }
17 } catch (ex) {
18 }
19 if (needToCallFinish) {
20 finishJSTest();
21 }
22 }
23
24 function stringifyObj(o) {
25 s = "";
26 if (o)
27 for (index in o) {
28 s += index + ": " + o[index] + "\n";
29 }
30 return s;
31 }
32
33 function onError(e) {
34 debug("Caught an error.");
35 if (e && e.code) { // Each FileError has a code.
36 debug("Error code: " + e.code);
37 }
38 testFailed(stringifyObj(e));
39 cleanUp();
40 }
41
42 function onSuccess() {
43 testPassed("Successfully wrote blob.");
44 cleanUp();
45 }
46
47 function tenXBlob(blob) {
48 var bb = new BlobBuilder();
49 for (var i = 0; i < 10; ++i) {
50 bb.append(blob);
51 }
52 return bb.getBlob();
53 }
54
55 function startWrite(writer) {
56 // Let's make it about a megabyte.
57 var bb = new BlobBuilder();
58 bb.append("lorem ipsum");
59 var blob = tenXBlob(bb.getBlob());
60 blob = tenXBlob(bb.getBlob());
61 blob = tenXBlob(bb.getBlob());
62 blob = tenXBlob(bb.getBlob());
63 blob = tenXBlob(bb.getBlob());
64 writer.onerror = onError;
65 writer.onwriteend = onSuccess;
66 writer.write(blob);
67 }
68
69 function useFileWriter(writer) {
70 startWrite(writer);
71 gc();
72 }
73
74 function fileCallback(f) {
75 fileEntry = f;
76 fileEntry.createWriter(useFileWriter, onError);
77 }
78
79 function runTest() {
80 debug("starting test");
81 if (requestFileSystem) {
82 requestFileSystem(0, 1024*1024,
83 function(fs) {
84 fs.root.getFile("test.txt", {create:true}, fileCallback,
85 onError);
86 },
87 onError);
88 } else {
89 debug("This test requires FileSystem API support.");
90 }
91 }
92 window.successfullyParsed = true;
93 window.jsTestIsAsync = true;
94</script>
95</head>
96<body onload="runTest()">
97 <div id="description"></div>
98 <div id="console"></div>
99 <script src="../js/resources/js-test-post.js"></script>
100</body>
101</html>
102