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