OLD | NEW |
1 // Copyright (c) 2011 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_path.h" |
12 #include "base/file_util.h" | 13 #include "base/file_util.h" |
13 #include "base/string_util.h" | 14 #include "base/string_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "ui/gfx/size.h" |
15 | 17 |
16 typedef struct _cairo cairo_t; | 18 typedef struct _cairo cairo_t; |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 class PdfPsTest : public testing::Test { | 22 class PdfPsTest : public testing::Test {}; |
21 protected: | |
22 base::FileDescriptor DevNullFD() { | |
23 return base::FileDescriptor(open("/dev/null", O_WRONLY), true); | |
24 } | |
25 }; | |
26 | 23 |
27 } // namespace | 24 } // namespace |
28 | 25 |
29 namespace printing { | 26 namespace printing { |
30 | 27 |
31 TEST_F(PdfPsTest, Pdf) { | 28 TEST_F(PdfPsTest, Pdf) { |
32 // Tests in-renderer constructor. | 29 // Tests in-renderer constructor. |
33 printing::PdfPsMetafile pdf; | 30 printing::PdfPsMetafile pdf; |
34 EXPECT_TRUE(pdf.Init()); | 31 EXPECT_TRUE(pdf.Init()); |
35 | 32 |
36 // Renders page 1. | 33 // Renders page 1. |
37 cairo_t* context = pdf.StartPage(72, 72, 1, 2, 3, 4); | 34 cairo_t* context = pdf.StartPage(gfx::Size(72 + 2 + 4, 72 + 1 + 3), 1, 4); |
38 EXPECT_TRUE(context != NULL); | 35 EXPECT_TRUE(context != NULL); |
39 EXPECT_EQ(printing::PdfPsMetafile::FromCairoContext(context), &pdf); | 36 EXPECT_EQ(printing::PdfPsMetafile::FromCairoContext(context), &pdf); |
40 // In theory, we should use Cairo to draw something on |context|. | 37 // In theory, we should use Cairo to draw something on |context|. |
41 EXPECT_TRUE(pdf.FinishPage()); | 38 EXPECT_TRUE(pdf.FinishPage()); |
42 | 39 |
43 // Renders page 2. | 40 // Renders page 2. |
44 context = pdf.StartPage(64, 64, 1, 2, 3, 4); | 41 context = pdf.StartPage(gfx::Size(64 + 2 + 4, 64 + 1 + 3), 1, 4); |
45 EXPECT_TRUE(context != NULL); | 42 EXPECT_TRUE(context != NULL); |
46 // In theory, we should use Cairo to draw something on |context|. | 43 // In theory, we should use Cairo to draw something on |context|. |
47 EXPECT_TRUE(pdf.FinishPage()); | 44 EXPECT_TRUE(pdf.FinishPage()); |
48 | 45 |
49 // Closes the file. | 46 // Closes the file. |
50 pdf.Close(); | 47 pdf.Close(); |
51 | 48 |
52 // Checks data size. | 49 // Checks data size. |
53 uint32 size = pdf.GetDataSize(); | 50 uint32 size = pdf.GetDataSize(); |
54 EXPECT_GT(size, 0u); | 51 EXPECT_GT(size, 0u); |
55 | 52 |
56 // Gets resulting data. | 53 // Gets resulting data. |
57 std::vector<char> buffer(size, 0x00); | 54 std::vector<char> buffer(size, 0x00); |
58 pdf.GetData(&buffer.front(), size); | 55 pdf.GetData(&buffer.front(), size); |
59 | 56 |
60 // Tests another constructor. | 57 // Tests another constructor. |
61 printing::PdfPsMetafile pdf2; | 58 printing::PdfPsMetafile pdf2; |
62 EXPECT_TRUE(pdf2.Init(&buffer.front(), size)); | 59 EXPECT_TRUE(pdf2.Init(&buffer.front(), size)); |
63 | 60 |
64 // Tries to get the first 4 characters from pdf2. | 61 // Tries to get the first 4 characters from pdf2. |
65 std::vector<char> buffer2(4, 0x00); | 62 std::vector<char> buffer2(4, 0x00); |
66 pdf2.GetData(&buffer2.front(), 4); | 63 pdf2.GetData(&buffer2.front(), 4); |
67 | 64 |
68 // Tests if the header begins with "%PDF". | 65 // Tests if the header begins with "%PDF". |
69 std::string header(&buffer2.front(), 4); | 66 std::string header(&buffer2.front(), 4); |
70 EXPECT_EQ(header.find("%PDF", 0), 0u); | 67 EXPECT_EQ(header.find("%PDF", 0), 0u); |
71 | 68 |
72 // Tests if we can save data. | 69 // Tests if we can save data. |
73 EXPECT_TRUE(pdf.SaveTo(DevNullFD())); | 70 EXPECT_TRUE(pdf.SaveTo(FilePath("/dev/null"))); |
74 | 71 |
75 // Test overriding the metafile with raw data. | 72 // Test overriding the metafile with raw data. |
76 printing::PdfPsMetafile pdf3; | 73 printing::PdfPsMetafile pdf3; |
77 EXPECT_TRUE(pdf3.Init()); | 74 EXPECT_TRUE(pdf3.Init()); |
78 context = pdf3.StartPage(72, 72, 1, 2, 3, 4); | 75 context = pdf3.StartPage(gfx::Size(72 + 2 + 4, 72 + 1 + 3), 1, 4); |
79 EXPECT_TRUE(context != NULL); | 76 EXPECT_TRUE(context != NULL); |
80 std::string test_raw_data = "Dummy PDF"; | 77 std::string test_raw_data = "Dummy PDF"; |
81 EXPECT_TRUE(pdf3.SetRawData(test_raw_data.c_str(), test_raw_data.size())); | 78 EXPECT_TRUE(pdf3.SetRawData(test_raw_data.c_str(), test_raw_data.size())); |
82 EXPECT_TRUE(pdf3.FinishPage()); | 79 EXPECT_TRUE(pdf3.FinishPage()); |
83 pdf3.Close(); | 80 pdf3.Close(); |
84 size = pdf3.GetDataSize(); | 81 size = pdf3.GetDataSize(); |
85 EXPECT_EQ(test_raw_data.size(), size); | 82 EXPECT_EQ(test_raw_data.size(), size); |
86 std::string output; | 83 std::string output; |
87 pdf3.GetData(WriteInto(&output, size + 1), size); | 84 pdf3.GetData(WriteInto(&output, size + 1), size); |
88 EXPECT_EQ(test_raw_data, output); | 85 EXPECT_EQ(test_raw_data, output); |
89 } | 86 } |
90 | 87 |
91 } // namespace printing | 88 } // namespace printing |
OLD | NEW |