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..f2e8bb690f553fa3e4663a0471d347bb082a6c57 |
--- /dev/null |
+++ b/chrome/test/data/file_manager/unit_tests/volume_manager_unittest.js |
@@ -0,0 +1,240 @@ |
+// 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; |
+ |
+loadTimeData.data = { |
+ DRIVE_DIRECTORY_LABEL: 'My Drive', |
+ DOWNLOADS_DIRECTORY_LABEL: 'Downloads' |
+}; |
+ |
+function setUp() { |
+ // 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) { |
+ 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) { |
+ 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); |
+ }), callback); |
+} |
+ |
+function testGetDriveConnectionState(callback) { |
+ reportPromise(VolumeManager.getInstance().then(function(volumeManager) { |
+ // 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()); |
+ }), 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'); |
+ |
+ reportPromise(VolumeManager.getInstance().then(function(volumeManager) { |
+ var numberOfVolumes = volumeManager.volumeInfoList.length; |
+ |
+ return new Promise(function(resolve, reject) { |
+ // Mount an archieve |
+ volumeManager.mountArchive( |
+ 'filesystem:chrome-extension://extensionid/external/Downloads-test/' + |
+ 'foobar.zip', |
+ resolve, reject); |
+ |
+ chrome.fileManagerPrivate.onMountCompleted.dispatchEvent({ |
+ eventType: 'mount', |
+ 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) { |
+ var volumeInfo = volumeManager.getCurrentProfileVolumeInfo( |
+ VolumeManagerCommon.VolumeType.DRIVE); |
+ |
+ assertEquals('drive:drive-foobar%40chromium.org-hash', |
+ volumeInfo.volumeId); |
+ assertEquals(VolumeManagerCommon.VolumeType.DRIVE, volumeInfo.volumeType); |
+ }), callback); |
+} |
+ |
+function testGetLocationInfo(callback) { |
+ reportPromise(VolumeManager.getInstance().then(function(volumeManager) { |
+ var downloadEntry = new MockFileEntry( |
+ createMockFileSystem('download:Downloads'), |
+ '/foo/bar/bla.zip'); |
+ var downloadLocationInfo = volumeManager.getLocationInfo(downloadEntry); |
+ assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS, |
+ downloadLocationInfo.rootType) |
+ assertFalse(downloadLocationInfo.isReadOnly); |
+ assertFalse(downloadLocationInfo.isRootEntry); |
+ |
+ var driveEntry = new MockFileEntry( |
+ createMockFileSystem('drive:drive-foobar%40chromium.org-hash'), |
+ '/root'); |
+ var driveLocationInfo = volumeManager.getLocationInfo(driveEntry); |
+ assertEquals(VolumeManagerCommon.VolumeType.DRIVE, |
+ driveLocationInfo.rootType) |
+ assertFalse(driveLocationInfo.isReadOnly); |
+ assertTrue(driveLocationInfo.isRootEntry); |
+ }), callback); |
+} |