| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 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/trees/layer_tree_impl.h" |
| 6 |
| 7 #include "cc/layers/heads_up_display_layer_impl.h" |
| 8 #include "cc/test/fake_impl_proxy.h" |
| 9 #include "cc/test/fake_layer_tree_host_impl.h" |
| 10 #include "cc/test/geometry_test_utils.h" |
| 11 #include "cc/test/layer_tree_host_common_test.h" |
| 12 #include "cc/test/test_shared_bitmap_manager.h" |
| 13 #include "cc/trees/layer_tree_host_impl.h" |
| 14 #include "ui/gfx/size_conversions.h" |
| 15 |
| 16 namespace cc { |
| 17 namespace { |
| 18 |
| 19 typedef LayerTreeHostCommonTest LayerTreeImplTest; |
| 20 |
| 21 TEST_F(LayerTreeImplTest, HitTestingForSingleLayer) { |
| 22 FakeImplProxy proxy; |
| 23 TestSharedBitmapManager shared_bitmap_manager; |
| 24 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 25 scoped_ptr<LayerImpl> root = |
| 26 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 27 |
| 28 gfx::Transform identity_matrix; |
| 29 gfx::PointF anchor; |
| 30 gfx::PointF position; |
| 31 gfx::Size bounds(100, 100); |
| 32 SetLayerPropertiesForTesting( |
| 33 root.get(), identity_matrix, anchor, position, bounds, true, false); |
| 34 root->SetDrawsContent(true); |
| 35 |
| 36 LayerImplList render_surface_layer_list; |
| 37 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 38 root.get(), root->bounds(), &render_surface_layer_list); |
| 39 inputs.can_adjust_raster_scales = true; |
| 40 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 41 |
| 42 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 43 |
| 44 // Sanity check the scenario we just created. |
| 45 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 46 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 47 |
| 48 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 49 |
| 50 // Hit testing for a point outside the layer should return a null pointer. |
| 51 gfx::Point test_point(101, 101); |
| 52 LayerImpl* result_layer = |
| 53 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 54 EXPECT_FALSE(result_layer); |
| 55 |
| 56 test_point = gfx::Point(-1, -1); |
| 57 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 58 EXPECT_FALSE(result_layer); |
| 59 |
| 60 // Hit testing for a point inside should return the root layer. |
| 61 test_point = gfx::Point(1, 1); |
| 62 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 63 ASSERT_TRUE(result_layer); |
| 64 EXPECT_EQ(12345, result_layer->id()); |
| 65 |
| 66 test_point = gfx::Point(99, 99); |
| 67 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 68 ASSERT_TRUE(result_layer); |
| 69 EXPECT_EQ(12345, result_layer->id()); |
| 70 } |
| 71 |
| 72 TEST_F(LayerTreeImplTest, HitTestingForSingleLayerAndHud) { |
| 73 FakeImplProxy proxy; |
| 74 TestSharedBitmapManager shared_bitmap_manager; |
| 75 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 76 scoped_ptr<LayerImpl> root = |
| 77 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 78 scoped_ptr<HeadsUpDisplayLayerImpl> hud = |
| 79 HeadsUpDisplayLayerImpl::Create(host_impl.active_tree(), 11111); |
| 80 |
| 81 gfx::Transform identity_matrix; |
| 82 gfx::PointF anchor; |
| 83 gfx::PointF position; |
| 84 gfx::Size bounds(100, 100); |
| 85 SetLayerPropertiesForTesting( |
| 86 root.get(), identity_matrix, anchor, position, bounds, true, false); |
| 87 root->SetDrawsContent(true); |
| 88 |
| 89 // Create hud and add it as a child of root. |
| 90 gfx::Size hud_bounds(200, 200); |
| 91 SetLayerPropertiesForTesting( |
| 92 hud.get(), identity_matrix, anchor, position, hud_bounds, true, false); |
| 93 hud->SetDrawsContent(true); |
| 94 |
| 95 host_impl.active_tree()->set_hud_layer(hud.get()); |
| 96 root->AddChild(hud.PassAs<LayerImpl>()); |
| 97 |
| 98 LayerImplList render_surface_layer_list; |
| 99 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 100 root.get(), hud_bounds, &render_surface_layer_list); |
| 101 inputs.can_adjust_raster_scales = true; |
| 102 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 103 |
| 104 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 105 |
| 106 // Sanity check the scenario we just created. |
| 107 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 108 ASSERT_EQ(2u, root->render_surface()->layer_list().size()); |
| 109 |
| 110 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 111 |
| 112 // Hit testing for a point inside HUD, but outside root should return null |
| 113 gfx::Point test_point(101, 101); |
| 114 LayerImpl* result_layer = |
| 115 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 116 EXPECT_FALSE(result_layer); |
| 117 |
| 118 test_point = gfx::Point(-1, -1); |
| 119 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 120 EXPECT_FALSE(result_layer); |
| 121 |
| 122 // Hit testing for a point inside should return the root layer, never the HUD |
| 123 // layer. |
| 124 test_point = gfx::Point(1, 1); |
| 125 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 126 ASSERT_TRUE(result_layer); |
| 127 EXPECT_EQ(12345, result_layer->id()); |
| 128 |
| 129 test_point = gfx::Point(99, 99); |
| 130 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 131 ASSERT_TRUE(result_layer); |
| 132 EXPECT_EQ(12345, result_layer->id()); |
| 133 } |
| 134 |
| 135 TEST_F(LayerTreeImplTest, HitTestingForUninvertibleTransform) { |
| 136 FakeImplProxy proxy; |
| 137 TestSharedBitmapManager shared_bitmap_manager; |
| 138 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 139 scoped_ptr<LayerImpl> root = |
| 140 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 141 |
| 142 gfx::Transform uninvertible_transform; |
| 143 uninvertible_transform.matrix().set(0, 0, 0.0); |
| 144 uninvertible_transform.matrix().set(1, 1, 0.0); |
| 145 uninvertible_transform.matrix().set(2, 2, 0.0); |
| 146 uninvertible_transform.matrix().set(3, 3, 0.0); |
| 147 ASSERT_FALSE(uninvertible_transform.IsInvertible()); |
| 148 |
| 149 gfx::Transform identity_matrix; |
| 150 gfx::PointF anchor; |
| 151 gfx::PointF position; |
| 152 gfx::Size bounds(100, 100); |
| 153 SetLayerPropertiesForTesting(root.get(), |
| 154 uninvertible_transform, |
| 155 anchor, |
| 156 position, |
| 157 bounds, |
| 158 true, |
| 159 false); |
| 160 root->SetDrawsContent(true); |
| 161 |
| 162 LayerImplList render_surface_layer_list; |
| 163 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 164 root.get(), root->bounds(), &render_surface_layer_list); |
| 165 inputs.can_adjust_raster_scales = true; |
| 166 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 167 |
| 168 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 169 |
| 170 // Sanity check the scenario we just created. |
| 171 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 172 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 173 ASSERT_FALSE(root->screen_space_transform().IsInvertible()); |
| 174 |
| 175 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 176 |
| 177 // Hit testing any point should not hit the layer. If the invertible matrix is |
| 178 // accidentally ignored and treated like an identity, then the hit testing |
| 179 // will incorrectly hit the layer when it shouldn't. |
| 180 gfx::Point test_point(1, 1); |
| 181 LayerImpl* result_layer = |
| 182 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 183 EXPECT_FALSE(result_layer); |
| 184 |
| 185 test_point = gfx::Point(10, 10); |
| 186 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 187 EXPECT_FALSE(result_layer); |
| 188 |
| 189 test_point = gfx::Point(10, 30); |
| 190 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 191 EXPECT_FALSE(result_layer); |
| 192 |
| 193 test_point = gfx::Point(50, 50); |
| 194 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 195 EXPECT_FALSE(result_layer); |
| 196 |
| 197 test_point = gfx::Point(67, 48); |
| 198 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 199 EXPECT_FALSE(result_layer); |
| 200 |
| 201 test_point = gfx::Point(99, 99); |
| 202 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 203 EXPECT_FALSE(result_layer); |
| 204 |
| 205 test_point = gfx::Point(-1, -1); |
| 206 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 207 EXPECT_FALSE(result_layer); |
| 208 } |
| 209 |
| 210 TEST_F(LayerTreeImplTest, HitTestingForSinglePositionedLayer) { |
| 211 FakeImplProxy proxy; |
| 212 TestSharedBitmapManager shared_bitmap_manager; |
| 213 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 214 scoped_ptr<LayerImpl> root = |
| 215 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 216 |
| 217 gfx::Transform identity_matrix; |
| 218 gfx::PointF anchor; |
| 219 // this layer is positioned, and hit testing should correctly know where the |
| 220 // layer is located. |
| 221 gfx::PointF position(50.f, 50.f); |
| 222 gfx::Size bounds(100, 100); |
| 223 SetLayerPropertiesForTesting( |
| 224 root.get(), identity_matrix, anchor, position, bounds, true, false); |
| 225 root->SetDrawsContent(true); |
| 226 |
| 227 LayerImplList render_surface_layer_list; |
| 228 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 229 root.get(), root->bounds(), &render_surface_layer_list); |
| 230 inputs.can_adjust_raster_scales = true; |
| 231 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 232 |
| 233 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 234 |
| 235 // Sanity check the scenario we just created. |
| 236 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 237 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 238 |
| 239 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 240 |
| 241 // Hit testing for a point outside the layer should return a null pointer. |
| 242 gfx::Point test_point(49, 49); |
| 243 LayerImpl* result_layer = |
| 244 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 245 EXPECT_FALSE(result_layer); |
| 246 |
| 247 // Even though the layer exists at (101, 101), it should not be visible there |
| 248 // since the root render surface would clamp it. |
| 249 test_point = gfx::Point(101, 101); |
| 250 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 251 EXPECT_FALSE(result_layer); |
| 252 |
| 253 // Hit testing for a point inside should return the root layer. |
| 254 test_point = gfx::Point(51, 51); |
| 255 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 256 ASSERT_TRUE(result_layer); |
| 257 EXPECT_EQ(12345, result_layer->id()); |
| 258 |
| 259 test_point = gfx::Point(99, 99); |
| 260 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 261 ASSERT_TRUE(result_layer); |
| 262 EXPECT_EQ(12345, result_layer->id()); |
| 263 } |
| 264 |
| 265 TEST_F(LayerTreeImplTest, HitTestingForSingleRotatedLayer) { |
| 266 FakeImplProxy proxy; |
| 267 TestSharedBitmapManager shared_bitmap_manager; |
| 268 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 269 scoped_ptr<LayerImpl> root = |
| 270 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 271 |
| 272 gfx::Transform identity_matrix; |
| 273 gfx::Transform rotation45_degrees_about_center; |
| 274 rotation45_degrees_about_center.Translate(50.0, 50.0); |
| 275 rotation45_degrees_about_center.RotateAboutZAxis(45.0); |
| 276 rotation45_degrees_about_center.Translate(-50.0, -50.0); |
| 277 gfx::PointF anchor; |
| 278 gfx::PointF position; |
| 279 gfx::Size bounds(100, 100); |
| 280 SetLayerPropertiesForTesting(root.get(), |
| 281 rotation45_degrees_about_center, |
| 282 anchor, |
| 283 position, |
| 284 bounds, |
| 285 true, |
| 286 false); |
| 287 root->SetDrawsContent(true); |
| 288 |
| 289 LayerImplList render_surface_layer_list; |
| 290 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 291 root.get(), root->bounds(), &render_surface_layer_list); |
| 292 inputs.can_adjust_raster_scales = true; |
| 293 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 294 |
| 295 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 296 |
| 297 // Sanity check the scenario we just created. |
| 298 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 299 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 300 |
| 301 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 302 |
| 303 // Hit testing for points outside the layer. |
| 304 // These corners would have been inside the un-transformed layer, but they |
| 305 // should not hit the correctly transformed layer. |
| 306 gfx::Point test_point(99, 99); |
| 307 LayerImpl* result_layer = |
| 308 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 309 EXPECT_FALSE(result_layer); |
| 310 |
| 311 test_point = gfx::Point(1, 1); |
| 312 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 313 EXPECT_FALSE(result_layer); |
| 314 |
| 315 // Hit testing for a point inside should return the root layer. |
| 316 test_point = gfx::Point(1, 50); |
| 317 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 318 ASSERT_TRUE(result_layer); |
| 319 EXPECT_EQ(12345, result_layer->id()); |
| 320 |
| 321 // Hit testing the corners that would overlap the unclipped layer, but are |
| 322 // outside the clipped region. |
| 323 test_point = gfx::Point(50, -1); |
| 324 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 325 ASSERT_FALSE(result_layer); |
| 326 |
| 327 test_point = gfx::Point(-1, 50); |
| 328 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 329 ASSERT_FALSE(result_layer); |
| 330 } |
| 331 |
| 332 TEST_F(LayerTreeImplTest, HitTestingForSinglePerspectiveLayer) { |
| 333 FakeImplProxy proxy; |
| 334 TestSharedBitmapManager shared_bitmap_manager; |
| 335 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 336 scoped_ptr<LayerImpl> root = |
| 337 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 338 |
| 339 gfx::Transform identity_matrix; |
| 340 |
| 341 // perspective_projection_about_center * translation_by_z is designed so that |
| 342 // the 100 x 100 layer becomes 50 x 50, and remains centered at (50, 50). |
| 343 gfx::Transform perspective_projection_about_center; |
| 344 perspective_projection_about_center.Translate(50.0, 50.0); |
| 345 perspective_projection_about_center.ApplyPerspectiveDepth(1.0); |
| 346 perspective_projection_about_center.Translate(-50.0, -50.0); |
| 347 gfx::Transform translation_by_z; |
| 348 translation_by_z.Translate3d(0.0, 0.0, -1.0); |
| 349 |
| 350 gfx::PointF anchor; |
| 351 gfx::PointF position; |
| 352 gfx::Size bounds(100, 100); |
| 353 SetLayerPropertiesForTesting( |
| 354 root.get(), |
| 355 perspective_projection_about_center * translation_by_z, |
| 356 anchor, |
| 357 position, |
| 358 bounds, |
| 359 true, |
| 360 false); |
| 361 root->SetDrawsContent(true); |
| 362 |
| 363 LayerImplList render_surface_layer_list; |
| 364 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 365 root.get(), root->bounds(), &render_surface_layer_list); |
| 366 inputs.can_adjust_raster_scales = true; |
| 367 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 368 |
| 369 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 370 |
| 371 // Sanity check the scenario we just created. |
| 372 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 373 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 374 |
| 375 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 376 |
| 377 // Hit testing for points outside the layer. |
| 378 // These corners would have been inside the un-transformed layer, but they |
| 379 // should not hit the correctly transformed layer. |
| 380 gfx::Point test_point(24, 24); |
| 381 LayerImpl* result_layer = |
| 382 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 383 EXPECT_FALSE(result_layer); |
| 384 |
| 385 test_point = gfx::Point(76, 76); |
| 386 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 387 EXPECT_FALSE(result_layer); |
| 388 |
| 389 // Hit testing for a point inside should return the root layer. |
| 390 test_point = gfx::Point(26, 26); |
| 391 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 392 ASSERT_TRUE(result_layer); |
| 393 EXPECT_EQ(12345, result_layer->id()); |
| 394 |
| 395 test_point = gfx::Point(74, 74); |
| 396 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 397 ASSERT_TRUE(result_layer); |
| 398 EXPECT_EQ(12345, result_layer->id()); |
| 399 } |
| 400 |
| 401 TEST_F(LayerTreeImplTest, HitTestingForSingleLayerWithScaledContents) { |
| 402 // A layer's visible content rect is actually in the layer's content space. |
| 403 // The screen space transform converts from the layer's origin space to screen |
| 404 // space. This test makes sure that hit testing works correctly accounts for |
| 405 // the contents scale. A contents scale that is not 1 effectively forces a |
| 406 // non-identity transform between layer's content space and layer's origin |
| 407 // space. The hit testing code must take this into account. |
| 408 // |
| 409 // To test this, the layer is positioned at (25, 25), and is size (50, 50). If |
| 410 // contents scale is ignored, then hit testing will mis-interpret the visible |
| 411 // content rect as being larger than the actual bounds of the layer. |
| 412 // |
| 413 FakeImplProxy proxy; |
| 414 TestSharedBitmapManager shared_bitmap_manager; |
| 415 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 416 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); |
| 417 |
| 418 gfx::Transform identity_matrix; |
| 419 gfx::PointF anchor; |
| 420 |
| 421 SetLayerPropertiesForTesting(root.get(), |
| 422 identity_matrix, |
| 423 anchor, |
| 424 gfx::PointF(), |
| 425 gfx::Size(100, 100), |
| 426 true, |
| 427 false); |
| 428 { |
| 429 gfx::PointF position(25.f, 25.f); |
| 430 gfx::Size bounds(50, 50); |
| 431 scoped_ptr<LayerImpl> test_layer = |
| 432 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 433 SetLayerPropertiesForTesting(test_layer.get(), |
| 434 identity_matrix, |
| 435 anchor, |
| 436 position, |
| 437 bounds, |
| 438 true, |
| 439 false); |
| 440 |
| 441 // override content bounds and contents scale |
| 442 test_layer->SetContentBounds(gfx::Size(100, 100)); |
| 443 test_layer->SetContentsScale(2, 2); |
| 444 |
| 445 test_layer->SetDrawsContent(true); |
| 446 root->AddChild(test_layer.Pass()); |
| 447 } |
| 448 |
| 449 LayerImplList render_surface_layer_list; |
| 450 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 451 root.get(), root->bounds(), &render_surface_layer_list); |
| 452 inputs.can_adjust_raster_scales = true; |
| 453 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 454 |
| 455 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 456 |
| 457 // Sanity check the scenario we just created. |
| 458 // The visible content rect for test_layer is actually 100x100, even though |
| 459 // its layout size is 50x50, positioned at 25x25. |
| 460 LayerImpl* test_layer = root->children()[0]; |
| 461 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), test_layer->visible_content_rect()); |
| 462 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 463 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 464 |
| 465 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 466 |
| 467 // Hit testing for a point outside the layer should return a null pointer (the |
| 468 // root layer does not draw content, so it will not be hit tested either). |
| 469 gfx::Point test_point(101, 101); |
| 470 LayerImpl* result_layer = |
| 471 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 472 EXPECT_FALSE(result_layer); |
| 473 |
| 474 test_point = gfx::Point(24, 24); |
| 475 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 476 EXPECT_FALSE(result_layer); |
| 477 |
| 478 test_point = gfx::Point(76, 76); |
| 479 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 480 EXPECT_FALSE(result_layer); |
| 481 |
| 482 // Hit testing for a point inside should return the test layer. |
| 483 test_point = gfx::Point(26, 26); |
| 484 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 485 ASSERT_TRUE(result_layer); |
| 486 EXPECT_EQ(12345, result_layer->id()); |
| 487 |
| 488 test_point = gfx::Point(74, 74); |
| 489 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 490 ASSERT_TRUE(result_layer); |
| 491 EXPECT_EQ(12345, result_layer->id()); |
| 492 } |
| 493 |
| 494 TEST_F(LayerTreeImplTest, HitTestingForSimpleClippedLayer) { |
| 495 // Test that hit-testing will only work for the visible portion of a layer, |
| 496 // and not the entire layer bounds. Here we just test the simple axis-aligned |
| 497 // case. |
| 498 gfx::Transform identity_matrix; |
| 499 gfx::PointF anchor; |
| 500 |
| 501 FakeImplProxy proxy; |
| 502 TestSharedBitmapManager shared_bitmap_manager; |
| 503 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 504 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); |
| 505 SetLayerPropertiesForTesting(root.get(), |
| 506 identity_matrix, |
| 507 anchor, |
| 508 gfx::PointF(), |
| 509 gfx::Size(100, 100), |
| 510 true, |
| 511 false); |
| 512 { |
| 513 scoped_ptr<LayerImpl> clipping_layer = |
| 514 LayerImpl::Create(host_impl.active_tree(), 123); |
| 515 // this layer is positioned, and hit testing should correctly know where the |
| 516 // layer is located. |
| 517 gfx::PointF position(25.f, 25.f); |
| 518 gfx::Size bounds(50, 50); |
| 519 SetLayerPropertiesForTesting(clipping_layer.get(), |
| 520 identity_matrix, |
| 521 anchor, |
| 522 position, |
| 523 bounds, |
| 524 true, |
| 525 false); |
| 526 clipping_layer->SetMasksToBounds(true); |
| 527 |
| 528 scoped_ptr<LayerImpl> child = |
| 529 LayerImpl::Create(host_impl.active_tree(), 456); |
| 530 position = gfx::PointF(-50.f, -50.f); |
| 531 bounds = gfx::Size(300, 300); |
| 532 SetLayerPropertiesForTesting( |
| 533 child.get(), identity_matrix, anchor, position, bounds, true, false); |
| 534 child->SetDrawsContent(true); |
| 535 clipping_layer->AddChild(child.Pass()); |
| 536 root->AddChild(clipping_layer.Pass()); |
| 537 } |
| 538 |
| 539 LayerImplList render_surface_layer_list; |
| 540 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 541 root.get(), root->bounds(), &render_surface_layer_list); |
| 542 inputs.can_adjust_raster_scales = true; |
| 543 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 544 |
| 545 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 546 |
| 547 // Sanity check the scenario we just created. |
| 548 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 549 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 550 ASSERT_EQ(456, root->render_surface()->layer_list().at(0)->id()); |
| 551 |
| 552 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 553 |
| 554 // Hit testing for a point outside the layer should return a null pointer. |
| 555 // Despite the child layer being very large, it should be clipped to the root |
| 556 // layer's bounds. |
| 557 gfx::Point test_point(24, 24); |
| 558 LayerImpl* result_layer = |
| 559 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 560 EXPECT_FALSE(result_layer); |
| 561 |
| 562 // Even though the layer exists at (101, 101), it should not be visible there |
| 563 // since the clipping_layer would clamp it. |
| 564 test_point = gfx::Point(76, 76); |
| 565 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 566 EXPECT_FALSE(result_layer); |
| 567 |
| 568 // Hit testing for a point inside should return the child layer. |
| 569 test_point = gfx::Point(26, 26); |
| 570 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 571 ASSERT_TRUE(result_layer); |
| 572 EXPECT_EQ(456, result_layer->id()); |
| 573 |
| 574 test_point = gfx::Point(74, 74); |
| 575 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 576 ASSERT_TRUE(result_layer); |
| 577 EXPECT_EQ(456, result_layer->id()); |
| 578 } |
| 579 |
| 580 TEST_F(LayerTreeImplTest, HitTestingForMultiClippedRotatedLayer) { |
| 581 // This test checks whether hit testing correctly avoids hit testing with |
| 582 // multiple ancestors that clip in non axis-aligned ways. To pass this test, |
| 583 // the hit testing algorithm needs to recognize that multiple parent layers |
| 584 // may clip the layer, and should not actually hit those clipped areas. |
| 585 // |
| 586 // The child and grand_child layers are both initialized to clip the |
| 587 // rotated_leaf. The child layer is rotated about the top-left corner, so that |
| 588 // the root + child clips combined create a triangle. The rotated_leaf will |
| 589 // only be visible where it overlaps this triangle. |
| 590 // |
| 591 FakeImplProxy proxy; |
| 592 TestSharedBitmapManager shared_bitmap_manager; |
| 593 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 594 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 123); |
| 595 |
| 596 gfx::Transform identity_matrix; |
| 597 gfx::PointF anchor; |
| 598 gfx::PointF position; |
| 599 gfx::Size bounds(100, 100); |
| 600 SetLayerPropertiesForTesting( |
| 601 root.get(), identity_matrix, anchor, position, bounds, true, false); |
| 602 root->SetMasksToBounds(true); |
| 603 { |
| 604 scoped_ptr<LayerImpl> child = |
| 605 LayerImpl::Create(host_impl.active_tree(), 456); |
| 606 scoped_ptr<LayerImpl> grand_child = |
| 607 LayerImpl::Create(host_impl.active_tree(), 789); |
| 608 scoped_ptr<LayerImpl> rotated_leaf = |
| 609 LayerImpl::Create(host_impl.active_tree(), 2468); |
| 610 |
| 611 position = gfx::PointF(10.f, 10.f); |
| 612 bounds = gfx::Size(80, 80); |
| 613 SetLayerPropertiesForTesting( |
| 614 child.get(), identity_matrix, anchor, position, bounds, true, false); |
| 615 child->SetMasksToBounds(true); |
| 616 |
| 617 gfx::Transform rotation45_degrees_about_corner; |
| 618 rotation45_degrees_about_corner.RotateAboutZAxis(45.0); |
| 619 |
| 620 // remember, positioned with respect to its parent which is already at 10, |
| 621 // 10 |
| 622 position = gfx::PointF(); |
| 623 bounds = |
| 624 gfx::Size(200, 200); // to ensure it covers at least sqrt(2) * 100. |
| 625 SetLayerPropertiesForTesting(grand_child.get(), |
| 626 rotation45_degrees_about_corner, |
| 627 anchor, |
| 628 position, |
| 629 bounds, |
| 630 true, |
| 631 false); |
| 632 grand_child->SetMasksToBounds(true); |
| 633 |
| 634 // Rotates about the center of the layer |
| 635 gfx::Transform rotated_leaf_transform; |
| 636 rotated_leaf_transform.Translate( |
| 637 -10.0, -10.0); // cancel out the grand_parent's position |
| 638 rotated_leaf_transform.RotateAboutZAxis( |
| 639 -45.0); // cancel out the corner 45-degree rotation of the parent. |
| 640 rotated_leaf_transform.Translate(50.0, 50.0); |
| 641 rotated_leaf_transform.RotateAboutZAxis(45.0); |
| 642 rotated_leaf_transform.Translate(-50.0, -50.0); |
| 643 position = gfx::PointF(); |
| 644 bounds = gfx::Size(100, 100); |
| 645 SetLayerPropertiesForTesting(rotated_leaf.get(), |
| 646 rotated_leaf_transform, |
| 647 anchor, |
| 648 position, |
| 649 bounds, |
| 650 true, |
| 651 false); |
| 652 rotated_leaf->SetDrawsContent(true); |
| 653 |
| 654 grand_child->AddChild(rotated_leaf.Pass()); |
| 655 child->AddChild(grand_child.Pass()); |
| 656 root->AddChild(child.Pass()); |
| 657 } |
| 658 |
| 659 LayerImplList render_surface_layer_list; |
| 660 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 661 root.get(), root->bounds(), &render_surface_layer_list); |
| 662 inputs.can_adjust_raster_scales = true; |
| 663 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 664 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 665 |
| 666 // Sanity check the scenario we just created. |
| 667 // The grand_child is expected to create a render surface because it |
| 668 // MasksToBounds and is not axis aligned. |
| 669 ASSERT_EQ(2u, render_surface_layer_list.size()); |
| 670 ASSERT_EQ( |
| 671 1u, |
| 672 render_surface_layer_list.at(0)->render_surface()->layer_list().size()); |
| 673 ASSERT_EQ(789, |
| 674 render_surface_layer_list.at(0) |
| 675 ->render_surface() |
| 676 ->layer_list() |
| 677 .at(0) |
| 678 ->id()); // grand_child's surface. |
| 679 ASSERT_EQ( |
| 680 1u, |
| 681 render_surface_layer_list.at(1)->render_surface()->layer_list().size()); |
| 682 ASSERT_EQ( |
| 683 2468, |
| 684 render_surface_layer_list[1]->render_surface()->layer_list().at(0)->id()); |
| 685 |
| 686 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 687 |
| 688 // (11, 89) is close to the the bottom left corner within the clip, but it is |
| 689 // not inside the layer. |
| 690 gfx::Point test_point(11, 89); |
| 691 LayerImpl* result_layer = |
| 692 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 693 EXPECT_FALSE(result_layer); |
| 694 |
| 695 // Closer inwards from the bottom left will overlap the layer. |
| 696 test_point = gfx::Point(25, 75); |
| 697 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 698 ASSERT_TRUE(result_layer); |
| 699 EXPECT_EQ(2468, result_layer->id()); |
| 700 |
| 701 // (4, 50) is inside the unclipped layer, but that corner of the layer should |
| 702 // be clipped away by the grandparent and should not get hit. If hit testing |
| 703 // blindly uses visible content rect without considering how parent may clip |
| 704 // the layer, then hit testing would accidentally think that the point |
| 705 // successfully hits the layer. |
| 706 test_point = gfx::Point(4, 50); |
| 707 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 708 EXPECT_FALSE(result_layer); |
| 709 |
| 710 // (11, 50) is inside the layer and within the clipped area. |
| 711 test_point = gfx::Point(11, 50); |
| 712 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 713 ASSERT_TRUE(result_layer); |
| 714 EXPECT_EQ(2468, result_layer->id()); |
| 715 |
| 716 // Around the middle, just to the right and up, would have hit the layer |
| 717 // except that that area should be clipped away by the parent. |
| 718 test_point = gfx::Point(51, 49); |
| 719 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 720 EXPECT_FALSE(result_layer); |
| 721 |
| 722 // Around the middle, just to the left and down, should successfully hit the |
| 723 // layer. |
| 724 test_point = gfx::Point(49, 51); |
| 725 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 726 ASSERT_TRUE(result_layer); |
| 727 EXPECT_EQ(2468, result_layer->id()); |
| 728 } |
| 729 |
| 730 TEST_F(LayerTreeImplTest, HitTestingForNonClippingIntermediateLayer) { |
| 731 // This test checks that hit testing code does not accidentally clip to layer |
| 732 // bounds for a layer that actually does not clip. |
| 733 gfx::Transform identity_matrix; |
| 734 gfx::PointF anchor; |
| 735 |
| 736 FakeImplProxy proxy; |
| 737 TestSharedBitmapManager shared_bitmap_manager; |
| 738 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 739 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); |
| 740 SetLayerPropertiesForTesting(root.get(), |
| 741 identity_matrix, |
| 742 anchor, |
| 743 gfx::PointF(), |
| 744 gfx::Size(100, 100), |
| 745 true, |
| 746 false); |
| 747 { |
| 748 scoped_ptr<LayerImpl> intermediate_layer = |
| 749 LayerImpl::Create(host_impl.active_tree(), 123); |
| 750 // this layer is positioned, and hit testing should correctly know where the |
| 751 // layer is located. |
| 752 gfx::PointF position(10.f, 10.f); |
| 753 gfx::Size bounds(50, 50); |
| 754 SetLayerPropertiesForTesting(intermediate_layer.get(), |
| 755 identity_matrix, |
| 756 anchor, |
| 757 position, |
| 758 bounds, |
| 759 true, |
| 760 false); |
| 761 // Sanity check the intermediate layer should not clip. |
| 762 ASSERT_FALSE(intermediate_layer->masks_to_bounds()); |
| 763 ASSERT_FALSE(intermediate_layer->mask_layer()); |
| 764 |
| 765 // The child of the intermediate_layer is translated so that it does not |
| 766 // overlap intermediate_layer at all. If child is incorrectly clipped, we |
| 767 // would not be able to hit it successfully. |
| 768 scoped_ptr<LayerImpl> child = |
| 769 LayerImpl::Create(host_impl.active_tree(), 456); |
| 770 position = gfx::PointF(60.f, 60.f); // 70, 70 in screen space |
| 771 bounds = gfx::Size(20, 20); |
| 772 SetLayerPropertiesForTesting( |
| 773 child.get(), identity_matrix, anchor, position, bounds, true, false); |
| 774 child->SetDrawsContent(true); |
| 775 intermediate_layer->AddChild(child.Pass()); |
| 776 root->AddChild(intermediate_layer.Pass()); |
| 777 } |
| 778 |
| 779 LayerImplList render_surface_layer_list; |
| 780 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 781 root.get(), root->bounds(), &render_surface_layer_list); |
| 782 inputs.can_adjust_raster_scales = true; |
| 783 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 784 |
| 785 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 786 |
| 787 // Sanity check the scenario we just created. |
| 788 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 789 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 790 ASSERT_EQ(456, root->render_surface()->layer_list().at(0)->id()); |
| 791 |
| 792 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 793 |
| 794 // Hit testing for a point outside the layer should return a null pointer. |
| 795 gfx::Point test_point(69, 69); |
| 796 LayerImpl* result_layer = |
| 797 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 798 EXPECT_FALSE(result_layer); |
| 799 |
| 800 test_point = gfx::Point(91, 91); |
| 801 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 802 EXPECT_FALSE(result_layer); |
| 803 |
| 804 // Hit testing for a point inside should return the child layer. |
| 805 test_point = gfx::Point(71, 71); |
| 806 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 807 ASSERT_TRUE(result_layer); |
| 808 EXPECT_EQ(456, result_layer->id()); |
| 809 |
| 810 test_point = gfx::Point(89, 89); |
| 811 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 812 ASSERT_TRUE(result_layer); |
| 813 EXPECT_EQ(456, result_layer->id()); |
| 814 } |
| 815 |
| 816 TEST_F(LayerTreeImplTest, HitTestingForMultipleLayers) { |
| 817 FakeImplProxy proxy; |
| 818 TestSharedBitmapManager shared_bitmap_manager; |
| 819 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 820 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); |
| 821 |
| 822 gfx::Transform identity_matrix; |
| 823 gfx::PointF anchor; |
| 824 gfx::PointF position; |
| 825 gfx::Size bounds(100, 100); |
| 826 SetLayerPropertiesForTesting( |
| 827 root.get(), identity_matrix, anchor, position, bounds, true, false); |
| 828 root->SetDrawsContent(true); |
| 829 { |
| 830 // child 1 and child2 are initialized to overlap between x=50 and x=60. |
| 831 // grand_child is set to overlap both child1 and child2 between y=50 and |
| 832 // y=60. The expected stacking order is: (front) child2, (second) |
| 833 // grand_child, (third) child1, and (back) the root layer behind all other |
| 834 // layers. |
| 835 |
| 836 scoped_ptr<LayerImpl> child1 = |
| 837 LayerImpl::Create(host_impl.active_tree(), 2); |
| 838 scoped_ptr<LayerImpl> child2 = |
| 839 LayerImpl::Create(host_impl.active_tree(), 3); |
| 840 scoped_ptr<LayerImpl> grand_child1 = |
| 841 LayerImpl::Create(host_impl.active_tree(), 4); |
| 842 |
| 843 position = gfx::PointF(10.f, 10.f); |
| 844 bounds = gfx::Size(50, 50); |
| 845 SetLayerPropertiesForTesting( |
| 846 child1.get(), identity_matrix, anchor, position, bounds, true, false); |
| 847 child1->SetDrawsContent(true); |
| 848 |
| 849 position = gfx::PointF(50.f, 10.f); |
| 850 bounds = gfx::Size(50, 50); |
| 851 SetLayerPropertiesForTesting( |
| 852 child2.get(), identity_matrix, anchor, position, bounds, true, false); |
| 853 child2->SetDrawsContent(true); |
| 854 |
| 855 // Remember that grand_child is positioned with respect to its parent (i.e. |
| 856 // child1). In screen space, the intended position is (10, 50), with size |
| 857 // 100 x 50. |
| 858 position = gfx::PointF(0.f, 40.f); |
| 859 bounds = gfx::Size(100, 50); |
| 860 SetLayerPropertiesForTesting(grand_child1.get(), |
| 861 identity_matrix, |
| 862 anchor, |
| 863 position, |
| 864 bounds, |
| 865 true, |
| 866 false); |
| 867 grand_child1->SetDrawsContent(true); |
| 868 |
| 869 child1->AddChild(grand_child1.Pass()); |
| 870 root->AddChild(child1.Pass()); |
| 871 root->AddChild(child2.Pass()); |
| 872 } |
| 873 |
| 874 LayerImpl* child1 = root->children()[0]; |
| 875 LayerImpl* child2 = root->children()[1]; |
| 876 LayerImpl* grand_child1 = child1->children()[0]; |
| 877 |
| 878 LayerImplList render_surface_layer_list; |
| 879 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 880 root.get(), root->bounds(), &render_surface_layer_list); |
| 881 inputs.can_adjust_raster_scales = true; |
| 882 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 883 |
| 884 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 885 |
| 886 // Sanity check the scenario we just created. |
| 887 ASSERT_TRUE(child1); |
| 888 ASSERT_TRUE(child2); |
| 889 ASSERT_TRUE(grand_child1); |
| 890 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 891 |
| 892 RenderSurfaceImpl* root_render_surface = root->render_surface(); |
| 893 ASSERT_EQ(4u, root_render_surface->layer_list().size()); |
| 894 ASSERT_EQ(1, root_render_surface->layer_list().at(0)->id()); // root layer |
| 895 ASSERT_EQ(2, root_render_surface->layer_list().at(1)->id()); // child1 |
| 896 ASSERT_EQ(4, root_render_surface->layer_list().at(2)->id()); // grand_child1 |
| 897 ASSERT_EQ(3, root_render_surface->layer_list().at(3)->id()); // child2 |
| 898 |
| 899 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 900 |
| 901 // Nothing overlaps the root_layer at (1, 1), so hit testing there should find |
| 902 // the root layer. |
| 903 gfx::Point test_point = gfx::Point(1, 1); |
| 904 LayerImpl* result_layer = |
| 905 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 906 ASSERT_TRUE(result_layer); |
| 907 EXPECT_EQ(1, result_layer->id()); |
| 908 |
| 909 // At (15, 15), child1 and root are the only layers. child1 is expected to be |
| 910 // on top. |
| 911 test_point = gfx::Point(15, 15); |
| 912 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 913 ASSERT_TRUE(result_layer); |
| 914 EXPECT_EQ(2, result_layer->id()); |
| 915 |
| 916 // At (51, 20), child1 and child2 overlap. child2 is expected to be on top. |
| 917 test_point = gfx::Point(51, 20); |
| 918 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 919 ASSERT_TRUE(result_layer); |
| 920 EXPECT_EQ(3, result_layer->id()); |
| 921 |
| 922 // At (80, 51), child2 and grand_child1 overlap. child2 is expected to be on |
| 923 // top. |
| 924 test_point = gfx::Point(80, 51); |
| 925 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 926 ASSERT_TRUE(result_layer); |
| 927 EXPECT_EQ(3, result_layer->id()); |
| 928 |
| 929 // At (51, 51), all layers overlap each other. child2 is expected to be on top |
| 930 // of all other layers. |
| 931 test_point = gfx::Point(51, 51); |
| 932 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 933 ASSERT_TRUE(result_layer); |
| 934 EXPECT_EQ(3, result_layer->id()); |
| 935 |
| 936 // At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to |
| 937 // be on top. |
| 938 test_point = gfx::Point(20, 51); |
| 939 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 940 ASSERT_TRUE(result_layer); |
| 941 EXPECT_EQ(4, result_layer->id()); |
| 942 } |
| 943 |
| 944 TEST_F(LayerTreeImplTest, HitTestingForMultipleLayersAtVaryingDepths) { |
| 945 FakeImplProxy proxy; |
| 946 TestSharedBitmapManager shared_bitmap_manager; |
| 947 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 948 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); |
| 949 |
| 950 gfx::Transform identity_matrix; |
| 951 gfx::PointF anchor; |
| 952 gfx::PointF position; |
| 953 gfx::Size bounds(100, 100); |
| 954 SetLayerPropertiesForTesting( |
| 955 root.get(), identity_matrix, anchor, position, bounds, true, false); |
| 956 root->SetDrawsContent(true); |
| 957 root->SetShouldFlattenTransform(false); |
| 958 root->SetIs3dSorted(true); |
| 959 { |
| 960 // child 1 and child2 are initialized to overlap between x=50 and x=60. |
| 961 // grand_child is set to overlap both child1 and child2 between y=50 and |
| 962 // y=60. The expected stacking order is: (front) child2, (second) |
| 963 // grand_child, (third) child1, and (back) the root layer behind all other |
| 964 // layers. |
| 965 |
| 966 scoped_ptr<LayerImpl> child1 = |
| 967 LayerImpl::Create(host_impl.active_tree(), 2); |
| 968 scoped_ptr<LayerImpl> child2 = |
| 969 LayerImpl::Create(host_impl.active_tree(), 3); |
| 970 scoped_ptr<LayerImpl> grand_child1 = |
| 971 LayerImpl::Create(host_impl.active_tree(), 4); |
| 972 |
| 973 position = gfx::PointF(10.f, 10.f); |
| 974 bounds = gfx::Size(50, 50); |
| 975 SetLayerPropertiesForTesting( |
| 976 child1.get(), identity_matrix, anchor, position, bounds, true, false); |
| 977 child1->SetDrawsContent(true); |
| 978 child1->SetShouldFlattenTransform(false); |
| 979 child1->SetIs3dSorted(true); |
| 980 |
| 981 position = gfx::PointF(50.f, 10.f); |
| 982 bounds = gfx::Size(50, 50); |
| 983 gfx::Transform translate_z; |
| 984 translate_z.Translate3d(0, 0, -10.f); |
| 985 SetLayerPropertiesForTesting( |
| 986 child2.get(), translate_z, anchor, position, bounds, true, false); |
| 987 child2->SetDrawsContent(true); |
| 988 child2->SetShouldFlattenTransform(false); |
| 989 child2->SetIs3dSorted(true); |
| 990 |
| 991 // Remember that grand_child is positioned with respect to its parent (i.e. |
| 992 // child1). In screen space, the intended position is (10, 50), with size |
| 993 // 100 x 50. |
| 994 position = gfx::PointF(0.f, 40.f); |
| 995 bounds = gfx::Size(100, 50); |
| 996 SetLayerPropertiesForTesting(grand_child1.get(), |
| 997 identity_matrix, |
| 998 anchor, |
| 999 position, |
| 1000 bounds, |
| 1001 true, |
| 1002 false); |
| 1003 grand_child1->SetDrawsContent(true); |
| 1004 grand_child1->SetShouldFlattenTransform(false); |
| 1005 |
| 1006 child1->AddChild(grand_child1.Pass()); |
| 1007 root->AddChild(child1.Pass()); |
| 1008 root->AddChild(child2.Pass()); |
| 1009 } |
| 1010 |
| 1011 LayerImpl* child1 = root->children()[0]; |
| 1012 LayerImpl* child2 = root->children()[1]; |
| 1013 LayerImpl* grand_child1 = child1->children()[0]; |
| 1014 |
| 1015 LayerImplList render_surface_layer_list; |
| 1016 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 1017 root.get(), root->bounds(), &render_surface_layer_list); |
| 1018 inputs.can_adjust_raster_scales = true; |
| 1019 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 1020 |
| 1021 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 1022 |
| 1023 // Sanity check the scenario we just created. |
| 1024 ASSERT_TRUE(child1); |
| 1025 ASSERT_TRUE(child2); |
| 1026 ASSERT_TRUE(grand_child1); |
| 1027 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 1028 |
| 1029 RenderSurfaceImpl* root_render_surface = root->render_surface(); |
| 1030 ASSERT_EQ(4u, root_render_surface->layer_list().size()); |
| 1031 ASSERT_EQ(3, root_render_surface->layer_list().at(0)->id()); |
| 1032 ASSERT_EQ(1, root_render_surface->layer_list().at(1)->id()); |
| 1033 ASSERT_EQ(2, root_render_surface->layer_list().at(2)->id()); |
| 1034 ASSERT_EQ(4, root_render_surface->layer_list().at(3)->id()); |
| 1035 |
| 1036 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 1037 |
| 1038 // Nothing overlaps the root_layer at (1, 1), so hit testing there should find |
| 1039 // the root layer. |
| 1040 gfx::Point test_point = gfx::Point(1, 1); |
| 1041 LayerImpl* result_layer = |
| 1042 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1043 ASSERT_TRUE(result_layer); |
| 1044 EXPECT_EQ(1, result_layer->id()); |
| 1045 |
| 1046 // At (15, 15), child1 and root are the only layers. child1 is expected to be |
| 1047 // on top. |
| 1048 test_point = gfx::Point(15, 15); |
| 1049 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1050 ASSERT_TRUE(result_layer); |
| 1051 EXPECT_EQ(2, result_layer->id()); |
| 1052 |
| 1053 // At (51, 20), child1 and child2 overlap. child2 is expected to be on top. |
| 1054 // (because 3 is transformed to the back). |
| 1055 test_point = gfx::Point(51, 20); |
| 1056 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1057 ASSERT_TRUE(result_layer); |
| 1058 EXPECT_EQ(2, result_layer->id()); |
| 1059 |
| 1060 // 3 Would have been on top if it hadn't been transformed to the background. |
| 1061 // Make sure that it isn't hit. |
| 1062 test_point = gfx::Point(80, 51); |
| 1063 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1064 ASSERT_TRUE(result_layer); |
| 1065 EXPECT_EQ(4, result_layer->id()); |
| 1066 |
| 1067 // 3 Would have been on top if it hadn't been transformed to the background. |
| 1068 // Make sure that it isn't hit. |
| 1069 test_point = gfx::Point(51, 51); |
| 1070 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1071 ASSERT_TRUE(result_layer); |
| 1072 EXPECT_EQ(4, result_layer->id()); |
| 1073 |
| 1074 // At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to |
| 1075 // be on top. |
| 1076 test_point = gfx::Point(20, 51); |
| 1077 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1078 ASSERT_TRUE(result_layer); |
| 1079 EXPECT_EQ(4, result_layer->id()); |
| 1080 } |
| 1081 |
| 1082 TEST_F(LayerTreeImplTest, HitTestingForMultipleLayerLists) { |
| 1083 // |
| 1084 // The geometry is set up similarly to the previous case, but |
| 1085 // all layers are forced to be render surfaces now. |
| 1086 // |
| 1087 FakeImplProxy proxy; |
| 1088 TestSharedBitmapManager shared_bitmap_manager; |
| 1089 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 1090 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); |
| 1091 |
| 1092 gfx::Transform identity_matrix; |
| 1093 gfx::PointF anchor; |
| 1094 gfx::PointF position; |
| 1095 gfx::Size bounds(100, 100); |
| 1096 SetLayerPropertiesForTesting( |
| 1097 root.get(), identity_matrix, anchor, position, bounds, true, false); |
| 1098 root->SetDrawsContent(true); |
| 1099 { |
| 1100 // child 1 and child2 are initialized to overlap between x=50 and x=60. |
| 1101 // grand_child is set to overlap both child1 and child2 between y=50 and |
| 1102 // y=60. The expected stacking order is: (front) child2, (second) |
| 1103 // grand_child, (third) child1, and (back) the root layer behind all other |
| 1104 // layers. |
| 1105 |
| 1106 scoped_ptr<LayerImpl> child1 = |
| 1107 LayerImpl::Create(host_impl.active_tree(), 2); |
| 1108 scoped_ptr<LayerImpl> child2 = |
| 1109 LayerImpl::Create(host_impl.active_tree(), 3); |
| 1110 scoped_ptr<LayerImpl> grand_child1 = |
| 1111 LayerImpl::Create(host_impl.active_tree(), 4); |
| 1112 |
| 1113 position = gfx::PointF(10.f, 10.f); |
| 1114 bounds = gfx::Size(50, 50); |
| 1115 SetLayerPropertiesForTesting( |
| 1116 child1.get(), identity_matrix, anchor, position, bounds, true, false); |
| 1117 child1->SetDrawsContent(true); |
| 1118 child1->SetForceRenderSurface(true); |
| 1119 |
| 1120 position = gfx::PointF(50.f, 10.f); |
| 1121 bounds = gfx::Size(50, 50); |
| 1122 SetLayerPropertiesForTesting( |
| 1123 child2.get(), identity_matrix, anchor, position, bounds, true, false); |
| 1124 child2->SetDrawsContent(true); |
| 1125 child2->SetForceRenderSurface(true); |
| 1126 |
| 1127 // Remember that grand_child is positioned with respect to its parent (i.e. |
| 1128 // child1). In screen space, the intended position is (10, 50), with size |
| 1129 // 100 x 50. |
| 1130 position = gfx::PointF(0.f, 40.f); |
| 1131 bounds = gfx::Size(100, 50); |
| 1132 SetLayerPropertiesForTesting(grand_child1.get(), |
| 1133 identity_matrix, |
| 1134 anchor, |
| 1135 position, |
| 1136 bounds, |
| 1137 true, |
| 1138 false); |
| 1139 grand_child1->SetDrawsContent(true); |
| 1140 grand_child1->SetForceRenderSurface(true); |
| 1141 |
| 1142 child1->AddChild(grand_child1.Pass()); |
| 1143 root->AddChild(child1.Pass()); |
| 1144 root->AddChild(child2.Pass()); |
| 1145 } |
| 1146 |
| 1147 LayerImpl* child1 = root->children()[0]; |
| 1148 LayerImpl* child2 = root->children()[1]; |
| 1149 LayerImpl* grand_child1 = child1->children()[0]; |
| 1150 |
| 1151 LayerImplList render_surface_layer_list; |
| 1152 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 1153 root.get(), root->bounds(), &render_surface_layer_list); |
| 1154 inputs.can_adjust_raster_scales = true; |
| 1155 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 1156 |
| 1157 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 1158 |
| 1159 // Sanity check the scenario we just created. |
| 1160 ASSERT_TRUE(child1); |
| 1161 ASSERT_TRUE(child2); |
| 1162 ASSERT_TRUE(grand_child1); |
| 1163 ASSERT_TRUE(child1->render_surface()); |
| 1164 ASSERT_TRUE(child2->render_surface()); |
| 1165 ASSERT_TRUE(grand_child1->render_surface()); |
| 1166 ASSERT_EQ(4u, render_surface_layer_list.size()); |
| 1167 // The root surface has the root layer, and child1's and child2's render |
| 1168 // surfaces. |
| 1169 ASSERT_EQ(3u, root->render_surface()->layer_list().size()); |
| 1170 // The child1 surface has the child1 layer and grand_child1's render surface. |
| 1171 ASSERT_EQ(2u, child1->render_surface()->layer_list().size()); |
| 1172 ASSERT_EQ(1u, child2->render_surface()->layer_list().size()); |
| 1173 ASSERT_EQ(1u, grand_child1->render_surface()->layer_list().size()); |
| 1174 ASSERT_EQ(1, render_surface_layer_list.at(0)->id()); // root layer |
| 1175 ASSERT_EQ(2, render_surface_layer_list[1]->id()); // child1 |
| 1176 ASSERT_EQ(4, render_surface_layer_list.at(2)->id()); // grand_child1 |
| 1177 ASSERT_EQ(3, render_surface_layer_list[3]->id()); // child2 |
| 1178 |
| 1179 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 1180 |
| 1181 // Nothing overlaps the root_layer at (1, 1), so hit testing there should find |
| 1182 // the root layer. |
| 1183 gfx::Point test_point = gfx::Point(1, 1); |
| 1184 LayerImpl* result_layer = |
| 1185 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1186 ASSERT_TRUE(result_layer); |
| 1187 EXPECT_EQ(1, result_layer->id()); |
| 1188 |
| 1189 // At (15, 15), child1 and root are the only layers. child1 is expected to be |
| 1190 // on top. |
| 1191 test_point = gfx::Point(15, 15); |
| 1192 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1193 ASSERT_TRUE(result_layer); |
| 1194 EXPECT_EQ(2, result_layer->id()); |
| 1195 |
| 1196 // At (51, 20), child1 and child2 overlap. child2 is expected to be on top. |
| 1197 test_point = gfx::Point(51, 20); |
| 1198 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1199 ASSERT_TRUE(result_layer); |
| 1200 EXPECT_EQ(3, result_layer->id()); |
| 1201 |
| 1202 // At (80, 51), child2 and grand_child1 overlap. child2 is expected to be on |
| 1203 // top. |
| 1204 test_point = gfx::Point(80, 51); |
| 1205 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1206 ASSERT_TRUE(result_layer); |
| 1207 EXPECT_EQ(3, result_layer->id()); |
| 1208 |
| 1209 // At (51, 51), all layers overlap each other. child2 is expected to be on top |
| 1210 // of all other layers. |
| 1211 test_point = gfx::Point(51, 51); |
| 1212 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1213 ASSERT_TRUE(result_layer); |
| 1214 EXPECT_EQ(3, result_layer->id()); |
| 1215 |
| 1216 // At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to |
| 1217 // be on top. |
| 1218 test_point = gfx::Point(20, 51); |
| 1219 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1220 ASSERT_TRUE(result_layer); |
| 1221 EXPECT_EQ(4, result_layer->id()); |
| 1222 } |
| 1223 |
| 1224 TEST_F(LayerTreeImplTest, HitTestingForEmptyLayers) { |
| 1225 FakeImplProxy proxy; |
| 1226 TestSharedBitmapManager shared_bitmap_manager; |
| 1227 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 1228 |
| 1229 // Layer 1 - root |
| 1230 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); |
| 1231 gfx::Transform identity_matrix; |
| 1232 gfx::PointF anchor; |
| 1233 gfx::PointF position; |
| 1234 gfx::Size bounds(100, 100); |
| 1235 SetLayerPropertiesForTesting( |
| 1236 root.get(), identity_matrix, anchor, position, bounds, true, false); |
| 1237 root->SetDrawsContent(true); |
| 1238 |
| 1239 { |
| 1240 // Layer 2 - empty: drawsContent=false |
| 1241 gfx::PointF position(10.f, 10.f); |
| 1242 gfx::Size bounds(30, 30); |
| 1243 scoped_ptr<LayerImpl> empty_layer = |
| 1244 LayerImpl::Create(host_impl.active_tree(), 2); |
| 1245 SetLayerPropertiesForTesting(empty_layer.get(), |
| 1246 identity_matrix, |
| 1247 anchor, |
| 1248 position, |
| 1249 bounds, |
| 1250 true, |
| 1251 false); |
| 1252 |
| 1253 empty_layer->SetDrawsContent(false); |
| 1254 root->AddChild(empty_layer.Pass()); |
| 1255 } |
| 1256 |
| 1257 { |
| 1258 // Layer 3 - empty, but has touch handler |
| 1259 gfx::PointF position(10.f, 60.f); |
| 1260 gfx::Size bounds(30, 30); |
| 1261 scoped_ptr<LayerImpl> test_layer = |
| 1262 LayerImpl::Create(host_impl.active_tree(), 3); |
| 1263 SetLayerPropertiesForTesting(test_layer.get(), |
| 1264 identity_matrix, |
| 1265 anchor, |
| 1266 position, |
| 1267 bounds, |
| 1268 true, |
| 1269 false); |
| 1270 |
| 1271 test_layer->SetDrawsContent(false); |
| 1272 Region touch_handler_region(gfx::Rect(10, 10, 10, 10)); |
| 1273 test_layer->SetTouchEventHandlerRegion(touch_handler_region); |
| 1274 root->AddChild(test_layer.Pass()); |
| 1275 } |
| 1276 |
| 1277 { |
| 1278 // Layer 4 - empty, but has mousewheel handler |
| 1279 gfx::PointF position(60.f, 60.f); |
| 1280 gfx::Size bounds(30, 30); |
| 1281 scoped_ptr<LayerImpl> test_layer = |
| 1282 LayerImpl::Create(host_impl.active_tree(), 4); |
| 1283 SetLayerPropertiesForTesting(test_layer.get(), |
| 1284 identity_matrix, |
| 1285 anchor, |
| 1286 position, |
| 1287 bounds, |
| 1288 true, |
| 1289 false); |
| 1290 |
| 1291 test_layer->SetDrawsContent(false); |
| 1292 test_layer->SetHaveWheelEventHandlers(true); |
| 1293 root->AddChild(test_layer.Pass()); |
| 1294 } |
| 1295 |
| 1296 LayerImplList render_surface_layer_list; |
| 1297 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 1298 root.get(), root->bounds(), &render_surface_layer_list); |
| 1299 inputs.can_adjust_raster_scales = true; |
| 1300 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 1301 |
| 1302 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 1303 |
| 1304 // Verify that the root layer and empty layers with touch/wheel handlers |
| 1305 // (but not the empty layer without a touch handler) are in the RSSL. |
| 1306 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 1307 EXPECT_EQ(1, render_surface_layer_list[0]->id()); |
| 1308 ASSERT_EQ(3u, root->render_surface()->layer_list().size()); |
| 1309 EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id()); |
| 1310 EXPECT_EQ(3, root->render_surface()->layer_list().at(1)->id()); |
| 1311 EXPECT_EQ(4, root->render_surface()->layer_list().at(2)->id()); |
| 1312 |
| 1313 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 1314 |
| 1315 // Hit testing for a point inside the empty no-handlers layer should return |
| 1316 // the root layer. |
| 1317 gfx::Point test_point = gfx::Point(15, 15); |
| 1318 LayerImpl* result_layer = |
| 1319 host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1320 ASSERT_TRUE(result_layer); |
| 1321 EXPECT_EQ(1, result_layer->id()); |
| 1322 |
| 1323 // Hit testing for a point inside the touch handler layer should return it. |
| 1324 test_point = gfx::Point(15, 75); |
| 1325 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1326 ASSERT_TRUE(result_layer); |
| 1327 EXPECT_EQ(3, result_layer->id()); |
| 1328 |
| 1329 // Hit testing for a point inside the mousewheel layer should return it. |
| 1330 test_point = gfx::Point(75, 75); |
| 1331 result_layer = host_impl.active_tree()->FindLayerThatIsHitByPoint(test_point); |
| 1332 ASSERT_TRUE(result_layer); |
| 1333 EXPECT_EQ(4, result_layer->id()); |
| 1334 } |
| 1335 |
| 1336 TEST_F(LayerTreeImplTest, HitCheckingTouchHandlerRegionsForSingleLayer) { |
| 1337 FakeImplProxy proxy; |
| 1338 TestSharedBitmapManager shared_bitmap_manager; |
| 1339 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 1340 scoped_ptr<LayerImpl> root = |
| 1341 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 1342 |
| 1343 gfx::Transform identity_matrix; |
| 1344 Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); |
| 1345 gfx::PointF anchor; |
| 1346 gfx::PointF position; |
| 1347 gfx::Size bounds(100, 100); |
| 1348 SetLayerPropertiesForTesting( |
| 1349 root.get(), identity_matrix, anchor, position, bounds, true, false); |
| 1350 root->SetDrawsContent(true); |
| 1351 |
| 1352 LayerImplList render_surface_layer_list; |
| 1353 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 1354 root.get(), root->bounds(), &render_surface_layer_list); |
| 1355 inputs.can_adjust_raster_scales = true; |
| 1356 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 1357 |
| 1358 host_impl.active_tree()->set_current_render_surface_list_id_for_testing(1); |
| 1359 |
| 1360 // Sanity check the scenario we just created. |
| 1361 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 1362 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 1363 |
| 1364 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 1365 |
| 1366 // Hit checking for any point should return a null pointer for a layer without |
| 1367 // any touch event handler regions. |
| 1368 gfx::Point test_point(11, 11); |
| 1369 LayerImpl* result_layer = |
| 1370 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1371 test_point); |
| 1372 EXPECT_FALSE(result_layer); |
| 1373 |
| 1374 host_impl.active_tree()->root_layer()->SetTouchEventHandlerRegion( |
| 1375 touch_handler_region); |
| 1376 // Hit checking for a point outside the layer should return a null pointer. |
| 1377 test_point = gfx::Point(101, 101); |
| 1378 result_layer = |
| 1379 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1380 test_point); |
| 1381 EXPECT_FALSE(result_layer); |
| 1382 |
| 1383 test_point = gfx::Point(-1, -1); |
| 1384 result_layer = |
| 1385 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1386 test_point); |
| 1387 EXPECT_FALSE(result_layer); |
| 1388 |
| 1389 // Hit checking for a point inside the layer, but outside the touch handler |
| 1390 // region should return a null pointer. |
| 1391 test_point = gfx::Point(1, 1); |
| 1392 result_layer = |
| 1393 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1394 test_point); |
| 1395 EXPECT_FALSE(result_layer); |
| 1396 |
| 1397 test_point = gfx::Point(99, 99); |
| 1398 result_layer = |
| 1399 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1400 test_point); |
| 1401 EXPECT_FALSE(result_layer); |
| 1402 |
| 1403 // Hit checking for a point inside the touch event handler region should |
| 1404 // return the root layer. |
| 1405 test_point = gfx::Point(11, 11); |
| 1406 result_layer = |
| 1407 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1408 test_point); |
| 1409 ASSERT_TRUE(result_layer); |
| 1410 EXPECT_EQ(12345, result_layer->id()); |
| 1411 |
| 1412 test_point = gfx::Point(59, 59); |
| 1413 result_layer = |
| 1414 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1415 test_point); |
| 1416 ASSERT_TRUE(result_layer); |
| 1417 EXPECT_EQ(12345, result_layer->id()); |
| 1418 } |
| 1419 |
| 1420 TEST_F(LayerTreeImplTest, |
| 1421 HitCheckingTouchHandlerRegionsForUninvertibleTransform) { |
| 1422 FakeImplProxy proxy; |
| 1423 TestSharedBitmapManager shared_bitmap_manager; |
| 1424 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 1425 scoped_ptr<LayerImpl> root = |
| 1426 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 1427 |
| 1428 gfx::Transform uninvertible_transform; |
| 1429 uninvertible_transform.matrix().set(0, 0, 0.0); |
| 1430 uninvertible_transform.matrix().set(1, 1, 0.0); |
| 1431 uninvertible_transform.matrix().set(2, 2, 0.0); |
| 1432 uninvertible_transform.matrix().set(3, 3, 0.0); |
| 1433 ASSERT_FALSE(uninvertible_transform.IsInvertible()); |
| 1434 |
| 1435 gfx::Transform identity_matrix; |
| 1436 Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); |
| 1437 gfx::PointF anchor; |
| 1438 gfx::PointF position; |
| 1439 gfx::Size bounds(100, 100); |
| 1440 SetLayerPropertiesForTesting(root.get(), |
| 1441 uninvertible_transform, |
| 1442 anchor, |
| 1443 position, |
| 1444 bounds, |
| 1445 true, |
| 1446 false); |
| 1447 root->SetDrawsContent(true); |
| 1448 root->SetTouchEventHandlerRegion(touch_handler_region); |
| 1449 |
| 1450 LayerImplList render_surface_layer_list; |
| 1451 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 1452 root.get(), root->bounds(), &render_surface_layer_list); |
| 1453 inputs.can_adjust_raster_scales = true; |
| 1454 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 1455 |
| 1456 // Sanity check the scenario we just created. |
| 1457 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 1458 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 1459 ASSERT_FALSE(root->screen_space_transform().IsInvertible()); |
| 1460 |
| 1461 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 1462 |
| 1463 // Hit checking any point should not hit the touch handler region on the |
| 1464 // layer. If the invertible matrix is accidentally ignored and treated like an |
| 1465 // identity, then the hit testing will incorrectly hit the layer when it |
| 1466 // shouldn't. |
| 1467 gfx::Point test_point(1, 1); |
| 1468 LayerImpl* result_layer = |
| 1469 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1470 test_point); |
| 1471 EXPECT_FALSE(result_layer); |
| 1472 |
| 1473 test_point = gfx::Point(10, 10); |
| 1474 result_layer = |
| 1475 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1476 test_point); |
| 1477 EXPECT_FALSE(result_layer); |
| 1478 |
| 1479 test_point = gfx::Point(10, 30); |
| 1480 result_layer = |
| 1481 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1482 test_point); |
| 1483 EXPECT_FALSE(result_layer); |
| 1484 |
| 1485 test_point = gfx::Point(50, 50); |
| 1486 result_layer = |
| 1487 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1488 test_point); |
| 1489 EXPECT_FALSE(result_layer); |
| 1490 |
| 1491 test_point = gfx::Point(67, 48); |
| 1492 result_layer = |
| 1493 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1494 test_point); |
| 1495 EXPECT_FALSE(result_layer); |
| 1496 |
| 1497 test_point = gfx::Point(99, 99); |
| 1498 result_layer = |
| 1499 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1500 test_point); |
| 1501 EXPECT_FALSE(result_layer); |
| 1502 |
| 1503 test_point = gfx::Point(-1, -1); |
| 1504 result_layer = |
| 1505 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1506 test_point); |
| 1507 EXPECT_FALSE(result_layer); |
| 1508 } |
| 1509 |
| 1510 TEST_F(LayerTreeImplTest, |
| 1511 HitCheckingTouchHandlerRegionsForSinglePositionedLayer) { |
| 1512 FakeImplProxy proxy; |
| 1513 TestSharedBitmapManager shared_bitmap_manager; |
| 1514 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 1515 scoped_ptr<LayerImpl> root = |
| 1516 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 1517 |
| 1518 gfx::Transform identity_matrix; |
| 1519 Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); |
| 1520 gfx::PointF anchor; |
| 1521 // this layer is positioned, and hit testing should correctly know where the |
| 1522 // layer is located. |
| 1523 gfx::PointF position(50.f, 50.f); |
| 1524 gfx::Size bounds(100, 100); |
| 1525 SetLayerPropertiesForTesting( |
| 1526 root.get(), identity_matrix, anchor, position, bounds, true, false); |
| 1527 root->SetDrawsContent(true); |
| 1528 root->SetTouchEventHandlerRegion(touch_handler_region); |
| 1529 |
| 1530 LayerImplList render_surface_layer_list; |
| 1531 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 1532 root.get(), root->bounds(), &render_surface_layer_list); |
| 1533 inputs.can_adjust_raster_scales = true; |
| 1534 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 1535 |
| 1536 // Sanity check the scenario we just created. |
| 1537 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 1538 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 1539 |
| 1540 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 1541 |
| 1542 // Hit checking for a point outside the layer should return a null pointer. |
| 1543 gfx::Point test_point(49, 49); |
| 1544 LayerImpl* result_layer = |
| 1545 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1546 test_point); |
| 1547 EXPECT_FALSE(result_layer); |
| 1548 |
| 1549 // Even though the layer has a touch handler region containing (101, 101), it |
| 1550 // should not be visible there since the root render surface would clamp it. |
| 1551 test_point = gfx::Point(101, 101); |
| 1552 result_layer = |
| 1553 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1554 test_point); |
| 1555 EXPECT_FALSE(result_layer); |
| 1556 |
| 1557 // Hit checking for a point inside the layer, but outside the touch handler |
| 1558 // region should return a null pointer. |
| 1559 test_point = gfx::Point(51, 51); |
| 1560 result_layer = |
| 1561 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1562 test_point); |
| 1563 EXPECT_FALSE(result_layer); |
| 1564 |
| 1565 // Hit checking for a point inside the touch event handler region should |
| 1566 // return the root layer. |
| 1567 test_point = gfx::Point(61, 61); |
| 1568 result_layer = |
| 1569 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1570 test_point); |
| 1571 ASSERT_TRUE(result_layer); |
| 1572 EXPECT_EQ(12345, result_layer->id()); |
| 1573 |
| 1574 test_point = gfx::Point(99, 99); |
| 1575 result_layer = |
| 1576 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1577 test_point); |
| 1578 ASSERT_TRUE(result_layer); |
| 1579 EXPECT_EQ(12345, result_layer->id()); |
| 1580 } |
| 1581 |
| 1582 TEST_F(LayerTreeImplTest, |
| 1583 HitCheckingTouchHandlerRegionsForSingleLayerWithScaledContents) { |
| 1584 // A layer's visible content rect is actually in the layer's content space. |
| 1585 // The screen space transform converts from the layer's origin space to screen |
| 1586 // space. This test makes sure that hit testing works correctly accounts for |
| 1587 // the contents scale. A contents scale that is not 1 effectively forces a |
| 1588 // non-identity transform between layer's content space and layer's origin |
| 1589 // space. The hit testing code must take this into account. |
| 1590 // |
| 1591 // To test this, the layer is positioned at (25, 25), and is size (50, 50). If |
| 1592 // contents scale is ignored, then hit checking will mis-interpret the visible |
| 1593 // content rect as being larger than the actual bounds of the layer. |
| 1594 // |
| 1595 FakeImplProxy proxy; |
| 1596 TestSharedBitmapManager shared_bitmap_manager; |
| 1597 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 1598 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); |
| 1599 |
| 1600 gfx::Transform identity_matrix; |
| 1601 gfx::PointF anchor; |
| 1602 |
| 1603 SetLayerPropertiesForTesting(root.get(), |
| 1604 identity_matrix, |
| 1605 anchor, |
| 1606 gfx::PointF(), |
| 1607 gfx::Size(100, 100), |
| 1608 true, |
| 1609 false); |
| 1610 { |
| 1611 Region touch_handler_region(gfx::Rect(10, 10, 30, 30)); |
| 1612 gfx::PointF position(25.f, 25.f); |
| 1613 gfx::Size bounds(50, 50); |
| 1614 scoped_ptr<LayerImpl> test_layer = |
| 1615 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 1616 SetLayerPropertiesForTesting(test_layer.get(), |
| 1617 identity_matrix, |
| 1618 anchor, |
| 1619 position, |
| 1620 bounds, |
| 1621 true, |
| 1622 false); |
| 1623 |
| 1624 // override content bounds and contents scale |
| 1625 test_layer->SetContentBounds(gfx::Size(100, 100)); |
| 1626 test_layer->SetContentsScale(2, 2); |
| 1627 |
| 1628 test_layer->SetDrawsContent(true); |
| 1629 test_layer->SetTouchEventHandlerRegion(touch_handler_region); |
| 1630 root->AddChild(test_layer.Pass()); |
| 1631 } |
| 1632 |
| 1633 LayerImplList render_surface_layer_list; |
| 1634 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 1635 root.get(), root->bounds(), &render_surface_layer_list); |
| 1636 inputs.can_adjust_raster_scales = true; |
| 1637 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 1638 |
| 1639 // Sanity check the scenario we just created. |
| 1640 // The visible content rect for test_layer is actually 100x100, even though |
| 1641 // its layout size is 50x50, positioned at 25x25. |
| 1642 LayerImpl* test_layer = root->children()[0]; |
| 1643 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), test_layer->visible_content_rect()); |
| 1644 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 1645 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 1646 |
| 1647 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 1648 |
| 1649 // Hit checking for a point outside the layer should return a null pointer |
| 1650 // (the root layer does not draw content, so it will not be tested either). |
| 1651 gfx::Point test_point(76, 76); |
| 1652 LayerImpl* result_layer = |
| 1653 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1654 test_point); |
| 1655 EXPECT_FALSE(result_layer); |
| 1656 |
| 1657 // Hit checking for a point inside the layer, but outside the touch handler |
| 1658 // region should return a null pointer. |
| 1659 test_point = gfx::Point(26, 26); |
| 1660 result_layer = |
| 1661 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1662 test_point); |
| 1663 EXPECT_FALSE(result_layer); |
| 1664 |
| 1665 test_point = gfx::Point(34, 34); |
| 1666 result_layer = |
| 1667 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1668 test_point); |
| 1669 EXPECT_FALSE(result_layer); |
| 1670 |
| 1671 test_point = gfx::Point(65, 65); |
| 1672 result_layer = |
| 1673 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1674 test_point); |
| 1675 EXPECT_FALSE(result_layer); |
| 1676 |
| 1677 test_point = gfx::Point(74, 74); |
| 1678 result_layer = |
| 1679 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1680 test_point); |
| 1681 EXPECT_FALSE(result_layer); |
| 1682 |
| 1683 // Hit checking for a point inside the touch event handler region should |
| 1684 // return the root layer. |
| 1685 test_point = gfx::Point(35, 35); |
| 1686 result_layer = |
| 1687 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1688 test_point); |
| 1689 ASSERT_TRUE(result_layer); |
| 1690 EXPECT_EQ(12345, result_layer->id()); |
| 1691 |
| 1692 test_point = gfx::Point(64, 64); |
| 1693 result_layer = |
| 1694 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1695 test_point); |
| 1696 ASSERT_TRUE(result_layer); |
| 1697 EXPECT_EQ(12345, result_layer->id()); |
| 1698 } |
| 1699 |
| 1700 TEST_F(LayerTreeImplTest, |
| 1701 HitCheckingTouchHandlerRegionsForSingleLayerWithDeviceScale) { |
| 1702 // The layer's device_scale_factor and page_scale_factor should scale the |
| 1703 // content rect and we should be able to hit the touch handler region by |
| 1704 // scaling the points accordingly. |
| 1705 FakeImplProxy proxy; |
| 1706 TestSharedBitmapManager shared_bitmap_manager; |
| 1707 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 1708 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); |
| 1709 |
| 1710 gfx::Transform identity_matrix; |
| 1711 gfx::PointF anchor; |
| 1712 // Set the bounds of the root layer big enough to fit the child when scaled. |
| 1713 SetLayerPropertiesForTesting(root.get(), |
| 1714 identity_matrix, |
| 1715 anchor, |
| 1716 gfx::PointF(), |
| 1717 gfx::Size(100, 100), |
| 1718 true, |
| 1719 false); |
| 1720 { |
| 1721 Region touch_handler_region(gfx::Rect(10, 10, 30, 30)); |
| 1722 gfx::PointF position(25.f, 25.f); |
| 1723 gfx::Size bounds(50, 50); |
| 1724 scoped_ptr<LayerImpl> test_layer = |
| 1725 LayerImpl::Create(host_impl.active_tree(), 12345); |
| 1726 SetLayerPropertiesForTesting(test_layer.get(), |
| 1727 identity_matrix, |
| 1728 anchor, |
| 1729 position, |
| 1730 bounds, |
| 1731 true, |
| 1732 false); |
| 1733 |
| 1734 test_layer->SetDrawsContent(true); |
| 1735 test_layer->SetTouchEventHandlerRegion(touch_handler_region); |
| 1736 root->AddChild(test_layer.Pass()); |
| 1737 } |
| 1738 |
| 1739 LayerImplList render_surface_layer_list; |
| 1740 float device_scale_factor = 3.f; |
| 1741 float page_scale_factor = 5.f; |
| 1742 gfx::Size scaled_bounds_for_root = gfx::ToCeiledSize( |
| 1743 gfx::ScaleSize(root->bounds(), device_scale_factor * page_scale_factor)); |
| 1744 |
| 1745 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 1746 root.get(), scaled_bounds_for_root, &render_surface_layer_list); |
| 1747 inputs.device_scale_factor = device_scale_factor; |
| 1748 inputs.page_scale_factor = page_scale_factor; |
| 1749 inputs.page_scale_application_layer = root.get(); |
| 1750 inputs.can_adjust_raster_scales = true; |
| 1751 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 1752 |
| 1753 // Sanity check the scenario we just created. |
| 1754 // The visible content rect for test_layer is actually 100x100, even though |
| 1755 // its layout size is 50x50, positioned at 25x25. |
| 1756 LayerImpl* test_layer = root->children()[0]; |
| 1757 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 1758 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 1759 |
| 1760 // Check whether the child layer fits into the root after scaled. |
| 1761 EXPECT_RECT_EQ(gfx::Rect(test_layer->content_bounds()), |
| 1762 test_layer->visible_content_rect()); |
| 1763 |
| 1764 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 1765 |
| 1766 // Hit checking for a point outside the layer should return a null pointer |
| 1767 // (the root layer does not draw content, so it will not be tested either). |
| 1768 gfx::PointF test_point(76.f, 76.f); |
| 1769 test_point = |
| 1770 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); |
| 1771 LayerImpl* result_layer = |
| 1772 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1773 test_point); |
| 1774 EXPECT_FALSE(result_layer); |
| 1775 |
| 1776 // Hit checking for a point inside the layer, but outside the touch handler |
| 1777 // region should return a null pointer. |
| 1778 test_point = gfx::Point(26, 26); |
| 1779 test_point = |
| 1780 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); |
| 1781 result_layer = |
| 1782 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1783 test_point); |
| 1784 EXPECT_FALSE(result_layer); |
| 1785 |
| 1786 test_point = gfx::Point(34, 34); |
| 1787 test_point = |
| 1788 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); |
| 1789 result_layer = |
| 1790 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1791 test_point); |
| 1792 EXPECT_FALSE(result_layer); |
| 1793 |
| 1794 test_point = gfx::Point(65, 65); |
| 1795 test_point = |
| 1796 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); |
| 1797 result_layer = |
| 1798 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1799 test_point); |
| 1800 EXPECT_FALSE(result_layer); |
| 1801 |
| 1802 test_point = gfx::Point(74, 74); |
| 1803 test_point = |
| 1804 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); |
| 1805 result_layer = |
| 1806 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1807 test_point); |
| 1808 EXPECT_FALSE(result_layer); |
| 1809 |
| 1810 // Hit checking for a point inside the touch event handler region should |
| 1811 // return the root layer. |
| 1812 test_point = gfx::Point(35, 35); |
| 1813 test_point = |
| 1814 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); |
| 1815 result_layer = |
| 1816 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1817 test_point); |
| 1818 ASSERT_TRUE(result_layer); |
| 1819 EXPECT_EQ(12345, result_layer->id()); |
| 1820 |
| 1821 test_point = gfx::Point(64, 64); |
| 1822 test_point = |
| 1823 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); |
| 1824 result_layer = |
| 1825 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1826 test_point); |
| 1827 ASSERT_TRUE(result_layer); |
| 1828 EXPECT_EQ(12345, result_layer->id()); |
| 1829 } |
| 1830 |
| 1831 TEST_F(LayerTreeImplTest, HitCheckingTouchHandlerRegionsForSimpleClippedLayer) { |
| 1832 // Test that hit-checking will only work for the visible portion of a layer, |
| 1833 // and not the entire layer bounds. Here we just test the simple axis-aligned |
| 1834 // case. |
| 1835 gfx::Transform identity_matrix; |
| 1836 gfx::PointF anchor; |
| 1837 |
| 1838 FakeImplProxy proxy; |
| 1839 TestSharedBitmapManager shared_bitmap_manager; |
| 1840 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 1841 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); |
| 1842 SetLayerPropertiesForTesting(root.get(), |
| 1843 identity_matrix, |
| 1844 anchor, |
| 1845 gfx::PointF(), |
| 1846 gfx::Size(100, 100), |
| 1847 true, |
| 1848 false); |
| 1849 { |
| 1850 scoped_ptr<LayerImpl> clipping_layer = |
| 1851 LayerImpl::Create(host_impl.active_tree(), 123); |
| 1852 // this layer is positioned, and hit testing should correctly know where the |
| 1853 // layer is located. |
| 1854 gfx::PointF position(25.f, 25.f); |
| 1855 gfx::Size bounds(50, 50); |
| 1856 SetLayerPropertiesForTesting(clipping_layer.get(), |
| 1857 identity_matrix, |
| 1858 anchor, |
| 1859 position, |
| 1860 bounds, |
| 1861 true, |
| 1862 false); |
| 1863 clipping_layer->SetMasksToBounds(true); |
| 1864 |
| 1865 scoped_ptr<LayerImpl> child = |
| 1866 LayerImpl::Create(host_impl.active_tree(), 456); |
| 1867 Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); |
| 1868 position = gfx::PointF(-50.f, -50.f); |
| 1869 bounds = gfx::Size(300, 300); |
| 1870 SetLayerPropertiesForTesting( |
| 1871 child.get(), identity_matrix, anchor, position, bounds, true, false); |
| 1872 child->SetDrawsContent(true); |
| 1873 child->SetTouchEventHandlerRegion(touch_handler_region); |
| 1874 clipping_layer->AddChild(child.Pass()); |
| 1875 root->AddChild(clipping_layer.Pass()); |
| 1876 } |
| 1877 |
| 1878 LayerImplList render_surface_layer_list; |
| 1879 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 1880 root.get(), root->bounds(), &render_surface_layer_list); |
| 1881 inputs.can_adjust_raster_scales = true; |
| 1882 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 1883 |
| 1884 // Sanity check the scenario we just created. |
| 1885 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 1886 ASSERT_EQ(1u, root->render_surface()->layer_list().size()); |
| 1887 ASSERT_EQ(456, root->render_surface()->layer_list().at(0)->id()); |
| 1888 |
| 1889 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 1890 |
| 1891 // Hit checking for a point outside the layer should return a null pointer. |
| 1892 // Despite the child layer being very large, it should be clipped to the root |
| 1893 // layer's bounds. |
| 1894 gfx::Point test_point(24, 24); |
| 1895 LayerImpl* result_layer = |
| 1896 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1897 test_point); |
| 1898 EXPECT_FALSE(result_layer); |
| 1899 |
| 1900 // Hit checking for a point inside the layer, but outside the touch handler |
| 1901 // region should return a null pointer. |
| 1902 test_point = gfx::Point(35, 35); |
| 1903 result_layer = |
| 1904 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1905 test_point); |
| 1906 EXPECT_FALSE(result_layer); |
| 1907 |
| 1908 test_point = gfx::Point(74, 74); |
| 1909 result_layer = |
| 1910 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1911 test_point); |
| 1912 EXPECT_FALSE(result_layer); |
| 1913 |
| 1914 // Hit checking for a point inside the touch event handler region should |
| 1915 // return the root layer. |
| 1916 test_point = gfx::Point(25, 25); |
| 1917 result_layer = |
| 1918 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1919 test_point); |
| 1920 ASSERT_TRUE(result_layer); |
| 1921 EXPECT_EQ(456, result_layer->id()); |
| 1922 |
| 1923 test_point = gfx::Point(34, 34); |
| 1924 result_layer = |
| 1925 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 1926 test_point); |
| 1927 ASSERT_TRUE(result_layer); |
| 1928 EXPECT_EQ(456, result_layer->id()); |
| 1929 } |
| 1930 |
| 1931 TEST_F(LayerTreeImplTest, HitCheckingTouchHandlerOverlappingRegions) { |
| 1932 gfx::Transform identity_matrix; |
| 1933 gfx::PointF anchor; |
| 1934 |
| 1935 FakeImplProxy proxy; |
| 1936 TestSharedBitmapManager shared_bitmap_manager; |
| 1937 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); |
| 1938 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); |
| 1939 SetLayerPropertiesForTesting(root.get(), |
| 1940 identity_matrix, |
| 1941 anchor, |
| 1942 gfx::PointF(), |
| 1943 gfx::Size(100, 100), |
| 1944 true, |
| 1945 false); |
| 1946 { |
| 1947 scoped_ptr<LayerImpl> touch_layer = |
| 1948 LayerImpl::Create(host_impl.active_tree(), 123); |
| 1949 // this layer is positioned, and hit testing should correctly know where the |
| 1950 // layer is located. |
| 1951 gfx::PointF position; |
| 1952 gfx::Size bounds(50, 50); |
| 1953 SetLayerPropertiesForTesting(touch_layer.get(), |
| 1954 identity_matrix, |
| 1955 anchor, |
| 1956 position, |
| 1957 bounds, |
| 1958 true, |
| 1959 false); |
| 1960 touch_layer->SetDrawsContent(true); |
| 1961 touch_layer->SetTouchEventHandlerRegion(gfx::Rect(0, 0, 50, 50)); |
| 1962 root->AddChild(touch_layer.Pass()); |
| 1963 } |
| 1964 |
| 1965 { |
| 1966 scoped_ptr<LayerImpl> notouch_layer = |
| 1967 LayerImpl::Create(host_impl.active_tree(), 1234); |
| 1968 // this layer is positioned, and hit testing should correctly know where the |
| 1969 // layer is located. |
| 1970 gfx::PointF position(0, 25); |
| 1971 gfx::Size bounds(50, 50); |
| 1972 SetLayerPropertiesForTesting(notouch_layer.get(), |
| 1973 identity_matrix, |
| 1974 anchor, |
| 1975 position, |
| 1976 bounds, |
| 1977 true, |
| 1978 false); |
| 1979 notouch_layer->SetDrawsContent(true); |
| 1980 root->AddChild(notouch_layer.Pass()); |
| 1981 } |
| 1982 |
| 1983 LayerImplList render_surface_layer_list; |
| 1984 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( |
| 1985 root.get(), root->bounds(), &render_surface_layer_list); |
| 1986 inputs.can_adjust_raster_scales = true; |
| 1987 LayerTreeHostCommon::CalculateDrawProperties(&inputs); |
| 1988 |
| 1989 // Sanity check the scenario we just created. |
| 1990 ASSERT_EQ(1u, render_surface_layer_list.size()); |
| 1991 ASSERT_EQ(2u, root->render_surface()->layer_list().size()); |
| 1992 ASSERT_EQ(123, root->render_surface()->layer_list().at(0)->id()); |
| 1993 ASSERT_EQ(1234, root->render_surface()->layer_list().at(1)->id()); |
| 1994 |
| 1995 host_impl.active_tree()->SetRootLayer(root.Pass()); |
| 1996 |
| 1997 gfx::Point test_point(35, 35); |
| 1998 LayerImpl* result_layer = |
| 1999 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 2000 test_point); |
| 2001 |
| 2002 // We should have passed through the no-touch layer and found the layer |
| 2003 // behind it. |
| 2004 EXPECT_TRUE(result_layer); |
| 2005 |
| 2006 host_impl.active_tree()->LayerById(1234)->SetContentsOpaque(true); |
| 2007 result_layer = |
| 2008 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 2009 test_point); |
| 2010 |
| 2011 // Even with an opaque layer in the middle, we should still find the layer |
| 2012 // with |
| 2013 // the touch handler behind it (since we can't assume that opaque layers are |
| 2014 // opaque to hit testing). |
| 2015 EXPECT_TRUE(result_layer); |
| 2016 |
| 2017 test_point = gfx::Point(35, 15); |
| 2018 result_layer = |
| 2019 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 2020 test_point); |
| 2021 ASSERT_TRUE(result_layer); |
| 2022 EXPECT_EQ(123, result_layer->id()); |
| 2023 |
| 2024 test_point = gfx::Point(35, 65); |
| 2025 result_layer = |
| 2026 host_impl.active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( |
| 2027 test_point); |
| 2028 EXPECT_FALSE(result_layer); |
| 2029 } |
| 2030 |
| 2031 } // namespace |
| 2032 } // namespace cc |
| OLD | NEW |