| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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. | |
| 4 | |
| 5 #include "ui/views/corewm/image_grid.h" | |
| 6 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | |
| 9 #include "ui/aura/test/aura_test_base.h" | |
| 10 #include "ui/gfx/image/image.h" | |
| 11 #include "ui/gfx/image/image_skia.h" | |
| 12 | |
| 13 namespace views { | |
| 14 namespace corewm { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Creates a gfx::Image with the requested dimensions. | |
| 19 gfx::Image* CreateImage(const gfx::Size& size) { | |
| 20 SkBitmap bitmap; | |
| 21 bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height()); | |
| 22 return new gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); | |
| 23 } | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 typedef aura::test::AuraTestBase ImageGridTest; | |
| 28 | |
| 29 // Test that an ImageGrid's layers are transformed correctly when SetSize() is | |
| 30 // called. | |
| 31 TEST_F(ImageGridTest, Basic) { | |
| 32 // Size of the images around the grid's border. | |
| 33 const int kBorder = 2; | |
| 34 | |
| 35 scoped_ptr<gfx::Image> image_1x1(CreateImage(gfx::Size(1, 1))); | |
| 36 scoped_ptr<gfx::Image> image_1xB(CreateImage(gfx::Size(1, kBorder))); | |
| 37 scoped_ptr<gfx::Image> image_Bx1(CreateImage(gfx::Size(kBorder, 1))); | |
| 38 scoped_ptr<gfx::Image> image_BxB(CreateImage(gfx::Size(kBorder, kBorder))); | |
| 39 | |
| 40 ImageGrid grid; | |
| 41 grid.SetImages(image_BxB.get(), image_1xB.get(), image_BxB.get(), | |
| 42 image_Bx1.get(), image_1x1.get(), image_Bx1.get(), | |
| 43 image_BxB.get(), image_1xB.get(), image_BxB.get()); | |
| 44 | |
| 45 ImageGrid::TestAPI test_api(&grid); | |
| 46 ASSERT_TRUE(grid.top_left_layer() != NULL); | |
| 47 ASSERT_TRUE(grid.top_layer() != NULL); | |
| 48 ASSERT_TRUE(grid.top_right_layer() != NULL); | |
| 49 ASSERT_TRUE(grid.left_layer() != NULL); | |
| 50 ASSERT_TRUE(grid.center_layer() != NULL); | |
| 51 ASSERT_TRUE(grid.right_layer() != NULL); | |
| 52 ASSERT_TRUE(grid.bottom_left_layer() != NULL); | |
| 53 ASSERT_TRUE(grid.bottom_layer() != NULL); | |
| 54 ASSERT_TRUE(grid.bottom_right_layer() != NULL); | |
| 55 | |
| 56 const gfx::Size size(20, 30); | |
| 57 grid.SetSize(size); | |
| 58 | |
| 59 // The top-left layer should be flush with the top-left corner and unscaled. | |
| 60 EXPECT_EQ(gfx::RectF(0, 0, kBorder, kBorder).ToString(), | |
| 61 test_api.GetTransformedLayerBounds( | |
| 62 *grid.top_left_layer()).ToString()); | |
| 63 | |
| 64 // The top layer should be flush with the top edge and stretched horizontally | |
| 65 // between the two top corners. | |
| 66 EXPECT_EQ(gfx::RectF( | |
| 67 kBorder, 0, size.width() - 2 * kBorder, kBorder).ToString(), | |
| 68 test_api.GetTransformedLayerBounds( | |
| 69 *grid.top_layer()).ToString()); | |
| 70 | |
| 71 // The top-right layer should be flush with the top-right corner and unscaled. | |
| 72 EXPECT_EQ(gfx::RectF(size.width() - kBorder, 0, kBorder, kBorder).ToString(), | |
| 73 test_api.GetTransformedLayerBounds( | |
| 74 *grid.top_right_layer()).ToString()); | |
| 75 | |
| 76 // The left layer should be flush with the left edge and stretched vertically | |
| 77 // between the two left corners. | |
| 78 EXPECT_EQ(gfx::RectF( | |
| 79 0, kBorder, kBorder, size.height() - 2 * kBorder).ToString(), | |
| 80 test_api.GetTransformedLayerBounds( | |
| 81 *grid.left_layer()).ToString()); | |
| 82 | |
| 83 // The center layer should fill the space in the middle of the grid. | |
| 84 EXPECT_EQ(gfx::RectF( | |
| 85 kBorder, kBorder, size.width() - 2 * kBorder, | |
| 86 size.height() - 2 * kBorder).ToString(), | |
| 87 test_api.GetTransformedLayerBounds( | |
| 88 *grid.center_layer()).ToString()); | |
| 89 | |
| 90 // The right layer should be flush with the right edge and stretched | |
| 91 // vertically between the two right corners. | |
| 92 EXPECT_EQ(gfx::RectF( | |
| 93 size.width() - kBorder, kBorder, | |
| 94 kBorder, size.height() - 2 * kBorder).ToString(), | |
| 95 test_api.GetTransformedLayerBounds( | |
| 96 *grid.right_layer()).ToString()); | |
| 97 | |
| 98 // The bottom-left layer should be flush with the bottom-left corner and | |
| 99 // unscaled. | |
| 100 EXPECT_EQ(gfx::RectF(0, size.height() - kBorder, kBorder, kBorder).ToString(), | |
| 101 test_api.GetTransformedLayerBounds( | |
| 102 *grid.bottom_left_layer()).ToString()); | |
| 103 | |
| 104 // The bottom layer should be flush with the bottom edge and stretched | |
| 105 // horizontally between the two bottom corners. | |
| 106 EXPECT_EQ(gfx::RectF( | |
| 107 kBorder, size.height() - kBorder, | |
| 108 size.width() - 2 * kBorder, kBorder).ToString(), | |
| 109 test_api.GetTransformedLayerBounds( | |
| 110 *grid.bottom_layer()).ToString()); | |
| 111 | |
| 112 // The bottom-right layer should be flush with the bottom-right corner and | |
| 113 // unscaled. | |
| 114 EXPECT_EQ(gfx::RectF( | |
| 115 size.width() - kBorder, size.height() - kBorder, | |
| 116 kBorder, kBorder).ToString(), | |
| 117 test_api.GetTransformedLayerBounds( | |
| 118 *grid.bottom_right_layer()).ToString()); | |
| 119 } | |
| 120 | |
| 121 // Test that an ImageGrid's layers are transformed correctly when | |
| 122 // SetContentBounds() is called. | |
| 123 TEST_F(ImageGridTest, SetContentBounds) { | |
| 124 // Size of the images around the grid's border. | |
| 125 const int kBorder = 2; | |
| 126 | |
| 127 scoped_ptr<gfx::Image> image_1x1(CreateImage(gfx::Size(1, 1))); | |
| 128 scoped_ptr<gfx::Image> image_1xB(CreateImage(gfx::Size(1, kBorder))); | |
| 129 scoped_ptr<gfx::Image> image_Bx1(CreateImage(gfx::Size(kBorder, 1))); | |
| 130 scoped_ptr<gfx::Image> image_BxB(CreateImage(gfx::Size(kBorder, kBorder))); | |
| 131 | |
| 132 ImageGrid grid; | |
| 133 grid.SetImages(image_BxB.get(), image_1xB.get(), image_BxB.get(), | |
| 134 image_Bx1.get(), image_1x1.get(), image_Bx1.get(), | |
| 135 image_BxB.get(), image_1xB.get(), image_BxB.get()); | |
| 136 | |
| 137 ImageGrid::TestAPI test_api(&grid); | |
| 138 | |
| 139 const gfx::Point origin(5, 10); | |
| 140 const gfx::Size size(20, 30); | |
| 141 grid.SetContentBounds(gfx::Rect(origin, size)); | |
| 142 | |
| 143 // The master layer is positioned above the top-left corner of the content | |
| 144 // bounds and has height/width that contain the grid and bounds. | |
| 145 EXPECT_EQ(gfx::RectF(origin.x() - kBorder, | |
| 146 origin.y() - kBorder, | |
| 147 size.width() + 2 * kBorder, | |
| 148 size.height() + 2 * kBorder).ToString(), | |
| 149 test_api.GetTransformedLayerBounds(*grid.layer()).ToString()); | |
| 150 } | |
| 151 | |
| 152 // Check that we don't crash if only a single image is supplied. | |
| 153 TEST_F(ImageGridTest, SingleImage) { | |
| 154 const int kBorder = 1; | |
| 155 scoped_ptr<gfx::Image> image(CreateImage(gfx::Size(kBorder, kBorder))); | |
| 156 | |
| 157 ImageGrid grid; | |
| 158 grid.SetImages(NULL, image.get(), NULL, | |
| 159 NULL, NULL, NULL, | |
| 160 NULL, NULL, NULL); | |
| 161 | |
| 162 ImageGrid::TestAPI test_api(&grid); | |
| 163 EXPECT_TRUE(grid.top_left_layer() == NULL); | |
| 164 ASSERT_TRUE(grid.top_layer() != NULL); | |
| 165 EXPECT_TRUE(grid.top_right_layer() == NULL); | |
| 166 EXPECT_TRUE(grid.left_layer() == NULL); | |
| 167 EXPECT_TRUE(grid.center_layer() == NULL); | |
| 168 EXPECT_TRUE(grid.right_layer() == NULL); | |
| 169 EXPECT_TRUE(grid.bottom_left_layer() == NULL); | |
| 170 EXPECT_TRUE(grid.bottom_layer() == NULL); | |
| 171 EXPECT_TRUE(grid.bottom_right_layer() == NULL); | |
| 172 | |
| 173 const gfx::Size kSize(10, 10); | |
| 174 grid.SetSize(kSize); | |
| 175 | |
| 176 // The top layer should be scaled horizontally across the entire width, but it | |
| 177 // shouldn't be scaled vertically. | |
| 178 EXPECT_EQ(gfx::RectF(0, 0, kSize.width(), kBorder).ToString(), | |
| 179 test_api.GetTransformedLayerBounds( | |
| 180 *grid.top_layer()).ToString()); | |
| 181 } | |
| 182 | |
| 183 // Check that we don't crash when we reset existing images to NULL and | |
| 184 // reset NULL images to new ones. | |
| 185 TEST_F(ImageGridTest, ResetImages) { | |
| 186 const int kBorder = 1; | |
| 187 scoped_ptr<gfx::Image> image(CreateImage(gfx::Size(kBorder, kBorder))); | |
| 188 | |
| 189 ImageGrid grid; | |
| 190 grid.SetImages(NULL, image.get(), NULL, | |
| 191 NULL, NULL, NULL, | |
| 192 NULL, NULL, NULL); | |
| 193 | |
| 194 // Only the top edge has a layer. | |
| 195 ImageGrid::TestAPI test_api(&grid); | |
| 196 ASSERT_TRUE(grid.top_left_layer() == NULL); | |
| 197 ASSERT_FALSE(grid.top_layer() == NULL); | |
| 198 ASSERT_TRUE(grid.top_right_layer() == NULL); | |
| 199 ASSERT_TRUE(grid.left_layer() == NULL); | |
| 200 ASSERT_TRUE(grid.center_layer() == NULL); | |
| 201 ASSERT_TRUE(grid.right_layer() == NULL); | |
| 202 ASSERT_TRUE(grid.bottom_left_layer() == NULL); | |
| 203 ASSERT_TRUE(grid.bottom_layer() == NULL); | |
| 204 ASSERT_TRUE(grid.bottom_right_layer() == NULL); | |
| 205 | |
| 206 grid.SetImages(NULL, NULL, NULL, | |
| 207 NULL, NULL, NULL, | |
| 208 NULL, image.get(), NULL); | |
| 209 | |
| 210 // Now only the bottom edge has a layer. | |
| 211 ASSERT_TRUE(grid.top_left_layer() == NULL); | |
| 212 ASSERT_TRUE(grid.top_layer() == NULL); | |
| 213 ASSERT_TRUE(grid.top_right_layer() == NULL); | |
| 214 ASSERT_TRUE(grid.left_layer() == NULL); | |
| 215 ASSERT_TRUE(grid.center_layer() == NULL); | |
| 216 ASSERT_TRUE(grid.right_layer() == NULL); | |
| 217 ASSERT_TRUE(grid.bottom_left_layer() == NULL); | |
| 218 ASSERT_FALSE(grid.bottom_layer() == NULL); | |
| 219 ASSERT_TRUE(grid.bottom_right_layer() == NULL); | |
| 220 } | |
| 221 | |
| 222 // Test that side (top, left, right, bottom) layers that are narrower than their | |
| 223 // adjacent corner layers stay pinned to the outside edges instead of getting | |
| 224 // moved inwards or scaled. This exercises the scenario used for shadows. | |
| 225 TEST_F(ImageGridTest, SmallerSides) { | |
| 226 const int kCorner = 2; | |
| 227 const int kEdge = 1; | |
| 228 | |
| 229 scoped_ptr<gfx::Image> top_left_image( | |
| 230 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 231 scoped_ptr<gfx::Image> top_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 232 scoped_ptr<gfx::Image> top_right_image( | |
| 233 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 234 scoped_ptr<gfx::Image> left_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 235 scoped_ptr<gfx::Image> right_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 236 | |
| 237 ImageGrid grid; | |
| 238 grid.SetImages(top_left_image.get(), top_image.get(), top_right_image.get(), | |
| 239 left_image.get(), NULL, right_image.get(), | |
| 240 NULL, NULL, NULL); | |
| 241 ImageGrid::TestAPI test_api(&grid); | |
| 242 | |
| 243 const gfx::Size kSize(20, 30); | |
| 244 grid.SetSize(kSize); | |
| 245 | |
| 246 // The top layer should be flush with the top edge and stretched horizontally | |
| 247 // between the two top corners. | |
| 248 EXPECT_EQ(gfx::RectF( | |
| 249 kCorner, 0, kSize.width() - 2 * kCorner, kEdge).ToString(), | |
| 250 test_api.GetTransformedLayerBounds( | |
| 251 *grid.top_layer()).ToString()); | |
| 252 | |
| 253 // The left layer should be flush with the left edge and stretched vertically | |
| 254 // between the top left corner and the bottom. | |
| 255 EXPECT_EQ(gfx::RectF( | |
| 256 0, kCorner, kEdge, kSize.height() - kCorner).ToString(), | |
| 257 test_api.GetTransformedLayerBounds( | |
| 258 *grid.left_layer()).ToString()); | |
| 259 | |
| 260 // The right layer should be flush with the right edge and stretched | |
| 261 // vertically between the top right corner and the bottom. | |
| 262 EXPECT_EQ(gfx::RectF( | |
| 263 kSize.width() - kEdge, kCorner, | |
| 264 kEdge, kSize.height() - kCorner).ToString(), | |
| 265 test_api.GetTransformedLayerBounds( | |
| 266 *grid.right_layer()).ToString()); | |
| 267 } | |
| 268 | |
| 269 // Test that we hide or clip layers as needed when the grid is assigned a small | |
| 270 // size. | |
| 271 TEST_F(ImageGridTest, TooSmall) { | |
| 272 const int kCorner = 5; | |
| 273 const int kCenter = 3; | |
| 274 const int kEdge = 3; | |
| 275 | |
| 276 scoped_ptr<gfx::Image> top_left_image( | |
| 277 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 278 scoped_ptr<gfx::Image> top_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 279 scoped_ptr<gfx::Image> top_right_image( | |
| 280 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 281 scoped_ptr<gfx::Image> left_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 282 scoped_ptr<gfx::Image> center_image(CreateImage(gfx::Size(kCenter, kCenter))); | |
| 283 scoped_ptr<gfx::Image> right_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 284 scoped_ptr<gfx::Image> bottom_left_image( | |
| 285 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 286 scoped_ptr<gfx::Image> bottom_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 287 scoped_ptr<gfx::Image> bottom_right_image( | |
| 288 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 289 | |
| 290 ImageGrid grid; | |
| 291 grid.SetImages( | |
| 292 top_left_image.get(), top_image.get(), top_right_image.get(), | |
| 293 left_image.get(), center_image.get(), right_image.get(), | |
| 294 bottom_left_image.get(), bottom_image.get(), bottom_right_image.get()); | |
| 295 ImageGrid::TestAPI test_api(&grid); | |
| 296 | |
| 297 // Set a size that's smaller than the combined (unscaled) corner images. | |
| 298 const gfx::Size kSmallSize(kCorner + kCorner - 3, kCorner + kCorner - 5); | |
| 299 grid.SetSize(kSmallSize); | |
| 300 | |
| 301 // The scalable images around the sides and in the center should be hidden. | |
| 302 EXPECT_FALSE(grid.top_layer()->visible()); | |
| 303 EXPECT_FALSE(grid.bottom_layer()->visible()); | |
| 304 EXPECT_FALSE(grid.left_layer()->visible()); | |
| 305 EXPECT_FALSE(grid.right_layer()->visible()); | |
| 306 EXPECT_FALSE(grid.center_layer()->visible()); | |
| 307 | |
| 308 // The corner images' clip rects should sum to the expected size. | |
| 309 EXPECT_EQ(kSmallSize.width(), | |
| 310 test_api.top_left_clip_rect().width() + | |
| 311 test_api.top_right_clip_rect().width()); | |
| 312 EXPECT_EQ(kSmallSize.width(), | |
| 313 test_api.bottom_left_clip_rect().width() + | |
| 314 test_api.bottom_right_clip_rect().width()); | |
| 315 EXPECT_EQ(kSmallSize.height(), | |
| 316 test_api.top_left_clip_rect().height() + | |
| 317 test_api.bottom_left_clip_rect().height()); | |
| 318 EXPECT_EQ(kSmallSize.height(), | |
| 319 test_api.top_right_clip_rect().height() + | |
| 320 test_api.bottom_right_clip_rect().height()); | |
| 321 | |
| 322 // Resize the grid to be large enough to show all images. | |
| 323 const gfx::Size kLargeSize(kCorner + kCorner + kCenter, | |
| 324 kCorner + kCorner + kCenter); | |
| 325 grid.SetSize(kLargeSize); | |
| 326 | |
| 327 // The scalable images should be visible now. | |
| 328 EXPECT_TRUE(grid.top_layer()->visible()); | |
| 329 EXPECT_TRUE(grid.bottom_layer()->visible()); | |
| 330 EXPECT_TRUE(grid.left_layer()->visible()); | |
| 331 EXPECT_TRUE(grid.right_layer()->visible()); | |
| 332 EXPECT_TRUE(grid.center_layer()->visible()); | |
| 333 | |
| 334 // We shouldn't be clipping the corner images anymore. | |
| 335 EXPECT_TRUE(test_api.top_left_clip_rect().IsEmpty()); | |
| 336 EXPECT_TRUE(test_api.top_right_clip_rect().IsEmpty()); | |
| 337 EXPECT_TRUE(test_api.bottom_left_clip_rect().IsEmpty()); | |
| 338 EXPECT_TRUE(test_api.bottom_right_clip_rect().IsEmpty()); | |
| 339 } | |
| 340 | |
| 341 } // namespace corewm | |
| 342 } // namespace views | |
| OLD | NEW |