blob: 3b4c473b4a4c5233fbf9c06bc6862de1d3479459 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// 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
11#include "base/basictypes.h"
[email protected]daee4972009-07-09 14:28:2412#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/path_service.h"
[email protected]2aa8e182010-07-12 16:25:0415#include "base/scoped_handle_win.h"
[email protected]daee4972009-07-09 14:28:2416#include "printing/printing_context.h"
initial.commit09911bf2008-07-26 23:55:2917#include "testing/gtest/include/gtest/gtest.h"
18
19namespace {
20
21// This test is automatically disabled if no printer named "UnitTest Printer" is
22// available.
23class EmfPrintingTest : public testing::Test {
24 public:
25 typedef testing::Test Parent;
26 static bool IsTestCaseDisabled() {
27 // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700.
28 HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL);
29 if (!hdc)
30 return true;
31 DeleteDC(hdc);
32 return false;
33 }
34};
35
36} // namespace
37
38TEST(EmfTest, DC) {
[email protected]b5ab3982010-02-16 23:58:2739 static const uint32 EMF_HEADER_SIZE = 128;
initial.commit09911bf2008-07-26 23:55:2940
41 // Simplest use case.
[email protected]0e0fca32009-07-06 15:25:5042 printing::Emf emf;
initial.commit09911bf2008-07-26 23:55:2943 RECT rect = {100, 100, 200, 200};
44 HDC hdc = CreateCompatibleDC(NULL);
45 EXPECT_TRUE(hdc != NULL);
46 EXPECT_TRUE(emf.CreateDc(hdc, &rect));
47 EXPECT_TRUE(emf.hdc() != NULL);
48 // In theory, you'd use the HDC with GDI functions here.
49 EXPECT_TRUE(emf.CloseDc());
[email protected]b5ab3982010-02-16 23:58:2750 uint32 size = emf.GetDataSize();
initial.commit09911bf2008-07-26 23:55:2951 EXPECT_EQ(size, EMF_HEADER_SIZE);
52 std::vector<BYTE> data;
53 EXPECT_TRUE(emf.GetData(&data));
54 EXPECT_EQ(data.size(), size);
55 emf.CloseEmf();
56 EXPECT_TRUE(DeleteDC(hdc));
57
58 // Playback the data.
59 hdc = CreateCompatibleDC(NULL);
60 EXPECT_TRUE(hdc);
61 EXPECT_TRUE(emf.CreateFromData(&data.front(), size));
62 RECT output_rect = {0, 0, 10, 10};
63 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
64 EXPECT_TRUE(DeleteDC(hdc));
65}
66
initial.commit09911bf2008-07-26 23:55:2967// Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
68TEST_F(EmfPrintingTest, Enumerate) {
69 if (IsTestCaseDisabled())
70 return;
71
72 printing::PrintSettings settings;
73
74 // My test case is a HP Color LaserJet 4550 PCL.
75 settings.set_device_name(L"UnitTest Printer");
76
77 // Initialize it.
78 printing::PrintingContext context;
79 EXPECT_EQ(context.InitWithSettings(settings), printing::PrintingContext::OK);
80
[email protected]daee4972009-07-09 14:28:2481 FilePath emf_file;
82 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file));
83 emf_file = emf_file.Append(FILE_PATH_LITERAL("printing"))
84 .Append(FILE_PATH_LITERAL("test"))
85 .Append(FILE_PATH_LITERAL("data"))
86 .Append(FILE_PATH_LITERAL("test4.emf"));
initial.commit09911bf2008-07-26 23:55:2987 // Load any EMF with an image.
[email protected]0e0fca32009-07-06 15:25:5088 printing::Emf emf;
initial.commit09911bf2008-07-26 23:55:2989 std::string emf_data;
[email protected]daee4972009-07-09 14:28:2490 file_util::ReadFileToString(emf_file, &emf_data);
initial.commit09911bf2008-07-26 23:55:2991 ASSERT_TRUE(emf_data.size());
92 EXPECT_TRUE(emf.CreateFromData(&emf_data[0], emf_data.size()));
93
94 // This will print to file. The reason is that when running inside a
95 // unit_test, printing::PrintingContext automatically dumps its files to the
96 // current directory.
97 // TODO(maruel): Clean the .PRN file generated in current directory.
98 context.NewDocument(L"EmfTest.Enumerate");
99 context.NewPage();
100 // Process one at a time.
[email protected]0e0fca32009-07-06 15:25:50101 printing::Emf::Enumerator emf_enum(emf, context.context(),
initial.commit09911bf2008-07-26 23:55:29102 &emf.GetBounds().ToRECT());
[email protected]0e0fca32009-07-06 15:25:50103 for (printing::Emf::Enumerator::const_iterator itr = emf_enum.begin();
initial.commit09911bf2008-07-26 23:55:29104 itr != emf_enum.end();
105 ++itr) {
106 // To help debugging.
107 ptrdiff_t index = itr - emf_enum.begin();
108 // If you get this assert, you need to lookup iType in wingdi.h. It starts
109 // with EMR_HEADER.
110 EMR_HEADER;
111 EXPECT_TRUE(itr->SafePlayback(NULL)) <<
112 " index: " << index << " type: " << itr->record()->iType;
113 }
114 context.PageDone();
115 context.DocumentDone();
116}
[email protected]daee4972009-07-09 14:28:24117
[email protected]2aa8e182010-07-12 16:25:04118// Disabled if no "UnitTest printer" exists.
119TEST_F(EmfPrintingTest, PageBreak) {
120 ScopedHDC dc(CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL));
121 if (!dc.Get())
122 return;
123 printing::Emf emf;
124 EXPECT_TRUE(emf.CreateDc(dc.Get(), NULL));
125 EXPECT_TRUE(emf.hdc() != NULL);
126 int pages = 3;
127 while (pages) {
128 EXPECT_TRUE(emf.StartPage());
129 ::Rectangle(emf.hdc(), 10, 10, 190, 190);
130 EXPECT_TRUE(emf.EndPage());
131 --pages;
132 }
133 EXPECT_TRUE(emf.CloseDc());
134 uint32 size = emf.GetDataSize();
135 std::vector<BYTE> data;
136 EXPECT_TRUE(emf.GetData(&data));
137 EXPECT_EQ(data.size(), size);
138 emf.CloseEmf();
139
140 // Playback the data.
141 DOCINFO di = {0};
142 di.cbSize = sizeof(DOCINFO);
143 di.lpszDocName = L"Test Job";
144 int job_id = ::StartDoc(dc.Get(), &di);
145 EXPECT_TRUE(emf.CreateFromData(&data.front(), size));
146 EXPECT_TRUE(emf.SafePlayback(dc.Get()));
147 ::EndDoc(dc.Get());
148 // Since presumably the printer is not real, let us just delete the job from
149 // the queue.
150 HANDLE printer = NULL;
151 if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) {
152 ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE);
153 ClosePrinter(printer);
154 }
155}
156