Commit 79f876ff authored by Gavin Williams's avatar Gavin Williams Committed by Commit Bot

scanning: Add test select change event function

Create a function for simulating changing a select element in tests.

Bug: 1059779
Change-Id: Ia67242113682c12860a2b59bbf370ad0ebbcc21c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2522120
Commit-Queue: Gavin Williams <gavinwill@chromium.org>
Reviewed-by: default avatarJesse Schettler <jschettler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824974}
parent aa94869c
...@@ -8,6 +8,8 @@ import {flush} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min ...@@ -8,6 +8,8 @@ import {flush} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min
import {assertEquals, assertFalse, assertTrue} from '../../chai_assert.js'; import {assertEquals, assertFalse, assertTrue} from '../../chai_assert.js';
import {changeSelect} from './scanning_app_test_utils.js';
const FileType = { const FileType = {
JPG: chromeos.scanning.mojom.FileType.kJpg, JPG: chromeos.scanning.mojom.FileType.kJpg,
PDF: chromeos.scanning.mojom.FileType.kPdf, PDF: chromeos.scanning.mojom.FileType.kPdf,
...@@ -33,7 +35,8 @@ export function fileTypeSelectTest() { ...@@ -33,7 +35,8 @@ export function fileTypeSelectTest() {
test('initializeFileTypeSelect', () => { test('initializeFileTypeSelect', () => {
// The dropdown should be initialized as enabled with three options. The // The dropdown should be initialized as enabled with three options. The
// default option should be PNG. // default option should be PNG.
const select = fileTypeSelect.$$('select'); const select =
/** @type {!HTMLSelectElement} */ (fileTypeSelect.$$('select'));
assertTrue(!!select); assertTrue(!!select);
assertFalse(select.disabled); assertFalse(select.disabled);
assertEquals(3, select.length); assertEquals(3, select.length);
...@@ -43,10 +46,11 @@ export function fileTypeSelectTest() { ...@@ -43,10 +46,11 @@ export function fileTypeSelectTest() {
assertEquals(FileType.PNG.toString(), select.value); assertEquals(FileType.PNG.toString(), select.value);
// Selecting a different option should update the selected value. // Selecting a different option should update the selected value.
select.value = FileType.JPG.toString(); return changeSelect(
select.dispatchEvent(new CustomEvent('change')); select, FileType.JPG.toString(), /* selectedIndex */ null)
flush(); .then(() => {
assertEquals(
assertEquals(FileType.JPG.toString(), fileTypeSelect.selectedFileType); FileType.JPG.toString(), fileTypeSelect.selectedFileType);
});
}); });
} }
...@@ -9,6 +9,8 @@ import {getPageSizeString} from 'chrome://scanning/scanning_app_util.js'; ...@@ -9,6 +9,8 @@ import {getPageSizeString} from 'chrome://scanning/scanning_app_util.js';
import {assertEquals, assertFalse, assertTrue} from '../../chai_assert.js'; import {assertEquals, assertFalse, assertTrue} from '../../chai_assert.js';
import {changeSelect} from './scanning_app_test_utils.js';
const PageSize = { const PageSize = {
A4: chromeos.scanning.mojom.PageSize.kIsoA4, A4: chromeos.scanning.mojom.PageSize.kIsoA4,
Letter: chromeos.scanning.mojom.PageSize.kNaLetter, Letter: chromeos.scanning.mojom.PageSize.kNaLetter,
...@@ -33,7 +35,8 @@ export function pageSizeSelectTest() { ...@@ -33,7 +35,8 @@ export function pageSizeSelectTest() {
test('initializePageSizeSelect', () => { test('initializePageSizeSelect', () => {
// Before options are added, the dropdown should be disabled and empty. // Before options are added, the dropdown should be disabled and empty.
const select = pageSizeSelect.$$('select'); const select =
/** @type {!HTMLSelectElement} */ (pageSizeSelect.$$('select'));
assertTrue(!!select); assertTrue(!!select);
assertTrue(select.disabled); assertTrue(select.disabled);
assertEquals(0, select.length); assertEquals(0, select.length);
...@@ -55,11 +58,12 @@ export function pageSizeSelectTest() { ...@@ -55,11 +58,12 @@ export function pageSizeSelectTest() {
assertEquals(firstPageSize.toString(), select.value); assertEquals(firstPageSize.toString(), select.value);
// Selecting a different option should update the selected value. // Selecting a different option should update the selected value.
select.value = secondPageSize.toString(); return changeSelect(
select.dispatchEvent(new CustomEvent('change')); select, secondPageSize.toString(), /* selectedIndex */ null)
flush(); .then(() => {
assertEquals(
assertEquals(secondPageSize.toString(), pageSizeSelect.selectedPageSize); secondPageSize.toString(), pageSizeSelect.selectedPageSize);
});
}); });
test('pageSizeSelectDisabled', () => { test('pageSizeSelectDisabled', () => {
......
...@@ -8,6 +8,8 @@ import {flush} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min ...@@ -8,6 +8,8 @@ import {flush} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min
import {assertEquals, assertFalse, assertTrue} from '../../chai_assert.js'; import {assertEquals, assertFalse, assertTrue} from '../../chai_assert.js';
import {changeSelect} from './scanning_app_test_utils.js';
export function resolutionSelectTest() { export function resolutionSelectTest() {
/** @type {?ResolutionSelectElement} */ /** @type {?ResolutionSelectElement} */
let resolutionSelect = null; let resolutionSelect = null;
...@@ -26,7 +28,8 @@ export function resolutionSelectTest() { ...@@ -26,7 +28,8 @@ export function resolutionSelectTest() {
test('initializeResolutionSelect', () => { test('initializeResolutionSelect', () => {
// Before options are added, the dropdown should be disabled and empty. // Before options are added, the dropdown should be disabled and empty.
const select = resolutionSelect.$$('select'); const select =
/** @type {!HTMLSelectElement} */ (resolutionSelect.$$('select'));
assertTrue(!!select); assertTrue(!!select);
assertTrue(select.disabled); assertTrue(select.disabled);
assertEquals(0, select.length); assertEquals(0, select.length);
...@@ -49,12 +52,12 @@ export function resolutionSelectTest() { ...@@ -49,12 +52,12 @@ export function resolutionSelectTest() {
assertEquals(firstResolution.toString(), select.value); assertEquals(firstResolution.toString(), select.value);
// Selecting a different option should update the selected value. // Selecting a different option should update the selected value.
select.value = secondResolution; return changeSelect(
select.dispatchEvent(new CustomEvent('change')); select, secondResolution.toString(), /* selectedIndex */ null)
flush(); .then(() => {
assertEquals(
assertEquals( secondResolution.toString(), resolutionSelect.selectedResolution);
secondResolution.toString(), resolutionSelect.selectedResolution); });
}); });
test('resolutionSelectDisabled', () => { test('resolutionSelectDisabled', () => {
......
...@@ -7,8 +7,8 @@ import 'chrome://scanning/scan_to_select.js'; ...@@ -7,8 +7,8 @@ import 'chrome://scanning/scan_to_select.js';
import {ScanningBrowserProxyImpl} from 'chrome://scanning/scanning_browser_proxy.js'; import {ScanningBrowserProxyImpl} from 'chrome://scanning/scanning_browser_proxy.js';
import {assertEquals, assertFalse, assertTrue} from '../../chai_assert.js'; import {assertEquals, assertFalse, assertTrue} from '../../chai_assert.js';
import {flushTasks} from '../../test_util.m.js';
import {changeSelect} from './scanning_app_test_utils.js';
import {TestScanningBrowserProxy} from './test_scanning_browser_proxy.js'; import {TestScanningBrowserProxy} from './test_scanning_browser_proxy.js';
export function scanToSelectTest() { export function scanToSelectTest() {
...@@ -58,10 +58,9 @@ export function scanToSelectTest() { ...@@ -58,10 +58,9 @@ export function scanToSelectTest() {
// Simulate clicking the 'Select folder' option. // Simulate clicking the 'Select folder' option.
scanningBrowserProxy.setSelectedPath( scanningBrowserProxy.setSelectedPath(
{baseName: myDownloads, filePath: myDownloadsPath}); {baseName: myDownloads, filePath: myDownloadsPath});
const select = scanToSelect.$$('select'); const select =
select.selectedIndex = 1; /** @type {!HTMLSelectElement} */ (scanToSelect.$$('select'));
select.dispatchEvent(new CustomEvent('change')); return changeSelect(select, /* value */ null, /* selectedIndex */ 1)
return flushTasks()
.then(() => { .then(() => {
assertEquals(myDownloadsPath, scanToSelect.selectedFilePath); assertEquals(myDownloadsPath, scanToSelect.selectedFilePath);
assertEquals( assertEquals(
...@@ -71,9 +70,7 @@ export function scanToSelectTest() { ...@@ -71,9 +70,7 @@ export function scanToSelectTest() {
scanningBrowserProxy.setSelectedPath( scanningBrowserProxy.setSelectedPath(
{baseName: googleDrive, filePath: googleDrivePath}); {baseName: googleDrive, filePath: googleDrivePath});
select.selectedIndex = 1; return changeSelect(select, /* value */ null, /* selectedIndex */ 1);
select.dispatchEvent(new CustomEvent('change'));
return flushTasks();
}) })
.then(() => { .then(() => {
assertEquals(googleDrivePath, scanToSelect.selectedFilePath); assertEquals(googleDrivePath, scanToSelect.selectedFilePath);
...@@ -93,10 +90,9 @@ export function scanToSelectTest() { ...@@ -93,10 +90,9 @@ export function scanToSelectTest() {
// Simulate clicking the 'Select folder' option. // Simulate clicking the 'Select folder' option.
scanningBrowserProxy.setSelectedPath( scanningBrowserProxy.setSelectedPath(
{baseName: myDownloads, filePath: myDownloadsPath}); {baseName: myDownloads, filePath: myDownloadsPath});
const select = scanToSelect.$$('select'); const select =
select.selectedIndex = 1; /** @type {!HTMLSelectElement} */ (scanToSelect.$$('select'));
select.dispatchEvent(new CustomEvent('change')); return changeSelect(select, /* value */ null, /* selectedIndex */ 1)
return flushTasks()
.then(() => { .then(() => {
assertEquals(myDownloadsPath, scanToSelect.selectedFilePath); assertEquals(myDownloadsPath, scanToSelect.selectedFilePath);
assertEquals( assertEquals(
...@@ -106,9 +102,7 @@ export function scanToSelectTest() { ...@@ -106,9 +102,7 @@ export function scanToSelectTest() {
// Simulate canceling the select dialog // Simulate canceling the select dialog
scanningBrowserProxy.setSelectedPath({baseName: '', filePath: ''}); scanningBrowserProxy.setSelectedPath({baseName: '', filePath: ''});
select.selectedIndex = 1; return changeSelect(select, /* value */ null, /* selectedIndex */ 1);
select.dispatchEvent(new CustomEvent('change'));
return flushTasks();
}) })
.then(() => { .then(() => {
assertEquals(myDownloadsPath, scanToSelect.selectedFilePath); assertEquals(myDownloadsPath, scanToSelect.selectedFilePath);
......
...@@ -12,7 +12,7 @@ import {tokenToString} from 'chrome://scanning/scanning_app_util.js'; ...@@ -12,7 +12,7 @@ import {tokenToString} from 'chrome://scanning/scanning_app_util.js';
import {assertEquals, assertFalse, assertTrue} from '../../chai_assert.js'; import {assertEquals, assertFalse, assertTrue} from '../../chai_assert.js';
import {flushTasks} from '../../test_util.m.js'; import {flushTasks} from '../../test_util.m.js';
import {createScanner, createScannerSource} from './scanning_app_test_utils.js'; import {changeSelect, createScanner, createScannerSource} from './scanning_app_test_utils.js';
const ColorMode = { const ColorMode = {
BLACK_AND_WHITE: chromeos.scanning.mojom.ColorMode.kBlackAndWhite, BLACK_AND_WHITE: chromeos.scanning.mojom.ColorMode.kBlackAndWhite,
...@@ -248,8 +248,34 @@ export function scanningAppTest() { ...@@ -248,8 +248,34 @@ export function scanningAppTest() {
capabilities.set(firstScannerId, firstCapabilities); capabilities.set(firstScannerId, firstCapabilities);
capabilities.set(secondScannerId, secondCapabilities); capabilities.set(secondScannerId, secondCapabilities);
/** @type {!HTMLSelectElement} */
let scannerSelect;
/** @type {!HTMLSelectElement} */
let sourceSelect;
/** @type {!HTMLSelectElement} */
let fileTypeSelect;
/** @type {!HTMLSelectElement} */
let colorModeSelect;
/** @type {!HTMLSelectElement} */
let pageSizeSelect;
/** @type {!HTMLSelectElement} */
let resolutionSelect;
/** @type {!CrButtonElement} */
let scanButton;
/** @type {!Element} */
let statusText;
return initializeScanningApp(expectedScanners, capabilities) return initializeScanningApp(expectedScanners, capabilities)
.then(() => { .then(() => {
scannerSelect = scanningApp.$$('#scannerSelect').$$('select');
sourceSelect = scanningApp.$$('#sourceSelect').$$('select');
fileTypeSelect = scanningApp.$$('#fileTypeSelect').$$('select');
colorModeSelect = scanningApp.$$('#colorModeSelect').$$('select');
pageSizeSelect = scanningApp.$$('#pageSizeSelect').$$('select');
resolutionSelect = scanningApp.$$('#resolutionSelect').$$('select');
scanButton =
/** @type {!CrButtonElement} */ (scanningApp.$$('#scanButton'));
statusText = /** @type {!Element} */ (scanningApp.$$('#statusText'));
return fakeScanService_.whenCalled('getScannerCapabilities'); return fakeScanService_.whenCalled('getScannerCapabilities');
}) })
.then(() => { .then(() => {
...@@ -270,29 +296,15 @@ export function scanningAppTest() { ...@@ -270,29 +296,15 @@ export function scanningAppTest() {
// Before the scan button is clicked, the settings and scan button // Before the scan button is clicked, the settings and scan button
// should be enabled, and there should be no scan status. // should be enabled, and there should be no scan status.
const scannerSelect = scanningApp.$$('#scannerSelect').$$('select');
assertFalse(scannerSelect.disabled); assertFalse(scannerSelect.disabled);
const sourceSelect = scanningApp.$$('#sourceSelect').$$('select');
assertFalse(sourceSelect.disabled); assertFalse(sourceSelect.disabled);
const fileTypeSelect = scanningApp.$$('#fileTypeSelect').$$('select');
assertFalse(fileTypeSelect.disabled); assertFalse(fileTypeSelect.disabled);
const colorModeSelect =
scanningApp.$$('#colorModeSelect').$$('select');
assertFalse(colorModeSelect.disabled); assertFalse(colorModeSelect.disabled);
const pageSizeSelect = scanningApp.$$('#pageSizeSelect').$$('select');
assertFalse(pageSizeSelect.disabled); assertFalse(pageSizeSelect.disabled);
const resolutionSelect =
scanningApp.$$('#resolutionSelect').$$('select');
assertFalse(resolutionSelect.disabled); assertFalse(resolutionSelect.disabled);
const scanButton = scanningApp.$$('#scanButton');
assertFalse(scanButton.disabled); assertFalse(scanButton.disabled);
const statusText = scanningApp.$$('#statusText');
assertEquals('', statusText.textContent.trim()); assertEquals('', statusText.textContent.trim());
// PNG is currently the only supported file type.
fileTypeSelect.value = FileType.PNG.toString();
fileTypeSelect.dispatchEvent(new CustomEvent('change'));
flush();
scanButton.click(); scanButton.click();
// After the scan button is clicked, the settings and scan button // After the scan button is clicked, the settings and scan button
...@@ -312,17 +324,16 @@ export function scanningAppTest() { ...@@ -312,17 +324,16 @@ export function scanningAppTest() {
// After scanning is complete, the settings and scan button should be // After scanning is complete, the settings and scan button should be
// enabled, and the scan status should indicate that scanning is // enabled, and the scan status should indicate that scanning is
// complete. // complete.
assertFalse(scanningApp.$$('#scannerSelect').$$('select').disabled); assertFalse(scannerSelect.disabled);
assertFalse(scanningApp.$$('#sourceSelect').$$('select').disabled); assertFalse(sourceSelect.disabled);
assertFalse(scanningApp.$$('#fileTypeSelect').$$('select').disabled); assertFalse(fileTypeSelect.disabled);
assertFalse(scanningApp.$$('#colorModeSelect').$$('select').disabled); assertFalse(colorModeSelect.disabled);
assertFalse(scanningApp.$$('#pageSizeSelect').$$('select').disabled); assertFalse(pageSizeSelect.disabled);
assertFalse( assertFalse(resolutionSelect.disabled);
scanningApp.$$('#resolutionSelect').$$('select').disabled); assertFalse(scanButton.disabled);
assertFalse(scanningApp.$$('#scanButton').disabled);
assertEquals( assertEquals(
'Scan complete! File(s) saved to /home/chronos/user/MyFiles.', 'Scan complete! File(s) saved to /home/chronos/user/MyFiles.',
scanningApp.$$('#statusText').textContent.trim()); statusText.textContent.trim());
}); });
}); });
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import {flushTasks} from '../../test_util.m.js';
/** /**
* @param {!mojoBase.mojom.UnguessableToken} id * @param {!mojoBase.mojom.UnguessableToken} id
* @param {string} displayName * @param {string} displayName
...@@ -27,11 +29,28 @@ export function createScannerSource(type, name, pageSizes) { ...@@ -27,11 +29,28 @@ export function createScannerSource(type, name, pageSizes) {
* @param {string} str * @param {string} str
* @return {!mojoBase.mojom.String16} * @return {!mojoBase.mojom.String16}
*/ */
export function strToMojoString16(str) { function strToMojoString16(str) {
let arr = []; let arr = [];
for (var i = 0; i < str.length; i++) { for (var i = 0; i < str.length; i++) {
arr[i] = str.charCodeAt(i); arr[i] = str.charCodeAt(i);
} }
return {data: arr}; return {data: arr};
} }
\ No newline at end of file
/**
* @param {!HTMLSelectElement} select
* @param {?string} value
* @param {?number} selectedIndex
* @return {!Promise}
*/
export function changeSelect(select, value, selectedIndex) {
if (value) {
select.value = value;
}
if (selectedIndex) {
select.selectedIndex = selectedIndex;
}
select.dispatchEvent(new CustomEvent('change'));
return flushTasks();
}
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