blob: c3846f35ef189b37afbd9246c0725055a5aac11d [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>
9
10#include "base/basictypes.h"
[email protected]daee4972009-07-09 14:28:2411#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/path_service.h"
[email protected]daee4972009-07-09 14:28:2414#include "printing/printing_context.h"
initial.commit09911bf2008-07-26 23:55:2915#include "testing/gtest/include/gtest/gtest.h"
16
17namespace {
18
19// This test is automatically disabled if no printer named "UnitTest Printer" is
20// available.
21class EmfPrintingTest : public testing::Test {
22 public:
23 typedef testing::Test Parent;
24 static bool IsTestCaseDisabled() {
25 // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700.
26 HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL);
27 if (!hdc)
28 return true;
29 DeleteDC(hdc);
30 return false;
31 }
32};
33
34} // namespace
35
36TEST(EmfTest, DC) {
37 static const int EMF_HEADER_SIZE = 128;
38
39 // Simplest use case.
[email protected]0e0fca32009-07-06 15:25:5040 printing::Emf emf;
initial.commit09911bf2008-07-26 23:55:2941 RECT rect = {100, 100, 200, 200};
42 HDC hdc = CreateCompatibleDC(NULL);
43 EXPECT_TRUE(hdc != NULL);
44 EXPECT_TRUE(emf.CreateDc(hdc, &rect));
45 EXPECT_TRUE(emf.hdc() != NULL);
46 // In theory, you'd use the HDC with GDI functions here.
47 EXPECT_TRUE(emf.CloseDc());
48 unsigned size = emf.GetDataSize();
49 EXPECT_EQ(size, EMF_HEADER_SIZE);
50 std::vector<BYTE> data;
51 EXPECT_TRUE(emf.GetData(&data));
52 EXPECT_EQ(data.size(), size);
53 emf.CloseEmf();
54 EXPECT_TRUE(DeleteDC(hdc));
55
56 // Playback the data.
57 hdc = CreateCompatibleDC(NULL);
58 EXPECT_TRUE(hdc);
59 EXPECT_TRUE(emf.CreateFromData(&data.front(), size));
60 RECT output_rect = {0, 0, 10, 10};
61 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
62 EXPECT_TRUE(DeleteDC(hdc));
63}
64
initial.commit09911bf2008-07-26 23:55:2965// Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
66TEST_F(EmfPrintingTest, Enumerate) {
67 if (IsTestCaseDisabled())
68 return;
69
70 printing::PrintSettings settings;
71
72 // My test case is a HP Color LaserJet 4550 PCL.
73 settings.set_device_name(L"UnitTest Printer");
74
75 // Initialize it.
76 printing::PrintingContext context;
77 EXPECT_EQ(context.InitWithSettings(settings), printing::PrintingContext::OK);
78
[email protected]daee4972009-07-09 14:28:2479 FilePath emf_file;
80 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file));
81 emf_file = emf_file.Append(FILE_PATH_LITERAL("printing"))
82 .Append(FILE_PATH_LITERAL("test"))
83 .Append(FILE_PATH_LITERAL("data"))
84 .Append(FILE_PATH_LITERAL("test4.emf"));
initial.commit09911bf2008-07-26 23:55:2985 // Load any EMF with an image.
[email protected]0e0fca32009-07-06 15:25:5086 printing::Emf emf;
initial.commit09911bf2008-07-26 23:55:2987 std::string emf_data;
[email protected]daee4972009-07-09 14:28:2488 file_util::ReadFileToString(emf_file, &emf_data);
initial.commit09911bf2008-07-26 23:55:2989 ASSERT_TRUE(emf_data.size());
90 EXPECT_TRUE(emf.CreateFromData(&emf_data[0], emf_data.size()));
91
92 // This will print to file. The reason is that when running inside a
93 // unit_test, printing::PrintingContext automatically dumps its files to the
94 // current directory.
95 // TODO(maruel): Clean the .PRN file generated in current directory.
96 context.NewDocument(L"EmfTest.Enumerate");
97 context.NewPage();
98 // Process one at a time.
[email protected]0e0fca32009-07-06 15:25:5099 printing::Emf::Enumerator emf_enum(emf, context.context(),
initial.commit09911bf2008-07-26 23:55:29100 &emf.GetBounds().ToRECT());
[email protected]0e0fca32009-07-06 15:25:50101 for (printing::Emf::Enumerator::const_iterator itr = emf_enum.begin();
initial.commit09911bf2008-07-26 23:55:29102 itr != emf_enum.end();
103 ++itr) {
104 // To help debugging.
105 ptrdiff_t index = itr - emf_enum.begin();
106 // If you get this assert, you need to lookup iType in wingdi.h. It starts
107 // with EMR_HEADER.
108 EMR_HEADER;
109 EXPECT_TRUE(itr->SafePlayback(NULL)) <<
110 " index: " << index << " type: " << itr->record()->iType;
111 }
112 context.PageDone();
113 context.DocumentDone();
114}
[email protected]daee4972009-07-09 14:28:24115