OLD | NEW |
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 chrome.power.keepAwakeRequested = true; |
| 23 }, |
| 24 releaseKeepAwake: function() { |
| 25 chrome.power.keepAwakeRequested = false; |
| 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 chrome.fileBrowserPrivate.onCopyProgress.listener_ = callback; |
| 39 }, |
| 40 removeListener: function() { |
| 41 chrome.fileBrowserPrivate.onCopyProgress.listener_ = null; |
| 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 // Call the function asynchronously. |
| 57 return Promise.resolve().then(function() { |
| 58 chrome.fileBrowserPrivate.onCopyProgress.listener_.apply( |
| 59 null, events[index]); |
| 60 if (index + 1 < events.length) |
| 61 return sendEvent(index + 1); |
| 62 else |
| 63 return null; |
| 64 }.bind(this)); |
| 65 }.bind(this); |
| 66 callback(id); |
| 67 sendEvent(0).catch(function(error) { |
| 68 console.log(error.stack || error); |
| 69 window.onerror(); |
| 70 }); |
| 71 } |
| 72 }; |
| 73 |
| 74 /** |
7 * Reports the result of promise to the test system. | 75 * Reports the result of promise to the test system. |
8 * @param {Promise} promise Promise to be fulfilled or rejected. | 76 * @param {Promise} promise Promise to be fulfilled or rejected. |
9 * @param {function(boolean:hasError)} callback Callback to be passed true on | 77 * @param {function(boolean:hasError)} callback Callback to be passed true on |
10 * error. | 78 * error. |
11 */ | 79 */ |
12 function reportPromise(promise, callback) { | 80 function reportPromise(promise, callback) { |
13 promise.then( | 81 promise.then( |
14 callback.bind(null, false), | 82 callback.bind(null, false), |
15 function(error) { | 83 function(error) { |
16 if (error instanceof FileOperationManager.Error) { | 84 if (error instanceof FileOperationManager.Error) { |
17 console.log('FileOperationManager.Error: code=' + error.code); | 85 console.log('FileOperationManager.Error: code=' + error.code); |
18 } else { | 86 } else { |
19 console.log(error.stack || error.name || error); | 87 console.log(error.stack || error.name || error); |
20 } | 88 } |
21 callback(true); | 89 callback(true); |
22 }); | 90 }); |
23 } | 91 } |
24 | 92 |
25 /** | 93 /** |
| 94 * Test target. |
| 95 * @type {FileOperationManager} |
| 96 */ |
| 97 var fileOperationManager; |
| 98 |
| 99 /** |
| 100 * Initializes the test environment. |
| 101 */ |
| 102 function setUp() { |
| 103 fileOperationManager = new FileOperationManager(); |
| 104 } |
| 105 |
| 106 /** |
26 * Tests the fileOperationUtil.resolvePath function. | 107 * Tests the fileOperationUtil.resolvePath function. |
27 * @param {function(boolean:hasError)} callback Callback to be passed true on | 108 * @param {function(boolean:hasError)} callback Callback to be passed true on |
28 * error. | 109 * error. |
29 */ | 110 */ |
30 function testResolvePath(callback) { | 111 function testResolvePath(callback) { |
31 var fileEntry = new MockFileEntry('testVolume', '/file', {}); | 112 var fileEntry = new MockFileEntry('testVolume', '/file', {}); |
32 var directoryEntry = new MockDirectoryEntry('testVolume', '/directory', {}); | 113 var directoryEntry = new MockDirectoryEntry('testVolume', '/directory', {}); |
33 var root = new MockDirectoryEntry('testVolume', '/', { | 114 var root = new MockDirectoryEntry('testVolume', '/', { |
34 '/file': fileEntry, | 115 '/file': fileEntry, |
35 '/directory': directoryEntry | 116 '/directory': directoryEntry |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 assertEquals(util.FileOperationErrorType.TARGET_EXISTS, error.code); | 186 assertEquals(util.FileOperationErrorType.TARGET_EXISTS, error.code); |
106 }); | 187 }); |
107 | 188 |
108 var testPromise = Promise.all([ | 189 var testPromise = Promise.all([ |
109 nonExistingPromise, | 190 nonExistingPromise, |
110 existingPathPromise, | 191 existingPathPromise, |
111 failedPromise | 192 failedPromise |
112 ]); | 193 ]); |
113 reportPromise(testPromise, callback); | 194 reportPromise(testPromise, callback); |
114 } | 195 } |
| 196 |
| 197 /** |
| 198 * Tests the fileOperationUtil.paste. |
| 199 */ |
| 200 function testCopy(callback) { |
| 201 // Prepare entries and their resolver. |
| 202 var sourceEntries = |
| 203 [new MockFileEntry('testVolume', '/test.txt', {size: 10})]; |
| 204 var targetEntry = new MockDirectoryEntry('testVolume', '/', {}); |
| 205 window.webkitResolveLocalFileSystemURL = function(url, success, failure) { |
| 206 if (url === sourceEntries[0].toURL()) |
| 207 success(sourceEntries[0]); |
| 208 else if (url === targetEntry.toURL()) |
| 209 success(targetEntry); |
| 210 else |
| 211 failure(); |
| 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 } |
OLD | NEW |