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

Side by Side Diff: chrome/test/data/extensions/api_test/file_system_provider/get_metadata/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}
9 */
10 var fileSystem = null;
11
12 /**
13 * @type {string}
14 * @const
15 */
16 var FILE_SYSTEM_ID = 'vanilla';
17
18 /**
19 * @type {Object} 8 * @type {Object}
20 * @const 9 * @const
21 */ 10 */
22 var TESTING_ROOT = Object.freeze({ 11 var TESTING_ROOT = Object.freeze({
23 isDirectory: true, 12 isDirectory: true,
24 name: '', 13 name: '',
25 size: 0, 14 size: 0,
26 modificationTime: new Date(2013, 3, 27, 9, 38, 14) 15 modificationTime: new Date(2013, 3, 27, 9, 38, 14)
27 }); 16 });
28 17
(...skipping 13 matching lines...) Expand all
42 * @const 31 * @const
43 */ 32 */
44 var TESTING_WRONG_TIME_FILE = Object.freeze({ 33 var TESTING_WRONG_TIME_FILE = Object.freeze({
45 isDirectory: false, 34 isDirectory: false,
46 name: 'invalid-time.txt', 35 name: 'invalid-time.txt',
47 size: 4096, 36 size: 4096,
48 modificationTime: new Date('Invalid date.') 37 modificationTime: new Date('Invalid date.')
49 }); 38 });
50 39
51 /** 40 /**
52 * Gets volume information for the provided file system.
53 *
54 * @param {string} fileSystemId Id of the provided file system.
55 * @param {function(Object)} callback Callback to be called on result, with the
56 * volume information object in case of success, or null if not found.
57 */
58 function getVolumeInfo(fileSystemId, callback) {
59 chrome.fileBrowserPrivate.getVolumeMetadataList(function(volumeList) {
60 for (var i = 0; i < volumeList.length; i++) {
61 if (volumeList[i].extensionId == chrome.runtime.id &&
62 volumeList[i].fileSystemId == fileSystemId) {
63 callback(volumeList[i]);
64 return;
65 }
66 }
67 callback(null);
68 });
69 }
70
71 /**
72 * Returns metadata for a requested entry. 41 * Returns metadata for a requested entry.
73 * 42 *
74 * @param {GetMetadataRequestedOptions} options Options. 43 * @param {GetMetadataRequestedOptions} options Options.
75 * @param {function(Object)} onSuccess Success callback with metadata passed 44 * @param {function(Object)} onSuccess Success callback with metadata passed
76 * an argument. 45 * an argument.
77 * @param {function(string)} onError Error callback with an error code. 46 * @param {function(string)} onError Error callback with an error code.
78 */ 47 */
79 function onGetMetadataRequested(options, onSuccess, onError) { 48 function onGetMetadataRequested(options, onSuccess, onError) {
80 if (options.fileSystemId != FILE_SYSTEM_ID) { 49 if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
81 onError('SECURITY'); // enum ProviderError. 50 onError('SECURITY'); // enum ProviderError.
82 return; 51 return;
83 } 52 }
84 53
85 if (options.entryPath == '/') { 54 if (options.entryPath == '/') {
86 onSuccess(TESTING_ROOT); 55 onSuccess(TESTING_ROOT);
87 return; 56 return;
88 } 57 }
89 58
90 if (options.entryPath == '/' + TESTING_FILE.name) { 59 if (options.entryPath == '/' + TESTING_FILE.name) {
91 onSuccess(TESTING_FILE); 60 onSuccess(TESTING_FILE);
92 return; 61 return;
93 } 62 }
94 63
95 if (options.entryPath == '/' + TESTING_WRONG_TIME_FILE.name) { 64 if (options.entryPath == '/' + TESTING_WRONG_TIME_FILE.name) {
96 onSuccess(TESTING_WRONG_TIME_FILE); 65 onSuccess(TESTING_WRONG_TIME_FILE);
97 return; 66 return;
98 } 67 }
99 68
100 onError('NOT_FOUND'); // enum ProviderError. 69 onError('NOT_FOUND'); // enum ProviderError.
101 } 70 }
102 71
103 /** 72 /**
104 * Sets up the tests. Called once per all test cases. In case of a failure, 73 * Sets up the tests. Called once per all test cases. In case of a failure,
105 * the callback is not called. 74 * the callback is not called.
106 * 75 *
107 * @param {function()} callback Success callback. 76 * @param {function()} callback Success callback.
108 */ 77 */
109 function setUp(callback) { 78 function setUp(callback) {
110 chrome.fileSystemProvider.mount( 79 chrome.fileSystemProvider.onGetMetadataRequested.addListener(
111 {fileSystemId: FILE_SYSTEM_ID, displayName: 'chocolate.zip'}, 80 onGetMetadataRequested);
112 function() { 81 test_util.mountFileSystem(callback);
113 chrome.fileSystemProvider.onGetMetadataRequested.addListener(
114 onGetMetadataRequested);
115
116 getVolumeInfo(FILE_SYSTEM_ID, function(volumeInfo) {
117 chrome.test.assertTrue(!!volumeInfo);
118 chrome.fileBrowserPrivate.requestFileSystem(
119 volumeInfo.volumeId,
120 function(inFileSystem) {
121 chrome.test.assertTrue(!!inFileSystem);
122
123 fileSystem = inFileSystem;
124 callback();
125 });
126 });
127 },
128 function() {
129 chrome.test.fail();
130 });
131 } 82 }
132 83
133 /** 84 /**
134 * Runs all of the test cases, one by one. 85 * Runs all of the test cases, one by one.
135 */ 86 */
136 function runTests() { 87 function runTests() {
137 chrome.test.runTests([ 88 chrome.test.runTests([
138 // Read metadata of the root. 89 // Read metadata of the root.
139 function getFileMetadataSuccess() { 90 function getFileMetadataSuccess() {
140 var onSuccess = chrome.test.callbackPass(); 91 var onSuccess = chrome.test.callbackPass();
141 fileSystem.root.getMetadata( 92 test_util.fileSystem.root.getMetadata(
142 function(metadata) { 93 function(metadata) {
143 chrome.test.assertEq(TESTING_ROOT.size, metadata.size); 94 chrome.test.assertEq(TESTING_ROOT.size, metadata.size);
144 chrome.test.assertEq( 95 chrome.test.assertEq(
145 TESTING_ROOT.modificationTime.toString(), 96 TESTING_ROOT.modificationTime.toString(),
146 metadata.modificationTime.toString()); 97 metadata.modificationTime.toString());
147 onSuccess(); 98 onSuccess();
148 }, function(error) { 99 }, function(error) {
149 chrome.test.fail(error.name); 100 chrome.test.fail(error.name);
150 }); 101 });
151 }, 102 },
152 // Read metadata of an existing testing file. 103 // Read metadata of an existing testing file.
153 function getFileMetadataSuccess() { 104 function getFileMetadataSuccess() {
154 var onSuccess = chrome.test.callbackPass(); 105 var onSuccess = chrome.test.callbackPass();
155 fileSystem.root.getFile( 106 test_util.fileSystem.root.getFile(
156 TESTING_FILE.name, 107 TESTING_FILE.name,
157 {create: false}, 108 {create: false},
158 function(fileEntry) { 109 function(fileEntry) {
159 chrome.test.assertEq(TESTING_FILE.name, fileEntry.name); 110 chrome.test.assertEq(TESTING_FILE.name, fileEntry.name);
160 chrome.test.assertEq( 111 chrome.test.assertEq(
161 TESTING_FILE.isDirectory, fileEntry.isDirectory); 112 TESTING_FILE.isDirectory, fileEntry.isDirectory);
162 fileEntry.getMetadata(function(metadata) { 113 fileEntry.getMetadata(function(metadata) {
163 chrome.test.assertEq(TESTING_FILE.size, metadata.size); 114 chrome.test.assertEq(TESTING_FILE.size, metadata.size);
164 chrome.test.assertEq( 115 chrome.test.assertEq(
165 TESTING_FILE.modificationTime.toString(), 116 TESTING_FILE.modificationTime.toString(),
166 metadata.modificationTime.toString()); 117 metadata.modificationTime.toString());
167 onSuccess(); 118 onSuccess();
168 }, function(error) { 119 }, function(error) {
169 chrome.test.fail(error.name); 120 chrome.test.fail(error.name);
170 }); 121 });
171 }, 122 },
172 function(error) { 123 function(error) {
173 chrome.test.fail(error.name); 124 chrome.test.fail(error.name);
174 }); 125 });
175 }, 126 },
176 // Read metadata of an existing testing file, which however has an invalid 127 // Read metadata of an existing testing file, which however has an invalid
177 // modification time. It should not cause an error, but an invalid date 128 // modification time. It should not cause an error, but an invalid date
178 // should be passed to fileapi instead. The reason is, that there is no 129 // should be passed to fileapi instead. The reason is, that there is no
179 // easy way to verify an incorrect modification time at early stage. 130 // easy way to verify an incorrect modification time at early stage.
180 function getFileMetadataWrongTimeSuccess() { 131 function getFileMetadataWrongTimeSuccess() {
181 var onSuccess = chrome.test.callbackPass(); 132 var onSuccess = chrome.test.callbackPass();
182 fileSystem.root.getFile( 133 test_util.fileSystem.root.getFile(
183 TESTING_WRONG_TIME_FILE.name, 134 TESTING_WRONG_TIME_FILE.name,
184 {create: false}, 135 {create: false},
185 function(fileEntry) { 136 function(fileEntry) {
186 chrome.test.assertEq(TESTING_WRONG_TIME_FILE.name, fileEntry.name); 137 chrome.test.assertEq(TESTING_WRONG_TIME_FILE.name, fileEntry.name);
187 fileEntry.getMetadata(function(metadata) { 138 fileEntry.getMetadata(function(metadata) {
188 chrome.test.assertTrue( 139 chrome.test.assertTrue(
189 Number.isNaN(metadata.modificationTime.getTime())); 140 Number.isNaN(metadata.modificationTime.getTime()));
190 onSuccess(); 141 onSuccess();
191 }, function(error) { 142 }, function(error) {
192 chrome.test.fail(error.name); 143 chrome.test.fail(error.name);
193 }); 144 });
194 }, function(error) { 145 }, function(error) {
195 chrome.test.fail(error.name); 146 chrome.test.fail(error.name);
196 }); 147 });
197 }, 148 },
198 // Read metadata of a directory which does not exist, what should return an 149 // Read metadata of a directory which does not exist, what should return an
199 // error. DirectoryEntry.getDirectory() causes fetching metadata. 150 // error. DirectoryEntry.getDirectory() causes fetching metadata.
200 function getFileMetadataNotFound() { 151 function getFileMetadataNotFound() {
201 var onSuccess = chrome.test.callbackPass(); 152 var onSuccess = chrome.test.callbackPass();
202 fileSystem.root.getDirectory( 153 test_util.fileSystem.root.getDirectory(
203 'cranberries', 154 'cranberries',
204 {create: false}, 155 {create: false},
205 function(dirEntry) { 156 function(dirEntry) {
206 chrome.test.fail(); 157 chrome.test.fail();
207 }, 158 },
208 function(error) { 159 function(error) {
209 chrome.test.assertEq('NotFoundError', error.name); 160 chrome.test.assertEq('NotFoundError', error.name);
210 onSuccess(); 161 onSuccess();
211 }); 162 });
212 }, 163 },
213 // Read metadata of a file using getDirectory(). An error should be returned 164 // Read metadata of a file using getDirectory(). An error should be returned
214 // because of type mismatching. DirectoryEntry.getDirectory() causes 165 // because of type mismatching. DirectoryEntry.getDirectory() causes
215 // fetching metadata. 166 // fetching metadata.
216 function getFileMetadataWrongType() { 167 function getFileMetadataWrongType() {
217 var onSuccess = chrome.test.callbackPass(); 168 var onSuccess = chrome.test.callbackPass();
218 fileSystem.root.getDirectory( 169 test_util.fileSystem.root.getDirectory(
219 TESTING_FILE.name, 170 TESTING_FILE.name,
220 {create: false}, 171 {create: false},
221 function(fileEntry) { 172 function(fileEntry) {
222 chrome.test.fail(); 173 chrome.test.fail();
223 }, 174 },
224 function(error) { 175 function(error) {
225 chrome.test.assertEq('TypeMismatchError', error.name); 176 chrome.test.assertEq('TypeMismatchError', error.name);
226 onSuccess(); 177 onSuccess();
227 }); 178 });
228 } 179 }
229 ]); 180 ]);
230 } 181 }
231 182
232 // Setup and run all of the test cases. 183 // Setup and run all of the test cases.
233 setUp(runTests); 184 setUp(runTests);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698