1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/animation/compositor_animation.h"
#include <memory>
#include "base/time/time.h"
#include "cc/animation/animation_id_provider.h"
#include "cc/animation/animation_timeline.h"
#include "third_party/blink/renderer/platform/animation/compositor_animation_client.h"
#include "third_party/blink/renderer/platform/animation/compositor_animation_delegate.h"
#include "third_party/blink/renderer/platform/testing/compositor_test.h"
namespace blink {
class CompositorAnimationDelegateForTesting
: public CompositorAnimationDelegate {
public:
CompositorAnimationDelegateForTesting() { ResetFlags(); }
void ResetFlags() {
started_ = false;
finished_ = false;
aborted_ = false;
}
void NotifyAnimationStarted(base::TimeDelta, int) override {
started_ = true;
}
void NotifyAnimationFinished(base::TimeDelta, int) override {
finished_ = true;
}
void NotifyAnimationAborted(base::TimeDelta, int) override {
aborted_ = true;
}
bool started_;
bool finished_;
bool aborted_;
};
class CompositorAnimationTestClient : public CompositorAnimationClient {
public:
CompositorAnimationTestClient() : animation_(CompositorAnimation::Create()) {}
CompositorAnimation* GetCompositorAnimation() const override {
return animation_.get();
}
std::unique_ptr<CompositorAnimation> animation_;
};
class CompositorAnimationTest : public CompositorTest {};
// Test that when the animation delegate is null, the animation animation
// doesn't forward the finish notification.
TEST_F(CompositorAnimationTest, NullDelegate) {
std::unique_ptr<CompositorAnimationDelegateForTesting> delegate(
new CompositorAnimationDelegateForTesting);
auto timeline =
cc::AnimationTimeline::Create(cc::AnimationIdProvider::NextTimelineId());
std::unique_ptr<CompositorAnimationTestClient> client(
new CompositorAnimationTestClient);
CompositorAnimation* animation = client->GetCompositorAnimation();
cc::Animation* cc_animation = animation->CcAnimation();
timeline->AttachAnimation(cc_animation);
int timeline_id = cc_animation->animation_timeline()->id();
auto curve = gfx::KeyframedFloatAnimationCurve::Create();
auto keyframe_model = cc::KeyframeModel::Create(
std::move(curve), cc::AnimationIdProvider::NextKeyframeModelId(), 1,
cc::KeyframeModel::TargetPropertyId(cc::TargetProperty::TRANSFORM));
int keyframe_model_id = keyframe_model->id();
animation->AddKeyframeModel(std::move(keyframe_model));
animation->SetAnimationDelegate(delegate.get());
EXPECT_FALSE(delegate->finished_);
cc_animation->NotifyKeyframeModelFinishedForTesting(
timeline_id, keyframe_model_id, cc::TargetProperty::TRANSFORM, 1);
EXPECT_TRUE(delegate->finished_);
delegate->ResetFlags();
animation->SetAnimationDelegate(nullptr);
cc_animation->NotifyKeyframeModelFinishedForTesting(
timeline_id, keyframe_model_id, cc::TargetProperty::TRANSFORM, 1);
EXPECT_FALSE(delegate->finished_);
}
TEST_F(CompositorAnimationTest, NotifyFromCCAfterCompositorAnimationDeletion) {
std::unique_ptr<CompositorAnimationDelegateForTesting> delegate(
new CompositorAnimationDelegateForTesting);
auto timeline =
cc::AnimationTimeline::Create(cc::AnimationIdProvider::NextTimelineId());
std::unique_ptr<CompositorAnimationTestClient> client(
new CompositorAnimationTestClient);
CompositorAnimation* animation = client->GetCompositorAnimation();
scoped_refptr<cc::Animation> cc_animation = animation->CcAnimation();
timeline->AttachAnimation(cc_animation);
int timeline_id = cc_animation->animation_timeline()->id();
auto curve = gfx::KeyframedFloatAnimationCurve::Create();
auto keyframe_model = cc::KeyframeModel::Create(
std::move(curve), cc::AnimationIdProvider::NextKeyframeModelId(), 1,
cc::KeyframeModel::TargetPropertyId(cc::TargetProperty::OPACITY));
int keyframe_model_id = keyframe_model->id();
animation->AddKeyframeModel(std::move(keyframe_model));
animation->SetAnimationDelegate(delegate.get());
EXPECT_FALSE(delegate->finished_);
cc_animation->NotifyKeyframeModelFinishedForTesting(
timeline_id, keyframe_model_id, cc::TargetProperty::OPACITY, 1);
EXPECT_TRUE(delegate->finished_);
delegate->finished_ = false;
// Delete CompositorAnimation. ccAnimation stays alive.
client = nullptr;
// No notifications. Doesn't crash.
cc_animation->NotifyKeyframeModelFinishedForTesting(
timeline_id, keyframe_model_id, cc::TargetProperty::OPACITY, 1);
EXPECT_FALSE(delegate->finished_);
}
TEST_F(CompositorAnimationTest,
CompositorAnimationDeletionDetachesFromCCTimeline) {
auto timeline =
cc::AnimationTimeline::Create(cc::AnimationIdProvider::NextTimelineId());
std::unique_ptr<CompositorAnimationTestClient> client(
new CompositorAnimationTestClient);
scoped_refptr<cc::Animation> cc_animation = client->animation_->CcAnimation();
EXPECT_FALSE(cc_animation->animation_timeline());
timeline->AttachAnimation(cc_animation);
EXPECT_TRUE(cc_animation->animation_timeline());
EXPECT_TRUE(timeline->GetAnimationById(cc_animation->id()));
// Delete client and CompositorAnimation while attached to timeline.
client = nullptr;
EXPECT_FALSE(cc_animation->animation_timeline());
EXPECT_FALSE(timeline->GetAnimationById(cc_animation->id()));
}
} // namespace blink
|