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