Commit 98c8d48f authored by Naoki Fukino's avatar Naoki Fukino Committed by Commit Bot

[Files] Add unit tests for FileTypeFiltersController.

Adding unit tests to evaluate the behavior of FileTypeFiltersController
class, which was added with CL:2354115.

Bug: 1114721
Test: Ran browser_tests --gtest_filter=*FileTypeFiltersController
Change-Id: I9cc1cc8379fb35de909202d3043bda6d5304a024
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2359314Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Commit-Queue: Naoki Fukino <fukino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798530}
parent 39dd8685
......@@ -143,6 +143,10 @@ IN_PROC_BROWSER_TEST_F(FileManagerJsTest, FileTransferController) {
RunTestURL("foreground/js/file_transfer_controller_unittest_gen.html");
}
IN_PROC_BROWSER_TEST_F(FileManagerJsTest, FileTypeFiltersController) {
RunTestURL("foreground/js/file_type_filters_controller_unittest_gen.html");
}
IN_PROC_BROWSER_TEST_F(FileManagerJsTest, FileType) {
RunTestURL("common/js/file_type_unittest_gen.html");
}
......
......@@ -497,6 +497,13 @@ js_library("file_type_filters_controller") {
]
}
js_unittest("file_type_filters_controller_unittest") {
deps = [
":file_type_filters_controller",
"//ui/webui/resources/js:webui_resource_test",
]
}
js_library("file_watcher") {
deps = [
"//ui/file_manager/base/js:volume_manager_types",
......@@ -856,6 +863,7 @@ js_test_gen_html("js_test_gen_html") {
":file_manager_commands_unittest",
":file_tasks_unittest",
":file_transfer_controller_unittest",
":file_type_filters_controller_unittest",
":import_controller_unittest",
":list_thumbnail_loader_unittest",
":navigation_list_model_unittest",
......
// Copyright 2020 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.
/**
* @type {!HTMLElement}
*/
let container;
/**
* @type {!DirectoryModel}
*/
let directoryModel;
/**
* @type {!FakeEntry}
*/
let recentEntry;
/**
* @type {!EntryList}
*/
let myFilesEntry;
/**
* @type {!FileTypeFiltersController}
*/
let fileTypeFiltersController;
function setUp() {
// Mock loadTimeData strings.
window.loadTimeData.data = {
MEDIA_VIEW_AUDIO_ROOT_LABEL: 'Audio',
MEDIA_VIEW_IMAGES_ROOT_LABEL: 'Images',
MEDIA_VIEW_VIDEOS_ROOT_LABEL: 'Videos',
};
class MockDirectoryModel extends cr.EventTarget {
constructor() {
super();
this.currentDirEntry = null;
window.isRescanCalled = false;
}
rescan(refresh) {
window.isRescanCalled = true;
}
changeDirectoryEntry(dirEntry) {
// Change the directory model's current directory to |dirEntry|.
const previousDirEntry = this.currentDirEntry;
this.currentDirEntry = dirEntry;
// Emit 'directory-changed' event synchronously to simplify testing.
const event = new Event('directory-changed');
event.previousDirEntry = previousDirEntry;
event.newDirEntry = this.currentDirEntry;
this.dispatchEvent(event);
}
static create() {
const model = /** @type {!Object} */ (new MockDirectoryModel());
return /** @type {!DirectoryModel} */ (model);
}
}
// Create FileTypeFiltersController instance with dependencies.
container = /** @type {!HTMLInputElement} */ (document.createElement('div'));
directoryModel = MockDirectoryModel.create();
recentEntry = new FakeEntry(
'Recent', VolumeManagerCommon.RootType.RECENT,
chrome.fileManagerPrivate.SourceRestriction.ANY_SOURCE);
fileTypeFiltersController =
new FileTypeFiltersController(container, directoryModel, recentEntry);
// Create a directory entry which is not Recents to simulate directory change.
myFilesEntry =
new EntryList('My Files', VolumeManagerCommon.RootType.MY_FILES);
}
/**
* Tests that creating FileTypeFiltersController generates three buttons in the
* given container element.
*/
function testCreatedButtonLabels() {
const buttons = container.children;
assertEquals(buttons.length, 3);
assertEquals(buttons[0].textContent, 'Audio');
assertEquals(buttons[1].textContent, 'Images');
assertEquals(buttons[2].textContent, 'Videos');
}
/**
* Tests that initial states of all buttons inside container are inactive.
*/
function testButtonInitialActiveState() {
const buttons = container.children;
assertEquals(buttons.length, 3);
assertFalse(buttons[0].classList.contains('active'));
assertFalse(buttons[0].classList.contains('active'));
assertFalse(buttons[0].classList.contains('active'));
}
/**
* Tests that click events toggle button state (inactive -> active -> inactive).
*/
function testButtonToggleState() {
const buttons = container.children;
assertEquals(buttons.length, 3);
assertFalse(buttons[0].classList.contains('active'));
buttons[0].click();
assertTrue(buttons[0].classList.contains('active'));
buttons[0].click();
assertFalse(buttons[0].classList.contains('active'));
}
/**
* Tests that only one button can be inactive.
* If button_1 is clicked then button_0 is active, button_0 becomes inactive and
* button_1 becomes active.
*/
function testOnlyOneButtonCanActive() {
const buttons = container.children;
assertEquals(buttons.length, 3);
assertFalse(buttons[0].classList.contains('active'));
buttons[0].click();
assertTrue(buttons[0].classList.contains('active'));
assertFalse(buttons[1].classList.contains('active'));
assertFalse(buttons[2].classList.contains('active'));
buttons[1].click();
assertFalse(buttons[0].classList.contains('active'));
assertTrue(buttons[1].classList.contains('active'));
assertFalse(buttons[2].classList.contains('active'));
buttons[2].click();
assertFalse(buttons[0].classList.contains('active'));
assertFalse(buttons[1].classList.contains('active'));
assertTrue(buttons[2].classList.contains('active'));
}
/**
* Tests that container element is visible only when the current directory is
* Recents view.
*/
function testContainerIsShownOnlyInRecents() {
container.hidden = true;
directoryModel.changeDirectoryEntry(recentEntry);
assertFalse(container.hidden);
directoryModel.changeDirectoryEntry(myFilesEntry);
assertTrue(container.hidden);
}
/**
* Tests that button's active state is reset to inactive when the user leaves
* Recents view.
*/
function testActiveButtonIsResetOnLeavingRecents() {
const buttons = container.children;
assertEquals(buttons.length, 3);
directoryModel.changeDirectoryEntry(recentEntry);
buttons[0].click();
assertTrue(buttons[0].classList.contains('active'));
assertFalse(buttons[1].classList.contains('active'));
assertFalse(buttons[2].classList.contains('active'));
directoryModel.changeDirectoryEntry(myFilesEntry);
assertFalse(buttons[0].classList.contains('active'));
assertFalse(buttons[1].classList.contains('active'));
assertFalse(buttons[2].classList.contains('active'));
}
/**
* Tests that the active state of each button is reflected to the Recent entry's
* recentFileType property, and DirectoryModel.rescan() is called after the
* Recent entry's property is modified.
*/
function testAppliedFilters() {
const buttons = container.children;
assertEquals(buttons.length, 3);
directoryModel.changeDirectoryEntry(recentEntry);
buttons[0].click();
assertEquals(
recentEntry.recentFileType,
chrome.fileManagerPrivate.RecentFileType.AUDIO);
assertTrue(window.isRescanCalled);
window.isRescanCalled = false;
buttons[1].click();
assertEquals(
recentEntry.recentFileType,
chrome.fileManagerPrivate.RecentFileType.IMAGE);
assertTrue(window.isRescanCalled);
window.isRescanCalled = false;
buttons[2].click();
assertEquals(
recentEntry.recentFileType,
chrome.fileManagerPrivate.RecentFileType.VIDEO);
assertTrue(window.isRescanCalled);
window.isRescanCalled = false;
buttons[2].click();
assertEquals(
recentEntry.recentFileType, chrome.fileManagerPrivate.RecentFileType.ALL);
assertTrue(window.isRescanCalled);
window.isRescanCalled = false;
}
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