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