blob: 4312b2ca7c389549ab40b3626f1e2a6eff086edd [file] [log] [blame]
[email protected]b90d7e802011-01-09 16:32:201// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]0e0fca32009-07-06 15:25:505#include "printing/emf_win.h"
initial.commit09911bf2008-07-26 23:55:296
7// For quick access.
8#include <wingdi.h>
[email protected]2aa8e182010-07-12 16:25:049#include <winspool.h>
initial.commit09911bf2008-07-26 23:55:2910
11#include "base/basictypes.h"
[email protected]daee4972009-07-09 14:28:2412#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/path_service.h"
[email protected]51e8d9352010-10-06 22:21:1715#include "base/scoped_ptr.h"
[email protected]f21104d2010-10-08 20:57:5716#include "base/scoped_temp_dir.h"
[email protected]0142bd4f2010-12-31 01:02:4717#include "base/win/scoped_hdc.h"
[email protected]daee4972009-07-09 14:28:2418#include "printing/printing_context.h"
initial.commit09911bf2008-07-26 23:55:2919#include "testing/gtest/include/gtest/gtest.h"
20
21namespace {
22
23// This test is automatically disabled if no printer named "UnitTest Printer" is
24// available.
25class 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]e6cddc572010-09-29 21:39:4538const uint32 EMF_HEADER_SIZE = 128;
39
initial.commit09911bf2008-07-26 23:55:2940} // namespace
41
42TEST(EmfTest, DC) {
initial.commit09911bf2008-07-26 23:55:2943 // Simplest use case.
[email protected]0e0fca32009-07-06 15:25:5044 printing::Emf emf;
initial.commit09911bf2008-07-26 23:55:2945 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]b5ab3982010-02-16 23:58:2752 uint32 size = emf.GetDataSize();
initial.commit09911bf2008-07-26 23:55:2953 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]e8980a52010-10-07 20:11:2463 EXPECT_TRUE(emf.Init(&data.front(), size));
initial.commit09911bf2008-07-26 23:55:2964 RECT output_rect = {0, 0, 10, 10};
65 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
66 EXPECT_TRUE(DeleteDC(hdc));
67}
68
initial.commit09911bf2008-07-26 23:55:2969// Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
70TEST_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]51e8d9352010-10-06 22:21:1780 scoped_ptr<printing::PrintingContext> context(
[email protected]ee5f36e42010-12-03 22:40:3781 printing::PrintingContext::Create(std::string()));
[email protected]51e8d9352010-10-06 22:21:1782 EXPECT_EQ(context->InitWithSettings(settings), printing::PrintingContext::OK);
initial.commit09911bf2008-07-26 23:55:2983
[email protected]daee4972009-07-09 14:28:2484 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.commit09911bf2008-07-26 23:55:2990 // Load any EMF with an image.
[email protected]0e0fca32009-07-06 15:25:5091 printing::Emf emf;
initial.commit09911bf2008-07-26 23:55:2992 std::string emf_data;
[email protected]daee4972009-07-09 14:28:2493 file_util::ReadFileToString(emf_file, &emf_data);
initial.commit09911bf2008-07-26 23:55:2994 ASSERT_TRUE(emf_data.size());
[email protected]e8980a52010-10-07 20:11:2495 EXPECT_TRUE(emf.Init(&emf_data[0], emf_data.size()));
initial.commit09911bf2008-07-26 23:55:2996
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]51e8d9352010-10-06 22:21:17101 context->NewDocument(L"EmfTest.Enumerate");
102 context->NewPage();
initial.commit09911bf2008-07-26 23:55:29103 // Process one at a time.
[email protected]51e8d9352010-10-06 22:21:17104 printing::Emf::Enumerator emf_enum(emf, context->context(),
initial.commit09911bf2008-07-26 23:55:29105 &emf.GetBounds().ToRECT());
[email protected]0e0fca32009-07-06 15:25:50106 for (printing::Emf::Enumerator::const_iterator itr = emf_enum.begin();
initial.commit09911bf2008-07-26 23:55:29107 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]51e8d9352010-10-06 22:21:17117 context->PageDone();
118 context->DocumentDone();
initial.commit09911bf2008-07-26 23:55:29119}
[email protected]daee4972009-07-09 14:28:24120
[email protected]2aa8e182010-07-12 16:25:04121// Disabled if no "UnitTest printer" exists.
122TEST_F(EmfPrintingTest, PageBreak) {
[email protected]0142bd4f2010-12-31 01:02:47123 base::win::ScopedHDC dc(
124 CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL));
[email protected]2aa8e182010-07-12 16:25:04125 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]e8980a52010-10-07 20:11:24149 EXPECT_TRUE(emf.Init(&data.front(), size));
[email protected]2aa8e182010-07-12 16:25:04150 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]e6cddc572010-09-29 21:39:45161TEST(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]f21104d2010-10-08 20:57:57167 ScopedTempDir scratch_metafile_dir;
168 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir());
[email protected]e6cddc572010-09-29 21:39:45169 FilePath metafile_path;
[email protected]f21104d2010-10-08 20:57:57170 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir.path(),
171 &metafile_path));
[email protected]e6cddc572010-09-29 21:39:45172 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]f21104d2010-10-08 20:57:57195 emf.CloseEmf();
[email protected]e6cddc572010-09-29 21:39:45196}
197