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