1 // Copyright 2013 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.
5 #include "cc/test/layer_tree_json_parser.h"
7 #include "cc/layers/layer.h"
8 #include "cc/test/fake_impl_proxy.h"
9 #include "cc/test/fake_layer_tree_host.h"
10 #include "cc/test/fake_layer_tree_host_impl.h"
11 #include "cc/test/geometry_test_utils.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 bool LayerTreesMatch(LayerImpl
* const layer_impl
,
20 #define RETURN_IF_EXPECTATION_FAILS(exp) \
23 if (testing::UnitTest::GetInstance()->current_test_info()-> \
28 RETURN_IF_EXPECTATION_FAILS(EXPECT_EQ(layer_impl
->children().size(),
29 layer
->children().size()));
30 RETURN_IF_EXPECTATION_FAILS(EXPECT_EQ(layer_impl
->bounds(), layer
->bounds()));
31 RETURN_IF_EXPECTATION_FAILS(
32 EXPECT_EQ(layer_impl
->position(), layer
->position()));
33 RETURN_IF_EXPECTATION_FAILS(
34 EXPECT_TRANSFORMATION_MATRIX_EQ(layer_impl
->draw_transform(),
35 layer
->draw_transform()));
36 RETURN_IF_EXPECTATION_FAILS(EXPECT_EQ(layer_impl
->contents_opaque(),
37 layer
->contents_opaque()));
38 RETURN_IF_EXPECTATION_FAILS(EXPECT_EQ(layer_impl
->scrollable(),
39 layer
->scrollable()));
40 RETURN_IF_EXPECTATION_FAILS(EXPECT_FLOAT_EQ(layer_impl
->opacity(),
42 RETURN_IF_EXPECTATION_FAILS(EXPECT_EQ(layer_impl
->have_wheel_event_handlers(),
43 layer
->have_wheel_event_handlers()));
44 RETURN_IF_EXPECTATION_FAILS(
45 EXPECT_EQ(layer_impl
->have_scroll_event_handlers(),
46 layer
->have_scroll_event_handlers()));
47 RETURN_IF_EXPECTATION_FAILS(
48 EXPECT_EQ(layer_impl
->touch_event_handler_region(),
49 layer
->touch_event_handler_region()));
51 for (size_t i
= 0; i
< layer_impl
->children().size(); ++i
) {
52 RETURN_IF_EXPECTATION_FAILS(EXPECT_TRUE(LayerTreesMatch(
53 layer_impl
->children()[i
], layer
->children()[i
].get())));
57 #undef RETURN_IF_EXPECTATION_FAILS
62 class LayerTreeJsonParserSanityCheck
: public testing::Test
{
65 TEST_F(LayerTreeJsonParserSanityCheck
, Basic
) {
67 TestSharedBitmapManager shared_bitmap_manager
;
68 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
, nullptr);
69 LayerTreeImpl
* tree
= host_impl
.active_tree();
71 scoped_ptr
<LayerImpl
> root_impl(LayerImpl::Create(tree
, 1));
72 scoped_ptr
<LayerImpl
> parent(LayerImpl::Create(tree
, 2));
73 scoped_ptr
<LayerImpl
> child(LayerImpl::Create(tree
, 3));
75 root_impl
->SetBounds(gfx::Size(100, 100));
76 parent
->SetBounds(gfx::Size(50, 50));
77 child
->SetBounds(gfx::Size(40, 40));
79 parent
->SetPosition(gfx::Point(25, 25));
81 child
->SetHaveWheelEventHandlers(true);
82 child
->SetHaveScrollEventHandlers(true);
84 parent
->AddChild(child
.Pass());
85 root_impl
->AddChild(parent
.Pass());
86 tree
->SetRootLayer(root_impl
.Pass());
88 std::string json
= host_impl
.LayerTreeAsJson();
89 scoped_refptr
<Layer
> root
= ParseTreeFromJson(json
, NULL
);
90 ASSERT_TRUE(root
.get());
91 EXPECT_TRUE(LayerTreesMatch(host_impl
.RootLayer(), root
.get()));
94 TEST_F(LayerTreeJsonParserSanityCheck
, EventHandlerRegions
) {
96 TestSharedBitmapManager shared_bitmap_manager
;
97 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
, nullptr);
98 LayerTreeImpl
* tree
= host_impl
.active_tree();
100 scoped_ptr
<LayerImpl
> root_impl(LayerImpl::Create(tree
, 1));
101 scoped_ptr
<LayerImpl
> touch_layer(LayerImpl::Create(tree
, 2));
103 root_impl
->SetBounds(gfx::Size(100, 100));
104 touch_layer
->SetBounds(gfx::Size(50, 50));
107 touch_region
.Union(gfx::Rect(10, 10, 20, 30));
108 touch_region
.Union(gfx::Rect(40, 10, 20, 20));
109 touch_layer
->SetTouchEventHandlerRegion(touch_region
);
111 root_impl
->AddChild(touch_layer
.Pass());
112 tree
->SetRootLayer(root_impl
.Pass());
114 std::string json
= host_impl
.LayerTreeAsJson();
115 scoped_refptr
<Layer
> root
= ParseTreeFromJson(json
, NULL
);
116 ASSERT_TRUE(root
.get());
117 EXPECT_TRUE(LayerTreesMatch(host_impl
.RootLayer(), root
.get()));