Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: chrome/test/data/extensions/api_test/file_system_provider/big_file/test.js

Issue 374543002: [fsp] Simplify browser tests by extracting the common code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * @type {DOMFileSystem} 8 * @type {DOMFileSystem}
9 */ 9 */
10 var fileSystem = null; 10 var fileSystem = null;
11 11
12 /** 12 /**
13 * Map of opened files, from a <code>openRequestId</code> to <code>filePath 13 * Map of opened files, from a <code>openRequestId</code> to <code>filePath
14 * </code>. 14 * </code>.
15 * @type {Object.<number, string>} 15 * @type {Object.<number, string>}
16 */ 16 */
17 var openedFiles = {}; 17 var openedFiles = {};
18 18
19 /** 19 /**
20 * @type {string} 20 * @type {string}
21 * @const 21 * @const
22 */ 22 */
23 var FILE_SYSTEM_ID = 'chocolate-id';
24
25 /**
26 * @type {string}
27 * @const
28 */
29 var TESTING_TEXT = 'We are bytes at the 5th GB of the file!'; 23 var TESTING_TEXT = 'We are bytes at the 5th GB of the file!';
30 24
31 /** 25 /**
32 * @type {number} 26 * @type {number}
33 * @const 27 * @const
34 */ 28 */
35 var TESTING_TEXT_OFFSET = 5 * 1000 * 1000 * 1000; 29 var TESTING_TEXT_OFFSET = 5 * 1000 * 1000 * 1000;
36 30
37 /** 31 /**
38 * @type {Object} 32 * @type {Object}
(...skipping 12 matching lines...) Expand all
51 * @const 45 * @const
52 */ 46 */
53 var TESTING_6GB_FILE = Object.freeze({ 47 var TESTING_6GB_FILE = Object.freeze({
54 isDirectory: false, 48 isDirectory: false,
55 name: '6gb.txt', 49 name: '6gb.txt',
56 size: 6 * 1024 * 1024 * 1024, 50 size: 6 * 1024 * 1024 * 1024,
57 modificationTime: new Date(2014, 1, 25, 7, 36, 12) 51 modificationTime: new Date(2014, 1, 25, 7, 36, 12)
58 }); 52 });
59 53
60 /** 54 /**
61 * Gets volume information for the provided file system.
62 *
63 * @param {string} fileSystemId Id of the provided file system.
64 * @param {function(Object)} callback Callback to be called on result, with the
65 * volume information object in case of success, or null if not found.
66 */
67 function getVolumeInfo(fileSystemId, callback) {
68 chrome.fileBrowserPrivate.getVolumeMetadataList(function(volumeList) {
69 for (var i = 0; i < volumeList.length; i++) {
70 if (volumeList[i].extensionId == chrome.runtime.id &&
71 volumeList[i].fileSystemId == fileSystemId) {
72 callback(volumeList[i]);
73 return;
74 }
75 }
76 callback(null);
77 });
78 }
79
80 /**
81 * Returns metadata for the requested entry. 55 * Returns metadata for the requested entry.
82 * 56 *
83 * To successfully acquire a DirectoryEntry, or even a DOMFileSystem, this event 57 * To successfully acquire a DirectoryEntry, or even a DOMFileSystem, this event
84 * must be implemented and return correct values. 58 * must be implemented and return correct values.
85 * 59 *
86 * @param {GetMetadataRequestedOptions} options Options. 60 * @param {GetMetadataRequestedOptions} options Options.
87 * @param {function(Object)} onSuccess Success callback with metadata passed 61 * @param {function(Object)} onSuccess Success callback with metadata passed
88 * an argument. 62 * an argument.
89 * @param {function(string)} onError Error callback with an error code. 63 * @param {function(string)} onError Error callback with an error code.
90 */ 64 */
91 function onGetMetadataRequested(options, onSuccess, onError) { 65 function onGetMetadataRequested(options, onSuccess, onError) {
92 if (options.fileSystemId != FILE_SYSTEM_ID) { 66 if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
93 onError('INVALID_OPERATION'); // enum ProviderError. 67 onError('INVALID_OPERATION'); // enum ProviderError.
94 return; 68 return;
95 } 69 }
96 70
97 if (options.entryPath == '/') { 71 if (options.entryPath == '/') {
98 onSuccess(TESTING_ROOT); 72 onSuccess(TESTING_ROOT);
99 return; 73 return;
100 } 74 }
101 75
102 if (options.entryPath == '/' + TESTING_6GB_FILE.name) { 76 if (options.entryPath == '/' + TESTING_6GB_FILE.name) {
103 onSuccess(TESTING_6GB_FILE); 77 onSuccess(TESTING_6GB_FILE);
104 return; 78 return;
105 } 79 }
106 80
107 onError('NOT_FOUND'); // enum ProviderError. 81 onError('NOT_FOUND'); // enum ProviderError.
108 } 82 }
109 83
110 /** 84 /**
111 * Requests opening a file at <code>filePath</code>. Further file operations 85 * Requests opening a file at <code>filePath</code>. Further file operations
112 * will be associated with the <code>requestId</code> 86 * will be associated with the <code>requestId</code>
113 * 87 *
114 * @param {OpenFileRequestedOptions} options Options. 88 * @param {OpenFileRequestedOptions} options Options.
115 * @param {function()} onSuccess Success callback. 89 * @param {function()} onSuccess Success callback.
116 * @param {function(string)} onError Error callback. 90 * @param {function(string)} onError Error callback.
117 */ 91 */
118 function onOpenFileRequested(options, onSuccess, onError) { 92 function onOpenFileRequested(options, onSuccess, onError) {
119 if (options.fileSystemId != FILE_SYSTEM_ID) { 93 if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
120 onError('INVALID_OPERATION'); // enum ProviderError. 94 onError('INVALID_OPERATION'); // enum ProviderError.
121 return; 95 return;
122 } 96 }
123 97
124 if (options.mode != 'READ' || options.create) { 98 if (options.mode != 'READ' || options.create) {
125 onError('ACCESS_DENIED'); // enum ProviderError. 99 onError('ACCESS_DENIED'); // enum ProviderError.
126 return; 100 return;
127 } 101 }
128 102
129 if (options.filePath != '/' + TESTING_6GB_FILE.name) { 103 if (options.filePath != '/' + TESTING_6GB_FILE.name) {
130 onError('NOT_FOUND'); // enum ProviderError. 104 onError('NOT_FOUND'); // enum ProviderError.
131 return; 105 return;
132 } 106 }
133 107
134 openedFiles[options.requestId] = options.filePath; 108 openedFiles[options.requestId] = options.filePath;
135 onSuccess(); 109 onSuccess();
136 } 110 }
137 111
138 /** 112 /**
139 * Requests closing a file previously opened with <code>openRequestId</code>. 113 * Requests closing a file previously opened with <code>openRequestId</code>.
140 * 114 *
141 * @param {CloseFileRequestedOptions} options Options. 115 * @param {CloseFileRequestedOptions} options Options.
142 * @param {function()} onSuccess Success callback. 116 * @param {function()} onSuccess Success callback.
143 * @param {function(string)} onError Error callback. 117 * @param {function(string)} onError Error callback.
144 */ 118 */
145 function onCloseFileRequested(options, onSuccess, onError) { 119 function onCloseFileRequested(options, onSuccess, onError) {
146 if (options.fileSystemId != FILE_SYSTEM_ID || 120 if (options.fileSystemId != test_util.FILE_SYSTEM_ID ||
147 !openedFiles[options.openRequestId]) { 121 !openedFiles[options.openRequestId]) {
148 onError('INVALID_OPERATION'); // enum ProviderError. 122 onError('INVALID_OPERATION'); // enum ProviderError.
149 return; 123 return;
150 } 124 }
151 125
152 delete openedFiles[options.openRequestId]; 126 delete openedFiles[options.openRequestId];
153 onSuccess(); 127 onSuccess();
154 } 128 }
155 129
156 /** 130 /**
157 * Requests reading contents of a file, previously opened with <code> 131 * Requests reading contents of a file, previously opened with <code>
158 * openRequestId</code>. 132 * openRequestId</code>.
159 * 133 *
160 * @param {ReadFileRequestedOptions} options Options. 134 * @param {ReadFileRequestedOptions} options Options.
161 * @param {function(ArrayBuffer, boolean)} onSuccess Success callback with a 135 * @param {function(ArrayBuffer, boolean)} onSuccess Success callback with a
162 * chunk of data, and information if more data will be provided later. 136 * chunk of data, and information if more data will be provided later.
163 * @param {function(string)} onError Error callback. 137 * @param {function(string)} onError Error callback.
164 */ 138 */
165 function onReadFileRequested(options, onSuccess, onError) { 139 function onReadFileRequested(options, onSuccess, onError) {
166 var filePath = openedFiles[options.openRequestId]; 140 var filePath = openedFiles[options.openRequestId];
167 if (options.fileSystemId != FILE_SYSTEM_ID || !filePath) { 141 if (options.fileSystemId != test_util.FILE_SYSTEM_ID || !filePath) {
168 onError('INVALID_OPERATION'); // enum ProviderError. 142 onError('INVALID_OPERATION'); // enum ProviderError.
169 return; 143 return;
170 } 144 }
171 145
172 if (filePath == '/' + TESTING_6GB_FILE.name) { 146 if (filePath == '/' + TESTING_6GB_FILE.name) {
173 if (options.offset < TESTING_TEXT_OFFSET || 147 if (options.offset < TESTING_TEXT_OFFSET ||
174 options.offset + options.length > 148 options.offset + options.length >
175 TESTING_TEXT_OFFSET + TESTING_TEXT.length) { 149 TESTING_TEXT_OFFSET + TESTING_TEXT.length) {
176 console.error('Reading from a wrong location in the file!'); 150 console.error('Reading from a wrong location in the file!');
177 onError('INVALID_FAILED'); // enum ProviderError. 151 onError('INVALID_FAILED'); // enum ProviderError.
(...skipping 13 matching lines...) Expand all
191 onError('INVALID_OPERATION'); // enum ProviderError. 165 onError('INVALID_OPERATION'); // enum ProviderError.
192 } 166 }
193 167
194 /** 168 /**
195 * Sets up the tests. Called once per all test cases. In case of a failure, 169 * Sets up the tests. Called once per all test cases. In case of a failure,
196 * the callback is not called. 170 * the callback is not called.
197 * 171 *
198 * @param {function()} callback Success callback. 172 * @param {function()} callback Success callback.
199 */ 173 */
200 function setUp(callback) { 174 function setUp(callback) {
201 chrome.fileSystemProvider.mount( 175 chrome.fileSystemProvider.onGetMetadataRequested.addListener(
202 {fileSystemId: FILE_SYSTEM_ID, displayName: 'chocolate.zip'}, 176 onGetMetadataRequested);
203 function() { 177 chrome.fileSystemProvider.onOpenFileRequested.addListener(
204 chrome.fileSystemProvider.onGetMetadataRequested.addListener( 178 onOpenFileRequested);
205 onGetMetadataRequested); 179 chrome.fileSystemProvider.onReadFileRequested.addListener(
206 chrome.fileSystemProvider.onOpenFileRequested.addListener( 180 onReadFileRequested);
207 onOpenFileRequested); 181 chrome.fileSystemProvider.onCloseFileRequested.addListener(
208 chrome.fileSystemProvider.onReadFileRequested.addListener( 182 onCloseFileRequested);
209 onReadFileRequested); 183 test_util.mountFileSystem(callback);
210 var volumeId =
211 'provided:' + chrome.runtime.id + '-' + FILE_SYSTEM_ID + '-user';
212
213 getVolumeInfo(FILE_SYSTEM_ID, function(volumeInfo) {
214 chrome.test.assertTrue(!!volumeInfo);
215 chrome.fileBrowserPrivate.requestFileSystem(
216 volumeInfo.volumeId,
217 function(inFileSystem) {
218 chrome.test.assertTrue(!!inFileSystem);
219
220 fileSystem = inFileSystem;
221 callback();
222 });
223 });
224 }, function() {
225 chrome.test.fail();
226 });
227 } 184 }
228 185
229 /** 186 /**
230 * Runs all of the test cases, one by one. 187 * Runs all of the test cases, one by one.
231 */ 188 */
232 function runTests() { 189 function runTests() {
233 chrome.test.runTests([ 190 chrome.test.runTests([
234 // Read contents of a new file, which is 6GB in size. Such size does not 191 // Read contents of a new file, which is 6GB in size. Such size does not
235 // fit in a 32bit int nor in size_t (unsigned). It should be safe to assume, 192 // fit in a 32bit int nor in size_t (unsigned). It should be safe to assume,
236 // that if 6GB are supported, then there is no 32bit bottle neck, and the 193 // that if 6GB are supported, then there is no 32bit bottle neck, and the
237 // next one would be 64bit. File System Provider API should support files 194 // next one would be 64bit. File System Provider API should support files
238 // with size greater or equal to 2^53. 195 // with size greater or equal to 2^53.
239 function readBigFileSuccess() { 196 function readBigFileSuccess() {
240 var onTestSuccess = chrome.test.callbackPass(); 197 var onTestSuccess = chrome.test.callbackPass();
241 fileSystem.root.getFile( 198 test_util.fileSystem.root.getFile(
242 TESTING_6GB_FILE.name, 199 TESTING_6GB_FILE.name,
243 {create: false}, 200 {create: false},
244 function(fileEntry) { 201 function(fileEntry) {
245 fileEntry.file(function(file) { 202 fileEntry.file(function(file) {
246 // Read 10 bytes from the 5th GB. 203 // Read 10 bytes from the 5th GB.
247 var fileSlice = 204 var fileSlice =
248 file.slice(TESTING_TEXT_OFFSET, 205 file.slice(TESTING_TEXT_OFFSET,
249 TESTING_TEXT_OFFSET + TESTING_TEXT.length); 206 TESTING_TEXT_OFFSET + TESTING_TEXT.length);
250 var fileReader = new FileReader(); 207 var fileReader = new FileReader();
251 fileReader.onload = function(e) { 208 fileReader.onload = function(e) {
(...skipping 12 matching lines...) Expand all
264 }, 221 },
265 function(error) { 222 function(error) {
266 chrome.test.fail(error.name); 223 chrome.test.fail(error.name);
267 }); 224 });
268 } 225 }
269 ]); 226 ]);
270 } 227 }
271 228
272 // Setup and run all of the test cases. 229 // Setup and run all of the test cases.
273 setUp(runTests); 230 setUp(runTests);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698