Index: chrome/test/data/webui/print_preview.js |
diff --git a/chrome/test/data/webui/print_preview.js b/chrome/test/data/webui/print_preview.js |
index beb1c14fa99de9994995b9de3285b0514197c330..7b83977251abe6df1f8a5be847f78a4467cb1c00 100644 |
--- a/chrome/test/data/webui/print_preview.js |
+++ b/chrome/test/data/webui/print_preview.js |
@@ -2,85 +2,132 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-(function() { |
- function MockHandler() { |
- this.__proto__ = MockHandler.prototype; |
- }; |
+/** |
+ * TestFixture for print preview WebUI testing. |
+ * @extends {testing.Test} |
+ * @constructor |
+ **/ |
+function PrintPreviewWebUITest() {} |
- MockHandler.prototype = { |
- 'getDefaultPrinter': function() { |
- console.log('getDefaultPrinter'); |
- setDefaultPrinter('FooDevice'); |
- }, |
- 'getPrinters': function() { |
- console.log('getPrinters'); |
- setPrinters([ |
- { |
- 'printerName': 'FooName', |
- 'deviceName': 'FooDevice', |
- }, |
- { |
- 'printerName': 'BarName', |
- 'deviceName': 'BarDevice', |
- }, |
- ]); |
- }, |
- 'getPreview': function(settings) { |
- console.log('getPreview(' + settings + ')'); |
- updatePrintPreview(1, 'title', true); |
- }, |
- 'print': function(settings) { |
- console.log('print(' + settings + ')'); |
- }, |
- 'getPrinterCapabilities': function(printer_name) { |
- console.log('getPrinterCapabilities(' + printer_name + ')'); |
- updateWithPrinterCapabilities({ |
- 'disableColorOption': true, |
- 'setColorAsDefault': true, |
- 'disableCopiesOption': true |
- }); |
- }, |
- 'showSystemDialog': function() { |
- console.log('showSystemDialog'); |
- }, |
- 'managePrinters': function() { |
- console.log('managePrinters'); |
- }, |
- 'closePrintPreviewTab': function() { |
- console.log('closePrintPreviewTab'); |
- }, |
- 'hidePreview': function() { |
- console.log('hidePreview'); |
- }, |
- }; |
+PrintPreviewWebUITest.prototype = { |
+ __proto__: testing.Test.prototype, |
- function registerCallbacks() { |
- console.log('registeringCallbacks'); |
- var mock_handler = new MockHandler(); |
- for (func in MockHandler.prototype) { |
- if (typeof(mock_handler[func]) == 'function') |
- registerMessageCallback(func, |
- mock_handler, |
- mock_handler[func]); |
- } |
- }; |
+ /** |
+ * Browse to the sample page, cause print preview & call our PreLoad(). |
+ **/ |
+ browsePrintPreload: 'print_preview_hello_world_test.html', |
- if ('window' in this && 'registerMessageCallback' in window) |
- registerCallbacks(); |
- })(); |
+ /** |
+ * Register a mock handler to ensure expectations are met and print preview |
+ * behaves correctly. |
+ **/ |
+ PreLoad: function() { |
-// Tests. |
-function FLAKY_TestPrinterList() { |
- var printer_list = $('printer-list'); |
- assertTrue(!!printer_list, 'printer_list'); |
- assertTrue(printer_list.options.length >= 2, 'printer-list has at least 2'); |
- expectEquals('FooName', printer_list.options[0].text, '0 text is FooName'); |
- expectEquals('FooDevice', printer_list.options[0].value, |
+ /** |
+ * Create a handler class with empty methods to allow mocking to register |
+ * expectations and for registration of handlers with chrome.send. |
+ **/ |
+ function MockPrintPreviewHandler() {} |
+ |
+ MockPrintPreviewHandler.prototype = { |
+ getDefaultPrinter: function() {}, |
+ getPrinters: function() {}, |
+ getPreview: function(settings) {}, |
+ print: function(settings) {}, |
+ getPrinterCapabilities: function(printerName) {}, |
+ showSystemDialog: function() {}, |
+ managePrinters: function() {}, |
+ closePrintPreviewTab: function() {}, |
+ hidePreview: function() {}, |
+ }; |
+ |
+ // Create the actual mock and register stubs for methods expected to be |
+ // called before our tests run. Specific expectations can be made in the |
+ // tests themselves. |
+ var mockHandler = this.mockHandler = mock(MockPrintPreviewHandler); |
+ mockHandler.stubs().getDefaultPrinter(). |
+ will(callFunction(function() { |
+ setDefaultPrinter('FooDevice'); |
+ })); |
+ mockHandler.stubs().getPrinterCapabilities(NOT_NULL). |
+ will(callFunction(function() { |
+ updateWithPrinterCapabilities({ |
+ disableColorOption: true, |
+ setColorAsDefault: true, |
+ disableCopiesOption: true, |
+ }); |
+ })); |
+ mockHandler.stubs().getPreview(NOT_NULL). |
+ will(callFunction(function() { |
+ updatePrintPreview(1, 'title', true); |
+ })); |
+ |
+ mockHandler.stubs().getPrinters(). |
+ will(callFunction(function() { |
+ setPrinters([{ |
+ printerName: 'FooName', |
+ deviceName: 'FooDevice', |
+ }, { |
+ printerName: 'BarName', |
+ deviceName: 'BarDevice', |
+ }, |
+ ]); |
+ })); |
+ |
+ // Register our mock as a handler of the chrome.send messages. |
+ registerMockMessageCallbacks(mockHandler, MockPrintPreviewHandler); |
+ }, |
+ testGenPreamble: function(testFixture, testName) { |
+ GEN(' if (!HasPDFLib()) {'); |
+ GEN(' LOG(WARNING)'); |
+ GEN(' << "Skipping test ' + testFixture + '.' + testName + '"'); |
+ GEN(' << ": No PDF Lib.";'); |
+ GEN(' SUCCEED();'); |
+ GEN(' return;'); |
+ GEN(' }'); |
+ }, |
+ typedefCppFixture: null, |
+}; |
+ |
+GEN('#include "base/command_line.h"'); |
+GEN('#include "base/path_service.h"'); |
+GEN('#include "base/stringprintf.h"'); |
+GEN('#include "chrome/browser/ui/webui/web_ui_browsertest.h"'); |
+GEN('#include "chrome/common/chrome_paths.h"'); |
+GEN('#include "chrome/common/chrome_switches.h"'); |
+GEN(''); |
+GEN('class PrintPreviewWebUITest'); |
+GEN(' : public WebUIBrowserTest {'); |
+GEN(' protected:'); |
+GEN(' // WebUIBrowserTest override.'); |
+GEN(' virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {'); |
+GEN(' WebUIBrowserTest::SetUpCommandLine(command_line);'); |
+GEN('#if !defined(GOOGLE_CHROME_BUILD) || defined(OS_CHROMEOS) || \\'); |
+GEN(' defined(OS_MACOSX)'); |
+GEN(' // Don\'t enable the flag for chrome builds, which should be on by ' + |
+ 'default.'); |
+GEN(' command_line->AppendSwitch(switches::kEnablePrintPreview);'); |
+GEN('#else'); |
+GEN(' ASSERT_TRUE(switches::IsPrintPreviewEnabled());'); |
+GEN('#endif'); |
+GEN(' }'); |
+GEN(''); |
+GEN(' bool HasPDFLib() const {'); |
+GEN(' FilePath pdf;'); |
+GEN(' return PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf) &&'); |
+GEN(' file_util::PathExists(pdf);'); |
+GEN(' }'); |
+GEN('};'); |
+GEN(''); |
+ |
+TEST_F('PrintPreviewWebUITest', 'FLAKY_TestPrinterList', function() { |
+ var printerList = $('printer-list'); |
+ assertTrue(!!printerList, 'printerList'); |
+ assertTrue(printerList.options.length >= 2, 'printer-list has at least 2'); |
+ expectEquals('FooName', printerList.options[0].text, '0 text is FooName'); |
+ expectEquals('FooDevice', printerList.options[0].value, |
'0 value is FooDevice'); |
- expectEquals('BarName', printer_list.options[1].text, '1 text is BarName'); |
- expectEquals('BarDevice', printer_list.options[1].value, |
+ expectEquals('BarName', printerList.options[1].text, '1 text is BarName'); |
+ expectEquals('BarDevice', printerList.options[1].value, |
'1 value is BarDevice'); |
-} |
- |
-var test_fixture = 'PrintPreviewWebUITest'; |
-var test_add_library = false; |
+}); |