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
3 // found in the LICENSE file.
5 #include "printing/emf_win.h"
13 #include "base/basictypes.h"
14 #include "base/file_path.h"
15 #include "base/file_util.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/path_service.h"
18 #include "base/scoped_temp_dir.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/point.h"
23 #include "ui/gfx/size.h"
27 // This test is automatically disabled if no printer named "UnitTest Printer" is
29 class EmfPrintingTest
: public testing::Test
{
31 typedef testing::Test Parent
;
32 static bool IsTestCaseDisabled() {
33 // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700.
34 HDC hdc
= CreateDC(L
"WINSPOOL", L
"UnitTest Printer", NULL
, NULL
);
42 const uint32 EMF_HEADER_SIZE
= 128;
51 std::vector
<BYTE
> data
;
54 EXPECT_TRUE(emf
.Init());
55 EXPECT_TRUE(emf
.context() != NULL
);
56 // An empty EMF is invalid, so we put at least a rectangle in it.
57 ::Rectangle(emf
.context(), 10, 10, 190, 190);
58 EXPECT_TRUE(emf
.FinishDocument());
59 size
= emf
.GetDataSize();
60 EXPECT_GT(size
, EMF_HEADER_SIZE
);
61 EXPECT_TRUE(emf
.GetDataAsVector(&data
));
62 EXPECT_EQ(data
.size(), size
);
67 EXPECT_TRUE(emf
.InitFromData(&data
.front(), size
));
68 HDC hdc
= CreateCompatibleDC(NULL
);
70 RECT output_rect
= {0, 0, 10, 10};
71 EXPECT_TRUE(emf
.Playback(hdc
, &output_rect
));
72 EXPECT_TRUE(DeleteDC(hdc
));
75 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
76 TEST_F(EmfPrintingTest
, Enumerate
) {
77 if (IsTestCaseDisabled())
80 PrintSettings settings
;
82 // My test case is a HP Color LaserJet 4550 PCL.
83 settings
.set_device_name(L
"UnitTest Printer");
86 scoped_ptr
<PrintingContext
> context(PrintingContext::Create(std::string()));
87 EXPECT_EQ(context
->InitWithSettings(settings
), PrintingContext::OK
);
90 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &emf_file
));
91 emf_file
= emf_file
.Append(FILE_PATH_LITERAL("printing"))
92 .Append(FILE_PATH_LITERAL("test"))
93 .Append(FILE_PATH_LITERAL("data"))
94 .Append(FILE_PATH_LITERAL("test4.emf"));
95 // Load any EMF with an image.
98 file_util::ReadFileToString(emf_file
, &emf_data
);
99 ASSERT_TRUE(emf_data
.size());
100 EXPECT_TRUE(emf
.InitFromData(&emf_data
[0], emf_data
.size()));
102 // This will print to file. The reason is that when running inside a
103 // unit_test, PrintingContext automatically dumps its files to the
104 // current directory.
105 // TODO(maruel): Clean the .PRN file generated in current directory.
106 context
->NewDocument(L
"EmfTest.Enumerate");
108 // Process one at a time.
109 Emf::Enumerator
emf_enum(emf
, context
->context(),
110 &emf
.GetPageBounds(1).ToRECT());
111 for (Emf::Enumerator::const_iterator itr
= emf_enum
.begin();
112 itr
!= emf_enum
.end();
114 // To help debugging.
115 ptrdiff_t index
= itr
- emf_enum
.begin();
116 // If you get this assert, you need to lookup iType in wingdi.h. It starts
119 EXPECT_TRUE(itr
->SafePlayback(NULL
)) <<
120 " index: " << index
<< " type: " << itr
->record()->iType
;
123 context
->DocumentDone();
126 // Disabled if no "UnitTest printer" exists.
127 TEST_F(EmfPrintingTest
, PageBreak
) {
128 base::win::ScopedCreateDC
dc(
129 CreateDC(L
"WINSPOOL", L
"UnitTest Printer", NULL
, NULL
));
133 std::vector
<BYTE
> data
;
136 EXPECT_TRUE(emf
.Init());
137 EXPECT_TRUE(emf
.context() != NULL
);
140 EXPECT_TRUE(emf
.StartPage(gfx::Size(), gfx::Rect(), 1));
141 ::Rectangle(emf
.context(), 10, 10, 190, 190);
142 EXPECT_TRUE(emf
.FinishPage());
145 EXPECT_EQ(3U, emf
.GetPageCount());
146 EXPECT_TRUE(emf
.FinishDocument());
147 size
= emf
.GetDataSize();
148 EXPECT_TRUE(emf
.GetDataAsVector(&data
));
149 EXPECT_EQ(data
.size(), size
);
152 // Playback the data.
154 di
.cbSize
= sizeof(DOCINFO
);
155 di
.lpszDocName
= L
"Test Job";
156 int job_id
= ::StartDoc(dc
.Get(), &di
);
158 EXPECT_TRUE(emf
.InitFromData(&data
.front(), size
));
159 EXPECT_TRUE(emf
.SafePlayback(dc
.Get()));
161 // Since presumably the printer is not real, let us just delete the job from
163 HANDLE printer
= NULL
;
164 if (::OpenPrinter(L
"UnitTest Printer", &printer
, NULL
)) {
165 ::SetJob(printer
, job_id
, 0, NULL
, JOB_CONTROL_DELETE
);
166 ClosePrinter(printer
);
170 TEST(EmfTest
, FileBackedEmf
) {
171 // Simplest use case.
172 ScopedTempDir scratch_metafile_dir
;
173 ASSERT_TRUE(scratch_metafile_dir
.CreateUniqueTempDir());
174 FilePath metafile_path
;
175 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir
.path(),
178 std::vector
<BYTE
> data
;
181 EXPECT_TRUE(emf
.InitToFile(metafile_path
));
182 EXPECT_TRUE(emf
.context() != NULL
);
183 // An empty EMF is invalid, so we put at least a rectangle in it.
184 ::Rectangle(emf
.context(), 10, 10, 190, 190);
185 EXPECT_TRUE(emf
.FinishDocument());
186 size
= emf
.GetDataSize();
187 EXPECT_GT(size
, EMF_HEADER_SIZE
);
188 EXPECT_TRUE(emf
.GetDataAsVector(&data
));
189 EXPECT_EQ(data
.size(), size
);
192 file_util::GetFileSize(metafile_path
, &file_size
);
193 EXPECT_EQ(size
, file_size
);
195 // Playback the data.
196 HDC hdc
= CreateCompatibleDC(NULL
);
199 EXPECT_TRUE(emf
.InitFromFile(metafile_path
));
200 RECT output_rect
= {0, 0, 10, 10};
201 EXPECT_TRUE(emf
.Playback(hdc
, &output_rect
));
202 EXPECT_TRUE(DeleteDC(hdc
));
205 } // namespace printing