Commit f5243dcd authored by rbpotter's avatar rbpotter Committed by Commit Bot

Reland Print Preview Componentization: Migrate preview area error tests

Original change: https://crrev.com/c/1015850

Chromium and Google Chrome show different messages when the PDF plugin
is missing in Print Preview. Modify the test to accept either message,
to fix official build failures.

Bug: 814860
Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: I1672512db0c50bbf2e95ae6c9798dca82e4ee71c
Reviewed-on: https://chromium-review.googlesource.com/1028566Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#553820}
parent 30038360
......@@ -145,9 +145,7 @@
<print-preview-preview-area id="previewArea" settings="{{settings}}"
destination="[[destination_]]" document-info="{{documentInfo_}}"
state="[[state]]" measurement-system="[[measurementSystem_]]"
on-invalid-printer="onInvalidPrinter_"
on-preview-failed="onPreviewFailed_"
on-preview-loaded="onPreviewLoaded_">
preview-state="{{previewState_}}">
</print-preview-preview-area>
</div>
</template>
......
......@@ -100,6 +100,12 @@ Polymer({
value: false,
},
/** @private {!print_preview_new.PreviewAreaState} */
previewState_: {
type: String,
observer: 'onPreviewAreaStateChanged_',
},
/** @private {boolean} */
settingsExpanded_: {
type: Boolean,
......@@ -301,6 +307,10 @@ Polymer({
/** @private */
onDestinationSelect_: function() {
// If the plugin does not exist do not attempt to load the preview.
if (this.state == print_preview_new.State.FATAL_ERROR)
return;
this.$.state.transitTo(print_preview_new.State.NOT_READY);
this.destination_ = this.destinationStore_.selectedDestination;
},
......@@ -310,8 +320,10 @@ Polymer({
this.set(
'destination_.capabilities',
this.destinationStore_.selectedDestination.capabilities);
if (this.state != print_preview_new.State.READY)
if (this.state != print_preview_new.State.READY &&
this.state != print_preview_new.State.FATAL_ERROR) {
this.$.state.transitTo(print_preview_new.State.READY);
}
if (!this.$.model.initialized())
this.$.model.applyStickySettings();
},
......@@ -353,12 +365,6 @@ Polymer({
}
},
/** @private */
onPreviewLoaded_: function() {
if (this.state == print_preview_new.State.HIDDEN)
this.$.state.transitTo(print_preview_new.State.PRINTING);
},
/** @private */
onPrintRequested_: function() {
this.$.state.transitTo(
......@@ -439,13 +445,30 @@ Polymer({
},
/** @private */
onPreviewFailed_: function() {
this.$.state.transitTo(print_preview_new.State.FATAL_ERROR);
onInvalidPrinter_: function() {
this.previewState_ =
print_preview_new.PreviewAreaState.UNSUPPORTED_CLOUD_PRINTER;
},
/** @private */
onInvalidPrinter_: function() {
onPreviewAreaStateChanged_: function() {
switch (this.previewState_) {
case print_preview_new.PreviewAreaState.PREVIEW_FAILED:
case print_preview_new.PreviewAreaState.NO_PLUGIN:
this.$.state.transitTo(print_preview_new.State.FATAL_ERROR);
break;
case print_preview_new.PreviewAreaState.INVALID_SETTINGS:
case print_preview_new.PreviewAreaState.UNSUPPORTED_CLOUD_PRINTER:
this.$.state.transitTo(print_preview_new.State.INVALID_PRINTER);
break;
case print_preview_new.PreviewAreaState.DISPLAY_PREVIEW:
case print_preview_new.PreviewAreaState.OPEN_IN_PREVIEW_LOADED:
if (this.state == print_preview_new.State.HIDDEN)
this.$.state.transitTo(print_preview_new.State.PRINTING);
break;
default:
break;
}
},
/**
......
......@@ -115,25 +115,25 @@
}
</style>
<div class$="preview-area-overlay-layer [[getInvisible_(previewState_)]]"
<div class$="preview-area-overlay-layer [[getInvisible_(previewState)]]"
aria-hidden$="[[previewLoaded_]]">
<div class="preview-area-messages">
<div class="preview-area-message">
<div>
<span>[[currentMessage_(previewState_)]]</span>
<span>[[currentMessage_(previewState)]]</span>
<span class$="preview-area-loading-message-jumping-dots
[[getJumpingDots_(previewState_)]]"
hidden$="[[!isPreviewLoading_(previewState_)]]">
[[getJumpingDots_(previewState)]]"
hidden$="[[!isPreviewLoading_(previewState)]]">
<span>.</span><span>.</span><span>.</span>
</span>
<a is="action-link" class="learn-more-link"
hidden$="[[!shouldShowLearnMoreLink_(previewState_)]]"
hidden$="[[!shouldShowLearnMoreLink_(previewState)]]"
on-click="onGcpErrorLearnMoreClick_">
$i18n{learnMore}
</a>
</div>
<div class="preview-area-action-area"
hidden$="[[!displaySystemDialogButton_(previewState_)]]">
hidden$="[[!displaySystemDialogButton_(previewState)]]">
<button class="preview-area-open-system-dialog-button">
$i18n{launchNativeDialog}
</button>
......
......@@ -34,14 +34,13 @@ cr.exportPath('print_preview_new');
*/
print_preview_new.PDFPlugin;
(function() {
'use strict';
/** @enum {string} */
const PreviewAreaState_ = {
print_preview_new.PreviewAreaState = {
NO_PLUGIN: 'no-plugin',
LOADING: 'loading',
DISPLAY_PREVIEW: 'display-preview',
OPEN_IN_PREVIEW: 'open-in-preview',
OPEN_IN_PREVIEW_LOADING: 'open-in-preview-loading',
OPEN_IN_PREVIEW_LOADED: 'open-in-preview-loaded',
INVALID_SETTINGS: 'invalid-settings',
PREVIEW_FAILED: 'preview-failed',
UNSUPPORTED_CLOUD_PRINTER: 'unsupported-cloud-printer',
......@@ -80,19 +79,18 @@ Polymer({
value: false,
},
/** @private {string} */
previewState_: {
/** @type {!print_preview_new.PreviewAreaState} */
previewState: {
type: String,
notify: true,
value: PreviewAreaState_.LOADING,
value: print_preview_new.PreviewAreaState.LOADING,
},
/** @private {boolean} */
previewLoaded_: {
type: Boolean,
notify: true,
computed: 'computePreviewLoaded_(' +
'previewState_, pluginLoaded_, documentReady_)',
computed: 'computePreviewLoaded_(previewState)',
},
},
......@@ -109,6 +107,7 @@ Polymer({
'settings.ranges.value, settings.selectionOnly.value, ' +
'settings.scaling.value, settings.rasterize.value, destination.id, ' +
'destination.capabilities)',
'pluginOrDocumentStatusChanged_(pluginLoaded_, documentReady_)',
],
/** @private {print_preview.NativeLayer} */
......@@ -136,14 +135,21 @@ Polymer({
this.addWebUIListener(
'page-preview-ready', this.onPagePreviewReady_.bind(this));
if (!this.checkPluginCompatibility())
this.previewState = print_preview_new.PreviewAreaState.NO_PLUGIN;
},
/**
* @return {boolean} Whether the plugin exists and is compatible. Overridden
* in tests.
*/
checkPluginCompatibility: function() {
const oopCompatObj =
this.$$('.preview-area-compatibility-object-out-of-process');
const isOOPCompatible = oopCompatObj.postMessage;
oopCompatObj.parentElement.removeChild(oopCompatObj);
if (!isOOPCompatible) {
this.previewState_ = PreviewAreaState_.PREVIEW_FAILED;
this.fire('preview-failed');
}
return isOOPCompatible;
},
/**
......@@ -151,9 +157,10 @@ Polymer({
* @private
*/
computePreviewLoaded_: function() {
return this.previewState_ == PreviewAreaState_.DISPLAY_PREVIEW ||
(this.documentReady_ && this.pluginLoaded_ &&
this.previewState_ == PreviewAreaState_.OPEN_IN_PREVIEW);
return this.previewState ==
print_preview_new.PreviewAreaState.DISPLAY_PREVIEW ||
this.previewState ==
print_preview_new.PreviewAreaState.OPEN_IN_PREVIEW_LOADED;
},
/** @return {boolean} Whether the preview is loaded. */
......@@ -206,6 +213,17 @@ Polymer({
this.requestPreviewWhenReady_ = true;
},
/** @private */
pluginOrDocumentStatusChanged_: function() {
if (!this.pluginLoaded_ || !this.documentReady_)
return;
this.previewState = this.previewState ==
print_preview_new.PreviewAreaState.OPEN_IN_PREVIEW_LOADING ?
print_preview_new.PreviewAreaState.OPEN_IN_PREVIEW_LOADED :
print_preview_new.PreviewAreaState.DISPLAY_PREVIEW;
},
/**
* @return {string} 'invisible' if overlay is invisible, '' otherwise.
* @private
......@@ -219,7 +237,7 @@ Polymer({
* @private
*/
isPreviewLoading_: function() {
return this.previewState_ == PreviewAreaState_.LOADING;
return this.previewState == print_preview_new.PreviewAreaState.LOADING;
},
/**
......@@ -235,8 +253,12 @@ Polymer({
* @private
*/
displaySystemDialogButton_: function() {
return this.previewState_ == PreviewAreaState_.INVALID_SETTINGS ||
this.previewState_ == PreviewAreaState_.OPEN_IN_PREVIEW;
return this.previewState ==
print_preview_new.PreviewAreaState.INVALID_SETTINGS ||
this.previewState ==
print_preview_new.PreviewAreaState.OPEN_IN_PREVIEW_LOADING ||
this.previewState ==
print_preview_new.PreviewAreaState.OPEN_IN_PREVIEW_LOADED;
},
/**
......@@ -245,7 +267,8 @@ Polymer({
* @private
*/
shouldShowLearnMoreLink_: function() {
return this.previewState_ == PreviewAreaState_.UNSUPPORTED_CLOUD_PRINTER;
return this.previewState ==
print_preview_new.PreviewAreaState.UNSUPPORTED_CLOUD_PRINTER;
},
/**
......@@ -253,73 +276,67 @@ Polymer({
* @private
*/
currentMessage_: function() {
if (this.previewState_ == PreviewAreaState_.LOADING)
switch (this.previewState) {
case print_preview_new.PreviewAreaState.NO_PLUGIN:
return this.i18n('noPlugin');
case print_preview_new.PreviewAreaState.LOADING:
return this.i18n('loading');
if (this.previewState_ == PreviewAreaState_.OPEN_IN_PREVIEW)
case print_preview_new.PreviewAreaState.OPEN_IN_PREVIEW_LOADING:
case print_preview_new.PreviewAreaState.OPEN_IN_PREVIEW_LOADED:
return this.i18n('openingPDFInPreview');
if (this.previewState_ == PreviewAreaState_.INVALID_SETTINGS)
case print_preview_new.PreviewAreaState.INVALID_SETTINGS:
return this.i18n('invalidPrinterSettings');
if (this.previewState_ == PreviewAreaState_.PREVIEW_FAILED)
case print_preview_new.PreviewAreaState.PREVIEW_FAILED:
return this.i18n('previewFailed');
if (this.previewState_ == PreviewAreaState_.UNSUPPORTED_CLOUD_PRINTER)
case print_preview_new.PreviewAreaState.UNSUPPORTED_CLOUD_PRINTER:
return this.i18n('unsupportedCloudPrinter');
default:
return '';
}
},
/** @private */
startPreview_: function() {
this.previewState_ = PreviewAreaState_.LOADING;
this.previewState = print_preview_new.PreviewAreaState.LOADING;
this.documentReady_ = false;
this.getPreview_().then(
previewUid => {
if (!this.documentInfo.isModifiable)
this.onPreviewStart_(previewUid, -1);
this.documentReady_ = true;
if (this.pluginLoaded_) {
if (this.previewState_ != PreviewAreaState_.OPEN_IN_PREVIEW)
this.previewState_ = PreviewAreaState_.DISPLAY_PREVIEW;
this.fire('preview-loaded');
}
},
type => {
if (/** @type{string} */ (type) == 'SETTINGS_INVALID') {
this.previewState_ = PreviewAreaState_.INVALID_SETTINGS;
this.fire('invalid-printer');
this.previewState =
print_preview_new.PreviewAreaState.INVALID_SETTINGS;
} else if (/** @type{string} */ (type) != 'CANCELLED') {
this.previewState_ = PreviewAreaState_.PREVIEW_FAILED;
this.fire('preview-failed');
this.previewState =
print_preview_new.PreviewAreaState.PREVIEW_FAILED;
}
});
},
/** @private */
onStateChanged_: function() {
switch (this.state) {
case (print_preview_new.State.NOT_READY):
if (this.state == print_preview_new.State.NOT_READY) {
// Resetting the destination clears the invalid settings error.
this.previewState_ = PreviewAreaState_.LOADING;
break;
case (print_preview_new.State.READY):
// Request a new preview.
if (this.requestPreviewWhenReady_) {
this.previewState = print_preview_new.PreviewAreaState.LOADING;
} else if (
this.state == print_preview_new.State.READY &&
this.requestPreviewWhenReady_) {
this.startPreview_();
this.requestPreviewWhenReady_ = false;
}
break;
case (print_preview_new.State.INVALID_PRINTER):
if (this.previewState_ != PreviewAreaState_.INVALID_SETTINGS)
this.previewState_ = PreviewAreaState_.UNSUPPORTED_CLOUD_PRINTER;
break;
default:
break;
}
},
// <if expr="is_macosx">
/** Set the preview state to display the "opening in preview" message. */
setOpeningPdfInPreview: function() {
assert(cr.isMac);
this.previewState_ = PreviewAreaState_.OPEN_IN_PREVIEW;
this.previewState =
(this.previewState == print_preview_new.PreviewAreaState.LOADING) ?
print_preview_new.PreviewAreaState.OPEN_IN_PREVIEW_LOADING :
print_preview_new.PreviewAreaState.OPEN_IN_PREVIEW_LOADED;
},
// </if>
......@@ -405,11 +422,6 @@ Polymer({
*/
onPluginLoad_: function() {
this.pluginLoaded_ = true;
if (this.documentReady_) {
if (this.previewState_ != PreviewAreaState_.OPEN_IN_PREVIEW)
this.previewState_ = PreviewAreaState_.DISPLAY_PREVIEW;
this.fire('preview-loaded');
}
},
/**
......@@ -637,4 +649,3 @@ Polymer({
return this.nativeLayer_.getPreview(JSON.stringify(ticket), pageCount);
},
});
})();
// Copyright 2018 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.
cr.define('invalid_settings_browsertest', function() {
/** @enum {string} */
const TestNames = {
NoPDFPluginError: 'no pdf plugin error',
InvalidSettingsError: 'invalid settings error',
InvalidCertificateError: 'invalid certificate error',
InvalidCertificateErrorReselectDestination: 'invalid certificate reselect',
};
const suiteName = 'InvalidSettingsBrowserTest';
suite(suiteName, function() {
/** @type {?PrintPreviewAppElement} */
let page = null;
/** @type {?print_preview.NativeLayer} */
let nativeLayer = null;
/** @type {!print_preview.NativeInitialSettings} */
const initialSettings = {
isInKioskAutoPrintMode: false,
isInAppKioskMode: false,
thousandsDelimeter: ',',
decimalDelimeter: '.',
unitType: 1,
previewModifiable: true,
documentTitle: 'title',
documentHasSelection: true,
shouldPrintSelectionOnly: false,
printerName: 'FooDevice',
serializedAppStateStr: null,
serializedDefaultDestinationSelectionRulesStr: null
};
/** @type {!Array<!print_preview.LocalDestinationInfo>} */
let localDestinationInfos = [
{ printerName: 'FooName', deviceName: 'FooDevice' },
{ printerName: 'BarName', deviceName: 'BarDevice' },
];
/** @override */
setup(function() {
cloudprint.CloudPrintInterface = print_preview.CloudPrintInterfaceStub;
nativeLayer = new print_preview.NativeLayerStub();
print_preview.NativeLayer.setInstance(nativeLayer);
PolymerTest.clearBody();
});
/**
* Initializes the page with initial settings and local destinations
* given by |initialSettings| and |localDestinationInfos|. Also creates
* the fake plugin. Moved out of setup so tests can set those parameters
* differently.
*/
function createPage() {
nativeLayer.setInitialSettings(initialSettings);
nativeLayer.setLocalDestinations(localDestinationInfos);
if (initialSettings.printerName) {
nativeLayer.setLocalDestinationCapabilities(
print_preview_test_utils.getCddTemplate(
initialSettings.printerName));
}
page = document.createElement('print-preview-app');
const previewArea = page.$$('print-preview-preview-area');
previewArea.plugin_ = new print_preview.PDFPluginStub(
previewArea.onPluginLoad_.bind(previewArea));
}
/**
* Performs some setup for invalid certificate tests using 2 destinations
* in |printers|. printers[0] will be set as the most recent destination,
* and printers[1] will be the second most recent destination. Sets up
* cloud print interface, user info, and runs createPage().
* @param {!Array<!print_preview.Destination>} printers
*/
function setupInvalidCertificateTest(printers) {
initialSettings.printerName = '';
initialSettings.serializedAppStateStr = JSON.stringify({
version: 2,
recentDestinations: [
print_preview.makeRecentDestination(printers[0]),
print_preview.makeRecentDestination(printers[1]),
],
});
localDestinationInfos = [];
createPage();
document.body.appendChild(page);
page.userInfo_.setUsers('foo@chromium.org', ['foo@chromium.org']);
cr.webUIListenerCallback('use-cloud-print', 'cloudprint url', false);
printers.forEach(printer => {
page.cloudPrintInterface_.setPrinter(printer.id, printer);
});
}
// Test that error message is displayed when plugin doesn't exist.
test(assert(TestNames.NoPDFPluginError), function() {
createPage();
const previewArea = page.$.previewArea;
previewArea.checkPluginCompatibility = function() {
return false;
};
document.body.appendChild(page);
return nativeLayer.whenCalled('getInitialSettings').then(function() {
const overlayEl = previewArea.$$('.preview-area-overlay-layer');
const messageEl = previewArea.$$('.preview-area-message');
// Make sure the overlay is visible.
assertFalse(overlayEl.classList.contains('invisible'));
// Make sure the correct text is shown.
const expectedMessageChromium = 'Chromium does not include the PDF ' +
'viewer which is required for Print Preview to function.';
const expectedMessageChrome = 'Google Chrome cannot show the print ' +
'preview when the built-in PDF viewer is missing.';
assertTrue(messageEl.textContent.includes(expectedMessageChromium) ||
messageEl.textContent.includes(expectedMessageChrome));
});
});
// Tests that when a printer cannot be communicated with correctly the
// preview area displays an invalid printer error message and printing
// is disabled. Verifies that the user can recover from this error by
// selecting a different, valid printer.
test(assert(TestNames.InvalidSettingsError), function() {
createPage();
const barDevice = print_preview_test_utils.getCddTemplate('BarDevice');
nativeLayer.setLocalDestinationCapabilities(barDevice);
// FooDevice is the default printer, so will be selected for the initial
// preview request.
nativeLayer.setInvalidPrinterId('FooDevice');
// Expected message
const expectedMessage = 'The selected printer is not available or not '
+ 'installed correctly.';
// Get references to relevant elements.
const previewAreaEl = page.$.previewArea;
const overlay = previewAreaEl.$$('.preview-area-overlay-layer');
const messageEl = previewAreaEl.$$('.preview-area-message');
const header = page.$$('print-preview-header');
const printButton = header.$$('.print');
document.body.appendChild(page);
return nativeLayer.whenCalled('getInitialSettings').then(function() {
page.destinationStore_.startLoadDestinations(
print_preview.PrinterType.LOCAL_PRINTER);
// Wait for the preview request.
return Promise.all([
nativeLayer.whenCalled('getPrinterCapabilities'),
nativeLayer.whenCalled('getPreview')
]);
}).then(function() {
// Print preview should have failed with invalid settings, since
// FooDevice was set as an invalid printer.
assertFalse(overlay.classList.contains('invisible'));
assertTrue(messageEl.textContent.includes(expectedMessage));
// Verify that the print button is disabled
assertTrue(printButton.disabled);
// Reset
nativeLayer.reset();
// Select a new destination
const barDestination =
page.destinationStore_.destinations().find(
d => d.id == 'BarDevice');
page.destinationStore_.selectDestination(barDestination);
// Wait for the preview to be updated.
return Promise.all([
nativeLayer.whenCalled('getPrinterCapabilities'),
nativeLayer.whenCalled('getPreview')
]);
}).then(function() {
// Message should be gone.
assertTrue(overlay.classList.contains('invisible'));
assertFalse(messageEl.textContent.includes(expectedMessage));
// Has active print button and successfully 'prints', indicating
assertFalse(printButton.disabled);
printButton.click();
// This should result in a call to print.
return nativeLayer.whenCalled('print');
}).then(
/**
* @param {string} printTicket The print ticket print() was called
* for.
*/
function(printTicket) {
// Sanity check some printing argument values.
const ticket = JSON.parse(printTicket);
assertEquals(barDevice.printer.deviceName, ticket.deviceName);
assertEquals(
print_preview_test_utils.getDefaultOrientation(barDevice) ==
'LANDSCAPE',
ticket.landscape);
assertEquals(1, ticket.copies);
const mediaDefault =
print_preview_test_utils.getDefaultMediaSize(barDevice);
assertEquals(
mediaDefault.width_microns, ticket.mediaSize.width_microns);
assertEquals(
mediaDefault.height_microns, ticket.mediaSize.height_microns);
return nativeLayer.whenCalled('dialogClose');
});
});
// Test that GCP invalid certificate printers disable the print preview when
// selected and display an error and that the preview dialog can be
// recovered by selecting a new destination. Verifies this works when the
// invalid printer is the most recent destination and is selected by
// default.
test(assert(TestNames.InvalidCertificateError), function() {
const invalidPrinter =
print_preview_test_utils.createDestinationWithCertificateStatus(
'FooDevice', 'FooName', true);
const validPrinter =
print_preview_test_utils.createDestinationWithCertificateStatus(
'BarDevice', 'BarName', false);
setupInvalidCertificateTest([invalidPrinter, validPrinter]);
// Expected message
const expectedMessage = 'The selected Google Cloud Print device is no '
+ 'longer supported. Try setting up the printer in your computer\'s '
+ 'system settings.';
// Get references to relevant elements.
const previewAreaEl = page.$.previewArea;
const overlayEl = previewAreaEl.$$('.preview-area-overlay-layer');
const messageEl = previewAreaEl.$$('.preview-area-message');
const header = page.$$('print-preview-header');
const printButton = header.$$('.print');
const destinationSettings = page.$$('print-preview-destination-settings');
const scalingSettings = page.$$('print-preview-scaling-settings');
const layoutSettings = page.$$('print-preview-layout-settings');
return nativeLayer.whenCalled('getInitialSettings').then(function() {
page.destinationStore_.startLoadCloudDestinations();
// FooDevice will be selected since it is the most recently used
// printer, so the invalid certificate error should be shown.
// The overlay must be visible for the message to be seen.
assertFalse(overlayEl.classList.contains('invisible'));
// Verify that the correct message is shown.
assertTrue(messageEl.textContent.includes(expectedMessage));
// Verify that the print button is disabled
assertTrue(printButton.disabled);
// Verify the state is invalid and that some settings sections are also
// disabled, so there is no way to regenerate the preview.
assertEquals(print_preview_new.State.INVALID_PRINTER, page.state);
assertTrue(layoutSettings.$$('select').disabled);
assertTrue(scalingSettings.$$('input').disabled);
// The destination settings button should be enabled, so that the user
// can select a new printer.
assertFalse(destinationSettings.$$('button').disabled);
// Reset
nativeLayer.reset();
// Select a new, valid cloud destination.
page.destinationStore_.selectDestination(validPrinter);
return nativeLayer.whenCalled('getPreview');
}).then(function() {
// Has active print button, indicating recovery from error state.
assertFalse(printButton.disabled);
// Settings sections are now active.
assertFalse(layoutSettings.$$('select').disabled);
assertFalse(scalingSettings.$$('input').disabled);
// The destination settings button should still be enabled.
assertFalse(destinationSettings.$$('button').disabled);
// Message text should have changed and overlay should be invisible.
assertFalse(messageEl.textContent.includes(expectedMessage));
assertTrue(overlayEl.classList.contains('invisible'));
});
});
// Test that GCP invalid certificate printers disable the print preview when
// selected and display an error and that the preview dialog can be
// recovered by selecting a new destination. Tests that even if destination
// was previously selected, the error is cleared.
test(assert(TestNames.InvalidCertificateErrorReselectDestination),
function() {
const invalidPrinter =
print_preview_test_utils.createDestinationWithCertificateStatus(
'FooDevice', 'FooName', true);
const validPrinter =
print_preview_test_utils.createDestinationWithCertificateStatus(
'BarDevice', 'BarName', false);
setupInvalidCertificateTest([validPrinter, invalidPrinter]);
// Get references to relevant elements.
const previewAreaEl = page.$.previewArea;
const overlayEl = previewAreaEl.$$('.preview-area-overlay-layer');
const messageEl = previewAreaEl.$$('.preview-area-message');
const header = page.$$('print-preview-header');
const printButton = header.$$('.print');
return nativeLayer.whenCalled('getInitialSettings').then(function() {
// Start loading cloud destinations so that the printer capabilities
// arrive.
page.destinationStore_.startLoadCloudDestinations();
return nativeLayer.whenCalled('getPreview');
}).then(function() {
// Has active print button.
assertFalse(printButton.disabled);
// No error message.
assertTrue(overlayEl.classList.contains('invisible'));
// Select the invalid destination and wait for the event.
const whenInvalid = test_util.eventToPromise(
print_preview.DestinationStore.EventType
.SELECTED_DESTINATION_UNSUPPORTED,
page.destinationStore_);
page.destinationStore_.selectDestination(invalidPrinter);
return whenInvalid;
}).then(function() {
// Should have error message.
assertFalse(overlayEl.classList.contains('invisible'));
// Reset
nativeLayer.reset();
// Reselect the valid cloud destination.
const whenSelected = test_util.eventToPromise(
print_preview.DestinationStore.EventType.DESTINATION_SELECT,
page.destinationStore_);
page.destinationStore_.selectDestination(validPrinter);
return whenSelected;
}).then(function() {
// Has active print button and no error message.
assertFalse(printButton.disabled);
assertTrue(overlayEl.classList.contains('invisible'));
});
});
});
return {
suiteName: suiteName,
TestNames: TestNames,
};
});
......@@ -95,12 +95,8 @@ cr.define('print_preview', function() {
pageCount: pageCount
});
const printTicketParsed = JSON.parse(printTicket);
if (printTicketParsed.deviceName == this.badPrinterId_) {
let rejectString = print_preview.PreviewArea.EventType.SETTINGS_INVALID;
rejectString = rejectString.substring(
rejectString.lastIndexOf('.') + 1, rejectString.length);
return Promise.reject(rejectString);
}
if (printTicketParsed.deviceName == this.badPrinterId_)
return Promise.reject('SETTINGS_INVALID');
const pageRanges = printTicketParsed.pageRange;
const requestId = printTicketParsed.requestID;
if (pageRanges.length == 0) { // assume full length document, 1 page.
......
......@@ -342,3 +342,57 @@ TEST_F('PrintPreviewSystemDialogBrowserTest', 'InvalidSettingsDisableLink',
system_dialog_browsertest.TestNames.InvalidSettingsDisableLink);
});
GEN('#endif'); // defined(OS_WIN) || defined(OS_MACOSX)
PrintPreviewInvalidSettingsBrowserTest = class extends NewPrintPreviewTest {
/** @override */
get browsePreload() {
return 'chrome://print/new/app.html';
}
/** @override */
get extraLibraries() {
return super.extraLibraries.concat([
ROOT_PATH + 'chrome/test/data/webui/settings/test_util.js',
ROOT_PATH + 'ui/webui/resources/js/cr/event_target.js',
'../test_browser_proxy.js',
'cloud_print_interface_stub.js',
'native_layer_stub.js',
'plugin_stub.js',
'print_preview_test_utils.js',
'invalid_settings_browsertest.js',
]);
}
/** @override */
get suiteName() {
return invalid_settings_browsertest.suiteName;
}
};
TEST_F('PrintPreviewInvalidSettingsBrowserTest', 'NoPDFPluginError',
function() {
this.runMochaTest(
invalid_settings_browsertest.TestNames.NoPDFPluginError);
});
TEST_F('PrintPreviewInvalidSettingsBrowserTest', 'InvalidSettingsError',
function() {
this.runMochaTest(
invalid_settings_browsertest.TestNames.InvalidSettingsError);
});
TEST_F('PrintPreviewInvalidSettingsBrowserTest', 'InvalidCertificateError',
function() {
loadTimeData.overrideValues({isEnterpriseManaged: false});
this.runMochaTest(
invalid_settings_browsertest.TestNames.InvalidCertificateError);
});
TEST_F('PrintPreviewInvalidSettingsBrowserTest',
'InvalidCertificateErrorReselectDestination',
function() {
loadTimeData.overrideValues({isEnterpriseManaged: false});
this.runMochaTest(
invalid_settings_browsertest.TestNames
.InvalidCertificateErrorReselectDestination);
});
......@@ -8,23 +8,14 @@ cr.define('print_preview', function() {
*/
class PDFPluginStub {
/**
* @param {!print_preview.PreviewArea} The PreviewArea that owns this
* plugin.
* @param {!Function} loadCallback The function to call when the plugin has
* loaded.
*/
constructor(area) {
constructor(loadCallback) {
/**
* @private {?Function} The callback to run when the plugin has loaded.
*/
this.loadCallback_ = null;
}
/**
* @param {!Function} callback The callback to run when the plugin has
* loaded.
* @private {!Function} The callback to run when the plugin has loaded.
*/
setLoadCallback(callback) {
this.loadCallback_ = callback;
this.loadCallback_ = loadCallback;
}
/**
......
......@@ -68,7 +68,8 @@ cr.define('preview_generation_test', function() {
page = document.createElement('print-preview-app');
const previewArea = page.$$('print-preview-preview-area');
previewArea.plugin_ = new print_preview.PDFPluginStub(previewArea);
previewArea.plugin_ = new print_preview.PDFPluginStub(
previewArea.onPluginLoad_.bind(previewArea));
document.body.appendChild(page);
return Promise.all([
nativeLayer.whenCalled('getInitialSettings'),
......
......@@ -68,7 +68,55 @@ cr.define('print_preview_test_utils', function() {
};
}
/**
* Creates a destination with a certificate status tag.
* @param {string} id Printer id
* @param {string} name Printer display name
* @param {boolean} invalid Whether printer has an invalid certificate.
* @return {!print_preview.Destination}
*/
function createDestinationWithCertificateStatus(id, name, invalid) {
const tags = {
certificateStatus: invalid ?
print_preview.DestinationCertificateStatus.NO :
print_preview.DestinationCertificateStatus.UNKNOWN,
};
const dest = new print_preview.Destination(
id, print_preview.DestinationType.GOOGLE,
print_preview.DestinationOrigin.COOKIES, name, true /* isRecent */,
print_preview.DestinationConnectionStatus.ONLINE, tags);
return dest;
}
/**
* Get the default media size for |device|.
* @param {!print_preview.PrinterCapabilitiesResponse} device
* @return {{width_microns: number,
* height_microns: number}} The width and height of the default
* media.
*/
function getDefaultMediaSize(device) {
const size = device.capabilities.printer.media_size.option.find(
opt => opt.is_default);
return { width_microns: size.width_microns,
height_microns: size.height_microns };
}
/**
* Get the default page orientation for |device|.
* @param {!print_preview.PrinterCapabilitiesResponse} device
* @return {string} The default orientation.
*/
function getDefaultOrientation(device) {
return device.capabilities.printer.page_orientation.option.find(
opt => opt.is_default).type;
}
return {
getCddTemplate: getCddTemplate,
getDefaultMediaSize: getDefaultMediaSize,
getDefaultOrientation: getDefaultOrientation,
createDestinationWithCertificateStatus:
createDestinationWithCertificateStatus,
};
});
......@@ -201,31 +201,6 @@ cr.define('print_preview_test', function() {
});
}
/**
* Get the default media size for |device|.
* @param {!print_preview.PrinterCapabilitiesResponse} device
* @return {{width_microns: number,
* height_microns: number}} The width and height of the default
* media.
*/
function getDefaultMediaSize(device) {
const size = device.capabilities.printer.media_size.option.find(
function(opt) { return opt.is_default; });
return { width_microns: size.width_microns,
height_microns: size.height_microns };
}
/**
* Get the default page orientation for |device|.
* @param {!print_preview.PrinterCapabilitiesResponse} device
* @return {string} The default orientation.
*/
function getDefaultOrientation(device) {
return device.capabilities.printer.page_orientation.option.find(
function(opt) { return opt.is_default; }).type;
}
/**
* @param {string} printerId
* @return {!Object}
......@@ -321,26 +296,6 @@ cr.define('print_preview_test', function() {
checkElementDisplayed(advancedSettingsCloseButton, true);
}
/**
* Creates a destination with a certificate status tag.
* @param {string} id Printer id
* @param {string} name Printer display name
* @param {boolean} invalid Whether printer has an invalid certificate.
* @return {!print_preview.Destination}
*/
function getDestinationWithCertificateStatus(id, name, invalid) {
const tags = {
certificateStatus: invalid ?
print_preview.DestinationCertificateStatus.NO :
print_preview.DestinationCertificateStatus.UNKNOWN,
};
const dest = new print_preview.Destination(
id, print_preview.DestinationType.GOOGLE,
print_preview.DestinationOrigin.COOKIES, name, true /* isRecent */,
print_preview.DestinationConnectionStatus.ONLINE, tags);
return dest;
}
/**
* Performs some setup for invalid certificate tests using 2 destinations
* in |printers|. printers[0] will be set as the most recent destination,
......@@ -413,8 +368,7 @@ cr.define('print_preview_test', function() {
print_preview.NativeLayer.setInstance(nativeLayer);
printPreview = new print_preview.PrintPreview();
previewArea = printPreview.getPreviewArea();
previewArea.plugin_ = new print_preview.PDFPluginStub(previewArea);
previewArea.plugin_.setLoadCallback(
previewArea.plugin_ = new print_preview.PDFPluginStub(
previewArea.onPluginLoad_.bind(previewArea));
});
......@@ -1493,10 +1447,12 @@ cr.define('print_preview_test', function() {
const ticket = JSON.parse(printTicket);
expectEquals(barDevice.printer.deviceName, ticket.deviceName);
expectEquals(
getDefaultOrientation(barDevice) == 'LANDSCAPE',
print_preview_test_utils.getDefaultOrientation(barDevice) ==
'LANDSCAPE',
ticket.landscape);
expectEquals(1, ticket.copies);
const mediaDefault = getDefaultMediaSize(barDevice);
const mediaDefault =
print_preview_test_utils.getDefaultMediaSize(barDevice);
expectEquals(
mediaDefault.width_microns, ticket.mediaSize.width_microns);
expectEquals(
......@@ -1512,9 +1468,11 @@ cr.define('print_preview_test', function() {
// default.
test('InvalidCertificateError', function() {
const invalidPrinter =
getDestinationWithCertificateStatus('FooDevice', 'FooName', true);
print_preview_test_utils.createDestinationWithCertificateStatus(
'FooDevice', 'FooName', true);
const validPrinter =
getDestinationWithCertificateStatus('BarDevice', 'BarName', false);
print_preview_test_utils.createDestinationWithCertificateStatus(
'BarDevice', 'BarName', false);
setupInvalidCertificateTest([invalidPrinter, validPrinter]);
// Get references to a few elements for testing.
......@@ -1573,9 +1531,11 @@ cr.define('print_preview_test', function() {
// was previously selected, the error is cleared.
test('InvalidCertificateErrorReselectDestination', function() {
const invalidPrinter =
getDestinationWithCertificateStatus('FooDevice', 'FooName', true);
print_preview_test_utils.createDestinationWithCertificateStatus(
'FooDevice', 'FooName', true);
const validPrinter =
getDestinationWithCertificateStatus('BarDevice', 'BarName', false);
print_preview_test_utils.createDestinationWithCertificateStatus(
'BarDevice', 'BarName', false);
setupInvalidCertificateTest([validPrinter, invalidPrinter]);
// Get references to a few elements for testing.
......@@ -1634,9 +1594,11 @@ cr.define('print_preview_test', function() {
// preview from regenerating when settings are toggled.
test('InvalidCertificateErrorNoPreview', function() {
const invalidPrinter =
getDestinationWithCertificateStatus('FooDevice', 'FooName', true);
print_preview_test_utils.createDestinationWithCertificateStatus(
'FooDevice', 'FooName', true);
const validPrinter =
getDestinationWithCertificateStatus('BarDevice', 'BarName', false);
print_preview_test_utils.createDestinationWithCertificateStatus(
'BarDevice', 'BarName', false);
// Set the valid printer first. If the invalid printer is the first
// printer loaded the bug does not occur since the print ticket store is
......
......@@ -77,7 +77,8 @@ cr.define('restore_state_test', function() {
page = document.createElement('print-preview-app');
const previewArea = page.$$('print-preview-preview-area');
previewArea.plugin_ = new print_preview.PDFPluginStub(previewArea);
previewArea.plugin_ = new print_preview.PDFPluginStub(
previewArea.onPluginLoad_.bind(previewArea));
document.body.appendChild(page);
return nativeLayer.whenCalled('getInitialSettings').then(function() {
return nativeLayer.whenCalled('getPrinterCapabilities');
......@@ -226,7 +227,8 @@ cr.define('restore_state_test', function() {
page = document.createElement('print-preview-app');
const previewArea = page.$$('print-preview-preview-area');
previewArea.plugin_ = new print_preview.PDFPluginStub(previewArea);
previewArea.plugin_ = new print_preview.PDFPluginStub(
previewArea.onPluginLoad_.bind(previewArea));
document.body.appendChild(page);
return nativeLayer.whenCalled('getInitialSettings').then(function() {
......
......@@ -56,7 +56,8 @@ cr.define('settings_sections_tests', function() {
PolymerTest.clearBody();
page = document.createElement('print-preview-app');
const previewArea = page.$$('print-preview-preview-area');
previewArea.plugin_ = new print_preview.PDFPluginStub(previewArea);
previewArea.plugin_ = new print_preview.PDFPluginStub(
previewArea.onPluginLoad_.bind(previewArea));
document.body.appendChild(page);
// Wait for initialization to complete.
......
......@@ -53,8 +53,7 @@ cr.define('system_dialog_browsertest', function() {
page = document.createElement('print-preview-app');
linkContainer = page.$$('print-preview-link-container');
const previewArea = page.$$('print-preview-preview-area');
previewArea.plugin_ = new print_preview.PDFPluginStub(previewArea);
previewArea.plugin_.setLoadCallback(
previewArea.plugin_ = new print_preview.PDFPluginStub(
previewArea.onPluginLoad_.bind(previewArea));
document.body.appendChild(page);
return Promise.all([
......
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