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

Unified Diff: chrome/test/data/extensions/api_test/file_system_provider/read_directory/test.js

Issue 246913003: [fsp] Add support for reading directories. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased. Created 6 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/test/data/extensions/api_test/file_system_provider/read_directory/manifest.json ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/test/data/extensions/api_test/file_system_provider/read_directory/test.js
diff --git a/chrome/test/data/extensions/api_test/file_system_provider/read_directory/test.js b/chrome/test/data/extensions/api_test/file_system_provider/read_directory/test.js
new file mode 100644
index 0000000000000000000000000000000000000000..835f2ea35ae745ae55391e7b2ccebb7eb92c196b
--- /dev/null
+++ b/chrome/test/data/extensions/api_test/file_system_provider/read_directory/test.js
@@ -0,0 +1,201 @@
+// 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';
+
+var fileSystemId;
+var fileSystem;
+
+/**
+ * @type {Object}
+ * @const
+ */
+var TESTING_ROOT = Object.freeze({
+ isDirectory: true,
+ name: '',
+ size: 0,
+ modificationTime: new Date(2014, 4, 28, 10, 39, 15)
+});
+
+/**
+ * @type {Object}
+ * @const
+ */
+var TESTING_HELLO_DIR = Object.freeze({
+ isDirectory: true,
+ name: 'hello',
+ size: 0,
+ modificationTime: new Date(2014, 3, 27, 9, 38, 14)
+});
+
+/**
+ * @type {Object}
+ * @const
+ */
+var TESTING_CANDIES_DIR = Object.freeze({
+ isDirectory: true,
+ name: 'candies',
+ size: 0,
+ modificationTime: new Date(2014, 2, 26, 8, 37, 13)
+});
+
+/**
+ * @type {Object}
+ * @const
+ */
+var TESTING_TIRAMISU_FILE = Object.freeze({
+ isDirectory: false,
+ name: 'tiramisu.txt',
+ size: 1986,
+ modificationTime: new Date(2014, 1, 25, 7, 36, 12)
+});
+
+/**
+ * Returns entries in the requested directory.
+ *
+ * @param {number} inFileSystemId ID of the file system.
+ * @param {string} directoryPath Path of the directory.
+ * @param {function(Array.<Object>, boolean)} onSuccess Success callback with
+ * a list of entries. May be called multiple times.
+ * @param {function(string)} onError Error callback with an error code.
+ */
+function onReadDirectoryRequested(
+ inFileSystemId, directoryPath, onSuccess, onError) {
+ if (inFileSystemId != fileSystemId) {
+ onError('SECURITY_ERROR'); // enum ProviderError.
+ return;
+ }
+
+ if (directoryPath != '/' + TESTING_HELLO_DIR.name) {
+ onError('NOT_FOUND'); // enum ProviderError.
+ return;
+ }
+
+ onSuccess([TESTING_TIRAMISU_FILE], true /* has_next */);
+ onSuccess([TESTING_CANDIES_DIR], false /* has_next */);
+}
+
+/**
+ * Returns metadata for the requested entry.
+ *
+ * To successfully acquire a DirectoryEntry, or even a DOMFileSystem, this event
+ * must be implemented and return correct values.
+ *
+ * @param {number} inFileSystemId ID of the file system.
+ * @param {string} entryPath Path of the requested entry.
+ * @param {function(Object)} onSuccess Success callback with metadata passed
+ * an argument.
+ * @param {function(string)} onError Error callback with an error code.
+ */
+function onGetMetadataRequested(
+ inFileSystemId, entryPath, onSuccess, onError) {
+ if (inFileSystemId != fileSystemId) {
+ onError('SECURITY_ERROR'); // enum ProviderError.
+ return;
+ }
+
+ if (entryPath == '/') {
+ onSuccess(TESTING_ROOT);
+ return;
+ }
+
+ if (entryPath == '/' + TESTING_HELLO_DIR.name) {
+ onSuccess(TESTING_HELLO_DIR);
+ return;
+ }
+
+ onError('NOT_FOUND'); // 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('chocolate.zip', function(id) {
+ fileSystemId = id;
+ chrome.fileSystemProvider.onReadDirectoryRequested.addListener(
+ onReadDirectoryRequested);
+ chrome.fileSystemProvider.onGetMetadataRequested.addListener(
+ onGetMetadataRequested);
+ var volumeId =
+ 'provided:' + chrome.runtime.id + '-' + fileSystemId + '-user';
+
+ chrome.fileBrowserPrivate.requestFileSystem(
+ volumeId,
+ function(inFileSystem) {
+ chrome.test.assertTrue(!!inFileSystem);
+
+ fileSystem = inFileSystem;
+ callback();
+ });
+ }, function() {
+ chrome.test.fail();
+ });
+}
+
+/**
+ * Runs all of the test cases, one by one.
+ */
+function runTests() {
+ chrome.test.runTests([
+ // Read contents of the /hello directory. This directory exists, so it
+ // should succeed.
+ function readEntriesSuccess() {
+ var onTestSuccess = chrome.test.callbackPass(function() {});
+ fileSystem.root.getDirectory(
+ 'hello',
+ {create: false},
+ function(dirEntry) {
+ var dirReader = dirEntry.createReader();
+ var entries = [];
+ var readEntriesNext = function() {
+ dirReader.readEntries(function(inEntries) {
+ Array.prototype.push.apply(entries, inEntries);
+ if (!inEntries.length) {
+ // No more entries, so verify.
+ chrome.test.assertEq(2, entries.length);
+ chrome.test.assertTrue(entries[0].isFile);
+ chrome.test.assertEq('tiramisu.txt', entries[0].name);
+ chrome.test.assertEq(
+ '/hello/tiramisu.txt', entries[0].fullPath);
+ chrome.test.assertTrue(entries[1].isDirectory);
+ chrome.test.assertEq('candies', entries[1].name);
+ chrome.test.assertEq('/hello/candies', entries[1].fullPath);
+ onTestSuccess();
+ } else {
+ readEntriesNext();
+ }
+ }, function(error) {
+ chrome.test.fail();
+ });
+ };
+ readEntriesNext();
+ },
+ function(error) {
+ chrome.test.fail();
+ });
+ },
+ // Read contents of a directory which does not exist, what should return an
+ // error.
+ function readEntriesError() {
+ var onTestSuccess = chrome.test.callbackPass(function() {});
+ fileSystem.root.getDirectory(
+ 'cranberries',
+ {create: false},
+ function(dirEntry) {
+ chrome.test.fail();
+ },
+ function(error) {
+ chrome.test.assertEq('NotFoundError', error.name);
+ onTestSuccess();
+ });
+ }
+ ]);
+}
+
+// Setup and run all of the test cases.
+setUp(runTests);
« no previous file with comments | « chrome/test/data/extensions/api_test/file_system_provider/read_directory/manifest.json ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698