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

Side by Side Diff: ui/views/controls/scroll_view_unittest.cc

Issue 1680613002: Adding momentum/overscroll to views:: ScrollViews Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Tableview layout. aaaand I think we are done Created 4 years, 4 months 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
« no previous file with comments | « ui/views/controls/scroll_view.cc ('k') | ui/views/controls/scrollbar/cocoa_scroll_bar.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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) 13 #if defined(OS_MACOSX)
14 #include "ui/base/test/scoped_preferred_scroller_style_mac.h" 14 #include "ui/base/test/scoped_preferred_scroller_style_mac.h"
15 #endif 15 #endif
16 16
17 namespace views { 17 namespace views {
18 namespace test {
19
20 class ScrollViewTestApi {
21 public:
22 explicit ScrollViewTestApi(ScrollView* scroll_view)
23 : scroll_view_(scroll_view) {}
24
25 gfx::Point IntegralViewOffset() {
26 return gfx::Point() - gfx::ScrollOffsetToFlooredVector2d(CurrentOffset());
27 }
28
29 gfx::ScrollOffset CurrentOffset() { return scroll_view_->CurrentOffset(); }
30 View* corner_view() { return scroll_view_->corner_view_; }
31 View* contents_viewport() { return scroll_view_->contents_viewport_; }
32
33 private:
34 ScrollView* scroll_view_;
35 };
36
37 } // namespace test
18 38
19 namespace { 39 namespace {
20 40
21 const int kWidth = 100; 41 const int kWidth = 100;
22 const int kMinHeight = 50; 42 const int kMinHeight = 50;
23 const int kMaxHeight = 100; 43 const int kMaxHeight = 100;
24 44
25 enum ScrollBarOrientation { HORIZONTAL, VERTICAL }; 45 enum ScrollBarOrientation { HORIZONTAL, VERTICAL };
26 46
27 // View implementation that allows setting the preferred size. 47 // View implementation that allows setting the preferred size.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 if (should_be_visible) { 82 if (should_be_visible) {
63 ASSERT_TRUE(scrollbar); 83 ASSERT_TRUE(scrollbar);
64 EXPECT_TRUE(scrollbar->visible()); 84 EXPECT_TRUE(scrollbar->visible());
65 } else { 85 } else {
66 EXPECT_TRUE(!scrollbar || !scrollbar->visible()); 86 EXPECT_TRUE(!scrollbar || !scrollbar->visible());
67 } 87 }
68 } 88 }
69 89
70 } // namespace 90 } // namespace
71 91
92 using test::ScrollViewTestApi;
93
72 // Verifies the viewport is sized to fit the available space. 94 // Verifies the viewport is sized to fit the available space.
73 TEST(ScrollViewTest, ViewportSizedToFit) { 95 TEST(ScrollViewTest, ViewportSizedToFit) {
74 ScrollView scroll_view; 96 ScrollView scroll_view;
97 ScrollViewTestApi test_api(&scroll_view);
75 View* contents = new View; 98 View* contents = new View;
76 scroll_view.SetContents(contents); 99 scroll_view.SetContents(contents);
77 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 100 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
78 scroll_view.Layout(); 101 scroll_view.Layout();
79 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); 102 EXPECT_EQ("0,0 100x100", test_api.contents_viewport()->bounds().ToString());
80 } 103 }
81 104
82 // Verifies the scrollbars are added as necessary. 105 // Verifies the scrollbars are added as necessary.
83 // If on Mac, test the non-overlay scrollbars. 106 // If on Mac, test the non-overlay scrollbars.
84 TEST(ScrollViewTest, ScrollBars) { 107 TEST(ScrollViewTest, ScrollBars) {
85 #if defined(OS_MACOSX) 108 #if defined(OS_MACOSX)
86 ui::test::ScopedPreferredScrollerStyle scroller_style_override(false); 109 ui::test::ScopedPreferredScrollerStyle scroller_style_override(false);
87 #endif 110 #endif
88 111
89 ScrollView scroll_view; 112 ScrollView scroll_view;
113 ScrollViewTestApi test_api(&scroll_view);
90 View* contents = new View; 114 View* contents = new View;
91 scroll_view.SetContents(contents); 115 scroll_view.SetContents(contents);
92 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 116 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
93 117
94 // Size the contents such that vertical scrollbar is needed. 118 // Size the contents such that vertical scrollbar is needed.
95 contents->SetBounds(0, 0, 50, 400); 119 contents->SetBounds(0, 0, 50, 400);
96 scroll_view.Layout(); 120 scroll_view.Layout();
97 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); 121 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(),
98 EXPECT_EQ(100, contents->parent()->height()); 122 test_api.contents_viewport()->width());
123 EXPECT_EQ(100, test_api.contents_viewport()->height());
99 CheckScrollbarVisibility(scroll_view, VERTICAL, true); 124 CheckScrollbarVisibility(scroll_view, VERTICAL, true);
100 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false); 125 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false);
101 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || 126 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() ||
102 !scroll_view.horizontal_scroll_bar()->visible()); 127 !scroll_view.horizontal_scroll_bar()->visible());
103 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); 128 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL);
104 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); 129 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible());
105 130
106 // Size the contents such that horizontal scrollbar is needed. 131 // Size the contents such that horizontal scrollbar is needed.
107 contents->SetBounds(0, 0, 400, 50); 132 contents->SetBounds(0, 0, 400, 50);
108 scroll_view.Layout(); 133 scroll_view.Layout();
109 EXPECT_EQ(100, contents->parent()->width()); 134 EXPECT_EQ(100, test_api.contents_viewport()->width());
110 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), 135 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(),
111 contents->parent()->height()); 136 test_api.contents_viewport()->height());
112 CheckScrollbarVisibility(scroll_view, VERTICAL, false); 137 CheckScrollbarVisibility(scroll_view, VERTICAL, false);
113 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); 138 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true);
114 139
115 // Both horizontal and vertical. 140 // Both horizontal and vertical.
116 contents->SetBounds(0, 0, 300, 400); 141 contents->SetBounds(0, 0, 300, 400);
117 scroll_view.Layout(); 142 scroll_view.Layout();
118 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); 143 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(),
144 test_api.contents_viewport()->width());
119 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), 145 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(),
120 contents->parent()->height()); 146 test_api.contents_viewport()->height());
121 CheckScrollbarVisibility(scroll_view, VERTICAL, true); 147 CheckScrollbarVisibility(scroll_view, VERTICAL, true);
122 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); 148 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true);
123 149
124 // Add a border, test vertical scrollbar. 150 // Add a border, test vertical scrollbar.
125 const int kTopPadding = 1; 151 const int kTopPadding = 1;
126 const int kLeftPadding = 2; 152 const int kLeftPadding = 2;
127 const int kBottomPadding = 3; 153 const int kBottomPadding = 3;
128 const int kRightPadding = 4; 154 const int kRightPadding = 4;
129 scroll_view.SetBorder(Border::CreateEmptyBorder( 155 scroll_view.SetBorder(Border::CreateEmptyBorder(
130 kTopPadding, kLeftPadding, kBottomPadding, kRightPadding)); 156 kTopPadding, kLeftPadding, kBottomPadding, kRightPadding));
131 contents->SetBounds(0, 0, 50, 400); 157 contents->SetBounds(0, 0, 50, 400);
132 scroll_view.Layout(); 158 scroll_view.Layout();
133 EXPECT_EQ( 159 EXPECT_EQ(
134 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding, 160 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding,
135 contents->parent()->width()); 161 test_api.contents_viewport()->width());
136 EXPECT_EQ(100 - kTopPadding - kBottomPadding, contents->parent()->height()); 162 EXPECT_EQ(100 - kTopPadding - kBottomPadding,
163 test_api.contents_viewport()->height());
137 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || 164 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() ||
138 !scroll_view.horizontal_scroll_bar()->visible()); 165 !scroll_view.horizontal_scroll_bar()->visible());
139 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); 166 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL);
140 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); 167 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible());
141 gfx::Rect bounds = scroll_view.vertical_scroll_bar()->bounds(); 168 gfx::Rect bounds = scroll_view.vertical_scroll_bar()->bounds();
142 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth() - kRightPadding, bounds.x()); 169 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth() - kRightPadding, bounds.x());
143 EXPECT_EQ(100 - kRightPadding, bounds.right()); 170 EXPECT_EQ(100 - kRightPadding, bounds.right());
144 EXPECT_EQ(kTopPadding, bounds.y()); 171 EXPECT_EQ(kTopPadding, bounds.y());
145 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); 172 EXPECT_EQ(100 - kBottomPadding, bounds.bottom());
146 173
147 // Horizontal with border. 174 // Horizontal with border.
148 contents->SetBounds(0, 0, 400, 50); 175 contents->SetBounds(0, 0, 400, 50);
149 scroll_view.Layout(); 176 scroll_view.Layout();
150 EXPECT_EQ(100 - kLeftPadding - kRightPadding, contents->parent()->width()); 177 EXPECT_EQ(100 - kLeftPadding - kRightPadding,
178 test_api.contents_viewport()->width());
151 EXPECT_EQ( 179 EXPECT_EQ(
152 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding, 180 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding,
153 contents->parent()->height()); 181 test_api.contents_viewport()->height());
154 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); 182 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL);
155 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); 183 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible());
156 EXPECT_TRUE(!scroll_view.vertical_scroll_bar() || 184 EXPECT_TRUE(!scroll_view.vertical_scroll_bar() ||
157 !scroll_view.vertical_scroll_bar()->visible()); 185 !scroll_view.vertical_scroll_bar()->visible());
158 bounds = scroll_view.horizontal_scroll_bar()->bounds(); 186 bounds = scroll_view.horizontal_scroll_bar()->bounds();
159 EXPECT_EQ(kLeftPadding, bounds.x()); 187 EXPECT_EQ(kLeftPadding, bounds.x());
160 EXPECT_EQ(100 - kRightPadding, bounds.right()); 188 EXPECT_EQ(100 - kRightPadding, bounds.right());
161 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), 189 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(),
162 bounds.y()); 190 bounds.y());
163 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); 191 EXPECT_EQ(100 - kBottomPadding, bounds.bottom());
164 192
165 // Both horizontal and vertical with border. 193 // Both horizontal and vertical with border.
166 contents->SetBounds(0, 0, 300, 400); 194 contents->SetBounds(0, 0, 300, 400);
167 scroll_view.Layout(); 195 scroll_view.Layout();
168 EXPECT_EQ( 196 EXPECT_EQ(
169 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding, 197 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding,
170 contents->parent()->width()); 198 test_api.contents_viewport()->width());
171 EXPECT_EQ( 199 EXPECT_EQ(
172 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding, 200 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding,
173 contents->parent()->height()); 201 test_api.contents_viewport()->height());
174 bounds = scroll_view.horizontal_scroll_bar()->bounds(); 202 bounds = scroll_view.horizontal_scroll_bar()->bounds();
175 // Check horiz. 203 // Check horiz.
176 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); 204 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL);
177 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); 205 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible());
178 bounds = scroll_view.horizontal_scroll_bar()->bounds(); 206 bounds = scroll_view.horizontal_scroll_bar()->bounds();
179 EXPECT_EQ(kLeftPadding, bounds.x()); 207 EXPECT_EQ(kLeftPadding, bounds.x());
180 EXPECT_EQ(100 - kRightPadding - scroll_view.GetScrollBarWidth(), 208 EXPECT_EQ(100 - kRightPadding - scroll_view.GetScrollBarWidth(),
181 bounds.right()); 209 bounds.right());
182 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), 210 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(),
183 bounds.y()); 211 bounds.y());
184 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); 212 EXPECT_EQ(100 - kBottomPadding, bounds.bottom());
185 // Check vert. 213 // Check vert.
186 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); 214 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL);
187 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); 215 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible());
188 bounds = scroll_view.vertical_scroll_bar()->bounds(); 216 bounds = scroll_view.vertical_scroll_bar()->bounds();
189 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth() - kRightPadding, bounds.x()); 217 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth() - kRightPadding, bounds.x());
190 EXPECT_EQ(100 - kRightPadding, bounds.right()); 218 EXPECT_EQ(100 - kRightPadding, bounds.right());
191 EXPECT_EQ(kTopPadding, bounds.y()); 219 EXPECT_EQ(kTopPadding, bounds.y());
192 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), 220 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(),
193 bounds.bottom()); 221 bounds.bottom());
194 } 222 }
195 223
196 // Assertions around adding a header. 224 // Assertions around adding a header.
197 TEST(ScrollViewTest, Header) { 225 TEST(ScrollViewTest, Header) {
198 ScrollView scroll_view; 226 ScrollView scroll_view;
227 ScrollViewTestApi test_api(&scroll_view);
199 View* contents = new View; 228 View* contents = new View;
200 CustomView* header = new CustomView; 229 CustomView* header = new CustomView;
201 scroll_view.SetHeader(header); 230 scroll_view.SetHeader(header);
202 View* header_parent = header->parent(); 231 View* header_parent = header->parent();
203 scroll_view.SetContents(contents); 232 scroll_view.SetContents(contents);
204 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 233 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
205 scroll_view.Layout(); 234 scroll_view.Layout();
206 // |header|s preferred size is empty, which should result in all space going 235 // |header|s preferred size is empty, which should result in all space going
207 // to contents. 236 // to contents.
208 EXPECT_EQ("0,0 100x0", header->parent()->bounds().ToString()); 237 EXPECT_EQ("0,0 100x0", header->parent()->bounds().ToString());
209 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); 238 EXPECT_EQ("0,0 100x100", test_api.contents_viewport()->bounds().ToString());
210 239
211 // Get the header a height of 20. 240 // Get the header a height of 20.
212 header->SetPreferredSize(gfx::Size(10, 20)); 241 header->SetPreferredSize(gfx::Size(10, 20));
213 EXPECT_EQ("0,0 100x20", header->parent()->bounds().ToString()); 242 EXPECT_EQ("0,0 100x20", header->parent()->bounds().ToString());
214 EXPECT_EQ("0,20 100x80", contents->parent()->bounds().ToString()); 243 EXPECT_EQ("0,20 100x80", test_api.contents_viewport()->bounds().ToString());
215 244
216 // Remove the header. 245 // Remove the header.
217 scroll_view.SetHeader(NULL); 246 scroll_view.SetHeader(NULL);
218 // SetHeader(NULL) deletes header. 247 // SetHeader(NULL) deletes header.
219 header = NULL; 248 header = NULL;
220 EXPECT_EQ("0,0 100x0", header_parent->bounds().ToString()); 249 EXPECT_EQ("0,0 100x0", header_parent->bounds().ToString());
221 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); 250 EXPECT_EQ("0,0 100x100", test_api.contents_viewport()->bounds().ToString());
222 } 251 }
223 252
224 // Verifies the scrollbars are added as necessary when a header is present. 253 // Verifies the scrollbars are added as necessary when a header is present.
225 TEST(ScrollViewTest, ScrollBarsWithHeader) { 254 TEST(ScrollViewTest, ScrollBarsWithHeader) {
226 ScrollView scroll_view; 255 ScrollView scroll_view;
256 ScrollViewTestApi test_api(&scroll_view);
227 View* contents = new View; 257 View* contents = new View;
228 scroll_view.SetContents(contents); 258 scroll_view.SetContents(contents);
229 CustomView* header = new CustomView; 259 CustomView* header = new CustomView;
230 scroll_view.SetHeader(header); 260 scroll_view.SetHeader(header);
231 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 261 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
232 262
233 header->SetPreferredSize(gfx::Size(10, 20)); 263 header->SetPreferredSize(gfx::Size(10, 20));
234 264
235 // Size the contents such that vertical scrollbar is needed. 265 // Size the contents such that vertical scrollbar is needed.
236 contents->SetBounds(0, 0, 50, 400); 266 contents->SetBounds(0, 0, 50, 400);
237 scroll_view.Layout(); 267 scroll_view.Layout();
238 EXPECT_EQ(0, contents->parent()->x()); 268 EXPECT_EQ(0, test_api.contents_viewport()->x());
239 EXPECT_EQ(20, contents->parent()->y()); 269 EXPECT_EQ(20, test_api.contents_viewport()->y());
240 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); 270 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(),
241 EXPECT_EQ(80, contents->parent()->height()); 271 test_api.contents_viewport()->width());
272 EXPECT_EQ(80, test_api.contents_viewport()->height());
242 EXPECT_EQ(0, header->parent()->x()); 273 EXPECT_EQ(0, header->parent()->x());
243 EXPECT_EQ(0, header->parent()->y()); 274 EXPECT_EQ(0, header->parent()->y());
244 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width()); 275 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width());
245 EXPECT_EQ(20, header->parent()->height()); 276 EXPECT_EQ(20, header->parent()->height());
246 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || 277 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() ||
247 !scroll_view.horizontal_scroll_bar()->visible()); 278 !scroll_view.horizontal_scroll_bar()->visible());
248 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); 279 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL);
249 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); 280 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible());
250 // Make sure the vertical scrollbar overlaps the header. 281 // Make sure the vertical scrollbar overlaps the header.
251 EXPECT_EQ(header->y(), scroll_view.vertical_scroll_bar()->y()); 282 EXPECT_EQ(header->y(), scroll_view.vertical_scroll_bar()->y());
252 EXPECT_EQ(header->y(), contents->y()); 283 EXPECT_EQ(header->y(), contents->y());
253 284
254 // Size the contents such that horizontal scrollbar is needed. 285 // Size the contents such that horizontal scrollbar is needed.
255 contents->SetBounds(0, 0, 400, 50); 286 contents->SetBounds(0, 0, 400, 50);
256 scroll_view.Layout(); 287 scroll_view.Layout();
257 EXPECT_EQ(0, contents->parent()->x()); 288 EXPECT_EQ(0, test_api.contents_viewport()->x());
258 EXPECT_EQ(20, contents->parent()->y()); 289 EXPECT_EQ(20, test_api.contents_viewport()->y());
259 EXPECT_EQ(100, contents->parent()->width()); 290 EXPECT_EQ(100, test_api.contents_viewport()->width());
260 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20, 291 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20,
261 contents->parent()->height()); 292 test_api.contents_viewport()->height());
262 EXPECT_EQ(0, header->parent()->x()); 293 EXPECT_EQ(0, header->parent()->x());
263 EXPECT_EQ(0, header->parent()->y()); 294 EXPECT_EQ(0, header->parent()->y());
264 EXPECT_EQ(100, header->parent()->width()); 295 EXPECT_EQ(100, header->parent()->width());
265 EXPECT_EQ(20, header->parent()->height()); 296 EXPECT_EQ(20, header->parent()->height());
266 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); 297 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL);
267 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); 298 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible());
268 EXPECT_TRUE(!scroll_view.vertical_scroll_bar() || 299 EXPECT_TRUE(!scroll_view.vertical_scroll_bar() ||
269 !scroll_view.vertical_scroll_bar()->visible()); 300 !scroll_view.vertical_scroll_bar()->visible());
270 301
271 // Both horizontal and vertical. 302 // Both horizontal and vertical.
272 contents->SetBounds(0, 0, 300, 400); 303 contents->SetBounds(0, 0, 300, 400);
273 scroll_view.Layout(); 304 scroll_view.Layout();
274 EXPECT_EQ(0, contents->parent()->x()); 305 EXPECT_EQ(0, test_api.contents_viewport()->x());
275 EXPECT_EQ(20, contents->parent()->y()); 306 EXPECT_EQ(20, test_api.contents_viewport()->y());
276 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); 307 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(),
308 test_api.contents_viewport()->width());
277 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20, 309 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20,
278 contents->parent()->height()); 310 test_api.contents_viewport()->height());
279 EXPECT_EQ(0, header->parent()->x()); 311 EXPECT_EQ(0, header->parent()->x());
280 EXPECT_EQ(0, header->parent()->y()); 312 EXPECT_EQ(0, header->parent()->y());
281 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width()); 313 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width());
282 EXPECT_EQ(20, header->parent()->height()); 314 EXPECT_EQ(20, header->parent()->height());
283 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); 315 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL);
284 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); 316 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible());
285 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); 317 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL);
286 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); 318 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible());
287 } 319 }
288 320
289 // Verifies the header scrolls horizontally with the content. 321 // Verifies the header scrolls horizontally with the content.
290 TEST(ScrollViewTest, HeaderScrollsWithContent) { 322 TEST(ScrollViewTest, HeaderScrollsWithContent) {
291 ScrollView scroll_view; 323 ScrollView scroll_view;
324 ScrollViewTestApi test_api(&scroll_view);
292 CustomView* contents = new CustomView; 325 CustomView* contents = new CustomView;
293 scroll_view.SetContents(contents); 326 scroll_view.SetContents(contents);
294 contents->SetPreferredSize(gfx::Size(500, 500)); 327 contents->SetPreferredSize(gfx::Size(500, 500));
295 328
296 CustomView* header = new CustomView; 329 CustomView* header = new CustomView;
297 scroll_view.SetHeader(header); 330 scroll_view.SetHeader(header);
298 header->SetPreferredSize(gfx::Size(500, 20)); 331 header->SetPreferredSize(gfx::Size(500, 20));
299 332
300 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 333 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
301 EXPECT_EQ("0,0", contents->bounds().origin().ToString()); 334 EXPECT_EQ("0,0", test_api.IntegralViewOffset().ToString());
302 EXPECT_EQ("0,0", header->bounds().origin().ToString()); 335 EXPECT_EQ("0,0", header->bounds().origin().ToString());
303 336
304 // Scroll the horizontal scrollbar. 337 // Scroll the horizontal scrollbar.
305 ASSERT_TRUE(scroll_view.horizontal_scroll_bar()); 338 ASSERT_TRUE(scroll_view.horizontal_scroll_bar());
306 scroll_view.ScrollToPosition( 339 scroll_view.ScrollToPosition(
307 const_cast<ScrollBar*>(scroll_view.horizontal_scroll_bar()), 1); 340 const_cast<ScrollBar*>(scroll_view.horizontal_scroll_bar()), 1);
308 EXPECT_EQ("-1,0", contents->bounds().origin().ToString()); 341 EXPECT_EQ("-1,0", test_api.IntegralViewOffset().ToString());
309 EXPECT_EQ("-1,0", header->bounds().origin().ToString()); 342 EXPECT_EQ("-1,0", header->bounds().origin().ToString());
310 343
311 // Scrolling the vertical scrollbar shouldn't effect the header. 344 // Scrolling the vertical scrollbar shouldn't effect the header.
312 ASSERT_TRUE(scroll_view.vertical_scroll_bar()); 345 ASSERT_TRUE(scroll_view.vertical_scroll_bar());
313 scroll_view.ScrollToPosition( 346 scroll_view.ScrollToPosition(
314 const_cast<ScrollBar*>(scroll_view.vertical_scroll_bar()), 1); 347 const_cast<ScrollBar*>(scroll_view.vertical_scroll_bar()), 1);
315 EXPECT_EQ("-1,-1", contents->bounds().origin().ToString()); 348 EXPECT_EQ("-1,-1", test_api.IntegralViewOffset().ToString());
316 EXPECT_EQ("-1,0", header->bounds().origin().ToString()); 349 EXPECT_EQ("-1,0", header->bounds().origin().ToString());
317 } 350 }
318 351
319 // Verifies ScrollRectToVisible() on the child works. 352 // Verifies ScrollRectToVisible() on the child works.
320 TEST(ScrollViewTest, ScrollRectToVisible) { 353 TEST(ScrollViewTest, ScrollRectToVisible) {
321 ScrollView scroll_view; 354 ScrollView scroll_view;
355 ScrollViewTestApi test_api(&scroll_view);
322 CustomView* contents = new CustomView; 356 CustomView* contents = new CustomView;
323 scroll_view.SetContents(contents); 357 scroll_view.SetContents(contents);
324 contents->SetPreferredSize(gfx::Size(500, 1000)); 358 contents->SetPreferredSize(gfx::Size(500, 1000));
325 359
326 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 360 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
327 scroll_view.Layout(); 361 scroll_view.Layout();
328 EXPECT_EQ("0,0", contents->bounds().origin().ToString()); 362 EXPECT_EQ("0,0", test_api.IntegralViewOffset().ToString());
329 363
330 // Scroll to y=405 height=10, this should make the y position of the content 364 // Scroll to y=405 height=10, this should make the y position of the content
331 // at (405 + 10) - viewport_height (scroll region bottom aligned). 365 // at (405 + 10) - viewport_height (scroll region bottom aligned).
332 contents->ScrollRectToVisible(gfx::Rect(0, 405, 10, 10)); 366 contents->ScrollRectToVisible(gfx::Rect(0, 405, 10, 10));
333 const int viewport_height = contents->parent()->height(); 367 const int viewport_height = test_api.contents_viewport()->height();
334 EXPECT_EQ(-(415 - viewport_height), contents->y()); 368
369 // Expect there to be a horizontal scrollbar, making the viewport shorter.
370 EXPECT_LT(viewport_height, 100);
371
372 gfx::ScrollOffset offset = test_api.CurrentOffset();
373 EXPECT_EQ(415 - viewport_height, offset.y());
335 374
336 // Scroll to the current y-location and 10x10; should do nothing. 375 // Scroll to the current y-location and 10x10; should do nothing.
337 contents->ScrollRectToVisible(gfx::Rect(0, -contents->y(), 10, 10)); 376 contents->ScrollRectToVisible(gfx::Rect(0, offset.y(), 10, 10));
338 EXPECT_EQ(-(415 - viewport_height), contents->y()); 377 EXPECT_EQ(415 - viewport_height, test_api.CurrentOffset().y());
339 } 378 }
340 379
341 // Verifies ClipHeightTo() uses the height of the content when it is between the 380 // Verifies ClipHeightTo() uses the height of the content when it is between the
342 // minimum and maximum height values. 381 // minimum and maximum height values.
343 TEST(ScrollViewTest, ClipHeightToNormalContentHeight) { 382 TEST(ScrollViewTest, ClipHeightToNormalContentHeight) {
344 ScrollView scroll_view; 383 ScrollView scroll_view;
345 384
346 scroll_view.ClipHeightTo(kMinHeight, kMaxHeight); 385 scroll_view.ClipHeightTo(kMinHeight, kMaxHeight);
347 386
348 const int kNormalContentHeight = 75; 387 const int kNormalContentHeight = 75;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 EXPECT_EQ(scroll_view.contents()->size().width(), expected_width); 468 EXPECT_EQ(scroll_view.contents()->size().width(), expected_width);
430 EXPECT_EQ(scroll_view.contents()->size().height(), 1000 * expected_width); 469 EXPECT_EQ(scroll_view.contents()->size().height(), 1000 * expected_width);
431 EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view.size()); 470 EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view.size());
432 } 471 }
433 472
434 TEST(ScrollViewTest, CornerViewVisibility) { 473 TEST(ScrollViewTest, CornerViewVisibility) {
435 ScrollView scroll_view; 474 ScrollView scroll_view;
436 View* contents = new View; 475 View* contents = new View;
437 scroll_view.SetContents(contents); 476 scroll_view.SetContents(contents);
438 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 477 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
439 View* corner_view = scroll_view.corner_view_; 478 View* corner_view = ScrollViewTestApi(&scroll_view).corner_view();
440 479
441 // Corner view should be visible when both scrollbars are visible. 480 // Corner view should be visible when both scrollbars are visible.
442 contents->SetBounds(0, 0, 200, 200); 481 contents->SetBounds(0, 0, 200, 200);
443 scroll_view.Layout(); 482 scroll_view.Layout();
444 EXPECT_EQ(&scroll_view, corner_view->parent()); 483 EXPECT_EQ(&scroll_view, corner_view->parent());
445 EXPECT_TRUE(corner_view->visible()); 484 EXPECT_TRUE(corner_view->visible());
446 485
447 // Corner view should be aligned to the scrollbars. 486 // Corner view should be aligned to the scrollbars.
448 EXPECT_EQ(scroll_view.vertical_scroll_bar()->x(), corner_view->x()); 487 EXPECT_EQ(scroll_view.vertical_scroll_bar()->x(), corner_view->x());
449 EXPECT_EQ(scroll_view.horizontal_scroll_bar()->y(), corner_view->y()); 488 EXPECT_EQ(scroll_view.horizontal_scroll_bar()->y(), corner_view->y());
(...skipping 24 matching lines...) Expand all
474 513
475 #if defined(OS_MACOSX) 514 #if defined(OS_MACOSX)
476 // Tests the overlay scrollbars on Mac. Ensure that they show up properly and 515 // Tests the overlay scrollbars on Mac. Ensure that they show up properly and
477 // do not overlap each other. 516 // do not overlap each other.
478 TEST(ScrollViewTest, CocoaOverlayScrollBars) { 517 TEST(ScrollViewTest, CocoaOverlayScrollBars) {
479 std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> 518 std::unique_ptr<ui::test::ScopedPreferredScrollerStyle>
480 scroller_style_override; 519 scroller_style_override;
481 scroller_style_override.reset( 520 scroller_style_override.reset(
482 new ui::test::ScopedPreferredScrollerStyle(true)); 521 new ui::test::ScopedPreferredScrollerStyle(true));
483 ScrollView scroll_view; 522 ScrollView scroll_view;
523 ScrollViewTestApi test_api(&scroll_view);
484 View* contents = new View; 524 View* contents = new View;
485 scroll_view.SetContents(contents); 525 scroll_view.SetContents(contents);
486 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 526 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
487 527
488 // Size the contents such that vertical scrollbar is needed. 528 // Size the contents such that vertical scrollbar is needed.
489 // Since it is overlaid, the ViewPort size should match the ScrollView. 529 // Since it is overlaid, the ViewPort size should match the ScrollView.
490 contents->SetBounds(0, 0, 50, 400); 530 contents->SetBounds(0, 0, 50, 400);
491 scroll_view.Layout(); 531 scroll_view.Layout();
492 EXPECT_EQ(100, contents->parent()->width()); 532 EXPECT_EQ(100, test_api.contents_viewport()->width());
493 EXPECT_EQ(100, contents->parent()->height()); 533 EXPECT_EQ(100, test_api.contents_viewport()->height());
494 EXPECT_EQ(0, scroll_view.GetScrollBarWidth()); 534 EXPECT_EQ(0, scroll_view.GetScrollBarWidth());
495 CheckScrollbarVisibility(scroll_view, VERTICAL, true); 535 CheckScrollbarVisibility(scroll_view, VERTICAL, true);
496 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false); 536 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false);
497 537
498 // Size the contents such that horizontal scrollbar is needed. 538 // Size the contents such that horizontal scrollbar is needed.
499 contents->SetBounds(0, 0, 400, 50); 539 contents->SetBounds(0, 0, 400, 50);
500 scroll_view.Layout(); 540 scroll_view.Layout();
501 EXPECT_EQ(100, contents->parent()->width()); 541 EXPECT_EQ(100, test_api.contents_viewport()->width());
502 EXPECT_EQ(100, contents->parent()->height()); 542 EXPECT_EQ(100, test_api.contents_viewport()->height());
503 EXPECT_EQ(0, scroll_view.GetScrollBarHeight()); 543 EXPECT_EQ(0, scroll_view.GetScrollBarHeight());
504 CheckScrollbarVisibility(scroll_view, VERTICAL, false); 544 CheckScrollbarVisibility(scroll_view, VERTICAL, false);
505 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); 545 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true);
506 546
507 // Both horizontal and vertical scrollbars. 547 // Both horizontal and vertical scrollbars.
508 contents->SetBounds(0, 0, 300, 400); 548 contents->SetBounds(0, 0, 300, 400);
509 scroll_view.Layout(); 549 scroll_view.Layout();
510 EXPECT_EQ(100, contents->parent()->width()); 550 EXPECT_EQ(100, test_api.contents_viewport()->width());
511 EXPECT_EQ(100, contents->parent()->height()); 551 EXPECT_EQ(100, test_api.contents_viewport()->height());
512 EXPECT_EQ(0, scroll_view.GetScrollBarWidth()); 552 EXPECT_EQ(0, scroll_view.GetScrollBarWidth());
513 EXPECT_EQ(0, scroll_view.GetScrollBarHeight()); 553 EXPECT_EQ(0, scroll_view.GetScrollBarHeight());
514 CheckScrollbarVisibility(scroll_view, VERTICAL, true); 554 CheckScrollbarVisibility(scroll_view, VERTICAL, true);
515 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); 555 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true);
516 556
517 // Make sure the horizontal and vertical scrollbars don't overlap each other. 557 // Make sure the horizontal and vertical scrollbars don't overlap each other.
518 gfx::Rect vert_bounds = scroll_view.vertical_scroll_bar()->bounds(); 558 gfx::Rect vert_bounds = scroll_view.vertical_scroll_bar()->bounds();
519 gfx::Rect horiz_bounds = scroll_view.horizontal_scroll_bar()->bounds(); 559 gfx::Rect horiz_bounds = scroll_view.horizontal_scroll_bar()->bounds();
520 EXPECT_EQ(vert_bounds.x(), horiz_bounds.right()); 560 EXPECT_EQ(vert_bounds.x(), horiz_bounds.right());
521 EXPECT_EQ(horiz_bounds.y(), vert_bounds.bottom()); 561 EXPECT_EQ(horiz_bounds.y(), vert_bounds.bottom());
522 562
523 // Switch to the non-overlay style and check that the ViewPort is now sized 563 // Switch to the non-overlay style and check that the ViewPort is now sized
524 // to be smaller, and ScrollbarWidth and ScrollbarHeight are non-zero. 564 // to be smaller, and ScrollbarWidth and ScrollbarHeight are non-zero.
525 scroller_style_override.reset( 565 scroller_style_override.reset(
526 new ui::test::ScopedPreferredScrollerStyle(false)); 566 new ui::test::ScopedPreferredScrollerStyle(false));
527 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); 567 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(),
568 test_api.contents_viewport()->width());
528 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), 569 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(),
529 contents->parent()->height()); 570 test_api.contents_viewport()->height());
530 EXPECT_NE(0, scroll_view.GetScrollBarWidth()); 571 EXPECT_NE(0, scroll_view.GetScrollBarWidth());
531 EXPECT_NE(0, scroll_view.GetScrollBarHeight()); 572 EXPECT_NE(0, scroll_view.GetScrollBarHeight());
532 } 573 }
533 #endif 574 #endif
534 575
535 } // namespace views 576 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/scroll_view.cc ('k') | ui/views/controls/scrollbar/cocoa_scroll_bar.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698