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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/animation/animation_container.h"
#include <memory>
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/animation/animation_container_observer.h"
#include "ui/gfx/animation/animation_test_api.h"
#include "ui/gfx/animation/linear_animation.h"
#include "ui/gfx/animation/test_animation_delegate.h"
namespace gfx {
namespace {
class FakeAnimationContainerObserver : public AnimationContainerObserver {
public:
FakeAnimationContainerObserver()
: progressed_count_(0),
empty_(false) {
}
FakeAnimationContainerObserver(const FakeAnimationContainerObserver&) =
delete;
FakeAnimationContainerObserver& operator=(
const FakeAnimationContainerObserver&) = delete;
int progressed_count() const { return progressed_count_; }
bool empty() const { return empty_; }
private:
void AnimationContainerProgressed(AnimationContainer* container) override {
progressed_count_++;
}
// Invoked when no more animations are being managed by this container.
void AnimationContainerEmpty(AnimationContainer* container) override {
empty_ = true;
}
void AnimationContainerShuttingDown(AnimationContainer* container) override {}
int progressed_count_;
bool empty_;
};
class TestAnimation : public LinearAnimation {
public:
explicit TestAnimation(AnimationDelegate* delegate)
: LinearAnimation(base::Milliseconds(20), 20, delegate) {}
TestAnimation(const TestAnimation&) = delete;
TestAnimation& operator=(const TestAnimation&) = delete;
void AnimateToState(double state) override {}
using LinearAnimation::duration;
};
} // namespace
class AnimationContainerTest: public testing::Test {
protected:
AnimationContainerTest()
: task_environment_(
base::test::SingleThreadTaskEnvironment::MainThreadType::UI) {}
private:
base::test::SingleThreadTaskEnvironment task_environment_;
};
// Makes sure the animation ups the ref count of the container and releases it
// appropriately.
TEST_F(AnimationContainerTest, Ownership) {
TestAnimationDelegate delegate;
scoped_refptr<AnimationContainer> container(new AnimationContainer());
std::unique_ptr<Animation> animation(new TestAnimation(&delegate));
animation->SetContainer(container.get());
// Setting the container should up the ref count.
EXPECT_FALSE(container->HasOneRef());
animation.reset();
// Releasing the animation should decrement the ref count.
EXPECT_TRUE(container->HasOneRef());
}
// Makes sure multiple animations are managed correctly.
TEST_F(AnimationContainerTest, Multi) {
base::RunLoop loop1;
TestAnimationDelegate delegate1;
TestAnimationDelegate delegate2;
delegate1.set_quit_closure(loop1.QuitWhenIdleClosure());
delegate2.set_quit_closure(loop1.QuitWhenIdleClosure());
scoped_refptr<AnimationContainer> container(new AnimationContainer());
TestAnimation animation1(&delegate1);
TestAnimation animation2(&delegate2);
animation1.SetContainer(container.get());
animation2.SetContainer(container.get());
// Start both animations.
animation1.Start();
EXPECT_TRUE(container->is_running());
animation2.Start();
EXPECT_TRUE(container->is_running());
// Run the message loop the delegate quits the message loop when notified.
loop1.Run();
// Both timers should have finished.
EXPECT_TRUE(delegate1.finished());
EXPECT_TRUE(delegate2.finished());
// And the container should no longer be runnings.
EXPECT_FALSE(container->is_running());
}
// Makes sure observer is notified appropriately.
TEST_F(AnimationContainerTest, Observer) {
base::RunLoop loop;
FakeAnimationContainerObserver observer;
TestAnimationDelegate delegate1;
delegate1.set_quit_closure(loop.QuitWhenIdleClosure());
scoped_refptr<AnimationContainer> container(new AnimationContainer());
container->set_observer(&observer);
TestAnimation animation1(&delegate1);
animation1.SetContainer(container.get());
// Start the animation.
animation1.Start();
EXPECT_TRUE(container->is_running());
// Run the message loop. The delegate quits the message loop when notified.
loop.Run();
EXPECT_EQ(1, observer.progressed_count());
// The timer should have finished.
EXPECT_TRUE(delegate1.finished());
EXPECT_TRUE(observer.empty());
// And the container should no longer be running.
EXPECT_FALSE(container->is_running());
container->set_observer(NULL);
}
// Tests that calling SetAnimationRunner() keeps running animations at their
// current point.
TEST_F(AnimationContainerTest, AnimationsRunAcrossRunnerChange) {
TestAnimationDelegate delegate;
auto container = base::MakeRefCounted<AnimationContainer>();
AnimationContainerTestApi test_api(container.get());
TestAnimation animation(&delegate);
animation.SetContainer(container.get());
animation.Start();
test_api.IncrementTime(animation.duration() / 2);
EXPECT_FALSE(delegate.finished());
container->SetAnimationRunner(nullptr);
AnimationRunner* runner = container->animation_runner_for_testing();
ASSERT_TRUE(runner);
ASSERT_FALSE(runner->step_is_null_for_testing());
EXPECT_FALSE(delegate.finished());
test_api.IncrementTime(animation.duration() / 2);
EXPECT_TRUE(delegate.finished());
}
} // namespace gfx
|