Home | History | Annotate | Download | only in animation
      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 "ui/gfx/animation/slide_animation.h"
      6 
      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"
     11 
     12 namespace gfx {
     13 
     14 // Class to provide access to SlideAnimation internals for testing.
     15 class SlideAnimation::TestApi {
     16  public:
     17   explicit TestApi(SlideAnimation* animation) : animation_(animation) {}
     18 
     19   void SetStartTime(base::TimeTicks ticks) {
     20     animation_->SetStartTime(ticks);
     21   }
     22 
     23   void Step(base::TimeTicks ticks) {
     24     animation_->Step(ticks);
     25   }
     26 
     27  private:
     28   SlideAnimation* animation_;
     29 
     30   DISALLOW_COPY_AND_ASSIGN(TestApi);
     31 };
     32 
     33 ////////////////////////////////////////////////////////////////////////////////
     34 // SlideAnimationTest
     35 class SlideAnimationTest: public testing::Test {
     36  private:
     37   base::MessageLoopForUI message_loop_;
     38 };
     39 
     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());
     51   // Starts at 0.
     52   EXPECT_EQ(0.0, animation.GetCurrentValue());
     53 }
     54 
     55 TEST_F(SlideAnimationTest, Basics) {
     56   SlideAnimation animation(NULL);
     57   SlideAnimation::TestApi test_api(&animation);
     58 
     59   // Use linear tweening to make the math easier below.
     60   animation.SetTweenType(Tween::LINEAR);
     61 
     62   // Duration can be set after construction.
     63   animation.SetSlideDuration(100);
     64   EXPECT_EQ(100, animation.GetSlideDuration());
     65 
     66   // Show toggles the appropriate state.
     67   animation.Show();
     68   EXPECT_TRUE(animation.IsShowing());
     69   EXPECT_FALSE(animation.IsClosing());
     70 
     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());
     75 
     76   // We can start hiding mid-way through the animation.
     77   animation.Hide();
     78   EXPECT_FALSE(animation.IsShowing());
     79   EXPECT_TRUE(animation.IsClosing());
     80 
     81   // Reset stops the animation.
     82   animation.Reset();
     83   EXPECT_EQ(0.0, animation.GetCurrentValue());
     84   EXPECT_FALSE(animation.IsShowing());
     85   EXPECT_FALSE(animation.IsClosing());
     86 }
     87 
     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));
     93 
     94   // Start the animation.
     95   animation->Show();
     96 
     97   // Delete the animation.
     98   animation.reset();
     99 
    100   // Make sure the delegate wasn't notified.
    101   EXPECT_FALSE(delegate.finished());
    102   EXPECT_FALSE(delegate.canceled());
    103 }
    104 
    105 }  // namespace gfx
    106