1 // Copyright (c) 2012 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/slide_animation.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/time/time.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/animation/test_animation_delegate.h"
14 // Class to provide access to SlideAnimation internals for testing.
15 class SlideAnimation::TestApi
{
17 explicit TestApi(SlideAnimation
* animation
) : animation_(animation
) {}
19 void SetStartTime(base::TimeTicks ticks
) {
20 animation_
->SetStartTime(ticks
);
23 void Step(base::TimeTicks ticks
) {
24 animation_
->Step(ticks
);
28 SlideAnimation
* animation_
;
30 DISALLOW_COPY_AND_ASSIGN(TestApi
);
33 ////////////////////////////////////////////////////////////////////////////////
35 class SlideAnimationTest
: public testing::Test
{
37 base::MessageLoopForUI message_loop_
;
40 // Tests animation construction.
41 TEST_F(SlideAnimationTest
, InitialState
) {
42 SlideAnimation
animation(NULL
);
43 // By default, slide animations are 60 Hz, so the timer interval should be
44 // 1/60th of a second.
45 EXPECT_EQ(1000 / 60, animation
.timer_interval().InMilliseconds());
46 // Duration defaults to 120 ms.
47 EXPECT_EQ(120, animation
.GetSlideDuration());
48 // Slide is neither showing nor closing.
49 EXPECT_FALSE(animation
.IsShowing());
50 EXPECT_FALSE(animation
.IsClosing());
52 EXPECT_EQ(0.0, animation
.GetCurrentValue());
55 TEST_F(SlideAnimationTest
, Basics
) {
56 SlideAnimation
animation(NULL
);
57 SlideAnimation::TestApi
test_api(&animation
);
59 // Use linear tweening to make the math easier below.
60 animation
.SetTweenType(Tween::LINEAR
);
62 // Duration can be set after construction.
63 animation
.SetSlideDuration(100);
64 EXPECT_EQ(100, animation
.GetSlideDuration());
66 // Show toggles the appropriate state.
68 EXPECT_TRUE(animation
.IsShowing());
69 EXPECT_FALSE(animation
.IsClosing());
71 // Simulate running the animation.
72 test_api
.SetStartTime(base::TimeTicks());
73 test_api
.Step(base::TimeTicks() + base::TimeDelta::FromMilliseconds(50));
74 EXPECT_EQ(0.5, animation
.GetCurrentValue());
76 // We can start hiding mid-way through the animation.
78 EXPECT_FALSE(animation
.IsShowing());
79 EXPECT_TRUE(animation
.IsClosing());
81 // Reset stops the animation.
83 EXPECT_EQ(0.0, animation
.GetCurrentValue());
84 EXPECT_FALSE(animation
.IsShowing());
85 EXPECT_FALSE(animation
.IsClosing());
88 // Tests that delegate is not notified when animation is running and is deleted.
89 // (Such a scenario would cause problems for BoundsAnimator).
90 TEST_F(SlideAnimationTest
, DontNotifyOnDelete
) {
91 TestAnimationDelegate delegate
;
92 scoped_ptr
<SlideAnimation
> animation(new SlideAnimation(&delegate
));
94 // Start the animation.
97 // Delete the animation.
100 // Make sure the delegate wasn't notified.
101 EXPECT_FALSE(delegate
.finished());
102 EXPECT_FALSE(delegate
.canceled());