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

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

Issue 2887003002: Print Preview: Migrate some JS tests to use Mocha. (Closed)
Patch Set: Replace more Created 3 years, 7 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
(Empty)
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
3 // found in the LICENSE file.
4
5 cr.define('print_preview_test', function() {
6 /**
7 * Index of the "Save as PDF" printer.
8 * @type {number}
9 * @const
10 */
11 var PDF_INDEX = 0;
12
13 /**
14 * Index of the Foo printer.
15 * @type {number}
16 * @const
17 */
18 var FOO_INDEX = 1;
19
20 /**
21 * Index of the Bar printer.
22 * @type {number}
23 * @const
24 */
25 var BAR_INDEX = 2;
26
27 var printPreview = null;
28 var nativeLayer = null;
29 var initialSettings = null;
30 var localDestinationInfos = null;
31 var previewArea = null;
32
33 /**
34 * Initialize print preview with the initial settings currently stored in
35 * |initialSettings|. Creates |printPreview| if it does not
36 * already exist.
37 */
38 function setInitialSettings() {
39 nativeLayer.setInitialSettings(initialSettings);
40 printPreview.initialize();
41 }
42
43 /**
44 * Dispatch the LOCAL_DESTINATIONS_SET event. This call is NOT async and will
45 * happen in the same thread.
46 */
47 function setLocalDestinations() {
48 var localDestsSetEvent =
49 new Event(print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET);
50 localDestsSetEvent.destinationInfos = localDestinationInfos;
51 nativeLayer.getEventTarget().dispatchEvent(localDestsSetEvent);
52 }
53
54 /**
55 * Dispatch the CAPABILITIES_SET event. This call is NOT async and will
56 * happen in the same thread.
57 * @param {!Object} device The device whose capabilities should be dispatched.
58 */
59 function setCapabilities(device) {
60 var capsSetEvent =
61 new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET);
62 capsSetEvent.settingsInfo = device;
63 nativeLayer.getEventTarget().dispatchEvent(capsSetEvent);
64 }
65
66 /**
67 * Verify that |section| visibility matches |visible|.
68 * @param {HTMLDivElement} section The section to check.
69 * @param {boolean} visible The expected state of visibility.
70 */
71 function checkSectionVisible(section, visible) {
72 assertNotEquals(null, section);
73 expectEquals(
74 visible,
75 section.classList.contains('visible'), 'section=' + section.id);
76 }
77
78 /**
79 * @param {string} printerId
80 * @return {!Object}
81 */
82 function getCddTemplate(printerId) {
83 return {
84 printerId: printerId,
85 capabilities: {
86 version: '1.0',
87 printer: {
88 supported_content_type: [{content_type: 'application/pdf'}],
89 collate: {},
90 color: {
91 option: [
92 {type: 'STANDARD_COLOR', is_default: true},
93 {type: 'STANDARD_MONOCHROME'}
94 ]
95 },
96 copies: {},
97 duplex: {
98 option: [
99 {type: 'NO_DUPLEX', is_default: true},
100 {type: 'LONG_EDGE'},
101 {type: 'SHORT_EDGE'}
102 ]
103 },
104 page_orientation: {
105 option: [
106 {type: 'PORTRAIT', is_default: true},
107 {type: 'LANDSCAPE'},
108 {type: 'AUTO'}
109 ]
110 },
111 media_size: {
112 option: [
113 { name: 'NA_LETTER',
114 width_microns: 215900,
115 height_microns: 279400,
116 is_default: true
117 }
118 ]
119 }
120 }
121 }
122 };
123 }
124
125 suite('PrintPreview', function() {
126 suiteSetup(function() {
dpapad 2017/05/26 18:47:12 Migrated the logic to replace CloudPrintInterface
rbpotter 2017/05/26 18:59:33 It seems like the only thing that should trigger t
dpapad 2017/05/26 19:24:19 Looking at the original CL who added that code, ht
127 function CloudPrintInterfaceStub() {
128 cr.EventTarget.call(this);
129 }
130 CloudPrintInterfaceStub.prototype = {
131 __proto__: cr.EventTarget.prototype,
132 search: function(isRecent) {}
133 };
134 var oldCpInterfaceEventType = cloudprint.CloudPrintInterfaceEventType;
135 cloudprint.CloudPrintInterface = CloudPrintInterfaceStub;
136 cloudprint.CloudPrintInterfaceEventType = oldCpInterfaceEventType;
137
138 print_preview.PreviewArea.prototype.checkPluginCompatibility_ =
139 function() {
140 return false;
141 };
142 });
143
144 setup(function() {
145 initialSettings = new print_preview.NativeInitialSettings(
146 false /*isInKioskAutoPrintMode*/,
147 false /*isInAppKioskMode*/,
148 ',' /*thousandsDelimeter*/,
149 '.' /*decimalDelimeter*/,
150 1 /*unitType*/,
151 true /*isDocumentModifiable*/,
152 'title' /*documentTitle*/,
153 true /*documentHasSelection*/,
154 false /*selectionOnly*/,
155 'FooDevice' /*systemDefaultDestinationId*/,
156 null /*serializedAppStateStr*/,
157 null /*serializedDefaultDestinationSelectionRulesStr*/);
158
159 localDestinationInfos = [
160 { printerName: 'FooName', deviceName: 'FooDevice' },
161 { printerName: 'BarName', deviceName: 'BarDevice' },
162 ];
163
164 nativeLayer = new print_preview.NativeLayerStub();
165 print_preview.NativeLayer.setInstance(nativeLayer);
166 printPreview = new print_preview.PrintPreview();
167 previewArea = printPreview.getPreviewArea();
168 });
169
170 // Test some basic assumptions about the print preview WebUI.
171 test('PrinterList', function() {
172 setInitialSettings();
173 return nativeLayer.whenCalled('getInitialSettings').then(
174 function() {
175 setLocalDestinations();
176 var recentList =
177 $('destination-search').querySelector('.recent-list ul');
178 var localList =
179 $('destination-search').querySelector('.local-list ul');
180 assertNotEquals(null, recentList);
181 assertEquals(1, recentList.childNodes.length);
182 assertEquals('FooName',
183 recentList.childNodes.item(0).querySelector(
184 '.destination-list-item-name').textContent);
185 assertNotEquals(null, localList);
186 assertEquals(3, localList.childNodes.length);
187 assertEquals(
188 'Save as PDF',
189 localList.childNodes.item(PDF_INDEX).
190 querySelector('.destination-list-item-name').textContent);
191 assertEquals(
192 'FooName',
193 localList.childNodes.item(FOO_INDEX).
194 querySelector('.destination-list-item-name').textContent);
195 assertEquals(
196 'BarName',
197 localList.childNodes.item(BAR_INDEX).
198 querySelector('.destination-list-item-name').textContent);
199 });
200 });
201
202 // Test that the printer list is structured correctly after calling
203 // addCloudPrinters with an empty list.
204 test('PrinterListCloudEmpty', function() {
205 setInitialSettings();
206
207 return nativeLayer.whenCalled('getInitialSettings').then(
208 function() {
209 setLocalDestinations();
210
211 var cloudPrintEnableEvent = new Event(
212 print_preview.NativeLayer.EventType.CLOUD_PRINT_ENABLE);
213 cloudPrintEnableEvent.baseCloudPrintUrl = 'cloudprint url';
214 nativeLayer.getEventTarget().dispatchEvent(
215 cloudPrintEnableEvent);
216
217 var searchDoneEvent =
218 new Event(cloudprint.CloudPrintInterfaceEventType.SEARCH_DONE);
219 searchDoneEvent.printers = [];
220 searchDoneEvent.isRecent = true;
221 searchDoneEvent.email = 'foo@chromium.org';
222 printPreview.cloudPrintInterface_.dispatchEvent(searchDoneEvent);
223
224 var recentList =
225 $('destination-search').querySelector('.recent-list ul');
226 var localList =
227 $('destination-search').querySelector('.local-list ul');
228 var cloudList =
229 $('destination-search').querySelector('.cloud-list ul');
230
231 assertNotEquals(null, recentList);
232 assertEquals(1, recentList.childNodes.length);
233 assertEquals('FooName',
234 recentList.childNodes.item(0).
235 querySelector('.destination-list-item-name').
236 textContent);
237
238 assertNotEquals(null, localList);
239 assertEquals(3, localList.childNodes.length);
240 assertEquals('Save as PDF',
241 localList.childNodes.item(PDF_INDEX).
242 querySelector('.destination-list-item-name').
243 textContent);
244 assertEquals('FooName',
245 localList.childNodes.
246 item(FOO_INDEX).
247 querySelector('.destination-list-item-name').
248 textContent);
249 assertEquals('BarName',
250 localList.childNodes.
251 item(BAR_INDEX).
252 querySelector('.destination-list-item-name').
253 textContent);
254
255 assertNotEquals(null, cloudList);
256 assertEquals(0, cloudList.childNodes.length);
257 });
258 });
259
260 // Test restore settings with one destination.
261 test('RestoreLocalDestination', function() {
262 initialSettings.serializedAppStateStr_ = JSON.stringify({
263 version: 2,
264 recentDestinations: [
265 {
266 id: 'ID',
267 origin: cr.isChromeOS ? 'chrome_os' : 'local',
268 account: '',
269 capabilities: 0,
270 name: '',
271 extensionId: '',
272 extensionName: '',
273 },
274 ],
275 });
276
277 setInitialSettings();
278 return nativeLayer.whenCalled('getInitialSettings');
279 });
280
281 test('RestoreMultipleDestinations', function() {
282 var origin = cr.isChromeOS ? 'chrome_os' : 'local';
283
284 initialSettings.serializedAppStateStr_ = JSON.stringify({
285 version: 2,
286 recentDestinations: [
287 {
288 id: 'ID1',
289 origin: origin,
290 account: '',
291 capabilities: 0,
292 name: '',
293 extensionId: '',
294 extensionName: '',
295 }, {
296 id: 'ID2',
297 origin: origin,
298 account: '',
299 capabilities: 0,
300 name: '',
301 extensionId: '',
302 extensionName: '',
303 }, {
304 id: 'ID3',
305 origin: origin,
306 account: '',
307 capabilities: 0,
308 name: '',
309 extensionId: '',
310 extensionName: '',
311 },
312 ],
313 });
314
315 setInitialSettings();
316
317 return nativeLayer.whenCalled('getInitialSettings').then(
318 function() {
319 // Set capabilities for the three recently used destinations + 1
320 // more.
321 setCapabilities(getCddTemplate('ID1'));
322 setCapabilities(getCddTemplate('ID2'));
323 setCapabilities(getCddTemplate('ID3'));
324 setCapabilities(getCddTemplate('ID4'));
325
326 // The most recently used destination should be the currently
327 // selected one. This is ID1.
328 assertEquals(
329 'ID1', printPreview.destinationStore_.selectedDestination.id);
330
331 // Look through the destinations. ID1, ID2, and ID3 should all be
332 // recent.
333 var destinations = printPreview.destinationStore_.destinations_;
334 var idsFound = [];
335
336 for (var i = 0; i < destinations.length; i++) {
337 if (!destinations[i])
338 continue;
339 if (destinations[i].isRecent)
340 idsFound.push(destinations[i].id);
341 }
342
343 // Make sure there were 3 recent destinations and that they are the
344 // correct IDs.
345 assertEquals(3, idsFound.length);
346 assertNotEquals(-1, idsFound.indexOf('ID1'));
347 assertNotEquals(-1, idsFound.indexOf('ID2'));
348 assertNotEquals(-1, idsFound.indexOf('ID3'));
349 });
350 });
351
352 test('DefaultDestinationSelectionRules', function() {
353 // It also makes sure these rules do override system default destination.
354 initialSettings.serializedDefaultDestinationSelectionRulesStr_ =
355 JSON.stringify({namePattern: '.*Bar.*'});
356 setInitialSettings();
357 return nativeLayer.whenCalled('getInitialSettings').then(
358 function() {
359 setLocalDestinations();
360 assertEquals(
361 'BarDevice',
362 printPreview.destinationStore_.selectedDestination.id);
363 });
364 });
365 });
366 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698