Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: printing/emf_win_unittest.cc

Issue 6695013: Cleanup NativeMetafile (win) interface and EMF class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Typo Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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
(...skipping 25 matching lines...) Expand all
36 }; 36 };
37 37
38 const uint32 EMF_HEADER_SIZE = 128; 38 const uint32 EMF_HEADER_SIZE = 128;
39 39
40 } // namespace 40 } // namespace
41 41
42 namespace printing { 42 namespace printing {
43 43
44 TEST(EmfTest, DC) { 44 TEST(EmfTest, DC) {
45 // Simplest use case. 45 // Simplest use case.
46 printing::Emf emf; 46 uint32 size;
47 RECT rect = {100, 100, 200, 200};
48 HDC hdc = CreateCompatibleDC(NULL);
49 EXPECT_TRUE(hdc != NULL);
50 EXPECT_TRUE(emf.CreateDc(hdc, &rect));
51 EXPECT_TRUE(emf.context() != NULL);
52 // In theory, you'd use the HDC with GDI functions here.
53 EXPECT_TRUE(emf.Close());
54 uint32 size = emf.GetDataSize();
55 EXPECT_EQ(size, EMF_HEADER_SIZE);
56 std::vector<BYTE> data; 47 std::vector<BYTE> data;
57 EXPECT_TRUE(emf.GetData(&data)); 48 {
58 EXPECT_EQ(data.size(), size); 49 printing::Emf emf;
59 emf.CloseEmf(); 50 EXPECT_TRUE(emf.Init());
60 EXPECT_TRUE(DeleteDC(hdc)); 51 EXPECT_TRUE(emf.context() != NULL);
52 // An empty EMF is invalid, so we put at least a rectangle in it.
53 ::Rectangle(emf.context(), 10, 10, 190, 190);
54 EXPECT_TRUE(emf.Close());
55 size = emf.GetDataSize();
56 EXPECT_GT(size, EMF_HEADER_SIZE);
57 EXPECT_TRUE(emf.GetData(&data));
58 EXPECT_EQ(data.size(), size);
59 }
61 60
62 // Playback the data. 61 // Playback the data.
63 hdc = CreateCompatibleDC(NULL); 62 printing::Emf emf;
63 EXPECT_TRUE(emf.Init(&data.front(), size));
64 HDC hdc = CreateCompatibleDC(NULL);
64 EXPECT_TRUE(hdc); 65 EXPECT_TRUE(hdc);
65 EXPECT_TRUE(emf.Init(&data.front(), size));
66 RECT output_rect = {0, 0, 10, 10}; 66 RECT output_rect = {0, 0, 10, 10};
67 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); 67 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
68 EXPECT_TRUE(DeleteDC(hdc)); 68 EXPECT_TRUE(DeleteDC(hdc));
69 } 69 }
70 70
71 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. 71 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598.
72 TEST_F(EmfPrintingTest, Enumerate) { 72 TEST_F(EmfPrintingTest, Enumerate) {
73 if (IsTestCaseDisabled()) 73 if (IsTestCaseDisabled())
74 return; 74 return;
75 75
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 context->PageDone(); 119 context->PageDone();
120 context->DocumentDone(); 120 context->DocumentDone();
121 } 121 }
122 122
123 // Disabled if no "UnitTest printer" exists. 123 // Disabled if no "UnitTest printer" exists.
124 TEST_F(EmfPrintingTest, PageBreak) { 124 TEST_F(EmfPrintingTest, PageBreak) {
125 base::win::ScopedHDC dc( 125 base::win::ScopedHDC dc(
126 CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); 126 CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL));
127 if (!dc.Get()) 127 if (!dc.Get())
128 return; 128 return;
129 printing::Emf emf; 129 uint32 size;
130 EXPECT_TRUE(emf.CreateDc(dc.Get(), NULL)); 130 std::vector<BYTE> data;
131 EXPECT_TRUE(emf.context() != NULL); 131 {
132 int pages = 3; 132 printing::Emf emf;
133 while (pages) { 133 EXPECT_TRUE(emf.Init());
134 EXPECT_TRUE(emf.StartPage()); 134 EXPECT_TRUE(emf.context() != NULL);
135 ::Rectangle(emf.context(), 10, 10, 190, 190); 135 int pages = 3;
136 EXPECT_TRUE(emf.FinishPage()); 136 while (pages) {
137 --pages; 137 EXPECT_TRUE(emf.StartPage());
138 ::Rectangle(emf.context(), 10, 10, 190, 190);
139 EXPECT_TRUE(emf.FinishPage());
140 --pages;
141 }
142 EXPECT_TRUE(emf.Close());
143 size = emf.GetDataSize();
144 EXPECT_TRUE(emf.GetData(&data));
145 EXPECT_EQ(data.size(), size);
138 } 146 }
139 EXPECT_TRUE(emf.Close());
140 uint32 size = emf.GetDataSize();
141 std::vector<BYTE> data;
142 EXPECT_TRUE(emf.GetData(&data));
143 EXPECT_EQ(data.size(), size);
144 emf.CloseEmf();
145 147
146 // Playback the data. 148 // Playback the data.
147 DOCINFO di = {0}; 149 DOCINFO di = {0};
148 di.cbSize = sizeof(DOCINFO); 150 di.cbSize = sizeof(DOCINFO);
149 di.lpszDocName = L"Test Job"; 151 di.lpszDocName = L"Test Job";
150 int job_id = ::StartDoc(dc.Get(), &di); 152 int job_id = ::StartDoc(dc.Get(), &di);
153 printing::Emf emf;
151 EXPECT_TRUE(emf.Init(&data.front(), size)); 154 EXPECT_TRUE(emf.Init(&data.front(), size));
152 EXPECT_TRUE(emf.SafePlayback(dc.Get())); 155 EXPECT_TRUE(emf.SafePlayback(dc.Get()));
153 ::EndDoc(dc.Get()); 156 ::EndDoc(dc.Get());
154 // Since presumably the printer is not real, let us just delete the job from 157 // Since presumably the printer is not real, let us just delete the job from
155 // the queue. 158 // the queue.
156 HANDLE printer = NULL; 159 HANDLE printer = NULL;
157 if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) { 160 if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) {
158 ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE); 161 ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE);
159 ClosePrinter(printer); 162 ClosePrinter(printer);
160 } 163 }
161 } 164 }
162 165
163 TEST(EmfTest, FileBackedDC) {
164 // Simplest use case.
165 printing::Emf emf;
166 RECT rect = {100, 100, 200, 200};
167 HDC hdc = CreateCompatibleDC(NULL);
168 EXPECT_TRUE(hdc != NULL);
169 ScopedTempDir scratch_metafile_dir;
170 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir());
171 FilePath metafile_path;
172 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir.path(),
173 &metafile_path));
174 EXPECT_TRUE(emf.CreateFileBackedDc(hdc, &rect, metafile_path));
175 EXPECT_TRUE(emf.context() != NULL);
176 // In theory, you'd use the HDC with GDI functions here.
177 EXPECT_TRUE(emf.Close());
178
179 uint32 size = emf.GetDataSize();
180 EXPECT_EQ(size, EMF_HEADER_SIZE);
181 std::vector<BYTE> data;
182 EXPECT_TRUE(emf.GetData(&data));
183 EXPECT_EQ(data.size(), size);
184 emf.CloseEmf();
185 int64 file_size = 0;
186 file_util::GetFileSize(metafile_path, &file_size);
187 EXPECT_EQ(size, file_size);
188 EXPECT_TRUE(DeleteDC(hdc));
189
190 // Playback the data.
191 hdc = CreateCompatibleDC(NULL);
192 EXPECT_TRUE(hdc);
193 EXPECT_TRUE(emf.CreateFromFile(metafile_path));
194 RECT output_rect = {0, 0, 10, 10};
195 EXPECT_TRUE(emf.Playback(hdc, &output_rect));
196 EXPECT_TRUE(DeleteDC(hdc));
197 emf.CloseEmf();
198 }
199
200 } // namespace printing 166 } // namespace printing
OLDNEW
« printing/emf_win.h ('K') | « printing/emf_win.cc ('k') | printing/native_metafile.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698