Chromium Code Reviews| Index: chrome/test/data/extensions/api_test/file_system_provider/create_directory/test.js |
| diff --git a/chrome/test/data/extensions/api_test/file_system_provider/create_directory/test.js b/chrome/test/data/extensions/api_test/file_system_provider/create_directory/test.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d970d15099ceaf98579274c98fe00ac01895e289 |
| --- /dev/null |
| +++ b/chrome/test/data/extensions/api_test/file_system_provider/create_directory/test.js |
| @@ -0,0 +1,189 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +'use strict'; |
| + |
| +/** |
| + * @type {DOMFileSystem} |
| + */ |
| +var fileSystem = null; |
| + |
| +/** |
| + * @type {Array.<string>} |
| + */ |
| +var createdDirectories = []; |
| + |
| +/** |
| + * @type {string} |
| + * @const |
| + */ |
| +var FILE_SYSTEM_ID = 'vanilla'; |
| + |
| +/** |
| + * @type {Object} |
| + * @const |
| + */ |
| +var TESTING_ROOT = Object.freeze({ |
| + isDirectory: true, |
| + name: '', |
| + size: 0, |
| + modificationTime: new Date(2013, 3, 27, 9, 38, 14) |
| +}); |
| + |
| +/** |
| + * @type {Object} |
| + * @const |
| + */ |
| +var TESTING_DIRECTORY = Object.freeze({ |
| + isDirectory: false, |
| + name: 'kitty', |
| + size: 0, |
| + modificationTime: new Date(2014, 4, 28, 10, 39, 15) |
| +}); |
| + |
| + |
| +/** |
| + * Gets volume information for the provided file system. |
| + * |
| + * @param {string} fileSystemId Id of the provided file system. |
| + * @param {function(Object)} callback Callback to be called on result, with the |
| + * volume information object in case of success, or null if not found. |
| + */ |
| +function getVolumeInfo(fileSystemId, callback) { |
| + chrome.fileBrowserPrivate.getVolumeMetadataList(function(volumeList) { |
| + for (var i = 0; i < volumeList.length; i++) { |
| + if (volumeList[i].extensionId == chrome.runtime.id && |
| + volumeList[i].fileSystemId == fileSystemId) { |
| + callback(volumeList[i]); |
| + return; |
| + } |
| + } |
| + callback(null); |
| + }); |
| +} |
| + |
| +/** |
| + * Returns metadata for a requested entry. |
| + * |
| + * @param {GetMetadataRequestedOptions} options Options. |
| + * @param {function(Object)} onSuccess Success callback with metadata passed |
| + * an argument. |
| + * @param {function(string)} onError Error callback with an error code. |
| + */ |
| +function onGetMetadataRequested(options, onSuccess, onError) { |
| + if (options.fileSystemId != FILE_SYSTEM_ID) { |
| + onError('SECURITY'); // enum ProviderError. |
| + return; |
| + } |
| + |
| + if (options.entryPath == '/') { |
| + onSuccess(TESTING_ROOT); |
| + return; |
| + } |
| + |
| + // For every created directory, return the same metadata (for simplicity). |
| + if (options.entryPath in createdDirectories) { |
| + onSuccess(TESTING_DIRECTORY); |
| + return; |
| + } |
| + |
| + onError('NOT_FOUND'); // enum ProviderError. |
| +} |
| + |
| +/** |
| + * Creates a directory. |
| + * |
| + * @param {CreateDirectoryRequestedOptions} options Options. |
| + * @param {function(Object)} onSuccess Success callback |
| + * @param {function(string)} onError Error callback with an error code. |
| + */ |
| +function onCreateDirectoryRequested(options, onSuccess, onError) { |
| + if (options.fileSystemId != FILE_SYSTEM_ID) { |
| + onError('SECURITY'); // enum ProviderError. |
| + return; |
| + } |
| + |
| + if (options.directoryPath == '/' || options.recursive) { |
| + onError('INVALID_OPERATION'); |
| + return; |
| + } |
| + |
| + if (options.exclusive && (createdDirectories.indexOf( |
| + options.directoryPath) != -1)) { |
| + onError('EXISTS'); |
| + return; |
| + } |
| + |
| + createdDirectories.push(options.directoryPath); |
| + onSuccess(); // enum ProviderError. |
| +} |
| + |
| +/** |
| + * 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. |
| + */ |
| +function setUp(callback) { |
| + chrome.fileSystemProvider.mount( |
| + {fileSystemId: FILE_SYSTEM_ID, displayName: 'chocolate.zip'}, |
| + function() { |
| + chrome.fileSystemProvider.onGetMetadataRequested.addListener( |
| + onGetMetadataRequested); |
| + chrome.fileSystemProvider.onCreateDirectoryRequested.addListener( |
| + onCreateDirectoryRequested); |
| + |
| + getVolumeInfo(FILE_SYSTEM_ID, function(volumeInfo) { |
| + chrome.test.assertTrue(!!volumeInfo); |
| + chrome.fileBrowserPrivate.requestFileSystem( |
| + volumeInfo.volumeId, |
| + function(inFileSystem) { |
| + chrome.test.assertTrue(!!inFileSystem); |
| + |
| + fileSystem = inFileSystem; |
| + callback(); |
| + }); |
| + }); |
| + }, |
| + function() { |
| + chrome.test.fail(); |
|
hirono
2014/07/07 03:00:52
nit: Please add an error message.
mtomasz
2014/07/07 05:53:55
Done.
|
| + }); |
| +} |
| + |
| +/** |
| + * Runs all of the test cases, one by one. |
| + */ |
| +function runTests() { |
| + chrome.test.runTests([ |
| + // Create a directory (not exclusive). Should succeed. |
| + function createDirectorySuccessSimple() { |
| + var onSuccess = chrome.test.callbackPass(); |
| + fileSystem.root.getDirectory( |
| + TESTING_DIRECTORY.name, {create: true, exclusive: false}, |
| + function(entry) { |
| + chrome.test.assertEq(TESTING_DIRECTORY.name, entry.name); |
| + chrome.test.assertTrue(entry.isDirectory); |
| + onSuccess(); |
| + }, function(error) { |
| + chrome.test.fail(error.name); |
| + }); |
| + }, |
| + // Create a directory (exclusive). Should fail, since the directory already |
| + // exists. |
| + function createDirectoryErrorExists() { |
| + var onSuccess = chrome.test.callbackPass(); |
| + fileSystem.root.getDirectory( |
| + TESTING_DIRECTORY.name, {create: true, exclusive: true}, |
| + function(entry) { |
| + chrome.test.fail('Created a directory, but should fail.'); |
| + }, function(error) { |
| + chrome.test.assertEq('InvalidModificationError', error.name); |
| + onSuccess(); |
| + }); |
| + }, |
| + ]); |
| +} |
| + |
| +// Setup and run all of the test cases. |
| +setUp(runTests); |