OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "printing/emf_win.h" | 5 #include "printing/emf_win.h" |
6 | 6 |
7 // For quick access. | 7 // For quick access. |
8 #include <wingdi.h> | 8 #include <wingdi.h> |
9 #include <winspool.h> | 9 #include <winspool.h> |
10 | 10 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 } // namespace | 44 } // namespace |
45 | 45 |
46 namespace printing { | 46 namespace printing { |
47 | 47 |
48 TEST(EmfTest, DC) { | 48 TEST(EmfTest, DC) { |
49 // Simplest use case. | 49 // Simplest use case. |
50 uint32 size; | 50 uint32 size; |
51 std::vector<BYTE> data; | 51 std::vector<BYTE> data; |
52 { | 52 { |
53 printing::Emf emf; | 53 Emf emf; |
54 EXPECT_TRUE(emf.Init()); | 54 EXPECT_TRUE(emf.Init()); |
55 EXPECT_TRUE(emf.context() != NULL); | 55 EXPECT_TRUE(emf.context() != NULL); |
56 // An empty EMF is invalid, so we put at least a rectangle in it. | 56 // An empty EMF is invalid, so we put at least a rectangle in it. |
57 ::Rectangle(emf.context(), 10, 10, 190, 190); | 57 ::Rectangle(emf.context(), 10, 10, 190, 190); |
58 EXPECT_TRUE(emf.FinishDocument()); | 58 EXPECT_TRUE(emf.FinishDocument()); |
59 size = emf.GetDataSize(); | 59 size = emf.GetDataSize(); |
60 EXPECT_GT(size, EMF_HEADER_SIZE); | 60 EXPECT_GT(size, EMF_HEADER_SIZE); |
61 EXPECT_TRUE(emf.GetDataAsVector(&data)); | 61 EXPECT_TRUE(emf.GetDataAsVector(&data)); |
62 EXPECT_EQ(data.size(), size); | 62 EXPECT_EQ(data.size(), size); |
63 } | 63 } |
64 | 64 |
65 // Playback the data. | 65 // Playback the data. |
66 printing::Emf emf; | 66 Emf emf; |
67 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); | 67 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); |
68 HDC hdc = CreateCompatibleDC(NULL); | 68 HDC hdc = CreateCompatibleDC(NULL); |
69 EXPECT_TRUE(hdc); | 69 EXPECT_TRUE(hdc); |
70 RECT output_rect = {0, 0, 10, 10}; | 70 RECT output_rect = {0, 0, 10, 10}; |
71 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); | 71 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
72 EXPECT_TRUE(DeleteDC(hdc)); | 72 EXPECT_TRUE(DeleteDC(hdc)); |
73 } | 73 } |
74 | 74 |
75 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. | 75 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. |
76 TEST_F(EmfPrintingTest, Enumerate) { | 76 TEST_F(EmfPrintingTest, Enumerate) { |
77 if (IsTestCaseDisabled()) | 77 if (IsTestCaseDisabled()) |
78 return; | 78 return; |
79 | 79 |
80 printing::PrintSettings settings; | 80 PrintSettings settings; |
81 | 81 |
82 // My test case is a HP Color LaserJet 4550 PCL. | 82 // My test case is a HP Color LaserJet 4550 PCL. |
83 settings.set_device_name(L"UnitTest Printer"); | 83 settings.set_device_name(L"UnitTest Printer"); |
84 | 84 |
85 // Initialize it. | 85 // Initialize it. |
86 scoped_ptr<printing::PrintingContext> context( | 86 scoped_ptr<PrintingContext> context(PrintingContext::Create(std::string())); |
87 printing::PrintingContext::Create(std::string())); | 87 EXPECT_EQ(context->InitWithSettings(settings), PrintingContext::OK); |
88 EXPECT_EQ(context->InitWithSettings(settings), printing::PrintingContext::OK); | |
89 | 88 |
90 FilePath emf_file; | 89 FilePath emf_file; |
91 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file)); | 90 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file)); |
92 emf_file = emf_file.Append(FILE_PATH_LITERAL("printing")) | 91 emf_file = emf_file.Append(FILE_PATH_LITERAL("printing")) |
93 .Append(FILE_PATH_LITERAL("test")) | 92 .Append(FILE_PATH_LITERAL("test")) |
94 .Append(FILE_PATH_LITERAL("data")) | 93 .Append(FILE_PATH_LITERAL("data")) |
95 .Append(FILE_PATH_LITERAL("test4.emf")); | 94 .Append(FILE_PATH_LITERAL("test4.emf")); |
96 // Load any EMF with an image. | 95 // Load any EMF with an image. |
97 printing::Emf emf; | 96 Emf emf; |
98 std::string emf_data; | 97 std::string emf_data; |
99 file_util::ReadFileToString(emf_file, &emf_data); | 98 file_util::ReadFileToString(emf_file, &emf_data); |
100 ASSERT_TRUE(emf_data.size()); | 99 ASSERT_TRUE(emf_data.size()); |
101 EXPECT_TRUE(emf.InitFromData(&emf_data[0], emf_data.size())); | 100 EXPECT_TRUE(emf.InitFromData(&emf_data[0], emf_data.size())); |
102 | 101 |
103 // This will print to file. The reason is that when running inside a | 102 // This will print to file. The reason is that when running inside a |
104 // unit_test, printing::PrintingContext automatically dumps its files to the | 103 // unit_test, PrintingContext automatically dumps its files to the |
105 // current directory. | 104 // current directory. |
106 // TODO(maruel): Clean the .PRN file generated in current directory. | 105 // TODO(maruel): Clean the .PRN file generated in current directory. |
107 context->NewDocument(L"EmfTest.Enumerate"); | 106 context->NewDocument(L"EmfTest.Enumerate"); |
108 context->NewPage(); | 107 context->NewPage(); |
109 // Process one at a time. | 108 // Process one at a time. |
110 printing::Emf::Enumerator emf_enum(emf, context->context(), | 109 Emf::Enumerator emf_enum(emf, context->context(), |
111 &emf.GetPageBounds(1).ToRECT()); | 110 &emf.GetPageBounds(1).ToRECT()); |
112 for (printing::Emf::Enumerator::const_iterator itr = emf_enum.begin(); | 111 for (Emf::Enumerator::const_iterator itr = emf_enum.begin(); |
113 itr != emf_enum.end(); | 112 itr != emf_enum.end(); |
114 ++itr) { | 113 ++itr) { |
115 // To help debugging. | 114 // To help debugging. |
116 ptrdiff_t index = itr - emf_enum.begin(); | 115 ptrdiff_t index = itr - emf_enum.begin(); |
117 // If you get this assert, you need to lookup iType in wingdi.h. It starts | 116 // If you get this assert, you need to lookup iType in wingdi.h. It starts |
118 // with EMR_HEADER. | 117 // with EMR_HEADER. |
119 EMR_HEADER; | 118 EMR_HEADER; |
120 EXPECT_TRUE(itr->SafePlayback(NULL)) << | 119 EXPECT_TRUE(itr->SafePlayback(NULL)) << |
121 " index: " << index << " type: " << itr->record()->iType; | 120 " index: " << index << " type: " << itr->record()->iType; |
122 } | 121 } |
123 context->PageDone(); | 122 context->PageDone(); |
124 context->DocumentDone(); | 123 context->DocumentDone(); |
125 } | 124 } |
126 | 125 |
127 // Disabled if no "UnitTest printer" exists. | 126 // Disabled if no "UnitTest printer" exists. |
128 TEST_F(EmfPrintingTest, PageBreak) { | 127 TEST_F(EmfPrintingTest, PageBreak) { |
129 base::win::ScopedCreateDC dc( | 128 base::win::ScopedCreateDC dc( |
130 CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); | 129 CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); |
131 if (!dc.Get()) | 130 if (!dc.Get()) |
132 return; | 131 return; |
133 uint32 size; | 132 uint32 size; |
134 std::vector<BYTE> data; | 133 std::vector<BYTE> data; |
135 { | 134 { |
136 printing::Emf emf; | 135 Emf emf; |
137 EXPECT_TRUE(emf.Init()); | 136 EXPECT_TRUE(emf.Init()); |
138 EXPECT_TRUE(emf.context() != NULL); | 137 EXPECT_TRUE(emf.context() != NULL); |
139 int pages = 3; | 138 int pages = 3; |
140 while (pages) { | 139 while (pages) { |
141 EXPECT_TRUE(emf.StartPage(gfx::Size(), gfx::Rect(), 1)); | 140 EXPECT_TRUE(emf.StartPage(gfx::Size(), gfx::Rect(), 1)); |
142 ::Rectangle(emf.context(), 10, 10, 190, 190); | 141 ::Rectangle(emf.context(), 10, 10, 190, 190); |
143 EXPECT_TRUE(emf.FinishPage()); | 142 EXPECT_TRUE(emf.FinishPage()); |
144 --pages; | 143 --pages; |
145 } | 144 } |
146 EXPECT_EQ(3U, emf.GetPageCount()); | 145 EXPECT_EQ(3U, emf.GetPageCount()); |
147 EXPECT_TRUE(emf.FinishDocument()); | 146 EXPECT_TRUE(emf.FinishDocument()); |
148 size = emf.GetDataSize(); | 147 size = emf.GetDataSize(); |
149 EXPECT_TRUE(emf.GetDataAsVector(&data)); | 148 EXPECT_TRUE(emf.GetDataAsVector(&data)); |
150 EXPECT_EQ(data.size(), size); | 149 EXPECT_EQ(data.size(), size); |
151 } | 150 } |
152 | 151 |
153 // Playback the data. | 152 // Playback the data. |
154 DOCINFO di = {0}; | 153 DOCINFO di = {0}; |
155 di.cbSize = sizeof(DOCINFO); | 154 di.cbSize = sizeof(DOCINFO); |
156 di.lpszDocName = L"Test Job"; | 155 di.lpszDocName = L"Test Job"; |
157 int job_id = ::StartDoc(dc.Get(), &di); | 156 int job_id = ::StartDoc(dc.Get(), &di); |
158 printing::Emf emf; | 157 Emf emf; |
159 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); | 158 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); |
160 EXPECT_TRUE(emf.SafePlayback(dc.Get())); | 159 EXPECT_TRUE(emf.SafePlayback(dc.Get())); |
161 ::EndDoc(dc.Get()); | 160 ::EndDoc(dc.Get()); |
162 // Since presumably the printer is not real, let us just delete the job from | 161 // Since presumably the printer is not real, let us just delete the job from |
163 // the queue. | 162 // the queue. |
164 HANDLE printer = NULL; | 163 HANDLE printer = NULL; |
165 if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) { | 164 if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) { |
166 ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE); | 165 ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE); |
167 ClosePrinter(printer); | 166 ClosePrinter(printer); |
168 } | 167 } |
169 } | 168 } |
170 | 169 |
171 TEST(EmfTest, FileBackedEmf) { | 170 TEST(EmfTest, FileBackedEmf) { |
172 // Simplest use case. | 171 // Simplest use case. |
173 ScopedTempDir scratch_metafile_dir; | 172 ScopedTempDir scratch_metafile_dir; |
174 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir()); | 173 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir()); |
175 FilePath metafile_path; | 174 FilePath metafile_path; |
176 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir.path(), | 175 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir.path(), |
177 &metafile_path)); | 176 &metafile_path)); |
178 uint32 size; | 177 uint32 size; |
179 std::vector<BYTE> data; | 178 std::vector<BYTE> data; |
180 { | 179 { |
181 printing::Emf emf; | 180 Emf emf; |
182 EXPECT_TRUE(emf.InitToFile(metafile_path)); | 181 EXPECT_TRUE(emf.InitToFile(metafile_path)); |
183 EXPECT_TRUE(emf.context() != NULL); | 182 EXPECT_TRUE(emf.context() != NULL); |
184 // An empty EMF is invalid, so we put at least a rectangle in it. | 183 // An empty EMF is invalid, so we put at least a rectangle in it. |
185 ::Rectangle(emf.context(), 10, 10, 190, 190); | 184 ::Rectangle(emf.context(), 10, 10, 190, 190); |
186 EXPECT_TRUE(emf.FinishDocument()); | 185 EXPECT_TRUE(emf.FinishDocument()); |
187 size = emf.GetDataSize(); | 186 size = emf.GetDataSize(); |
188 EXPECT_GT(size, EMF_HEADER_SIZE); | 187 EXPECT_GT(size, EMF_HEADER_SIZE); |
189 EXPECT_TRUE(emf.GetDataAsVector(&data)); | 188 EXPECT_TRUE(emf.GetDataAsVector(&data)); |
190 EXPECT_EQ(data.size(), size); | 189 EXPECT_EQ(data.size(), size); |
191 } | 190 } |
192 int64 file_size = 0; | 191 int64 file_size = 0; |
193 file_util::GetFileSize(metafile_path, &file_size); | 192 file_util::GetFileSize(metafile_path, &file_size); |
194 EXPECT_EQ(size, file_size); | 193 EXPECT_EQ(size, file_size); |
195 | 194 |
196 // Playback the data. | 195 // Playback the data. |
197 HDC hdc = CreateCompatibleDC(NULL); | 196 HDC hdc = CreateCompatibleDC(NULL); |
198 EXPECT_TRUE(hdc); | 197 EXPECT_TRUE(hdc); |
199 printing::Emf emf; | 198 Emf emf; |
200 EXPECT_TRUE(emf.InitFromFile(metafile_path)); | 199 EXPECT_TRUE(emf.InitFromFile(metafile_path)); |
201 RECT output_rect = {0, 0, 10, 10}; | 200 RECT output_rect = {0, 0, 10, 10}; |
202 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); | 201 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
203 EXPECT_TRUE(DeleteDC(hdc)); | 202 EXPECT_TRUE(DeleteDC(hdc)); |
204 } | 203 } |
205 | 204 |
206 } // namespace printing | 205 } // namespace printing |
OLD | NEW |