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

Unified Diff: ui/file_manager/file_manager/background/js/mock_media_scanner.js

Issue 792233009: Reflect scanning status in command UI. Only allow import once scanning is complete. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix FM.jstests.cc && Finalize scan results in MediaImportHandlerTest...unbreaking the test. Created 6 years 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: ui/file_manager/file_manager/background/js/mock_media_scanner.js
diff --git a/ui/file_manager/file_manager/background/js/mock_media_scanner.js b/ui/file_manager/file_manager/background/js/mock_media_scanner.js
index 8f34a64dd13682c45094f91e4c147671b69e832c..386599591047cbf2f8cb3c4e432f90110c58bb57 100644
--- a/ui/file_manager/file_manager/background/js/mock_media_scanner.js
+++ b/ui/file_manager/file_manager/background/js/mock_media_scanner.js
@@ -8,9 +8,12 @@
* @constructor
* @struct
* @implements {importer.MediaScanner}
- * @implements {importer.ScanResult}
*/
function TestMediaScanner() {
+
+ /** @private {!importer.ScanResult} */
+ this.scans_ = [];
+
/**
* List of file entries found while scanning.
* @type {!Array.<!FileEntry>}
@@ -23,48 +26,131 @@ function TestMediaScanner() {
/** @type {number} */
this.scanDuration = 100;
- /** @type {!Promise.<!importer.ScanResult>} */
- this.whenFinished;
+ /** @private {!Array.<!importer.ScanObserver>} */
+ this.observers = [];
}
/** @override */
+TestMediaScanner.prototype.addObserver = function(observer) {
+ this.observers.push(observer);
+};
+
+/** @override */
+TestMediaScanner.prototype.removeObserver = function(observer) {
+ var index = this.observers.indexOf(observer);
+ if (index > -1) {
+ this.observers.splice(index, 1);
+ } else {
+ console.warn('Ignoring request to remove observer that is not registered.');
+ }
+};
+
+/** @override */
TestMediaScanner.prototype.scan = function(entries) {
- var result = new TestScanResult(this);
- this.whenFinished = Promise.resolve(result);
+ var result = new TestScanResult(this.fileEntries);
+ result.totalBytes = this.totalBytes;
+ result.scanDuration = this.scanDuration;
+ this.scans_.push(result);
return result;
};
/**
+ * Finalizes all previously started scans.
+ */
+TestMediaScanner.prototype.finalizeScans = function() {
+ this.scans_.forEach(this.finalize.bind(this));
+};
+
+/**
+ * Notifies observers that a scan has finished.
+ * @param {!importer.ScanResult} result
+ */
+TestMediaScanner.prototype.finalize = function(result) {
+ result.finalize();
+ this.observers.forEach(
+ function(observer) {
+ observer(importer.ScanEvent.FINALIZED, result);
+ });
+};
+
+/**
+ * @param {number} expected
+ */
+TestMediaScanner.prototype.assertScanCount = function(expected) {
+ assertEquals(expected, this.scans_.length);
+};
+
+/**
* importer.MediaScanner and importer.ScanResult test double.
*
* @constructor
* @struct
- * @implements {importer.MediaScanner}
* @implements {importer.ScanResult}
*
- * @param {!TestMediaScanner} scanner
+ * @param {!Array.<!FileEntry>} fileEntries
*/
-function TestScanResult(scanner) {
- /** @private {!TestMediaScanner} */
- this.scanner_ = scanner;
+function TestScanResult(fileEntries) {
+ /**
+ * List of file entries found while scanning.
+ * @type {!Array.<!FileEntry>}
+ */
+ this.fileEntries = fileEntries.slice();
+
+ /** @type {number} */
+ this.totalBytes = 100;
+
+ /** @type {number} */
+ this.scanDuration = 100;
+
+ /** @type {function} */
+ this.resolveResult_;
+
+ /** @type {function} */
+ this.rejectResult_;
+
+ /** @type {boolean} */
+ this.settled_ = false;
+
+ /** @type {!Promise.<!importer.ScanResult>} */
+ this.whenFinal_ = new Promise(
+ function(resolve, reject) {
+ this.resolveResult_ = function(result) {
+ this.settled_ = true;
+ resolve(result);
+ }.bind(this);
+ this.rejectResult_ = function() {
+ this.settled_ = true;
+ reject();
+ }.bind(this);
+ }.bind(this));
}
/** @override */
TestScanResult.prototype.getFileEntries = function() {
- return this.scanner_.fileEntries;
+ return this.fileEntries;
};
/** @override */
TestScanResult.prototype.getTotalBytes = function() {
- return this.scanner_.totalBytes;
+ return this.totalBytes;
};
/** @override */
TestScanResult.prototype.getScanDurationMs = function() {
- return this.scanner_.scanDuration;
+ return this.scanDuration;
+};
+
+/** @override */
+TestScanResult.prototype.finalize = function() {
+ return this.resolveResult_(this);
+};
+
+/** @override */
+TestScanResult.prototype.whenFinal = function() {
+ return this.whenFinal_;
};
/** @override */
-TestScanResult.prototype.whenFinished = function() {
- return this.scanner_.whenFinished;
+TestScanResult.prototype.isFinal = function() {
+ return this.settled_;
};

Powered by Google App Engine
This is Rietveld 408576698