1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "printing/emf_win.h"
13 #include "base/basictypes.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/files/scoped_temp_dir.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/path_service.h"
19 #include "base/win/scoped_hdc.h"
20 #include "printing/printing_context.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/gfx/geometry/point.h"
23 #include "ui/gfx/geometry/size.h"
29 // This test is automatically disabled if no printer named "UnitTest Printer" is
31 class EmfPrintingTest
: public testing::Test
, public PrintingContext::Delegate
{
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
);
43 // PrintingContext::Delegate methods.
44 gfx::NativeView
GetParentView() override
{ return NULL
; }
45 std::string
GetAppLocale() override
{ return std::string(); }
48 const uint32_t EMF_HEADER_SIZE
= 128;
49 const int ONE_MB
= 1024 * 1024;
56 std::vector
<char> data
;
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
);
66 EXPECT_TRUE(emf
.GetDataAsVector(&data
));
67 EXPECT_EQ(data
.size(), size
);
72 EXPECT_TRUE(emf
.InitFromData(&data
.front(), size
));
73 HDC hdc
= CreateCompatibleDC(NULL
);
75 RECT output_rect
= {0, 0, 10, 10};
76 EXPECT_TRUE(emf
.Playback(hdc
, &output_rect
));
77 EXPECT_TRUE(DeleteDC(hdc
));
80 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
81 TEST_F(EmfPrintingTest
, Enumerate
) {
82 if (IsTestCaseDisabled())
85 PrintSettings settings
;
87 // My test case is a HP Color LaserJet 4550 PCL.
88 settings
.set_device_name(L
"UnitTest Printer");
91 scoped_ptr
<PrintingContext
> context(PrintingContext::Create(this));
92 EXPECT_EQ(context
->InitWithSettings(settings
), PrintingContext::OK
);
94 base::FilePath emf_file
;
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"));
100 // Load any EMF with an image.
102 std::string emf_data
;
103 base::ReadFileToString(emf_file
, &emf_data
);
104 ASSERT_TRUE(emf_data
.size());
105 EXPECT_TRUE(emf
.InitFromData(&emf_data
[0], emf_data
.size()));
107 // This will print to file. The reason is that when running inside a
108 // unit_test, PrintingContext automatically dumps its files to the
109 // current directory.
110 // TODO(maruel): Clean the .PRN file generated in current directory.
111 context
->NewDocument(L
"EmfTest.Enumerate");
113 // Process one at a time.
114 RECT page_bounds
= emf
.GetPageBounds(1).ToRECT();
115 Emf::Enumerator
emf_enum(emf
, context
->context(), &page_bounds
);
116 for (Emf::Enumerator::const_iterator itr
= emf_enum
.begin();
117 itr
!= emf_enum
.end();
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
124 EXPECT_TRUE(itr
->SafePlayback(&emf_enum
.context_
)) <<
125 " index: " << index
<< " type: " << itr
->record()->iType
;
128 context
->DocumentDone();
131 // Disabled if no "UnitTest printer" exists.
132 TEST_F(EmfPrintingTest
, PageBreak
) {
133 base::win::ScopedCreateDC
dc(
134 CreateDC(L
"WINSPOOL", L
"UnitTest Printer", NULL
, NULL
));
138 std::vector
<char> data
;
141 EXPECT_TRUE(emf
.Init());
142 EXPECT_TRUE(emf
.context() != NULL
);
145 EXPECT_TRUE(emf
.StartPage(gfx::Size(), gfx::Rect(), 1));
146 ::Rectangle(emf
.context(), 10, 10, 190, 190);
147 EXPECT_TRUE(emf
.FinishPage());
150 EXPECT_EQ(3U, emf
.GetPageCount());
151 EXPECT_TRUE(emf
.FinishDocument());
152 size
= emf
.GetDataSize();
153 EXPECT_TRUE(emf
.GetDataAsVector(&data
));
154 EXPECT_EQ(data
.size(), size
);
157 // Playback the data.
159 di
.cbSize
= sizeof(DOCINFO
);
160 di
.lpszDocName
= L
"Test Job";
161 int job_id
= ::StartDoc(dc
.Get(), &di
);
163 EXPECT_TRUE(emf
.InitFromData(&data
.front(), size
));
164 EXPECT_TRUE(emf
.SafePlayback(dc
.Get()));
166 // Since presumably the printer is not real, let us just delete the job from
168 HANDLE printer
= NULL
;
169 if (::OpenPrinter(const_cast<LPTSTR
>(L
"UnitTest Printer"), &printer
, NULL
)) {
170 ::SetJob(printer
, job_id
, 0, NULL
, JOB_CONTROL_DELETE
);
171 ClosePrinter(printer
);
175 TEST(EmfTest
, FileBackedEmf
) {
176 // Simplest use case.
177 base::ScopedTempDir scratch_metafile_dir
;
178 ASSERT_TRUE(scratch_metafile_dir
.CreateUniqueTempDir());
179 base::FilePath metafile_path
;
180 EXPECT_TRUE(base::CreateTemporaryFileInDir(scratch_metafile_dir
.path(),
183 std::vector
<char> data
;
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
);
193 EXPECT_TRUE(emf
.GetDataAsVector(&data
));
194 EXPECT_EQ(data
.size(), size
);
196 int64_t file_size
= 0;
197 base::GetFileSize(metafile_path
, &file_size
);
198 EXPECT_EQ(size
, file_size
);
200 // Playback the data.
201 HDC hdc
= CreateCompatibleDC(NULL
);
204 EXPECT_TRUE(emf
.InitFromFile(metafile_path
));
205 RECT output_rect
= {0, 0, 10, 10};
206 EXPECT_TRUE(emf
.Playback(hdc
, &output_rect
));
207 EXPECT_TRUE(DeleteDC(hdc
));
210 TEST(EmfTest
, RasterizeMetafile
) {
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
);
219 EXPECT_TRUE(emf
.FinishDocument());
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));
225 raster
= emf
.RasterizeMetafile(20);
226 EXPECT_EQ(emf
.GetPageBounds(1), raster
->GetPageBounds(1));
228 raster
= emf
.RasterizeMetafile(16 * ONE_MB
);
229 // Expected size about 64MB.
230 EXPECT_LE(abs(static_cast<int>(raster
->GetDataSize()) - 64 * ONE_MB
), ONE_MB
);
231 // Bounds should still be the same.
232 EXPECT_EQ(emf
.GetPageBounds(1), raster
->GetPageBounds(1));
235 } // namespace printing