Index: chrome/test/data/file_manager/unit_tests/file_operation_manager_unittest.js |
diff --git a/chrome/test/data/file_manager/unit_tests/file_operation_manager_unittest.js b/chrome/test/data/file_manager/unit_tests/file_operation_manager_unittest.js |
index 260b8b82b1060b08f6cb38f4c302de3619f9e93c..e2fe9f0c3d0d4d5280d6ea5e526173e313c84440 100644 |
--- a/chrome/test/data/file_manager/unit_tests/file_operation_manager_unittest.js |
+++ b/chrome/test/data/file_manager/unit_tests/file_operation_manager_unittest.js |
@@ -4,6 +4,74 @@ |
'use strict'; |
/** |
+ * Mock of chrome.runtime. |
+ * @type {Object} |
+ * @const |
+ */ |
+chrome.runtime = { |
+ lastError: null |
+}; |
+ |
+/** |
+ * Mock of chrome.power. |
+ * @type {Object} |
+ * @const |
+ */ |
+chrome.power = { |
+ requestKeepAwake: function() { |
+ chrome.power.keepAwakeRequested = true; |
+ }, |
+ releaseKeepAwake: function() { |
+ chrome.power.keepAwakeRequested = false; |
+ }, |
+ keepAwakeRequested: false |
+}; |
+ |
+/** |
+ * Mock of chrome.fileBrowserPrivate. |
+ * @type {Object} |
+ * @const |
+ */ |
+chrome.fileBrowserPrivate = { |
+ onCopyProgress: { |
+ addListener: function(callback) { |
+ chrome.fileBrowserPrivate.onCopyProgress.listener_ = callback; |
+ }, |
+ removeListener: function() { |
+ chrome.fileBrowserPrivate.onCopyProgress.listener_ = null; |
+ }, |
+ listener_: null |
+ }, |
+ startCopy: function(source, destination, newName, callback) { |
+ var id = 1; |
+ var events = [ |
+ 'begin_copy_entry', |
+ 'progress', |
+ 'end_copy_entry', |
+ 'success' |
+ ].map(function(type) { |
+ return [id, {type: type, sourceUrl: source, destinationUrl: destination}]; |
+ }); |
+ var sendEvent = function(index) { |
+ // Call the function asynchronously. |
+ return Promise.resolve().then(function() { |
+ chrome.fileBrowserPrivate.onCopyProgress.listener_.apply( |
+ null, events[index]); |
+ if (index + 1 < events.length) |
+ return sendEvent(index + 1); |
+ else |
+ return null; |
+ }.bind(this)); |
+ }.bind(this); |
+ callback(id); |
+ sendEvent(0).catch(function(error) { |
+ console.log(error.stack || error); |
+ window.onerror(); |
+ }); |
+ } |
+}; |
+ |
+/** |
* Reports the result of promise to the test system. |
* @param {Promise} promise Promise to be fulfilled or rejected. |
* @param {function(boolean:hasError)} callback Callback to be passed true on |
@@ -23,6 +91,19 @@ function reportPromise(promise, callback) { |
} |
/** |
+ * Test target. |
+ * @type {FileOperationManager} |
+ */ |
+var fileOperationManager; |
+ |
+/** |
+ * Initializes the test environment. |
+ */ |
+function setUp() { |
+ fileOperationManager = new FileOperationManager(); |
+} |
+ |
+/** |
* Tests the fileOperationUtil.resolvePath function. |
* @param {function(boolean:hasError)} callback Callback to be passed true on |
* error. |
@@ -112,3 +193,56 @@ function testDeduplicatePath(callback) { |
]); |
reportPromise(testPromise, callback); |
} |
+ |
+/** |
+ * Tests the fileOperationUtil.paste. |
+ */ |
+function testCopy(callback) { |
+ // Prepare entries and their resolver. |
+ var sourceEntries = |
+ [new MockFileEntry('testVolume', '/test.txt', {size: 10})]; |
+ var targetEntry = new MockDirectoryEntry('testVolume', '/', {}); |
+ window.webkitResolveLocalFileSystemURL = function(url, success, failure) { |
+ if (url === sourceEntries[0].toURL()) |
+ success(sourceEntries[0]); |
+ else if (url === targetEntry.toURL()) |
+ success(targetEntry); |
+ else |
+ failure(); |
+ }; |
+ |
+ // Observing manager's events. |
+ var eventsPromise = new Promise(function(fulfill) { |
+ var events = []; |
+ fileOperationManager.addEventListener('copy-progress', function(event) { |
+ events.push(event); |
+ if (event.reason === 'SUCCESS') |
+ fulfill(events); |
+ }); |
+ fileOperationManager.addEventListener('entry-changed', function(event) { |
+ events.push(event); |
+ }); |
+ }); |
+ |
+ // Verify the events. |
+ reportPromise(eventsPromise.then(function(events) { |
+ var firstEvent = events[0]; |
+ assertEquals('BEGIN', firstEvent.reason); |
+ assertEquals(1, firstEvent.status.numRemainingItems); |
+ assertEquals(0, firstEvent.status.processedBytes); |
+ assertEquals(1, firstEvent.status.totalBytes); |
+ |
+ var lastEvent = events[events.length - 1]; |
+ assertEquals('SUCCESS', lastEvent.reason); |
+ assertEquals(0, lastEvent.status.numRemainingItems); |
+ assertEquals(10, lastEvent.status.processedBytes); |
+ assertEquals(10, lastEvent.status.totalBytes); |
+ |
+ assertTrue(events.some(function(event) { |
+ return event.type === 'entry-changed' && |
+ event.entry === targetEntry; |
+ })); |
+ }), callback); |
+ |
+ fileOperationManager.paste(sourceEntries, targetEntry, false); |
+} |