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

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

Issue 2442223002: Views: Give ScrollViewTest a test harness. (Closed)
Patch Set: Created 4 years, 1 month 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/base/test/scoped_preferred_scroller_style_mac.mm ('k') | no next file » | 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 "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/test/test_timeouts.h" 9 #include "base/test/test_timeouts.h"
10 #include "base/threading/thread_task_runner_handle.h" 10 #include "base/threading/thread_task_runner_handle.h"
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 125
126 ui::MouseEvent TestLeftMouseAt(const gfx::Point& location, ui::EventType type) { 126 ui::MouseEvent TestLeftMouseAt(const gfx::Point& location, ui::EventType type) {
127 return ui::MouseEvent(type, location, location, base::TimeTicks(), 127 return ui::MouseEvent(type, location, location, base::TimeTicks(),
128 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); 128 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
129 } 129 }
130 130
131 } // namespace 131 } // namespace
132 132
133 using test::ScrollViewTestApi; 133 using test::ScrollViewTestApi;
134 134
135 // Simple test harness for testing a ScrollView directly.
136 class ScrollViewTest : public testing::Test {
137 public:
138 ScrollViewTest() {}
139
140 View* InstallContents() {
141 const gfx::Rect default_outer_bounds(0, 0, 100, 100);
142 View* contents = new View;
143 scroll_view_.SetContents(contents);
144 scroll_view_.SetBoundsRect(default_outer_bounds);
145 return contents;
146 }
147
148 protected:
149 #if defined(OS_MACOSX)
150 void SetOverlayScrollersEnabled(bool enabled) {
151 scroller_style_.reset();
sky 2016/10/24 16:09:49 It's worth a comment as to why you need two resets
tapted 2016/10/25 02:44:12 Done.
152 scroller_style_.reset(new ui::test::ScopedPreferredScrollerStyle(enabled));
sky 2016/10/24 16:09:49 MakeUnique?
tapted 2016/10/25 02:44:12 Done. There's actually a way to omit the template
153 }
154
155 private:
156 // Disable overlay scrollers by default. This needs to be set before
157 // |scroll_view_| is initialized, otherwise scrollers will try to animate to
158 // change modes, which requires a MessageLoop to exist. Tests should only
tapted 2016/10/24 06:50:51 It's possible that later on I'll need a MessageLoo
159 // modify this via SetOverlayScrollersEnabled().
160 std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> scroller_style_ =
161 base::MakeUnique<ui::test::ScopedPreferredScrollerStyle>(false);
162
163 protected:
164 #endif
165 ScrollView scroll_view_;
166
167 private:
168 DISALLOW_COPY_AND_ASSIGN(ScrollViewTest);
169 };
170
135 // Test harness that includes a Widget to help test ui::Event handling. 171 // Test harness that includes a Widget to help test ui::Event handling.
136 class WidgetScrollViewTest : public test::WidgetTest, 172 class WidgetScrollViewTest : public test::WidgetTest,
137 public ui::CompositorObserver { 173 public ui::CompositorObserver {
138 public: 174 public:
139 static const int kDefaultHeight = 100; 175 static const int kDefaultHeight = 100;
140 static const int kDefaultWidth = 100; 176 static const int kDefaultWidth = 100;
141 177
142 WidgetScrollViewTest() { 178 WidgetScrollViewTest() {
143 #if defined(OS_MACOSX) 179 #if defined(OS_MACOSX)
144 // Disable scrollbar hiding (i.e. disable overlay scrollbars) by default. 180 // Disable scrollbar hiding (i.e. disable overlay scrollbars) by default.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> scroller_style_; 262 std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> scroller_style_;
227 #endif 263 #endif
228 264
229 DISALLOW_COPY_AND_ASSIGN(WidgetScrollViewTest); 265 DISALLOW_COPY_AND_ASSIGN(WidgetScrollViewTest);
230 }; 266 };
231 267
232 const int WidgetScrollViewTest::kDefaultHeight; 268 const int WidgetScrollViewTest::kDefaultHeight;
233 const int WidgetScrollViewTest::kDefaultWidth; 269 const int WidgetScrollViewTest::kDefaultWidth;
234 270
235 // Verifies the viewport is sized to fit the available space. 271 // Verifies the viewport is sized to fit the available space.
236 TEST(ScrollViewTest, ViewportSizedToFit) { 272 TEST_F(ScrollViewTest, ViewportSizedToFit) {
237 ScrollView scroll_view; 273 View* contents = InstallContents();
238 View* contents = new View; 274 scroll_view_.Layout();
239 scroll_view.SetContents(contents);
240 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
241 scroll_view.Layout();
242 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); 275 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString());
243 } 276 }
244 277
245 // Verifies the viewport and content is sized to fit the available space for 278 // Verifies the viewport and content is sized to fit the available space for
246 // bounded scroll view. 279 // bounded scroll view.
247 TEST(ScrollViewTest, BoundedViewportSizedToFit) { 280 TEST_F(ScrollViewTest, BoundedViewportSizedToFit) {
248 ScrollView scroll_view; 281 View* contents = InstallContents();
249 View* contents = new View; 282 scroll_view_.ClipHeightTo(100, 200);
250 scroll_view.SetContents(contents); 283 scroll_view_.SetBorder(Border::CreateSolidBorder(2, 0));
251 scroll_view.ClipHeightTo(100, 200); 284 scroll_view_.Layout();
252 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
253 scroll_view.SetBorder(Border::CreateSolidBorder(2, 0));
254 scroll_view.Layout();
255 EXPECT_EQ("2,2 96x96", contents->parent()->bounds().ToString()); 285 EXPECT_EQ("2,2 96x96", contents->parent()->bounds().ToString());
256 286
257 // Make sure the width of |contents| is set properly not to overflow the 287 // Make sure the width of |contents| is set properly not to overflow the
258 // viewport. 288 // viewport.
259 EXPECT_EQ(96, contents->width()); 289 EXPECT_EQ(96, contents->width());
260 } 290 }
261 291
262 // Verifies the scrollbars are added as necessary. 292 // Verifies the scrollbars are added as necessary.
263 // If on Mac, test the non-overlay scrollbars. 293 // If on Mac, test the non-overlay scrollbars.
264 TEST(ScrollViewTest, ScrollBars) { 294 TEST_F(ScrollViewTest, ScrollBars) {
265 #if defined(OS_MACOSX) 295 View* contents = InstallContents();
266 ui::test::ScopedPreferredScrollerStyle scroller_style_override(false);
267 #endif
268
269 ScrollView scroll_view;
270 View* contents = new View;
271 scroll_view.SetContents(contents);
272 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
273 296
274 // Size the contents such that vertical scrollbar is needed. 297 // Size the contents such that vertical scrollbar is needed.
275 contents->SetBounds(0, 0, 50, 400); 298 contents->SetBounds(0, 0, 50, 400);
276 scroll_view.Layout(); 299 scroll_view_.Layout();
277 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); 300 EXPECT_EQ(100 - scroll_view_.GetScrollBarWidth(),
301 contents->parent()->width());
278 EXPECT_EQ(100, contents->parent()->height()); 302 EXPECT_EQ(100, contents->parent()->height());
279 CheckScrollbarVisibility(scroll_view, VERTICAL, true); 303 CheckScrollbarVisibility(scroll_view_, VERTICAL, true);
280 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false); 304 CheckScrollbarVisibility(scroll_view_, HORIZONTAL, false);
281 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || 305 EXPECT_TRUE(!scroll_view_.horizontal_scroll_bar() ||
282 !scroll_view.horizontal_scroll_bar()->visible()); 306 !scroll_view_.horizontal_scroll_bar()->visible());
283 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); 307 ASSERT_TRUE(scroll_view_.vertical_scroll_bar() != NULL);
284 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); 308 EXPECT_TRUE(scroll_view_.vertical_scroll_bar()->visible());
285 309
286 // Size the contents such that horizontal scrollbar is needed. 310 // Size the contents such that horizontal scrollbar is needed.
287 contents->SetBounds(0, 0, 400, 50); 311 contents->SetBounds(0, 0, 400, 50);
288 scroll_view.Layout(); 312 scroll_view_.Layout();
289 EXPECT_EQ(100, contents->parent()->width()); 313 EXPECT_EQ(100, contents->parent()->width());
290 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), 314 EXPECT_EQ(100 - scroll_view_.GetScrollBarHeight(),
291 contents->parent()->height()); 315 contents->parent()->height());
292 CheckScrollbarVisibility(scroll_view, VERTICAL, false); 316 CheckScrollbarVisibility(scroll_view_, VERTICAL, false);
293 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); 317 CheckScrollbarVisibility(scroll_view_, HORIZONTAL, true);
294 318
295 // Both horizontal and vertical. 319 // Both horizontal and vertical.
296 contents->SetBounds(0, 0, 300, 400); 320 contents->SetBounds(0, 0, 300, 400);
297 scroll_view.Layout(); 321 scroll_view_.Layout();
298 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); 322 EXPECT_EQ(100 - scroll_view_.GetScrollBarWidth(),
299 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), 323 contents->parent()->width());
324 EXPECT_EQ(100 - scroll_view_.GetScrollBarHeight(),
300 contents->parent()->height()); 325 contents->parent()->height());
301 CheckScrollbarVisibility(scroll_view, VERTICAL, true); 326 CheckScrollbarVisibility(scroll_view_, VERTICAL, true);
302 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); 327 CheckScrollbarVisibility(scroll_view_, HORIZONTAL, true);
303 328
304 // Add a border, test vertical scrollbar. 329 // Add a border, test vertical scrollbar.
305 const int kTopPadding = 1; 330 const int kTopPadding = 1;
306 const int kLeftPadding = 2; 331 const int kLeftPadding = 2;
307 const int kBottomPadding = 3; 332 const int kBottomPadding = 3;
308 const int kRightPadding = 4; 333 const int kRightPadding = 4;
309 scroll_view.SetBorder(Border::CreateEmptyBorder( 334 scroll_view_.SetBorder(Border::CreateEmptyBorder(
310 kTopPadding, kLeftPadding, kBottomPadding, kRightPadding)); 335 kTopPadding, kLeftPadding, kBottomPadding, kRightPadding));
311 contents->SetBounds(0, 0, 50, 400); 336 contents->SetBounds(0, 0, 50, 400);
312 scroll_view.Layout(); 337 scroll_view_.Layout();
313 EXPECT_EQ( 338 EXPECT_EQ(
314 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding, 339 100 - scroll_view_.GetScrollBarWidth() - kLeftPadding - kRightPadding,
315 contents->parent()->width()); 340 contents->parent()->width());
316 EXPECT_EQ(100 - kTopPadding - kBottomPadding, contents->parent()->height()); 341 EXPECT_EQ(100 - kTopPadding - kBottomPadding, contents->parent()->height());
317 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || 342 EXPECT_TRUE(!scroll_view_.horizontal_scroll_bar() ||
318 !scroll_view.horizontal_scroll_bar()->visible()); 343 !scroll_view_.horizontal_scroll_bar()->visible());
319 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); 344 ASSERT_TRUE(scroll_view_.vertical_scroll_bar() != NULL);
320 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); 345 EXPECT_TRUE(scroll_view_.vertical_scroll_bar()->visible());
321 gfx::Rect bounds = scroll_view.vertical_scroll_bar()->bounds(); 346 gfx::Rect bounds = scroll_view_.vertical_scroll_bar()->bounds();
322 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth() - kRightPadding, bounds.x()); 347 EXPECT_EQ(100 - scroll_view_.GetScrollBarWidth() - kRightPadding, bounds.x());
323 EXPECT_EQ(100 - kRightPadding, bounds.right()); 348 EXPECT_EQ(100 - kRightPadding, bounds.right());
324 EXPECT_EQ(kTopPadding, bounds.y()); 349 EXPECT_EQ(kTopPadding, bounds.y());
325 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); 350 EXPECT_EQ(100 - kBottomPadding, bounds.bottom());
326 351
327 // Horizontal with border. 352 // Horizontal with border.
328 contents->SetBounds(0, 0, 400, 50); 353 contents->SetBounds(0, 0, 400, 50);
329 scroll_view.Layout(); 354 scroll_view_.Layout();
330 EXPECT_EQ(100 - kLeftPadding - kRightPadding, contents->parent()->width()); 355 EXPECT_EQ(100 - kLeftPadding - kRightPadding, contents->parent()->width());
331 EXPECT_EQ( 356 EXPECT_EQ(
332 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding, 357 100 - scroll_view_.GetScrollBarHeight() - kTopPadding - kBottomPadding,
333 contents->parent()->height()); 358 contents->parent()->height());
334 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); 359 ASSERT_TRUE(scroll_view_.horizontal_scroll_bar() != NULL);
335 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); 360 EXPECT_TRUE(scroll_view_.horizontal_scroll_bar()->visible());
336 EXPECT_TRUE(!scroll_view.vertical_scroll_bar() || 361 EXPECT_TRUE(!scroll_view_.vertical_scroll_bar() ||
337 !scroll_view.vertical_scroll_bar()->visible()); 362 !scroll_view_.vertical_scroll_bar()->visible());
338 bounds = scroll_view.horizontal_scroll_bar()->bounds(); 363 bounds = scroll_view_.horizontal_scroll_bar()->bounds();
339 EXPECT_EQ(kLeftPadding, bounds.x()); 364 EXPECT_EQ(kLeftPadding, bounds.x());
340 EXPECT_EQ(100 - kRightPadding, bounds.right()); 365 EXPECT_EQ(100 - kRightPadding, bounds.right());
341 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), 366 EXPECT_EQ(100 - kBottomPadding - scroll_view_.GetScrollBarHeight(),
342 bounds.y()); 367 bounds.y());
343 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); 368 EXPECT_EQ(100 - kBottomPadding, bounds.bottom());
344 369
345 // Both horizontal and vertical with border. 370 // Both horizontal and vertical with border.
346 contents->SetBounds(0, 0, 300, 400); 371 contents->SetBounds(0, 0, 300, 400);
347 scroll_view.Layout(); 372 scroll_view_.Layout();
348 EXPECT_EQ( 373 EXPECT_EQ(
349 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding, 374 100 - scroll_view_.GetScrollBarWidth() - kLeftPadding - kRightPadding,
350 contents->parent()->width()); 375 contents->parent()->width());
351 EXPECT_EQ( 376 EXPECT_EQ(
352 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding, 377 100 - scroll_view_.GetScrollBarHeight() - kTopPadding - kBottomPadding,
353 contents->parent()->height()); 378 contents->parent()->height());
354 bounds = scroll_view.horizontal_scroll_bar()->bounds(); 379 bounds = scroll_view_.horizontal_scroll_bar()->bounds();
355 // Check horiz. 380 // Check horiz.
356 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); 381 ASSERT_TRUE(scroll_view_.horizontal_scroll_bar() != NULL);
357 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); 382 EXPECT_TRUE(scroll_view_.horizontal_scroll_bar()->visible());
358 bounds = scroll_view.horizontal_scroll_bar()->bounds(); 383 bounds = scroll_view_.horizontal_scroll_bar()->bounds();
359 EXPECT_EQ(kLeftPadding, bounds.x()); 384 EXPECT_EQ(kLeftPadding, bounds.x());
360 EXPECT_EQ(100 - kRightPadding - scroll_view.GetScrollBarWidth(), 385 EXPECT_EQ(100 - kRightPadding - scroll_view_.GetScrollBarWidth(),
361 bounds.right()); 386 bounds.right());
362 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), 387 EXPECT_EQ(100 - kBottomPadding - scroll_view_.GetScrollBarHeight(),
363 bounds.y()); 388 bounds.y());
364 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); 389 EXPECT_EQ(100 - kBottomPadding, bounds.bottom());
365 // Check vert. 390 // Check vert.
366 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); 391 ASSERT_TRUE(scroll_view_.vertical_scroll_bar() != NULL);
367 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); 392 EXPECT_TRUE(scroll_view_.vertical_scroll_bar()->visible());
368 bounds = scroll_view.vertical_scroll_bar()->bounds(); 393 bounds = scroll_view_.vertical_scroll_bar()->bounds();
369 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth() - kRightPadding, bounds.x()); 394 EXPECT_EQ(100 - scroll_view_.GetScrollBarWidth() - kRightPadding, bounds.x());
370 EXPECT_EQ(100 - kRightPadding, bounds.right()); 395 EXPECT_EQ(100 - kRightPadding, bounds.right());
371 EXPECT_EQ(kTopPadding, bounds.y()); 396 EXPECT_EQ(kTopPadding, bounds.y());
372 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), 397 EXPECT_EQ(100 - kBottomPadding - scroll_view_.GetScrollBarHeight(),
373 bounds.bottom()); 398 bounds.bottom());
374 } 399 }
375 400
376 // Assertions around adding a header. 401 // Assertions around adding a header.
377 TEST(ScrollViewTest, Header) { 402 TEST_F(ScrollViewTest, Header) {
378 ScrollView scroll_view;
379 View* contents = new View;
380 CustomView* header = new CustomView; 403 CustomView* header = new CustomView;
381 scroll_view.SetHeader(header); 404 scroll_view_.SetHeader(header);
382 View* header_parent = header->parent(); 405 View* header_parent = header->parent();
383 scroll_view.SetContents(contents); 406 View* contents = InstallContents();
384 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 407
385 scroll_view.Layout(); 408 scroll_view_.Layout();
386 // |header|s preferred size is empty, which should result in all space going 409 // |header|s preferred size is empty, which should result in all space going
387 // to contents. 410 // to contents.
388 EXPECT_EQ("0,0 100x0", header->parent()->bounds().ToString()); 411 EXPECT_EQ("0,0 100x0", header->parent()->bounds().ToString());
389 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); 412 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString());
390 413
391 // With layered scrolling, ScrollView::Layout() will impose a size on the 414 // With layered scrolling, ScrollView::Layout() will impose a size on the
392 // contents that fills the viewport. Since the test view doesn't have its own 415 // contents that fills the viewport. Since the test view doesn't have its own
393 // Layout, reset it in this case so that adding a header doesn't shift the 416 // Layout, reset it in this case so that adding a header doesn't shift the
394 // contents down and require scrollbars. 417 // contents down and require scrollbars.
395 if (contents->layer()) { 418 if (contents->layer()) {
396 EXPECT_EQ("0,0 100x100", contents->bounds().ToString()); 419 EXPECT_EQ("0,0 100x100", contents->bounds().ToString());
397 contents->SetBoundsRect(gfx::Rect()); 420 contents->SetBoundsRect(gfx::Rect());
398 } 421 }
399 EXPECT_EQ("0,0 0x0", contents->bounds().ToString()); 422 EXPECT_EQ("0,0 0x0", contents->bounds().ToString());
400 423
401 // Get the header a height of 20. 424 // Get the header a height of 20.
402 header->SetPreferredSize(gfx::Size(10, 20)); 425 header->SetPreferredSize(gfx::Size(10, 20));
403 EXPECT_EQ("0,0 100x20", header->parent()->bounds().ToString()); 426 EXPECT_EQ("0,0 100x20", header->parent()->bounds().ToString());
404 EXPECT_EQ("0,20 100x80", contents->parent()->bounds().ToString()); 427 EXPECT_EQ("0,20 100x80", contents->parent()->bounds().ToString());
405 if (contents->layer()) { 428 if (contents->layer()) {
406 EXPECT_EQ("0,0 100x80", contents->bounds().ToString()); 429 EXPECT_EQ("0,0 100x80", contents->bounds().ToString());
407 contents->SetBoundsRect(gfx::Rect()); 430 contents->SetBoundsRect(gfx::Rect());
408 } 431 }
409 EXPECT_EQ("0,0 0x0", contents->bounds().ToString()); 432 EXPECT_EQ("0,0 0x0", contents->bounds().ToString());
410 433
411 // Remove the header. 434 // Remove the header.
412 scroll_view.SetHeader(NULL); 435 scroll_view_.SetHeader(NULL);
413 // SetHeader(NULL) deletes header. 436 // SetHeader(NULL) deletes header.
414 header = NULL; 437 header = NULL;
415 EXPECT_EQ("0,0 100x0", header_parent->bounds().ToString()); 438 EXPECT_EQ("0,0 100x0", header_parent->bounds().ToString());
416 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); 439 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString());
417 } 440 }
418 441
419 // Verifies the scrollbars are added as necessary when a header is present. 442 // Verifies the scrollbars are added as necessary when a header is present.
420 TEST(ScrollViewTest, ScrollBarsWithHeader) { 443 TEST_F(ScrollViewTest, ScrollBarsWithHeader) {
421 ScrollView scroll_view;
422 View* contents = new View;
423 scroll_view.SetContents(contents);
424 CustomView* header = new CustomView; 444 CustomView* header = new CustomView;
425 scroll_view.SetHeader(header); 445 scroll_view_.SetHeader(header);
426 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 446 View* contents = InstallContents();
427 447
428 header->SetPreferredSize(gfx::Size(10, 20)); 448 header->SetPreferredSize(gfx::Size(10, 20));
429 449
430 // Size the contents such that vertical scrollbar is needed. 450 // Size the contents such that vertical scrollbar is needed.
431 contents->SetBounds(0, 0, 50, 400); 451 contents->SetBounds(0, 0, 50, 400);
432 scroll_view.Layout(); 452 scroll_view_.Layout();
433 EXPECT_EQ(0, contents->parent()->x()); 453 EXPECT_EQ(0, contents->parent()->x());
434 EXPECT_EQ(20, contents->parent()->y()); 454 EXPECT_EQ(20, contents->parent()->y());
435 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); 455 EXPECT_EQ(100 - scroll_view_.GetScrollBarWidth(),
456 contents->parent()->width());
436 EXPECT_EQ(80, contents->parent()->height()); 457 EXPECT_EQ(80, contents->parent()->height());
437 EXPECT_EQ(0, header->parent()->x()); 458 EXPECT_EQ(0, header->parent()->x());
438 EXPECT_EQ(0, header->parent()->y()); 459 EXPECT_EQ(0, header->parent()->y());
439 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width()); 460 EXPECT_EQ(100 - scroll_view_.GetScrollBarWidth(), header->parent()->width());
440 EXPECT_EQ(20, header->parent()->height()); 461 EXPECT_EQ(20, header->parent()->height());
441 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || 462 EXPECT_TRUE(!scroll_view_.horizontal_scroll_bar() ||
442 !scroll_view.horizontal_scroll_bar()->visible()); 463 !scroll_view_.horizontal_scroll_bar()->visible());
443 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); 464 ASSERT_TRUE(scroll_view_.vertical_scroll_bar() != NULL);
444 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); 465 EXPECT_TRUE(scroll_view_.vertical_scroll_bar()->visible());
445 // Make sure the vertical scrollbar overlaps the header. 466 // Make sure the vertical scrollbar overlaps the header.
446 EXPECT_EQ(header->y(), scroll_view.vertical_scroll_bar()->y()); 467 EXPECT_EQ(header->y(), scroll_view_.vertical_scroll_bar()->y());
447 EXPECT_EQ(header->y(), contents->y()); 468 EXPECT_EQ(header->y(), contents->y());
448 469
449 // Size the contents such that horizontal scrollbar is needed. 470 // Size the contents such that horizontal scrollbar is needed.
450 contents->SetBounds(0, 0, 400, 50); 471 contents->SetBounds(0, 0, 400, 50);
451 scroll_view.Layout(); 472 scroll_view_.Layout();
452 EXPECT_EQ(0, contents->parent()->x()); 473 EXPECT_EQ(0, contents->parent()->x());
453 EXPECT_EQ(20, contents->parent()->y()); 474 EXPECT_EQ(20, contents->parent()->y());
454 EXPECT_EQ(100, contents->parent()->width()); 475 EXPECT_EQ(100, contents->parent()->width());
455 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20, 476 EXPECT_EQ(100 - scroll_view_.GetScrollBarHeight() - 20,
456 contents->parent()->height()); 477 contents->parent()->height());
457 EXPECT_EQ(0, header->parent()->x()); 478 EXPECT_EQ(0, header->parent()->x());
458 EXPECT_EQ(0, header->parent()->y()); 479 EXPECT_EQ(0, header->parent()->y());
459 EXPECT_EQ(100, header->parent()->width()); 480 EXPECT_EQ(100, header->parent()->width());
460 EXPECT_EQ(20, header->parent()->height()); 481 EXPECT_EQ(20, header->parent()->height());
461 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); 482 ASSERT_TRUE(scroll_view_.horizontal_scroll_bar() != NULL);
462 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); 483 EXPECT_TRUE(scroll_view_.horizontal_scroll_bar()->visible());
463 EXPECT_TRUE(!scroll_view.vertical_scroll_bar() || 484 EXPECT_TRUE(!scroll_view_.vertical_scroll_bar() ||
464 !scroll_view.vertical_scroll_bar()->visible()); 485 !scroll_view_.vertical_scroll_bar()->visible());
465 486
466 // Both horizontal and vertical. 487 // Both horizontal and vertical.
467 contents->SetBounds(0, 0, 300, 400); 488 contents->SetBounds(0, 0, 300, 400);
468 scroll_view.Layout(); 489 scroll_view_.Layout();
469 EXPECT_EQ(0, contents->parent()->x()); 490 EXPECT_EQ(0, contents->parent()->x());
470 EXPECT_EQ(20, contents->parent()->y()); 491 EXPECT_EQ(20, contents->parent()->y());
471 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); 492 EXPECT_EQ(100 - scroll_view_.GetScrollBarWidth(),
472 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20, 493 contents->parent()->width());
494 EXPECT_EQ(100 - scroll_view_.GetScrollBarHeight() - 20,
473 contents->parent()->height()); 495 contents->parent()->height());
474 EXPECT_EQ(0, header->parent()->x()); 496 EXPECT_EQ(0, header->parent()->x());
475 EXPECT_EQ(0, header->parent()->y()); 497 EXPECT_EQ(0, header->parent()->y());
476 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width()); 498 EXPECT_EQ(100 - scroll_view_.GetScrollBarWidth(), header->parent()->width());
477 EXPECT_EQ(20, header->parent()->height()); 499 EXPECT_EQ(20, header->parent()->height());
478 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); 500 ASSERT_TRUE(scroll_view_.horizontal_scroll_bar() != NULL);
479 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); 501 EXPECT_TRUE(scroll_view_.horizontal_scroll_bar()->visible());
480 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); 502 ASSERT_TRUE(scroll_view_.vertical_scroll_bar() != NULL);
481 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); 503 EXPECT_TRUE(scroll_view_.vertical_scroll_bar()->visible());
482 } 504 }
483 505
484 // Verifies the header scrolls horizontally with the content. 506 // Verifies the header scrolls horizontally with the content.
485 TEST(ScrollViewTest, HeaderScrollsWithContent) { 507 TEST_F(ScrollViewTest, HeaderScrollsWithContent) {
486 ScrollView scroll_view; 508 ScrollViewTestApi test_api(&scroll_view_);
487 ScrollViewTestApi test_api(&scroll_view);
488 CustomView* contents = new CustomView; 509 CustomView* contents = new CustomView;
489 scroll_view.SetContents(contents); 510 scroll_view_.SetContents(contents);
490 contents->SetPreferredSize(gfx::Size(500, 500)); 511 contents->SetPreferredSize(gfx::Size(500, 500));
491 512
492 CustomView* header = new CustomView; 513 CustomView* header = new CustomView;
493 scroll_view.SetHeader(header); 514 scroll_view_.SetHeader(header);
494 header->SetPreferredSize(gfx::Size(500, 20)); 515 header->SetPreferredSize(gfx::Size(500, 20));
495 516
496 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 517 scroll_view_.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
497 EXPECT_EQ("0,0", test_api.IntegralViewOffset().ToString()); 518 EXPECT_EQ("0,0", test_api.IntegralViewOffset().ToString());
498 EXPECT_EQ("0,0", header->bounds().origin().ToString()); 519 EXPECT_EQ("0,0", header->bounds().origin().ToString());
499 520
500 // Scroll the horizontal scrollbar. 521 // Scroll the horizontal scrollbar.
501 ASSERT_TRUE(scroll_view.horizontal_scroll_bar()); 522 ASSERT_TRUE(scroll_view_.horizontal_scroll_bar());
502 scroll_view.ScrollToPosition( 523 scroll_view_.ScrollToPosition(
503 const_cast<ScrollBar*>(scroll_view.horizontal_scroll_bar()), 1); 524 const_cast<ScrollBar*>(scroll_view_.horizontal_scroll_bar()), 1);
504 EXPECT_EQ("-1,0", test_api.IntegralViewOffset().ToString()); 525 EXPECT_EQ("-1,0", test_api.IntegralViewOffset().ToString());
505 EXPECT_EQ("-1,0", header->bounds().origin().ToString()); 526 EXPECT_EQ("-1,0", header->bounds().origin().ToString());
506 527
507 // Scrolling the vertical scrollbar shouldn't effect the header. 528 // Scrolling the vertical scrollbar shouldn't effect the header.
508 ASSERT_TRUE(scroll_view.vertical_scroll_bar()); 529 ASSERT_TRUE(scroll_view_.vertical_scroll_bar());
509 scroll_view.ScrollToPosition( 530 scroll_view_.ScrollToPosition(
510 const_cast<ScrollBar*>(scroll_view.vertical_scroll_bar()), 1); 531 const_cast<ScrollBar*>(scroll_view_.vertical_scroll_bar()), 1);
511 EXPECT_EQ("-1,-1", test_api.IntegralViewOffset().ToString()); 532 EXPECT_EQ("-1,-1", test_api.IntegralViewOffset().ToString());
512 EXPECT_EQ("-1,0", header->bounds().origin().ToString()); 533 EXPECT_EQ("-1,0", header->bounds().origin().ToString());
513 } 534 }
514 535
515 // Verifies ScrollRectToVisible() on the child works. 536 // Verifies ScrollRectToVisible() on the child works.
516 TEST(ScrollViewTest, ScrollRectToVisible) { 537 TEST_F(ScrollViewTest, ScrollRectToVisible) {
517 #if defined(OS_MACOSX) 538 ScrollViewTestApi test_api(&scroll_view_);
518 ui::test::ScopedPreferredScrollerStyle scroller_style_override(false);
519 #endif
520 ScrollView scroll_view;
521 ScrollViewTestApi test_api(&scroll_view);
522 CustomView* contents = new CustomView; 539 CustomView* contents = new CustomView;
523 scroll_view.SetContents(contents); 540 scroll_view_.SetContents(contents);
524 contents->SetPreferredSize(gfx::Size(500, 1000)); 541 contents->SetPreferredSize(gfx::Size(500, 1000));
525 542
526 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 543 scroll_view_.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
527 scroll_view.Layout(); 544 scroll_view_.Layout();
528 EXPECT_EQ("0,0", test_api.IntegralViewOffset().ToString()); 545 EXPECT_EQ("0,0", test_api.IntegralViewOffset().ToString());
529 546
530 // Scroll to y=405 height=10, this should make the y position of the content 547 // Scroll to y=405 height=10, this should make the y position of the content
531 // at (405 + 10) - viewport_height (scroll region bottom aligned). 548 // at (405 + 10) - viewport_height (scroll region bottom aligned).
532 contents->ScrollRectToVisible(gfx::Rect(0, 405, 10, 10)); 549 contents->ScrollRectToVisible(gfx::Rect(0, 405, 10, 10));
533 const int viewport_height = test_api.contents_viewport()->height(); 550 const int viewport_height = test_api.contents_viewport()->height();
534 551
535 // Expect there to be a horizontal scrollbar, making the viewport shorter. 552 // Expect there to be a horizontal scrollbar, making the viewport shorter.
536 EXPECT_LT(viewport_height, 100); 553 EXPECT_LT(viewport_height, 100);
537 554
538 gfx::ScrollOffset offset = test_api.CurrentOffset(); 555 gfx::ScrollOffset offset = test_api.CurrentOffset();
539 EXPECT_EQ(415 - viewport_height, offset.y()); 556 EXPECT_EQ(415 - viewport_height, offset.y());
540 557
541 // Scroll to the current y-location and 10x10; should do nothing. 558 // Scroll to the current y-location and 10x10; should do nothing.
542 contents->ScrollRectToVisible(gfx::Rect(0, offset.y(), 10, 10)); 559 contents->ScrollRectToVisible(gfx::Rect(0, offset.y(), 10, 10));
543 EXPECT_EQ(415 - viewport_height, test_api.CurrentOffset().y()); 560 EXPECT_EQ(415 - viewport_height, test_api.CurrentOffset().y());
544 } 561 }
545 562
546 // Verifies ClipHeightTo() uses the height of the content when it is between the 563 // Verifies ClipHeightTo() uses the height of the content when it is between the
547 // minimum and maximum height values. 564 // minimum and maximum height values.
548 TEST(ScrollViewTest, ClipHeightToNormalContentHeight) { 565 TEST_F(ScrollViewTest, ClipHeightToNormalContentHeight) {
549 ScrollView scroll_view; 566 scroll_view_.ClipHeightTo(kMinHeight, kMaxHeight);
550
551 scroll_view.ClipHeightTo(kMinHeight, kMaxHeight);
552 567
553 const int kNormalContentHeight = 75; 568 const int kNormalContentHeight = 75;
554 scroll_view.SetContents( 569 scroll_view_.SetContents(
555 new views::StaticSizedView(gfx::Size(kWidth, kNormalContentHeight))); 570 new views::StaticSizedView(gfx::Size(kWidth, kNormalContentHeight)));
556 571
557 EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight), 572 EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight),
558 scroll_view.GetPreferredSize()); 573 scroll_view_.GetPreferredSize());
559 574
560 scroll_view.SizeToPreferredSize(); 575 scroll_view_.SizeToPreferredSize();
561 scroll_view.Layout(); 576 scroll_view_.Layout();
562 577
563 EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight), 578 EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight),
564 scroll_view.contents()->size()); 579 scroll_view_.contents()->size());
565 EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight), scroll_view.size()); 580 EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight), scroll_view_.size());
566 } 581 }
567 582
568 // Verifies ClipHeightTo() uses the minimum height when the content is shorter 583 // Verifies ClipHeightTo() uses the minimum height when the content is shorter
569 // than the minimum height value. 584 // than the minimum height value.
570 TEST(ScrollViewTest, ClipHeightToShortContentHeight) { 585 TEST_F(ScrollViewTest, ClipHeightToShortContentHeight) {
571 ScrollView scroll_view; 586 scroll_view_.ClipHeightTo(kMinHeight, kMaxHeight);
572
573 scroll_view.ClipHeightTo(kMinHeight, kMaxHeight);
574 587
575 const int kShortContentHeight = 10; 588 const int kShortContentHeight = 10;
576 View* contents = 589 View* contents =
577 new views::StaticSizedView(gfx::Size(kWidth, kShortContentHeight)); 590 new views::StaticSizedView(gfx::Size(kWidth, kShortContentHeight));
578 scroll_view.SetContents(contents); 591 scroll_view_.SetContents(contents);
579 592
580 EXPECT_EQ(gfx::Size(kWidth, kMinHeight), scroll_view.GetPreferredSize()); 593 EXPECT_EQ(gfx::Size(kWidth, kMinHeight), scroll_view_.GetPreferredSize());
581 594
582 scroll_view.SizeToPreferredSize(); 595 scroll_view_.SizeToPreferredSize();
583 scroll_view.Layout(); 596 scroll_view_.Layout();
584 597
585 // Layered scrolling requires the contents to fill the viewport. 598 // Layered scrolling requires the contents to fill the viewport.
586 if (contents->layer()) { 599 if (contents->layer()) {
587 EXPECT_EQ(gfx::Size(kWidth, kMinHeight), scroll_view.contents()->size()); 600 EXPECT_EQ(gfx::Size(kWidth, kMinHeight), scroll_view_.contents()->size());
588 } else { 601 } else {
589 EXPECT_EQ(gfx::Size(kWidth, kShortContentHeight), 602 EXPECT_EQ(gfx::Size(kWidth, kShortContentHeight),
590 scroll_view.contents()->size()); 603 scroll_view_.contents()->size());
591 } 604 }
592 EXPECT_EQ(gfx::Size(kWidth, kMinHeight), scroll_view.size()); 605 EXPECT_EQ(gfx::Size(kWidth, kMinHeight), scroll_view_.size());
593 } 606 }
594 607
595 // Verifies ClipHeightTo() uses the maximum height when the content is longer 608 // Verifies ClipHeightTo() uses the maximum height when the content is longer
596 // thamn the maximum height value. 609 // thamn the maximum height value.
597 TEST(ScrollViewTest, ClipHeightToTallContentHeight) { 610 TEST_F(ScrollViewTest, ClipHeightToTallContentHeight) {
598 ScrollView scroll_view;
599
600 // Use a scrollbar that is disabled by default, so the width of the content is 611 // Use a scrollbar that is disabled by default, so the width of the content is
601 // not affected. 612 // not affected.
602 scroll_view.SetVerticalScrollBar(new views::OverlayScrollBar(false)); 613 scroll_view_.SetVerticalScrollBar(new views::OverlayScrollBar(false));
603 614
604 scroll_view.ClipHeightTo(kMinHeight, kMaxHeight); 615 scroll_view_.ClipHeightTo(kMinHeight, kMaxHeight);
605 616
606 const int kTallContentHeight = 1000; 617 const int kTallContentHeight = 1000;
607 scroll_view.SetContents( 618 scroll_view_.SetContents(
608 new views::StaticSizedView(gfx::Size(kWidth, kTallContentHeight))); 619 new views::StaticSizedView(gfx::Size(kWidth, kTallContentHeight)));
609 620
610 EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view.GetPreferredSize()); 621 EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view_.GetPreferredSize());
611 622
612 scroll_view.SizeToPreferredSize(); 623 scroll_view_.SizeToPreferredSize();
613 scroll_view.Layout(); 624 scroll_view_.Layout();
614 625
615 EXPECT_EQ(gfx::Size(kWidth, kTallContentHeight), 626 EXPECT_EQ(gfx::Size(kWidth, kTallContentHeight),
616 scroll_view.contents()->size()); 627 scroll_view_.contents()->size());
617 EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view.size()); 628 EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view_.size());
618 } 629 }
619 630
620 // Verifies that when ClipHeightTo() produces a scrollbar, it reduces the width 631 // Verifies that when ClipHeightTo() produces a scrollbar, it reduces the width
621 // of the inner content of the ScrollView. 632 // of the inner content of the ScrollView.
622 TEST(ScrollViewTest, ClipHeightToScrollbarUsesWidth) { 633 TEST_F(ScrollViewTest, ClipHeightToScrollbarUsesWidth) {
623 ScrollView scroll_view; 634 scroll_view_.ClipHeightTo(kMinHeight, kMaxHeight);
624
625 scroll_view.ClipHeightTo(kMinHeight, kMaxHeight);
626 635
627 // Create a view that will be much taller than it is wide. 636 // Create a view that will be much taller than it is wide.
628 scroll_view.SetContents(new views::ProportionallySizedView(1000)); 637 scroll_view_.SetContents(new views::ProportionallySizedView(1000));
629 638
630 // Without any width, it will default to 0,0 but be overridden by min height. 639 // Without any width, it will default to 0,0 but be overridden by min height.
631 scroll_view.SizeToPreferredSize(); 640 scroll_view_.SizeToPreferredSize();
632 EXPECT_EQ(gfx::Size(0, kMinHeight), scroll_view.GetPreferredSize()); 641 EXPECT_EQ(gfx::Size(0, kMinHeight), scroll_view_.GetPreferredSize());
633 642
634 gfx::Size new_size(kWidth, scroll_view.GetHeightForWidth(kWidth)); 643 gfx::Size new_size(kWidth, scroll_view_.GetHeightForWidth(kWidth));
635 scroll_view.SetSize(new_size); 644 scroll_view_.SetSize(new_size);
636 scroll_view.Layout(); 645 scroll_view_.Layout();
637 646
638 int scroll_bar_width = scroll_view.GetScrollBarWidth(); 647 int scroll_bar_width = scroll_view_.GetScrollBarWidth();
639 int expected_width = kWidth - scroll_bar_width; 648 int expected_width = kWidth - scroll_bar_width;
640 EXPECT_EQ(scroll_view.contents()->size().width(), expected_width); 649 EXPECT_EQ(scroll_view_.contents()->size().width(), expected_width);
641 EXPECT_EQ(scroll_view.contents()->size().height(), 1000 * expected_width); 650 EXPECT_EQ(scroll_view_.contents()->size().height(), 1000 * expected_width);
642 EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view.size()); 651 EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view_.size());
643 } 652 }
644 653
645 TEST(ScrollViewTest, CornerViewVisibility) { 654 TEST_F(ScrollViewTest, CornerViewVisibility) {
646 ScrollView scroll_view; 655 View* contents = InstallContents();
647 View* contents = new View; 656 View* corner_view = ScrollViewTestApi(&scroll_view_).corner_view();
648 scroll_view.SetContents(contents);
649 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
650 View* corner_view = ScrollViewTestApi(&scroll_view).corner_view();
651 657
652 // Corner view should be visible when both scrollbars are visible. 658 // Corner view should be visible when both scrollbars are visible.
653 contents->SetBounds(0, 0, 200, 200); 659 contents->SetBounds(0, 0, 200, 200);
654 scroll_view.Layout(); 660 scroll_view_.Layout();
655 EXPECT_EQ(&scroll_view, corner_view->parent()); 661 EXPECT_EQ(&scroll_view_, corner_view->parent());
656 EXPECT_TRUE(corner_view->visible()); 662 EXPECT_TRUE(corner_view->visible());
657 663
658 // Corner view should be aligned to the scrollbars. 664 // Corner view should be aligned to the scrollbars.
659 EXPECT_EQ(scroll_view.vertical_scroll_bar()->x(), corner_view->x()); 665 EXPECT_EQ(scroll_view_.vertical_scroll_bar()->x(), corner_view->x());
660 EXPECT_EQ(scroll_view.horizontal_scroll_bar()->y(), corner_view->y()); 666 EXPECT_EQ(scroll_view_.horizontal_scroll_bar()->y(), corner_view->y());
661 EXPECT_EQ(scroll_view.GetScrollBarWidth(), corner_view->width()); 667 EXPECT_EQ(scroll_view_.GetScrollBarWidth(), corner_view->width());
662 EXPECT_EQ(scroll_view.GetScrollBarHeight(), corner_view->height()); 668 EXPECT_EQ(scroll_view_.GetScrollBarHeight(), corner_view->height());
663 669
664 // Corner view should be removed when only the vertical scrollbar is visible. 670 // Corner view should be removed when only the vertical scrollbar is visible.
665 contents->SetBounds(0, 0, 50, 200); 671 contents->SetBounds(0, 0, 50, 200);
666 scroll_view.Layout(); 672 scroll_view_.Layout();
667 EXPECT_FALSE(corner_view->parent()); 673 EXPECT_FALSE(corner_view->parent());
668 674
669 // ... or when only the horizontal scrollbar is visible. 675 // ... or when only the horizontal scrollbar is visible.
670 contents->SetBounds(0, 0, 200, 50); 676 contents->SetBounds(0, 0, 200, 50);
671 scroll_view.Layout(); 677 scroll_view_.Layout();
672 EXPECT_FALSE(corner_view->parent()); 678 EXPECT_FALSE(corner_view->parent());
673 679
674 // ... or when no scrollbar is visible. 680 // ... or when no scrollbar is visible.
675 contents->SetBounds(0, 0, 50, 50); 681 contents->SetBounds(0, 0, 50, 50);
676 scroll_view.Layout(); 682 scroll_view_.Layout();
677 EXPECT_FALSE(corner_view->parent()); 683 EXPECT_FALSE(corner_view->parent());
678 684
679 // Corner view should reappear when both scrollbars reappear. 685 // Corner view should reappear when both scrollbars reappear.
680 contents->SetBounds(0, 0, 200, 200); 686 contents->SetBounds(0, 0, 200, 200);
681 scroll_view.Layout(); 687 scroll_view_.Layout();
682 EXPECT_EQ(&scroll_view, corner_view->parent()); 688 EXPECT_EQ(&scroll_view_, corner_view->parent());
683 EXPECT_TRUE(corner_view->visible()); 689 EXPECT_TRUE(corner_view->visible());
684 } 690 }
685 691
686 #if defined(OS_MACOSX) 692 #if defined(OS_MACOSX)
687 // Tests the overlay scrollbars on Mac. Ensure that they show up properly and 693 // Tests the overlay scrollbars on Mac. Ensure that they show up properly and
688 // do not overlap each other. 694 // do not overlap each other.
689 TEST(ScrollViewTest, CocoaOverlayScrollBars) { 695 TEST_F(ScrollViewTest, CocoaOverlayScrollBars) {
690 std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> 696 SetOverlayScrollersEnabled(true);
691 scroller_style_override; 697 View* contents = InstallContents();
692 scroller_style_override.reset(
693 new ui::test::ScopedPreferredScrollerStyle(true));
694 ScrollView scroll_view;
695 View* contents = new View;
696 scroll_view.SetContents(contents);
697 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
698 698
699 // Size the contents such that vertical scrollbar is needed. 699 // Size the contents such that vertical scrollbar is needed.
700 // Since it is overlaid, the ViewPort size should match the ScrollView. 700 // Since it is overlaid, the ViewPort size should match the ScrollView.
701 contents->SetBounds(0, 0, 50, 400); 701 contents->SetBounds(0, 0, 50, 400);
702 scroll_view.Layout(); 702 scroll_view_.Layout();
703 EXPECT_EQ(100, contents->parent()->width()); 703 EXPECT_EQ(100, contents->parent()->width());
704 EXPECT_EQ(100, contents->parent()->height()); 704 EXPECT_EQ(100, contents->parent()->height());
705 EXPECT_EQ(0, scroll_view.GetScrollBarWidth()); 705 EXPECT_EQ(0, scroll_view_.GetScrollBarWidth());
706 CheckScrollbarVisibility(scroll_view, VERTICAL, true); 706 CheckScrollbarVisibility(scroll_view_, VERTICAL, true);
707 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false); 707 CheckScrollbarVisibility(scroll_view_, HORIZONTAL, false);
708 708
709 // Size the contents such that horizontal scrollbar is needed. 709 // Size the contents such that horizontal scrollbar is needed.
710 contents->SetBounds(0, 0, 400, 50); 710 contents->SetBounds(0, 0, 400, 50);
711 scroll_view.Layout(); 711 scroll_view_.Layout();
712 EXPECT_EQ(100, contents->parent()->width()); 712 EXPECT_EQ(100, contents->parent()->width());
713 EXPECT_EQ(100, contents->parent()->height()); 713 EXPECT_EQ(100, contents->parent()->height());
714 EXPECT_EQ(0, scroll_view.GetScrollBarHeight()); 714 EXPECT_EQ(0, scroll_view_.GetScrollBarHeight());
715 CheckScrollbarVisibility(scroll_view, VERTICAL, false); 715 CheckScrollbarVisibility(scroll_view_, VERTICAL, false);
716 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); 716 CheckScrollbarVisibility(scroll_view_, HORIZONTAL, true);
717 717
718 // Both horizontal and vertical scrollbars. 718 // Both horizontal and vertical scrollbars.
719 contents->SetBounds(0, 0, 300, 400); 719 contents->SetBounds(0, 0, 300, 400);
720 scroll_view.Layout(); 720 scroll_view_.Layout();
721 EXPECT_EQ(100, contents->parent()->width()); 721 EXPECT_EQ(100, contents->parent()->width());
722 EXPECT_EQ(100, contents->parent()->height()); 722 EXPECT_EQ(100, contents->parent()->height());
723 EXPECT_EQ(0, scroll_view.GetScrollBarWidth()); 723 EXPECT_EQ(0, scroll_view_.GetScrollBarWidth());
724 EXPECT_EQ(0, scroll_view.GetScrollBarHeight()); 724 EXPECT_EQ(0, scroll_view_.GetScrollBarHeight());
725 CheckScrollbarVisibility(scroll_view, VERTICAL, true); 725 CheckScrollbarVisibility(scroll_view_, VERTICAL, true);
726 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); 726 CheckScrollbarVisibility(scroll_view_, HORIZONTAL, true);
727 727
728 // Make sure the horizontal and vertical scrollbars don't overlap each other. 728 // Make sure the horizontal and vertical scrollbars don't overlap each other.
729 gfx::Rect vert_bounds = scroll_view.vertical_scroll_bar()->bounds(); 729 gfx::Rect vert_bounds = scroll_view_.vertical_scroll_bar()->bounds();
730 gfx::Rect horiz_bounds = scroll_view.horizontal_scroll_bar()->bounds(); 730 gfx::Rect horiz_bounds = scroll_view_.horizontal_scroll_bar()->bounds();
731 EXPECT_EQ(vert_bounds.x(), horiz_bounds.right()); 731 EXPECT_EQ(vert_bounds.x(), horiz_bounds.right());
732 EXPECT_EQ(horiz_bounds.y(), vert_bounds.bottom()); 732 EXPECT_EQ(horiz_bounds.y(), vert_bounds.bottom());
733 733
734 // Switch to the non-overlay style and check that the ViewPort is now sized 734 // Switch to the non-overlay style and check that the ViewPort is now sized
735 // to be smaller, and ScrollbarWidth and ScrollbarHeight are non-zero. 735 // to be smaller, and ScrollbarWidth and ScrollbarHeight are non-zero.
736 scroller_style_override.reset( 736 SetOverlayScrollersEnabled(false);
737 new ui::test::ScopedPreferredScrollerStyle(false)); 737 EXPECT_EQ(100 - scroll_view_.GetScrollBarWidth(),
738 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); 738 contents->parent()->width());
739 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), 739 EXPECT_EQ(100 - scroll_view_.GetScrollBarHeight(),
740 contents->parent()->height()); 740 contents->parent()->height());
741 EXPECT_NE(0, scroll_view.GetScrollBarWidth()); 741 EXPECT_NE(0, scroll_view_.GetScrollBarWidth());
742 EXPECT_NE(0, scroll_view.GetScrollBarHeight()); 742 EXPECT_NE(0, scroll_view_.GetScrollBarHeight());
743 } 743 }
744 #endif 744 #endif
745 745
746 // Test that increasing the size of the viewport "below" scrolled content causes 746 // Test that increasing the size of the viewport "below" scrolled content causes
747 // the content to scroll up so that it still fills the viewport. 747 // the content to scroll up so that it still fills the viewport.
748 TEST(ScrollViewTest, ConstrainScrollToBounds) { 748 TEST_F(ScrollViewTest, ConstrainScrollToBounds) {
749 ScrollView scroll_view; 749 ScrollViewTestApi test_api(&scroll_view_);
750 ScrollViewTestApi test_api(&scroll_view);
751 750
752 View* contents = new View; 751 View* contents = InstallContents();
753 contents->SetBoundsRect(gfx::Rect(0, 0, 300, 300)); 752 contents->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
754 scroll_view.SetContents(contents); 753 scroll_view_.Layout();
755 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
756 scroll_view.Layout();
757 754
758 EXPECT_EQ(gfx::ScrollOffset(), test_api.CurrentOffset()); 755 EXPECT_EQ(gfx::ScrollOffset(), test_api.CurrentOffset());
759 756
760 // Scroll as far as it goes and query location to discount scroll bars. 757 // Scroll as far as it goes and query location to discount scroll bars.
761 contents->ScrollRectToVisible(gfx::Rect(300, 300, 1, 1)); 758 contents->ScrollRectToVisible(gfx::Rect(300, 300, 1, 1));
762 const gfx::ScrollOffset fully_scrolled = test_api.CurrentOffset(); 759 const gfx::ScrollOffset fully_scrolled = test_api.CurrentOffset();
763 EXPECT_NE(gfx::ScrollOffset(), fully_scrolled); 760 EXPECT_NE(gfx::ScrollOffset(), fully_scrolled);
764 761
765 // Making the viewport 55 pixels taller should scroll up the same amount. 762 // Making the viewport 55 pixels taller should scroll up the same amount.
766 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 155)); 763 scroll_view_.SetBoundsRect(gfx::Rect(0, 0, 100, 155));
767 scroll_view.Layout(); 764 scroll_view_.Layout();
768 EXPECT_EQ(fully_scrolled.y() - 55, test_api.CurrentOffset().y()); 765 EXPECT_EQ(fully_scrolled.y() - 55, test_api.CurrentOffset().y());
769 EXPECT_EQ(fully_scrolled.x(), test_api.CurrentOffset().x()); 766 EXPECT_EQ(fully_scrolled.x(), test_api.CurrentOffset().x());
770 767
771 // And 77 pixels wider should scroll left. Also make it short again: the y- 768 // And 77 pixels wider should scroll left. Also make it short again: the y-
772 // offset from the last change should remain. 769 // offset from the last change should remain.
773 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 177, 100)); 770 scroll_view_.SetBoundsRect(gfx::Rect(0, 0, 177, 100));
774 scroll_view.Layout(); 771 scroll_view_.Layout();
775 EXPECT_EQ(fully_scrolled.y() - 55, test_api.CurrentOffset().y()); 772 EXPECT_EQ(fully_scrolled.y() - 55, test_api.CurrentOffset().y());
776 EXPECT_EQ(fully_scrolled.x() - 77, test_api.CurrentOffset().x()); 773 EXPECT_EQ(fully_scrolled.x() - 77, test_api.CurrentOffset().x());
777 } 774 }
778 775
779 // Test scrolling behavior when clicking on the scroll track. 776 // Test scrolling behavior when clicking on the scroll track.
780 TEST_F(WidgetScrollViewTest, ScrollTrackScrolling) { 777 TEST_F(WidgetScrollViewTest, ScrollTrackScrolling) {
781 // Set up with a vertical scroller. 778 // Set up with a vertical scroller.
782 ScrollView* scroll_view = 779 ScrollView* scroll_view_ =
783 AddScrollViewWithContentSize(gfx::Size(10, kDefaultHeight * 5)); 780 AddScrollViewWithContentSize(gfx::Size(10, kDefaultHeight * 5));
784 ScrollViewTestApi test_api(scroll_view); 781 ScrollViewTestApi test_api(scroll_view_);
785 BaseScrollBar* scroll_bar = test_api.GetBaseScrollBar(VERTICAL); 782 BaseScrollBar* scroll_bar = test_api.GetBaseScrollBar(VERTICAL);
786 View* thumb = test_api.GetScrollBarThumb(VERTICAL); 783 View* thumb = test_api.GetScrollBarThumb(VERTICAL);
787 784
788 // Click in the middle of the track, ensuring it's below the thumb. 785 // Click in the middle of the track, ensuring it's below the thumb.
789 const gfx::Point location = scroll_bar->bounds().CenterPoint(); 786 const gfx::Point location = scroll_bar->bounds().CenterPoint();
790 EXPECT_GT(location.y(), thumb->bounds().bottom()); 787 EXPECT_GT(location.y(), thumb->bounds().bottom());
791 ui::MouseEvent press(TestLeftMouseAt(location, ui::ET_MOUSE_PRESSED)); 788 ui::MouseEvent press(TestLeftMouseAt(location, ui::ET_MOUSE_PRESSED));
792 ui::MouseEvent release(TestLeftMouseAt(location, ui::ET_MOUSE_RELEASED)); 789 ui::MouseEvent release(TestLeftMouseAt(location, ui::ET_MOUSE_RELEASED));
793 790
794 const base::Timer& timer = test_api.GetScrollBarTimer(VERTICAL); 791 const base::Timer& timer = test_api.GetScrollBarTimer(VERTICAL);
795 EXPECT_FALSE(timer.IsRunning()); 792 EXPECT_FALSE(timer.IsRunning());
796 793
797 EXPECT_EQ(0, scroll_view->GetVisibleRect().y()); 794 EXPECT_EQ(0, scroll_view_->GetVisibleRect().y());
798 scroll_bar->OnMouseEvent(&press); 795 scroll_bar->OnMouseEvent(&press);
799 796
800 // Clicking the scroll track should scroll one "page". 797 // Clicking the scroll track should scroll one "page".
801 EXPECT_EQ(kDefaultHeight, scroll_view->GetVisibleRect().y()); 798 EXPECT_EQ(kDefaultHeight, scroll_view_->GetVisibleRect().y());
802 799
803 // While the mouse is pressed, timer should trigger more scroll events. 800 // While the mouse is pressed, timer should trigger more scroll events.
804 EXPECT_TRUE(timer.IsRunning()); 801 EXPECT_TRUE(timer.IsRunning());
805 802
806 // Upon release timer should stop (and scroll position should remain). 803 // Upon release timer should stop (and scroll position should remain).
807 scroll_bar->OnMouseEvent(&release); 804 scroll_bar->OnMouseEvent(&release);
808 EXPECT_FALSE(timer.IsRunning()); 805 EXPECT_FALSE(timer.IsRunning());
809 EXPECT_EQ(kDefaultHeight, scroll_view->GetVisibleRect().y()); 806 EXPECT_EQ(kDefaultHeight, scroll_view_->GetVisibleRect().y());
810 } 807 }
811 808
812 // Test that LocatedEvents are transformed correctly when scrolling. 809 // Test that LocatedEvents are transformed correctly when scrolling.
813 TEST_F(WidgetScrollViewTest, EventLocation) { 810 TEST_F(WidgetScrollViewTest, EventLocation) {
814 // Set up with both scrollers. 811 // Set up with both scrollers.
815 CustomView* contents = new CustomView; 812 CustomView* contents = new CustomView;
816 contents->SetPreferredSize(gfx::Size(kDefaultHeight * 5, kDefaultHeight * 5)); 813 contents->SetPreferredSize(gfx::Size(kDefaultHeight * 5, kDefaultHeight * 5));
817 AddScrollViewWithContents(contents); 814 AddScrollViewWithContents(contents);
818 815
819 const gfx::Point location_in_widget(10, 10); 816 const gfx::Point location_in_widget(10, 10);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 // Scroll via ScrollView API. Should be reflected on the impl side. 891 // Scroll via ScrollView API. Should be reflected on the impl side.
895 offset.set_y(kDefaultHeight * 4); 892 offset.set_y(kDefaultHeight * 4);
896 scroll_view->contents()->ScrollRectToVisible(offset); 893 scroll_view->contents()->ScrollRectToVisible(offset);
897 EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset()); 894 EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset());
898 895
899 EXPECT_TRUE(compositor->GetScrollOffsetForLayer(layer_id, &impl_offset)); 896 EXPECT_TRUE(compositor->GetScrollOffsetForLayer(layer_id, &impl_offset));
900 EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), impl_offset); 897 EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), impl_offset);
901 } 898 }
902 899
903 } // namespace views 900 } // namespace views
OLDNEW
« no previous file with comments | « ui/base/test/scoped_preferred_scroller_style_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698