OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 /** | 5 /** |
6 * Test fixture for print preview WebUI testing. | 6 * Test fixture for print preview WebUI testing. |
7 * @extends {testing.Test} | 7 * @extends {testing.Test} |
8 * @constructor | 8 * @constructor |
9 **/ | 9 */ |
10 function PrintPreviewWebUITest() {} | 10 function PrintPreviewWebUITest() {} |
11 | 11 |
12 PrintPreviewWebUITest.prototype = { | 12 PrintPreviewWebUITest.prototype = { |
13 __proto__: testing.Test.prototype, | 13 __proto__: testing.Test.prototype, |
14 | 14 |
15 /** | 15 /** |
16 * Browse to the sample page, cause print preview & call PreLoad(). | 16 * Browse to the sample page, cause print preview & call preLoad(). |
17 * @type {string} | 17 * @type {string} |
18 * @override | 18 * @override |
19 **/ | 19 */ |
20 browsePrintPreload: 'print_preview_hello_world_test.html', | 20 browsePrintPreload: 'print_preview_hello_world_test.html', |
21 | 21 |
22 /** | 22 /** |
23 * Register a mock handler to ensure expectations are met and print preview | 23 * Register a mock handler to ensure expectations are met and print preview |
24 * behaves correctly. | 24 * behaves correctly. |
25 * @type {Function} | 25 * @type {Function} |
26 * @override | 26 * @override |
27 **/ | 27 */ |
28 PreLoad: function() { | 28 preLoad: function() { |
29 // TODO(scr) remove this after tests pass consistently. | 29 // TODO(scr) remove this after tests pass consistently. |
30 console.info('PreLoad'); | 30 console.info('preLoad'); |
31 | 31 |
32 /** | 32 /** |
33 * Create a handler class with empty methods to allow mocking to register | 33 * Create a handler class with empty methods to allow mocking to register |
34 * expectations and for registration of handlers with chrome.send. | 34 * expectations and for registration of handlers with chrome.send. |
35 **/ | 35 * @constructor |
| 36 */ |
36 function MockPrintPreviewHandler() {} | 37 function MockPrintPreviewHandler() {} |
37 | 38 |
38 MockPrintPreviewHandler.prototype = { | 39 MockPrintPreviewHandler.prototype = { |
39 getDefaultPrinter: function() {}, | 40 getDefaultPrinter: function() {}, |
40 getPrinters: function() {}, | 41 getPrinters: function() {}, |
41 getPreview: function(settings) {}, | 42 getPreview: function(settings) {}, |
42 print: function(settings) {}, | 43 print: function(settings) {}, |
43 getPrinterCapabilities: function(printerName) {}, | 44 getPrinterCapabilities: function(printerName) {}, |
44 showSystemDialog: function() {}, | 45 showSystemDialog: function() {}, |
45 morePrinters: function() {}, | 46 morePrinters: function() {}, |
(...skipping 16 matching lines...) Expand all Loading... |
62 setDefaultPrinter('FooDevice'); | 63 setDefaultPrinter('FooDevice'); |
63 })); | 64 })); |
64 mockHandler.stubs().getPrinterCapabilities(NOT_NULL). | 65 mockHandler.stubs().getPrinterCapabilities(NOT_NULL). |
65 will(callFunction(function() { | 66 will(callFunction(function() { |
66 updateWithPrinterCapabilities({ | 67 updateWithPrinterCapabilities({ |
67 disableColorOption: true, | 68 disableColorOption: true, |
68 setColorAsDefault: true, | 69 setColorAsDefault: true, |
69 disableCopiesOption: true, | 70 disableCopiesOption: true, |
70 }); | 71 }); |
71 })); | 72 })); |
72 mockHandler.stubs().getPreview(NOT_NULL). | 73 var savedArgs = new SaveMockArguments(); |
73 will(callFunction(function() { | 74 mockHandler.stubs().getPreview(savedArgs.match(NOT_NULL)). |
74 updatePrintPreview('title', true, 1, 1); | 75 will(callFunctionWithSavedArgs(savedArgs, function(options) { |
| 76 updatePrintPreview('title', true, 1, JSON.parse(options).requestID); |
75 })); | 77 })); |
76 | 78 |
77 mockHandler.stubs().getPrinters(). | 79 mockHandler.stubs().getPrinters(). |
78 will(callFunction(function() { | 80 will(callFunction(function() { |
79 setUseCloudPrint(false, ""); | 81 setUseCloudPrint(false, ''); |
80 setPrinters([{ | 82 setPrinters([{ |
81 printerName: 'FooName', | 83 printerName: 'FooName', |
82 deviceName: 'FooDevice', | 84 deviceName: 'FooDevice', |
83 }, { | 85 }, { |
84 printerName: 'BarName', | 86 printerName: 'BarName', |
85 deviceName: 'BarDevice', | 87 deviceName: 'BarDevice', |
86 }, | 88 }, |
87 ]); | 89 ]); |
88 })); | 90 })); |
89 | 91 |
90 // Register mock as a handler of the chrome.send messages. | 92 // Register mock as a handler of the chrome.send messages. |
91 registerMockMessageCallbacks(mockHandler, MockPrintPreviewHandler); | 93 registerMockMessageCallbacks(mockHandler, MockPrintPreviewHandler); |
92 | 94 |
| 95 /** |
| 96 * Create a class to hold global functions to watch for. |
| 97 * @constructor |
| 98 */ |
| 99 function MockGlobals() {} |
| 100 |
| 101 MockGlobals.prototype = { |
| 102 updateWithPrinterCapabilities: function(settingInfo) {}, |
| 103 }; |
| 104 |
| 105 var mockGlobals = this.mockGlobals = mock(MockGlobals); |
| 106 mockGlobals.stubs().updateWithPrinterCapabilities( |
| 107 savedArgs.match(ANYTHING)). |
| 108 will(callGlobalWithSavedArgs( |
| 109 savedArgs, 'updateWithPrinterCapabilities')); |
| 110 |
| 111 // Register globals to mock out for us. |
| 112 registerMockGlobals(mockGlobals, MockGlobals); |
| 113 |
93 // Override checkCompatiblePluginExists to return a value consistent with | 114 // Override checkCompatiblePluginExists to return a value consistent with |
94 // the state being tested and stub out the pdf viewer if it doesn't exist, | 115 // the state being tested and stub out the pdf viewer if it doesn't exist, |
95 // such as on non-official builds. When the plugin exists, use the real | 116 // such as on non-official builds. When the plugin exists, use the real |
96 // thing. | 117 // thing. |
97 var self = this; | 118 var self = this; |
98 window.addEventListener('DOMContentLoaded', function() { | 119 window.addEventListener('DOMContentLoaded', function() { |
99 if (!this.checkCompatiblePluginExists()) { | 120 if (!this.checkCompatiblePluginExists()) { |
100 // TODO(scr) remove this after tests pass consistently. | 121 // TODO(scr) remove this after tests pass consistently. |
101 console.info('no PDF Plugin; providing fake methods.'); | 122 console.info('no PDF Plugin; providing fake methods.'); |
102 this.createPDFPlugin = self.createPDFPlugin; | 123 this.createPDFPlugin = self.createPDFPlugin; |
103 } | 124 } |
104 | 125 |
105 this.checkCompatiblePluginExists = | 126 this.checkCompatiblePluginExists = |
106 self.checkCompatiblePluginExists; | 127 self.checkCompatiblePluginExists; |
107 }); | 128 }); |
108 }, | 129 }, |
109 | 130 |
110 /** | 131 /** |
111 * Generate a real C++ class; don't typedef. | 132 * Generate a real C++ class; don't typedef. |
112 * @type {?string} | 133 * @type {?string} |
113 * @override | 134 * @override |
114 **/ | 135 */ |
115 typedefCppFixture: null, | 136 typedefCppFixture: null, |
116 | 137 |
117 /** | 138 /** |
118 * Create the PDF plugin or reload the existing one. This function replaces | 139 * Create the PDF plugin or reload the existing one. This function replaces |
119 * createPDFPlugin defined in | 140 * createPDFPlugin defined in |
120 * chrome/browser/resources/print_preview/print_preview.js when there is no | 141 * chrome/browser/resources/print_preview/print_preview.js when there is no |
121 * official pdf plugin so that the WebUI logic can be tested. It creates and | 142 * official pdf plugin so that the WebUI logic can be tested. It creates and |
122 * attaches an HTMLDivElement to the |mainview| element with attributes and | 143 * attaches an HTMLDivElement to the |mainview| element with attributes and |
123 * empty methods, which are used by testing and that would be provided by the | 144 * empty methods, which are used by testing and that would be provided by the |
124 * HTMLEmbedElement when the PDF plugin exists. | 145 * HTMLEmbedElement when the PDF plugin exists. |
(...skipping 18 matching lines...) Expand all Loading... |
143 pdfViewer.removePrintButton = fakeFunction; | 164 pdfViewer.removePrintButton = fakeFunction; |
144 pdfViewer.fitToHeight = fakeFunction; | 165 pdfViewer.fitToHeight = fakeFunction; |
145 pdfViewer.grayscale = fakeFunction; | 166 pdfViewer.grayscale = fakeFunction; |
146 $('mainview').appendChild(pdfViewer); | 167 $('mainview').appendChild(pdfViewer); |
147 onPDFLoad(); | 168 onPDFLoad(); |
148 }, | 169 }, |
149 | 170 |
150 /** | 171 /** |
151 * Always return true so tests run on systems without plugin available. | 172 * Always return true so tests run on systems without plugin available. |
152 * @return {boolean} Always true. | 173 * @return {boolean} Always true. |
153 **/ | 174 */ |
154 checkCompatiblePluginExists: function() { | 175 checkCompatiblePluginExists: function() { |
155 return true; | 176 return true; |
156 }, | 177 }, |
157 }; | 178 }; |
158 | 179 |
159 GEN('#include "base/command_line.h"'); | 180 GEN('#include "base/command_line.h"'); |
160 GEN('#include "chrome/browser/ui/webui/web_ui_browsertest.h"'); | 181 GEN('#include "chrome/browser/ui/webui/web_ui_browsertest.h"'); |
161 GEN('#include "chrome/common/chrome_switches.h"'); | 182 GEN('#include "chrome/common/chrome_switches.h"'); |
162 GEN(''); | 183 GEN(''); |
163 GEN('class PrintPreviewWebUITest'); | 184 GEN('class PrintPreviewWebUITest'); |
164 GEN(' : public WebUIBrowserTest {'); | 185 GEN(' : public WebUIBrowserTest {'); |
165 GEN(' protected:'); | 186 GEN(' protected:'); |
166 GEN(' // WebUIBrowserTest override.'); | 187 GEN(' // WebUIBrowserTest override.'); |
167 GEN(' virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {'); | 188 GEN(' virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {'); |
168 GEN(' WebUIBrowserTest::SetUpCommandLine(command_line);'); | 189 GEN(' WebUIBrowserTest::SetUpCommandLine(command_line);'); |
169 GEN(' command_line->AppendSwitch(switches::kEnablePrintPreview);'); | 190 GEN(' command_line->AppendSwitch(switches::kEnablePrintPreview);'); |
170 GEN(' }'); | 191 GEN(' }'); |
171 GEN(''); | 192 GEN(''); |
172 GEN('};'); | 193 GEN('};'); |
173 GEN(''); | 194 GEN(''); |
174 | 195 |
175 /** | 196 /** |
176 * The expected length of the |printer-list| element. | 197 * The expected length of the |printer-list| element. |
177 * @type {number} | 198 * @type {number} |
178 * @const | 199 * @const |
179 **/ | 200 */ |
180 var printerListMinLength = 2; | 201 var printerListMinLength = 2; |
181 | 202 |
182 /** | 203 /** |
183 * The expected index of the "foo" printer returned by the stubbed handler. | 204 * The expected index of the "foo" printer returned by the stubbed handler. |
184 * @type {number} | 205 * @type {number} |
185 * @const | 206 * @const |
186 **/ | 207 */ |
187 var fooIndex = 0; | 208 var fooIndex = 0; |
188 | 209 |
189 /** | 210 /** |
190 * The expected index of the "bar" printer returned by the stubbed handler. | 211 * The expected index of the "bar" printer returned by the stubbed handler. |
191 * @type {number} | 212 * @type {number} |
192 * @const | 213 * @const |
193 **/ | 214 */ |
194 var barIndex = 1; | 215 var barIndex = 1; |
195 | 216 |
196 // Test some basic assumptions about the print preview WebUI. | 217 // Test some basic assumptions about the print preview WebUI. |
197 TEST_F('PrintPreviewWebUITest', 'TestPrinterList', function() { | 218 TEST_F('PrintPreviewWebUITest', 'TestPrinterList', function() { |
198 var printerList = $('printer-list'); | 219 var printerList = $('printer-list'); |
199 assertNotEquals(null, printerList); | 220 assertNotEquals(null, printerList); |
200 assertGE(printerList.options.length, printerListMinLength); | 221 assertGE(printerList.options.length, printerListMinLength); |
201 expectEquals(fooIndex, printerList.selectedIndex); | 222 expectEquals(fooIndex, printerList.selectedIndex); |
202 expectEquals('FooName', printerList.options[fooIndex].text, | 223 expectEquals('FooName', printerList.options[fooIndex].text, |
203 'fooIndex=' + fooIndex); | 224 'fooIndex=' + fooIndex); |
(...skipping 28 matching lines...) Expand all Loading... |
232 expectEquals(localStrings.getString('signIn'), | 253 expectEquals(localStrings.getString('signIn'), |
233 printerList.options[1].text); | 254 printerList.options[1].text); |
234 }); | 255 }); |
235 | 256 |
236 // Test that the printer list is structured correctly after attempting to add | 257 // Test that the printer list is structured correctly after attempting to add |
237 // individual cloud printers until no more can be added. | 258 // individual cloud printers until no more can be added. |
238 TEST_F('PrintPreviewWebUITest', 'FLAKY_TestPrinterListCloud', function() { | 259 TEST_F('PrintPreviewWebUITest', 'FLAKY_TestPrinterListCloud', function() { |
239 var printerList = $('printer-list'); | 260 var printerList = $('printer-list'); |
240 assertNotEquals(null, printerList); | 261 assertNotEquals(null, printerList); |
241 var printer = new Object; | 262 var printer = new Object; |
242 printer['name'] = "FooCloud"; | 263 printer['name'] = 'FooCloud'; |
243 for (var i = 0; i < maxCloudPrinters; i++) { | 264 for (var i = 0; i < maxCloudPrinters; i++) { |
244 printer['id'] = String(i); | 265 printer['id'] = String(i); |
245 addCloudPrinters([printer]); | 266 addCloudPrinters([printer]); |
246 expectEquals(localStrings.getString('cloudPrinters'), | 267 expectEquals(localStrings.getString('cloudPrinters'), |
247 printerList.options[0].text); | 268 printerList.options[0].text); |
248 expectEquals("FooCloud", printerList.options[i + 1].text); | 269 expectEquals('FooCloud', printerList.options[i + 1].text); |
249 expectEquals(String(i), printerList.options[i + 1].value); | 270 expectEquals(String(i), printerList.options[i + 1].value); |
250 } | 271 } |
251 printer['id'] = maxCloudPrinters + 1; | 272 printer['id'] = maxCloudPrinters + 1; |
252 addCloudPrinters([printer]); | 273 addCloudPrinters([printer]); |
253 expectEquals("", printerList.options[maxCloudPrinters + 1].text); | 274 expectEquals('', printerList.options[maxCloudPrinters + 1].text); |
254 expectEquals(localStrings.getString('morePrinters'), | 275 expectEquals(localStrings.getString('morePrinters'), |
255 printerList.options[maxCloudPrinters + 2].text); | 276 printerList.options[maxCloudPrinters + 2].text); |
256 }); | 277 }); |
257 | 278 |
258 /** | 279 /** |
259 * Verify that |section| visibility matches |visible|. | 280 * Verify that |section| visibility matches |visible|. |
260 * @param {HTMLDivElement} section The section to check. | 281 * @param {HTMLDivElement} section The section to check. |
261 * @param {boolean} visible The expected state of visibility. | 282 * @param {boolean} visible The expected state of visibility. |
262 **/ | 283 */ |
263 function checkSectionVisible(section, visible) { | 284 function checkSectionVisible(section, visible) { |
264 assertNotEquals(null, section); | 285 assertNotEquals(null, section); |
265 expectEquals(section.classList.contains('visible'), visible, | 286 expectEquals(section.classList.contains('visible'), visible, |
266 'section=' + section); | 287 'section=' + section); |
267 } | 288 } |
268 | 289 |
269 // Test that disabled settings hide the disabled sections. | 290 // Test that disabled settings hide the disabled sections. |
270 TEST_F('PrintPreviewWebUITest', 'TestSectionsDisabled', function() { | 291 TEST_F('PrintPreviewWebUITest', 'TestSectionsDisabled', function() { |
271 this.mockHandler.expects(once()).getPrinterCapabilities('FooDevice'). | 292 this.mockHandler.expects(once()).getPrinterCapabilities('FooDevice'). |
272 will(callFunction(function() { | 293 will(callFunction(function() { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 disableLandscapeOption: false, | 331 disableLandscapeOption: false, |
311 }); | 332 }); |
312 })); | 333 })); |
313 | 334 |
314 updateControlsWithSelectedPrinterCapabilities(); | 335 updateControlsWithSelectedPrinterCapabilities(); |
315 expectFalse(colorSettings.colorRadioButton.checked); | 336 expectFalse(colorSettings.colorRadioButton.checked); |
316 expectTrue(colorSettings.bwRadioButton.checked); | 337 expectTrue(colorSettings.bwRadioButton.checked); |
317 }); | 338 }); |
318 | 339 |
319 // Test that changing the selected printer updates the preview. | 340 // Test that changing the selected printer updates the preview. |
320 TEST_F('PrintPreviewWebUITest', 'TestPrinterChangeUpdatesPreview', | 341 TEST_F('PrintPreviewWebUITest', 'TestPrinterChangeUpdatesPreview', function() { |
321 function() { | 342 var savedArgs = new SaveMockArguments(); |
322 var matchAnythingSave = new SaveArgumentsMatcher(ANYTHING); | 343 this.mockHandler.expects(once()).getPreview(savedArgs.match(ANYTHING)). |
| 344 will(callFunctionWithSavedArgs(savedArgs, function(options) { |
| 345 updatePrintPreview('title', true, 2, |
| 346 JSON.parse(options).requestID); |
| 347 })); |
323 | 348 |
324 this.mockHandler.expects(once()).getPreview(matchAnythingSave). | 349 this.mockGlobals.expects(once()).updateWithPrinterCapabilities( |
325 will(callFunction(function() { | 350 savedArgs.match(ANYTHING)). |
326 updatePrintPreview('title', true, 2, | 351 will(callGlobalWithSavedArgs( |
327 matchAnythingSave.argument.requestID); | 352 savedArgs, 'updateWithPrinterCapabilities')); |
328 })); | |
329 | 353 |
330 var printerList = $('printer-list'); | 354 var printerList = $('printer-list'); |
331 assertNotEquals(null, printerList, 'printerList'); | 355 assertNotEquals(null, printerList, 'printerList'); |
332 assertGE(printerList.options.length, printerListMinLength); | 356 assertGE(printerList.options.length, printerListMinLength); |
333 expectEquals(fooIndex, printerList.selectedIndex, | 357 expectEquals(fooIndex, printerList.selectedIndex, |
334 'fooIndex=' + fooIndex); | 358 'fooIndex=' + fooIndex); |
335 var oldLastPreviewRequestID = lastPreviewRequestID; | 359 var oldLastPreviewRequestID = lastPreviewRequestID; |
336 ++printerList.selectedIndex; | 360 ++printerList.selectedIndex; |
337 updateControlsWithSelectedPrinterCapabilities(); | 361 updateControlsWithSelectedPrinterCapabilities(); |
338 expectNotEquals(oldLastPreviewRequestID, lastPreviewRequestID); | 362 expectNotEquals(oldLastPreviewRequestID, lastPreviewRequestID); |
339 }); | 363 }); |
340 | 364 |
341 /** | 365 /** |
342 * Test fixture to test case when no PDF plugin exists. | 366 * Test fixture to test case when no PDF plugin exists. |
343 * @extends {PrintPreviewWebUITest} | 367 * @extends {PrintPreviewWebUITest} |
344 * @constructor | 368 * @constructor |
345 **/ | 369 */ |
346 function PrintPreviewNoPDFWebUITest() {} | 370 function PrintPreviewNoPDFWebUITest() {} |
347 | 371 |
348 PrintPreviewNoPDFWebUITest.prototype = { | 372 PrintPreviewNoPDFWebUITest.prototype = { |
349 __proto__: PrintPreviewWebUITest.prototype, | 373 __proto__: PrintPreviewWebUITest.prototype, |
350 | 374 |
351 /** | 375 /** |
352 * Provide a typedef for C++ to correspond to JS subclass. | 376 * Provide a typedef for C++ to correspond to JS subclass. |
353 * @type {?string} | 377 * @type {?string} |
354 * @override | 378 * @override |
355 */ | 379 */ |
(...skipping 11 matching lines...) Expand all Loading... |
367 | 391 |
368 // Test that error message is displayed when plugin doesn't exist. | 392 // Test that error message is displayed when plugin doesn't exist. |
369 TEST_F('PrintPreviewNoPDFWebUITest', 'TestErrorMessage', function() { | 393 TEST_F('PrintPreviewNoPDFWebUITest', 'TestErrorMessage', function() { |
370 var errorButton = $('error-button'); | 394 var errorButton = $('error-button'); |
371 assertNotEquals(null, errorButton); | 395 assertNotEquals(null, errorButton); |
372 expectFalse(errorButton.disabled); | 396 expectFalse(errorButton.disabled); |
373 var errorText = $('error-text'); | 397 var errorText = $('error-text'); |
374 assertNotEquals(null, errorText); | 398 assertNotEquals(null, errorText); |
375 expectFalse(errorText.classList.contains('hidden')); | 399 expectFalse(errorText.classList.contains('hidden')); |
376 }); | 400 }); |
OLD | NEW |