[email protected] | 128bec3c | 2011-01-20 22:57:35 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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 "printing/printing_context_win.h" |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 6 | |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 12 | #include "base/bind.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 13 | #include "base/test/task_environment.h" |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 14 | #include "base/win/scoped_handle.h" |
| 15 | #include "base/win/scoped_hdc.h" |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 16 | #include "printing/backend/printing_info_win.h" |
| 17 | #include "printing/backend/win_helper.h" |
Alan Screen | 3fb63bf | 2020-08-19 00:11:23 | [diff] [blame] | 18 | #include "printing/mojom/print.mojom.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 19 | #include "printing/print_settings.h" |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 20 | #include "printing/printing_context_system_dialog_win.h" |
| 21 | #include "printing/printing_test.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 24 | namespace printing { |
| 25 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | // This test is automatically disabled if no printer is available. |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 27 | class PrintingContextTest : public PrintingTest<testing::Test>, |
| 28 | public PrintingContext::Delegate { |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 29 | public: |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 30 | void PrintSettingsCallback(PrintingContext::Result result) { |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 31 | result_ = result; |
| 32 | } |
| 33 | |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 34 | // PrintingContext::Delegate methods. |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 35 | gfx::NativeView GetParentView() override { return nullptr; } |
nick | bc76161 | 2015-04-27 23:30:46 | [diff] [blame] | 36 | std::string GetAppLocale() override { return std::string(); } |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 37 | |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 38 | protected: |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 39 | PrintingContext::Result result() const { return result_; } |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 40 | |
| 41 | private: |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 42 | PrintingContext::Result result_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | }; |
| 44 | |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 45 | namespace { |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 46 | |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 47 | struct FreeHandleTraits { |
| 48 | typedef HANDLE Handle; |
| 49 | static void CloseHandle(HANDLE handle) { GlobalFree(handle); } |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 50 | static bool IsHandleValid(HANDLE handle) { return handle != nullptr; } |
| 51 | static HANDLE NullHandle() { return nullptr; } |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 52 | }; |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 53 | |
| 54 | using ScopedGlobalAlloc = |
| 55 | base::win::GenericScopedHandle<FreeHandleTraits, |
| 56 | base::win::DummyVerifierTraits>; |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 57 | |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 58 | } // namespace |
| 59 | |
| 60 | class MockPrintingContextWin : public PrintingContextSystemDialogWin { |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 61 | public: |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 62 | explicit MockPrintingContextWin(Delegate* delegate) |
| 63 | : PrintingContextSystemDialogWin(delegate) {} |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 64 | |
| 65 | protected: |
| 66 | // This is a fake PrintDlgEx implementation that sets the right fields in |
| 67 | // |lppd| to trigger a bug in older revisions of PrintingContext. |
anujk.sharma | 263c313 | 2014-10-07 07:29:42 | [diff] [blame] | 68 | HRESULT ShowPrintDialog(PRINTDLGEX* lppd) override { |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 69 | // The interesting bits: |
| 70 | // Pretend the user hit print |
| 71 | lppd->dwResultAction = PD_RESULT_PRINT; |
| 72 | |
| 73 | // Pretend the page range is 1-5, but since lppd->Flags does not have |
| 74 | // PD_SELECTION set, this really shouldn't matter. |
| 75 | lppd->nPageRanges = 1; |
| 76 | lppd->lpPageRanges[0].nFromPage = 1; |
| 77 | lppd->lpPageRanges[0].nToPage = 5; |
| 78 | |
| 79 | base::string16 printer_name = PrintingContextTest::GetDefaultPrinter(); |
| 80 | ScopedPrinterHandle printer; |
Lei Zhang | 151b1b9 | 2019-05-24 20:48:30 | [diff] [blame] | 81 | if (!printer.OpenPrinterWithName(printer_name.c_str())) |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 82 | return E_FAIL; |
| 83 | |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 84 | const DEVMODE* dev_mode = nullptr; |
| 85 | lppd->hDC = nullptr; |
| 86 | lppd->hDevMode = nullptr; |
| 87 | lppd->hDevNames = nullptr; |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 88 | |
| 89 | PrinterInfo2 info_2; |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 90 | if (info_2.Init(printer.Get())) |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 91 | dev_mode = info_2.get()->pDevMode; |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 92 | if (!dev_mode) |
| 93 | return E_FAIL; |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 94 | |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 95 | base::win::ScopedCreateDC hdc( |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 96 | CreateDC(L"WINSPOOL", printer_name.c_str(), nullptr, dev_mode)); |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 97 | if (!hdc.Get()) |
| 98 | return E_FAIL; |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 99 | |
| 100 | size_t dev_mode_size = dev_mode->dmSize + dev_mode->dmDriverExtra; |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 101 | ScopedGlobalAlloc dev_mode_mem(GlobalAlloc(GMEM_MOVEABLE, dev_mode_size)); |
| 102 | if (!dev_mode_mem.Get()) |
| 103 | return E_FAIL; |
| 104 | void* dev_mode_ptr = GlobalLock(dev_mode_mem.Get()); |
| 105 | if (!dev_mode_ptr) |
| 106 | return E_FAIL; |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 107 | memcpy(dev_mode_ptr, dev_mode, dev_mode_size); |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 108 | GlobalUnlock(dev_mode_mem.Get()); |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 109 | dev_mode_ptr = nullptr; |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 110 | |
| 111 | size_t driver_size = |
| 112 | 2 + sizeof(wchar_t) * lstrlen(info_2.get()->pDriverName); |
| 113 | size_t printer_size = |
| 114 | 2 + sizeof(wchar_t) * lstrlen(info_2.get()->pPrinterName); |
| 115 | size_t port_size = 2 + sizeof(wchar_t) * lstrlen(info_2.get()->pPortName); |
| 116 | size_t dev_names_size = |
| 117 | sizeof(DEVNAMES) + driver_size + printer_size + port_size; |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 118 | ScopedGlobalAlloc dev_names_mem(GlobalAlloc(GHND, dev_names_size)); |
| 119 | if (!dev_names_mem.Get()) |
| 120 | return E_FAIL; |
| 121 | void* dev_names_ptr = GlobalLock(dev_names_mem.Get()); |
| 122 | if (!dev_names_ptr) |
| 123 | return E_FAIL; |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 124 | DEVNAMES* dev_names = reinterpret_cast<DEVNAMES*>(dev_names_ptr); |
| 125 | dev_names->wDefault = 1; |
| 126 | dev_names->wDriverOffset = sizeof(DEVNAMES) / sizeof(wchar_t); |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 127 | memcpy(reinterpret_cast<uint8_t*>(dev_names_ptr) + dev_names->wDriverOffset, |
| 128 | info_2.get()->pDriverName, driver_size); |
brucedawson | c8e9e0d00 | 2015-12-03 01:54:27 | [diff] [blame] | 129 | dev_names->wDeviceOffset = base::checked_cast<WORD>( |
| 130 | dev_names->wDriverOffset + driver_size / sizeof(wchar_t)); |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 131 | memcpy(reinterpret_cast<uint8_t*>(dev_names_ptr) + dev_names->wDeviceOffset, |
| 132 | info_2.get()->pPrinterName, printer_size); |
brucedawson | c8e9e0d00 | 2015-12-03 01:54:27 | [diff] [blame] | 133 | dev_names->wOutputOffset = base::checked_cast<WORD>( |
| 134 | dev_names->wDeviceOffset + printer_size / sizeof(wchar_t)); |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 135 | memcpy(reinterpret_cast<uint8_t*>(dev_names_ptr) + dev_names->wOutputOffset, |
| 136 | info_2.get()->pPortName, port_size); |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 137 | GlobalUnlock(dev_names_mem.Get()); |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 138 | dev_names_ptr = nullptr; |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 139 | |
thakis | e03b42a | 2015-07-14 18:44:10 | [diff] [blame] | 140 | lppd->hDC = hdc.Take(); |
| 141 | lppd->hDevMode = dev_mode_mem.Take(); |
| 142 | lppd->hDevNames = dev_names_mem.Take(); |
| 143 | return S_OK; |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 144 | } |
| 145 | }; |
| 146 | |
| 147 | TEST_F(PrintingContextTest, PrintAll) { |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 148 | if (IsTestCaseDisabled()) |
| 149 | return; |
| 150 | |
Gabriel Charette | 7f4fc23d | 2019-09-05 18:07:39 | [diff] [blame] | 151 | base::test::SingleThreadTaskEnvironment task_environment; |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 152 | MockPrintingContextWin context(this); |
| 153 | context.AskUserForSettings( |
tzik | ea7713cd | 2018-03-09 09:39:09 | [diff] [blame] | 154 | 123, false, false, |
| 155 | base::BindOnce(&PrintingContextTest::PrintSettingsCallback, |
| 156 | base::Unretained(this))); |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 157 | EXPECT_EQ(PrintingContext::OK, result()); |
Lei Zhang | 0c4f392de | 2018-05-15 23:05:26 | [diff] [blame] | 158 | const PrintSettings& settings = context.settings(); |
thakis | 6ef917b | 2015-12-10 19:29:57 | [diff] [blame] | 159 | EXPECT_EQ(0u, settings.ranges().size()); |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 160 | } |
| 161 | |
Lei Zhang | 0c4f392de | 2018-05-15 23:05:26 | [diff] [blame] | 162 | TEST_F(PrintingContextTest, Color) { |
| 163 | if (IsTestCaseDisabled()) |
| 164 | return; |
| 165 | |
Gabriel Charette | 7f4fc23d | 2019-09-05 18:07:39 | [diff] [blame] | 166 | base::test::SingleThreadTaskEnvironment task_environment; |
Lei Zhang | 0c4f392de | 2018-05-15 23:05:26 | [diff] [blame] | 167 | MockPrintingContextWin context(this); |
| 168 | context.AskUserForSettings( |
| 169 | 123, false, false, |
| 170 | base::BindOnce(&PrintingContextTest::PrintSettingsCallback, |
| 171 | base::Unretained(this))); |
| 172 | EXPECT_EQ(PrintingContext::OK, result()); |
| 173 | const PrintSettings& settings = context.settings(); |
Alan Screen | 3fb63bf | 2020-08-19 00:11:23 | [diff] [blame] | 174 | EXPECT_NE(settings.color(), mojom::ColorModel::kUnknownColorModel); |
Lei Zhang | 0c4f392de | 2018-05-15 23:05:26 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | e055fe2b | 2010-12-01 02:05:39 | [diff] [blame] | 177 | TEST_F(PrintingContextTest, Base) { |
[email protected] | e055fe2b | 2010-12-01 02:05:39 | [diff] [blame] | 178 | if (IsTestCaseDisabled()) |
| 179 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 180 | |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 181 | auto settings = std::make_unique<PrintSettings>(); |
| 182 | settings->set_device_name(GetDefaultPrinter()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 183 | // Initialize it. |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 184 | PrintingContextWin context(this); |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 185 | EXPECT_EQ(PrintingContext::OK, |
| 186 | context.InitWithSettingsForTest(std::move(settings))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 188 | // The print may lie to use and may not support world transformation. |
| 189 | // Verify right now. |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 190 | XFORM random_matrix = {1, 0.1f, 0, 1.5f, 0, 1}; |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 191 | EXPECT_TRUE(SetWorldTransform(context.context(), &random_matrix)); |
| 192 | EXPECT_TRUE(ModifyWorldTransform(context.context(), nullptr, MWT_IDENTITY)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | } |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 194 | |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 195 | } // namespace printing |