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

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

Issue 2910503003: Print Preview: Migrate JS tests to use Mocha, part 2. (Closed)
Patch Set: Nit Created 3 years, 6 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 13 matching lines...) Expand all
24 */ 24 */
25 var BAR_INDEX = 2; 25 var BAR_INDEX = 2;
26 26
27 var printPreview = null; 27 var printPreview = null;
28 var nativeLayer = null; 28 var nativeLayer = null;
29 var initialSettings = null; 29 var initialSettings = null;
30 var localDestinationInfos = null; 30 var localDestinationInfos = null;
31 var previewArea = null; 31 var previewArea = null;
32 32
33 /** 33 /**
34 * Creates an instance of print_preview.PrintPreview and initializes the
35 * |nativeLayer| and |previewArea|.
36 */
37 function createPrintPreview() {
38 nativeLayer = new print_preview.NativeLayerStub();
39 print_preview.NativeLayer.setInstance(nativeLayer);
40 printPreview = new print_preview.PrintPreview();
41 previewArea = printPreview.getPreviewArea();
42 }
43
44 /**
34 * Initialize print preview with the initial settings currently stored in 45 * Initialize print preview with the initial settings currently stored in
35 * |initialSettings|. Creates |printPreview| if it does not 46 * |initialSettings|. Creates |printPreview| if it does not
36 * already exist. 47 * already exist.
37 */ 48 */
38 function setInitialSettings() { 49 function setInitialSettings() {
39 nativeLayer.setInitialSettings(initialSettings); 50 nativeLayer.setInitialSettings(initialSettings);
40 printPreview.initialize(); 51 printPreview.initialize();
41 } 52 }
42 53
43 /** 54 /**
(...skipping 24 matching lines...) Expand all
68 * @param {HTMLDivElement} section The section to check. 79 * @param {HTMLDivElement} section The section to check.
69 * @param {boolean} visible The expected state of visibility. 80 * @param {boolean} visible The expected state of visibility.
70 */ 81 */
71 function checkSectionVisible(section, visible) { 82 function checkSectionVisible(section, visible) {
72 assertNotEquals(null, section); 83 assertNotEquals(null, section);
73 expectEquals( 84 expectEquals(
74 visible, 85 visible,
75 section.classList.contains('visible'), 'section=' + section.id); 86 section.classList.contains('visible'), 'section=' + section.id);
76 } 87 }
77 88
89 function checkElementDisplayed(el, isDisplayed) {
rbpotter 2017/05/26 18:50:17 @param {HTMLElement} el @param {boolean} isDisplay
dpapad 2017/05/26 18:57:54 Done.
90 assertNotEquals(null, el);
91 expectEquals(isDisplayed,
92 !el.hidden,
93 'element="' + el.id + '" of class "' + el.classList + '"');
94 }
95
78 function getCddTemplate(printerId) { 96 function getCddTemplate(printerId) {
79 return { 97 return {
80 printerId: printerId, 98 printerId: printerId,
81 capabilities: { 99 capabilities: {
82 version: '1.0', 100 version: '1.0',
83 printer: { 101 printer: {
84 supported_content_type: [{content_type: 'application/pdf'}], 102 supported_content_type: [{content_type: 'application/pdf'}],
85 collate: {}, 103 collate: {},
86 color: { 104 color: {
87 option: [ 105 option: [
(...skipping 23 matching lines...) Expand all
111 height_microns: 279400, 129 height_microns: 279400,
112 is_default: true 130 is_default: true
113 } 131 }
114 ] 132 ]
115 } 133 }
116 } 134 }
117 } 135 }
118 }; 136 };
119 } 137 }
120 138
139 /**
140 * Even though animation duration and delay is set to zero, it is necessary to
141 * wait until the animation has finished.
142 * @return {!Promise} A promise firing when the animation is done.
143 */
144 function whenAnimationDone(elementId) {
145 return new Promise(function(resolve) {
146 // Add a listener for the animation end event.
147 var element = $(elementId);
148 element.addEventListener('animationend', function f(e) {
149 element.removeEventListener(f, 'animationend');
150 resolve();
151 });
152 });
153 }
154
155 /**
156 * Expand the 'More Settings' div to expose all options.
157 */
158 function expandMoreSettings() {
159 var moreSettings = $('more-settings');
160 checkSectionVisible(moreSettings, true);
161 moreSettings.click();
162 }
163
164 /** @return {boolean} */
165 function isPrintAsImageEnabled() {
166 // Should be enabled by default on non Windows/Mac.
167 return (!cr.isWindows && !cr.isMac &&
168 loadTimeData.getBoolean('printPdfAsImageEnabled'));
169 }
170
121 suite('PrintPreview', function() { 171 suite('PrintPreview', function() {
122 setup(function() { 172 setup(function() {
123 initialSettings = new print_preview.NativeInitialSettings( 173 initialSettings = new print_preview.NativeInitialSettings(
124 false /*isInKioskAutoPrintMode*/, 174 false /*isInKioskAutoPrintMode*/,
125 false /*isInAppKioskMode*/, 175 false /*isInAppKioskMode*/,
126 ',' /*thousandsDelimeter*/, 176 ',' /*thousandsDelimeter*/,
127 '.' /*decimalDelimeter*/, 177 '.' /*decimalDelimeter*/,
128 1 /*unitType*/, 178 1 /*unitType*/,
129 true /*isDocumentModifiable*/, 179 true /*isDocumentModifiable*/,
130 'title' /*documentTitle*/, 180 'title' /*documentTitle*/,
131 true /*documentHasSelection*/, 181 true /*documentHasSelection*/,
132 false /*selectionOnly*/, 182 false /*selectionOnly*/,
133 'FooDevice' /*systemDefaultDestinationId*/, 183 'FooDevice' /*systemDefaultDestinationId*/,
134 null /*serializedAppStateStr*/, 184 null /*serializedAppStateStr*/,
135 null /*serializedDefaultDestinationSelectionRulesStr*/); 185 null /*serializedDefaultDestinationSelectionRulesStr*/);
136 186
137 localDestinationInfos = [ 187 localDestinationInfos = [
138 { printerName: 'FooName', deviceName: 'FooDevice' }, 188 { printerName: 'FooName', deviceName: 'FooDevice' },
139 { printerName: 'BarName', deviceName: 'BarDevice' }, 189 { printerName: 'BarName', deviceName: 'BarDevice' },
140 ]; 190 ];
141 191
142 nativeLayer = new print_preview.NativeLayerStub(); 192 createPrintPreview();
rbpotter 2017/05/26 18:50:17 See comment below. If the second call in the "Sect
143 print_preview.NativeLayer.setInstance(nativeLayer);
144 printPreview = new print_preview.PrintPreview();
145 previewArea = printPreview.getPreviewArea();
146 }); 193 });
147 194
148 // Test some basic assumptions about the print preview WebUI. 195 // Test some basic assumptions about the print preview WebUI.
149 test('PrinterList', function() { 196 test('PrinterList', function() {
150 setInitialSettings(); 197 setInitialSettings();
151 return nativeLayer.whenCalled('getInitialSettings').then( 198 return nativeLayer.whenCalled('getInitialSettings').then(
152 function() { 199 function() {
153 setLocalDestinations(); 200 setLocalDestinations();
154 var recentList = 201 var recentList =
155 $('destination-search').querySelector('.recent-list ul'); 202 $('destination-search').querySelector('.recent-list ul');
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 initialSettings.serializedDefaultDestinationSelectionRulesStr_ = 379 initialSettings.serializedDefaultDestinationSelectionRulesStr_ =
333 JSON.stringify({namePattern: '.*Bar.*'}); 380 JSON.stringify({namePattern: '.*Bar.*'});
334 setInitialSettings(); 381 setInitialSettings();
335 return nativeLayer.whenCalled('getInitialSettings').then( 382 return nativeLayer.whenCalled('getInitialSettings').then(
336 function() { 383 function() {
337 setLocalDestinations(); 384 setLocalDestinations();
338 assertEquals( 385 assertEquals(
339 'BarDevice', 386 'BarDevice',
340 printPreview.destinationStore_.selectedDestination.id); 387 printPreview.destinationStore_.selectedDestination.id);
341 }); 388 });
342 }); 389 });
390
391 test('SystemDialogLinkIsHiddenInAppKioskMode', function() {
392 if (!cr.isChromeOS)
393 initialSettings.isInAppKioskMode_ = true;
394
395 setInitialSettings();
396 return nativeLayer.whenCalled('getInitialSettings').then(
397 function() {
398 if (cr.isChromeOS)
399 assertEquals(null, $('system-dialog-link'));
400 else
401 checkElementDisplayed($('system-dialog-link'), false);
402 });
403 });
404
405 test('SectionsDisabled', function() {
406 createPrintPreview();
rbpotter 2017/05/26 18:50:17 It looks like this is called in setup() already. D
dpapad 2017/05/26 18:57:54 Removed. It looks that it is not needed.
407 checkSectionVisible($('layout-settings'), false);
408 checkSectionVisible($('color-settings'), false);
409 checkSectionVisible($('copies-settings'), false);
410
411 setInitialSettings();
412 return nativeLayer.whenCalled('getInitialSettings').then(
413 function() {
414 setLocalDestinations();
415 var device = getCddTemplate('FooDevice');
416 device.capabilities.printer.color = {
417 option: [{is_default: true, type: 'STANDARD_COLOR'}]
418 };
419 delete device.capabilities.printer.copies;
420 setCapabilities(device);
421
422 checkSectionVisible($('layout-settings'), true);
423 checkSectionVisible($('color-settings'), false);
424 checkSectionVisible($('copies-settings'), false);
425
426 return whenAnimationDone('other-options-collapsible');
427 });
428 });
429
430 // When the source is 'PDF' and 'Save as PDF' option is selected, we hide
431 // the fit to page option.
432 test('PrintToPDFSelectedCapabilities', function() {
433 // Add PDF printer.
434 initialSettings.isDocumentModifiable_ = false;
435 initialSettings.systemDefaultDestinationId_ = 'Save as PDF';
436 setInitialSettings();
437
438 return nativeLayer.whenCalled('getInitialSettings').then(function() {
439 var device = {
440 printerId: 'Save as PDF',
441 capabilities: {
442 version: '1.0',
443 printer: {
444 page_orientation: {
445 option: [
446 {type: 'AUTO', is_default: true},
447 {type: 'PORTRAIT'},
448 {type: 'LANDSCAPE'}
449 ]
450 },
451 color: {
452 option: [
453 {type: 'STANDARD_COLOR', is_default: true}
454 ]
455 },
456 media_size: {
457 option: [
458 { name: 'NA_LETTER',
459 width_microns: 0,
460 height_microns: 0,
461 is_default: true
462 }
463 ]
464 }
465 }
466 }
467 };
468 setCapabilities(device);
469
470 var otherOptions = $('other-options-settings');
471 // If rasterization is an option, other options should be visible. If
472 // not, there should be no available other options.
473 checkSectionVisible(otherOptions, isPrintAsImageEnabled());
474 if (isPrintAsImageEnabled()) {
475 checkElementDisplayed(
476 otherOptions.querySelector('#fit-to-page-container'), false);
477 checkElementDisplayed(
478 otherOptions.querySelector('#rasterize-container'), true);
479 }
480 checkSectionVisible($('media-size-settings'), false);
481 checkSectionVisible($('scaling-settings'), false);
482 });
483 });
484
485 // When the source is 'HTML', we always hide the fit to page option and show
486 // media size option.
487 test('SourceIsHTMLCapabilities', function() {
488 setInitialSettings();
489 return nativeLayer.whenCalled('getInitialSettings').then(function() {
490 setLocalDestinations();
491 setCapabilities(getCddTemplate("FooDevice"));
rbpotter 2017/05/26 18:50:17 optional: set to single quotes for consistency wit
dpapad 2017/05/26 18:57:54 Done.
492
493 var otherOptions = $('other-options-settings');
494 var fitToPage = otherOptions.querySelector('#fit-to-page-container');
495 var rasterize;
496 if (isPrintAsImageEnabled())
497 rasterize = otherOptions.querySelector('#rasterize-container');
498 var mediaSize = $('media-size-settings');
499 var scalingSettings = $('scaling-settings');
500
501 // Check that options are collapsed (section is visible, because duplex
502 // is available).
503 checkSectionVisible(otherOptions, true);
504 checkElementDisplayed(fitToPage, false);
505 if (isPrintAsImageEnabled())
506 checkElementDisplayed(rasterize, false);
507 checkSectionVisible(mediaSize, false);
508 checkSectionVisible(scalingSettings, false);
509
510 expandMoreSettings();
511
512 checkElementDisplayed(fitToPage, false);
513 if (isPrintAsImageEnabled())
514 checkElementDisplayed(rasterize, false);
515 checkSectionVisible(mediaSize, true);
516 checkSectionVisible(scalingSettings, true);
517
518 return whenAnimationDone('more-settings');
519 });
520 });
521
522 // When the source is "PDF", depending on the selected destination printer,
523 // we show/hide the fit to page option and hide media size selection.
524 test('SourceIsPDFCapabilities', function() {
525 initialSettings.isDocumentModifiable_ = false;
526 setInitialSettings();
527 return nativeLayer.whenCalled('getInitialSettings').then(
528 function() {
529 setLocalDestinations();
530 setCapabilities(getCddTemplate("FooDevice"));
531
532 var otherOptions = $('other-options-settings');
533 var scalingSettings = $('scaling-settings');
534 var fitToPageContainer =
535 otherOptions.querySelector('#fit-to-page-container');
536 var rasterizeContainer;
537 if (isPrintAsImageEnabled()) {
538 rasterizeContainer =
539 otherOptions.querySelector('#rasterize-container');
540 }
541
542 checkSectionVisible(otherOptions, true);
543 checkElementDisplayed(fitToPageContainer, true);
544 if (isPrintAsImageEnabled())
545 checkElementDisplayed(rasterizeContainer, false);
546 expectTrue(
547 fitToPageContainer.querySelector('.checkbox').checked);
548 expandMoreSettings();
549 if (isPrintAsImageEnabled()) {
550 checkElementDisplayed(rasterizeContainer, true);
551 expectFalse(
552 rasterizeContainer.querySelector('.checkbox').checked);
553 }
554 checkSectionVisible($('media-size-settings'), true);
555 checkSectionVisible(scalingSettings, true);
556
557 return whenAnimationDone('other-options-collapsible');
558 });
559 });
560
561 // When the source is "PDF", depending on the selected destination printer,
562 // we show/hide the fit to page option and hide media size selection.
563 test('ScalingUnchecksFitToPage', function() {
564 initialSettings.isDocumentModifiable_ = false;
565 setInitialSettings();
566 return nativeLayer.whenCalled('getInitialSettings').then(
567 function() {
568 setLocalDestinations();
569 setCapabilities(getCddTemplate("FooDevice"));
570
571 var otherOptions = $('other-options-settings');
572 var scalingSettings = $('scaling-settings');
573
574 checkSectionVisible(otherOptions, true);
575 var fitToPageContainer =
576 otherOptions.querySelector('#fit-to-page-container');
577 checkElementDisplayed(fitToPageContainer, true);
578 expectTrue(
579 fitToPageContainer.querySelector('.checkbox').checked);
580 expandMoreSettings();
581 checkSectionVisible($('media-size-settings'), true);
582 checkSectionVisible(scalingSettings, true);
583
584 // Change scaling input
585 var scalingInput = scalingSettings.querySelector('.user-value');
586 expectEquals('100', scalingInput.value);
587 scalingInput.stepUp(5);
588 expectEquals('105', scalingInput.value);
589
590 // Trigger the event
591 var enterEvent = document.createEvent('Event');
592 enterEvent.initEvent('keydown');
593 enterEvent.keyCode = 'Enter';
594 scalingInput.dispatchEvent(enterEvent);
595 expectFalse(
596 fitToPageContainer.querySelector('.checkbox').checked);
597
598 return whenAnimationDone('other-options-collapsible');
599 });
600 });
601
602 // When the number of copies print preset is set for source 'PDF', we update
603 // the copies value if capability is supported by printer.
604 test('CheckNumCopiesPrintPreset', function() {
605 initialSettings.isDocumentModifiable_ = false;
606 setInitialSettings();
607 return nativeLayer.whenCalled('getInitialSettings').then(function() {
608 setLocalDestinations();
609 setCapabilities(getCddTemplate("FooDevice"));
610
611 // Indicate that the number of copies print preset is set for source
612 // PDF.
613 var printPresetOptions = {
614 disableScaling: true,
615 copies: 2
616 };
617 var printPresetOptionsEvent = new Event(
618 print_preview.NativeLayer.EventType.PRINT_PRESET_OPTIONS);
619 printPresetOptionsEvent.optionsFromDocument = printPresetOptions;
620 nativeLayer.getEventTarget().
621 dispatchEvent(printPresetOptionsEvent);
622
623 checkSectionVisible($('copies-settings'), true);
624 expectEquals(
625 printPresetOptions.copies,
626 parseInt($('copies-settings').querySelector('.user-value').value));
627
628 return whenAnimationDone('other-options-collapsible');
629 });
630 });
631
632 // When the duplex print preset is set for source 'PDF', we update the
633 // duplex setting if capability is supported by printer.
634 test('CheckDuplexPrintPreset', function() {
635 initialSettings.isDocumentModifiable_ = false;
636 setInitialSettings();
637
638 return nativeLayer.whenCalled('getInitialSettings').then(
639 function() {
640 setLocalDestinations();
641 setCapabilities(getCddTemplate("FooDevice"));
642
643 // Indicate that the duplex print preset is set to "long edge" for
644 // source PDF.
645 var printPresetOptions = {
646 duplex: 1
647 };
648 var printPresetOptionsEvent = new Event(
649 print_preview.NativeLayer.EventType.PRINT_PRESET_OPTIONS);
650 printPresetOptionsEvent.optionsFromDocument = printPresetOptions;
651 nativeLayer.getEventTarget().
652 dispatchEvent(printPresetOptionsEvent);
653
654 var otherOptions = $('other-options-settings');
655 checkSectionVisible(otherOptions, true);
656 var duplexContainer =
657 otherOptions.querySelector('#duplex-container');
658 checkElementDisplayed(duplexContainer, true);
659 expectTrue(duplexContainer.querySelector('.checkbox').checked);
660
661 return whenAnimationDone('other-options-collapsible');
662 });
663 });
664
665 // Make sure that custom margins controls are properly set up.
666 test('CustomMarginsControlsCheck', function() {
667 setInitialSettings();
668 return nativeLayer.whenCalled('getInitialSettings').then(
669 function() {
670 setLocalDestinations();
671 setCapabilities(getCddTemplate("FooDevice"));
672
673 printPreview.printTicketStore_.marginsType.updateValue(
674 print_preview.ticket_items.MarginsTypeValue.CUSTOM);
675
676 ['left', 'top', 'right', 'bottom'].forEach(function(margin) {
677 var control =
678 $('preview-area').querySelector('.margin-control-' + margin);
679 assertNotEquals(null, control);
680 var input = control.querySelector('.margin-control-textbox');
681 assertTrue(input.hasAttribute('aria-label'));
682 assertNotEquals('undefined', input.getAttribute('aria-label'));
683 });
684 return whenAnimationDone('more-settings');
685 });
686 });
687
688 // Page layout has zero margins. Hide header and footer option.
689 test('PageLayoutHasNoMarginsHideHeaderFooter', function() {
690 setInitialSettings();
691 return nativeLayer.whenCalled('getInitialSettings').then(
692 function() {
693 setLocalDestinations();
694 setCapabilities(getCddTemplate("FooDevice"));
695
696 var otherOptions = $('other-options-settings');
697 var headerFooter =
698 otherOptions.querySelector('#header-footer-container');
699
700 // Check that options are collapsed (section is visible, because
701 // duplex is available).
702 checkSectionVisible(otherOptions, true);
703 checkElementDisplayed(headerFooter, false);
704
705 expandMoreSettings();
706
707 checkElementDisplayed(headerFooter, true);
708
709 printPreview.printTicketStore_.marginsType.updateValue(
710 print_preview.ticket_items.MarginsTypeValue.CUSTOM);
711 printPreview.printTicketStore_.customMargins.updateValue(
712 new print_preview.Margins(0, 0, 0, 0));
713
714 checkElementDisplayed(headerFooter, false);
715
716 return whenAnimationDone('more-settings');
717 });
718 });
719
720 // Page layout has half-inch margins. Show header and footer option.
721 test('PageLayoutHasMarginsShowHeaderFooter', function() {
722 setInitialSettings();
723 nativeLayer.whenCalled('getInitialSettings').then(
724 function() {
725 setLocalDestinations();
726 setCapabilities(getCddTemplate("FooDevice"));
727
728 var otherOptions = $('other-options-settings');
729 var headerFooter =
730 otherOptions.querySelector('#header-footer-container');
731
732 // Check that options are collapsed (section is visible, because
733 // duplex is available).
734 checkSectionVisible(otherOptions, true);
735 checkElementDisplayed(headerFooter, false);
736
737 expandMoreSettings();
738
739 checkElementDisplayed(headerFooter, true);
740
741 printPreview.printTicketStore_.marginsType.updateValue(
742 print_preview.ticket_items.MarginsTypeValue.CUSTOM);
743 printPreview.printTicketStore_.customMargins.updateValue(
744 new print_preview.Margins(36, 36, 36, 36));
745
746 checkElementDisplayed(headerFooter, true);
747
748 return whenAnimationDone('more-settings');
749 });
750 });
751
752
343 }); 753 });
344 }); 754 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698