| OLD | NEW |
| 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 "base/base64.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
| 10 #include "ui/gfx/codec/png_codec.h" | 10 #include "ui/gfx/codec/png_codec.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 78 |
| 79 bool is_opaque = true; | 79 bool is_opaque = true; |
| 80 gfx::Canvas canvas(gfx::Size(100, 100), image_scale, is_opaque); | 80 gfx::Canvas canvas(gfx::Size(100, 100), image_scale, is_opaque); |
| 81 | 81 |
| 82 gfx::Vector2d offset(20, 10); | 82 gfx::Vector2d offset(20, 10); |
| 83 canvas.Translate(offset); | 83 canvas.Translate(offset); |
| 84 | 84 |
| 85 gfx::Rect bounds(0, 0, 50, 50); | 85 gfx::Rect bounds(0, 0, 50, 50); |
| 86 painter.Paint(&canvas, bounds); | 86 painter.Paint(&canvas, bounds); |
| 87 | 87 |
| 88 SkBitmap result = canvas.ToBitmap(); | 88 SkBitmap result = canvas.GetBitmap(); |
| 89 | 89 |
| 90 gfx::Vector2d paint_offset = | 90 gfx::Vector2d paint_offset = |
| 91 gfx::ToFlooredVector2d(gfx::ScaleVector2d(offset, image_scale)); | 91 gfx::ToFlooredVector2d(gfx::ScaleVector2d(offset, image_scale)); |
| 92 gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset; | 92 gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset; |
| 93 gfx::Rect outer_rect = gfx::Rect(100, 100) + paint_offset; | 93 gfx::Rect outer_rect = gfx::Rect(100, 100) + paint_offset; |
| 94 ExpectRedWithGreenRect(result, outer_rect, green_rect); | 94 ExpectRedWithGreenRect(result, outer_rect, green_rect); |
| 95 } | 95 } |
| 96 | 96 |
| 97 TEST(NineImagePainterTest, PaintStaysInBounds) { | 97 TEST(NineImagePainterTest, PaintStaysInBounds) { |
| 98 // In this test the bounds rect is 1x1 but each image is 2x2. | 98 // In this test the bounds rect is 1x1 but each image is 2x2. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 109 gfx::NineImagePainter painter(image, insets); | 109 gfx::NineImagePainter painter(image, insets); |
| 110 | 110 |
| 111 int image_scale = 1; | 111 int image_scale = 1; |
| 112 bool is_opaque = true; | 112 bool is_opaque = true; |
| 113 gfx::Canvas canvas(gfx::Size(3, 3), image_scale, is_opaque); | 113 gfx::Canvas canvas(gfx::Size(3, 3), image_scale, is_opaque); |
| 114 canvas.DrawColor(SK_ColorBLACK); | 114 canvas.DrawColor(SK_ColorBLACK); |
| 115 | 115 |
| 116 gfx::Rect bounds(1, 1, 1, 1); | 116 gfx::Rect bounds(1, 1, 1, 1); |
| 117 painter.Paint(&canvas, bounds); | 117 painter.Paint(&canvas, bounds); |
| 118 | 118 |
| 119 SkBitmap result = canvas.ToBitmap(); | 119 SkBitmap result = canvas.GetBitmap(); |
| 120 | 120 |
| 121 EXPECT_EQ(SK_ColorGREEN, result.getColor(1, 1)); | 121 EXPECT_EQ(SK_ColorGREEN, result.getColor(1, 1)); |
| 122 | 122 |
| 123 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 0)); | 123 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 0)); |
| 124 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 1)); | 124 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 1)); |
| 125 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 2)); | 125 EXPECT_EQ(SK_ColorBLACK, result.getColor(0, 2)); |
| 126 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 0)); | 126 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 0)); |
| 127 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 2)); | 127 EXPECT_EQ(SK_ColorBLACK, result.getColor(1, 2)); |
| 128 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 0)); | 128 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 0)); |
| 129 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 1)); | 129 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 1)); |
| 130 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 2)); | 130 EXPECT_EQ(SK_ColorBLACK, result.getColor(2, 2)); |
| 131 } | 131 } |
| 132 | 132 |
| 133 TEST(NineImagePainterTest, PaintWithBoundOffset) { | 133 TEST(NineImagePainterTest, PaintWithBoundOffset) { |
| 134 SkBitmap src; | 134 SkBitmap src; |
| 135 src.allocN32Pixels(10, 10); | 135 src.allocN32Pixels(10, 10); |
| 136 src.eraseColor(SK_ColorRED); | 136 src.eraseColor(SK_ColorRED); |
| 137 src.eraseArea(SkIRect::MakeXYWH(1, 1, 8, 8), SK_ColorGREEN); | 137 src.eraseArea(SkIRect::MakeXYWH(1, 1, 8, 8), SK_ColorGREEN); |
| 138 | 138 |
| 139 gfx::ImageSkia image(gfx::ImageSkiaRep(src, 0.0f)); | 139 gfx::ImageSkia image(gfx::ImageSkiaRep(src, 0.0f)); |
| 140 gfx::Insets insets(1, 1, 1, 1); | 140 gfx::Insets insets(1, 1, 1, 1); |
| 141 gfx::NineImagePainter painter(image, insets); | 141 gfx::NineImagePainter painter(image, insets); |
| 142 | 142 |
| 143 bool is_opaque = true; | 143 bool is_opaque = true; |
| 144 gfx::Canvas canvas(gfx::Size(10, 10), 1, is_opaque); | 144 gfx::Canvas canvas(gfx::Size(10, 10), 1, is_opaque); |
| 145 | 145 |
| 146 gfx::Rect bounds(1, 1, 10, 10); | 146 gfx::Rect bounds(1, 1, 10, 10); |
| 147 painter.Paint(&canvas, bounds); | 147 painter.Paint(&canvas, bounds); |
| 148 | 148 |
| 149 SkBitmap result = canvas.ToBitmap(); | 149 SkBitmap result = canvas.GetBitmap(); |
| 150 | 150 |
| 151 SkIRect green_rect = SkIRect::MakeLTRB(2, 2, 10, 10); | 151 SkIRect green_rect = SkIRect::MakeLTRB(2, 2, 10, 10); |
| 152 for (int y = 1; y < 10; y++) { | 152 for (int y = 1; y < 10; y++) { |
| 153 for (int x = 1; x < 10; x++) { | 153 for (int x = 1; x < 10; x++) { |
| 154 if (green_rect.contains(x, y)) { | 154 if (green_rect.contains(x, y)) { |
| 155 ASSERT_EQ(SK_ColorGREEN, result.getColor(x, y)); | 155 ASSERT_EQ(SK_ColorGREEN, result.getColor(x, y)); |
| 156 } else { | 156 } else { |
| 157 ASSERT_EQ(SK_ColorRED, result.getColor(x, y)); | 157 ASSERT_EQ(SK_ColorRED, result.getColor(x, y)); |
| 158 } | 158 } |
| 159 } | 159 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 175 bool is_opaque = true; | 175 bool is_opaque = true; |
| 176 gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque); | 176 gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque); |
| 177 | 177 |
| 178 gfx::Vector2d offset(20, 10); | 178 gfx::Vector2d offset(20, 10); |
| 179 canvas.Translate(offset); | 179 canvas.Translate(offset); |
| 180 canvas.Scale(2, 1); | 180 canvas.Scale(2, 1); |
| 181 | 181 |
| 182 gfx::Rect bounds(0, 0, 50, 50); | 182 gfx::Rect bounds(0, 0, 50, 50); |
| 183 painter.Paint(&canvas, bounds); | 183 painter.Paint(&canvas, bounds); |
| 184 | 184 |
| 185 SkBitmap result = canvas.ToBitmap(); | 185 SkBitmap result = canvas.GetBitmap(); |
| 186 | 186 |
| 187 gfx::Vector2d paint_offset = | 187 gfx::Vector2d paint_offset = |
| 188 gfx::ToFlooredVector2d(gfx::ScaleVector2d(offset, image_scale)); | 188 gfx::ToFlooredVector2d(gfx::ScaleVector2d(offset, image_scale)); |
| 189 gfx::Rect green_rect = gfx::Rect(20, 10, 160, 80) + paint_offset; | 189 gfx::Rect green_rect = gfx::Rect(20, 10, 160, 80) + paint_offset; |
| 190 gfx::Rect outer_rect = gfx::Rect(200, 100) + paint_offset; | 190 gfx::Rect outer_rect = gfx::Rect(200, 100) + paint_offset; |
| 191 ExpectRedWithGreenRect(result, outer_rect, green_rect); | 191 ExpectRedWithGreenRect(result, outer_rect, green_rect); |
| 192 } | 192 } |
| 193 | 193 |
| 194 TEST(NineImagePainterTest, PaintWithNegativeScale) { | 194 TEST(NineImagePainterTest, PaintWithNegativeScale) { |
| 195 SkBitmap src; | 195 SkBitmap src; |
| 196 src.allocN32Pixels(100, 100); | 196 src.allocN32Pixels(100, 100); |
| 197 src.eraseColor(SK_ColorRED); | 197 src.eraseColor(SK_ColorRED); |
| 198 src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN); | 198 src.eraseArea(SkIRect::MakeXYWH(10, 10, 80, 80), SK_ColorGREEN); |
| 199 | 199 |
| 200 float image_scale = 2.f; | 200 float image_scale = 2.f; |
| 201 | 201 |
| 202 gfx::ImageSkia image(gfx::ImageSkiaRep(src, image_scale)); | 202 gfx::ImageSkia image(gfx::ImageSkiaRep(src, image_scale)); |
| 203 gfx::Insets insets(10, 10, 10, 10); | 203 gfx::Insets insets(10, 10, 10, 10); |
| 204 gfx::NineImagePainter painter(image, insets); | 204 gfx::NineImagePainter painter(image, insets); |
| 205 | 205 |
| 206 bool is_opaque = true; | 206 bool is_opaque = true; |
| 207 gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque); | 207 gfx::Canvas canvas(gfx::Size(400, 400), image_scale, is_opaque); |
| 208 canvas.Translate(gfx::Vector2d(70, 60)); | 208 canvas.Translate(gfx::Vector2d(70, 60)); |
| 209 canvas.Scale(-1, -1); | 209 canvas.Scale(-1, -1); |
| 210 | 210 |
| 211 gfx::Rect bounds(0, 0, 50, 50); | 211 gfx::Rect bounds(0, 0, 50, 50); |
| 212 painter.Paint(&canvas, bounds); | 212 painter.Paint(&canvas, bounds); |
| 213 | 213 |
| 214 SkBitmap result = canvas.ToBitmap(); | 214 SkBitmap result = canvas.GetBitmap(); |
| 215 | 215 |
| 216 // The painting space is 50x50 and the scale of -1,-1 means an offset of 50,50 | 216 // The painting space is 50x50 and the scale of -1,-1 means an offset of 50,50 |
| 217 // would put the output in the top left corner. Since the offset is 70,60 it | 217 // would put the output in the top left corner. Since the offset is 70,60 it |
| 218 // moves by 20,10. Since the output is 2x DPI it will become offset by 40,20. | 218 // moves by 20,10. Since the output is 2x DPI it will become offset by 40,20. |
| 219 gfx::Vector2d paint_offset(40, 20); | 219 gfx::Vector2d paint_offset(40, 20); |
| 220 gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset; | 220 gfx::Rect green_rect = gfx::Rect(10, 10, 80, 80) + paint_offset; |
| 221 gfx::Rect outer_rect = gfx::Rect(100, 100) + paint_offset; | 221 gfx::Rect outer_rect = gfx::Rect(100, 100) + paint_offset; |
| 222 ExpectRedWithGreenRect(result, outer_rect, green_rect); | 222 ExpectRedWithGreenRect(result, outer_rect, green_rect); |
| 223 } | 223 } |
| 224 | 224 |
| 225 } // namespace gfx | 225 } // namespace gfx |
| OLD | NEW |