| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "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" |
| 9 #include "base/test/test_timeouts.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/views/border.h" | 12 #include "ui/views/border.h" |
| 10 #include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h" | 13 #include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h" |
| 11 #include "ui/views/controls/scrollbar/native_scroll_bar.h" | 14 #include "ui/views/controls/scrollbar/native_scroll_bar.h" |
| 12 #include "ui/views/controls/scrollbar/native_scroll_bar_views.h" | 15 #include "ui/views/controls/scrollbar/native_scroll_bar_views.h" |
| 13 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h" | 16 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h" |
| 14 #include "ui/views/test/test_views.h" | 17 #include "ui/views/test/test_views.h" |
| 15 #include "ui/views/test/widget_test.h" | 18 #include "ui/views/test/widget_test.h" |
| 16 | 19 |
| 17 #if defined(OS_MACOSX) | 20 #if defined(OS_MACOSX) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 39 } | 42 } |
| 40 | 43 |
| 41 const base::Timer& GetScrollBarTimer(ScrollBarOrientation orientation) { | 44 const base::Timer& GetScrollBarTimer(ScrollBarOrientation orientation) { |
| 42 return GetBaseScrollBar(orientation)->repeater_.timer_for_testing(); | 45 return GetBaseScrollBar(orientation)->repeater_.timer_for_testing(); |
| 43 } | 46 } |
| 44 | 47 |
| 45 BaseScrollBarThumb* GetScrollBarThumb(ScrollBarOrientation orientation) { | 48 BaseScrollBarThumb* GetScrollBarThumb(ScrollBarOrientation orientation) { |
| 46 return GetBaseScrollBar(orientation)->thumb_; | 49 return GetBaseScrollBar(orientation)->thumb_; |
| 47 } | 50 } |
| 48 | 51 |
| 52 gfx::Point IntegralViewOffset() { |
| 53 return gfx::Point() - gfx::ScrollOffsetToFlooredVector2d(CurrentOffset()); |
| 54 } |
| 55 |
| 56 gfx::ScrollOffset CurrentOffset() { return scroll_view_->CurrentOffset(); } |
| 57 |
| 49 View* corner_view() { return scroll_view_->corner_view_; } | 58 View* corner_view() { return scroll_view_->corner_view_; } |
| 59 View* contents_viewport() { return scroll_view_->contents_viewport_; } |
| 60 View* contents_container() { return scroll_view_->contents_container_; } |
| 50 | 61 |
| 51 private: | 62 private: |
| 52 ScrollView* scroll_view_; | 63 ScrollView* scroll_view_; |
| 53 | 64 |
| 54 DISALLOW_COPY_AND_ASSIGN(ScrollViewTestApi); | 65 DISALLOW_COPY_AND_ASSIGN(ScrollViewTestApi); |
| 55 }; | 66 }; |
| 56 | 67 |
| 57 } // namespace test | 68 } // namespace test |
| 58 | 69 |
| 59 namespace { | 70 namespace { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 ui::MouseEvent TestLeftMouseAt(const gfx::Point& location, ui::EventType type) { | 119 ui::MouseEvent TestLeftMouseAt(const gfx::Point& location, ui::EventType type) { |
| 109 return ui::MouseEvent(type, location, location, base::TimeTicks(), | 120 return ui::MouseEvent(type, location, location, base::TimeTicks(), |
| 110 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); | 121 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| 111 } | 122 } |
| 112 | 123 |
| 113 } // namespace | 124 } // namespace |
| 114 | 125 |
| 115 using test::ScrollViewTestApi; | 126 using test::ScrollViewTestApi; |
| 116 | 127 |
| 117 // Test harness that includes a Widget to help test ui::Event handling. | 128 // Test harness that includes a Widget to help test ui::Event handling. |
| 118 class WidgetScrollViewTest : public test::WidgetTest { | 129 class WidgetScrollViewTest : public test::WidgetTest, |
| 130 public ui::CompositorObserver { |
| 119 public: | 131 public: |
| 120 static const int kDefaultHeight = 100; | 132 static const int kDefaultHeight = 100; |
| 121 static const int kDefaultWidth = 100; | 133 static const int kDefaultWidth = 100; |
| 122 | 134 |
| 123 WidgetScrollViewTest() { | 135 WidgetScrollViewTest() { |
| 124 #if defined(OS_MACOSX) | 136 #if defined(OS_MACOSX) |
| 125 // Disable scrollbar hiding (i.e. disable overlay scrollbars) by default. | 137 // Disable scrollbar hiding (i.e. disable overlay scrollbars) by default. |
| 126 scroller_style_.reset(new ui::test::ScopedPreferredScrollerStyle(false)); | 138 scroller_style_.reset(new ui::test::ScopedPreferredScrollerStyle(false)); |
| 127 #endif | 139 #endif |
| 128 } | 140 } |
| 129 | 141 |
| 130 // Adds a ScrollView with a contents view of the given |size| and does layout. | 142 // Adds a ScrollView with a contents view of the given |size| and does layout. |
| 131 ScrollView* AddScrollViewWithContentSize(const gfx::Size& contents_size) { | 143 ScrollView* AddScrollViewWithContentSize(const gfx::Size& contents_size, |
| 144 bool commit_layers = true) { |
| 132 const gfx::Rect default_bounds(50, 50, kDefaultWidth, kDefaultHeight); | 145 const gfx::Rect default_bounds(50, 50, kDefaultWidth, kDefaultHeight); |
| 133 widget_ = CreateTopLevelFramelessPlatformWidget(); | 146 widget_ = CreateTopLevelFramelessPlatformWidget(); |
| 134 | 147 |
| 135 ScrollView* scroll_view = new ScrollView(); | 148 ScrollView* scroll_view = new ScrollView(); |
| 136 View* contents = new View; | 149 View* contents = new View; |
| 137 scroll_view->SetContents(contents); | 150 scroll_view->SetContents(contents); |
| 138 contents->SetSize(contents_size); | 151 contents->SetSize(contents_size); |
| 139 | 152 |
| 140 widget_->SetBounds(default_bounds); | 153 widget_->SetBounds(default_bounds); |
| 141 widget_->Show(); | 154 widget_->Show(); |
| 142 | 155 |
| 143 widget_->SetContentsView(scroll_view); | 156 widget_->SetContentsView(scroll_view); |
| 144 scroll_view->Layout(); | 157 scroll_view->Layout(); |
| 158 |
| 159 widget_->GetCompositor()->AddObserver(this); |
| 160 |
| 161 // Ensure the Compositor has committed layer changes before attempting to |
| 162 // use them for impl-side scrolling. Note that simply RunUntilIdle() works |
| 163 // when tests are run in isolation, but compositor scheduling can interact |
| 164 // between test runs in the general case. |
| 165 if (commit_layers) |
| 166 WaitForCommit(); |
| 145 return scroll_view; | 167 return scroll_view; |
| 146 } | 168 } |
| 147 | 169 |
| 170 // Wait for a commit to be observed on the compositor. |
| 171 void WaitForCommit() { |
| 172 base::RunLoop run_loop; |
| 173 quit_closure_ = run_loop.QuitClosure(); |
| 174 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 175 FROM_HERE, quit_closure_, TestTimeouts::action_timeout()); |
| 176 run_loop.Run(); |
| 177 EXPECT_TRUE(quit_closure_.is_null()) << "Timed out waiting for a commit."; |
| 178 } |
| 179 |
| 148 // testing::Test: | 180 // testing::Test: |
| 149 void TearDown() override { | 181 void TearDown() override { |
| 182 widget_->GetCompositor()->RemoveObserver(this); |
| 150 if (widget_) | 183 if (widget_) |
| 151 widget_->CloseNow(); | 184 widget_->CloseNow(); |
| 152 WidgetTest::TearDown(); | 185 WidgetTest::TearDown(); |
| 153 } | 186 } |
| 154 | 187 |
| 155 private: | 188 private: |
| 189 // ui::CompositorObserver: |
| 190 void OnCompositingDidCommit(ui::Compositor* compositor) override { |
| 191 quit_closure_.Run(); |
| 192 quit_closure_.Reset(); |
| 193 } |
| 194 void OnCompositingStarted(ui::Compositor* compositor, |
| 195 base::TimeTicks start_time) override {} |
| 196 void OnCompositingEnded(ui::Compositor* compositor) override {} |
| 197 void OnCompositingAborted(ui::Compositor* compositor) override {} |
| 198 void OnCompositingLockStateChanged(ui::Compositor* compositor) override {} |
| 199 void OnCompositingShuttingDown(ui::Compositor* compositor) override {} |
| 200 |
| 156 Widget* widget_ = nullptr; | 201 Widget* widget_ = nullptr; |
| 157 | 202 |
| 203 base::Closure quit_closure_; |
| 204 |
| 158 #if defined(OS_MACOSX) | 205 #if defined(OS_MACOSX) |
| 159 std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> scroller_style_; | 206 std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> scroller_style_; |
| 160 #endif | 207 #endif |
| 161 | 208 |
| 162 DISALLOW_COPY_AND_ASSIGN(WidgetScrollViewTest); | 209 DISALLOW_COPY_AND_ASSIGN(WidgetScrollViewTest); |
| 163 }; | 210 }; |
| 164 | 211 |
| 165 const int WidgetScrollViewTest::kDefaultHeight; | 212 const int WidgetScrollViewTest::kDefaultHeight; |
| 166 const int WidgetScrollViewTest::kDefaultWidth; | 213 const int WidgetScrollViewTest::kDefaultWidth; |
| 167 | 214 |
| 168 // Verifies the viewport is sized to fit the available space. | 215 // Verifies the viewport is sized to fit the available space. |
| 169 TEST(ScrollViewTest, ViewportSizedToFit) { | 216 TEST(ScrollViewTest, ViewportSizedToFit) { |
| 170 ScrollView scroll_view; | 217 ScrollView scroll_view; |
| 218 ScrollViewTestApi test_api(&scroll_view); |
| 171 View* contents = new View; | 219 View* contents = new View; |
| 172 scroll_view.SetContents(contents); | 220 scroll_view.SetContents(contents); |
| 173 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); | 221 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); |
| 174 scroll_view.Layout(); | 222 scroll_view.Layout(); |
| 175 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); | 223 EXPECT_EQ("0,0 100x100", test_api.contents_viewport()->bounds().ToString()); |
| 176 } | 224 } |
| 177 | 225 |
| 178 // Verifies the scrollbars are added as necessary. | 226 // Verifies the scrollbars are added as necessary. |
| 179 // If on Mac, test the non-overlay scrollbars. | 227 // If on Mac, test the non-overlay scrollbars. |
| 180 TEST(ScrollViewTest, ScrollBars) { | 228 TEST(ScrollViewTest, ScrollBars) { |
| 181 #if defined(OS_MACOSX) | 229 #if defined(OS_MACOSX) |
| 182 ui::test::ScopedPreferredScrollerStyle scroller_style_override(false); | 230 ui::test::ScopedPreferredScrollerStyle scroller_style_override(false); |
| 183 #endif | 231 #endif |
| 184 | 232 |
| 185 ScrollView scroll_view; | 233 ScrollView scroll_view; |
| 234 ScrollViewTestApi test_api(&scroll_view); |
| 186 View* contents = new View; | 235 View* contents = new View; |
| 187 scroll_view.SetContents(contents); | 236 scroll_view.SetContents(contents); |
| 188 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); | 237 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); |
| 189 | 238 |
| 190 // Size the contents such that vertical scrollbar is needed. | 239 // Size the contents such that vertical scrollbar is needed. |
| 191 contents->SetBounds(0, 0, 50, 400); | 240 contents->SetBounds(0, 0, 50, 400); |
| 192 scroll_view.Layout(); | 241 scroll_view.Layout(); |
| 193 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); | 242 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), |
| 194 EXPECT_EQ(100, contents->parent()->height()); | 243 test_api.contents_viewport()->width()); |
| 244 EXPECT_EQ(100, test_api.contents_viewport()->height()); |
| 195 CheckScrollbarVisibility(scroll_view, VERTICAL, true); | 245 CheckScrollbarVisibility(scroll_view, VERTICAL, true); |
| 196 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false); | 246 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false); |
| 197 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || | 247 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || |
| 198 !scroll_view.horizontal_scroll_bar()->visible()); | 248 !scroll_view.horizontal_scroll_bar()->visible()); |
| 199 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); | 249 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); |
| 200 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); | 250 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); |
| 201 | 251 |
| 202 // Size the contents such that horizontal scrollbar is needed. | 252 // Size the contents such that horizontal scrollbar is needed. |
| 203 contents->SetBounds(0, 0, 400, 50); | 253 contents->SetBounds(0, 0, 400, 50); |
| 204 scroll_view.Layout(); | 254 scroll_view.Layout(); |
| 205 EXPECT_EQ(100, contents->parent()->width()); | 255 EXPECT_EQ(100, test_api.contents_viewport()->width()); |
| 206 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), | 256 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), |
| 207 contents->parent()->height()); | 257 test_api.contents_viewport()->height()); |
| 208 CheckScrollbarVisibility(scroll_view, VERTICAL, false); | 258 CheckScrollbarVisibility(scroll_view, VERTICAL, false); |
| 209 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); | 259 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); |
| 210 | 260 |
| 211 // Both horizontal and vertical. | 261 // Both horizontal and vertical. |
| 212 contents->SetBounds(0, 0, 300, 400); | 262 contents->SetBounds(0, 0, 300, 400); |
| 213 scroll_view.Layout(); | 263 scroll_view.Layout(); |
| 214 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); | 264 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), |
| 265 test_api.contents_viewport()->width()); |
| 215 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), | 266 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), |
| 216 contents->parent()->height()); | 267 test_api.contents_viewport()->height()); |
| 217 CheckScrollbarVisibility(scroll_view, VERTICAL, true); | 268 CheckScrollbarVisibility(scroll_view, VERTICAL, true); |
| 218 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); | 269 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); |
| 219 | 270 |
| 220 // Add a border, test vertical scrollbar. | 271 // Add a border, test vertical scrollbar. |
| 221 const int kTopPadding = 1; | 272 const int kTopPadding = 1; |
| 222 const int kLeftPadding = 2; | 273 const int kLeftPadding = 2; |
| 223 const int kBottomPadding = 3; | 274 const int kBottomPadding = 3; |
| 224 const int kRightPadding = 4; | 275 const int kRightPadding = 4; |
| 225 scroll_view.SetBorder(Border::CreateEmptyBorder( | 276 scroll_view.SetBorder(Border::CreateEmptyBorder( |
| 226 kTopPadding, kLeftPadding, kBottomPadding, kRightPadding)); | 277 kTopPadding, kLeftPadding, kBottomPadding, kRightPadding)); |
| 227 contents->SetBounds(0, 0, 50, 400); | 278 contents->SetBounds(0, 0, 50, 400); |
| 228 scroll_view.Layout(); | 279 scroll_view.Layout(); |
| 229 EXPECT_EQ( | 280 EXPECT_EQ( |
| 230 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding, | 281 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding, |
| 231 contents->parent()->width()); | 282 test_api.contents_viewport()->width()); |
| 232 EXPECT_EQ(100 - kTopPadding - kBottomPadding, contents->parent()->height()); | 283 EXPECT_EQ(100 - kTopPadding - kBottomPadding, |
| 284 test_api.contents_viewport()->height()); |
| 233 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || | 285 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || |
| 234 !scroll_view.horizontal_scroll_bar()->visible()); | 286 !scroll_view.horizontal_scroll_bar()->visible()); |
| 235 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); | 287 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); |
| 236 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); | 288 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); |
| 237 gfx::Rect bounds = scroll_view.vertical_scroll_bar()->bounds(); | 289 gfx::Rect bounds = scroll_view.vertical_scroll_bar()->bounds(); |
| 238 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth() - kRightPadding, bounds.x()); | 290 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth() - kRightPadding, bounds.x()); |
| 239 EXPECT_EQ(100 - kRightPadding, bounds.right()); | 291 EXPECT_EQ(100 - kRightPadding, bounds.right()); |
| 240 EXPECT_EQ(kTopPadding, bounds.y()); | 292 EXPECT_EQ(kTopPadding, bounds.y()); |
| 241 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); | 293 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); |
| 242 | 294 |
| 243 // Horizontal with border. | 295 // Horizontal with border. |
| 244 contents->SetBounds(0, 0, 400, 50); | 296 contents->SetBounds(0, 0, 400, 50); |
| 245 scroll_view.Layout(); | 297 scroll_view.Layout(); |
| 246 EXPECT_EQ(100 - kLeftPadding - kRightPadding, contents->parent()->width()); | 298 EXPECT_EQ(100 - kLeftPadding - kRightPadding, |
| 299 test_api.contents_viewport()->width()); |
| 247 EXPECT_EQ( | 300 EXPECT_EQ( |
| 248 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding, | 301 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding, |
| 249 contents->parent()->height()); | 302 test_api.contents_viewport()->height()); |
| 250 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); | 303 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); |
| 251 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); | 304 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); |
| 252 EXPECT_TRUE(!scroll_view.vertical_scroll_bar() || | 305 EXPECT_TRUE(!scroll_view.vertical_scroll_bar() || |
| 253 !scroll_view.vertical_scroll_bar()->visible()); | 306 !scroll_view.vertical_scroll_bar()->visible()); |
| 254 bounds = scroll_view.horizontal_scroll_bar()->bounds(); | 307 bounds = scroll_view.horizontal_scroll_bar()->bounds(); |
| 255 EXPECT_EQ(kLeftPadding, bounds.x()); | 308 EXPECT_EQ(kLeftPadding, bounds.x()); |
| 256 EXPECT_EQ(100 - kRightPadding, bounds.right()); | 309 EXPECT_EQ(100 - kRightPadding, bounds.right()); |
| 257 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), | 310 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), |
| 258 bounds.y()); | 311 bounds.y()); |
| 259 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); | 312 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); |
| 260 | 313 |
| 261 // Both horizontal and vertical with border. | 314 // Both horizontal and vertical with border. |
| 262 contents->SetBounds(0, 0, 300, 400); | 315 contents->SetBounds(0, 0, 300, 400); |
| 263 scroll_view.Layout(); | 316 scroll_view.Layout(); |
| 264 EXPECT_EQ( | 317 EXPECT_EQ( |
| 265 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding, | 318 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding, |
| 266 contents->parent()->width()); | 319 test_api.contents_viewport()->width()); |
| 267 EXPECT_EQ( | 320 EXPECT_EQ( |
| 268 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding, | 321 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding, |
| 269 contents->parent()->height()); | 322 test_api.contents_viewport()->height()); |
| 270 bounds = scroll_view.horizontal_scroll_bar()->bounds(); | 323 bounds = scroll_view.horizontal_scroll_bar()->bounds(); |
| 271 // Check horiz. | 324 // Check horiz. |
| 272 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); | 325 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); |
| 273 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); | 326 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); |
| 274 bounds = scroll_view.horizontal_scroll_bar()->bounds(); | 327 bounds = scroll_view.horizontal_scroll_bar()->bounds(); |
| 275 EXPECT_EQ(kLeftPadding, bounds.x()); | 328 EXPECT_EQ(kLeftPadding, bounds.x()); |
| 276 EXPECT_EQ(100 - kRightPadding - scroll_view.GetScrollBarWidth(), | 329 EXPECT_EQ(100 - kRightPadding - scroll_view.GetScrollBarWidth(), |
| 277 bounds.right()); | 330 bounds.right()); |
| 278 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), | 331 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), |
| 279 bounds.y()); | 332 bounds.y()); |
| 280 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); | 333 EXPECT_EQ(100 - kBottomPadding, bounds.bottom()); |
| 281 // Check vert. | 334 // Check vert. |
| 282 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); | 335 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); |
| 283 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); | 336 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); |
| 284 bounds = scroll_view.vertical_scroll_bar()->bounds(); | 337 bounds = scroll_view.vertical_scroll_bar()->bounds(); |
| 285 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth() - kRightPadding, bounds.x()); | 338 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth() - kRightPadding, bounds.x()); |
| 286 EXPECT_EQ(100 - kRightPadding, bounds.right()); | 339 EXPECT_EQ(100 - kRightPadding, bounds.right()); |
| 287 EXPECT_EQ(kTopPadding, bounds.y()); | 340 EXPECT_EQ(kTopPadding, bounds.y()); |
| 288 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), | 341 EXPECT_EQ(100 - kBottomPadding - scroll_view.GetScrollBarHeight(), |
| 289 bounds.bottom()); | 342 bounds.bottom()); |
| 290 } | 343 } |
| 291 | 344 |
| 292 // Assertions around adding a header. | 345 // Assertions around adding a header. |
| 293 TEST(ScrollViewTest, Header) { | 346 TEST(ScrollViewTest, Header) { |
| 294 ScrollView scroll_view; | 347 ScrollView scroll_view; |
| 348 ScrollViewTestApi test_api(&scroll_view); |
| 295 View* contents = new View; | 349 View* contents = new View; |
| 296 CustomView* header = new CustomView; | 350 CustomView* header = new CustomView; |
| 297 scroll_view.SetHeader(header); | 351 scroll_view.SetHeader(header); |
| 298 View* header_parent = header->parent(); | 352 View* header_parent = header->parent(); |
| 299 scroll_view.SetContents(contents); | 353 scroll_view.SetContents(contents); |
| 300 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); | 354 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); |
| 301 scroll_view.Layout(); | 355 scroll_view.Layout(); |
| 302 // |header|s preferred size is empty, which should result in all space going | 356 // |header|s preferred size is empty, which should result in all space going |
| 303 // to contents. | 357 // to contents. |
| 304 EXPECT_EQ("0,0 100x0", header->parent()->bounds().ToString()); | 358 EXPECT_EQ("0,0 100x0", header->parent()->bounds().ToString()); |
| 305 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); | 359 EXPECT_EQ("0,0 100x100", test_api.contents_viewport()->bounds().ToString()); |
| 306 | 360 |
| 307 // Get the header a height of 20. | 361 // Get the header a height of 20. |
| 308 header->SetPreferredSize(gfx::Size(10, 20)); | 362 header->SetPreferredSize(gfx::Size(10, 20)); |
| 309 EXPECT_EQ("0,0 100x20", header->parent()->bounds().ToString()); | 363 EXPECT_EQ("0,0 100x20", header->parent()->bounds().ToString()); |
| 310 EXPECT_EQ("0,20 100x80", contents->parent()->bounds().ToString()); | 364 EXPECT_EQ("0,20 100x80", test_api.contents_viewport()->bounds().ToString()); |
| 311 | 365 |
| 312 // Remove the header. | 366 // Remove the header. |
| 313 scroll_view.SetHeader(NULL); | 367 scroll_view.SetHeader(NULL); |
| 314 // SetHeader(NULL) deletes header. | 368 // SetHeader(NULL) deletes header. |
| 315 header = NULL; | 369 header = NULL; |
| 316 EXPECT_EQ("0,0 100x0", header_parent->bounds().ToString()); | 370 EXPECT_EQ("0,0 100x0", header_parent->bounds().ToString()); |
| 317 EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString()); | 371 EXPECT_EQ("0,0 100x100", test_api.contents_viewport()->bounds().ToString()); |
| 318 } | 372 } |
| 319 | 373 |
| 320 // Verifies the scrollbars are added as necessary when a header is present. | 374 // Verifies the scrollbars are added as necessary when a header is present. |
| 321 TEST(ScrollViewTest, ScrollBarsWithHeader) { | 375 TEST(ScrollViewTest, ScrollBarsWithHeader) { |
| 322 ScrollView scroll_view; | 376 ScrollView scroll_view; |
| 377 ScrollViewTestApi test_api(&scroll_view); |
| 323 View* contents = new View; | 378 View* contents = new View; |
| 324 scroll_view.SetContents(contents); | 379 scroll_view.SetContents(contents); |
| 325 CustomView* header = new CustomView; | 380 CustomView* header = new CustomView; |
| 326 scroll_view.SetHeader(header); | 381 scroll_view.SetHeader(header); |
| 327 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); | 382 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); |
| 328 | 383 |
| 329 header->SetPreferredSize(gfx::Size(10, 20)); | 384 header->SetPreferredSize(gfx::Size(10, 20)); |
| 330 | 385 |
| 331 // Size the contents such that vertical scrollbar is needed. | 386 // Size the contents such that vertical scrollbar is needed. |
| 332 contents->SetBounds(0, 0, 50, 400); | 387 contents->SetBounds(0, 0, 50, 400); |
| 333 scroll_view.Layout(); | 388 scroll_view.Layout(); |
| 334 EXPECT_EQ(0, contents->parent()->x()); | 389 EXPECT_EQ(0, test_api.contents_viewport()->x()); |
| 335 EXPECT_EQ(20, contents->parent()->y()); | 390 EXPECT_EQ(20, test_api.contents_viewport()->y()); |
| 336 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); | 391 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), |
| 337 EXPECT_EQ(80, contents->parent()->height()); | 392 test_api.contents_viewport()->width()); |
| 393 EXPECT_EQ(80, test_api.contents_viewport()->height()); |
| 338 EXPECT_EQ(0, header->parent()->x()); | 394 EXPECT_EQ(0, header->parent()->x()); |
| 339 EXPECT_EQ(0, header->parent()->y()); | 395 EXPECT_EQ(0, header->parent()->y()); |
| 340 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width()); | 396 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width()); |
| 341 EXPECT_EQ(20, header->parent()->height()); | 397 EXPECT_EQ(20, header->parent()->height()); |
| 342 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || | 398 EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() || |
| 343 !scroll_view.horizontal_scroll_bar()->visible()); | 399 !scroll_view.horizontal_scroll_bar()->visible()); |
| 344 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); | 400 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); |
| 345 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); | 401 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); |
| 346 // Make sure the vertical scrollbar overlaps the header. | 402 // Make sure the vertical scrollbar overlaps the header. |
| 347 EXPECT_EQ(header->y(), scroll_view.vertical_scroll_bar()->y()); | 403 EXPECT_EQ(header->y(), scroll_view.vertical_scroll_bar()->y()); |
| 348 EXPECT_EQ(header->y(), contents->y()); | 404 EXPECT_EQ(header->y(), contents->y()); |
| 349 | 405 |
| 350 // Size the contents such that horizontal scrollbar is needed. | 406 // Size the contents such that horizontal scrollbar is needed. |
| 351 contents->SetBounds(0, 0, 400, 50); | 407 contents->SetBounds(0, 0, 400, 50); |
| 352 scroll_view.Layout(); | 408 scroll_view.Layout(); |
| 353 EXPECT_EQ(0, contents->parent()->x()); | 409 EXPECT_EQ(0, test_api.contents_viewport()->x()); |
| 354 EXPECT_EQ(20, contents->parent()->y()); | 410 EXPECT_EQ(20, test_api.contents_viewport()->y()); |
| 355 EXPECT_EQ(100, contents->parent()->width()); | 411 EXPECT_EQ(100, test_api.contents_viewport()->width()); |
| 356 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20, | 412 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20, |
| 357 contents->parent()->height()); | 413 test_api.contents_viewport()->height()); |
| 358 EXPECT_EQ(0, header->parent()->x()); | 414 EXPECT_EQ(0, header->parent()->x()); |
| 359 EXPECT_EQ(0, header->parent()->y()); | 415 EXPECT_EQ(0, header->parent()->y()); |
| 360 EXPECT_EQ(100, header->parent()->width()); | 416 EXPECT_EQ(100, header->parent()->width()); |
| 361 EXPECT_EQ(20, header->parent()->height()); | 417 EXPECT_EQ(20, header->parent()->height()); |
| 362 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); | 418 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); |
| 363 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); | 419 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); |
| 364 EXPECT_TRUE(!scroll_view.vertical_scroll_bar() || | 420 EXPECT_TRUE(!scroll_view.vertical_scroll_bar() || |
| 365 !scroll_view.vertical_scroll_bar()->visible()); | 421 !scroll_view.vertical_scroll_bar()->visible()); |
| 366 | 422 |
| 367 // Both horizontal and vertical. | 423 // Both horizontal and vertical. |
| 368 contents->SetBounds(0, 0, 300, 400); | 424 contents->SetBounds(0, 0, 300, 400); |
| 369 scroll_view.Layout(); | 425 scroll_view.Layout(); |
| 370 EXPECT_EQ(0, contents->parent()->x()); | 426 EXPECT_EQ(0, test_api.contents_viewport()->x()); |
| 371 EXPECT_EQ(20, contents->parent()->y()); | 427 EXPECT_EQ(20, test_api.contents_viewport()->y()); |
| 372 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); | 428 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), |
| 429 test_api.contents_viewport()->width()); |
| 373 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20, | 430 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20, |
| 374 contents->parent()->height()); | 431 test_api.contents_viewport()->height()); |
| 375 EXPECT_EQ(0, header->parent()->x()); | 432 EXPECT_EQ(0, header->parent()->x()); |
| 376 EXPECT_EQ(0, header->parent()->y()); | 433 EXPECT_EQ(0, header->parent()->y()); |
| 377 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width()); | 434 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width()); |
| 378 EXPECT_EQ(20, header->parent()->height()); | 435 EXPECT_EQ(20, header->parent()->height()); |
| 379 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); | 436 ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL); |
| 380 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); | 437 EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible()); |
| 381 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); | 438 ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL); |
| 382 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); | 439 EXPECT_TRUE(scroll_view.vertical_scroll_bar()->visible()); |
| 383 } | 440 } |
| 384 | 441 |
| 385 // Verifies the header scrolls horizontally with the content. | 442 // Verifies the header scrolls horizontally with the content. |
| 386 TEST(ScrollViewTest, HeaderScrollsWithContent) { | 443 TEST(ScrollViewTest, HeaderScrollsWithContent) { |
| 387 ScrollView scroll_view; | 444 ScrollView scroll_view; |
| 445 ScrollViewTestApi test_api(&scroll_view); |
| 388 CustomView* contents = new CustomView; | 446 CustomView* contents = new CustomView; |
| 389 scroll_view.SetContents(contents); | 447 scroll_view.SetContents(contents); |
| 390 contents->SetPreferredSize(gfx::Size(500, 500)); | 448 contents->SetPreferredSize(gfx::Size(500, 500)); |
| 391 | 449 |
| 392 CustomView* header = new CustomView; | 450 CustomView* header = new CustomView; |
| 393 scroll_view.SetHeader(header); | 451 scroll_view.SetHeader(header); |
| 394 header->SetPreferredSize(gfx::Size(500, 20)); | 452 header->SetPreferredSize(gfx::Size(500, 20)); |
| 395 | 453 |
| 396 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); | 454 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); |
| 397 EXPECT_EQ("0,0", contents->bounds().origin().ToString()); | 455 EXPECT_EQ("0,0", test_api.IntegralViewOffset().ToString()); |
| 398 EXPECT_EQ("0,0", header->bounds().origin().ToString()); | 456 EXPECT_EQ("0,0", header->bounds().origin().ToString()); |
| 399 | 457 |
| 400 // Scroll the horizontal scrollbar. | 458 // Scroll the horizontal scrollbar. |
| 401 ASSERT_TRUE(scroll_view.horizontal_scroll_bar()); | 459 ASSERT_TRUE(scroll_view.horizontal_scroll_bar()); |
| 402 scroll_view.ScrollToPosition( | 460 scroll_view.ScrollToPosition( |
| 403 const_cast<ScrollBar*>(scroll_view.horizontal_scroll_bar()), 1); | 461 const_cast<ScrollBar*>(scroll_view.horizontal_scroll_bar()), 1); |
| 404 EXPECT_EQ("-1,0", contents->bounds().origin().ToString()); | 462 EXPECT_EQ("-1,0", test_api.IntegralViewOffset().ToString()); |
| 405 EXPECT_EQ("-1,0", header->bounds().origin().ToString()); | 463 EXPECT_EQ("-1,0", header->bounds().origin().ToString()); |
| 406 | 464 |
| 407 // Scrolling the vertical scrollbar shouldn't effect the header. | 465 // Scrolling the vertical scrollbar shouldn't effect the header. |
| 408 ASSERT_TRUE(scroll_view.vertical_scroll_bar()); | 466 ASSERT_TRUE(scroll_view.vertical_scroll_bar()); |
| 409 scroll_view.ScrollToPosition( | 467 scroll_view.ScrollToPosition( |
| 410 const_cast<ScrollBar*>(scroll_view.vertical_scroll_bar()), 1); | 468 const_cast<ScrollBar*>(scroll_view.vertical_scroll_bar()), 1); |
| 411 EXPECT_EQ("-1,-1", contents->bounds().origin().ToString()); | 469 EXPECT_EQ("-1,-1", test_api.IntegralViewOffset().ToString()); |
| 412 EXPECT_EQ("-1,0", header->bounds().origin().ToString()); | 470 EXPECT_EQ("-1,0", header->bounds().origin().ToString()); |
| 413 } | 471 } |
| 414 | 472 |
| 415 // Verifies ScrollRectToVisible() on the child works. | 473 // Verifies ScrollRectToVisible() on the child works. |
| 416 TEST(ScrollViewTest, ScrollRectToVisible) { | 474 TEST(ScrollViewTest, ScrollRectToVisible) { |
| 475 #if defined(OS_MACOSX) |
| 476 ui::test::ScopedPreferredScrollerStyle scroller_style_override(false); |
| 477 #endif |
| 417 ScrollView scroll_view; | 478 ScrollView scroll_view; |
| 479 ScrollViewTestApi test_api(&scroll_view); |
| 418 CustomView* contents = new CustomView; | 480 CustomView* contents = new CustomView; |
| 419 scroll_view.SetContents(contents); | 481 scroll_view.SetContents(contents); |
| 420 contents->SetPreferredSize(gfx::Size(500, 1000)); | 482 contents->SetPreferredSize(gfx::Size(500, 1000)); |
| 421 | 483 |
| 422 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); | 484 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); |
| 423 scroll_view.Layout(); | 485 scroll_view.Layout(); |
| 424 EXPECT_EQ("0,0", contents->bounds().origin().ToString()); | 486 EXPECT_EQ("0,0", test_api.IntegralViewOffset().ToString()); |
| 425 | 487 |
| 426 // Scroll to y=405 height=10, this should make the y position of the content | 488 // Scroll to y=405 height=10, this should make the y position of the content |
| 427 // at (405 + 10) - viewport_height (scroll region bottom aligned). | 489 // at (405 + 10) - viewport_height (scroll region bottom aligned). |
| 428 contents->ScrollRectToVisible(gfx::Rect(0, 405, 10, 10)); | 490 contents->ScrollRectToVisible(gfx::Rect(0, 405, 10, 10)); |
| 429 const int viewport_height = contents->parent()->height(); | 491 const int viewport_height = test_api.contents_viewport()->height(); |
| 430 EXPECT_EQ(-(415 - viewport_height), contents->y()); | 492 |
| 493 // Expect there to be a horizontal scrollbar, making the viewport shorter. |
| 494 EXPECT_LT(viewport_height, 100); |
| 495 |
| 496 gfx::ScrollOffset offset = test_api.CurrentOffset(); |
| 497 EXPECT_EQ(415 - viewport_height, offset.y()); |
| 431 | 498 |
| 432 // Scroll to the current y-location and 10x10; should do nothing. | 499 // Scroll to the current y-location and 10x10; should do nothing. |
| 433 contents->ScrollRectToVisible(gfx::Rect(0, -contents->y(), 10, 10)); | 500 contents->ScrollRectToVisible(gfx::Rect(0, offset.y(), 10, 10)); |
| 434 EXPECT_EQ(-(415 - viewport_height), contents->y()); | 501 EXPECT_EQ(415 - viewport_height, test_api.CurrentOffset().y()); |
| 435 } | 502 } |
| 436 | 503 |
| 437 // Verifies ClipHeightTo() uses the height of the content when it is between the | 504 // Verifies ClipHeightTo() uses the height of the content when it is between the |
| 438 // minimum and maximum height values. | 505 // minimum and maximum height values. |
| 439 TEST(ScrollViewTest, ClipHeightToNormalContentHeight) { | 506 TEST(ScrollViewTest, ClipHeightToNormalContentHeight) { |
| 440 ScrollView scroll_view; | 507 ScrollView scroll_view; |
| 441 | 508 |
| 442 scroll_view.ClipHeightTo(kMinHeight, kMaxHeight); | 509 scroll_view.ClipHeightTo(kMinHeight, kMaxHeight); |
| 443 | 510 |
| 444 const int kNormalContentHeight = 75; | 511 const int kNormalContentHeight = 75; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 | 637 |
| 571 #if defined(OS_MACOSX) | 638 #if defined(OS_MACOSX) |
| 572 // Tests the overlay scrollbars on Mac. Ensure that they show up properly and | 639 // Tests the overlay scrollbars on Mac. Ensure that they show up properly and |
| 573 // do not overlap each other. | 640 // do not overlap each other. |
| 574 TEST(ScrollViewTest, CocoaOverlayScrollBars) { | 641 TEST(ScrollViewTest, CocoaOverlayScrollBars) { |
| 575 std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> | 642 std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> |
| 576 scroller_style_override; | 643 scroller_style_override; |
| 577 scroller_style_override.reset( | 644 scroller_style_override.reset( |
| 578 new ui::test::ScopedPreferredScrollerStyle(true)); | 645 new ui::test::ScopedPreferredScrollerStyle(true)); |
| 579 ScrollView scroll_view; | 646 ScrollView scroll_view; |
| 647 ScrollViewTestApi test_api(&scroll_view); |
| 580 View* contents = new View; | 648 View* contents = new View; |
| 581 scroll_view.SetContents(contents); | 649 scroll_view.SetContents(contents); |
| 582 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); | 650 scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100)); |
| 583 | 651 |
| 584 // Size the contents such that vertical scrollbar is needed. | 652 // Size the contents such that vertical scrollbar is needed. |
| 585 // Since it is overlaid, the ViewPort size should match the ScrollView. | 653 // Since it is overlaid, the ViewPort size should match the ScrollView. |
| 586 contents->SetBounds(0, 0, 50, 400); | 654 contents->SetBounds(0, 0, 50, 400); |
| 587 scroll_view.Layout(); | 655 scroll_view.Layout(); |
| 588 EXPECT_EQ(100, contents->parent()->width()); | 656 EXPECT_EQ(100, test_api.contents_viewport()->width()); |
| 589 EXPECT_EQ(100, contents->parent()->height()); | 657 EXPECT_EQ(100, test_api.contents_viewport()->height()); |
| 590 EXPECT_EQ(0, scroll_view.GetScrollBarWidth()); | 658 EXPECT_EQ(0, scroll_view.GetScrollBarWidth()); |
| 591 CheckScrollbarVisibility(scroll_view, VERTICAL, true); | 659 CheckScrollbarVisibility(scroll_view, VERTICAL, true); |
| 592 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false); | 660 CheckScrollbarVisibility(scroll_view, HORIZONTAL, false); |
| 593 | 661 |
| 594 // Size the contents such that horizontal scrollbar is needed. | 662 // Size the contents such that horizontal scrollbar is needed. |
| 595 contents->SetBounds(0, 0, 400, 50); | 663 contents->SetBounds(0, 0, 400, 50); |
| 596 scroll_view.Layout(); | 664 scroll_view.Layout(); |
| 597 EXPECT_EQ(100, contents->parent()->width()); | 665 EXPECT_EQ(100, test_api.contents_viewport()->width()); |
| 598 EXPECT_EQ(100, contents->parent()->height()); | 666 EXPECT_EQ(100, test_api.contents_viewport()->height()); |
| 599 EXPECT_EQ(0, scroll_view.GetScrollBarHeight()); | 667 EXPECT_EQ(0, scroll_view.GetScrollBarHeight()); |
| 600 CheckScrollbarVisibility(scroll_view, VERTICAL, false); | 668 CheckScrollbarVisibility(scroll_view, VERTICAL, false); |
| 601 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); | 669 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); |
| 602 | 670 |
| 603 // Both horizontal and vertical scrollbars. | 671 // Both horizontal and vertical scrollbars. |
| 604 contents->SetBounds(0, 0, 300, 400); | 672 contents->SetBounds(0, 0, 300, 400); |
| 605 scroll_view.Layout(); | 673 scroll_view.Layout(); |
| 606 EXPECT_EQ(100, contents->parent()->width()); | 674 EXPECT_EQ(100, test_api.contents_viewport()->width()); |
| 607 EXPECT_EQ(100, contents->parent()->height()); | 675 EXPECT_EQ(100, test_api.contents_viewport()->height()); |
| 608 EXPECT_EQ(0, scroll_view.GetScrollBarWidth()); | 676 EXPECT_EQ(0, scroll_view.GetScrollBarWidth()); |
| 609 EXPECT_EQ(0, scroll_view.GetScrollBarHeight()); | 677 EXPECT_EQ(0, scroll_view.GetScrollBarHeight()); |
| 610 CheckScrollbarVisibility(scroll_view, VERTICAL, true); | 678 CheckScrollbarVisibility(scroll_view, VERTICAL, true); |
| 611 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); | 679 CheckScrollbarVisibility(scroll_view, HORIZONTAL, true); |
| 612 | 680 |
| 613 // Make sure the horizontal and vertical scrollbars don't overlap each other. | 681 // Make sure the horizontal and vertical scrollbars don't overlap each other. |
| 614 gfx::Rect vert_bounds = scroll_view.vertical_scroll_bar()->bounds(); | 682 gfx::Rect vert_bounds = scroll_view.vertical_scroll_bar()->bounds(); |
| 615 gfx::Rect horiz_bounds = scroll_view.horizontal_scroll_bar()->bounds(); | 683 gfx::Rect horiz_bounds = scroll_view.horizontal_scroll_bar()->bounds(); |
| 616 EXPECT_EQ(vert_bounds.x(), horiz_bounds.right()); | 684 EXPECT_EQ(vert_bounds.x(), horiz_bounds.right()); |
| 617 EXPECT_EQ(horiz_bounds.y(), vert_bounds.bottom()); | 685 EXPECT_EQ(horiz_bounds.y(), vert_bounds.bottom()); |
| 618 | 686 |
| 619 // Switch to the non-overlay style and check that the ViewPort is now sized | 687 // Switch to the non-overlay style and check that the ViewPort is now sized |
| 620 // to be smaller, and ScrollbarWidth and ScrollbarHeight are non-zero. | 688 // to be smaller, and ScrollbarWidth and ScrollbarHeight are non-zero. |
| 621 scroller_style_override.reset( | 689 scroller_style_override.reset( |
| 622 new ui::test::ScopedPreferredScrollerStyle(false)); | 690 new ui::test::ScopedPreferredScrollerStyle(false)); |
| 623 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width()); | 691 EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), |
| 692 test_api.contents_viewport()->width()); |
| 624 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), | 693 EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(), |
| 625 contents->parent()->height()); | 694 test_api.contents_viewport()->height()); |
| 626 EXPECT_NE(0, scroll_view.GetScrollBarWidth()); | 695 EXPECT_NE(0, scroll_view.GetScrollBarWidth()); |
| 627 EXPECT_NE(0, scroll_view.GetScrollBarHeight()); | 696 EXPECT_NE(0, scroll_view.GetScrollBarHeight()); |
| 628 } | 697 } |
| 629 #endif | 698 #endif |
| 630 | 699 |
| 631 // Test scrolling behavior when clicking on the scroll track. | 700 // Test scrolling behavior when clicking on the scroll track. |
| 632 TEST_F(WidgetScrollViewTest, ScrollTrackScrolling) { | 701 TEST_F(WidgetScrollViewTest, ScrollTrackScrolling) { |
| 633 // Set up with a vertical scroller. | 702 // Set up with a vertical scroller. |
| 634 ScrollView* scroll_view = | 703 ScrollView* scroll_view = |
| 635 AddScrollViewWithContentSize(gfx::Size(10, kDefaultHeight * 5)); | 704 AddScrollViewWithContentSize(gfx::Size(10, kDefaultHeight * 5)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 654 | 723 |
| 655 // While the mouse is pressed, timer should trigger more scroll events. | 724 // While the mouse is pressed, timer should trigger more scroll events. |
| 656 EXPECT_TRUE(timer.IsRunning()); | 725 EXPECT_TRUE(timer.IsRunning()); |
| 657 | 726 |
| 658 // Upon release timer should stop (and scroll position should remain). | 727 // Upon release timer should stop (and scroll position should remain). |
| 659 scroll_bar->OnMouseEvent(&release); | 728 scroll_bar->OnMouseEvent(&release); |
| 660 EXPECT_FALSE(timer.IsRunning()); | 729 EXPECT_FALSE(timer.IsRunning()); |
| 661 EXPECT_EQ(kDefaultHeight, scroll_view->GetVisibleRect().y()); | 730 EXPECT_EQ(kDefaultHeight, scroll_view->GetVisibleRect().y()); |
| 662 } | 731 } |
| 663 | 732 |
| 733 // Test that views scroll offsets are in sync with the layer scroll offsets. |
| 734 TEST_F(WidgetScrollViewTest, ScrollOffsetUsingLayers) { |
| 735 // Set up with a vertical scroller, but don't commit the layer changes yet. |
| 736 ScrollView* scroll_view = |
| 737 AddScrollViewWithContentSize(gfx::Size(10, kDefaultHeight * 5), false); |
| 738 ScrollViewTestApi test_api(scroll_view); |
| 739 |
| 740 EXPECT_EQ(gfx::ScrollOffset(0, 0), test_api.CurrentOffset()); |
| 741 |
| 742 // UI code may request a scroll before layer changes are committed. |
| 743 gfx::Rect offset(0, kDefaultHeight * 2, 1, kDefaultHeight); |
| 744 scroll_view->contents()->ScrollRectToVisible(offset); |
| 745 EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset()); |
| 746 |
| 747 // The following only makes sense when layered scrolling is enabled. |
| 748 View* container = test_api.contents_container(); |
| 749 #if defined(OS_MACOSX) |
| 750 // Sanity check: Mac should always scroll with layers. |
| 751 EXPECT_TRUE(container); |
| 752 #endif |
| 753 if (!container) |
| 754 return; |
| 755 |
| 756 // Container and viewport should have layers. |
| 757 EXPECT_TRUE(container->layer()); |
| 758 EXPECT_TRUE(test_api.contents_viewport()->layer()); |
| 759 |
| 760 // In a Widget, so there should be a compositor. |
| 761 ui::Compositor* compositor = container->layer()->GetCompositor(); |
| 762 EXPECT_TRUE(compositor); |
| 763 |
| 764 // But setting on the impl side should fail since the layer isn't committed. |
| 765 int layer_id = container->layer()->cc_layer_for_testing()->id(); |
| 766 EXPECT_FALSE(compositor->ScrollLayerTo(layer_id, gfx::ScrollOffset(0, 0))); |
| 767 EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset()); |
| 768 |
| 769 WaitForCommit(); |
| 770 EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset()); |
| 771 |
| 772 // Upon commit, the impl side should report the same value too. |
| 773 gfx::ScrollOffset impl_offset; |
| 774 EXPECT_TRUE(compositor->GetScrollOffsetForLayer(layer_id, &impl_offset)); |
| 775 EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), impl_offset); |
| 776 |
| 777 // Now impl-side scrolling should work, and also update the ScrollView. |
| 778 offset.set_y(kDefaultHeight * 3); |
| 779 EXPECT_TRUE( |
| 780 compositor->ScrollLayerTo(layer_id, gfx::ScrollOffset(0, offset.y()))); |
| 781 EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset()); |
| 782 |
| 783 // Scroll via ScrollView API. Should be reflected on the impl side. |
| 784 offset.set_y(kDefaultHeight * 4); |
| 785 scroll_view->contents()->ScrollRectToVisible(offset); |
| 786 EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset()); |
| 787 |
| 788 EXPECT_TRUE(compositor->GetScrollOffsetForLayer(layer_id, &impl_offset)); |
| 789 EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), impl_offset); |
| 790 } |
| 791 |
| 664 } // namespace views | 792 } // namespace views |
| OLD | NEW |