Chromium Code Reviews| 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 var ROOT_PATH = '../../../../'; | 5 var ROOT_PATH = '../../../../'; |
| 6 | 6 |
| 7 GEN_INCLUDE( | 7 GEN_INCLUDE( |
| 8 [ROOT_PATH + 'chrome/test/data/webui/polymer_browser_test_base.js']); | 8 [ROOT_PATH + 'chrome/test/data/webui/polymer_browser_test_base.js']); |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * Test fixture for DestinationSearch of Print Preview. | 11 * Test fixture for DestinationSearch of Print Preview. |
| 12 * @constructor | 12 * @constructor |
| 13 * @extends {PolymerTest} | 13 * @extends {PolymerTest} |
| 14 */ | 14 */ |
| 15 function PrintPreviewDestinationSearchTest() {} | 15 function PrintPreviewDestinationSearchTest() {} |
| 16 | 16 |
| 17 PrintPreviewDestinationSearchTest.prototype = { | 17 PrintPreviewDestinationSearchTest.prototype = { |
| 18 __proto__: PolymerTest.prototype, | 18 __proto__: PolymerTest.prototype, |
| 19 | 19 |
| 20 /** @override */ | 20 /** @override */ |
| 21 browsePreload: 'chrome://print', | 21 browsePreload: 'chrome://print', |
| 22 | 22 |
| 23 /** @override */ | 23 /** @override */ |
| 24 runAccessibilityChecks: false, | 24 runAccessibilityChecks: false, |
| 25 | 25 |
| 26 /** @override */ | 26 /** @override */ |
| 27 extraLibraries: PolymerTest.getLibraries(ROOT_PATH), | 27 extraLibraries: PolymerTest.getLibraries(ROOT_PATH).concat([ |
| 28 ROOT_PATH + 'chrome/test/data/webui/settings/test_browser_proxy.js', | |
| 29 ]), | |
| 30 | |
| 28 }; | 31 }; |
| 29 | 32 |
| 30 TEST_F('PrintPreviewDestinationSearchTest', 'Select', function() { | 33 TEST_F('PrintPreviewDestinationSearchTest', 'Select', function() { |
| 31 var self = this; | 34 var self = this; |
| 32 | 35 |
| 33 suite('DestinationSearchTest', function() { | 36 suite('DestinationSearchTest', function() { |
| 34 var root_; | 37 var root_; |
| 35 | 38 |
| 36 var destinationSearch_; | 39 var destinationSearch_; |
| 37 var nativeLayer_; | 40 var nativeLayer_; |
| 38 var invitationStore_; | 41 var invitationStore_; |
| 39 var destinationStore_; | 42 var destinationStore_; |
| 40 var userInfo_; | 43 var userInfo_; |
| 41 | 44 |
| 45 /** | |
| 46 * Test version of the native layer. | |
| 47 * TODO (rbpotter): Merge this with NativeLayerStub() from print_preview.js | |
| 48 * and put into a separate file. | |
| 49 * @constructor | |
| 50 * @extends {settings.TestBrowserProxy} | |
| 51 */ | |
| 52 function NativeLayerStub() { | |
| 53 settings.TestBrowserProxy.call(this, [ 'setupPrinter' ]); | |
| 54 this.destinationToWatch_ = ''; | |
| 55 this.eventTarget_ = mock(cr.EventTarget); | |
| 56 this.getLocalDestinationCapabilitiesCallCount_ = 0; | |
| 57 this.setupPrinterResponse_ = null; | |
| 58 this.shouldReject_ = false; | |
| 59 } | |
| 60 | |
| 61 NativeLayerStub.prototype = { | |
| 62 __proto__: settings.TestBrowserProxy.prototype, | |
| 63 didGetCapabilitiesOnce: function(destinationId) { | |
| 64 return (destinationId == this.destinationToWatch_ && | |
| 65 this.getLocalDestinationCapabilitiesCallCount_ == 1); | |
| 66 }, | |
| 67 getEventTarget: function() { return this.eventTarget_; }, | |
| 68 setDestinationToWatch: function(destinationId) { | |
| 69 this.destinationToWatch_ = destinationId; | |
| 70 this.getLocalDestinationCapabilitiesCallCount_ = 0; | |
| 71 }, | |
| 72 setSetupPrinterResponse: function(reject, response) { | |
| 73 this.shouldReject_ = reject; | |
| 74 this.setupPrinterResponse_ = response; | |
| 75 }, | |
| 76 setupPrinter: function(printerId) { | |
| 77 this.methodCalled('setupPrinter', printerId); | |
| 78 return this.shouldReject_ ? | |
| 79 Promise.reject(this.setupPrinterResponse_) : | |
| 80 Promise.resolve(this.setupPrinterResponse_); | |
| 81 }, | |
| 82 startGetLocalDestinationCapabilities: function(destinationId) { | |
| 83 if (destinationId == this.destinationToWatch_) | |
| 84 this.getLocalDestinationCapabilitiesCallCount_++; | |
| 85 }, | |
| 86 }; | |
| 87 NativeLayerStub.EventType = print_preview.NativeLayer.EventType; | |
| 88 | |
| 42 function getCaps() { | 89 function getCaps() { |
| 43 return { | 90 return { |
| 44 'printer': { | 91 'printer': { |
| 45 'color': { | 92 'color': { |
| 46 'option': [{ | 93 'option': [{ |
| 47 'is_default': true, | 94 'is_default': true, |
| 48 'type': 'STANDARD_MONOCHROME', | 95 'type': 'STANDARD_MONOCHROME', |
| 49 'vendor_id': '13' | 96 'vendor_id': '13' |
| 50 }] | 97 }] |
| 51 }, | 98 }, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 var listener = function(e) { | 139 var listener = function(e) { |
| 93 resolve(); | 140 resolve(); |
| 94 element.removeEventListener(eventName, listener); | 141 element.removeEventListener(eventName, listener); |
| 95 }; | 142 }; |
| 96 | 143 |
| 97 element.addEventListener(eventName, listener); | 144 element.addEventListener(eventName, listener); |
| 98 }); | 145 }); |
| 99 }; | 146 }; |
| 100 | 147 |
| 101 function mockSetupCall(destId, nativeLayerMock) { | 148 function mockSetupCall(destId, nativeLayerMock) { |
| 149 assert (!cr.isChromeOS); | |
| 150 nativeLayerMock.setDestinationToWatch(destId); | |
| 102 var resolver = new PromiseResolver(); | 151 var resolver = new PromiseResolver(); |
| 103 | 152 |
| 104 if (cr.isChromeOS) { | |
| 105 nativeLayerMock.expects(once()).setupPrinter(destId). | |
| 106 will(returnValue(resolver.promise)); | |
| 107 | |
| 108 return resolver; | |
| 109 } | |
| 110 | |
| 111 nativeLayerMock.expects(once()).startGetLocalDestinationCapabilities( | |
| 112 destId); | |
| 113 resolver.promise.then( | 153 resolver.promise.then( |
| 114 function(result) { | 154 function(result) { |
| 115 // Simulate the native layer dispatching capabilities. | 155 // Simulate the native layer dispatching capabilities. |
| 116 var capsSetEvent = | 156 var capsSetEvent = |
| 117 new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET); | 157 new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET); |
| 118 capsSetEvent.settingsInfo = result; | 158 capsSetEvent.settingsInfo = result; |
| 119 destinationStore_.onLocalDestinationCapabilitiesSet_(capsSetEvent); | 159 destinationStore_.onLocalDestinationCapabilitiesSet_(capsSetEvent); |
| 160 expectTrue(nativeLayerMock.didGetCapabilitiesOnce(destId)); | |
| 120 }.bind(this), | 161 }.bind(this), |
| 121 function() { | 162 function() { |
| 122 var failEvent = new Event( | 163 var failEvent = new Event( |
| 123 print_preview.NativeLayer.EventType.GET_CAPABILITIES_FAIL); | 164 print_preview.NativeLayer.EventType.GET_CAPABILITIES_FAIL); |
| 124 failEvent.destinationId = destId; | 165 failEvent.destinationId = destId; |
| 125 destinationStore_.onGetCapabilitiesFail_(failEvent); | 166 destinationStore_.onGetCapabilitiesFail_(failEvent); |
| 167 expectTrue(nativeLayerMock.didGetCapabilitiesOnce(destId)); | |
| 126 }.bind(this)); | 168 }.bind(this)); |
| 127 | 169 |
| 128 return resolver; | 170 return resolver; |
| 129 }; | 171 }; |
| 130 | 172 |
| 131 function requestSetup(destId, destinationSearch) { | 173 function requestSetup(destId, destinationSearch) { |
| 132 var origin = cr.isChromeOS ? print_preview.DestinationOrigin.CROS : | 174 var origin = cr.isChromeOS ? print_preview.DestinationOrigin.CROS : |
| 133 print_preview.DestinationOrigin.LOCAL; | 175 print_preview.DestinationOrigin.LOCAL; |
| 134 | 176 |
| 135 var dest = new print_preview.Destination(destId, | 177 var dest = new print_preview.Destination(destId, |
| 136 print_preview.DestinationType.LOCAL, | 178 print_preview.DestinationType.LOCAL, |
| 137 origin, | 179 origin, |
| 138 "displayName", | 180 "displayName", |
| 139 print_preview.DestinationConnectionStatus.ONLINE); | 181 print_preview.DestinationConnectionStatus.ONLINE); |
| 140 | 182 |
| 141 // Add the destination to the list. | 183 // Add the destination to the list. |
| 142 destinationSearch.localList_.updateDestinations([dest]); | 184 destinationSearch.localList_.updateDestinations([dest]); |
| 143 | 185 |
| 144 // Select destination. | 186 // Select destination. |
| 145 if (cr.isChromeOS) { | 187 if (cr.isChromeOS) { |
| 146 destinationSearch.handleConfigureDestination_(dest); | 188 destinationSearch.handleConfigureDestination_(dest); |
| 147 } else { | 189 } else { |
| 148 destinationSearch.handleOnDestinationSelect_(dest); | 190 destinationSearch.handleOnDestinationSelect_(dest); |
| 149 } | 191 } |
| 150 }; | 192 }; |
| 151 | 193 |
| 152 function resolveSetup(resolver, printerId, success, capabilities) { | |
| 153 var response = { | |
| 154 printerId: printerId, | |
| 155 capabilities: capabilities, | |
| 156 success: success | |
| 157 }; | |
| 158 | |
| 159 resolver.resolve(response); | |
| 160 } | |
| 161 | |
| 162 setup(function() { | 194 setup(function() { |
| 163 Mock4JS.clearMocksToVerify(); | 195 Mock4JS.clearMocksToVerify(); |
| 164 | 196 nativeLayer_ = new NativeLayerStub(); |
| 165 nativeLayer_ = mock(print_preview.NativeLayer); | 197 var nativeLayerEventTarget = nativeLayer_.getEventTarget(); |
| 166 nativeLayer_.expects(atLeastOnce()) | 198 nativeLayerEventTarget.expects(atLeastOnce()) |
| 167 .addEventListener(ANYTHING, ANYTHING, ANYTHING); | 199 .addEventListener(ANYTHING, ANYTHING, ANYTHING); |
| 168 | 200 |
| 169 invitationStore_ = new print_preview.InvitationStore(); | 201 invitationStore_ = new print_preview.InvitationStore(); |
| 202 var nativeLayerProxy = nativeLayer_; | |
| 203 nativeLayerProxy.eventTarget_ = nativeLayerEventTarget.proxy(); | |
| 170 destinationStore_ = new print_preview.DestinationStore( | 204 destinationStore_ = new print_preview.DestinationStore( |
| 171 nativeLayer_.proxy(), new print_preview.UserInfo(), | 205 nativeLayerProxy, new print_preview.UserInfo(), |
| 172 new print_preview.AppState()); | 206 new print_preview.AppState()); |
| 173 userInfo_ = new print_preview.UserInfo(); | 207 userInfo_ = new print_preview.UserInfo(); |
| 174 | 208 |
| 175 destinationSearch_ = new print_preview.DestinationSearch( | 209 destinationSearch_ = new print_preview.DestinationSearch( |
| 176 destinationStore_, invitationStore_, userInfo_); | 210 destinationStore_, invitationStore_, userInfo_); |
| 177 destinationSearch_.decorate($('destination-search')); | 211 destinationSearch_.decorate($('destination-search')); |
| 178 }); | 212 }); |
| 179 | 213 |
| 180 teardown(function() { | 214 teardown(function() { |
| 181 Mock4JS.verifyAllMocks(); | 215 Mock4JS.verifyAllMocks(); |
| 182 }); | 216 }); |
| 183 | 217 |
| 184 test('ResolutionFails', function() { | 218 test('ResolutionFails', function() { |
| 185 var destId = "001122DEADBEEF"; | 219 var destId = "001122DEADBEEF"; |
| 186 | 220 if (cr.isChromeOS) { |
| 187 var resolver = mockSetupCall(destId, nativeLayer_); | 221 nativeLayer_.setSetupPrinterResponse(true, { printerId: destId, |
| 188 requestSetup(destId, destinationSearch_); | 222 success: false,}); |
| 189 resolver.reject(destId); | 223 requestSetup(destId, destinationSearch_); |
| 224 return nativeLayer_.whenCalled('setupPrinter').then( | |
| 225 function(actualDestId) { | |
| 226 assertEquals(destId, actualDestId); | |
| 227 }); | |
| 228 } else { | |
| 229 var resolver = mockSetupCall(destId, nativeLayer_); | |
| 230 requestSetup(destId, destinationSearch_); | |
| 231 resolver.reject(destId); | |
| 232 } | |
| 190 }); | 233 }); |
| 191 | 234 |
| 192 test('ReceiveSuccessfulSetup', function() { | 235 test('ReceiveSuccessfulSetup', function() { |
| 193 var destId = "00112233DEADBEEF"; | 236 var destId = "00112233DEADBEEF"; |
| 237 var response = { | |
| 238 printerId: destId, | |
| 239 capabilities: getCaps(), | |
| 240 success: true, | |
| 241 }; | |
| 242 if (cr.isChromeOS) | |
| 243 nativeLayer_.setSetupPrinterResponse(false, response); | |
| 194 | 244 |
| 195 var waiter = waitForEvent( | 245 var waiter = waitForEvent( |
| 196 destinationStore_, | 246 destinationStore_, |
| 197 print_preview.DestinationStore.EventType.DESTINATION_SELECT); | 247 print_preview.DestinationStore.EventType.DESTINATION_SELECT); |
| 198 | 248 if (cr.isChromeOS) { |
| 199 var resolver = mockSetupCall(destId, nativeLayer_); | 249 requestSetup(destId, destinationSearch_); |
| 200 | 250 return Promise.all([nativeLayer_.whenCalled('setupPrinter').then( |
|
dpapad
2017/05/19 02:07:06
Instead of adding a then() in the individual promi
rbpotter
2017/05/19 02:25:38
Done.
| |
| 201 requestSetup(destId, destinationSearch_); | 251 function(actualDestId) { |
| 202 resolveSetup(resolver, destId, true, getCaps()); | 252 assertEquals(destId, actualDestId); |
| 203 | 253 }), |
| 204 // wait for event propogation to complete. | 254 waiter]). |
| 205 return waiter.then(function() { | 255 then(function() { |
| 206 // after setup succeeds, the destination should be selected. | 256 // after setup succeeds and event arrives, the destination should |
| 207 assertNotEquals(null, destinationStore_.selectedDestination); | 257 // be selected. |
| 208 assertEquals(destId, destinationStore_.selectedDestination.id); | 258 assertNotEquals(null, destinationStore_.selectedDestination); |
| 209 }); | 259 assertEquals(destId, destinationStore_.selectedDestination.id); |
| 260 }); | |
| 261 } else { //!cr.isChromeOS | |
| 262 var resolver = mockSetupCall(destId, nativeLayer_); | |
| 263 requestSetup(destId, destinationSearch_); | |
| 264 resolver.resolve(response); | |
| 265 return waiter.then(function() { | |
| 266 // after setup succeeds, the destination should be selected. | |
| 267 assertNotEquals(null, destinationStore_.selectedDestination); | |
| 268 assertEquals(destId, destinationStore_.selectedDestination.id); | |
| 269 }); | |
| 270 } | |
| 210 }); | 271 }); |
| 211 | 272 |
| 212 if (cr.isChromeOS) { | 273 if (cr.isChromeOS) { |
| 213 // The 'ResolutionFails' test covers this case for non-CrOS. | 274 // The 'ResolutionFails' test covers this case for non-CrOS. |
| 214 test('ReceiveFailedSetup', function() { | 275 test('ReceiveFailedSetup', function() { |
| 215 var destId = '00112233DEADBEEF'; | 276 var destId = '00112233DEADBEEF'; |
| 216 | 277 var response = { |
| 217 var resolver = mockSetupCall(destId, nativeLayer_); | 278 printerId: destId, |
| 279 capabilities: getCaps(), | |
| 280 success: false, | |
| 281 }; | |
| 282 nativeLayer_.setSetupPrinterResponse(false, response); | |
| 218 requestSetup(destId, destinationSearch_); | 283 requestSetup(destId, destinationSearch_); |
| 219 | 284 return nativeLayer_.whenCalled('setupPrinter').then( |
| 220 // Force resolution to fail. | 285 function (actualDestId) { |
| 221 resolveSetup(resolver, destId, false, null); | 286 // Selection should not change on ChromeOS. |
| 222 | 287 assertEquals(destId, actualDestId); |
| 223 // Selection should not change on ChromeOS. | 288 assertEquals(null, destinationStore_.selectedDestination); |
| 224 assertEquals(null, destinationStore_.selectedDestination); | 289 }); |
| 225 }); | 290 }); |
| 226 } | 291 } |
| 227 | 292 |
| 228 test('CloudKioskPrinter', function() { | 293 test('CloudKioskPrinter', function() { |
| 229 var printerId = 'cloud-printer-id'; | 294 var printerId = 'cloud-printer-id'; |
| 230 | 295 |
| 231 // Create cloud destination. | 296 // Create cloud destination. |
| 232 var cloudDest = new print_preview.Destination(printerId, | 297 var cloudDest = new print_preview.Destination(printerId, |
| 233 print_preview.DestinationType.GOOGLE, | 298 print_preview.DestinationType.GOOGLE, |
| 234 print_preview.DestinationOrigin.DEVICE, | 299 print_preview.DestinationOrigin.DEVICE, |
| 235 "displayName", | 300 "displayName", |
| 236 print_preview.DestinationConnectionStatus.ONLINE); | 301 print_preview.DestinationConnectionStatus.ONLINE); |
| 237 cloudDest.capabilities = getCaps(); | 302 cloudDest.capabilities = getCaps(); |
| 238 | 303 |
| 239 // Place destination in the local list as happens for Kiosk printers. | 304 // Place destination in the local list as happens for Kiosk printers. |
| 240 destinationSearch_.localList_.updateDestinations([cloudDest]); | 305 destinationSearch_.localList_.updateDestinations([cloudDest]); |
| 241 var dest = destinationSearch_.localList_.getDestinationItem(printerId); | 306 var dest = destinationSearch_.localList_.getDestinationItem(printerId); |
| 242 // Simulate a click. | 307 // Simulate a click. |
| 243 dest.onActivate_(); | 308 dest.onActivate_(); |
| 244 | 309 |
| 245 // Verify that the destination has been selected. | 310 // Verify that the destination has been selected. |
| 246 assertEquals(printerId, destinationStore_.selectedDestination.id); | 311 assertEquals(printerId, destinationStore_.selectedDestination.id); |
| 247 }); | 312 }); |
| 248 }); | 313 }); |
| 249 | 314 |
| 250 mocha.run(); | 315 mocha.run(); |
| 251 }); | 316 }); |
| OLD | NEW |