Commit 025ed709 authored by hirono@chromium.org's avatar hirono@chromium.org

Files.app: Create unit tests for MetadataCache.

BUG=386553
TEST=run the test

Review URL: https://codereview.chromium.org/340843005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278736 0039d316-1c4b-4281-b951-d872f2087c98
parent 80bfae11
......@@ -48,6 +48,10 @@ IN_PROC_BROWSER_TEST_F(
IN_PROC_BROWSER_TEST_F(
FileManagerJsTest, DeviceHandlerTest) {
RunTest(base::FilePath(
FILE_PATH_LITERAL("device_handler_unittest.html")));
RunTest(base::FilePath(FILE_PATH_LITERAL("device_handler_unittest.html")));
}
IN_PROC_BROWSER_TEST_F(
FileManagerJsTest, MetadataCacheTest) {
RunTest(base::FilePath(FILE_PATH_LITERAL("metadata_cache_unittest.html")));
}
<!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/file_manager/file_manager/foreground/js/metadata/metadata_cache.js"></script>
<script src="mocks/mock_file_entry.js"></script>
<script src="metadata_cache_unittest.js"></script>
</body>
</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.
var metadataCache;
var provider;
/**
* Mock of MetadataProvider.
* @constructor
*/
function MockProvider() {
MetadataProvider.call(this);
this.callbackPool = [];
Object.freeze(this);
}
MockProvider.prototype = {
__proto__: MetadataProvider.prototype
};
MockProvider.prototype.supportsEntry = function(entry) {
return true;
};
MockProvider.prototype.providesType = function(type) {
return type === 'stub';
};
MockProvider.prototype.getId = function() {
return 'stub';
};
MockProvider.prototype.fetch = function(entry, type, callback) {
this.callbackPool.push(callback);
};
/**
* Short hand for the metadataCache.get.
*
* @param {Array.<Entry>} entries Entries.
* @param {string} type Metadata type.
* @return {Promise} Promise to be fulfilled with the result metadata.
*/
function getMetadata(entries, type) {
return new Promise(metadataCache.get.bind(metadataCache, entries, type));
};
/**
* Invokes a callback function depending on the result of promise.
*
* @param {Promise} promise Promise.
* @param {function(boolean)} calllback Callback function. True is passed if the
* test failed.
*/
function reportPromise(promise, callback) {
promise.then(function() {
callback(/* error */ false);
}, function(error) {
console.error(error.stack || error);
callback(/* error */ true);
});
}
/**
* Constructs the metadata cache and its provider.
*/
function setUp() {
provider = new MockProvider();
metadataCache = new MetadataCache([provider]);
}
/**
* Confirms metadata is cached for the same entry.
*
* @param {function(boolean=)} callback Callback to be called when test
* completes. If the test fails, true is passed to the function.
*/
function testCached(callback) {
var entry = new MockFileEntry('volumeId', '/banjo.txt');
var promises = [];
var firstPromise = getMetadata([entry], 'stub');
var cachedBeforeFetchingPromise = getMetadata([entry], 'stub');
provider.callbackPool[0]({stub: {value: 'banjo'}});
var cachedAfterFethingPromise = getMetadata([entry], 'stub');
// Provide should be called only once.
assertEquals(1, provider.callbackPool.length);
reportPromise(Promise.all([
firstPromise,
cachedBeforeFetchingPromise,
cachedAfterFethingPromise
]).then(function(metadata) {
assertDeepEquals([{value: 'banjo'}], metadata[0]);
assertDeepEquals([{value: 'banjo'}], metadata[1]);
assertDeepEquals([{value: 'banjo'}], metadata[1]);
}), callback);
}
/**
* Confirms metadata is not cached for different entries.
*
* @param {function(boolean=)} callback Callback to be called when test
* completes. If the test fails, true is passed to the function.
*/
function testNotCached(callback) {
var entry1 = new MockFileEntry('volumeId', '/banjo.txt');
var entry2 = new MockFileEntry('volumeId', '/fiddle.txt');
var firstPromise = getMetadata([entry1], 'stub');
var anotherPromise = getMetadata([entry2], 'stub');
// Provide should be called for each entry.
assertEquals(2, provider.callbackPool.length);
provider.callbackPool[0]({stub: {value: 'banjo'}});
provider.callbackPool[1]({stub: {value: 'fiddle'}});
reportPromise(Promise.all([
firstPromise,
anotherPromise
]).then(function(metadata) {
assertDeepEquals([{value: 'banjo'}], metadata[0]);
assertDeepEquals([{value: 'fiddle'}], metadata[1]);
}), callback);
}
......@@ -13,3 +13,12 @@ function MockFileEntry(volumeId, fullPath) {
this.volumeId = volumeId;
this.fullPath = fullPath;
}
/**
* Returns fake URL.
*
* @return {string} Fake URL.
*/
MockFileEntry.prototype.toURL = function() {
return 'filesystem:' + this.volumeId + this.fullPath;
};
......@@ -46,9 +46,10 @@
* var size = (cached && cached.size) || UNKNOWN_SIZE;
* }
*
* @param {Array.<MetadataProvider>} providers Metadata providers.
* @constructor
*/
function MetadataCache() {
function MetadataCache(providers) {
/**
* Map from Entry (using Entry.toURL) to metadata. Metadata contains
* |properties| - an hierarchical object of values, and an object for each
......@@ -61,7 +62,7 @@ function MetadataCache() {
* List of metadata providers.
* @private
*/
this.providers_ = [];
this.providers_ = providers;
/**
* List of observers added. Each one is an object with fields:
......@@ -81,9 +82,13 @@ function MetadataCache() {
/**
* Time of first get query of the current batch. Items updated later than this
* will not be evicted.
*
* @type {Date}
* @private
*/
this.lastBatchStart_ = new Date();
Object.seal(this);
}
/**
......@@ -114,13 +119,13 @@ MetadataCache.EVICTION_THRESHOLD_MARGIN = 500;
* @return {MetadataCache!} The cache with all providers.
*/
MetadataCache.createFull = function(volumeManager) {
var cache = new MetadataCache();
// DriveProvider should be prior to FileSystemProvider, because it covers
// FileSystemProvider for files in Drive.
cache.providers_.push(new DriveProvider(volumeManager));
cache.providers_.push(new FilesystemProvider());
cache.providers_.push(new ContentProvider());
return cache;
return new MetadataCache([
new DriveProvider(volumeManager),
new FilesystemProvider(),
new ContentProvider()
]);
};
/**
......
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