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