Performance histograms for extension content verification
[chromium-blink-merge.git] / ui / gfx / animation / animation_container_unittest.cc
blob70d3b0e10f8e0721db09fbfec65815689b7006fb
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.
5 #include "ui/gfx/animation/animation_container.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/animation/animation_container_observer.h"
10 #include "ui/gfx/animation/linear_animation.h"
11 #include "ui/gfx/animation/test_animation_delegate.h"
13 namespace gfx {
15 namespace {
17 class FakeAnimationContainerObserver : public AnimationContainerObserver {
18 public:
19 FakeAnimationContainerObserver()
20 : progressed_count_(0),
21 empty_(false) {
24 int progressed_count() const { return progressed_count_; }
25 bool empty() const { return empty_; }
27 private:
28 virtual void AnimationContainerProgressed(
29 AnimationContainer* container) OVERRIDE {
30 progressed_count_++;
33 // Invoked when no more animations are being managed by this container.
34 virtual void AnimationContainerEmpty(AnimationContainer* container) OVERRIDE {
35 empty_ = true;
38 int progressed_count_;
39 bool empty_;
41 DISALLOW_COPY_AND_ASSIGN(FakeAnimationContainerObserver);
44 class TestAnimation : public LinearAnimation {
45 public:
46 explicit TestAnimation(AnimationDelegate* delegate)
47 : LinearAnimation(20, 20, delegate) {
50 virtual void AnimateToState(double state) OVERRIDE {
53 private:
54 DISALLOW_COPY_AND_ASSIGN(TestAnimation);
57 } // namespace
59 class AnimationContainerTest: public testing::Test {
60 private:
61 base::MessageLoopForUI message_loop_;
64 // Makes sure the animation ups the ref count of the container and releases it
65 // appropriately.
66 TEST_F(AnimationContainerTest, Ownership) {
67 TestAnimationDelegate delegate;
68 scoped_refptr<AnimationContainer> container(new AnimationContainer());
69 scoped_ptr<Animation> animation(new TestAnimation(&delegate));
70 animation->SetContainer(container.get());
71 // Setting the container should up the ref count.
72 EXPECT_FALSE(container->HasOneRef());
74 animation.reset();
76 // Releasing the animation should decrement the ref count.
77 EXPECT_TRUE(container->HasOneRef());
80 // Makes sure multiple animations are managed correctly.
81 TEST_F(AnimationContainerTest, Multi) {
82 TestAnimationDelegate delegate1;
83 TestAnimationDelegate delegate2;
85 scoped_refptr<AnimationContainer> container(new AnimationContainer());
86 TestAnimation animation1(&delegate1);
87 TestAnimation animation2(&delegate2);
88 animation1.SetContainer(container.get());
89 animation2.SetContainer(container.get());
91 // Start both animations.
92 animation1.Start();
93 EXPECT_TRUE(container->is_running());
94 animation2.Start();
95 EXPECT_TRUE(container->is_running());
97 // Run the message loop the delegate quits the message loop when notified.
98 base::MessageLoop::current()->Run();
100 // Both timers should have finished.
101 EXPECT_TRUE(delegate1.finished());
102 EXPECT_TRUE(delegate2.finished());
104 // And the container should no longer be runnings.
105 EXPECT_FALSE(container->is_running());
108 // Makes sure observer is notified appropriately.
109 TEST_F(AnimationContainerTest, Observer) {
110 FakeAnimationContainerObserver observer;
111 TestAnimationDelegate delegate1;
113 scoped_refptr<AnimationContainer> container(new AnimationContainer());
114 container->set_observer(&observer);
115 TestAnimation animation1(&delegate1);
116 animation1.SetContainer(container.get());
118 // Start the animation.
119 animation1.Start();
120 EXPECT_TRUE(container->is_running());
122 // Run the message loop. The delegate quits the message loop when notified.
123 base::MessageLoop::current()->Run();
125 EXPECT_EQ(1, observer.progressed_count());
127 // The timer should have finished.
128 EXPECT_TRUE(delegate1.finished());
130 EXPECT_TRUE(observer.empty());
132 // And the container should no longer be running.
133 EXPECT_FALSE(container->is_running());
135 container->set_observer(NULL);
138 } // namespace gfx