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