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.
8 * Sends a test message.
9 * @param {Object} message Message to be sent. It is converted into JSON string
11 * @return {Promise} Promise to be fulfilled with a returned value.
13 function sendTestMessage(message
) {
14 return new Promise(function(fulfill
) {
15 chrome
.test
.sendMessage(JSON
.stringify(message
), fulfill
);
20 * Returns promise to be fulfilled after the given milliseconds.
21 * @param {number} time Time in milliseconds.
24 return new Promise(function(callback
) {
25 setTimeout(callback
, time
);
30 * Interval milliseconds between checks of repeatUntil.
34 var REPEAT_UNTIL_INTERVAL
= 200;
37 * Interval milliseconds between log output of repeatUntil.
41 var LOG_INTERVAL
= 3000;
44 * Returns a pending marker. See also the repeatUntil function.
45 * @param {string} message Pending reason including %s, %d, or %j markers. %j
46 * format an object as JSON.
47 * @param {Array.<*>} var_args Values to be assigined to %x markers.
48 * @return {Object} Object which returns true for the expression: obj instanceof
51 function pending(message
, var_args
) {
54 var formattedMessage
= message
.replace(/%[sdj]/g, function(pattern
) {
55 var arg
= args
[index
++];
57 case '%s': return String(arg
);
58 case '%d': return Number(arg
);
59 case '%j': return JSON
.stringify(arg
);
60 default: return pattern
;
63 var pendingMarker
= Object
.create(pending
.prototype);
64 pendingMarker
.message
= formattedMessage
;
69 * Waits until the checkFunction returns a value but a pending marker.
70 * @param {function():*} checkFunction Function to check a condition. It can
71 * return a pending marker created by a pending function.
72 * @return {Promise} Promise to be fulfilled with the return value of
73 * checkFunction when the checkFunction reutrns a value but a pending
76 function repeatUntil(checkFunction
) {
77 var logTime
= Date
.now() + LOG_INTERVAL
;
78 var step = function() {
79 return Promise
.resolve(checkFunction()).then(function(result
) {
80 if (result
instanceof pending
) {
81 if (Date
.now() > logTime
) {
82 console
.log(result
.message
);
83 logTime
+= LOG_INTERVAL
;
85 return wait(REPEAT_UNTIL_INTERVAL
).then(step
);
95 * Adds the givin entries to the target volume(s).
96 * @param {Array.<string>} volumeNames Names of target volumes.
97 * @param {Array.<TestEntryInfo>} entries List of entries to be added.
98 * @param {function(boolean)=} opt_callback Callback function to be passed the
99 * result of function. The argument is true on success.
100 * @return {Promise} Promise to be fulfilled when the entries are added.
102 function addEntries(volumeNames
, entries
, opt_callback
) {
103 if (volumeNames
.length
== 0) {
107 var volumeResultPromises
= volumeNames
.map(function(volume
) {
108 return sendTestMessage({
114 var resultPromise
= Promise
.all(volumeResultPromises
);
116 resultPromise
.then(opt_callback
.bind(null, true),
117 opt_callback
.bind(null, false));
119 return resultPromise
;
126 var EntryType
= Object
.freeze({
128 DIRECTORY
: 'directory'
135 var SharedOption
= Object
.freeze({
143 var RootPath
= Object
.seal({
144 DOWNLOADS
: '/must-be-filled-in-test-setup',
145 DRIVE
: '/must-be-filled-in-test-setup',
149 * File system entry information for tests.
151 * @param {EntryType} type Entry type.
152 * @param {string} sourceFileName Source file name that provides file contents.
153 * @param {string} targetName Name of entry on the test file system.
154 * @param {string} mimeType Mime type.
155 * @param {SharedOption} sharedOption Shared option.
156 * @param {string} lastModifiedTime Last modified time as a text to be shown in
157 * the last modified column.
158 * @param {string} nameText File name to be shown in the name column.
159 * @param {string} sizeText Size text to be shown in the size column.
160 * @param {string} typeText Type name to be shown in the type column.
163 function TestEntryInfo(type
,
173 this.sourceFileName
= sourceFileName
|| '';
174 this.targetPath
= targetPath
;
175 this.mimeType
= mimeType
|| '';
176 this.sharedOption
= sharedOption
;
177 this.lastModifiedTime
= lastModifiedTime
;
178 this.nameText
= nameText
;
179 this.sizeText
= sizeText
;
180 this.typeText
= typeText
;
184 TestEntryInfo
.getExpectedRows = function(entries
) {
185 return entries
.map(function(entry
) { return entry
.getExpectedRow(); });
189 * Obtains a expected row contents of the file in the file list.
191 TestEntryInfo
.prototype.getExpectedRow = function() {
192 return [this.nameText
, this.sizeText
, this.typeText
, this.lastModifiedTime
];
196 * Filesystem entries used by the test cases.
197 * @type {Object.<string, TestEntryInfo>}
201 hello
: new TestEntryInfo(
202 EntryType
.FILE
, 'text.txt', 'hello.txt',
203 'text/plain', SharedOption
.NONE
, 'Sep 4, 1998, 12:34 PM',
204 'hello.txt', '51 bytes', 'Plain text'),
206 world
: new TestEntryInfo(
207 EntryType
.FILE
, 'video.ogv', 'world.ogv',
208 'text/plain', SharedOption
.NONE
, 'Jul 4, 2012, 10:35 AM',
209 'world.ogv', '59 KB', 'OGG video'),
211 unsupported
: new TestEntryInfo(
212 EntryType
.FILE
, 'random.bin', 'unsupported.foo',
213 'application/x-foo', SharedOption
.NONE
, 'Jul 4, 2012, 10:36 AM',
214 'unsupported.foo', '8 KB', 'FOO file'),
216 desktop
: new TestEntryInfo(
217 EntryType
.FILE
, 'image.png', 'My Desktop Background.png',
218 'image/png', SharedOption
.NONE
, 'Jan 18, 2038, 1:02 AM',
219 'My Desktop Background.png', '272 bytes', 'PNG image'),
221 image2
: new TestEntryInfo(
222 EntryType
.FILE
, 'image2.png', 'image2.png',
223 'image/png', SharedOption
.NONE
, 'Jan 18, 2038, 1:02 AM',
224 'image2.png', '272 bytes', 'PNG image'),
226 image3
: new TestEntryInfo(
227 EntryType
.FILE
, 'image3.jpg', 'image3.jpg',
228 'image/jpg', SharedOption
.NONE
, 'Jan 18, 2038, 1:02 AM',
229 'image3.jpg', '272 bytes', 'JPEG image'),
231 beautiful
: new TestEntryInfo(
232 EntryType
.FILE
, 'music.ogg', 'Beautiful Song.ogg',
233 'text/plain', SharedOption
.NONE
, 'Nov 12, 2086, 12:00 PM',
234 'Beautiful Song.ogg', '14 KB', 'OGG audio'),
236 photos
: new TestEntryInfo(
237 EntryType
.DIRECTORY
, null, 'photos',
238 null, SharedOption
.NONE
, 'Jan 1, 1980, 11:59 PM',
239 'photos', '--', 'Folder'),
241 testDocument
: new TestEntryInfo(
242 EntryType
.FILE
, null, 'Test Document',
243 'application/vnd.google-apps.document',
244 SharedOption
.NONE
, 'Apr 10, 2013, 4:20 PM',
245 'Test Document.gdoc', '--', 'Google document'),
247 testSharedDocument
: new TestEntryInfo(
248 EntryType
.FILE
, null, 'Test Shared Document',
249 'application/vnd.google-apps.document',
250 SharedOption
.SHARED
, 'Mar 20, 2013, 10:40 PM',
251 'Test Shared Document.gdoc', '--', 'Google document'),
253 newlyAdded
: new TestEntryInfo(
254 EntryType
.FILE
, 'music.ogg', 'newly added file.ogg',
255 'audio/ogg', SharedOption
.NONE
, 'Sep 4, 1998, 12:00 AM',
256 'newly added file.ogg', '14 KB', 'OGG audio'),
258 directoryA
: new TestEntryInfo(
259 EntryType
.DIRECTORY
, null, 'A',
260 null, SharedOption
.NONE
, 'Jan 1, 2000, 1:00 AM',
261 'A', '--', 'Folder'),
263 directoryB
: new TestEntryInfo(
264 EntryType
.DIRECTORY
, null, 'A/B',
265 null, SharedOption
.NONE
, 'Jan 1, 2000, 1:00 AM',
266 'B', '--', 'Folder'),
268 directoryC
: new TestEntryInfo(
269 EntryType
.DIRECTORY
, null, 'A/B/C',
270 null, SharedOption
.NONE
, 'Jan 1, 2000, 1:00 AM',
271 'C', '--', 'Folder'),
273 directoryD
: new TestEntryInfo(
274 EntryType
.DIRECTORY
, null, 'D',
275 null, SharedOption
.NONE
, 'Jan 1, 2000, 1:00 AM',
276 'D', '--', 'Folder'),
278 directoryE
: new TestEntryInfo(
279 EntryType
.DIRECTORY
, null, 'D/E',
280 null, SharedOption
.NONE
, 'Jan 1, 2000, 1:00 AM',
281 'E', '--', 'Folder'),
283 directoryF
: new TestEntryInfo(
284 EntryType
.DIRECTORY
, null, 'D/E/F',
285 null, SharedOption
.NONE
, 'Jan 1, 2000, 1:00 AM',
286 'F', '--', 'Folder'),
288 zipArchive
: new TestEntryInfo(
289 EntryType
.FILE
, 'archive.zip', 'archive.zip',
290 'application/x-zip', SharedOption
.NONE
, 'Jan 1, 2014, 1:00 AM',
291 'archive.zip', '533 bytes', 'Zip archive')