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