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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 #include "ui/views/controls/scrollbar/native_scroll_bar.h" | |
7 #include "ui/views/controls/scrollbar/native_scroll_bar_views.h" | |
8 #include "ui/views/controls/scrollbar/scroll_bar.h" | 6 #include "ui/views/controls/scrollbar/scroll_bar.h" |
| 7 #include "ui/views/controls/scrollbar/scroll_bar_views.h" |
9 #include "ui/views/test/views_test_base.h" | 8 #include "ui/views/test/views_test_base.h" |
10 #include "ui/views/widget/widget.h" | 9 #include "ui/views/widget/widget.h" |
11 | 10 |
12 namespace { | 11 namespace { |
13 | 12 |
14 // The Scrollbar controller. This is the widget that should do the real | 13 // The Scrollbar controller. This is the widget that should do the real |
15 // scrolling of contents. | 14 // scrolling of contents. |
16 class TestScrollBarController : public views::ScrollBarController { | 15 class TestScrollBarController : public views::ScrollBarController { |
17 public: | 16 public: |
18 virtual ~TestScrollBarController() {} | 17 virtual ~TestScrollBarController() {} |
(...skipping 20 matching lines...) Expand all Loading... |
39 views::ScrollBar* last_source; | 38 views::ScrollBar* last_source; |
40 bool last_is_positive; | 39 bool last_is_positive; |
41 bool last_is_page; | 40 bool last_is_page; |
42 int last_position; | 41 int last_position; |
43 }; | 42 }; |
44 | 43 |
45 } // namespace | 44 } // namespace |
46 | 45 |
47 namespace views { | 46 namespace views { |
48 | 47 |
49 class NativeScrollBarTest : public ViewsTestBase { | 48 class ScrollBarViewsTest : public ViewsTestBase { |
50 public: | 49 public: |
51 NativeScrollBarTest() : widget_(NULL), scrollbar_(NULL) {} | 50 ScrollBarViewsTest() : widget_(nullptr), scrollbar_(nullptr) {} |
52 | 51 |
53 void SetUp() override { | 52 void SetUp() override { |
54 ViewsTestBase::SetUp(); | 53 ViewsTestBase::SetUp(); |
55 controller_.reset(new TestScrollBarController()); | 54 controller_.reset(new TestScrollBarController()); |
56 | 55 |
57 ASSERT_FALSE(scrollbar_); | |
58 native_scrollbar_ = new NativeScrollBar(true); | |
59 native_scrollbar_->SetBounds(0, 0, 100, 100); | |
60 native_scrollbar_->set_controller(controller_.get()); | |
61 | |
62 widget_ = new Widget; | 56 widget_ = new Widget; |
63 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 57 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
64 params.bounds = gfx::Rect(0, 0, 100, 100); | 58 params.bounds = gfx::Rect(0, 0, 100, 100); |
65 widget_->Init(params); | 59 widget_->Init(params); |
66 View* container = new View(); | 60 View* container = new View(); |
67 widget_->SetContentsView(container); | 61 widget_->SetContentsView(container); |
68 container->AddChildView(native_scrollbar_); | |
69 | 62 |
70 scrollbar_ = | 63 scrollbar_ = new ScrollBarViews(true); |
71 static_cast<NativeScrollBarViews*>(native_scrollbar_->native_wrapper_); | |
72 scrollbar_->SetBounds(0, 0, 100, 100); | 64 scrollbar_->SetBounds(0, 0, 100, 100); |
73 scrollbar_->Update(100, 1000, 0); | 65 scrollbar_->Update(100, 1000, 0); |
| 66 scrollbar_->set_controller(controller_.get()); |
| 67 container->AddChildView(scrollbar_); |
74 | 68 |
75 track_size_ = scrollbar_->GetTrackBounds().width(); | 69 track_size_ = scrollbar_->GetTrackBounds().width(); |
76 } | 70 } |
77 | 71 |
78 void TearDown() override { | 72 void TearDown() override { |
79 widget_->Close(); | 73 widget_->Close(); |
80 ViewsTestBase::TearDown(); | 74 ViewsTestBase::TearDown(); |
81 } | 75 } |
82 | 76 |
83 protected: | 77 protected: |
84 Widget* widget_; | 78 Widget* widget_; |
85 | 79 |
86 // This is the native scrollbar the Views one wraps around. | |
87 NativeScrollBar* native_scrollbar_; | |
88 | |
89 // This is the Views scrollbar. | 80 // This is the Views scrollbar. |
90 BaseScrollBar* scrollbar_; | 81 BaseScrollBar* scrollbar_; |
91 | 82 |
92 // Keep track of the size of the track. This is how we can tell when we | 83 // Keep track of the size of the track. This is how we can tell when we |
93 // scroll to the middle. | 84 // scroll to the middle. |
94 int track_size_; | 85 int track_size_; |
95 | 86 |
96 std::unique_ptr<TestScrollBarController> controller_; | 87 std::unique_ptr<TestScrollBarController> controller_; |
97 }; | 88 }; |
98 | 89 |
99 // TODO(dnicoara) Can't run the test on Windows since the scrollbar |Part| | 90 // TODO(dnicoara) Can't run the test on Windows since the scrollbar |Part| |
100 // isn't handled in NativeTheme. | 91 // isn't handled in NativeTheme. |
101 #if defined(OS_WIN) | 92 #if defined(OS_WIN) |
102 #define MAYBE_Scrolling DISABLED_Scrolling | 93 #define MAYBE_Scrolling DISABLED_Scrolling |
103 #define MAYBE_ScrollBarFitsToBottom DISABLED_ScrollBarFitsToBottom | 94 #define MAYBE_ScrollBarFitsToBottom DISABLED_ScrollBarFitsToBottom |
104 #else | 95 #else |
105 #define MAYBE_Scrolling Scrolling | 96 #define MAYBE_Scrolling Scrolling |
106 #define MAYBE_ScrollBarFitsToBottom ScrollBarFitsToBottom | 97 #define MAYBE_ScrollBarFitsToBottom ScrollBarFitsToBottom |
107 #endif | 98 #endif |
108 | 99 |
109 TEST_F(NativeScrollBarTest, MAYBE_Scrolling) { | 100 TEST_F(ScrollBarViewsTest, MAYBE_Scrolling) { |
110 EXPECT_EQ(0, scrollbar_->GetPosition()); | 101 EXPECT_EQ(0, scrollbar_->GetPosition()); |
111 EXPECT_EQ(900, scrollbar_->GetMaxPosition()); | 102 EXPECT_EQ(900, scrollbar_->GetMaxPosition()); |
112 EXPECT_EQ(0, scrollbar_->GetMinPosition()); | 103 EXPECT_EQ(0, scrollbar_->GetMinPosition()); |
113 | 104 |
114 // Scroll to middle. | 105 // Scroll to middle. |
115 scrollbar_->ScrollToThumbPosition(track_size_ / 2, true); | 106 scrollbar_->ScrollToThumbPosition(track_size_ / 2, true); |
116 EXPECT_EQ(450, controller_->last_position); | 107 EXPECT_EQ(450, controller_->last_position); |
117 EXPECT_EQ(native_scrollbar_, controller_->last_source); | 108 EXPECT_EQ(scrollbar_, controller_->last_source); |
118 | 109 |
119 // Scroll to the end. | 110 // Scroll to the end. |
120 scrollbar_->ScrollToThumbPosition(track_size_, true); | 111 scrollbar_->ScrollToThumbPosition(track_size_, true); |
121 EXPECT_EQ(900, controller_->last_position); | 112 EXPECT_EQ(900, controller_->last_position); |
122 | 113 |
123 // Overscroll. Last position should be the maximum position. | 114 // Overscroll. Last position should be the maximum position. |
124 scrollbar_->ScrollToThumbPosition(track_size_ + 100, true); | 115 scrollbar_->ScrollToThumbPosition(track_size_ + 100, true); |
125 EXPECT_EQ(900, controller_->last_position); | 116 EXPECT_EQ(900, controller_->last_position); |
126 | 117 |
127 // Underscroll. Last position should be the minimum position. | 118 // Underscroll. Last position should be the minimum position. |
128 scrollbar_->ScrollToThumbPosition(-10, false); | 119 scrollbar_->ScrollToThumbPosition(-10, false); |
129 EXPECT_EQ(0, controller_->last_position); | 120 EXPECT_EQ(0, controller_->last_position); |
130 | 121 |
131 // Test the different fixed scrolling amounts. Generally used by buttons, | 122 // Test the different fixed scrolling amounts. Generally used by buttons, |
132 // or click on track. | 123 // or click on track. |
133 scrollbar_->ScrollToThumbPosition(0, false); | 124 scrollbar_->ScrollToThumbPosition(0, false); |
134 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_LINE); | 125 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_LINE); |
135 EXPECT_EQ(10, controller_->last_position); | 126 EXPECT_EQ(10, controller_->last_position); |
136 | 127 |
137 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_PREV_LINE); | 128 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_PREV_LINE); |
138 EXPECT_EQ(0, controller_->last_position); | 129 EXPECT_EQ(0, controller_->last_position); |
139 | 130 |
140 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_PAGE); | 131 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_PAGE); |
141 EXPECT_EQ(20, controller_->last_position); | 132 EXPECT_EQ(20, controller_->last_position); |
142 | 133 |
143 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_PREV_PAGE); | 134 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_PREV_PAGE); |
144 EXPECT_EQ(0, controller_->last_position); | 135 EXPECT_EQ(0, controller_->last_position); |
145 } | 136 } |
146 | 137 |
147 TEST_F(NativeScrollBarTest, MAYBE_ScrollBarFitsToBottom) { | 138 TEST_F(ScrollBarViewsTest, MAYBE_ScrollBarFitsToBottom) { |
148 scrollbar_->Update(100, 1999, 0); | 139 scrollbar_->Update(100, 1999, 0); |
149 EXPECT_EQ(0, scrollbar_->GetPosition()); | 140 EXPECT_EQ(0, scrollbar_->GetPosition()); |
150 EXPECT_EQ(1899, scrollbar_->GetMaxPosition()); | 141 EXPECT_EQ(1899, scrollbar_->GetMaxPosition()); |
151 EXPECT_EQ(0, scrollbar_->GetMinPosition()); | 142 EXPECT_EQ(0, scrollbar_->GetMinPosition()); |
152 | 143 |
153 // Scroll to the midpoint of the document. | 144 // Scroll to the midpoint of the document. |
154 scrollbar_->Update(100, 1999, 950); | 145 scrollbar_->Update(100, 1999, 950); |
155 EXPECT_EQ((scrollbar_->GetTrackBounds().width() - | 146 EXPECT_EQ((scrollbar_->GetTrackBounds().width() - |
156 scrollbar_->GetThumbSizeForTest()) / | 147 scrollbar_->GetThumbSizeForTest()) / |
157 2, | 148 2, |
158 scrollbar_->GetPosition()); | 149 scrollbar_->GetPosition()); |
159 | 150 |
160 // Scroll to the end of the document. | 151 // Scroll to the end of the document. |
161 scrollbar_->Update(100, 1999, 1899); | 152 scrollbar_->Update(100, 1999, 1899); |
162 EXPECT_EQ( | 153 EXPECT_EQ( |
163 scrollbar_->GetTrackBounds().width() - scrollbar_->GetThumbSizeForTest(), | 154 scrollbar_->GetTrackBounds().width() - scrollbar_->GetThumbSizeForTest(), |
164 scrollbar_->GetPosition()); | 155 scrollbar_->GetPosition()); |
165 } | 156 } |
166 | 157 |
167 TEST_F(NativeScrollBarTest, ScrollToEndAfterShrinkAndExpand) { | 158 TEST_F(ScrollBarViewsTest, ScrollToEndAfterShrinkAndExpand) { |
168 // Scroll to the end of the content. | 159 // Scroll to the end of the content. |
169 scrollbar_->Update(100, 1001, 0); | 160 scrollbar_->Update(100, 1001, 0); |
170 EXPECT_TRUE(scrollbar_->ScrollByContentsOffset(-1)); | 161 EXPECT_TRUE(scrollbar_->ScrollByContentsOffset(-1)); |
171 // Shrink and then re-expand the content. | 162 // Shrink and then re-expand the content. |
172 scrollbar_->Update(100, 1000, 0); | 163 scrollbar_->Update(100, 1000, 0); |
173 scrollbar_->Update(100, 1001, 0); | 164 scrollbar_->Update(100, 1001, 0); |
174 // Ensure the scrollbar allows scrolling to the end. | 165 // Ensure the scrollbar allows scrolling to the end. |
175 EXPECT_TRUE(scrollbar_->ScrollByContentsOffset(-1)); | 166 EXPECT_TRUE(scrollbar_->ScrollByContentsOffset(-1)); |
176 } | 167 } |
177 | 168 |
178 TEST_F(NativeScrollBarTest, ThumbFullLengthOfTrack) { | 169 TEST_F(ScrollBarViewsTest, ThumbFullLengthOfTrack) { |
179 // Shrink content so that it fits within the viewport. | 170 // Shrink content so that it fits within the viewport. |
180 scrollbar_->Update(100, 10, 0); | 171 scrollbar_->Update(100, 10, 0); |
181 EXPECT_EQ(scrollbar_->GetTrackBounds().width(), | 172 EXPECT_EQ(scrollbar_->GetTrackBounds().width(), |
182 scrollbar_->GetThumbSizeForTest()); | 173 scrollbar_->GetThumbSizeForTest()); |
183 // Emulate a click on the full size scroll bar. | 174 // Emulate a click on the full size scroll bar. |
184 scrollbar_->ScrollToThumbPosition(0, false); | 175 scrollbar_->ScrollToThumbPosition(0, false); |
185 EXPECT_EQ(0, scrollbar_->GetPosition()); | 176 EXPECT_EQ(0, scrollbar_->GetPosition()); |
186 // Emulate a key down. | 177 // Emulate a key down. |
187 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_LINE); | 178 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_LINE); |
188 EXPECT_EQ(0, scrollbar_->GetPosition()); | 179 EXPECT_EQ(0, scrollbar_->GetPosition()); |
189 | 180 |
190 // Expand content so that it fits *exactly* within the viewport. | 181 // Expand content so that it fits *exactly* within the viewport. |
191 scrollbar_->Update(100, 100, 0); | 182 scrollbar_->Update(100, 100, 0); |
192 EXPECT_EQ(scrollbar_->GetTrackBounds().width(), | 183 EXPECT_EQ(scrollbar_->GetTrackBounds().width(), |
193 scrollbar_->GetThumbSizeForTest()); | 184 scrollbar_->GetThumbSizeForTest()); |
194 // Emulate a click on the full size scroll bar. | 185 // Emulate a click on the full size scroll bar. |
195 scrollbar_->ScrollToThumbPosition(0, false); | 186 scrollbar_->ScrollToThumbPosition(0, false); |
196 EXPECT_EQ(0, scrollbar_->GetPosition()); | 187 EXPECT_EQ(0, scrollbar_->GetPosition()); |
197 // Emulate a key down. | 188 // Emulate a key down. |
198 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_LINE); | 189 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_LINE); |
199 EXPECT_EQ(0, scrollbar_->GetPosition()); | 190 EXPECT_EQ(0, scrollbar_->GetPosition()); |
200 } | 191 } |
201 | 192 |
202 } // namespace views | 193 } // namespace views |
OLD | NEW |