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

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

Issue 663513003: Add an unittest for background/volume_manager.js (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use reportPromise. Created 6 years, 1 month 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
OLDNEW
(Empty)
1 // Copyright 2014 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 chrome;
6 var initialized_ = false;
7
8 /**
9 * Invokes a callback function depending on the result of promise.
10 *
11 * @param {Promise} promise Promise.
12 * @param {function(boolean)} calllback Callback function. True is passed if the
13 * test failed.
hirono 2014/10/29 10:02:46 4 spaces are needed when wrapping a line.
yawano 2014/10/30 09:11:46 Done.
14 */
15 function reportPromise(promise, callback) {
hirono 2014/10/29 10:02:46 Could you please add test_util.js and share the ut
yawano 2014/10/30 09:11:46 Done.
16 promise.then(function() {
17 callback(/* error */ false);
18 }, function(error) {
19 console.error(error.stack || error);
20 callback(/* error */ true);
21 });
22 }
23
24 function initialize_() {
25 // It is allowed only once to set loadTimeData.data.
26 loadTimeData.data = {
hirono 2014/10/29 10:02:46 Maybe we can just put it at global scope.
yawano 2014/10/30 09:11:46 Done.
27 DRIVE_DIRECTORY_LABEL: 'My Drive',
28 DOWNLOADS_DIRECTORY_LABEL: 'Downloads'
29 };
30
31 initialized_ = true;
32 }
33
34 function setUp() {
35 // Set up string assets.
36 if(!initialized_) {
37 initialize_();
38 }
39
40 // Set up mock of chrome.fileManagerPrivate APIs.
41 chrome = {
42 fileManagerPrivate: {
43 mountSourcePath_: null,
44 onMountCompletedListeners_: [],
45 onDriveConnectionStatusChangedListeners_: [],
46 addMount: function(fileUrl, callback) {
47 callback(chrome.fileManagerPrivate.mountSourcePath_);
48 },
49 removeMount: function(volumeId) {
50 var event = {
51 eventType: 'unmount',
52 status: 'success',
53 volumeMetadata: {
54 volumeId: volumeId
55 }
56 };
57 chrome.fileManagerPrivate.onMountCompleted.dispatchEvent(event);
58 },
59 onDriveConnectionStatusChanged: {
60 addListener: function(listener) {
61 chrome.fileManagerPrivate.onDriveConnectionStatusChangedListeners_
62 .push(listener);
63 },
64 dispatchEvent: function(event) {
65 chrome.fileManagerPrivate
66 .onDriveConnectionStatusChangedListeners_
67 .forEach(function(listener) { listener(event); });
68 }
69 },
70 onMountCompleted: {
71 addListener: function(listener) {
72 chrome.fileManagerPrivate.onMountCompletedListeners_.push(listener);
73 },
74 dispatchEvent: function(event) {
75 chrome.fileManagerPrivate
76 .onMountCompletedListeners_.forEach(function(listener) {
77 listener(event);
78 });
79 }
80 },
81 getDriveConnectionState: function(callback) {
82 callback(chrome.fileManagerPrivate.driveConnectionState_);
83 },
84 getVolumeMetadataList: function(callback) {
85 callback(chrome.fileManagerPrivate.volumeMetadataList_);
86 },
87 requestFileSystem: function(volumeId, callback) {
88 callback(chrome.fileManagerPrivate.fileSystemMap_[volumeId]);
89 },
90 set driveConnectionState (state) {
hirono 2014/10/29 10:02:46 We don't add a space between property name and arg
yawano 2014/10/30 09:11:47 Done.
91 chrome.fileManagerPrivate.driveConnectionState_ = state;
92 chrome.fileManagerPrivate.onDriveConnectionStatusChanged
93 .dispatchEvent(null);
94 }
95 }
96 };
97
98 chrome.fileManagerPrivate.mountSourcePath_ = null;
99 chrome.fileManagerPrivate.onMountCompletedListeners_ = [];
100 chrome.fileManagerPrivate.onDriveConnectionStatusChangedListeners_ = [];
101 chrome.fileManagerPrivate.driveConnectionState_ =
102 VolumeManagerCommon.DriveConnectionType.ONLINE;
103 chrome.fileManagerPrivate.volumeMetadataList_ = [
104 {
105 volumeId: 'download:Downloads',
106 volumeLabel: '',
107 volumeType: VolumeManagerCommon.VolumeType.DOWNLOADS,
108 isReadOnly: false,
109 profile: getMockProfile()
110 },
111 {
112 volumeId: 'drive:drive-foobar%40chromium.org-hash',
113 volumeLabel: '',
114 volumeType: VolumeManagerCommon.VolumeType.DRIVE,
115 isReadOnly: false,
116 profile: getMockProfile()
117 }
118 ];
119 chrome.fileManagerPrivate.fileSystemMap_ = {
120 'download:Downloads': createMockFileSystem('download:Downloads'),
121 'drive:drive-foobar%40chromium.org-hash':
122 createMockFileSystem('drive:drive-foobar%40chromium.org-hash')
123 };
124 }
125
126 function tearDown() {
127 VolumeManager.revokeInstanceForTesting();
128 chrome = null;
129 }
130
131 /**
132 * Returns a mock profile.
133 *
134 * @return {{displayName:string, isCurrentProfile:boolean, profileId:string}}
135 * Mock profile
136 */
137 function getMockProfile() {
138 return {
139 displayName: 'foobar@chromium.org',
140 isCurrentProfile: true,
141 profileId: ''
142 };
143 }
144
145 /**
146 * Creates a mock file system.
147 *
148 * @return {MockFileSystem} A mock file system.
149 */
150 function createMockFileSystem(volumeId) {
151 var fileSystem = new MockFileSystem(volumeId, 'filesystem:' + volumeId);
152 fileSystem.entries['/'] = new MockDirectoryEntry(fileSystem, '/');
153 return fileSystem;
154 }
155
156 function testGetVolumeInfo(callback) {
157 reportPromise(VolumeManager.getInstance().then(function(volumeManager) {
158 return new Promise(function(resolve, reject) {
hirono 2014/10/29 10:02:46 The Promise is needed?
yawano 2014/10/30 09:11:46 Done. The promise was unnecessary. I didn't know t
159 var entry = new MockFileEntry(createMockFileSystem('download:Downloads'),
160 '/foo/bar/bla.zip');
161
162 var volumeInfo = volumeManager.getVolumeInfo(entry);
163 assertEquals('download:Downloads', volumeInfo.volumeId);
164 assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS,
165 volumeInfo.volumeType);
166
167 resolve(true);
168 });
169 }), callback);
170 }
171
172 function testGetDriveConnectionState(callback) {
173 reportPromise(VolumeManager.getInstance().then(function(volumeManager) {
174 return new Promise(function(resolve, reject) {
hirono 2014/10/29 10:02:46 The Promise is needed?
yawano 2014/10/30 09:11:46 Done.
175 // Default connection state is online
176 assertEquals(VolumeManagerCommon.DriveConnectionType.ONLINE,
177 volumeManager.getDriveConnectionState());
178
179 // Sets it to offline.
180 chrome.fileManagerPrivate.driveConnectionState =
181 VolumeManagerCommon.DriveConnectionType.OFFLINE;
182 assertEquals(VolumeManagerCommon.DriveConnectionType.OFFLINE,
183 volumeManager.getDriveConnectionState());
184
185 // Sets it back to online
186 chrome.fileManagerPrivate.driveConnectionState =
187 VolumeManagerCommon.DriveConnectionType.ONLINE;
188 assertEquals(VolumeManagerCommon.DriveConnectionType.ONLINE,
189 volumeManager.getDriveConnectionState());
190
191 resolve(true);
192 });
193 }), callback);
194 }
195
196 function testMountArchiveAndUnmount(callback) {
197 // Set states of mock fileManagerPrivate APIs.
198 const mountSourcePath = '/usr/local/home/test/Downloads/foobar.zip';
199 chrome.fileManagerPrivate.mountSourcePath_ = mountSourcePath;
200 chrome.fileManagerPrivate.fileSystemMap_['archive:foobar.zip'] =
201 createMockFileSystem('archive:foobar.zip');
202
203 var numberOfVolumes;
204 var volumeManager;
205
206 reportPromise(VolumeManager.getInstance().then(function(volumeManager_) {
hirono 2014/10/29 10:02:46 volumeManager_ -> volumeManager
yawano 2014/10/30 09:11:47 Done. I moved scopes of volumeManager and numberOf
207 volumeManager = volumeManager_;
208 numberOfVolumes = volumeManager.volumeInfoList.length;
209
210 return new Promise(function(resolve, reject) {
211 // Mount an archieve
212 volumeManager.mountArchive(
213 'filesystem:chrome-extension://extensionid/external/Downloads-test/' +
hirono 2014/10/29 10:02:46 4 spaces indent.
yawano 2014/10/30 09:11:47 Done.
214 'foobar.zip',
215 resolve, reject);
216
217 chrome.fileManagerPrivate.onMountCompleted.dispatchEvent(
218 {eventType: 'mount',
hirono 2014/10/29 10:02:46 We usually do either: xxxxxxxxxxxxxxxx({ eventT
yawano 2014/10/30 09:11:47 Done.
219 status: 'success',
220 volumeMetadata: {
221 volumeId: 'archive:foobar.zip',
222 volumeLabel: 'foobar.zip',
223 volumeType: VolumeManagerCommon.VolumeType.ARCHIVE,
224 isReadOnly: true,
225 sourcePath: mountSourcePath,
226 profile: getMockProfile()
227 }
228 });
229 });
230 }).then(function(result) {
231 assertEquals(numberOfVolumes + 1, volumeManager.volumeInfoList.length);
232
233 return new Promise(function(resolve, reject) {
234 // Unmount the mounted archievea
235 volumeManager.volumeInfoList.addEventListener('splice', function(e) {
236 assertEquals(numberOfVolumes, volumeManager.volumeInfoList.length);
237 resolve(true);
238 });
239 var entry = new MockFileEntry(createMockFileSystem('archive:foobar.zip'),
240 '/foo.txt');
241 var volumeInfo = volumeManager.getVolumeInfo(entry);
242 volumeManager.unmount(volumeInfo);
243 });
244 }), callback);
245 }
246
247 function testGetCurrentProfileVolumeInfo(callback) {
248 reportPromise(VolumeManager.getInstance().then(function(volumeManager) {
249 return new Promise(function(resolve, reject) {
hirono 2014/10/29 10:02:46 The Promise is needed?
yawano 2014/10/30 09:11:47 Done.
250 var volumeInfo = volumeManager.getCurrentProfileVolumeInfo(
251 VolumeManagerCommon.VolumeType.DRIVE);
252
253 assertEquals('drive:drive-foobar%40chromium.org-hash',
254 volumeInfo.volumeId);
255 assertEquals(VolumeManagerCommon.VolumeType.DRIVE, volumeInfo.volumeType);
256
257 resolve(true);
258 });
259 }), callback);
260 }
261
262 function testGetLocationInfo(callback) {
263 reportPromise(VolumeManager.getInstance().then(function(volumeManager) {
264 return new Promise(function(resolve, reject) {
hirono 2014/10/29 10:02:46 The Promise is needed?
yawano 2014/10/30 09:11:47 Done.
265 var entry1 = new MockFileEntry(createMockFileSystem('download:Downloads'),
hirono 2014/10/29 10:02:46 I prefer the specific name such as downloadEntry r
yawano 2014/10/30 09:11:46 Done.
266 '/foo/bar/bla.zip');
267 var locationInfo1 = volumeManager.getLocationInfo(entry1);
268 assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS,
269 locationInfo1.rootType)
270 assertFalse(locationInfo1.isReadOnly);
271 assertFalse(locationInfo1.isRootEntry);
272
273 var entry2 = new MockFileEntry(
274 createMockFileSystem('drive:drive-foobar%40chromium.org-hash'),
275 '/root');
276 var locationInfo2 = volumeManager.getLocationInfo(entry2);
277 assertEquals(VolumeManagerCommon.VolumeType.DRIVE, locationInfo2.rootType)
278 assertFalse(locationInfo2.isReadOnly);
279 assertTrue(locationInfo2.isRootEntry);
280
281 resolve(true);
282 });
283 }), callback);
284 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698