Chromium Code Reviews| OLD | NEW |
|---|---|
| (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() { | |
|
dpapad
2017/05/26 00:49:26
Migrated helper methods from print_preview.js here
| |
| 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 * @device - The device whose capabilities should be dispatched. | |
|
rbpotter
2017/05/26 01:34:25
@device should probably be updated to @param { ...
dpapad
2017/05/26 18:14:07
Changed to @param {!Object} for now, and planning
| |
| 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 function getCddTemplate(printerId) { | |
|
rbpotter
2017/05/26 01:34:24
@param {string} printerId
@return {...} same respo
dpapad
2017/05/26 18:14:07
Done. Using @param {!Object} for now, as in previo
| |
| 79 return { | |
| 80 printerId: printerId, | |
| 81 capabilities: { | |
| 82 version: '1.0', | |
| 83 printer: { | |
| 84 supported_content_type: [{content_type: 'application/pdf'}], | |
| 85 collate: {}, | |
| 86 color: { | |
| 87 option: [ | |
| 88 {type: 'STANDARD_COLOR', is_default: true}, | |
| 89 {type: 'STANDARD_MONOCHROME'} | |
| 90 ] | |
| 91 }, | |
| 92 copies: {}, | |
| 93 duplex: { | |
| 94 option: [ | |
| 95 {type: 'NO_DUPLEX', is_default: true}, | |
| 96 {type: 'LONG_EDGE'}, | |
| 97 {type: 'SHORT_EDGE'} | |
| 98 ] | |
| 99 }, | |
| 100 page_orientation: { | |
| 101 option: [ | |
| 102 {type: 'PORTRAIT', is_default: true}, | |
| 103 {type: 'LANDSCAPE'}, | |
| 104 {type: 'AUTO'} | |
| 105 ] | |
| 106 }, | |
| 107 media_size: { | |
| 108 option: [ | |
| 109 { name: 'NA_LETTER', | |
| 110 width_microns: 215900, | |
| 111 height_microns: 279400, | |
| 112 is_default: true | |
| 113 } | |
| 114 ] | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 }; | |
| 119 } | |
| 120 | |
| 121 suite('PrintPreview', function() { | |
| 122 setup(function() { | |
| 123 initialSettings = new print_preview.NativeInitialSettings( | |
| 124 false /*isInKioskAutoPrintMode*/, | |
| 125 false /*isInAppKioskMode*/, | |
| 126 ',' /*thousandsDelimeter*/, | |
| 127 '.' /*decimalDelimeter*/, | |
| 128 1 /*unitType*/, | |
| 129 true /*isDocumentModifiable*/, | |
| 130 'title' /*documentTitle*/, | |
| 131 true /*documentHasSelection*/, | |
| 132 false /*selectionOnly*/, | |
| 133 'FooDevice' /*systemDefaultDestinationId*/, | |
| 134 null /*serializedAppStateStr*/, | |
| 135 null /*serializedDefaultDestinationSelectionRulesStr*/); | |
| 136 | |
| 137 localDestinationInfos = [ | |
| 138 { printerName: 'FooName', deviceName: 'FooDevice' }, | |
| 139 { printerName: 'BarName', deviceName: 'BarDevice' }, | |
| 140 ]; | |
| 141 | |
| 142 nativeLayer = new print_preview.NativeLayerStub(); | |
| 143 print_preview.NativeLayer.setInstance(nativeLayer); | |
| 144 printPreview = new print_preview.PrintPreview(); | |
| 145 previewArea = printPreview.getPreviewArea(); | |
| 146 }); | |
| 147 | |
| 148 // Test some basic assumptions about the print preview WebUI. | |
| 149 test('PrinterList', function() { | |
| 150 setInitialSettings(); | |
| 151 nativeLayer.whenCalled('getInitialSettings').then( | |
| 152 function() { | |
| 153 setLocalDestinations(); | |
| 154 var recentList = | |
| 155 $('destination-search').querySelector('.recent-list ul'); | |
| 156 var localList = | |
| 157 $('destination-search').querySelector('.local-list ul'); | |
| 158 assertNotEquals(null, recentList); | |
| 159 assertEquals(1, recentList.childNodes.length); | |
| 160 assertEquals('FooName', | |
| 161 recentList.childNodes.item(0).querySelector( | |
| 162 '.destination-list-item-name').textContent); | |
| 163 assertNotEquals(null, localList); | |
| 164 assertEquals(3, localList.childNodes.length); | |
| 165 assertEquals( | |
| 166 'Save as PDF', | |
| 167 localList.childNodes.item(PDF_INDEX). | |
| 168 querySelector('.destination-list-item-name').textContent); | |
| 169 assertEquals( | |
| 170 'FooName', | |
| 171 localList.childNodes.item(FOO_INDEX). | |
| 172 querySelector('.destination-list-item-name').textContent); | |
| 173 assertEquals( | |
| 174 'BarName', | |
| 175 localList.childNodes.item(BAR_INDEX). | |
| 176 querySelector('.destination-list-item-name').textContent); | |
| 177 }); | |
| 178 }); | |
| 179 | |
| 180 // Test that the printer list is structured correctly after calling | |
| 181 // addCloudPrinters with an empty list. | |
| 182 test('PrinterListCloudEmpty', function() { | |
| 183 setInitialSettings(); | |
| 184 | |
| 185 return nativeLayer.whenCalled('getInitialSettings').then( | |
| 186 function() { | |
| 187 setLocalDestinations(); | |
| 188 | |
| 189 var cloudPrintEnableEvent = new Event( | |
| 190 print_preview.NativeLayer.EventType.CLOUD_PRINT_ENABLE); | |
| 191 cloudPrintEnableEvent.baseCloudPrintUrl = 'cloudprint url'; | |
| 192 nativeLayer.getEventTarget().dispatchEvent( | |
| 193 cloudPrintEnableEvent); | |
| 194 | |
| 195 var searchDoneEvent = | |
| 196 new Event(cloudprint.CloudPrintInterfaceEventType.SEARCH_DONE); | |
| 197 searchDoneEvent.printers = []; | |
| 198 searchDoneEvent.isRecent = true; | |
| 199 searchDoneEvent.email = 'foo@chromium.org'; | |
| 200 printPreview.cloudPrintInterface_.dispatchEvent(searchDoneEvent); | |
| 201 | |
| 202 var recentList = | |
| 203 $('destination-search').querySelector('.recent-list ul'); | |
| 204 var localList = | |
| 205 $('destination-search').querySelector('.local-list ul'); | |
| 206 var cloudList = | |
| 207 $('destination-search').querySelector('.cloud-list ul'); | |
| 208 | |
| 209 assertNotEquals(null, recentList); | |
| 210 assertEquals(1, recentList.childNodes.length); | |
| 211 assertEquals('FooName', | |
| 212 recentList.childNodes.item(0). | |
| 213 querySelector('.destination-list-item-name'). | |
| 214 textContent); | |
| 215 | |
| 216 assertNotEquals(null, localList); | |
| 217 assertEquals(3, localList.childNodes.length); | |
| 218 assertEquals('Save as PDF', | |
| 219 localList.childNodes.item(PDF_INDEX). | |
| 220 querySelector('.destination-list-item-name'). | |
| 221 textContent); | |
| 222 assertEquals('FooName', | |
| 223 localList.childNodes. | |
| 224 item(FOO_INDEX). | |
| 225 querySelector('.destination-list-item-name'). | |
| 226 textContent); | |
| 227 assertEquals('BarName', | |
| 228 localList.childNodes. | |
| 229 item(BAR_INDEX). | |
| 230 querySelector('.destination-list-item-name'). | |
| 231 textContent); | |
| 232 | |
| 233 assertNotEquals(null, cloudList); | |
| 234 assertEquals(0, cloudList.childNodes.length); | |
| 235 }); | |
| 236 }); | |
| 237 | |
| 238 // Test restore settings with one destination. | |
| 239 test('RestoreLocalDestination', function() { | |
| 240 initialSettings.serializedAppStateStr_ = JSON.stringify({ | |
| 241 version: 2, | |
| 242 recentDestinations: [ | |
| 243 { | |
| 244 id: 'ID', | |
| 245 origin: 'local', | |
|
rbpotter
2017/05/26 01:34:25
It is like this in the original test also, but won
dpapad
2017/05/26 18:14:07
Done.
| |
| 246 account: '', | |
| 247 capabilities: 0, | |
| 248 name: '', | |
| 249 extensionId: '', | |
| 250 extensionName: '', | |
| 251 }, | |
| 252 ], | |
| 253 }); | |
| 254 | |
| 255 setInitialSettings(); | |
| 256 return nativeLayer.whenCalled('getInitialSettings'); | |
| 257 }); | |
| 258 | |
| 259 test('RestoreMultipleDestinations', function() { | |
| 260 var origin = cr.isChromeOS ? 'chrome_os' : 'local'; | |
| 261 | |
| 262 initialSettings.serializedAppStateStr_ = JSON.stringify({ | |
| 263 version: 2, | |
| 264 recentDestinations: [ | |
| 265 { | |
| 266 id: 'ID1', | |
| 267 origin: origin, | |
| 268 account: '', | |
| 269 capabilities: 0, | |
| 270 name: '', | |
| 271 extensionId: '', | |
| 272 extensionName: '', | |
| 273 }, { | |
| 274 id: 'ID2', | |
| 275 origin: origin, | |
| 276 account: '', | |
| 277 capabilities: 0, | |
| 278 name: '', | |
| 279 extensionId: '', | |
| 280 extensionName: '', | |
| 281 }, { | |
| 282 id: 'ID3', | |
| 283 origin: origin, | |
| 284 account: '', | |
| 285 capabilities: 0, | |
| 286 name: '', | |
| 287 extensionId: '', | |
| 288 extensionName: '', | |
| 289 }, | |
| 290 ], | |
| 291 }); | |
| 292 | |
| 293 setInitialSettings(); | |
| 294 | |
| 295 return nativeLayer.whenCalled('getInitialSettings').then( | |
| 296 function() { | |
| 297 // Set capabilities for the three recently used destinations + 1 | |
| 298 // more. | |
| 299 setCapabilities(getCddTemplate('ID1')); | |
| 300 setCapabilities(getCddTemplate('ID2')); | |
| 301 setCapabilities(getCddTemplate('ID3')); | |
| 302 setCapabilities(getCddTemplate('ID4')); | |
| 303 | |
| 304 // The most recently used destination should be the currently | |
| 305 // selected one. This is ID1. | |
| 306 assertEquals( | |
| 307 'ID1', printPreview.destinationStore_.selectedDestination.id); | |
| 308 | |
| 309 // Look through the destinations. ID1, ID2, and ID3 should all be | |
| 310 // recent. | |
| 311 var destinations = printPreview.destinationStore_.destinations_; | |
| 312 var idsFound = []; | |
| 313 | |
| 314 for (var i = 0; i < destinations.length; i++) { | |
| 315 if (!destinations[i]) | |
| 316 continue; | |
| 317 if (destinations[i].isRecent) | |
| 318 idsFound.push(destinations[i].id); | |
| 319 } | |
| 320 | |
| 321 // Make sure there were 3 recent destinations and that they are the | |
| 322 // correct IDs. | |
| 323 assertEquals(3, idsFound.length); | |
| 324 assertNotEquals(-1, idsFound.indexOf('ID1')); | |
| 325 assertNotEquals(-1, idsFound.indexOf('ID2')); | |
| 326 assertNotEquals(-1, idsFound.indexOf('ID3')); | |
| 327 }); | |
| 328 }); | |
| 329 | |
| 330 test('DefaultDestinationSelectionRules', function() { | |
| 331 // It also makes sure these rules do override system default destination. | |
| 332 initialSettings.serializedDefaultDestinationSelectionRulesStr_ = | |
| 333 JSON.stringify({namePattern: '.*Bar.*'}); | |
| 334 setInitialSettings(); | |
| 335 return nativeLayer.whenCalled('getInitialSettings').then( | |
| 336 function() { | |
| 337 setLocalDestinations(); | |
| 338 assertEquals( | |
| 339 'BarDevice', | |
| 340 printPreview.destinationStore_.selectedDestination.id); | |
| 341 }); | |
| 342 }); | |
| 343 }); | |
| 344 }); | |
| OLD | NEW |