var TESTING_CHOCOLATE_FILE_NAME = 'chocolate.txt';
/**
+ * List of callbacks to be called when a file write is requested.
+ * @type {Array.<function(string)>}
+ */
+var writeFileRequestedCallbacks = [];
+
+/**
* Requests writing contents to a file, previously opened with <code>
* openRequestId</code>.
*
* @param {ReadFileRequestedOptions} options Options.
- * @param {function(} onSuccess Success callback.
+ * @param {function()} onSuccess Success callback.
* @param {function(string)} onError Error callback.
*/
function onWriteFileRequested(options, onSuccess, onError) {
var filePath = test_util.openedFiles[options.openRequestId];
+ writeFileRequestedCallbacks.forEach(function(callback) {
+ callback(filePath);
+ });
+
if (options.fileSystemId != test_util.FILE_SYSTEM_ID || !filePath) {
onError('SECURITY'); // enum ProviderError.
return;
chrome.test.runTests([
// Write contents to a non-existing file. It should succeed.
function writeNewFileSuccess() {
- var onTestSuccess = chrome.test.callbackPass();
test_util.fileSystem.root.getFile(
TESTING_NEW_FILE_NAME,
{create: true, exclusive: true},
- function(fileEntry) {
- fileEntry.createWriter(function(fileWriter) {
- fileWriter.onwriteend = function(e) {
- // Note that onwriteend() is called even if an error happened.
- if (fileWriter.error)
- return;
- chrome.test.assertEq(
- TESTING_TEXT_TO_WRITE,
- fileContents['/' + TESTING_NEW_FILE_NAME]);
- onTestSuccess();
- };
- fileWriter.onerror = function(e) {
- chrome.test.fail(fileWriter.error.name);
- };
- var blob = new Blob(
- [TESTING_TEXT_TO_WRITE], {type: 'text/plain'});
- fileWriter.write(blob);
- },
- function(error) {
- chrome.test.fail(error.name);
- });
- },
+ chrome.test.callbackPass(function(fileEntry) {
+ fileEntry.createWriter(
+ chrome.test.callbackPass(function(fileWriter) {
+ fileWriter.onwriteend = chrome.test.callbackPass(function(e) {
+ // Note that onwriteend() is called even if an error
+ // happened.
+ if (fileWriter.error)
+ return;
+ chrome.test.assertEq(
+ TESTING_TEXT_TO_WRITE,
+ fileContents['/' + TESTING_NEW_FILE_NAME]);
+ });
+ fileWriter.onerror = function(e) {
+ chrome.test.fail(fileWriter.error.name);
+ };
+ var blob = new Blob(
+ [TESTING_TEXT_TO_WRITE], {type: 'text/plain'});
+ fileWriter.write(blob);
+ }),
+ function(error) {
+ chrome.test.fail(error.name);
+ });
+ }),
function(error) {
chrome.test.fail(error.name);
});
// Overwrite contents in an existing file. It should succeed.
function overwriteFileSuccess() {
- var onTestSuccess = chrome.test.callbackPass();
test_util.fileSystem.root.getFile(
TESTING_TIRAMISU_FILE_NAME,
{create: true, exclusive: false},
- function(fileEntry) {
- fileEntry.createWriter(function(fileWriter) {
- fileWriter.onwriteend = function(e) {
- if (fileWriter.error)
- return;
- chrome.test.assertEq(
- TESTING_TEXT_TO_WRITE,
- fileContents['/' + TESTING_TIRAMISU_FILE_NAME]);
- onTestSuccess();
- };
- fileWriter.onerror = function(e) {
- chrome.test.fail(fileWriter.error.name);
- };
- var blob = new Blob(
- [TESTING_TEXT_TO_WRITE], {type: 'text/plain'});
- fileWriter.write(blob);
- },
- function(error) {
- chrome.test.fail(error.name);
- });
- },
+ chrome.test.callbackPass(function(fileEntry) {
+ fileEntry.createWriter(
+ chrome.test.callbackPass(function(fileWriter) {
+ fileWriter.onwriteend = chrome.test.callbackPass(function(e) {
+ if (fileWriter.error)
+ return;
+ chrome.test.assertEq(
+ TESTING_TEXT_TO_WRITE,
+ fileContents['/' + TESTING_TIRAMISU_FILE_NAME]);
+ });
+ fileWriter.onerror = function(e) {
+ chrome.test.fail(fileWriter.error.name);
+ };
+ var blob = new Blob(
+ [TESTING_TEXT_TO_WRITE], {type: 'text/plain'});
+ fileWriter.write(blob);
+ }),
+ function(error) {
+ chrome.test.fail(error.name);
+ });
+ }),
function(error) {
chrome.test.fail(error.name);
});
fileWriter.onabort = function(e) {
hadAbort = true;
};
+ writeFileRequestedCallbacks.push(
+ function(filePath) {
+ // Abort the operation after it's started.
+ if (filePath == '/' + TESTING_CHOCOLATE_FILE_NAME)
+ fileWriter.abort();
+ });
var blob = new Blob(['A lot of cherries.'], {type: 'text/plain'});
fileWriter.write(blob);
- setTimeout(function() {
- // Abort the operation after it's started.
- fileWriter.abort();
- }, 0);
},
function(error) {
chrome.test.fail();