Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Side by Side Diff: chrome/test/data/webui/print_preview/print_preview_tests.js

Issue 2962983002: Print Preview: change getPreview to cr.sendWithPromise (Closed)
Patch Set: Address comments #2 Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 cr.define('print_preview_test', function() { 5 cr.define('print_preview_test', function() {
6 /** 6 /**
7 * Index of the "Save as PDF" printer. 7 * Index of the "Save as PDF" printer.
8 * @type {number} 8 * @type {number}
9 * @const 9 * @const
10 */ 10 */
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 CloudPrintInterfaceStub.prototype = { 267 CloudPrintInterfaceStub.prototype = {
268 __proto__: cr.EventTarget.prototype, 268 __proto__: cr.EventTarget.prototype,
269 search: function(isRecent) {} 269 search: function(isRecent) {}
270 }; 270 };
271 var oldCpInterfaceEventType = cloudprint.CloudPrintInterfaceEventType; 271 var oldCpInterfaceEventType = cloudprint.CloudPrintInterfaceEventType;
272 cloudprint.CloudPrintInterface = CloudPrintInterfaceStub; 272 cloudprint.CloudPrintInterface = CloudPrintInterfaceStub;
273 cloudprint.CloudPrintInterfaceEventType = oldCpInterfaceEventType; 273 cloudprint.CloudPrintInterfaceEventType = oldCpInterfaceEventType;
274 274
275 print_preview.PreviewArea.prototype.checkPluginCompatibility_ = 275 print_preview.PreviewArea.prototype.checkPluginCompatibility_ =
276 function() { 276 function() {
277 return false; 277 return true;
278 }; 278 };
279 }); 279 });
280 280
281 setup(function() { 281 setup(function() {
282 initialSettings = new print_preview.NativeInitialSettings( 282 initialSettings = new print_preview.NativeInitialSettings(
283 false /*isInKioskAutoPrintMode*/, 283 false /*isInKioskAutoPrintMode*/,
284 false /*isInAppKioskMode*/, 284 false /*isInAppKioskMode*/,
285 ',' /*thousandsDelimeter*/, 285 ',' /*thousandsDelimeter*/,
286 '.' /*decimalDelimeter*/, 286 '.' /*decimalDelimeter*/,
287 1 /*unitType*/, 287 1 /*unitType*/,
288 true /*isDocumentModifiable*/, 288 true /*isDocumentModifiable*/,
289 'title' /*documentTitle*/, 289 'title' /*documentTitle*/,
290 true /*documentHasSelection*/, 290 true /*documentHasSelection*/,
291 false /*selectionOnly*/, 291 false /*selectionOnly*/,
292 'FooDevice' /*systemDefaultDestinationId*/, 292 'FooDevice' /*systemDefaultDestinationId*/,
293 null /*serializedAppStateStr*/, 293 null /*serializedAppStateStr*/,
294 null /*serializedDefaultDestinationSelectionRulesStr*/); 294 null /*serializedDefaultDestinationSelectionRulesStr*/);
295 295
296 localDestinationInfos = [ 296 localDestinationInfos = [
297 { printerName: 'FooName', deviceName: 'FooDevice' }, 297 { printerName: 'FooName', deviceName: 'FooDevice' },
298 { printerName: 'BarName', deviceName: 'BarDevice' }, 298 { printerName: 'BarName', deviceName: 'BarDevice' },
299 ]; 299 ];
300 300
301 nativeLayer = new print_preview.NativeLayerStub(); 301 nativeLayer = new print_preview.NativeLayerStub();
302 print_preview.NativeLayer.setInstance(nativeLayer); 302 print_preview.NativeLayer.setInstance(nativeLayer);
303 printPreview = new print_preview.PrintPreview(); 303 printPreview = new print_preview.PrintPreview();
304 previewArea = printPreview.getPreviewArea(); 304 previewArea = printPreview.getPreviewArea();
305 previewArea.setIsBrowserTest(true);
305 }); 306 });
306 307
307 // Test some basic assumptions about the print preview WebUI. 308 // Test some basic assumptions about the print preview WebUI.
308 test('PrinterList', function() { 309 test('PrinterList', function() {
309 return setupSettingsAndDestinationsWithCapabilities().then(function() { 310 return setupSettingsAndDestinationsWithCapabilities().then(function() {
310 var recentList = 311 var recentList =
311 $('destination-search').querySelector('.recent-list ul'); 312 $('destination-search').querySelector('.recent-list ul');
312 var localList = 313 var localList =
313 $('destination-search').querySelector('.local-list ul'); 314 $('destination-search').querySelector('.local-list ul');
314 assertNotEquals(null, recentList); 315 assertNotEquals(null, recentList);
(...skipping 757 matching lines...) Expand 10 before | Expand all | Expand 10 after
1072 // Now it should be visible. 1073 // Now it should be visible.
1073 checkSectionVisible(otherOptions, true); 1074 checkSectionVisible(otherOptions, true);
1074 expectTrue(otherOptions.querySelector('#duplex-container').hidden); 1075 expectTrue(otherOptions.querySelector('#duplex-container').hidden);
1075 1076
1076 return whenAnimationDone('more-settings'); 1077 return whenAnimationDone('more-settings');
1077 }); 1078 });
1078 }); 1079 });
1079 1080
1080 // Test that changing the selected printer updates the preview. 1081 // Test that changing the selected printer updates the preview.
1081 test('PrinterChangeUpdatesPreview', function() { 1082 test('PrinterChangeUpdatesPreview', function() {
1082 return setupSettingsAndDestinationsWithCapabilities().then(function() { 1083 return Promise.all([
1083 var previewGenerator = mock(print_preview.PreviewGenerator); 1084 setupSettingsAndDestinationsWithCapabilities(),
1084 previewArea.previewGenerator_ = previewGenerator.proxy(); 1085 nativeLayer.whenCalled('getPreview'),
1085 1086 ]).then(function(args) {
1086 // The number of settings that can change due to a change in the 1087 expectEquals(0, args[1].requestId);
1087 // destination that will therefore dispatch ticket item change events. 1088 expectEquals('FooDevice', args[1].destination.id);
1088 previewGenerator.expects(exactly(9)).requestPreview(); 1089 nativeLayer.resetResolver('getPrinterCapabilities');
1090 nativeLayer.resetResolver('getPreview');
1089 1091
1090 // Setup capabilities for BarDevice. 1092 // Setup capabilities for BarDevice.
1091 var device = getCddTemplate('BarDevice'); 1093 var device = getCddTemplate('BarDevice');
1092 device.capabilities.printer.color = { 1094 device.capabilities.printer.color = {
1093 'option': [ 1095 'option': [
1094 {'is_default': true, 'type': 'STANDARD_MONOCHROME'} 1096 {'is_default': true, 'type': 'STANDARD_MONOCHROME'}
1095 ] 1097 ]
1096 }; 1098 };
1097 nativeLayer.setLocalDestinationCapabilities(device); 1099 nativeLayer.setLocalDestinationCapabilities(device);
1098
1099 // Select BarDevice 1100 // Select BarDevice
1100 var barDestination = 1101 var barDestination =
1101 printPreview.destinationStore_.destinations().find( 1102 printPreview.destinationStore_.destinations().find(
1102 function(d) { 1103 function(d) {
1103 return d.id == 'BarDevice'; 1104 return d.id == 'BarDevice';
1104 }); 1105 });
1105 printPreview.destinationStore_.selectDestination(barDestination); 1106 printPreview.destinationStore_.selectDestination(barDestination);
1106 return nativeLayer.whenCalled('getPrinterCapabilities', 'BarDevice'); 1107 return Promise.all([
1107 }).then(function(){ 1108 nativeLayer.whenCalled('getPrinterCapabilities'),
1108 return whenAnimationDone('more-settings'); 1109 nativeLayer.whenCalled('getPreview'),
1110 ]);
1111 }).then(function(args) {
1112 expectEquals(1, args[1].requestId);
1113 expectEquals('BarDevice', args[1].destination.id);
1109 }); 1114 });
1110 }); 1115 });
1111 1116
1112 // Test that error message is displayed when plugin doesn't exist. 1117 // Test that error message is displayed when plugin doesn't exist.
1113 test('NoPDFPluginErrorMessage', function() { 1118 test('NoPDFPluginErrorMessage', function() {
1119 previewArea.checkPluginCompatibility_ = function() {
1120 return false;
1121 }
1114 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('FooDevice')); 1122 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('FooDevice'));
1115 setInitialSettings(); 1123 setInitialSettings();
1116 return nativeLayer.whenCalled('getInitialSettings').then(function() { 1124 return nativeLayer.whenCalled('getInitialSettings').then(function() {
1117 var previewAreaEl = $('preview-area'); 1125 var previewAreaEl = $('preview-area');
1118 1126
1119 var loadingMessageEl = 1127 var loadingMessageEl =
1120 previewAreaEl. 1128 previewAreaEl.
1121 getElementsByClassName('preview-area-loading-message')[0]; 1129 getElementsByClassName('preview-area-loading-message')[0];
1122 expectTrue(loadingMessageEl.hidden); 1130 expectTrue(loadingMessageEl.hidden);
1123 1131
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
1240 extensionId: '', extensionName: '' 1248 extensionId: '', extensionName: ''
1241 }; 1249 };
1242 }), 1250 }),
1243 }); 1251 });
1244 1252
1245 // Ensure all capabilities are available for fetch. 1253 // Ensure all capabilities are available for fetch.
1246 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID1')); 1254 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID1'));
1247 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID2')) 1255 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID2'))
1248 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID3')); 1256 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID3'));
1249 1257
1250 // Use a real preview generator. 1258 // For crbug.com/666595. If multiple destinations are fetched there may
1251 previewArea.previewGenerator_ = 1259 // be multiple preview requests. This verifies the first fetch is for
1252 new print_preview.PreviewGenerator(printPreview.destinationStore_, 1260 // ID1, which ensures no other destinations are fetched earlier. The last
1253 printPreview.printTicketStore_, nativeLayer, 1261 // destination retrieved before timeout will end up in the preview
1254 printPreview.documentInfo_); 1262 // request. Ensure this is also ID1.
1255
1256 // Preview generator starts out with inFlightRequestId_ == -1. The id
1257 // increments by 1 for each startGetPreview call it makes. It should only
1258 // make one such call during initialization or there will be a race; see
1259 // crbug.com/666595
1260 expectEquals(-1, previewArea.previewGenerator_.inFlightRequestId_);
1261 setInitialSettings(); 1263 setInitialSettings();
1262 return nativeLayer.whenCalled('getInitialSettings').then(function() { 1264 var initialSettingsSet = nativeLayer.whenCalled('getInitialSettings');
1263 return nativeLayer.whenCalled('getPrinterCapabilities', 'ID1'); 1265 return initialSettingsSet.then(function() {
1264 }).then(function() { 1266 return nativeLayer.whenCalled('getPrinterCapabilities');
1265 expectEquals(0, previewArea.previewGenerator_.inFlightRequestId_); 1267 }).then(function(id) {
1268 expectEquals('ID1', id);
1269 return nativeLayer.whenCalled('getPreview');
1270 }).then(function(previewArgs) {
1271 expectEquals(0, previewArgs.requestId);
1272 expectEquals('ID1', previewArgs.destination.id);
1266 }); 1273 });
1267 }); 1274 });
1268 1275
1269 // Test that invalid settings errors disable the print preview and display 1276 // Test that invalid settings errors disable the print preview and display
1270 // an error and that the preview dialog can be recovered by selecting a 1277 // an error and that the preview dialog can be recovered by selecting a
1271 // new destination. 1278 // new destination.
1272 test('InvalidSettingsError', function() { 1279 test('InvalidSettingsError', function() {
1273 return setupSettingsAndDestinationsWithCapabilities().then(function() { 1280 var barDevice = getCddTemplate('BarDevice');
1274 // Manually enable the print header. This is needed since there is no 1281 nativeLayer.setLocalDestinationCapabilities(barDevice);
1275 // plugin during test, so it will be set as disabled normally. 1282 nativeLayer.setInvalidPrinterId('FooDevice');
dpapad 2017/06/29 23:28:31 Nit: Maybe add a comment that "FooDevice" is the c
rbpotter 2017/06/30 18:46:24 Done.
1276 printPreview.printHeader_.isEnabled = true; 1283 return Promise.all([
1277 1284 setupSettingsAndDestinationsWithCapabilities(),
1285 nativeLayer.whenCalled('getPreview'),
1286 ]).then(function() {
1278 // There will be an error message in the preview area since the plugin 1287 // There will be an error message in the preview area since the plugin
1279 // is not running. However, it should not be the invalid settings 1288 // is not running. However, it should not be the invalid settings
dpapad 2017/06/29 23:28:31 I think the 2nd sentence is no longer accurate.
rbpotter 2017/06/30 18:46:24 Done.
1280 // error. 1289 // error.
1281 var previewAreaEl = $('preview-area'); 1290 var previewAreaEl = $('preview-area');
1282 var customMessageEl = 1291 var customMessageEl =
1283 previewAreaEl. 1292 previewAreaEl.
1284 getElementsByClassName('preview-area-custom-message')[0]; 1293 getElementsByClassName('preview-area-custom-message')[0];
1285 expectFalse(customMessageEl.hidden); 1294 expectFalse(customMessageEl.hidden);
1286 var expectedMessageStart = 'The selected printer is not available or ' 1295 var expectedMessageStart = 'The selected printer is not available or '
1287 + 'not installed correctly.' 1296 + 'not installed correctly.'
1288 expectFalse(customMessageEl.textContent.includes( 1297 expectTrue(customMessageEl.textContent.includes(
1289 expectedMessageStart)); 1298 expectedMessageStart));
1290 1299
1291 // Verify that the print button is enabled. 1300 // Verify that the print button is disabled
1292 var printHeader = $('print-header'); 1301 var printButton = $('print-header').querySelector('button.print');
1293 var printButton = printHeader.querySelector('button.print');
1294 checkElementDisplayed(printButton, true); 1302 checkElementDisplayed(printButton, true);
1295 expectFalse(printButton.disabled); 1303 expectTrue(printButton.disabled);
1296 1304
1297 // Report invalid settings error. 1305 // Reset
1298 var invalidSettingsEvent = 1306 nativeLayer.resetResolver('getPrinterCapabilities');
1299 new Event(print_preview.NativeLayer.EventType.SETTINGS_INVALID); 1307 nativeLayer.resetResolver('getPreview');
1300 nativeLayer.getEventTarget().dispatchEvent(invalidSettingsEvent);
1301
1302 // Should be in an error state, print button disabled, invalid custom
1303 // error message shown.
1304 expectFalse(customMessageEl.hidden);
1305 expectTrue(customMessageEl.textContent.includes(
1306 expectedMessageStart));
1307 expectTrue(printButton.disabled);
1308 1308
1309 // Select a new destination 1309 // Select a new destination
1310 var barDestination = 1310 var barDestination =
1311 printPreview.destinationStore_.destinations().find( 1311 printPreview.destinationStore_.destinations().find(
1312 function(d) { 1312 function(d) {
1313 return d.id == 'BarDevice'; 1313 return d.id == 'BarDevice';
1314 }); 1314 });
1315
1316 var barDevice = getCddTemplate('BarDevice');
1317 nativeLayer.setLocalDestinationCapabilities(barDevice);
1318 printPreview.destinationStore_.selectDestination(barDestination); 1315 printPreview.destinationStore_.selectDestination(barDestination);
1319 1316 return Promise.all([
1320 return nativeLayer.whenCalled('getPrinterCapabilities', 'BarDevice') 1317 nativeLayer.whenCalled('getPrinterCapabilities'),
1321 .then(function() { 1318 nativeLayer.whenCalled('getPreview'),
1322 // Dispatch event indicating new preview has loaded. 1319 ]);
1323 var previewDoneEvent = new Event( 1320 }).then(function() {
1324 print_preview.PreviewArea.EventType.PREVIEW_GENERATION_DONE); 1321 // Has active print button and successfully 'prints', indicating
1325 previewArea.dispatchEvent(previewDoneEvent); 1322 // recovery from error state.
1326 1323 var printButton = $('print-header').querySelector('button.print');
1327 // Has active print button and successfully 'prints', indicating 1324 expectFalse(printButton.disabled);
1328 // recovery from error state. 1325 printButton.click();
1329 expectFalse(printButton.disabled); 1326 // This should result in a call to print.
1330 printButton.click(); 1327 return nativeLayer.whenCalled('print');
1331 // This should result in a call to print. 1328 }).then(
1332 return nativeLayer.whenCalled('print').then( 1329 /**
1333 /** 1330 * @param {{destination: !print_preview.Destination,
1334 * @param {{destination: !print_preview.Destination, 1331 * printTicketStore: !print_preview.PrintTicketStore,
1335 * printTicketStore: !print_preview.PrintTicketStore, 1332 * cloudPrintInterface: print_preview
1336 * cloudPrintInterface: print_preview 1333 * .CloudPrintInterface,
1337 * .CloudPrintInterface, 1334 * documentInfo: print_preview.DocumentInfo}} args
1338 * documentInfo: print_preview.DocumentInfo}} args 1335 * The arguments that print() was called with.
1339 * The arguments that print() was called with. 1336 */
1340 */ 1337 function(args) {
1341 function(args) { 1338 // Sanity check some printing argument values.
1342 // Sanity check some printing argument values. 1339 var printTicketStore = args.printTicketStore;
1343 var printTicketStore = args.printTicketStore; 1340 expectEquals(barDevice.printerId, args.destination.id);
1344 expectEquals(barDevice.printerId, args.destination.id); 1341 expectEquals(
1345 expectEquals( 1342 getDefaultOrientation(barDevice) == 'LANDSCAPE',
1346 getDefaultOrientation(barDevice) == 'LANDSCAPE', 1343 printTicketStore.landscape.getValue());
1347 printTicketStore.landscape.getValue()); 1344 expectEquals(1, printTicketStore.copies.getValueAsNumber());
1348 expectEquals(1, printTicketStore.copies.getValueAsNumber()); 1345 var mediaDefault = getDefaultMediaSize(barDevice);
1349 var mediaDefault = getDefaultMediaSize(barDevice); 1346 expectEquals(
1350 expectEquals( 1347 mediaDefault.width_microns,
1351 mediaDefault.width_microns, 1348 printTicketStore.mediaSize.getValue().width_microns);
1352 printTicketStore.mediaSize.getValue().width_microns); 1349 expectEquals(
1353 expectEquals( 1350 mediaDefault.height_microns,
1354 mediaDefault.height_microns, 1351 printTicketStore.mediaSize.getValue().height_microns);
1355 printTicketStore.mediaSize.getValue().height_microns); 1352 });
1356 });
1357 });
1358 });
1359 }); 1353 });
1360 1354
1361 // Test the preview generator to make sure the generate draft parameter is 1355 // Test the preview generator to make sure the generate draft parameter is
1362 // set correctly. It should be false if the only change is the page range. 1356 // set correctly. It should be false if the only change is the page range.
1363 test('GenerateDraft', function() { 1357 test('GenerateDraft', function() {
1364 // Use a real preview generator. 1358 return Promise.all([
1365 previewArea.previewGenerator_ = 1359 setupSettingsAndDestinationsWithCapabilities(),
1366 new print_preview.PreviewGenerator(printPreview.destinationStore_, 1360 nativeLayer.whenCalled('getPreview'),
1367 printPreview.printTicketStore_, nativeLayer, 1361 ]).then(function(args) {
1368 printPreview.documentInfo_);
1369 return setupSettingsAndDestinationsWithCapabilities().then(function() {
1370 // The first request should generate draft because there was no 1362 // The first request should generate draft because there was no
1371 // previous print preview draft. 1363 // previous print preview draft.
1372 expectTrue(nativeLayer.generateDraft()); 1364 expectTrue(args[1].generateDraft);
1365 expectEquals(0, args[1].requestId);
1366 nativeLayer.resetResolver('getPreview');
1373 1367
1374 // Change the page range - no new draft needed. 1368 // Change the page range - no new draft needed.
1375 printPreview.printTicketStore_.pageRange.updateValue('2'); 1369 printPreview.printTicketStore_.pageRange.updateValue('2');
1376 expectFalse(nativeLayer.generateDraft()); 1370 return nativeLayer.whenCalled('getPreview');
1371 }).then(function(args) {
1372 expectFalse(args.generateDraft);
1373 expectEquals(1, args.requestId);
1374 nativeLayer.resetResolver('getPreview');
1377 1375
1378 // Change the margin type - need to regenerate again. 1376 // Change the margin type - need to regenerate again.
1379 printPreview.printTicketStore_.marginsType.updateValue( 1377 printPreview.printTicketStore_.marginsType.updateValue(
1380 print_preview.ticket_items.MarginsTypeValue.NO_MARGINS); 1378 print_preview.ticket_items.MarginsTypeValue.NO_MARGINS);
1381 expectTrue(nativeLayer.generateDraft()); 1379 return nativeLayer.whenCalled('getPreview');
1380 }).then(function(args) {
1381 expectTrue(args.generateDraft);
1382 expectEquals(2, args.requestId);
1382 }); 1383 });
1383 }); 1384 });
1384 1385
1385 // Test that the policy to use the system default printer by default 1386 // Test that the policy to use the system default printer by default
1386 // instead of the most recently used destination works. 1387 // instead of the most recently used destination works.
1387 test('SystemDefaultPrinterPolicy', function() { 1388 test('SystemDefaultPrinterPolicy', function() {
1388 // Add recent destination. 1389 // Add recent destination.
1389 initialSettings.serializedAppStateStr_ = JSON.stringify({ 1390 initialSettings.serializedAppStateStr_ = JSON.stringify({
1390 version: 2, 1391 version: 2,
1391 recentDestinations: [ 1392 recentDestinations: [
(...skipping 20 matching lines...) Expand all
1412 return setupSettingsAndDestinationsWithCapabilities().then(function() { 1413 return setupSettingsAndDestinationsWithCapabilities().then(function() {
1413 // The system default destination should be used instead of the 1414 // The system default destination should be used instead of the
1414 // most recent destination. 1415 // most recent destination.
1415 assertEquals( 1416 assertEquals(
1416 'FooDevice', 1417 'FooDevice',
1417 printPreview.destinationStore_.selectedDestination.id); 1418 printPreview.destinationStore_.selectedDestination.id);
1418 }); 1419 });
1419 }); 1420 });
1420 }); 1421 });
1421 }); 1422 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698