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