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

Side by Side Diff: ui/views/bubble/bubble_border_unittest.cc

Issue 2555373004: Apply new MD shadows to CrOS tray bubbles. (Closed)
Patch Set: . Created 4 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
OLDNEW
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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 EXPECT_EQ(cases[i].expected_without_arrow, 313 EXPECT_EQ(cases[i].expected_without_arrow,
314 border.GetSizeForContentsSize(cases[i].content)); 314 border.GetSizeForContentsSize(cases[i].content));
315 315
316 border.set_paint_arrow(BubbleBorder::PAINT_NONE); 316 border.set_paint_arrow(BubbleBorder::PAINT_NONE);
317 EXPECT_EQ(cases[i].expected_without_arrow, 317 EXPECT_EQ(cases[i].expected_without_arrow,
318 border.GetSizeForContentsSize(cases[i].content)); 318 border.GetSizeForContentsSize(cases[i].content));
319 } 319 }
320 } 320 }
321 321
322 TEST_F(BubbleBorderTest, GetBoundsOriginTest) { 322 TEST_F(BubbleBorderTest, GetBoundsOriginTest) {
323 views::BubbleBorder border(BubbleBorder::TOP_LEFT, 323 for (int i = 0; i < BubbleBorder::SHADOW_COUNT; ++i) {
324 BubbleBorder::NO_SHADOW, 324 BubbleBorder::Shadow shadow = static_cast<BubbleBorder::Shadow>(i);
325 SK_ColorWHITE); 325 SCOPED_TRACE(testing::Message() << "BubbleBorder::Shadow: " << shadow);
326 326
327 const gfx::Rect kAnchor(100, 100, 20, 30); 327 views::BubbleBorder border(BubbleBorder::TOP_LEFT, shadow, SK_ColorWHITE);
328 const gfx::Size kContentSize(50, 60);
329 328
330 const views::internal::BorderImages* kImages = border.GetImagesForTest(); 329 const gfx::Rect kAnchor(100, 100, 20, 30);
330 const gfx::Size kContentSize(500, 600);
Evan Stade 2016/12/08 21:00:27 summary of changes to this test (since it's hard t
331 331
332 border.set_arrow(BubbleBorder::TOP_LEFT); 332 const views::internal::BorderImages* kImages = border.GetImagesForTest();
333 const gfx::Size kTotalSizeWithHorizArrow =
334 border.GetSizeForContentsSize(kContentSize);
335 333
336 border.set_arrow(BubbleBorder::RIGHT_BOTTOM); 334 border.set_arrow(BubbleBorder::TOP_LEFT);
337 const gfx::Size kTotalSizeWithVertArrow = 335 const gfx::Size kTotalSizeWithHorizArrow =
338 border.GetSizeForContentsSize(kContentSize); 336 border.GetSizeForContentsSize(kContentSize);
339 337
340 border.set_arrow(BubbleBorder::NONE); 338 border.set_arrow(BubbleBorder::RIGHT_BOTTOM);
341 const gfx::Size kTotalSizeWithNoArrow = 339 const gfx::Size kTotalSizeWithVertArrow =
342 border.GetSizeForContentsSize(kContentSize); 340 border.GetSizeForContentsSize(kContentSize);
343 341
344 const int kBorderThickness = border.GetBorderThickness(); 342 border.set_arrow(BubbleBorder::NONE);
343 const gfx::Size kTotalSizeWithNoArrow =
344 border.GetSizeForContentsSize(kContentSize);
345 345
346 const int kArrowOffsetForHorizCenter = kTotalSizeWithHorizArrow.width() / 2; 346 const int kBorderThickness = border.GetBorderThickness();
347 const int kArrowOffsetForVertCenter = kTotalSizeWithVertArrow.height() / 2;
348 const int kArrowOffsetForNotCenter =
349 kImages->border_thickness + (kImages->arrow_width / 2);
350 347
351 const int kArrowThickness = kImages->arrow_interior_thickness; 348 const int kArrowOffsetForHorizCenter = kTotalSizeWithHorizArrow.width() / 2;
352 const int kArrowShift = 349 const int kArrowOffsetForVertCenter = kTotalSizeWithVertArrow.height() / 2;
353 kArrowThickness + BubbleBorder::kStroke - kImages->arrow_thickness; 350 const int kArrowOffsetForNotCenter =
354 const int kHeightDifference = kTotalSizeWithHorizArrow.height() - 351 kImages->border_thickness + (kImages->arrow_width / 2);
355 kTotalSizeWithNoArrow.height();
356 const int kWidthDifference = kTotalSizeWithVertArrow.width() -
357 kTotalSizeWithNoArrow.width();
358 EXPECT_EQ(kHeightDifference, kWidthDifference);
359 EXPECT_EQ(kHeightDifference, kArrowThickness);
360 352
361 const int kTopHorizArrowY = kAnchor.y() + kAnchor.height() + kArrowShift; 353 const int kStrokeWidth =
362 const int kBottomHorizArrowY = 354 shadow == BubbleBorder::NO_ASSETS ? 0 : BubbleBorder::kStroke;
363 kAnchor.y() - kArrowShift - kTotalSizeWithHorizArrow.height();
364 const int kLeftVertArrowX = kAnchor.x() + kAnchor.width() + kArrowShift;
365 const int kRightVertArrowX =
366 kAnchor.x() - kArrowShift - kTotalSizeWithVertArrow.width();
367 355
368 struct TestCase { 356 const int kArrowThickness = kImages->arrow_interior_thickness;
369 BubbleBorder::Arrow arrow; 357 const int kArrowShift =
370 BubbleBorder::BubbleAlignment alignment; 358 kArrowThickness + kStrokeWidth - kImages->arrow_thickness;
371 int expected_x; 359 const int kHeightDifference =
372 int expected_y; 360 kTotalSizeWithHorizArrow.height() - kTotalSizeWithNoArrow.height();
373 }; 361 const int kWidthDifference =
362 kTotalSizeWithVertArrow.width() - kTotalSizeWithNoArrow.width();
363 EXPECT_EQ(kHeightDifference, kWidthDifference);
374 364
375 TestCase cases[] = { 365 // The arrow only makes a difference in height if it is longer than the
376 // Horizontal arrow tests. 366 // shadow.
377 { BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, 367 const int kExpectedHeightDifference =
378 kAnchor.CenterPoint().x() - kArrowOffsetForNotCenter, kTopHorizArrowY }, 368 std::max(kImages->arrow_thickness + kImages->border_interior_thickness,
379 { BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, 369 kImages->border_thickness) -
380 kAnchor.x() + BubbleBorder::kStroke - kBorderThickness, kTopHorizArrowY }, 370 std::max(kImages->border_interior_thickness, kImages->border_thickness);
381 { BubbleBorder::TOP_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, 371 EXPECT_EQ(kExpectedHeightDifference, kHeightDifference)
382 kAnchor.CenterPoint().x() - kArrowOffsetForHorizCenter, kTopHorizArrowY }, 372 << "Size with arrow: " << kTotalSizeWithHorizArrow.ToString()
383 { BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, 373 << " vs. size without arrow: " << kTotalSizeWithNoArrow.ToString();
384 kAnchor.CenterPoint().x() + kArrowOffsetForNotCenter -
385 kTotalSizeWithHorizArrow.width(), kBottomHorizArrowY },
386 { BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE,
387 kAnchor.x() + kAnchor.width() - kTotalSizeWithHorizArrow.width() +
388 kBorderThickness - BubbleBorder::kStroke, kBottomHorizArrowY },
389 374
390 // Vertical arrow tests. 375 const int kTopHorizArrowY = kAnchor.y() + kAnchor.height() + kArrowShift;
391 { BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, 376 const int kBottomHorizArrowY =
392 kLeftVertArrowX, kAnchor.CenterPoint().y() - kArrowOffsetForNotCenter }, 377 kAnchor.y() - kArrowShift - kTotalSizeWithHorizArrow.height();
393 { BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, 378 const int kLeftVertArrowX = kAnchor.x() + kAnchor.width() + kArrowShift;
394 kLeftVertArrowX, kAnchor.y() + BubbleBorder::kStroke - kBorderThickness }, 379 const int kRightVertArrowX =
395 { BubbleBorder::LEFT_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, 380 kAnchor.x() - kArrowShift - kTotalSizeWithVertArrow.width();
396 kLeftVertArrowX, kAnchor.CenterPoint().y() - kArrowOffsetForVertCenter },
397 { BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR,
398 kRightVertArrowX, kAnchor.CenterPoint().y() + kArrowOffsetForNotCenter -
399 kTotalSizeWithVertArrow.height() },
400 { BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE,
401 kRightVertArrowX, kAnchor.y() + kAnchor.height() -
402 kTotalSizeWithVertArrow.height() + kBorderThickness -
403 BubbleBorder::kStroke },
404 381
405 // No arrow tests. 382 struct TestCase {
406 { BubbleBorder::NONE, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, 383 BubbleBorder::Arrow arrow;
407 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, 384 BubbleBorder::BubbleAlignment alignment;
408 kAnchor.y() + kAnchor.height() }, 385 int expected_x;
409 { BubbleBorder::FLOAT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, 386 int expected_y;
410 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, 387 };
411 kAnchor.y() + (kAnchor.height() - kTotalSizeWithNoArrow.height()) / 2 },
412 };
413 388
414 for (size_t i = 0; i < arraysize(cases); ++i) { 389 TestCase cases[] = {
415 SCOPED_TRACE(base::StringPrintf("i=%d arrow=%d alignment=%d", 390 // Horizontal arrow tests.
416 static_cast<int>(i), cases[i].arrow, cases[i].alignment)); 391 {BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR,
417 const BubbleBorder::Arrow arrow = cases[i].arrow; 392 kAnchor.CenterPoint().x() - kArrowOffsetForNotCenter, kTopHorizArrowY},
418 border.set_arrow(arrow); 393 {BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE,
419 border.set_alignment(cases[i].alignment); 394 kAnchor.x() + kStrokeWidth - kBorderThickness, kTopHorizArrowY},
395 {BubbleBorder::TOP_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR,
396 kAnchor.CenterPoint().x() - kArrowOffsetForHorizCenter,
397 kTopHorizArrowY},
398 {BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR,
399 kAnchor.CenterPoint().x() + kArrowOffsetForNotCenter -
400 kTotalSizeWithHorizArrow.width(),
401 kBottomHorizArrowY},
402 {BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE,
403 kAnchor.x() + kAnchor.width() - kTotalSizeWithHorizArrow.width() +
404 kBorderThickness - kStrokeWidth,
405 kBottomHorizArrowY},
420 406
421 border.set_paint_arrow(BubbleBorder::PAINT_NORMAL); 407 // Vertical arrow tests.
422 gfx::Point origin = border.GetBounds(kAnchor, kContentSize).origin(); 408 {BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR,
423 int expected_x = cases[i].expected_x; 409 kLeftVertArrowX, kAnchor.CenterPoint().y() - kArrowOffsetForNotCenter},
424 int expected_y = cases[i].expected_y; 410 {BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE,
425 EXPECT_EQ(expected_x, origin.x()); 411 kLeftVertArrowX, kAnchor.y() + kStrokeWidth - kBorderThickness},
426 EXPECT_EQ(expected_y, origin.y()); 412 {BubbleBorder::LEFT_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR,
413 kLeftVertArrowX,
414 kAnchor.CenterPoint().y() - kArrowOffsetForVertCenter},
415 {BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR,
416 kRightVertArrowX,
417 kAnchor.CenterPoint().y() + kArrowOffsetForNotCenter -
418 kTotalSizeWithVertArrow.height()},
419 {BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE,
420 kRightVertArrowX,
421 kAnchor.y() + kAnchor.height() - kTotalSizeWithVertArrow.height() +
422 kBorderThickness - kStrokeWidth},
427 423
428 border.set_paint_arrow(BubbleBorder::PAINT_TRANSPARENT); 424 // No arrow tests.
429 origin = border.GetBounds(kAnchor, kContentSize).origin(); 425 {BubbleBorder::NONE, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR,
430 if (border.is_arrow_on_horizontal(arrow)) { 426 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2,
431 expected_y += BubbleBorder::is_arrow_on_top(arrow) 427 kAnchor.y() + kAnchor.height()},
432 ? kArrowThickness : (-kArrowThickness + kHeightDifference); 428 {BubbleBorder::FLOAT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR,
433 } else if (BubbleBorder::has_arrow(arrow)) { 429 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2,
434 expected_x += BubbleBorder::is_arrow_on_left(arrow) 430 kAnchor.y() + (kAnchor.height() - kTotalSizeWithNoArrow.height()) / 2},
435 ? kArrowThickness : (-kArrowThickness + kWidthDifference); 431 };
432
433 for (size_t i = 0; i < arraysize(cases); ++i) {
434 SCOPED_TRACE(base::StringPrintf("i=%d arrow=%d alignment=%d",
435 static_cast<int>(i), cases[i].arrow,
436 cases[i].alignment));
437 const BubbleBorder::Arrow arrow = cases[i].arrow;
438 border.set_arrow(arrow);
439 border.set_alignment(cases[i].alignment);
440
441 border.set_paint_arrow(BubbleBorder::PAINT_NORMAL);
442 gfx::Point origin = border.GetBounds(kAnchor, kContentSize).origin();
443 int expected_x = cases[i].expected_x;
444 int expected_y = cases[i].expected_y;
445 EXPECT_EQ(expected_x, origin.x());
446 EXPECT_EQ(expected_y, origin.y());
447
448 border.set_paint_arrow(BubbleBorder::PAINT_TRANSPARENT);
449 origin = border.GetBounds(kAnchor, kContentSize).origin();
450 if (border.is_arrow_on_horizontal(arrow)) {
451 expected_y += BubbleBorder::is_arrow_on_top(arrow)
452 ? kArrowThickness
453 : (-kArrowThickness + kHeightDifference);
454 } else if (BubbleBorder::has_arrow(arrow)) {
455 expected_x += BubbleBorder::is_arrow_on_left(arrow)
456 ? kArrowThickness
457 : (-kArrowThickness + kWidthDifference);
458 }
459 EXPECT_EQ(expected_x, origin.x());
460 EXPECT_EQ(expected_y, origin.y());
461
462 border.set_paint_arrow(BubbleBorder::PAINT_NONE);
463 origin = border.GetBounds(kAnchor, kContentSize).origin();
464 expected_x = cases[i].expected_x;
465 expected_y = cases[i].expected_y;
466 if (border.is_arrow_on_horizontal(arrow) &&
467 !BubbleBorder::is_arrow_on_top(arrow)) {
468 expected_y += kHeightDifference;
469 } else if (BubbleBorder::has_arrow(arrow) &&
470 !border.is_arrow_on_horizontal(arrow) &&
471 !BubbleBorder::is_arrow_on_left(arrow)) {
472 expected_x += kWidthDifference;
473 }
474 EXPECT_EQ(expected_x, origin.x());
475 EXPECT_EQ(expected_y, origin.y());
436 } 476 }
437 EXPECT_EQ(expected_x, origin.x()); 477 }
438 EXPECT_EQ(expected_y, origin.y());
439
440 border.set_paint_arrow(BubbleBorder::PAINT_NONE);
441 origin = border.GetBounds(kAnchor, kContentSize).origin();
442 expected_x = cases[i].expected_x;
443 expected_y = cases[i].expected_y;
444 if (border.is_arrow_on_horizontal(arrow) &&
445 !BubbleBorder::is_arrow_on_top(arrow)) {
446 expected_y += kHeightDifference;
447 } else if (BubbleBorder::has_arrow(arrow) &&
448 !border.is_arrow_on_horizontal(arrow) &&
449 !BubbleBorder::is_arrow_on_left(arrow)) {
450 expected_x += kWidthDifference;
451 }
452 EXPECT_EQ(expected_x, origin.x());
453 EXPECT_EQ(expected_y, origin.y());
454 }
455 } 478 }
456 479
457 // Ensure all the shadow types pass some size validation and paint sanely. 480 // Ensure all the shadow types pass some size validation and paint sanely.
458 TEST_F(BubbleBorderTest, ShadowTypes) { 481 TEST_F(BubbleBorderTest, ShadowTypes) {
459 const gfx::Rect rect(0, 0, 320, 200); 482 const gfx::Rect rect(0, 0, 320, 200);
460 View paint_view; 483 View paint_view;
461 paint_view.SetBoundsRect(rect); 484 paint_view.SetBoundsRect(rect);
462 485
463 for (int i = 0; i < BubbleBorder::SHADOW_COUNT; ++i) { 486 for (int i = 0; i < BubbleBorder::SHADOW_COUNT; ++i) {
464 BubbleBorder::Shadow shadow = static_cast<BubbleBorder::Shadow>(i); 487 BubbleBorder::Shadow shadow = static_cast<BubbleBorder::Shadow>(i);
(...skipping 12 matching lines...) Expand all
477 EXPECT_GE(border_images->border_thickness, 500 EXPECT_GE(border_images->border_thickness,
478 border_images->border_interior_thickness); 501 border_images->border_interior_thickness);
479 502
480 // For a TOP_LEFT arrow, the x-offset always matches the border thickness. 503 // For a TOP_LEFT arrow, the x-offset always matches the border thickness.
481 EXPECT_EQ(border.GetArrowRect(rect).x(), border_images->border_thickness); 504 EXPECT_EQ(border.GetArrowRect(rect).x(), border_images->border_thickness);
482 border.Paint(paint_view, &canvas); 505 border.Paint(paint_view, &canvas);
483 } 506 }
484 } 507 }
485 508
486 } // namespace views 509 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698