Commit 07a368c9 authored by rbpotter's avatar rbpotter Committed by Commit Bot

Print Preview: Always select a printer on startup

In kiosk mode, the PDF printer is not available. If there are no recent
destinations, no system default, and no default destinations configured,
the preview will spin forever. Instead, select the first printer that
is detected.

Bug: 901115
Change-Id: I048a973b8c8161459f8ef724a57616fb5298f21e
Reviewed-on: https://chromium-review.googlesource.com/c/1332721
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609088}
parent 5d1ccd25
......@@ -188,6 +188,13 @@ cr.define('print_preview', function() {
*/
this.pdfPrinterEnabled_ = false;
/**
* Whether to select the first printer that is found. Used when
* pdfPrinterEnabled_ is false.
* @private {boolean}
*/
this.selectFirstDestination_ = false;
/**
* ID of the system default destination.
* @private {string}
......@@ -391,7 +398,7 @@ cr.define('print_preview', function() {
if (this.systemDefaultDestinationId_.length == 0 &&
!isRecentDestinationValid) {
this.selectPdfDestination_();
this.selectFinalFallbackDestination_();
return;
}
......@@ -453,7 +460,7 @@ cr.define('print_preview', function() {
return;
}
this.selectPdfDestination_();
this.selectFinalFallbackDestination_();
}
/**
......@@ -789,20 +796,37 @@ cr.define('print_preview', function() {
}
/**
* Selects 'Save to PDF' destination (since it always exists).
* Selects the Save as PDF fallback if it is available. If not, selects the
* first destination if it exists. If the store is empty, starts loading all
* printers to find one to select.
* @private
*/
selectPdfDestination_() {
selectFinalFallbackDestination_() {
// Save as PDF should always exist if it is enabled.
if (this.pdfPrinterEnabled_) {
const saveToPdfKey = this.getDestinationKey_(
print_preview.DestinationOrigin.LOCAL,
print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, '');
this.selectDestination(
this.destinationMap_[saveToPdfKey] || this.destinations_[0] || null);
this.selectDestination(assert(this.destinationMap_[saveToPdfKey]));
return;
}
// Try selecting the first destination if there is at least one
// destination already loaded.
if (this.destinations_.length > 0) {
this.selectDestination(this.destinations_[0]);
return;
}
// Load all destinations to find one to select.
this.selectFirstDestination_ = true;
this.startLoadAllDestinations();
}
/**
* Attempts to select system default destination with a fallback to
* 'Save to PDF' destination.
* Attempts to select system default destination with a fallback to the
* 'Save to PDF' destination and a final fallback to the first destination
* in the store.
* @private
*/
selectDefaultDestination_() {
......@@ -818,7 +842,7 @@ cr.define('print_preview', function() {
}
}
}
this.selectPdfDestination_();
this.selectFinalFallbackDestination_();
}
/**
......@@ -1231,8 +1255,12 @@ cr.define('print_preview', function() {
* @private
*/
onCloudPrintSearchDone_(event) {
if (event.printers) {
if (event.printers && event.printers.length > 0) {
this.insertDestinations_(event.printers);
if (this.selectFirstDestination_) {
this.selectDestination(this.destinations_[0]);
this.selectFirstDestination_ = false;
}
}
if (event.searchDone) {
const origins = this.loadedCloudOrigins_[event.user] || [];
......@@ -1309,6 +1337,11 @@ cr.define('print_preview', function() {
}
this.insertDestinations_(printers.map(
printer => print_preview.parseDestination(type, printer)));
if (this.selectFirstDestination_) {
this.selectDestination(this.destinations_[0]);
this.selectFirstDestination_ = false;
}
}
/**
......
......@@ -11,6 +11,7 @@ cr.define('destination_select_test', function() {
'multiple recent destinations one request',
DefaultDestinationSelectionRules: 'default destination selection rules',
SystemDefaultPrinterPolicy: 'system default printer policy',
KioskModeSelectsFirstPrinter: 'kiosk mode selects first printer',
};
const suiteName = 'DestinationSelectTests';
......@@ -58,6 +59,23 @@ cr.define('destination_select_test', function() {
]);
}
/**
* Checks that a printer is displayed to the user with the name given
* by |printerName|.
* @param {string} printerName The printer name that should be displayed.
*/
function assertPrinterDisplay(printerName) {
const destinationSettings = page.$$('print-preview-destination-settings');
// Check that the throbber is hidden and the destination info is shown.
assertTrue(destinationSettings.$$('.throbber-container').hidden);
assertFalse(destinationSettings.$$('.destination-settings-box').hidden);
// Check that the destination matches the expected destination.
assertEquals(
printerName, destinationSettings.$$('.destination-name').textContent);
}
/**
* Tests that if the user has a single valid recent destination the
* destination is automatically reselected.
......@@ -74,6 +92,7 @@ cr.define('destination_select_test', function() {
assertEquals('ID1', argsArray[1].destinationId);
assertEquals(print_preview.PrinterType.LOCAL, argsArray[1].type);
assertEquals('ID1', page.destination_.id);
assertPrinterDisplay('One');
});
});
......@@ -98,6 +117,7 @@ cr.define('destination_select_test', function() {
assertEquals('ID1', argsArray[1].destinationId);
assertEquals(print_preview.PrinterType.LOCAL, argsArray[1].type);
assertEquals('ID1', page.destination_.id);
assertPrinterDisplay('One');
// Load all local destinations.
page.destinationStore_.startLoadDestinations(
......@@ -175,6 +195,7 @@ cr.define('destination_select_test', function() {
assertEquals('ID4', argsArray[1].destinationId);
assertEquals(print_preview.PrinterType.LOCAL, argsArray[1].type);
assertEquals('ID4', page.destination_.id);
assertPrinterDisplay('Four');
});
});
......@@ -202,6 +223,28 @@ cr.define('destination_select_test', function() {
assertEquals('FooDevice', argsArray[1].destinationId);
assertEquals(print_preview.PrinterType.LOCAL, argsArray[1].type);
assertEquals('FooDevice', page.destination_.id);
assertPrinterDisplay('FooName');
});
});
/**
* Tests that if there is no system default destination, the default
* selection rules and recent destinations are empty, and the preview
* is in app kiosk mode (so no PDF printer), the first destination returned
* from printer fetch is selected.
*/
test(assert(TestNames.KioskModeSelectsFirstPrinter), function() {
initialSettings.serializedDefaultDestinationSelectionRulesStr = '';
initialSettings.serializedAppStateStr = '';
initialSettings.isInAppKioskMode = true;
initialSettings.printerName = '';
return setInitialSettings().then(function(argsArray) {
// Should have loaded the first destination as the selected printer.
assertEquals(destinations[0].id, argsArray[1].destinationId);
assertEquals(print_preview.PrinterType.LOCAL, argsArray[1].type);
assertEquals(destinations[0].id, page.destination_.id);
assertPrinterDisplay(destinations[0].displayName);
});
});
});
......
......@@ -662,6 +662,13 @@ TEST_F(
});
GEN('#endif');
TEST_F(
'PrintPreviewDestinationSelectTest', 'KioskModeSelectsFirstPrinter',
function() {
this.runMochaTest(
destination_select_test.TestNames.KioskModeSelectsFirstPrinter);
});
PrintPreviewDestinationDialogTest = class extends NewPrintPreviewTest {
/** @override */
get browsePreload() {
......
......@@ -254,6 +254,7 @@ PrintPreviewDestinationDialogTest.*
PrintPreviewDestinationItemTest.*
PrintPreviewDestinationListTest.*
PrintPreviewDestinationSearchTest.*
PrintPreviewDestinationSelectTest.*
PrintPreviewDestinationSettingsTest.*
PrintPreviewHeaderTest.*
PrintPreviewKeyEventTest.*
......
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