Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(438)

Side by Side Diff: chrome/test/data/file_manager/unit_tests/file_operation_manager_unittest.js

Issue 442393003: Files.app: Add units test of FileOperationManager#paste. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 'use strict'; 4 'use strict';
5 5
6 /** 6 /**
7 * Mock of chrome.runtime.
8 * @type {Object}
9 * @const
10 */
11 chrome.runtime = {
12 lastError: null
13 };
14
15 /**
16 * Mock of chrome.power.
17 * @type {Object}
18 * @const
19 */
20 chrome.power = {
21 requestKeepAwake: function() {
22 this.keepAwakeRequested = true;
yoshiki 2014/08/07 09:35:29 Don't use 'this' here, since 'this' is not an inst
hirono 2014/08/08 03:37:41 Done.
23 },
24 releaseKeepAwake: function() {
25 this.keepAwakeRequested = false;
yoshiki 2014/08/07 09:35:29 ditto
hirono 2014/08/08 03:37:41 Done.
26 },
27 keepAwakeRequested: false
28 };
29
30 /**
31 * Mock of chrome.fileBrowserPrivate.
32 * @type {Object}
33 * @const
34 */
35 chrome.fileBrowserPrivate = {
36 onCopyProgress: {
37 addListener: function(callback) {
38 this.listener_ = callback;
yoshiki 2014/08/07 09:35:29 ditto
hirono 2014/08/08 03:37:40 Done.
39 },
40 removeListener: function() {
41 this.listener_ = null;
yoshiki 2014/08/07 09:35:29 ditto
hirono 2014/08/08 03:37:41 Done.
42 },
43 listener_: null
44 },
45 startCopy: function(source, destination, newName, callback) {
46 var id = 1;
47 var events = [
48 'begin_copy_entry',
49 'progress',
50 'end_copy_entry',
51 'success'
52 ].map(function(type) {
53 return [id, {type: type, sourceUrl: source, destinationUrl: destination}];
54 });
55 var sendEvent = function(index) {
56 return Promise.resolve().then(function() {
57 this.onCopyProgress.listener_.apply(null, events[index]);
yoshiki 2014/08/07 09:35:29 ditto
hirono 2014/08/08 03:37:41 Done.
58 if (index + 1 < events.length)
59 return sendEvent(index + 1);
60 else
61 return null;
62 }.bind(this));
63 }.bind(this);
64 callback(id);
65 sendEvent(0).catch(function(error) {
66 console.log(error.stack || error);
67 window.onerror();
68 });
69 }
70 };
71
72 /**
7 * Reports the result of promise to the test system. 73 * Reports the result of promise to the test system.
8 * @param {Promise} promise Promise to be fulfilled or rejected. 74 * @param {Promise} promise Promise to be fulfilled or rejected.
9 * @param {function(boolean:hasError)} callback Callback to be passed true on 75 * @param {function(boolean:hasError)} callback Callback to be passed true on
10 * error. 76 * error.
11 */ 77 */
12 function reportPromise(promise, callback) { 78 function reportPromise(promise, callback) {
13 promise.then( 79 promise.then(
14 callback.bind(null, false), 80 callback.bind(null, false),
15 function(error) { 81 function(error) {
16 if (error instanceof FileOperationManager.Error) { 82 if (error instanceof FileOperationManager.Error) {
17 console.log('FileOperationManager.Error: code=' + error.code); 83 console.log('FileOperationManager.Error: code=' + error.code);
18 } else { 84 } else {
19 console.log(error.stack || error.name || error); 85 console.log(error.stack || error.name || error);
20 } 86 }
21 callback(true); 87 callback(true);
22 }); 88 });
23 } 89 }
24 90
25 /** 91 /**
92 * Test target.
93 * @type {FileOperationManager}
94 */
95 var fileOperationManager;
96
97 /**
98 * Initializes the test environment.
99 */
100 function setUp() {
101 fileOperationManager = new FileOperationManager();
102 }
103
104 /**
26 * Tests the fileOperationUtil.resolvePath function. 105 * Tests the fileOperationUtil.resolvePath function.
27 * @param {function(boolean:hasError)} callback Callback to be passed true on 106 * @param {function(boolean:hasError)} callback Callback to be passed true on
28 * error. 107 * error.
29 */ 108 */
30 function testResolvePath(callback) { 109 function testResolvePath(callback) {
31 var fileEntry = new MockFileEntry('testVolume', '/file', {}); 110 var fileEntry = new MockFileEntry('testVolume', '/file', {});
32 var directoryEntry = new MockDirectoryEntry('testVolume', '/directory', {}); 111 var directoryEntry = new MockDirectoryEntry('testVolume', '/directory', {});
33 var root = new MockDirectoryEntry('testVolume', '/', { 112 var root = new MockDirectoryEntry('testVolume', '/', {
34 '/file': fileEntry, 113 '/file': fileEntry,
35 '/directory': directoryEntry 114 '/directory': directoryEntry
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 assertEquals(util.FileOperationErrorType.TARGET_EXISTS, error.code); 184 assertEquals(util.FileOperationErrorType.TARGET_EXISTS, error.code);
106 }); 185 });
107 186
108 var testPromise = Promise.all([ 187 var testPromise = Promise.all([
109 nonExistingPromise, 188 nonExistingPromise,
110 existingPathPromise, 189 existingPathPromise,
111 failedPromise 190 failedPromise
112 ]); 191 ]);
113 reportPromise(testPromise, callback); 192 reportPromise(testPromise, callback);
114 } 193 }
194
195 /**
196 * Tests the fileOperationUtil.paste.
197 */
198 function testCopy(callback) {
199 // Prepare entries and their resolver.
200 var sourceEntries =
201 [new MockFileEntry('testVolume', '/test.txt', {size: 10})];
yoshiki 2014/08/07 09:35:30 nit: 4 space indent
hirono 2014/08/08 03:37:40 I counted 4 spaces from the head of 'var' in the p
202 var targetEntry = new MockDirectoryEntry('testVolume', '/', {});
203 window.webkitResolveLocalFileSystemURL = function(url, success, failure) {
204 if (url == sourceEntries[0].toURL())
yoshiki 2014/08/07 09:35:29 nit ===
hirono 2014/08/08 03:37:40 Done.
205 success(sourceEntries[0]);
206 else if (url == targetEntry.toURL())
yoshiki 2014/08/07 09:35:30 ditto
hirono 2014/08/08 03:37:41 Done.
207 success(targetEntry);
208 else {
yoshiki 2014/08/07 09:35:29 If you uses curly brackets here, please use curly
hirono 2014/08/08 03:37:40 Done.
209 console.log('Cannot find ' + url + '.');
yoshiki 2014/08/07 09:35:29 Please use 'console.error' if it's an error.
hirono 2014/08/08 03:37:41 Let me remove the line. This is the debug log.
210 failure();
211 }
212 };
213
214 // Observing manager's events.
215 var eventsPromise = new Promise(function(fulfill) {
216 var events = [];
217 fileOperationManager.addEventListener('copy-progress', function(event) {
218 events.push(event);
219 if (event.reason == 'SUCCESS')
220 fulfill(events);
221 });
222 fileOperationManager.addEventListener('entry-changed', function(event) {
223 events.push(event);
224 });
225 });
226
227 // Verify the events.
228 reportPromise(eventsPromise.then(function(events) {
229 var firstEvent = events[0];
230 assertEquals('BEGIN', firstEvent.reason);
231 assertEquals(1, firstEvent.status.numRemainingItems);
232 assertEquals(0, firstEvent.status.processedBytes);
233 assertEquals(1, firstEvent.status.totalBytes);
234
235 var lastEvent = events[events.length - 1];
236 assertEquals('SUCCESS', lastEvent.reason);
237 assertEquals(0, lastEvent.status.numRemainingItems);
238 assertEquals(10, lastEvent.status.processedBytes);
239 assertEquals(10, lastEvent.status.totalBytes);
240
241 assertTrue(events.some(function(event) {
242 return event.type === 'entry-changed' &&
243 event.entry === targetEntry;
244 }));
245 }), callback);
246
247 fileOperationManager.paste(sourceEntries, targetEntry, false);
248 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698