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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 'use strict';
6
7 var fileSystemId;
8 var fileSystem;
9
10 /**
11 * @type {Object}
12 * @const
13 */
14 var TESTING_ROOT = Object.freeze({
15 isDirectory: true,
16 name: '',
17 size: 0,
18 modificationTime: new Date(2014, 4, 28, 10, 39, 15)
19 });
20
21 /**
22 * @type {Object}
23 * @const
24 */
25 var TESTING_HELLO_DIR = Object.freeze({
26 isDirectory: true,
27 name: 'hello',
28 size: 0,
29 modificationTime: new Date(2014, 3, 27, 9, 38, 14)
30 });
31
32 /**
33 * @type {Object}
34 * @const
35 */
36 var TESTING_CANDIES_DIR = Object.freeze({
37 isDirectory: true,
38 name: 'candies',
39 size: 0,
40 modificationTime: new Date(2014, 2, 26, 8, 37, 13)
41 });
42
43 /**
44 * @type {Object}
45 * @const
46 */
47 var TESTING_TIRAMISU_FILE = Object.freeze({
48 isDirectory: false,
49 name: 'tiramisu.txt',
50 size: 1986,
51 modificationTime: new Date(2014, 1, 25, 7, 36, 12)
52 });
53
54 /**
55 * Returns entries in the requested directory.
56 *
57 * @param {number} inFileSystemId ID of the file system.
58 * @param {string} directoryPath Path of the directory.
59 * @param {function(Array.<Object>, boolean)} onSuccess Success callback with
60 * a list of entries. May be called multiple times.
61 * @param {function(string)} onError Error callback with an error code.
62 */
63 function onReadDirectoryRequested(
64 inFileSystemId, directoryPath, onSuccess, onError) {
65 if (inFileSystemId != fileSystemId) {
66 onError('SECURITY_ERROR'); // enum ProviderError.
67 return;
68 }
69
70 if (directoryPath != '/' + TESTING_HELLO_DIR.name) {
71 onError('NOT_FOUND'); // enum ProviderError.
72 return;
73 }
74
75 onSuccess([TESTING_TIRAMISU_FILE], true /* has_next */);
76 onSuccess([TESTING_CANDIES_DIR], false /* has_next */);
77 }
78
79 /**
80 * Returns metadata for the requested entry.
81 *
82 * To successfully acquire a DirectoryEntry, or even a DOMFileSystem, this event
83 * must be implemented and return correct values.
84 *
85 * @param {number} inFileSystemId ID of the file system.
86 * @param {string} entryPath Path of the requested entry.
87 * @param {function(Object)} onSuccess Success callback with metadata passed
88 * an argument.
89 * @param {function(string)} onError Error callback with an error code.
90 */
91 function onGetMetadataRequested(
92 inFileSystemId, entryPath, onSuccess, onError) {
93 if (inFileSystemId != fileSystemId) {
94 onError('SECURITY_ERROR'); // enum ProviderError.
95 return;
96 }
97
98 if (entryPath == '/') {
99 onSuccess(TESTING_ROOT);
100 return;
101 }
102
103 if (entryPath == '/' + TESTING_HELLO_DIR.name) {
104 onSuccess(TESTING_HELLO_DIR);
105 return;
106 }
107
108 onError('NOT_FOUND'); // enum ProviderError.
109 }
110
111 /**
112 * Sets up the tests. Called once per all test cases. In case of a failure,
113 * the callback is not called.
114 *
115 * @param {function()} callback Success callback.
116 */
117 function setUp(callback) {
118 chrome.fileSystemProvider.mount('chocolate.zip', function(id) {
119 fileSystemId = id;
120 chrome.fileSystemProvider.onReadDirectoryRequested.addListener(
121 onReadDirectoryRequested);
122 chrome.fileSystemProvider.onGetMetadataRequested.addListener(
123 onGetMetadataRequested);
124 var volumeId =
125 'provided:' + chrome.runtime.id + '-' + fileSystemId + '-user';
126
127 chrome.fileBrowserPrivate.requestFileSystem(
128 volumeId,
129 function(inFileSystem) {
130 chrome.test.assertTrue(!!inFileSystem);
131
132 fileSystem = inFileSystem;
133 callback();
134 });
135 }, function() {
136 chrome.test.fail();
137 });
138 }
139
140 /**
141 * Runs all of the test cases, one by one.
142 */
143 function runTests() {
144 chrome.test.runTests([
145 // Read contents of the /hello directory. This directory exists, so it
146 // should succeed.
147 function readEntriesSuccess() {
148 var onTestSuccess = chrome.test.callbackPass(function() {});
149 fileSystem.root.getDirectory(
150 'hello',
151 {create: false},
152 function(dirEntry) {
153 var dirReader = dirEntry.createReader();
154 var entries = [];
155 var readEntriesNext = function() {
156 dirReader.readEntries(function(inEntries) {
157 Array.prototype.push.apply(entries, inEntries);
158 if (!inEntries.length) {
159 // No more entries, so verify.
160 chrome.test.assertEq(2, entries.length);
161 chrome.test.assertTrue(entries[0].isFile);
162 chrome.test.assertEq('tiramisu.txt', entries[0].name);
163 chrome.test.assertEq(
164 '/hello/tiramisu.txt', entries[0].fullPath);
165 chrome.test.assertTrue(entries[1].isDirectory);
166 chrome.test.assertEq('candies', entries[1].name);
167 chrome.test.assertEq('/hello/candies', entries[1].fullPath);
168 onTestSuccess();
169 } else {
170 readEntriesNext();
171 }
172 }, function(error) {
173 chrome.test.fail();
174 });
175 };
176 readEntriesNext();
177 },
178 function(error) {
179 chrome.test.fail();
180 });
181 },
182 // Read contents of a directory which does not exist, what should return an
183 // error.
184 function readEntriesError() {
185 var onTestSuccess = chrome.test.callbackPass(function() {});
186 fileSystem.root.getDirectory(
187 'cranberries',
188 {create: false},
189 function(dirEntry) {
190 chrome.test.fail();
191 },
192 function(error) {
193 chrome.test.assertEq('NotFoundError', error.name);
194 onTestSuccess();
195 });
196 }
197 ]);
198 }
199
200 // Setup and run all of the test cases.
201 setUp(runTests);
OLDNEW
« 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