blob: 247da12434913c8e841080706fd83e9638454dbd [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.
anujk.sharma263c3132014-10-07 07:29:4244 virtual gfx::NativeView GetParentView() override { return NULL; }
45 virtual std::string GetAppLocale() override { return std::string(); }
initial.commit09911bf2008-07-26 23:55:2946};
47
[email protected]e6cddc572010-09-29 21:39:4548const uint32 EMF_HEADER_SIZE = 128;
49
initial.commit09911bf2008-07-26 23:55:2950} // namespace
51
52TEST(EmfTest, DC) {
initial.commit09911bf2008-07-26 23:55:2953 // Simplest use case.
[email protected]481edc52011-03-22 06:36:5854 uint32 size;
vitalybuka5d1290582014-09-12 09:19:5955 std::vector<char> data;
[email protected]481edc52011-03-22 06:36:5856 {
[email protected]d91db112011-10-18 20:58:5157 Emf emf;
[email protected]481edc52011-03-22 06:36:5858 EXPECT_TRUE(emf.Init());
59 EXPECT_TRUE(emf.context() != NULL);
60 // An empty EMF is invalid, so we put at least a rectangle in it.
61 ::Rectangle(emf.context(), 10, 10, 190, 190);
62 EXPECT_TRUE(emf.FinishDocument());
63 size = emf.GetDataSize();
64 EXPECT_GT(size, EMF_HEADER_SIZE);
[email protected]96fddd82011-03-24 23:37:4565 EXPECT_TRUE(emf.GetDataAsVector(&data));
[email protected]481edc52011-03-22 06:36:5866 EXPECT_EQ(data.size(), size);
67 }
initial.commit09911bf2008-07-26 23:55:2968
69 // Playback the data.
[email protected]d91db112011-10-18 20:58:5170 Emf emf;
[email protected]f2da4b02011-03-18 05:45:5171 EXPECT_TRUE(emf.InitFromData(&data.front(), size));
[email protected]481edc52011-03-22 06:36:5872 HDC hdc = CreateCompatibleDC(NULL);
73 EXPECT_TRUE(hdc);
initial.commit09911bf2008-07-26 23:55:2974 RECT output_rect = {0, 0, 10, 10};
75 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
76 EXPECT_TRUE(DeleteDC(hdc));
77}
78
initial.commit09911bf2008-07-26 23:55:2979// Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
80TEST_F(EmfPrintingTest, Enumerate) {
81 if (IsTestCaseDisabled())
82 return;
83
[email protected]d91db112011-10-18 20:58:5184 PrintSettings settings;
initial.commit09911bf2008-07-26 23:55:2985
86 // My test case is a HP Color LaserJet 4550 PCL.
87 settings.set_device_name(L"UnitTest Printer");
88
89 // Initialize it.
Vitaly Bukabd7c9812014-08-26 08:57:5490 scoped_ptr<PrintingContext> context(PrintingContext::Create(this));
[email protected]d91db112011-10-18 20:58:5191 EXPECT_EQ(context->InitWithSettings(settings), PrintingContext::OK);
initial.commit09911bf2008-07-26 23:55:2992
[email protected]79f63882013-02-10 05:15:4593 base::FilePath emf_file;
[email protected]daee4972009-07-09 14:28:2494 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file));
95 emf_file = emf_file.Append(FILE_PATH_LITERAL("printing"))
96 .Append(FILE_PATH_LITERAL("test"))
97 .Append(FILE_PATH_LITERAL("data"))
98 .Append(FILE_PATH_LITERAL("test4.emf"));
initial.commit09911bf2008-07-26 23:55:2999 // Load any EMF with an image.
[email protected]d91db112011-10-18 20:58:51100 Emf emf;
initial.commit09911bf2008-07-26 23:55:29101 std::string emf_data;
[email protected]82f84b92013-08-30 18:23:50102 base::ReadFileToString(emf_file, &emf_data);
initial.commit09911bf2008-07-26 23:55:29103 ASSERT_TRUE(emf_data.size());
[email protected]89eff96a2011-03-17 23:22:06104 EXPECT_TRUE(emf.InitFromData(&emf_data[0], emf_data.size()));
initial.commit09911bf2008-07-26 23:55:29105
106 // This will print to file. The reason is that when running inside a
[email protected]d91db112011-10-18 20:58:51107 // unit_test, PrintingContext automatically dumps its files to the
initial.commit09911bf2008-07-26 23:55:29108 // current directory.
109 // TODO(maruel): Clean the .PRN file generated in current directory.
[email protected]51e8d9352010-10-06 22:21:17110 context->NewDocument(L"EmfTest.Enumerate");
111 context->NewPage();
initial.commit09911bf2008-07-26 23:55:29112 // Process one at a time.
[email protected]59149922014-07-04 22:08:04113 RECT page_bounds = emf.GetPageBounds(1).ToRECT();
114 Emf::Enumerator emf_enum(emf, context->context(), &page_bounds);
[email protected]d91db112011-10-18 20:58:51115 for (Emf::Enumerator::const_iterator itr = emf_enum.begin();
initial.commit09911bf2008-07-26 23:55:29116 itr != emf_enum.end();
117 ++itr) {
118 // To help debugging.
119 ptrdiff_t index = itr - emf_enum.begin();
120 // If you get this assert, you need to lookup iType in wingdi.h. It starts
121 // with EMR_HEADER.
122 EMR_HEADER;
[email protected]0064b392012-06-20 01:57:38123 EXPECT_TRUE(itr->SafePlayback(&emf_enum.context_)) <<
initial.commit09911bf2008-07-26 23:55:29124 " index: " << index << " type: " << itr->record()->iType;
125 }
[email protected]51e8d9352010-10-06 22:21:17126 context->PageDone();
127 context->DocumentDone();
initial.commit09911bf2008-07-26 23:55:29128}
[email protected]daee4972009-07-09 14:28:24129
[email protected]2aa8e182010-07-12 16:25:04130// Disabled if no "UnitTest printer" exists.
131TEST_F(EmfPrintingTest, PageBreak) {
[email protected]83deae22011-10-07 05:56:32132 base::win::ScopedCreateDC dc(
[email protected]0142bd4f2010-12-31 01:02:47133 CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL));
[email protected]6f0db2b2012-02-14 03:56:18134 if (!dc.Get())
[email protected]2aa8e182010-07-12 16:25:04135 return;
[email protected]481edc52011-03-22 06:36:58136 uint32 size;
vitalybuka5d1290582014-09-12 09:19:59137 std::vector<char> data;
[email protected]481edc52011-03-22 06:36:58138 {
[email protected]d91db112011-10-18 20:58:51139 Emf emf;
[email protected]481edc52011-03-22 06:36:58140 EXPECT_TRUE(emf.Init());
141 EXPECT_TRUE(emf.context() != NULL);
142 int pages = 3;
143 while (pages) {
[email protected]39892b92011-04-30 02:24:44144 EXPECT_TRUE(emf.StartPage(gfx::Size(), gfx::Rect(), 1));
[email protected]481edc52011-03-22 06:36:58145 ::Rectangle(emf.context(), 10, 10, 190, 190);
146 EXPECT_TRUE(emf.FinishPage());
147 --pages;
148 }
[email protected]830cf742011-04-01 16:06:25149 EXPECT_EQ(3U, emf.GetPageCount());
[email protected]481edc52011-03-22 06:36:58150 EXPECT_TRUE(emf.FinishDocument());
151 size = emf.GetDataSize();
[email protected]96fddd82011-03-24 23:37:45152 EXPECT_TRUE(emf.GetDataAsVector(&data));
[email protected]481edc52011-03-22 06:36:58153 EXPECT_EQ(data.size(), size);
154 }
[email protected]2aa8e182010-07-12 16:25:04155
156 // Playback the data.
157 DOCINFO di = {0};
158 di.cbSize = sizeof(DOCINFO);
159 di.lpszDocName = L"Test Job";
[email protected]6f0db2b2012-02-14 03:56:18160 int job_id = ::StartDoc(dc.Get(), &di);
[email protected]d91db112011-10-18 20:58:51161 Emf emf;
[email protected]89eff96a2011-03-17 23:22:06162 EXPECT_TRUE(emf.InitFromData(&data.front(), size));
[email protected]6f0db2b2012-02-14 03:56:18163 EXPECT_TRUE(emf.SafePlayback(dc.Get()));
164 ::EndDoc(dc.Get());
[email protected]2aa8e182010-07-12 16:25:04165 // Since presumably the printer is not real, let us just delete the job from
166 // the queue.
167 HANDLE printer = NULL;
[email protected]59149922014-07-04 22:08:04168 if (::OpenPrinter(const_cast<LPTSTR>(L"UnitTest Printer"), &printer, NULL)) {
[email protected]2aa8e182010-07-12 16:25:04169 ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE);
170 ClosePrinter(printer);
171 }
172}
173
[email protected]481edc52011-03-22 06:36:58174TEST(EmfTest, FileBackedEmf) {
[email protected]e6cddc572010-09-29 21:39:45175 // Simplest use case.
[email protected]ea1a3f62012-11-16 20:34:23176 base::ScopedTempDir scratch_metafile_dir;
[email protected]f21104d2010-10-08 20:57:57177 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir());
[email protected]79f63882013-02-10 05:15:45178 base::FilePath metafile_path;
[email protected]03d9afc02013-12-03 17:55:52179 EXPECT_TRUE(base::CreateTemporaryFileInDir(scratch_metafile_dir.path(),
180 &metafile_path));
[email protected]481edc52011-03-22 06:36:58181 uint32 size;
vitalybuka5d1290582014-09-12 09:19:59182 std::vector<char> data;
[email protected]481edc52011-03-22 06:36:58183 {
[email protected]d91db112011-10-18 20:58:51184 Emf emf;
[email protected]481edc52011-03-22 06:36:58185 EXPECT_TRUE(emf.InitToFile(metafile_path));
186 EXPECT_TRUE(emf.context() != NULL);
187 // An empty EMF is invalid, so we put at least a rectangle in it.
188 ::Rectangle(emf.context(), 10, 10, 190, 190);
189 EXPECT_TRUE(emf.FinishDocument());
190 size = emf.GetDataSize();
191 EXPECT_GT(size, EMF_HEADER_SIZE);
[email protected]96fddd82011-03-24 23:37:45192 EXPECT_TRUE(emf.GetDataAsVector(&data));
[email protected]481edc52011-03-22 06:36:58193 EXPECT_EQ(data.size(), size);
194 }
[email protected]e6cddc572010-09-29 21:39:45195 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:49196 base::GetFileSize(metafile_path, &file_size);
[email protected]e6cddc572010-09-29 21:39:45197 EXPECT_EQ(size, file_size);
[email protected]e6cddc572010-09-29 21:39:45198
199 // Playback the data.
[email protected]481edc52011-03-22 06:36:58200 HDC hdc = CreateCompatibleDC(NULL);
[email protected]e6cddc572010-09-29 21:39:45201 EXPECT_TRUE(hdc);
[email protected]d91db112011-10-18 20:58:51202 Emf emf;
[email protected]481edc52011-03-22 06:36:58203 EXPECT_TRUE(emf.InitFromFile(metafile_path));
[email protected]e6cddc572010-09-29 21:39:45204 RECT output_rect = {0, 0, 10, 10};
205 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
206 EXPECT_TRUE(DeleteDC(hdc));
207}
208
[email protected]60d77bd2012-08-22 00:10:07209TEST(EmfTest, RasterizeMetafile) {
210 Emf emf;
211 EXPECT_TRUE(emf.Init());
212 EXPECT_TRUE(emf.context() != NULL);
213 HBRUSH brush = static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
214 for (int i = 0; i < 4; ++i) {
215 RECT rect = { 5 + i, 5 + i, 5 + i + 1, 5 + i + 2};
216 FillRect(emf.context(), &rect, brush);
217 }
218 EXPECT_TRUE(emf.FinishDocument());
219
220 scoped_ptr<Emf> raster(emf.RasterizeMetafile(1));
221 // Just 1px bitmap but should be stretched to the same bounds.
222 EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1));
223
vitalybuka5d1290582014-09-12 09:19:59224 raster = emf.RasterizeMetafile(20);
[email protected]60d77bd2012-08-22 00:10:07225 EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1));
226
vitalybuka5d1290582014-09-12 09:19:59227 raster = emf.RasterizeMetafile(16 * 1024 * 1024);
[email protected]60d77bd2012-08-22 00:10:07228 // Expected size about 64MB.
vitalybuka5d1290582014-09-12 09:19:59229 EXPECT_LE(abs(int(raster->GetDataSize()) - 64 * 1024 * 1024), 1024 * 1024);
[email protected]60d77bd2012-08-22 00:10:07230 // Bounds should still be the same.
231 EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1));
232}
233
[email protected]5ad76172011-03-02 19:25:17234} // namespace printing