Replace custom functions with gfx::test::CreateBitmap and others
CreateBitmap, CreateImageSkia and CreateImage APIs are changed with a
new function overload to take a single size parameter. All of them are
also changed by adding an optional color parameter. Then, custom
functions used across the codebase are replaced with these functions.
Bug: 1497199
Change-Id: I5debbb8735215470b0206ef7bdd9c57977cb77d7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4863568
Reviewed-by: Daniel Cheng <[email protected]>
Reviewed-by: Sylvain Defresne <[email protected]>
Reviewed-by: James Cook <[email protected]>
Reviewed-by: Avi Drissman <[email protected]>
Commit-Queue: Henrique Ferreiro <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1232685}
diff --git a/ui/gfx/icon_util_unittest.cc b/ui/gfx/icon_util_unittest.cc
index 0e2ff57..313c2a1 100644
--- a/ui/gfx/icon_util_unittest.cc
+++ b/ui/gfx/icon_util_unittest.cc
@@ -19,6 +19,7 @@
#include "ui/gfx/icon_util_unittests_resource.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_family.h"
+#include "ui/gfx/image/image_unittest_util.h"
namespace {
@@ -209,10 +210,8 @@
temp_directory_.GetPath().AppendASCII("<>?.ico");
// Invalid file name.
- SkBitmap bitmap;
image_family.clear();
- bitmap.allocN32Pixels(1, 1);
- image_family.Add(gfx::Image::CreateFrom1xBitmap(bitmap));
+ image_family.Add(gfx::test::CreateImage(/*size=*/1));
EXPECT_FALSE(IconUtil::CreateIconFileFromImageFamily(image_family,
invalid_icon_filename));
EXPECT_FALSE(base::PathExists(invalid_icon_filename));
diff --git a/ui/gfx/image/image_unittest_util.cc b/ui/gfx/image/image_unittest_util.cc
index c8787bb..dcafb094 100644
--- a/ui/gfx/image/image_unittest_util.cc
+++ b/ui/gfx/image/image_unittest_util.cc
@@ -43,15 +43,23 @@
} // namespace
-const SkBitmap CreateBitmap(int width, int height) {
+const SkBitmap CreateBitmap(int size, SkColor color) {
+ return CreateBitmap(size, size, color);
+}
+
+const SkBitmap CreateBitmap(int width, int height, SkColor color) {
SkBitmap bitmap;
bitmap.allocN32Pixels(width, height);
- bitmap.eraseARGB(255, 0, 255, 0);
+ bitmap.eraseColor(color);
return bitmap;
}
-gfx::ImageSkia CreateImageSkia(int width, int height) {
- return gfx::ImageSkia::CreateFrom1xBitmap(CreateBitmap(width, height));
+gfx::ImageSkia CreateImageSkia(int size, SkColor color) {
+ return CreateImageSkia(size, size, color);
+}
+
+gfx::ImageSkia CreateImageSkia(int width, int height, SkColor color) {
+ return gfx::ImageSkia::CreateFrom1xBitmap(CreateBitmap(width, height, color));
}
scoped_refptr<base::RefCountedMemory> CreatePNGBytes(int edge_size) {
@@ -65,8 +73,12 @@
return CreateImage(100, 50);
}
-gfx::Image CreateImage(int width, int height) {
- return gfx::Image::CreateFrom1xBitmap(CreateBitmap(width, height));
+gfx::Image CreateImage(int size, SkColor color) {
+ return CreateImage(size, size, color);
+}
+
+gfx::Image CreateImage(int width, int height, SkColor color) {
+ return gfx::Image::CreateFrom1xBitmap(CreateBitmap(width, height, color));
}
bool AreImagesEqual(const gfx::Image& img1, const gfx::Image& img2) {
diff --git a/ui/gfx/image/image_unittest_util.h b/ui/gfx/image/image_unittest_util.h
index 71e9442..d9dfa7d 100644
--- a/ui/gfx/image/image_unittest_util.h
+++ b/ui/gfx/image/image_unittest_util.h
@@ -26,19 +26,31 @@
typedef gfx::ImageSkia PlatformImage;
#endif
-// Create a bitmap of |width|x|height|.
-const SkBitmap CreateBitmap(int width, int height);
+// Create a bitmap of `size`x`size` and color `color`.
+const SkBitmap CreateBitmap(int size, SkColor color = SK_ColorGREEN);
-// Creates an ImageSkia of |width|x|height| DIP with bitmap data for an
-// arbitrary scale factor.
-gfx::ImageSkia CreateImageSkia(int width, int height);
+// Create a bitmap of `width`x`height` and color `color`.
+const SkBitmap CreateBitmap(int width,
+ int height,
+ SkColor color = SK_ColorGREEN);
+
+// Creates an ImageSkia of `size`x`size` DIP and color `color` with bitmap
+// data for an arbitrary scale factor.
+gfx::ImageSkia CreateImageSkia(int size, SkColor color = SK_ColorGREEN);
+
+// Creates an ImageSkia of `width`x`height` DIP and color `color` with bitmap
+// data for an arbitrary scale factor.
+gfx::ImageSkia CreateImageSkia(int width,
+ int height,
+ SkColor color = SK_ColorGREEN);
// Returns PNG encoded bytes for a bitmap of |edge_size|x|edge_size|.
scoped_refptr<base::RefCountedMemory> CreatePNGBytes(int edge_size);
// TODO(rohitrao): Remove the no-argument version of CreateImage().
gfx::Image CreateImage();
-gfx::Image CreateImage(int width, int height);
+gfx::Image CreateImage(int size, SkColor color = SK_ColorGREEN);
+gfx::Image CreateImage(int width, int height, SkColor color = SK_ColorGREEN);
// Returns true if the images are equal. Converts the images to ImageSkia to
// compare them.
diff --git a/ui/gfx/nine_image_painter_unittest.cc b/ui/gfx/nine_image_painter_unittest.cc
index 0e13d7c..c252fbc3 100644
--- a/ui/gfx/nine_image_painter_unittest.cc
+++ b/ui/gfx/nine_image_painter_unittest.cc
@@ -14,6 +14,7 @@
#include "ui/gfx/geometry/vector2d_conversions.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
+#include "ui/gfx/image/image_unittest_util.h"
namespace gfx {
@@ -66,9 +67,7 @@
}
TEST(NineImagePainterTest, PaintHighDPI) {
- SkBitmap src;
- src.allocN32Pixels(100, 100);
- src.eraseColor(SK_ColorRED);
+ SkBitmap src = gfx::test::CreateBitmap(/*size=*/100, SK_ColorRED);
src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN);
float image_scale = 2.f;
@@ -100,9 +99,7 @@
// The NineImagePainter should not paint outside the bounds.
// The border images should be cropped, but still painted.
- SkBitmap src;
- src.allocN32Pixels(6, 6);
- src.eraseColor(SK_ColorGREEN);
+ SkBitmap src = gfx::test::CreateBitmap(/*size=*/6, SK_ColorGREEN);
src.erase(SK_ColorRED, SkIRect::MakeXYWH(2, 2, 2, 2));
gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(src);
@@ -132,9 +129,7 @@
}
TEST(NineImagePainterTest, PaintWithBoundOffset) {
- SkBitmap src;
- src.allocN32Pixels(10, 10);
- src.eraseColor(SK_ColorRED);
+ SkBitmap src = gfx::test::CreateBitmap(/*size=*/10, SK_ColorRED);
src.eraseArea(SkIRect::MakeXYWH(1, 1, 8, 8), SK_ColorGREEN);
gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(src);
@@ -162,9 +157,7 @@
}
TEST(NineImagePainterTest, PaintWithScale) {
- SkBitmap src;
- src.allocN32Pixels(100, 100);
- src.eraseColor(SK_ColorRED);
+ SkBitmap src = gfx::test::CreateBitmap(/*size=*/100, SK_ColorRED);
src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN);
float image_scale = 2.f;
@@ -193,9 +186,7 @@
}
TEST(NineImagePainterTest, PaintWithNegativeScale) {
- SkBitmap src;
- src.allocN32Pixels(100, 100);
- src.eraseColor(SK_ColorRED);
+ SkBitmap src = gfx::test::CreateBitmap(/*size=*/100, SK_ColorRED);
src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN);
float image_scale = 2.f;