| 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/controls/scroll_view.h" | 5 #include "ui/views/controls/scroll_view.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/views/border.h" | 9 #include "ui/views/border.h" |
| 10 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h" | 10 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h" |
| 11 #include "ui/views/test/test_views.h" | 11 #include "ui/views/test/test_views.h" |
| 12 | 12 |
| 13 #if defined(OS_MACOSX) |
| 14 #include "ui/base/test/scoped_preferred_scroller_style_mac.h" |
| 15 #endif |
| 16 |
| 13 namespace views { | 17 namespace views { |
| 14 | 18 |
| 15 namespace { | 19 namespace { |
| 16 | 20 |
| 17 const int kWidth = 100; | 21 const int kWidth = 100; |
| 18 const int kMinHeight = 50; | 22 const int kMinHeight = 50; |
| 19 const int kMaxHeight = 100; | 23 const int kMaxHeight = 100; |
| 20 | 24 |
| 25 enum ScrollBarOrientation { HORIZONTAL, VERTICAL }; |
| 26 |
| 21 // View implementation that allows setting the preferred size. | 27 // View implementation that allows setting the preferred size. |
| 22 class CustomView : public View { | 28 class CustomView : public View { |
| 23 public: | 29 public: |
| 24 CustomView() {} | 30 CustomView() {} |
| 25 | 31 |
| 26 void SetPreferredSize(const gfx::Size& size) { | 32 void SetPreferredSize(const gfx::Size& size) { |
| 27 preferred_size_ = size; | 33 preferred_size_ = size; |
| 28 PreferredSizeChanged(); | 34 PreferredSizeChanged(); |
| 29 } | 35 } |
| 30 | 36 |
| 31 gfx::Size GetPreferredSize() const override { return preferred_size_; } | 37 gfx::Size GetPreferredSize() const override { return preferred_size_; } |
| 32 | 38 |
| 33 void Layout() override { | 39 void Layout() override { |
| 34 gfx::Size pref = GetPreferredSize(); | 40 gfx::Size pref = GetPreferredSize(); |
| 35 int width = pref.width(); | 41 int width = pref.width(); |
| 36 int height = pref.height(); | 42 int height = pref.height(); |
| 37 if (parent()) { | 43 if (parent()) { |
| 38 width = std::max(parent()->width(), width); | 44 width = std::max(parent()->width(), width); |
| 39 height = std::max(parent()->height(), height); | 45 height = std::max(parent()->height(), height); |
| 40 } | 46 } |
| 41 SetBounds(x(), y(), width, height); | 47 SetBounds(x(), y(), width, height); |
| 42 } | 48 } |
| 43 | 49 |
| 44 private: | 50 private: |
| 45 gfx::Size preferred_size_; | 51 gfx::Size preferred_size_; |
| 46 | 52 |
| 47 DISALLOW_COPY_AND_ASSIGN(CustomView); | 53 DISALLOW_COPY_AND_ASSIGN(CustomView); |
| 48 }; | 54 }; |
| 49 | 55 |
| 56 void CheckScrollbarVisibility(const ScrollView& scroll_view, |
| 57 ScrollBarOrientation orientation, |
| 58 bool should_be_visible) { |
| 59 const ScrollBar* scrollbar = orientation == HORIZONTAL |
| 60 ? scroll_view.horizontal_scroll_bar() |
| 61 : scroll_view.vertical_scroll_bar(); |
| 62 if (should_be_visible) { |
| 63 ASSERT_TRUE(scrollbar); |
| 64 EXPECT_TRUE(scrollbar->visible()); |
| 65 } else { |
| 66 EXPECT_TRUE(!scrollbar || !scrollbar->visible()); |
| 67 } |
| 68 } |
| 69 |
| 50 } // namespace | 70 } // namespace |
| 51 | 71 |
| 52 // Verifies the viewport is sized to fit the available space. | 72 // Verifies the viewport is sized to fit the available space. |
| 53 TEST(ScrollViewTest, ViewportSizedToFit) { | 73 TEST(ScrollViewTest, ViewportSizedToFit) { |
| 54 ScrollView scroll_view; | 74 ScrollView scroll_view; |
| 55 View* contents = new View; | 75 View* contents = new View; |
| 56 scroll_view.SetContents(contents); | 76 scroll_view.SetContents(contents); |
| 57 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); | 77 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); |
| 58 scroll_view.Layout(); | 78 scroll_view.Layout(); |
| 59 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); | 79 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); |
| 60 } | 80 } |
| 61 | 81 |
| 62 // Verifies the scrollbars are added as necessary. | 82 // Verifies the scrollbars are added as necessary. |
| 83 // If on Mac, test the non-overlay scrollbars. |
| 63 TEST(ScrollViewTest, ScrollBars) { | 84 TEST(ScrollViewTest, ScrollBars) { |
| 85 #if defined(OS_MACOSX) |
| 86 ui::test::ScopedPreferredScrollerStyle scroller_style_override(false); |
| 87 #endif |
| 88 |
| 64 ScrollView scroll_view; | 89 ScrollView scroll_view; |
| 65 View* contents = new View; | 90 View* contents = new View; |
| 66 scroll_view.SetContents(contents); | 91 scroll_view.SetContents(contents); |
| 67 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); | 92 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); |
| 68 | 93 |
| 69 // Size the contents such that vertical scrollbar is needed. | 94 // Size the contents such that vertical scrollbar is needed. |
| 70 contents->SetBounds(0, 0, 50, 400); | 95 contents->SetBounds(0, 0, 50, 400); |
| 71 scroll_view.Layout(); | 96 scroll_view.Layout(); |
| 72 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); | 97 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); |
| 73 EXPECT_EQ(100, contents->parent()->height()); | 98 EXPECT_EQ(100, contents->parent()->height()); |
| 99 CheckScrollbarVisibility(scroll_view, VERTICAL, true); |
| 100 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false); |
| 74 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || | 101 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || |
| 75 !scroll_view.horizontal_scroll_bar()->visible()); | 102 !scroll_view.horizontal_scroll_bar()->visible()); |
| 76 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); | 103 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); |
| 77 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); | 104 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); |
| 78 | 105 |
| 79 // Size the contents such that horizontal scrollbar is needed. | 106 // Size the contents such that horizontal scrollbar is needed. |
| 80 contents->SetBounds(0, 0, 400, 50); | 107 contents->SetBounds(0, 0, 400, 50); |
| 81 scroll_view.Layout(); | 108 scroll_view.Layout(); |
| 82 EXPECT_EQ(100, contents->parent()->width()); | 109 EXPECT_EQ(100, contents->parent()->width()); |
| 83 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), | 110 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), |
| 84 contents->parent()->height()); | 111 contents->parent()->height()); |
| 85 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); | 112 CheckScrollbarVisibility(scroll_view, VERTICAL, false); |
| 86 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); | 113 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); |
| 87 EXPECT_TRUE(!scroll_view.vertical_scroll_bar() || | |
| 88 !scroll_view.vertical_scroll_bar()->visible()); | |
| 89 | 114 |
| 90 // Both horizontal and vertical. | 115 // Both horizontal and vertical. |
| 91 contents->SetBounds(0, 0, 300, 400); | 116 contents->SetBounds(0, 0, 300, 400); |
| 92 scroll_view.Layout(); | 117 scroll_view.Layout(); |
| 93 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); | 118 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); |
| 94 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), | 119 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), |
| 95 contents->parent()->height()); | 120 contents->parent()->height()); |
| 96 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); | 121 CheckScrollbarVisibility(scroll_view, VERTICAL, true); |
| 97 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); | 122 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); |
| 98 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); | |
| 99 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); | |
| 100 | 123 |
| 101 // Add a border, test vertical scrollbar. | 124 // Add a border, test vertical scrollbar. |
| 102 const int kTopPadding = 1; | 125 const int kTopPadding = 1; |
| 103 const int kLeftPadding = 2; | 126 const int kLeftPadding = 2; |
| 104 const int kBottomPadding = 3; | 127 const int kBottomPadding = 3; |
| 105 const int kRightPadding = 4; | 128 const int kRightPadding = 4; |
| 106 scroll_view.SetBorder(Border::CreateEmptyBorder( | 129 scroll_view.SetBorder(Border::CreateEmptyBorder( |
| 107 kTopPadding, kLeftPadding, kBottomPadding, kRightPadding)); | 130 kTopPadding, kLeftPadding, kBottomPadding, kRightPadding)); |
| 108 contents->SetBounds(0, 0, 50, 400); | 131 contents->SetBounds(0, 0, 50, 400); |
| 109 scroll_view.Layout(); | 132 scroll_view.Layout(); |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 scroll_view.Layout(); | 465 scroll_view.Layout(); |
| 443 EXPECT_FALSE(corner_view->parent()); | 466 EXPECT_FALSE(corner_view->parent()); |
| 444 | 467 |
| 445 // Corner view should reappear when both scrollbars reappear. | 468 // Corner view should reappear when both scrollbars reappear. |
| 446 contents->SetBounds(0, 0, 200, 200); | 469 contents->SetBounds(0, 0, 200, 200); |
| 447 scroll_view.Layout(); | 470 scroll_view.Layout(); |
| 448 EXPECT_EQ(&scroll_view, corner_view->parent()); | 471 EXPECT_EQ(&scroll_view, corner_view->parent()); |
| 449 EXPECT_TRUE(corner_view->visible()); | 472 EXPECT_TRUE(corner_view->visible()); |
| 450 } | 473 } |
| 451 | 474 |
| 475 #if defined(OS_MACOSX) |
| 476 // Tests the overlay scrollbars on Mac. Ensure that they show up properly and |
| 477 // do not overlap each other. |
| 478 TEST(ScrollViewTest, CocoaOverlayScrollBars) { |
| 479 scoped_ptr<ui::test::ScopedPreferredScrollerStyle> scroller_style_override; |
| 480 scroller_style_override.reset( |
| 481 new ui::test::ScopedPreferredScrollerStyle(true)); |
| 482 ScrollView scroll_view; |
| 483 View* contents = new View; |
| 484 scroll_view.SetContents(contents); |
| 485 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); |
| 486 |
| 487 // Size the contents such that vertical scrollbar is needed. |
| 488 // Since it is overlaid, the ViewPort size should match the ScrollView. |
| 489 contents->SetBounds(0, 0, 50, 400); |
| 490 scroll_view.Layout(); |
| 491 EXPECT_EQ(100, contents->parent()->width()); |
| 492 EXPECT_EQ(100, contents->parent()->height()); |
| 493 EXPECT_EQ(0, scroll_view.GetScrollBarWidth()); |
| 494 CheckScrollbarVisibility(scroll_view, VERTICAL, true); |
| 495 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false); |
| 496 |
| 497 // Size the contents such that horizontal scrollbar is needed. |
| 498 contents->SetBounds(0, 0, 400, 50); |
| 499 scroll_view.Layout(); |
| 500 EXPECT_EQ(100, contents->parent()->width()); |
| 501 EXPECT_EQ(100, contents->parent()->height()); |
| 502 EXPECT_EQ(0, scroll_view.GetScrollBarHeight()); |
| 503 CheckScrollbarVisibility(scroll_view, VERTICAL, false); |
| 504 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); |
| 505 |
| 506 // Both horizontal and vertical scrollbars. |
| 507 contents->SetBounds(0, 0, 300, 400); |
| 508 scroll_view.Layout(); |
| 509 EXPECT_EQ(100, contents->parent()->width()); |
| 510 EXPECT_EQ(100, contents->parent()->height()); |
| 511 EXPECT_EQ(0, scroll_view.GetScrollBarWidth()); |
| 512 EXPECT_EQ(0, scroll_view.GetScrollBarHeight()); |
| 513 CheckScrollbarVisibility(scroll_view, VERTICAL, true); |
| 514 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); |
| 515 |
| 516 // Make sure the horizontal and vertical scrollbars don't overlap each other. |
| 517 gfx::Rect vert_bounds = scroll_view.vertical_scroll_bar()->bounds(); |
| 518 gfx::Rect horiz_bounds = scroll_view.horizontal_scroll_bar()->bounds(); |
| 519 EXPECT_EQ(vert_bounds.x(), horiz_bounds.right()); |
| 520 EXPECT_EQ(horiz_bounds.y(), vert_bounds.bottom()); |
| 521 |
| 522 // Switch to the non-overlay style and check that the ViewPort is now sized |
| 523 // to be smaller, and ScrollbarWidth and ScrollbarHeight are non-zero. |
| 524 scroller_style_override.reset( |
| 525 new ui::test::ScopedPreferredScrollerStyle(false)); |
| 526 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); |
| 527 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), |
| 528 contents->parent()->height()); |
| 529 EXPECT_NE(0, scroll_view.GetScrollBarWidth()); |
| 530 EXPECT_NE(0, scroll_view.GetScrollBarHeight()); |
| 531 } |
| 532 #endif |
| 533 |
| 452 } // namespace views | 534 } // namespace views |
| OLD | NEW |