Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / thumbnail / test.js
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 /**
8  * @type {Object}
9  * @const
10  */
11 var TESTING_ROOT = Object.freeze({
12   isDirectory: true,
13   name: '',
14   size: 0,
15   modificationTime: new Date(2013, 3, 27, 9, 38, 14)
16 });
17
18 /**
19  * @type {Object}
20  * @const
21  */
22 var TESTING_WITH_VALID_THUMBNAIL_FILE = Object.freeze({
23   isDirectory: false,
24   name: 'valid-thumbnail.txt',
25   size: 4096,
26   modificationTime: new Date(2014, 4, 28, 10, 39, 15),
27   thumbnail: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA' +
28              'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO' +
29              '9TXL0Y4OHwAAAABJRU5ErkJggg=='
30 });
31
32 /**
33  * @type {Object}
34  * @const
35  */
36 var TESTING_ALWAYS_WITH_THUMBNAIL_FILE = Object.freeze({
37   isDirectory: false,
38   name: 'always-with-thumbnail.txt',
39   size: 4096,
40   modificationTime: new Date(2014, 4, 28, 10, 39, 15),
41   thumbnail: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA' +
42              'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO' +
43              '9TXL0Y4OHwAAAABJRU5ErkJggg=='
44 });
45
46 /**
47  * @type {Object}
48  * @const
49  */
50 var TESTING_WITH_INVALID_THUMBNAIL_FILE = Object.freeze({
51   isDirectory: false,
52   name: 'invalid-thumbnail.txt',
53   size: 4096,
54   modificationTime: new Date(2014, 4, 28, 10, 39, 15),
55   thumbnail: 'https://www.foobar.com/evil'
56 });
57
58 /**
59  * Returns metadata for a requested entry.
60  *
61  * @param {GetMetadataRequestedOptions} options Options.
62  * @param {function(Object)} onSuccess Success callback with metadata passed
63  *     an argument.
64  * @param {function(string)} onError Error callback with an error code.
65  */
66 function onGetMetadataRequested(options, onSuccess, onError) {
67   if (options.fileSystemId != test_util.FILE_SYSTEM_ID) {
68     onError('SECURITY');  // enum ProviderError.
69     return;
70   }
71
72   // Metadata to be returned.
73   var metadata;
74
75   switch (options.entryPath) {
76     case '/':
77       metadata = TESTING_ROOT;
78       break;
79
80     case '/' + TESTING_WITH_VALID_THUMBNAIL_FILE.name:
81       metadata = TESTING_WITH_VALID_THUMBNAIL_FILE;
82       break;
83
84     case '/' + TESTING_ALWAYS_WITH_THUMBNAIL_FILE.name:
85       metadata = TESTING_ALWAYS_WITH_THUMBNAIL_FILE;
86       break;
87
88     case '/' + TESTING_WITH_INVALID_THUMBNAIL_FILE.name:
89       metadata = TESTING_WITH_INVALID_THUMBNAIL_FILE;
90       break;
91
92     default:
93       onError('NOT_FOUND');  // enum ProviderError.
94       return;
95   }
96
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,
104       name: metadata.name,
105       size: metadata.size,
106       modificationTime: metadata.modificationTime
107     };
108     onSuccess(metadataWithoutThumbnail);
109   } else {
110     onSuccess(metadata);
111   }
112 }
113
114 /**
115  * Sets up the tests. Called once per all test cases. In case of a failure,
116  * the callback is not called.
117  *
118  * @param {function()} callback Success callback.
119  */
120 function setUp(callback) {
121   chrome.fileSystemProvider.onGetMetadataRequested.addListener(
122       onGetMetadataRequested);
123   test_util.mountFileSystem(callback);
124 }
125
126 /**
127  * Runs all of the test cases, one by one.
128  */
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,
137           {create: false},
138           function(fileEntry) {
139             onSuccess();
140           }, function(error) {
141             chrome.test.fail(error.name);
142           });
143     },
144
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,
151           {create: false},
152           function(fileEntry) {
153             chrome.test.fail(
154                 'Thumbnail returned when not requested should result in an ' +
155                 'error, but the operation succeeded.');
156           }, function(error) {
157             chrome.test.assertEq('InvalidStateError', error.name);
158             onSuccess();
159           });
160     },
161
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,
167           {create: false},
168           function(fileEntry) {
169             chrome.fileManagerPrivate.getEntryProperties(
170                 [fileEntry.toURL()],
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));
182                   onSuccess();
183                 });
184             },
185             function(error) {
186               chrome.test.fail(error.name);
187             });
188     },
189
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,
196           {create: false},
197           function(fileEntry) {
198             chrome.fileManagerPrivate.getEntryProperties(
199                 [fileEntry.toURL()],
200                 function(fileProperties) {
201                   chrome.test.assertEq(1, fileProperties.length);
202                   // The results for an entry is an empty dictionary in case of
203                   // an error.
204                   chrome.test.assertEq(
205                       0, Object.keys(fileProperties[0]).length);
206                   onSuccess();
207                 });
208             },
209             function(error) {
210               chrome.test.fail(error.name);
211             });
212     }
213   ]);
214 }
215
216 // Setup and run all of the test cases.
217 setUp(runTests);