blob: f6ea72a3577b1c1c1f74e5a5f13aadc877e317a7 [file] [log] [blame]
[email protected]0064b392012-06-20 01:57:381// Copyright (c) 2012 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
[email protected]83deae22011-10-07 05:56:3211#include <string>
12
initial.commit09911bf2008-07-26 23:55:2913#include "base/basictypes.h"
[email protected]57999812013-02-24 05:40:5214#include "base/files/file_path.h"
thestig22dfc4012014-09-05 08:29:4415#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2316#include "base/files/scoped_temp_dir.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2918#include "base/path_service.h"
[email protected]0142bd4f2010-12-31 01:02:4719#include "base/win/scoped_hdc.h"
[email protected]daee4972009-07-09 14:28:2420#include "printing/printing_context.h"
initial.commit09911bf2008-07-26 23:55:2921#include "testing/gtest/include/gtest/gtest.h"
tfarina655f81d2014-12-23 02:38:5022#include "ui/gfx/geometry/point.h"
tfarinaebe974f02015-01-03 04:25:3223#include "ui/gfx/geometry/size.h"
initial.commit09911bf2008-07-26 23:55:2924
Vitaly Bukabd7c9812014-08-26 08:57:5425namespace printing {
26
initial.commit09911bf2008-07-26 23:55:2927namespace {
28
29// This test is automatically disabled if no printer named "UnitTest Printer" is
30// available.
Vitaly Bukabd7c9812014-08-26 08:57:5431class EmfPrintingTest : public testing::Test, public PrintingContext::Delegate {
initial.commit09911bf2008-07-26 23:55:2932 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;
41 }
Vitaly Bukabd7c9812014-08-26 08:57:5442
43 // PrintingContext::Delegate methods.
nickbc761612015-04-27 23:30:4644 gfx::NativeView GetParentView() override { return NULL; }
45 std::string GetAppLocale() override { return std::string(); }
initial.commit09911bf2008-07-26 23:55:2946};
47
thestig707a24b22015-09-14 18:16:3348const uint32_t EMF_HEADER_SIZE = 128;
49const int ONE_MB = 1024 * 1024;
[email protected]e6cddc572010-09-29 21:39:4550
initial.commit09911bf2008-07-26 23:55:2951} // namespace
52
53TEST(EmfTest, DC) {
initial.commit09911bf2008-07-26 23:55:2954 // Simplest use case.
thestig707a24b22015-09-14 18:16:3355 uint32_t size;
vitalybuka5d1290582014-09-12 09:19:5956 std::vector<char> data;
[email protected]481edc52011-03-22 06:36:5857 {
[email protected]d91db112011-10-18 20:58:5158 Emf emf;
[email protected]481edc52011-03-22 06:36:5859 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);
[email protected]96fddd82011-03-24 23:37:4566 EXPECT_TRUE(emf.GetDataAsVector(&data));
[email protected]481edc52011-03-22 06:36:5867 EXPECT_EQ(data.size(), size);
68 }
initial.commit09911bf2008-07-26 23:55:2969
70 // Playback the data.
[email protected]d91db112011-10-18 20:58:5171 Emf emf;
[email protected]f2da4b02011-03-18 05:45:5172 EXPECT_TRUE(emf.InitFromData(&data.front(), size));
[email protected]481edc52011-03-22 06:36:5873 HDC hdc = CreateCompatibleDC(NULL);
74 EXPECT_TRUE(hdc);
initial.commit09911bf2008-07-26 23:55:2975 RECT output_rect = {0, 0, 10, 10};
76 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
77 EXPECT_TRUE(DeleteDC(hdc));
78}
79
initial.commit09911bf2008-07-26 23:55:2980// Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
81TEST_F(EmfPrintingTest, Enumerate) {
82 if (IsTestCaseDisabled())
83 return;
84
[email protected]d91db112011-10-18 20:58:5185 PrintSettings settings;
initial.commit09911bf2008-07-26 23:55:2986
87 // My test case is a HP Color LaserJet 4550 PCL.
88 settings.set_device_name(L"UnitTest Printer");
89
90 // Initialize it.
Vitaly Bukabd7c9812014-08-26 08:57:5491 scoped_ptr<PrintingContext> context(PrintingContext::Create(this));
[email protected]d91db112011-10-18 20:58:5192 EXPECT_EQ(context->InitWithSettings(settings), PrintingContext::OK);
initial.commit09911bf2008-07-26 23:55:2993
[email protected]79f63882013-02-10 05:15:4594 base::FilePath emf_file;
[email protected]daee4972009-07-09 14:28:2495 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"));
initial.commit09911bf2008-07-26 23:55:29100 // Load any EMF with an image.
[email protected]d91db112011-10-18 20:58:51101 Emf emf;
initial.commit09911bf2008-07-26 23:55:29102 std::string emf_data;
[email protected]82f84b92013-08-30 18:23:50103 base::ReadFileToString(emf_file, &emf_data);
initial.commit09911bf2008-07-26 23:55:29104 ASSERT_TRUE(emf_data.size());
[email protected]89eff96a2011-03-17 23:22:06105 EXPECT_TRUE(emf.InitFromData(&emf_data[0], emf_data.size()));
initial.commit09911bf2008-07-26 23:55:29106
107 // This will print to file. The reason is that when running inside a
[email protected]d91db112011-10-18 20:58:51108 // unit_test, PrintingContext automatically dumps its files to the
initial.commit09911bf2008-07-26 23:55:29109 // current directory.
110 // TODO(maruel): Clean the .PRN file generated in current directory.
[email protected]51e8d9352010-10-06 22:21:17111 context->NewDocument(L"EmfTest.Enumerate");
112 context->NewPage();
initial.commit09911bf2008-07-26 23:55:29113 // Process one at a time.
[email protected]59149922014-07-04 22:08:04114 RECT page_bounds = emf.GetPageBounds(1).ToRECT();
115 Emf::Enumerator emf_enum(emf, context->context(), &page_bounds);
[email protected]d91db112011-10-18 20:58:51116 for (Emf::Enumerator::const_iterator itr = emf_enum.begin();
initial.commit09911bf2008-07-26 23:55:29117 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;
[email protected]0064b392012-06-20 01:57:38124 EXPECT_TRUE(itr->SafePlayback(&emf_enum.context_)) <<
initial.commit09911bf2008-07-26 23:55:29125 " index: " << index << " type: " << itr->record()->iType;
126 }
[email protected]51e8d9352010-10-06 22:21:17127 context->PageDone();
128 context->DocumentDone();
initial.commit09911bf2008-07-26 23:55:29129}
[email protected]daee4972009-07-09 14:28:24130
[email protected]2aa8e182010-07-12 16:25:04131// Disabled if no "UnitTest printer" exists.
132TEST_F(EmfPrintingTest, PageBreak) {
[email protected]83deae22011-10-07 05:56:32133 base::win::ScopedCreateDC dc(
[email protected]0142bd4f2010-12-31 01:02:47134 CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL));
[email protected]6f0db2b2012-02-14 03:56:18135 if (!dc.Get())
[email protected]2aa8e182010-07-12 16:25:04136 return;
thestig707a24b22015-09-14 18:16:33137 uint32_t size;
vitalybuka5d1290582014-09-12 09:19:59138 std::vector<char> data;
[email protected]481edc52011-03-22 06:36:58139 {
[email protected]d91db112011-10-18 20:58:51140 Emf emf;
[email protected]481edc52011-03-22 06:36:58141 EXPECT_TRUE(emf.Init());
142 EXPECT_TRUE(emf.context() != NULL);
143 int pages = 3;
144 while (pages) {
[email protected]39892b92011-04-30 02:24:44145 EXPECT_TRUE(emf.StartPage(gfx::Size(), gfx::Rect(), 1));
[email protected]481edc52011-03-22 06:36:58146 ::Rectangle(emf.context(), 10, 10, 190, 190);
147 EXPECT_TRUE(emf.FinishPage());
148 --pages;
149 }
[email protected]830cf742011-04-01 16:06:25150 EXPECT_EQ(3U, emf.GetPageCount());
[email protected]481edc52011-03-22 06:36:58151 EXPECT_TRUE(emf.FinishDocument());
152 size = emf.GetDataSize();
[email protected]96fddd82011-03-24 23:37:45153 EXPECT_TRUE(emf.GetDataAsVector(&data));
[email protected]481edc52011-03-22 06:36:58154 EXPECT_EQ(data.size(), size);
155 }
[email protected]2aa8e182010-07-12 16:25:04156
157 // Playback the data.
158 DOCINFO di = {0};
159 di.cbSize = sizeof(DOCINFO);
160 di.lpszDocName = L"Test Job";
[email protected]6f0db2b2012-02-14 03:56:18161 int job_id = ::StartDoc(dc.Get(), &di);
[email protected]d91db112011-10-18 20:58:51162 Emf emf;
[email protected]89eff96a2011-03-17 23:22:06163 EXPECT_TRUE(emf.InitFromData(&data.front(), size));
[email protected]6f0db2b2012-02-14 03:56:18164 EXPECT_TRUE(emf.SafePlayback(dc.Get()));
165 ::EndDoc(dc.Get());
[email protected]2aa8e182010-07-12 16:25:04166 // Since presumably the printer is not real, let us just delete the job from
167 // the queue.
168 HANDLE printer = NULL;
[email protected]59149922014-07-04 22:08:04169 if (::OpenPrinter(const_cast<LPTSTR>(L"UnitTest Printer"), &printer, NULL)) {
[email protected]2aa8e182010-07-12 16:25:04170 ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE);
171 ClosePrinter(printer);
172 }
173}
174
[email protected]481edc52011-03-22 06:36:58175TEST(EmfTest, FileBackedEmf) {
[email protected]e6cddc572010-09-29 21:39:45176 // Simplest use case.
[email protected]ea1a3f62012-11-16 20:34:23177 base::ScopedTempDir scratch_metafile_dir;
[email protected]f21104d2010-10-08 20:57:57178 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir());
[email protected]79f63882013-02-10 05:15:45179 base::FilePath metafile_path;
[email protected]03d9afc02013-12-03 17:55:52180 EXPECT_TRUE(base::CreateTemporaryFileInDir(scratch_metafile_dir.path(),
181 &metafile_path));
thestig707a24b22015-09-14 18:16:33182 uint32_t size;
vitalybuka5d1290582014-09-12 09:19:59183 std::vector<char> data;
[email protected]481edc52011-03-22 06:36:58184 {
[email protected]d91db112011-10-18 20:58:51185 Emf emf;
[email protected]481edc52011-03-22 06:36:58186 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);
[email protected]96fddd82011-03-24 23:37:45193 EXPECT_TRUE(emf.GetDataAsVector(&data));
[email protected]481edc52011-03-22 06:36:58194 EXPECT_EQ(data.size(), size);
195 }
thestig707a24b22015-09-14 18:16:33196 int64_t file_size = 0;
[email protected]56285702013-12-04 18:22:49197 base::GetFileSize(metafile_path, &file_size);
[email protected]e6cddc572010-09-29 21:39:45198 EXPECT_EQ(size, file_size);
[email protected]e6cddc572010-09-29 21:39:45199
200 // Playback the data.
[email protected]481edc52011-03-22 06:36:58201 HDC hdc = CreateCompatibleDC(NULL);
[email protected]e6cddc572010-09-29 21:39:45202 EXPECT_TRUE(hdc);
[email protected]d91db112011-10-18 20:58:51203 Emf emf;
[email protected]481edc52011-03-22 06:36:58204 EXPECT_TRUE(emf.InitFromFile(metafile_path));
[email protected]e6cddc572010-09-29 21:39:45205 RECT output_rect = {0, 0, 10, 10};
206 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
207 EXPECT_TRUE(DeleteDC(hdc));
208}
209
[email protected]60d77bd2012-08-22 00:10:07210TEST(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);
218 }
219 EXPECT_TRUE(emf.FinishDocument());
220
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));
224
vitalybuka5d1290582014-09-12 09:19:59225 raster = emf.RasterizeMetafile(20);
[email protected]60d77bd2012-08-22 00:10:07226 EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1));
227
thestig707a24b22015-09-14 18:16:33228 raster = emf.RasterizeMetafile(16 * ONE_MB);
[email protected]60d77bd2012-08-22 00:10:07229 // Expected size about 64MB.
thestig707a24b22015-09-14 18:16:33230 EXPECT_LE(abs(static_cast<int>(raster->GetDataSize()) - 64 * ONE_MB), ONE_MB);
[email protected]60d77bd2012-08-22 00:10:07231 // Bounds should still be the same.
232 EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1));
233}
234
[email protected]5ad76172011-03-02 19:25:17235} // namespace printing