Commit b78a1594 authored by Jesse Schettler's avatar Jesse Schettler Committed by Commit Bot

scanning: Sort resolutions in resolution dropdown

Sort the resolutions in descending order and correctly select the
default option.

Bug: 1059779
Change-Id: If971e961b8174aec7d538cd21ca5ef0eada0f2c7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2533395Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Commit-Queue: Jesse Schettler <jschettler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826571}
parent 74d6858f
......@@ -44,12 +44,12 @@ export function resolutionSelectTest() {
assertFalse(select.disabled);
assertEquals(2, select.length);
assertEquals(
firstResolution.toString() + ' dpi',
secondResolution.toString() + ' dpi',
select.options[0].textContent.trim());
assertEquals(
secondResolution.toString() + ' dpi',
firstResolution.toString() + ' dpi',
select.options[1].textContent.trim());
assertEquals(firstResolution.toString(), select.value);
assertEquals(secondResolution.toString(), select.value);
// Selecting a different option should update the selected value.
return changeSelect(
......@@ -80,4 +80,27 @@ export function resolutionSelectTest() {
assertEquals(2, select.length);
assertFalse(select.disabled);
});
test('resolutionsSortedCorrectly', () => {
resolutionSelect.resolutions = [150, 300, 75, 600, 1200, 200];
flush();
// Verify the resolutions are sorted in descending order and that 300 is
// selected by default.
for (let i = 0; i < resolutionSelect.resolutions.length - 1; i++) {
assert(
resolutionSelect.resolutions[i] >
resolutionSelect.resolutions[i + 1]);
}
assertEquals('300', resolutionSelect.selectedResolution);
});
test('firstResolutionUsedWhenDefaultNotAvailable', () => {
resolutionSelect.resolutions = [150, 75, 600, 1200, 200];
flush();
// Verify the first resolution in the sorted resolution array is selected by
// default when 300 is not an available option.
assertEquals('1200', resolutionSelect.selectedResolution);
});
}
......@@ -8,7 +8,8 @@
disabled="[[disabled]]">
<!-- TODO(jschettler): Sort the resolutions. -->
<template is="dom-repeat" items="[[resolutions]]" as="resolution">
<option value="[[resolution]]">
<option value="[[resolution]]"
selected$="[[isDefaultResolution_(resolution)]]">
[[getResolutionString_(resolution)]]
</option>
</template>
......
......@@ -12,6 +12,9 @@ import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bun
import {SelectBehavior} from './select_behavior.js';
/** @type {number} */
const DEFAULT_RESOLUTION = 300;
/**
* @fileoverview
* 'resolution-select' displays the available scan resolutions in a dropdown.
......@@ -37,7 +40,10 @@ Polymer({
},
},
observers: ['onNumOptionsChange(resolutions.length)'],
observers: [
'onNumOptionsChange(resolutions.length)',
'onResolutionsChange_(resolutions.*)'
],
/**
* @param {number} resolution
......@@ -48,4 +54,47 @@ Polymer({
return loadTimeData.getStringF(
'resolutionOptionText', resolution.toString());
},
/**
* 300 dpi should be the default option if it exists. If not, use the first
* resolution in the resolutions array.
* @return {string}
* @private
*/
getDefaultSelectedResolution_() {
const defaultResolutionIndex = this.resolutions.findIndex((resolution) => {
return this.isDefaultResolution_(resolution);
});
return defaultResolutionIndex === -1 ?
this.resolutions[0].toString() :
this.resolutions[defaultResolutionIndex].toString();
},
/**
* Sorts the resolutions and sets the selected resolution when the resolutions
* array changes.
* @private
*/
onResolutionsChange_() {
if (this.resolutions.length > 1) {
// Sort the resolutions in descending order.
this.resolutions.sort(function(a, b) {
return b - a;
});
}
if (this.resolutions.length > 0) {
this.selectedResolution = this.getDefaultSelectedResolution_();
}
},
/**
* @param {number} resolution
* @return {boolean}
* @private
*/
isDefaultResolution_(resolution) {
return resolution === DEFAULT_RESOLUTION;
},
});
......@@ -232,7 +232,6 @@ Polymer({
this.selectedColorMode = this.capabilities_.colorModes[0].toString();
this.selectedPageSize =
this.capabilities_.sources[0].pageSizes[0].toString();
this.selectedResolution = this.capabilities_.resolutions[0].toString();
// TODO(jschettler): Change default file type back to PDF when it's
// supported.
......
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