Chromium Code Reviews| Index: chrome/test/data/file_manager/unit_tests/volume_manager_unittest.js |
| diff --git a/chrome/test/data/file_manager/unit_tests/volume_manager_unittest.js b/chrome/test/data/file_manager/unit_tests/volume_manager_unittest.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6db159099e582f5d020c6f7dc99854105fbe1c53 |
| --- /dev/null |
| +++ b/chrome/test/data/file_manager/unit_tests/volume_manager_unittest.js |
| @@ -0,0 +1,284 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +var chrome; |
| +var initialized_ = false; |
| + |
| +/** |
| + * Invokes a callback function depending on the result of promise. |
| + * |
| + * @param {Promise} promise Promise. |
| + * @param {function(boolean)} calllback Callback function. True is passed if the |
| + * 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.
|
| + */ |
| +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.
|
| + promise.then(function() { |
| + callback(/* error */ false); |
| + }, function(error) { |
| + console.error(error.stack || error); |
| + callback(/* error */ true); |
| + }); |
| +} |
| + |
| +function initialize_() { |
| + // It is allowed only once to set loadTimeData.data. |
| + 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.
|
| + DRIVE_DIRECTORY_LABEL: 'My Drive', |
| + DOWNLOADS_DIRECTORY_LABEL: 'Downloads' |
| + }; |
| + |
| + initialized_ = true; |
| +} |
| + |
| +function setUp() { |
| + // Set up string assets. |
| + if(!initialized_) { |
| + initialize_(); |
| + } |
| + |
| + // Set up mock of chrome.fileManagerPrivate APIs. |
| + chrome = { |
| + fileManagerPrivate: { |
| + mountSourcePath_: null, |
| + onMountCompletedListeners_: [], |
| + onDriveConnectionStatusChangedListeners_: [], |
| + addMount: function(fileUrl, callback) { |
| + callback(chrome.fileManagerPrivate.mountSourcePath_); |
| + }, |
| + removeMount: function(volumeId) { |
| + var event = { |
| + eventType: 'unmount', |
| + status: 'success', |
| + volumeMetadata: { |
| + volumeId: volumeId |
| + } |
| + }; |
| + chrome.fileManagerPrivate.onMountCompleted.dispatchEvent(event); |
| + }, |
| + onDriveConnectionStatusChanged: { |
| + addListener: function(listener) { |
| + chrome.fileManagerPrivate.onDriveConnectionStatusChangedListeners_ |
| + .push(listener); |
| + }, |
| + dispatchEvent: function(event) { |
| + chrome.fileManagerPrivate |
| + .onDriveConnectionStatusChangedListeners_ |
| + .forEach(function(listener) { listener(event); }); |
| + } |
| + }, |
| + onMountCompleted: { |
| + addListener: function(listener) { |
| + chrome.fileManagerPrivate.onMountCompletedListeners_.push(listener); |
| + }, |
| + dispatchEvent: function(event) { |
| + chrome.fileManagerPrivate |
| + .onMountCompletedListeners_.forEach(function(listener) { |
| + listener(event); |
| + }); |
| + } |
| + }, |
| + getDriveConnectionState: function(callback) { |
| + callback(chrome.fileManagerPrivate.driveConnectionState_); |
| + }, |
| + getVolumeMetadataList: function(callback) { |
| + callback(chrome.fileManagerPrivate.volumeMetadataList_); |
| + }, |
| + requestFileSystem: function(volumeId, callback) { |
| + callback(chrome.fileManagerPrivate.fileSystemMap_[volumeId]); |
| + }, |
| + 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.
|
| + chrome.fileManagerPrivate.driveConnectionState_ = state; |
| + chrome.fileManagerPrivate.onDriveConnectionStatusChanged |
| + .dispatchEvent(null); |
| + } |
| + } |
| + }; |
| + |
| + chrome.fileManagerPrivate.mountSourcePath_ = null; |
| + chrome.fileManagerPrivate.onMountCompletedListeners_ = []; |
| + chrome.fileManagerPrivate.onDriveConnectionStatusChangedListeners_ = []; |
| + chrome.fileManagerPrivate.driveConnectionState_ = |
| + VolumeManagerCommon.DriveConnectionType.ONLINE; |
| + chrome.fileManagerPrivate.volumeMetadataList_ = [ |
| + { |
| + volumeId: 'download:Downloads', |
| + volumeLabel: '', |
| + volumeType: VolumeManagerCommon.VolumeType.DOWNLOADS, |
| + isReadOnly: false, |
| + profile: getMockProfile() |
| + }, |
| + { |
| + volumeId: 'drive:drive-foobar%40chromium.org-hash', |
| + volumeLabel: '', |
| + volumeType: VolumeManagerCommon.VolumeType.DRIVE, |
| + isReadOnly: false, |
| + profile: getMockProfile() |
| + } |
| + ]; |
| + chrome.fileManagerPrivate.fileSystemMap_ = { |
| + 'download:Downloads': createMockFileSystem('download:Downloads'), |
| + 'drive:drive-foobar%40chromium.org-hash': |
| + createMockFileSystem('drive:drive-foobar%40chromium.org-hash') |
| + }; |
| +} |
| + |
| +function tearDown() { |
| + VolumeManager.revokeInstanceForTesting(); |
| + chrome = null; |
| +} |
| + |
| +/** |
| + * Returns a mock profile. |
| + * |
| + * @return {{displayName:string, isCurrentProfile:boolean, profileId:string}} |
| + * Mock profile |
| + */ |
| +function getMockProfile() { |
| + return { |
| + displayName: 'foobar@chromium.org', |
| + isCurrentProfile: true, |
| + profileId: '' |
| + }; |
| +} |
| + |
| +/** |
| + * Creates a mock file system. |
| + * |
| + * @return {MockFileSystem} A mock file system. |
| + */ |
| +function createMockFileSystem(volumeId) { |
| + var fileSystem = new MockFileSystem(volumeId, 'filesystem:' + volumeId); |
| + fileSystem.entries['/'] = new MockDirectoryEntry(fileSystem, '/'); |
| + return fileSystem; |
| +} |
| + |
| +function testGetVolumeInfo(callback) { |
| + reportPromise(VolumeManager.getInstance().then(function(volumeManager) { |
| + 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
|
| + var entry = new MockFileEntry(createMockFileSystem('download:Downloads'), |
| + '/foo/bar/bla.zip'); |
| + |
| + var volumeInfo = volumeManager.getVolumeInfo(entry); |
| + assertEquals('download:Downloads', volumeInfo.volumeId); |
| + assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS, |
| + volumeInfo.volumeType); |
| + |
| + resolve(true); |
| + }); |
| + }), callback); |
| +} |
| + |
| +function testGetDriveConnectionState(callback) { |
| + reportPromise(VolumeManager.getInstance().then(function(volumeManager) { |
| + 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.
|
| + // Default connection state is online |
| + assertEquals(VolumeManagerCommon.DriveConnectionType.ONLINE, |
| + volumeManager.getDriveConnectionState()); |
| + |
| + // Sets it to offline. |
| + chrome.fileManagerPrivate.driveConnectionState = |
| + VolumeManagerCommon.DriveConnectionType.OFFLINE; |
| + assertEquals(VolumeManagerCommon.DriveConnectionType.OFFLINE, |
| + volumeManager.getDriveConnectionState()); |
| + |
| + // Sets it back to online |
| + chrome.fileManagerPrivate.driveConnectionState = |
| + VolumeManagerCommon.DriveConnectionType.ONLINE; |
| + assertEquals(VolumeManagerCommon.DriveConnectionType.ONLINE, |
| + volumeManager.getDriveConnectionState()); |
| + |
| + resolve(true); |
| + }); |
| + }), callback); |
| +} |
| + |
| +function testMountArchiveAndUnmount(callback) { |
| + // Set states of mock fileManagerPrivate APIs. |
| + const mountSourcePath = '/usr/local/home/test/Downloads/foobar.zip'; |
| + chrome.fileManagerPrivate.mountSourcePath_ = mountSourcePath; |
| + chrome.fileManagerPrivate.fileSystemMap_['archive:foobar.zip'] = |
| + createMockFileSystem('archive:foobar.zip'); |
| + |
| + var numberOfVolumes; |
| + var volumeManager; |
| + |
| + 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
|
| + volumeManager = volumeManager_; |
| + numberOfVolumes = volumeManager.volumeInfoList.length; |
| + |
| + return new Promise(function(resolve, reject) { |
| + // Mount an archieve |
| + volumeManager.mountArchive( |
| + '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.
|
| + 'foobar.zip', |
| + resolve, reject); |
| + |
| + chrome.fileManagerPrivate.onMountCompleted.dispatchEvent( |
| + {eventType: 'mount', |
|
hirono
2014/10/29 10:02:46
We usually do either:
xxxxxxxxxxxxxxxx({
eventT
yawano
2014/10/30 09:11:47
Done.
|
| + status: 'success', |
| + volumeMetadata: { |
| + volumeId: 'archive:foobar.zip', |
| + volumeLabel: 'foobar.zip', |
| + volumeType: VolumeManagerCommon.VolumeType.ARCHIVE, |
| + isReadOnly: true, |
| + sourcePath: mountSourcePath, |
| + profile: getMockProfile() |
| + } |
| + }); |
| + }); |
| + }).then(function(result) { |
| + assertEquals(numberOfVolumes + 1, volumeManager.volumeInfoList.length); |
| + |
| + return new Promise(function(resolve, reject) { |
| + // Unmount the mounted archievea |
| + volumeManager.volumeInfoList.addEventListener('splice', function(e) { |
| + assertEquals(numberOfVolumes, volumeManager.volumeInfoList.length); |
| + resolve(true); |
| + }); |
| + var entry = new MockFileEntry(createMockFileSystem('archive:foobar.zip'), |
| + '/foo.txt'); |
| + var volumeInfo = volumeManager.getVolumeInfo(entry); |
| + volumeManager.unmount(volumeInfo); |
| + }); |
| + }), callback); |
| +} |
| + |
| +function testGetCurrentProfileVolumeInfo(callback) { |
| + reportPromise(VolumeManager.getInstance().then(function(volumeManager) { |
| + 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.
|
| + var volumeInfo = volumeManager.getCurrentProfileVolumeInfo( |
| + VolumeManagerCommon.VolumeType.DRIVE); |
| + |
| + assertEquals('drive:drive-foobar%40chromium.org-hash', |
| + volumeInfo.volumeId); |
| + assertEquals(VolumeManagerCommon.VolumeType.DRIVE, volumeInfo.volumeType); |
| + |
| + resolve(true); |
| + }); |
| + }), callback); |
| +} |
| + |
| +function testGetLocationInfo(callback) { |
| + reportPromise(VolumeManager.getInstance().then(function(volumeManager) { |
| + 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.
|
| + 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.
|
| + '/foo/bar/bla.zip'); |
| + var locationInfo1 = volumeManager.getLocationInfo(entry1); |
| + assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS, |
| + locationInfo1.rootType) |
| + assertFalse(locationInfo1.isReadOnly); |
| + assertFalse(locationInfo1.isRootEntry); |
| + |
| + var entry2 = new MockFileEntry( |
| + createMockFileSystem('drive:drive-foobar%40chromium.org-hash'), |
| + '/root'); |
| + var locationInfo2 = volumeManager.getLocationInfo(entry2); |
| + assertEquals(VolumeManagerCommon.VolumeType.DRIVE, locationInfo2.rootType) |
| + assertFalse(locationInfo2.isReadOnly); |
| + assertTrue(locationInfo2.isRootEntry); |
| + |
| + resolve(true); |
| + }); |
| + }), callback); |
| +} |