[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [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] | 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 | |
| 11 | #include "base/basictypes.h" |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 12 | #include "base/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "base/file_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "base/path_service.h" |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 15 | #include "base/scoped_ptr.h" |
[email protected] | f21104d | 2010-10-08 20:57:57 | [diff] [blame] | 16 | #include "base/scoped_temp_dir.h" |
[email protected] | 0142bd4f | 2010-12-31 01:02:47 | [diff] [blame] | 17 | #include "base/win/scoped_hdc.h" |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 18 | #include "printing/printing_context.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | // This test is automatically disabled if no printer named "UnitTest Printer" is |
| 24 | // available. |
| 25 | class EmfPrintingTest : public testing::Test { |
| 26 | public: |
| 27 | typedef testing::Test Parent; |
| 28 | static bool IsTestCaseDisabled() { |
| 29 | // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700. |
| 30 | HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL); |
| 31 | if (!hdc) |
| 32 | return true; |
| 33 | DeleteDC(hdc); |
| 34 | return false; |
| 35 | } |
| 36 | }; |
| 37 | |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 38 | const uint32 EMF_HEADER_SIZE = 128; |
| 39 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | } // namespace |
| 41 | |
| 42 | TEST(EmfTest, DC) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | // Simplest use case. |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 44 | printing::Emf emf; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | RECT rect = {100, 100, 200, 200}; |
| 46 | HDC hdc = CreateCompatibleDC(NULL); |
| 47 | EXPECT_TRUE(hdc != NULL); |
| 48 | EXPECT_TRUE(emf.CreateDc(hdc, &rect)); |
| 49 | EXPECT_TRUE(emf.hdc() != NULL); |
| 50 | // In theory, you'd use the HDC with GDI functions here. |
| 51 | EXPECT_TRUE(emf.CloseDc()); |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 52 | uint32 size = emf.GetDataSize(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | EXPECT_EQ(size, EMF_HEADER_SIZE); |
| 54 | std::vector<BYTE> data; |
| 55 | EXPECT_TRUE(emf.GetData(&data)); |
| 56 | EXPECT_EQ(data.size(), size); |
| 57 | emf.CloseEmf(); |
| 58 | EXPECT_TRUE(DeleteDC(hdc)); |
| 59 | |
| 60 | // Playback the data. |
| 61 | hdc = CreateCompatibleDC(NULL); |
| 62 | EXPECT_TRUE(hdc); |
[email protected] | e8980a5 | 2010-10-07 20:11:24 | [diff] [blame] | 63 | EXPECT_TRUE(emf.Init(&data.front(), size)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | RECT output_rect = {0, 0, 10, 10}; |
| 65 | EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 66 | EXPECT_TRUE(DeleteDC(hdc)); |
| 67 | } |
| 68 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. |
| 70 | TEST_F(EmfPrintingTest, Enumerate) { |
| 71 | if (IsTestCaseDisabled()) |
| 72 | return; |
| 73 | |
| 74 | printing::PrintSettings settings; |
| 75 | |
| 76 | // My test case is a HP Color LaserJet 4550 PCL. |
| 77 | settings.set_device_name(L"UnitTest Printer"); |
| 78 | |
| 79 | // Initialize it. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 80 | scoped_ptr<printing::PrintingContext> context( |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 81 | printing::PrintingContext::Create(std::string())); |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 82 | EXPECT_EQ(context->InitWithSettings(settings), printing::PrintingContext::OK); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 84 | FilePath emf_file; |
| 85 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file)); |
| 86 | emf_file = emf_file.Append(FILE_PATH_LITERAL("printing")) |
| 87 | .Append(FILE_PATH_LITERAL("test")) |
| 88 | .Append(FILE_PATH_LITERAL("data")) |
| 89 | .Append(FILE_PATH_LITERAL("test4.emf")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | // Load any EMF with an image. |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 91 | printing::Emf emf; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | std::string emf_data; |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 93 | file_util::ReadFileToString(emf_file, &emf_data); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | ASSERT_TRUE(emf_data.size()); |
[email protected] | e8980a5 | 2010-10-07 20:11:24 | [diff] [blame] | 95 | EXPECT_TRUE(emf.Init(&emf_data[0], emf_data.size())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | |
| 97 | // This will print to file. The reason is that when running inside a |
| 98 | // unit_test, printing::PrintingContext automatically dumps its files to the |
| 99 | // current directory. |
| 100 | // TODO(maruel): Clean the .PRN file generated in current directory. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 101 | context->NewDocument(L"EmfTest.Enumerate"); |
| 102 | context->NewPage(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | // Process one at a time. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 104 | printing::Emf::Enumerator emf_enum(emf, context->context(), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | &emf.GetBounds().ToRECT()); |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 106 | for (printing::Emf::Enumerator::const_iterator itr = emf_enum.begin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | itr != emf_enum.end(); |
| 108 | ++itr) { |
| 109 | // To help debugging. |
| 110 | ptrdiff_t index = itr - emf_enum.begin(); |
| 111 | // If you get this assert, you need to lookup iType in wingdi.h. It starts |
| 112 | // with EMR_HEADER. |
| 113 | EMR_HEADER; |
| 114 | EXPECT_TRUE(itr->SafePlayback(NULL)) << |
| 115 | " index: " << index << " type: " << itr->record()->iType; |
| 116 | } |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 117 | context->PageDone(); |
| 118 | context->DocumentDone(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | } |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 120 | |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 121 | // Disabled if no "UnitTest printer" exists. |
| 122 | TEST_F(EmfPrintingTest, PageBreak) { |
[email protected] | 0142bd4f | 2010-12-31 01:02:47 | [diff] [blame] | 123 | base::win::ScopedHDC dc( |
| 124 | CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 125 | if (!dc.Get()) |
| 126 | return; |
| 127 | printing::Emf emf; |
| 128 | EXPECT_TRUE(emf.CreateDc(dc.Get(), NULL)); |
| 129 | EXPECT_TRUE(emf.hdc() != NULL); |
| 130 | int pages = 3; |
| 131 | while (pages) { |
| 132 | EXPECT_TRUE(emf.StartPage()); |
| 133 | ::Rectangle(emf.hdc(), 10, 10, 190, 190); |
| 134 | EXPECT_TRUE(emf.EndPage()); |
| 135 | --pages; |
| 136 | } |
| 137 | EXPECT_TRUE(emf.CloseDc()); |
| 138 | uint32 size = emf.GetDataSize(); |
| 139 | std::vector<BYTE> data; |
| 140 | EXPECT_TRUE(emf.GetData(&data)); |
| 141 | EXPECT_EQ(data.size(), size); |
| 142 | emf.CloseEmf(); |
| 143 | |
| 144 | // Playback the data. |
| 145 | DOCINFO di = {0}; |
| 146 | di.cbSize = sizeof(DOCINFO); |
| 147 | di.lpszDocName = L"Test Job"; |
| 148 | int job_id = ::StartDoc(dc.Get(), &di); |
[email protected] | e8980a5 | 2010-10-07 20:11:24 | [diff] [blame] | 149 | EXPECT_TRUE(emf.Init(&data.front(), size)); |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 150 | EXPECT_TRUE(emf.SafePlayback(dc.Get())); |
| 151 | ::EndDoc(dc.Get()); |
| 152 | // Since presumably the printer is not real, let us just delete the job from |
| 153 | // the queue. |
| 154 | HANDLE printer = NULL; |
| 155 | if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) { |
| 156 | ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE); |
| 157 | ClosePrinter(printer); |
| 158 | } |
| 159 | } |
| 160 | |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 161 | TEST(EmfTest, FileBackedDC) { |
| 162 | // Simplest use case. |
| 163 | printing::Emf emf; |
| 164 | RECT rect = {100, 100, 200, 200}; |
| 165 | HDC hdc = CreateCompatibleDC(NULL); |
| 166 | EXPECT_TRUE(hdc != NULL); |
[email protected] | f21104d | 2010-10-08 20:57:57 | [diff] [blame] | 167 | ScopedTempDir scratch_metafile_dir; |
| 168 | ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir()); |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 169 | FilePath metafile_path; |
[email protected] | f21104d | 2010-10-08 20:57:57 | [diff] [blame] | 170 | EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir.path(), |
| 171 | &metafile_path)); |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 172 | EXPECT_TRUE(emf.CreateFileBackedDc(hdc, &rect, metafile_path)); |
| 173 | EXPECT_TRUE(emf.hdc() != NULL); |
| 174 | // In theory, you'd use the HDC with GDI functions here. |
| 175 | EXPECT_TRUE(emf.CloseDc()); |
| 176 | |
| 177 | uint32 size = emf.GetDataSize(); |
| 178 | EXPECT_EQ(size, EMF_HEADER_SIZE); |
| 179 | std::vector<BYTE> data; |
| 180 | EXPECT_TRUE(emf.GetData(&data)); |
| 181 | EXPECT_EQ(data.size(), size); |
| 182 | emf.CloseEmf(); |
| 183 | int64 file_size = 0; |
| 184 | file_util::GetFileSize(metafile_path, &file_size); |
| 185 | EXPECT_EQ(size, file_size); |
| 186 | EXPECT_TRUE(DeleteDC(hdc)); |
| 187 | |
| 188 | // Playback the data. |
| 189 | hdc = CreateCompatibleDC(NULL); |
| 190 | EXPECT_TRUE(hdc); |
| 191 | EXPECT_TRUE(emf.CreateFromFile(metafile_path)); |
| 192 | RECT output_rect = {0, 0, 10, 10}; |
| 193 | EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 194 | EXPECT_TRUE(DeleteDC(hdc)); |
[email protected] | f21104d | 2010-10-08 20:57:57 | [diff] [blame] | 195 | emf.CloseEmf(); |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 196 | } |
| 197 | |