- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / webui / print_preview.js
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6  * Test fixture for print preview WebUI testing.
7  * @constructor
8  * @extends {testing.Test}
9  */
10 function PrintPreviewWebUITest() {
11   testing.Test.call(this);
12   this.nativeLayer_ = null;
13   this.initialSettings_ = null;
14   this.localDestinationInfos_ = null;
15 }
16
17 /**
18  * Index of the "Save as PDF" printer.
19  * @type {number}
20  * @const
21  */
22 PrintPreviewWebUITest.PDF_INDEX = 0;
23
24 /**
25  * Index of the Foo printer.
26  * @type {number}
27  * @const
28  */
29 PrintPreviewWebUITest.FOO_INDEX = 1;
30
31 /**
32  * Index of the Bar printer.
33  * @type {number}
34  * @const
35  */
36 PrintPreviewWebUITest.BAR_INDEX = 2;
37
38 PrintPreviewWebUITest.prototype = {
39   __proto__: testing.Test.prototype,
40
41   /**
42    * Browse to the sample page, cause print preview & call preLoad().
43    * @type {string}
44    * @override
45    */
46   browsePrintPreload: 'print_preview_hello_world_test.html',
47
48   /**
49    * Stub out low-level functionality like the NativeLayer and
50    * CloudPrintInterface.
51    * @this {PrintPreviewWebUITest}
52    * @override
53    */
54   preLoad: function() {
55     window.addEventListener('DOMContentLoaded', function() {
56       function NativeLayerStub() {
57         cr.EventTarget.call(this);
58       }
59       NativeLayerStub.prototype = {
60         __proto__: cr.EventTarget.prototype,
61         startGetInitialSettings: function() {},
62         startGetLocalDestinations: function() {},
63         startGetLocalDestinationCapabilities: function(destinationId) {}
64       };
65       var oldNativeLayerEventType = print_preview.NativeLayer.EventType;
66       var oldDuplexMode = print_preview.NativeLayer.DuplexMode;
67       print_preview.NativeLayer = NativeLayerStub;
68       print_preview.NativeLayer.EventType = oldNativeLayerEventType;
69       print_preview.NativeLayer.DuplexMode = oldDuplexMode;
70
71       function CloudPrintInterfaceStub() {
72         cr.EventTarget.call(this);
73       }
74       CloudPrintInterfaceStub.prototype = {
75         __proto__: cr.EventTarget.prototype,
76         search: function(isRecent) {}
77       };
78       var oldCpInterfaceEventType = cloudprint.CloudPrintInterface.EventType;
79       cloudprint.CloudPrintInterface = CloudPrintInterfaceStub;
80       cloudprint.CloudPrintInterface.EventType = oldCpInterfaceEventType;
81
82       print_preview.PreviewArea.prototype.checkPluginCompatibility_ =
83           function() {
84         return false;
85       };
86     }.bind(this));
87   },
88
89   /**
90    * Generate a real C++ class; don't typedef.
91    * @type {?string}
92    * @override
93    */
94   typedefCppFixture: null,
95
96   /**
97    * @this {PrintPreviewWebUITest}
98    * @override
99    */
100   setUp: function() {
101     Mock4JS.clearMocksToVerify();
102
103     this.initialSettings_ = new print_preview.NativeInitialSettings(
104       false /*isInKioskAutoPrintMode*/,
105       false /*hidePrintWithSystemDialogLink*/,
106       ',' /*thousandsDelimeter*/,
107       '.' /*decimalDelimeter*/,
108       1 /*unitType*/,
109       true /*isDocumentModifiable*/,
110       'title' /*documentTitle*/,
111       true /*documentHasSelection*/,
112       false /*selectionOnly*/,
113       'FooDevice' /*systemDefaultDestinationId*/,
114       null /*serializedAppStateStr*/,
115       false /*documentHasSelection*/);
116     this.localDestinationInfos_ = [
117       { printerName: 'FooName', deviceName: 'FooDevice' },
118       { printerName: 'BarName', deviceName: 'BarDevice' }
119     ];
120     this.nativeLayer_ = printPreview.nativeLayer_;
121   }
122 };
123
124 GEN('#include "chrome/test/data/webui/print_preview.h"');
125
126 // Test some basic assumptions about the print preview WebUI.
127 TEST_F('PrintPreviewWebUITest', 'TestPrinterList', function() {
128   var initialSettingsSetEvent =
129       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
130   initialSettingsSetEvent.initialSettings = this.initialSettings_;
131   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
132
133   var localDestsSetEvent =
134       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
135   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
136   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
137
138   var recentList = $('destination-search').querySelector('.recent-list ul');
139   var localList = $('destination-search').querySelector('.local-list ul');
140   assertNotEquals(null, recentList);
141   assertEquals(1, recentList.childNodes.length);
142   assertEquals('FooName',
143                recentList.childNodes.item(0).querySelector(
144                    '.destination-list-item-name').textContent);
145
146   assertNotEquals(null, localList);
147   assertEquals(3, localList.childNodes.length);
148   assertEquals('Save as PDF',
149                localList.childNodes.item(PrintPreviewWebUITest.PDF_INDEX).
150                    querySelector('.destination-list-item-name').textContent);
151   assertEquals('FooName',
152                localList.childNodes.item(PrintPreviewWebUITest.FOO_INDEX).
153                    querySelector('.destination-list-item-name').textContent);
154   assertEquals('BarName',
155                localList.childNodes.item(PrintPreviewWebUITest.BAR_INDEX).
156                    querySelector('.destination-list-item-name').textContent);
157 });
158
159 // Test that the printer list is structured correctly after calling
160 // addCloudPrinters with an empty list.
161 TEST_F('PrintPreviewWebUITest', 'TestPrinterListCloudEmpty', function() {
162   var initialSettingsSetEvent =
163       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
164   initialSettingsSetEvent.initialSettings = this.initialSettings_;
165   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
166
167   var localDestsSetEvent =
168       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
169   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
170   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
171
172   var cloudPrintEnableEvent =
173       new Event(print_preview.NativeLayer.EventType.CLOUD_PRINT_ENABLE);
174   cloudPrintEnableEvent.baseCloudPrintUrl = 'cloudprint url';
175   this.nativeLayer_.dispatchEvent(cloudPrintEnableEvent);
176
177   var searchDoneEvent =
178       new Event(cloudprint.CloudPrintInterface.EventType.SEARCH_DONE);
179   searchDoneEvent.printers = [];
180   searchDoneEvent.isRecent = true;
181   searchDoneEvent.email = 'foo@chromium.org';
182   printPreview.cloudPrintInterface_.dispatchEvent(searchDoneEvent);
183
184   var recentList = $('destination-search').querySelector('.recent-list ul');
185   var localList = $('destination-search').querySelector('.local-list ul');
186   var cloudList = $('destination-search').querySelector('.cloud-list ul');
187
188   assertNotEquals(null, recentList);
189   assertEquals(1, recentList.childNodes.length);
190   assertEquals('FooName',
191                recentList.childNodes.item(0).querySelector(
192                    '.destination-list-item-name').textContent);
193
194   assertNotEquals(null, localList);
195   assertEquals(3, localList.childNodes.length);
196   assertEquals('Save as PDF',
197                localList.childNodes.item(PrintPreviewWebUITest.PDF_INDEX).
198                    querySelector('.destination-list-item-name').textContent);
199   assertEquals('FooName',
200                localList.childNodes.item(PrintPreviewWebUITest.FOO_INDEX).
201                    querySelector('.destination-list-item-name').textContent);
202   assertEquals('BarName',
203                localList.childNodes.item(PrintPreviewWebUITest.BAR_INDEX).
204                    querySelector('.destination-list-item-name').textContent);
205
206   assertNotEquals(null, cloudList);
207   assertEquals(0, cloudList.childNodes.length);
208 });
209
210 /**
211  * Verify that |section| visibility matches |visible|.
212  * @param {HTMLDivElement} section The section to check.
213  * @param {boolean} visible The expected state of visibility.
214  */
215 function checkSectionVisible(section, visible) {
216   assertNotEquals(null, section);
217   expectEquals(
218       visible, section.classList.contains('visible'), 'section=' + section.id);
219 }
220
221 function checkElementDisplayed(el, isDisplayed) {
222   assertNotEquals(null, el);
223   expectEquals(isDisplayed, !el.hidden);
224 }
225
226 // Test that disabled settings hide the disabled sections.
227 TEST_F('PrintPreviewWebUITest', 'TestSectionsDisabled', function() {
228   checkSectionVisible($('layout-settings'), true);
229   checkSectionVisible($('color-settings'), true);
230   checkSectionVisible($('copies-settings'), true);
231
232   var initialSettingsSetEvent =
233       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
234   initialSettingsSetEvent.initialSettings = this.initialSettings_;
235   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
236
237   var localDestsSetEvent =
238       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
239   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
240   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
241
242   var capsSetEvent =
243       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
244   capsSetEvent.settingsInfo = {
245     'printerId': 'FooDevice',
246     'disableColorOption': true,
247     'setColorAsDefault': true,
248     'disableCopiesOption': true,
249     'disableLandscapeOption': true,
250     'printerDefaultDuplexValue': 0
251   };
252   this.nativeLayer_.dispatchEvent(capsSetEvent);
253
254   checkSectionVisible($('layout-settings'), false);
255   checkSectionVisible($('color-settings'), false);
256   checkSectionVisible($('copies-settings'), false);
257 });
258
259 // When the source is 'PDF' and 'Save as PDF' option is selected, we hide the
260 // fit to page option.
261 TEST_F('PrintPreviewWebUITest',
262        'PrintToPDFSelectedHideFitToPageOption',
263        function() {
264   // Add PDF printer.
265   this.initialSettings_.isDocumentModifiable_ = false;
266   this.initialSettings_.systemDefaultDestinationId_ = 'Save as PDF';
267   this.localDestinationInfos_.push(
268       {printerName: 'Save as PDF', deviceName: 'Save as PDF'});
269
270   var initialSettingsSetEvent =
271       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
272   initialSettingsSetEvent.initialSettings = this.initialSettings_;
273   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
274
275   var localDestsSetEvent =
276       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
277   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
278   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
279
280   var capsSetEvent =
281       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
282   capsSetEvent.settingsInfo = {
283     'printerId': 'FooDevice',
284     'disableColorOption': false,
285     'setColorAsDefault': true,
286     'disableCopiesOption': true,
287     'disableLandscapeOption': true,
288     'printerDefaultDuplexValue': 0
289   };
290   this.nativeLayer_.dispatchEvent(capsSetEvent);
291
292   checkElementDisplayed(
293       $('other-options-settings').querySelector('.fit-to-page-container'),
294       false);
295 });
296
297 // When the source is 'HTML', we always hide the fit to page option.
298 TEST_F('PrintPreviewWebUITest', 'SourceIsHTMLHideFitToPageOption', function() {
299   var initialSettingsSetEvent =
300       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
301   initialSettingsSetEvent.initialSettings = this.initialSettings_;
302   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
303
304   var localDestsSetEvent =
305       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
306   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
307   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
308
309   var capsSetEvent =
310       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
311   capsSetEvent.settingsInfo = {
312     'printerId': 'FooDevice',
313     'disableColorOption': false,
314     'setColorAsDefault': true,
315     'disableCopiesOption': true,
316     'disableLandscapeOption': true,
317     'printerDefaultDuplexValue': 0
318   };
319   this.nativeLayer_.dispatchEvent(capsSetEvent);
320
321   checkElementDisplayed(
322       $('other-options-settings').querySelector('.fit-to-page-container'),
323       false);
324 });
325
326 // When the source is "PDF", depending on the selected destination printer, we
327 // show/hide the fit to page option.
328 TEST_F('PrintPreviewWebUITest', 'SourceIsPDFShowFitToPageOption', function() {
329   this.initialSettings_.isDocumentModifiable_ = false;
330
331   var initialSettingsSetEvent =
332       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
333   initialSettingsSetEvent.initialSettings = this.initialSettings_;
334   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
335
336   var localDestsSetEvent =
337       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
338   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
339   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
340
341   var capsSetEvent =
342       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
343   capsSetEvent.settingsInfo = {
344     'printerId': 'FooDevice',
345     'disableColorOption': false,
346     'setColorAsDefault': true,
347     'disableCopiesOption': true,
348     'disableLandscapeOption': true,
349     'printerDefaultDuplexValue': 0
350   };
351   this.nativeLayer_.dispatchEvent(capsSetEvent);
352
353   checkElementDisplayed(
354       $('other-options-settings').querySelector('.fit-to-page-container'),
355       true);
356   expectTrue(
357       $('other-options-settings').querySelector('.fit-to-page-checkbox').
358           checked);
359 });
360
361 // When the print scaling is disabled for the source "PDF", we show the fit
362 // to page option but the state is unchecked by default.
363 TEST_F('PrintPreviewWebUITest', 'PrintScalingDisabledForPlugin', function() {
364   this.initialSettings_.isDocumentModifiable_ = false;
365
366   var initialSettingsSetEvent =
367       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
368   initialSettingsSetEvent.initialSettings = this.initialSettings_;
369   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
370
371   var localDestsSetEvent =
372       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
373   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
374   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
375
376   var capsSetEvent =
377       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
378   capsSetEvent.settingsInfo = {
379     'printerId': 'FooDevice',
380     'disableColorOption': false,
381     'setColorAsDefault': true,
382     'disableCopiesOption': true,
383     'disableLandscapeOption': true,
384     'printerDefaultDuplexValue': 0
385   };
386   this.nativeLayer_.dispatchEvent(capsSetEvent);
387
388   // Indicate that the PDF does not support scaling by default.
389   cr.dispatchSimpleEvent(
390       this.nativeLayer_, print_preview.NativeLayer.EventType.DISABLE_SCALING);
391
392   checkElementDisplayed(
393       $('other-options-settings').querySelector('.fit-to-page-container'),
394       true);
395   expectFalse(
396       $('other-options-settings').querySelector('.fit-to-page-checkbox').
397           checked);
398 });
399
400 // Page layout has zero margins. Hide header and footer option.
401 TEST_F('PrintPreviewWebUITest',
402        'PageLayoutHasNoMarginsHideHeaderFooter',
403        function() {
404   var initialSettingsSetEvent =
405       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
406   initialSettingsSetEvent.initialSettings = this.initialSettings_;
407   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
408
409   var localDestsSetEvent =
410       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
411   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
412   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
413
414   var capsSetEvent =
415       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
416   capsSetEvent.settingsInfo = {
417     'printerId': 'FooDevice',
418     'disableColorOption': false,
419     'setColorAsDefault': true,
420     'disableCopiesOption': true,
421     'disableLandscapeOption': true,
422     'printerDefaultDuplexValue': 0
423   };
424   this.nativeLayer_.dispatchEvent(capsSetEvent);
425
426   checkElementDisplayed(
427       $('other-options-settings').querySelector('.header-footer-container'),
428       true);
429
430   printPreview.printTicketStore_.marginsType.updateValue(
431       print_preview.ticket_items.MarginsType.Value.CUSTOM);
432   printPreview.printTicketStore_.customMargins.updateValue(
433       new print_preview.Margins(0, 0, 0, 0));
434
435   checkElementDisplayed(
436       $('other-options-settings').querySelector('.header-footer-container'),
437       false);
438 });
439
440 // Page layout has half-inch margins. Show header and footer option.
441 TEST_F('PrintPreviewWebUITest',
442        'PageLayoutHasMarginsShowHeaderFooter',
443        function() {
444   var initialSettingsSetEvent =
445       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
446   initialSettingsSetEvent.initialSettings = this.initialSettings_;
447   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
448
449   var localDestsSetEvent =
450       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
451   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
452   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
453
454   var capsSetEvent =
455       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
456   capsSetEvent.settingsInfo = {
457     'printerId': 'FooDevice',
458     'disableColorOption': false,
459     'setColorAsDefault': true,
460     'disableCopiesOption': true,
461     'disableLandscapeOption': true,
462     'printerDefaultDuplexValue': 0
463   };
464   this.nativeLayer_.dispatchEvent(capsSetEvent);
465
466   checkElementDisplayed(
467       $('other-options-settings').querySelector('.header-footer-container'),
468       true);
469
470   printPreview.printTicketStore_.marginsType.updateValue(
471       print_preview.ticket_items.MarginsType.Value.CUSTOM);
472   printPreview.printTicketStore_.customMargins.updateValue(
473       new print_preview.Margins(36, 36, 36, 36));
474
475   checkElementDisplayed(
476       $('other-options-settings').querySelector('.header-footer-container'),
477       true);
478 });
479
480 // Page layout has zero top and bottom margins. Hide header and footer option.
481 TEST_F('PrintPreviewWebUITest',
482        'ZeroTopAndBottomMarginsHideHeaderFooter',
483        function() {
484   var initialSettingsSetEvent =
485       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
486   initialSettingsSetEvent.initialSettings = this.initialSettings_;
487   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
488
489   var localDestsSetEvent =
490       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
491   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
492   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
493
494   var capsSetEvent =
495       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
496   capsSetEvent.settingsInfo = {
497     'printerId': 'FooDevice',
498     'disableColorOption': false,
499     'setColorAsDefault': true,
500     'disableCopiesOption': true,
501     'disableLandscapeOption': true,
502     'printerDefaultDuplexValue': 0
503   };
504   this.nativeLayer_.dispatchEvent(capsSetEvent);
505
506   checkElementDisplayed(
507       $('other-options-settings').querySelector('.header-footer-container'),
508       true);
509
510   printPreview.printTicketStore_.marginsType.updateValue(
511       print_preview.ticket_items.MarginsType.Value.CUSTOM);
512   printPreview.printTicketStore_.customMargins.updateValue(
513       new print_preview.Margins(0, 36, 0, 36));
514
515   checkElementDisplayed(
516       $('other-options-settings').querySelector('.header-footer-container'),
517       false);
518 });
519
520 // Page layout has zero top and half-inch bottom margin. Show header and footer
521 // option.
522 TEST_F('PrintPreviewWebUITest',
523        'ZeroTopAndNonZeroBottomMarginShowHeaderFooter',
524        function() {
525   var initialSettingsSetEvent =
526       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
527   initialSettingsSetEvent.initialSettings = this.initialSettings_;
528   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
529
530   var localDestsSetEvent =
531       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
532   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
533   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
534
535   var capsSetEvent =
536       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
537   capsSetEvent.settingsInfo = {
538     'printerId': 'FooDevice',
539     'disableColorOption': false,
540     'setColorAsDefault': true,
541     'disableCopiesOption': true,
542     'disableLandscapeOption': true,
543     'printerDefaultDuplexValue': 0
544   };
545   this.nativeLayer_.dispatchEvent(capsSetEvent);
546
547   checkElementDisplayed(
548       $('other-options-settings').querySelector('.header-footer-container'),
549       true);
550
551   printPreview.printTicketStore_.marginsType.updateValue(
552       print_preview.ticket_items.MarginsType.Value.CUSTOM);
553   printPreview.printTicketStore_.customMargins.updateValue(
554       new print_preview.Margins(0, 36, 36, 36));
555
556   checkElementDisplayed(
557       $('other-options-settings').querySelector('.header-footer-container'),
558       true);
559 });
560
561 // Test that the color settings are set according to the printer capabilities.
562 TEST_F('PrintPreviewWebUITest', 'TestColorSettingsTrue', function() {
563   var initialSettingsSetEvent =
564       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
565   initialSettingsSetEvent.initialSettings = this.initialSettings_;
566   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
567
568   var localDestsSetEvent =
569       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
570   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
571   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
572
573   checkSectionVisible($('color-settings'), true);
574
575   var capsSetEvent =
576       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
577   capsSetEvent.settingsInfo = {
578     'printerId': 'FooDevice',
579     'disableColorOption': false,
580     'setColorAsDefault': true,
581     'disableCopiesOption': false,
582     'disableLandscapeOption': true,
583     'printerDefaultDuplexValue': 0
584   };
585   this.nativeLayer_.dispatchEvent(capsSetEvent);
586
587   checkSectionVisible($('color-settings'), true);
588
589   var colorOption = $('color-settings').querySelector('.color-option');
590   var bwOption = $('color-settings').querySelector('.bw-option');
591   expectTrue(colorOption.checked);
592   expectFalse(bwOption.checked);
593 });
594
595 //Test that the color settings are set according to the printer capabilities.
596 TEST_F('PrintPreviewWebUITest', 'TestColorSettingsFalse', function() {
597   var initialSettingsSetEvent =
598       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
599   initialSettingsSetEvent.initialSettings = this.initialSettings_;
600   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
601
602   var localDestsSetEvent =
603       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
604   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
605   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
606
607   checkSectionVisible($('color-settings'), true);
608
609   var capsSetEvent =
610       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
611   capsSetEvent.settingsInfo = {
612     'printerId': 'FooDevice',
613     'disableColorOption': true,
614     'setColorAsDefault': false,
615     'disableCopiesOption': false,
616     'disableLandscapeOption': true,
617     'printerDefaultDuplexValue': 0
618   };
619   this.nativeLayer_.dispatchEvent(capsSetEvent);
620
621   checkSectionVisible($('color-settings'), false);
622
623   var colorOption = $('color-settings').querySelector('.color-option');
624   var bwOption = $('color-settings').querySelector('.bw-option');
625   expectFalse(colorOption.checked);
626   expectTrue(bwOption.checked);
627 });
628
629 // Test to verify that duplex settings are set according to the printer
630 // capabilities.
631 TEST_F('PrintPreviewWebUITest', 'TestDuplexSettingsTrue', function() {
632   var initialSettingsSetEvent =
633       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
634   initialSettingsSetEvent.initialSettings = this.initialSettings_;
635   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
636
637   var localDestsSetEvent =
638       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
639   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
640   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
641
642   var otherOptionsDiv = $('other-options-settings');
643   var duplexDiv = otherOptionsDiv.querySelector('.duplex-container');
644   var duplexCheckbox = otherOptionsDiv.querySelector('.duplex-checkbox');
645
646   var capsSetEvent =
647       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
648   capsSetEvent.settingsInfo = {
649     'printerId': 'FooDevice',
650     'disableColorOption': false,
651     'setColorAsDefault': true,
652     'disableCopiesOption': false,
653     'disableLandscapeOption': true,
654     'printerDefaultDuplexValue': 0,
655     'setDuplexAsDefault': false
656   };
657   this.nativeLayer_.dispatchEvent(capsSetEvent);
658
659   checkSectionVisible(otherOptionsDiv, true);
660   expectFalse(duplexDiv.hidden);
661   expectFalse(duplexCheckbox.checked);
662 });
663
664 //Test to verify that duplex settings are set according to the printer
665 //capabilities.
666 TEST_F('PrintPreviewWebUITest', 'TestDuplexSettingsFalse', function() {
667   var initialSettingsSetEvent =
668      new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
669   initialSettingsSetEvent.initialSettings = this.initialSettings_;
670   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
671
672   var localDestsSetEvent =
673      new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
674   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
675   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
676
677   var otherOptionsDiv = $('other-options-settings');
678   var duplexDiv = otherOptionsDiv.querySelector('.duplex-container');
679
680   var capsSetEvent =
681      new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
682   capsSetEvent.settingsInfo = {
683    'printerId': 'FooDevice',
684    'disableColorOption': false,
685    'setColorAsDefault': true,
686    'disableCopiesOption': false,
687    'disableLandscapeOption': true,
688    'printerDefaultDuplexValue': -1,
689    'setDuplexAsDefault': false
690   };
691   this.nativeLayer_.dispatchEvent(capsSetEvent);
692
693   checkSectionVisible(otherOptionsDiv, true);
694   expectTrue(duplexDiv.hidden);
695 });
696
697 // Test that changing the selected printer updates the preview.
698 TEST_F('PrintPreviewWebUITest', 'TestPrinterChangeUpdatesPreview', function() {
699
700   var initialSettingsSetEvent =
701       new Event(print_preview.NativeLayer.EventType.INITIAL_SETTINGS_SET);
702   initialSettingsSetEvent.initialSettings = this.initialSettings_;
703   this.nativeLayer_.dispatchEvent(initialSettingsSetEvent);
704
705   var localDestsSetEvent =
706       new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
707   localDestsSetEvent.destinationInfos = this.localDestinationInfos_;
708   this.nativeLayer_.dispatchEvent(localDestsSetEvent);
709
710   var capsSetEvent =
711       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
712   capsSetEvent.settingsInfo = {
713     'printerId': 'FooDevice',
714     'disableColorOption': false,
715     'setColorAsDefault': true,
716     'disableCopiesOption': true,
717     'disableLandscapeOption': true,
718     'printerDefaultDuplexValue': 0
719   };
720   this.nativeLayer_.dispatchEvent(capsSetEvent);
721
722   var previewGenerator = mock(print_preview.PreviewGenerator);
723   printPreview.previewArea_.previewGenerator_ = previewGenerator.proxy();
724   previewGenerator.expects(exactly(6)).requestPreview();
725
726   var barDestination;
727   var destinations = printPreview.destinationStore_.destinations;
728   for (var destination, i = 0; destination = destinations[i]; i++) {
729     if (destination.id == 'BarDevice') {
730       barDestination = destination;
731       break;
732     }
733   }
734
735   printPreview.destinationStore_.selectDestination(barDestination);
736
737   var capsSetEvent =
738       new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
739   capsSetEvent.settingsInfo = {
740     'printerId': 'BarDevice',
741     'disableColorOption': true,
742     'setColorAsDefault': false,
743     'disableCopiesOption': true,
744     'disableLandscapeOption': true,
745     'printerDefaultDuplexValue': 0
746   };
747   this.nativeLayer_.dispatchEvent(capsSetEvent);
748 });
749
750 // Test that error message is displayed when plugin doesn't exist.
751 TEST_F('PrintPreviewWebUITest', 'TestNoPDFPluginErrorMessage', function() {
752   var previewAreaEl = $('preview-area');
753
754   var loadingMessageEl =
755       previewAreaEl.getElementsByClassName('preview-area-loading-message')[0];
756   expectEquals(true, loadingMessageEl.hidden);
757
758   var previewFailedMessageEl = previewAreaEl.getElementsByClassName(
759       'preview-area-preview-failed-message')[0];
760   expectEquals(true, previewFailedMessageEl.hidden);
761
762   var printFailedMessageEl =
763       previewAreaEl.getElementsByClassName('preview-area-print-failed')[0];
764   expectEquals(true, printFailedMessageEl.hidden);
765
766   var customMessageEl =
767       previewAreaEl.getElementsByClassName('preview-area-custom-message')[0];
768   expectEquals(false, customMessageEl.hidden);
769 });