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 test_util
.fileSystem
.root
.getFile(
135 TESTING_WITH_VALID_THUMBNAIL_FILE
.name
,
137 chrome
.test
.callbackPass(),
139 chrome
.test
.fail(error
.name
);
143 // If providers return a thumbnail data despite not being requested for
144 // that, then the operation must fail.
145 function notRequestedButProvidedThumbnailError() {
146 test_util
.fileSystem
.root
.getFile(
147 TESTING_ALWAYS_WITH_THUMBNAIL_FILE
.name
,
149 function(fileEntry
) {
151 'Thumbnail returned when not requested should result in an ' +
152 'error, but the operation succeeded.');
153 }, chrome
.test
.callbackPass(function(error
) {
154 chrome
.test
.assertEq('InvalidStateError', error
.name
);
158 // Thumbnails should be returned when available for private API request.
159 function getEntryPropertiesWithThumbnailSuccess() {
160 test_util
.fileSystem
.root
.getFile(
161 TESTING_WITH_VALID_THUMBNAIL_FILE
.name
,
163 chrome
.test
.callbackPass(function(fileEntry
) {
164 chrome
.fileManagerPrivate
.getEntryProperties(
166 chrome
.test
.callbackPass(function(fileProperties
) {
167 chrome
.test
.assertEq(1, fileProperties
.length
);
168 chrome
.test
.assertEq(
169 TESTING_WITH_VALID_THUMBNAIL_FILE
.thumbnail
,
170 fileProperties
[0].thumbnailUrl
);
171 chrome
.test
.assertEq(
172 TESTING_WITH_VALID_THUMBNAIL_FILE
.fileSize
,
173 fileProperties
[0].size
);
174 chrome
.test
.assertEq(
175 TESTING_WITH_VALID_THUMBNAIL_FILE
.modificationTime
,
176 new Date(fileProperties
[0].lastModifiedTime
));
180 chrome
.test
.fail(error
.name
);
184 // Confirm that extensions are not able to pass an invalid thumbnail url,
185 // including evil urls.
186 function getEntryPropertiesWithInvalidThumbnail() {
187 test_util
.fileSystem
.root
.getFile(
188 TESTING_WITH_INVALID_THUMBNAIL_FILE
.name
,
190 chrome
.test
.callbackPass(function(fileEntry
) {
191 chrome
.fileManagerPrivate
.getEntryProperties(
193 chrome
.test
.callbackPass(function(fileProperties
) {
194 chrome
.test
.assertEq(1, fileProperties
.length
);
195 // The results for an entry is an empty dictionary in case of
197 chrome
.test
.assertEq(
198 0, Object
.keys(fileProperties
[0]).length
);
202 chrome
.test
.fail(error
.name
);
208 // Setup and run all of the test cases.