OLD | NEW |
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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 * @param {boolean} visible The expected state of visibility. | 69 * @param {boolean} visible The expected state of visibility. |
70 */ | 70 */ |
71 function checkSectionVisible(section, visible) { | 71 function checkSectionVisible(section, visible) { |
72 assertNotEquals(null, section); | 72 assertNotEquals(null, section); |
73 expectEquals( | 73 expectEquals( |
74 visible, | 74 visible, |
75 section.classList.contains('visible'), 'section=' + section.id); | 75 section.classList.contains('visible'), 'section=' + section.id); |
76 } | 76 } |
77 | 77 |
78 /** | 78 /** |
| 79 * @param {?HTMLElement} el |
| 80 * @param {boolean} isDisplayed |
| 81 */ |
| 82 function checkElementDisplayed(el, isDisplayed) { |
| 83 assertNotEquals(null, el); |
| 84 expectEquals(isDisplayed, |
| 85 !el.hidden, |
| 86 'element="' + el.id + '" of class "' + el.classList + '"'); |
| 87 } |
| 88 |
| 89 /** |
79 * @param {string} printerId | 90 * @param {string} printerId |
80 * @return {!Object} | 91 * @return {!Object} |
81 */ | 92 */ |
82 function getCddTemplate(printerId) { | 93 function getCddTemplate(printerId) { |
83 return { | 94 return { |
84 printerId: printerId, | 95 printerId: printerId, |
85 capabilities: { | 96 capabilities: { |
86 version: '1.0', | 97 version: '1.0', |
87 printer: { | 98 printer: { |
88 supported_content_type: [{content_type: 'application/pdf'}], | 99 supported_content_type: [{content_type: 'application/pdf'}], |
(...skipping 26 matching lines...) Expand all Loading... |
115 height_microns: 279400, | 126 height_microns: 279400, |
116 is_default: true | 127 is_default: true |
117 } | 128 } |
118 ] | 129 ] |
119 } | 130 } |
120 } | 131 } |
121 } | 132 } |
122 }; | 133 }; |
123 } | 134 } |
124 | 135 |
| 136 /** |
| 137 * Even though animation duration and delay is set to zero, it is necessary to |
| 138 * wait until the animation has finished. |
| 139 * @return {!Promise} A promise firing when the animation is done. |
| 140 */ |
| 141 function whenAnimationDone(elementId) { |
| 142 return new Promise(function(resolve) { |
| 143 // Add a listener for the animation end event. |
| 144 var element = $(elementId); |
| 145 element.addEventListener('animationend', function f(e) { |
| 146 element.removeEventListener(f, 'animationend'); |
| 147 resolve(); |
| 148 }); |
| 149 }); |
| 150 } |
| 151 |
| 152 /** |
| 153 * Expand the 'More Settings' div to expose all options. |
| 154 */ |
| 155 function expandMoreSettings() { |
| 156 var moreSettings = $('more-settings'); |
| 157 checkSectionVisible(moreSettings, true); |
| 158 moreSettings.click(); |
| 159 } |
| 160 |
| 161 /** @return {boolean} */ |
| 162 function isPrintAsImageEnabled() { |
| 163 // Should be enabled by default on non Windows/Mac. |
| 164 return (!cr.isWindows && !cr.isMac && |
| 165 loadTimeData.getBoolean('printPdfAsImageEnabled')); |
| 166 } |
| 167 |
125 suite('PrintPreview', function() { | 168 suite('PrintPreview', function() { |
126 suiteSetup(function() { | 169 suiteSetup(function() { |
127 function CloudPrintInterfaceStub() { | 170 function CloudPrintInterfaceStub() { |
128 cr.EventTarget.call(this); | 171 cr.EventTarget.call(this); |
129 } | 172 } |
130 CloudPrintInterfaceStub.prototype = { | 173 CloudPrintInterfaceStub.prototype = { |
131 __proto__: cr.EventTarget.prototype, | 174 __proto__: cr.EventTarget.prototype, |
132 search: function(isRecent) {} | 175 search: function(isRecent) {} |
133 }; | 176 }; |
134 var oldCpInterfaceEventType = cloudprint.CloudPrintInterfaceEventType; | 177 var oldCpInterfaceEventType = cloudprint.CloudPrintInterfaceEventType; |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 initialSettings.serializedDefaultDestinationSelectionRulesStr_ = | 397 initialSettings.serializedDefaultDestinationSelectionRulesStr_ = |
355 JSON.stringify({namePattern: '.*Bar.*'}); | 398 JSON.stringify({namePattern: '.*Bar.*'}); |
356 setInitialSettings(); | 399 setInitialSettings(); |
357 return nativeLayer.whenCalled('getInitialSettings').then( | 400 return nativeLayer.whenCalled('getInitialSettings').then( |
358 function() { | 401 function() { |
359 setLocalDestinations(); | 402 setLocalDestinations(); |
360 assertEquals( | 403 assertEquals( |
361 'BarDevice', | 404 'BarDevice', |
362 printPreview.destinationStore_.selectedDestination.id); | 405 printPreview.destinationStore_.selectedDestination.id); |
363 }); | 406 }); |
364 }); | 407 }); |
| 408 |
| 409 test('SystemDialogLinkIsHiddenInAppKioskMode', function() { |
| 410 if (!cr.isChromeOS) |
| 411 initialSettings.isInAppKioskMode_ = true; |
| 412 |
| 413 setInitialSettings(); |
| 414 return nativeLayer.whenCalled('getInitialSettings').then( |
| 415 function() { |
| 416 if (cr.isChromeOS) |
| 417 assertEquals(null, $('system-dialog-link')); |
| 418 else |
| 419 checkElementDisplayed($('system-dialog-link'), false); |
| 420 }); |
| 421 }); |
| 422 |
| 423 test('SectionsDisabled', function() { |
| 424 checkSectionVisible($('layout-settings'), false); |
| 425 checkSectionVisible($('color-settings'), false); |
| 426 checkSectionVisible($('copies-settings'), false); |
| 427 |
| 428 setInitialSettings(); |
| 429 return nativeLayer.whenCalled('getInitialSettings').then( |
| 430 function() { |
| 431 setLocalDestinations(); |
| 432 var device = getCddTemplate('FooDevice'); |
| 433 device.capabilities.printer.color = { |
| 434 option: [{is_default: true, type: 'STANDARD_COLOR'}] |
| 435 }; |
| 436 delete device.capabilities.printer.copies; |
| 437 setCapabilities(device); |
| 438 |
| 439 checkSectionVisible($('layout-settings'), true); |
| 440 checkSectionVisible($('color-settings'), false); |
| 441 checkSectionVisible($('copies-settings'), false); |
| 442 |
| 443 return whenAnimationDone('other-options-collapsible'); |
| 444 }); |
| 445 }); |
| 446 |
| 447 // When the source is 'PDF' and 'Save as PDF' option is selected, we hide |
| 448 // the fit to page option. |
| 449 test('PrintToPDFSelectedCapabilities', function() { |
| 450 // Add PDF printer. |
| 451 initialSettings.isDocumentModifiable_ = false; |
| 452 initialSettings.systemDefaultDestinationId_ = 'Save as PDF'; |
| 453 setInitialSettings(); |
| 454 |
| 455 return nativeLayer.whenCalled('getInitialSettings').then(function() { |
| 456 var device = { |
| 457 printerId: 'Save as PDF', |
| 458 capabilities: { |
| 459 version: '1.0', |
| 460 printer: { |
| 461 page_orientation: { |
| 462 option: [ |
| 463 {type: 'AUTO', is_default: true}, |
| 464 {type: 'PORTRAIT'}, |
| 465 {type: 'LANDSCAPE'} |
| 466 ] |
| 467 }, |
| 468 color: { |
| 469 option: [ |
| 470 {type: 'STANDARD_COLOR', is_default: true} |
| 471 ] |
| 472 }, |
| 473 media_size: { |
| 474 option: [ |
| 475 { name: 'NA_LETTER', |
| 476 width_microns: 0, |
| 477 height_microns: 0, |
| 478 is_default: true |
| 479 } |
| 480 ] |
| 481 } |
| 482 } |
| 483 } |
| 484 }; |
| 485 setCapabilities(device); |
| 486 |
| 487 var otherOptions = $('other-options-settings'); |
| 488 // If rasterization is an option, other options should be visible. If |
| 489 // not, there should be no available other options. |
| 490 checkSectionVisible(otherOptions, isPrintAsImageEnabled()); |
| 491 if (isPrintAsImageEnabled()) { |
| 492 checkElementDisplayed( |
| 493 otherOptions.querySelector('#fit-to-page-container'), false); |
| 494 checkElementDisplayed( |
| 495 otherOptions.querySelector('#rasterize-container'), true); |
| 496 } |
| 497 checkSectionVisible($('media-size-settings'), false); |
| 498 checkSectionVisible($('scaling-settings'), false); |
| 499 }); |
| 500 }); |
| 501 |
| 502 // When the source is 'HTML', we always hide the fit to page option and show |
| 503 // media size option. |
| 504 test('SourceIsHTMLCapabilities', function() { |
| 505 setInitialSettings(); |
| 506 return nativeLayer.whenCalled('getInitialSettings').then(function() { |
| 507 setLocalDestinations(); |
| 508 setCapabilities(getCddTemplate('FooDevice')); |
| 509 |
| 510 var otherOptions = $('other-options-settings'); |
| 511 var fitToPage = otherOptions.querySelector('#fit-to-page-container'); |
| 512 var rasterize; |
| 513 if (isPrintAsImageEnabled()) |
| 514 rasterize = otherOptions.querySelector('#rasterize-container'); |
| 515 var mediaSize = $('media-size-settings'); |
| 516 var scalingSettings = $('scaling-settings'); |
| 517 |
| 518 // Check that options are collapsed (section is visible, because duplex |
| 519 // is available). |
| 520 checkSectionVisible(otherOptions, true); |
| 521 checkElementDisplayed(fitToPage, false); |
| 522 if (isPrintAsImageEnabled()) |
| 523 checkElementDisplayed(rasterize, false); |
| 524 checkSectionVisible(mediaSize, false); |
| 525 checkSectionVisible(scalingSettings, false); |
| 526 |
| 527 expandMoreSettings(); |
| 528 |
| 529 checkElementDisplayed(fitToPage, false); |
| 530 if (isPrintAsImageEnabled()) |
| 531 checkElementDisplayed(rasterize, false); |
| 532 checkSectionVisible(mediaSize, true); |
| 533 checkSectionVisible(scalingSettings, true); |
| 534 |
| 535 return whenAnimationDone('more-settings'); |
| 536 }); |
| 537 }); |
| 538 |
| 539 // When the source is "PDF", depending on the selected destination printer, |
| 540 // we show/hide the fit to page option and hide media size selection. |
| 541 test('SourceIsPDFCapabilities', function() { |
| 542 initialSettings.isDocumentModifiable_ = false; |
| 543 setInitialSettings(); |
| 544 return nativeLayer.whenCalled('getInitialSettings').then( |
| 545 function() { |
| 546 setLocalDestinations(); |
| 547 setCapabilities(getCddTemplate('FooDevice')); |
| 548 |
| 549 var otherOptions = $('other-options-settings'); |
| 550 var scalingSettings = $('scaling-settings'); |
| 551 var fitToPageContainer = |
| 552 otherOptions.querySelector('#fit-to-page-container'); |
| 553 var rasterizeContainer; |
| 554 if (isPrintAsImageEnabled()) { |
| 555 rasterizeContainer = |
| 556 otherOptions.querySelector('#rasterize-container'); |
| 557 } |
| 558 |
| 559 checkSectionVisible(otherOptions, true); |
| 560 checkElementDisplayed(fitToPageContainer, true); |
| 561 if (isPrintAsImageEnabled()) |
| 562 checkElementDisplayed(rasterizeContainer, false); |
| 563 expectTrue( |
| 564 fitToPageContainer.querySelector('.checkbox').checked); |
| 565 expandMoreSettings(); |
| 566 if (isPrintAsImageEnabled()) { |
| 567 checkElementDisplayed(rasterizeContainer, true); |
| 568 expectFalse( |
| 569 rasterizeContainer.querySelector('.checkbox').checked); |
| 570 } |
| 571 checkSectionVisible($('media-size-settings'), true); |
| 572 checkSectionVisible(scalingSettings, true); |
| 573 |
| 574 return whenAnimationDone('other-options-collapsible'); |
| 575 }); |
| 576 }); |
| 577 |
| 578 // When the source is "PDF", depending on the selected destination printer, |
| 579 // we show/hide the fit to page option and hide media size selection. |
| 580 test('ScalingUnchecksFitToPage', function() { |
| 581 initialSettings.isDocumentModifiable_ = false; |
| 582 setInitialSettings(); |
| 583 return nativeLayer.whenCalled('getInitialSettings').then( |
| 584 function() { |
| 585 setLocalDestinations(); |
| 586 setCapabilities(getCddTemplate('FooDevice')); |
| 587 |
| 588 var otherOptions = $('other-options-settings'); |
| 589 var scalingSettings = $('scaling-settings'); |
| 590 |
| 591 checkSectionVisible(otherOptions, true); |
| 592 var fitToPageContainer = |
| 593 otherOptions.querySelector('#fit-to-page-container'); |
| 594 checkElementDisplayed(fitToPageContainer, true); |
| 595 expectTrue( |
| 596 fitToPageContainer.querySelector('.checkbox').checked); |
| 597 expandMoreSettings(); |
| 598 checkSectionVisible($('media-size-settings'), true); |
| 599 checkSectionVisible(scalingSettings, true); |
| 600 |
| 601 // Change scaling input |
| 602 var scalingInput = scalingSettings.querySelector('.user-value'); |
| 603 expectEquals('100', scalingInput.value); |
| 604 scalingInput.stepUp(5); |
| 605 expectEquals('105', scalingInput.value); |
| 606 |
| 607 // Trigger the event |
| 608 var enterEvent = document.createEvent('Event'); |
| 609 enterEvent.initEvent('keydown'); |
| 610 enterEvent.keyCode = 'Enter'; |
| 611 scalingInput.dispatchEvent(enterEvent); |
| 612 expectFalse( |
| 613 fitToPageContainer.querySelector('.checkbox').checked); |
| 614 |
| 615 return whenAnimationDone('other-options-collapsible'); |
| 616 }); |
| 617 }); |
| 618 |
| 619 // When the number of copies print preset is set for source 'PDF', we update |
| 620 // the copies value if capability is supported by printer. |
| 621 test('CheckNumCopiesPrintPreset', function() { |
| 622 initialSettings.isDocumentModifiable_ = false; |
| 623 setInitialSettings(); |
| 624 return nativeLayer.whenCalled('getInitialSettings').then(function() { |
| 625 setLocalDestinations(); |
| 626 setCapabilities(getCddTemplate('FooDevice')); |
| 627 |
| 628 // Indicate that the number of copies print preset is set for source |
| 629 // PDF. |
| 630 var printPresetOptions = { |
| 631 disableScaling: true, |
| 632 copies: 2 |
| 633 }; |
| 634 var printPresetOptionsEvent = new Event( |
| 635 print_preview.NativeLayer.EventType.PRINT_PRESET_OPTIONS); |
| 636 printPresetOptionsEvent.optionsFromDocument = printPresetOptions; |
| 637 nativeLayer.getEventTarget(). |
| 638 dispatchEvent(printPresetOptionsEvent); |
| 639 |
| 640 checkSectionVisible($('copies-settings'), true); |
| 641 expectEquals( |
| 642 printPresetOptions.copies, |
| 643 parseInt($('copies-settings').querySelector('.user-value').value)); |
| 644 |
| 645 return whenAnimationDone('other-options-collapsible'); |
| 646 }); |
| 647 }); |
| 648 |
| 649 // When the duplex print preset is set for source 'PDF', we update the |
| 650 // duplex setting if capability is supported by printer. |
| 651 test('CheckDuplexPrintPreset', function() { |
| 652 initialSettings.isDocumentModifiable_ = false; |
| 653 setInitialSettings(); |
| 654 |
| 655 return nativeLayer.whenCalled('getInitialSettings').then( |
| 656 function() { |
| 657 setLocalDestinations(); |
| 658 setCapabilities(getCddTemplate('FooDevice')); |
| 659 |
| 660 // Indicate that the duplex print preset is set to "long edge" for |
| 661 // source PDF. |
| 662 var printPresetOptions = { |
| 663 duplex: 1 |
| 664 }; |
| 665 var printPresetOptionsEvent = new Event( |
| 666 print_preview.NativeLayer.EventType.PRINT_PRESET_OPTIONS); |
| 667 printPresetOptionsEvent.optionsFromDocument = printPresetOptions; |
| 668 nativeLayer.getEventTarget(). |
| 669 dispatchEvent(printPresetOptionsEvent); |
| 670 |
| 671 var otherOptions = $('other-options-settings'); |
| 672 checkSectionVisible(otherOptions, true); |
| 673 var duplexContainer = |
| 674 otherOptions.querySelector('#duplex-container'); |
| 675 checkElementDisplayed(duplexContainer, true); |
| 676 expectTrue(duplexContainer.querySelector('.checkbox').checked); |
| 677 |
| 678 return whenAnimationDone('other-options-collapsible'); |
| 679 }); |
| 680 }); |
| 681 |
| 682 // Make sure that custom margins controls are properly set up. |
| 683 test('CustomMarginsControlsCheck', function() { |
| 684 setInitialSettings(); |
| 685 return nativeLayer.whenCalled('getInitialSettings').then( |
| 686 function() { |
| 687 setLocalDestinations(); |
| 688 setCapabilities(getCddTemplate('FooDevice')); |
| 689 |
| 690 printPreview.printTicketStore_.marginsType.updateValue( |
| 691 print_preview.ticket_items.MarginsTypeValue.CUSTOM); |
| 692 |
| 693 ['left', 'top', 'right', 'bottom'].forEach(function(margin) { |
| 694 var control = |
| 695 $('preview-area').querySelector('.margin-control-' + margin); |
| 696 assertNotEquals(null, control); |
| 697 var input = control.querySelector('.margin-control-textbox'); |
| 698 assertTrue(input.hasAttribute('aria-label')); |
| 699 assertNotEquals('undefined', input.getAttribute('aria-label')); |
| 700 }); |
| 701 return whenAnimationDone('more-settings'); |
| 702 }); |
| 703 }); |
| 704 |
| 705 // Page layout has zero margins. Hide header and footer option. |
| 706 test('PageLayoutHasNoMarginsHideHeaderFooter', function() { |
| 707 setInitialSettings(); |
| 708 return nativeLayer.whenCalled('getInitialSettings').then( |
| 709 function() { |
| 710 setLocalDestinations(); |
| 711 setCapabilities(getCddTemplate('FooDevice')); |
| 712 |
| 713 var otherOptions = $('other-options-settings'); |
| 714 var headerFooter = |
| 715 otherOptions.querySelector('#header-footer-container'); |
| 716 |
| 717 // Check that options are collapsed (section is visible, because |
| 718 // duplex is available). |
| 719 checkSectionVisible(otherOptions, true); |
| 720 checkElementDisplayed(headerFooter, false); |
| 721 |
| 722 expandMoreSettings(); |
| 723 |
| 724 checkElementDisplayed(headerFooter, true); |
| 725 |
| 726 printPreview.printTicketStore_.marginsType.updateValue( |
| 727 print_preview.ticket_items.MarginsTypeValue.CUSTOM); |
| 728 printPreview.printTicketStore_.customMargins.updateValue( |
| 729 new print_preview.Margins(0, 0, 0, 0)); |
| 730 |
| 731 checkElementDisplayed(headerFooter, false); |
| 732 |
| 733 return whenAnimationDone('more-settings'); |
| 734 }); |
| 735 }); |
| 736 |
| 737 // Page layout has half-inch margins. Show header and footer option. |
| 738 test('PageLayoutHasMarginsShowHeaderFooter', function() { |
| 739 setInitialSettings(); |
| 740 nativeLayer.whenCalled('getInitialSettings').then( |
| 741 function() { |
| 742 setLocalDestinations(); |
| 743 setCapabilities(getCddTemplate('FooDevice')); |
| 744 |
| 745 var otherOptions = $('other-options-settings'); |
| 746 var headerFooter = |
| 747 otherOptions.querySelector('#header-footer-container'); |
| 748 |
| 749 // Check that options are collapsed (section is visible, because |
| 750 // duplex is available). |
| 751 checkSectionVisible(otherOptions, true); |
| 752 checkElementDisplayed(headerFooter, false); |
| 753 |
| 754 expandMoreSettings(); |
| 755 |
| 756 checkElementDisplayed(headerFooter, true); |
| 757 |
| 758 printPreview.printTicketStore_.marginsType.updateValue( |
| 759 print_preview.ticket_items.MarginsTypeValue.CUSTOM); |
| 760 printPreview.printTicketStore_.customMargins.updateValue( |
| 761 new print_preview.Margins(36, 36, 36, 36)); |
| 762 |
| 763 checkElementDisplayed(headerFooter, true); |
| 764 |
| 765 return whenAnimationDone('more-settings'); |
| 766 }); |
| 767 }); |
| 768 |
| 769 |
365 }); | 770 }); |
366 }); | 771 }); |
OLD | NEW |