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

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: Merged TestFileSystem and MockFileSystem. 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
« no previous file with comments | « chrome/test/data/file_manager/unit_tests/volume_manager_unittest.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 function initialize_() {
9 // Set up string assets.
10 loadTimeData.data = {
11 DRIVE_DIRECTORY_LABEL: 'My Drive',
12 DOWNLOADS_DIRECTORY_LABEL: 'Downloads'
13 };
14
15 // Set up mock of chrome.fileManagerPrivate APIs.
16 chrome = {
17 fileManagerPrivate: {
18 mountSourcePath_: null,
19 onMountCompletedListeners_: [],
20 onDriveConnectionStatusChangedListeners_: [],
21 addMount: function(fileUrl, callback) {
22 callback(chrome.fileManagerPrivate.mountSourcePath_);
23 },
24 removeMount: function(volumeId) {
25 var event = {
26 eventType: 'unmount',
27 status: 'success',
28 volumeMetadata: {
29 volumeId: volumeId
30 }
31 };
32 chrome.fileManagerPrivate.onMountCompleted.dispatchEvent(event);
33 },
34 onDriveConnectionStatusChanged: {
35 addListener: function(listener) {
36 chrome.fileManagerPrivate.onDriveConnectionStatusChangedListeners_
37 .push(listener);
38 },
39 dispatchEvent: function(event) {
40 chrome.fileManagerPrivate
41 .onDriveConnectionStatusChangedListeners_
42 .forEach(function(listener) { listener(event); });
43 }
44 },
45 onMountCompleted: {
46 addListener: function(listener) {
47 chrome.fileManagerPrivate.onMountCompletedListeners_.push(listener);
48 },
49 dispatchEvent: function(event) {
50 chrome.fileManagerPrivate
51 .onMountCompletedListeners_.forEach(function(listener) {
52 listener(event);
53 });
54 }
55 },
56 getDriveConnectionState: function(callback) {
57 callback(chrome.fileManagerPrivate.driveConnectionState_);
58 },
59 getVolumeMetadataList: function(callback) {
60 callback(chrome.fileManagerPrivate.volumeMetadataList_);
61 },
62 requestFileSystem: function(volumeId, callback) {
63 callback(chrome.fileManagerPrivate.fileSystemMap_[volumeId]);
64 },
65 set driveConnectionState (state) {
66 chrome.fileManagerPrivate.driveConnectionState_ = state;
67 chrome.fileManagerPrivate.onDriveConnectionStatusChanged
68 .dispatchEvent(null);
69 }
70 }
71 };
72
73 // Initialize states of mock fileManagerPrivate APIs.
74 // States of fileManagerPrivate APIs are initialized only once through the all
75 // tests since volume manager uses singleton model, and it register events in
76 // initialize phase, so we need to keep registered event listeners.
77 chrome.fileManagerPrivate.mountSourcePath_ = null;
78 chrome.fileManagerPrivate.onMountCompletedListeners_ = [];
79 chrome.fileManagerPrivate.onDriveConnectionStatusChangedListeners_ = [];
80 chrome.fileManagerPrivate.driveConnectionState_ =
81 VolumeManagerCommon.DriveConnectionType.ONLINE;
82 chrome.fileManagerPrivate.volumeMetadataList_ = [
83 {
84 volumeId: 'download:Downloads',
85 volumeLabel: '',
86 volumeType: VolumeManagerCommon.VolumeType.DOWNLOADS,
87 isReadOnly: false,
88 profile: getMockProfile()
89 },
90 {
91 volumeId: 'drive:drive-foobar%40chromium.org-hash',
92 volumeLabel: '',
93 volumeType: VolumeManagerCommon.VolumeType.DRIVE,
94 isReadOnly: false,
95 profile: getMockProfile()
96 }
97 ];
98 chrome.fileManagerPrivate.fileSystemMap_ = {
99 'download:Downloads': createMockFileSystem('download:Downloads'),
100 'drive:drive-foobar%40chromium.org-hash':
101 createMockFileSystem('drive:drive-foobar%40chromium.org-hash')
102 };
103
104 initialized = true;
105 }
106
107 function setUp() {
108 if(!initialized) {
109 initialize_();
110 }
111 }
112
113 /**
114 * Returns a mock profile.
115 *
116 * @return {{displayName:string, isCurrentProfile:boolean, profileId:string}}
117 * Mock profile
118 */
119 function getMockProfile() {
120 return {
121 displayName: 'foobar@chromium.org',
122 isCurrentProfile: true,
123 profileId: ''
124 };
125 }
126
127 /**
128 * Creates a mock file system.
129 *
130 * @return {MockFileSystem} A mock file system.
131 */
132 function createMockFileSystem(volumeId) {
133 var fileSystem = new MockFileSystem(volumeId, 'filesystem:' + volumeId);
134 fileSystem.entries['/'] = new MockDirectoryEntry(fileSystem, '/');
135 return fileSystem;
136 }
137
138 function testGetVolumeInfo(callback) {
139 VolumeManager.getInstance().then(function(volumeManager) {
140 var entry = new MockFileEntry(createMockFileSystem('download:Downloads'),
141 '/foo/bar/bla.zip');
142
143 var volumeInfo = volumeManager.getVolumeInfo(entry);
144 assertEquals('download:Downloads', volumeInfo.volumeId);
145 assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS,
146 volumeInfo.volumeType);
147
148 callback(false);
hirono 2014/10/27 09:50:12 Could you use 'reportPromise' utility function, ht
yawano 2014/10/29 04:07:23 Done.
149 }).catch(function(error) {
150 console.log(error.stack || error);
151 callback(true);
152 });
153 }
154
155 function testGetDriveConnectionState(callback) {
156 VolumeManager.getInstance().then(function(volumeManager) {
157 // Default connection state is online
158 assertEquals(VolumeManagerCommon.DriveConnectionType.ONLINE,
159 volumeManager.getDriveConnectionState());
160
161 // Sets it to offline.
162 chrome.fileManagerPrivate.driveConnectionState =
163 VolumeManagerCommon.DriveConnectionType.OFFLINE;
164 assertEquals(VolumeManagerCommon.DriveConnectionType.OFFLINE,
165 volumeManager.getDriveConnectionState());
166
167 // Sets it back to online
168 chrome.fileManagerPrivate.driveConnectionState =
169 VolumeManagerCommon.DriveConnectionType.ONLINE;
170 assertEquals(VolumeManagerCommon.DriveConnectionType.ONLINE,
171 volumeManager.getDriveConnectionState());
172
173 callback(false);
174 }).catch(function(error) {
175 console.log(error.stack || error);
176 callback(true);
177 });
178 }
179
180 function testMountArchiveAndUnmount(callback) {
181 // Set states of mock fileManagerPrivate APIs.
182 const mountSourcePath = '/usr/local/home/test/Downloads/foobar.zip';
183 chrome.fileManagerPrivate.mountSourcePath_ = mountSourcePath;
184 chrome.fileManagerPrivate.fileSystemMap_['archive:foobar.zip'] =
185 createMockFileSystem('archive:foobar.zip');
186
187 var numberOfVolumes;
188 var volumeManager;
189
190 VolumeManager.getInstance().then(function(volumeManager_) {
191 volumeManager = volumeManager_;
192 numberOfVolumes = volumeManager.volumeInfoList.length;
193
194 return new Promise(function(resolve, reject) {
195 // Mount an archieve
196 volumeManager.mountArchive(
197 'filesystem:chrome-extension://extensionid/external/Downloads-test/' +
198 'foobar.zip',
199 resolve, reject);
200
201 chrome.fileManagerPrivate.onMountCompleted.dispatchEvent(
202 {eventType: 'mount',
203 status: 'success',
204 volumeMetadata: {
205 volumeId: 'archive:foobar.zip',
206 volumeLabel: 'foobar.zip',
207 volumeType: VolumeManagerCommon.VolumeType.ARCHIVE,
208 isReadOnly: true,
209 sourcePath: mountSourcePath,
210 profile: getMockProfile()
211 }
212 });
213 });
214 }).then(function(result) {
215 assertEquals(numberOfVolumes + 1, volumeManager.volumeInfoList.length);
216
217 // Unmount the mounted archievea
218 volumeManager.volumeInfoList.addEventListener('splice', function(e) {
219 assertEquals(numberOfVolumes, volumeManager.volumeInfoList.length);
220 callback(false);
221 });
222 var entry = new MockFileEntry(createMockFileSystem('archive:foobar.zip'),
223 '/foo.txt');
224 var volumeInfo = volumeManager.getVolumeInfo(entry);
225 volumeManager.unmount(volumeInfo);
226 }).catch(function(error) {
227 console.log(error.stack || error);
228 callback(true);
229 });
230 }
231
232 function testGetCurrentProfileVolumeInfo(callback) {
233 VolumeManager.getInstance().then(function(volumeManager) {
234 var volumeInfo = volumeManager.getCurrentProfileVolumeInfo(
235 VolumeManagerCommon.VolumeType.DRIVE);
236
237 assertEquals('drive:drive-foobar%40chromium.org-hash', volumeInfo.volumeId);
238 assertEquals(VolumeManagerCommon.VolumeType.DRIVE, volumeInfo.volumeType);
239
240 callback(false);
241 }).catch(function(error) {
242 console.log(error.stack || error);
243 callback(true);
244 });
245 }
246
247 function testGetLocationInfo(callback) {
248 VolumeManager.getInstance().then(function(volumeManager) {
249 var entry1 = new MockFileEntry(createMockFileSystem('download:Downloads'),
250 '/foo/bar/bla.zip');
251 var locationInfo1 = volumeManager.getLocationInfo(entry1);
252 assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS,
253 locationInfo1.rootType)
254 assertFalse(locationInfo1.isReadOnly);
255 assertFalse(locationInfo1.isRootEntry);
256
257 var entry2 = new MockFileEntry(
258 createMockFileSystem('drive:drive-foobar%40chromium.org-hash'),
259 '/root');
260 var locationInfo2 = volumeManager.getLocationInfo(entry2);
261 assertEquals(VolumeManagerCommon.VolumeType.DRIVE, locationInfo2.rootType)
262 assertFalse(locationInfo2.isReadOnly);
263 assertTrue(locationInfo2.isRootEntry);
264
265 callback(false);
266 }).catch(function(error) {
267 console.log(error.stack || error);
268 callback(true);
269 });
270 }
OLDNEW
« no previous file with comments | « chrome/test/data/file_manager/unit_tests/volume_manager_unittest.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698