1 // Copyright 2015 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/animation_timelines_test_common.h"
7 #include "cc/animation/animation_events.h"
8 #include "cc/animation/animation_id_provider.h"
9 #include "cc/animation/animation_player.h"
10 #include "cc/animation/animation_registrar.h"
11 #include "cc/animation/animation_timeline.h"
12 #include "cc/animation/element_animations.h"
13 #include "cc/output/filter_operation.h"
14 #include "cc/output/filter_operations.h"
15 #include "ui/gfx/transform.h"
19 scoped_ptr
<TestLayer
> TestLayer::Create() {
20 return make_scoped_ptr(new TestLayer());
23 TestLayer::TestLayer() {
24 ClearMutatedProperties();
27 void TestLayer::ClearMutatedProperties() {
34 for (int i
= 0; i
<= Animation::LAST_TARGET_PROPERTY
; ++i
)
35 mutated_properties_
[i
] = false;
38 TestHostClient::TestHostClient()
39 : host_(AnimationHost::Create()), mutators_need_commit_(false) {
40 host_
->SetMutatorHostClient(this);
43 TestHostClient::~TestHostClient() {
44 host_
->SetMutatorHostClient(nullptr);
47 void TestHostClient::ClearMutatedProperties() {
48 for (auto& kv
: layers_in_pending_tree_
)
49 kv
.second
->ClearMutatedProperties();
50 for (auto& kv
: layers_in_active_tree_
)
51 kv
.second
->ClearMutatedProperties();
54 bool TestHostClient::IsLayerInTree(int layer_id
,
55 LayerTreeType tree_type
) const {
56 return tree_type
== LayerTreeType::ACTIVE
57 ? layers_in_active_tree_
.count(layer_id
)
58 : layers_in_pending_tree_
.count(layer_id
);
61 void TestHostClient::SetMutatorsNeedCommit() {
62 mutators_need_commit_
= true;
65 void TestHostClient::SetLayerFilterMutated(int layer_id
,
66 LayerTreeType tree_type
,
67 const FilterOperations
& filters
) {
68 for (unsigned i
= 0; i
< filters
.size(); ++i
) {
69 const FilterOperation
& filter
= filters
.at(i
);
70 if (filter
.type() == FilterOperation::BRIGHTNESS
) {
71 TestLayer
* layer
= FindTestLayer(layer_id
, tree_type
);
72 layer
->set_brightness(filter
.amount());
77 void TestHostClient::SetLayerOpacityMutated(int layer_id
,
78 LayerTreeType tree_type
,
80 TestLayer
* layer
= FindTestLayer(layer_id
, tree_type
);
81 layer
->set_opacity(opacity
);
84 void TestHostClient::SetLayerTransformMutated(int layer_id
,
85 LayerTreeType tree_type
,
86 const gfx::Transform
& transform
) {
87 TestLayer
* layer
= FindTestLayer(layer_id
, tree_type
);
88 gfx::Vector2dF vec
= transform
.To2dTranslation();
89 layer
->set_transform(static_cast<int>(vec
.x()), static_cast<int>(vec
.y()));
92 void TestHostClient::SetLayerScrollOffsetMutated(
94 LayerTreeType tree_type
,
95 const gfx::ScrollOffset
& scroll_offset
) {
96 TestLayer
* layer
= FindTestLayer(layer_id
, tree_type
);
97 layer
->set_scroll_offset(scroll_offset
);
100 void TestHostClient::RegisterLayer(int layer_id
, LayerTreeType tree_type
) {
101 LayerIdToTestLayer
& layers_in_tree
= tree_type
== LayerTreeType::ACTIVE
102 ? layers_in_active_tree_
103 : layers_in_pending_tree_
;
104 DCHECK(layers_in_tree
.find(layer_id
) == layers_in_tree
.end());
105 layers_in_tree
.add(layer_id
, TestLayer::Create());
108 host_
->RegisterLayer(layer_id
, tree_type
);
111 void TestHostClient::UnregisterLayer(int layer_id
, LayerTreeType tree_type
) {
113 host_
->UnregisterLayer(layer_id
, tree_type
);
115 LayerIdToTestLayer
& layers_in_tree
= tree_type
== LayerTreeType::ACTIVE
116 ? layers_in_active_tree_
117 : layers_in_pending_tree_
;
118 auto kv
= layers_in_tree
.find(layer_id
);
119 DCHECK(kv
!= layers_in_tree
.end());
120 layers_in_tree
.erase(kv
);
123 bool TestHostClient::IsPropertyMutated(
125 LayerTreeType tree_type
,
126 Animation::TargetProperty property
) const {
127 TestLayer
* layer
= FindTestLayer(layer_id
, tree_type
);
128 return layer
->is_property_mutated(property
);
131 void TestHostClient::ExpectFilterPropertyMutated(int layer_id
,
132 LayerTreeType tree_type
,
133 float brightness
) const {
134 TestLayer
* layer
= FindTestLayer(layer_id
, tree_type
);
135 EXPECT_TRUE(layer
->is_property_mutated(Animation::OPACITY
));
136 EXPECT_EQ(brightness
, layer
->brightness());
139 void TestHostClient::ExpectOpacityPropertyMutated(int layer_id
,
140 LayerTreeType tree_type
,
141 float opacity
) const {
142 TestLayer
* layer
= FindTestLayer(layer_id
, tree_type
);
143 EXPECT_TRUE(layer
->is_property_mutated(Animation::OPACITY
));
144 EXPECT_EQ(opacity
, layer
->opacity());
147 void TestHostClient::ExpectTransformPropertyMutated(int layer_id
,
148 LayerTreeType tree_type
,
150 int transform_y
) const {
151 TestLayer
* layer
= FindTestLayer(layer_id
, tree_type
);
152 EXPECT_TRUE(layer
->is_property_mutated(Animation::OPACITY
));
153 EXPECT_EQ(transform_x
, layer
->transform_x());
154 EXPECT_EQ(transform_y
, layer
->transform_y());
157 void TestHostClient::ExpectScrollOffsetPropertyMutated(
159 LayerTreeType tree_type
,
160 const gfx::ScrollOffset
& scroll_offset
) const {
161 TestLayer
* layer
= FindTestLayer(layer_id
, tree_type
);
162 EXPECT_TRUE(layer
->is_property_mutated(Animation::OPACITY
));
163 EXPECT_EQ(scroll_offset
, layer
->scroll_offset());
166 TestLayer
* TestHostClient::FindTestLayer(int layer_id
,
167 LayerTreeType tree_type
) const {
168 const LayerIdToTestLayer
& layers_in_tree
= tree_type
== LayerTreeType::ACTIVE
169 ? layers_in_active_tree_
170 : layers_in_pending_tree_
;
171 auto kv
= layers_in_tree
.find(layer_id
);
172 DCHECK(kv
!= layers_in_tree
.end());
177 TestAnimationDelegate::TestAnimationDelegate()
178 : started_(false), finished_(false) {
181 void TestAnimationDelegate::NotifyAnimationStarted(
182 base::TimeTicks monotonic_time
,
183 Animation::TargetProperty target_property
,
187 void TestAnimationDelegate::NotifyAnimationFinished(
188 base::TimeTicks monotonic_time
,
189 Animation::TargetProperty target_property
,
194 AnimationTimelinesTest::AnimationTimelinesTest()
195 : timeline_id_(AnimationIdProvider::NextTimelineId()),
196 player_id_(AnimationIdProvider::NextPlayerId()),
198 host_
= client_
.host();
199 host_impl_
= client_impl_
.host();
202 AnimationTimelinesTest::~AnimationTimelinesTest() {
205 void AnimationTimelinesTest::SetUp() {
206 timeline_
= AnimationTimeline::Create(timeline_id_
);
207 player_
= AnimationPlayer::Create(player_id_
);
210 void AnimationTimelinesTest::GetImplTimelineAndPlayerByID() {
211 timeline_impl_
= host_impl_
->GetTimelineById(timeline_id_
);
212 EXPECT_TRUE(timeline_impl_
);
213 player_impl_
= timeline_impl_
->GetPlayerById(player_id_
);
214 EXPECT_TRUE(player_impl_
);
217 void AnimationTimelinesTest::ReleaseRefPtrs() {
220 player_impl_
= nullptr;
221 timeline_impl_
= nullptr;
224 void AnimationTimelinesTest::AnimateLayersTransferEvents(
225 base::TimeTicks time
,
226 unsigned expect_events
) {
227 scoped_ptr
<AnimationEventsVector
> events
=
228 host_
->animation_registrar()->CreateEvents();
230 host_impl_
->animation_registrar()->AnimateLayers(time
);
231 host_impl_
->animation_registrar()->UpdateAnimationState(true, events
.get());
232 EXPECT_EQ(expect_events
, events
->size());
234 host_
->animation_registrar()->AnimateLayers(time
);
235 host_
->animation_registrar()->UpdateAnimationState(true, nullptr);
236 host_
->animation_registrar()->SetAnimationEvents(events
.Pass());
239 AnimationPlayer
* AnimationTimelinesTest::GetPlayerForLayerId(int layer_id
) {
240 const ElementAnimations
* element_animations
=
241 host_
->GetElementAnimationsForLayerId(layer_id
);
242 return element_animations
? element_animations
->players_list().head()->value()
246 AnimationPlayer
* AnimationTimelinesTest::GetImplPlayerForLayerId(int layer_id
) {
247 const ElementAnimations
* element_animations
=
248 host_impl_
->GetElementAnimationsForLayerId(layer_id
);
249 return element_animations
? element_animations
->players_list().head()->value()