license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame^] | 15 | #include "base/scoped_handle_win.h" |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 16 | #include "printing/printing_context.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // This test is automatically disabled if no printer named "UnitTest Printer" is |
| 22 | // available. |
| 23 | class EmfPrintingTest : public testing::Test { |
| 24 | public: |
| 25 | typedef testing::Test Parent; |
| 26 | static bool IsTestCaseDisabled() { |
| 27 | // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700. |
| 28 | HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL); |
| 29 | if (!hdc) |
| 30 | return true; |
| 31 | DeleteDC(hdc); |
| 32 | return false; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | TEST(EmfTest, DC) { |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 39 | static const uint32 EMF_HEADER_SIZE = 128; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | |
| 41 | // Simplest use case. |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 42 | printing::Emf emf; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | RECT rect = {100, 100, 200, 200}; |
| 44 | HDC hdc = CreateCompatibleDC(NULL); |
| 45 | EXPECT_TRUE(hdc != NULL); |
| 46 | EXPECT_TRUE(emf.CreateDc(hdc, &rect)); |
| 47 | EXPECT_TRUE(emf.hdc() != NULL); |
| 48 | // In theory, you'd use the HDC with GDI functions here. |
| 49 | EXPECT_TRUE(emf.CloseDc()); |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 50 | uint32 size = emf.GetDataSize(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | EXPECT_EQ(size, EMF_HEADER_SIZE); |
| 52 | std::vector<BYTE> data; |
| 53 | EXPECT_TRUE(emf.GetData(&data)); |
| 54 | EXPECT_EQ(data.size(), size); |
| 55 | emf.CloseEmf(); |
| 56 | EXPECT_TRUE(DeleteDC(hdc)); |
| 57 | |
| 58 | // Playback the data. |
| 59 | hdc = CreateCompatibleDC(NULL); |
| 60 | EXPECT_TRUE(hdc); |
| 61 | EXPECT_TRUE(emf.CreateFromData(&data.front(), size)); |
| 62 | RECT output_rect = {0, 0, 10, 10}; |
| 63 | EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 64 | EXPECT_TRUE(DeleteDC(hdc)); |
| 65 | } |
| 66 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. |
| 68 | TEST_F(EmfPrintingTest, Enumerate) { |
| 69 | if (IsTestCaseDisabled()) |
| 70 | return; |
| 71 | |
| 72 | printing::PrintSettings settings; |
| 73 | |
| 74 | // My test case is a HP Color LaserJet 4550 PCL. |
| 75 | settings.set_device_name(L"UnitTest Printer"); |
| 76 | |
| 77 | // Initialize it. |
| 78 | printing::PrintingContext context; |
| 79 | EXPECT_EQ(context.InitWithSettings(settings), printing::PrintingContext::OK); |
| 80 | |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 81 | FilePath emf_file; |
| 82 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file)); |
| 83 | emf_file = emf_file.Append(FILE_PATH_LITERAL("printing")) |
| 84 | .Append(FILE_PATH_LITERAL("test")) |
| 85 | .Append(FILE_PATH_LITERAL("data")) |
| 86 | .Append(FILE_PATH_LITERAL("test4.emf")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | // Load any EMF with an image. |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 88 | printing::Emf emf; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | std::string emf_data; |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 90 | file_util::ReadFileToString(emf_file, &emf_data); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | ASSERT_TRUE(emf_data.size()); |
| 92 | EXPECT_TRUE(emf.CreateFromData(&emf_data[0], emf_data.size())); |
| 93 | |
| 94 | // This will print to file. The reason is that when running inside a |
| 95 | // unit_test, printing::PrintingContext automatically dumps its files to the |
| 96 | // current directory. |
| 97 | // TODO(maruel): Clean the .PRN file generated in current directory. |
| 98 | context.NewDocument(L"EmfTest.Enumerate"); |
| 99 | context.NewPage(); |
| 100 | // Process one at a time. |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 101 | printing::Emf::Enumerator emf_enum(emf, context.context(), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 102 | &emf.GetBounds().ToRECT()); |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 103 | for (printing::Emf::Enumerator::const_iterator itr = emf_enum.begin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | itr != emf_enum.end(); |
| 105 | ++itr) { |
| 106 | // To help debugging. |
| 107 | ptrdiff_t index = itr - emf_enum.begin(); |
| 108 | // If you get this assert, you need to lookup iType in wingdi.h. It starts |
| 109 | // with EMR_HEADER. |
| 110 | EMR_HEADER; |
| 111 | EXPECT_TRUE(itr->SafePlayback(NULL)) << |
| 112 | " index: " << index << " type: " << itr->record()->iType; |
| 113 | } |
| 114 | context.PageDone(); |
| 115 | context.DocumentDone(); |
| 116 | } |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 117 | |
[email protected] | 2aa8e18 | 2010-07-12 16:25:04 | [diff] [blame^] | 118 | // Disabled if no "UnitTest printer" exists. |
| 119 | TEST_F(EmfPrintingTest, PageBreak) { |
| 120 | ScopedHDC dc(CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); |
| 121 | if (!dc.Get()) |
| 122 | return; |
| 123 | printing::Emf emf; |
| 124 | EXPECT_TRUE(emf.CreateDc(dc.Get(), NULL)); |
| 125 | EXPECT_TRUE(emf.hdc() != NULL); |
| 126 | int pages = 3; |
| 127 | while (pages) { |
| 128 | EXPECT_TRUE(emf.StartPage()); |
| 129 | ::Rectangle(emf.hdc(), 10, 10, 190, 190); |
| 130 | EXPECT_TRUE(emf.EndPage()); |
| 131 | --pages; |
| 132 | } |
| 133 | EXPECT_TRUE(emf.CloseDc()); |
| 134 | uint32 size = emf.GetDataSize(); |
| 135 | std::vector<BYTE> data; |
| 136 | EXPECT_TRUE(emf.GetData(&data)); |
| 137 | EXPECT_EQ(data.size(), size); |
| 138 | emf.CloseEmf(); |
| 139 | |
| 140 | // Playback the data. |
| 141 | DOCINFO di = {0}; |
| 142 | di.cbSize = sizeof(DOCINFO); |
| 143 | di.lpszDocName = L"Test Job"; |
| 144 | int job_id = ::StartDoc(dc.Get(), &di); |
| 145 | EXPECT_TRUE(emf.CreateFromData(&data.front(), size)); |
| 146 | EXPECT_TRUE(emf.SafePlayback(dc.Get())); |
| 147 | ::EndDoc(dc.Get()); |
| 148 | // Since presumably the printer is not real, let us just delete the job from |
| 149 | // the queue. |
| 150 | HANDLE printer = NULL; |
| 151 | if (::OpenPrinter(L"UnitTest Printer", &printer, NULL)) { |
| 152 | ::SetJob(printer, job_id, 0, NULL, JOB_CONTROL_DELETE); |
| 153 | ClosePrinter(printer); |
| 154 | } |
| 155 | } |
| 156 | |