Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: chrome/test/data/webui/print_preview_destination_search_test.js

Issue 2881213003: Print Preview: Use cr.sendWithPromise for getInitialSettings (Closed)
Patch Set: Revert extra change from rebase Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/test/data/webui/print_preview.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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);
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]);
198 254
199 var resolver = mockSetupCall(destId, nativeLayer_); 255 // after setup succeeds and event arrives, the destination should
200 256 // be selected.
201 requestSetup(destId, destinationSearch_); 257 assertNotEquals(null, destinationStore_.selectedDestination);
202 resolveSetup(resolver, destId, true, getCaps()); 258 assertEquals(destId, destinationStore_.selectedDestination.id);
203 259 });
204 // wait for event propogation to complete. 260 } else { //!cr.isChromeOS
205 return waiter.then(function() { 261 var resolver = mockSetupCall(destId, nativeLayer_);
206 // after setup succeeds, the destination should be selected. 262 requestSetup(destId, destinationSearch_);
207 assertNotEquals(null, destinationStore_.selectedDestination); 263 resolver.resolve(response);
208 assertEquals(destId, destinationStore_.selectedDestination.id); 264 return waiter.then(function() {
209 }); 265 // after setup succeeds, the destination should be selected.
266 assertNotEquals(null, destinationStore_.selectedDestination);
267 assertEquals(destId, destinationStore_.selectedDestination.id);
268 });
269 }
210 }); 270 });
211 271
212 if (cr.isChromeOS) { 272 if (cr.isChromeOS) {
213 // The 'ResolutionFails' test covers this case for non-CrOS. 273 // The 'ResolutionFails' test covers this case for non-CrOS.
214 test('ReceiveFailedSetup', function() { 274 test('ReceiveFailedSetup', function() {
215 var destId = '00112233DEADBEEF'; 275 var destId = '00112233DEADBEEF';
216 276 var response = {
217 var resolver = mockSetupCall(destId, nativeLayer_); 277 printerId: destId,
278 capabilities: getCaps(),
279 success: false,
280 };
281 nativeLayer_.setSetupPrinterResponse(false, response);
218 requestSetup(destId, destinationSearch_); 282 requestSetup(destId, destinationSearch_);
219 283 return nativeLayer_.whenCalled('setupPrinter').then(
220 // Force resolution to fail. 284 function (actualDestId) {
221 resolveSetup(resolver, destId, false, null); 285 // Selection should not change on ChromeOS.
222 286 assertEquals(destId, actualDestId);
223 // Selection should not change on ChromeOS. 287 assertEquals(null, destinationStore_.selectedDestination);
224 assertEquals(null, destinationStore_.selectedDestination); 288 });
225 }); 289 });
226 } 290 }
227 291
228 test('CloudKioskPrinter', function() { 292 test('CloudKioskPrinter', function() {
229 var printerId = 'cloud-printer-id'; 293 var printerId = 'cloud-printer-id';
230 294
231 // Create cloud destination. 295 // Create cloud destination.
232 var cloudDest = new print_preview.Destination(printerId, 296 var cloudDest = new print_preview.Destination(printerId,
233 print_preview.DestinationType.GOOGLE, 297 print_preview.DestinationType.GOOGLE,
234 print_preview.DestinationOrigin.DEVICE, 298 print_preview.DestinationOrigin.DEVICE,
235 "displayName", 299 "displayName",
236 print_preview.DestinationConnectionStatus.ONLINE); 300 print_preview.DestinationConnectionStatus.ONLINE);
237 cloudDest.capabilities = getCaps(); 301 cloudDest.capabilities = getCaps();
238 302
239 // Place destination in the local list as happens for Kiosk printers. 303 // Place destination in the local list as happens for Kiosk printers.
240 destinationSearch_.localList_.updateDestinations([cloudDest]); 304 destinationSearch_.localList_.updateDestinations([cloudDest]);
241 var dest = destinationSearch_.localList_.getDestinationItem(printerId); 305 var dest = destinationSearch_.localList_.getDestinationItem(printerId);
242 // Simulate a click. 306 // Simulate a click.
243 dest.onActivate_(); 307 dest.onActivate_();
244 308
245 // Verify that the destination has been selected. 309 // Verify that the destination has been selected.
246 assertEquals(printerId, destinationStore_.selectedDestination.id); 310 assertEquals(printerId, destinationStore_.selectedDestination.id);
247 }); 311 });
248 }); 312 });
249 313
250 mocha.run(); 314 mocha.run();
251 }); 315 });
OLDNEW
« no previous file with comments | « chrome/test/data/webui/print_preview.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698