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