Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2540)

Unified Diff: chrome/test/data/file_manager/unit_tests/volume_manager_unittest.js

Issue 663513003: Add an unittest for background/volume_manager.js (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added comments. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a2e18d230f4d0946dc0d6741f55256d4080ef2b5
--- /dev/null
+++ b/chrome/test/data/file_manager/unit_tests/volume_manager_unittest.js
@@ -0,0 +1,253 @@
+// 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;
+
+function initialize_() {
+ // Set up string assets.
+ loadTimeData.data = {
+ DRIVE_DIRECTORY_LABEL: 'My Drive',
+ DOWNLOADS_DIRECTORY_LABEL: 'Downloads'
+ };
+
+ // 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);
+ }
+ }
+ };
+
+ // Initialize states of mock fileManagerPrivate APIs.
+ // States of fileManagerPrivate APIs are initialized only once through the all
+ // tests since volume manager uses singleton model, and it register events in
+ // initialize phase, so we need to keep registered event listeners.
+ 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: getSampleProfile()
+ },
+ {
+ volumeId: 'drive:drive-foobar%40chromium.org-hash',
+ volumeLabel: '',
+ volumeType: VolumeManagerCommon.VolumeType.DRIVE,
+ isReadOnly: false,
+ profile: getSampleProfile()
+ }
+ ];
+ chrome.fileManagerPrivate.fileSystemMap_ = {
+ 'download:Downloads': new MockFileSystem('download:Downloads'),
+ 'drive:drive-foobar%40chromium.org-hash':
+ new MockFileSystem('drive:drive-foobar%40chromium.org-hash')
+ };
+
+ initialized = true;
+}
+
+function setUp() {
+ 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
+ initialize_();
+ }
+}
+
+/**
+ * Returns a sample profile.
hirono 2014/10/24 07:12:22 Please add @return
yawano 2014/10/27 00:25:08 Done.
+ */
+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.
+ return {
+ displayName: 'foobar@chromium.org',
+ isCurrentProfile: true,
+ profileId: ''
+ };
+}
+
+function testGetVolumeInfo(callback) {
+ VolumeManager.getInstance().then(function(volumeManager) {
+ var entry = new MockFileEntry(new MockFileSystem('download:Downloads'),
+ '/foo/bar/bla.zip');
+
+ var volumeInfo = volumeManager.getVolumeInfo(entry);
+ assertEquals('download:Downloads', volumeInfo.volumeId);
+ assertEquals(VolumeManagerCommon.VolumeType.DOWNLOADS,
+ volumeInfo.volumeType);
+
+ callback(false);
+ }).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.
+ console.log(error.stack || error);
+ callback(true);
+ });
+}
+
+function testGetDriveConnectionState(callback) {
+ VolumeManager.getInstance().then(function(volumeManager) {
+ // Default connection state is online
+ assertEquals('online', volumeManager.getDriveConnectionState());
+
+ // Sets it to offline.
+ chrome.fileManagerPrivate.driveConnectionState =
+ VolumeManagerCommon.DriveConnectionType.OFFLINE;
+ assertEquals('offline', volumeManager.getDriveConnectionState());
+
+ // Sets it back to online
+ chrome.fileManagerPrivate.driveConnectionState =
+ VolumeManagerCommon.DriveConnectionType.ONLINE;
+ assertEquals('online', volumeManager.getDriveConnectionState());
+
+ callback(false);
+ }).catch (function(error) {
+ console.log(error.stack || error);
+ callback(true);
+ });
+}
+
+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'] =
+ new MockFileSystem('archive:foobar.zip');
+
+ var numberOfVolumes;
+ var volumeManager;
+
+ VolumeManager.getInstance().then(function(volumeManager_) {
+ volumeManager = volumeManager_;
+ 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: getSampleProfile()
+ }
+ });
+ });
+ }).then(function(result) {
+ assertEquals(numberOfVolumes + 1, volumeManager.volumeInfoList.length);
+
+ // Unmount the mounted archievea
+ volumeManager.volumeInfoList.addEventListener('splice', function(e) {
+ assertEquals(numberOfVolumes, volumeManager.volumeInfoList.length);
+ callback(false);
+ });
+ var entry = new MockFileEntry(new MockFileSystem('archive:foobar.zip'),
+ '/foo.txt');
+ var volumeInfo = volumeManager.getVolumeInfo(entry);
+ volumeManager.unmount(volumeInfo);
+ }).catch (function(error) {
+ console.log(error.stack || error);
+ callback(true);
+ });
+}
+
+function testGetCurrentProfileVolumeInfo(callback) {
+ 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(false);
+ }).catch (function(error) {
+ console.log(error.stack || error);
+ callback(true);
+ });
+}
+
+function testGetLocationInfo(callback) {
+ VolumeManager.getInstance().then(function(volumeManager) {
+ var entry1 = new MockFileEntry(new MockFileSystem('download:Downloads'),
+ '/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(
+ new MockFileSystem('drive:drive-foobar%40chromium.org-hash'),
+ '/root');
+ var locationInfo2 = volumeManager.getLocationInfo(entry2);
+ assertEquals(VolumeManagerCommon.VolumeType.DRIVE, locationInfo2.rootType)
+ assertFalse(locationInfo2.isReadOnly);
+ assertTrue(locationInfo2.isRootEntry);
+
+ callback(false);
+ }).catch (function(error) {
+ console.log(error.stack || error);
+ callback(true);
+ });
+}

Powered by Google App Engine
This is Rietveld 408576698