Commit 604f2464 authored by Noel Gordon's avatar Noel Gordon Committed by Commit Bot

Closure compile media_scanner_unittest

 - remove media_scanner_unittest.html, update build rules to closure
   compile this unittest, auto-generate this unittest file.
 - fix closure type errors (the test mocks and unittest code diverged
   from the real world over time, sadly), fix or add commentary.
 - import*ScanResults => import*ScanResult, the former is undefined.
 - scanDirectory|File require a |mode| argument per the externs, so
   add it everywhere.
 - re-order the test media scan and scan result class definitions to
   follow the definition order of their extern file.
 - run the js formatter over the code.

Bug: 905931
Change-Id: I27239333e2ab2d8bc1c50c6bcade75cc2d6b6701
Reviewed-on: https://chromium-review.googlesource.com/c/1348874Reviewed-by: default avatarStuart Langley <slangley@chromium.org>
Commit-Queue: Noel Gordon <noel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610506}
parent 7dd3be88
......@@ -71,8 +71,7 @@ IN_PROC_BROWSER_TEST_F(FileManagerJsTest, FileTableTest) {
}
IN_PROC_BROWSER_TEST_F(FileManagerJsTest, MediaScannerTest) {
RunTest(base::FilePath(
FILE_PATH_LITERAL("background/js/media_scanner_unittest.html")));
RunGeneratedTest("/background/js/media_scanner_unittest.html");
}
IN_PROC_BROWSER_TEST_F(FileManagerJsTest, LRUCacheTest) {
......
......@@ -197,6 +197,8 @@ js_library("file_operation_util") {
"../../common/js:async_util",
"../../common/js:util",
]
externs_list =
[ "//ui/file_manager/externs/file_operation_progress_event.js" ]
}
js_library("metadata_proxy") {
......@@ -250,11 +252,28 @@ js_library("media_import_handler") {
]
}
js_library("mock_media_scanner") {
testonly = true
deps = [
":test_import_history",
]
externs_list = [ "../../../externs/background/media_scanner.js" ]
}
js_library("media_scanner") {
deps = [
":file_operation_util",
"../../common/js:importer_common",
]
externs_list = [ "../../../externs/background/media_scanner.js" ]
}
js_unittest("media_scanner_unittest") {
deps = [
":media_scanner",
":mock_media_scanner",
"//ui/file_manager/file_manager/common/js:test_importer_common",
]
}
js_library("mock_volume_manager") {
......@@ -287,6 +306,13 @@ js_library("task_queue") {
js_library("test_util_base") {
}
js_library("test_import_history") {
testonly = true
deps = [
":import_history",
]
}
js_library("runtime_loaded_test_util") {
# TODO(tapted): Move this target to //ui/file_manager/base. It is used in the
# background page of all |related_apps|, but loaded at runtime by
......@@ -359,6 +385,7 @@ js_unit_tests("unit_tests") {
deps = [
":crostini_unittest",
":import_history_unittest",
":media_scanner_unittest",
":volume_manager_unittest",
]
}
<!DOCTYPE html>
<!-- Copyright 2014 The Chromium Authors. All rights reserved.
-- Use of this source code is governed by a BSD-style license that can be
-- found in the LICENSE file.
-->
<html>
<body>
<script src="../../../../../ui/webui/resources/js/cr.js"></script>
<script src="../../../../../ui/webui/resources/js/cr/event_target.js"></script>
<script src="../../../../../ui/webui/resources/js/load_time_data.js"></script>
<script src="../../../base/js/volume_manager_types.js"></script>
<script src="../../common/js/file_type.js"></script>
<script src="../../common/js/importer_common.js"></script>
<script src="../../common/js/lru_cache.js"></script>
<script src="../../common/js/mock_entry.js"></script>
<script src="../../common/js/mock_file_system.js"></script>
<script src="../../../base/js/test_error_reporting.js"></script>
<script src="../../common/js/unittest_util.js"></script>
<script src="../../common/js/util.js"></script>
<script src="metadata_proxy.js"></script>
<script src="file_operation_util.js"></script>
<script src="import_history.js"></script>
<script src="media_scanner.js"></script>
<script src="mock_media_scanner.js"></script>
<script src="test_import_history.js"></script>
<script src="media_scanner_unittest.js"></script>
</body>
</html>
......@@ -14,10 +14,15 @@ var metrics = {
/** @type {!importer.DefaultMediaScanner} */
var scanner;
/**
* @const {importer.ScanMode}
*/
var scanMode = importer.ScanMode.HISTORY;
/** @type {!importer.TestImportHistory} */
var importHistory;
/** @type {!importer.TestDirectoryWatcher} */
/** @type {!TestDirectoryWatcher} */
var watcher;
/**
......@@ -28,12 +33,10 @@ var dispositionChecker;
// Set up the test components.
function setUp() {
importHistory = new importer.TestImportHistory();
// This is the default disposition checker.
// Tests can replace this at runtime if they
// want specialized behaviors.
// Setup a default disposition checker. Tests can replace it at runtime
// if they need specialized disposition check behavior.
dispositionChecker = function() {
return Promise.resolve(importer.Disposition.ORIGINAL);
};
......@@ -56,9 +59,8 @@ function setUp() {
* Verifies that scanning an empty filesystem produces an empty list.
*/
function testEmptySourceList() {
assertThrows(
function() {
scanner.scanFiles([]);
assertThrows(function() {
scanner.scanFiles([], scanMode);
});
}
......@@ -76,7 +78,7 @@ function testIsScanning(callback) {
* @param {!DirectoryEntry} root
*/
function(root) {
var results = scanner.scanDirectory(root);
var results = scanner.scanDirectory(root, scanMode);
assertFalse(results.isFinal());
}),
callback);
......@@ -98,8 +100,8 @@ function testObserverNotifiedOnScanFinish(callback) {
// Kick off a scan so we can get notified of a scan being finished.
// We kick this off first so we can capture the result for
// use in an assert. Promises ensure the scan won't finish
// until after our funciton is fully processed.
var result = scanner.scanDirectory(root);
// until after our function is fully processed.
var result = scanner.scanDirectory(root, scanMode);
scanner.addObserver(
function(eventType, scanResult) {
assertEquals(importer.ScanEvent.FINALIZED, eventType);
......@@ -107,8 +109,7 @@ function testObserverNotifiedOnScanFinish(callback) {
callback(false);
});
})
.catch(
function() {
.catch(function() {
callback(true);
});
}
......@@ -135,7 +136,7 @@ function testScanFiles(callback) {
.then(
/** @param {!Array<!FileEntry>} files */
function(files) {
return scanner.scanFiles(files).whenFinal();
return scanner.scanFiles(files, scanMode).whenFinal();
})
.then(assertFilesFound.bind(null, expectedFiles)),
callback);
......@@ -177,7 +178,7 @@ function testScanFilesIgnoresPreviousImports(callback) {
.then(
/** @param {!Array<!FileEntry>} files */
function(files) {
return scanner.scanFiles(files).whenFinal();
return scanner.scanFiles(files, scanMode).whenFinal();
})
.then(assertFilesFound.bind(null, expectedFiles)),
callback);
......@@ -200,7 +201,7 @@ function testEmptyScanResults(callback) {
* @param {!DirectoryEntry} root
*/
function(root) {
return scanner.scanDirectory(root).whenFinal();
return scanner.scanDirectory(root, scanMode).whenFinal();
})
.then(assertFilesFound.bind(null, [])),
callback);
......@@ -232,7 +233,7 @@ function testSingleLevel(callback) {
* @param {!DirectoryEntry} root
*/
function(root) {
return scanner.scanDirectory(root).whenFinal();
return scanner.scanDirectory(root, scanMode).whenFinal();
})
.then(assertFilesFound.bind(null, expectedFiles)),
callback);
......@@ -265,7 +266,7 @@ function testProgress(callback) {
* @param {!DirectoryEntry} root
*/
function(root) {
return scanner.scanDirectory(root).whenFinal();
return scanner.scanDirectory(root, scanMode).whenFinal();
})
.then(assertProgress.bind(null, 100)),
callback);
......@@ -304,7 +305,8 @@ function testIgnoresPreviousImports(callback) {
'/testIgnoresPreviousImports/baz.avi'
];
var promise = makeTestFileSystemRoot('testIgnoresPreviousImports')
var promise =
makeTestFileSystemRoot('testIgnoresPreviousImports')
.then(populateDir.bind(null, filenames))
.then(
/**
......@@ -312,7 +314,7 @@ function testIgnoresPreviousImports(callback) {
* @param {!DirectoryEntry} root
*/
function(root) {
return scanner.scanDirectory(root).whenFinal();
return scanner.scanDirectory(root, scanMode).whenFinal();
})
.then(assertFilesFound.bind(null, expectedFiles));
......@@ -351,8 +353,8 @@ function testTracksDuplicates(callback) {
'/testTracksDuplicates/driveimage9999.jpg'
];
var promise = makeTestFileSystemRoot('testTracksDuplicates')
var promise =
makeTestFileSystemRoot('testTracksDuplicates')
.then(populateDir.bind(null, filenames))
.then(
/**
......@@ -360,7 +362,7 @@ function testTracksDuplicates(callback) {
* @param {!DirectoryEntry} root
*/
function(root) {
return scanner.scanDirectory(root).whenFinal();
return scanner.scanDirectory(root, scanMode).whenFinal();
})
.then(assertDuplicatesFound.bind(null, expectedDuplicates));
......@@ -400,7 +402,7 @@ function testMultiLevel(callback) {
* @param {!DirectoryEntry} root
*/
function(root) {
return scanner.scanDirectory(root).whenFinal();
return scanner.scanDirectory(root, scanMode).whenFinal();
})
.then(assertFilesFound.bind(null, expectedFiles)),
callback);
......@@ -440,7 +442,7 @@ function testDedupesFilesInScanResult(callback) {
* @param {!DirectoryEntry} root
*/
function(root) {
return scanner.scanDirectory(root).whenFinal();
return scanner.scanDirectory(root, scanMode).whenFinal();
})
.then(assertFilesFound.bind(null, expectedFiles)),
callback);
......@@ -453,7 +455,7 @@ function testDefaultScanResult() {
var hashGenerator = function(file) {
return file.toURL();
};
var scan = new importer.DefaultScanResult(hashGenerator);
var scan = new importer.DefaultScanResult(scanMode, hashGenerator);
// 0 before we set candidate count
assertProgress(0, scan);
......@@ -482,7 +484,7 @@ function testInvalidation(callback) {
* @param {!DirectoryEntry} root
*/
function(root) {
scan = scanner.scanDirectory(root);
scanner.scanDirectory(root, scanMode);
watcher.callback();
return invalidatePromise;
}),
......@@ -492,8 +494,8 @@ function testInvalidation(callback) {
/**
* Verifies the results of the media scan are as expected.
* @param {number} expected, 0-100
* @param {!importer.ScanResults} scan
* @return {!importer.ScanResults}
* @param {!importer.ScanResult} scan
* @return {!importer.ScanResult}
*/
function assertProgress(expected, scan) {
assertEquals(expected, scan.getStatistics().progress);
......@@ -503,8 +505,8 @@ function assertProgress(expected, scan) {
/**
* Verifies the results of the media scan are as expected.
* @param {!Array<string>} expected
* @param {!importer.ScanResults} scan
* @return {!importer.ScanResults}
* @param {!importer.ScanResult} scan
* @return {!importer.ScanResult}
*/
function assertFilesFound(expected, scan) {
assertFileEntryPathsEqual(expected, scan.getFileEntries());
......@@ -515,8 +517,8 @@ function assertFilesFound(expected, scan) {
/**
* Verifies the results of the media scan are as expected.
* @param {!Array<string>} expected
* @param {!importer.ScanResults} scan
* @return {!importer.ScanResults}
* @param {!importer.ScanResult} scan
* @return {!importer.ScanResult}
*/
function assertDuplicatesFound(expected, scan) {
assertFileEntryPathsEqual(expected, scan.getDuplicateFileEntries());
......@@ -580,9 +582,9 @@ function populateDir(filenames, dir) {
})
.then(populateDir.bind(null, filename));
} else {
return new Promise(
function(resolve, reject) {
dir.getFile(filename, {create: true}, resolve, reject);
var name = /** @type {string} */ (filename);
return new Promise(function(resolve, reject) {
dir.getFile(name, {create: true}, resolve, reject);
});
}
})).then(
......
......@@ -10,8 +10,7 @@
* @implements {importer.MediaScanner}
*/
function TestMediaScanner() {
/** @private {!importer.ScanResult} */
/** @private {!Array<!importer.ScanResult>} */
this.scans_ = [];
/**
......@@ -21,38 +20,26 @@ function TestMediaScanner() {
this.fileEntries = [];
/**
* List of file entries found while scanning.
* List of duplicate file entries found while scanning.
* @type {!Array<!FileEntry>}
*/
this.duplicateFileEntries = [];
/** @type {number} */
/**
* List of scan observers.
* @private {!Array<!importer.ScanObserver>}
*/
this.observers = [];
/** @private {number} */
this.totalBytes = 100;
/** @type {number} */
/** @private {number} */
this.scanDuration = 100;
/** @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.scanDirectory = function(directory) {
TestMediaScanner.prototype.scanDirectory = function(directory, mode) {
var scan = new TestScanResult(this.fileEntries);
scan.totalBytes = this.totalBytes;
scan.scanDuration = this.scanDuration;
......@@ -61,7 +48,7 @@ TestMediaScanner.prototype.scanDirectory = function(directory) {
};
/** @override */
TestMediaScanner.prototype.scanFiles = function(entries) {
TestMediaScanner.prototype.scanFiles = function(entries, mode) {
var scan = new TestScanResult(this.fileEntries);
scan.totalBytes = this.totalBytes;
scan.scanDuration = this.scanDuration;
......@@ -69,6 +56,21 @@ TestMediaScanner.prototype.scanFiles = function(entries) {
return scan;
};
/** @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 unregistered observer');
}
};
/**
* Finalizes all previously started scans.
*/
......@@ -78,7 +80,6 @@ TestMediaScanner.prototype.finalizeScans = function() {
/**
* Notifies observers that the most recently started scan has been updated.
* @param {!importer.ScanResult} result
*/
TestMediaScanner.prototype.update = function() {
assertTrue(this.scans_.length > 0);
......@@ -91,13 +92,15 @@ TestMediaScanner.prototype.update = function() {
/**
* Notifies observers that a scan has finished.
* @param {!importer.ScanResult} result
* @param {!importer.ScanResult} scan
*/
TestMediaScanner.prototype.finalize = function(result) {
result.finalize();
this.observers.forEach(
function(observer) {
observer(importer.ScanEvent.FINALIZED, result);
TestMediaScanner.prototype.finalize = function(scan) {
// Note the |scan| has {!TestScanResult} type in test, and needs a
// finalize() call before being notified to scan observers.
/** @type {!TestScanResult} */ (scan).finalize();
this.observers.forEach(function(observer) {
observer(importer.ScanEvent.FINALIZED, scan);
});
};
......@@ -137,7 +140,7 @@ function TestScanResult(fileEntries) {
this.fileEntries = fileEntries.slice();
/**
* List of file entries found while scanning.
* List of duplicate file entries found while scanning.
* @type {!Array<!FileEntry>}
*/
this.duplicateFileEntries = [];
......@@ -148,10 +151,10 @@ function TestScanResult(fileEntries) {
/** @type {number} */
this.scanDuration = 100;
/** @type {function} */
/** @type {function(*)} */
this.resolveResult_;
/** @type {function} */
/** @type {function()} */
this.rejectResult_;
/** @type {boolean} */
......@@ -186,52 +189,61 @@ TestScanResult.prototype = {
};
/** @override */
TestScanResult.prototype.getFileEntries = function() {
return this.fileEntries;
TestScanResult.prototype.isFinal = function() {
return this.settled_;
};
/** @override */
TestScanResult.prototype.getDuplicateFileEntries = function() {
return this.duplicateFileEntries;
TestScanResult.prototype.cancel = function() {
this.canceled_ = true;
};
/** @override */
TestScanResult.prototype.finalize = function() {
return this.resolveResult_(this);
TestScanResult.prototype.canceled = function() {
return this.canceled_;
};
/** @override */
TestScanResult.prototype.whenFinal = function() {
return this.whenFinal_;
TestScanResult.prototype.setCandidateCount = function() {
console.warn('setCandidateCount: not implemented');
};
/** @override */
TestScanResult.prototype.isFinal = function() {
return this.settled_;
TestScanResult.prototype.onCandidatesProcessed = function() {
console.warn('onCandidatesProcessed: not implemented');
};
/** @override */
TestScanResult.prototype.cancel = function() {
this.canceled_ = true;
TestScanResult.prototype.getFileEntries = function() {
return this.fileEntries;
};
/** @override */
TestScanResult.prototype.canceled = function() {
return this.canceled_;
TestScanResult.prototype.getDuplicateFileEntries = function() {
return this.duplicateFileEntries;
};
/** @override */
TestScanResult.prototype.whenFinal = function() {
return this.whenFinal_;
};
/** @override */
TestScanResult.prototype.getStatistics = function() {
duplicates = {};
var duplicates = {};
duplicates[importer.Disposition.CONTENT_DUPLICATE] = 0;
duplicates[importer.Disposition.HISTORY_DUPLICATE] = 0;
duplicates[importer.Disposition.SCAN_DUPLICATE] = 0;
return {
return /** @type {importer.ScanResult.Statistics} */ ({
scanDuration: this.scanDuration,
newFileCount: this.fileEntries.length,
duplicates: duplicates,
sizeBytes: this.totalBytes
};
});
};
TestScanResult.prototype.finalize = function() {
return this.resolveResult_(this);
};
/**
......
......@@ -24,45 +24,35 @@ importer.TestImportHistory = function() {
/**
* If null, history has been loaded and listeners notified.
*
* @private {Array<!importer.ImportHistory>}
* @private {Array<!function(!importer.ImportHistory)>}
*/
this.loadListeners_ = [];
};
/** @override */
importer.TestImportHistory.prototype.getHistory =
function() {
Promise.resolve().then(
/** @this {importer.TestImportHistory} */
function() {
importer.TestImportHistory.prototype.getHistory = function() {
Promise.resolve().then(() => {
if (this.loadListeners_) {
this.loadListeners_.forEach(
/** @param {!Array<!importer.ImportHistory>} listener */
function(listener) {
listener(this);
}.bind(this));
this.loadListeners_.forEach((listener) => listener(this));
// Null out listeners...this is our signal that history has
// been loaded ... resulting in all future listener added
// being notified immediately
this.loadListeners_ = null;
}
}.bind(this));
});
return Promise.resolve(this);
};
/** @override */
importer.TestImportHistory.prototype.addHistoryLoadedListener =
function(listener) {
// Notify immediately if history is already loaded.
importer.TestImportHistory.prototype.addHistoryLoadedListener = function(
listener) {
assertTrue(typeof listener === 'function');
// Notify listener immediately if history is already loaded.
if (this.loadListeners_ === null) {
Promise.resolve(this.history_).then(
/** @param {!importer.ImportHistory} history */
function(history) {
listener(history);
});
setTimeout(listener, 0, this);
} else {
this.loadListeners_.push(listeners);
this.loadListeners_.push(listener);
}
};
......@@ -154,6 +144,12 @@ importer.TestImportHistory.prototype.markImported =
return Promise.resolve();
};
/** @override */
importer.TestImportHistory.prototype.whenReady = function() {};
/** @override */
importer.TestImportHistory.prototype.markImportedByUrl = function() {};
/** @override */
importer.TestImportHistory.prototype.addObserver = function() {};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment