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

Unified 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 side-by-side diff with in-line comments
Download patch
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..1af6e13c6aea4de35281b66ae67f02ae2b6cb8dd 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,72 @@
'use strict';
/**
+ * Mock of chrome.runtime.
+ * @type {Object}
+ * @const
+ */
+chrome.runtime = {
+ lastError: null
+};
+
+/**
+ * Mock of chrome.power.
+ * @type {Object}
+ * @const
+ */
+chrome.power = {
+ requestKeepAwake: function() {
+ 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.
+ },
+ releaseKeepAwake: function() {
+ this.keepAwakeRequested = false;
yoshiki 2014/08/07 09:35:29 ditto
hirono 2014/08/08 03:37:41 Done.
+ },
+ keepAwakeRequested: false
+};
+
+/**
+ * Mock of chrome.fileBrowserPrivate.
+ * @type {Object}
+ * @const
+ */
+chrome.fileBrowserPrivate = {
+ onCopyProgress: {
+ addListener: function(callback) {
+ this.listener_ = callback;
yoshiki 2014/08/07 09:35:29 ditto
hirono 2014/08/08 03:37:40 Done.
+ },
+ removeListener: function() {
+ this.listener_ = null;
yoshiki 2014/08/07 09:35:29 ditto
hirono 2014/08/08 03:37:41 Done.
+ },
+ 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) {
+ return Promise.resolve().then(function() {
+ this.onCopyProgress.listener_.apply(null, events[index]);
yoshiki 2014/08/07 09:35:29 ditto
hirono 2014/08/08 03:37:41 Done.
+ 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 +89,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 +191,58 @@ 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})];
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
+ var targetEntry = new MockDirectoryEntry('testVolume', '/', {});
+ window.webkitResolveLocalFileSystemURL = function(url, success, failure) {
+ if (url == sourceEntries[0].toURL())
yoshiki 2014/08/07 09:35:29 nit ===
hirono 2014/08/08 03:37:40 Done.
+ success(sourceEntries[0]);
+ else if (url == targetEntry.toURL())
yoshiki 2014/08/07 09:35:30 ditto
hirono 2014/08/08 03:37:41 Done.
+ success(targetEntry);
+ 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.
+ 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.
+ 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);
+}

Powered by Google App Engine
This is Rietveld 408576698