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

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

Issue 1671313002: MacViews: Overlay Scrollbars with Show/Hide Animations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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
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)
14 #include "ui/base/test/scoped_preferred_scroller_style_mac.h"
15 #endif
16
13 namespace views { 17 namespace views {
14 18
15 namespace { 19 namespace {
16 20
17 const int kWidth = 100; 21 const int kWidth = 100;
18 const int kMinHeight = 50; 22 const int kMinHeight = 50;
19 const int kMaxHeight = 100; 23 const int kMaxHeight = 100;
20 24
25 enum ScrollBarOrientation { HORIZONTAL, VERTICAL };
tapted 2016/02/19 06:56:37 `enum` -> `enum class`? (otherwise, I think HORIZO
spqchan 2016/02/19 19:35:32 True, I removed the prefix. Is it okay if I stick
26
21 // View implementation that allows setting the preferred size. 27 // View implementation that allows setting the preferred size.
22 class CustomView : public View { 28 class CustomView : public View {
23 public: 29 public:
24 CustomView() {} 30 CustomView() {}
25 31
26 void SetPreferredSize(const gfx::Size& size) { 32 void SetPreferredSize(const gfx::Size& size) {
27 preferred_size_ = size; 33 preferred_size_ = size;
28 PreferredSizeChanged(); 34 PreferredSizeChanged();
29 } 35 }
30 36
31 gfx::Size GetPreferredSize() const override { return preferred_size_; } 37 gfx::Size GetPreferredSize() const override { return preferred_size_; }
32 38
33 void Layout() override { 39 void Layout() override {
34 gfx::Size pref = GetPreferredSize(); 40 gfx::Size pref = GetPreferredSize();
35 int width = pref.width(); 41 int width = pref.width();
36 int height = pref.height(); 42 int height = pref.height();
37 if (parent()) { 43 if (parent()) {
38 width = std::max(parent()->width(), width); 44 width = std::max(parent()->width(), width);
39 height = std::max(parent()->height(), height); 45 height = std::max(parent()->height(), height);
40 } 46 }
41 SetBounds(x(), y(), width, height); 47 SetBounds(x(), y(), width, height);
42 } 48 }
43 49
44 private: 50 private:
45 gfx::Size preferred_size_; 51 gfx::Size preferred_size_;
46 52
47 DISALLOW_COPY_AND_ASSIGN(CustomView); 53 DISALLOW_COPY_AND_ASSIGN(CustomView);
48 }; 54 };
49 55
56 void CheckScrollbarVisibility(ScrollView& scroll_view,
tapted 2016/02/19 06:56:37 does const-reference work? (otherwise ScrollView*
spqchan 2016/02/19 19:35:32 Done.
57 ScrollBarOrientation orientation,
58 bool is_visible) {
tapted 2016/02/19 06:56:37 maybe is_visible -> should_be_visible?
spqchan 2016/02/19 19:35:32 Done.
59 const ScrollBar* scrollbar = orientation == ScrollBarOrientation::HORIZONTAL
60 ? scroll_view.horizontal_scroll_bar()
61 : scroll_view.vertical_scroll_bar();
62 if (is_visible) {
63 ASSERT_TRUE(scrollbar != NULL);
tapted 2016/02/19 06:56:37 typically I just see ASSERT_TRUE(scrollbar); for n
spqchan 2016/02/19 19:35:32 Done.
64 EXPECT_TRUE(scrollbar->visible());
65 } else {
66 EXPECT_TRUE(!scrollbar || !scrollbar->visible());
67 }
68 }
69
50 } // namespace 70 } // namespace
51 71
52 // Verifies the viewport is sized to fit the available space. 72 // Verifies the viewport is sized to fit the available space.
53 TEST(ScrollViewTest, ViewportSizedToFit) { 73 TEST(ScrollViewTest, ViewportSizedToFit) {
54 ScrollView scroll_view; 74 ScrollView scroll_view;
55 View* contents = new View; 75 View* contents = new View;
56 scroll_view.SetContents(contents); 76 scroll_view.SetContents(contents);
57 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 77 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
58 scroll_view.Layout(); 78 scroll_view.Layout();
59 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); 79 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString());
60 } 80 }
61 81
62 // Verifies the scrollbars are added as necessary. 82 // Verifies the scrollbars are added as necessary.
83 // If on Mac, test the legacy scrollbars.
tapted 2016/02/19 06:56:37 nit: legacy -> non-overlay style
spqchan 2016/02/19 19:35:32 Done.
63 TEST(ScrollViewTest, ScrollBars) { 84 TEST(ScrollViewTest, ScrollBars) {
85 #if defined(OS_MACOSX)
86 ui::test::ScopedPreferredScrollerStyle scroller_style_override(false);
87 #endif
88
64 ScrollView scroll_view; 89 ScrollView scroll_view;
65 View* contents = new View; 90 View* contents = new View;
66 scroll_view.SetContents(contents); 91 scroll_view.SetContents(contents);
67 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); 92 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
68 93
69 // Size the contents such that vertical scrollbar is needed. 94 // Size the contents such that vertical scrollbar is needed.
70 contents->SetBounds(0, 0, 50, 400); 95 contents->SetBounds(0, 0, 50, 400);
71 scroll_view.Layout(); 96 scroll_view.Layout();
72 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); 97 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width());
73 EXPECT_EQ(100, contents->parent()->height()); 98 EXPECT_EQ(100, contents->parent()->height());
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 scroll_view.Layout(); 467 scroll_view.Layout();
443 EXPECT_FALSE(corner_view->parent()); 468 EXPECT_FALSE(corner_view->parent());
444 469
445 // Corner view should reappear when both scrollbars reappear. 470 // Corner view should reappear when both scrollbars reappear.
446 contents->SetBounds(0, 0, 200, 200); 471 contents->SetBounds(0, 0, 200, 200);
447 scroll_view.Layout(); 472 scroll_view.Layout();
448 EXPECT_EQ(&scroll_view, corner_view->parent()); 473 EXPECT_EQ(&scroll_view, corner_view->parent());
449 EXPECT_TRUE(corner_view->visible()); 474 EXPECT_TRUE(corner_view->visible());
450 } 475 }
451 476
477 #if defined(OS_MACOSX)
478 // Tests the overlay scrollbars on Mac. Ensure that they show up properly and
479 // do not overlap each other.
480 TEST(ScrollViewTest, CocoaOverlayScrollBars) {
481 ui::test::ScopedPreferredScrollerStyle scroller_style_override(true);
482 ScrollView scroll_view;
483 View* contents = new View;
484 scroll_view.SetContents(contents);
485 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
486
487 // Size the contents such that vertical scrollbar is needed.
488 contents->SetBounds(0, 0, 50, 400);
489 scroll_view.Layout();
490 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width());
tapted 2016/02/19 06:56:37 should scroll_view.GetScrollBarWidth() be zero for
spqchan 2016/02/19 19:35:32 Done.
491 EXPECT_EQ(100, contents->parent()->height());
492 CheckScrollbarVisibility(scroll_view, ScrollBarOrientation::VERTICAL, true);
493 CheckScrollbarVisibility(scroll_view, ScrollBarOrientation::HORIZONTAL,
494 false);
495
496 // Size the contents such that horizontal scrollbar is needed.
497 contents->SetBounds(0, 0, 400, 50);
498 scroll_view.Layout();
499 CheckScrollbarVisibility(scroll_view, ScrollBarOrientation::VERTICAL, false);
500 CheckScrollbarVisibility(scroll_view, ScrollBarOrientation::HORIZONTAL, true);
501
502 // Both horizontal and vertical scrollbars.
503 contents->SetBounds(0, 0, 300, 400);
504 scroll_view.Layout();
505 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width());
506 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(),
507 contents->parent()->height());
508 CheckScrollbarVisibility(scroll_view, ScrollBarOrientation::VERTICAL, true);
509 CheckScrollbarVisibility(scroll_view, ScrollBarOrientation::HORIZONTAL, true);
510
511 // Make sure the horizontal and vertical scrollbars don't overlap each other.
512 gfx::Rect vert_bounds = scroll_view.vertical_scroll_bar()->bounds();
513 gfx::Rect horiz_bounds = scroll_view.horizontal_scroll_bar()->bounds();
514 EXPECT_EQ(vert_bounds.x(), horiz_bounds.right());
515 EXPECT_EQ(horiz_bounds.y(), vert_bounds.bottom());
516 }
tapted 2016/02/19 06:56:37 I think a good test would be to (here) make scroll
spqchan 2016/02/19 19:35:32 Done.
517 #endif
518
452 } // namespace views 519 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698