license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame^] | 5 | #include <ocidl.h> |
| 6 | #include <commdlg.h> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame^] | 8 | #include <string> |
| 9 | |
| 10 | #include "base/scoped_ptr.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 11 | #include "printing/printing_test.h" |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame^] | 12 | #include "printing/printing_context.h" |
| 13 | #include "printing/printing_context_win.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 14 | #include "printing/print_settings.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | |
| 17 | // This test is automatically disabled if no printer is available. |
| 18 | class PrintingContextTest : public PrintingTest<testing::Test> { |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 19 | public: |
| 20 | void PrintSettingsCallback(printing::PrintingContext::Result result) { |
| 21 | result_ = result; |
| 22 | } |
| 23 | |
| 24 | protected: |
| 25 | printing::PrintingContext::Result result() const { return result_; } |
| 26 | |
| 27 | private: |
| 28 | printing::PrintingContext::Result result_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | }; |
| 30 | |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 31 | // This is a fake PrintDlgEx implementation that sets the right fields in |
| 32 | // |lppd| to trigger a bug in older revisions of PrintingContext. |
| 33 | HRESULT WINAPI PrintDlgExMock(LPPRINTDLGEX lppd) { |
| 34 | // The interesting bits: |
| 35 | // Pretend the user hit print |
| 36 | lppd->dwResultAction = PD_RESULT_PRINT; |
| 37 | |
| 38 | // Pretend the page range is 1-5, but since lppd->Flags does not have |
| 39 | // PD_SELECTION set, this really shouldn't matter. |
| 40 | lppd->nPageRanges = 1; |
| 41 | lppd->lpPageRanges = new PRINTPAGERANGE[1]; |
| 42 | lppd->lpPageRanges[0].nFromPage = 1; |
| 43 | lppd->lpPageRanges[0].nToPage = 5; |
| 44 | |
| 45 | // Painful paperwork. |
| 46 | std::wstring printer_name = PrintingContextTest::GetDefaultPrinter(); |
| 47 | HANDLE printer; |
| 48 | if (!OpenPrinter(const_cast<wchar_t*>(printer_name.c_str()), &printer, NULL)) |
| 49 | return E_FAIL; |
| 50 | |
| 51 | scoped_array<uint8> buffer; |
| 52 | DEVMODE* dev_mode = NULL; |
| 53 | PRINTER_INFO_2* info_2 = NULL; |
| 54 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame^] | 55 | printing::PrintingContextWin::GetPrinterHelper(printer, 2, &buffer); |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 56 | if (buffer.get()) { |
| 57 | info_2 = reinterpret_cast<PRINTER_INFO_2*>(buffer.get()); |
| 58 | if (info_2->pDevMode != NULL) |
| 59 | dev_mode = info_2->pDevMode; |
| 60 | } |
| 61 | if (!dev_mode) |
| 62 | return E_FAIL; |
| 63 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame^] | 64 | if (!printing::PrintingContextWin::AllocateContext(printer_name, dev_mode, |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 65 | &lppd->hDC)) { |
| 66 | return E_FAIL; |
| 67 | } |
| 68 | |
| 69 | size_t dev_mode_size = dev_mode->dmSize + dev_mode->dmDriverExtra; |
| 70 | lppd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, dev_mode_size); |
| 71 | if (!lppd->hDevMode) |
| 72 | return E_FAIL; |
| 73 | void* dev_mode_ptr = GlobalLock(lppd->hDevMode); |
| 74 | if (!dev_mode_ptr) |
| 75 | return E_FAIL; |
| 76 | memcpy(dev_mode_ptr, dev_mode, dev_mode_size); |
| 77 | GlobalUnlock(lppd->hDevMode); |
| 78 | |
| 79 | size_t driver_size = 2 + sizeof(wchar_t) * lstrlen(info_2->pDriverName); |
| 80 | size_t printer_size = 2 + sizeof(wchar_t) * lstrlen(info_2->pPrinterName); |
| 81 | size_t port_size = 2 + sizeof(wchar_t) * lstrlen(info_2->pPortName); |
| 82 | size_t dev_names_size = sizeof(DEVNAMES) + driver_size + printer_size + |
| 83 | port_size; |
| 84 | lppd->hDevNames = GlobalAlloc(GHND, dev_names_size); |
| 85 | if (!lppd->hDevNames) |
| 86 | return E_FAIL; |
| 87 | void* dev_names_ptr = GlobalLock(lppd->hDevNames); |
| 88 | if (!dev_names_ptr) |
| 89 | return E_FAIL; |
| 90 | DEVNAMES* dev_names = reinterpret_cast<DEVNAMES*>(dev_names_ptr); |
| 91 | dev_names->wDefault = 1; |
| 92 | dev_names->wDriverOffset = sizeof(DEVNAMES); |
| 93 | memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wDriverOffset, |
| 94 | info_2->pDriverName, driver_size); |
| 95 | dev_names->wDeviceOffset = dev_names->wDriverOffset + driver_size; |
| 96 | memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wDeviceOffset, |
| 97 | info_2->pPrinterName, printer_size); |
| 98 | dev_names->wOutputOffset = dev_names->wDeviceOffset + printer_size; |
| 99 | memcpy(reinterpret_cast<uint8*>(dev_names_ptr) + dev_names->wOutputOffset, |
| 100 | info_2->pPortName, port_size); |
| 101 | GlobalUnlock(lppd->hDevNames); |
| 102 | |
| 103 | return S_OK; |
| 104 | } |
| 105 | |
[email protected] | 8390e3d | 2009-06-21 23:22:57 | [diff] [blame] | 106 | TEST_F(PrintingContextTest, Base) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | printing::PrintSettings settings; |
| 108 | |
| 109 | settings.set_device_name(GetDefaultPrinter()); |
| 110 | // Initialize it. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame^] | 111 | scoped_ptr<printing::PrintingContext> context( |
| 112 | printing::PrintingContext::Create()); |
| 113 | EXPECT_EQ(printing::PrintingContext::OK, context->InitWithSettings(settings)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | // The print may lie to use and may not support world transformation. |
| 116 | // Verify right now. |
| 117 | XFORM random_matrix = { 1, 0.1f, 0, 1.5f, 0, 1 }; |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame^] | 118 | EXPECT_TRUE(SetWorldTransform(context->context(), &random_matrix)); |
| 119 | EXPECT_TRUE(ModifyWorldTransform(context->context(), NULL, MWT_IDENTITY)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | } |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 121 | |
| 122 | TEST_F(PrintingContextTest, PrintAll) { |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame^] | 123 | printing::PrintingContextWin context; |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 124 | context.SetPrintDialog(&PrintDlgExMock); |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 125 | context.AskUserForSettings( |
| 126 | NULL, |
| 127 | 123, |
| 128 | false, |
| 129 | NewCallback(static_cast<PrintingContextTest*>(this), |
| 130 | &PrintingContextTest::PrintSettingsCallback)); |
| 131 | ASSERT_EQ(printing::PrintingContext::OK, result()); |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 132 | printing::PrintSettings settings = context.settings(); |
| 133 | EXPECT_EQ(settings.ranges.size(), 0); |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 134 | } |