| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stdint.h> | 8 #include <stdint.h> |
| 9 #include <wingdi.h> | 9 #include <wingdi.h> |
| 10 #include <winspool.h> | 10 #include <winspool.h> |
| 11 | 11 |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 17 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
| 18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 19 #include "base/win/scoped_hdc.h" | 19 #include "base/win/scoped_hdc.h" |
| 20 #include "printing/printing_context.h" | 20 #include "printing/printing_context.h" |
| 21 #include "printing/printing_context_win.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "ui/gfx/geometry/point.h" | 23 #include "ui/gfx/geometry/point.h" |
| 23 #include "ui/gfx/geometry/size.h" | 24 #include "ui/gfx/geometry/size.h" |
| 24 | 25 |
| 25 namespace printing { | 26 namespace printing { |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 // This test is automatically disabled if no printer named "UnitTest Printer" is | 30 // This test is automatically disabled if no printer named "UnitTest Printer" is |
| 30 // available. | 31 // available. |
| 31 class EmfPrintingTest : public testing::Test, public PrintingContext::Delegate { | 32 class EmfPrintingTest : public testing::Test, public PrintingContext::Delegate { |
| 32 public: | 33 public: |
| 33 typedef testing::Test Parent; | 34 typedef testing::Test Parent; |
| 34 static bool IsTestCaseDisabled() { | 35 static bool IsTestCaseDisabled() { |
| 35 // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700. | 36 // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700. |
| 36 HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL); | 37 HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", nullptr, nullptr); |
| 37 if (!hdc) | 38 if (!hdc) |
| 38 return true; | 39 return true; |
| 39 DeleteDC(hdc); | 40 DeleteDC(hdc); |
| 40 return false; | 41 return false; |
| 41 } | 42 } |
| 42 | 43 |
| 43 // PrintingContext::Delegate methods. | 44 // PrintingContext::Delegate methods. |
| 44 gfx::NativeView GetParentView() override { return NULL; } | 45 gfx::NativeView GetParentView() override { return nullptr; } |
| 45 std::string GetAppLocale() override { return std::string(); } | 46 std::string GetAppLocale() override { return std::string(); } |
| 46 }; | 47 }; |
| 47 | 48 |
| 48 const uint32_t EMF_HEADER_SIZE = 128; | 49 const uint32_t EMF_HEADER_SIZE = 128; |
| 49 const int ONE_MB = 1024 * 1024; | 50 const int ONE_MB = 1024 * 1024; |
| 50 | 51 |
| 51 } // namespace | 52 } // namespace |
| 52 | 53 |
| 53 TEST(EmfTest, DC) { | 54 TEST(EmfTest, DC) { |
| 54 // Simplest use case. | 55 // Simplest use case. |
| 55 uint32_t size; | 56 uint32_t size; |
| 56 std::vector<char> data; | 57 std::vector<char> data; |
| 57 { | 58 { |
| 58 Emf emf; | 59 Emf emf; |
| 59 EXPECT_TRUE(emf.Init()); | 60 EXPECT_TRUE(emf.Init()); |
| 60 EXPECT_TRUE(emf.context()); | 61 EXPECT_TRUE(emf.context()); |
| 61 // An empty EMF is invalid, so we put at least a rectangle in it. | 62 // An empty EMF is invalid, so we put at least a rectangle in it. |
| 62 ::Rectangle(emf.context(), 10, 10, 190, 190); | 63 ::Rectangle(emf.context(), 10, 10, 190, 190); |
| 63 EXPECT_TRUE(emf.FinishDocument()); | 64 EXPECT_TRUE(emf.FinishDocument()); |
| 64 size = emf.GetDataSize(); | 65 size = emf.GetDataSize(); |
| 65 EXPECT_GT(size, EMF_HEADER_SIZE); | 66 EXPECT_GT(size, EMF_HEADER_SIZE); |
| 66 EXPECT_TRUE(emf.GetDataAsVector(&data)); | 67 EXPECT_TRUE(emf.GetDataAsVector(&data)); |
| 67 EXPECT_EQ(data.size(), size); | 68 EXPECT_EQ(data.size(), size); |
| 68 } | 69 } |
| 69 | 70 |
| 70 // Playback the data. | 71 // Playback the data. |
| 71 Emf emf; | 72 Emf emf; |
| 72 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); | 73 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); |
| 73 HDC hdc = CreateCompatibleDC(NULL); | 74 HDC hdc = CreateCompatibleDC(nullptr); |
| 74 EXPECT_TRUE(hdc); | 75 EXPECT_TRUE(hdc); |
| 75 RECT output_rect = {0, 0, 10, 10}; | 76 RECT output_rect = {0, 0, 10, 10}; |
| 76 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); | 77 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 77 EXPECT_TRUE(DeleteDC(hdc)); | 78 EXPECT_TRUE(DeleteDC(hdc)); |
| 78 } | 79 } |
| 79 | 80 |
| 80 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. | 81 // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. |
| 81 TEST_F(EmfPrintingTest, Enumerate) { | 82 TEST_F(EmfPrintingTest, Enumerate) { |
| 82 if (IsTestCaseDisabled()) | 83 if (IsTestCaseDisabled()) |
| 83 return; | 84 return; |
| 84 | 85 |
| 85 PrintSettings settings; | 86 PrintSettings settings; |
| 86 | 87 |
| 87 // My test case is a HP Color LaserJet 4550 PCL. | 88 // My test case is a HP Color LaserJet 4550 PCL. |
| 88 settings.set_device_name(L"UnitTest Printer"); | 89 settings.set_device_name(L"UnitTest Printer"); |
| 89 | 90 |
| 90 // Initialize it. | 91 // Initialize it. |
| 91 std::unique_ptr<PrintingContext> context(PrintingContext::Create(this)); | 92 PrintingContextWin context(this); |
| 92 EXPECT_EQ(context->InitWithSettings(settings), PrintingContext::OK); | 93 EXPECT_EQ(PrintingContext::OK, context.InitWithSettingsForTest(settings)); |
| 93 | 94 |
| 94 base::FilePath emf_file; | 95 base::FilePath emf_file; |
| 95 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file)); | 96 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file)); |
| 96 emf_file = emf_file.Append(FILE_PATH_LITERAL("printing")) | 97 emf_file = emf_file.Append(FILE_PATH_LITERAL("printing")) |
| 97 .Append(FILE_PATH_LITERAL("test")) | 98 .Append(FILE_PATH_LITERAL("test")) |
| 98 .Append(FILE_PATH_LITERAL("data")) | 99 .Append(FILE_PATH_LITERAL("data")) |
| 99 .Append(FILE_PATH_LITERAL("test4.emf")); | 100 .Append(FILE_PATH_LITERAL("test4.emf")); |
| 100 // Load any EMF with an image. | 101 // Load any EMF with an image. |
| 101 Emf emf; | 102 Emf emf; |
| 102 std::string emf_data; | 103 std::string emf_data; |
| 103 base::ReadFileToString(emf_file, &emf_data); | 104 base::ReadFileToString(emf_file, &emf_data); |
| 104 ASSERT_TRUE(emf_data.size()); | 105 ASSERT_TRUE(emf_data.size()); |
| 105 EXPECT_TRUE(emf.InitFromData(&emf_data[0], emf_data.size())); | 106 EXPECT_TRUE(emf.InitFromData(&emf_data[0], emf_data.size())); |
| 106 | 107 |
| 107 // This will print to file. The reason is that when running inside a | 108 // This will print to file. The reason is that when running inside a |
| 108 // unit_test, PrintingContext automatically dumps its files to the | 109 // unit_test, PrintingContext automatically dumps its files to the |
| 109 // current directory. | 110 // current directory. |
| 110 // TODO(maruel): Clean the .PRN file generated in current directory. | 111 // TODO(maruel): Clean the .PRN file generated in current directory. |
| 111 context->NewDocument(L"EmfTest.Enumerate"); | 112 context.NewDocument(L"EmfTest.Enumerate"); |
| 112 context->NewPage(); | 113 context.NewPage(); |
| 113 // Process one at a time. | 114 // Process one at a time. |
| 114 RECT page_bounds = emf.GetPageBounds(1).ToRECT(); | 115 RECT page_bounds = emf.GetPageBounds(1).ToRECT(); |
| 115 Emf::Enumerator emf_enum(emf, context->context(), &page_bounds); | 116 Emf::Enumerator emf_enum(emf, context.context(), &page_bounds); |
| 116 for (Emf::Enumerator::const_iterator itr = emf_enum.begin(); | 117 for (Emf::Enumerator::const_iterator itr = emf_enum.begin(); |
| 117 itr != emf_enum.end(); | 118 itr != emf_enum.end(); |
| 118 ++itr) { | 119 ++itr) { |
| 119 // To help debugging. | 120 // To help debugging. |
| 120 ptrdiff_t index = itr - emf_enum.begin(); | 121 ptrdiff_t index = itr - emf_enum.begin(); |
| 121 // If you get this assert, you need to lookup iType in wingdi.h. It starts | 122 // If you get this assert, you need to lookup iType in wingdi.h. It starts |
| 122 // with EMR_HEADER. | 123 // with EMR_HEADER. |
| 123 EMR_HEADER; | 124 EMR_HEADER; |
| 124 EXPECT_TRUE(itr->SafePlayback(&emf_enum.context_)) << | 125 EXPECT_TRUE(itr->SafePlayback(&emf_enum.context_)) << |
| 125 " index: " << index << " type: " << itr->record()->iType; | 126 " index: " << index << " type: " << itr->record()->iType; |
| 126 } | 127 } |
| 127 context->PageDone(); | 128 context.PageDone(); |
| 128 context->DocumentDone(); | 129 context.DocumentDone(); |
| 129 } | 130 } |
| 130 | 131 |
| 131 // Disabled if no "UnitTest printer" exists. | 132 // Disabled if no "UnitTest printer" exists. |
| 132 TEST_F(EmfPrintingTest, PageBreak) { | 133 TEST_F(EmfPrintingTest, PageBreak) { |
| 133 base::win::ScopedCreateDC dc( | 134 base::win::ScopedCreateDC dc( |
| 134 CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); | 135 CreateDC(L"WINSPOOL", L"UnitTest Printer", nullptr, nullptr)); |
| 135 if (!dc.Get()) | 136 if (!dc.Get()) |
| 136 return; | 137 return; |
| 137 uint32_t size; | 138 uint32_t size; |
| 138 std::vector<char> data; | 139 std::vector<char> data; |
| 139 { | 140 { |
| 140 Emf emf; | 141 Emf emf; |
| 141 EXPECT_TRUE(emf.Init()); | 142 EXPECT_TRUE(emf.Init()); |
| 142 EXPECT_TRUE(emf.context()); | 143 EXPECT_TRUE(emf.context()); |
| 143 int pages = 3; | 144 int pages = 3; |
| 144 while (pages) { | 145 while (pages) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 158 DOCINFO di = {0}; | 159 DOCINFO di = {0}; |
| 159 di.cbSize = sizeof(DOCINFO); | 160 di.cbSize = sizeof(DOCINFO); |
| 160 di.lpszDocName = L"Test Job"; | 161 di.lpszDocName = L"Test Job"; |
| 161 int job_id = ::StartDoc(dc.Get(), &di); | 162 int job_id = ::StartDoc(dc.Get(), &di); |
| 162 Emf emf; | 163 Emf emf; |
| 163 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); | 164 EXPECT_TRUE(emf.InitFromData(&data.front(), size)); |
| 164 EXPECT_TRUE(emf.SafePlayback(dc.Get())); | 165 EXPECT_TRUE(emf.SafePlayback(dc.Get())); |
| 165 ::EndDoc(dc.Get()); | 166 ::EndDoc(dc.Get()); |
| 166 // Since presumably the printer is not real, let us just delete the job from | 167 // Since presumably the printer is not real, let us just delete the job from |
| 167 // the queue. | 168 // the queue. |
| 168 HANDLE printer = NULL; | 169 HANDLE printer = nullptr; |
| 169 if (::OpenPrinter(const_cast<LPTSTR>(L"UnitTest Printer"), &printer, NULL)) { | 170 if (::OpenPrinter(const_cast<LPTSTR>(L"UnitTest Printer"), &printer, |
| 170 ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE); | 171 nullptr)) { |
| 172 ::SetJob(printer, job_id, 0, nullptr, JOB_CONTROL_DELETE); |
| 171 ClosePrinter(printer); | 173 ClosePrinter(printer); |
| 172 } | 174 } |
| 173 } | 175 } |
| 174 | 176 |
| 175 TEST(EmfTest, FileBackedEmf) { | 177 TEST(EmfTest, FileBackedEmf) { |
| 176 // Simplest use case. | 178 // Simplest use case. |
| 177 base::ScopedTempDir scratch_metafile_dir; | 179 base::ScopedTempDir scratch_metafile_dir; |
| 178 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir()); | 180 ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir()); |
| 179 base::FilePath metafile_path; | 181 base::FilePath metafile_path; |
| 180 EXPECT_TRUE(base::CreateTemporaryFileInDir(scratch_metafile_dir.GetPath(), | 182 EXPECT_TRUE(base::CreateTemporaryFileInDir(scratch_metafile_dir.GetPath(), |
| (...skipping 10 matching lines...) Expand all Loading... |
| 191 size = emf.GetDataSize(); | 193 size = emf.GetDataSize(); |
| 192 EXPECT_GT(size, EMF_HEADER_SIZE); | 194 EXPECT_GT(size, EMF_HEADER_SIZE); |
| 193 EXPECT_TRUE(emf.GetDataAsVector(&data)); | 195 EXPECT_TRUE(emf.GetDataAsVector(&data)); |
| 194 EXPECT_EQ(data.size(), size); | 196 EXPECT_EQ(data.size(), size); |
| 195 } | 197 } |
| 196 int64_t file_size = 0; | 198 int64_t file_size = 0; |
| 197 base::GetFileSize(metafile_path, &file_size); | 199 base::GetFileSize(metafile_path, &file_size); |
| 198 EXPECT_EQ(size, file_size); | 200 EXPECT_EQ(size, file_size); |
| 199 | 201 |
| 200 // Playback the data. | 202 // Playback the data. |
| 201 HDC hdc = CreateCompatibleDC(NULL); | 203 HDC hdc = CreateCompatibleDC(nullptr); |
| 202 EXPECT_TRUE(hdc); | 204 EXPECT_TRUE(hdc); |
| 203 Emf emf; | 205 Emf emf; |
| 204 EXPECT_TRUE(emf.InitFromFile(metafile_path)); | 206 EXPECT_TRUE(emf.InitFromFile(metafile_path)); |
| 205 RECT output_rect = {0, 0, 10, 10}; | 207 RECT output_rect = {0, 0, 10, 10}; |
| 206 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); | 208 EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 207 EXPECT_TRUE(DeleteDC(hdc)); | 209 EXPECT_TRUE(DeleteDC(hdc)); |
| 208 } | 210 } |
| 209 | 211 |
| 210 TEST(EmfTest, RasterizeMetafile) { | 212 TEST(EmfTest, RasterizeMetafile) { |
| 211 Emf emf; | 213 Emf emf; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 226 EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1)); | 228 EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1)); |
| 227 | 229 |
| 228 raster = emf.RasterizeMetafile(16 * ONE_MB); | 230 raster = emf.RasterizeMetafile(16 * ONE_MB); |
| 229 // Expected size about 64MB. | 231 // Expected size about 64MB. |
| 230 EXPECT_LE(abs(static_cast<int>(raster->GetDataSize()) - 64 * ONE_MB), ONE_MB); | 232 EXPECT_LE(abs(static_cast<int>(raster->GetDataSize()) - 64 * ONE_MB), ONE_MB); |
| 231 // Bounds should still be the same. | 233 // Bounds should still be the same. |
| 232 EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1)); | 234 EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1)); |
| 233 } | 235 } |
| 234 | 236 |
| 235 } // namespace printing | 237 } // namespace printing |
| OLD | NEW |