[KeyEvent Mac] Move WebInputEventFactory into chromium.
[chromium-blink-merge.git] / printing / emf_win_unittest.cc
blobf6ea72a3577b1c1c1f74e5a5f13aadc877e317a7
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"
7 // For quick access.
8 #include <wingdi.h>
9 #include <winspool.h>
11 #include <string>
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"
25 namespace printing {
27 namespace {
29 // This test is automatically disabled if no printer named "UnitTest Printer" is
30 // available.
31 class EmfPrintingTest : public testing::Test, public PrintingContext::Delegate {
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;
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;
51 } // namespace
53 TEST(EmfTest, DC) {
54 // Simplest use case.
55 uint32_t size;
56 std::vector<char> data;
58 Emf emf;
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);
70 // Playback the data.
71 Emf emf;
72 EXPECT_TRUE(emf.InitFromData(&data.front(), size));
73 HDC hdc = CreateCompatibleDC(NULL);
74 EXPECT_TRUE(hdc);
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())
83 return;
85 PrintSettings settings;
87 // My test case is a HP Color LaserJet 4550 PCL.
88 settings.set_device_name(L"UnitTest Printer");
90 // Initialize it.
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.
101 Emf emf;
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");
112 context->NewPage();
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();
118 ++itr) {
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
122 // with EMR_HEADER.
123 EMR_HEADER;
124 EXPECT_TRUE(itr->SafePlayback(&emf_enum.context_)) <<
125 " index: " << index << " type: " << itr->record()->iType;
127 context->PageDone();
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));
135 if (!dc.Get())
136 return;
137 uint32_t size;
138 std::vector<char> data;
140 Emf emf;
141 EXPECT_TRUE(emf.Init());
142 EXPECT_TRUE(emf.context() != NULL);
143 int pages = 3;
144 while (pages) {
145 EXPECT_TRUE(emf.StartPage(gfx::Size(), gfx::Rect(), 1));
146 ::Rectangle(emf.context(), 10, 10, 190, 190);
147 EXPECT_TRUE(emf.FinishPage());
148 --pages;
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.
158 DOCINFO di = {0};
159 di.cbSize = sizeof(DOCINFO);
160 di.lpszDocName = L"Test Job";
161 int job_id = ::StartDoc(dc.Get(), &di);
162 Emf emf;
163 EXPECT_TRUE(emf.InitFromData(&data.front(), size));
164 EXPECT_TRUE(emf.SafePlayback(dc.Get()));
165 ::EndDoc(dc.Get());
166 // Since presumably the printer is not real, let us just delete the job from
167 // the queue.
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(),
181 &metafile_path));
182 uint32_t size;
183 std::vector<char> data;
185 Emf emf;
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);
202 EXPECT_TRUE(hdc);
203 Emf emf;
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) {
211 Emf emf;
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