diff options
author | ccameron@chromium.org <ccameron@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-20 01:15:36 +0000 |
---|---|---|
committer | ccameron@chromium.org <ccameron@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-20 01:15:36 +0000 |
commit | e22f03fac4fc1b0dd1874a2d8fef69c9220b52c9 (patch) | |
tree | 3093a956cf71e66f1fd81b529dc1f156eedb465f | |
parent | 47203f336d91408ee2dfb9e3628ca1c42f8f2228 (diff) | |
download | chromium_src-e22f03fac4fc1b0dd1874a2d8fef69c9220b52c9.zip chromium_src-e22f03fac4fc1b0dd1874a2d8fef69c9220b52c9.tar.gz chromium_src-e22f03fac4fc1b0dd1874a2d8fef69c9220b52c9.tar.bz2 |
Correct computation of scrollbar track start
The track_start_ value should be the difference between where the track
starts and where the scrollbar starts. It was being set to the absolute
position where the track starts.
Add tests for ScrollbarLayerImpl::ComputeThumbQuadRect. Move syncing
geometry into a separate frunction from ScrollbarLayer::Update to
simplify tests.
BUG=271914
Review URL: https://chromiumcodereview.appspot.com/23007012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@218390 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | cc/layers/scrollbar_layer.cc | 19 | ||||
-rw-r--r-- | cc/layers/scrollbar_layer.h | 1 | ||||
-rw-r--r-- | cc/layers/scrollbar_layer_unittest.cc | 92 | ||||
-rw-r--r-- | cc/test/fake_scrollbar.cc | 12 | ||||
-rw-r--r-- | cc/test/fake_scrollbar.h | 8 | ||||
-rw-r--r-- | cc/test/fake_scrollbar_layer.cc | 19 | ||||
-rw-r--r-- | cc/test/fake_scrollbar_layer.h | 15 |
7 files changed, 146 insertions, 20 deletions
diff --git a/cc/layers/scrollbar_layer.cc b/cc/layers/scrollbar_layer.cc index a2fdd58..a7b2eba 100644 --- a/cc/layers/scrollbar_layer.cc +++ b/cc/layers/scrollbar_layer.cc @@ -122,10 +122,12 @@ void ScrollbarLayer::PushPropertiesTo(LayerImpl* layer) { } scrollbar_layer->SetThumbLength(thumb_length_); if (Orientation() == HORIZONTAL) { - scrollbar_layer->SetTrackStart(track_rect_.x()); + scrollbar_layer->SetTrackStart( + track_rect_.x() - scrollbar_->Location().x()); scrollbar_layer->SetTrackLength(track_rect_.width()); } else { - scrollbar_layer->SetTrackStart(track_rect_.y()); + scrollbar_layer->SetTrackStart( + track_rect_.y() - scrollbar_->Location().y()); scrollbar_layer->SetTrackLength(track_rect_.height()); } @@ -180,9 +182,18 @@ gfx::Rect ScrollbarLayer::OriginThumbRect() const { return ScrollbarLayerRectToContentRect(gfx::Rect(thumb_size)); } +void ScrollbarLayer::UpdateThumbAndTrackGeometry() { + track_rect_ = scrollbar_->TrackRect(); + if (scrollbar_->HasThumb()) { + thumb_thickness_ = scrollbar_->ThumbThickness(); + thumb_length_ = scrollbar_->ThumbLength(); + } +} + bool ScrollbarLayer::Update(ResourceUpdateQueue* queue, const OcclusionTracker* occlusion) { - track_rect_ = scrollbar_->TrackRect(); + UpdateThumbAndTrackGeometry(); + gfx::Rect scaled_track_rect = ScrollbarLayerRectToContentRect( gfx::Rect(scrollbar_->Location(), bounds())); @@ -201,8 +212,6 @@ bool ScrollbarLayer::Update(ResourceUpdateQueue* queue, gfx::Rect thumb_rect = OriginThumbRect(); if (scrollbar_->HasThumb() && !thumb_rect.IsEmpty()) { - thumb_thickness_ = scrollbar_->ThumbThickness(); - thumb_length_ = scrollbar_->ThumbLength(); thumb_resource_ = ScopedUIResource::Create( layer_tree_host(), RasterizeScrollbarPart(thumb_rect, THUMB)); } diff --git a/cc/layers/scrollbar_layer.h b/cc/layers/scrollbar_layer.h index a162a7f..feea62d 100644 --- a/cc/layers/scrollbar_layer.h +++ b/cc/layers/scrollbar_layer.h @@ -57,6 +57,7 @@ class CC_EXPORT ScrollbarLayer : public ContentsScalingLayer { UIResourceId thumb_resource_id() { return thumb_resource_.get() ? thumb_resource_->id() : 0; } + void UpdateThumbAndTrackGeometry(); private: gfx::Rect ScrollbarLayerRectToContentRect(gfx::Rect layer_rect) const; diff --git a/cc/layers/scrollbar_layer_unittest.cc b/cc/layers/scrollbar_layer_unittest.cc index 79cfbb1..50d666a 100644 --- a/cc/layers/scrollbar_layer_unittest.cc +++ b/cc/layers/scrollbar_layer_unittest.cc @@ -156,6 +156,98 @@ TEST(ScrollbarLayerTest, ScrollOffsetSynchronization) { EXPECT_EQ(300, cc_scrollbar_layer->Maximum()); } +TEST(ScrollbarLayerTest, ThumbRect) { + scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(); + scoped_refptr<Layer> root_layer = Layer::Create(); + scoped_refptr<Layer> content_layer = Layer::Create(); + scoped_refptr<FakeScrollbarLayer> scrollbar_layer = + FakeScrollbarLayer::Create(false, true, root_layer->id()); + + root_layer->SetScrollable(true); + root_layer->SetMaxScrollOffset(gfx::Vector2d(80, 0)); + root_layer->SetBounds(gfx::Size(100, 50)); + content_layer->SetBounds(gfx::Size(100, 50)); + + host->SetRootLayer(root_layer); + root_layer->AddChild(content_layer); + root_layer->AddChild(scrollbar_layer); + + root_layer->SetScrollOffset(gfx::Vector2d(0, 0)); + scrollbar_layer->SetBounds(gfx::Size(70, 10)); + scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(20, 10)); + scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10)); + scrollbar_layer->fake_scrollbar()->set_thumb_thickness(10); + scrollbar_layer->fake_scrollbar()->set_thumb_length(4); + scrollbar_layer->UpdateThumbAndTrackGeometry(); + LayerImpl* root_layer_impl = NULL; + ScrollbarLayerImpl* scrollbar_layer_impl = NULL; + + // Thumb is at the edge of the scrollbar (should be inset to + // the start of the track within the scrollbar layer's + // position). + scrollbar_layer->UpdateThumbAndTrackGeometry(); + root_layer_impl = host->CommitAndCreateLayerImplTree(); + scrollbar_layer_impl = static_cast<ScrollbarLayerImpl*>( + root_layer_impl->children()[1]); + EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(), + scrollbar_layer_impl->ComputeThumbQuadRect().ToString()); + + // Under-scroll (thumb position should clamp and be unchanged). + root_layer->SetScrollOffset(gfx::Vector2d(-5, 0)); + + scrollbar_layer->UpdateThumbAndTrackGeometry(); + root_layer_impl = host->CommitAndCreateLayerImplTree(); + scrollbar_layer_impl = static_cast<ScrollbarLayerImpl*>( + root_layer_impl->children()[1]); + EXPECT_EQ(gfx::Rect(10, 0, 4, 10).ToString(), + scrollbar_layer_impl->ComputeThumbQuadRect().ToString()); + + // Over-scroll (thumb position should clamp on the far side). + root_layer->SetScrollOffset(gfx::Vector2d(85, 0)); + + scrollbar_layer->UpdateThumbAndTrackGeometry(); + root_layer_impl = host->CommitAndCreateLayerImplTree(); + scrollbar_layer_impl = static_cast<ScrollbarLayerImpl*>( + root_layer_impl->children()[1]); + EXPECT_EQ(gfx::Rect(56, 0, 4, 10).ToString(), + scrollbar_layer_impl->ComputeThumbQuadRect().ToString()); + + // Change thumb thickness and length. + scrollbar_layer->fake_scrollbar()->set_thumb_thickness(4); + scrollbar_layer->fake_scrollbar()->set_thumb_length(6); + + scrollbar_layer->UpdateThumbAndTrackGeometry(); + root_layer_impl = host->CommitAndCreateLayerImplTree(); + scrollbar_layer_impl = static_cast<ScrollbarLayerImpl*>( + root_layer_impl->children()[1]); + EXPECT_EQ(gfx::Rect(54, 0, 6, 4).ToString(), + scrollbar_layer_impl->ComputeThumbQuadRect().ToString()); + + // Shrink the scrollbar layer to cover only the track. + scrollbar_layer->SetBounds(gfx::Size(50, 10)); + scrollbar_layer->fake_scrollbar()->set_location(gfx::Point(30, 10)); + scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 10, 50, 10)); + + scrollbar_layer->UpdateThumbAndTrackGeometry(); + root_layer_impl = host->CommitAndCreateLayerImplTree(); + scrollbar_layer_impl = static_cast<ScrollbarLayerImpl*>( + root_layer_impl->children()[1]); + EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(), + scrollbar_layer_impl->ComputeThumbQuadRect().ToString()); + + // Shrink the track in the non-scrolling dimension so that it only covers the + // middle third of the scrollbar layer (this does not affect the thumb + // position). + scrollbar_layer->fake_scrollbar()->set_track_rect(gfx::Rect(30, 12, 50, 6)); + + scrollbar_layer->UpdateThumbAndTrackGeometry(); + root_layer_impl = host->CommitAndCreateLayerImplTree(); + scrollbar_layer_impl = static_cast<ScrollbarLayerImpl*>( + root_layer_impl->children()[1]); + EXPECT_EQ(gfx::Rect(44, 0, 6, 4).ToString(), + scrollbar_layer_impl->ComputeThumbQuadRect().ToString()); +} + TEST(ScrollbarLayerTest, SolidColorDrawQuads) { LayerTreeSettings layer_tree_settings; layer_tree_settings.solid_color_scrollbars = true; diff --git a/cc/test/fake_scrollbar.cc b/cc/test/fake_scrollbar.cc index e6b4253..67166f6 100644 --- a/cc/test/fake_scrollbar.cc +++ b/cc/test/fake_scrollbar.cc @@ -12,12 +12,18 @@ FakeScrollbar::FakeScrollbar() : paint_(false), has_thumb_(false), is_overlay_(false), + thumb_thickness_(10), + thumb_length_(5), + track_rect_(0, 0, 100, 10), fill_color_(SK_ColorGREEN) {} FakeScrollbar::FakeScrollbar(bool paint, bool has_thumb, bool is_overlay) : paint_(paint), has_thumb_(has_thumb), is_overlay_(is_overlay), + thumb_thickness_(10), + thumb_length_(5), + track_rect_(0, 0, 100, 10), fill_color_(SK_ColorGREEN) {} FakeScrollbar::~FakeScrollbar() {} @@ -33,15 +39,15 @@ bool FakeScrollbar::IsOverlay() const { return is_overlay_; } bool FakeScrollbar::HasThumb() const { return has_thumb_; } int FakeScrollbar::ThumbThickness() const { - return 10; + return thumb_thickness_; } int FakeScrollbar::ThumbLength() const { - return 10; + return thumb_length_; } gfx::Rect FakeScrollbar::TrackRect() const { - return gfx::Rect(0, 0, 100, 10); + return track_rect_; } void FakeScrollbar::PaintPart(SkCanvas* canvas, diff --git a/cc/test/fake_scrollbar.h b/cc/test/fake_scrollbar.h index b9ac409..0f068b0 100644 --- a/cc/test/fake_scrollbar.h +++ b/cc/test/fake_scrollbar.h @@ -30,12 +30,20 @@ class FakeScrollbar : public Scrollbar { gfx::Rect content_rect) OVERRIDE; void set_location(gfx::Point location) { location_ = location; } + void set_track_rect(gfx::Rect track_rect) { track_rect_ = track_rect; } + void set_thumb_thickness(int thumb_thickness) { + thumb_thickness_ = thumb_thickness; + } + void set_thumb_length(int thumb_length) { thumb_length_ = thumb_length; } private: bool paint_; bool has_thumb_; bool is_overlay_; + int thumb_thickness_; + int thumb_length_; gfx::Point location_; + gfx::Rect track_rect_; SkColor fill_color_; DISALLOW_COPY_AND_ASSIGN(FakeScrollbar); diff --git a/cc/test/fake_scrollbar_layer.cc b/cc/test/fake_scrollbar_layer.cc index 77cf826..d588cf5 100644 --- a/cc/test/fake_scrollbar_layer.cc +++ b/cc/test/fake_scrollbar_layer.cc @@ -10,15 +10,24 @@ namespace cc { -FakeScrollbarLayer::FakeScrollbarLayer(bool paint_during_update, - bool has_thumb, +scoped_refptr<FakeScrollbarLayer> FakeScrollbarLayer::Create( + bool paint_during_update, + bool has_thumb, + int scrolling_layer_id) { + FakeScrollbar* fake_scrollbar = new FakeScrollbar( + paint_during_update, has_thumb, false); + return make_scoped_refptr(new FakeScrollbarLayer( + fake_scrollbar, scrolling_layer_id)); +} + +FakeScrollbarLayer::FakeScrollbarLayer(FakeScrollbar* fake_scrollbar, int scrolling_layer_id) : ScrollbarLayer( - scoped_ptr<Scrollbar>( - new FakeScrollbar(paint_during_update, has_thumb, false)).Pass(), + scoped_ptr<Scrollbar>(fake_scrollbar).Pass(), scrolling_layer_id), update_count_(0), - push_properties_count_(0) { + push_properties_count_(0), + fake_scrollbar_(fake_scrollbar) { SetAnchorPoint(gfx::PointF(0.f, 0.f)); SetBounds(gfx::Size(1, 1)); SetIsDrawable(true); diff --git a/cc/test/fake_scrollbar_layer.h b/cc/test/fake_scrollbar_layer.h index 21d0399..775681d 100644 --- a/cc/test/fake_scrollbar_layer.h +++ b/cc/test/fake_scrollbar_layer.h @@ -7,6 +7,7 @@ #include "base/memory/scoped_ptr.h" #include "cc/layers/scrollbar_layer.h" +#include "cc/test/fake_scrollbar.h" namespace base { template<typename T> class AutoReset; } @@ -16,11 +17,7 @@ class FakeScrollbarLayer : public ScrollbarLayer { public: static scoped_refptr<FakeScrollbarLayer> Create(bool paint_during_update, bool has_thumb, - int scrolling_layer_id) { - return make_scoped_refptr(new FakeScrollbarLayer( - paint_during_update, has_thumb, scrolling_layer_id)); - } - + int scrolling_layer_id); int update_count() const { return update_count_; } void reset_update_count() { update_count_ = 0; } @@ -41,15 +38,19 @@ class FakeScrollbarLayer : public ScrollbarLayer { UIResourceId thumb_resource_id() { return ScrollbarLayer::thumb_resource_id(); } + FakeScrollbar* fake_scrollbar() { + return fake_scrollbar_; + } + using ScrollbarLayer::UpdateThumbAndTrackGeometry; private: - FakeScrollbarLayer(bool paint_during_update, - bool has_thumb, + FakeScrollbarLayer(FakeScrollbar* fake_scrollbar, int scrolling_layer_id); virtual ~FakeScrollbarLayer(); int update_count_; size_t push_properties_count_; + FakeScrollbar* fake_scrollbar_; }; } // namespace cc |