OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/gfx/emf.h" | 5 #include "printing/emf_win.h" |
6 | 6 |
7 // For quick access. | 7 // For quick access. |
8 #include <wingdi.h> | 8 #include <wingdi.h> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/path_service.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" | 13 #include "testing/gtest/include/gtest/gtest.h" |
16 | 14 |
17 namespace { | 15 namespace { |
18 | 16 |
19 // This test is automatically disabled if no printer named "UnitTest Printer" is | 17 // This test is automatically disabled if no printer named "UnitTest Printer" is |
20 // available. | 18 // available. |
21 class EmfPrintingTest : public testing::Test { | 19 class EmfPrintingTest : public testing::Test { |
22 public: | 20 public: |
23 typedef testing::Test Parent; | 21 typedef testing::Test Parent; |
24 static bool IsTestCaseDisabled() { | 22 static bool IsTestCaseDisabled() { |
25 // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700. | 23 // 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); | 24 HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL); |
27 if (!hdc) | 25 if (!hdc) |
28 return true; | 26 return true; |
29 DeleteDC(hdc); | 27 DeleteDC(hdc); |
30 return false; | 28 return false; |
31 } | 29 } |
32 }; | 30 }; |
33 | 31 |
34 } // namespace | 32 } // namespace |
35 | 33 |
36 TEST(EmfTest, DC) { | 34 TEST(EmfTest, DC) { |
37 static const int EMF_HEADER_SIZE = 128; | 35 static const int EMF_HEADER_SIZE = 128; |
38 | 36 |
39 // Simplest use case. | 37 // Simplest use case. |
40 gfx::Emf emf; | 38 printing::Emf emf; |
41 RECT rect = {100, 100, 200, 200}; | 39 RECT rect = {100, 100, 200, 200}; |
42 HDC hdc = CreateCompatibleDC(NULL); | 40 HDC hdc = CreateCompatibleDC(NULL); |
43 EXPECT_TRUE(hdc != NULL); | 41 EXPECT_TRUE(hdc != NULL); |
44 EXPECT_TRUE(emf.CreateDc(hdc, &rect)); | 42 EXPECT_TRUE(emf.CreateDc(hdc, &rect)); |
45 EXPECT_TRUE(emf.hdc() != NULL); | 43 EXPECT_TRUE(emf.hdc() != NULL); |
46 // In theory, you'd use the HDC with GDI functions here. | 44 // In theory, you'd use the HDC with GDI functions here. |
47 EXPECT_TRUE(emf.CloseDc()); | 45 EXPECT_TRUE(emf.CloseDc()); |
48 unsigned size = emf.GetDataSize(); | 46 unsigned size = emf.GetDataSize(); |
49 EXPECT_EQ(size, EMF_HEADER_SIZE); | 47 EXPECT_EQ(size, EMF_HEADER_SIZE); |
50 std::vector<BYTE> data; | 48 std::vector<BYTE> data; |
51 EXPECT_TRUE(emf.GetData(&data)); | 49 EXPECT_TRUE(emf.GetData(&data)); |
52 EXPECT_EQ(data.size(), size); | 50 EXPECT_EQ(data.size(), size); |
53 emf.CloseEmf(); | 51 emf.CloseEmf(); |
54 EXPECT_TRUE(DeleteDC(hdc)); | 52 EXPECT_TRUE(DeleteDC(hdc)); |
55 | 53 |
56 // Playback the data. | 54 // Playback the data. |
57 hdc = CreateCompatibleDC(NULL); | 55 hdc = CreateCompatibleDC(NULL); |
58 EXPECT_TRUE(hdc); | 56 EXPECT_TRUE(hdc); |
59 EXPECT_TRUE(emf.CreateFromData(&data.front(), size)); | 57 EXPECT_TRUE(emf.CreateFromData(&data.front(), size)); |
60 RECT output_rect = {0, 0, 10, 10}; | 58 RECT output_rect = {0, 0, 10, 10}; |
61 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); | 59 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
62 EXPECT_TRUE(DeleteDC(hdc)); | 60 EXPECT_TRUE(DeleteDC(hdc)); |
63 } | 61 } |
64 | 62 |
| 63 // TODO(sverrir): Re-enable after win_printing_context has been moved here. |
| 64 /* |
| 65 #include "chrome/browser/printing/win_printing_context.h" |
| 66 #include "chrome/common/chrome_paths.h" |
65 | 67 |
66 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. | 68 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. |
67 TEST_F(EmfPrintingTest, Enumerate) { | 69 TEST_F(EmfPrintingTest, Enumerate) { |
68 if (IsTestCaseDisabled()) | 70 if (IsTestCaseDisabled()) |
69 return; | 71 return; |
70 | 72 |
71 printing::PrintSettings settings; | 73 printing::PrintSettings settings; |
72 | 74 |
73 // My test case is a HP Color LaserJet 4550 PCL. | 75 // My test case is a HP Color LaserJet 4550 PCL. |
74 settings.set_device_name(L"UnitTest Printer"); | 76 settings.set_device_name(L"UnitTest Printer"); |
75 | 77 |
76 // Initialize it. | 78 // Initialize it. |
77 printing::PrintingContext context; | 79 printing::PrintingContext context; |
78 EXPECT_EQ(context.InitWithSettings(settings), printing::PrintingContext::OK); | 80 EXPECT_EQ(context.InitWithSettings(settings), printing::PrintingContext::OK); |
79 | 81 |
80 std::wstring test_file; | 82 std::wstring test_file; |
81 PathService::Get(chrome::DIR_TEST_DATA, &test_file); | 83 PathService::Get(chrome::DIR_TEST_DATA, &test_file); |
82 | 84 |
83 // Load any EMF with an image. | 85 // Load any EMF with an image. |
84 gfx::Emf emf; | 86 printing::Emf emf; |
85 file_util::AppendToPath(&test_file, L"printing"); | 87 file_util::AppendToPath(&test_file, L"printing"); |
86 file_util::AppendToPath(&test_file, L"test4.emf"); | 88 file_util::AppendToPath(&test_file, L"test4.emf"); |
87 std::string emf_data; | 89 std::string emf_data; |
88 file_util::ReadFileToString(test_file, &emf_data); | 90 file_util::ReadFileToString(test_file, &emf_data); |
89 ASSERT_TRUE(emf_data.size()); | 91 ASSERT_TRUE(emf_data.size()); |
90 EXPECT_TRUE(emf.CreateFromData(&emf_data[0], emf_data.size())); | 92 EXPECT_TRUE(emf.CreateFromData(&emf_data[0], emf_data.size())); |
91 | 93 |
92 // This will print to file. The reason is that when running inside a | 94 // This will print to file. The reason is that when running inside a |
93 // unit_test, printing::PrintingContext automatically dumps its files to the | 95 // unit_test, printing::PrintingContext automatically dumps its files to the |
94 // current directory. | 96 // current directory. |
95 // TODO(maruel): Clean the .PRN file generated in current directory. | 97 // TODO(maruel): Clean the .PRN file generated in current directory. |
96 context.NewDocument(L"EmfTest.Enumerate"); | 98 context.NewDocument(L"EmfTest.Enumerate"); |
97 context.NewPage(); | 99 context.NewPage(); |
98 // Process one at a time. | 100 // Process one at a time. |
99 gfx::Emf::Enumerator emf_enum(emf, context.context(), | 101 printing::Emf::Enumerator emf_enum(emf, context.context(), |
100 &emf.GetBounds().ToRECT()); | 102 &emf.GetBounds().ToRECT()); |
101 for (gfx::Emf::Enumerator::const_iterator itr = emf_enum.begin(); | 103 for (printing::Emf::Enumerator::const_iterator itr = emf_enum.begin(); |
102 itr != emf_enum.end(); | 104 itr != emf_enum.end(); |
103 ++itr) { | 105 ++itr) { |
104 // To help debugging. | 106 // To help debugging. |
105 ptrdiff_t index = itr - emf_enum.begin(); | 107 ptrdiff_t index = itr - emf_enum.begin(); |
106 // If you get this assert, you need to lookup iType in wingdi.h. It starts | 108 // If you get this assert, you need to lookup iType in wingdi.h. It starts |
107 // with EMR_HEADER. | 109 // with EMR_HEADER. |
108 EMR_HEADER; | 110 EMR_HEADER; |
109 EXPECT_TRUE(itr->SafePlayback(NULL)) << | 111 EXPECT_TRUE(itr->SafePlayback(NULL)) << |
110 " index: " << index << " type: " << itr->record()->iType; | 112 " index: " << index << " type: " << itr->record()->iType; |
111 } | 113 } |
112 context.PageDone(); | 114 context.PageDone(); |
113 context.DocumentDone(); | 115 context.DocumentDone(); |
114 } | 116 } |
| 117 */ |
OLD | NEW |