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. 4 5 #include "base/memory/scoped_ptr.h" 6 #include "testing/gmock/include/gmock/gmock.h" 7 #include "testing/gtest/include/gtest/gtest.h" 8 #include "ui/base/animation/animation_container.h" 9 #include "ui/base/animation/animation_container_observer.h" 10 #include "ui/base/animation/linear_animation.h" 11 #include "ui/base/animation/test_animation_delegate.h" 12 13 using testing::AtLeast; 14 15 namespace ui { 16 17 namespace { 18 19 class MockObserver : public AnimationContainerObserver { 20 public: 21 MockObserver() {} 22 23 MOCK_METHOD1(AnimationContainerProgressed, void(AnimationContainer*)); 24 MOCK_METHOD1(AnimationContainerEmpty, void(AnimationContainer*)); 25 26 private: 27 DISALLOW_COPY_AND_ASSIGN(MockObserver); 28 }; 29 30 class TestAnimation : public LinearAnimation { 31 public: 32 explicit TestAnimation(AnimationDelegate* delegate) 33 : LinearAnimation(20, 20, delegate) { 34 } 35 36 virtual void AnimateToState(double state) OVERRIDE { 37 } 38 39 private: 40 DISALLOW_COPY_AND_ASSIGN(TestAnimation); 41 }; 42 43 } // namespace 44 45 class AnimationContainerTest: public testing::Test { 46 private: 47 base::MessageLoopForUI message_loop_; 48 }; 49 50 // Makes sure the animation ups the ref count of the container and releases it 51 // appropriately. 52 TEST_F(AnimationContainerTest, Ownership) { 53 TestAnimationDelegate delegate; 54 scoped_refptr<AnimationContainer> container(new AnimationContainer()); 55 scoped_ptr<Animation> animation(new TestAnimation(&delegate)); 56 animation->SetContainer(container.get()); 57 // Setting the container should up the ref count. 58 EXPECT_FALSE(container->HasOneRef()); 59 60 animation.reset(); 61 62 // Releasing the animation should decrement the ref count. 63 EXPECT_TRUE(container->HasOneRef()); 64 } 65 66 // Makes sure multiple animations are managed correctly. 67 TEST_F(AnimationContainerTest, Multi) { 68 TestAnimationDelegate delegate1; 69 TestAnimationDelegate delegate2; 70 71 scoped_refptr<AnimationContainer> container(new AnimationContainer()); 72 TestAnimation animation1(&delegate1); 73 TestAnimation animation2(&delegate2); 74 animation1.SetContainer(container.get()); 75 animation2.SetContainer(container.get()); 76 77 // Start both animations. 78 animation1.Start(); 79 EXPECT_TRUE(container->is_running()); 80 animation2.Start(); 81 EXPECT_TRUE(container->is_running()); 82 83 // Run the message loop the delegate quits the message loop when notified. 84 base::MessageLoop::current()->Run(); 85 86 // Both timers should have finished. 87 EXPECT_TRUE(delegate1.finished()); 88 EXPECT_TRUE(delegate2.finished()); 89 90 // And the container should no longer be runnings. 91 EXPECT_FALSE(container->is_running()); 92 } 93 94 // Makes sure observer is notified appropriately. 95 TEST_F(AnimationContainerTest, Observer) { 96 MockObserver observer; 97 TestAnimationDelegate delegate1; 98 99 scoped_refptr<AnimationContainer> container(new AnimationContainer()); 100 container->set_observer(&observer); 101 TestAnimation animation1(&delegate1); 102 animation1.SetContainer(container.get()); 103 104 // We expect to get these two calls: the animation progressed, and then when 105 // the animation completed the container went empty. 106 EXPECT_CALL(observer, AnimationContainerProgressed(container.get())).Times( 107 AtLeast(1)); 108 EXPECT_CALL(observer, AnimationContainerEmpty(container.get())).Times(1); 109 110 // Start the animation. 111 animation1.Start(); 112 EXPECT_TRUE(container->is_running()); 113 114 // Run the message loop. The delegate quits the message loop when notified. 115 base::MessageLoop::current()->Run(); 116 117 // The timer should have finished. 118 EXPECT_TRUE(delegate1.finished()); 119 120 // And the container should no longer be running. 121 EXPECT_FALSE(container->is_running()); 122 123 container->set_observer(NULL); 124 } 125 126 } // namespace ui 127