| OLD | NEW |
| 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 27 matching lines...) Expand all Loading... |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 const uint32 EMF_HEADER_SIZE = 128; | 40 const uint32 EMF_HEADER_SIZE = 128; |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 namespace printing { | 44 namespace printing { |
| 45 | 45 |
| 46 TEST(EmfTest, DC) { | 46 TEST(EmfTest, DC) { |
| 47 // Simplest use case. | 47 // Simplest use case. |
| 48 printing::Emf emf; | 48 uint32 size; |
| 49 RECT rect = {100, 100, 200, 200}; | |
| 50 HDC hdc = CreateCompatibleDC(NULL); | |
| 51 EXPECT_TRUE(hdc != NULL); | |
| 52 EXPECT_TRUE(emf.CreateDc(hdc, &rect)); | |
| 53 EXPECT_TRUE(emf.context() != NULL); | |
| 54 // In theory, you'd use the HDC with GDI functions here. | |
| 55 EXPECT_TRUE(emf.FinishDocument()); | |
| 56 uint32 size = emf.GetDataSize(); | |
| 57 EXPECT_EQ(size, EMF_HEADER_SIZE); | |
| 58 std::vector<BYTE> data; | 49 std::vector<BYTE> data; |
| 59 EXPECT_TRUE(emf.GetData(&data)); | 50 { |
| 60 EXPECT_EQ(data.size(), size); | 51 printing::Emf emf; |
| 61 emf.CloseEmf(); | 52 EXPECT_TRUE(emf.Init()); |
| 62 EXPECT_TRUE(DeleteDC(hdc)); | 53 EXPECT_TRUE(emf.context() != NULL); |
| 54 // An empty EMF is invalid, so we put at least a rectangle in it. |
| 55 ::Rectangle(emf.context(), 10, 10, 190, 190); |
| 56 EXPECT_TRUE(emf.FinishDocument()); |
| 57 size = emf.GetDataSize(); |
| 58 EXPECT_GT(size, EMF_HEADER_SIZE); |
| 59 EXPECT_TRUE(emf.GetData(&data)); |
| 60 EXPECT_EQ(data.size(), size); |
| 61 } |
| 63 | 62 |
| 64 // Playback the data. | 63 // Playback the data. |
| 65 hdc = CreateCompatibleDC(NULL); | 64 printing::Emf emf; |
| 65 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); |
| 66 HDC hdc = CreateCompatibleDC(NULL); |
| 66 EXPECT_TRUE(hdc); | 67 EXPECT_TRUE(hdc); |
| 67 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); | |
| 68 RECT output_rect = {0, 0, 10, 10}; | 68 RECT output_rect = {0, 0, 10, 10}; |
| 69 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); | 69 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 70 EXPECT_TRUE(DeleteDC(hdc)); | 70 EXPECT_TRUE(DeleteDC(hdc)); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. | 73 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. |
| 74 TEST_F(EmfPrintingTest, Enumerate) { | 74 TEST_F(EmfPrintingTest, Enumerate) { |
| 75 if (IsTestCaseDisabled()) | 75 if (IsTestCaseDisabled()) |
| 76 return; | 76 return; |
| 77 | 77 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 context->PageDone(); | 121 context->PageDone(); |
| 122 context->DocumentDone(); | 122 context->DocumentDone(); |
| 123 } | 123 } |
| 124 | 124 |
| 125 // Disabled if no "UnitTest printer" exists. | 125 // Disabled if no "UnitTest printer" exists. |
| 126 TEST_F(EmfPrintingTest, PageBreak) { | 126 TEST_F(EmfPrintingTest, PageBreak) { |
| 127 base::win::ScopedHDC dc( | 127 base::win::ScopedHDC dc( |
| 128 CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); | 128 CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); |
| 129 if (!dc.Get()) | 129 if (!dc.Get()) |
| 130 return; | 130 return; |
| 131 printing::Emf emf; | 131 uint32 size; |
| 132 EXPECT_TRUE(emf.CreateDc(dc.Get(), NULL)); | 132 std::vector<BYTE> data; |
| 133 EXPECT_TRUE(emf.context() != NULL); | 133 { |
| 134 int pages = 3; | 134 printing::Emf emf; |
| 135 while (pages) { | 135 EXPECT_TRUE(emf.Init()); |
| 136 EXPECT_TRUE(emf.StartPage(gfx::Size(), gfx::Point(), 1)); | 136 EXPECT_TRUE(emf.context() != NULL); |
| 137 ::Rectangle(emf.context(), 10, 10, 190, 190); | 137 int pages = 3; |
| 138 EXPECT_TRUE(emf.FinishPage()); | 138 while (pages) { |
| 139 --pages; | 139 EXPECT_TRUE(emf.StartPage(gfx::Size(), gfx::Point(), 1)); |
| 140 ::Rectangle(emf.context(), 10, 10, 190, 190); |
| 141 EXPECT_TRUE(emf.FinishPage()); |
| 142 --pages; |
| 143 } |
| 144 EXPECT_TRUE(emf.FinishDocument()); |
| 145 size = emf.GetDataSize(); |
| 146 EXPECT_TRUE(emf.GetData(&data)); |
| 147 EXPECT_EQ(data.size(), size); |
| 140 } | 148 } |
| 141 EXPECT_TRUE(emf.FinishDocument()); | |
| 142 uint32 size = emf.GetDataSize(); | |
| 143 std::vector<BYTE> data; | |
| 144 EXPECT_TRUE(emf.GetData(&data)); | |
| 145 EXPECT_EQ(data.size(), size); | |
| 146 emf.CloseEmf(); | |
| 147 | 149 |
| 148 // Playback the data. | 150 // Playback the data. |
| 149 DOCINFO di = {0}; | 151 DOCINFO di = {0}; |
| 150 di.cbSize = sizeof(DOCINFO); | 152 di.cbSize = sizeof(DOCINFO); |
| 151 di.lpszDocName = L"Test Job"; | 153 di.lpszDocName = L"Test Job"; |
| 152 int job_id = ::StartDoc(dc.Get(), &di); | 154 int job_id = ::StartDoc(dc.Get(), &di); |
| 155 printing::Emf emf; |
| 153 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); | 156 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); |
| 154 EXPECT_TRUE(emf.SafePlayback(dc.Get())); | 157 EXPECT_TRUE(emf.SafePlayback(dc.Get())); |
| 155 ::EndDoc(dc.Get()); | 158 ::EndDoc(dc.Get()); |
| 156 // Since presumably the printer is not real, let us just delete the job from | 159 // Since presumably the printer is not real, let us just delete the job from |
| 157 // the queue. | 160 // the queue. |
| 158 HANDLE printer = NULL; | 161 HANDLE printer = NULL; |
| 159 if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) { | 162 if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) { |
| 160 ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE); | 163 ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE); |
| 161 ClosePrinter(printer); | 164 ClosePrinter(printer); |
| 162 } | 165 } |
| 163 } | 166 } |
| 164 | 167 |
| 165 TEST(EmfTest, FileBackedDC) { | 168 TEST(EmfTest, FileBackedEmf) { |
| 166 // Simplest use case. | 169 // Simplest use case. |
| 167 printing::Emf emf; | |
| 168 RECT rect = {100, 100, 200, 200}; | |
| 169 HDC hdc = CreateCompatibleDC(NULL); | |
| 170 EXPECT_TRUE(hdc != NULL); | |
| 171 ScopedTempDir scratch_metafile_dir; | 170 ScopedTempDir scratch_metafile_dir; |
| 172 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir()); | 171 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir()); |
| 173 FilePath metafile_path; | 172 FilePath metafile_path; |
| 174 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir.path(), | 173 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir.path(), |
| 175 &metafile_path)); | 174 &metafile_path)); |
| 176 EXPECT_TRUE(emf.CreateFileBackedDc(hdc, &rect, metafile_path)); | 175 uint32 size; |
| 177 EXPECT_TRUE(emf.context() != NULL); | |
| 178 // In theory, you'd use the HDC with GDI functions here. | |
| 179 EXPECT_TRUE(emf.FinishDocument()); | |
| 180 | |
| 181 uint32 size = emf.GetDataSize(); | |
| 182 EXPECT_EQ(size, EMF_HEADER_SIZE); | |
| 183 std::vector<BYTE> data; | 176 std::vector<BYTE> data; |
| 184 EXPECT_TRUE(emf.GetData(&data)); | 177 { |
| 185 EXPECT_EQ(data.size(), size); | 178 printing::Emf emf; |
| 186 emf.CloseEmf(); | 179 EXPECT_TRUE(emf.InitToFile(metafile_path)); |
| 180 EXPECT_TRUE(emf.context() != NULL); |
| 181 // An empty EMF is invalid, so we put at least a rectangle in it. |
| 182 ::Rectangle(emf.context(), 10, 10, 190, 190); |
| 183 EXPECT_TRUE(emf.FinishDocument()); |
| 184 size = emf.GetDataSize(); |
| 185 EXPECT_GT(size, EMF_HEADER_SIZE); |
| 186 EXPECT_TRUE(emf.GetData(&data)); |
| 187 EXPECT_EQ(data.size(), size); |
| 188 } |
| 187 int64 file_size = 0; | 189 int64 file_size = 0; |
| 188 file_util::GetFileSize(metafile_path, &file_size); | 190 file_util::GetFileSize(metafile_path, &file_size); |
| 189 EXPECT_EQ(size, file_size); | 191 EXPECT_EQ(size, file_size); |
| 190 EXPECT_TRUE(DeleteDC(hdc)); | |
| 191 | 192 |
| 192 // Playback the data. | 193 // Playback the data. |
| 193 hdc = CreateCompatibleDC(NULL); | 194 HDC hdc = CreateCompatibleDC(NULL); |
| 194 EXPECT_TRUE(hdc); | 195 EXPECT_TRUE(hdc); |
| 195 EXPECT_TRUE(emf.CreateFromFile(metafile_path)); | 196 printing::Emf emf; |
| 197 EXPECT_TRUE(emf.InitFromFile(metafile_path)); |
| 196 RECT output_rect = {0, 0, 10, 10}; | 198 RECT output_rect = {0, 0, 10, 10}; |
| 197 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); | 199 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 198 EXPECT_TRUE(DeleteDC(hdc)); | 200 EXPECT_TRUE(DeleteDC(hdc)); |
| 199 emf.CloseEmf(); | |
| 200 } | 201 } |
| 201 | 202 |
| 202 } // namespace printing | 203 } // namespace printing |
| OLD | NEW |