1 // Copyright 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "cc/layers/layer_impl.h" 6 7 #include "cc/output/filter_operation.h" 8 #include "cc/output/filter_operations.h" 9 #include "cc/test/fake_impl_proxy.h" 10 #include "cc/test/fake_layer_tree_host_impl.h" 11 #include "cc/test/fake_output_surface.h" 12 #include "cc/test/geometry_test_utils.h" 13 #include "cc/trees/layer_tree_impl.h" 14 #include "cc/trees/single_thread_proxy.h" 15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "third_party/skia/include/effects/SkBlurImageFilter.h" 18 19 namespace cc { 20 namespace { 21 22 #define EXECUTE_AND_VERIFY_SUBTREE_CHANGED(code_to_test) \ 23 root->ResetAllChangeTrackingForSubtree(); \ 24 code_to_test; \ 25 EXPECT_TRUE(root->LayerPropertyChanged()); \ 26 EXPECT_TRUE(child->LayerPropertyChanged()); \ 27 EXPECT_TRUE(grand_child->LayerPropertyChanged()); 28 29 #define EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(code_to_test) \ 30 root->ResetAllChangeTrackingForSubtree(); \ 31 code_to_test; \ 32 EXPECT_FALSE(root->LayerPropertyChanged()); \ 33 EXPECT_FALSE(child->LayerPropertyChanged()); \ 34 EXPECT_FALSE(grand_child->LayerPropertyChanged()); 35 36 #define EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(code_to_test) \ 37 root->ResetAllChangeTrackingForSubtree(); \ 38 code_to_test; \ 39 EXPECT_TRUE(root->LayerPropertyChanged()); \ 40 EXPECT_FALSE(child->LayerPropertyChanged()); \ 41 EXPECT_FALSE(grand_child->LayerPropertyChanged()); 42 43 #define EXECUTE_AND_VERIFY_ONLY_DESCENDANTS_CHANGED(code_to_test) \ 44 root->ResetAllChangeTrackingForSubtree(); \ 45 code_to_test; \ 46 EXPECT_FALSE(root->LayerPropertyChanged()); \ 47 EXPECT_TRUE(child->LayerPropertyChanged()); \ 48 EXPECT_TRUE(grand_child->LayerPropertyChanged()); 49 50 #define VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \ 51 root->ResetAllChangeTrackingForSubtree(); \ 52 host_impl.ForcePrepareToDraw(); \ 53 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \ 54 code_to_test; \ 55 EXPECT_TRUE(host_impl.active_tree()->needs_update_draw_properties()); 56 57 #define VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \ 58 root->ResetAllChangeTrackingForSubtree(); \ 59 host_impl.ForcePrepareToDraw(); \ 60 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \ 61 code_to_test; \ 62 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); 63 64 TEST(LayerImplTest, VerifyLayerChangesAreTrackedProperly) { 65 // 66 // This test checks that layerPropertyChanged() has the correct behavior. 67 // 68 69 // The constructor on this will fake that we are on the correct thread. 70 // Create a simple LayerImpl tree: 71 FakeImplProxy proxy; 72 FakeLayerTreeHostImpl host_impl(&proxy); 73 EXPECT_TRUE(host_impl.InitializeRenderer(CreateFakeOutputSurface())); 74 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); 75 root->AddChild(LayerImpl::Create(host_impl.active_tree(), 2)); 76 LayerImpl* child = root->children()[0]; 77 child->AddChild(LayerImpl::Create(host_impl.active_tree(), 3)); 78 LayerImpl* grand_child = child->children()[0]; 79 80 root->SetScrollable(true); 81 82 // Adding children is an internal operation and should not mark layers as 83 // changed. 84 EXPECT_FALSE(root->LayerPropertyChanged()); 85 EXPECT_FALSE(child->LayerPropertyChanged()); 86 EXPECT_FALSE(grand_child->LayerPropertyChanged()); 87 88 gfx::PointF arbitrary_point_f = gfx::PointF(0.125f, 0.25f); 89 float arbitrary_number = 0.352f; 90 gfx::Size arbitrary_size = gfx::Size(111, 222); 91 gfx::Point arbitrary_point = gfx::Point(333, 444); 92 gfx::Vector2d arbitrary_vector2d = gfx::Vector2d(111, 222); 93 gfx::Rect arbitrary_rect = gfx::Rect(arbitrary_point, arbitrary_size); 94 gfx::RectF arbitrary_rect_f = 95 gfx::RectF(arbitrary_point_f, gfx::SizeF(1.234f, 5.678f)); 96 SkColor arbitrary_color = SkColorSetRGB(10, 20, 30); 97 gfx::Transform arbitrary_transform; 98 arbitrary_transform.Scale3d(0.1f, 0.2f, 0.3f); 99 FilterOperations arbitrary_filters; 100 arbitrary_filters.Append(FilterOperation::CreateOpacityFilter(0.5f)); 101 SkXfermode::Mode arbitrary_blend_mode = SkXfermode::kMultiply_Mode; 102 103 // These properties are internal, and should not be considered "change" when 104 // they are used. 105 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 106 root->set_update_rect(arbitrary_rect_f)); 107 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 108 root->SetMaxScrollOffset(arbitrary_vector2d)); 109 110 // Changing these properties affects the entire subtree of layers. 111 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetAnchorPoint(arbitrary_point_f)); 112 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetAnchorPointZ(arbitrary_number)); 113 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetFilters(arbitrary_filters)); 114 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetFilters(FilterOperations())); 115 EXECUTE_AND_VERIFY_SUBTREE_CHANGED( 116 root->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 4))); 117 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetMasksToBounds(true)); 118 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetContentsOpaque(true)); 119 EXECUTE_AND_VERIFY_SUBTREE_CHANGED( 120 root->SetReplicaLayer(LayerImpl::Create(host_impl.active_tree(), 5))); 121 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetPosition(arbitrary_point_f)); 122 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetPreserves3d(true)); 123 EXECUTE_AND_VERIFY_SUBTREE_CHANGED( 124 root->SetDoubleSided(false)); // constructor initializes it to "true". 125 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->ScrollBy(arbitrary_vector2d)); 126 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetScrollDelta(gfx::Vector2d())); 127 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetScrollOffset(arbitrary_vector2d)); 128 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetHideLayerAndSubtree(true)); 129 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetOpacity(arbitrary_number)); 130 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetBlendMode(arbitrary_blend_mode)); 131 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetTransform(arbitrary_transform)); 132 133 // Changing these properties only affects the layer itself. 134 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root->SetContentBounds(arbitrary_size)); 135 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED( 136 root->SetContentsScale(arbitrary_number, arbitrary_number)); 137 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root->SetDrawsContent(true)); 138 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED( 139 root->SetBackgroundColor(arbitrary_color)); 140 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED( 141 root->SetBackgroundFilters(arbitrary_filters)); 142 143 // Changing these properties affects all layer's descendants, 144 // but not the layer itself. 145 EXECUTE_AND_VERIFY_ONLY_DESCENDANTS_CHANGED( 146 root->SetSublayerTransform(arbitrary_transform)); 147 148 // Special case: check that SetBounds changes behavior depending on 149 // masksToBounds. 150 root->SetMasksToBounds(false); 151 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root->SetBounds(gfx::Size(135, 246))); 152 root->SetMasksToBounds(true); 153 // Should be a different size than previous call, to ensure it marks tree 154 // changed. 155 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetBounds(arbitrary_size)); 156 157 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 158 root->SetIsRootForIsolatedGroup(true)); 159 160 // After setting all these properties already, setting to the exact same 161 // values again should not cause any change. 162 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 163 root->SetAnchorPoint(arbitrary_point_f)); 164 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 165 root->SetAnchorPointZ(arbitrary_number)); 166 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetMasksToBounds(true)); 167 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 168 root->SetPosition(arbitrary_point_f)); 169 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetPreserves3d(true)); 170 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 171 root->SetTransform(arbitrary_transform)); 172 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 173 root->SetDoubleSided(false)); // constructor initializes it to "true". 174 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 175 root->SetScrollDelta(gfx::Vector2d())); 176 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 177 root->SetScrollOffset(arbitrary_vector2d)); 178 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 179 root->SetContentBounds(arbitrary_size)); 180 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 181 root->SetContentsScale(arbitrary_number, arbitrary_number)); 182 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetContentsOpaque(true)); 183 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetOpacity(arbitrary_number)); 184 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 185 root->SetBlendMode(arbitrary_blend_mode)); 186 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 187 root->SetIsRootForIsolatedGroup(true)); 188 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetDrawsContent(true)); 189 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE( 190 root->SetSublayerTransform(arbitrary_transform)); 191 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetBounds(arbitrary_size)); 192 } 193 194 TEST(LayerImplTest, VerifyNeedsUpdateDrawProperties) { 195 FakeImplProxy proxy; 196 FakeLayerTreeHostImpl host_impl(&proxy); 197 EXPECT_TRUE(host_impl.InitializeRenderer(CreateFakeOutputSurface())); 198 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); 199 root->SetScrollable(true); 200 201 gfx::PointF arbitrary_point_f = gfx::PointF(0.125f, 0.25f); 202 float arbitrary_number = 0.352f; 203 gfx::Size arbitrary_size = gfx::Size(111, 222); 204 gfx::Point arbitrary_point = gfx::Point(333, 444); 205 gfx::Vector2d arbitrary_vector2d = gfx::Vector2d(111, 222); 206 gfx::Vector2d large_vector2d = gfx::Vector2d(1000, 1000); 207 gfx::Rect arbitrary_rect = gfx::Rect(arbitrary_point, arbitrary_size); 208 gfx::RectF arbitrary_rect_f = 209 gfx::RectF(arbitrary_point_f, gfx::SizeF(1.234f, 5.678f)); 210 SkColor arbitrary_color = SkColorSetRGB(10, 20, 30); 211 gfx::Transform arbitrary_transform; 212 arbitrary_transform.Scale3d(0.1f, 0.2f, 0.3f); 213 FilterOperations arbitrary_filters; 214 arbitrary_filters.Append(FilterOperation::CreateOpacityFilter(0.5f)); 215 SkXfermode::Mode arbitrary_blend_mode = SkXfermode::kMultiply_Mode; 216 217 // Related filter functions. 218 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetFilters(arbitrary_filters)); 219 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetFilters(arbitrary_filters)); 220 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetFilters(FilterOperations())); 221 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetFilters(arbitrary_filters)); 222 223 // Related scrolling functions. 224 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetMaxScrollOffset(large_vector2d)); 225 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 226 root->SetMaxScrollOffset(large_vector2d)); 227 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->ScrollBy(arbitrary_vector2d)); 228 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->ScrollBy(gfx::Vector2d())); 229 root->SetScrollDelta(gfx::Vector2d(0, 0)); 230 host_impl.ForcePrepareToDraw(); 231 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetScrollDelta(arbitrary_vector2d)); 232 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 233 root->SetScrollDelta(arbitrary_vector2d)); 234 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES( 235 root->SetScrollOffset(arbitrary_vector2d)); 236 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 237 root->SetScrollOffset(arbitrary_vector2d)); 238 239 // Unrelated functions, always set to new values, always set needs update. 240 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetAnchorPointZ(arbitrary_number)); 241 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES( 242 root->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 4))); 243 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetMasksToBounds(true)); 244 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetContentsOpaque(true)); 245 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES( 246 root->SetReplicaLayer(LayerImpl::Create(host_impl.active_tree(), 5))); 247 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetPosition(arbitrary_point_f)); 248 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetPreserves3d(true)); 249 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES( 250 root->SetDoubleSided(false)); // constructor initializes it to "true". 251 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetContentBounds(arbitrary_size)); 252 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES( 253 root->SetContentsScale(arbitrary_number, arbitrary_number)); 254 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetDrawsContent(true)); 255 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES( 256 root->SetBackgroundColor(arbitrary_color)); 257 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES( 258 root->SetBackgroundFilters(arbitrary_filters)); 259 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetOpacity(arbitrary_number)); 260 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetBlendMode(arbitrary_blend_mode)); 261 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetTransform(arbitrary_transform)); 262 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES( 263 root->SetSublayerTransform(arbitrary_transform)); 264 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetBounds(arbitrary_size)); 265 266 // Unrelated functions, set to the same values, no needs update. 267 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 268 root->SetAnchorPointZ(arbitrary_number)); 269 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetIsRootForIsolatedGroup(true)); 270 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetFilters(arbitrary_filters)); 271 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetMasksToBounds(true)); 272 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetContentsOpaque(true)); 273 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetPosition(arbitrary_point_f)); 274 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetPreserves3d(true)); 275 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 276 root->SetDoubleSided(false)); // constructor initializes it to "true". 277 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 278 root->SetContentBounds(arbitrary_size)); 279 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 280 root->SetContentsScale(arbitrary_number, arbitrary_number)); 281 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetDrawsContent(true)); 282 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 283 root->SetBackgroundColor(arbitrary_color)); 284 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 285 root->SetBackgroundFilters(arbitrary_filters)); 286 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetOpacity(arbitrary_number)); 287 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 288 root->SetBlendMode(arbitrary_blend_mode)); 289 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetIsRootForIsolatedGroup(true)); 290 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 291 root->SetTransform(arbitrary_transform)); 292 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES( 293 root->SetSublayerTransform(arbitrary_transform)); 294 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetBounds(arbitrary_size)); 295 } 296 297 TEST(LayerImplTest, SafeOpaqueBackgroundColor) { 298 FakeImplProxy proxy; 299 FakeLayerTreeHostImpl host_impl(&proxy); 300 EXPECT_TRUE(host_impl.InitializeRenderer(CreateFakeOutputSurface())); 301 scoped_ptr<LayerImpl> layer = LayerImpl::Create(host_impl.active_tree(), 1); 302 303 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) { 304 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) { 305 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) { 306 layer->SetContentsOpaque(!!contents_opaque); 307 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED 308 : SK_ColorTRANSPARENT); 309 host_impl.active_tree()->set_background_color( 310 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT); 311 312 SkColor safe_color = layer->SafeOpaqueBackgroundColor(); 313 if (contents_opaque) { 314 EXPECT_EQ(SkColorGetA(safe_color), 255u) 315 << "Flags: " << contents_opaque << ", " << layer_opaque << ", " 316 << host_opaque << "\n"; 317 } else { 318 EXPECT_NE(SkColorGetA(safe_color), 255u) 319 << "Flags: " << contents_opaque << ", " << layer_opaque << ", " 320 << host_opaque << "\n"; 321 } 322 } 323 } 324 } 325 } 326 327 class LayerImplScrollTest : public testing::Test { 328 public: 329 LayerImplScrollTest() : host_impl_(&proxy_), root_id_(7) { 330 host_impl_.active_tree() 331 ->SetRootLayer(LayerImpl::Create(host_impl_.active_tree(), root_id_)); 332 host_impl_.active_tree()->root_layer()->SetScrollable(true); 333 } 334 335 LayerImpl* layer() { return host_impl_.active_tree()->root_layer(); } 336 337 private: 338 FakeImplProxy proxy_; 339 FakeLayerTreeHostImpl host_impl_; 340 int root_id_; 341 }; 342 343 TEST_F(LayerImplScrollTest, ScrollByWithZeroOffset) { 344 // Test that LayerImpl::ScrollBy only affects ScrollDelta and total scroll 345 // offset is bounded by the range [0, max scroll offset]. 346 gfx::Vector2d max_scroll_offset(50, 80); 347 layer()->SetMaxScrollOffset(max_scroll_offset); 348 349 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->TotalScrollOffset()); 350 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset()); 351 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta()); 352 353 layer()->ScrollBy(gfx::Vector2dF(-100, 100)); 354 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset()); 355 356 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset()); 357 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset()); 358 359 layer()->ScrollBy(gfx::Vector2dF(100, -100)); 360 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset()); 361 362 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset()); 363 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset()); 364 } 365 366 TEST_F(LayerImplScrollTest, ScrollByWithNonZeroOffset) { 367 gfx::Vector2d max_scroll_offset(50, 80); 368 gfx::Vector2d scroll_offset(10, 5); 369 layer()->SetMaxScrollOffset(max_scroll_offset); 370 layer()->SetScrollOffset(scroll_offset); 371 372 EXPECT_VECTOR_EQ(scroll_offset, layer()->TotalScrollOffset()); 373 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 374 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta()); 375 376 layer()->ScrollBy(gfx::Vector2dF(-100, 100)); 377 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset()); 378 379 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset, 380 layer()->TotalScrollOffset()); 381 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 382 383 layer()->ScrollBy(gfx::Vector2dF(100, -100)); 384 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset()); 385 386 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset, 387 layer()->TotalScrollOffset()); 388 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 389 } 390 391 class ScrollDelegateIgnore : public LayerScrollOffsetDelegate { 392 public: 393 virtual void SetMaxScrollOffset(gfx::Vector2dF max_scroll_offset) OVERRIDE {} 394 virtual void SetTotalScrollOffset(gfx::Vector2dF new_value) OVERRIDE {} 395 virtual gfx::Vector2dF GetTotalScrollOffset() OVERRIDE { 396 return fixed_offset_; 397 } 398 virtual bool IsExternalFlingActive() const OVERRIDE { return false; } 399 400 void set_fixed_offset(gfx::Vector2dF fixed_offset) { 401 fixed_offset_ = fixed_offset; 402 } 403 404 virtual void SetTotalPageScaleFactor(float page_scale_factor) OVERRIDE {} 405 virtual void SetScrollableSize(gfx::SizeF scrollable_size) OVERRIDE {} 406 407 private: 408 gfx::Vector2dF fixed_offset_; 409 }; 410 411 TEST_F(LayerImplScrollTest, ScrollByWithIgnoringDelegate) { 412 gfx::Vector2d max_scroll_offset(50, 80); 413 gfx::Vector2d scroll_offset(10, 5); 414 layer()->SetMaxScrollOffset(max_scroll_offset); 415 layer()->SetScrollOffset(scroll_offset); 416 417 EXPECT_VECTOR_EQ(scroll_offset, layer()->TotalScrollOffset()); 418 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 419 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta()); 420 421 ScrollDelegateIgnore delegate; 422 gfx::Vector2dF fixed_offset(32, 12); 423 delegate.set_fixed_offset(fixed_offset); 424 layer()->SetScrollOffsetDelegate(&delegate); 425 426 EXPECT_VECTOR_EQ(fixed_offset, layer()->TotalScrollOffset()); 427 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 428 429 layer()->ScrollBy(gfx::Vector2dF(-100, 100)); 430 431 EXPECT_VECTOR_EQ(fixed_offset, layer()->TotalScrollOffset()); 432 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 433 434 layer()->SetScrollOffsetDelegate(NULL); 435 436 EXPECT_VECTOR_EQ(fixed_offset, layer()->TotalScrollOffset()); 437 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 438 439 gfx::Vector2dF scroll_delta(1, 1); 440 layer()->ScrollBy(scroll_delta); 441 442 EXPECT_VECTOR_EQ(fixed_offset + scroll_delta, layer()->TotalScrollOffset()); 443 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 444 } 445 446 class ScrollDelegateAccept : public LayerScrollOffsetDelegate { 447 public: 448 virtual void SetMaxScrollOffset(gfx::Vector2dF max_scroll_offset) OVERRIDE {} 449 virtual void SetTotalScrollOffset(gfx::Vector2dF new_value) OVERRIDE { 450 current_offset_ = new_value; 451 } 452 virtual gfx::Vector2dF GetTotalScrollOffset() OVERRIDE { 453 return current_offset_; 454 } 455 virtual bool IsExternalFlingActive() const OVERRIDE { return false; } 456 virtual void SetTotalPageScaleFactor(float page_scale_factor) OVERRIDE {} 457 virtual void SetScrollableSize(gfx::SizeF scrollable_size) OVERRIDE {} 458 459 private: 460 gfx::Vector2dF current_offset_; 461 }; 462 463 TEST_F(LayerImplScrollTest, ScrollByWithAcceptingDelegate) { 464 gfx::Vector2d max_scroll_offset(50, 80); 465 gfx::Vector2d scroll_offset(10, 5); 466 layer()->SetMaxScrollOffset(max_scroll_offset); 467 layer()->SetScrollOffset(scroll_offset); 468 469 EXPECT_VECTOR_EQ(scroll_offset, layer()->TotalScrollOffset()); 470 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 471 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta()); 472 473 ScrollDelegateAccept delegate; 474 layer()->SetScrollOffsetDelegate(&delegate); 475 476 EXPECT_VECTOR_EQ(scroll_offset, layer()->TotalScrollOffset()); 477 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 478 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta()); 479 480 layer()->ScrollBy(gfx::Vector2dF(-100, 100)); 481 482 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset()); 483 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 484 485 layer()->SetScrollOffsetDelegate(NULL); 486 487 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset()); 488 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 489 490 gfx::Vector2dF scroll_delta(1, 1); 491 layer()->ScrollBy(scroll_delta); 492 493 EXPECT_VECTOR_EQ(gfx::Vector2dF(1, 80), layer()->TotalScrollOffset()); 494 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 495 } 496 497 TEST_F(LayerImplScrollTest, ApplySentScrollsNoDelegate) { 498 gfx::Vector2d max_scroll_offset(50, 80); 499 gfx::Vector2d scroll_offset(10, 5); 500 gfx::Vector2dF scroll_delta(20.5f, 8.5f); 501 gfx::Vector2d sent_scroll_delta(12, -3); 502 503 layer()->SetMaxScrollOffset(max_scroll_offset); 504 layer()->SetScrollOffset(scroll_offset); 505 layer()->ScrollBy(scroll_delta); 506 layer()->SetSentScrollDelta(sent_scroll_delta); 507 508 EXPECT_VECTOR_EQ(scroll_offset + scroll_delta, layer()->TotalScrollOffset()); 509 EXPECT_VECTOR_EQ(scroll_delta, layer()->ScrollDelta()); 510 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 511 EXPECT_VECTOR_EQ(sent_scroll_delta, layer()->sent_scroll_delta()); 512 513 layer()->ApplySentScrollDeltasFromAbortedCommit(); 514 515 EXPECT_VECTOR_EQ(scroll_offset + scroll_delta, layer()->TotalScrollOffset()); 516 EXPECT_VECTOR_EQ(scroll_delta - sent_scroll_delta, layer()->ScrollDelta()); 517 EXPECT_VECTOR_EQ(scroll_offset + sent_scroll_delta, layer()->scroll_offset()); 518 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta()); 519 } 520 521 TEST_F(LayerImplScrollTest, ApplySentScrollsWithIgnoringDelegate) { 522 gfx::Vector2d max_scroll_offset(50, 80); 523 gfx::Vector2d scroll_offset(10, 5); 524 gfx::Vector2d sent_scroll_delta(12, -3); 525 gfx::Vector2dF fixed_offset(32, 12); 526 527 layer()->SetMaxScrollOffset(max_scroll_offset); 528 layer()->SetScrollOffset(scroll_offset); 529 ScrollDelegateIgnore delegate; 530 delegate.set_fixed_offset(fixed_offset); 531 layer()->SetScrollOffsetDelegate(&delegate); 532 layer()->SetSentScrollDelta(sent_scroll_delta); 533 534 EXPECT_VECTOR_EQ(fixed_offset, layer()->TotalScrollOffset()); 535 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 536 EXPECT_VECTOR_EQ(sent_scroll_delta, layer()->sent_scroll_delta()); 537 538 layer()->ApplySentScrollDeltasFromAbortedCommit(); 539 540 EXPECT_VECTOR_EQ(fixed_offset, layer()->TotalScrollOffset()); 541 EXPECT_VECTOR_EQ(scroll_offset + sent_scroll_delta, layer()->scroll_offset()); 542 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta()); 543 } 544 545 TEST_F(LayerImplScrollTest, ApplySentScrollsWithAcceptingDelegate) { 546 gfx::Vector2d max_scroll_offset(50, 80); 547 gfx::Vector2d scroll_offset(10, 5); 548 gfx::Vector2d sent_scroll_delta(12, -3); 549 gfx::Vector2dF scroll_delta(20.5f, 8.5f); 550 551 layer()->SetMaxScrollOffset(max_scroll_offset); 552 layer()->SetScrollOffset(scroll_offset); 553 ScrollDelegateAccept delegate; 554 layer()->SetScrollOffsetDelegate(&delegate); 555 layer()->ScrollBy(scroll_delta); 556 layer()->SetSentScrollDelta(sent_scroll_delta); 557 558 EXPECT_VECTOR_EQ(scroll_offset + scroll_delta, layer()->TotalScrollOffset()); 559 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset()); 560 EXPECT_VECTOR_EQ(sent_scroll_delta, layer()->sent_scroll_delta()); 561 562 layer()->ApplySentScrollDeltasFromAbortedCommit(); 563 564 EXPECT_VECTOR_EQ(scroll_offset + scroll_delta, layer()->TotalScrollOffset()); 565 EXPECT_VECTOR_EQ(scroll_offset + sent_scroll_delta, layer()->scroll_offset()); 566 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta()); 567 } 568 569 // The user-scrollability breaks for zoomed-in pages. So disable this. 570 // http://crbug.com/322223 571 TEST_F(LayerImplScrollTest, DISABLED_ScrollUserUnscrollableLayer) { 572 gfx::Vector2d max_scroll_offset(50, 80); 573 gfx::Vector2d scroll_offset(10, 5); 574 gfx::Vector2dF scroll_delta(20.5f, 8.5f); 575 576 layer()->set_user_scrollable_vertical(false); 577 layer()->SetMaxScrollOffset(max_scroll_offset); 578 layer()->SetScrollOffset(scroll_offset); 579 gfx::Vector2dF unscrolled = layer()->ScrollBy(scroll_delta); 580 581 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 8.5f), unscrolled); 582 EXPECT_VECTOR_EQ(gfx::Vector2dF(30.5f, 5), layer()->TotalScrollOffset()); 583 } 584 585 } // namespace 586 } // namespace cc 587