Chromium Code Reviews| 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 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: getSampleProfile() | |
| 89 }, | |
| 90 { | |
| 91 volumeId: 'drive:drive-foobar%40chromium.org-hash', | |
| 92 volumeLabel: '', | |
| 93 volumeType: VolumeManagerCommon.VolumeType.DRIVE, | |
| 94 isReadOnly: false, | |
| 95 profile: getSampleProfile() | |
| 96 } | |
| 97 ]; | |
| 98 chrome.fileManagerPrivate.fileSystemMap_ = { | |
| 99 'download:Downloads': new MockFileSystem('download:Downloads'), | |
| 100 'drive:drive-foobar%40chromium.org-hash': | |
| 101 new MockFileSystem('drive:drive-foobar%40chromium.org-hash') | |
| 102 }; | |
| 103 | |
| 104 initialized = true; | |
| 105 } | |
| 106 | |
| 107 function setUp() { | |
| 108 if(!initialized) { | |
|
hirono
2014/10/24 07:12:22
Is this check is needed?
yawano
2014/10/27 00:25:08
Yes, if we initialize twice or more, event handler
hirono
2014/10/27 09:50:12
You are right.
I'd like to have VolumeManager.revo
yawano
2014/10/29 04:07:23
I agree with you in order to keep independency bet
| |
| 109 initialize_(); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 /** | |
| 114 * Returns a sample profile. | |
|
hirono
2014/10/24 07:12:22
Please add @return
yawano
2014/10/27 00:25:08
Done.
| |
| 115 */ | |
| 116 function getSampleProfile() { | |
|
hirono
2014/10/24 07:12:22
The word "sample" does not sound familiar in our c
yawano
2014/10/27 00:25:08
Done.
| |
| 117 return { | |
| 118 displayName: 'foobar@chromium.org', | |
| 119 isCurrentProfile: true, | |
| 120 profileId: '' | |
| 121 }; | |
| 122 } | |
| 123 | |
| 124 function testGetVolumeInfo(callback) { | |
| 125 VolumeManager.getInstance().then(function(volumeManager) { | |
| 126 var entry = new MockFileEntry(new MockFileSystem('download:Downloads'), | |
| 127 '/foo/bar/bla.zip'); | |
| 128 | |
| 129 var volumeInfo = volumeManager.getVolumeInfo(entry); | |
| 130 assertEquals('download:Downloads', volumeInfo.volumeId); | |
| 131 assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS, | |
| 132 volumeInfo.volumeType); | |
| 133 | |
| 134 callback(false); | |
| 135 }).catch (function(error) { | |
|
hirono
2014/10/24 07:12:22
We usually don't add a space after 'catch' if it i
yawano
2014/10/27 00:25:08
Done.
| |
| 136 console.log(error.stack || error); | |
| 137 callback(true); | |
| 138 }); | |
| 139 } | |
| 140 | |
| 141 function testGetDriveConnectionState(callback) { | |
| 142 VolumeManager.getInstance().then(function(volumeManager) { | |
| 143 // Default connection state is online | |
| 144 assertEquals('online', volumeManager.getDriveConnectionState()); | |
| 145 | |
| 146 // Sets it to offline. | |
| 147 chrome.fileManagerPrivate.driveConnectionState = | |
| 148 VolumeManagerCommon.DriveConnectionType.OFFLINE; | |
| 149 assertEquals('offline', volumeManager.getDriveConnectionState()); | |
| 150 | |
| 151 // Sets it back to online | |
| 152 chrome.fileManagerPrivate.driveConnectionState = | |
| 153 VolumeManagerCommon.DriveConnectionType.ONLINE; | |
| 154 assertEquals('online', volumeManager.getDriveConnectionState()); | |
| 155 | |
| 156 callback(false); | |
| 157 }).catch (function(error) { | |
| 158 console.log(error.stack || error); | |
| 159 callback(true); | |
| 160 }); | |
| 161 } | |
| 162 | |
| 163 function testMountArchiveAndUnmount(callback) { | |
| 164 // Set states of mock fileManagerPrivate APIs. | |
| 165 const mountSourcePath = '/usr/local/home/test/Downloads/foobar.zip'; | |
| 166 chrome.fileManagerPrivate.mountSourcePath_ = mountSourcePath; | |
| 167 chrome.fileManagerPrivate.fileSystemMap_['archive:foobar.zip'] = | |
| 168 new MockFileSystem('archive:foobar.zip'); | |
| 169 | |
| 170 var numberOfVolumes; | |
| 171 var volumeManager; | |
| 172 | |
| 173 VolumeManager.getInstance().then(function(volumeManager_) { | |
| 174 volumeManager = volumeManager_; | |
| 175 numberOfVolumes = volumeManager.volumeInfoList.length; | |
| 176 | |
| 177 return new Promise(function(resolve, reject) { | |
| 178 // Mount an archieve | |
| 179 volumeManager.mountArchive( | |
| 180 'filesystem:chrome-extension://extensionid/external/Downloads-test/' + | |
| 181 'foobar.zip', | |
| 182 resolve, reject); | |
| 183 | |
| 184 chrome.fileManagerPrivate.onMountCompleted.dispatchEvent( | |
| 185 {eventType: 'mount', | |
| 186 status: 'success', | |
| 187 volumeMetadata: { | |
| 188 volumeId: 'archive:foobar.zip', | |
| 189 volumeLabel: 'foobar.zip', | |
| 190 volumeType: VolumeManagerCommon.VolumeType.ARCHIVE, | |
| 191 isReadOnly: true, | |
| 192 sourcePath: mountSourcePath, | |
| 193 profile: getSampleProfile() | |
| 194 } | |
| 195 }); | |
| 196 }); | |
| 197 }).then(function(result) { | |
| 198 assertEquals(numberOfVolumes + 1, volumeManager.volumeInfoList.length); | |
| 199 | |
| 200 // Unmount the mounted archievea | |
| 201 volumeManager.volumeInfoList.addEventListener('splice', function(e) { | |
| 202 assertEquals(numberOfVolumes, volumeManager.volumeInfoList.length); | |
| 203 callback(false); | |
| 204 }); | |
| 205 var entry = new MockFileEntry(new MockFileSystem('archive:foobar.zip'), | |
| 206 '/foo.txt'); | |
| 207 var volumeInfo = volumeManager.getVolumeInfo(entry); | |
| 208 volumeManager.unmount(volumeInfo); | |
| 209 }).catch (function(error) { | |
| 210 console.log(error.stack || error); | |
| 211 callback(true); | |
| 212 }); | |
| 213 } | |
| 214 | |
| 215 function testGetCurrentProfileVolumeInfo(callback) { | |
| 216 VolumeManager.getInstance().then(function(volumeManager) { | |
| 217 var volumeInfo = volumeManager.getCurrentProfileVolumeInfo( | |
| 218 VolumeManagerCommon.VolumeType.DRIVE); | |
| 219 | |
| 220 assertEquals('drive:drive-foobar%40chromium.org-hash', volumeInfo.volumeId); | |
| 221 assertEquals(VolumeManagerCommon.VolumeType.DRIVE, volumeInfo.volumeType); | |
| 222 | |
| 223 callback(false); | |
| 224 }).catch (function(error) { | |
| 225 console.log(error.stack || error); | |
| 226 callback(true); | |
| 227 }); | |
| 228 } | |
| 229 | |
| 230 function testGetLocationInfo(callback) { | |
| 231 VolumeManager.getInstance().then(function(volumeManager) { | |
| 232 var entry1 = new MockFileEntry(new MockFileSystem('download:Downloads'), | |
| 233 '/foo/bar/bla.zip'); | |
| 234 var locationInfo1 = volumeManager.getLocationInfo(entry1); | |
| 235 assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS, | |
| 236 locationInfo1.rootType) | |
| 237 assertFalse(locationInfo1.isReadOnly); | |
| 238 assertFalse(locationInfo1.isRootEntry); | |
| 239 | |
| 240 var entry2 = new MockFileEntry( | |
| 241 new MockFileSystem('drive:drive-foobar%40chromium.org-hash'), | |
| 242 '/root'); | |
| 243 var locationInfo2 = volumeManager.getLocationInfo(entry2); | |
| 244 assertEquals(VolumeManagerCommon.VolumeType.DRIVE, locationInfo2.rootType) | |
| 245 assertFalse(locationInfo2.isReadOnly); | |
| 246 assertTrue(locationInfo2.isRootEntry); | |
| 247 | |
| 248 callback(false); | |
| 249 }).catch (function(error) { | |
| 250 console.log(error.stack || error); | |
| 251 callback(true); | |
| 252 }); | |
| 253 } | |
| OLD | NEW |