Commit b3af0cfa authored by Gavin Williams's avatar Gavin Williams Committed by Commit Bot

Update status icon on printer status change

Add function to monitor the destinations dialog's destinations
printerStatusReason for changes then update to the appropriate
PrinterState. Needed since printer statuses are allowed to change after
the dialog is opened.

Bug: 1123754
Change-Id: I869aaf91543f710398d8125e7b449bd5ca438813
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2406675Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Commit-Queue: Gavin Williams <gavinwill@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808231}
parent 6f707829
......@@ -77,7 +77,7 @@
</iron-icon>
<if expr="chromeos">
<printer-status-icon-cros icon-location="[[iconLocation_.DROPDOWN]]"
hidden="[[!isDestinationCrosLocal_]]">
printer-state="[[printerState_]]" hidden="[[!isDestinationCrosLocal_]]">
</printer-status-icon-cros>
</if>
<span class="name searchable">[[destination.displayName]]</span>
......
......@@ -21,7 +21,7 @@ import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bun
import {Destination, DestinationOrigin} from '../data/destination.js';
// <if expr="chromeos">
import {IconLocation} from '../data/printer_status_cros.js';
import {computePrinterState, IconLocation, PrinterState, PrinterStatusReason} from '../data/printer_status_cros.js';
// </if>
import {updateHighlights} from './highlight_utils.js';
......@@ -97,6 +97,12 @@ Polymer({
},
readOnly: true,
},
/** @private */
printerState_: {
type: Number,
value: PrinterState.UNKNOWN,
}
// </if>
},
......@@ -105,6 +111,9 @@ Polymer({
'destination.displayName, destination.isOfflineOrInvalid, ' +
'destination.isExtension)',
'updateHighlightsAndHint_(destination, searchQuery)',
// <if expr="chromeos">
'requestPrinterStatus_(destination.key)',
// </if>
],
/** @private {!Array<!Node>} */
......@@ -205,4 +214,32 @@ Polymer({
this.destination.origin === DestinationOrigin.CROS;
// </if>
},
// <if expr="chromeos">
/** @private */
requestPrinterStatus_() {
if (!this.printerStatusFlagEnabled_) {
return;
}
// Requesting printer status only allowed for local CrOS printers.
if (this.destination.origin !== DestinationOrigin.CROS) {
return;
}
this.destination.requestPrinterStatus().then(
destinationKey => this.onPrinterStatusReceived_(destinationKey));
},
/**
* @param {string} destinationKey
* @private
*/
onPrinterStatusReceived_(destinationKey) {
if (this.destination.key === destinationKey) {
this.printerState_ =
computePrinterState(this.destination.printerStatusReason);
}
},
// </if>
});
......@@ -72,6 +72,7 @@ js_type_check("closure_compile") {
if (is_chromeos) {
deps += [
":destination_dropdown_cros_test",
":destination_item_test_cros",
":destination_search_test_chromeos",
":destination_select_test_cros",
]
......@@ -279,6 +280,16 @@ if (is_chromeos) {
]
externs_list = [ "$externs_path/mocha-2.5.js" ]
}
js_library("destination_item_test_cros") {
deps = [
":print_preview_test_utils",
"..:chai_assert",
"//chrome/browser/resources/print_preview:print_preview",
"//ui/webui/resources/js:assert.m",
]
externs_list = [ "$externs_path/mocha-2.5.js" ]
}
} else {
js_library("destination_select_test") {
deps = [
......
// 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.
import {Destination, DestinationConnectionStatus, DestinationOrigin, DestinationType, NativeLayer, NativeLayerImpl, PrinterState, PrinterStatusReason, PrinterStatusSeverity} from 'chrome://print/print_preview.js';
import {assert} from 'chrome://resources/js/assert.m.js';
import {flush} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {assertEquals} from '../chai_assert.js';
import {waitBeforeNextRender} from '../test_util.m.js';
import {NativeLayerStub} from './native_layer_stub.js';
window.destination_item_test_cros = {};
const destination_item_test_cros = window.destination_item_test_cros;
destination_item_test_cros.suiteName = 'DestinationItemTestCros';
/** @enum {string} */
destination_item_test_cros.TestNames = {
NewStatusUpdatesIcon: 'new status updates icon',
ChangingDestinationUpdatesIcon: 'changing destination updates icon',
OnlyUpdateMatchingDestination: 'only update matching destination',
};
suite(destination_item_test_cros.suiteName, function() {
/** @type {!PrintPreviewDestinationListItemElement} */
let listItem;
/** @type {?NativeLayerStub} */
let nativeLayer = null;
function setNativeLayerPrinterStatusMap() {
[{
printerId: 'One',
statusReasons: [{
reason: PrinterStatusReason.NO_ERROR,
severity: PrinterStatusSeverity.UNKNOWN_SEVERITY
}],
},
{
printerId: 'Two',
statusReasons: [{
reason: PrinterStatusReason.OUT_OF_INK,
severity: PrinterStatusSeverity.ERROR
}],
}]
.forEach(
status =>
nativeLayer.addPrinterStatusToMap(status.printerId, status));
}
/** @override */
setup(function() {
document.body.innerHTML = `
<print-preview-destination-list-item id="listItem">
</print-preview-destination-list-item>`;
// Stub out native layer.
nativeLayer = new NativeLayerStub();
NativeLayerImpl.instance_ = nativeLayer;
setNativeLayerPrinterStatusMap();
listItem = /** @type {!PrintPreviewDestinationListItemElement} */ (
document.body.querySelector('#listItem'));
listItem.destination = new Destination(
'One', DestinationType.LOCAL, DestinationOrigin.CROS, 'Destination One',
DestinationConnectionStatus.ONLINE, {description: 'ABC'});
flush();
});
test(
assert(destination_item_test_cros.TestNames.NewStatusUpdatesIcon),
function() {
const statusIcon = listItem.$$('printer-status-icon-cros');
assertEquals(PrinterState.UNKNOWN, statusIcon.printerState);
return listItem.destination.requestPrinterStatus().then(() => {
assertEquals(PrinterState.GOOD, statusIcon.printerState);
});
});
test(
assert(
destination_item_test_cros.TestNames.ChangingDestinationUpdatesIcon),
function() {
const statusIcon = listItem.$$('printer-status-icon-cros');
assertEquals(PrinterState.UNKNOWN, statusIcon.printerState);
listItem.destination = new Destination(
'Two', DestinationType.LOCAL, DestinationOrigin.CROS,
'Destination Two', DestinationConnectionStatus.ONLINE,
{description: 'ABC'});
return waitBeforeNextRender(listItem).then(() => {
assertEquals(PrinterState.ERROR, statusIcon.printerState);
});
});
// Tests that the printer stauts icon is only notified to update if the
// destination key in the printer status response matches the current
// destination.
test(
assert(
destination_item_test_cros.TestNames.OnlyUpdateMatchingDestination),
function() {
const statusIcon = listItem.$$('printer-status-icon-cros');
assertEquals(PrinterState.UNKNOWN, statusIcon.printerState);
const firstDestinationStatusRequestPromise =
listItem.destination.requestPrinterStatus();
// Simulate destination_list updating and switching the destination
// after the request for the original destination was already sent out.
listItem.destination = new Destination(
'Two', DestinationType.LOCAL, DestinationOrigin.CROS,
'Destination Two', DestinationConnectionStatus.ONLINE,
{description: 'ABC'});
return firstDestinationStatusRequestPromise.then(() => {
// PrinterState should stay the same because the destination in the
// status request response doesn't match.
assertEquals(PrinterState.UNKNOWN, statusIcon.printerState);
});
});
});
......@@ -1024,6 +1024,51 @@ TEST_F('PrintPreviewDestinationItemTest', 'QueryDescription', function() {
this.runMochaTest(destination_item_test.TestNames.QueryDescription);
});
GEN('#if defined(OS_CHROMEOS)');
// eslint-disable-next-line no-var
var PrintPreviewDestinationItemTestCros = class extends PrintPreviewTest {
/** @override */
get browsePreload() {
return 'chrome://print/test_loader.html?module=print_preview/destination_item_test_cros.js';
}
/** @override */
get suiteName() {
return destination_item_test_cros.suiteName;
}
/** @override */
get featureList() {
const kPrinterStatusDialog = ['chromeos::features::kPrinterStatusDialog'];
const featureList = super.featureList || [];
featureList.enabled = featureList.enabled ?
featureList.enabled.concat(kPrinterStatusDialog) :
kPrinterStatusDialog;
return featureList;
}
};
TEST_F(
'PrintPreviewDestinationItemTestCros', 'NewStatusUpdatesIcon', function() {
this.runMochaTest(
destination_item_test_cros.TestNames.NewStatusUpdatesIcon);
});
TEST_F(
'PrintPreviewDestinationItemTestCros', 'ChangingDestinationUpdatesIcon',
function() {
this.runMochaTest(
destination_item_test_cros.TestNames.ChangingDestinationUpdatesIcon);
});
TEST_F(
'PrintPreviewDestinationItemTestCros', 'OnlyUpdateMatchingDestination',
function() {
this.runMochaTest(
destination_item_test_cros.TestNames.OnlyUpdateMatchingDestination);
});
GEN('#endif');
// eslint-disable-next-line no-var
var PrintPreviewAdvancedItemTest = class extends PrintPreviewTest {
/** @override */
......
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