0

Add chrome_pdf::MatchesPngFile() utility

Adds a MatchesPngFile() utility wrapper around cc::MatchesPNGFile() that
tests if an SkImage matches a PNG file within //pdf/test/data.

Bug: 1317832
Change-Id: I0ae8322e2530736378c3fb54e855b5d467fb196d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3597663
Reviewed-by: Sunny Sachanandani <sunnyps@chromium.org>
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: K. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/main@{#994456}
This commit is contained in:
K. Moon
2022-04-21 00:26:19 +00:00
committed by Chromium LUCI CQ
parent 9164d056da
commit d7fda8135e
6 changed files with 45 additions and 17 deletions

@ -331,6 +331,7 @@ if (enable_pdf) {
":internal", ":internal",
":ppapi_migration", ":ppapi_migration",
"//base", "//base",
"//cc:test_support",
"//testing/gmock", "//testing/gmock",
"//testing/gtest", "//testing/gtest",
"//third_party/blink/public/common:headers", "//third_party/blink/public/common:headers",

@ -12,8 +12,6 @@
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "cc/test/pixel_comparator.h"
#include "cc/test/pixel_test_utils.h"
#include "pdf/accessibility_structs.h" #include "pdf/accessibility_structs.h"
#include "pdf/pdfium/pdfium_engine.h" #include "pdf/pdfium/pdfium_engine.h"
#include "pdf/pdfium/pdfium_test_base.h" #include "pdf/pdfium/pdfium_test_base.h"
@ -23,7 +21,10 @@
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h" #include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/pdfium/public/fpdf_formfill.h" #include "third_party/pdfium/public/fpdf_formfill.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkPixmap.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "ui/accessibility/accessibility_features.h" #include "ui/accessibility/accessibility_features.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h" #include "ui/gfx/geometry/rect_f.h"
@ -776,17 +777,14 @@ class PDFiumPageThumbnailTest : public PDFiumTestBase {
ASSERT_GT(stride, 0); ASSERT_GT(stride, 0);
ASSERT_EQ(image_info.minRowBytes(), static_cast<size_t>(stride)); ASSERT_EQ(image_info.minRowBytes(), static_cast<size_t>(stride));
std::vector<uint8_t> data = thumbnail.TakeData(); std::vector<uint8_t> data = thumbnail.TakeData();
SkBitmap bitmap; sk_sp<SkImage> image = SkImage::MakeRasterCopy(
EXPECT_TRUE(bitmap.installPixels(image_info, data.data(), SkPixmap(image_info, data.data(), image_info.minRowBytes()));
image_info.minRowBytes())); ASSERT_TRUE(image);
base::FilePath expectation_png_file_path = GetThumbnailTestData( base::FilePath expectation_png_file_path = GetThumbnailTestData(
expectation_file_prefix, page_index, device_pixel_ratio); expectation_file_prefix, page_index, device_pixel_ratio);
EXPECT_TRUE(cc::MatchesPNGFile( EXPECT_TRUE(MatchesPngFile(image.get(), expectation_png_file_path));
bitmap, GetTestDataFilePath(expectation_png_file_path),
cc::ExactPixelComparator(/*discard_alpha=*/false)))
<< "Reference: " << expectation_png_file_path;
} }
}; };

@ -8,7 +8,6 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "cc/test/pixel_test_utils.h"
#include "pdf/pdfium/pdfium_engine.h" #include "pdf/pdfium/pdfium_engine.h"
#include "pdf/pdfium/pdfium_engine_exports.h" #include "pdf/pdfium/pdfium_engine_exports.h"
#include "pdf/pdfium/pdfium_test_base.h" #include "pdf/pdfium/pdfium_test_base.h"
@ -20,7 +19,9 @@
#include "third_party/abseil-cpp/absl/types/optional.h" #include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/blink/public/web/web_print_params.h" #include "third_party/blink/public/web/web_print_params.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkImageInfo.h" #include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size_f.h" #include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/geometry/skia_conversions.h" #include "ui/gfx/geometry/skia_conversions.h"
@ -39,8 +40,8 @@ constexpr gfx::Rect kPrintableAreaRect = {{18, 18}, {576, 733}};
using ExpectedDimensions = std::vector<gfx::SizeF>; using ExpectedDimensions = std::vector<gfx::SizeF>;
base::FilePath GetReferenceFilePath(base::StringPiece test_filename) { base::FilePath GetReferenceFilePath(base::StringPiece test_filename) {
return GetTestDataFilePath(base::FilePath(FILE_PATH_LITERAL("pdfium_print")) return base::FilePath(FILE_PATH_LITERAL("pdfium_print"))
.AppendASCII(test_filename)); .AppendASCII(test_filename);
} }
blink::WebPrintParams GetDefaultPrintParams() { blink::WebPrintParams GetDefaultPrintParams() {
@ -97,10 +98,8 @@ void CheckPdfRendering(const std::vector<uint8_t>& pdf_data,
ASSERT_TRUE(exports.RenderPDFPageToBitmap(pdf_data, page_number, settings, ASSERT_TRUE(exports.RenderPDFPageToBitmap(pdf_data, page_number, settings,
page_bitmap.getPixels())); page_bitmap.getPixels()));
EXPECT_TRUE(cc::MatchesPNGFile( EXPECT_TRUE(MatchesPngFile(page_bitmap.asImage().get(),
page_bitmap, GetReferenceFilePath(expected_png_filename), GetReferenceFilePath(expected_png_filename)));
cc::ExactPixelComparator(/*discard_alpha=*/false)))
<< "Reference: " << expected_png_filename;
} }
} // namespace } // namespace

3
pdf/test/DEPS Normal file

@ -0,0 +1,3 @@
include_rules = [
"+cc/test",
]

@ -7,9 +7,13 @@
#include "base/base_paths.h" #include "base/base_paths.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "cc/test/pixel_comparator.h"
#include "cc/test/pixel_test_utils.h"
#include "pdf/ppapi_migration/bitmap.h" #include "pdf/ppapi_migration/bitmap.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkImage.h"
#include "ui/gfx/geometry/skia_conversions.h" #include "ui/gfx/geometry/skia_conversions.h"
namespace chrome_pdf { namespace chrome_pdf {
@ -25,6 +29,21 @@ base::FilePath GetTestDataFilePath(const base::FilePath& path) {
.Append(path); .Append(path);
} }
testing::AssertionResult MatchesPngFile(
const SkImage* actual_image,
const base::FilePath& expected_png_file) {
SkBitmap actual_bitmap;
if (!actual_image->asLegacyBitmap(&actual_bitmap))
return testing::AssertionFailure() << "Reference: " << expected_png_file;
if (!cc::MatchesPNGFile(actual_bitmap, GetTestDataFilePath(expected_png_file),
cc::ExactPixelComparator(/*discard_alpha=*/false))) {
return testing::AssertionFailure() << "Reference: " << expected_png_file;
}
return testing::AssertionSuccess();
}
SkBitmap CreateSkiaImageForTesting(const gfx::Size& size, SkColor color) { SkBitmap CreateSkiaImageForTesting(const gfx::Size& size, SkColor color) {
SkBitmap bitmap = CreateN32PremulSkBitmap(gfx::SizeToSkISize(size)); SkBitmap bitmap = CreateN32PremulSkBitmap(gfx::SizeToSkISize(size));
bitmap.eraseColor(color); bitmap.eraseColor(color);

@ -6,9 +6,11 @@
#define PDF_TEST_TEST_HELPERS_H_ #define PDF_TEST_TEST_HELPERS_H_
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
class SkBitmap; class SkBitmap;
class SkImage;
namespace gfx { namespace gfx {
class Size; class Size;
@ -20,6 +22,12 @@ namespace chrome_pdf {
// the empty path if the source root can't be found. // the empty path if the source root can't be found.
base::FilePath GetTestDataFilePath(const base::FilePath& path); base::FilePath GetTestDataFilePath(const base::FilePath& path);
// Matches `actual_image` against the PNG at the file path `expected_png_file`.
// The path must be relative to //pdf/test/data.
testing::AssertionResult MatchesPngFile(
const SkImage* actual_image,
const base::FilePath& expected_png_file);
// Creates a Skia-format `Image` of a given size filled with a given color. // Creates a Skia-format `Image` of a given size filled with a given color.
SkBitmap CreateSkiaImageForTesting(const gfx::Size& size, SkColor color); SkBitmap CreateSkiaImageForTesting(const gfx::Size& size, SkColor color);