| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "views/controls/scrollbar/scroll_bar.h" | |
| 6 #include "views/controls/scrollbar/native_scroll_bar.h" | |
| 7 #include "views/controls/scrollbar/native_scroll_bar_views.h" | |
| 8 #include "views/test/views_test_base.h" | |
| 9 #include "views/widget/widget.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 // The Scrollbar controller. This is the widget that should do the real | |
| 14 // scrolling of contents. | |
| 15 class TestScrollBarController : public views::ScrollBarController { | |
| 16 public: | |
| 17 virtual void ScrollToPosition(views::ScrollBar* source, | |
| 18 int position) OVERRIDE { | |
| 19 last_source = source; | |
| 20 last_position = position; | |
| 21 } | |
| 22 | |
| 23 virtual int GetScrollIncrement(views::ScrollBar* source, | |
| 24 bool is_page, | |
| 25 bool is_positive) OVERRIDE { | |
| 26 last_source = source; | |
| 27 last_is_page = is_page; | |
| 28 last_is_positive = is_positive; | |
| 29 | |
| 30 if (is_page) | |
| 31 return 20; | |
| 32 return 10; | |
| 33 } | |
| 34 | |
| 35 // We save the last values in order to assert the corectness of the scroll | |
| 36 // operation. | |
| 37 views::ScrollBar* last_source; | |
| 38 bool last_is_positive; | |
| 39 bool last_is_page; | |
| 40 int last_position; | |
| 41 }; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 namespace views { | |
| 46 | |
| 47 class NativeScrollBarTest : public ViewsTestBase { | |
| 48 public: | |
| 49 NativeScrollBarTest() | |
| 50 : widget_(NULL), | |
| 51 scrollbar_(NULL), | |
| 52 controller_(NULL) { | |
| 53 } | |
| 54 | |
| 55 virtual void SetUp() { | |
| 56 ViewsTestBase::SetUp(); | |
| 57 Widget::SetPureViews(true); | |
| 58 } | |
| 59 | |
| 60 virtual void TearDown() { | |
| 61 Widget::SetPureViews(false); | |
| 62 if (widget_) | |
| 63 widget_->Close(); | |
| 64 ViewsTestBase::TearDown(); | |
| 65 } | |
| 66 | |
| 67 void InitScrollBar() { | |
| 68 controller_.reset(new TestScrollBarController()); | |
| 69 | |
| 70 ASSERT_FALSE(scrollbar_); | |
| 71 native_scrollbar_ = new NativeScrollBar(true); | |
| 72 native_scrollbar_->SetBounds(0, 0, 100, 100); | |
| 73 scrollbar_ = new NativeScrollBarViews(native_scrollbar_); | |
| 74 scrollbar_->SetController(controller_.get()); | |
| 75 | |
| 76 widget_ = new Widget; | |
| 77 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | |
| 78 params.bounds = gfx::Rect(0, 0, 100, 100); | |
| 79 widget_->Init(params); | |
| 80 View* container = new View(); | |
| 81 widget_->SetContentsView(container); | |
| 82 container->AddChildView(scrollbar_); | |
| 83 scrollbar_->SetBounds(0, 0, 100, 100); | |
| 84 scrollbar_->Update(100, 200, 0); | |
| 85 | |
| 86 track_size_ = scrollbar_->GetTrackBounds().width(); | |
| 87 } | |
| 88 | |
| 89 protected: | |
| 90 Widget* widget_; | |
| 91 | |
| 92 // This is the native scrollbar the Views one wraps around. | |
| 93 NativeScrollBar* native_scrollbar_; | |
| 94 | |
| 95 // This is the Views scrollbar. | |
| 96 BaseScrollBar* scrollbar_; | |
| 97 | |
| 98 // Keep track of the size of the track. This is how we can tell when we | |
| 99 // scroll to the middle. | |
| 100 int track_size_; | |
| 101 | |
| 102 scoped_ptr<TestScrollBarController> controller_; | |
| 103 }; | |
| 104 | |
| 105 // TODO(dnicoara) Can't run the test on Windows since the scrollbar |Part| | |
| 106 // isn't handled in NativeTheme. | |
| 107 #if defined(OS_WIN) | |
| 108 TEST_F(NativeScrollBarTest, DISABLED_Scrolling) { | |
| 109 #else | |
| 110 TEST_F(NativeScrollBarTest, Scrolling) { | |
| 111 #endif | |
| 112 InitScrollBar(); | |
| 113 EXPECT_EQ(scrollbar_->GetPosition(), 0); | |
| 114 EXPECT_EQ(scrollbar_->GetMaxPosition(), 100); | |
| 115 EXPECT_EQ(scrollbar_->GetMinPosition(), 0); | |
| 116 | |
| 117 // Scroll to middle. | |
| 118 scrollbar_->ScrollToThumbPosition(track_size_ / 4, false); | |
| 119 EXPECT_EQ(controller_->last_position, 50); | |
| 120 EXPECT_EQ(controller_->last_source, native_scrollbar_); | |
| 121 | |
| 122 // Scroll to the end. | |
| 123 scrollbar_->ScrollToThumbPosition(track_size_ / 2, false); | |
| 124 EXPECT_EQ(controller_->last_position, 100); | |
| 125 | |
| 126 // Overscroll. Last position should be the maximum position. | |
| 127 scrollbar_->ScrollToThumbPosition(track_size_, false); | |
| 128 EXPECT_EQ(controller_->last_position, 100); | |
| 129 | |
| 130 // Underscroll. Last position should be the minimum position. | |
| 131 scrollbar_->ScrollToThumbPosition(-10, false); | |
| 132 EXPECT_EQ(controller_->last_position, 0); | |
| 133 | |
| 134 // Test the different fixed scrolling amounts. Generally used by buttons, | |
| 135 // or click on track. | |
| 136 scrollbar_->ScrollToThumbPosition(0, false); | |
| 137 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_LINE); | |
| 138 EXPECT_EQ(controller_->last_position, 10); | |
| 139 | |
| 140 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_PREV_LINE); | |
| 141 EXPECT_EQ(controller_->last_position, 0); | |
| 142 | |
| 143 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_NEXT_PAGE); | |
| 144 EXPECT_EQ(controller_->last_position, 20); | |
| 145 | |
| 146 scrollbar_->ScrollByAmount(BaseScrollBar::SCROLL_PREV_PAGE); | |
| 147 EXPECT_EQ(controller_->last_position, 0); | |
| 148 } | |
| 149 | |
| 150 } // namespace views | |
| OLD | NEW |