Skip to content

Commit

Permalink
Add test for PdfMetafileCg::RenderPage().
Browse files Browse the repository at this point in the history
Add a basic test to make sure RenderPage() works correctly for simple
rendering.

Change-Id: I2b2fd287167c532cb2f35c50726b880f56cb496b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2117354
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Daniel Hosseinian <dhoss@chromium.org>
Cr-Commit-Position: refs/heads/master@{#753069}
  • Loading branch information
leizleiz authored and Commit Bot committed Mar 25, 2020
1 parent f65dc68 commit 670bf3b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
5 changes: 4 additions & 1 deletion printing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ test("printing_unittests") {
"//ui/gfx/geometry",
]

data = [ "../pdf/test/data/" ]
data = [
"test/data/",
"../pdf/test/data/",
]

if (is_win || is_mac) {
sources += [ "printed_document_unittest.cc" ]
Expand Down
74 changes: 74 additions & 0 deletions printing/pdf_metafile_cg_mac_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
#include <vector>

#include "base/files/file_util.h"
#include "base/hash/sha1.h"
#include "base/path_service.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/rect.h"

namespace printing {
Expand All @@ -26,6 +28,17 @@ base::FilePath GetPdfTestData(const base::FilePath::StringType& filename) {
return root_path.Append("pdf").Append("test").Append("data").Append(filename);
}

base::FilePath GetPrintingTestData(const base::FilePath::StringType& filename) {
base::FilePath root_path;
if (!base::PathService::Get(base::DIR_SOURCE_ROOT, &root_path))
return base::FilePath();
return root_path.Append("printing")
.Append("test")
.Append("data")
.Append("pdf_cg")
.Append(filename);
}

} // namespace

TEST(PdfMetafileCgTest, Pdf) {
Expand Down Expand Up @@ -111,4 +124,65 @@ TEST(PdfMetafileCgTest, GetPageBounds) {
}
}

TEST(PdfMetafileCgTest, RenderPageBasic) {
// Get test data.
base::FilePath pdf_file = GetPdfTestData("rectangles.pdf");
ASSERT_FALSE(pdf_file.empty());
std::string pdf_data;
ASSERT_TRUE(base::ReadFileToString(pdf_file, &pdf_data));

base::FilePath expected_png_file =
GetPrintingTestData("rectangles_cg_expected.pdf.0.png");
ASSERT_FALSE(expected_png_file.empty());
std::string expected_png_data;
ASSERT_TRUE(base::ReadFileToString(expected_png_file, &expected_png_data));

// Initialize and check metafile.
constexpr int kExpectedWidth = 200;
constexpr int kExpectedHeight = 300;
PdfMetafileCg pdf_cg;
ASSERT_TRUE(pdf_cg.InitFromData(pdf_data.data(), pdf_data.size()));
ASSERT_EQ(1u, pdf_cg.GetPageCount());
gfx::Rect bounds = pdf_cg.GetPageBounds(1);
ASSERT_EQ(0, bounds.x());
ASSERT_EQ(0, bounds.y());
ASSERT_EQ(kExpectedWidth, bounds.width());
ASSERT_EQ(kExpectedHeight, bounds.height());

// Set up rendering context.
constexpr size_t kBitsPerComponent = 8;
constexpr size_t kBytesPerPixel = 4;
constexpr size_t kStride = kExpectedWidth * kBytesPerPixel;
std::vector<uint8_t> rendered_bitmap(kStride * kExpectedHeight);
base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
CGColorSpaceCreateDeviceRGB());
base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate(
rendered_bitmap.data(), kExpectedWidth, kExpectedHeight,
kBitsPerComponent, kStride, color_space,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little));

// Render using metafile and calculate the output hash.
Metafile::MacRenderPageParams params;
params.autorotate = true;
ASSERT_TRUE(pdf_cg.RenderPage(1, context, bounds.ToCGRect(), params));
std::array<uint8_t, base::kSHA1Length> rendered_hash =
base::SHA1HashSpan(rendered_bitmap);

// Decode expected PNG and calculate the output hash.
std::vector<uint8_t> expected_png_bitmap;
int png_width;
int png_height;
ASSERT_TRUE(gfx::PNGCodec::Decode(
reinterpret_cast<const uint8_t*>(expected_png_data.data()),
expected_png_data.size(), gfx::PNGCodec::FORMAT_BGRA,
&expected_png_bitmap, &png_width, &png_height));
ASSERT_EQ(kExpectedWidth, png_width);
ASSERT_EQ(kExpectedHeight, png_height);
std::array<uint8_t, base::kSHA1Length> expected_hash =
base::SHA1HashSpan(expected_png_bitmap);

// Make sure the hashes match.
EXPECT_EQ(expected_hash, rendered_hash);
}

} // namespace printing
6 changes: 6 additions & 0 deletions printing/test/data/pdf_cg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pdf_cg test expectations
---
The PNG files in this directory are the CoreGraphics rendering outputs for PDFs
in //pdf/test/data/. They are generated from raw bitmaps using
gfx::PNGCodec::Encode() using the |gfx::PNGCodec::FORMAT_BGRA| format. The PNGs
are further optimized with optipng.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 670bf3b

Please sign in to comment.