Skip to content

Commit

Permalink
[pdf] Don't mutate SkBitmap in tests
Browse files Browse the repository at this point in the history
Replaces mutating SkBitmap operations in PDF unit tests with SkCanvas
operations on an intermediate SkSurface, which is then converted to an
SkBitmap. A future change will make the SkBitmaps immutable.

Bug: 1284255
Change-Id: I6453a49a01b837bc81022694c248c576e45c11cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3615537
Commit-Queue: K. Moon <kmoon@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#997986}
  • Loading branch information
kmoon-work authored and Chromium LUCI CQ committed Apr 29, 2022
1 parent a2cd118 commit adbd438
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
27 changes: 20 additions & 7 deletions pdf/paint_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/skia_conversions.h"
Expand Down Expand Up @@ -111,9 +113,15 @@ class PaintManagerTest : public testing::Test {
SkBitmap snapshot_bitmap;
ASSERT_TRUE(snapshot->asLegacyBitmap(&snapshot_bitmap));

SkBitmap expected_bitmap =
CreateSkiaImageForTesting(plugin_size, SK_ColorMAGENTA);
expected_bitmap.erase(SK_ColorRED, gfx::RectToSkIRect(overlapped_rect));
sk_sp<SkSurface> expected_surface =
CreateSkiaSurfaceForTesting(plugin_size, SK_ColorMAGENTA);
expected_surface->getCanvas()->clipIRect(
gfx::RectToSkIRect(overlapped_rect));
expected_surface->getCanvas()->clear(SK_ColorRED);

SkBitmap expected_bitmap;
ASSERT_TRUE(expected_surface->makeImageSnapshot()->asLegacyBitmap(
&expected_bitmap));

EXPECT_TRUE(
cc::MatchesBitmap(snapshot_bitmap, expected_bitmap,
Expand All @@ -128,10 +136,15 @@ class PaintManagerTest : public testing::Test {
ASSERT_GE(plugin_size.width(), 4);
ASSERT_GE(plugin_size.height(), 4);

SkBitmap initial_bitmap =
CreateSkiaImageForTesting(plugin_size, SK_ColorRED);
initial_bitmap.erase(SK_ColorGREEN, {1, 1, plugin_size.width() - 1,
plugin_size.height() - 2});
sk_sp<SkSurface> initial_surface =
CreateSkiaSurfaceForTesting(plugin_size, SK_ColorRED);
initial_surface->getCanvas()->clipIRect(SkIRect::MakeLTRB(
1, 1, plugin_size.width() - 1, plugin_size.height() - 2));
initial_surface->getCanvas()->clear(SK_ColorGREEN);

SkBitmap initial_bitmap;
ASSERT_TRUE(
initial_surface->makeImageSnapshot()->asLegacyBitmap(&initial_bitmap));

paint_manager_.Invalidate();
ASSERT_TRUE(
Expand Down
14 changes: 11 additions & 3 deletions pdf/pdf_view_web_plugin_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
#include "third_party/blink/public/web/web_plugin_container.h"
#include "third_party/blink/public/web/web_plugin_params.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "ui/base/cursor/cursor.h"
#include "ui/events/blink/blink_event_util.h"
#include "ui/events/keycodes/dom/dom_code.h"
Expand Down Expand Up @@ -118,9 +121,14 @@ MATCHER_P(IsExpectedImeKeyEvent, expected_text, "") {
// clipped area and `kDefaultColor` as the background color.
SkBitmap GenerateExpectedBitmapForPaint(const gfx::Rect& expected_clipped_rect,
SkColor paint_color) {
SkBitmap expected_bitmap =
CreateSkiaImageForTesting(kCanvasSize, kDefaultColor);
expected_bitmap.erase(paint_color, gfx::RectToSkIRect(expected_clipped_rect));
sk_sp<SkSurface> expected_surface =
CreateSkiaSurfaceForTesting(kCanvasSize, kDefaultColor);
expected_surface->getCanvas()->clipIRect(
gfx::RectToSkIRect(expected_clipped_rect));
expected_surface->getCanvas()->clear(paint_color);

SkBitmap expected_bitmap;
expected_surface->makeImageSnapshot()->asLegacyBitmap(&expected_bitmap);
return expected_bitmap;
}

Expand Down
18 changes: 15 additions & 3 deletions pdf/test/test_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
#include "cc/test/pixel_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.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 "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "ui/gfx/geometry/size.h"

namespace chrome_pdf {

Expand Down Expand Up @@ -43,10 +46,19 @@ testing::AssertionResult MatchesPngFile(
return testing::AssertionSuccess();
}

sk_sp<SkSurface> CreateSkiaSurfaceForTesting(const gfx::Size& size,
SkColor color) {
auto surface = SkSurface::MakeRasterN32Premul(size.width(), size.height());
surface->getCanvas()->clear(color);
return surface;
}

SkBitmap CreateSkiaImageForTesting(const gfx::Size& size, SkColor color) {
sk_sp<SkImage> snapshot =
CreateSkiaSurfaceForTesting(size, color)->makeImageSnapshot();

SkBitmap bitmap;
bitmap.allocPixels(SkImageInfo::MakeN32Premul(gfx::SizeToSkISize(size)));
bitmap.eraseColor(color);
snapshot->asLegacyBitmap(&bitmap);
return bitmap;
}

Expand Down
8 changes: 7 additions & 1 deletion pdf/test/test_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#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/SkRefCnt.h"

class SkBitmap;
class SkImage;
class SkSurface;

namespace gfx {
class Size;
Expand All @@ -28,7 +30,11 @@ 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 surface with dimensions `size` and filled with `color`.
sk_sp<SkSurface> CreateSkiaSurfaceForTesting(const gfx::Size& size,
SkColor color);

// Creates a Skia image with dimensions `size` and filled with `color`.
SkBitmap CreateSkiaImageForTesting(const gfx::Size& size, SkColor color);

} // namespace chrome_pdf
Expand Down

0 comments on commit adbd438

Please sign in to comment.