Chromium Code Reviews| Index: ui/gfx/nine_image_painter_unittest.cc |
| diff --git a/ui/gfx/nine_image_painter_unittest.cc b/ui/gfx/nine_image_painter_unittest.cc |
| index 3394c5e9f736f01b0e0f7f520939d67a15a8cc10..ac379ebf167df76bdb456ab6a8daad73d77c5554 100644 |
| --- a/ui/gfx/nine_image_painter_unittest.cc |
| +++ b/ui/gfx/nine_image_painter_unittest.cc |
| @@ -4,14 +4,27 @@ |
| #include "ui/gfx/nine_image_painter.h" |
| +#include "base/base64.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "ui/gfx/canvas.h" |
| +#include "ui/gfx/codec/png_codec.h" |
| #include "ui/gfx/geometry/insets.h" |
| #include "ui/gfx/geometry/rect.h" |
| #include "ui/gfx/image/image_skia.h" |
| namespace gfx { |
| +static std::string GetPNGDataUrl(const SkBitmap& bitmap) { |
| + std::vector<unsigned char> png_data; |
| + gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data); |
| + std::string data_url; |
| + data_url.insert(data_url.end(), png_data.begin(), png_data.end()); |
| + base::Base64Encode(data_url, &data_url); |
| + data_url.insert(0, "data:image/png;base64,"); |
| + |
| + return data_url; |
| +} |
| + |
| TEST(NineImagePainterTest, GetSubsetRegions) { |
| SkBitmap src; |
| src.allocN32Pixels(40, 50); |
| @@ -31,22 +44,23 @@ TEST(NineImagePainterTest, GetSubsetRegions) { |
| EXPECT_EQ(gfx::Rect(36, 47, 4, 3), rects[8]); |
| } |
| -TEST(NineImagePainterTest, PaintScale) { |
| +TEST(NineImagePainterTest, PaintHighDPI) { |
| SkBitmap src; |
| src.allocN32Pixels(100, 100); |
| src.eraseColor(SK_ColorRED); |
| src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN); |
| - gfx::ImageSkia image(gfx::ImageSkiaRep(src, 0.0f)); |
| + float image_scale = 2.f; |
| + |
| + gfx::ImageSkia image(gfx::ImageSkiaRep(src, image_scale)); |
| gfx::Insets insets(10, 10, 10, 10); |
| gfx::NineImagePainter painter(image, insets); |
| - int image_scale = 2; |
| bool is_opaque = true; |
| - gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque); |
| - canvas.Scale(2, 1); |
| + gfx::Canvas canvas(gfx::Size(100, 100), image_scale, is_opaque); |
| + canvas.Translate(gfx::Vector2d(20, 10)); |
| - gfx::Rect bounds(0, 0, 100, 100); |
| + gfx::Rect bounds(0, 0, 50, 50); |
| painter.Paint(&canvas, bounds); |
| SkBitmap result; |
| @@ -54,13 +68,18 @@ TEST(NineImagePainterTest, PaintScale) { |
| result.allocN32Pixels(size.width(), size.height()); |
| canvas.sk_canvas()->readPixels(&result, 0, 0); |
| - SkIRect green_rect = SkIRect::MakeLTRB(40, 20, 360, 180); |
| - for (int y = 0; y < 200; y++) { |
| - for (int x = 0; x < 400; x++) { |
| - if (green_rect.contains(x, y)) { |
| - ASSERT_EQ(SK_ColorGREEN, result.getColor(x, y)); |
| + gfx::Vector2d paint_offset(40, 20); // Translated 20,10 with 2x dpi. |
|
Peter Kasting
2015/10/29 21:26:15
Why not store the original translation vector from
danakj
2015/10/29 22:15:12
Sure, done. Except the negative one is weird, I th
|
| + gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset; |
| + for (int y = paint_offset.y(); y < 100 + paint_offset.y(); y++) { |
|
Peter Kasting
2015/10/29 21:26:15
You repeat this loop several times; factor out int
danakj
2015/10/29 22:15:12
Done.
|
| + SCOPED_TRACE(y); |
| + for (int x = paint_offset.x(); x < 100 + paint_offset.x(); x++) { |
| + SCOPED_TRACE(x); |
| + if (green_rect.Contains(x, y)) { |
| + ASSERT_EQ(SK_ColorGREEN, result.getColor(x, y)) |
| + << "Output image:\n" << GetPNGDataUrl(result); |
| } else { |
| - ASSERT_EQ(SK_ColorRED, result.getColor(x, y)); |
| + ASSERT_EQ(SK_ColorRED, result.getColor(x, y)) << "Output image:\n" |
| + << GetPNGDataUrl(result); |
| } |
| } |
| } |
| @@ -136,23 +155,24 @@ TEST(NineImagePainterTest, PaintWithBoundOffset) { |
| } |
| } |
| -TEST(NineImagePainterTest, PaintWithNagativeScale) { |
| +TEST(NineImagePainterTest, PaintWithScale) { |
| SkBitmap src; |
| src.allocN32Pixels(100, 100); |
| src.eraseColor(SK_ColorRED); |
| src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN); |
| - gfx::ImageSkia image(gfx::ImageSkiaRep(src, 0.0f)); |
| + float image_scale = 2.f; |
| + |
| + gfx::ImageSkia image(gfx::ImageSkiaRep(src, image_scale)); |
| gfx::Insets insets(10, 10, 10, 10); |
| gfx::NineImagePainter painter(image, insets); |
| - int image_scale = 2; |
| bool is_opaque = true; |
| gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque); |
| - canvas.Translate(gfx::Vector2d(200, 200)); |
| - canvas.Scale(-2, -1); |
| + canvas.Translate(gfx::Vector2d(20, 10)); |
| + canvas.Scale(2, 1); |
| - gfx::Rect bounds(0, 0, 100, 100); |
| + gfx::Rect bounds(0, 0, 50, 50); |
| painter.Paint(&canvas, bounds); |
| SkBitmap result; |
| @@ -160,13 +180,63 @@ TEST(NineImagePainterTest, PaintWithNagativeScale) { |
| result.allocN32Pixels(size.width(), size.height()); |
| canvas.sk_canvas()->readPixels(&result, 0, 0); |
| - SkIRect green_rect = SkIRect::MakeLTRB(40, 220, 360, 380); |
| - for (int y = 200; y < 400; y++) { |
| - for (int x = 0; x < 400; x++) { |
| - if (green_rect.contains(x, y)) { |
| - ASSERT_EQ(SK_ColorGREEN, result.getColor(x, y)); |
| + gfx::Vector2d paint_offset(40, 20); // Translated 20,10 with 2x dpi. |
| + gfx::Rect green_rect = gfx::Rect(20, 10, 160, 80) + paint_offset; |
| + for (int y = paint_offset.y(); y < 100 + paint_offset.y(); y++) { |
| + SCOPED_TRACE(y); |
| + for (int x = paint_offset.x(); x < 200 + paint_offset.x(); x++) { |
| + SCOPED_TRACE(x); |
| + if (green_rect.Contains(x, y)) { |
| + ASSERT_EQ(SK_ColorGREEN, result.getColor(x, y)) |
| + << "Output image:\n" << GetPNGDataUrl(result); |
| } else { |
| - ASSERT_EQ(SK_ColorRED, result.getColor(x, y)); |
| + ASSERT_EQ(SK_ColorRED, result.getColor(x, y)) << "Output image:\n" |
| + << GetPNGDataUrl(result); |
| + } |
| + } |
| + } |
| +} |
| + |
| +TEST(NineImagePainterTest, PaintWithNegativeScale) { |
| + SkBitmap src; |
| + src.allocN32Pixels(100, 100); |
| + src.eraseColor(SK_ColorRED); |
| + src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN); |
| + |
| + float image_scale = 2.f; |
| + |
| + gfx::ImageSkia image(gfx::ImageSkiaRep(src, image_scale)); |
| + gfx::Insets insets(10, 10, 10, 10); |
| + gfx::NineImagePainter painter(image, insets); |
| + |
| + bool is_opaque = true; |
| + gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque); |
| + canvas.Translate(gfx::Vector2d(70, 60)); |
| + canvas.Scale(-1, -1); |
| + |
| + gfx::Rect bounds(0, 0, 50, 50); |
| + painter.Paint(&canvas, bounds); |
| + |
| + SkBitmap result; |
| + const SkISize size = canvas.sk_canvas()->getDeviceSize(); |
| + result.allocN32Pixels(size.width(), size.height()); |
| + canvas.sk_canvas()->readPixels(&result, 0, 0); |
| + |
| + // The painting space is 50x50 and the scale of -1m-1 means an offset of 50,50 |
| + // would put the output in the top left corner. Since the offset is 70,60 it |
| + // moves by 20,10. Since the output is 2x DPI it will become offset by 40,20. |
| + gfx::Vector2d paint_offset(40, 20); |
| + gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset; |
| + for (int y = paint_offset.y(); y < 100 + paint_offset.y(); y++) { |
| + SCOPED_TRACE(y); |
| + for (int x = paint_offset.x(); x < 100 + paint_offset.x(); x++) { |
| + SCOPED_TRACE(x); |
| + if (green_rect.Contains(x, y)) { |
| + ASSERT_EQ(SK_ColorGREEN, result.getColor(x, y)) |
| + << "Output image:\n" << GetPNGDataUrl(result); |
| + } else { |
| + ASSERT_EQ(SK_ColorRED, result.getColor(x, y)) << "Output image:\n" |
| + << GetPNGDataUrl(result); |
| } |
| } |
| } |