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

Side by Side Diff: chrome/test/data/file_manager/unit_tests/metadata_cache_unittest.js

Issue 340843005: Files.app: Create unit tests for MetadataCache. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed. Created 6 years, 6 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var metadataCache;
6 var provider;
7
8 /**
9 * Mock of MetadataProvider.
10 * @constructor
11 */
12 function MockProvider() {
13 MetadataProvider.call(this);
14 this.callbackPool = [];
15 Object.freeze(this);
16 }
17
18 MockProvider.prototype = {
19 __proto__: MetadataProvider.prototype
20 };
21
22 MockProvider.prototype.supportsEntry = function(entry) {
23 return true;
24 };
25
26 MockProvider.prototype.providesType = function(type) {
27 return type === 'stub';
28 };
29
30 MockProvider.prototype.getId = function() {
31 return 'stub';
32 };
33
34 MockProvider.prototype.fetch = function(entry, type, callback) {
35 this.callbackPool.push(callback);
36 };
37
38 /**
39 * Short hand for the metadataCache.get.
40 *
41 * @param {Array.<Entry>} entries Entries.
42 * @param {string} type Metadata type.
43 * @return {Promise} Promise to be fulfilled with the result metadata.
44 */
45 function getMetadata(entries, type) {
46 return new Promise(metadataCache.get.bind(metadataCache, entries, type));
47 };
48
49 /**
50 * Invokes a callback function depending on the result of promise.
51 *
52 * @param {Promise} promise Promise.
53 * @param {function(boolean)} calllback Callback function. True is passed if the
54 * test failed.
55 */
56 function reportPromise(promise, callback) {
57 promise.then(function() {
58 callback(/* error */ false);
59 }, function(error) {
60 console.error(error.stack || error);
61 callback(/* error */ true);
62 });
63 }
64
65 /**
66 * Constructs the metadata cache and its provider.
67 */
68 function setUp() {
69 provider = new MockProvider();
70 metadataCache = new MetadataCache([provider]);
71 }
72
73 /**
74 * Confirms metadata is cached for the same entry.
75 *
76 * @param {function(boolean=)} callback Callback to be called when test
77 * completes. If the test fails, true is passed to the function.
78 */
79 function testCached(callback) {
80 var entry = new MockFileEntry('volumeId', '/banjo.txt');
81
82 var promises = [];
83 var firstPromise = getMetadata([entry], 'stub');
84 var cachedBeforeFetchingPromise = getMetadata([entry], 'stub');
85 provider.callbackPool[0]({stub: {value: 'banjo'}});
86 var cachedAfterFethingPromise = getMetadata([entry], 'stub');
87
88 // Provide should be called only once.
89 assertEquals(1, provider.callbackPool.length);
90
91 reportPromise(Promise.all([
92 firstPromise,
93 cachedBeforeFetchingPromise,
94 cachedAfterFethingPromise
95 ]).then(function(metadata) {
96 assertDeepEquals([{value: 'banjo'}], metadata[0]);
97 assertDeepEquals([{value: 'banjo'}], metadata[1]);
98 assertDeepEquals([{value: 'banjo'}], metadata[1]);
99 }), callback);
100 }
101
102 /**
103 * Confirms metadata is not cached for different entries.
104 *
105 * @param {function(boolean=)} callback Callback to be called when test
106 * completes. If the test fails, true is passed to the function.
107 */
108 function testNotCached(callback) {
109 var entry1 = new MockFileEntry('volumeId', '/banjo.txt');
110 var entry2 = new MockFileEntry('volumeId', '/fiddle.txt');
111
112 var firstPromise = getMetadata([entry1], 'stub');
113 var anotherPromise = getMetadata([entry2], 'stub');
114
115 // Provide should be called for each entry.
116 assertEquals(2, provider.callbackPool.length);
117
118 provider.callbackPool[0]({stub: {value: 'banjo'}});
119 provider.callbackPool[1]({stub: {value: 'fiddle'}});
120
121 reportPromise(Promise.all([
122 firstPromise,
123 anotherPromise
124 ]).then(function(metadata) {
125 assertDeepEquals([{value: 'banjo'}], metadata[0]);
126 assertDeepEquals([{value: 'fiddle'}], metadata[1]);
127 }), callback);
128 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698