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'); | |
dpapad
2017/05/19 00:10:18
this.methodCalled('setupPrinter', printerId);
Thi
rbpotter
2017/05/19 01:27:01
Done.
| |
78 if (this.shouldReject_) | |
dpapad
2017/05/19 00:10:18
return this.shouldReject_ ?
Promise.reject(thi
rbpotter
2017/05/19 01:27:00
Done.
| |
79 return Promise.reject(this.setupPrinterResponse_); | |
80 else | |
81 return Promise.resolve(this.setupPrinterResponse_); | |
82 }, | |
83 startGetLocalDestinationCapabilities: function(destinationId) { | |
84 if (destinationId == this.destinationToWatch_) | |
85 this.getLocalDestinationCapabilitiesCallCount_++; | |
86 }, | |
87 }; | |
88 NativeLayerStub.EventType = print_preview.NativeLayer.EventType; | |
89 | |
42 function getCaps() { | 90 function getCaps() { |
43 return { | 91 return { |
44 'printer': { | 92 'printer': { |
45 'color': { | 93 'color': { |
46 'option': [{ | 94 'option': [{ |
47 'is_default': true, | 95 'is_default': true, |
48 'type': 'STANDARD_MONOCHROME', | 96 'type': 'STANDARD_MONOCHROME', |
49 'vendor_id': '13' | 97 'vendor_id': '13' |
50 }] | 98 }] |
51 }, | 99 }, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 var listener = function(e) { | 140 var listener = function(e) { |
93 resolve(); | 141 resolve(); |
94 element.removeEventListener(eventName, listener); | 142 element.removeEventListener(eventName, listener); |
95 }; | 143 }; |
96 | 144 |
97 element.addEventListener(eventName, listener); | 145 element.addEventListener(eventName, listener); |
98 }); | 146 }); |
99 }; | 147 }; |
100 | 148 |
101 function mockSetupCall(destId, nativeLayerMock) { | 149 function mockSetupCall(destId, nativeLayerMock) { |
150 assert (!cr.isChromeOS); | |
151 nativeLayerMock.setDestinationToWatch(destId); | |
102 var resolver = new PromiseResolver(); | 152 var resolver = new PromiseResolver(); |
103 | 153 |
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( | 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; |
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 if (cr.isChromeOS) { |
187 var resolver = mockSetupCall(destId, nativeLayer_); | 222 nativeLayer_.setSetupPrinterResponse(true, { printerId: destId, |
188 requestSetup(destId, destinationSearch_); | 223 success: false,}); |
189 resolver.reject(destId); | 224 requestSetup(destId, destinationSearch_); |
225 return nativeLayer_.whenCalled('setupPrinter', destId); | |
226 } else { | |
227 var resolver = mockSetupCall(destId, nativeLayer_); | |
228 requestSetup(destId, destinationSearch_); | |
229 resolver.reject(destId); | |
230 } | |
190 }); | 231 }); |
191 | 232 |
192 test('ReceiveSuccessfulSetup', function() { | 233 test('ReceiveSuccessfulSetup', function() { |
193 var destId = "00112233DEADBEEF"; | 234 var destId = "00112233DEADBEEF"; |
235 var response = { | |
236 printerId: destId, | |
237 capabilities: getCaps(), | |
238 success: true, | |
239 }; | |
240 if (cr.isChromeOS) | |
241 nativeLayer_.setSetupPrinterResponse(false, response); | |
194 | 242 |
195 var waiter = waitForEvent( | 243 var waiter = waitForEvent( |
196 destinationStore_, | 244 destinationStore_, |
197 print_preview.DestinationStore.EventType.DESTINATION_SELECT); | 245 print_preview.DestinationStore.EventType.DESTINATION_SELECT); |
198 | 246 if (cr.isChromeOS) { |
199 var resolver = mockSetupCall(destId, nativeLayer_); | 247 requestSetup(destId, destinationSearch_); |
200 | 248 return Promise.all([nativeLayer_.whenCalled('setupPrinter', destId), |
201 requestSetup(destId, destinationSearch_); | 249 waiter]). |
202 resolveSetup(resolver, destId, true, getCaps()); | 250 then(function() { |
203 | 251 // after setup succeeds and event arrives, the destination should |
204 // wait for event propogation to complete. | 252 // be selected. |
205 return waiter.then(function() { | 253 assertNotEquals(null, destinationStore_.selectedDestination); |
206 // after setup succeeds, the destination should be selected. | 254 assertEquals(destId, destinationStore_.selectedDestination.id); |
207 assertNotEquals(null, destinationStore_.selectedDestination); | 255 }.bind(destinationStore_, destId)); |
208 assertEquals(destId, destinationStore_.selectedDestination.id); | 256 } else { //!cr.isChromeOS |
209 }); | 257 var resolver = mockSetupCall(destId, nativeLayer_); |
258 requestSetup(destId, destinationSearch_); | |
259 resolver.resolve(response); | |
260 return waiter.then(function() { | |
261 // after setup succeeds, the destination should be selected. | |
262 assertNotEquals(null, destinationStore_.selectedDestination); | |
263 assertEquals(destId, destinationStore_.selectedDestination.id); | |
264 }); | |
265 } | |
210 }); | 266 }); |
211 | 267 |
212 if (cr.isChromeOS) { | 268 if (cr.isChromeOS) { |
213 // The 'ResolutionFails' test covers this case for non-CrOS. | 269 // The 'ResolutionFails' test covers this case for non-CrOS. |
214 test('ReceiveFailedSetup', function() { | 270 test('ReceiveFailedSetup', function() { |
215 var destId = '00112233DEADBEEF'; | 271 var destId = '00112233DEADBEEF'; |
216 | 272 var response = { |
217 var resolver = mockSetupCall(destId, nativeLayer_); | 273 printerId: destId, |
274 capabilities: getCaps(), | |
275 success: false, | |
276 }; | |
277 nativeLayer_.setSetupPrinterResponse(false, response); | |
218 requestSetup(destId, destinationSearch_); | 278 requestSetup(destId, destinationSearch_); |
219 | 279 return nativeLayer_.whenCalled('setupPrinter', destId).then( |
dpapad
2017/05/19 00:10:18
whenCalled() only accepts one parameter (here and
rbpotter
2017/05/19 01:27:00
Done.
| |
220 // Force resolution to fail. | 280 function () { |
221 resolveSetup(resolver, destId, false, null); | 281 // Selection should not change on ChromeOS. |
222 | 282 assertEquals(null, destinationStore_.selectedDestination); |
223 // Selection should not change on ChromeOS. | 283 }.bind(destinationStore_)); |
dpapad
2017/05/19 00:10:18
Is the bind() call necessary? At first glance I do
rbpotter
2017/05/19 01:27:01
Done.
| |
224 assertEquals(null, destinationStore_.selectedDestination); | |
225 }); | 284 }); |
226 } | 285 } |
227 | 286 |
228 test('CloudKioskPrinter', function() { | 287 test('CloudKioskPrinter', function() { |
229 var printerId = 'cloud-printer-id'; | 288 var printerId = 'cloud-printer-id'; |
230 | 289 |
231 // Create cloud destination. | 290 // Create cloud destination. |
232 var cloudDest = new print_preview.Destination(printerId, | 291 var cloudDest = new print_preview.Destination(printerId, |
233 print_preview.DestinationType.GOOGLE, | 292 print_preview.DestinationType.GOOGLE, |
234 print_preview.DestinationOrigin.DEVICE, | 293 print_preview.DestinationOrigin.DEVICE, |
235 "displayName", | 294 "displayName", |
236 print_preview.DestinationConnectionStatus.ONLINE); | 295 print_preview.DestinationConnectionStatus.ONLINE); |
237 cloudDest.capabilities = getCaps(); | 296 cloudDest.capabilities = getCaps(); |
238 | 297 |
239 // Place destination in the local list as happens for Kiosk printers. | 298 // Place destination in the local list as happens for Kiosk printers. |
240 destinationSearch_.localList_.updateDestinations([cloudDest]); | 299 destinationSearch_.localList_.updateDestinations([cloudDest]); |
241 var dest = destinationSearch_.localList_.getDestinationItem(printerId); | 300 var dest = destinationSearch_.localList_.getDestinationItem(printerId); |
242 // Simulate a click. | 301 // Simulate a click. |
243 dest.onActivate_(); | 302 dest.onActivate_(); |
244 | 303 |
245 // Verify that the destination has been selected. | 304 // Verify that the destination has been selected. |
246 assertEquals(printerId, destinationStore_.selectedDestination.id); | 305 assertEquals(printerId, destinationStore_.selectedDestination.id); |
247 }); | 306 }); |
248 }); | 307 }); |
249 | 308 |
250 mocha.run(); | 309 mocha.run(); |
251 }); | 310 }); |
OLD | NEW |