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> |
| 9 | |
| 10 | #include "base/basictypes.h" |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 11 | #include "base/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "base/file_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "base/path_service.h" |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 14 | #include "printing/printing_context.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // This test is automatically disabled if no printer named "UnitTest Printer" is |
| 20 | // available. |
| 21 | class EmfPrintingTest : public testing::Test { |
| 22 | public: |
| 23 | typedef testing::Test Parent; |
| 24 | static bool IsTestCaseDisabled() { |
| 25 | // It is assumed this printer is a HP Color LaserJet 4550 PCL or 4700. |
| 26 | HDC hdc = CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL); |
| 27 | if (!hdc) |
| 28 | return true; |
| 29 | DeleteDC(hdc); |
| 30 | return false; |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | TEST(EmfTest, DC) { |
| 37 | static const int EMF_HEADER_SIZE = 128; |
| 38 | |
| 39 | // Simplest use case. |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 40 | printing::Emf emf; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | RECT rect = {100, 100, 200, 200}; |
| 42 | HDC hdc = CreateCompatibleDC(NULL); |
| 43 | EXPECT_TRUE(hdc != NULL); |
| 44 | EXPECT_TRUE(emf.CreateDc(hdc, &rect)); |
| 45 | EXPECT_TRUE(emf.hdc() != NULL); |
| 46 | // In theory, you'd use the HDC with GDI functions here. |
| 47 | EXPECT_TRUE(emf.CloseDc()); |
| 48 | unsigned size = emf.GetDataSize(); |
| 49 | EXPECT_EQ(size, EMF_HEADER_SIZE); |
| 50 | std::vector<BYTE> data; |
| 51 | EXPECT_TRUE(emf.GetData(&data)); |
| 52 | EXPECT_EQ(data.size(), size); |
| 53 | emf.CloseEmf(); |
| 54 | EXPECT_TRUE(DeleteDC(hdc)); |
| 55 | |
| 56 | // Playback the data. |
| 57 | hdc = CreateCompatibleDC(NULL); |
| 58 | EXPECT_TRUE(hdc); |
| 59 | EXPECT_TRUE(emf.CreateFromData(&data.front(), size)); |
| 60 | RECT output_rect = {0, 0, 10, 10}; |
| 61 | EXPECT_TRUE(emf.Playback(hdc, &output_rect)); |
| 62 | EXPECT_TRUE(DeleteDC(hdc)); |
| 63 | } |
| 64 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | // Disabled if no "UnitTest printer" exist. Useful to reproduce bug 1186598. |
| 66 | TEST_F(EmfPrintingTest, Enumerate) { |
| 67 | if (IsTestCaseDisabled()) |
| 68 | return; |
| 69 | |
| 70 | printing::PrintSettings settings; |
| 71 | |
| 72 | // My test case is a HP Color LaserJet 4550 PCL. |
| 73 | settings.set_device_name(L"UnitTest Printer"); |
| 74 | |
| 75 | // Initialize it. |
| 76 | printing::PrintingContext context; |
| 77 | EXPECT_EQ(context.InitWithSettings(settings), printing::PrintingContext::OK); |
| 78 | |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 79 | FilePath emf_file; |
| 80 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &emf_file)); |
| 81 | emf_file = emf_file.Append(FILE_PATH_LITERAL("printing")) |
| 82 | .Append(FILE_PATH_LITERAL("test")) |
| 83 | .Append(FILE_PATH_LITERAL("data")) |
| 84 | .Append(FILE_PATH_LITERAL("test4.emf")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | // Load any EMF with an image. |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 86 | printing::Emf emf; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | std::string emf_data; |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 88 | file_util::ReadFileToString(emf_file, &emf_data); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | ASSERT_TRUE(emf_data.size()); |
| 90 | EXPECT_TRUE(emf.CreateFromData(&emf_data[0], emf_data.size())); |
| 91 | |
| 92 | // This will print to file. The reason is that when running inside a |
| 93 | // unit_test, printing::PrintingContext automatically dumps its files to the |
| 94 | // current directory. |
| 95 | // TODO(maruel): Clean the .PRN file generated in current directory. |
| 96 | context.NewDocument(L"EmfTest.Enumerate"); |
| 97 | context.NewPage(); |
| 98 | // Process one at a time. |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 99 | printing::Emf::Enumerator emf_enum(emf, context.context(), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | &emf.GetBounds().ToRECT()); |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 101 | for (printing::Emf::Enumerator::const_iterator itr = emf_enum.begin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 102 | itr != emf_enum.end(); |
| 103 | ++itr) { |
| 104 | // To help debugging. |
| 105 | ptrdiff_t index = itr - emf_enum.begin(); |
| 106 | // If you get this assert, you need to lookup iType in wingdi.h. It starts |
| 107 | // with EMR_HEADER. |
| 108 | EMR_HEADER; |
| 109 | EXPECT_TRUE(itr->SafePlayback(NULL)) << |
| 110 | " index: " << index << " type: " << itr->record()->iType; |
| 111 | } |
| 112 | context.PageDone(); |
| 113 | context.DocumentDone(); |
| 114 | } |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 115 | |