| OLD | NEW |
| 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} | |
| 21 * @const | |
| 22 */ | |
| 23 var FILE_SYSTEM_ID = 'chocolate-id'; | |
| 24 | |
| 25 /** | |
| 26 * @type {Object} | 20 * @type {Object} |
| 27 * @const | 21 * @const |
| 28 */ | 22 */ |
| 29 var TESTING_ROOT = Object.freeze({ | 23 var TESTING_ROOT = Object.freeze({ |
| 30 isDirectory: true, | 24 isDirectory: true, |
| 31 name: '', | 25 name: '', |
| 32 size: 0, | 26 size: 0, |
| 33 modificationTime: new Date(2014, 4, 28, 10, 39, 15) | 27 modificationTime: new Date(2014, 4, 28, 10, 39, 15) |
| 34 }); | 28 }); |
| 35 | 29 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 58 * @const | 52 * @const |
| 59 */ | 53 */ |
| 60 var TESTING_BROKEN_TIRAMISU_FILE = Object.freeze({ | 54 var TESTING_BROKEN_TIRAMISU_FILE = Object.freeze({ |
| 61 isDirectory: false, | 55 isDirectory: false, |
| 62 name: 'broken-tiramisu.txt', | 56 name: 'broken-tiramisu.txt', |
| 63 size: TESTING_TEXT.length, | 57 size: TESTING_TEXT.length, |
| 64 modificationTime: new Date(2014, 1, 25, 7, 36, 12) | 58 modificationTime: new Date(2014, 1, 25, 7, 36, 12) |
| 65 }); | 59 }); |
| 66 | 60 |
| 67 /** | 61 /** |
| 68 * Gets volume information for the provided file system. | |
| 69 * | |
| 70 * @param {string} fileSystemId Id of the provided file system. | |
| 71 * @param {function(Object)} callback Callback to be called on result, with the | |
| 72 * volume information object in case of success, or null if not found. | |
| 73 */ | |
| 74 function getVolumeInfo(fileSystemId, callback) { | |
| 75 chrome.fileBrowserPrivate.getVolumeMetadataList(function(volumeList) { | |
| 76 for (var i = 0; i < volumeList.length; i++) { | |
| 77 if (volumeList[i].extensionId == chrome.runtime.id && | |
| 78 volumeList[i].fileSystemId == fileSystemId) { | |
| 79 callback(volumeList[i]); | |
| 80 return; | |
| 81 } | |
| 82 } | |
| 83 callback(null); | |
| 84 }); | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * Returns metadata for the requested entry. | 62 * Returns metadata for the requested entry. |
| 89 * | 63 * |
| 90 * To successfully acquire a DirectoryEntry, or even a DOMFileSystem, this event | 64 * To successfully acquire a DirectoryEntry, or even a DOMFileSystem, this event |
| 91 * must be implemented and return correct values. | 65 * must be implemented and return correct values. |
| 92 * | 66 * |
| 93 * @param {GetMetadataRequestedOptions} options Options. | 67 * @param {GetMetadataRequestedOptions} options Options. |
| 94 * @param {function(Object)} onSuccess Success callback with metadata passed | 68 * @param {function(Object)} onSuccess Success callback with metadata passed |
| 95 * an argument. | 69 * an argument. |
| 96 * @param {function(string)} onError Error callback with an error code. | 70 * @param {function(string)} onError Error callback with an error code. |
| 97 */ | 71 */ |
| 98 function onGetMetadataRequested(options, onSuccess, onError) { | 72 function onGetMetadataRequested(options, onSuccess, onError) { |
| 99 if (options.fileSystemId != FILE_SYSTEM_ID) { | 73 if (options.fileSystemId != test_util.FILE_SYSTEM_ID) { |
| 100 onError('SECURITY'); // enum ProviderError. | 74 onError('SECURITY'); // enum ProviderError. |
| 101 return; | 75 return; |
| 102 } | 76 } |
| 103 | 77 |
| 104 if (options.entryPath == '/') { | 78 if (options.entryPath == '/') { |
| 105 onSuccess(TESTING_ROOT); | 79 onSuccess(TESTING_ROOT); |
| 106 return; | 80 return; |
| 107 } | 81 } |
| 108 | 82 |
| 109 if (options.entryPath == '/' + TESTING_TIRAMISU_FILE.name) { | 83 if (options.entryPath == '/' + TESTING_TIRAMISU_FILE.name) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 121 | 95 |
| 122 /** | 96 /** |
| 123 * Requests opening a file at <code>filePath</code>. Further file operations | 97 * Requests opening a file at <code>filePath</code>. Further file operations |
| 124 * will be associated with the <code>requestId</code> | 98 * will be associated with the <code>requestId</code> |
| 125 * | 99 * |
| 126 * @param {OpenFileRequestedOptions} options Options. | 100 * @param {OpenFileRequestedOptions} options Options. |
| 127 * @param {function()} onSuccess Success callback. | 101 * @param {function()} onSuccess Success callback. |
| 128 * @param {function(string)} onError Error callback. | 102 * @param {function(string)} onError Error callback. |
| 129 */ | 103 */ |
| 130 function onOpenFileRequested(options, onSuccess, onError) { | 104 function onOpenFileRequested(options, onSuccess, onError) { |
| 131 if (options.fileSystemId != FILE_SYSTEM_ID || options.mode != 'READ' || | 105 if (options.fileSystemId != test_util.FILE_SYSTEM_ID || |
| 132 options.create) { | 106 options.mode != 'READ' || options.create) { |
| 133 onError('SECURITY'); // enum ProviderError. | 107 onError('SECURITY'); // enum ProviderError. |
| 134 return; | 108 return; |
| 135 } | 109 } |
| 136 | 110 |
| 137 if (options.filePath == '/' + TESTING_TIRAMISU_FILE.name || | 111 if (options.filePath == '/' + TESTING_TIRAMISU_FILE.name || |
| 138 options.filePath == '/' + TESTING_BROKEN_TIRAMISU_FILE.name) { | 112 options.filePath == '/' + TESTING_BROKEN_TIRAMISU_FILE.name) { |
| 139 openedFiles[options.requestId] = options.filePath; | 113 openedFiles[options.requestId] = options.filePath; |
| 140 onSuccess(); | 114 onSuccess(); |
| 141 } else { | 115 } else { |
| 142 onError('NOT_FOUND'); // enum ProviderError. | 116 onError('NOT_FOUND'); // enum ProviderError. |
| 143 } | 117 } |
| 144 } | 118 } |
| 145 | 119 |
| 146 /** | 120 /** |
| 147 * Requests closing a file previously opened with <code>openRequestId</code>. | 121 * Requests closing a file previously opened with <code>openRequestId</code>. |
| 148 * | 122 * |
| 149 * @param {CloseFileRequestedOptions} options Options. | 123 * @param {CloseFileRequestedOptions} options Options. |
| 150 * @param {function()} onSuccess Success callback. | 124 * @param {function()} onSuccess Success callback. |
| 151 * @param {function(string)} onError Error callback. | 125 * @param {function(string)} onError Error callback. |
| 152 */ | 126 */ |
| 153 function onCloseFileRequested(options, onSuccess, onError) { | 127 function onCloseFileRequested(options, onSuccess, onError) { |
| 154 if (options.fileSystemId != FILE_SYSTEM_ID || | 128 if (options.fileSystemId != test_util.FILE_SYSTEM_ID || |
| 155 !openedFiles[options.openRequestId]) { | 129 !openedFiles[options.openRequestId]) { |
| 156 onError('SECURITY'); // enum ProviderError. | 130 onError('SECURITY'); // enum ProviderError. |
| 157 return; | 131 return; |
| 158 } | 132 } |
| 159 | 133 |
| 160 delete openedFiles[options.openRequestId]; | 134 delete openedFiles[options.openRequestId]; |
| 161 onSuccess(); | 135 onSuccess(); |
| 162 } | 136 } |
| 163 | 137 |
| 164 /** | 138 /** |
| 165 * Requests reading contents of a file, previously opened with <code> | 139 * Requests reading contents of a file, previously opened with <code> |
| 166 * openRequestId</code>. | 140 * openRequestId</code>. |
| 167 * | 141 * |
| 168 * @param {ReadFileRequestedOptions} options Options. | 142 * @param {ReadFileRequestedOptions} options Options. |
| 169 * @param {function(ArrayBuffer, boolean)} onSuccess Success callback with a | 143 * @param {function(ArrayBuffer, boolean)} onSuccess Success callback with a |
| 170 * chunk of data, and information if more data will be provided later. | 144 * chunk of data, and information if more data will be provided later. |
| 171 * @param {function(string)} onError Error callback. | 145 * @param {function(string)} onError Error callback. |
| 172 */ | 146 */ |
| 173 function onReadFileRequested(options, onSuccess, onError) { | 147 function onReadFileRequested(options, onSuccess, onError) { |
| 174 var filePath = openedFiles[options.openRequestId]; | 148 var filePath = openedFiles[options.openRequestId]; |
| 175 if (options.fileSystemId != FILE_SYSTEM_ID || !filePath) { | 149 if (options.fileSystemId != test_util.FILE_SYSTEM_ID || !filePath) { |
| 176 onError('SECURITY'); // enum ProviderError. | 150 onError('SECURITY'); // enum ProviderError. |
| 177 return; | 151 return; |
| 178 } | 152 } |
| 179 | 153 |
| 180 if (filePath == '/' + TESTING_TIRAMISU_FILE.name) { | 154 if (filePath == '/' + TESTING_TIRAMISU_FILE.name) { |
| 181 var textToSend = TESTING_TEXT.substr(options.offset, options.length); | 155 var textToSend = TESTING_TEXT.substr(options.offset, options.length); |
| 182 var textToSendInChunks = textToSend.split(/(?= )/); | 156 var textToSendInChunks = textToSend.split(/(?= )/); |
| 183 | 157 |
| 184 textToSendInChunks.forEach(function(item, index) { | 158 textToSendInChunks.forEach(function(item, index) { |
| 185 // Convert item (string) to an ArrayBuffer. | 159 // Convert item (string) to an ArrayBuffer. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 204 onError('INVALID_OPERATION'); // enum ProviderError. | 178 onError('INVALID_OPERATION'); // enum ProviderError. |
| 205 } | 179 } |
| 206 | 180 |
| 207 /** | 181 /** |
| 208 * Sets up the tests. Called once per all test cases. In case of a failure, | 182 * Sets up the tests. Called once per all test cases. In case of a failure, |
| 209 * the callback is not called. | 183 * the callback is not called. |
| 210 * | 184 * |
| 211 * @param {function()} callback Success callback. | 185 * @param {function()} callback Success callback. |
| 212 */ | 186 */ |
| 213 function setUp(callback) { | 187 function setUp(callback) { |
| 214 chrome.fileSystemProvider.mount( | 188 chrome.fileSystemProvider.onGetMetadataRequested.addListener( |
| 215 {fileSystemId: FILE_SYSTEM_ID, displayName: 'chocolate.zip'}, | 189 onGetMetadataRequested); |
| 216 function() { | 190 chrome.fileSystemProvider.onOpenFileRequested.addListener( |
| 217 chrome.fileSystemProvider.onGetMetadataRequested.addListener( | 191 onOpenFileRequested); |
| 218 onGetMetadataRequested); | 192 chrome.fileSystemProvider.onReadFileRequested.addListener( |
| 219 chrome.fileSystemProvider.onOpenFileRequested.addListener( | 193 onReadFileRequested); |
| 220 onOpenFileRequested); | 194 chrome.fileSystemProvider.onCloseFileRequested.addListener( |
| 221 chrome.fileSystemProvider.onReadFileRequested.addListener( | 195 onCloseFileRequested); test_util.mountFileSystem(callback); |
| 222 onReadFileRequested); | |
| 223 var volumeId = | |
| 224 'provided:' + chrome.runtime.id + '-' + FILE_SYSTEM_ID + '-user'; | |
| 225 | |
| 226 getVolumeInfo(FILE_SYSTEM_ID, function(volumeInfo) { | |
| 227 chrome.test.assertTrue(!!volumeInfo); | |
| 228 chrome.fileBrowserPrivate.requestFileSystem( | |
| 229 volumeInfo.volumeId, | |
| 230 function(inFileSystem) { | |
| 231 chrome.test.assertTrue(!!inFileSystem); | |
| 232 | |
| 233 fileSystem = inFileSystem; | |
| 234 callback(); | |
| 235 }); | |
| 236 }); | |
| 237 }, | |
| 238 function() { | |
| 239 chrome.test.fail(); | |
| 240 }); | |
| 241 } | 196 } |
| 242 | 197 |
| 243 /** | 198 /** |
| 244 * Runs all of the test cases, one by one. | 199 * Runs all of the test cases, one by one. |
| 245 */ | 200 */ |
| 246 function runTests() { | 201 function runTests() { |
| 247 chrome.test.runTests([ | 202 chrome.test.runTests([ |
| 248 // Read contents of the /tiramisu.txt file. This file exists, so it should | 203 // Read contents of the /tiramisu.txt file. This file exists, so it should |
| 249 // succeed. | 204 // succeed. |
| 250 function readFileSuccess() { | 205 function readFileSuccess() { |
| 251 var onTestSuccess = chrome.test.callbackPass(); | 206 var onTestSuccess = chrome.test.callbackPass(); |
| 252 fileSystem.root.getFile( | 207 test_util.fileSystem.root.getFile( |
| 253 TESTING_TIRAMISU_FILE.name, | 208 TESTING_TIRAMISU_FILE.name, |
| 254 {create: false}, | 209 {create: false}, |
| 255 function(fileEntry) { | 210 function(fileEntry) { |
| 256 fileEntry.file(function(file) { | 211 fileEntry.file(function(file) { |
| 257 var fileReader = new FileReader(); | 212 var fileReader = new FileReader(); |
| 258 fileReader.onload = function(e) { | 213 fileReader.onload = function(e) { |
| 259 var text = fileReader.result; | 214 var text = fileReader.result; |
| 260 chrome.test.assertEq(TESTING_TEXT, text); | 215 chrome.test.assertEq(TESTING_TEXT, text); |
| 261 onTestSuccess(); | 216 onTestSuccess(); |
| 262 }; | 217 }; |
| 263 fileReader.onerror = function(e) { | 218 fileReader.onerror = function(e) { |
| 264 chrome.test.fail(fileReader.error.name); | 219 chrome.test.fail(fileReader.error.name); |
| 265 }; | 220 }; |
| 266 fileReader.readAsText(file); | 221 fileReader.readAsText(file); |
| 267 }, | 222 }, |
| 268 function(error) { | 223 function(error) { |
| 269 chrome.test.fail(error.name); | 224 chrome.test.fail(error.name); |
| 270 }); | 225 }); |
| 271 }, | 226 }, |
| 272 function(error) { | 227 function(error) { |
| 273 chrome.test.fail(error.name); | 228 chrome.test.fail(error.name); |
| 274 }); | 229 }); |
| 275 }, | 230 }, |
| 276 // Read contents of a file file, but with an error on the way. This should | 231 // Read contents of a file file, but with an error on the way. This should |
| 277 // result in an error. | 232 // result in an error. |
| 278 function readEntriesError() { | 233 function readEntriesError() { |
| 279 var onTestSuccess = chrome.test.callbackPass(); | 234 var onTestSuccess = chrome.test.callbackPass(); |
| 280 fileSystem.root.getFile( | 235 test_util.fileSystem.root.getFile( |
| 281 TESTING_BROKEN_TIRAMISU_FILE.name, | 236 TESTING_BROKEN_TIRAMISU_FILE.name, |
| 282 {create: false}, | 237 {create: false}, |
| 283 function(fileEntry) { | 238 function(fileEntry) { |
| 284 fileEntry.file(function(file) { | 239 fileEntry.file(function(file) { |
| 285 var fileReader = new FileReader(); | 240 var fileReader = new FileReader(); |
| 286 fileReader.onload = function(e) { | 241 fileReader.onload = function(e) { |
| 287 chrome.test.fail(); | 242 chrome.test.fail(); |
| 288 }; | 243 }; |
| 289 fileReader.onerror = function(e) { | 244 fileReader.onerror = function(e) { |
| 290 chrome.test.assertEq('NotReadableError', fileReader.error.name); | 245 chrome.test.assertEq('NotReadableError', fileReader.error.name); |
| 291 onTestSuccess(); | 246 onTestSuccess(); |
| 292 }; | 247 }; |
| 293 fileReader.readAsText(file); | 248 fileReader.readAsText(file); |
| 294 }, | 249 }, |
| 295 function(error) { | 250 function(error) { |
| 296 chrome.test.fail(); | 251 chrome.test.fail(); |
| 297 }); | 252 }); |
| 298 }, | 253 }, |
| 299 function(error) { | 254 function(error) { |
| 300 chrome.test.fail(error.name); | 255 chrome.test.fail(error.name); |
| 301 }); | 256 }); |
| 302 } | 257 } |
| 303 ]); | 258 ]); |
| 304 } | 259 } |
| 305 | 260 |
| 306 // Setup and run all of the test cases. | 261 // Setup and run all of the test cases. |
| 307 setUp(runTests); | 262 setUp(runTests); |
| OLD | NEW |