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