tizen beta release
[framework/web/webkit-efl.git] / LayoutTests / fast / filesystem / resources / file-writer-abort-continue.js
1 if (this.importScripts) {
2     importScripts('fs-worker-common.js');
3     importScripts('file-writer-utils.js');
4 }
5
6 description("Test that FileWriter can continue immediately after an abort.");
7
8 var sawWriteStart;
9 var sawAbort;
10 var sawWriteEnd;
11 var writer;
12 var expectedLength;
13 var truncateLength = 7;
14 var blobSize = 1100000;
15
16 var methodSet = [
17   {  // Setup method set that writes, then aborts that write before completion.
18     action : startWrite,
19     verifyLength : 0,
20     onwritestart : abortWrite,
21     onwrite : onError,
22     onabort : logAbort,
23     onwriteend : checkLengthAndCallFollowOnAction
24   },
25   {  // Method set that does a complete write.
26     action : startWrite,
27     verifyLength : blobSize,
28     onwritestart : nop,
29     onwrite : nop,
30     onabort : onError,
31     onwriteend : checkLengthAndStartNextTest
32   },
33   {  // Setup method set that writes, then aborts that write before completion.
34     action : startWrite,
35     verifyLength : blobSize,  // Left over from the previous test.
36     onwritestart : abortWrite,
37     onwrite : onError,
38     onabort : logAbort,
39     onwriteend : checkLengthAndCallFollowOnAction
40   },
41   { // Method set that does a complete truncate.
42     action : startTruncate,
43     verifyLength : truncateLength,
44     onwritestart : nop,
45     onwrite : nop,
46     onabort : onError,
47     onwriteend : checkLengthAndCompleteTest
48   }];
49
50 function nop() {
51 }
52
53 function tenXBlob(blob) {
54     var bb = new WebKitBlobBuilder();
55     for (var i = 0; i < 10; ++i) {
56         bb.append(blob);
57     }
58     return bb.getBlob();
59 }
60
61 // These methods set up a write, abort it as soon as it starts, then initiate
62 // the follow on action.
63 function abortWrite(e) {
64     testPassed("Calling abort");
65     writer.abort();
66 }
67
68 function logAbort(e) {
69     testPassed("Saw abort");
70 }
71
72 function checkLengthAndCallFollowOnAction(e) {
73     testPassed("Saw writeend 0.");
74     shouldBe("writer.length", "" + expectedLength);
75     doFollowOnAction();
76 }
77
78 // For the second method set, verify completion and move on to the next test.
79 function checkLengthAndStartNextTest(e) {
80     shouldBe("writer.length", "" + expectedLength);
81     testPassed("Saw writeend 1.");
82     runTest(2, 3);
83 }
84
85 function checkLengthAndCompleteTest(e) {
86     shouldBe("writer.length", "" + expectedLength);
87     testPassed("All tests complete.");
88     cleanUp();
89 }
90
91 function startWrite() {
92     // Let's make it about a megabyte.
93     var bb = new WebKitBlobBuilder();
94     bb.append("lorem ipsum");
95     var blob = tenXBlob(bb.getBlob());
96     blob = tenXBlob(blob);
97     blob = tenXBlob(blob);
98     blob = tenXBlob(blob);
99     blob = tenXBlob(blob);
100     var size = blob.size;
101     shouldBe("" + size, "blobSize");
102     writer.write(blob);
103 }
104
105 function startTruncate() {
106     writer.truncate(truncateLength);
107 }
108
109 function setupWriter(methodSetIndex, writer) {
110     writer.onerror = onError;
111
112     var methods = methodSet[methodSetIndex];
113     writer.onabort = methods.onabort;
114     writer.onwritestart = methods.onwritestart;
115     writer.onwrite = methods.onwrite;
116     writer.onwriteend = methods.onwriteend;
117     expectedLength = methods.verifyLength;
118     methods.action();
119 }
120
121 function runTest(initIndex, testIndex) {
122     followOnAction = testIndex;
123     setupWriter(initIndex, writer);
124 }
125
126 function doFollowOnAction() {
127     shouldBeTrue("followOnAction == 1 || followOnAction == 3");
128     setupWriter(followOnAction, writer);
129 }
130
131 var jsTestIsAsync = true;
132 setupAndRunTest(2*1024*1024, 'file-writer-abort',
133                 function (fileEntry, fileWriter) {
134                     fileEntryForCleanup = fileEntry;
135                     writer = fileWriter;
136                     runTest(0, 1);
137                 });