Chromium Code Reviews| Index: cc/animation/animation_player_unittest.cc |
| diff --git a/cc/animation/animation_player_unittest.cc b/cc/animation/animation_player_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a969af1bb4af616331d1eb250d4ba9902781d1ed |
| --- /dev/null |
| +++ b/cc/animation/animation_player_unittest.cc |
| @@ -0,0 +1,490 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "cc/animation/animation_player.h" |
| + |
| +#include "base/containers/hash_tables.h" |
| +#include "cc/animation/animation_delegate.h" |
| +#include "cc/animation/animation_host.h" |
| +#include "cc/animation/animation_id_provider.h" |
| +#include "cc/animation/animation_registrar.h" |
| +#include "cc/animation/animation_timeline.h" |
| +#include "cc/test/animation_test_common.h" |
| +#include "cc/trees/layer_tree_mutators_client.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace cc { |
| +namespace { |
| + |
| +struct LayerTest { |
| + LayerTest() { ClearMutatedProperties(); } |
| + |
| + void ClearMutatedProperties() { |
| + for (int i = 0; i <= Animation::LAST_TARGET_PROPERTY; ++i) |
| + mutated_properties_[i] = false; |
| + } |
| + |
| + bool mutated_properties_[Animation::LAST_TARGET_PROPERTY + 1]; |
| +}; |
| + |
| +class LayerTreeMutatorsClientTest : public LayerTreeMutatorsClient { |
| + public: |
| + explicit LayerTreeMutatorsClientTest(bool is_impl_instance) |
| + : host_(AnimationHost::Create(is_impl_instance)), |
| + mutators_need_commit_(false) { |
| + host_->SetLayerTreeMutatorsClient(this); |
| + } |
| + |
| + ~LayerTreeMutatorsClientTest() { |
| + for (auto& kv : layers_in_pending_tree_) |
| + delete kv.second; |
| + for (auto& kv : layers_in_active_tree_) |
| + delete kv.second; |
| + host_->SetLayerTreeMutatorsClient(nullptr); |
| + } |
| + |
| + void ClearMutatedProperties() { |
| + for (auto& kv : layers_in_pending_tree_) |
| + kv.second->ClearMutatedProperties(); |
| + for (auto& kv : layers_in_active_tree_) |
| + kv.second->ClearMutatedProperties(); |
| + } |
| + |
| + bool IsLayerInActiveTree(int layer_id) const override { |
| + return layers_in_active_tree_.count(layer_id); |
| + } |
| + |
| + bool IsLayerInPendingTree(int layer_id) const override { |
| + return layers_in_pending_tree_.count(layer_id); |
| + } |
| + |
| + void SetMutatorsNeedCommit() override { mutators_need_commit_ = true; } |
| + |
| + void SetLayerFilterMutated(int layer_id, |
| + bool active_tree, |
| + const FilterOperations& filters) override { |
| + SetLayerMutated(layer_id, active_tree, Animation::FILTER); |
| + } |
| + |
| + void SetLayerOpacityMutated(int layer_id, |
| + bool active_tree, |
| + float opacity) override { |
| + SetLayerMutated(layer_id, active_tree, Animation::OPACITY); |
| + } |
| + |
| + void SetLayerTransformMutated(int layer_id, |
| + bool active_tree, |
| + const gfx::Transform& transform) override { |
| + SetLayerMutated(layer_id, active_tree, Animation::TRANSFORM); |
| + } |
| + |
| + void SetLayerScrollOffsetMutated( |
| + int layer_id, |
| + bool active_tree, |
| + const gfx::ScrollOffset& scroll_offset) override { |
| + SetLayerMutated(layer_id, active_tree, Animation::SCROLL_OFFSET); |
| + } |
| + |
| + bool mutators_need_commit() const { return mutators_need_commit_; } |
| + void set_mutators_need_commit(bool need) { mutators_need_commit_ = need; } |
| + |
| + void RegisterLayer(int layer_id, bool active_tree) { |
| + LayerIdToTestLayer& layers_in_tree = |
| + active_tree ? layers_in_active_tree_ : layers_in_pending_tree_; |
| + DCHECK(layers_in_tree.find(layer_id) == layers_in_tree.end()); |
| + layers_in_tree.insert(std::make_pair(layer_id, new LayerTest())); |
| + |
| + DCHECK(host_); |
| + host_->RegisterLayer(layer_id, active_tree); |
| + } |
| + |
| + void UnregisterLayer(int layer_id, bool active_tree) { |
| + DCHECK(host_); |
| + host_->UnregisterLayer(layer_id, active_tree); |
| + |
| + LayerIdToTestLayer& layers_in_tree = |
| + active_tree ? layers_in_active_tree_ : layers_in_pending_tree_; |
| + auto kv = layers_in_tree.find(layer_id); |
| + DCHECK(kv != layers_in_tree.end()); |
| + delete kv->second; |
| + layers_in_tree.erase(kv); |
| + } |
| + |
| + AnimationHost* host() { |
| + DCHECK(host_); |
| + return host_.get(); |
| + } |
| + |
| + bool IsPropertyMutated(int layer_id, |
| + bool active_tree, |
| + Animation::TargetProperty property) { |
| + LayerIdToTestLayer& layers_in_tree = |
| + active_tree ? layers_in_active_tree_ : layers_in_pending_tree_; |
| + auto kv = layers_in_tree.find(layer_id); |
| + DCHECK(kv != layers_in_tree.end()); |
| + return kv->second->mutated_properties_[property]; |
| + } |
| + |
| + private: |
| + void SetLayerMutated(int layer_id, |
| + bool active_tree, |
| + Animation::TargetProperty property) { |
| + LayerIdToTestLayer& layers_in_tree = |
| + active_tree ? layers_in_active_tree_ : layers_in_pending_tree_; |
| + auto kv = layers_in_tree.find(layer_id); |
| + DCHECK(kv != layers_in_tree.end()); |
| + kv->second->mutated_properties_[property] = true; |
| + } |
| + |
| + scoped_ptr<AnimationHost> host_; |
| + |
| + typedef base::hash_map<int, LayerTest*> LayerIdToTestLayer; |
| + LayerIdToTestLayer layers_in_active_tree_; |
| + LayerIdToTestLayer layers_in_pending_tree_; |
| + |
| + bool mutators_need_commit_; |
| +}; |
| + |
| +class AnimationDelegateTest : public AnimationDelegate { |
|
Ian Vollick
2015/05/05 03:52:07
Name's a bit confusing; this isn't a test.
How ab
loyso (OOO)
2015/05/05 06:51:18
Acknowledged.
loyso (OOO)
2015/06/22 07:48:51
Done.
|
| + public: |
| + AnimationDelegateTest() : started_(false), finished_(false) {} |
| + |
| + void NotifyAnimationStarted(base::TimeTicks monotonic_time, |
| + Animation::TargetProperty target_property, |
| + int group) override { |
| + started_ = true; |
| + } |
| + void NotifyAnimationFinished(base::TimeTicks monotonic_time, |
| + Animation::TargetProperty target_property, |
| + int group) override { |
| + finished_ = true; |
| + } |
| + |
| + bool started_; |
| + bool finished_; |
| +}; |
| + |
| +class AnimationPlayerTest : public testing::Test { |
| + public: |
| + AnimationPlayerTest() |
| + : client_(false), |
| + client_impl_(true), |
| + timeline_id_(AnimationIdProvider::NextTimelineId()), |
| + player_id_(AnimationIdProvider::NextPlayerId()), |
| + layer_id_(1) { |
| + host_ = client_.host(); |
| + host_impl_ = client_impl_.host(); |
| + } |
| + |
| + protected: |
| + void SetUp() override { |
| + timeline_ = AnimationTimeline::Create(timeline_id_); |
| + player_ = AnimationPlayer::Create(player_id_); |
| + } |
| + |
| + void GetImplObjectsByIDs() { |
|
Ian Vollick
2015/05/05 03:52:07
nit: I find the name of this function uninformativ
loyso (OOO)
2015/05/05 06:51:18
It brings me impl-side object pointers if only IDs
Ian Vollick
2015/05/07 13:58:43
I don't feel strongly about this one, but the bit
loyso (OOO)
2015/05/08 01:04:35
Acknowledged.
loyso (OOO)
2015/06/22 07:48:51
Done.
|
| + timeline_impl_ = host_impl_->GetTimelineById(timeline_id_); |
| + EXPECT_TRUE(timeline_impl_); |
| + player_impl_ = timeline_impl_->GetPlayerById(player_id_); |
| + EXPECT_TRUE(player_impl_); |
| + } |
| + |
| + void ReleaseRefPtrs() { |
| + player_ = nullptr; |
| + timeline_ = nullptr; |
| + player_impl_ = nullptr; |
| + timeline_impl_ = nullptr; |
| + } |
| + |
| + LayerTreeMutatorsClientTest client_; |
| + LayerTreeMutatorsClientTest client_impl_; |
| + |
| + AnimationHost* host_; |
| + AnimationHost* host_impl_; |
| + |
| + const int timeline_id_; |
| + const int player_id_; |
| + const int layer_id_; |
| + |
| + scoped_refptr<AnimationTimeline> timeline_; |
| + scoped_refptr<AnimationPlayer> player_; |
| + |
| + scoped_refptr<AnimationTimeline> timeline_impl_; |
| + scoped_refptr<AnimationPlayer> player_impl_; |
| +}; |
| + |
| +TEST_F(AnimationPlayerTest, AttachDetachLayerIfTimelineAttached) { |
| + host_->AddAnimationTimeline(timeline_.get()); |
| + timeline_->AttachPlayer(player_.get()); |
| + EXPECT_FALSE(player_->layer_animation_controller()); |
| + EXPECT_FALSE(player_->layer_id()); |
| + |
| + host_->PushPropertiesTo(host_impl_); |
| + |
| + EXPECT_FALSE(host_impl_->GetPlayerForLayerId(layer_id_)); |
| + |
| + GetImplObjectsByIDs(); |
| + |
| + EXPECT_FALSE(player_impl_->layer_animation_controller()); |
| + EXPECT_FALSE(player_impl_->layer_id()); |
| + |
| + player_->AttachLayer(layer_id_); |
| + EXPECT_EQ(player_, host_->GetPlayerForLayerId(layer_id_)); |
| + EXPECT_TRUE(player_->layer_animation_controller()); |
| + EXPECT_EQ(player_->layer_id(), layer_id_); |
| + |
| + host_->PushPropertiesTo(host_impl_); |
| + |
| + EXPECT_EQ(player_impl_, host_impl_->GetPlayerForLayerId(layer_id_)); |
| + EXPECT_TRUE(player_impl_->layer_animation_controller()); |
| + EXPECT_EQ(player_impl_->layer_id(), layer_id_); |
| + |
| + player_->DetachLayer(); |
| + EXPECT_FALSE(host_->GetPlayerForLayerId(layer_id_)); |
| + EXPECT_FALSE(player_->layer_animation_controller()); |
| + EXPECT_FALSE(player_->layer_id()); |
| + |
| + host_->PushPropertiesTo(host_impl_); |
| + |
| + EXPECT_FALSE(host_impl_->GetPlayerForLayerId(layer_id_)); |
| + EXPECT_FALSE(player_impl_->layer_animation_controller()); |
| + EXPECT_FALSE(player_impl_->layer_id()); |
| + |
| + timeline_->DetachPlayer(player_.get()); |
| + EXPECT_FALSE(player_->animation_timeline()); |
| + EXPECT_FALSE(player_->layer_animation_controller()); |
| + EXPECT_FALSE(player_->layer_id()); |
| +} |
| + |
| +TEST_F(AnimationPlayerTest, AttachDetachTimelineIfLayerAttached) { |
| + host_->AddAnimationTimeline(timeline_.get()); |
| + |
| + EXPECT_FALSE(player_->layer_animation_controller()); |
| + EXPECT_FALSE(player_->layer_id()); |
| + |
| + player_->AttachLayer(layer_id_); |
| + EXPECT_FALSE(player_->animation_timeline()); |
| + EXPECT_FALSE(host_->GetPlayerForLayerId(layer_id_)); |
| + EXPECT_FALSE(player_->layer_animation_controller()); |
| + EXPECT_EQ(player_->layer_id(), layer_id_); |
| + |
| + timeline_->AttachPlayer(player_.get()); |
| + EXPECT_EQ(timeline_, player_->animation_timeline()); |
| + EXPECT_EQ(player_, host_->GetPlayerForLayerId(layer_id_)); |
| + EXPECT_TRUE(player_->layer_animation_controller()); |
| + EXPECT_EQ(player_->layer_id(), layer_id_); |
| + |
| + // Removing player from timeline detaches layer. |
| + timeline_->DetachPlayer(player_.get()); |
| + EXPECT_FALSE(player_->animation_timeline()); |
| + EXPECT_FALSE(host_->GetPlayerForLayerId(layer_id_)); |
| + EXPECT_FALSE(player_->layer_animation_controller()); |
| + EXPECT_FALSE(player_->layer_id()); |
| +} |
| + |
| +TEST_F(AnimationPlayerTest, AttachToLayerInActiveTree) { |
| + // Set up the layer which is in active tree for main thread and not |
| + // yet passed onto the impl thread. |
| + client_.RegisterLayer(layer_id_, true); |
| + client_impl_.RegisterLayer(layer_id_, false); |
| + |
| + EXPECT_TRUE(client_.IsLayerInActiveTree(layer_id_)); |
| + EXPECT_FALSE(client_.IsLayerInPendingTree(layer_id_)); |
| + |
| + host_->AddAnimationTimeline(timeline_.get()); |
| + |
| + timeline_->AttachPlayer(player_.get()); |
| + player_->AttachLayer(layer_id_); |
| + EXPECT_TRUE(player_->has_active_value_observer_for_testing()); |
| + EXPECT_FALSE(player_->has_pending_value_observer_for_testing()); |
| + EXPECT_TRUE(player_->layer_animation_controller()); |
| + |
| + host_->PushPropertiesTo(host_impl_); |
| + |
| + GetImplObjectsByIDs(); |
| + |
| + EXPECT_FALSE(player_impl_->has_active_value_observer_for_testing()); |
| + EXPECT_TRUE(player_impl_->has_pending_value_observer_for_testing()); |
| + |
| + // Create the layer in the impl active tree. |
| + client_impl_.RegisterLayer(layer_id_, true); |
| + EXPECT_TRUE(player_impl_->has_active_value_observer_for_testing()); |
| + EXPECT_TRUE(player_impl_->has_pending_value_observer_for_testing()); |
| + |
| + EXPECT_TRUE(client_impl_.IsLayerInActiveTree(layer_id_)); |
| + EXPECT_TRUE(client_impl_.IsLayerInPendingTree(layer_id_)); |
| + |
| + // kill layer on main thread. |
| + client_.UnregisterLayer(layer_id_, true); |
| + EXPECT_FALSE(player_->has_active_value_observer_for_testing()); |
| + EXPECT_FALSE(player_->has_pending_value_observer_for_testing()); |
| + EXPECT_TRUE(player_->layer_animation_controller()); |
| + |
| + // Sync doesn't detach LayerImpl. |
| + host_->PushPropertiesTo(host_impl_); |
| + EXPECT_TRUE(player_impl_->has_active_value_observer_for_testing()); |
| + EXPECT_TRUE(player_impl_->has_pending_value_observer_for_testing()); |
| + EXPECT_TRUE(player_impl_->layer_animation_controller()); |
| + |
| + // Kill layer on impl thread in pending tree. |
| + client_impl_.UnregisterLayer(layer_id_, false); |
| + EXPECT_TRUE(player_impl_->has_active_value_observer_for_testing()); |
| + EXPECT_FALSE(player_impl_->has_pending_value_observer_for_testing()); |
| + EXPECT_TRUE(player_impl_->layer_animation_controller()); |
| + |
| + // Kill layer on impl thread in active tree. |
| + client_impl_.UnregisterLayer(layer_id_, true); |
| + EXPECT_FALSE(player_impl_->has_active_value_observer_for_testing()); |
| + EXPECT_FALSE(player_impl_->has_pending_value_observer_for_testing()); |
| + EXPECT_TRUE(player_impl_->layer_animation_controller()); |
| + |
| + // Sync doesn't change anything. |
| + host_->PushPropertiesTo(host_impl_); |
| + EXPECT_FALSE(player_impl_->has_active_value_observer_for_testing()); |
| + EXPECT_FALSE(player_impl_->has_pending_value_observer_for_testing()); |
| + EXPECT_TRUE(player_impl_->layer_animation_controller()); |
| + |
| + player_->DetachLayer(); |
| + EXPECT_FALSE(player_->layer_animation_controller()); |
| + |
| + // Release ptrs now to test the order of destruction. |
| + ReleaseRefPtrs(); |
| +} |
| + |
| +TEST_F(AnimationPlayerTest, AttachToNotYetCreatedLayer) { |
| + host_->AddAnimationTimeline(timeline_.get()); |
| + timeline_->AttachPlayer(player_.get()); |
| + |
| + host_->PushPropertiesTo(host_impl_); |
| + |
| + GetImplObjectsByIDs(); |
| + |
| + player_->AttachLayer(layer_id_); |
| + EXPECT_FALSE(player_->has_active_value_observer_for_testing()); |
| + EXPECT_FALSE(player_->has_pending_value_observer_for_testing()); |
| + EXPECT_TRUE(player_->layer_animation_controller()); |
| + |
| + host_->PushPropertiesTo(host_impl_); |
| + EXPECT_FALSE(player_impl_->has_active_value_observer_for_testing()); |
| + EXPECT_FALSE(player_impl_->has_pending_value_observer_for_testing()); |
| + EXPECT_TRUE(player_impl_->layer_animation_controller()); |
| + |
| + // Create layer. |
| + client_.RegisterLayer(layer_id_, true); |
| + EXPECT_TRUE(player_->has_active_value_observer_for_testing()); |
| + EXPECT_FALSE(player_->has_pending_value_observer_for_testing()); |
| + |
| + client_impl_.RegisterLayer(layer_id_, false); |
| + EXPECT_FALSE(player_impl_->has_active_value_observer_for_testing()); |
| + EXPECT_TRUE(player_impl_->has_pending_value_observer_for_testing()); |
| + |
| + client_impl_.RegisterLayer(layer_id_, true); |
| + EXPECT_TRUE(player_impl_->has_active_value_observer_for_testing()); |
| + EXPECT_TRUE(player_impl_->has_pending_value_observer_for_testing()); |
| +} |
| + |
| +TEST_F(AnimationPlayerTest, PropertiesMutate) { |
| + client_.RegisterLayer(layer_id_, true); |
| + client_impl_.RegisterLayer(layer_id_, false); |
| + client_impl_.RegisterLayer(layer_id_, true); |
| + |
| + host_->AddAnimationTimeline(timeline_.get()); |
| + timeline_->AttachPlayer(player_.get()); |
| + player_->AttachLayer(layer_id_); |
| + |
| + AddOpacityTransitionToPlayer(player_.get(), 1., .7f, .3f, false); |
| + AddAnimatedTransformToPlayer(player_.get(), 1., 1, 1); |
| + AddAnimatedFilterToPlayer(player_.get(), 1., .6f, .4f); |
| + |
| + host_->PushPropertiesTo(host_impl_); |
| + |
| + EXPECT_FALSE(client_.IsPropertyMutated(layer_id_, true, Animation::OPACITY)); |
| + EXPECT_FALSE( |
| + client_.IsPropertyMutated(layer_id_, true, Animation::TRANSFORM)); |
| + EXPECT_FALSE(client_.IsPropertyMutated(layer_id_, true, Animation::FILTER)); |
| + |
| + EXPECT_FALSE( |
| + client_impl_.IsPropertyMutated(layer_id_, true, Animation::OPACITY)); |
| + EXPECT_FALSE( |
| + client_impl_.IsPropertyMutated(layer_id_, true, Animation::TRANSFORM)); |
| + EXPECT_FALSE( |
| + client_impl_.IsPropertyMutated(layer_id_, true, Animation::FILTER)); |
| + |
| + const base::TimeTicks sec = |
| + base::TimeTicks::FromInternalValue(base::Time::kMicrosecondsPerSecond); |
| + |
| + host_->animation_registrar()->AnimateLayers(sec); |
| + host_impl_->animation_registrar()->ActivateAnimations(); |
| + host_impl_->animation_registrar()->AnimateLayers(sec); |
| + |
| + EXPECT_TRUE(client_.IsPropertyMutated(layer_id_, true, Animation::OPACITY)); |
| + EXPECT_TRUE(client_.IsPropertyMutated(layer_id_, true, Animation::TRANSFORM)); |
| + EXPECT_TRUE(client_.IsPropertyMutated(layer_id_, true, Animation::FILTER)); |
| + |
| + EXPECT_TRUE( |
| + client_impl_.IsPropertyMutated(layer_id_, true, Animation::OPACITY)); |
| + EXPECT_TRUE( |
| + client_impl_.IsPropertyMutated(layer_id_, true, Animation::TRANSFORM)); |
| + EXPECT_TRUE( |
| + client_impl_.IsPropertyMutated(layer_id_, true, Animation::FILTER)); |
| + |
| + EXPECT_TRUE( |
| + client_impl_.IsPropertyMutated(layer_id_, false, Animation::OPACITY)); |
| + EXPECT_TRUE( |
| + client_impl_.IsPropertyMutated(layer_id_, false, Animation::TRANSFORM)); |
| + EXPECT_TRUE( |
| + client_impl_.IsPropertyMutated(layer_id_, false, Animation::FILTER)); |
|
Ian Vollick
2015/05/05 03:52:07
Should we check that they get mutated to the right
loyso (OOO)
2015/05/05 06:51:18
That's a matter of balance :) Yes, we can.
loyso (OOO)
2015/06/22 07:48:51
Done.
|
| +} |
| + |
| +TEST_F(AnimationPlayerTest, LayerAnimationDelegate) { |
| + AnimationDelegateTest delegate; |
| + |
| + client_.RegisterLayer(layer_id_, true); |
| + host_->AddAnimationTimeline(timeline_.get()); |
| + timeline_->AttachPlayer(player_.get()); |
| + |
| + // Set delegate before controller created. |
| + player_->set_layer_animation_delegate(&delegate); |
| + player_->AttachLayer(layer_id_); |
| + |
| + AddOpacityTransitionToPlayer(player_.get(), 1., .7f, .3f, false); |
| + |
| + EXPECT_FALSE(delegate.started_); |
| + host_->animation_registrar()->AnimateLayers( |
| + base::TimeTicks::FromInternalValue(base::Time::kMicrosecondsPerSecond)); |
| + |
| + scoped_ptr<AnimationEventsVector> events = |
| + host_->animation_registrar()->CreateEvents(); |
| + host_->animation_registrar()->UpdateAnimationState(true, events.get()); |
| + host_->animation_registrar()->SetAnimationEvents(events.Pass()); |
| + |
| + EXPECT_TRUE(delegate.started_); |
|
Ian Vollick
2015/05/05 03:52:06
Do we need to test started _and_ finished?
loyso (OOO)
2015/05/05 06:51:18
This test is about the delegate accumulation/buffe
Ian Vollick
2015/05/07 13:58:43
I'm not sure how I understand how this is testing
loyso (OOO)
2015/05/08 01:04:35
Acknowledged.
loyso (OOO)
2015/06/22 07:48:51
Done.
|
| +} |
| + |
| +TEST_F(AnimationPlayerTest, AddRemoveAnimationCausesSetNeedsCommit) { |
| + client_.RegisterLayer(layer_id_, true); |
| + host_->AddAnimationTimeline(timeline_.get()); |
| + timeline_->AttachPlayer(player_.get()); |
| + player_->AttachLayer(layer_id_); |
| + |
| + EXPECT_FALSE(client_.mutators_need_commit()); |
| + |
| + const int animation_id = |
| + AddOpacityTransitionToPlayer(player_.get(), 1., .7f, .3f, false); |
| + |
| + EXPECT_TRUE(client_.mutators_need_commit()); |
| + client_.set_mutators_need_commit(false); |
| + |
| + player_->PauseAnimation(animation_id, 1.); |
| + EXPECT_TRUE(client_.mutators_need_commit()); |
| + client_.set_mutators_need_commit(false); |
| + |
| + player_->RemoveAnimation(animation_id); |
| + EXPECT_TRUE(client_.mutators_need_commit()); |
| + client_.set_mutators_need_commit(false); |
| +} |
| + |
| +} // namespace |
| +} // namespace cc |