Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: ui/gfx/nine_image_painter_unittest.cc

Issue 1505393002: ui: Fix NinePatchPainter to work in physical pixels. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2526
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/nine_image_painter.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/nine_image_painter.h" 5 #include "ui/gfx/nine_image_painter.h"
6 6
7 #include "base/base64.h"
7 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gfx/canvas.h" 9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/codec/png_codec.h"
9 #include "ui/gfx/geometry/insets.h" 11 #include "ui/gfx/geometry/insets.h"
10 #include "ui/gfx/geometry/rect.h" 12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/geometry/vector2d.h"
14 #include "ui/gfx/geometry/vector2d_conversions.h"
11 #include "ui/gfx/image/image_skia.h" 15 #include "ui/gfx/image/image_skia.h"
12 16
13 namespace gfx { 17 namespace gfx {
14 18
19 static std::string GetPNGDataUrl(const SkBitmap& bitmap) {
20 std::vector<unsigned char> png_data;
21 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data);
22 std::string data_url;
23 data_url.insert(data_url.end(), png_data.begin(), png_data.end());
24 base::Base64Encode(data_url, &data_url);
25 data_url.insert(0, "data:image/png;base64,");
26
27 return data_url;
28 }
29
30 void ExpectRedWithGreenRect(const SkBitmap& bitmap,
31 const Rect& outer_rect,
32 const Rect& green_rect) {
33 for (int y = outer_rect.y(); y < outer_rect.bottom(); y++) {
34 SCOPED_TRACE(y);
35 for (int x = outer_rect.x(); x < outer_rect.right(); x++) {
36 SCOPED_TRACE(x);
37 if (green_rect.Contains(x, y)) {
38 ASSERT_EQ(SK_ColorGREEN, bitmap.getColor(x, y))
39 << "Output image:\n" << GetPNGDataUrl(bitmap);
40 } else {
41 ASSERT_EQ(SK_ColorRED, bitmap.getColor(x, y)) << "Output image:\n"
42 << GetPNGDataUrl(bitmap);
43 }
44 }
45 }
46 }
47
15 TEST(NineImagePainterTest, GetSubsetRegions) { 48 TEST(NineImagePainterTest, GetSubsetRegions) {
16 SkBitmap src; 49 SkBitmap src;
17 src.allocN32Pixels(40, 50); 50 src.allocN32Pixels(40, 50);
18 const ImageSkia image_skia(ImageSkiaRep(src, 1.0)); 51 const ImageSkia image_skia(ImageSkiaRep(src, 1.0));
19 const Insets insets(1, 2, 3, 4); 52 const Insets insets(1, 2, 3, 4);
20 std::vector<Rect> rects; 53 std::vector<Rect> rects;
21 NineImagePainter::GetSubsetRegions(image_skia, insets, &rects); 54 NineImagePainter::GetSubsetRegions(image_skia, insets, &rects);
22 ASSERT_EQ(9u, rects.size()); 55 ASSERT_EQ(9u, rects.size());
23 EXPECT_EQ(gfx::Rect(0, 0, 2, 1), rects[0]); 56 EXPECT_EQ(gfx::Rect(0, 0, 2, 1), rects[0]);
24 EXPECT_EQ(gfx::Rect(2, 0, 34, 1), rects[1]); 57 EXPECT_EQ(gfx::Rect(2, 0, 34, 1), rects[1]);
25 EXPECT_EQ(gfx::Rect(36, 0, 4, 1), rects[2]); 58 EXPECT_EQ(gfx::Rect(36, 0, 4, 1), rects[2]);
26 EXPECT_EQ(gfx::Rect(0, 1, 2, 46), rects[3]); 59 EXPECT_EQ(gfx::Rect(0, 1, 2, 46), rects[3]);
27 EXPECT_EQ(gfx::Rect(2, 1, 34, 46), rects[4]); 60 EXPECT_EQ(gfx::Rect(2, 1, 34, 46), rects[4]);
28 EXPECT_EQ(gfx::Rect(36, 1, 4, 46), rects[5]); 61 EXPECT_EQ(gfx::Rect(36, 1, 4, 46), rects[5]);
29 EXPECT_EQ(gfx::Rect(0, 47, 2, 3), rects[6]); 62 EXPECT_EQ(gfx::Rect(0, 47, 2, 3), rects[6]);
30 EXPECT_EQ(gfx::Rect(2, 47, 34, 3), rects[7]); 63 EXPECT_EQ(gfx::Rect(2, 47, 34, 3), rects[7]);
31 EXPECT_EQ(gfx::Rect(36, 47, 4, 3), rects[8]); 64 EXPECT_EQ(gfx::Rect(36, 47, 4, 3), rects[8]);
32 } 65 }
33 66
34 TEST(NineImagePainterTest, PaintScale) { 67 TEST(NineImagePainterTest, PaintHighDPI) {
35 SkBitmap src; 68 SkBitmap src;
36 src.allocN32Pixels(100, 100); 69 src.allocN32Pixels(100, 100);
37 src.eraseColor(SK_ColorRED); 70 src.eraseColor(SK_ColorRED);
38 src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN); 71 src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN);
39 72
40 gfx::ImageSkia image(gfx::ImageSkiaRep(src, 0.0f)); 73 float image_scale = 2.f;
74
75 gfx::ImageSkia image(gfx::ImageSkiaRep(src, image_scale));
41 gfx::Insets insets(10, 10, 10, 10); 76 gfx::Insets insets(10, 10, 10, 10);
42 gfx::NineImagePainter painter(image, insets); 77 gfx::NineImagePainter painter(image, insets);
43 78
44 int image_scale = 2;
45 bool is_opaque = true; 79 bool is_opaque = true;
46 gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque); 80 gfx::Canvas canvas(gfx::Size(100, 100), image_scale, is_opaque);
47 canvas.Scale(2, 1);
48 81
49 gfx::Rect bounds(0, 0, 100, 100); 82 gfx::Vector2d offset(20, 10);
83 canvas.Translate(offset);
84
85 gfx::Rect bounds(0, 0, 50, 50);
50 painter.Paint(&canvas, bounds); 86 painter.Paint(&canvas, bounds);
51 87
52 SkBitmap result; 88 SkBitmap result;
53 const SkISize size = canvas.sk_canvas()->getDeviceSize(); 89 const SkISize size = canvas.sk_canvas()->getDeviceSize();
54 result.allocN32Pixels(size.width(), size.height()); 90 result.allocN32Pixels(size.width(), size.height());
55 canvas.sk_canvas()->readPixels(&result, 0, 0); 91 canvas.sk_canvas()->readPixels(&result, 0, 0);
56 92
57 SkIRect green_rect = SkIRect::MakeLTRB(40, 20, 360, 180); 93 gfx::Vector2d paint_offset =
58 for (int y = 0; y < 200; y++) { 94 gfx::ToFlooredVector2d(gfx::ScaleVector2d(offset, image_scale));
59 for (int x = 0; x < 400; x++) { 95 gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset;
60 if (green_rect.contains(x, y)) { 96 gfx::Rect outer_rect = gfx::Rect(100, 100) + paint_offset;
61 EXPECT_EQ(SK_ColorGREEN, result.getColor(x, y)); 97 ExpectRedWithGreenRect(result, outer_rect, green_rect);
62 } else {
63 EXPECT_EQ(SK_ColorRED, result.getColor(x, y));
64 }
65 }
66 }
67 } 98 }
68 99
69 TEST(NineImagePainterTest, PaintStaysInBounds) { 100 TEST(NineImagePainterTest, PaintStaysInBounds) {
70 // In this test the bounds rect is 1x1 but each image is 2x2. 101 // In this test the bounds rect is 1x1 but each image is 2x2.
71 // The NineImagePainter should not paint outside the bounds. 102 // The NineImagePainter should not paint outside the bounds.
72 // The border images should be cropped, but still painted. 103 // The border images should be cropped, but still painted.
73 104
74 SkBitmap src; 105 SkBitmap src;
75 src.allocN32Pixels(6, 6); 106 src.allocN32Pixels(6, 6);
76 src.eraseColor(SK_ColorGREEN); 107 src.eraseColor(SK_ColorGREEN);
(...skipping 21 matching lines...) Expand all
98 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 0)); 129 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 0));
99 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 1)); 130 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 1));
100 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 2)); 131 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 2));
101 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 0)); 132 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 0));
102 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 2)); 133 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 2));
103 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 0)); 134 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 0));
104 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 1)); 135 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 1));
105 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 2)); 136 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 2));
106 } 137 }
107 138
139 TEST(NineImagePainterTest, PaintWithBoundOffset) {
140 SkBitmap src;
141 src.allocN32Pixels(10, 10);
142 src.eraseColor(SK_ColorRED);
143 src.eraseArea(SkIRect::MakeXYWH(1, 1, 8, 8), SK_ColorGREEN);
144
145 gfx::ImageSkia image(gfx::ImageSkiaRep(src, 0.0f));
146 gfx::Insets insets(1, 1, 1, 1);
147 gfx::NineImagePainter painter(image, insets);
148
149 bool is_opaque = true;
150 gfx::Canvas canvas(gfx::Size(10, 10), 1, is_opaque);
151
152 gfx::Rect bounds(1, 1, 10, 10);
153 painter.Paint(&canvas, bounds);
154
155 SkBitmap result;
156 const SkISize size = canvas.sk_canvas()->getDeviceSize();
157 result.allocN32Pixels(size.width(), size.height());
158 canvas.sk_canvas()->readPixels(&result, 0, 0);
159
160 SkIRect green_rect = SkIRect::MakeLTRB(2, 2, 10, 10);
161 for (int y = 1; y < 10; y++) {
162 for (int x = 1; x < 10; x++) {
163 if (green_rect.contains(x, y)) {
164 ASSERT_EQ(SK_ColorGREEN, result.getColor(x, y));
165 } else {
166 ASSERT_EQ(SK_ColorRED, result.getColor(x, y));
167 }
168 }
169 }
170 }
171
172 TEST(NineImagePainterTest, PaintWithScale) {
173 SkBitmap src;
174 src.allocN32Pixels(100, 100);
175 src.eraseColor(SK_ColorRED);
176 src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN);
177
178 float image_scale = 2.f;
179
180 gfx::ImageSkia image(gfx::ImageSkiaRep(src, image_scale));
181 gfx::Insets insets(10, 10, 10, 10);
182 gfx::NineImagePainter painter(image, insets);
183
184 bool is_opaque = true;
185 gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque);
186
187 gfx::Vector2d offset(20, 10);
188 canvas.Translate(offset);
189 canvas.Scale(2, 1);
190
191 gfx::Rect bounds(0, 0, 50, 50);
192 painter.Paint(&canvas, bounds);
193
194 SkBitmap result;
195 const SkISize size = canvas.sk_canvas()->getDeviceSize();
196 result.allocN32Pixels(size.width(), size.height());
197 canvas.sk_canvas()->readPixels(&result, 0, 0);
198
199 gfx::Vector2d paint_offset =
200 gfx::ToFlooredVector2d(gfx::ScaleVector2d(offset, image_scale));
201 gfx::Rect green_rect = gfx::Rect(20, 10, 160, 80) + paint_offset;
202 gfx::Rect outer_rect = gfx::Rect(200, 100) + paint_offset;
203 ExpectRedWithGreenRect(result, outer_rect, green_rect);
204 }
205
206 TEST(NineImagePainterTest, PaintWithNegativeScale) {
207 SkBitmap src;
208 src.allocN32Pixels(100, 100);
209 src.eraseColor(SK_ColorRED);
210 src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN);
211
212 float image_scale = 2.f;
213
214 gfx::ImageSkia image(gfx::ImageSkiaRep(src, image_scale));
215 gfx::Insets insets(10, 10, 10, 10);
216 gfx::NineImagePainter painter(image, insets);
217
218 bool is_opaque = true;
219 gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque);
220 canvas.Translate(gfx::Vector2d(70, 60));
221 canvas.Scale(-1, -1);
222
223 gfx::Rect bounds(0, 0, 50, 50);
224 painter.Paint(&canvas, bounds);
225
226 SkBitmap result;
227 const SkISize size = canvas.sk_canvas()->getDeviceSize();
228 result.allocN32Pixels(size.width(), size.height());
229 canvas.sk_canvas()->readPixels(&result, 0, 0);
230
231 // The painting space is 50x50 and the scale of -1,-1 means an offset of 50,50
232 // would put the output in the top left corner. Since the offset is 70,60 it
233 // moves by 20,10. Since the output is 2x DPI it will become offset by 40,20.
234 gfx::Vector2d paint_offset(40, 20);
235 gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset;
236 gfx::Rect outer_rect = gfx::Rect(100, 100) + paint_offset;
237 ExpectRedWithGreenRect(result, outer_rect, green_rect);
238 }
239
108 } // namespace gfx 240 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/nine_image_painter.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698