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_metafile_cg_mac.h" | 5 #include "printing/pdf_metafile_cg_mac.h" |
6 | 6 |
7 #import <ApplicationServices/ApplicationServices.h> | 7 #include <ApplicationServices/ApplicationServices.h> |
8 #include <CoreFoundation/CoreFoundation.h> | |
8 #include <stdint.h> | 9 #include <stdint.h> |
9 | 10 |
10 #include <string> | 11 #include <string> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
14 #include "base/mac/scoped_cftyperef.h" | |
15 #include "printing/pdf_metafile_skia.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "ui/gfx/geometry/rect.h" | 17 #include "ui/gfx/geometry/rect.h" |
15 | 18 |
19 using base::ScopedCFTypeRef; | |
20 | |
16 namespace printing { | 21 namespace printing { |
17 | 22 |
18 TEST(PdfMetafileCgTest, Pdf) { | 23 TEST(PdfMetafileCgTest, Pdf) { |
19 // Test in-renderer constructor. | 24 // Create test PDF. |
20 PdfMetafileCg pdf; | 25 PdfMetafileSkia pdf(PDF_SKIA_DOCUMENT_TYPE); |
21 EXPECT_TRUE(pdf.Init()); | 26 EXPECT_TRUE(pdf.Init()); |
22 EXPECT_TRUE(pdf.context() != NULL); | 27 EXPECT_TRUE(pdf.context() != NULL); |
23 | 28 |
24 // Render page 1. | 29 // Render page 1. |
25 gfx::Rect rect_1(10, 10, 520, 700); | 30 gfx::Rect rect_1(10, 10, 520, 700); |
26 gfx::Size size_1(540, 720); | 31 gfx::Size size_1(540, 720); |
27 pdf.StartPage(size_1, rect_1, 1.25); | 32 pdf.StartPage(size_1, rect_1, 1.25); |
28 pdf.FinishPage(); | 33 pdf.FinishPage(); |
29 | 34 |
30 // Render page 2. | 35 // Render page 2. |
31 gfx::Rect rect_2(10, 10, 520, 700); | 36 gfx::Rect rect_2(10, 10, 520, 700); |
32 gfx::Size size_2(720, 540); | 37 gfx::Size size_2(720, 540); |
33 pdf.StartPage(size_2, rect_2, 2.0); | 38 pdf.StartPage(size_2, rect_2, 2.0); |
34 pdf.FinishPage(); | 39 pdf.FinishPage(); |
35 | 40 |
36 pdf.FinishDocument(); | 41 pdf.FinishDocument(); |
37 | 42 |
38 // Check data size. | 43 // Check data size. |
39 uint32_t size = pdf.GetDataSize(); | 44 uint32_t size = pdf.GetDataSize(); |
40 EXPECT_GT(size, 0U); | 45 EXPECT_GT(size, 0U); |
Lei Zhang
2017/04/08 01:24:24
This should be an ASSERT_GT(), BTW.
hal.canary
2017/04/11 19:07:27
I reverted changes to this file. No longer appli
| |
41 | 46 |
42 // Get resulting data. | 47 // Get resulting data. |
43 std::vector<char> buffer(size, 0); | 48 std::vector<char> buffer(size, 0); |
44 pdf.GetData(&buffer.front(), size); | 49 pdf.GetData(buffer.data(), size); |
45 | |
46 // Test browser-side constructor. | |
47 PdfMetafileCg pdf2; | |
48 EXPECT_TRUE(pdf2.InitFromData(&buffer.front(), size)); | |
49 | |
50 // Get the first 4 characters from pdf2. | |
51 std::vector<char> buffer2(4, 0); | |
52 pdf2.GetData(&buffer2.front(), 4); | |
53 | 50 |
54 // Test that the header begins with "%PDF". | 51 // Test that the header begins with "%PDF". |
55 std::string header(&buffer2.front(), 4); | 52 std::string header(buffer.data(), 4); |
56 EXPECT_EQ(0U, header.find("%PDF", 0)); | 53 EXPECT_EQ(0U, header.find("%PDF", 0)); |
57 | 54 |
58 // Test that the PDF is correctly reconstructed. | 55 base::ScopedCFTypeRef<CFMutableDataRef> pdf_data( |
59 EXPECT_EQ(2U, pdf2.GetPageCount()); | 56 CFDataCreateMutable(kCFAllocatorDefault, 0)); |
60 gfx::Size page_size = pdf2.GetPageBounds(1).size(); | 57 EXPECT_TRUE(pdf_data.get() != nullptr); |
61 EXPECT_EQ(540, page_size.width()); | 58 ScopedCFTypeRef<CGDataConsumerRef> pdf_consumer( |
62 EXPECT_EQ(720, page_size.height()); | 59 CGDataConsumerCreateWithCFData(pdf_data)); |
63 page_size = pdf2.GetPageBounds(2).size(); | 60 EXPECT_TRUE(pdf_consumer.get() != nullptr); |
64 EXPECT_EQ(720, page_size.width()); | 61 base::ScopedCFTypeRef<CGContextRef> context( |
65 EXPECT_EQ(540, page_size.height()); | 62 CGPDFContextCreate(pdf_consumer, nullptr, nullptr)); |
63 EXPECT_TRUE(context.get() != nullptr); | |
64 | |
65 PdfMetafileCg::RenderPageParams params; | |
66 EXPECT_TRUE(PdfMetafileCg::RenderPage(buffer.data(), size, 1, context.get(), | |
67 CGRectMake(0, 0, 540, 720), params)); | |
66 } | 68 } |
67 | 69 |
68 } // namespace printing | 70 } // namespace printing |
OLD | NEW |