Chromium Code Reviews| 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 return Promise.resolve().then(function() { | |
|
yoshiki
2014/08/08 04:36:42
Why do you use Promise.resolve()? (maybe make it a
hirono
2014/08/08 04:56:19
Done.
| |
| 57 chrome.fileBrowserPrivate.onCopyProgress.listener_.apply( | |
| 58 null, events[index]); | |
| 59 if (index + 1 < events.length) | |
| 60 return sendEvent(index + 1); | |
| 61 else | |
| 62 return null; | |
| 63 }.bind(this)); | |
| 64 }.bind(this); | |
| 65 callback(id); | |
| 66 sendEvent(0).catch(function(error) { | |
| 67 console.log(error.stack || error); | |
| 68 window.onerror(); | |
| 69 }); | |
| 70 } | |
| 71 }; | |
| 72 | |
| 73 /** | |
| 7 * Reports the result of promise to the test system. | 74 * Reports the result of promise to the test system. |
| 8 * @param {Promise} promise Promise to be fulfilled or rejected. | 75 * @param {Promise} promise Promise to be fulfilled or rejected. |
| 9 * @param {function(boolean:hasError)} callback Callback to be passed true on | 76 * @param {function(boolean:hasError)} callback Callback to be passed true on |
| 10 * error. | 77 * error. |
| 11 */ | 78 */ |
| 12 function reportPromise(promise, callback) { | 79 function reportPromise(promise, callback) { |
| 13 promise.then( | 80 promise.then( |
| 14 callback.bind(null, false), | 81 callback.bind(null, false), |
| 15 function(error) { | 82 function(error) { |
| 16 if (error instanceof FileOperationManager.Error) { | 83 if (error instanceof FileOperationManager.Error) { |
| 17 console.log('FileOperationManager.Error: code=' + error.code); | 84 console.log('FileOperationManager.Error: code=' + error.code); |
| 18 } else { | 85 } else { |
| 19 console.log(error.stack || error.name || error); | 86 console.log(error.stack || error.name || error); |
| 20 } | 87 } |
| 21 callback(true); | 88 callback(true); |
| 22 }); | 89 }); |
| 23 } | 90 } |
| 24 | 91 |
| 25 /** | 92 /** |
| 93 * Test target. | |
| 94 * @type {FileOperationManager} | |
| 95 */ | |
| 96 var fileOperationManager; | |
| 97 | |
| 98 /** | |
| 99 * Initializes the test environment. | |
| 100 */ | |
| 101 function setUp() { | |
| 102 fileOperationManager = new FileOperationManager(); | |
| 103 } | |
| 104 | |
| 105 /** | |
| 26 * Tests the fileOperationUtil.resolvePath function. | 106 * Tests the fileOperationUtil.resolvePath function. |
| 27 * @param {function(boolean:hasError)} callback Callback to be passed true on | 107 * @param {function(boolean:hasError)} callback Callback to be passed true on |
| 28 * error. | 108 * error. |
| 29 */ | 109 */ |
| 30 function testResolvePath(callback) { | 110 function testResolvePath(callback) { |
| 31 var fileEntry = new MockFileEntry('testVolume', '/file', {}); | 111 var fileEntry = new MockFileEntry('testVolume', '/file', {}); |
| 32 var directoryEntry = new MockDirectoryEntry('testVolume', '/directory', {}); | 112 var directoryEntry = new MockDirectoryEntry('testVolume', '/directory', {}); |
| 33 var root = new MockDirectoryEntry('testVolume', '/', { | 113 var root = new MockDirectoryEntry('testVolume', '/', { |
| 34 '/file': fileEntry, | 114 '/file': fileEntry, |
| 35 '/directory': directoryEntry | 115 '/directory': directoryEntry |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 assertEquals(util.FileOperationErrorType.TARGET_EXISTS, error.code); | 185 assertEquals(util.FileOperationErrorType.TARGET_EXISTS, error.code); |
| 106 }); | 186 }); |
| 107 | 187 |
| 108 var testPromise = Promise.all([ | 188 var testPromise = Promise.all([ |
| 109 nonExistingPromise, | 189 nonExistingPromise, |
| 110 existingPathPromise, | 190 existingPathPromise, |
| 111 failedPromise | 191 failedPromise |
| 112 ]); | 192 ]); |
| 113 reportPromise(testPromise, callback); | 193 reportPromise(testPromise, callback); |
| 114 } | 194 } |
| 195 | |
| 196 /** | |
| 197 * Tests the fileOperationUtil.paste. | |
| 198 */ | |
| 199 function testCopy(callback) { | |
| 200 // Prepare entries and their resolver. | |
| 201 var sourceEntries = | |
| 202 [new MockFileEntry('testVolume', '/test.txt', {size: 10})]; | |
| 203 var targetEntry = new MockDirectoryEntry('testVolume', '/', {}); | |
| 204 window.webkitResolveLocalFileSystemURL = function(url, success, failure) { | |
| 205 if (url === sourceEntries[0].toURL()) | |
| 206 success(sourceEntries[0]); | |
| 207 else if (url === targetEntry.toURL()) | |
| 208 success(targetEntry); | |
| 209 else | |
| 210 failure(); | |
| 211 }; | |
| 212 | |
| 213 // Observing manager's events. | |
| 214 var eventsPromise = new Promise(function(fulfill) { | |
| 215 var events = []; | |
| 216 fileOperationManager.addEventListener('copy-progress', function(event) { | |
| 217 events.push(event); | |
| 218 if (event.reason == 'SUCCESS') | |
|
yoshiki
2014/08/08 04:36:42
nit: ===
hirono
2014/08/08 04:56:19
Done.
| |
| 219 fulfill(events); | |
| 220 }); | |
| 221 fileOperationManager.addEventListener('entry-changed', function(event) { | |
| 222 events.push(event); | |
| 223 }); | |
| 224 }); | |
| 225 | |
| 226 // Verify the events. | |
| 227 reportPromise(eventsPromise.then(function(events) { | |
| 228 var firstEvent = events[0]; | |
| 229 assertEquals('BEGIN', firstEvent.reason); | |
| 230 assertEquals(1, firstEvent.status.numRemainingItems); | |
| 231 assertEquals(0, firstEvent.status.processedBytes); | |
| 232 assertEquals(1, firstEvent.status.totalBytes); | |
| 233 | |
| 234 var lastEvent = events[events.length - 1]; | |
| 235 assertEquals('SUCCESS', lastEvent.reason); | |
| 236 assertEquals(0, lastEvent.status.numRemainingItems); | |
| 237 assertEquals(10, lastEvent.status.processedBytes); | |
| 238 assertEquals(10, lastEvent.status.totalBytes); | |
| 239 | |
| 240 assertTrue(events.some(function(event) { | |
| 241 return event.type === 'entry-changed' && | |
| 242 event.entry === targetEntry; | |
| 243 })); | |
| 244 }), callback); | |
| 245 | |
| 246 fileOperationManager.paste(sourceEntries, targetEntry, false); | |
| 247 } | |
| OLD | NEW |