| Index: ui/views/controls/scroll_view_unittest.cc
|
| diff --git a/ui/views/controls/scroll_view_unittest.cc b/ui/views/controls/scroll_view_unittest.cc
|
| index b18ebe7647c0215e468a89c7e0dd23d7372b2940..0db9f7a5decb8e7c053872d8ec30f16b3bbf5765 100644
|
| --- a/ui/views/controls/scroll_view_unittest.cc
|
| +++ b/ui/views/controls/scroll_view_unittest.cc
|
| @@ -5,6 +5,9 @@
|
| #include "ui/views/controls/scroll_view.h"
|
|
|
| #include "base/macros.h"
|
| +#include "base/run_loop.h"
|
| +#include "base/test/test_timeouts.h"
|
| +#include "base/threading/thread_task_runner_handle.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| #include "ui/views/border.h"
|
| #include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h"
|
| @@ -46,7 +49,15 @@ class ScrollViewTestApi {
|
| return GetBaseScrollBar(orientation)->thumb_;
|
| }
|
|
|
| + gfx::Point IntegralViewOffset() {
|
| + return gfx::Point() - gfx::ScrollOffsetToFlooredVector2d(CurrentOffset());
|
| + }
|
| +
|
| + gfx::ScrollOffset CurrentOffset() { return scroll_view_->CurrentOffset(); }
|
| +
|
| View* corner_view() { return scroll_view_->corner_view_; }
|
| + View* contents_viewport() { return scroll_view_->contents_viewport_; }
|
| + View* contents_container() { return scroll_view_->contents_container_; }
|
|
|
| private:
|
| ScrollView* scroll_view_;
|
| @@ -115,7 +126,8 @@ ui::MouseEvent TestLeftMouseAt(const gfx::Point& location, ui::EventType type) {
|
| using test::ScrollViewTestApi;
|
|
|
| // Test harness that includes a Widget to help test ui::Event handling.
|
| -class WidgetScrollViewTest : public test::WidgetTest {
|
| +class WidgetScrollViewTest : public test::WidgetTest,
|
| + public ui::CompositorObserver {
|
| public:
|
| static const int kDefaultHeight = 100;
|
| static const int kDefaultWidth = 100;
|
| @@ -128,7 +140,8 @@ class WidgetScrollViewTest : public test::WidgetTest {
|
| }
|
|
|
| // Adds a ScrollView with a contents view of the given |size| and does layout.
|
| - ScrollView* AddScrollViewWithContentSize(const gfx::Size& contents_size) {
|
| + ScrollView* AddScrollViewWithContentSize(const gfx::Size& contents_size,
|
| + bool commit_layers = true) {
|
| const gfx::Rect default_bounds(50, 50, kDefaultWidth, kDefaultHeight);
|
| widget_ = CreateTopLevelFramelessPlatformWidget();
|
|
|
| @@ -142,19 +155,53 @@ class WidgetScrollViewTest : public test::WidgetTest {
|
|
|
| widget_->SetContentsView(scroll_view);
|
| scroll_view->Layout();
|
| +
|
| + widget_->GetCompositor()->AddObserver(this);
|
| +
|
| + // Ensure the Compositor has committed layer changes before attempting to
|
| + // use them for impl-side scrolling. Note that simply RunUntilIdle() works
|
| + // when tests are run in isolation, but compositor scheduling can interact
|
| + // between test runs in the general case.
|
| + if (commit_layers)
|
| + WaitForCommit();
|
| return scroll_view;
|
| }
|
|
|
| + // Wait for a commit to be observed on the compositor.
|
| + void WaitForCommit() {
|
| + base::RunLoop run_loop;
|
| + quit_closure_ = run_loop.QuitClosure();
|
| + base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
|
| + FROM_HERE, quit_closure_, TestTimeouts::action_timeout());
|
| + run_loop.Run();
|
| + EXPECT_TRUE(quit_closure_.is_null()) << "Timed out waiting for a commit.";
|
| + }
|
| +
|
| // testing::Test:
|
| void TearDown() override {
|
| + widget_->GetCompositor()->RemoveObserver(this);
|
| if (widget_)
|
| widget_->CloseNow();
|
| WidgetTest::TearDown();
|
| }
|
|
|
| private:
|
| + // ui::CompositorObserver:
|
| + void OnCompositingDidCommit(ui::Compositor* compositor) override {
|
| + quit_closure_.Run();
|
| + quit_closure_.Reset();
|
| + }
|
| + void OnCompositingStarted(ui::Compositor* compositor,
|
| + base::TimeTicks start_time) override {}
|
| + void OnCompositingEnded(ui::Compositor* compositor) override {}
|
| + void OnCompositingAborted(ui::Compositor* compositor) override {}
|
| + void OnCompositingLockStateChanged(ui::Compositor* compositor) override {}
|
| + void OnCompositingShuttingDown(ui::Compositor* compositor) override {}
|
| +
|
| Widget* widget_ = nullptr;
|
|
|
| + base::Closure quit_closure_;
|
| +
|
| #if defined(OS_MACOSX)
|
| std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> scroller_style_;
|
| #endif
|
| @@ -168,11 +215,12 @@ const int WidgetScrollViewTest::kDefaultWidth;
|
| // Verifies the viewport is sized to fit the available space.
|
| TEST(ScrollViewTest, ViewportSizedToFit) {
|
| ScrollView scroll_view;
|
| + ScrollViewTestApi test_api(&scroll_view);
|
| View* contents = new View;
|
| scroll_view.SetContents(contents);
|
| scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
|
| scroll_view.Layout();
|
| - EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString());
|
| + EXPECT_EQ("0,0 100x100", test_api.contents_viewport()->bounds().ToString());
|
| }
|
|
|
| // Verifies the scrollbars are added as necessary.
|
| @@ -183,6 +231,7 @@ TEST(ScrollViewTest, ScrollBars) {
|
| #endif
|
|
|
| ScrollView scroll_view;
|
| + ScrollViewTestApi test_api(&scroll_view);
|
| View* contents = new View;
|
| scroll_view.SetContents(contents);
|
| scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
|
| @@ -190,8 +239,9 @@ TEST(ScrollViewTest, ScrollBars) {
|
| // Size the contents such that vertical scrollbar is needed.
|
| contents->SetBounds(0, 0, 50, 400);
|
| scroll_view.Layout();
|
| - EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width());
|
| - EXPECT_EQ(100, contents->parent()->height());
|
| + EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(),
|
| + test_api.contents_viewport()->width());
|
| + EXPECT_EQ(100, test_api.contents_viewport()->height());
|
| CheckScrollbarVisibility(scroll_view, VERTICAL, true);
|
| CheckScrollbarVisibility(scroll_view, HORIZONTAL, false);
|
| EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() ||
|
| @@ -202,18 +252,19 @@ TEST(ScrollViewTest, ScrollBars) {
|
| // Size the contents such that horizontal scrollbar is needed.
|
| contents->SetBounds(0, 0, 400, 50);
|
| scroll_view.Layout();
|
| - EXPECT_EQ(100, contents->parent()->width());
|
| + EXPECT_EQ(100, test_api.contents_viewport()->width());
|
| EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(),
|
| - contents->parent()->height());
|
| + test_api.contents_viewport()->height());
|
| CheckScrollbarVisibility(scroll_view, VERTICAL, false);
|
| CheckScrollbarVisibility(scroll_view, HORIZONTAL, true);
|
|
|
| // Both horizontal and vertical.
|
| contents->SetBounds(0, 0, 300, 400);
|
| scroll_view.Layout();
|
| - EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width());
|
| + EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(),
|
| + test_api.contents_viewport()->width());
|
| EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(),
|
| - contents->parent()->height());
|
| + test_api.contents_viewport()->height());
|
| CheckScrollbarVisibility(scroll_view, VERTICAL, true);
|
| CheckScrollbarVisibility(scroll_view, HORIZONTAL, true);
|
|
|
| @@ -228,8 +279,9 @@ TEST(ScrollViewTest, ScrollBars) {
|
| scroll_view.Layout();
|
| EXPECT_EQ(
|
| 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding,
|
| - contents->parent()->width());
|
| - EXPECT_EQ(100 - kTopPadding - kBottomPadding, contents->parent()->height());
|
| + test_api.contents_viewport()->width());
|
| + EXPECT_EQ(100 - kTopPadding - kBottomPadding,
|
| + test_api.contents_viewport()->height());
|
| EXPECT_TRUE(!scroll_view.horizontal_scroll_bar() ||
|
| !scroll_view.horizontal_scroll_bar()->visible());
|
| ASSERT_TRUE(scroll_view.vertical_scroll_bar() != NULL);
|
| @@ -243,10 +295,11 @@ TEST(ScrollViewTest, ScrollBars) {
|
| // Horizontal with border.
|
| contents->SetBounds(0, 0, 400, 50);
|
| scroll_view.Layout();
|
| - EXPECT_EQ(100 - kLeftPadding - kRightPadding, contents->parent()->width());
|
| + EXPECT_EQ(100 - kLeftPadding - kRightPadding,
|
| + test_api.contents_viewport()->width());
|
| EXPECT_EQ(
|
| 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding,
|
| - contents->parent()->height());
|
| + test_api.contents_viewport()->height());
|
| ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL);
|
| EXPECT_TRUE(scroll_view.horizontal_scroll_bar()->visible());
|
| EXPECT_TRUE(!scroll_view.vertical_scroll_bar() ||
|
| @@ -263,10 +316,10 @@ TEST(ScrollViewTest, ScrollBars) {
|
| scroll_view.Layout();
|
| EXPECT_EQ(
|
| 100 - scroll_view.GetScrollBarWidth() - kLeftPadding - kRightPadding,
|
| - contents->parent()->width());
|
| + test_api.contents_viewport()->width());
|
| EXPECT_EQ(
|
| 100 - scroll_view.GetScrollBarHeight() - kTopPadding - kBottomPadding,
|
| - contents->parent()->height());
|
| + test_api.contents_viewport()->height());
|
| bounds = scroll_view.horizontal_scroll_bar()->bounds();
|
| // Check horiz.
|
| ASSERT_TRUE(scroll_view.horizontal_scroll_bar() != NULL);
|
| @@ -292,6 +345,7 @@ TEST(ScrollViewTest, ScrollBars) {
|
| // Assertions around adding a header.
|
| TEST(ScrollViewTest, Header) {
|
| ScrollView scroll_view;
|
| + ScrollViewTestApi test_api(&scroll_view);
|
| View* contents = new View;
|
| CustomView* header = new CustomView;
|
| scroll_view.SetHeader(header);
|
| @@ -302,24 +356,25 @@ TEST(ScrollViewTest, Header) {
|
| // |header|s preferred size is empty, which should result in all space going
|
| // to contents.
|
| EXPECT_EQ("0,0 100x0", header->parent()->bounds().ToString());
|
| - EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString());
|
| + EXPECT_EQ("0,0 100x100", test_api.contents_viewport()->bounds().ToString());
|
|
|
| // Get the header a height of 20.
|
| header->SetPreferredSize(gfx::Size(10, 20));
|
| EXPECT_EQ("0,0 100x20", header->parent()->bounds().ToString());
|
| - EXPECT_EQ("0,20 100x80", contents->parent()->bounds().ToString());
|
| + EXPECT_EQ("0,20 100x80", test_api.contents_viewport()->bounds().ToString());
|
|
|
| // Remove the header.
|
| scroll_view.SetHeader(NULL);
|
| // SetHeader(NULL) deletes header.
|
| header = NULL;
|
| EXPECT_EQ("0,0 100x0", header_parent->bounds().ToString());
|
| - EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString());
|
| + EXPECT_EQ("0,0 100x100", test_api.contents_viewport()->bounds().ToString());
|
| }
|
|
|
| // Verifies the scrollbars are added as necessary when a header is present.
|
| TEST(ScrollViewTest, ScrollBarsWithHeader) {
|
| ScrollView scroll_view;
|
| + ScrollViewTestApi test_api(&scroll_view);
|
| View* contents = new View;
|
| scroll_view.SetContents(contents);
|
| CustomView* header = new CustomView;
|
| @@ -331,10 +386,11 @@ TEST(ScrollViewTest, ScrollBarsWithHeader) {
|
| // Size the contents such that vertical scrollbar is needed.
|
| contents->SetBounds(0, 0, 50, 400);
|
| scroll_view.Layout();
|
| - EXPECT_EQ(0, contents->parent()->x());
|
| - EXPECT_EQ(20, contents->parent()->y());
|
| - EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width());
|
| - EXPECT_EQ(80, contents->parent()->height());
|
| + EXPECT_EQ(0, test_api.contents_viewport()->x());
|
| + EXPECT_EQ(20, test_api.contents_viewport()->y());
|
| + EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(),
|
| + test_api.contents_viewport()->width());
|
| + EXPECT_EQ(80, test_api.contents_viewport()->height());
|
| EXPECT_EQ(0, header->parent()->x());
|
| EXPECT_EQ(0, header->parent()->y());
|
| EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width());
|
| @@ -350,11 +406,11 @@ TEST(ScrollViewTest, ScrollBarsWithHeader) {
|
| // Size the contents such that horizontal scrollbar is needed.
|
| contents->SetBounds(0, 0, 400, 50);
|
| scroll_view.Layout();
|
| - EXPECT_EQ(0, contents->parent()->x());
|
| - EXPECT_EQ(20, contents->parent()->y());
|
| - EXPECT_EQ(100, contents->parent()->width());
|
| + EXPECT_EQ(0, test_api.contents_viewport()->x());
|
| + EXPECT_EQ(20, test_api.contents_viewport()->y());
|
| + EXPECT_EQ(100, test_api.contents_viewport()->width());
|
| EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20,
|
| - contents->parent()->height());
|
| + test_api.contents_viewport()->height());
|
| EXPECT_EQ(0, header->parent()->x());
|
| EXPECT_EQ(0, header->parent()->y());
|
| EXPECT_EQ(100, header->parent()->width());
|
| @@ -367,11 +423,12 @@ TEST(ScrollViewTest, ScrollBarsWithHeader) {
|
| // Both horizontal and vertical.
|
| contents->SetBounds(0, 0, 300, 400);
|
| scroll_view.Layout();
|
| - EXPECT_EQ(0, contents->parent()->x());
|
| - EXPECT_EQ(20, contents->parent()->y());
|
| - EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width());
|
| + EXPECT_EQ(0, test_api.contents_viewport()->x());
|
| + EXPECT_EQ(20, test_api.contents_viewport()->y());
|
| + EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(),
|
| + test_api.contents_viewport()->width());
|
| EXPECT_EQ(100 - scroll_view.GetScrollBarHeight() - 20,
|
| - contents->parent()->height());
|
| + test_api.contents_viewport()->height());
|
| EXPECT_EQ(0, header->parent()->x());
|
| EXPECT_EQ(0, header->parent()->y());
|
| EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), header->parent()->width());
|
| @@ -385,6 +442,7 @@ TEST(ScrollViewTest, ScrollBarsWithHeader) {
|
| // Verifies the header scrolls horizontally with the content.
|
| TEST(ScrollViewTest, HeaderScrollsWithContent) {
|
| ScrollView scroll_view;
|
| + ScrollViewTestApi test_api(&scroll_view);
|
| CustomView* contents = new CustomView;
|
| scroll_view.SetContents(contents);
|
| contents->SetPreferredSize(gfx::Size(500, 500));
|
| @@ -394,44 +452,53 @@ TEST(ScrollViewTest, HeaderScrollsWithContent) {
|
| header->SetPreferredSize(gfx::Size(500, 20));
|
|
|
| scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
|
| - EXPECT_EQ("0,0", contents->bounds().origin().ToString());
|
| + EXPECT_EQ("0,0", test_api.IntegralViewOffset().ToString());
|
| EXPECT_EQ("0,0", header->bounds().origin().ToString());
|
|
|
| // Scroll the horizontal scrollbar.
|
| ASSERT_TRUE(scroll_view.horizontal_scroll_bar());
|
| scroll_view.ScrollToPosition(
|
| const_cast<ScrollBar*>(scroll_view.horizontal_scroll_bar()), 1);
|
| - EXPECT_EQ("-1,0", contents->bounds().origin().ToString());
|
| + EXPECT_EQ("-1,0", test_api.IntegralViewOffset().ToString());
|
| EXPECT_EQ("-1,0", header->bounds().origin().ToString());
|
|
|
| // Scrolling the vertical scrollbar shouldn't effect the header.
|
| ASSERT_TRUE(scroll_view.vertical_scroll_bar());
|
| scroll_view.ScrollToPosition(
|
| const_cast<ScrollBar*>(scroll_view.vertical_scroll_bar()), 1);
|
| - EXPECT_EQ("-1,-1", contents->bounds().origin().ToString());
|
| + EXPECT_EQ("-1,-1", test_api.IntegralViewOffset().ToString());
|
| EXPECT_EQ("-1,0", header->bounds().origin().ToString());
|
| }
|
|
|
| // Verifies ScrollRectToVisible() on the child works.
|
| TEST(ScrollViewTest, ScrollRectToVisible) {
|
| +#if defined(OS_MACOSX)
|
| + ui::test::ScopedPreferredScrollerStyle scroller_style_override(false);
|
| +#endif
|
| ScrollView scroll_view;
|
| + ScrollViewTestApi test_api(&scroll_view);
|
| CustomView* contents = new CustomView;
|
| scroll_view.SetContents(contents);
|
| contents->SetPreferredSize(gfx::Size(500, 1000));
|
|
|
| scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
|
| scroll_view.Layout();
|
| - EXPECT_EQ("0,0", contents->bounds().origin().ToString());
|
| + EXPECT_EQ("0,0", test_api.IntegralViewOffset().ToString());
|
|
|
| // Scroll to y=405 height=10, this should make the y position of the content
|
| // at (405 + 10) - viewport_height (scroll region bottom aligned).
|
| contents->ScrollRectToVisible(gfx::Rect(0, 405, 10, 10));
|
| - const int viewport_height = contents->parent()->height();
|
| - EXPECT_EQ(-(415 - viewport_height), contents->y());
|
| + const int viewport_height = test_api.contents_viewport()->height();
|
| +
|
| + // Expect there to be a horizontal scrollbar, making the viewport shorter.
|
| + EXPECT_LT(viewport_height, 100);
|
| +
|
| + gfx::ScrollOffset offset = test_api.CurrentOffset();
|
| + EXPECT_EQ(415 - viewport_height, offset.y());
|
|
|
| // Scroll to the current y-location and 10x10; should do nothing.
|
| - contents->ScrollRectToVisible(gfx::Rect(0, -contents->y(), 10, 10));
|
| - EXPECT_EQ(-(415 - viewport_height), contents->y());
|
| + contents->ScrollRectToVisible(gfx::Rect(0, offset.y(), 10, 10));
|
| + EXPECT_EQ(415 - viewport_height, test_api.CurrentOffset().y());
|
| }
|
|
|
| // Verifies ClipHeightTo() uses the height of the content when it is between the
|
| @@ -577,6 +644,7 @@ TEST(ScrollViewTest, CocoaOverlayScrollBars) {
|
| scroller_style_override.reset(
|
| new ui::test::ScopedPreferredScrollerStyle(true));
|
| ScrollView scroll_view;
|
| + ScrollViewTestApi test_api(&scroll_view);
|
| View* contents = new View;
|
| scroll_view.SetContents(contents);
|
| scroll_view.SetBoundsRect(gfx::Rect(0, 0, 100, 100));
|
| @@ -585,8 +653,8 @@ TEST(ScrollViewTest, CocoaOverlayScrollBars) {
|
| // Since it is overlaid, the ViewPort size should match the ScrollView.
|
| contents->SetBounds(0, 0, 50, 400);
|
| scroll_view.Layout();
|
| - EXPECT_EQ(100, contents->parent()->width());
|
| - EXPECT_EQ(100, contents->parent()->height());
|
| + EXPECT_EQ(100, test_api.contents_viewport()->width());
|
| + EXPECT_EQ(100, test_api.contents_viewport()->height());
|
| EXPECT_EQ(0, scroll_view.GetScrollBarWidth());
|
| CheckScrollbarVisibility(scroll_view, VERTICAL, true);
|
| CheckScrollbarVisibility(scroll_view, HORIZONTAL, false);
|
| @@ -594,8 +662,8 @@ TEST(ScrollViewTest, CocoaOverlayScrollBars) {
|
| // Size the contents such that horizontal scrollbar is needed.
|
| contents->SetBounds(0, 0, 400, 50);
|
| scroll_view.Layout();
|
| - EXPECT_EQ(100, contents->parent()->width());
|
| - EXPECT_EQ(100, contents->parent()->height());
|
| + EXPECT_EQ(100, test_api.contents_viewport()->width());
|
| + EXPECT_EQ(100, test_api.contents_viewport()->height());
|
| EXPECT_EQ(0, scroll_view.GetScrollBarHeight());
|
| CheckScrollbarVisibility(scroll_view, VERTICAL, false);
|
| CheckScrollbarVisibility(scroll_view, HORIZONTAL, true);
|
| @@ -603,8 +671,8 @@ TEST(ScrollViewTest, CocoaOverlayScrollBars) {
|
| // Both horizontal and vertical scrollbars.
|
| contents->SetBounds(0, 0, 300, 400);
|
| scroll_view.Layout();
|
| - EXPECT_EQ(100, contents->parent()->width());
|
| - EXPECT_EQ(100, contents->parent()->height());
|
| + EXPECT_EQ(100, test_api.contents_viewport()->width());
|
| + EXPECT_EQ(100, test_api.contents_viewport()->height());
|
| EXPECT_EQ(0, scroll_view.GetScrollBarWidth());
|
| EXPECT_EQ(0, scroll_view.GetScrollBarHeight());
|
| CheckScrollbarVisibility(scroll_view, VERTICAL, true);
|
| @@ -620,9 +688,10 @@ TEST(ScrollViewTest, CocoaOverlayScrollBars) {
|
| // to be smaller, and ScrollbarWidth and ScrollbarHeight are non-zero.
|
| scroller_style_override.reset(
|
| new ui::test::ScopedPreferredScrollerStyle(false));
|
| - EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(), contents->parent()->width());
|
| + EXPECT_EQ(100 - scroll_view.GetScrollBarWidth(),
|
| + test_api.contents_viewport()->width());
|
| EXPECT_EQ(100 - scroll_view.GetScrollBarHeight(),
|
| - contents->parent()->height());
|
| + test_api.contents_viewport()->height());
|
| EXPECT_NE(0, scroll_view.GetScrollBarWidth());
|
| EXPECT_NE(0, scroll_view.GetScrollBarHeight());
|
| }
|
| @@ -661,4 +730,63 @@ TEST_F(WidgetScrollViewTest, ScrollTrackScrolling) {
|
| EXPECT_EQ(kDefaultHeight, scroll_view->GetVisibleRect().y());
|
| }
|
|
|
| +// Test that views scroll offsets are in sync with the layer scroll offsets.
|
| +TEST_F(WidgetScrollViewTest, ScrollOffsetUsingLayers) {
|
| + // Set up with a vertical scroller, but don't commit the layer changes yet.
|
| + ScrollView* scroll_view =
|
| + AddScrollViewWithContentSize(gfx::Size(10, kDefaultHeight * 5), false);
|
| + ScrollViewTestApi test_api(scroll_view);
|
| +
|
| + EXPECT_EQ(gfx::ScrollOffset(0, 0), test_api.CurrentOffset());
|
| +
|
| + // UI code may request a scroll before layer changes are committed.
|
| + gfx::Rect offset(0, kDefaultHeight * 2, 1, kDefaultHeight);
|
| + scroll_view->contents()->ScrollRectToVisible(offset);
|
| + EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset());
|
| +
|
| + // The following only makes sense when layered scrolling is enabled.
|
| + View* container = test_api.contents_container();
|
| +#if defined(OS_MACOSX)
|
| + // Sanity check: Mac should always scroll with layers.
|
| + EXPECT_TRUE(container);
|
| +#endif
|
| + if (!container)
|
| + return;
|
| +
|
| + // Container and viewport should have layers.
|
| + EXPECT_TRUE(container->layer());
|
| + EXPECT_TRUE(test_api.contents_viewport()->layer());
|
| +
|
| + // In a Widget, so there should be a compositor.
|
| + ui::Compositor* compositor = container->layer()->GetCompositor();
|
| + EXPECT_TRUE(compositor);
|
| +
|
| + // But setting on the impl side should fail since the layer isn't committed.
|
| + int layer_id = container->layer()->cc_layer_for_testing()->id();
|
| + EXPECT_FALSE(compositor->ScrollLayerTo(layer_id, gfx::ScrollOffset(0, 0)));
|
| + EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset());
|
| +
|
| + WaitForCommit();
|
| + EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset());
|
| +
|
| + // Upon commit, the impl side should report the same value too.
|
| + gfx::ScrollOffset impl_offset;
|
| + EXPECT_TRUE(compositor->GetScrollOffsetForLayer(layer_id, &impl_offset));
|
| + EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), impl_offset);
|
| +
|
| + // Now impl-side scrolling should work, and also update the ScrollView.
|
| + offset.set_y(kDefaultHeight * 3);
|
| + EXPECT_TRUE(
|
| + compositor->ScrollLayerTo(layer_id, gfx::ScrollOffset(0, offset.y())));
|
| + EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset());
|
| +
|
| + // Scroll via ScrollView API. Should be reflected on the impl side.
|
| + offset.set_y(kDefaultHeight * 4);
|
| + scroll_view->contents()->ScrollRectToVisible(offset);
|
| + EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), test_api.CurrentOffset());
|
| +
|
| + EXPECT_TRUE(compositor->GetScrollOffsetForLayer(layer_id, &impl_offset));
|
| + EXPECT_EQ(gfx::ScrollOffset(0, offset.y()), impl_offset);
|
| +}
|
| +
|
| } // namespace views
|
|
|