Set up message passing between C++ and JavaScript for end-to-end testing of...

Set up message passing between C++ and JavaScript for end-to-end testing of print preview. Allows preview settings, such as the page range, to be set during a browser test.

BUG=388517

Review URL: https://codereview.chromium.org/381453003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283669 0039d316-1c4b-4281-b951-d872f2087c98
parent 61719bdb
......@@ -14,38 +14,40 @@ cr.define('print_preview', function() {
cr.EventTarget.call(this);
// Bind global handlers
global['setInitialSettings'] = this.onSetInitialSettings_.bind(this);
global['setUseCloudPrint'] = this.onSetUseCloudPrint_.bind(this);
global['setPrinters'] = this.onSetPrinters_.bind(this);
global['updateWithPrinterCapabilities'] =
global.setInitialSettings = this.onSetInitialSettings_.bind(this);
global.setUseCloudPrint = this.onSetUseCloudPrint_.bind(this);
global.setPrinters = this.onSetPrinters_.bind(this);
global.updateWithPrinterCapabilities =
this.onUpdateWithPrinterCapabilities_.bind(this);
global['failedToGetPrinterCapabilities'] =
global.failedToGetPrinterCapabilities =
this.onFailedToGetPrinterCapabilities_.bind(this);
global['failedToGetPrivetPrinterCapabilities'] =
global.failedToGetPrivetPrinterCapabilities =
this.onFailedToGetPrivetPrinterCapabilities_.bind(this);
global['reloadPrintersList'] = this.onReloadPrintersList_.bind(this);
global['printToCloud'] = this.onPrintToCloud_.bind(this);
global['fileSelectionCancelled'] =
global.reloadPrintersList = this.onReloadPrintersList_.bind(this);
global.printToCloud = this.onPrintToCloud_.bind(this);
global.fileSelectionCancelled =
this.onFileSelectionCancelled_.bind(this);
global['fileSelectionCompleted'] =
global.fileSelectionCompleted =
this.onFileSelectionCompleted_.bind(this);
global['printPreviewFailed'] = this.onPrintPreviewFailed_.bind(this);
global['invalidPrinterSettings'] =
global.printPreviewFailed = this.onPrintPreviewFailed_.bind(this);
global.invalidPrinterSettings =
this.onInvalidPrinterSettings_.bind(this);
global['onDidGetDefaultPageLayout'] =
global.onDidGetDefaultPageLayout =
this.onDidGetDefaultPageLayout_.bind(this);
global['onDidGetPreviewPageCount'] =
global.onDidGetPreviewPageCount =
this.onDidGetPreviewPageCount_.bind(this);
global['onDidPreviewPage'] = this.onDidPreviewPage_.bind(this);
global['updatePrintPreview'] = this.onUpdatePrintPreview_.bind(this);
global['printScalingDisabledForSourcePDF'] =
global.onDidPreviewPage = this.onDidPreviewPage_.bind(this);
global.updatePrintPreview = this.onUpdatePrintPreview_.bind(this);
global.printScalingDisabledForSourcePDF =
this.onPrintScalingDisabledForSourcePDF_.bind(this);
global['onDidGetAccessToken'] = this.onDidGetAccessToken_.bind(this);
global['autoCancelForTesting'] = this.autoCancelForTesting_.bind(this);
global['onPrivetPrinterChanged'] = this.onPrivetPrinterChanged_.bind(this);
global['onPrivetCapabilitiesSet'] =
global.onDidGetAccessToken = this.onDidGetAccessToken_.bind(this);
global.autoCancelForTesting = this.autoCancelForTesting_.bind(this);
global.onPrivetPrinterChanged = this.onPrivetPrinterChanged_.bind(this);
global.onPrivetCapabilitiesSet =
this.onPrivetCapabilitiesSet_.bind(this);
global['onPrivetPrintFailed'] = this.onPrivetPrintFailed_.bind(this);
global.onPrivetPrintFailed = this.onPrivetPrintFailed_.bind(this);
global.onEnableManipulateSettingsForTest =
this.onEnableManipulateSettingsForTest_.bind(this);
};
/**
......@@ -65,6 +67,8 @@ cr.define('print_preview', function() {
GET_CAPABILITIES_FAIL: 'print_preview.NativeLayer.GET_CAPABILITIES_FAIL',
INITIAL_SETTINGS_SET: 'print_preview.NativeLayer.INITIAL_SETTINGS_SET',
LOCAL_DESTINATIONS_SET: 'print_preview.NativeLayer.LOCAL_DESTINATIONS_SET',
MANIPULATE_SETTINGS_FOR_TEST:
'print_preview.NativeLayer.MANIPULATE_SETTINGS_FOR_TEST',
PAGE_COUNT_READY: 'print_preview.NativeLayer.PAGE_COUNT_READY',
PAGE_LAYOUT_READY: 'print_preview.NativeLayer.PAGE_LAYOUT_READY',
PAGE_PREVIEW_READY: 'print_preview.NativeLayer.PAGE_PREVIEW_READY',
......@@ -77,7 +81,7 @@ cr.define('print_preview', function() {
PRIVET_PRINTER_CHANGED: 'print_preview.NativeLayer.PRIVET_PRINTER_CHANGED',
PRIVET_CAPABILITIES_SET:
'print_preview.NativeLayer.PRIVET_CAPABILITIES_SET',
PRIVET_PRINT_FAILED: 'print_preview.NativeLayer.PRIVET_PRINT_FAILED'
PRIVET_PRINT_FAILED: 'print_preview.NativeLayer.PRIVET_PRINT_FAILED',
};
/**
......@@ -690,6 +694,49 @@ cr.define('print_preview', function() {
new Event(NativeLayer.EventType.PRIVET_PRINT_FAILED);
privetPrintFailedEvent.httpError = http_error;
this.dispatchEvent(privetPrintFailedEvent);
},
/**
* Allows for onManipulateSettings to be called
* from the native layer.
* @private
*/
onEnableManipulateSettingsForTest_: function() {
global.onManipulateSettingsForTest =
this.onManipulateSettingsForTest_.bind(this);
},
/**
* Dispatches an event to print_preview.js to change
* a particular setting for print preview.
* @param {!Object} settings Object containing the value to be
* changed and that value should be set to.
* @private
*/
onManipulateSettingsForTest_: function(settings) {
var manipulateSettingsEvent =
new Event(NativeLayer.EventType.MANIPULATE_SETTINGS_FOR_TEST);
manipulateSettingsEvent.settings = settings;
this.dispatchEvent(manipulateSettingsEvent);
},
/**
* Sends a message to the test, letting it know that an
* option has been set to a particular value and that the change has
* finished modifying the preview area.
*/
previewReadyForTest: function() {
if (global.onManipulateSettingsForTest)
chrome.send('UILoadedForTest');
},
/**
* Notifies the test that the option it tried to change
* had not been changed successfully.
*/
previewFailedForTest: function() {
if (global.onManipulateSettingsForTest)
chrome.send('UIFailedLoadingForTest');
}
};
......
......@@ -293,7 +293,10 @@ cr.define('print_preview', function() {
this.nativeLayer_,
print_preview.NativeLayer.EventType.PRIVET_PRINT_FAILED,
this.onPrivetPrintFailed_.bind(this));
this.tracker.add(
this.nativeLayer_,
print_preview.NativeLayer.EventType.MANIPULATE_SETTINGS_FOR_TEST,
this.onManipulateSettingsForTest_.bind(this));
this.tracker.add(
$('system-dialog-link'),
......@@ -704,6 +707,7 @@ cr.define('print_preview', function() {
onPreviewGenerationDone_: function() {
this.isPreviewGenerationInProgress_ = false;
this.printHeader_.isPrintButtonEnabled = true;
this.nativeLayer_.previewReadyForTest();
this.printIfReady_();
},
......@@ -902,6 +906,141 @@ cr.define('print_preview', function() {
localStrings.getString('couldNotPrint'));
},
/**
* Called when the print preview settings need to be changed for testing.
* @param {Event} event Event object that contains the option that is to
* be changed and what to set that option.
* @private
*/
onManipulateSettingsForTest_: function(event) {
if ('selectSaveAsPdfDestination' in event.settings) {
this.saveAsPdfForTest_(); // No parameters.
} else if ('layoutSettings' in event.settings) {
this.setLayoutSettingsForTest_(event.settings.layoutSettings.portrait);
} else if ('pageRange' in event.settings) {
this.setPageRangeForTest_(event.settings.pageRange);
} else if ('headersAndFooters' in event.settings) {
this.setHeadersAndFootersForTest_(event.settings.headersAndFooters);
} else if ('backgroundColorsAndImages' in event.settings) {
this.setBackgroundColorsAndImagesForTest_(
event.settings.backgroundColorsAndImages);
} else if ('margins' in event.settings) {
this.setMarginsForTest_(event.settings.margins);
}
},
/**
* Called by onManipulateSettingsForTest_(). Sets the print destination
* as a pdf.
* @private
*/
saveAsPdfForTest_: function() {
if (this.destinationStore_.selectedDestination &&
print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ==
this.destinationStore_.selectedDestination.id) {
this.nativeLayer_.previewReadyForTest();
return;
}
var destinations = this.destinationStore_.destinations();
var pdfDestination = null;
for (var i = 0; i < destinations.length; i++) {
if (destinations[i].id ==
print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) {
pdfDestination = destinations[i];
break;
}
}
if (pdfDestination)
this.destinationStore_.selectDestination(pdfDestination);
else
this.nativeLayer_.previewFailedForTest();
},
/**
* Called by onManipulateSettingsForTest_(). Sets the layout settings to
* either portrait or landscape.
* @param {boolean} portrait Whether to use portrait page layout;
* if false: landscape.
* @private
*/
setLayoutSettingsForTest_: function(portrait) {
var element = document.querySelector(portrait ?
'.layout-settings-portrait-radio' :
'.layout-settings-landscape-radio');
if (element.checked)
this.nativeLayer_.previewReadyForTest();
else
element.click();
},
/**
* Called by onManipulateSettingsForTest_(). Sets the page range for
* for the print preview settings.
* @param {string} pageRange Sets the page range to the desired value(s).
* Ex: "1-5,9" means pages 1 through 5 and page 9 will be printed.
* @private
*/
setPageRangeForTest_: function(pageRange) {
var textbox = document.querySelector('.page-settings-custom-input');
if (textbox.value == pageRange) {
this.nativeLayer_.previewReadyForTest();
} else {
textbox.value = pageRange;
document.querySelector('.page-settings-custom-radio').click();
}
},
/**
* Called by onManipulateSettings_(). Checks or unchecks the headers and
* footers option on print preview.
* @param {boolean} headersAndFooters Whether the "Headers and Footers"
* checkbox should be checked.
* @private
*/
setHeadersAndFootersForTest_: function(headersAndFooters) {
var checkbox = document.querySelector('.header-footer-checkbox');
if (headersAndFooters == checkbox.checked)
this.nativeLayer_.previewReadyForTest();
else
checkbox.click();
},
/**
* Called by onManipulateSettings_(). Checks or unchecks the background
* colors and images option on print preview.
* @param {boolean} backgroundColorsAndImages If true, the checkbox should
* be checked. Otherwise it should be unchecked.
* @private
*/
setBackgroundColorsAndImagesForTest_: function(backgroundColorsAndImages) {
var checkbox = document.querySelector('.css-background-checkbox');
if (backgroundColorsAndImages == checkbox.checked)
this.nativeLayer_.previewReadyForTest();
else
checkbox.click();
},
/**
* Called by onManipulateSettings_(). Sets the margin settings
* that are desired. Custom margin settings aren't currently supported.
* @param {number} margins The desired margins combobox index. Must be
* a valid index or else the test fails.
* @private
*/
setMarginsForTest_: function(margins) {
var combobox = document.querySelector('.margin-settings-select');
if (margins == combobox.selectedIndex) {
this.nativeLayer_.previewReadyForTest();
} else if (margins >= 0 && margins < combobox.length) {
combobox.selectedIndex = margins;
this.marginSettings_.onSelectChange_();
} else {
this.nativeLayer_.previewFailedForTest();
}
},
/**
* Called when the open-cloud-print-dialog link is clicked. Opens the Google
* Cloud Print web dialog.
......
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