Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / read_file / test.js
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 'use strict';
6
7 /**
8  * Map of opened files, from a <code>openRequestId</code> to <code>filePath
9  * </code>.
10  * @type {Object.<number, string>}
11  */
12 var openedFiles = {};
13
14 /**
15  * Testing contents for files.
16  * @type {string}
17  * @const
18  */
19 var TESTING_TEXT = 'I have a basket full of fruits.';
20
21 /**
22  * Metadata of a healthy file used to read contents from.
23  * @type {Object}
24  * @const
25  */
26 var TESTING_TIRAMISU_FILE = Object.freeze({
27   isDirectory: false,
28   name: 'tiramisu.txt',
29   size: TESTING_TEXT.length,
30   modificationTime: new Date(2014, 1, 25, 7, 36, 12)
31 });
32
33 /**
34  * Metadata of a broken file used to read contents from.
35  * @type {Object}
36  * @const
37  */
38 var TESTING_BROKEN_TIRAMISU_FILE = Object.freeze({
39   isDirectory: false,
40   name: 'broken-tiramisu.txt',
41   size: TESTING_TEXT.length,
42   modificationTime: new Date(2014, 1, 25, 7, 36, 12)
43 });
44
45 /**
46  * Metadata of a broken file used to read contents from, but it simulates
47  * a very long read, in order to verify the aborting mechanism.
48  * @type {Object}
49  * @const
50  */
51 var TESTING_VANILLA_FOR_ABORT_FILE = Object.freeze({
52   isDirectory: false,
53   name: 'vanilla.txt',
54   size: TESTING_TEXT.length,
55   modificationTime: new Date(2014, 1, 25, 7, 36, 12)
56 });
57
58 /**
59  * Requests reading contents of a file, previously opened with <code>
60  * openRequestId</code>.
61  *
62  * @param {ReadFileRequestedOptions} options Options.
63  * @param {function(ArrayBuffer, boolean)} onSuccess Success callback with a
64  *     chunk of data, and information if more data will be provided later.
65  * @param {function(string)} onError Error callback.
66  */
67 function onReadFileRequested(options, onSuccess, onError) {
68   var filePath = test_util.openedFiles[options.openRequestId];
69   if (options.fileSystemId != test_util.FILE_SYSTEM_ID || !filePath) {
70     onError('SECURITY');  // enum ProviderError.
71     return;
72   }
73
74   if (filePath == '/' + TESTING_TIRAMISU_FILE.name) {
75     var textToSend = TESTING_TEXT.substr(options.offset, options.length);
76     var textToSendInChunks = textToSend.split(/(?= )/);
77
78     textToSendInChunks.forEach(function(item, index) {
79       // Convert item (string) to an ArrayBuffer.
80       var reader = new FileReader();
81
82       reader.onload = function(e) {
83         onSuccess(
84             e.target.result,
85             index < textToSendInChunks.length - 1 /* hasMore */);
86       };
87
88       reader.readAsArrayBuffer(new Blob([item]));
89     });
90     return;
91   }
92
93   if (filePath == '/' + TESTING_VANILLA_FOR_ABORT_FILE.name) {
94     // Do nothing. This simulates a very slow read.
95     return;
96   }
97
98   if (filePath == '/' + TESTING_BROKEN_TIRAMISU_FILE.name) {
99     onError('ACCESS_DENIED');  // enum ProviderError.
100     return;
101   }
102
103   onError('INVALID_OPERATION');  // enum ProviderError.
104 }
105
106 /**
107  * Sets up the tests. Called once per all test cases. In case of a failure,
108  * the callback is not called.
109  *
110  * @param {function()} callback Success callback.
111  */
112 function setUp(callback) {
113   chrome.fileSystemProvider.onGetMetadataRequested.addListener(
114       test_util.onGetMetadataRequestedDefault);
115   chrome.fileSystemProvider.onOpenFileRequested.addListener(
116       test_util.onOpenFileRequested);
117   chrome.fileSystemProvider.onCloseFileRequested.addListener(
118       test_util.onCloseFileRequested);
119
120   test_util.defaultMetadata['/' + TESTING_TIRAMISU_FILE.name] =
121       TESTING_TIRAMISU_FILE;
122   test_util.defaultMetadata['/' + TESTING_BROKEN_TIRAMISU_FILE.name] =
123       TESTING_BROKEN_TIRAMISU_FILE;
124   test_util.defaultMetadata['/' + TESTING_VANILLA_FOR_ABORT_FILE.name] =
125       TESTING_VANILLA_FOR_ABORT_FILE;
126
127   chrome.fileSystemProvider.onReadFileRequested.addListener(
128       onReadFileRequested);
129
130   test_util.mountFileSystem(callback);
131 }
132
133 /**
134  * Runs all of the test cases, one by one.
135  */
136 function runTests() {
137   chrome.test.runTests([
138     // Read contents of the /tiramisu.txt file. This file exists, so it should
139     // succeed.
140     function readFileSuccess() {
141       var onTestSuccess = chrome.test.callbackPass();
142       test_util.fileSystem.root.getFile(
143           TESTING_TIRAMISU_FILE.name,
144           {create: false},
145           function(fileEntry) {
146             fileEntry.file(function(file) {
147               var fileReader = new FileReader();
148               fileReader.onload = function(e) {
149                 var text = fileReader.result;
150                 chrome.test.assertEq(TESTING_TEXT, text);
151                 onTestSuccess();
152               };
153               fileReader.onerror = function(e) {
154                 chrome.test.fail(fileReader.error.name);
155               };
156               fileReader.readAsText(file);
157             },
158             function(error) {
159               chrome.test.fail(error.name);
160             });
161           },
162           function(error) {
163             chrome.test.fail(error.name);
164           });
165     },
166
167     // Read contents of a file,  but with an error on the way. This should
168     // result in an error.
169     function readEntriesError() {
170       var onTestSuccess = chrome.test.callbackPass();
171       test_util.fileSystem.root.getFile(
172           TESTING_BROKEN_TIRAMISU_FILE.name,
173           {create: false},
174           function(fileEntry) {
175             fileEntry.file(function(file) {
176               var fileReader = new FileReader();
177               fileReader.onload = function(e) {
178                 chrome.test.fail();
179               };
180               fileReader.onerror = function(e) {
181                 chrome.test.assertEq('NotReadableError', fileReader.error.name);
182                 onTestSuccess();
183               };
184               fileReader.readAsText(file);
185             },
186             function(error) {
187               chrome.test.fail(error.name);
188             });
189           },
190           function(error) {
191             chrome.test.fail(error.name);
192           });
193     },
194
195     // Abort reading a file with a registered abort handler. Should result in a
196     // gracefully terminated reading operation.
197     function abortReadingSuccess() {
198       var onTestSuccess = chrome.test.callbackPass();
199
200       var onAbortRequested = function(options, onSuccess, onError) {
201         chrome.fileSystemProvider.onAbortRequested.removeListener(
202             onAbortRequested);
203         onSuccess();
204         onTestSuccess();
205       };
206
207       chrome.fileSystemProvider.onAbortRequested.addListener(
208           onAbortRequested);
209
210       test_util.fileSystem.root.getFile(
211           TESTING_VANILLA_FOR_ABORT_FILE.name,
212           {create: false, exclusive: false},
213           function(fileEntry) {
214             fileEntry.file(function(file) {
215               var hadAbort = false;
216               var fileReader = new FileReader();
217               fileReader.onload = function(e) {
218                 if (!hadAbort) {
219                   chrome.test.fail(
220                       'Unexpectedly finished writing, despite aborting.');
221                   return;
222                 }
223                 chrome.test.fail();
224               };
225               fileReader.onerror = function(e) {
226                 chrome.test.assertEq(
227                     'AbortError', fileReader.error.name);
228               };
229               fileReader.readAsText(file);
230               setTimeout(function() {
231                 // Abort the operation after it's started.
232                 fileReader.abort();
233               }, 0);
234             },
235             function(error) {
236               chrome.test.fail(error.name);
237             });
238           },
239           function(error) {
240             chrome.test.fail(error.name);
241           });
242     }
243   ]);
244 }
245
246 // Setup and run all of the test cases.
247 setUp(runTests);