blob: add23f9ddd07c42f93fa6f6eb0e1984dea5acdd3 [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
[email protected]83deae22011-10-07 05:56:3211#include <string>
12
initial.commit09911bf2008-07-26 23:55:2913#include "base/basictypes.h"
[email protected]daee4972009-07-09 14:28:2414#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:2915#include "base/file_util.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2917#include "base/path_service.h"
[email protected]e0785902011-05-19 23:34:1718#include "base/scoped_temp_dir.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"
[email protected]edc531f92011-03-18 17:52:2322#include "ui/gfx/point.h"
23#include "ui/gfx/size.h"
initial.commit09911bf2008-07-26 23:55:2924
25namespace {
26
27// This test is automatically disabled if no printer named "UnitTest Printer" is
28// available.
29class EmfPrintingTest : public testing::Test {
30 public:
31 typedef testing::Test Parent;
32 static bool IsTestCaseDisabled() {
33 // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700.
34 HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL);
35 if (!hdc)
36 return true;
37 DeleteDC(hdc);
38 return false;
39 }
40};
41
[email protected]e6cddc572010-09-29 21:39:4542const uint32 EMF_HEADER_SIZE = 128;
43
initial.commit09911bf2008-07-26 23:55:2944} // namespace
45
[email protected]5ad76172011-03-02 19:25:1746namespace printing {
47
initial.commit09911bf2008-07-26 23:55:2948TEST(EmfTest, DC) {
initial.commit09911bf2008-07-26 23:55:2949 // Simplest use case.
[email protected]481edc52011-03-22 06:36:5850 uint32 size;
initial.commit09911bf2008-07-26 23:55:2951 std::vector<BYTE> data;
[email protected]481edc52011-03-22 06:36:5852 {
[email protected]d91db112011-10-18 20:58:5153 Emf emf;
[email protected]481edc52011-03-22 06:36:5854 EXPECT_TRUE(emf.Init());
55 EXPECT_TRUE(emf.context() != NULL);
56 // An empty EMF is invalid, so we put at least a rectangle in it.
57 ::Rectangle(emf.context(), 10, 10, 190, 190);
58 EXPECT_TRUE(emf.FinishDocument());
59 size = emf.GetDataSize();
60 EXPECT_GT(size, EMF_HEADER_SIZE);
[email protected]96fddd82011-03-24 23:37:4561 EXPECT_TRUE(emf.GetDataAsVector(&data));
[email protected]481edc52011-03-22 06:36:5862 EXPECT_EQ(data.size(), size);
63 }
initial.commit09911bf2008-07-26 23:55:2964
65 // Playback the data.
[email protected]d91db112011-10-18 20:58:5166 Emf emf;
[email protected]f2da4b02011-03-18 05:45:5167 EXPECT_TRUE(emf.InitFromData(&data.front(), size));
[email protected]481edc52011-03-22 06:36:5868 HDC hdc = CreateCompatibleDC(NULL);
69 EXPECT_TRUE(hdc);
initial.commit09911bf2008-07-26 23:55:2970 RECT output_rect = {0, 0, 10, 10};
71 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
72 EXPECT_TRUE(DeleteDC(hdc));
73}
74
initial.commit09911bf2008-07-26 23:55:2975// Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
76TEST_F(EmfPrintingTest, Enumerate) {
77 if (IsTestCaseDisabled())
78 return;
79
[email protected]d91db112011-10-18 20:58:5180 PrintSettings settings;
initial.commit09911bf2008-07-26 23:55:2981
82 // My test case is a HP Color LaserJet 4550 PCL.
83 settings.set_device_name(L"UnitTest Printer");
84
85 // Initialize it.
[email protected]d91db112011-10-18 20:58:5186 scoped_ptr<PrintingContext> context(PrintingContext::Create(std::string()));
87 EXPECT_EQ(context->InitWithSettings(settings), PrintingContext::OK);
initial.commit09911bf2008-07-26 23:55:2988
[email protected]daee4972009-07-09 14:28:2489 FilePath emf_file;
90 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file));
91 emf_file = emf_file.Append(FILE_PATH_LITERAL("printing"))
92 .Append(FILE_PATH_LITERAL("test"))
93 .Append(FILE_PATH_LITERAL("data"))
94 .Append(FILE_PATH_LITERAL("test4.emf"));
initial.commit09911bf2008-07-26 23:55:2995 // Load any EMF with an image.
[email protected]d91db112011-10-18 20:58:5196 Emf emf;
initial.commit09911bf2008-07-26 23:55:2997 std::string emf_data;
[email protected]daee4972009-07-09 14:28:2498 file_util::ReadFileToString(emf_file, &emf_data);
initial.commit09911bf2008-07-26 23:55:2999 ASSERT_TRUE(emf_data.size());
[email protected]89eff96a2011-03-17 23:22:06100 EXPECT_TRUE(emf.InitFromData(&emf_data[0], emf_data.size()));
initial.commit09911bf2008-07-26 23:55:29101
102 // This will print to file. The reason is that when running inside a
[email protected]d91db112011-10-18 20:58:51103 // unit_test, PrintingContext automatically dumps its files to the
initial.commit09911bf2008-07-26 23:55:29104 // current directory.
105 // TODO(maruel): Clean the .PRN file generated in current directory.
[email protected]51e8d9352010-10-06 22:21:17106 context->NewDocument(L"EmfTest.Enumerate");
107 context->NewPage();
initial.commit09911bf2008-07-26 23:55:29108 // Process one at a time.
[email protected]d91db112011-10-18 20:58:51109 Emf::Enumerator emf_enum(emf, context->context(),
110 &emf.GetPageBounds(1).ToRECT());
111 for (Emf::Enumerator::const_iterator itr = emf_enum.begin();
initial.commit09911bf2008-07-26 23:55:29112 itr != emf_enum.end();
113 ++itr) {
114 // To help debugging.
115 ptrdiff_t index = itr - emf_enum.begin();
116 // If you get this assert, you need to lookup iType in wingdi.h. It starts
117 // with EMR_HEADER.
118 EMR_HEADER;
119 EXPECT_TRUE(itr->SafePlayback(NULL)) <<
120 " index: " << index << " type: " << itr->record()->iType;
121 }
[email protected]51e8d9352010-10-06 22:21:17122 context->PageDone();
123 context->DocumentDone();
initial.commit09911bf2008-07-26 23:55:29124}
[email protected]daee4972009-07-09 14:28:24125
[email protected]2aa8e182010-07-12 16:25:04126// Disabled if no "UnitTest printer" exists.
127TEST_F(EmfPrintingTest, PageBreak) {
[email protected]83deae22011-10-07 05:56:32128 base::win::ScopedCreateDC dc(
[email protected]0142bd4f2010-12-31 01:02:47129 CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL));
[email protected]2aa8e182010-07-12 16:25:04130 if (!dc.Get())
131 return;
[email protected]481edc52011-03-22 06:36:58132 uint32 size;
[email protected]f2da4b02011-03-18 05:45:51133 std::vector<BYTE> data;
[email protected]481edc52011-03-22 06:36:58134 {
[email protected]d91db112011-10-18 20:58:51135 Emf emf;
[email protected]481edc52011-03-22 06:36:58136 EXPECT_TRUE(emf.Init());
137 EXPECT_TRUE(emf.context() != NULL);
138 int pages = 3;
139 while (pages) {
[email protected]39892b92011-04-30 02:24:44140 EXPECT_TRUE(emf.StartPage(gfx::Size(), gfx::Rect(), 1));
[email protected]481edc52011-03-22 06:36:58141 ::Rectangle(emf.context(), 10, 10, 190, 190);
142 EXPECT_TRUE(emf.FinishPage());
143 --pages;
144 }
[email protected]830cf742011-04-01 16:06:25145 EXPECT_EQ(3U, emf.GetPageCount());
[email protected]481edc52011-03-22 06:36:58146 EXPECT_TRUE(emf.FinishDocument());
147 size = emf.GetDataSize();
[email protected]96fddd82011-03-24 23:37:45148 EXPECT_TRUE(emf.GetDataAsVector(&data));
[email protected]481edc52011-03-22 06:36:58149 EXPECT_EQ(data.size(), size);
150 }
[email protected]2aa8e182010-07-12 16:25:04151
152 // Playback the data.
153 DOCINFO di = {0};
154 di.cbSize = sizeof(DOCINFO);
155 di.lpszDocName = L"Test Job";
156 int job_id = ::StartDoc(dc.Get(), &di);
[email protected]d91db112011-10-18 20:58:51157 Emf emf;
[email protected]89eff96a2011-03-17 23:22:06158 EXPECT_TRUE(emf.InitFromData(&data.front(), size));
[email protected]2aa8e182010-07-12 16:25:04159 EXPECT_TRUE(emf.SafePlayback(dc.Get()));
160 ::EndDoc(dc.Get());
161 // Since presumably the printer is not real, let us just delete the job from
162 // the queue.
163 HANDLE printer = NULL;
164 if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) {
165 ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE);
166 ClosePrinter(printer);
167 }
168}
169
[email protected]481edc52011-03-22 06:36:58170TEST(EmfTest, FileBackedEmf) {
[email protected]e6cddc572010-09-29 21:39:45171 // Simplest use case.
[email protected]f21104d2010-10-08 20:57:57172 ScopedTempDir scratch_metafile_dir;
173 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir());
[email protected]e6cddc572010-09-29 21:39:45174 FilePath metafile_path;
[email protected]f21104d2010-10-08 20:57:57175 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir.path(),
176 &metafile_path));
[email protected]481edc52011-03-22 06:36:58177 uint32 size;
[email protected]e6cddc572010-09-29 21:39:45178 std::vector<BYTE> data;
[email protected]481edc52011-03-22 06:36:58179 {
[email protected]d91db112011-10-18 20:58:51180 Emf emf;
[email protected]481edc52011-03-22 06:36:58181 EXPECT_TRUE(emf.InitToFile(metafile_path));
182 EXPECT_TRUE(emf.context() != NULL);
183 // An empty EMF is invalid, so we put at least a rectangle in it.
184 ::Rectangle(emf.context(), 10, 10, 190, 190);
185 EXPECT_TRUE(emf.FinishDocument());
186 size = emf.GetDataSize();
187 EXPECT_GT(size, EMF_HEADER_SIZE);
[email protected]96fddd82011-03-24 23:37:45188 EXPECT_TRUE(emf.GetDataAsVector(&data));
[email protected]481edc52011-03-22 06:36:58189 EXPECT_EQ(data.size(), size);
190 }
[email protected]e6cddc572010-09-29 21:39:45191 int64 file_size = 0;
192 file_util::GetFileSize(metafile_path, &file_size);
193 EXPECT_EQ(size, file_size);
[email protected]e6cddc572010-09-29 21:39:45194
195 // Playback the data.
[email protected]481edc52011-03-22 06:36:58196 HDC hdc = CreateCompatibleDC(NULL);
[email protected]e6cddc572010-09-29 21:39:45197 EXPECT_TRUE(hdc);
[email protected]d91db112011-10-18 20:58:51198 Emf emf;
[email protected]481edc52011-03-22 06:36:58199 EXPECT_TRUE(emf.InitFromFile(metafile_path));
[email protected]e6cddc572010-09-29 21:39:45200 RECT output_rect = {0, 0, 10, 10};
201 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
202 EXPECT_TRUE(DeleteDC(hdc));
203}
204
[email protected]5ad76172011-03-02 19:25:17205} // namespace printing