Commit 592b47eb authored by yawano's avatar yawano Committed by Commit bot

Add an unittest for background/volume_manager.js

BUG=315436
TEST=out/Release/browser_tests --gtest_filter=FileManagerJsTest.*

Review URL: https://codereview.chromium.org/663513003

Cr-Commit-Position: refs/heads/master@{#302230}
parent 340ba206
......@@ -65,3 +65,8 @@ IN_PROC_BROWSER_TEST_F(FileManagerJsTest, ImportHistoryTest) {
RunTest(base::FilePath(
FILE_PATH_LITERAL("import_history_unittest.html")));
}
IN_PROC_BROWSER_TEST_F(FileManagerJsTest, VolumeManagerTest) {
RunTest(base::FilePath(
FILE_PATH_LITERAL("volume_manager_unittest.html")));
}
......@@ -77,7 +77,7 @@ var DIRECTORY_SIZE = -1;
* size. If the size is equals to DIRECTORY_SIZE, the entry is derectory.
*/
function createTestFileSystem(id, entries) {
var fileSystem = new TestFileSystem(id);
var fileSystem = new MockFileSystem(id, 'filesystem:' + id);
for (var path in entries) {
if (entries[path] === DIRECTORY_SIZE) {
fileSystem.entries[path] = new MockDirectoryEntry(fileSystem, path);
......
......@@ -21,7 +21,7 @@ var GOOGLE_DRIVE = 'Google Drive';
*/
var SPACE_CLOUD = 'Space Cloud';
/** @type {!TestFileSystem|undefined} */
/** @type {!MockFileSystem|undefined} */
var testFileSystem;
/** @type {!MockFileEntry|undefined} */
......@@ -35,7 +35,7 @@ var historyLoader;
// Set up the test components.
function setUp() {
testFileSystem = new TestFileSystem('abc-123');
testFileSystem = new MockFileSystem('abc-123', 'filesystem:abc-123');
testFileEntry = new MockFileEntry(
testFileSystem,
FILE_PATH, {
......
......@@ -13,17 +13,19 @@ function joinPath(a, b) {
};
/**
* Test file system.
* Mock class for DOMFileSystem.
*
* @param {string} fileSystemId File system ID.
* @param {string} volumeId Volume ID.
* @param {string} rootURL URL string of root which is used in MockEntry.toURL.
* @constructor
*/
function TestFileSystem(fileSystemId) {
this.fileSystemId = fileSystemId;
function MockFileSystem(volumeId, rootURL) {
this.name = volumeId;
this.entries = {};
this.rootURL = rootURL;
};
TestFileSystem.prototype = {
MockFileSystem.prototype = {
get root() { return this.entries['/']; }
};
......@@ -54,7 +56,7 @@ MockEntry.prototype = {
* @return {string} Fake URL.
*/
MockEntry.prototype.toURL = function() {
return 'filesystem:' + this.filesystem.fileSystemId + this.fullPath;
return this.filesystem.rootURL + this.fullPath;
};
/**
......@@ -212,3 +214,23 @@ MockDirectoryEntry.prototype.getDirectory =
else
onSuccess(this.filesystem.entries[fullPath]);
};
/**
* Creates a MockDirectoryReader for the entry.
*/
MockDirectoryEntry.prototype.createReader = function() {
return new MockDirectoryReader();
}
/**
* Mock class for DirectoryReader.
*/
function MockDirectoryReader() {}
/**
* Reads entries.
* Current implementation just calls success callback.
*/
MockDirectoryReader.prototype.readEntries = function(success, error) {
success();
}
// 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.
/**
* Mock class for DOMFileSystem.
* @param {string} volumeId Volume ID for the file system.
* @constructor
*/
function MockFileSystem(volumeId) {
this.name = '';
this.root = new MockFileEntry(volumeId, '/');
}
......@@ -36,7 +36,8 @@ MockVolumeManager.prototype.getVolumeInfo = function(entry) {
* @return {VolumeInfo} Created mock VolumeInfo.
*/
MockVolumeManager.createMockVolumeInfo = function(type, volumeId) {
var fileSystem = new MockFileSystem(volumeId);
var fileSystem = new MockFileSystem(volumeId, 'filesystem:' + volumeId);
fileSystem.entries['/'] = new MockDirectoryEntry(fileSystem, '/');
var volumeInfo = new VolumeInfo(
type,
......
......@@ -21,7 +21,6 @@
<script src="../../../../../ui/file_manager/file_manager/foreground/js/navigation_list_model.js"></script>
<script src="mocks/mock_entry.js"></script>
<script src="mocks/mock_file_system.js"></script>
<script src="mocks/mock_volume_manager.js"></script>
<script src="mocks/mock_folder_shortcut_data_model.js"></script>
......
// 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.
/**
* 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.
*/
function reportPromise(promise, callback) {
promise.then(function() {
callback(/* error */ false);
}, function(error) {
console.error(error.stack || error);
callback(/* error */ true);
});
}
<!DOCTYPE html>
<!-- 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.
-->
<html>
<body>
<script src="../../../../../ui/webui/resources/js/cr.js"></script>
<script src="../../../../../ui/webui/resources/js/cr/event_target.js"></script>
<script src="../../../../../ui/webui/resources/js/cr/ui/array_data_model.js"></script>
<script src="../../../../../ui/webui/resources/js/load_time_data.js"></script>
<script src="../../../../../ui/file_manager/file_manager/common/js/async_util.js"></script>
<script src="../../../../../ui/file_manager/file_manager/common/js/util.js"></script>
<script src="../../../../../ui/file_manager/file_manager/common/js/volume_manager_common.js"></script>
<script src="../../../../../ui/file_manager/file_manager/background/js/volume_manager.js"></script>
<script src="test_util.js"></script>
<script src="mocks/mock_entry.js"></script>
<script src="volume_manager_unittest.js"></script>
</body>
</html>
// 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);
}
......@@ -502,13 +502,6 @@ VolumeManager.prototype.__proto__ = cr.EventTarget.prototype;
*/
VolumeManager.TIMEOUT = 15 * 60 * 1000;
/**
* Queue to run getInstance sequentially.
* @type {AsyncUtil.Queue}
* @private
*/
VolumeManager.getInstanceQueue_ = new AsyncUtil.Queue();
/**
* The singleton instance of VolumeManager. Initialized by the first invocation
* of getInstance().
......@@ -554,6 +547,14 @@ VolumeManager.getInstance = function(opt_callback) {
return VolumeManager.instancePromise_;
};
/**
* Revokes the singleton instance for testing.
*/
VolumeManager.revokeInstanceForTesting = function() {
VolumeManager.instancePromise_ = null;
VolumeManager.instance_ = null;
}
/**
* Initializes mount points.
* @param {function()} callback Called upon the completion of the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment