| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h" | |
| 6 #include "third_party/skia/include/core/SkBitmap.h" | |
| 7 #include "ui/aura_shell/image_grid.h" | |
| 8 #include "ui/aura_shell/test/aura_shell_test_base.h" | |
| 9 #include "ui/gfx/image/image.h" | |
| 10 | |
| 11 using aura_shell::internal::ImageGrid; | |
| 12 | |
| 13 namespace aura_shell { | |
| 14 namespace test { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Creates a gfx::Image with the requested dimensions. | |
| 19 gfx::Image* CreateImage(const gfx::Size& size) { | |
| 20 SkBitmap* bitmap = new SkBitmap(); | |
| 21 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height()); | |
| 22 return new gfx::Image(bitmap); | |
| 23 } | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 typedef aura_shell::test::AuraShellTestBase 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.Init(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(test_api.top_left_layer() != NULL); | |
| 47 ASSERT_TRUE(test_api.top_layer() != NULL); | |
| 48 ASSERT_TRUE(test_api.top_right_layer() != NULL); | |
| 49 ASSERT_TRUE(test_api.left_layer() != NULL); | |
| 50 ASSERT_TRUE(test_api.center_layer() != NULL); | |
| 51 ASSERT_TRUE(test_api.right_layer() != NULL); | |
| 52 ASSERT_TRUE(test_api.bottom_left_layer() != NULL); | |
| 53 ASSERT_TRUE(test_api.bottom_layer() != NULL); | |
| 54 ASSERT_TRUE(test_api.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::Rect(0, 0, kBorder, kBorder).ToString(), | |
| 61 test_api.GetTransformedLayerBounds( | |
| 62 *test_api.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::Rect( | |
| 67 kBorder, 0, size.width() - 2 * kBorder, kBorder).ToString(), | |
| 68 test_api.GetTransformedLayerBounds( | |
| 69 *test_api.top_layer()).ToString()); | |
| 70 | |
| 71 // The top-right layer should be flush with the top-right corner and unscaled. | |
| 72 EXPECT_EQ(gfx::Rect(size.width() - kBorder, 0, kBorder, kBorder).ToString(), | |
| 73 test_api.GetTransformedLayerBounds( | |
| 74 *test_api.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::Rect( | |
| 79 0, kBorder, kBorder, size.height() - 2 * kBorder).ToString(), | |
| 80 test_api.GetTransformedLayerBounds( | |
| 81 *test_api.left_layer()).ToString()); | |
| 82 | |
| 83 // The center layer should fill the space in the middle of the grid. | |
| 84 EXPECT_EQ(gfx::Rect( | |
| 85 kBorder, kBorder, size.width() - 2 * kBorder, | |
| 86 size.height() - 2 * kBorder).ToString(), | |
| 87 test_api.GetTransformedLayerBounds( | |
| 88 *test_api.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::Rect( | |
| 93 size.width() - kBorder, kBorder, | |
| 94 kBorder, size.height() - 2 * kBorder).ToString(), | |
| 95 test_api.GetTransformedLayerBounds( | |
| 96 *test_api.right_layer()).ToString()); | |
| 97 | |
| 98 // The bottom-left layer should be flush with the bottom-left corner and | |
| 99 // unscaled. | |
| 100 EXPECT_EQ(gfx::Rect(0, size.height() - kBorder, kBorder, kBorder).ToString(), | |
| 101 test_api.GetTransformedLayerBounds( | |
| 102 *test_api.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::Rect( | |
| 107 kBorder, size.height() - kBorder, | |
| 108 size.width() - 2 * kBorder, kBorder).ToString(), | |
| 109 test_api.GetTransformedLayerBounds( | |
| 110 *test_api.bottom_layer()).ToString()); | |
| 111 | |
| 112 // The bottom-right layer should be flush with the bottom-right corner and | |
| 113 // unscaled. | |
| 114 EXPECT_EQ(gfx::Rect( | |
| 115 size.width() - kBorder, size.height() - kBorder, | |
| 116 kBorder, kBorder).ToString(), | |
| 117 test_api.GetTransformedLayerBounds( | |
| 118 *test_api.bottom_right_layer()).ToString()); | |
| 119 } | |
| 120 | |
| 121 // Check that we don't crash if only a single image is supplied. | |
| 122 TEST_F(ImageGridTest, SingleImage) { | |
| 123 const int kBorder = 1; | |
| 124 scoped_ptr<gfx::Image> image(CreateImage(gfx::Size(kBorder, kBorder))); | |
| 125 | |
| 126 ImageGrid grid; | |
| 127 grid.Init(NULL, image.get(), NULL, | |
| 128 NULL, NULL, NULL, | |
| 129 NULL, NULL, NULL); | |
| 130 | |
| 131 ImageGrid::TestAPI test_api(&grid); | |
| 132 EXPECT_TRUE(test_api.top_left_layer() == NULL); | |
| 133 ASSERT_TRUE(test_api.top_layer() != NULL); | |
| 134 EXPECT_TRUE(test_api.top_right_layer() == NULL); | |
| 135 EXPECT_TRUE(test_api.left_layer() == NULL); | |
| 136 EXPECT_TRUE(test_api.center_layer() == NULL); | |
| 137 EXPECT_TRUE(test_api.right_layer() == NULL); | |
| 138 EXPECT_TRUE(test_api.bottom_left_layer() == NULL); | |
| 139 EXPECT_TRUE(test_api.bottom_layer() == NULL); | |
| 140 EXPECT_TRUE(test_api.bottom_right_layer() == NULL); | |
| 141 | |
| 142 const gfx::Size kSize(10, 10); | |
| 143 grid.SetSize(kSize); | |
| 144 | |
| 145 // The top layer should be scaled horizontally across the entire width, but it | |
| 146 // shouldn't be scaled vertically. | |
| 147 EXPECT_EQ(gfx::Rect(0, 0, kSize.width(), kBorder).ToString(), | |
| 148 test_api.GetTransformedLayerBounds( | |
| 149 *test_api.top_layer()).ToString()); | |
| 150 } | |
| 151 | |
| 152 // Test that side (top, left, right, bottom) layers that are narrower than their | |
| 153 // adjacent corner layers stay pinned to the outside edges instead of getting | |
| 154 // moved inwards or scaled. This exercises the scenario used for shadows. | |
| 155 TEST_F(ImageGridTest, SmallerSides) { | |
| 156 const int kCorner = 2; | |
| 157 const int kEdge = 1; | |
| 158 | |
| 159 scoped_ptr<gfx::Image> top_left_image( | |
| 160 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 161 scoped_ptr<gfx::Image> top_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 162 scoped_ptr<gfx::Image> top_right_image( | |
| 163 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 164 scoped_ptr<gfx::Image> left_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 165 scoped_ptr<gfx::Image> right_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 166 | |
| 167 ImageGrid grid; | |
| 168 grid.Init(top_left_image.get(), top_image.get(), top_right_image.get(), | |
| 169 left_image.get(), NULL, right_image.get(), | |
| 170 NULL, NULL, NULL); | |
| 171 ImageGrid::TestAPI test_api(&grid); | |
| 172 | |
| 173 const gfx::Size kSize(20, 30); | |
| 174 grid.SetSize(kSize); | |
| 175 | |
| 176 // The top layer should be flush with the top edge and stretched horizontally | |
| 177 // between the two top corners. | |
| 178 EXPECT_EQ(gfx::Rect( | |
| 179 kCorner, 0, kSize.width() - 2 * kCorner, kEdge).ToString(), | |
| 180 test_api.GetTransformedLayerBounds( | |
| 181 *test_api.top_layer()).ToString()); | |
| 182 | |
| 183 // The left layer should be flush with the left edge and stretched vertically | |
| 184 // between the top left corner and the bottom. | |
| 185 EXPECT_EQ(gfx::Rect( | |
| 186 0, kCorner, kEdge, kSize.height() - kCorner).ToString(), | |
| 187 test_api.GetTransformedLayerBounds( | |
| 188 *test_api.left_layer()).ToString()); | |
| 189 | |
| 190 // The right layer should be flush with the right edge and stretched | |
| 191 // vertically between the top right corner and the bottom. | |
| 192 EXPECT_EQ(gfx::Rect( | |
| 193 kSize.width() - kEdge, kCorner, | |
| 194 kEdge, kSize.height() - kCorner).ToString(), | |
| 195 test_api.GetTransformedLayerBounds( | |
| 196 *test_api.right_layer()).ToString()); | |
| 197 } | |
| 198 | |
| 199 // Test that we hide or clip layers as needed when the grid is assigned a small | |
| 200 // size. | |
| 201 TEST_F(ImageGridTest, TooSmall) { | |
| 202 const int kCorner = 5; | |
| 203 const int kCenter = 3; | |
| 204 const int kEdge = 3; | |
| 205 | |
| 206 scoped_ptr<gfx::Image> top_left_image( | |
| 207 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 208 scoped_ptr<gfx::Image> top_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 209 scoped_ptr<gfx::Image> top_right_image( | |
| 210 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 211 scoped_ptr<gfx::Image> left_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 212 scoped_ptr<gfx::Image> center_image(CreateImage(gfx::Size(kCenter, kCenter))); | |
| 213 scoped_ptr<gfx::Image> right_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 214 scoped_ptr<gfx::Image> bottom_left_image( | |
| 215 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 216 scoped_ptr<gfx::Image> bottom_image(CreateImage(gfx::Size(kEdge, kEdge))); | |
| 217 scoped_ptr<gfx::Image> bottom_right_image( | |
| 218 CreateImage(gfx::Size(kCorner, kCorner))); | |
| 219 | |
| 220 ImageGrid grid; | |
| 221 grid.Init( | |
| 222 top_left_image.get(), top_image.get(), top_right_image.get(), | |
| 223 left_image.get(), center_image.get(), right_image.get(), | |
| 224 bottom_left_image.get(), bottom_image.get(), bottom_right_image.get()); | |
| 225 ImageGrid::TestAPI test_api(&grid); | |
| 226 | |
| 227 // Set a size that's smaller than the combined (unscaled) corner images. | |
| 228 const gfx::Size kSmallSize(kCorner + kCorner - 3, kCorner + kCorner - 5); | |
| 229 grid.SetSize(kSmallSize); | |
| 230 | |
| 231 // The scalable images around the sides and in the center should be hidden. | |
| 232 EXPECT_FALSE(test_api.top_layer()->visible()); | |
| 233 EXPECT_FALSE(test_api.bottom_layer()->visible()); | |
| 234 EXPECT_FALSE(test_api.left_layer()->visible()); | |
| 235 EXPECT_FALSE(test_api.right_layer()->visible()); | |
| 236 EXPECT_FALSE(test_api.center_layer()->visible()); | |
| 237 | |
| 238 // The corner images' clip rects should sum to the expected size. | |
| 239 EXPECT_EQ(kSmallSize.width(), | |
| 240 test_api.top_left_clip_rect().width() + | |
| 241 test_api.top_right_clip_rect().width()); | |
| 242 EXPECT_EQ(kSmallSize.width(), | |
| 243 test_api.bottom_left_clip_rect().width() + | |
| 244 test_api.bottom_right_clip_rect().width()); | |
| 245 EXPECT_EQ(kSmallSize.height(), | |
| 246 test_api.top_left_clip_rect().height() + | |
| 247 test_api.bottom_left_clip_rect().height()); | |
| 248 EXPECT_EQ(kSmallSize.height(), | |
| 249 test_api.top_right_clip_rect().height() + | |
| 250 test_api.bottom_right_clip_rect().height()); | |
| 251 | |
| 252 // Resize the grid to be large enough to show all images. | |
| 253 const gfx::Size kLargeSize(kCorner + kCorner + kCenter, | |
| 254 kCorner + kCorner + kCenter); | |
| 255 grid.SetSize(kLargeSize); | |
| 256 | |
| 257 // The scalable images should be visible now. | |
| 258 EXPECT_TRUE(test_api.top_layer()->visible()); | |
| 259 EXPECT_TRUE(test_api.bottom_layer()->visible()); | |
| 260 EXPECT_TRUE(test_api.left_layer()->visible()); | |
| 261 EXPECT_TRUE(test_api.right_layer()->visible()); | |
| 262 EXPECT_TRUE(test_api.center_layer()->visible()); | |
| 263 | |
| 264 // We shouldn't be clipping the corner images anymore. | |
| 265 EXPECT_TRUE(test_api.top_left_clip_rect().IsEmpty()); | |
| 266 EXPECT_TRUE(test_api.top_right_clip_rect().IsEmpty()); | |
| 267 EXPECT_TRUE(test_api.bottom_left_clip_rect().IsEmpty()); | |
| 268 EXPECT_TRUE(test_api.bottom_right_clip_rect().IsEmpty()); | |
| 269 } | |
| 270 | |
| 271 } // namespace test | |
| 272 } // namespace aura_shell | |
| OLD | NEW |