OLD | NEW |
| (Empty) |
1 // 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. | |
4 | |
5 #include "chrome/common/gfx/emf.h" | |
6 | |
7 // For quick access. | |
8 #include <wingdi.h> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/file_util.h" | |
12 #include "base/path_service.h" | |
13 #include "chrome/browser/printing/win_printing_context.h" | |
14 #include "chrome/common/chrome_paths.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace { | |
18 | |
19 // This test is automatically disabled if no printer named "UnitTest Printer" is | |
20 // available. | |
21 class 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 | |
36 TEST(EmfTest, DC) { | |
37 static const int EMF_HEADER_SIZE = 128; | |
38 | |
39 // Simplest use case. | |
40 gfx::Emf emf; | |
41 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 | |
65 | |
66 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. | |
67 TEST_F(EmfPrintingTest, Enumerate) { | |
68 if (IsTestCaseDisabled()) | |
69 return; | |
70 | |
71 printing::PrintSettings settings; | |
72 | |
73 // My test case is a HP Color LaserJet 4550 PCL. | |
74 settings.set_device_name(L"UnitTest Printer"); | |
75 | |
76 // Initialize it. | |
77 printing::PrintingContext context; | |
78 EXPECT_EQ(context.InitWithSettings(settings), printing::PrintingContext::OK); | |
79 | |
80 std::wstring test_file; | |
81 PathService::Get(chrome::DIR_TEST_DATA, &test_file); | |
82 | |
83 // Load any EMF with an image. | |
84 gfx::Emf emf; | |
85 file_util::AppendToPath(&test_file, L"printing"); | |
86 file_util::AppendToPath(&test_file, L"test4.emf"); | |
87 std::string emf_data; | |
88 file_util::ReadFileToString(test_file, &emf_data); | |
89 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. | |
99 gfx::Emf::Enumerator emf_enum(emf, context.context(), | |
100 &emf.GetBounds().ToRECT()); | |
101 for (gfx::Emf::Enumerator::const_iterator itr = emf_enum.begin(); | |
102 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 } | |
OLD | NEW |