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.
5 #include "ui/compositor/layer_owner.h"
7 #include "base/test/null_task_runner.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/compositor/compositor.h"
10 #include "ui/compositor/layer.h"
11 #include "ui/compositor/layer_animation_observer.h"
12 #include "ui/compositor/layer_animator.h"
13 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
14 #include "ui/compositor/scoped_layer_animation_settings.h"
15 #include "ui/compositor/test/context_factories_for_test.h"
16 #include "ui/gfx/native_widget_types.h"
21 // An animation observer that confirms upon animation completion, that the
22 // compositor is not null.
23 class TestLayerAnimationObserver
: public ImplicitAnimationObserver
{
25 TestLayerAnimationObserver(Layer
* layer
) : layer_(layer
) {}
26 ~TestLayerAnimationObserver() override
{}
28 // ImplicitAnimationObserver:
29 void OnImplicitAnimationsCompleted() override
{
30 EXPECT_NE(nullptr, layer_
->GetCompositor());
36 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationObserver
);
39 // Test fixture for LayerOwner tests that require a ui::Compositor.
40 class LayerOwnerTestWithCompositor
: public testing::Test
{
42 LayerOwnerTestWithCompositor();
43 ~LayerOwnerTestWithCompositor() override
;
45 void SetUp() override
;
46 void TearDown() override
;
49 ui::Compositor
* compositor() { return compositor_
.get(); }
52 scoped_ptr
<ui::Compositor
> compositor_
;
54 DISALLOW_COPY_AND_ASSIGN(LayerOwnerTestWithCompositor
);
57 LayerOwnerTestWithCompositor::LayerOwnerTestWithCompositor() {
60 LayerOwnerTestWithCompositor::~LayerOwnerTestWithCompositor() {
63 void LayerOwnerTestWithCompositor::SetUp() {
64 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
=
65 new base::NullTaskRunner();
67 ui::ContextFactory
* context_factory
=
68 ui::InitializeContextFactoryForTests(false);
70 compositor_
.reset(new ui::Compositor(context_factory
, task_runner
));
71 compositor_
->SetAcceleratedWidgetAndStartCompositor(
72 gfx::kNullAcceleratedWidget
);
75 void LayerOwnerTestWithCompositor::TearDown() {
77 ui::TerminateContextFactoryForTests();
82 TEST(LayerOwnerTest
, RecreateLayerHonorsAnimationTargets
) {
84 Layer
* layer
= new Layer(LAYER_SOLID_COLOR
);
85 layer
->SetVisible(true);
86 layer
->SetOpacity(1.0f
);
87 layer
->SetColor(SK_ColorRED
);
89 owner
.SetLayer(layer
);
91 ScopedLayerAnimationSettings
settings(layer
->GetAnimator());
92 layer
->SetVisible(false);
93 layer
->SetOpacity(0.0f
);
94 layer
->SetColor(SK_ColorGREEN
);
95 EXPECT_TRUE(layer
->visible());
96 EXPECT_EQ(1.0f
, layer
->opacity());
97 EXPECT_EQ(SK_ColorRED
, layer
->background_color());
99 scoped_ptr
<Layer
> old_layer(owner
.RecreateLayer());
100 EXPECT_FALSE(owner
.layer()->visible());
101 EXPECT_EQ(0.0f
, owner
.layer()->opacity());
102 EXPECT_EQ(SK_ColorGREEN
, owner
.layer()->background_color());
105 // Tests that when a LAYER_SOLID_COLOR which is not backed by a SolidColorLayer
106 // that opaqueness and color targets are maintained when the
107 // LayerOwner::RecreateLayers is called.
108 TEST(LayerOwnerTest
, RecreateLayerSolidColorWithChangedCCLayerHonorsTargets
) {
109 SkColor transparent
= SK_ColorTRANSPARENT
;
111 Layer
* layer
= new Layer(LAYER_SOLID_COLOR
);
112 owner
.SetLayer(layer
);
113 layer
->SetFillsBoundsOpaquely(false);
114 layer
->SetColor(transparent
);
115 // Changing the backing layer takes LAYER_SOLID_COLOR off of the normal layer
116 // flow, need to ensure that set values are maintained.
117 layer
->SwitchCCLayerForTest();
119 EXPECT_FALSE(layer
->fills_bounds_opaquely());
120 EXPECT_EQ(transparent
, layer
->background_color());
121 EXPECT_EQ(transparent
, layer
->GetTargetColor());
123 scoped_ptr
<Layer
> old_layer(owner
.RecreateLayer());
124 EXPECT_FALSE(owner
.layer()->fills_bounds_opaquely());
125 EXPECT_EQ(transparent
, owner
.layer()->background_color());
126 EXPECT_EQ(transparent
, owner
.layer()->GetTargetColor());
129 TEST(LayerOwnerTest
, RecreateRootLayerWithNullCompositor
) {
131 Layer
* layer
= new Layer
;
132 owner
.SetLayer(layer
);
134 scoped_ptr
<Layer
> layer_copy
= owner
.RecreateLayer();
136 EXPECT_EQ(nullptr, owner
.layer()->GetCompositor());
137 EXPECT_EQ(nullptr, layer_copy
->GetCompositor());
140 TEST_F(LayerOwnerTestWithCompositor
, RecreateRootLayerWithCompositor
) {
142 Layer
* layer
= new Layer
;
143 owner
.SetLayer(layer
);
145 compositor()->SetRootLayer(layer
);
147 scoped_ptr
<Layer
> layer_copy
= owner
.RecreateLayer();
149 EXPECT_EQ(compositor(), owner
.layer()->GetCompositor());
150 EXPECT_EQ(owner
.layer(), compositor()->root_layer());
151 EXPECT_EQ(nullptr, layer_copy
->GetCompositor());
154 // Tests that recreating the root layer, while one of its children is animating,
155 // properly updates the compositor. So that compositor is not null for observers
156 // of animations being cancelled.
157 TEST_F(LayerOwnerTestWithCompositor
, RecreateRootLayerDuringAnimation
) {
159 Layer
* layer
= new Layer
;
160 owner
.SetLayer(layer
);
161 compositor()->SetRootLayer(layer
);
163 scoped_ptr
<Layer
> child(new Layer
);
164 child
->SetBounds(gfx::Rect(0, 0, 100, 100));
165 layer
->Add(child
.get());
167 // This observer checks that the compositor of |child| is not null upon
168 // animation completion.
169 scoped_ptr
<TestLayerAnimationObserver
> observer(
170 new TestLayerAnimationObserver(child
.get()));
171 scoped_ptr
<ui::ScopedAnimationDurationScaleMode
> long_duration_animation(
172 new ui::ScopedAnimationDurationScaleMode(
173 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION
));
175 ui::ScopedLayerAnimationSettings
animation(child
->GetAnimator());
176 animation
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(1000));
177 animation
.AddObserver(observer
.get());
178 gfx::Transform transform
;
179 transform
.Scale(0.5f
, 0.5f
);
180 child
->SetTransform(transform
);
183 scoped_ptr
<Layer
> layer_copy
= owner
.RecreateLayer();
186 // Tests that recreating a non-root layer, while one of its children is
187 // animating, properly updates the compositor. So that compositor is not null
188 // for observers of animations being cancelled.
189 TEST_F(LayerOwnerTestWithCompositor
, RecreateNonRootLayerDuringAnimation
) {
190 scoped_ptr
<Layer
> root_layer(new Layer
);
191 compositor()->SetRootLayer(root_layer
.get());
194 Layer
* layer
= new Layer
;
195 owner
.SetLayer(layer
);
196 root_layer
->Add(layer
);
198 scoped_ptr
<Layer
> child(new Layer
);
199 child
->SetBounds(gfx::Rect(0, 0, 100, 100));
200 layer
->Add(child
.get());
202 // This observer checks that the compositor of |child| is not null upon
203 // animation completion.
204 scoped_ptr
<TestLayerAnimationObserver
> observer(
205 new TestLayerAnimationObserver(child
.get()));
206 scoped_ptr
<ui::ScopedAnimationDurationScaleMode
> long_duration_animation(
207 new ui::ScopedAnimationDurationScaleMode(
208 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION
));
210 ui::ScopedLayerAnimationSettings
animation(child
->GetAnimator());
211 animation
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(1000));
212 animation
.AddObserver(observer
.get());
213 gfx::Transform transform
;
214 transform
.Scale(0.5f
, 0.5f
);
215 child
->SetTransform(transform
);
218 scoped_ptr
<Layer
> layer_copy
= owner
.RecreateLayer();