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 "printing/emf_win.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 #include <winspool.h> | 9 #include <winspool.h> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/scoped_handle_win.h" | 15 #include "base/scoped_handle_win.h" |
| 16 #include "base/scoped_ptr.h" |
16 #include "printing/printing_context.h" | 17 #include "printing/printing_context.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // This test is automatically disabled if no printer named "UnitTest Printer" is | 22 // This test is automatically disabled if no printer named "UnitTest Printer" is |
22 // available. | 23 // available. |
23 class EmfPrintingTest : public testing::Test { | 24 class EmfPrintingTest : public testing::Test { |
24 public: | 25 public: |
25 typedef testing::Test Parent; | 26 typedef testing::Test Parent; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 TEST_F(EmfPrintingTest, Enumerate) { | 69 TEST_F(EmfPrintingTest, Enumerate) { |
69 if (IsTestCaseDisabled()) | 70 if (IsTestCaseDisabled()) |
70 return; | 71 return; |
71 | 72 |
72 printing::PrintSettings settings; | 73 printing::PrintSettings settings; |
73 | 74 |
74 // My test case is a HP Color LaserJet 4550 PCL. | 75 // My test case is a HP Color LaserJet 4550 PCL. |
75 settings.set_device_name(L"UnitTest Printer"); | 76 settings.set_device_name(L"UnitTest Printer"); |
76 | 77 |
77 // Initialize it. | 78 // Initialize it. |
78 printing::PrintingContext context; | 79 scoped_ptr<printing::PrintingContext> context( |
79 EXPECT_EQ(context.InitWithSettings(settings), printing::PrintingContext::OK); | 80 printing::PrintingContext::Create()); |
| 81 EXPECT_EQ(context->InitWithSettings(settings), printing::PrintingContext::OK); |
80 | 82 |
81 FilePath emf_file; | 83 FilePath emf_file; |
82 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file)); | 84 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file)); |
83 emf_file = emf_file.Append(FILE_PATH_LITERAL("printing")) | 85 emf_file = emf_file.Append(FILE_PATH_LITERAL("printing")) |
84 .Append(FILE_PATH_LITERAL("test")) | 86 .Append(FILE_PATH_LITERAL("test")) |
85 .Append(FILE_PATH_LITERAL("data")) | 87 .Append(FILE_PATH_LITERAL("data")) |
86 .Append(FILE_PATH_LITERAL("test4.emf")); | 88 .Append(FILE_PATH_LITERAL("test4.emf")); |
87 // Load any EMF with an image. | 89 // Load any EMF with an image. |
88 printing::Emf emf; | 90 printing::Emf emf; |
89 std::string emf_data; | 91 std::string emf_data; |
90 file_util::ReadFileToString(emf_file, &emf_data); | 92 file_util::ReadFileToString(emf_file, &emf_data); |
91 ASSERT_TRUE(emf_data.size()); | 93 ASSERT_TRUE(emf_data.size()); |
92 EXPECT_TRUE(emf.CreateFromData(&emf_data[0], emf_data.size())); | 94 EXPECT_TRUE(emf.CreateFromData(&emf_data[0], emf_data.size())); |
93 | 95 |
94 // This will print to file. The reason is that when running inside a | 96 // This will print to file. The reason is that when running inside a |
95 // unit_test, printing::PrintingContext automatically dumps its files to the | 97 // unit_test, printing::PrintingContext automatically dumps its files to the |
96 // current directory. | 98 // current directory. |
97 // TODO(maruel): Clean the .PRN file generated in current directory. | 99 // TODO(maruel): Clean the .PRN file generated in current directory. |
98 context.NewDocument(L"EmfTest.Enumerate"); | 100 context->NewDocument(L"EmfTest.Enumerate"); |
99 context.NewPage(); | 101 context->NewPage(); |
100 // Process one at a time. | 102 // Process one at a time. |
101 printing::Emf::Enumerator emf_enum(emf, context.context(), | 103 printing::Emf::Enumerator emf_enum(emf, context->context(), |
102 &emf.GetBounds().ToRECT()); | 104 &emf.GetBounds().ToRECT()); |
103 for (printing::Emf::Enumerator::const_iterator itr = emf_enum.begin(); | 105 for (printing::Emf::Enumerator::const_iterator itr = emf_enum.begin(); |
104 itr != emf_enum.end(); | 106 itr != emf_enum.end(); |
105 ++itr) { | 107 ++itr) { |
106 // To help debugging. | 108 // To help debugging. |
107 ptrdiff_t index = itr - emf_enum.begin(); | 109 ptrdiff_t index = itr - emf_enum.begin(); |
108 // If you get this assert, you need to lookup iType in wingdi.h. It starts | 110 // If you get this assert, you need to lookup iType in wingdi.h. It starts |
109 // with EMR_HEADER. | 111 // with EMR_HEADER. |
110 EMR_HEADER; | 112 EMR_HEADER; |
111 EXPECT_TRUE(itr->SafePlayback(NULL)) << | 113 EXPECT_TRUE(itr->SafePlayback(NULL)) << |
112 " index: " << index << " type: " << itr->record()->iType; | 114 " index: " << index << " type: " << itr->record()->iType; |
113 } | 115 } |
114 context.PageDone(); | 116 context->PageDone(); |
115 context.DocumentDone(); | 117 context->DocumentDone(); |
116 } | 118 } |
117 | 119 |
118 // Disabled if no "UnitTest printer" exists. | 120 // Disabled if no "UnitTest printer" exists. |
119 TEST_F(EmfPrintingTest, PageBreak) { | 121 TEST_F(EmfPrintingTest, PageBreak) { |
120 ScopedHDC dc(CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); | 122 ScopedHDC dc(CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); |
121 if (!dc.Get()) | 123 if (!dc.Get()) |
122 return; | 124 return; |
123 printing::Emf emf; | 125 printing::Emf emf; |
124 EXPECT_TRUE(emf.CreateDc(dc.Get(), NULL)); | 126 EXPECT_TRUE(emf.CreateDc(dc.Get(), NULL)); |
125 EXPECT_TRUE(emf.hdc() != NULL); | 127 EXPECT_TRUE(emf.hdc() != NULL); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 | 182 |
181 // Playback the data. | 183 // Playback the data. |
182 hdc = CreateCompatibleDC(NULL); | 184 hdc = CreateCompatibleDC(NULL); |
183 EXPECT_TRUE(hdc); | 185 EXPECT_TRUE(hdc); |
184 EXPECT_TRUE(emf.CreateFromFile(metafile_path)); | 186 EXPECT_TRUE(emf.CreateFromFile(metafile_path)); |
185 RECT output_rect = {0, 0, 10, 10}; | 187 RECT output_rect = {0, 0, 10, 10}; |
186 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); | 188 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
187 EXPECT_TRUE(DeleteDC(hdc)); | 189 EXPECT_TRUE(DeleteDC(hdc)); |
188 } | 190 } |
189 | 191 |
OLD | NEW |