OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "cc/test/animation_timelines_test_common.h" | |
6 | |
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" | |
16 | |
17 namespace cc { | |
18 | |
19 TestLayer::TestLayer() { | |
20 ClearMutatedProperties(); | |
21 } | |
22 | |
23 void TestLayer::ClearMutatedProperties() { | |
24 transform_x_ = 0; | |
25 transform_y_ = 0; | |
26 | |
27 opacity_ = 0; | |
28 brightness_ = 0; | |
29 | |
30 for (int i = 0; i <= Animation::LAST_TARGET_PROPERTY; ++i) | |
31 mutated_properties_[i] = false; | |
32 } | |
33 | |
34 TestHostClient::TestHostClient(ThreadInstance thread_instance) | |
35 : host_(AnimationHost::Create(thread_instance)), | |
36 mutators_need_commit_(false) { | |
37 host_->SetMutatorHostClient(this); | |
38 } | |
39 | |
40 TestHostClient::~TestHostClient() { | |
41 for (auto& kv : layers_in_pending_tree_) | |
42 delete kv.second; | |
43 for (auto& kv : layers_in_active_tree_) | |
44 delete kv.second; | |
ajuma
2015/06/30 15:56:55
Can we express ownership using smart pointers, rat
loyso (OOO)
2015/07/01 02:25:47
Done.
| |
45 host_->SetMutatorHostClient(nullptr); | |
46 } | |
47 | |
48 void TestHostClient::ClearMutatedProperties() { | |
49 for (auto& kv : layers_in_pending_tree_) | |
50 kv.second->ClearMutatedProperties(); | |
51 for (auto& kv : layers_in_active_tree_) | |
52 kv.second->ClearMutatedProperties(); | |
53 } | |
54 | |
55 bool TestHostClient::IsLayerInTree(int layer_id, | |
56 LayerTreeType tree_type) const { | |
57 return tree_type == LayerTreeType::ACTIVE | |
58 ? layers_in_active_tree_.count(layer_id) | |
59 : layers_in_pending_tree_.count(layer_id); | |
60 } | |
61 | |
62 void TestHostClient::SetMutatorsNeedCommit() { | |
63 mutators_need_commit_ = true; | |
64 } | |
65 | |
66 void TestHostClient::SetLayerFilterMutated(int layer_id, | |
67 LayerTreeType tree_type, | |
68 const FilterOperations& filters) { | |
69 for (unsigned i = 0; i < filters.size(); ++i) { | |
70 const FilterOperation& filter = filters.at(i); | |
71 if (filter.type() == FilterOperation::BRIGHTNESS) { | |
72 TestLayer* layer = FindTestLayer(layer_id, tree_type); | |
73 layer->mutated_properties_[Animation::FILTER] = true; | |
74 layer->brightness_ = filter.amount(); | |
75 } | |
76 } | |
77 } | |
78 | |
79 void TestHostClient::SetLayerOpacityMutated(int layer_id, | |
80 LayerTreeType tree_type, | |
81 float opacity) { | |
82 TestLayer* layer = FindTestLayer(layer_id, tree_type); | |
83 layer->mutated_properties_[Animation::OPACITY] = true; | |
84 layer->opacity_ = opacity; | |
85 } | |
86 | |
87 void TestHostClient::SetLayerTransformMutated(int layer_id, | |
88 LayerTreeType tree_type, | |
89 const gfx::Transform& transform) { | |
90 TestLayer* layer = FindTestLayer(layer_id, tree_type); | |
91 layer->mutated_properties_[Animation::TRANSFORM] = true; | |
92 gfx::Vector2dF vec = transform.To2dTranslation(); | |
93 layer->transform_x_ = static_cast<int>(vec.x()); | |
94 layer->transform_y_ = static_cast<int>(vec.y()); | |
95 } | |
96 | |
97 void TestHostClient::SetLayerScrollOffsetMutated( | |
98 int layer_id, | |
99 LayerTreeType tree_type, | |
100 const gfx::ScrollOffset& scroll_offset) { | |
101 TestLayer* layer = FindTestLayer(layer_id, tree_type); | |
102 layer->mutated_properties_[Animation::SCROLL_OFFSET] = true; | |
103 layer->scroll_offset_ = scroll_offset; | |
104 } | |
105 | |
106 void TestHostClient::RegisterLayer(int layer_id, LayerTreeType tree_type) { | |
107 LayerIdToTestLayer& layers_in_tree = tree_type == LayerTreeType::ACTIVE | |
108 ? layers_in_active_tree_ | |
109 : layers_in_pending_tree_; | |
110 DCHECK(layers_in_tree.find(layer_id) == layers_in_tree.end()); | |
111 layers_in_tree.insert(std::make_pair(layer_id, new TestLayer())); | |
112 | |
113 DCHECK(host_); | |
114 host_->RegisterLayer(layer_id, tree_type); | |
115 } | |
116 | |
117 void TestHostClient::UnregisterLayer(int layer_id, LayerTreeType tree_type) { | |
118 DCHECK(host_); | |
119 host_->UnregisterLayer(layer_id, tree_type); | |
120 | |
121 LayerIdToTestLayer& layers_in_tree = tree_type == LayerTreeType::ACTIVE | |
122 ? layers_in_active_tree_ | |
123 : layers_in_pending_tree_; | |
124 auto kv = layers_in_tree.find(layer_id); | |
125 DCHECK(kv != layers_in_tree.end()); | |
126 delete kv->second; | |
127 layers_in_tree.erase(kv); | |
128 } | |
129 | |
130 bool TestHostClient::IsPropertyMutated( | |
131 int layer_id, | |
132 LayerTreeType tree_type, | |
133 Animation::TargetProperty property) const { | |
134 TestLayer* layer = FindTestLayer(layer_id, tree_type); | |
135 return layer->mutated_properties_[property]; | |
136 } | |
137 | |
138 void TestHostClient::ExpectFilterPropertyMutated(int layer_id, | |
139 LayerTreeType tree_type, | |
140 float brightness) const { | |
141 TestLayer* layer = FindTestLayer(layer_id, tree_type); | |
142 EXPECT_TRUE(layer->mutated_properties_[Animation::OPACITY]); | |
143 EXPECT_EQ(brightness, layer->brightness_); | |
144 } | |
145 | |
146 void TestHostClient::ExpectOpacityPropertyMutated(int layer_id, | |
147 LayerTreeType tree_type, | |
148 float opacity) const { | |
149 TestLayer* layer = FindTestLayer(layer_id, tree_type); | |
150 EXPECT_TRUE(layer->mutated_properties_[Animation::OPACITY]); | |
151 EXPECT_EQ(opacity, layer->opacity_); | |
152 } | |
153 | |
154 void TestHostClient::ExpectTransformPropertyMutated(int layer_id, | |
155 LayerTreeType tree_type, | |
156 int transform_x, | |
157 int transform_y) const { | |
158 TestLayer* layer = FindTestLayer(layer_id, tree_type); | |
159 EXPECT_TRUE(layer->mutated_properties_[Animation::OPACITY]); | |
160 EXPECT_EQ(transform_x, layer->transform_x_); | |
161 EXPECT_EQ(transform_y, layer->transform_y_); | |
162 } | |
163 | |
164 void TestHostClient::ExpectScrollOffsetPropertyMutated( | |
165 int layer_id, | |
166 LayerTreeType tree_type, | |
167 const gfx::ScrollOffset& scroll_offset) const { | |
168 TestLayer* layer = FindTestLayer(layer_id, tree_type); | |
169 EXPECT_TRUE(layer->mutated_properties_[Animation::OPACITY]); | |
170 EXPECT_EQ(scroll_offset, layer->scroll_offset_); | |
171 } | |
172 | |
173 TestLayer* TestHostClient::FindTestLayer(int layer_id, | |
174 LayerTreeType tree_type) const { | |
175 const LayerIdToTestLayer& layers_in_tree = tree_type == LayerTreeType::ACTIVE | |
176 ? layers_in_active_tree_ | |
177 : layers_in_pending_tree_; | |
178 auto kv = layers_in_tree.find(layer_id); | |
179 DCHECK(kv != layers_in_tree.end()); | |
180 DCHECK(kv->second); | |
181 return kv->second; | |
182 } | |
183 | |
184 TestAnimationDelegate::TestAnimationDelegate() | |
185 : started_(false), finished_(false) { | |
186 } | |
187 | |
188 void TestAnimationDelegate::NotifyAnimationStarted( | |
189 base::TimeTicks monotonic_time, | |
190 Animation::TargetProperty target_property, | |
191 int group) { | |
192 started_ = true; | |
193 } | |
194 void TestAnimationDelegate::NotifyAnimationFinished( | |
195 base::TimeTicks monotonic_time, | |
196 Animation::TargetProperty target_property, | |
197 int group) { | |
198 finished_ = true; | |
199 } | |
200 | |
201 AnimationTimelinesTest::AnimationTimelinesTest() | |
202 : client_(ThreadInstance::MAIN), | |
203 client_impl_(ThreadInstance::IMPL), | |
204 timeline_id_(AnimationIdProvider::NextTimelineId()), | |
205 player_id_(AnimationIdProvider::NextPlayerId()), | |
206 layer_id_(1) { | |
207 host_ = client_.host(); | |
208 host_impl_ = client_impl_.host(); | |
209 } | |
210 | |
211 AnimationTimelinesTest::~AnimationTimelinesTest() { | |
212 } | |
213 | |
214 void AnimationTimelinesTest::SetUp() { | |
215 timeline_ = AnimationTimeline::Create(timeline_id_); | |
216 player_ = AnimationPlayer::Create(player_id_); | |
217 } | |
218 | |
219 void AnimationTimelinesTest::GetImplTimelineAndPlayerByID() { | |
220 timeline_impl_ = host_impl_->GetTimelineById(timeline_id_); | |
221 EXPECT_TRUE(timeline_impl_); | |
222 player_impl_ = timeline_impl_->GetPlayerById(player_id_); | |
223 EXPECT_TRUE(player_impl_); | |
224 } | |
225 | |
226 void AnimationTimelinesTest::ReleaseRefPtrs() { | |
227 player_ = nullptr; | |
228 timeline_ = nullptr; | |
229 player_impl_ = nullptr; | |
230 timeline_impl_ = nullptr; | |
231 } | |
232 | |
233 void AnimationTimelinesTest::AnimateLayersTransferEvents( | |
234 base::TimeTicks time, | |
235 unsigned expect_events) { | |
236 scoped_ptr<AnimationEventsVector> events = | |
237 host_->animation_registrar()->CreateEvents(); | |
238 | |
239 host_impl_->animation_registrar()->AnimateLayers(time); | |
240 host_impl_->animation_registrar()->UpdateAnimationState(true, events.get()); | |
241 EXPECT_EQ(expect_events, events->size()); | |
242 | |
243 host_->animation_registrar()->AnimateLayers(time); | |
244 host_->animation_registrar()->UpdateAnimationState(true, nullptr); | |
245 host_->animation_registrar()->SetAnimationEvents(events.Pass()); | |
246 } | |
247 | |
248 AnimationPlayer* AnimationTimelinesTest::GetPlayerForLayerId(int layer_id) { | |
249 const ElementAnimations* element_animations = | |
250 host_->GetElementAnimationsForLayerId(layer_id); | |
251 return element_animations ? element_animations->players_list().head()->value() | |
252 : nullptr; | |
253 } | |
254 | |
255 AnimationPlayer* AnimationTimelinesTest::GetImplPlayerForLayerId(int layer_id) { | |
256 const ElementAnimations* element_animations = | |
257 host_impl_->GetElementAnimationsForLayerId(layer_id); | |
258 return element_animations ? element_animations->players_list().head()->value() | |
259 : nullptr; | |
260 } | |
261 | |
262 } // namespace cc | |
OLD | NEW |