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 var ROOT_PATH = '../../../../'; | |
6 | |
7 GEN_INCLUDE( | |
8 [ROOT_PATH + 'chrome/test/data/webui/polymer_browser_test_base.js']); | |
9 | |
10 /** | |
11 * Test fixture for DestinationSearch of Print Preview. | |
12 * @constructor | |
13 * @extends {PolymerTest} | |
14 */ | |
15 function PrintPreviewDestinationSearchTest() {} | |
16 | |
17 PrintPreviewDestinationSearchTest.prototype = { | |
18 __proto__: PolymerTest.prototype, | |
19 | |
20 /** @override */ | |
21 browsePreload: 'chrome://print', | |
22 | |
23 /** @override */ | |
24 runAccessibilityChecks: false, | |
25 | |
26 /** @override */ | |
27 extraLibraries: PolymerTest.getLibraries(ROOT_PATH).concat([ | |
28 ROOT_PATH + 'chrome/test/data/webui/settings/test_browser_proxy.js', | |
29 ]), | |
30 | |
31 }; | |
32 | |
33 TEST_F('PrintPreviewDestinationSearchTest', 'Select', function() { | |
34 var self = this; | |
35 | |
36 suite('DestinationSearchTest', function() { | |
37 var root_; | |
38 | |
39 var destinationSearch_; | |
40 var nativeLayer_; | |
41 var invitationStore_; | |
42 var destinationStore_; | |
43 var userInfo_; | |
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 | |
89 function getCaps() { | |
90 return { | |
91 'printer': { | |
92 'color': { | |
93 'option': [{ | |
94 'is_default': true, | |
95 'type': 'STANDARD_MONOCHROME', | |
96 'vendor_id': '13' | |
97 }] | |
98 }, | |
99 'copies': {}, | |
100 'duplex': { | |
101 'option': [ | |
102 {'type': 'NO_DUPLEX'}, {'is_default': true, 'type': 'LONG_EDGE'}, | |
103 {'type': 'SHORT_EDGE'} | |
104 ] | |
105 }, | |
106 'media_size': { | |
107 'option': [ | |
108 { | |
109 'custom_display_name': 'na letter', | |
110 'height_microns': 279400, | |
111 'is_default': true, | |
112 'name': 'NA_LETTER', | |
113 'vendor_id': 'na_letter_8.5x11in', | |
114 'width_microns': 215900 | |
115 }, | |
116 { | |
117 'custom_display_name': 'na legal', | |
118 'height_microns': 355600, | |
119 'name': 'NA_LEGAL', | |
120 'vendor_id': 'na_legal_8.5x14in', | |
121 'width_microns': 215900 | |
122 } | |
123 ] | |
124 }, | |
125 'page_orientation': { | |
126 'option': [ | |
127 {'is_default': true, 'type': 'PORTRAIT'}, {'type': 'LANDSCAPE'}, | |
128 {'type': 'AUTO'} | |
129 ] | |
130 }, | |
131 'supported_content_type': [{'content_type': 'application/pdf'}] | |
132 }, | |
133 'version': '1.0' | |
134 }; | |
135 }; | |
136 | |
137 function waitForEvent(element, eventName) { | |
138 return new Promise(function(resolve) { | |
139 var listener = function(e) { | |
140 resolve(); | |
141 element.removeEventListener(eventName, listener); | |
142 }; | |
143 | |
144 element.addEventListener(eventName, listener); | |
145 }); | |
146 }; | |
147 | |
148 function mockSetupCall(destId, nativeLayerMock) { | |
149 assert (!cr.isChromeOS); | |
150 nativeLayerMock.setDestinationToWatch(destId); | |
151 var resolver = new PromiseResolver(); | |
152 | |
153 resolver.promise.then( | |
154 function(result) { | |
155 // Simulate the native layer dispatching capabilities. | |
156 var capsSetEvent = | |
157 new Event(print_preview.NativeLayer.EventType.CAPABILITIES_SET); | |
158 capsSetEvent.settingsInfo = result; | |
159 destinationStore_.onLocalDestinationCapabilitiesSet_(capsSetEvent); | |
160 expectTrue(nativeLayerMock.didGetCapabilitiesOnce(destId)); | |
161 }.bind(this), | |
162 function() { | |
163 var failEvent = new Event( | |
164 print_preview.NativeLayer.EventType.GET_CAPABILITIES_FAIL); | |
165 failEvent.destinationId = destId; | |
166 destinationStore_.onGetCapabilitiesFail_(failEvent); | |
167 expectTrue(nativeLayerMock.didGetCapabilitiesOnce(destId)); | |
168 }.bind(this)); | |
169 | |
170 return resolver; | |
171 }; | |
172 | |
173 function requestSetup(destId, destinationSearch) { | |
174 var origin = cr.isChromeOS ? print_preview.DestinationOrigin.CROS : | |
175 print_preview.DestinationOrigin.LOCAL; | |
176 | |
177 var dest = new print_preview.Destination(destId, | |
178 print_preview.DestinationType.LOCAL, | |
179 origin, | |
180 "displayName", | |
181 print_preview.DestinationConnectionStatus.ONLINE); | |
182 | |
183 // Add the destination to the list. | |
184 destinationSearch.localList_.updateDestinations([dest]); | |
185 | |
186 // Select destination. | |
187 if (cr.isChromeOS) { | |
188 destinationSearch.handleConfigureDestination_(dest); | |
189 } else { | |
190 destinationSearch.handleOnDestinationSelect_(dest); | |
191 } | |
192 }; | |
193 | |
194 setup(function() { | |
195 Mock4JS.clearMocksToVerify(); | |
196 nativeLayer_ = new NativeLayerStub(); | |
197 var nativeLayerEventTarget = nativeLayer_.getEventTarget(); | |
198 nativeLayerEventTarget.expects(atLeastOnce()) | |
199 .addEventListener(ANYTHING, ANYTHING, ANYTHING); | |
200 | |
201 invitationStore_ = new print_preview.InvitationStore(); | |
202 var nativeLayerProxy = nativeLayer_; | |
203 nativeLayerProxy.eventTarget_ = nativeLayerEventTarget.proxy(); | |
204 destinationStore_ = new print_preview.DestinationStore( | |
205 nativeLayerProxy, new print_preview.UserInfo(), | |
206 new print_preview.AppState()); | |
207 userInfo_ = new print_preview.UserInfo(); | |
208 | |
209 destinationSearch_ = new print_preview.DestinationSearch( | |
210 destinationStore_, invitationStore_, userInfo_); | |
211 destinationSearch_.decorate($('destination-search')); | |
212 }); | |
213 | |
214 teardown(function() { | |
215 Mock4JS.verifyAllMocks(); | |
216 }); | |
217 | |
218 test('ResolutionFails', function() { | |
219 var destId = "001122DEADBEEF"; | |
220 if (cr.isChromeOS) { | |
221 nativeLayer_.setSetupPrinterResponse(true, { printerId: destId, | |
222 success: false,}); | |
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 } | |
233 }); | |
234 | |
235 test('ReceiveSuccessfulSetup', function() { | |
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); | |
244 | |
245 var waiter = waitForEvent( | |
246 destinationStore_, | |
247 print_preview.DestinationStore.EventType.DESTINATION_SELECT); | |
248 if (cr.isChromeOS) { | |
249 requestSetup(destId, destinationSearch_); | |
250 return Promise.all([ | |
251 nativeLayer_.whenCalled('setupPrinter'), waiter | |
252 ]).then(function(results) { | |
253 assertEquals(destId, results[0]); | |
254 | |
255 // after setup succeeds and event arrives, the destination should | |
256 // be selected. | |
257 assertNotEquals(null, destinationStore_.selectedDestination); | |
258 assertEquals(destId, destinationStore_.selectedDestination.id); | |
259 }); | |
260 } else { //!cr.isChromeOS | |
261 var resolver = mockSetupCall(destId, nativeLayer_); | |
262 requestSetup(destId, destinationSearch_); | |
263 resolver.resolve(response); | |
264 return waiter.then(function() { | |
265 // after setup succeeds, the destination should be selected. | |
266 assertNotEquals(null, destinationStore_.selectedDestination); | |
267 assertEquals(destId, destinationStore_.selectedDestination.id); | |
268 }); | |
269 } | |
270 }); | |
271 | |
272 if (cr.isChromeOS) { | |
273 // The 'ResolutionFails' test covers this case for non-CrOS. | |
274 test('ReceiveFailedSetup', function() { | |
275 var destId = '00112233DEADBEEF'; | |
276 var response = { | |
277 printerId: destId, | |
278 capabilities: getCaps(), | |
279 success: false, | |
280 }; | |
281 nativeLayer_.setSetupPrinterResponse(false, response); | |
282 requestSetup(destId, destinationSearch_); | |
283 return nativeLayer_.whenCalled('setupPrinter').then( | |
284 function (actualDestId) { | |
285 // Selection should not change on ChromeOS. | |
286 assertEquals(destId, actualDestId); | |
287 assertEquals(null, destinationStore_.selectedDestination); | |
288 }); | |
289 }); | |
290 } | |
291 | |
292 test('CloudKioskPrinter', function() { | |
293 var printerId = 'cloud-printer-id'; | |
294 | |
295 // Create cloud destination. | |
296 var cloudDest = new print_preview.Destination(printerId, | |
297 print_preview.DestinationType.GOOGLE, | |
298 print_preview.DestinationOrigin.DEVICE, | |
299 "displayName", | |
300 print_preview.DestinationConnectionStatus.ONLINE); | |
301 cloudDest.capabilities = getCaps(); | |
302 | |
303 // Place destination in the local list as happens for Kiosk printers. | |
304 destinationSearch_.localList_.updateDestinations([cloudDest]); | |
305 var dest = destinationSearch_.localList_.getDestinationItem(printerId); | |
306 // Simulate a click. | |
307 dest.onActivate_(); | |
308 | |
309 // Verify that the destination has been selected. | |
310 assertEquals(printerId, destinationStore_.selectedDestination.id); | |
311 }); | |
312 }); | |
313 | |
314 mocha.run(); | |
315 }); | |
OLD | NEW |