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

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: Fix tests 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;
dpapad 2017/06/29 20:58:31 Why is this change necessary?
rbpotter 2017/06/29 22:31:22 This function determines if the Preview Generator
dpapad 2017/06/29 23:28:31 Thanks. Current version seems fine to me, now that
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 758 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 setupSettingsAndDestinationsWithCapabilities().then(function() {
1083 var previewGenerator = mock(print_preview.PreviewGenerator); 1084 return nativeLayer.whenCalled('getPreview');
1084 previewArea.previewGenerator_ = previewGenerator.proxy(); 1085 }).then(function(args0) {
dpapad 2017/06/29 20:58:31 Nit s/args0/args?
rbpotter 2017/06/29 22:31:23 Done.
1085 1086 expectEquals(0, args0.requestId);
1086 // The number of settings that can change due to a change in the 1087 expectEquals('FooDevice', args0.destination.id);
1087 // destination that will therefore dispatch ticket item change events. 1088 nativeLayer.resetResolver('getPrinterCapabilities');
1088 previewGenerator.expects(exactly(9)).requestPreview(); 1089 nativeLayer.resetResolver('getPreview');
1089 1090
1090 // Setup capabilities for BarDevice. 1091 // Setup capabilities for BarDevice.
1091 var device = getCddTemplate('BarDevice'); 1092 var device = getCddTemplate('BarDevice');
1092 device.capabilities.printer.color = { 1093 device.capabilities.printer.color = {
1093 'option': [ 1094 'option': [
1094 {'is_default': true, 'type': 'STANDARD_MONOCHROME'} 1095 {'is_default': true, 'type': 'STANDARD_MONOCHROME'}
1095 ] 1096 ]
1096 }; 1097 };
1097 nativeLayer.setLocalDestinationCapabilities(device); 1098 nativeLayer.setLocalDestinationCapabilities(device);
1098
1099 // Select BarDevice 1099 // Select BarDevice
1100 var barDestination = 1100 var barDestination =
1101 printPreview.destinationStore_.destinations().find( 1101 printPreview.destinationStore_.destinations().find(
1102 function(d) { 1102 function(d) {
1103 return d.id == 'BarDevice'; 1103 return d.id == 'BarDevice';
1104 }); 1104 });
1105 printPreview.destinationStore_.selectDestination(barDestination); 1105 printPreview.destinationStore_.selectDestination(barDestination);
1106 return nativeLayer.whenCalled('getPrinterCapabilities', 'BarDevice'); 1106 return nativeLayer.whenCalled('getPrinterCapabilities');
1107 }).then(function(){ 1107 }).then(function(){
1108 return whenAnimationDone('more-settings'); 1108 // Verify new preview is requested with new ID and destination.
1109 return nativeLayer.whenCalled('getPreview');
1110 }).then(function(args1) {
dpapad 2017/06/29 20:58:31 Same here. No need to use a different name, since
rbpotter 2017/06/29 22:31:23 Done.
1111 expectEquals(1, args1.requestId);
1112 expectEquals('BarDevice', args1.destination.id);
1109 }); 1113 });
1110 }); 1114 });
1111 1115
1112 // Test that error message is displayed when plugin doesn't exist. 1116 // Test that error message is displayed when plugin doesn't exist.
1113 test('NoPDFPluginErrorMessage', function() { 1117 test('NoPDFPluginErrorMessage', function() {
1118 previewArea.checkPluginCompatibility_ = function() {
1119 return false;
1120 }
1114 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('FooDevice')); 1121 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('FooDevice'));
1115 setInitialSettings(); 1122 setInitialSettings();
1116 return nativeLayer.whenCalled('getInitialSettings').then(function() { 1123 return nativeLayer.whenCalled('getInitialSettings').then(function() {
1117 var previewAreaEl = $('preview-area'); 1124 var previewAreaEl = $('preview-area');
1118 1125
1119 var loadingMessageEl = 1126 var loadingMessageEl =
1120 previewAreaEl. 1127 previewAreaEl.
1121 getElementsByClassName('preview-area-loading-message')[0]; 1128 getElementsByClassName('preview-area-loading-message')[0];
1122 expectTrue(loadingMessageEl.hidden); 1129 expectTrue(loadingMessageEl.hidden);
1123 1130
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
1240 extensionId: '', extensionName: '' 1247 extensionId: '', extensionName: ''
1241 }; 1248 };
1242 }), 1249 }),
1243 }); 1250 });
1244 1251
1245 // Ensure all capabilities are available for fetch. 1252 // Ensure all capabilities are available for fetch.
1246 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID1')); 1253 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID1'));
1247 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID2')) 1254 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID2'))
1248 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID3')); 1255 nativeLayer.setLocalDestinationCapabilities(getCddTemplate('ID3'));
1249 1256
1250 // Use a real preview generator. 1257 // For crbug.com/666595. If multiple destinations are fetched there may
1251 previewArea.previewGenerator_ = 1258 // be multiple preview requests. This verifies the first fetch is for
1252 new print_preview.PreviewGenerator(printPreview.destinationStore_, 1259 // ID1, which ensures no other destinations are fetched earlier. The last
1253 printPreview.printTicketStore_, nativeLayer, 1260 // destination retrieved before timeout will end up in the preview
1254 printPreview.documentInfo_); 1261 // 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(); 1262 setInitialSettings();
1262 return nativeLayer.whenCalled('getInitialSettings').then(function() { 1263 var initialSettingsSet = nativeLayer.whenCalled('getInitialSettings');
1263 return nativeLayer.whenCalled('getPrinterCapabilities', 'ID1'); 1264 return initialSettingsSet.then(function() {
1264 }).then(function() { 1265 return nativeLayer.whenCalled('getPrinterCapabilities');
1265 expectEquals(0, previewArea.previewGenerator_.inFlightRequestId_); 1266 }).then(function(id) {
1267 expectEquals('ID1', id);
1268 return nativeLayer.whenCalled('getPreview');
1269 }).then(function(preview_args) {
dpapad 2017/06/29 20:58:31 previewArgs
rbpotter 2017/06/29 22:31:22 Done.
1270 expectEquals(0, preview_args.requestId);
1271 expectEquals('ID1', preview_args.destination.id);
1266 }); 1272 });
1267 }); 1273 });
1268 1274
1269 // Test that invalid settings errors disable the print preview and display 1275 // 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 1276 // an error and that the preview dialog can be recovered by selecting a
1271 // new destination. 1277 // new destination.
1272 test('InvalidSettingsError', function() { 1278 test('InvalidSettingsError', function() {
1279 var barDevice = getCddTemplate('BarDevice');
1280 nativeLayer.setLocalDestinationCapabilities(barDevice);
1281 nativeLayer.setInvalidPrinterId('FooDevice');
1273 return setupSettingsAndDestinationsWithCapabilities().then(function() { 1282 return setupSettingsAndDestinationsWithCapabilities().then(function() {
1274 // Manually enable the print header. This is needed since there is no 1283 return nativeLayer.whenCalled('getPreview');
1275 // plugin during test, so it will be set as disabled normally. 1284 }).then(function() {
1276 printPreview.printHeader_.isEnabled = true;
1277
1278 // There will be an error message in the preview area since the plugin 1285 // 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 1286 // is not running. However, it should not be the invalid settings
1280 // error. 1287 // error.
1281 var previewAreaEl = $('preview-area'); 1288 var previewAreaEl = $('preview-area');
1282 var customMessageEl = 1289 var customMessageEl =
1283 previewAreaEl. 1290 previewAreaEl.
1284 getElementsByClassName('preview-area-custom-message')[0]; 1291 getElementsByClassName('preview-area-custom-message')[0];
1285 expectFalse(customMessageEl.hidden); 1292 expectFalse(customMessageEl.hidden);
1286 var expectedMessageStart = 'The selected printer is not available or ' 1293 var expectedMessageStart = 'The selected printer is not available or '
1287 + 'not installed correctly.' 1294 + 'not installed correctly.'
1288 expectFalse(customMessageEl.textContent.includes( 1295 expectTrue(customMessageEl.textContent.includes(
1289 expectedMessageStart)); 1296 expectedMessageStart));
1290 1297
1291 // Verify that the print button is enabled. 1298 // Verify that the print button is disabled
1292 var printHeader = $('print-header'); 1299 var printButton = $('print-header').querySelector('button.print');
1293 var printButton = printHeader.querySelector('button.print');
1294 checkElementDisplayed(printButton, true); 1300 checkElementDisplayed(printButton, true);
1295 expectFalse(printButton.disabled); 1301 expectTrue(printButton.disabled);
1296 1302
1297 // Report invalid settings error. 1303 // Reset
1298 var invalidSettingsEvent = 1304 nativeLayer.resetResolver('getPrinterCapabilities');
1299 new Event(print_preview.NativeLayer.EventType.SETTINGS_INVALID); 1305 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 1306
1309 // Select a new destination 1307 // Select a new destination
1310 var barDestination = 1308 var barDestination =
1311 printPreview.destinationStore_.destinations().find( 1309 printPreview.destinationStore_.destinations().find(
1312 function(d) { 1310 function(d) {
1313 return d.id == 'BarDevice'; 1311 return d.id == 'BarDevice';
1314 }); 1312 });
1315
1316 var barDevice = getCddTemplate('BarDevice');
1317 nativeLayer.setLocalDestinationCapabilities(barDevice);
1318 printPreview.destinationStore_.selectDestination(barDestination); 1313 printPreview.destinationStore_.selectDestination(barDestination);
1319 1314 return nativeLayer.whenCalled('getPrinterCapabilities');
1320 return nativeLayer.whenCalled('getPrinterCapabilities', 'BarDevice') 1315 }).then(function() {
dpapad 2017/06/29 20:58:31 If you don't need to do anything here, then how ab
rbpotter 2017/06/29 22:31:23 Done.
1321 .then(function() { 1316 return nativeLayer.whenCalled('getPreview');
1322 // Dispatch event indicating new preview has loaded. 1317 }).then(function() {
1323 var previewDoneEvent = new Event( 1318 // Has active print button and successfully 'prints', indicating
1324 print_preview.PreviewArea.EventType.PREVIEW_GENERATION_DONE); 1319 // recovery from error state.
1325 previewArea.dispatchEvent(previewDoneEvent); 1320 var printButton = $('print-header').querySelector('button.print');
1326 1321 expectFalse(printButton.disabled);
1327 // Has active print button and successfully 'prints', indicating 1322 printButton.click();
1328 // recovery from error state. 1323 // This should result in a call to print.
1329 expectFalse(printButton.disabled); 1324 return nativeLayer.whenCalled('print');
1330 printButton.click(); 1325 }).then(
1331 // This should result in a call to print. 1326 /**
1332 return nativeLayer.whenCalled('print').then( 1327 * @param {{destination: !print_preview.Destination,
1333 /** 1328 * printTicketStore: !print_preview.PrintTicketStore,
1334 * @param {{destination: !print_preview.Destination, 1329 * cloudPrintInterface: print_preview
1335 * printTicketStore: !print_preview.PrintTicketStore, 1330 * .CloudPrintInterface,
1336 * cloudPrintInterface: print_preview 1331 * documentInfo: print_preview.DocumentInfo}} args
1337 * .CloudPrintInterface, 1332 * The arguments that print() was called with.
1338 * documentInfo: print_preview.DocumentInfo}} args 1333 */
1339 * The arguments that print() was called with. 1334 function(args) {
1340 */ 1335 // Sanity check some printing argument values.
1341 function(args) { 1336 var printTicketStore = args.printTicketStore;
1342 // Sanity check some printing argument values. 1337 expectEquals(barDevice.printerId, args.destination.id);
1343 var printTicketStore = args.printTicketStore; 1338 expectEquals(
1344 expectEquals(barDevice.printerId, args.destination.id); 1339 getDefaultOrientation(barDevice) == 'LANDSCAPE',
1345 expectEquals( 1340 printTicketStore.landscape.getValue());
1346 getDefaultOrientation(barDevice) == 'LANDSCAPE', 1341 expectEquals(1, printTicketStore.copies.getValueAsNumber());
1347 printTicketStore.landscape.getValue()); 1342 var mediaDefault = getDefaultMediaSize(barDevice);
1348 expectEquals(1, printTicketStore.copies.getValueAsNumber()); 1343 expectEquals(
1349 var mediaDefault = getDefaultMediaSize(barDevice); 1344 mediaDefault.width_microns,
1350 expectEquals( 1345 printTicketStore.mediaSize.getValue().width_microns);
1351 mediaDefault.width_microns, 1346 expectEquals(
1352 printTicketStore.mediaSize.getValue().width_microns); 1347 mediaDefault.height_microns,
1353 expectEquals( 1348 printTicketStore.mediaSize.getValue().height_microns);
1354 mediaDefault.height_microns, 1349 });
1355 printTicketStore.mediaSize.getValue().height_microns);
1356 });
1357 });
1358 });
1359 }); 1350 });
1360 1351
1361 // Test the preview generator to make sure the generate draft parameter is 1352 // 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. 1353 // set correctly. It should be false if the only change is the page range.
1363 test('GenerateDraft', function() { 1354 test('GenerateDraft', function() {
1364 // Use a real preview generator.
1365 previewArea.previewGenerator_ =
1366 new print_preview.PreviewGenerator(printPreview.destinationStore_,
1367 printPreview.printTicketStore_, nativeLayer,
1368 printPreview.documentInfo_);
1369 return setupSettingsAndDestinationsWithCapabilities().then(function() { 1355 return setupSettingsAndDestinationsWithCapabilities().then(function() {
1356 return nativeLayer.whenCalled('getPreview');
1357 }).then(function(args0) {
1370 // The first request should generate draft because there was no 1358 // The first request should generate draft because there was no
1371 // previous print preview draft. 1359 // previous print preview draft.
1372 expectTrue(nativeLayer.generateDraft()); 1360 expectTrue(args0.generateDraft);
1361 expectEquals(0, args0.requestId);
1362 nativeLayer.resetResolver('getPreview');
1373 1363
1374 // Change the page range - no new draft needed. 1364 // Change the page range - no new draft needed.
1375 printPreview.printTicketStore_.pageRange.updateValue('2'); 1365 printPreview.printTicketStore_.pageRange.updateValue('2');
1376 expectFalse(nativeLayer.generateDraft()); 1366 return nativeLayer.whenCalled('getPreview');
1367 }).then(function(args1) {
1368 expectFalse(args1.generateDraft);
1369 expectEquals(1, args1.requestId);
1370 nativeLayer.resetResolver('getPreview');
1377 1371
1378 // Change the margin type - need to regenerate again. 1372 // Change the margin type - need to regenerate again.
1379 printPreview.printTicketStore_.marginsType.updateValue( 1373 printPreview.printTicketStore_.marginsType.updateValue(
1380 print_preview.ticket_items.MarginsTypeValue.NO_MARGINS); 1374 print_preview.ticket_items.MarginsTypeValue.NO_MARGINS);
1381 expectTrue(nativeLayer.generateDraft()); 1375 return nativeLayer.whenCalled('getPreview');
1376 }).then(function(args2) {
1377 expectTrue(args2.generateDraft);
1378 expectEquals(2, args2.requestId);
1382 }); 1379 });
1383 }); 1380 });
1384 1381
1385 // Test that the policy to use the system default printer by default 1382 // Test that the policy to use the system default printer by default
1386 // instead of the most recently used destination works. 1383 // instead of the most recently used destination works.
1387 test('SystemDefaultPrinterPolicy', function() { 1384 test('SystemDefaultPrinterPolicy', function() {
1388 // Add recent destination. 1385 // Add recent destination.
1389 initialSettings.serializedAppStateStr_ = JSON.stringify({ 1386 initialSettings.serializedAppStateStr_ = JSON.stringify({
1390 version: 2, 1387 version: 2,
1391 recentDestinations: [ 1388 recentDestinations: [
(...skipping 20 matching lines...) Expand all
1412 return setupSettingsAndDestinationsWithCapabilities().then(function() { 1409 return setupSettingsAndDestinationsWithCapabilities().then(function() {
1413 // The system default destination should be used instead of the 1410 // The system default destination should be used instead of the
1414 // most recent destination. 1411 // most recent destination.
1415 assertEquals( 1412 assertEquals(
1416 'FooDevice', 1413 'FooDevice',
1417 printPreview.destinationStore_.selectedDestination.id); 1414 printPreview.destinationStore_.selectedDestination.id);
1418 }); 1415 });
1419 }); 1416 });
1420 }); 1417 });
1421 }); 1418 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698