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

Side by Side Diff: chrome/test/data/webui/print_preview/native_layer_stub.js

Issue 2931843003: Print Preview: Change getPrinterCapabilities to cr.sendWithPromise (Closed)
Patch Set: Cleanup Created 3 years, 6 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
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 cr.define('print_preview', function() { 5 cr.define('print_preview', function() {
6 /** 6 /**
7 * Test version of the native layer. 7 * Test version of the native layer.
8 * @constructor 8 * @constructor
9 * @extends {TestBrowserProxy} 9 * @extends {TestBrowserProxy}
10 */ 10 */
11 function NativeLayerStub() { 11 function NativeLayerStub() {
12 TestBrowserProxy.call(this, [ 12 TestBrowserProxy.call(this, [
13 'getInitialSettings', 13 'getInitialSettings',
14 'getPrinters', 14 'getPrinters',
15 'getExtensionPrinters', 15 'getExtensionPrinters',
16 'getPrivetPrinters', 16 'getPrivetPrinters',
17 'getPrinterCapabilities',
17 'setupPrinter' 18 'setupPrinter'
18 ]); 19 ]);
19 20
20 /** 21 /**
21 * @private {!cr.EventTarget} The event target used for dispatching and 22 * @private {!cr.EventTarget} The event target used for dispatching and
22 * receiving events. 23 * receiving events.
23 */ 24 */
24 this.eventTarget_ = new cr.EventTarget(); 25 this.eventTarget_ = new cr.EventTarget();
25 26
26 /** @private {boolean} Whether the native layer has sent a print message. */ 27 /** @private {boolean} Whether the native layer has sent a print message. */
(...skipping 12 matching lines...) Expand all
39 this.initialSettings_ = null; 40 this.initialSettings_ = null;
40 41
41 /** 42 /**
42 * 43 *
43 * @private {!Array<!print_preview.LocalDestinationInfo>} Local destination 44 * @private {!Array<!print_preview.LocalDestinationInfo>} Local destination
44 * list to be used for the response to |getPrinters|. 45 * list to be used for the response to |getPrinters|.
45 */ 46 */
46 this.localDestinationInfos_ = []; 47 this.localDestinationInfos_ = [];
47 48
48 /** 49 /**
50 * @private {!Map<string,
51 * {response: !print_preview.PrinterCapabilitiesResponse,
52 * reject: boolean}>}
53 * A map from destination IDs to the responses to be sent when
54 * |getPrinterCapabilities| is called for the ID.
55 */
56 this.localDestinationCapabilities_ = new Map();
57
58 /**
49 * @private {!print_preview.PrinterSetupResponse} The response to be sent 59 * @private {!print_preview.PrinterSetupResponse} The response to be sent
50 * on a |setupPrinter| call. 60 * on a |setupPrinter| call.
51 */ 61 */
52 this.setupPrinterResponse_ = null; 62 this.setupPrinterResponse_ = null;
53 63
54 /** 64 /**
55 * @private {boolean} Whether the printer setup request should be rejected. 65 * @private {boolean} Whether the printer setup request should be rejected.
56 */ 66 */
57 this.shouldRejectPrinterSetup_ = false; 67 this.shouldRejectPrinterSetup_ = false;
58
59 /**
60 * @private {string} The destination id to watch for counting calls to
61 * |getLocalDestinationCapabilities|.
62 */
63 this.destinationToWatch_ = '';
64
65 /**
66 * @private {number} The number of calls to
67 * |getLocalDestinationCapabilities| with id = |destinationToWatch_|.
68 */
69 this.getLocalDestinationCapabilitiesCallCount_ = 0;
70 } 68 }
71 69
72 NativeLayerStub.prototype = { 70 NativeLayerStub.prototype = {
73 __proto__: TestBrowserProxy.prototype, 71 __proto__: TestBrowserProxy.prototype,
74 72
75 /** @override */ 73 /** @override */
76 getInitialSettings: function() { 74 getInitialSettings: function() {
77 this.methodCalled('getInitialSettings'); 75 this.methodCalled('getInitialSettings');
78 return Promise.resolve(this.initialSettings_); 76 return Promise.resolve(this.initialSettings_);
79 }, 77 },
(...skipping 10 matching lines...) Expand all
90 return Promise.resolve(true); 88 return Promise.resolve(true);
91 }, 89 },
92 90
93 /** @override */ 91 /** @override */
94 getPrivetPrinters: function() { 92 getPrivetPrinters: function() {
95 this.methodCalled('getPrivetPrinters'); 93 this.methodCalled('getPrivetPrinters');
96 return Promise.resolve(true); 94 return Promise.resolve(true);
97 }, 95 },
98 96
99 /** @override */ 97 /** @override */
98 getPrinterCapabilities: function(printerId) {
99 this.methodCalled('getPrinterCapabilities', printerId);
100 var responseAndReject = this.localDestinationCapabilities_.get(printerId);
101 if (responseAndReject && !responseAndReject.reject) {
102 return Promise.resolve(responseAndReject.response);
103 } else {
104 return Promise.reject(printerId);
105 }
106 },
107
108 /** @override */
100 setupPrinter: function(printerId) { 109 setupPrinter: function(printerId) {
101 this.methodCalled('setupPrinter', printerId); 110 this.methodCalled('setupPrinter', printerId);
102 return this.shouldRejectPrinterSetup_ ? 111 return this.shouldRejectPrinterSetup_ ?
103 Promise.reject(this.setupPrinterResponse_) : 112 Promise.reject(this.setupPrinterResponse_) :
104 Promise.resolve(this.setupPrinterResponse_); 113 Promise.resolve(this.setupPrinterResponse_);
105 }, 114 },
106 115
107 /** Stubs for |print_preview.NativeLayer| methods that call C++ handlers. */ 116 /** Stubs for |print_preview.NativeLayer| methods that call C++ handlers. */
108 previewReadyForTest: function() {}, 117 previewReadyForTest: function() {},
109 startGetLocalDestinationCapabilities: function(destinationId) { 118
110 if (destinationId == this.destinationToWatch_)
111 this.getLocalDestinationCapabilitiesCallCount_++;
112 },
113 startGetPreview: function(destination, printTicketStore, documentInfo, 119 startGetPreview: function(destination, printTicketStore, documentInfo,
114 generateDraft, requestId) { 120 generateDraft, requestId) {
115 this.generateDraft_ = generateDraft; 121 this.generateDraft_ = generateDraft;
116 }, 122 },
117 startPrint: function () { this.printStarted_ = true; }, 123 startPrint: function () { this.printStarted_ = true; },
118 startHideDialog: function () {}, 124 startHideDialog: function () {},
119 125
120 /** @return {!cr.EventTarget} The native layer event target. */ 126 /** @return {!cr.EventTarget} The native layer event target. */
121 getEventTarget: function() { return this.eventTarget_; }, 127 getEventTarget: function() { return this.eventTarget_; },
122 128
123 /** @param {!cr.EventTarget} eventTarget The event target to use. */ 129 /** @param {!cr.EventTarget} eventTarget The event target to use. */
124 setEventTarget: function(eventTarget) { 130 setEventTarget: function(eventTarget) {
125 this.eventTarget_ = eventTarget; 131 this.eventTarget_ = eventTarget;
126 }, 132 },
127 133
128 /**
129 * @return {boolean} Whether capabilities have been requested exactly once
130 * for |destinationToWatch_|.
131 */
132 didGetCapabilitiesOnce: function(destinationId) {
133 return (destinationId == this.destinationToWatch_ &&
134 this.getLocalDestinationCapabilitiesCallCount_ == 1);
135 },
136
137 /** @return {boolean} Whether a new draft was requested for preview. */ 134 /** @return {boolean} Whether a new draft was requested for preview. */
138 generateDraft: function() { return this.generateDraft_; }, 135 generateDraft: function() { return this.generateDraft_; },
139 136
140 /** @return {boolean} Whether a print request has been issued. */ 137 /** @return {boolean} Whether a print request has been issued. */
141 isPrintStarted: function() { return this.printStarted_; }, 138 isPrintStarted: function() { return this.printStarted_; },
142 139
143 /** 140 /**
144 * @param {string} destinationId The destination ID to watch for
145 * |getLocalDestinationCapabilities| calls.
146 * Resets |getLocalDestinationCapabilitiesCallCount_|.
147 */
148 setDestinationToWatch: function(destinationId) {
149 this.destinationToWatch_ = destinationId;
150 this.getLocalDestinationCapabilitiesCallCount_ = 0;
151 },
152
153 /**
154 * @param {!print_preview.NativeInitialSettings} settings The settings 141 * @param {!print_preview.NativeInitialSettings} settings The settings
155 * to return as a response to |getInitialSettings|. 142 * to return as a response to |getInitialSettings|.
156 */ 143 */
157 setInitialSettings: function(settings) { 144 setInitialSettings: function(settings) {
158 this.initialSettings_ = settings; 145 this.initialSettings_ = settings;
159 }, 146 },
160 147
161 /** 148 /**
162 * @param {!Array<!print_preview.LocalDestinationInfo>} localDestinations 149 * @param {!Array<!print_preview.LocalDestinationInfo>} localDestinations
163 * The local destinations to return as a response to |getPrinters|. 150 * The local destinations to return as a response to |getPrinters|.
164 */ 151 */
165 setLocalDestinations: function(localDestinations) { 152 setLocalDestinations: function(localDestinations) {
166 this.localDestinationInfos_ = localDestinations; 153 this.localDestinationInfos_ = localDestinations;
167 }, 154 },
168 155
169 /** 156 /**
157 * @param {!print_preview.PrinterCapabilitiesResponse>} localDestinations
158 * The local destinations to return as a response to
159 * |getPrinters(printerId)|.
160 * @param {boolean | undefined} reject Whether to reject the callback for
dpapad 2017/06/09 17:59:11 The proper way to define optional parameters is as
rbpotter 2017/06/14 03:55:08 Done.
161 * this destination. Defaults to false (will resolve callback) if not
162 * provided.
163 */
164 setLocalDestinationCapabilities: function(capabilities, reject) {
165 reject = reject || false;
166 this.localDestinationCapabilities_.set(capabilities.printerId,
167 {"response": capabilities, "reject": reject});
dpapad 2017/06/09 17:59:11 Can we directly pass the Promise to be eventually
rbpotter 2017/06/14 03:55:08 Done.
168 },
169
170 /**
170 * @param {boolean} reject Whether printSetup requests should be rejected. 171 * @param {boolean} reject Whether printSetup requests should be rejected.
171 * @param {!print_preview.PrinterSetupResponse} The response to send when 172 * @param {!print_preview.PrinterSetupResponse} The response to send when
172 * |setupPrinter| is called. 173 * |setupPrinter| is called.
173 */ 174 */
174 setSetupPrinterResponse: function(reject, response) { 175 setSetupPrinterResponse: function(reject, response) {
175 this.shouldRejectPrinterSetup_ = reject; 176 this.shouldRejectPrinterSetup_ = reject;
176 this.setupPrinterResponse_ = response; 177 this.setupPrinterResponse_ = response;
177 }, 178 },
178 }; 179 };
179 180
180 return { 181 return {
181 NativeLayerStub: NativeLayerStub, 182 NativeLayerStub: NativeLayerStub,
182 }; 183 };
183 }); 184 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698