Index: chrome/test/data/extensions/api_test/file_system_provider/read_file/test.js |
diff --git a/chrome/test/data/extensions/api_test/file_system_provider/read_file/test.js b/chrome/test/data/extensions/api_test/file_system_provider/read_file/test.js |
index dbd1c0004ce30d726db5da938611ee9f5dadf0f7..12d5e5a000bc5b4adfa53fa7846f527a8294f55e 100644 |
--- a/chrome/test/data/extensions/api_test/file_system_provider/read_file/test.js |
+++ b/chrome/test/data/extensions/api_test/file_system_provider/read_file/test.js |
@@ -5,13 +5,6 @@ |
'use strict'; |
/** |
- * Map of opened files, from a <code>openRequestId</code> to <code>filePath |
- * </code>. |
- * @type {Object.<number, string>} |
- */ |
-var openedFiles = {}; |
- |
-/** |
* Testing contents for files. |
* @type {string} |
* @const |
@@ -56,6 +49,15 @@ var TESTING_VANILLA_FOR_ABORT_FILE = Object.freeze({ |
}); |
/** |
+ * Read breakpoint callback invoked when reading some testing files. |
+ * The first argument is a file path, and the second one is a callback to resume |
+ * reading the file. |
+ * |
+ * @type {?function(string, function()} |
+ */ |
+var readBreakpointCallback = null; |
+ |
+/** |
* Requests reading contents of a file, previously opened with <code> |
* openRequestId</code>. |
* |
@@ -72,21 +74,28 @@ function onReadFileRequested(options, onSuccess, onError) { |
} |
if (filePath == '/' + TESTING_TIRAMISU_FILE.name) { |
- var textToSend = TESTING_TEXT.substr(options.offset, options.length); |
- var textToSendInChunks = textToSend.split(/(?= )/); |
+ var continueRead = function() { |
+ var textToSend = TESTING_TEXT.substr(options.offset, options.length); |
+ var textToSendInChunks = textToSend.split(/(?= )/); |
- textToSendInChunks.forEach(function(item, index) { |
- // Convert item (string) to an ArrayBuffer. |
- var reader = new FileReader(); |
+ textToSendInChunks.forEach(function(item, index) { |
+ // Convert item (string) to an ArrayBuffer. |
+ var reader = new FileReader(); |
- reader.onload = function(e) { |
- onSuccess( |
- e.target.result, |
- index < textToSendInChunks.length - 1 /* hasMore */); |
- }; |
+ reader.onload = function(e) { |
+ onSuccess( |
+ e.target.result, |
+ index < textToSendInChunks.length - 1 /* hasMore */); |
+ }; |
- reader.readAsArrayBuffer(new Blob([item])); |
- }); |
+ reader.readAsArrayBuffer(new Blob([item])); |
+ }); |
+ }; |
+ |
+ if (readBreakpointCallback) |
+ readBreakpointCallback(filePath, continueRead); |
+ else |
+ continueRead(); |
return; |
} |
@@ -104,12 +113,11 @@ function onReadFileRequested(options, onSuccess, onError) { |
} |
/** |
- * Sets up the tests. Called once per all test cases. In case of a failure, |
- * the callback is not called. |
- * |
- * @param {function()} callback Success callback. |
+ * Sets up the tests. Called once per all test cases. For each test case, |
+ * setUpFileSystem() must to be called with additional, test-case specific |
+ * options. |
*/ |
-function setUp(callback) { |
+function setUp() { |
chrome.fileSystemProvider.onGetMetadataRequested.addListener( |
test_util.onGetMetadataRequestedDefault); |
chrome.fileSystemProvider.onOpenFileRequested.addListener( |
@@ -126,8 +134,29 @@ function setUp(callback) { |
chrome.fileSystemProvider.onReadFileRequested.addListener( |
onReadFileRequested); |
+} |
- test_util.mountFileSystem(callback); |
+/** |
+ * Sets up a testing provided file system. If it was previously mounted, then |
+ * unmounts it first. In case of an error, fails with an assert. |
+ * |
+ * @param {number} openedFilesLimit Limit of opened files at once. If 0, then |
+ * not limited. |
+ * @param {function()} callback Completion callback. |
+ */ |
+function setUpFileSystem(openedFilesLimit, callback) { |
+ var options = {}; |
+ if (openedFilesLimit) |
+ options.openedFilesLimit = openedFilesLimit; |
+ if (test_util.fileSystem) { |
+ chrome.fileSystemProvider.unmount({ |
+ fileSystemId: test_util.FILE_SYSTEM_ID |
+ }, chrome.test.callbackPass(function() { |
+ test_util.mountFileSystem(callback, options); |
+ })); |
+ } else { |
+ test_util.mountFileSystem(callback, options); |
+ } |
} |
/** |
@@ -138,104 +167,161 @@ function runTests() { |
// Read contents of the /tiramisu.txt file. This file exists, so it should |
// succeed. |
function readFileSuccess() { |
- var onTestSuccess = chrome.test.callbackPass(); |
- test_util.fileSystem.root.getFile( |
- TESTING_TIRAMISU_FILE.name, |
- {create: false}, |
- function(fileEntry) { |
- fileEntry.file(function(file) { |
- var fileReader = new FileReader(); |
- fileReader.onload = function(e) { |
- var text = fileReader.result; |
- chrome.test.assertEq(TESTING_TEXT, text); |
- onTestSuccess(); |
- }; |
- fileReader.onerror = function(e) { |
- chrome.test.fail(fileReader.error.name); |
- }; |
- fileReader.readAsText(file); |
- }, |
+ setUpFileSystem(0 /* no limit */, chrome.test.callbackPass(function() { |
+ test_util.fileSystem.root.getFile( |
+ TESTING_TIRAMISU_FILE.name, |
+ {create: false}, |
+ chrome.test.callbackPass(function(fileEntry) { |
+ fileEntry.file(chrome.test.callbackPass(function(file) { |
+ var fileReader = new FileReader(); |
+ fileReader.onload = chrome.test.callbackPass(function(e) { |
+ var text = fileReader.result; |
+ chrome.test.assertEq(TESTING_TEXT, text); |
+ }); |
+ fileReader.onerror = function(e) { |
+ chrome.test.fail(fileReader.error.name); |
+ }; |
+ fileReader.readAsText(file); |
+ }), |
+ function(error) { |
+ chrome.test.fail(error.name); |
+ }); |
+ }), |
function(error) { |
chrome.test.fail(error.name); |
}); |
- }, |
- function(error) { |
- chrome.test.fail(error.name); |
+ })); |
+ }, |
+ |
+ // Read contents of the /tiramisu.txt multiple times at once. Verify that |
+ // there is at most as many opened files at once as permitted per limit. |
+ function readFileWithOpenedFilesLimitSuccess() { |
+ setUpFileSystem(2 /* two files */, chrome.test.callbackPass(function() { |
+ var initAllReadsPromise; |
+ |
+ // Set a breakpoint on reading a file, and continue once another file |
+ // is queued. |
+ readBreakpointCallback = function(filePath, continueCallback) { |
+ chrome.test.assertEq('/' + TESTING_TIRAMISU_FILE.name, filePath); |
+ // Continue after all reads are initiated. |
+ initAllReadsPromise.then(chrome.test.callbackPass(function() { |
+ chrome.test.assertTrue( |
+ Object.keys(test_util.openedFiles).length <= 2); |
+ continueCallback(); |
+ })).catch(function(error) { |
+ chrome.test.fail(error.rname); |
}); |
+ }; |
+ |
+ // Initiate reads, but all of them will be stoped on a breakpoint on |
+ // the first read. |
+ var initReadPromises = []; |
+ for (var i = 0; i < 16; i++) { |
+ initReadPromises.push(new Promise( |
+ chrome.test.callbackPass(function(fulfill) { |
+ test_util.fileSystem.root.getFile( |
+ TESTING_TIRAMISU_FILE.name, |
+ {create: false}, |
+ chrome.test.callbackPass(function(fileEntry) { |
+ fileEntry.file(chrome.test.callbackPass(function(file) { |
+ var fileReader = new FileReader(); |
+ fileReader.onload = chrome.test.callbackPass(function(e) { |
+ var text = fileReader.result; |
+ chrome.test.assertEq(TESTING_TEXT, text); |
+ }); |
+ fileReader.onerror = function(e) { |
+ chrome.test.fail(fileReader.error.name); |
+ }; |
+ fileReader.readAsText(file); |
+ fulfill(); |
+ }), |
+ function(error) { |
+ chrome.test.fail(error.name); |
+ }); |
+ }), |
+ function(error) { |
+ chrome.test.fail(error.name); |
+ }); |
+ }))); |
+ } |
+ |
+ initAllReadsPromise = Promise.all(initReadPromises); |
+ })); |
}, |
// Read contents of a file, but with an error on the way. This should |
// result in an error. |
function readEntriesError() { |
- test_util.fileSystem.root.getFile( |
- TESTING_BROKEN_TIRAMISU_FILE.name, |
- {create: false}, |
- chrome.test.callbackPass(function(fileEntry) { |
- fileEntry.file(chrome.test.callbackPass(function(file) { |
- var fileReader = new FileReader(); |
- fileReader.onload = function(e) { |
- chrome.test.fail(); |
- }; |
- fileReader.onerror = chrome.test.callbackPass(function(e) { |
- chrome.test.assertEq('NotReadableError', fileReader.error.name); |
+ setUpFileSystem(0 /* no limit */, chrome.test.callbackPass(function() { |
+ // Reset the breakpoint from the previous test case. |
+ readBreakpointCallback = null; |
+ test_util.fileSystem.root.getFile( |
+ TESTING_BROKEN_TIRAMISU_FILE.name, |
+ {create: false}, |
+ chrome.test.callbackPass(function(fileEntry) { |
+ fileEntry.file(chrome.test.callbackPass(function(file) { |
+ var fileReader = new FileReader(); |
+ fileReader.onload = function(e) { |
+ chrome.test.fail(); |
+ }; |
+ fileReader.onerror = chrome.test.callbackPass(function(e) { |
+ chrome.test.assertEq( |
+ 'NotReadableError', fileReader.error.name); |
+ }); |
+ fileReader.readAsText(file); |
+ }), |
+ function(error) { |
+ chrome.test.fail(error.name); |
}); |
- fileReader.readAsText(file); |
}), |
function(error) { |
chrome.test.fail(error.name); |
}); |
- }), |
- function(error) { |
- chrome.test.fail(error.name); |
- }); |
+ })); |
}, |
// Abort reading a file with a registered abort handler. Should result in a |
// gracefully terminated reading operation. |
function abortReadingSuccess() { |
- var onAbortRequested = chrome.test.callbackPass( |
- function(options, onSuccess, onError) { |
- chrome.fileSystemProvider.onAbortRequested.removeListener( |
- onAbortRequested); |
- }); |
+ setUpFileSystem(0 /* no limit */, chrome.test.callbackPass(function() { |
+ var onAbortRequested = chrome.test.callbackPass( |
+ function(options, onSuccess, onError) { |
+ onSuccess(); |
+ chrome.fileSystemProvider.onAbortRequested.removeListener( |
+ onAbortRequested); |
+ }); |
+ |
+ chrome.fileSystemProvider.onAbortRequested.addListener( |
+ onAbortRequested); |
- chrome.fileSystemProvider.onAbortRequested.addListener( |
- onAbortRequested); |
- |
- test_util.fileSystem.root.getFile( |
- TESTING_VANILLA_FOR_ABORT_FILE.name, |
- {create: false, exclusive: false}, |
- chrome.test.callbackPass(function(fileEntry) { |
- fileEntry.file(chrome.test.callbackPass(function(file) { |
- var hadAbort = false; |
- var fileReader = new FileReader(); |
- fileReader.onload = function(e) { |
- if (!hadAbort) { |
- chrome.test.fail( |
- 'Unexpectedly finished writing, despite aborting.'); |
- return; |
- } |
- }; |
- fileReader.onerror = chrome.test.callbackPass(function(e) { |
- chrome.test.assertEq( |
- 'AbortError', fileReader.error.name); |
+ test_util.fileSystem.root.getFile( |
+ TESTING_VANILLA_FOR_ABORT_FILE.name, |
+ {create: false, exclusive: false}, |
+ chrome.test.callbackPass(function(fileEntry) { |
+ fileEntry.file(chrome.test.callbackPass(function(file) { |
+ var fileReader = new FileReader(); |
+ fileReader.onerror = chrome.test.callbackPass(function(e) { |
+ chrome.test.assertEq( |
+ 'AbortError', fileReader.error.name); |
+ }); |
+ fileReader.readAsText(file); |
+ setTimeout(chrome.test.callbackPass(function() { |
+ // Abort the operation after it's started. |
+ fileReader.abort(); |
+ }), 0); |
+ }), |
+ function(error) { |
+ chrome.test.fail(error.name); |
}); |
- fileReader.readAsText(file); |
- setTimeout(chrome.test.callbackPass(function() { |
- // Abort the operation after it's started. |
- fileReader.abort(); |
- }), 0); |
}), |
function(error) { |
chrome.test.fail(error.name); |
}); |
- }), |
- function(error) { |
- chrome.test.fail(error.name); |
- }); |
+ })); |
} |
]); |
} |
// Setup and run all of the test cases. |
-setUp(runTests); |
+setUp(); |
+runTests(); |