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
3 // found in the LICENSE file.
5 #include "ui/views/controls/scrollbar/native_scroll_bar.h"
6 #include "ui/views/controls/scrollbar/native_scroll_bar_views.h"
7 #include "ui/views/controls/scrollbar/scroll_bar.h"
8 #include "ui/views/test/views_test_base.h"
9 #include "ui/views/widget/widget.h"
13 // The Scrollbar controller. This is the widget that should do the real
14 // scrolling of contents.
15 class TestScrollBarController
: public views::ScrollBarController
{
17 virtual ~TestScrollBarController() {}
19 void ScrollToPosition(views::ScrollBar
* source
, int position
) override
{
21 last_position
= position
;
24 int GetScrollIncrement(views::ScrollBar
* source
,
26 bool is_positive
) override
{
28 last_is_page
= is_page
;
29 last_is_positive
= is_positive
;
36 // We save the last values in order to assert the corectness of the scroll
38 views::ScrollBar
* last_source
;
39 bool last_is_positive
;
48 class NativeScrollBarTest
: public ViewsTestBase
{
50 NativeScrollBarTest() : widget_(NULL
), scrollbar_(NULL
) {}
52 void SetUp() override
{
53 ViewsTestBase::SetUp();
54 controller_
.reset(new TestScrollBarController());
56 ASSERT_FALSE(scrollbar_
);
57 native_scrollbar_
= new NativeScrollBar(true);
58 native_scrollbar_
->SetBounds(0, 0, 100, 100);
59 native_scrollbar_
->set_controller(controller_
.get());
62 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
63 params
.bounds
= gfx::Rect(0, 0, 100, 100);
64 widget_
->Init(params
);
65 View
* container
= new View();
66 widget_
->SetContentsView(container
);
67 container
->AddChildView(native_scrollbar_
);
70 static_cast<NativeScrollBarViews
*>(native_scrollbar_
->native_wrapper_
);
71 scrollbar_
->SetBounds(0, 0, 100, 100);
72 scrollbar_
->Update(100, 200, 0);
74 track_size_
= scrollbar_
->GetTrackBounds().width();
77 void TearDown() override
{
79 ViewsTestBase::TearDown();
85 // This is the native scrollbar the Views one wraps around.
86 NativeScrollBar
* native_scrollbar_
;
88 // This is the Views scrollbar.
89 BaseScrollBar
* scrollbar_
;
91 // Keep track of the size of the track. This is how we can tell when we
92 // scroll to the middle.
95 scoped_ptr
<TestScrollBarController
> controller_
;
98 // TODO(dnicoara) Can't run the test on Windows since the scrollbar |Part|
99 // isn't handled in NativeTheme.
101 #define MAYBE_Scrolling DISABLED_Scrolling
102 #define MAYBE_ScrollBarFitsToBottom DISABLED_ScrollBarFitsToBottom
104 #define MAYBE_Scrolling Scrolling
105 #define MAYBE_ScrollBarFitsToBottom ScrollBarFitsToBottom
108 TEST_F(NativeScrollBarTest
, MAYBE_Scrolling
) {
109 EXPECT_EQ(scrollbar_
->GetPosition(), 0);
110 EXPECT_EQ(scrollbar_
->GetMaxPosition(), 100);
111 EXPECT_EQ(scrollbar_
->GetMinPosition(), 0);
114 scrollbar_
->ScrollToThumbPosition(track_size_
/ 4, false);
115 EXPECT_EQ(controller_
->last_position
, 50);
116 EXPECT_EQ(controller_
->last_source
, native_scrollbar_
);
118 // Scroll to the end.
119 scrollbar_
->ScrollToThumbPosition(track_size_
/ 2, false);
120 EXPECT_EQ(controller_
->last_position
, 100);
122 // Overscroll. Last position should be the maximum position.
123 scrollbar_
->ScrollToThumbPosition(track_size_
, false);
124 EXPECT_EQ(controller_
->last_position
, 100);
126 // Underscroll. Last position should be the minimum position.
127 scrollbar_
->ScrollToThumbPosition(-10, false);
128 EXPECT_EQ(controller_
->last_position
, 0);
130 // Test the different fixed scrolling amounts. Generally used by buttons,
131 // or click on track.
132 scrollbar_
->ScrollToThumbPosition(0, false);
133 scrollbar_
->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_LINE
);
134 EXPECT_EQ(controller_
->last_position
, 10);
136 scrollbar_
->ScrollByAmount(BaseScrollBar::SCROLL_PREV_LINE
);
137 EXPECT_EQ(controller_
->last_position
, 0);
139 scrollbar_
->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_PAGE
);
140 EXPECT_EQ(controller_
->last_position
, 20);
142 scrollbar_
->ScrollByAmount(BaseScrollBar::SCROLL_PREV_PAGE
);
143 EXPECT_EQ(controller_
->last_position
, 0);
146 TEST_F(NativeScrollBarTest
, MAYBE_ScrollBarFitsToBottom
) {
147 scrollbar_
->Update(100, 199, 0);
148 EXPECT_EQ(0, scrollbar_
->GetPosition());
149 EXPECT_EQ(99, scrollbar_
->GetMaxPosition());
150 EXPECT_EQ(0, scrollbar_
->GetMinPosition());
152 scrollbar_
->Update(100, 199, 99);
154 scrollbar_
->GetTrackBounds().width() - scrollbar_
->GetThumbSizeForTest(),
155 scrollbar_
->GetPosition());
158 TEST_F(NativeScrollBarTest
, ScrollToEndAfterShrinkAndExpand
) {
159 // Scroll to the end of the content.
160 scrollbar_
->Update(100, 101, 0);
161 EXPECT_TRUE(scrollbar_
->ScrollByContentsOffset(-1));
162 // Shrink and then re-exapnd the content.
163 scrollbar_
->Update(100, 100, 0);
164 scrollbar_
->Update(100, 101, 0);
165 // Ensure the scrollbar allows scrolling to the end.
166 EXPECT_TRUE(scrollbar_
->ScrollByContentsOffset(-1));