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