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.
11 var TESTING_ROOT = Object.freeze({
15 modificationTime: new Date(2013, 3, 27, 9, 38, 14)
22 var TESTING_WITH_VALID_THUMBNAIL_FILE = Object.freeze({
24 name: 'valid-thumbnail.txt',
26 modificationTime: new Date(2014, 4, 28, 10, 39, 15),
27 thumbnail: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA' +
28 'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO' +
29 '9TXL0Y4OHwAAAABJRU5ErkJggg=='
36 var TESTING_ALWAYS_WITH_THUMBNAIL_FILE = Object.freeze({
38 name: 'always-with-thumbnail.txt',
40 modificationTime: new Date(2014, 4, 28, 10, 39, 15),
41 thumbnail: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA' +
42 'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO' +
43 '9TXL0Y4OHwAAAABJRU5ErkJggg=='
50 var TESTING_WITH_INVALID_THUMBNAIL_FILE = Object.freeze({
52 name: 'invalid-thumbnail.txt',
54 modificationTime: new Date(2014, 4, 28, 10, 39, 15),
55 thumbnail: 'https://www.foobar.com/evil'
59 * Returns metadata for a requested entry.
61 * @param {GetMetadataRequestedOptions} options Options.
62 * @param {function(Object)} onSuccess Success callback with metadata passed
64 * @param {function(string)} onError Error callback with an error code.
66 function onGetMetadataRequested(options, onSuccess, onError) {
67 if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
68 onError('SECURITY'); // enum ProviderError.
72 // Metadata to be returned.
75 switch (options.entryPath) {
77 metadata = TESTING_ROOT;
80 case '/' + TESTING_WITH_VALID_THUMBNAIL_FILE.name:
81 metadata = TESTING_WITH_VALID_THUMBNAIL_FILE;
84 case '/' + TESTING_ALWAYS_WITH_THUMBNAIL_FILE.name:
85 metadata = TESTING_ALWAYS_WITH_THUMBNAIL_FILE;
88 case '/' + TESTING_WITH_INVALID_THUMBNAIL_FILE.name:
89 metadata = TESTING_WITH_INVALID_THUMBNAIL_FILE;
93 onError('NOT_FOUND'); // enum ProviderError.
97 // Returning a thumbnail while not requested is not allowed for performance
98 // reasons. Remove the field if needed. However, do not remove it for one
99 // file, to simulate an error.
100 if (!options.thumbnail && metadata.thumbnail &&
101 options.entryPath != '/' + TESTING_ALWAYS_WITH_THUMBNAIL_FILE.name) {
102 var metadataWithoutThumbnail = {
103 isDirectory: metadata.isDirectory,
106 modificationTime: metadata.modificationTime
108 onSuccess(metadataWithoutThumbnail);
115 * Sets up the tests. Called once per all test cases. In case of a failure,
116 * the callback is not called.
118 * @param {function()} callback Success callback.
120 function setUp(callback) {
121 chrome.fileSystemProvider.onGetMetadataRequested.addListener(
122 onGetMetadataRequested);
123 test_util.mountFileSystem(callback);
127 * Runs all of the test cases, one by one.
129 function runTests() {
130 chrome.test.runTests([
131 // Test if providers are notified that no thumbnail is requested when normal
132 // metadata is requested.
133 function notRequestedAndNotProvidedThumbnailSuccess() {
134 var onSuccess = chrome.test.callbackPass();
135 test_util.fileSystem.root.getFile(
136 TESTING_WITH_VALID_THUMBNAIL_FILE.name,
138 function(fileEntry) {
141 chrome.test.fail(error.name);
145 // If providers return a thumbnail data despite not being requested for
146 // that, then the operation must fail.
147 function notRequestedButProvidedThumbnailError() {
148 var onSuccess = chrome.test.callbackPass();
149 test_util.fileSystem.root.getFile(
150 TESTING_ALWAYS_WITH_THUMBNAIL_FILE.name,
152 function(fileEntry) {
154 'Thumbnail returned when not requested should result in an ' +
155 'error, but the operation succeeded.');
157 chrome.test.assertEq('InvalidStateError', error.name);
162 // Thumbnails should be returned when available for private API request.
163 function getEntryPropertiesWithThumbnailSuccess() {
164 var onSuccess = chrome.test.callbackPass();
165 test_util.fileSystem.root.getFile(
166 TESTING_WITH_VALID_THUMBNAIL_FILE.name,
168 function(fileEntry) {
169 chrome.fileManagerPrivate.getEntryProperties(
171 function(fileProperties) {
172 chrome.test.assertEq(1, fileProperties.length);
173 chrome.test.assertEq(
174 TESTING_WITH_VALID_THUMBNAIL_FILE.thumbnail,
175 fileProperties[0].thumbnailUrl);
176 chrome.test.assertEq(
177 TESTING_WITH_VALID_THUMBNAIL_FILE.fileSize,
178 fileProperties[0].size);
179 chrome.test.assertEq(
180 TESTING_WITH_VALID_THUMBNAIL_FILE.modificationTime,
181 new Date(fileProperties[0].lastModifiedTime));
186 chrome.test.fail(error.name);
190 // Confirm that extensions are not able to pass an invalid thumbnail url,
191 // including evil urls.
192 function getEntryPropertiesWithInvalidThumbnail() {
193 var onSuccess = chrome.test.callbackPass();
194 test_util.fileSystem.root.getFile(
195 TESTING_WITH_INVALID_THUMBNAIL_FILE.name,
197 function(fileEntry) {
198 chrome.fileManagerPrivate.getEntryProperties(
200 function(fileProperties) {
201 chrome.test.assertEq(1, fileProperties.length);
202 // The results for an entry is an empty dictionary in case of
204 chrome.test.assertEq(
205 0, Object.keys(fileProperties[0]).length);
210 chrome.test.fail(error.name);
216 // Setup and run all of the test cases.