1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "printing/emf_win.h"
// For quick access.
#include <stdint.h>
#include <wingdi.h>
#include <winspool.h>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/win/scoped_hdc.h"
#include "printing/mojom/print.mojom.h"
#include "printing/printing_context.h"
#include "printing/printing_context_win.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/size.h"
namespace printing {
namespace {
// This test is automatically disabled if no printer named "UnitTest Printer" is
// available.
class EmfPrintingTest : public testing::Test, public PrintingContext::Delegate {
public:
typedef testing::Test Parent;
static bool IsTestCaseDisabled() {
// It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700.
HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", nullptr, nullptr);
if (!hdc)
return true;
DeleteDC(hdc);
return false;
}
// PrintingContext::Delegate methods.
gfx::NativeView GetParentView() override { return gfx::NativeView(); }
std::string GetAppLocale() override { return std::string(); }
};
const uint32_t EMF_HEADER_SIZE = 128;
} // namespace
TEST(EmfTest, DC) {
// Simplest use case.
std::vector<char> data;
{
Emf emf;
EXPECT_TRUE(emf.Init());
EXPECT_TRUE(emf.context());
// An empty EMF is invalid, so we put at least a rectangle in it.
::Rectangle(emf.context(), 10, 10, 190, 190);
EXPECT_TRUE(emf.FinishDocument());
uint32_t size = emf.GetDataSize();
EXPECT_GT(size, EMF_HEADER_SIZE);
EXPECT_TRUE(emf.GetDataAsVector(&data));
ASSERT_EQ(data.size(), size);
}
// Playback the data.
Emf emf;
// TODO(thestig): Make `data` uint8_t and avoid the base::as_byte_span() call.
EXPECT_TRUE(emf.InitFromData(base::as_byte_span(data)));
HDC hdc = CreateCompatibleDC(nullptr);
EXPECT_TRUE(hdc);
RECT output_rect = {0, 0, 10, 10};
EXPECT_TRUE(emf.Playback(hdc, &output_rect));
EXPECT_TRUE(DeleteDC(hdc));
}
// Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
TEST_F(EmfPrintingTest, Enumerate) {
if (IsTestCaseDisabled())
return;
auto settings = std::make_unique<PrintSettings>();
// My test case is a HP Color LaserJet 4550 PCL.
settings->set_device_name(u"UnitTest Printer");
// Initialize it.
PrintingContextWin context(this,
PrintingContext::OutOfProcessBehavior::kDisabled);
EXPECT_EQ(mojom::ResultCode::kSuccess,
context.InitWithSettingsForTest(std::move(settings)));
base::FilePath emf_file;
EXPECT_TRUE(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &emf_file));
emf_file = emf_file.Append(FILE_PATH_LITERAL("printing"))
.Append(FILE_PATH_LITERAL("test"))
.Append(FILE_PATH_LITERAL("data"))
.Append(FILE_PATH_LITERAL("emf"))
.Append(FILE_PATH_LITERAL("test4.emf"));
// Load any EMF with an image.
std::string emf_data;
base::ReadFileToString(emf_file, &emf_data);
ASSERT_TRUE(emf_data.size());
Emf emf;
EXPECT_TRUE(emf.InitFromData(base::as_byte_span(emf_data)));
// This will print to file. The reason is that when running inside a
// unit_test, PrintingContext automatically dumps its files to the
// current directory.
// TODO(maruel): Clean the .PRN file generated in current directory.
context.NewDocument(u"EmfTest.Enumerate");
// Process one at a time.
RECT page_bounds = emf.GetPageBounds(1).ToRECT();
Emf::Enumerator emf_enum(emf, context.context(), &page_bounds);
for (Emf::Enumerator::const_iterator itr = emf_enum.begin();
itr != emf_enum.end(); ++itr) {
// To help debugging.
ptrdiff_t index = itr - emf_enum.begin();
// If you get this assert, you need to lookup iType in wingdi.h. It starts
// with EMR_HEADER.
EMR_HEADER;
EXPECT_TRUE(itr->SafePlayback(&emf_enum.context_))
<< " index: " << index << " type: " << itr->record()->iType;
}
context.DocumentDone();
}
// Disabled if no "UnitTest printer" exists.
TEST_F(EmfPrintingTest, PageBreak) {
base::win::ScopedCreateDC dc(
CreateDC(L"WINSPOOL", L"UnitTest Printer", nullptr, nullptr));
if (!dc.Get())
return;
std::vector<char> data;
{
Emf emf;
EXPECT_TRUE(emf.Init());
EXPECT_TRUE(emf.context());
int pages = 3;
while (pages) {
emf.StartPage(gfx::Size(), gfx::Rect(), 1,
mojom::PageOrientation::kUpright);
::Rectangle(emf.context(), 10, 10, 190, 190);
EXPECT_TRUE(emf.FinishPage());
--pages;
}
EXPECT_EQ(3U, emf.GetPageCount());
EXPECT_TRUE(emf.FinishDocument());
uint32_t size = emf.GetDataSize();
EXPECT_TRUE(emf.GetDataAsVector(&data));
ASSERT_EQ(data.size(), size);
}
// Playback the data.
DOCINFO di = {0};
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = L"Test Job";
int job_id = ::StartDoc(dc.Get(), &di);
Emf emf;
// TODO(thestig): Make `data` uint8_t and avoid the base::as_byte_span() call.
EXPECT_TRUE(emf.InitFromData(base::as_byte_span(data)));
EXPECT_TRUE(emf.SafePlayback(dc.Get()));
::EndDoc(dc.Get());
// Since presumably the printer is not real, let us just delete the job from
// the queue.
HANDLE printer = nullptr;
if (::OpenPrinter(const_cast<LPTSTR>(L"UnitTest Printer"), &printer,
nullptr)) {
::SetJob(printer, job_id, 0, nullptr, JOB_CONTROL_DELETE);
ClosePrinter(printer);
}
}
TEST(EmfTest, FileBackedEmf) {
// Simplest use case.
base::ScopedTempDir scratch_metafile_dir;
ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir());
base::FilePath metafile_path;
EXPECT_TRUE(base::CreateTemporaryFileInDir(scratch_metafile_dir.GetPath(),
&metafile_path));
uint32_t size;
std::vector<char> data;
{
Emf emf;
EXPECT_TRUE(emf.InitToFile(metafile_path));
EXPECT_TRUE(emf.context());
// An empty EMF is invalid, so we put at least a rectangle in it.
::Rectangle(emf.context(), 10, 10, 190, 190);
EXPECT_TRUE(emf.FinishDocument());
size = emf.GetDataSize();
EXPECT_GT(size, EMF_HEADER_SIZE);
EXPECT_TRUE(emf.GetDataAsVector(&data));
EXPECT_EQ(data.size(), size);
}
std::optional<int64_t> file_size = base::GetFileSize(metafile_path);
ASSERT_TRUE(file_size.has_value());
EXPECT_EQ(size, file_size.value());
// Playback the data.
HDC hdc = CreateCompatibleDC(nullptr);
EXPECT_TRUE(hdc);
Emf emf;
EXPECT_TRUE(emf.InitFromFile(metafile_path));
RECT output_rect = {0, 0, 10, 10};
EXPECT_TRUE(emf.Playback(hdc, &output_rect));
EXPECT_TRUE(DeleteDC(hdc));
}
} // namespace printing
|