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

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

Issue 374543002: [fsp] Simplify browser tests by extracting the common code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed. Created 6 years, 5 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * @type {DOMFileSystem} 8 * @type {DOMFileSystem}
9 */ 9 */
10 var fileSystem = null; 10 var fileSystem = null;
11 11
12 /** 12 /**
13 * @type {string}
14 * @const
15 */
16 var FILE_SYSTEM_ID = 'vanilla';
17
18 /**
19 * @type {Object} 13 * @type {Object}
20 * @const 14 * @const
21 */ 15 */
22 var TESTING_ROOT = Object.freeze({ 16 var TESTING_ROOT = Object.freeze({
23 isDirectory: true, 17 isDirectory: true,
24 name: '', 18 name: '',
25 size: 0, 19 size: 0,
26 modificationTime: new Date(2014, 4, 28, 10, 39, 15) 20 modificationTime: new Date(2014, 4, 28, 10, 39, 15)
27 }); 21 });
28 22
(...skipping 24 matching lines...) Expand all
53 * @const 47 * @const
54 */ 48 */
55 var TESTING_TIRAMISU_FILE = Object.freeze({ 49 var TESTING_TIRAMISU_FILE = Object.freeze({
56 isDirectory: false, 50 isDirectory: false,
57 name: 'tiramisu.txt', 51 name: 'tiramisu.txt',
58 size: 1986, 52 size: 1986,
59 modificationTime: new Date(2014, 1, 25, 7, 36, 12) 53 modificationTime: new Date(2014, 1, 25, 7, 36, 12)
60 }); 54 });
61 55
62 /** 56 /**
63 * Gets volume information for the provided file system.
64 *
65 * @param {string} fileSystemId Id of the provided file system.
66 * @param {function(Object)} callback Callback to be called on result, with the
67 * volume information object in case of success, or null if not found.
68 */
69 function getVolumeInfo(fileSystemId, callback) {
70 chrome.fileBrowserPrivate.getVolumeMetadataList(function(volumeList) {
71 for (var i = 0; i < volumeList.length; i++) {
72 if (volumeList[i].extensionId == chrome.runtime.id &&
73 volumeList[i].fileSystemId == fileSystemId) {
74 callback(volumeList[i]);
75 return;
76 }
77 }
78 callback(null);
79 });
80 }
81
82 /**
83 * Returns entries in the requested directory. 57 * Returns entries in the requested directory.
84 * 58 *
85 * @param {ReadDirectoryRequestedOptions} options Options. 59 * @param {ReadDirectoryRequestedOptions} options Options.
86 * @param {function(Array.<Object>, boolean)} onSuccess Success callback with 60 * @param {function(Array.<Object>, boolean)} onSuccess Success callback with
87 * a list of entries. May be called multiple times. 61 * a list of entries. May be called multiple times.
88 * @param {function(string)} onError Error callback with an error code. 62 * @param {function(string)} onError Error callback with an error code.
89 */ 63 */
90 function onReadDirectoryRequested(options, onSuccess, onError) { 64 function onReadDirectoryRequested(options, onSuccess, onError) {
91 if (options.fileSystemId != FILE_SYSTEM_ID) { 65 if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
92 onError('SECURITY'); // enum ProviderError. 66 onError('SECURITY'); // enum ProviderError.
93 return; 67 return;
94 } 68 }
95 69
96 if (options.directoryPath != '/' + TESTING_HELLO_DIR.name) { 70 if (options.directoryPath != '/' + TESTING_HELLO_DIR.name) {
97 onError('NOT_FOUND'); // enum ProviderError. 71 onError('NOT_FOUND'); // enum ProviderError.
98 return; 72 return;
99 } 73 }
100 74
101 onSuccess([TESTING_TIRAMISU_FILE], true /* hasMore */); 75 onSuccess([TESTING_TIRAMISU_FILE], true /* hasMore */);
102 onSuccess([TESTING_CANDIES_DIR], false /* hasMore */); 76 onSuccess([TESTING_CANDIES_DIR], false /* hasMore */);
103 } 77 }
104 78
105 /** 79 /**
106 * Returns metadata for the requested entry. 80 * Returns metadata for the requested entry.
107 * 81 *
108 * To successfully acquire a DirectoryEntry, or even a DOMFileSystem, this event 82 * To successfully acquire a DirectoryEntry, or even a DOMFileSystem, this event
109 * must be implemented and return correct values. 83 * must be implemented and return correct values.
110 * 84 *
111 * @param {GetMetadataRequestedOptions} options Options. 85 * @param {GetMetadataRequestedOptions} options Options.
112 * @param {function(Object)} onSuccess Success callback with metadata passed 86 * @param {function(Object)} onSuccess Success callback with metadata passed
113 * an argument. 87 * an argument.
114 * @param {function(string)} onError Error callback with an error code. 88 * @param {function(string)} onError Error callback with an error code.
115 */ 89 */
116 function onGetMetadataRequested(options, onSuccess, onError) { 90 function onGetMetadataRequested(options, onSuccess, onError) {
117 if (options.fileSystemId != FILE_SYSTEM_ID) { 91 if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
118 onError('SECURITY'); // enum ProviderError. 92 onError('SECURITY'); // enum ProviderError.
119 return; 93 return;
120 } 94 }
121 95
122 if (options.entryPath == '/') { 96 if (options.entryPath == '/') {
123 onSuccess(TESTING_ROOT); 97 onSuccess(TESTING_ROOT);
124 return; 98 return;
125 } 99 }
126 100
127 if (options.entryPath == '/' + TESTING_HELLO_DIR.name) { 101 if (options.entryPath == '/' + TESTING_HELLO_DIR.name) {
128 onSuccess(TESTING_HELLO_DIR); 102 onSuccess(TESTING_HELLO_DIR);
129 return; 103 return;
130 } 104 }
131 105
132 onError('NOT_FOUND'); // enum ProviderError. 106 onError('NOT_FOUND'); // enum ProviderError.
133 } 107 }
134 108
135 /** 109 /**
136 * Sets up the tests. Called once per all test cases. In case of a failure, 110 * Sets up the tests. Called once per all test cases. In case of a failure,
137 * the callback is not called. 111 * the callback is not called.
138 * 112 *
139 * @param {function()} callback Success callback. 113 * @param {function()} callback Success callback.
140 */ 114 */
141 function setUp(callback) { 115 function setUp(callback) {
142 chrome.fileSystemProvider.mount( 116 chrome.fileSystemProvider.onGetMetadataRequested.addListener(
143 { 117 onGetMetadataRequested);
144 fileSystemId: FILE_SYSTEM_ID, 118 chrome.fileSystemProvider.onReadDirectoryRequested.addListener(
145 displayName: 'chocolate.zip' 119 onReadDirectoryRequested);
146 }, 120 test_util.mountFileSystem(callback);
147 function() {
148 chrome.fileSystemProvider.onReadDirectoryRequested.addListener(
149 onReadDirectoryRequested);
150 chrome.fileSystemProvider.onGetMetadataRequested.addListener(
151 onGetMetadataRequested);
152 getVolumeInfo(FILE_SYSTEM_ID, function(volumeInfo) {
153 chrome.test.assertTrue(!!volumeInfo);
154 chrome.fileBrowserPrivate.requestFileSystem(
155 volumeInfo.volumeId,
156 function(inFileSystem) {
157 chrome.test.assertTrue(!!inFileSystem);
158
159 fileSystem = inFileSystem;
160 callback();
161 });
162 });
163 },
164 function() {
165 chrome.test.fail();
166 });
167 } 121 }
168 122
169 /** 123 /**
170 * Runs all of the test cases, one by one. 124 * Runs all of the test cases, one by one.
171 */ 125 */
172 function runTests() { 126 function runTests() {
173 chrome.test.runTests([ 127 chrome.test.runTests([
174 // Read contents of the /hello directory. This directory exists, so it 128 // Read contents of the /hello directory. This directory exists, so it
175 // should succeed. 129 // should succeed.
176 function readEntriesSuccess() { 130 function readEntriesSuccess() {
177 var onTestSuccess = chrome.test.callbackPass(); 131 var onTestSuccess = chrome.test.callbackPass();
178 fileSystem.root.getDirectory( 132 test_util.fileSystem.root.getDirectory(
179 'hello', 133 'hello',
180 {create: false}, 134 {create: false},
181 function(dirEntry) { 135 function(dirEntry) {
182 var dirReader = dirEntry.createReader(); 136 var dirReader = dirEntry.createReader();
183 var entries = []; 137 var entries = [];
184 var readEntriesNext = function() { 138 var readEntriesNext = function() {
185 dirReader.readEntries(function(inEntries) { 139 dirReader.readEntries(function(inEntries) {
186 Array.prototype.push.apply(entries, inEntries); 140 Array.prototype.push.apply(entries, inEntries);
187 if (!inEntries.length) { 141 if (!inEntries.length) {
188 // No more entries, so verify. 142 // No more entries, so verify.
(...skipping 16 matching lines...) Expand all
205 readEntriesNext(); 159 readEntriesNext();
206 }, 160 },
207 function(error) { 161 function(error) {
208 chrome.test.fail(); 162 chrome.test.fail();
209 }); 163 });
210 }, 164 },
211 // Read contents of a directory which does not exist, what should return an 165 // Read contents of a directory which does not exist, what should return an
212 // error. 166 // error.
213 function readEntriesError() { 167 function readEntriesError() {
214 var onTestSuccess = chrome.test.callbackPass(); 168 var onTestSuccess = chrome.test.callbackPass();
215 fileSystem.root.getDirectory( 169 test_util.fileSystem.root.getDirectory(
216 'cranberries', 170 'cranberries',
217 {create: false}, 171 {create: false},
218 function(dirEntry) { 172 function(dirEntry) {
219 chrome.test.fail(); 173 chrome.test.fail();
220 }, 174 },
221 function(error) { 175 function(error) {
222 chrome.test.assertEq('NotFoundError', error.name); 176 chrome.test.assertEq('NotFoundError', error.name);
223 onTestSuccess(); 177 onTestSuccess();
224 }); 178 });
225 } 179 }
226 ]); 180 ]);
227 } 181 }
228 182
229 // Setup and run all of the test cases. 183 // Setup and run all of the test cases.
230 setUp(runTests); 184 setUp(runTests);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698