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