1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/nine_image_painter.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gfx/canvas.h"
9 #include "ui/gfx/geometry/insets.h"
10 #include "ui/gfx/geometry/rect.h"
11 #include "ui/gfx/image/image_skia.h"
15 TEST(NineImagePainterTest
, GetSubsetRegions
) {
17 src
.allocN32Pixels(40, 50);
18 const ImageSkia
image_skia(ImageSkiaRep(src
, 1.0));
19 const Insets
insets(1, 2, 3, 4);
20 std::vector
<Rect
> rects
;
21 NineImagePainter::GetSubsetRegions(image_skia
, insets
, &rects
);
22 ASSERT_EQ(9u, rects
.size());
23 EXPECT_EQ(gfx::Rect(0, 0, 2, 1), rects
[0]);
24 EXPECT_EQ(gfx::Rect(2, 0, 34, 1), rects
[1]);
25 EXPECT_EQ(gfx::Rect(36, 0, 4, 1), rects
[2]);
26 EXPECT_EQ(gfx::Rect(0, 1, 2, 46), rects
[3]);
27 EXPECT_EQ(gfx::Rect(2, 1, 34, 46), rects
[4]);
28 EXPECT_EQ(gfx::Rect(36, 1, 4, 46), rects
[5]);
29 EXPECT_EQ(gfx::Rect(0, 47, 2, 3), rects
[6]);
30 EXPECT_EQ(gfx::Rect(2, 47, 34, 3), rects
[7]);
31 EXPECT_EQ(gfx::Rect(36, 47, 4, 3), rects
[8]);
34 TEST(NineImagePainterTest
, PaintScale
) {
36 src
.allocN32Pixels(100, 100);
37 src
.eraseColor(SK_ColorRED
);
38 src
.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN
);
40 gfx::ImageSkia
image(gfx::ImageSkiaRep(src
, 0.0f
));
41 gfx::Insets
insets(10, 10, 10, 10);
42 gfx::NineImagePainter
painter(image
, insets
);
45 bool is_opaque
= true;
46 gfx::Canvas
canvas(gfx::Size(400, 400), image_scale
, is_opaque
);
49 gfx::Rect
bounds(0, 0, 100, 100);
50 painter
.Paint(&canvas
, bounds
);
53 const SkISize size
= canvas
.sk_canvas()->getDeviceSize();
54 result
.allocN32Pixels(size
.width(), size
.height());
55 canvas
.sk_canvas()->readPixels(&result
, 0, 0);
57 SkIRect green_rect
= SkIRect::MakeLTRB(40, 20, 360, 180);
58 for (int y
= 0; y
< 200; y
++) {
59 for (int x
= 0; x
< 400; x
++) {
60 if (green_rect
.contains(x
, y
)) {
61 EXPECT_EQ(SK_ColorGREEN
, result
.getColor(x
, y
));
63 EXPECT_EQ(SK_ColorRED
, result
.getColor(x
, y
));