[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 5 | #include "printing/emf_win.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | // For quick access. |
| 8 | #include <wingdi.h> |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 9 | #include <winspool.h> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | |
| 11 | #include "base/basictypes.h" |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 12 | #include "base/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "base/file_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "base/path_service.h" |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 15 | #include "base/scoped_ptr.h" |
[email protected] | f21104d | 2010-10-08 20:57:57 | [diff] [blame] | 16 | #include "base/scoped_temp_dir.h" |
[email protected] | 0142bd4f | 2010-12-31 01:02:47 | [diff] [blame] | 17 | #include "base/win/scoped_hdc.h" |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 18 | #include "printing/printing_context.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | edc531f9 | 2011-03-18 17:52:23 | [diff] [blame] | 20 | #include "ui/gfx/point.h" |
| 21 | #include "ui/gfx/size.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
| 25 | // This test is automatically disabled if no printer named "UnitTest Printer" is |
| 26 | // available. |
| 27 | class EmfPrintingTest : public testing::Test { |
| 28 | public: |
| 29 | typedef testing::Test Parent; |
| 30 | static bool IsTestCaseDisabled() { |
| 31 | // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700. |
| 32 | HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL); |
| 33 | if (!hdc) |
| 34 | return true; |
| 35 | DeleteDC(hdc); |
| 36 | return false; |
| 37 | } |
| 38 | }; |
| 39 | |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 40 | const uint32 EMF_HEADER_SIZE = 128; |
| 41 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | } // namespace |
| 43 | |
[email protected] | 5ad7617 | 2011-03-02 19:25:17 | [diff] [blame] | 44 | namespace printing { |
| 45 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | TEST(EmfTest, DC) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | // Simplest use case. |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 48 | uint32 size; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | std::vector<BYTE> data; |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 50 | { |
| 51 | printing::Emf emf; |
| 52 | EXPECT_TRUE(emf.Init()); |
| 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 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | |
| 63 | // Playback the data. |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 64 | printing::Emf emf; |
[email protected] | f2da4b0 | 2011-03-18 05:45:51 | [diff] [blame] | 65 | EXPECT_TRUE(emf.InitFromData(&data.front(), size)); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 66 | HDC hdc = CreateCompatibleDC(NULL); |
| 67 | EXPECT_TRUE(hdc); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | RECT output_rect = {0, 0, 10, 10}; |
| 69 | EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 70 | EXPECT_TRUE(DeleteDC(hdc)); |
| 71 | } |
| 72 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. |
| 74 | TEST_F(EmfPrintingTest, Enumerate) { |
| 75 | if (IsTestCaseDisabled()) |
| 76 | return; |
| 77 | |
| 78 | printing::PrintSettings settings; |
| 79 | |
| 80 | // My test case is a HP Color LaserJet 4550 PCL. |
| 81 | settings.set_device_name(L"UnitTest Printer"); |
| 82 | |
| 83 | // Initialize it. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 84 | scoped_ptr<printing::PrintingContext> context( |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 85 | printing::PrintingContext::Create(std::string())); |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 86 | EXPECT_EQ(context->InitWithSettings(settings), printing::PrintingContext::OK); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 88 | FilePath emf_file; |
| 89 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file)); |
| 90 | emf_file = emf_file.Append(FILE_PATH_LITERAL("printing")) |
| 91 | .Append(FILE_PATH_LITERAL("test")) |
| 92 | .Append(FILE_PATH_LITERAL("data")) |
| 93 | .Append(FILE_PATH_LITERAL("test4.emf")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | // Load any EMF with an image. |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 95 | printing::Emf emf; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | std::string emf_data; |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 97 | file_util::ReadFileToString(emf_file, &emf_data); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | ASSERT_TRUE(emf_data.size()); |
[email protected] | 89eff96a | 2011-03-17 23:22:06 | [diff] [blame] | 99 | EXPECT_TRUE(emf.InitFromData(&emf_data[0], emf_data.size())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | |
| 101 | // This will print to file. The reason is that when running inside a |
| 102 | // unit_test, printing::PrintingContext automatically dumps its files to the |
| 103 | // current directory. |
| 104 | // TODO(maruel): Clean the .PRN file generated in current directory. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 105 | context->NewDocument(L"EmfTest.Enumerate"); |
| 106 | context->NewPage(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | // Process one at a time. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 108 | printing::Emf::Enumerator emf_enum(emf, context->context(), |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 109 | &emf.GetPageBounds(1).ToRECT()); |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 110 | for (printing::Emf::Enumerator::const_iterator itr = emf_enum.begin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | itr != emf_enum.end(); |
| 112 | ++itr) { |
| 113 | // To help debugging. |
| 114 | ptrdiff_t index = itr - emf_enum.begin(); |
| 115 | // If you get this assert, you need to lookup iType in wingdi.h. It starts |
| 116 | // with EMR_HEADER. |
| 117 | EMR_HEADER; |
| 118 | EXPECT_TRUE(itr->SafePlayback(NULL)) << |
| 119 | " index: " << index << " type: " << itr->record()->iType; |
| 120 | } |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 121 | context->PageDone(); |
| 122 | context->DocumentDone(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | } |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 124 | |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 125 | // Disabled if no "UnitTest printer" exists. |
| 126 | TEST_F(EmfPrintingTest, PageBreak) { |
[email protected] | 0142bd4f | 2010-12-31 01:02:47 | [diff] [blame] | 127 | base::win::ScopedHDC dc( |
| 128 | CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 129 | if (!dc.Get()) |
| 130 | return; |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 131 | uint32 size; |
[email protected] | f2da4b0 | 2011-03-18 05:45:51 | [diff] [blame] | 132 | std::vector<BYTE> data; |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 133 | { |
| 134 | printing::Emf emf; |
| 135 | EXPECT_TRUE(emf.Init()); |
| 136 | EXPECT_TRUE(emf.context() != NULL); |
| 137 | int pages = 3; |
| 138 | while (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); |
| 148 | } |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 149 | |
| 150 | // Playback the data. |
| 151 | DOCINFO di = {0}; |
| 152 | di.cbSize = sizeof(DOCINFO); |
| 153 | di.lpszDocName = L"Test Job"; |
| 154 | int job_id = ::StartDoc(dc.Get(), &di); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 155 | printing::Emf emf; |
[email protected] | 89eff96a | 2011-03-17 23:22:06 | [diff] [blame] | 156 | EXPECT_TRUE(emf.InitFromData(&data.front(), size)); |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame] | 157 | EXPECT_TRUE(emf.SafePlayback(dc.Get())); |
| 158 | ::EndDoc(dc.Get()); |
| 159 | // Since presumably the printer is not real, let us just delete the job from |
| 160 | // the queue. |
| 161 | HANDLE printer = NULL; |
| 162 | if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) { |
| 163 | ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE); |
| 164 | ClosePrinter(printer); |
| 165 | } |
| 166 | } |
| 167 | |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 168 | TEST(EmfTest, FileBackedEmf) { |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 169 | // Simplest use case. |
[email protected] | f21104d | 2010-10-08 20:57:57 | [diff] [blame] | 170 | ScopedTempDir scratch_metafile_dir; |
| 171 | ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir()); |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 172 | FilePath metafile_path; |
[email protected] | f21104d | 2010-10-08 20:57:57 | [diff] [blame] | 173 | EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir.path(), |
| 174 | &metafile_path)); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 175 | uint32 size; |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 176 | std::vector<BYTE> data; |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 177 | { |
| 178 | printing::Emf emf; |
| 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 | } |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 189 | int64 file_size = 0; |
| 190 | file_util::GetFileSize(metafile_path, &file_size); |
| 191 | EXPECT_EQ(size, file_size); |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 192 | |
| 193 | // Playback the data. |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 194 | HDC hdc = CreateCompatibleDC(NULL); |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 195 | EXPECT_TRUE(hdc); |
[email protected] | 481edc5 | 2011-03-22 06:36:58 | [diff] [blame^] | 196 | printing::Emf emf; |
| 197 | EXPECT_TRUE(emf.InitFromFile(metafile_path)); |
[email protected] | e6cddc57 | 2010-09-29 21:39:45 | [diff] [blame] | 198 | RECT output_rect = {0, 0, 10, 10}; |
| 199 | EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 200 | EXPECT_TRUE(DeleteDC(hdc)); |
| 201 | } |
| 202 | |
[email protected] | 5ad7617 | 2011-03-02 19:25:17 | [diff] [blame] | 203 | } // namespace printing |