[email protected] | 0064b39 | 2012-06-20 01:57:38 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 5 | #include "printing/emf_win.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | // For quick access. |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 8 | #include <stdint.h> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include <wingdi.h> |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 10 | #include <winspool.h> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 12 | #include <memory> |
[email protected] | 83deae2 | 2011-10-07 05:56:32 | [diff] [blame] | 13 | #include <string> |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 14 | #include <utility> |
[email protected] | 83deae2 | 2011-10-07 05:56:32 | [diff] [blame] | 15 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 17 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 18 | #include "base/files/scoped_temp_dir.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "base/path_service.h" |
[email protected] | 0142bd4f | 2010-12-31 01:02:47 | [diff] [blame] | 20 | #include "base/win/scoped_hdc.h" |
Morten Stenshorne | eebf2db | 2020-05-25 09:00:05 | [diff] [blame] | 21 | #include "printing/mojom/print.mojom.h" |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 22 | #include "printing/printing_context.h" |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 23 | #include "printing/printing_context_win.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
tfarina | 655f81d | 2014-12-23 02:38:50 | [diff] [blame] | 25 | #include "ui/gfx/geometry/point.h" |
tfarina | ebe974f0 | 2015-01-03 04:25:32 | [diff] [blame] | 26 | #include "ui/gfx/geometry/size.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 28 | namespace printing { |
| 29 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | // This test is automatically disabled if no printer named "UnitTest Printer" is |
| 33 | // available. |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 34 | class EmfPrintingTest : public testing::Test, public PrintingContext::Delegate { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | public: |
| 36 | typedef testing::Test Parent; |
| 37 | static bool IsTestCaseDisabled() { |
| 38 | // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700. |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 39 | HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", nullptr, nullptr); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | if (!hdc) |
| 41 | return true; |
| 42 | DeleteDC(hdc); |
| 43 | return false; |
| 44 | } |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 45 | |
| 46 | // PrintingContext::Delegate methods. |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 47 | gfx::NativeView GetParentView() override { return nullptr; } |
nick | bc76161 | 2015-04-27 23:30:46 | [diff] [blame] | 48 | std::string GetAppLocale() override { return std::string(); } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | }; |
| 50 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 51 | const uint32_t EMF_HEADER_SIZE = 128; |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 52 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | } // namespace |
| 54 | |
| 55 | TEST(EmfTest, DC) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | // Simplest use case. |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 57 | std::vector<char> data; |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 58 | { |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 59 | Emf emf; |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 60 | EXPECT_TRUE(emf.Init()); |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 61 | EXPECT_TRUE(emf.context()); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 62 | // An empty EMF is invalid, so we put at least a rectangle in it. |
| 63 | ::Rectangle(emf.context(), 10, 10, 190, 190); |
| 64 | EXPECT_TRUE(emf.FinishDocument()); |
Lei Zhang | b4ef0d5d | 2020-03-27 23:18:29 | [diff] [blame] | 65 | uint32_t size = emf.GetDataSize(); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 66 | EXPECT_GT(size, EMF_HEADER_SIZE); |
[email protected] | 96fddd8 | 2011-03-24 23:37:45 | [diff] [blame] | 67 | EXPECT_TRUE(emf.GetDataAsVector(&data)); |
Lei Zhang | b4ef0d5d | 2020-03-27 23:18:29 | [diff] [blame] | 68 | ASSERT_EQ(data.size(), size); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 69 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | |
| 71 | // Playback the data. |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 72 | Emf emf; |
Lei Zhang | b4ef0d5d | 2020-03-27 23:18:29 | [diff] [blame] | 73 | // TODO(thestig): Make |data| uint8_t and avoid the base::as_bytes() call. |
| 74 | EXPECT_TRUE(emf.InitFromData(base::as_bytes(base::make_span(data)))); |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 75 | HDC hdc = CreateCompatibleDC(nullptr); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 76 | EXPECT_TRUE(hdc); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | RECT output_rect = {0, 0, 10, 10}; |
| 78 | EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 79 | EXPECT_TRUE(DeleteDC(hdc)); |
| 80 | } |
| 81 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. |
| 83 | TEST_F(EmfPrintingTest, Enumerate) { |
| 84 | if (IsTestCaseDisabled()) |
| 85 | return; |
| 86 | |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 87 | auto settings = std::make_unique<PrintSettings>(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | |
| 89 | // My test case is a HP Color LaserJet 4550 PCL. |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 90 | settings->set_device_name(L"UnitTest Printer"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | |
| 92 | // Initialize it. |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 93 | PrintingContextWin context(this); |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 94 | EXPECT_EQ(PrintingContext::OK, |
| 95 | context.InitWithSettingsForTest(std::move(settings))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 97 | base::FilePath emf_file; |
Avi Drissman | ea15ea0 | 2018-05-07 18:55:12 | [diff] [blame] | 98 | EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &emf_file)); |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 99 | emf_file = emf_file.Append(FILE_PATH_LITERAL("printing")) |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 100 | .Append(FILE_PATH_LITERAL("test")) |
| 101 | .Append(FILE_PATH_LITERAL("data")) |
Daniel Hosseinian | 347944d | 2020-04-01 02:18:37 | [diff] [blame] | 102 | .Append(FILE_PATH_LITERAL("emf")) |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 103 | .Append(FILE_PATH_LITERAL("test4.emf")); |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 104 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | // Load any EMF with an image. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | std::string emf_data; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 107 | base::ReadFileToString(emf_file, &emf_data); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | ASSERT_TRUE(emf_data.size()); |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 109 | |
| 110 | Emf emf; |
Lei Zhang | b4ef0d5d | 2020-03-27 23:18:29 | [diff] [blame] | 111 | EXPECT_TRUE(emf.InitFromData(base::as_bytes(base::make_span(emf_data)))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | |
| 113 | // This will print to file. The reason is that when running inside a |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 114 | // unit_test, PrintingContext automatically dumps its files to the |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | // current directory. |
| 116 | // TODO(maruel): Clean the .PRN file generated in current directory. |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 117 | context.NewDocument(L"EmfTest.Enumerate"); |
| 118 | context.NewPage(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | // Process one at a time. |
[email protected] | 5914992 | 2014-07-04 22:08:04 | [diff] [blame] | 120 | RECT page_bounds = emf.GetPageBounds(1).ToRECT(); |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 121 | Emf::Enumerator emf_enum(emf, context.context(), &page_bounds); |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 122 | for (Emf::Enumerator::const_iterator itr = emf_enum.begin(); |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 123 | itr != emf_enum.end(); ++itr) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | // To help debugging. |
| 125 | ptrdiff_t index = itr - emf_enum.begin(); |
| 126 | // If you get this assert, you need to lookup iType in wingdi.h. It starts |
| 127 | // with EMR_HEADER. |
| 128 | EMR_HEADER; |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 129 | EXPECT_TRUE(itr->SafePlayback(&emf_enum.context_)) |
| 130 | << " index: " << index << " type: " << itr->record()->iType; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | } |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 132 | context.PageDone(); |
| 133 | context.DocumentDone(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | } |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 135 | |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 136 | // Disabled if no "UnitTest printer" exists. |
| 137 | TEST_F(EmfPrintingTest, PageBreak) { |
[email protected] | 83deae2 | 2011-10-07 05:56:32 | [diff] [blame] | 138 | base::win::ScopedCreateDC dc( |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 139 | CreateDC(L"WINSPOOL", L"UnitTest Printer", nullptr, nullptr)); |
[email protected] | 6f0db2b | 2012-02-14 03:56:18 | [diff] [blame] | 140 | if (!dc.Get()) |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 141 | return; |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 142 | std::vector<char> data; |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 143 | { |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 144 | Emf emf; |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 145 | EXPECT_TRUE(emf.Init()); |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 146 | EXPECT_TRUE(emf.context()); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 147 | int pages = 3; |
| 148 | while (pages) { |
Morten Stenshorne | eebf2db | 2020-05-25 09:00:05 | [diff] [blame] | 149 | emf.StartPage(gfx::Size(), gfx::Rect(), 1, |
| 150 | mojom::PageOrientation::kUpright); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 151 | ::Rectangle(emf.context(), 10, 10, 190, 190); |
| 152 | EXPECT_TRUE(emf.FinishPage()); |
| 153 | --pages; |
| 154 | } |
[email protected] | 830cf74 | 2011-04-01 16:06:25 | [diff] [blame] | 155 | EXPECT_EQ(3U, emf.GetPageCount()); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 156 | EXPECT_TRUE(emf.FinishDocument()); |
Lei Zhang | b4ef0d5d | 2020-03-27 23:18:29 | [diff] [blame] | 157 | uint32_t size = emf.GetDataSize(); |
[email protected] | 96fddd8 | 2011-03-24 23:37:45 | [diff] [blame] | 158 | EXPECT_TRUE(emf.GetDataAsVector(&data)); |
Lei Zhang | b4ef0d5d | 2020-03-27 23:18:29 | [diff] [blame] | 159 | ASSERT_EQ(data.size(), size); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 160 | } |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 161 | |
| 162 | // Playback the data. |
| 163 | DOCINFO di = {0}; |
| 164 | di.cbSize = sizeof(DOCINFO); |
| 165 | di.lpszDocName = L"Test Job"; |
[email protected] | 6f0db2b | 2012-02-14 03:56:18 | [diff] [blame] | 166 | int job_id = ::StartDoc(dc.Get(), &di); |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 167 | Emf emf; |
Lei Zhang | b4ef0d5d | 2020-03-27 23:18:29 | [diff] [blame] | 168 | // TODO(thestig): Make |data| uint8_t and avoid the base::as_bytes() call. |
| 169 | EXPECT_TRUE(emf.InitFromData(base::as_bytes(base::make_span(data)))); |
[email protected] | 6f0db2b | 2012-02-14 03:56:18 | [diff] [blame] | 170 | EXPECT_TRUE(emf.SafePlayback(dc.Get())); |
| 171 | ::EndDoc(dc.Get()); |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 172 | // Since presumably the printer is not real, let us just delete the job from |
| 173 | // the queue. |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 174 | HANDLE printer = nullptr; |
| 175 | if (::OpenPrinter(const_cast<LPTSTR>(L"UnitTest Printer"), &printer, |
| 176 | nullptr)) { |
| 177 | ::SetJob(printer, job_id, 0, nullptr, JOB_CONTROL_DELETE); |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 178 | ClosePrinter(printer); |
| 179 | } |
| 180 | } |
| 181 | |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 182 | TEST(EmfTest, FileBackedEmf) { |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 183 | // Simplest use case. |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 184 | base::ScopedTempDir scratch_metafile_dir; |
[email protected] | f21104d | 2010-10-08 20:57:57 | [diff] [blame] | 185 | ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir()); |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 186 | base::FilePath metafile_path; |
vabr | 16e5f60 | 2016-09-15 18:14:00 | [diff] [blame] | 187 | EXPECT_TRUE(base::CreateTemporaryFileInDir(scratch_metafile_dir.GetPath(), |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 188 | &metafile_path)); |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 189 | uint32_t size; |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 190 | std::vector<char> data; |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 191 | { |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 192 | Emf emf; |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 193 | EXPECT_TRUE(emf.InitToFile(metafile_path)); |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 194 | EXPECT_TRUE(emf.context()); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 195 | // An empty EMF is invalid, so we put at least a rectangle in it. |
| 196 | ::Rectangle(emf.context(), 10, 10, 190, 190); |
| 197 | EXPECT_TRUE(emf.FinishDocument()); |
| 198 | size = emf.GetDataSize(); |
| 199 | EXPECT_GT(size, EMF_HEADER_SIZE); |
[email protected] | 96fddd8 | 2011-03-24 23:37:45 | [diff] [blame] | 200 | EXPECT_TRUE(emf.GetDataAsVector(&data)); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 201 | EXPECT_EQ(data.size(), size); |
| 202 | } |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 203 | int64_t file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 204 | base::GetFileSize(metafile_path, &file_size); |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 205 | EXPECT_EQ(size, file_size); |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 206 | |
| 207 | // Playback the data. |
thestig | 5f68391 | 2016-09-30 22:42:16 | [diff] [blame] | 208 | HDC hdc = CreateCompatibleDC(nullptr); |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 209 | EXPECT_TRUE(hdc); |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 210 | Emf emf; |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame] | 211 | EXPECT_TRUE(emf.InitFromFile(metafile_path)); |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 212 | RECT output_rect = {0, 0, 10, 10}; |
| 213 | EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 214 | EXPECT_TRUE(DeleteDC(hdc)); |
| 215 | } |
| 216 | |
[email protected] | 5ad7617 | 2011-03-02 19:25:17 | [diff] [blame] | 217 | } // namespace printing |