This source file includes following definitions.
- CreateAnimation
- GetAndClearDeleted
- GetAndClearCanceled
- AnimationCanceled
- SchedulePaintInRect
- dirty_rect
- animator_
- parent
- child
- animator
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "ui/views/animation/bounds_animator.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/animation/slide_animation.h"
#include "ui/gfx/animation/test_animation_delegate.h"
#include "ui/views/view.h"
using gfx::Animation;
using gfx::SlideAnimation;
using gfx::TestAnimationDelegate;
namespace views {
namespace {
class TestBoundsAnimator : public BoundsAnimator {
public:
explicit TestBoundsAnimator(View* view) : BoundsAnimator(view) {
}
protected:
virtual SlideAnimation* CreateAnimation() OVERRIDE {
SlideAnimation* animation = BoundsAnimator::CreateAnimation();
animation->SetSlideDuration(10);
return animation;
}
private:
DISALLOW_COPY_AND_ASSIGN(TestBoundsAnimator);
};
class OwnedDelegate : public BoundsAnimator::OwnedAnimationDelegate {
public:
OwnedDelegate() {}
virtual ~OwnedDelegate() {
deleted_ = true;
}
static bool GetAndClearDeleted() {
bool value = deleted_;
deleted_ = false;
return value;
}
static bool GetAndClearCanceled() {
bool value = canceled_;
canceled_ = false;
return value;
}
virtual void AnimationCanceled(const Animation* animation) OVERRIDE {
canceled_ = true;
}
private:
static bool deleted_;
static bool canceled_;
DISALLOW_COPY_AND_ASSIGN(OwnedDelegate);
};
bool OwnedDelegate::deleted_ = false;
bool OwnedDelegate::canceled_ = false;
class TestView : public View {
public:
TestView() {}
virtual void SchedulePaintInRect(const gfx::Rect& r) OVERRIDE {
if (dirty_rect_.IsEmpty())
dirty_rect_ = r;
else
dirty_rect_.Union(r);
}
const gfx::Rect& dirty_rect() const { return dirty_rect_; }
private:
gfx::Rect dirty_rect_;
DISALLOW_COPY_AND_ASSIGN(TestView);
};
}
class BoundsAnimatorTest : public testing::Test {
public:
BoundsAnimatorTest() : child_(new TestView()), animator_(&parent_) {
parent_.AddChildView(child_);
}
TestView* parent() { return &parent_; }
TestView* child() { return child_; }
TestBoundsAnimator* animator() { return &animator_; }
private:
base::MessageLoopForUI message_loop_;
TestView parent_;
TestView* child_;
TestBoundsAnimator animator_;
DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorTest);
};
TEST_F(BoundsAnimatorTest, AnimateViewTo) {
TestAnimationDelegate delegate;
gfx::Rect initial_bounds(0, 0, 10, 10);
child()->SetBoundsRect(initial_bounds);
gfx::Rect target_bounds(10, 10, 20, 20);
animator()->AnimateViewTo(child(), target_bounds);
animator()->SetAnimationDelegate(child(), &delegate, false);
EXPECT_TRUE(animator()->IsAnimating());
base::MessageLoop::current()->Run();
EXPECT_EQ(target_bounds, child()->bounds());
EXPECT_EQ(gfx::UnionRects(target_bounds, initial_bounds),
parent()->dirty_rect());
}
TEST_F(BoundsAnimatorTest, DeleteDelegateOnCancel) {
animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
animator()->SetAnimationDelegate(child(), new OwnedDelegate(), true);
animator()->Cancel();
EXPECT_FALSE(animator()->IsAnimating());
EXPECT_TRUE(OwnedDelegate::GetAndClearCanceled());
EXPECT_TRUE(OwnedDelegate::GetAndClearDeleted());
}
TEST_F(BoundsAnimatorTest, DeleteDelegateOnNewAnimate) {
animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
animator()->SetAnimationDelegate(child(), new OwnedDelegate(), true);
animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
EXPECT_TRUE(OwnedDelegate::GetAndClearDeleted());
EXPECT_TRUE(OwnedDelegate::GetAndClearCanceled());
}
TEST_F(BoundsAnimatorTest, StopAnimating) {
scoped_ptr<OwnedDelegate> delegate(new OwnedDelegate());
animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
animator()->SetAnimationDelegate(child(), new OwnedDelegate(), true);
animator()->StopAnimatingView(child());
EXPECT_FALSE(animator()->IsAnimating());
EXPECT_TRUE(OwnedDelegate::GetAndClearDeleted());
EXPECT_TRUE(OwnedDelegate::GetAndClearCanceled());
}
}