Commit 13f9a519 authored by yoshiki@chromium.org's avatar yoshiki@chromium.org

[Files.app] Add an unittest of NavigationListModel

This patch adds an unittest framework for JavaScript components. In addition, this also adds an unit test of NavigationListModel using the framework.

BUG=309946
TEST=browser_test passes
R=hashimoto@chromium.org, mtomasz@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@232892 0039d316-1c4b-4281-b951-d872f2087c98
parent c4f51d24
// Copyright 2013 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.
#include <vector>
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
class FileManagerJsTest : public InProcessBrowserTest {
public:
// Runs all test functions in |file|, waiting for them to complete.
void RunTest(const base::FilePath& file) {
GURL url = ui_test_utils::GetTestUrl(
base::FilePath(FILE_PATH_LITERAL("file_manager/unit_tests")), file);
ui_test_utils::NavigateToURL(browser(), url);
content::RenderViewHost* rvh = browser()->tab_strip_model()
->GetActiveWebContents()->GetRenderViewHost();
ASSERT_TRUE(rvh);
const std::vector<int> empty_libraries;
EXPECT_TRUE(ExecuteWebUIResourceTest(rvh, empty_libraries));
}
};
IN_PROC_BROWSER_TEST_F(
FileManagerJsTest, NavigationListModelTest) {
RunTest(base::FilePath(
FILE_PATH_LITERAL("navigation_list_model_unittest.html")));
}
...@@ -1042,6 +1042,7 @@ ...@@ -1042,6 +1042,7 @@
'browser/chromeos/file_manager/drive_test_util.h', 'browser/chromeos/file_manager/drive_test_util.h',
'browser/chromeos/file_manager/external_filesystem_apitest.cc', 'browser/chromeos/file_manager/external_filesystem_apitest.cc',
'browser/chromeos/file_manager/file_manager_browsertest.cc', 'browser/chromeos/file_manager/file_manager_browsertest.cc',
'browser/chromeos/file_manager/file_manager_jstest.cc',
'browser/chromeos/input_method/input_method_engine_ibus_browserttests.cc', 'browser/chromeos/input_method/input_method_engine_ibus_browserttests.cc',
'browser/chromeos/kiosk_mode/mock_kiosk_mode_settings.cc', 'browser/chromeos/kiosk_mode/mock_kiosk_mode_settings.cc',
'browser/chromeos/kiosk_mode/mock_kiosk_mode_settings.h', 'browser/chromeos/kiosk_mode/mock_kiosk_mode_settings.h',
......
// Copyright 2013 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 FileEntry.
* @constructor
*/
function MockFileEntry() {
this.fullPath = null;
}
// Copyright 2013 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 FolderShortcutDataModel.
* @param {...string} var_args List of the initial shortcuts.
* @extends {cr.ui.ArrayDataModel}
* @constructor
*/
function MockFolderShortcutDataModel(var_args) {
cr.ui.ArrayDataModel.apply(this, arguments);
}
MockFolderShortcutDataModel.prototype = {
__proto__: cr.ui.ArrayDataModel.prototype
};
/**
* Mock function for FolderShortcutDataModel.compare().
* @param {string} a First parameter to be compared.
* @param {string} b Second parameter to be compared with.
* @return {number} Negative if a < b, positive if a > b, or zero if a == b.
*/
MockFolderShortcutDataModel.prototype.compare = function(a, b) {
return a.localeCompare(b);
};
// Copyright 2013 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 VolumeManager.
* @constructor
*/
function MockVolumeManager() {
this.volumeInfoList = new cr.ui.ArrayDataModel([]);
this.volumeInfoList.push(MockVolumeManager.createMockVolumeInfo(
util.VolumeType.DRIVE, '/drive'));
this.volumeInfoList.push(MockVolumeManager.createMockVolumeInfo(
util.VolumeType.DOWNLOADS, '/Downloads'));
}
/**
* Returns the corresponding VolumeInfo.
* @param {string} mountPath Path to be looking for with.
* @return {VolumeInfo} Corresponding VolumeInfo.
*/
MockVolumeManager.prototype.getVolumeInfo = function(mountPath) {
for (var i = 0; i < this.volumeInfoList.length; i++) {
if (this.volumeInfoList.item(i).mountPath === mountPath)
return this.volumeInfoList.item(i);
}
return null;
};
/**
* Resolve FileEntry.
* @param {string} path
* @param {function(FileEntry)} successCallback Callback on success.
* @param {function()} errorCallback Callback on error.
*/
MockVolumeManager.prototype.resolvePath = function(
path, successCallback, errorCallback) {
var mockFileEntry = new MockFileEntry();
mockFileEntry.fullPath = path;
successCallback(mockFileEntry);
};
/**
* Utility function to create a mock VolumeInfo.
* @param {VolumeType} type Volume type.
* @param {string} path Volume path.
* @return {VolumeInfo} Created mock VolumeInfo.
*/
MockVolumeManager.createMockVolumeInfo = function(type, path) {
var entry = new MockFileEntry();
entry.fullPath = path;
var volumeInfo = new VolumeInfo(
type,
path,
entry, // Directory entry.
'', // error
'', // deviceType
false); // readonly
return volumeInfo;
};
<!DOCTYPE html>
<!-- Copyright 2013 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/ui.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="../../../../browser/resources/file_manager/common/js/async_util.js"></script>
<script src="../../../../browser/resources/file_manager/common/js/path_util.js"></script>
<script src="../../../../browser/resources/file_manager/common/js/util.js"></script>
<script src="../../../../browser/resources/file_manager/background/js/volume_manager.js"></script>
<script src="../../../../browser/resources/file_manager/foreground/js/navigation_list_model.js"></script>
<script src="mocks/mock_file_entry.js"></script>
<script src="mocks/mock_volume_manager.js"></script>
<script src="mocks/mock_folder_shortcut_data_model.js"></script>
<script src="navigation_list_model_unittest.js"></script>
</body>
</html>
// Copyright 2013 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.
function testModel() {
var volumeManager = new MockVolumeManager();
var shortcutListModel =
new MockFolderShortcutDataModel(['/drive/root/shortcut']);
var model = new NavigationListModel(volumeManager, shortcutListModel);
assertEquals(3, model.length);
assertEquals('/drive/root', model.item(0).path);
assertEquals('/Downloads', model.item(1).path);
assertEquals('/drive/root/shortcut', model.item(2).path);
}
function testAddAndRemoveShortcuts() {
var volumeManager = new MockVolumeManager();
var shortcutListModel =
new MockFolderShortcutDataModel(['/drive/root/shortcut']);
var model = new NavigationListModel(volumeManager, shortcutListModel);
assertEquals(3, model.length);
// Add a shortcut at the tail.
shortcutListModel.splice(1, 0, '/drive/root/shortcut2');
assertEquals(4, model.length);
assertEquals('/drive/root/shortcut2', model.item(3).path);
// Add a shortcut at the head.
shortcutListModel.splice(0, 0, '/drive/root/hoge');
assertEquals(5, model.length);
assertEquals('/drive/root/hoge', model.item(2).path);
assertEquals('/drive/root/shortcut', model.item(3).path);
assertEquals('/drive/root/shortcut2', model.item(4).path);
// Remove the last shortcut.
shortcutListModel.splice(2, 1);
assertEquals(4, model.length);
assertEquals('/drive/root/hoge', model.item(2).path);
assertEquals('/drive/root/shortcut', model.item(3).path);
// Remove the first shortcut.
shortcutListModel.splice(0, 1);
assertEquals(3, model.length);
assertEquals('/drive/root/shortcut', model.item(2).path);
}
function testAddAndRemoveVolumes() {
var volumeManager = new MockVolumeManager();
var shortcutListModel =
new MockFolderShortcutDataModel(['/drive/root/shortcut']);
var model = new NavigationListModel(volumeManager, shortcutListModel);
assertEquals(3, model.length);
// Removable volume 'hoge' is mounted.
volumeManager.volumeInfoList.push(MockVolumeManager.createMockVolumeInfo(
util.VolumeType.REMOVABLE, '/removable/hoge'));
assertEquals(4, model.length);
assertEquals('/drive/root', model.item(0).path);
assertEquals('/Downloads', model.item(1).path);
assertEquals('/removable/hoge', model.item(2).path);
assertEquals('/drive/root/shortcut', model.item(3).path);
// Removable volume 'fuga' is mounted.
volumeManager.volumeInfoList.push(MockVolumeManager.createMockVolumeInfo(
util.VolumeType.REMOVABLE, '/removable/fuga'));
assertEquals(5, model.length);
assertEquals('/drive/root', model.item(0).path);
assertEquals('/Downloads', model.item(1).path);
assertEquals('/removable/hoge', model.item(2).path);
assertEquals('/removable/fuga', model.item(3).path);
assertEquals('/drive/root/shortcut', model.item(4).path);
// A shortcut is created on the 'hoge' volume.
shortcutListModel.splice(1, 0, '/removable/hoge/shortcut2');
assertEquals(6, model.length);
assertEquals('/drive/root', model.item(0).path);
assertEquals('/Downloads', model.item(1).path);
assertEquals('/removable/hoge', model.item(2).path);
assertEquals('/removable/fuga', model.item(3).path);
assertEquals('/drive/root/shortcut', model.item(4).path);
assertEquals('/removable/hoge/shortcut2', model.item(5).path);
// The 'hoge' is unmounted. A shortcut on 'hoge' is removed.
volumeManager.volumeInfoList.splice(2, 1);
assertEquals(4, model.length);
assertEquals('/drive/root', model.item(0).path);
assertEquals('/Downloads', model.item(1).path);
assertEquals('/removable/fuga', model.item(2).path);
assertEquals('/drive/root/shortcut', model.item(3).path);
// The Drive is unmounted. A shortcut on the Drive is removed.
volumeManager.volumeInfoList.splice(0, 1);
assertEquals(2, model.length);
assertEquals('/Downloads', model.item(0).path);
assertEquals('/removable/fuga', model.item(1).path);
}
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