| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/pdf_ps_metafile_cairo.h" | 5 #include "printing/pdf_ps_metafile_cairo.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/file_descriptor_posix.h" | 11 #include "base/file_descriptor_posix.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/scoped_ptr.h" |
| 13 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "printing/native_metafile.h" |
| 16 #include "printing/native_metafile_factory.h" |
| 17 #include "printing/pdf_ps_metafile.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 19 |
| 16 typedef struct _cairo cairo_t; | 20 typedef struct _cairo cairo_t; |
| 17 | 21 |
| 18 class PdfPsTest : public testing::Test { | 22 class PdfPsTest : public testing::Test { |
| 19 protected: | 23 protected: |
| 20 base::FileDescriptor DevNullFD() { | 24 base::FileDescriptor DevNullFD() { |
| 21 return base::FileDescriptor(open("/dev/null", O_WRONLY), true); | 25 return base::FileDescriptor(open("/dev/null", O_WRONLY), true); |
| 22 } | 26 } |
| 23 }; | 27 }; |
| 24 | 28 |
| 25 TEST_F(PdfPsTest, Pdf) { | 29 TEST_F(PdfPsTest, Pdf) { |
| 26 // Tests in-renderer constructor. | 30 // Tests in-renderer constructor. |
| 27 printing::PdfPsMetafile pdf(printing::PdfPsMetafile::PDF); | 31 scoped_ptr<printing::NativeMetafile> pdf( |
| 28 EXPECT_TRUE(pdf.Init()); | 32 printing::NativeMetafileFactory::CreateMetafile()); |
| 33 EXPECT_TRUE(pdf->Init()); |
| 29 | 34 |
| 30 // Renders page 1. | 35 // Renders page 1. |
| 31 cairo_t* context = pdf.StartPage(72, 72, 1, 2, 3, 4); | 36 cairo_t* context = pdf->StartPage(72, 72, 1, 2, 3, 4); |
| 32 EXPECT_TRUE(context != NULL); | 37 EXPECT_TRUE(context != NULL); |
| 33 EXPECT_EQ(printing::PdfPsMetafile::FromCairoContext(context), &pdf); | 38 |
| 39 EXPECT_EQ(printing::PdfPsMetafile::FromCairoContext(context), pdf.get()); |
| 34 // In theory, we should use Cairo to draw something on |context|. | 40 // In theory, we should use Cairo to draw something on |context|. |
| 35 EXPECT_TRUE(pdf.FinishPage()); | 41 EXPECT_TRUE(pdf->FinishPage()); |
| 36 | 42 |
| 37 // Renders page 2. | 43 // Renders page 2. |
| 38 context = pdf.StartPage(64, 64, 1, 2, 3, 4); | 44 context = pdf->StartPage(64, 64, 1, 2, 3, 4); |
| 39 EXPECT_TRUE(context != NULL); | 45 EXPECT_TRUE(context != NULL); |
| 40 // In theory, we should use Cairo to draw something on |context|. | 46 // In theory, we should use Cairo to draw something on |context|. |
| 41 EXPECT_TRUE(pdf.FinishPage()); | 47 EXPECT_TRUE(pdf->FinishPage()); |
| 42 | 48 |
| 43 // Closes the file. | 49 // Closes the file. |
| 44 pdf.Close(); | 50 pdf->Close(); |
| 45 | 51 |
| 46 // Checks data size. | 52 // Checks data size. |
| 47 uint32 size = pdf.GetDataSize(); | 53 uint32 size = pdf->GetDataSize(); |
| 48 EXPECT_GT(size, 0u); | 54 EXPECT_GT(size, 0u); |
| 49 | 55 |
| 50 // Gets resulting data. | 56 // Gets resulting data. |
| 51 std::vector<char> buffer(size, 0x00); | 57 std::vector<char> buffer(size, 0x00); |
| 52 pdf.GetData(&buffer.front(), size); | 58 pdf->GetData(&buffer.front(), size); |
| 53 | 59 |
| 54 // Tests another constructor. | 60 // Tests another constructor. |
| 55 printing::PdfPsMetafile pdf2(printing::PdfPsMetafile::PDF); | 61 scoped_ptr<printing::NativeMetafile> pdf2( |
| 56 EXPECT_TRUE(pdf2.Init(&buffer.front(), size)); | 62 printing::NativeMetafileFactory::CreateMetafile()); |
| 63 EXPECT_TRUE(pdf2->Init(&buffer.front(), size)); |
| 57 | 64 |
| 58 // Tries to get the first 4 characters from pdf2. | 65 // Tries to get the first 4 characters from pdf2. |
| 59 std::vector<char> buffer2(4, 0x00); | 66 std::vector<char> buffer2(4, 0x00); |
| 60 pdf2.GetData(&buffer2.front(), 4); | 67 pdf2->GetData(&buffer2.front(), 4); |
| 61 | 68 |
| 62 // Tests if the header begins with "%PDF". | 69 // Tests if the header begins with "%PDF". |
| 63 std::string header(&buffer2.front(), 4); | 70 std::string header(&buffer2.front(), 4); |
| 64 EXPECT_EQ(header.find("%PDF", 0), 0u); | 71 EXPECT_EQ(header.find("%PDF", 0), 0u); |
| 65 | 72 |
| 66 // Tests if we can save data. | 73 // Tests if we can save data. |
| 67 EXPECT_TRUE(pdf.SaveTo(DevNullFD())); | 74 EXPECT_TRUE(pdf->SaveTo(DevNullFD())); |
| 68 | 75 |
| 69 // Test overriding the metafile with raw data. | 76 // Test overriding the metafile with raw data. |
| 70 printing::PdfPsMetafile pdf3(printing::PdfPsMetafile::PDF); | 77 scoped_ptr<printing::NativeMetafile> pdf3( |
| 71 EXPECT_TRUE(pdf3.Init()); | 78 printing::NativeMetafileFactory::CreateMetafile()); |
| 72 context = pdf3.StartPage(72, 72, 1, 2, 3, 4); | 79 EXPECT_TRUE(pdf3->Init()); |
| 80 context = pdf3->StartPage(72, 72, 1, 2, 3, 4); |
| 73 EXPECT_TRUE(context != NULL); | 81 EXPECT_TRUE(context != NULL); |
| 74 std::string test_raw_data = "Dummy PDF"; | 82 std::string test_raw_data = "Dummy PDF"; |
| 75 EXPECT_TRUE(pdf3.SetRawData(test_raw_data.c_str(), test_raw_data.size())); | 83 EXPECT_TRUE(pdf3->SetRawData(test_raw_data.c_str(), test_raw_data.size())); |
| 76 EXPECT_TRUE(pdf3.FinishPage()); | 84 EXPECT_TRUE(pdf3->FinishPage()); |
| 77 pdf3.Close(); | 85 pdf3->Close(); |
| 78 size = pdf3.GetDataSize(); | 86 size = pdf3->GetDataSize(); |
| 79 EXPECT_EQ(test_raw_data.size(), size); | 87 EXPECT_EQ(test_raw_data.size(), size); |
| 80 std::string output; | 88 std::string output; |
| 81 pdf3.GetData(WriteInto(&output, size + 1), size); | 89 pdf3->GetData(WriteInto(&output, size + 1), size); |
| 82 EXPECT_EQ(test_raw_data, output); | 90 EXPECT_EQ(test_raw_data, output); |
| 83 } | 91 } |
| 84 | |
| 85 TEST_F(PdfPsTest, Ps) { | |
| 86 // Tests in-renderer constructor. | |
| 87 printing::PdfPsMetafile ps(printing::PdfPsMetafile::PS); | |
| 88 EXPECT_TRUE(ps.Init()); | |
| 89 | |
| 90 // Renders page 1. | |
| 91 cairo_t* context = ps.StartPage(72, 72, 1, 2, 3, 4); | |
| 92 EXPECT_TRUE(context != NULL); | |
| 93 EXPECT_EQ(printing::PdfPsMetafile::FromCairoContext(context), &ps); | |
| 94 // In theory, we should use Cairo to draw something on |context|. | |
| 95 EXPECT_TRUE(ps.FinishPage()); | |
| 96 | |
| 97 // Renders page 2. | |
| 98 context = ps.StartPage(64, 64, 1, 2, 3, 4); | |
| 99 EXPECT_TRUE(context != NULL); | |
| 100 // In theory, we should use Cairo to draw something on |context|. | |
| 101 EXPECT_TRUE(ps.FinishPage()); | |
| 102 | |
| 103 // Closes the file. | |
| 104 ps.Close(); | |
| 105 | |
| 106 // Checks data size. | |
| 107 uint32 size = ps.GetDataSize(); | |
| 108 EXPECT_GT(size, 0u); | |
| 109 | |
| 110 // Gets resulting data. | |
| 111 std::vector<char> buffer(size, 0x00); | |
| 112 ps.GetData(&buffer.front(), size); | |
| 113 | |
| 114 // Tests another constructor. | |
| 115 printing::PdfPsMetafile ps2(printing::PdfPsMetafile::PS); | |
| 116 EXPECT_TRUE(ps2.Init(&buffer.front(), size)); | |
| 117 | |
| 118 // Tries to get the first 4 characters from ps2. | |
| 119 std::vector<char> buffer2(4, 0x00); | |
| 120 ps2.GetData(&buffer2.front(), 4); | |
| 121 | |
| 122 // Tests if the header begins with "%!PS". | |
| 123 std::string header(&buffer2.front(), 4); | |
| 124 EXPECT_EQ(header.find("%!PS", 0), 0u); | |
| 125 | |
| 126 // Tests if we can save data. | |
| 127 EXPECT_TRUE(ps.SaveTo(DevNullFD())); | |
| 128 } | |
| OLD | NEW |