OLD | NEW |
---|---|
(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 | |
7 loadTimeData.data = { | |
8 DRIVE_DIRECTORY_LABEL: 'My Drive', | |
9 DOWNLOADS_DIRECTORY_LABEL: 'Downloads' | |
10 }; | |
11 | |
12 function setUp() { | |
13 // Set up mock of chrome.fileManagerPrivate APIs. | |
14 chrome = { | |
15 fileManagerPrivate: { | |
16 mountSourcePath_: null, | |
17 onMountCompletedListeners_: [], | |
18 onDriveConnectionStatusChangedListeners_: [], | |
19 addMount: function(fileUrl, callback) { | |
20 callback(chrome.fileManagerPrivate.mountSourcePath_); | |
21 }, | |
22 removeMount: function(volumeId) { | |
23 var event = { | |
24 eventType: 'unmount', | |
25 status: 'success', | |
26 volumeMetadata: { | |
27 volumeId: volumeId | |
28 } | |
29 }; | |
30 chrome.fileManagerPrivate.onMountCompleted.dispatchEvent(event); | |
31 }, | |
32 onDriveConnectionStatusChanged: { | |
33 addListener: function(listener) { | |
34 chrome.fileManagerPrivate.onDriveConnectionStatusChangedListeners_ | |
35 .push(listener); | |
36 }, | |
37 dispatchEvent: function(event) { | |
38 chrome.fileManagerPrivate | |
39 .onDriveConnectionStatusChangedListeners_ | |
40 .forEach(function(listener) { listener(event); }); | |
41 } | |
42 }, | |
43 onMountCompleted: { | |
44 addListener: function(listener) { | |
45 chrome.fileManagerPrivate.onMountCompletedListeners_.push(listener); | |
46 }, | |
47 dispatchEvent: function(event) { | |
48 chrome.fileManagerPrivate | |
49 .onMountCompletedListeners_.forEach(function(listener) { | |
50 listener(event); | |
51 }); | |
52 } | |
53 }, | |
54 getDriveConnectionState: function(callback) { | |
55 callback(chrome.fileManagerPrivate.driveConnectionState_); | |
56 }, | |
57 getVolumeMetadataList: function(callback) { | |
58 callback(chrome.fileManagerPrivate.volumeMetadataList_); | |
59 }, | |
60 requestFileSystem: function(volumeId, callback) { | |
61 callback(chrome.fileManagerPrivate.fileSystemMap_[volumeId]); | |
62 }, | |
63 set driveConnectionState(state) { | |
64 chrome.fileManagerPrivate.driveConnectionState_ = state; | |
65 chrome.fileManagerPrivate.onDriveConnectionStatusChanged | |
66 .dispatchEvent(null); | |
67 } | |
68 } | |
69 }; | |
70 | |
71 chrome.fileManagerPrivate.mountSourcePath_ = null; | |
72 chrome.fileManagerPrivate.onMountCompletedListeners_ = []; | |
73 chrome.fileManagerPrivate.onDriveConnectionStatusChangedListeners_ = []; | |
74 chrome.fileManagerPrivate.driveConnectionState_ = | |
75 VolumeManagerCommon.DriveConnectionType.ONLINE; | |
76 chrome.fileManagerPrivate.volumeMetadataList_ = [ | |
77 { | |
78 volumeId: 'download:Downloads', | |
79 volumeLabel: '', | |
80 volumeType: VolumeManagerCommon.VolumeType.DOWNLOADS, | |
81 isReadOnly: false, | |
82 profile: getMockProfile() | |
83 }, | |
84 { | |
85 volumeId: 'drive:drive-foobar%40chromium.org-hash', | |
86 volumeLabel: '', | |
87 volumeType: VolumeManagerCommon.VolumeType.DRIVE, | |
88 isReadOnly: false, | |
89 profile: getMockProfile() | |
90 } | |
91 ]; | |
92 chrome.fileManagerPrivate.fileSystemMap_ = { | |
93 'download:Downloads': createMockFileSystem('download:Downloads'), | |
94 'drive:drive-foobar%40chromium.org-hash': | |
95 createMockFileSystem('drive:drive-foobar%40chromium.org-hash') | |
96 }; | |
97 } | |
98 | |
99 function tearDown() { | |
100 VolumeManager.revokeInstanceForTesting(); | |
101 chrome = null; | |
102 } | |
103 | |
104 /** | |
105 * Returns a mock profile. | |
106 * | |
107 * @return {{displayName:string, isCurrentProfile:boolean, profileId:string}} | |
108 * Mock profile | |
109 */ | |
110 function getMockProfile() { | |
111 return { | |
112 displayName: 'foobar@chromium.org', | |
113 isCurrentProfile: true, | |
114 profileId: '' | |
115 }; | |
116 } | |
117 | |
118 /** | |
119 * Creates a mock file system. | |
120 * | |
121 * @return {MockFileSystem} A mock file system. | |
122 */ | |
123 function createMockFileSystem(volumeId) { | |
124 var fileSystem = new MockFileSystem(volumeId, 'filesystem:' + volumeId); | |
125 fileSystem.entries['/'] = new MockDirectoryEntry(fileSystem, '/'); | |
126 return fileSystem; | |
127 } | |
128 | |
129 function testGetVolumeInfo(callback) { | |
130 reportPromise(VolumeManager.getInstance().then(function(volumeManager) { | |
131 var entry = new MockFileEntry(createMockFileSystem('download:Downloads'), | |
132 '/foo/bar/bla.zip'); | |
133 | |
134 var volumeInfo = volumeManager.getVolumeInfo(entry); | |
135 assertEquals('download:Downloads', volumeInfo.volumeId); | |
136 assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS, | |
137 volumeInfo.volumeType); | |
138 }), callback); | |
139 } | |
140 | |
141 function testGetDriveConnectionState(callback) { | |
142 reportPromise(VolumeManager.getInstance().then(function(volumeManager) { | |
143 // Default connection state is online | |
144 assertEquals(VolumeManagerCommon.DriveConnectionType.ONLINE, | |
145 volumeManager.getDriveConnectionState()); | |
146 | |
147 // Sets it to offline. | |
148 chrome.fileManagerPrivate.driveConnectionState = | |
149 VolumeManagerCommon.DriveConnectionType.OFFLINE; | |
150 assertEquals(VolumeManagerCommon.DriveConnectionType.OFFLINE, | |
151 volumeManager.getDriveConnectionState()); | |
152 | |
153 // Sets it back to online | |
154 chrome.fileManagerPrivate.driveConnectionState = | |
155 VolumeManagerCommon.DriveConnectionType.ONLINE; | |
156 assertEquals(VolumeManagerCommon.DriveConnectionType.ONLINE, | |
157 volumeManager.getDriveConnectionState()); | |
158 }), callback); | |
159 } | |
160 | |
161 function testMountArchiveAndUnmount(callback) { | |
162 // Set states of mock fileManagerPrivate APIs. | |
163 const mountSourcePath = '/usr/local/home/test/Downloads/foobar.zip'; | |
164 chrome.fileManagerPrivate.mountSourcePath_ = mountSourcePath; | |
165 chrome.fileManagerPrivate.fileSystemMap_['archive:foobar.zip'] = | |
166 createMockFileSystem('archive:foobar.zip'); | |
167 | |
168 reportPromise(VolumeManager.getInstance().then(function(volumeManager) { | |
169 var numberOfVolumes = volumeManager.volumeInfoList.length; | |
170 | |
171 return (new Promise(function(resolve, reject) { | |
hirono
2014/10/30 09:35:50
nit: The '()' is not needed.
yawano
2014/10/30 10:18:30
Done.
| |
172 // Mount an archieve | |
173 volumeManager.mountArchive( | |
174 'filesystem:chrome-extension://extensionid/external/Downloads-test/' + | |
175 'foobar.zip', | |
176 resolve, reject); | |
177 | |
178 chrome.fileManagerPrivate.onMountCompleted.dispatchEvent({ | |
179 eventType: 'mount', | |
180 status: 'success', | |
181 volumeMetadata: { | |
182 volumeId: 'archive:foobar.zip', | |
183 volumeLabel: 'foobar.zip', | |
184 volumeType: VolumeManagerCommon.VolumeType.ARCHIVE, | |
185 isReadOnly: true, | |
186 sourcePath: mountSourcePath, | |
187 profile: getMockProfile() | |
188 } | |
189 }); | |
190 })).then(function(result) { | |
191 assertEquals(numberOfVolumes + 1, volumeManager.volumeInfoList.length); | |
192 | |
193 return new Promise(function(resolve, reject) { | |
194 // Unmount the mounted archievea | |
195 volumeManager.volumeInfoList.addEventListener('splice', function(e) { | |
196 assertEquals(numberOfVolumes, volumeManager.volumeInfoList.length); | |
197 resolve(true); | |
198 }); | |
199 var entry = new MockFileEntry( | |
200 createMockFileSystem('archive:foobar.zip'), | |
201 '/foo.txt'); | |
202 var volumeInfo = volumeManager.getVolumeInfo(entry); | |
203 volumeManager.unmount(volumeInfo); | |
204 }); | |
205 }); | |
206 }), callback); | |
207 } | |
208 | |
209 function testGetCurrentProfileVolumeInfo(callback) { | |
210 reportPromise(VolumeManager.getInstance().then(function(volumeManager) { | |
211 var volumeInfo = volumeManager.getCurrentProfileVolumeInfo( | |
212 VolumeManagerCommon.VolumeType.DRIVE); | |
213 | |
214 assertEquals('drive:drive-foobar%40chromium.org-hash', | |
215 volumeInfo.volumeId); | |
216 assertEquals(VolumeManagerCommon.VolumeType.DRIVE, volumeInfo.volumeType); | |
217 }), callback); | |
218 } | |
219 | |
220 function testGetLocationInfo(callback) { | |
221 reportPromise(VolumeManager.getInstance().then(function(volumeManager) { | |
222 var downloadEntry = new MockFileEntry( | |
223 createMockFileSystem('download:Downloads'), | |
224 '/foo/bar/bla.zip'); | |
225 var downloadLocationInfo = volumeManager.getLocationInfo(downloadEntry); | |
226 assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS, | |
227 downloadLocationInfo.rootType) | |
228 assertFalse(downloadLocationInfo.isReadOnly); | |
229 assertFalse(downloadLocationInfo.isRootEntry); | |
230 | |
231 var driveEntry = new MockFileEntry( | |
232 createMockFileSystem('drive:drive-foobar%40chromium.org-hash'), | |
233 '/root'); | |
234 var driveLocationInfo = volumeManager.getLocationInfo(driveEntry); | |
235 assertEquals(VolumeManagerCommon.VolumeType.DRIVE, | |
236 driveLocationInfo.rootType) | |
237 assertFalse(driveLocationInfo.isReadOnly); | |
238 assertTrue(driveLocationInfo.isRootEntry); | |
239 }), callback); | |
240 } | |
OLD | NEW |