OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/views/bubble/bubble_border.h" | 5 #include "ui/views/bubble/bubble_border.h" |
6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/stringprintf.h" |
7 #include "ui/views/test/views_test_base.h" | 9 #include "ui/views/test/views_test_base.h" |
8 | 10 |
9 namespace views { | 11 namespace views { |
10 | 12 |
11 typedef ViewsTestBase BubbleBorderTest; | 13 typedef views::ViewsTestBase BubbleBorderTest; |
12 | 14 |
13 TEST_F(BubbleBorderTest, GetMirroredArrow) { | 15 TEST_F(BubbleBorderTest, GetMirroredArrow) { |
14 // Horizontal mirroring. | 16 // Horizontal mirroring. |
15 EXPECT_EQ(BubbleBorder::TOP_RIGHT, | 17 EXPECT_EQ(BubbleBorder::TOP_RIGHT, |
16 BubbleBorder::horizontal_mirror(BubbleBorder::TOP_LEFT)); | 18 BubbleBorder::horizontal_mirror(BubbleBorder::TOP_LEFT)); |
17 EXPECT_EQ(BubbleBorder::TOP_LEFT, | 19 EXPECT_EQ(BubbleBorder::TOP_LEFT, |
18 BubbleBorder::horizontal_mirror(BubbleBorder::TOP_RIGHT)); | 20 BubbleBorder::horizontal_mirror(BubbleBorder::TOP_RIGHT)); |
19 | 21 |
20 EXPECT_EQ(BubbleBorder::BOTTOM_RIGHT, | 22 EXPECT_EQ(BubbleBorder::BOTTOM_RIGHT, |
21 BubbleBorder::horizontal_mirror(BubbleBorder::BOTTOM_LEFT)); | 23 BubbleBorder::horizontal_mirror(BubbleBorder::BOTTOM_LEFT)); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::TOP_CENTER)); | 197 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::TOP_CENTER)); |
196 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::BOTTOM_CENTER)); | 198 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::BOTTOM_CENTER)); |
197 | 199 |
198 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::LEFT_CENTER)); | 200 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::LEFT_CENTER)); |
199 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::RIGHT_CENTER)); | 201 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::RIGHT_CENTER)); |
200 | 202 |
201 EXPECT_FALSE(BubbleBorder::is_arrow_at_center(BubbleBorder::NONE)); | 203 EXPECT_FALSE(BubbleBorder::is_arrow_at_center(BubbleBorder::NONE)); |
202 EXPECT_FALSE(BubbleBorder::is_arrow_at_center(BubbleBorder::FLOAT)); | 204 EXPECT_FALSE(BubbleBorder::is_arrow_at_center(BubbleBorder::FLOAT)); |
203 } | 205 } |
204 | 206 |
205 TEST_F(BubbleBorderTest, TestMinimalSize) { | 207 TEST_F(BubbleBorderTest, GetSizeForContentsSizeTest) { |
206 gfx::Rect anchor = gfx::Rect(100, 100, 20, 20); | 208 views::BubbleBorder border(BubbleBorder::NONE, |
207 gfx::Size contents = gfx::Size(10, 10); | 209 BubbleBorder::NO_SHADOW, |
208 BubbleBorder b1(BubbleBorder::RIGHT_TOP, BubbleBorder::NO_SHADOW, 0); | 210 SK_ColorWHITE); |
209 | 211 |
210 // The height should be much bigger then the requested size + border and | 212 const views::internal::BorderImages* kImages = border.GetImagesForTest(); |
211 // padding since it needs to be able to include the tip bitmap. | 213 |
212 gfx::Rect visible_tip_1 = b1.GetBounds(anchor, contents); | 214 // kSmallSize is smaller than the minimum allowable size and does not |
213 EXPECT_GE(visible_tip_1.height(), 30); | 215 // contribute to the resulting size. |
214 EXPECT_LE(visible_tip_1.width(), 30); | 216 const gfx::Size kSmallSize = gfx::Size(1, 2); |
215 | 217 // kMediumSize is larger than the minimum allowable size and contributes to |
216 // With the tip being invisible the height should now be much smaller. | 218 // the resulting size. |
217 b1.set_paint_arrow(BubbleBorder::PAINT_TRANSPARENT); | 219 const gfx::Size kMediumSize = gfx::Size(50, 60); |
218 gfx::Rect invisible_tip_1 = b1.GetBounds(anchor, contents); | 220 |
219 EXPECT_LE(invisible_tip_1.height(), 30); | 221 const gfx::Size kSmallHorizArrow( |
220 EXPECT_LE(invisible_tip_1.width(), 30); | 222 2 * kImages->border_thickness + kImages->top_arrow.width(), |
221 | 223 kImages->border_thickness + kImages->arrow_thickness + |
222 // When the orientation of the tip changes, the above mentioned tests need to | 224 kImages->border_interior_thickness); |
223 // be reverse for width and height. | 225 |
224 BubbleBorder b2(BubbleBorder::TOP_RIGHT, BubbleBorder::NO_SHADOW, 0); | 226 const gfx::Size kSmallVertArrow(kSmallHorizArrow.height(), |
225 | 227 kSmallHorizArrow.width()); |
226 // The width should be much bigger then the requested size + border and | 228 |
227 // padding since it needs to be able to include the tip bitmap. | 229 const gfx::Size kSmallNoArrow(2 * kImages->border_thickness, |
228 gfx::Rect visible_tip_2 = b2.GetBounds(anchor, contents); | 230 2 * kImages->border_thickness); |
229 EXPECT_GE(visible_tip_2.width(), 30); | 231 |
230 EXPECT_LE(visible_tip_2.height(), 30); | 232 const gfx::Size kMediumHorizArrow( |
231 | 233 kMediumSize.width() + 2 * border.GetBorderThickness(), |
232 // With the tip being invisible the width should now be much smaller. | 234 kMediumSize.height() + border.GetBorderThickness() + |
233 b2.set_paint_arrow(BubbleBorder::PAINT_TRANSPARENT); | 235 kImages->arrow_thickness); |
234 gfx::Rect invisible_tip_2 = b2.GetBounds(anchor, contents); | 236 |
235 EXPECT_LE(invisible_tip_2.width(), 30); | 237 const gfx::Size kMediumVertArrow( |
236 EXPECT_LE(invisible_tip_2.height(), 30); | 238 kMediumSize.width() + border.GetBorderThickness() + |
| 239 kImages->arrow_thickness, |
| 240 kMediumSize.height() + 2 * border.GetBorderThickness()); |
| 241 |
| 242 const gfx::Size kMediumNoArrow( |
| 243 kMediumSize.width() + 2 * border.GetBorderThickness(), |
| 244 kMediumSize.height() + 2 * border.GetBorderThickness()); |
| 245 |
| 246 struct TestCase { |
| 247 BubbleBorder::Arrow arrow; |
| 248 gfx::Size content; |
| 249 gfx::Size expected_with_arrow; |
| 250 gfx::Size expected_without_arrow; |
| 251 }; |
| 252 |
| 253 TestCase cases[] = { |
| 254 // Content size: kSmallSize |
| 255 { BubbleBorder::TOP_LEFT, kSmallSize, kSmallHorizArrow, kSmallNoArrow }, |
| 256 { BubbleBorder::TOP_CENTER, kSmallSize, kSmallHorizArrow, kSmallNoArrow }, |
| 257 { BubbleBorder::TOP_RIGHT, kSmallSize, kSmallHorizArrow, kSmallNoArrow }, |
| 258 { BubbleBorder::BOTTOM_LEFT, kSmallSize, kSmallHorizArrow, kSmallNoArrow }, |
| 259 { BubbleBorder::BOTTOM_CENTER, kSmallSize, kSmallHorizArrow, |
| 260 kSmallNoArrow }, |
| 261 { BubbleBorder::BOTTOM_RIGHT, kSmallSize, kSmallHorizArrow, kSmallNoArrow }, |
| 262 { BubbleBorder::LEFT_TOP, kSmallSize, kSmallVertArrow, kSmallNoArrow }, |
| 263 { BubbleBorder::LEFT_CENTER, kSmallSize, kSmallVertArrow, kSmallNoArrow }, |
| 264 { BubbleBorder::LEFT_BOTTOM, kSmallSize, kSmallVertArrow, kSmallNoArrow }, |
| 265 { BubbleBorder::RIGHT_TOP, kSmallSize, kSmallVertArrow, kSmallNoArrow }, |
| 266 { BubbleBorder::RIGHT_CENTER, kSmallSize, kSmallVertArrow, kSmallNoArrow }, |
| 267 { BubbleBorder::RIGHT_BOTTOM, kSmallSize, kSmallVertArrow, kSmallNoArrow }, |
| 268 { BubbleBorder::NONE, kSmallSize, kSmallNoArrow, kSmallNoArrow }, |
| 269 { BubbleBorder::FLOAT, kSmallSize, kSmallNoArrow, kSmallNoArrow }, |
| 270 |
| 271 // Content size: kMediumSize |
| 272 { BubbleBorder::TOP_LEFT, kMediumSize, kMediumHorizArrow, kMediumNoArrow }, |
| 273 { BubbleBorder::TOP_CENTER, kMediumSize, kMediumHorizArrow, |
| 274 kMediumNoArrow }, |
| 275 { BubbleBorder::TOP_RIGHT, kMediumSize, kMediumHorizArrow, kMediumNoArrow }, |
| 276 { BubbleBorder::BOTTOM_LEFT, kMediumSize, kMediumHorizArrow, |
| 277 kMediumNoArrow }, |
| 278 { BubbleBorder::BOTTOM_CENTER, kMediumSize, kMediumHorizArrow, |
| 279 kMediumNoArrow }, |
| 280 { BubbleBorder::BOTTOM_RIGHT, kMediumSize, kMediumHorizArrow, |
| 281 kMediumNoArrow }, |
| 282 { BubbleBorder::LEFT_TOP, kMediumSize, kMediumVertArrow, kMediumNoArrow }, |
| 283 { BubbleBorder::LEFT_CENTER, kMediumSize, kMediumVertArrow, |
| 284 kMediumNoArrow }, |
| 285 { BubbleBorder::LEFT_BOTTOM, kMediumSize, kMediumVertArrow, |
| 286 kMediumNoArrow }, |
| 287 { BubbleBorder::RIGHT_TOP, kMediumSize, kMediumVertArrow, kMediumNoArrow }, |
| 288 { BubbleBorder::RIGHT_CENTER, kMediumSize, kMediumVertArrow, |
| 289 kMediumNoArrow }, |
| 290 { BubbleBorder::RIGHT_BOTTOM, kMediumSize, kMediumVertArrow, |
| 291 kMediumNoArrow }, |
| 292 { BubbleBorder::NONE, kMediumSize, kMediumNoArrow, kMediumNoArrow }, |
| 293 { BubbleBorder::FLOAT, kMediumSize, kMediumNoArrow, kMediumNoArrow } |
| 294 }; |
| 295 |
| 296 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 297 SCOPED_TRACE(base::StringPrintf("i=%d arrow=%d", |
| 298 static_cast<int>(i), cases[i].arrow)); |
| 299 |
| 300 border.set_arrow(cases[i].arrow); |
| 301 |
| 302 border.set_paint_arrow(BubbleBorder::PAINT_NORMAL); |
| 303 EXPECT_EQ(cases[i].expected_with_arrow, |
| 304 border.GetSizeForContentsSize(cases[i].content)); |
| 305 |
| 306 border.set_paint_arrow(BubbleBorder::PAINT_TRANSPARENT); |
| 307 EXPECT_EQ(cases[i].expected_with_arrow, |
| 308 border.GetSizeForContentsSize(cases[i].content)); |
| 309 |
| 310 border.set_paint_arrow(BubbleBorder::PAINT_NONE); |
| 311 EXPECT_EQ(cases[i].expected_without_arrow, |
| 312 border.GetSizeForContentsSize(cases[i].content)); |
| 313 } |
237 } | 314 } |
238 | 315 |
| 316 TEST_F(BubbleBorderTest, GetBoundsOriginTest) { |
| 317 views::BubbleBorder border(BubbleBorder::TOP_LEFT, |
| 318 BubbleBorder::NO_SHADOW, |
| 319 SK_ColorWHITE); |
| 320 |
| 321 const gfx::Rect kAnchor(100, 100, 20, 30); |
| 322 const gfx::Size kContentSize(50, 60); |
| 323 |
| 324 const views::internal::BorderImages* kImages = border.GetImagesForTest(); |
| 325 |
| 326 border.set_arrow(BubbleBorder::TOP_LEFT); |
| 327 const gfx::Size kTotalSizeWithHorizArrow = |
| 328 border.GetSizeForContentsSize(kContentSize); |
| 329 |
| 330 border.set_arrow(BubbleBorder::RIGHT_BOTTOM); |
| 331 const gfx::Size kTotalSizeWithVertArrow = |
| 332 border.GetSizeForContentsSize(kContentSize); |
| 333 |
| 334 border.set_arrow(BubbleBorder::NONE); |
| 335 const gfx::Size kTotalSizeWithNoArrow = |
| 336 border.GetSizeForContentsSize(kContentSize); |
| 337 |
| 338 const int kBorderThickness = border.GetBorderThickness(); |
| 339 |
| 340 const int kArrowOffsetForHorizCenter = kTotalSizeWithHorizArrow.width() / 2; |
| 341 const int kArrowOffsetForVertCenter = kTotalSizeWithVertArrow.height() / 2; |
| 342 const int kArrowOffsetForNotCenter = |
| 343 kImages->border_thickness + (kImages->top_arrow.width() / 2); |
| 344 |
| 345 const int kArrowSize = |
| 346 kImages->arrow_interior_thickness + BubbleBorder::kStroke - |
| 347 kImages->arrow_thickness; |
| 348 |
| 349 const int kTopHorizArrowY = kAnchor.y() + kAnchor.height() + kArrowSize; |
| 350 const int kBottomHorizArrowY = |
| 351 kAnchor.y() - kArrowSize - kTotalSizeWithHorizArrow.height(); |
| 352 |
| 353 const int kLeftVertArrowX = kAnchor.x() + kAnchor.width() + kArrowSize; |
| 354 const int kRightVertArrowX = |
| 355 kAnchor.x() - kArrowSize - kTotalSizeWithVertArrow.width(); |
| 356 |
| 357 struct TestCase { |
| 358 BubbleBorder::Arrow arrow; |
| 359 BubbleBorder::BubbleAlignment alignment; |
| 360 int expected_x; |
| 361 int expected_y; |
| 362 }; |
| 363 |
| 364 TestCase cases[] = { |
| 365 // Horizontal arrow tests. |
| 366 { BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 367 kAnchor.CenterPoint().x() - kArrowOffsetForNotCenter, kTopHorizArrowY }, |
| 368 { BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, |
| 369 kAnchor.x() + BubbleBorder::kStroke - kBorderThickness, kTopHorizArrowY }, |
| 370 { BubbleBorder::TOP_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 371 kAnchor.CenterPoint().x() - kArrowOffsetForHorizCenter, kTopHorizArrowY }, |
| 372 { BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 373 kAnchor.CenterPoint().x() + kArrowOffsetForNotCenter - |
| 374 kTotalSizeWithHorizArrow.width(), kBottomHorizArrowY }, |
| 375 { BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, |
| 376 kAnchor.x() + kAnchor.width() - kTotalSizeWithHorizArrow.width() + |
| 377 kBorderThickness - BubbleBorder::kStroke, kBottomHorizArrowY }, |
| 378 |
| 379 // Vertical arrow tests. |
| 380 { BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 381 kLeftVertArrowX, kAnchor.CenterPoint().y() - kArrowOffsetForNotCenter }, |
| 382 { BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, |
| 383 kLeftVertArrowX, kAnchor.y() + BubbleBorder::kStroke - kBorderThickness }, |
| 384 { BubbleBorder::LEFT_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 385 kLeftVertArrowX, kAnchor.CenterPoint().y() - kArrowOffsetForVertCenter }, |
| 386 { BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 387 kRightVertArrowX, kAnchor.CenterPoint().y() + kArrowOffsetForNotCenter - |
| 388 kTotalSizeWithVertArrow.height() }, |
| 389 { BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, |
| 390 kRightVertArrowX, kAnchor.y() + kAnchor.height() - |
| 391 kTotalSizeWithVertArrow.height() + kBorderThickness - |
| 392 BubbleBorder::kStroke }, |
| 393 |
| 394 // No arrow tests. |
| 395 { BubbleBorder::NONE, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 396 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, |
| 397 kAnchor.y() + kAnchor.height() }, |
| 398 { BubbleBorder::FLOAT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, |
| 399 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, |
| 400 kAnchor.y() + (kAnchor.height() - kTotalSizeWithNoArrow.height()) / 2 }, |
| 401 }; |
| 402 |
| 403 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 404 SCOPED_TRACE(base::StringPrintf("i=%d arrow=%d alignment=%d", |
| 405 static_cast<int>(i), cases[i].arrow, cases[i].alignment)); |
| 406 border.set_arrow(cases[i].arrow); |
| 407 border.set_alignment(cases[i].alignment); |
| 408 |
| 409 gfx::Point origin = border.GetBounds(kAnchor, kContentSize).origin(); |
| 410 EXPECT_EQ(cases[i].expected_x, origin.x()); |
| 411 EXPECT_EQ(cases[i].expected_y, origin.y()); |
| 412 } |
| 413 } |
239 | 414 |
240 } // namespace views | 415 } // namespace views |
OLD | NEW |