OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/animation/animation_container.h" | 5 #include "ui/gfx/animation/animation_container.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "testing/gmock/include/gmock/gmock.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "ui/gfx/animation/animation_container_observer.h" | 9 #include "ui/gfx/animation/animation_container_observer.h" |
11 #include "ui/gfx/animation/linear_animation.h" | 10 #include "ui/gfx/animation/linear_animation.h" |
12 #include "ui/gfx/animation/test_animation_delegate.h" | 11 #include "ui/gfx/animation/test_animation_delegate.h" |
13 | 12 |
14 using testing::AtLeast; | |
15 | |
16 namespace gfx { | 13 namespace gfx { |
17 | 14 |
18 namespace { | 15 namespace { |
19 | 16 |
20 class MockObserver : public AnimationContainerObserver { | 17 class FakeAnimationContainerObserver : public AnimationContainerObserver { |
21 public: | 18 public: |
22 MockObserver() {} | 19 FakeAnimationContainerObserver() |
| 20 : progressed_count_(0), |
| 21 empty_(false) { |
| 22 } |
23 | 23 |
24 MOCK_METHOD1(AnimationContainerProgressed, void(AnimationContainer*)); | 24 int progressed_count() const { return progressed_count_; } |
25 MOCK_METHOD1(AnimationContainerEmpty, void(AnimationContainer*)); | 25 bool empty() const { return empty_; } |
26 | 26 |
27 private: | 27 private: |
28 DISALLOW_COPY_AND_ASSIGN(MockObserver); | 28 virtual void AnimationContainerProgressed( |
| 29 AnimationContainer* container) OVERRIDE { |
| 30 progressed_count_++; |
| 31 } |
| 32 |
| 33 // Invoked when no more animations are being managed by this container. |
| 34 virtual void AnimationContainerEmpty(AnimationContainer* container) OVERRIDE { |
| 35 empty_ = true; |
| 36 } |
| 37 |
| 38 int progressed_count_; |
| 39 bool empty_; |
| 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(FakeAnimationContainerObserver); |
29 }; | 42 }; |
30 | 43 |
31 class TestAnimation : public LinearAnimation { | 44 class TestAnimation : public LinearAnimation { |
32 public: | 45 public: |
33 explicit TestAnimation(AnimationDelegate* delegate) | 46 explicit TestAnimation(AnimationDelegate* delegate) |
34 : LinearAnimation(20, 20, delegate) { | 47 : LinearAnimation(20, 20, delegate) { |
35 } | 48 } |
36 | 49 |
37 virtual void AnimateToState(double state) OVERRIDE { | 50 virtual void AnimateToState(double state) OVERRIDE { |
38 } | 51 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // Both timers should have finished. | 100 // Both timers should have finished. |
88 EXPECT_TRUE(delegate1.finished()); | 101 EXPECT_TRUE(delegate1.finished()); |
89 EXPECT_TRUE(delegate2.finished()); | 102 EXPECT_TRUE(delegate2.finished()); |
90 | 103 |
91 // And the container should no longer be runnings. | 104 // And the container should no longer be runnings. |
92 EXPECT_FALSE(container->is_running()); | 105 EXPECT_FALSE(container->is_running()); |
93 } | 106 } |
94 | 107 |
95 // Makes sure observer is notified appropriately. | 108 // Makes sure observer is notified appropriately. |
96 TEST_F(AnimationContainerTest, Observer) { | 109 TEST_F(AnimationContainerTest, Observer) { |
97 MockObserver observer; | 110 FakeAnimationContainerObserver observer; |
98 TestAnimationDelegate delegate1; | 111 TestAnimationDelegate delegate1; |
99 | 112 |
100 scoped_refptr<AnimationContainer> container(new AnimationContainer()); | 113 scoped_refptr<AnimationContainer> container(new AnimationContainer()); |
101 container->set_observer(&observer); | 114 container->set_observer(&observer); |
102 TestAnimation animation1(&delegate1); | 115 TestAnimation animation1(&delegate1); |
103 animation1.SetContainer(container.get()); | 116 animation1.SetContainer(container.get()); |
104 | 117 |
105 // We expect to get these two calls: the animation progressed, and then when | |
106 // the animation completed the container went empty. | |
107 EXPECT_CALL(observer, AnimationContainerProgressed(container.get())).Times( | |
108 AtLeast(1)); | |
109 EXPECT_CALL(observer, AnimationContainerEmpty(container.get())).Times(1); | |
110 | |
111 // Start the animation. | 118 // Start the animation. |
112 animation1.Start(); | 119 animation1.Start(); |
113 EXPECT_TRUE(container->is_running()); | 120 EXPECT_TRUE(container->is_running()); |
114 | 121 |
115 // Run the message loop. The delegate quits the message loop when notified. | 122 // Run the message loop. The delegate quits the message loop when notified. |
116 base::MessageLoop::current()->Run(); | 123 base::MessageLoop::current()->Run(); |
117 | 124 |
| 125 EXPECT_EQ(1, observer.progressed_count()); |
| 126 |
118 // The timer should have finished. | 127 // The timer should have finished. |
119 EXPECT_TRUE(delegate1.finished()); | 128 EXPECT_TRUE(delegate1.finished()); |
120 | 129 |
| 130 EXPECT_TRUE(observer.empty()); |
| 131 |
121 // And the container should no longer be running. | 132 // And the container should no longer be running. |
122 EXPECT_FALSE(container->is_running()); | 133 EXPECT_FALSE(container->is_running()); |
123 | 134 |
124 container->set_observer(NULL); | 135 container->set_observer(NULL); |
125 } | 136 } |
126 | 137 |
127 } // namespace gfx | 138 } // namespace gfx |
OLD | NEW |