view_shadow_unittest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2019 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. #include "ash/public/cpp/view_shadow.h"
  5. #include "ui/compositor/layer.h"
  6. #include "ui/compositor_extra/shadow.h"
  7. #include "ui/gfx/shadow_util.h"
  8. #include "ui/views/test/views_test_base.h"
  9. #include "ui/views/view.h"
  10. namespace ash {
  11. using ViewShadowTest = views::ViewsTestBase;
  12. TEST_F(ViewShadowTest, UseShadow) {
  13. views::View root;
  14. root.SetPaintToLayer();
  15. views::View* v1 = root.AddChildView(std::make_unique<views::View>());
  16. v1->SetPaintToLayer();
  17. views::View* v2 = root.AddChildView(std::make_unique<views::View>());
  18. v2->SetPaintToLayer();
  19. views::View* v3 = root.AddChildView(std::make_unique<views::View>());
  20. v3->SetPaintToLayer();
  21. auto shadow = std::make_unique<ViewShadow>(v2, 1);
  22. ASSERT_EQ(4u, root.layer()->children().size());
  23. EXPECT_EQ(v1->layer(), root.layer()->children()[0]);
  24. EXPECT_EQ(shadow->shadow()->layer(), root.layer()->children()[1]);
  25. EXPECT_EQ(v2->layer(), root.layer()->children()[2]);
  26. EXPECT_EQ(v3->layer(), root.layer()->children()[3]);
  27. shadow.reset();
  28. EXPECT_EQ(3u, root.layer()->children().size());
  29. EXPECT_EQ(v1->layer(), root.layer()->children()[0]);
  30. EXPECT_EQ(v2->layer(), root.layer()->children()[1]);
  31. EXPECT_EQ(v3->layer(), root.layer()->children()[2]);
  32. }
  33. TEST_F(ViewShadowTest, ShadowBoundsFollowView) {
  34. views::View view;
  35. view.SetBoundsRect(gfx::Rect(10, 20, 30, 40));
  36. ViewShadow shadow(&view, 1);
  37. EXPECT_EQ(gfx::Rect(10, 20, 30, 40), shadow.shadow()->content_bounds());
  38. view.SetBoundsRect(gfx::Rect(100, 110, 120, 130));
  39. EXPECT_EQ(gfx::Rect(100, 110, 120, 130), shadow.shadow()->content_bounds());
  40. }
  41. TEST_F(ViewShadowTest, ShadowBoundsFollowIndirectViewBoundsChange) {
  42. views::View root;
  43. root.SetPaintToLayer();
  44. root.SetBoundsRect(gfx::Rect(100, 100, 200, 200));
  45. views::View* parent = root.AddChildView(std::make_unique<views::View>());
  46. parent->SetBoundsRect(gfx::Rect(10, 20, 70, 80));
  47. views::View* view = parent->AddChildView(std::make_unique<views::View>());
  48. view->SetBoundsRect(gfx::Rect(5, 10, 20, 30));
  49. ViewShadow shadow(view, 1);
  50. EXPECT_EQ(gfx::Rect(15, 30, 20, 30), shadow.shadow()->content_bounds());
  51. parent->SetBoundsRect(gfx::Rect(5, 15, 60, 70));
  52. EXPECT_EQ(gfx::Rect(10, 25, 20, 30), shadow.shadow()->content_bounds());
  53. }
  54. TEST_F(ViewShadowTest, ShadowCornerRadius) {
  55. views::View view;
  56. view.SetBoundsRect(gfx::Rect(10, 20, 30, 40));
  57. ViewShadow shadow(&view, 1);
  58. shadow.SetRoundedCornerRadius(5);
  59. EXPECT_EQ(gfx::RoundedCornersF(5), view.layer()->rounded_corner_radii());
  60. EXPECT_EQ(gfx::ShadowDetails::Get(1, 5).values,
  61. shadow.shadow()->details_for_testing()->values);
  62. shadow.SetRoundedCornerRadius(2);
  63. EXPECT_EQ(gfx::RoundedCornersF(2), view.layer()->rounded_corner_radii());
  64. EXPECT_EQ(gfx::ShadowDetails::Get(1, 2).values,
  65. shadow.shadow()->details_for_testing()->values);
  66. }
  67. TEST_F(ViewShadowTest, ViewDestruction) {
  68. views::View root;
  69. root.SetPaintToLayer();
  70. root.SetBoundsRect(gfx::Rect(10, 20, 30, 40));
  71. views::View* v1 = root.AddChildView(std::make_unique<views::View>());
  72. ViewShadow shadow(v1, 1);
  73. EXPECT_EQ(2u, root.layer()->children().size());
  74. delete v1;
  75. EXPECT_TRUE(root.layer()->children().empty());
  76. }
  77. TEST_F(ViewShadowTest, ShadowKeepsLayerType) {
  78. views::View view;
  79. view.SetPaintToLayer(ui::LAYER_SOLID_COLOR);
  80. view.SetBoundsRect(gfx::Rect(10, 20, 30, 40));
  81. ViewShadow shadow(&view, 1);
  82. EXPECT_TRUE(view.layer());
  83. EXPECT_EQ(ui::LAYER_SOLID_COLOR, view.layer()->type());
  84. }
  85. } // namespace ash