blob: a8ff6741740a1b6890912a2adf01b823a26bf1f2 [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.
avi126e93c2015-12-21 21:48:168#include <stdint.h>
initial.commit09911bf2008-07-26 23:55:299#include <wingdi.h>
[email protected]2aa8e182010-07-12 16:25:0410#include <winspool.h>
initial.commit09911bf2008-07-26 23:55:2911
dchengc3df9ba2016-04-07 23:09:3212#include <memory>
[email protected]83deae22011-10-07 05:56:3213#include <string>
Vladislav Kuzkokov19998222019-08-12 14:26:0914#include <utility>
[email protected]83deae22011-10-07 05:56:3215
[email protected]57999812013-02-24 05:40:5216#include "base/files/file_path.h"
thestig22dfc4012014-09-05 08:29:4417#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2318#include "base/files/scoped_temp_dir.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/path_service.h"
[email protected]0142bd4f2010-12-31 01:02:4720#include "base/win/scoped_hdc.h"
Morten Stenshorneeebf2db2020-05-25 09:00:0521#include "printing/mojom/print.mojom.h"
[email protected]daee4972009-07-09 14:28:2422#include "printing/printing_context.h"
thestig5f683912016-09-30 22:42:1623#include "printing/printing_context_win.h"
initial.commit09911bf2008-07-26 23:55:2924#include "testing/gtest/include/gtest/gtest.h"
tfarina655f81d2014-12-23 02:38:5025#include "ui/gfx/geometry/point.h"
tfarinaebe974f02015-01-03 04:25:3226#include "ui/gfx/geometry/size.h"
initial.commit09911bf2008-07-26 23:55:2927
Vitaly Bukabd7c9812014-08-26 08:57:5428namespace printing {
29
initial.commit09911bf2008-07-26 23:55:2930namespace {
31
32// This test is automatically disabled if no printer named "UnitTest Printer" is
33// available.
Vitaly Bukabd7c9812014-08-26 08:57:5434class EmfPrintingTest : public testing::Test, public PrintingContext::Delegate {
initial.commit09911bf2008-07-26 23:55:2935 public:
36 typedef testing::Test Parent;
37 static bool IsTestCaseDisabled() {
38 // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700.
thestig5f683912016-09-30 22:42:1639 HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", nullptr, nullptr);
initial.commit09911bf2008-07-26 23:55:2940 if (!hdc)
41 return true;
42 DeleteDC(hdc);
43 return false;
44 }
Vitaly Bukabd7c9812014-08-26 08:57:5445
46 // PrintingContext::Delegate methods.
thestig5f683912016-09-30 22:42:1647 gfx::NativeView GetParentView() override { return nullptr; }
nickbc761612015-04-27 23:30:4648 std::string GetAppLocale() override { return std::string(); }
initial.commit09911bf2008-07-26 23:55:2949};
50
thestig707a24b22015-09-14 18:16:3351const uint32_t EMF_HEADER_SIZE = 128;
[email protected]e6cddc572010-09-29 21:39:4552
initial.commit09911bf2008-07-26 23:55:2953} // namespace
54
55TEST(EmfTest, DC) {
initial.commit09911bf2008-07-26 23:55:2956 // Simplest use case.
vitalybuka5d1290582014-09-12 09:19:5957 std::vector<char> data;
[email protected]481edc52011-03-22 06:36:5858 {
[email protected]d91db112011-10-18 20:58:5159 Emf emf;
[email protected]481edc52011-03-22 06:36:5860 EXPECT_TRUE(emf.Init());
thestig192677ec2016-06-09 07:43:1761 EXPECT_TRUE(emf.context());
[email protected]481edc52011-03-22 06:36:5862 // An empty EMF is invalid, so we put at least a rectangle in it.
63 ::Rectangle(emf.context(), 10, 10, 190, 190);
64 EXPECT_TRUE(emf.FinishDocument());
Lei Zhangb4ef0d5d2020-03-27 23:18:2965 uint32_t size = emf.GetDataSize();
[email protected]481edc52011-03-22 06:36:5866 EXPECT_GT(size, EMF_HEADER_SIZE);
[email protected]96fddd82011-03-24 23:37:4567 EXPECT_TRUE(emf.GetDataAsVector(&data));
Lei Zhangb4ef0d5d2020-03-27 23:18:2968 ASSERT_EQ(data.size(), size);
[email protected]481edc52011-03-22 06:36:5869 }
initial.commit09911bf2008-07-26 23:55:2970
71 // Playback the data.
[email protected]d91db112011-10-18 20:58:5172 Emf emf;
Lei Zhangb4ef0d5d2020-03-27 23:18:2973 // TODO(thestig): Make |data| uint8_t and avoid the base::as_bytes() call.
74 EXPECT_TRUE(emf.InitFromData(base::as_bytes(base::make_span(data))));
thestig5f683912016-09-30 22:42:1675 HDC hdc = CreateCompatibleDC(nullptr);
[email protected]481edc52011-03-22 06:36:5876 EXPECT_TRUE(hdc);
initial.commit09911bf2008-07-26 23:55:2977 RECT output_rect = {0, 0, 10, 10};
78 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
79 EXPECT_TRUE(DeleteDC(hdc));
80}
81
initial.commit09911bf2008-07-26 23:55:2982// Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
83TEST_F(EmfPrintingTest, Enumerate) {
84 if (IsTestCaseDisabled())
85 return;
86
Vladislav Kuzkokov19998222019-08-12 14:26:0987 auto settings = std::make_unique<PrintSettings>();
initial.commit09911bf2008-07-26 23:55:2988
89 // My test case is a HP Color LaserJet 4550 PCL.
Vladislav Kuzkokov19998222019-08-12 14:26:0990 settings->set_device_name(L"UnitTest Printer");
initial.commit09911bf2008-07-26 23:55:2991
92 // Initialize it.
thestig5f683912016-09-30 22:42:1693 PrintingContextWin context(this);
Vladislav Kuzkokov19998222019-08-12 14:26:0994 EXPECT_EQ(PrintingContext::OK,
95 context.InitWithSettingsForTest(std::move(settings)));
initial.commit09911bf2008-07-26 23:55:2996
[email protected]79f63882013-02-10 05:15:4597 base::FilePath emf_file;
Avi Drissmanea15ea02018-05-07 18:55:1298 EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &emf_file));
[email protected]daee4972009-07-09 14:28:2499 emf_file = emf_file.Append(FILE_PATH_LITERAL("printing"))
Lei Zhang01a1d3c02019-05-21 04:59:10100 .Append(FILE_PATH_LITERAL("test"))
101 .Append(FILE_PATH_LITERAL("data"))
Daniel Hosseinian347944d2020-04-01 02:18:37102 .Append(FILE_PATH_LITERAL("emf"))
Lei Zhang01a1d3c02019-05-21 04:59:10103 .Append(FILE_PATH_LITERAL("test4.emf"));
thestig6b4461f2016-10-28 19:34:30104
initial.commit09911bf2008-07-26 23:55:29105 // Load any EMF with an image.
initial.commit09911bf2008-07-26 23:55:29106 std::string emf_data;
[email protected]82f84b92013-08-30 18:23:50107 base::ReadFileToString(emf_file, &emf_data);
initial.commit09911bf2008-07-26 23:55:29108 ASSERT_TRUE(emf_data.size());
thestig6b4461f2016-10-28 19:34:30109
110 Emf emf;
Lei Zhangb4ef0d5d2020-03-27 23:18:29111 EXPECT_TRUE(emf.InitFromData(base::as_bytes(base::make_span(emf_data))));
initial.commit09911bf2008-07-26 23:55:29112
113 // This will print to file. The reason is that when running inside a
[email protected]d91db112011-10-18 20:58:51114 // unit_test, PrintingContext automatically dumps its files to the
initial.commit09911bf2008-07-26 23:55:29115 // current directory.
116 // TODO(maruel): Clean the .PRN file generated in current directory.
thestig5f683912016-09-30 22:42:16117 context.NewDocument(L"EmfTest.Enumerate");
118 context.NewPage();
initial.commit09911bf2008-07-26 23:55:29119 // Process one at a time.
[email protected]59149922014-07-04 22:08:04120 RECT page_bounds = emf.GetPageBounds(1).ToRECT();
thestig5f683912016-09-30 22:42:16121 Emf::Enumerator emf_enum(emf, context.context(), &page_bounds);
[email protected]d91db112011-10-18 20:58:51122 for (Emf::Enumerator::const_iterator itr = emf_enum.begin();
Lei Zhang01a1d3c02019-05-21 04:59:10123 itr != emf_enum.end(); ++itr) {
initial.commit09911bf2008-07-26 23:55:29124 // To help debugging.
125 ptrdiff_t index = itr - emf_enum.begin();
126 // If you get this assert, you need to lookup iType in wingdi.h. It starts
127 // with EMR_HEADER.
128 EMR_HEADER;
Lei Zhang01a1d3c02019-05-21 04:59:10129 EXPECT_TRUE(itr->SafePlayback(&emf_enum.context_))
130 << " index: " << index << " type: " << itr->record()->iType;
initial.commit09911bf2008-07-26 23:55:29131 }
thestig5f683912016-09-30 22:42:16132 context.PageDone();
133 context.DocumentDone();
initial.commit09911bf2008-07-26 23:55:29134}
[email protected]daee4972009-07-09 14:28:24135
[email protected]2aa8e182010-07-12 16:25:04136// Disabled if no "UnitTest printer" exists.
137TEST_F(EmfPrintingTest, PageBreak) {
[email protected]83deae22011-10-07 05:56:32138 base::win::ScopedCreateDC dc(
thestig5f683912016-09-30 22:42:16139 CreateDC(L"WINSPOOL", L"UnitTest Printer", nullptr, nullptr));
[email protected]6f0db2b2012-02-14 03:56:18140 if (!dc.Get())
[email protected]2aa8e182010-07-12 16:25:04141 return;
vitalybuka5d1290582014-09-12 09:19:59142 std::vector<char> data;
[email protected]481edc52011-03-22 06:36:58143 {
[email protected]d91db112011-10-18 20:58:51144 Emf emf;
[email protected]481edc52011-03-22 06:36:58145 EXPECT_TRUE(emf.Init());
thestig192677ec2016-06-09 07:43:17146 EXPECT_TRUE(emf.context());
[email protected]481edc52011-03-22 06:36:58147 int pages = 3;
148 while (pages) {
Morten Stenshorneeebf2db2020-05-25 09:00:05149 emf.StartPage(gfx::Size(), gfx::Rect(), 1,
150 mojom::PageOrientation::kUpright);
[email protected]481edc52011-03-22 06:36:58151 ::Rectangle(emf.context(), 10, 10, 190, 190);
152 EXPECT_TRUE(emf.FinishPage());
153 --pages;
154 }
[email protected]830cf742011-04-01 16:06:25155 EXPECT_EQ(3U, emf.GetPageCount());
[email protected]481edc52011-03-22 06:36:58156 EXPECT_TRUE(emf.FinishDocument());
Lei Zhangb4ef0d5d2020-03-27 23:18:29157 uint32_t size = emf.GetDataSize();
[email protected]96fddd82011-03-24 23:37:45158 EXPECT_TRUE(emf.GetDataAsVector(&data));
Lei Zhangb4ef0d5d2020-03-27 23:18:29159 ASSERT_EQ(data.size(), size);
[email protected]481edc52011-03-22 06:36:58160 }
[email protected]2aa8e182010-07-12 16:25:04161
162 // Playback the data.
163 DOCINFO di = {0};
164 di.cbSize = sizeof(DOCINFO);
165 di.lpszDocName = L"Test Job";
[email protected]6f0db2b2012-02-14 03:56:18166 int job_id = ::StartDoc(dc.Get(), &di);
[email protected]d91db112011-10-18 20:58:51167 Emf emf;
Lei Zhangb4ef0d5d2020-03-27 23:18:29168 // TODO(thestig): Make |data| uint8_t and avoid the base::as_bytes() call.
169 EXPECT_TRUE(emf.InitFromData(base::as_bytes(base::make_span(data))));
[email protected]6f0db2b2012-02-14 03:56:18170 EXPECT_TRUE(emf.SafePlayback(dc.Get()));
171 ::EndDoc(dc.Get());
[email protected]2aa8e182010-07-12 16:25:04172 // Since presumably the printer is not real, let us just delete the job from
173 // the queue.
thestig5f683912016-09-30 22:42:16174 HANDLE printer = nullptr;
175 if (::OpenPrinter(const_cast<LPTSTR>(L"UnitTest Printer"), &printer,
176 nullptr)) {
177 ::SetJob(printer, job_id, 0, nullptr, JOB_CONTROL_DELETE);
[email protected]2aa8e182010-07-12 16:25:04178 ClosePrinter(printer);
179 }
180}
181
[email protected]481edc52011-03-22 06:36:58182TEST(EmfTest, FileBackedEmf) {
[email protected]e6cddc572010-09-29 21:39:45183 // Simplest use case.
[email protected]ea1a3f62012-11-16 20:34:23184 base::ScopedTempDir scratch_metafile_dir;
[email protected]f21104d2010-10-08 20:57:57185 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir());
[email protected]79f63882013-02-10 05:15:45186 base::FilePath metafile_path;
vabr16e5f602016-09-15 18:14:00187 EXPECT_TRUE(base::CreateTemporaryFileInDir(scratch_metafile_dir.GetPath(),
[email protected]03d9afc02013-12-03 17:55:52188 &metafile_path));
thestig707a24b22015-09-14 18:16:33189 uint32_t size;
vitalybuka5d1290582014-09-12 09:19:59190 std::vector<char> data;
[email protected]481edc52011-03-22 06:36:58191 {
[email protected]d91db112011-10-18 20:58:51192 Emf emf;
[email protected]481edc52011-03-22 06:36:58193 EXPECT_TRUE(emf.InitToFile(metafile_path));
thestig192677ec2016-06-09 07:43:17194 EXPECT_TRUE(emf.context());
[email protected]481edc52011-03-22 06:36:58195 // An empty EMF is invalid, so we put at least a rectangle in it.
196 ::Rectangle(emf.context(), 10, 10, 190, 190);
197 EXPECT_TRUE(emf.FinishDocument());
198 size = emf.GetDataSize();
199 EXPECT_GT(size, EMF_HEADER_SIZE);
[email protected]96fddd82011-03-24 23:37:45200 EXPECT_TRUE(emf.GetDataAsVector(&data));
[email protected]481edc52011-03-22 06:36:58201 EXPECT_EQ(data.size(), size);
202 }
thestig707a24b22015-09-14 18:16:33203 int64_t file_size = 0;
[email protected]56285702013-12-04 18:22:49204 base::GetFileSize(metafile_path, &file_size);
[email protected]e6cddc572010-09-29 21:39:45205 EXPECT_EQ(size, file_size);
[email protected]e6cddc572010-09-29 21:39:45206
207 // Playback the data.
thestig5f683912016-09-30 22:42:16208 HDC hdc = CreateCompatibleDC(nullptr);
[email protected]e6cddc572010-09-29 21:39:45209 EXPECT_TRUE(hdc);
[email protected]d91db112011-10-18 20:58:51210 Emf emf;
[email protected]481edc52011-03-22 06:36:58211 EXPECT_TRUE(emf.InitFromFile(metafile_path));
[email protected]e6cddc572010-09-29 21:39:45212 RECT output_rect = {0, 0, 10, 10};
213 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
214 EXPECT_TRUE(DeleteDC(hdc));
215}
216
[email protected]5ad76172011-03-02 19:25:17217} // namespace printing