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.
5 #include "cc/layer_impl.h"
7 #include "cc/single_thread_proxy.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
11 #include <public/WebFilterOperation.h>
12 #include <public/WebFilterOperations.h>
14 using namespace WebKit
;
19 #define EXECUTE_AND_VERIFY_SUBTREE_CHANGED(codeToTest) \
20 root->resetAllChangeTrackingForSubtree(); \
22 EXPECT_TRUE(root->layerPropertyChanged()); \
23 EXPECT_TRUE(child->layerPropertyChanged()); \
24 EXPECT_TRUE(grandChild->layerPropertyChanged()); \
25 EXPECT_FALSE(root->layerSurfacePropertyChanged())
28 #define EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(codeToTest) \
29 root->resetAllChangeTrackingForSubtree(); \
31 EXPECT_FALSE(root->layerPropertyChanged()); \
32 EXPECT_FALSE(child->layerPropertyChanged()); \
33 EXPECT_FALSE(grandChild->layerPropertyChanged()); \
34 EXPECT_FALSE(root->layerSurfacePropertyChanged())
36 #define EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(codeToTest) \
37 root->resetAllChangeTrackingForSubtree(); \
39 EXPECT_TRUE(root->layerPropertyChanged()); \
40 EXPECT_FALSE(child->layerPropertyChanged()); \
41 EXPECT_FALSE(grandChild->layerPropertyChanged()); \
42 EXPECT_FALSE(root->layerSurfacePropertyChanged())
44 #define EXECUTE_AND_VERIFY_ONLY_SURFACE_CHANGED(codeToTest) \
45 root->resetAllChangeTrackingForSubtree(); \
47 EXPECT_FALSE(root->layerPropertyChanged()); \
48 EXPECT_FALSE(child->layerPropertyChanged()); \
49 EXPECT_FALSE(grandChild->layerPropertyChanged()); \
50 EXPECT_TRUE(root->layerSurfacePropertyChanged())
52 TEST(LayerImplTest
, verifyLayerChangesAreTrackedProperly
)
55 // This test checks that layerPropertyChanged() has the correct behavior.
58 // The constructor on this will fake that we are on the correct thread.
59 // Create a simple LayerImpl tree:
60 scoped_ptr
<LayerImpl
> root
= LayerImpl::create(1);
61 root
->addChild(LayerImpl::create(2));
62 LayerImpl
* child
= root
->children()[0];
63 child
->addChild(LayerImpl::create(3));
64 LayerImpl
* grandChild
= child
->children()[0];
66 // Adding children is an internal operation and should not mark layers as changed.
67 EXPECT_FALSE(root
->layerPropertyChanged());
68 EXPECT_FALSE(child
->layerPropertyChanged());
69 EXPECT_FALSE(grandChild
->layerPropertyChanged());
71 gfx::PointF arbitraryPointF
= gfx::PointF(0.125f
, 0.25f
);
72 float arbitraryNumber
= 0.352f
;
73 gfx::Size arbitrarySize
= gfx::Size(111, 222);
74 gfx::Point arbitraryPoint
= gfx::Point(333, 444);
75 gfx::Vector2d arbitraryVector2d
= gfx::Vector2d(111, 222);
76 gfx::Rect arbitraryRect
= gfx::Rect(arbitraryPoint
, arbitrarySize
);
77 gfx::RectF arbitraryRectF
= gfx::RectF(arbitraryPointF
, gfx::SizeF(1.234f
, 5.678f
));
78 SkColor arbitraryColor
= SkColorSetRGB(10, 20, 30);
79 WebTransformationMatrix arbitraryTransform
;
80 arbitraryTransform
.scale3d(0.1, 0.2, 0.3);
81 WebFilterOperations arbitraryFilters
;
82 arbitraryFilters
.append(WebFilterOperation::createOpacityFilter(0.5));
83 SkAutoTUnref
<SkImageFilter
> arbitraryFilter(new SkBlurImageFilter(SK_Scalar1
, SK_Scalar1
));
85 // These properties are internal, and should not be considered "change" when they are used.
86 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setUseLCDText(true));
87 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setDrawOpacity(arbitraryNumber
));
88 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setRenderTarget(0));
89 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setDrawTransform(arbitraryTransform
));
90 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setScreenSpaceTransform(arbitraryTransform
));
91 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setDrawableContentRect(arbitraryRect
));
92 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setUpdateRect(arbitraryRectF
));
93 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setVisibleContentRect(arbitraryRect
));
94 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setMaxScrollOffset(arbitraryVector2d
));
96 // Changing these properties affects the entire subtree of layers.
97 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setAnchorPoint(arbitraryPointF
));
98 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setAnchorPointZ(arbitraryNumber
));
99 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setFilters(arbitraryFilters
));
100 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setFilters(WebFilterOperations()));
101 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setFilter(arbitraryFilter
));
102 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setMaskLayer(LayerImpl::create(4)));
103 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setMasksToBounds(true));
104 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setContentsOpaque(true));
105 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setReplicaLayer(LayerImpl::create(5)));
106 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setPosition(arbitraryPointF
));
107 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setPreserves3D(true));
108 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setDoubleSided(false)); // constructor initializes it to "true".
109 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->scrollBy(arbitraryVector2d
));
110 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setScrollDelta(gfx::Vector2d()));
111 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setScrollOffset(arbitraryVector2d
));
112 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setImplTransform(arbitraryTransform
));
114 // Changing these properties only affects the layer itself.
115 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->setContentBounds(arbitrarySize
));
116 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->setContentsScale(arbitraryNumber
, arbitraryNumber
));
117 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->setDrawsContent(true));
118 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->setBackgroundColor(SK_ColorGRAY
));
119 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->setBackgroundFilters(arbitraryFilters
));
121 // Changing these properties only affects how render surface is drawn
122 EXECUTE_AND_VERIFY_ONLY_SURFACE_CHANGED(root
->setOpacity(arbitraryNumber
));
123 EXECUTE_AND_VERIFY_ONLY_SURFACE_CHANGED(root
->setTransform(arbitraryTransform
));
125 // Special case: check that sublayer transform changes all layer's descendants, but not the layer itself.
126 root
->resetAllChangeTrackingForSubtree();
127 root
->setSublayerTransform(arbitraryTransform
);
128 EXPECT_FALSE(root
->layerPropertyChanged());
129 EXPECT_TRUE(child
->layerPropertyChanged());
130 EXPECT_TRUE(grandChild
->layerPropertyChanged());
132 // Special case: check that setBounds changes behavior depending on masksToBounds.
133 root
->setMasksToBounds(false);
134 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->setBounds(gfx::Size(135, 246)));
135 root
->setMasksToBounds(true);
136 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->setBounds(arbitrarySize
)); // should be a different size than previous call, to ensure it marks tree changed.
138 // After setting all these properties already, setting to the exact same values again should
139 // not cause any change.
140 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setAnchorPoint(arbitraryPointF
));
141 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setAnchorPointZ(arbitraryNumber
));
142 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setMasksToBounds(true));
143 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setPosition(arbitraryPointF
));
144 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setPreserves3D(true));
145 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setTransform(arbitraryTransform
));
146 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setDoubleSided(false)); // constructor initializes it to "true".
147 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setScrollDelta(gfx::Vector2d()));
148 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setScrollOffset(arbitraryVector2d
));
149 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setImplTransform(arbitraryTransform
));
150 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setContentBounds(arbitrarySize
));
151 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setContentsScale(arbitraryNumber
, arbitraryNumber
));
152 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setContentsOpaque(true));
153 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setOpacity(arbitraryNumber
));
154 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setDrawsContent(true));
155 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setSublayerTransform(arbitraryTransform
));
156 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->setBounds(arbitrarySize
));