Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <ocidl.h> |
| 6 | #include <commdlg.h> |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/bind.h" |
| 11 | #include "base/bind_helpers.h" |
| 12 | #include "base/memory/scoped_ptr.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 13 | #include "printing/backend/printing_info_win.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 14 | #include "printing/printing_test.h" |
| 15 | #include "printing/printing_context.h" |
| 16 | #include "printing/printing_context_win.h" |
| 17 | #include "printing/print_settings.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | // This test is automatically disabled if no printer is available. |
| 21 | class PrintingContextTest : public PrintingTest<testing::Test> { |
| 22 | public: |
| 23 | void PrintSettingsCallback(printing::PrintingContext::Result result) { |
| 24 | result_ = result; |
| 25 | } |
| 26 | |
| 27 | protected: |
| 28 | printing::PrintingContext::Result result() const { return result_; } |
| 29 | |
| 30 | private: |
| 31 | printing::PrintingContext::Result result_; |
| 32 | }; |
| 33 | |
| 34 | // This is a fake PrintDlgEx implementation that sets the right fields in |
| 35 | // |lppd| to trigger a bug in older revisions of PrintingContext. |
| 36 | HRESULT WINAPI PrintDlgExMock(LPPRINTDLGEX lppd) { |
| 37 | // The interesting bits: |
| 38 | // Pretend the user hit print |
| 39 | lppd->dwResultAction = PD_RESULT_PRINT; |
| 40 | |
| 41 | // Pretend the page range is 1-5, but since lppd->Flags does not have |
| 42 | // PD_SELECTION set, this really shouldn't matter. |
| 43 | lppd->nPageRanges = 1; |
| 44 | lppd->lpPageRanges[0].nFromPage = 1; |
| 45 | lppd->lpPageRanges[0].nToPage = 5; |
| 46 | |
| 47 | // Painful paperwork. |
| 48 | std::wstring printer_name = PrintingContextTest::GetDefaultPrinter(); |
| 49 | HANDLE printer; |
| 50 | if (!OpenPrinter(const_cast<wchar_t*>(printer_name.c_str()), &printer, NULL)) |
| 51 | return E_FAIL; |
| 52 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 53 | scoped_ptr<uint8[]> buffer; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 54 | const DEVMODE* dev_mode = NULL; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 55 | HRESULT result = S_OK; |
| 56 | lppd->hDC = NULL; |
| 57 | lppd->hDevMode = NULL; |
| 58 | lppd->hDevNames = NULL; |
| 59 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 60 | printing::PrinterInfo2 info_2; |
| 61 | if (info_2.Init(printer)) { |
| 62 | dev_mode = info_2.get()->pDevMode; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 63 | } |
| 64 | if (!dev_mode) { |
| 65 | result = E_FAIL; |
| 66 | goto Cleanup; |
| 67 | } |
| 68 | |
| 69 | if (!printing::PrintingContextWin::AllocateContext(printer_name, dev_mode, |
| 70 | &lppd->hDC)) { |
| 71 | result = E_FAIL; |
| 72 | goto Cleanup; |
| 73 | } |
| 74 | |
| 75 | size_t dev_mode_size = dev_mode->dmSize + dev_mode->dmDriverExtra; |
| 76 | lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, dev_mode_size); |
| 77 | if (!lppd->hDevMode) { |
| 78 | result = E_FAIL; |
| 79 | goto Cleanup; |
| 80 | } |
| 81 | void* dev_mode_ptr = GlobalLock(lppd->hDevMode); |
| 82 | if (!dev_mode_ptr) { |
| 83 | result = E_FAIL; |
| 84 | goto Cleanup; |
| 85 | } |
| 86 | memcpy(dev_mode_ptr, dev_mode, dev_mode_size); |
| 87 | GlobalUnlock(lppd->hDevMode); |
| 88 | dev_mode_ptr = NULL; |
| 89 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 90 | size_t driver_size = 2 + sizeof(wchar_t) * lstrlen(info_2.get()->pDriverName); |
| 91 | size_t printer_size = 2 + sizeof(wchar_t) * |
| 92 | lstrlen(info_2.get()->pPrinterName); |
| 93 | size_t port_size = 2 + sizeof(wchar_t) * lstrlen(info_2.get()->pPortName); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 94 | size_t dev_names_size = sizeof(DEVNAMES) + driver_size + printer_size + |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 95 | port_size; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 96 | lppd->hDevNames = GlobalAlloc(GHND, dev_names_size); |
| 97 | if (!lppd->hDevNames) { |
| 98 | result = E_FAIL; |
| 99 | goto Cleanup; |
| 100 | } |
| 101 | void* dev_names_ptr = GlobalLock(lppd->hDevNames); |
| 102 | if (!dev_names_ptr) { |
| 103 | result = E_FAIL; |
| 104 | goto Cleanup; |
| 105 | } |
| 106 | DEVNAMES* dev_names = reinterpret_cast<DEVNAMES*>(dev_names_ptr); |
| 107 | dev_names->wDefault = 1; |
| 108 | dev_names->wDriverOffset = sizeof(DEVNAMES); |
| 109 | memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wDriverOffset, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 110 | info_2.get()->pDriverName, driver_size); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 111 | dev_names->wDeviceOffset = dev_names->wDriverOffset + driver_size; |
| 112 | memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wDeviceOffset, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 113 | info_2.get()->pPrinterName, printer_size); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 114 | dev_names->wOutputOffset = dev_names->wDeviceOffset + printer_size; |
| 115 | memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wOutputOffset, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 116 | info_2.get()->pPortName, port_size); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 117 | GlobalUnlock(lppd->hDevNames); |
| 118 | dev_names_ptr = NULL; |
| 119 | |
| 120 | Cleanup: |
| 121 | // Note: This section does proper deallocation/free of DC/global handles. We |
| 122 | // did not use ScopedHGlobal or ScopedHandle because they did not |
| 123 | // perform what we need. Goto's are used based on Windows programming |
| 124 | // idiom, to avoid deeply nested if's, and try-catch-finally is not |
| 125 | // allowed in Chromium. |
| 126 | if (FAILED(result)) { |
| 127 | if (lppd->hDC) { |
| 128 | DeleteDC(lppd->hDC); |
| 129 | } |
| 130 | if (lppd->hDevMode) { |
| 131 | GlobalFree(lppd->hDevMode); |
| 132 | } |
| 133 | if (lppd->hDevNames) { |
| 134 | GlobalFree(lppd->hDevNames); |
| 135 | } |
| 136 | } |
| 137 | ClosePrinter(printer); |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | TEST_F(PrintingContextTest, Base) { |
| 142 | // Sometimes ::GetDefaultPrinter() fails? bug 61509. |
| 143 | if (IsTestCaseDisabled()) |
| 144 | return; |
| 145 | |
| 146 | printing::PrintSettings settings; |
| 147 | settings.set_device_name(GetDefaultPrinter()); |
| 148 | // Initialize it. |
| 149 | scoped_ptr<printing::PrintingContext> context( |
| 150 | printing::PrintingContext::Create(std::string())); |
| 151 | EXPECT_EQ(printing::PrintingContext::OK, context->InitWithSettings(settings)); |
| 152 | |
| 153 | // The print may lie to use and may not support world transformation. |
| 154 | // Verify right now. |
| 155 | XFORM random_matrix = { 1, 0.1f, 0, 1.5f, 0, 1 }; |
| 156 | EXPECT_TRUE(SetWorldTransform(context->context(), &random_matrix)); |
| 157 | EXPECT_TRUE(ModifyWorldTransform(context->context(), NULL, MWT_IDENTITY)); |
| 158 | } |
| 159 | |
| 160 | TEST_F(PrintingContextTest, PrintAll) { |
| 161 | // Sometimes ::GetDefaultPrinter() fails? bug 61509. |
| 162 | if (IsTestCaseDisabled()) |
| 163 | return; |
| 164 | |
| 165 | std::string dummy_locale; |
| 166 | printing::PrintingContextWin context(dummy_locale); |
| 167 | context.SetPrintDialog(&PrintDlgExMock); |
| 168 | context.AskUserForSettings( |
| 169 | NULL, 123, false, base::Bind(&PrintingContextTest::PrintSettingsCallback, |
| 170 | base::Unretained(this))); |
| 171 | EXPECT_EQ(printing::PrintingContext::OK, result()); |
| 172 | printing::PrintSettings settings = context.settings(); |
| 173 | EXPECT_EQ(settings.ranges.size(), 0); |
| 174 | } |