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.
8 * Map of opened files, from a <code>openRequestId</code> to <code>filePath
10 * @type {Object.<number, string>}
15 * Testing contents for files.
19 var TESTING_TEXT = 'I have a basket full of fruits.';
22 * Metadata of a healthy file used to read contents from.
26 var TESTING_TIRAMISU_FILE = Object.freeze({
29 size: TESTING_TEXT.length,
30 modificationTime: new Date(2014, 1, 25, 7, 36, 12)
34 * Metadata of a broken file used to read contents from.
38 var TESTING_BROKEN_TIRAMISU_FILE = Object.freeze({
40 name: 'broken-tiramisu.txt',
41 size: TESTING_TEXT.length,
42 modificationTime: new Date(2014, 1, 25, 7, 36, 12)
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.
51 var TESTING_VANILLA_FOR_ABORT_FILE = Object.freeze({
54 size: TESTING_TEXT.length,
55 modificationTime: new Date(2014, 1, 25, 7, 36, 12)
59 * Requests reading contents of a file, previously opened with <code>
60 * openRequestId</code>.
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.
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.
74 if (filePath == '/' + TESTING_TIRAMISU_FILE.name) {
75 var textToSend = TESTING_TEXT.substr(options.offset, options.length);
76 var textToSendInChunks = textToSend.split(/(?= )/);
78 textToSendInChunks.forEach(function(item, index) {
79 // Convert item (string) to an ArrayBuffer.
80 var reader = new FileReader();
82 reader.onload = function(e) {
85 index < textToSendInChunks.length - 1 /* hasMore */);
88 reader.readAsArrayBuffer(new Blob([item]));
93 if (filePath == '/' + TESTING_VANILLA_FOR_ABORT_FILE.name) {
94 // Do nothing. This simulates a very slow read.
98 if (filePath == '/' + TESTING_BROKEN_TIRAMISU_FILE.name) {
99 onError('ACCESS_DENIED'); // enum ProviderError.
103 onError('INVALID_OPERATION'); // enum ProviderError.
107 * Sets up the tests. Called once per all test cases. In case of a failure,
108 * the callback is not called.
110 * @param {function()} callback Success callback.
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);
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;
127 chrome.fileSystemProvider.onReadFileRequested.addListener(
128 onReadFileRequested);
130 test_util.mountFileSystem(callback);
134 * Runs all of the test cases, one by one.
136 function runTests() {
137 chrome.test.runTests([
138 // Read contents of the /tiramisu.txt file. This file exists, so it should
140 function readFileSuccess() {
141 var onTestSuccess = chrome.test.callbackPass();
142 test_util.fileSystem.root.getFile(
143 TESTING_TIRAMISU_FILE.name,
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);
153 fileReader.onerror = function(e) {
154 chrome.test.fail(fileReader.error.name);
156 fileReader.readAsText(file);
159 chrome.test.fail(error.name);
163 chrome.test.fail(error.name);
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,
174 function(fileEntry) {
175 fileEntry.file(function(file) {
176 var fileReader = new FileReader();
177 fileReader.onload = function(e) {
180 fileReader.onerror = function(e) {
181 chrome.test.assertEq('NotReadableError', fileReader.error.name);
184 fileReader.readAsText(file);
187 chrome.test.fail(error.name);
191 chrome.test.fail(error.name);
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();
200 var onAbortRequested = function(options, onSuccess, onError) {
201 chrome.fileSystemProvider.onAbortRequested.removeListener(
207 chrome.fileSystemProvider.onAbortRequested.addListener(
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) {
220 'Unexpectedly finished writing, despite aborting.');
225 fileReader.onerror = function(e) {
226 chrome.test.assertEq(
227 'AbortError', fileReader.error.name);
229 fileReader.readAsText(file);
230 setTimeout(function() {
231 // Abort the operation after it's started.
236 chrome.test.fail(error.name);
240 chrome.test.fail(error.name);
246 // Setup and run all of the test cases.