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

Print Preview componentization: use initial settings

Use initial settings to populate some fields.

Bug: 773928
Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: I817c34fabf416b7a935633bdc1830d3e69fc4086
Reviewed-on: https://chromium-review.googlesource.com/809708
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#523340}
parent 5838b11f
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
'target_name': 'header', 'target_name': 'header',
'dependencies': [ 'dependencies': [
'../data/compiled_resources2.gyp:destination', '../data/compiled_resources2.gyp:destination',
'model',
'settings_behavior', 'settings_behavior',
'<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:load_time_data', '<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:load_time_data',
], ],
...@@ -128,18 +129,20 @@ ...@@ -128,18 +129,20 @@
{ {
'target_name': 'settings_behavior', 'target_name': 'settings_behavior',
'dependencies': [ 'dependencies': [
'model',
'<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:assert', '<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:assert',
'<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:cr',
], ],
'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'], 'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'],
}, },
{ {
'target_name': 'model', 'target_name': 'model',
'dependencies': [ 'dependencies': [
'settings_behavior',
'<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:cr', '<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:cr',
'../compiled_resources2.gyp:native_layer', '../compiled_resources2.gyp:native_layer',
'../data/compiled_resources2.gyp:destination', '../data/compiled_resources2.gyp:destination',
'../data/compiled_resources2.gyp:document_info', '../data/compiled_resources2.gyp:document_info',
'../data/compiled_resources2.gyp:measurement_system',
], ],
'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'], 'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'],
} }
......
...@@ -3,5 +3,7 @@ ...@@ -3,5 +3,7 @@
<link rel="import" href="../native_layer.html"> <link rel="import" href="../native_layer.html">
<link rel="import" href="../data/destination.html"> <link rel="import" href="../data/destination.html">
<link rel="import" href="../data/document_info.html"> <link rel="import" href="../data/document_info.html">
<link rel="import" href="../data/measurement_system.html">
<link rel="import" href="settings_behavior.html">
<script src="model.js"></script> <script src="model.js"></script>
...@@ -3,17 +3,6 @@ ...@@ -3,17 +3,6 @@
// found in the LICENSE file. // found in the LICENSE file.
cr.exportPath('print_preview_new'); cr.exportPath('print_preview_new');
/**
* @typedef {{
* value: *,
* valid: boolean,
* available: boolean,
* updatesPreview: boolean
* }}
*/
print_preview_new.Setting;
/** /**
* @typedef {{ * @typedef {{
* previewLoading: boolean, * previewLoading: boolean,
...@@ -25,9 +14,64 @@ print_preview_new.Setting; ...@@ -25,9 +14,64 @@ print_preview_new.Setting;
*/ */
print_preview_new.State; print_preview_new.State;
/**
* @typedef {{id: string,
* origin: print_preview.DestinationOrigin,
* account: string,
* capabilities: ?print_preview.Cdd,
* displayName: string,
* extensionId: string,
* extensionName: string}}
*/
print_preview_new.RecentDestination;
/**
* Must be kept in sync with the C++ MarginType enum in
* printing/print_job_constants.h.
* @enum {number}
*/
print_preview_new.MarginsTypeValue = {
DEFAULT: 0,
NO_MARGINS: 1,
MINIMUM: 2,
CUSTOM: 3
};
/**
* @typedef {{
* version: string,
* recentDestinations: (!Array<!print_preview_new.RecentDestination> |
* undefined),
* dpi: ({horizontal_dpi: number,
* vertical_dpi: number,
* is_default: (boolean | undefined)} | undefined),
* mediaSize: ({height_microns: number,
* width_microns: number,
* custom_display_name: (string | undefined),
* is_default: (boolean | undefined)} | undefined),
* marginsType: (print_preview_new.MarginsTypeValue | undefined),
* customMargins: ({marginTop: number,
* marginBottom: number,
* marginLeft: number,
* marginRight: number} | undefined),
* isColorEnabled: (boolean | undefined),
* isDuplexEnabled: (boolean | undefined),
* isHeaderFooterEnabled: (boolean | undefined),
* isLandscapeEnabled: (boolean | undefined),
* isCollateEnabled: (boolean | undefined),
* isFitToPageEnabled: (boolean | undefined),
* isCssBackgroundEnabled: (boolean | undefined),
* scaling: (string | undefined),
* vendor_options: (Object | undefined)
* }}
*/
print_preview_new.SerializedSettings;
Polymer({ Polymer({
is: 'print-preview-model', is: 'print-preview-model',
behaviors: [SettingsBehavior],
properties: { properties: {
/** /**
* Object containing current settings of Print Preview, for use by Polymer * Object containing current settings of Print Preview, for use by Polymer
...@@ -57,7 +101,7 @@ Polymer({ ...@@ -57,7 +101,7 @@ Polymer({
notify: true, notify: true,
value: { value: {
pages: { pages: {
value: [1, 2, 3, 4, 5], value: [1],
valid: true, valid: true,
available: true, available: true,
updatesPreview: true, updatesPreview: true,
...@@ -233,13 +277,7 @@ Polymer({ ...@@ -233,13 +277,7 @@ Polymer({
documentInfo: { documentInfo: {
type: Object, type: Object,
notify: true, notify: true,
value: function() { value: new print_preview.DocumentInfo(),
const info = new print_preview.DocumentInfo();
info.init(false, 'DocumentTitle', true);
info.updatePageCount(5);
info.fitToPageScaling_ = 94;
return info;
},
}, },
/** @type {!print_preview_new.State} */ /** @type {!print_preview_new.State} */
...@@ -273,11 +311,16 @@ Polymer({ ...@@ -273,11 +311,16 @@ Polymer({
*/ */
MONOCHROME_TYPES_: ['STANDARD_MONOCHROME', 'CUSTOM_MONOCHROME'], MONOCHROME_TYPES_: ['STANDARD_MONOCHROME', 'CUSTOM_MONOCHROME'],
/** @private {!print_preview.NativeLayer} */ /** @private {?print_preview.NativeLayer} */
nativeLayer_: print_preview.NativeLayer.getInstance(), nativeLayer_: null,
/** @type {!print_preview.MeasurementSystem} */
measurementSystem_: new print_preview.MeasurementSystem(
',', '.', print_preview.MeasurementSystemUnitType.IMPERIAL),
/** @override */ /** @override */
attached: function() { attached: function() {
this.nativeLayer_ = print_preview.NativeLayer.getInstance(),
this.nativeLayer_.getInitialSettings().then( this.nativeLayer_.getInitialSettings().then(
this.onInitialSettingsSet_.bind(this)); this.onInitialSettingsSet_.bind(this));
}, },
...@@ -287,7 +330,52 @@ Polymer({ ...@@ -287,7 +330,52 @@ Polymer({
* @private * @private
*/ */
onInitialSettingsSet_: function(settings) { onInitialSettingsSet_: function(settings) {
// Do nothing here for now. this.documentInfo.init(
settings.previewModifiable, settings.documentTitle,
settings.documentHasSelection);
// Temporary setting, will be replaced when page count is known from
// the page-count-ready webUI event.
this.documentInfo.updatePageCount(5);
this.notifyPath('documentInfo.isModifiable');
// Before triggering the final notification for settings availability,
// set initialized = true.
this.notifyPath('documentInfo.hasSelection');
this.notifyPath('documentInfo.title');
this.notifyPath('documentInfo.pageCount');
this.updateFromStickySettings_(settings.serializedAppStateStr);
this.measurementSystem_.setSystem(
settings.thousandsDelimeter, settings.decimalDelimeter,
settings.unitType);
this.setSetting('selectionOnly', settings.shouldPrintSelectionOnly);
// TODO(rbpotter): add destination store initialization.
},
/**
* @param {?string} savedSettingsStr The sticky settings from native layer
* @private
*/
updateFromStickySettings_(savedSettingsStr) {
if (!savedSettingsStr)
return;
let savedSettings;
try {
savedSettings = /** @type {print_preview_new.SerializedSettings} */ (
JSON.parse(savedSettingsStr));
} catch (e) {
console.error('Unable to parse state ' + e);
return; // use default values rather than updating.
}
const updateIfDefined = (key1, key2) => {
if (savedSettings[key2] != undefined)
this.setSetting(key1, savedSettings[key2]);
};
[['dpi', 'dpi'], ['mediaSize', 'mediaSize'], ['margins', 'marginsType'],
['color', 'isColorEnabled'], ['headerFooter', 'isHeaderFooterEnabled'],
['layout', 'isLandscapeEnabled'], ['collate', 'isCollateEnabled'],
['fitToPage', 'isFitToPageEnabled'],
['cssBackground', 'isCssBackgroundEnabled'], ['scaling', 'scaling'],
].forEach(keys => updateIfDefined(keys[0], keys[1]));
}, },
/** /**
......
...@@ -54,7 +54,7 @@ Polymer({ ...@@ -54,7 +54,7 @@ Polymer({
/** @private {!PagesInputErrorState} */ /** @private {!PagesInputErrorState} */
errorState_: { errorState_: {
type: Number, type: Number,
computed: 'computeErrorState_(pagesToPrint_)', computed: 'computeErrorState_(documentInfo.pageCount, pagesToPrint_)',
}, },
}, },
...@@ -133,6 +133,8 @@ Polymer({ ...@@ -133,6 +133,8 @@ Polymer({
* @private * @private
*/ */
computeErrorState_: function() { computeErrorState_: function() {
if (this.documentInfo.pageCount == 0) // page count not yet initialized
return PagesInputErrorState.NO_ERROR;
if (this.pagesToPrint_.length == 0) if (this.pagesToPrint_.length == 0)
return PagesInputErrorState.INVALID_SYNTAX; return PagesInputErrorState.INVALID_SYNTAX;
if (this.pagesToPrint_[0] == -1) if (this.pagesToPrint_[0] == -1)
......
...@@ -2,6 +2,17 @@ ...@@ -2,6 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
cr.exportPath('print_preview_new');
/**
* @typedef {{
* value: *,
* valid: boolean,
* available: boolean,
* updatesPreview: boolean
* }}
*/
print_preview_new.Setting;
/** @polymerBehavior */ /** @polymerBehavior */
const SettingsBehavior = { const SettingsBehavior = {
properties: { properties: {
......
...@@ -65,6 +65,12 @@ ...@@ -65,6 +65,12 @@
<structure name="IDR_PRINT_PREVIEW_DATA_COORDINATE2D_JS" <structure name="IDR_PRINT_PREVIEW_DATA_COORDINATE2D_JS"
file="data/coordinate2d.js" file="data/coordinate2d.js"
type="chrome_html" /> type="chrome_html" />
<structure name="IDR_PRINT_PREVIEW_DATA_MEASUREMENT_SYSTEM_HTML"
file="data/measurement_system.html"
type="chrome_html" />
<structure name="IDR_PRINT_PREVIEW_DATA_MEASUREMENT_SYSTEM_JS"
file="data/measurement_system.js"
type="chrome_html" />
<structure name="IDR_PRINT_PREVIEW_DATA_PRINTABLE_AREA_HTML" <structure name="IDR_PRINT_PREVIEW_DATA_PRINTABLE_AREA_HTML"
file="data/printable_area.html" file="data/printable_area.html"
type="chrome_html" /> type="chrome_html" />
......
...@@ -91,3 +91,32 @@ TEST_F('PrintPreviewSettingsSectionsTest', 'Scaling', function() { ...@@ -91,3 +91,32 @@ TEST_F('PrintPreviewSettingsSectionsTest', 'Scaling', function() {
TEST_F('PrintPreviewSettingsSectionsTest', 'Other', function() { TEST_F('PrintPreviewSettingsSectionsTest', 'Other', function() {
this.runMochaTest(settings_sections_tests.TestNames.Other); this.runMochaTest(settings_sections_tests.TestNames.Other);
}); });
PrintPreviewRestoreStateTest = class extends NewPrintPreviewTest {
/** @override */
get browsePreload() {
return 'chrome://print/new/app.html';
}
/** @override */
get extraLibraries() {
return super.extraLibraries.concat([
'../test_browser_proxy.js',
'native_layer_stub.js',
'restore_state_test.js',
]);
}
/** @override */
get suiteName() {
return restore_state_test.suiteName;
}
};
TEST_F('PrintPreviewRestoreStateTest', 'RestoreTrueValues', function() {
this.runMochaTest(restore_state_test.TestNames.RestoreTrueValues);
});
TEST_F('PrintPreviewRestoreStateTest', 'RestoreFalseValues', function() {
this.runMochaTest(restore_state_test.TestNames.RestoreFalseValues);
});
// Copyright 2017 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('restore_state_test', function() {
/** @enum {string} */
const TestNames = {
RestoreTrueValues: 'restore true values',
RestoreFalseValues: 'restore false values',
};
const suiteName = 'RestoreStateTest';
suite(suiteName, function() {
let page = null;
let nativeLayer = null;
const initialSettings = {
isInKioskAutoPrintMode: false,
isInAppKioskMode: false,
thousandsDelimeter: ',',
decimalDelimeter: '.',
unitType: 1,
previewModifiable: true,
documentTitle: 'title',
documentHasSelection: true,
shouldPrintSelectionOnly: false,
printerName: 'FooDevice',
serializedAppStateStr: null,
serializedDefaultDestinationSelectionRulesStr: null
};
/** @override */
setup(function() {
nativeLayer = new print_preview.NativeLayerStub();
print_preview.NativeLayer.setInstance(nativeLayer);
PolymerTest.clearBody();
});
/**
* @param {!print_preview_new.SerializedSettings} stickySettings Settings
* to verify.
*/
function verifyStickySettingsApplied(stickySettings) {
assertEquals(stickySettings.dpi.horizontal_dpi,
page.settings.dpi.value.horizontal_dpi);
assertEquals(stickySettings.dpi.vertical_dpi,
page.settings.dpi.value.vertical_dpi);
assertEquals(stickySettings.mediaSize.name,
page.settings.mediaSize.value.name);
assertEquals(stickySettings.mediaSize.height_microns,
page.settings.mediaSize.value.height_microns);
assertEquals(stickySettings.mediaSize.width_microns,
page.settings.mediaSize.value.width_microns);
[['margins', 'marginsType'],
['color', 'isColorEnabled'], ['headerFooter', 'isHeaderFooterEnabled'],
['layout', 'isLandscapeEnabled'], ['collate', 'isCollateEnabled'],
['fitToPage', 'isFitToPageEnabled'],
['cssBackground', 'isCssBackgroundEnabled'], ['scaling', 'scaling'],
].forEach(keys => {
assertEquals(stickySettings[keys[1]], page.settings[keys[0]].value);
});
}
/**
* @param {!print_preview_new.SerializedSettings} stickySettings
* @return {!Promise} Promise that resolves when initialization is done and
* settings have been verified.
*/
function testInitializeWithStickySettings(stickySettings) {
initialSettings.serializedAppStateStr = JSON.stringify(stickySettings);
nativeLayer.setInitialSettings(initialSettings);
page = document.createElement('print-preview-app');
document.body.appendChild(page);
return nativeLayer.whenCalled('getInitialSettings').then(function() {
verifyStickySettingsApplied(stickySettings);
});
}
/**
* Tests state restoration with all boolean settings set to true, scaling =
* 90, dpi = 100, custom square paper, and custom margins.
*/
test(assert(TestNames.RestoreTrueValues), function() {
const stickySettings = {
version: 2,
recentDestinations: [],
dpi: {horizontal_dpi: 100, vertical_dpi: 100},
mediaSize: {name: 'CUSTOM_SQUARE',
width_microns: 215900,
height_microns: 215900,
custom_display_name: 'CUSTOM_SQUARE'},
customMargins: {top: 74, right: 74, bottom: 74, left: 74},
vendorOptions: {},
marginsType: 3, /* custom */
scaling: '90',
isHeaderFooterEnabled: true,
isCssBackgroundEnabled: true,
isFitToPageEnabled: true,
isCollateEnabled: true,
isDuplexEnabled: true,
isLandscapeEnabled: true,
isColorEnabled: true,
};
return testInitializeWithStickySettings(stickySettings);
});
/**
* Tests state restoration with all boolean settings set to false, scaling =
* 120, dpi = 200, letter paper and default margins.
*/
test(assert(TestNames.RestoreFalseValues), function() {
const stickySettings = {
version: 2,
recentDestinations: [],
dpi: {horizontal_dpi: 200, vertical_dpi: 200},
mediaSize: {name: 'NA_LETTER',
width_microns: 215900,
height_microns: 279400,
is_default: true,
custom_display_name: 'Letter'},
customMargins: {},
vendorOptions: {},
marginsType: 0, /* default */
scaling: '120',
isHeaderFooterEnabled: false,
isCssBackgroundEnabled: false,
isFitToPageEnabled: false,
isCollateEnabled: false,
isDuplexEnabled: false,
isLandscapeEnabled: false,
isColorEnabled: false,
};
return testInitializeWithStickySettings(stickySettings);
});
});
return {
suiteName: suiteName,
TestNames: TestNames,
};
});
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