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.
7 * Mock of chrome.runtime.
16 * Mock of chrome.power.
21 requestKeepAwake: function() {
22 chrome.power.keepAwakeRequested = true;
24 releaseKeepAwake: function() {
25 chrome.power.keepAwakeRequested = false;
27 keepAwakeRequested: false
31 * Mock of chrome.fileManagerPrivate.
35 chrome.fileManagerPrivate = {
37 addListener: function(callback) {
38 chrome.fileManagerPrivate.onCopyProgress.listener_ = callback;
40 removeListener: function() {
41 chrome.fileManagerPrivate.onCopyProgress.listener_ = null;
48 * Reports the result of promise to the test system.
49 * @param {Promise} promise Promise to be fulfilled or rejected.
50 * @param {function(boolean:hasError)} callback Callback to be passed true on
53 function reportPromise(promise, callback) {
55 callback.bind(null, false),
57 if (error instanceof FileOperationManager.Error) {
58 console.error('FileOperationManager.Error: code=' + error.code);
60 console.error(error.stack || error.name || error);
71 var DIRECTORY_SIZE = -1;
74 * Creates test file system.
75 * @param {string} id File system ID.
76 * @param {Object.<string, number>} entries Map of entries' paths and their
77 * size. If the size is equals to DIRECTORY_SIZE, the entry is derectory.
79 function createTestFileSystem(id, entries) {
80 var fileSystem = new TestFileSystem(id);
81 for (var path in entries) {
82 if (entries[path] === DIRECTORY_SIZE) {
83 fileSystem.entries[path] = new MockDirectoryEntry(fileSystem, path);
85 fileSystem.entries[path] =
86 new MockFileEntry(fileSystem, path, {size: entries[path]});
93 * Resolves URL on the file system.
94 * @param {FakeFileSystem} fileSystem Fake file system.
95 * @param {string} url URL.
96 * @param {function(MockEntry)} success Success callback.
97 * @param {function()} failure Failure callback.
99 function resolveTestFileSystemURL(fileSystem, url, success, failure) {
100 for (var name in fileSystem.entries) {
101 var entry = fileSystem.entries[name];
102 if (entry.toURL() == url) {
111 * Waits for events until 'success'.
112 * @param {FileOperationManager} fileOperationManager File operation manager.
113 * @return {Promise} Promise to be fulfilled with an event list.
115 function waitForEvents(fileOperationManager) {
116 return new Promise(function(fulfill) {
118 fileOperationManager.addEventListener('copy-progress', function(event) {
120 if (event.reason === 'SUCCESS')
123 fileOperationManager.addEventListener('entries-changed', function(event) {
126 fileOperationManager.addEventListener('delete', function(event) {
128 if (event.reason === 'SUCCESS')
136 * @type {FileOperationManager}
138 var fileOperationManager;
141 * Initializes the test environment.
144 fileOperationManager = new FileOperationManager();
148 * Tests the fileOperationUtil.resolvePath function.
149 * @param {function(boolean:hasError)} callback Callback to be passed true on
152 function testResolvePath(callback) {
153 var fileSystem = createTestFileSystem('testVolume', {
156 '/directory': DIRECTORY_SIZE
158 var root = fileSystem.root;
159 var rootPromise = fileOperationUtil.resolvePath(root, '/');
160 var filePromise = fileOperationUtil.resolvePath(root, '/file');
161 var directoryPromise = fileOperationUtil.resolvePath(root, '/directory');
162 var errorPromise = fileOperationUtil.resolvePath(root, '/not_found').then(
163 function() { assertTrue(false, 'The NOT_FOUND error is not reported.'); },
164 function(error) { return error.name; });
165 reportPromise(Promise.all([
170 ]).then(function(results) {
172 fileSystem.entries['/'],
173 fileSystem.entries['/file'],
174 fileSystem.entries['/directory'],
181 * Tests the fileOperationUtil.deduplicatePath
182 * @param {function(boolean:hasError)} callback Callback to be passed true on
185 function testDeduplicatePath(callback) {
186 var fileSystem1 = createTestFileSystem('testVolume', {'/': DIRECTORY_SIZE});
187 var fileSystem2 = createTestFileSystem('testVolume', {
191 var fileSystem3 = createTestFileSystem('testVolume', {
205 var nonExistingPromise =
206 fileOperationUtil.deduplicatePath(fileSystem1.root, 'file.txt').
207 then(function(path) {
208 assertEquals('file.txt', path);
210 var existingPathPromise =
211 fileOperationUtil.deduplicatePath(fileSystem2.root, 'file.txt').
212 then(function(path) {
213 assertEquals('file (1).txt', path);
216 fileOperationUtil.deduplicatePath(fileSystem3.root, 'file.txt').
218 assertTrue(false, 'FileOperationManager.Error is not reported.');
220 assertTrue(error instanceof FileOperationManager.Error);
221 assertEquals(util.FileOperationErrorType.TARGET_EXISTS, error.code);
224 var testPromise = Promise.all([
229 reportPromise(testPromise, callback);
233 * Tests the fileOperationUtil.paste.
234 * @param {function(boolean:hasError)} callback Callback to be passed true on
237 function testCopy(callback) {
238 // Prepare entries and their resolver.
239 var fileSystem = createTestFileSystem('testVolume', {
243 window.webkitResolveLocalFileSystemURL =
244 resolveTestFileSystemURL.bind(null, fileSystem);
246 chrome.fileManagerPrivate.startCopy =
247 function(source, destination, newName, callback) {
248 var makeStatus = function(type) {
249 return {type: type, sourceUrl: source, destinationUrl: destination};
252 var listener = chrome.fileManagerPrivate.onCopyProgress.listener_;
253 listener(1, makeStatus('begin_copy_entry'));
254 listener(1, makeStatus('progress'));
255 var newPath = joinPath('/', newName);
256 fileSystem.entries[newPath] =
257 fileSystem.entries['/test.txt'].clone(newPath);
258 listener(1, makeStatus('end_copy_entry'));
259 listener(1, makeStatus('success'));
262 // Observing manager's events.
263 var eventsPromise = waitForEvents(fileOperationManager);
265 // Verify the events.
266 reportPromise(eventsPromise.then(function(events) {
267 var firstEvent = events[0];
268 assertEquals('BEGIN', firstEvent.reason);
269 assertEquals(1, firstEvent.status.numRemainingItems);
270 assertEquals(0, firstEvent.status.processedBytes);
271 assertEquals(1, firstEvent.status.totalBytes);
273 var lastEvent = events[events.length - 1];
274 assertEquals('SUCCESS', lastEvent.reason);
275 assertEquals(0, lastEvent.status.numRemainingItems);
276 assertEquals(10, lastEvent.status.processedBytes);
277 assertEquals(10, lastEvent.status.totalBytes);
279 assertTrue(events.some(function(event) {
280 return event.type === 'entries-changed' &&
281 event.kind === util.EntryChangedKind.CREATED &&
282 event.entries[0].fullPath === '/test (1).txt';
285 assertFalse(events.some(function(event) {
286 return event.type === 'delete';
290 fileOperationManager.paste(
291 [fileSystem.entries['/test.txt']],
292 fileSystem.entries['/'],
297 * Tests the fileOperationUtil.paste for move.
298 * @param {function(boolean:hasError)} callback Callback to be passed true on
301 function testMove(callback) {
302 // Prepare entries and their resolver.
303 var fileSystem = createTestFileSystem('testVolume', {
305 '/directory': DIRECTORY_SIZE,
308 window.webkitResolveLocalFileSystemURL =
309 resolveTestFileSystemURL.bind(null, fileSystem);
311 // Observing manager's events.
312 var eventsPromise = waitForEvents(fileOperationManager);
314 // Verify the events.
315 reportPromise(eventsPromise.then(function(events) {
316 var firstEvent = events[0];
317 assertEquals('BEGIN', firstEvent.reason);
318 assertEquals(1, firstEvent.status.numRemainingItems);
319 assertEquals(0, firstEvent.status.processedBytes);
320 assertEquals(1, firstEvent.status.totalBytes);
322 var lastEvent = events[events.length - 1];
323 assertEquals('SUCCESS', lastEvent.reason);
324 assertEquals(0, lastEvent.status.numRemainingItems);
325 assertEquals(1, lastEvent.status.processedBytes);
326 assertEquals(1, lastEvent.status.totalBytes);
328 assertTrue(events.some(function(event) {
329 return event.type === 'entries-changed' &&
330 event.kind === util.EntryChangedKind.DELETED &&
331 event.entries[0].fullPath === '/test.txt';
334 assertTrue(events.some(function(event) {
335 return event.type === 'entries-changed' &&
336 event.kind === util.EntryChangedKind.CREATED &&
337 event.entries[0].fullPath === '/directory/test.txt';
340 assertFalse(events.some(function(event) {
341 return event.type === 'delete';
345 fileOperationManager.paste(
346 [fileSystem.entries['/test.txt']],
347 fileSystem.entries['/directory'],
352 * Tests the fileOperationUtil.deleteEntries.
353 * @param {function(boolean:hasError)} callback Callback to be passed true on
356 function testDelete(callback) {
357 // Prepare entries and their resolver.
358 var fileSystem = createTestFileSystem('testVolume', {
362 window.webkitResolveLocalFileSystemURL =
363 resolveTestFileSystemURL.bind(null, fileSystem);
365 // Observing manager's events.
366 reportPromise(waitForEvents(fileOperationManager).then(function(events) {
367 assertEquals('delete', events[0].type);
368 assertEquals('BEGIN', events[0].reason);
369 assertEquals(10, events[0].totalBytes);
370 assertEquals(0, events[0].processedBytes);
372 var lastEvent = events[events.length - 1];
373 assertEquals('delete', lastEvent.type);
374 assertEquals('SUCCESS', lastEvent.reason);
375 assertEquals(10, lastEvent.totalBytes);
376 assertEquals(10, lastEvent.processedBytes);
378 assertFalse(events.some(function(event) {
379 return event.type === 'copy-progress';
383 fileOperationManager.deleteEntries([fileSystem.entries['/test.txt']]);
387 * Tests the fileOperationUtil.zipSelection.
388 * @param {function(boolean:hasError)} callback Callback to be passed true on
391 function testZip(callback) {
392 // Prepare entries and their resolver.
393 var fileSystem = createTestFileSystem('testVolume', {
397 window.webkitResolveLocalFileSystemURL =
398 resolveTestFileSystemURL.bind(null, fileSystem);
399 chrome.fileManagerPrivate.zipSelection =
400 function(parentURL, sources, newName, success, error) {
401 var newPath = joinPath('/', newName);
402 var newEntry = new MockFileEntry(fileSystem, newPath, {size: 10});
403 fileSystem.entries[newPath] = newEntry;
407 // Observing manager's events.
408 reportPromise(waitForEvents(fileOperationManager).then(function(events) {
409 assertEquals('copy-progress', events[0].type);
410 assertEquals('BEGIN', events[0].reason);
411 assertEquals(1, events[0].status.totalBytes);
412 assertEquals(0, events[0].status.processedBytes);
414 var lastEvent = events[events.length - 1];
415 assertEquals('copy-progress', lastEvent.type);
416 assertEquals('SUCCESS', lastEvent.reason);
417 assertEquals(10, lastEvent.status.totalBytes);
418 assertEquals(10, lastEvent.status.processedBytes);
420 assertFalse(events.some(function(event) {
421 return event.type === 'delete';
424 assertTrue(events.some(function(event) {
425 return event.type === 'entries-changed' &&
426 event.entries[0].fullPath === '/test.zip';
430 fileOperationManager.zipSelection(
431 fileSystem.entries['/'],
432 [fileSystem.entries['/test.txt']]);