1 // Copyright (c) 2011 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.
5 #include "ui/gfx/animation/animation_container.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/animation/animation_container_observer.h"
10 #include "ui/gfx/animation/linear_animation.h"
11 #include "ui/gfx/animation/test_animation_delegate.h"
17 class FakeAnimationContainerObserver
: public AnimationContainerObserver
{
19 FakeAnimationContainerObserver()
20 : progressed_count_(0),
24 int progressed_count() const { return progressed_count_
; }
25 bool empty() const { return empty_
; }
28 void AnimationContainerProgressed(AnimationContainer
* container
) override
{
32 // Invoked when no more animations are being managed by this container.
33 void AnimationContainerEmpty(AnimationContainer
* container
) override
{
37 int progressed_count_
;
40 DISALLOW_COPY_AND_ASSIGN(FakeAnimationContainerObserver
);
43 class TestAnimation
: public LinearAnimation
{
45 explicit TestAnimation(AnimationDelegate
* delegate
)
46 : LinearAnimation(20, 20, delegate
) {
49 void AnimateToState(double state
) override
{}
52 DISALLOW_COPY_AND_ASSIGN(TestAnimation
);
57 class AnimationContainerTest
: public testing::Test
{
59 base::MessageLoopForUI message_loop_
;
62 // Makes sure the animation ups the ref count of the container and releases it
64 TEST_F(AnimationContainerTest
, Ownership
) {
65 TestAnimationDelegate delegate
;
66 scoped_refptr
<AnimationContainer
> container(new AnimationContainer());
67 scoped_ptr
<Animation
> animation(new TestAnimation(&delegate
));
68 animation
->SetContainer(container
.get());
69 // Setting the container should up the ref count.
70 EXPECT_FALSE(container
->HasOneRef());
74 // Releasing the animation should decrement the ref count.
75 EXPECT_TRUE(container
->HasOneRef());
78 // Makes sure multiple animations are managed correctly.
79 TEST_F(AnimationContainerTest
, Multi
) {
80 TestAnimationDelegate delegate1
;
81 TestAnimationDelegate delegate2
;
83 scoped_refptr
<AnimationContainer
> container(new AnimationContainer());
84 TestAnimation
animation1(&delegate1
);
85 TestAnimation
animation2(&delegate2
);
86 animation1
.SetContainer(container
.get());
87 animation2
.SetContainer(container
.get());
89 // Start both animations.
91 EXPECT_TRUE(container
->is_running());
93 EXPECT_TRUE(container
->is_running());
95 // Run the message loop the delegate quits the message loop when notified.
96 base::MessageLoop::current()->Run();
98 // Both timers should have finished.
99 EXPECT_TRUE(delegate1
.finished());
100 EXPECT_TRUE(delegate2
.finished());
102 // And the container should no longer be runnings.
103 EXPECT_FALSE(container
->is_running());
106 // Makes sure observer is notified appropriately.
107 TEST_F(AnimationContainerTest
, Observer
) {
108 FakeAnimationContainerObserver observer
;
109 TestAnimationDelegate delegate1
;
111 scoped_refptr
<AnimationContainer
> container(new AnimationContainer());
112 container
->set_observer(&observer
);
113 TestAnimation
animation1(&delegate1
);
114 animation1
.SetContainer(container
.get());
116 // Start the animation.
118 EXPECT_TRUE(container
->is_running());
120 // Run the message loop. The delegate quits the message loop when notified.
121 base::MessageLoop::current()->Run();
123 EXPECT_EQ(1, observer
.progressed_count());
125 // The timer should have finished.
126 EXPECT_TRUE(delegate1
.finished());
128 EXPECT_TRUE(observer
.empty());
130 // And the container should no longer be running.
131 EXPECT_FALSE(container
->is_running());
133 container
->set_observer(NULL
);