1 // Copyright (c) 2012 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/views/corewm/window_animations.h"
7 #include "base/time/time.h"
8 #include "ui/aura/client/animation_host.h"
9 #include "ui/aura/test/aura_test_base.h"
10 #include "ui/aura/test/test_windows.h"
11 #include "ui/aura/window.h"
12 #include "ui/base/animation/animation_container_element.h"
13 #include "ui/compositor/layer.h"
14 #include "ui/compositor/layer_animator.h"
15 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
23 class WindowAnimationsTest
: public aura::test::AuraTestBase
{
25 WindowAnimationsTest() {}
27 virtual void TearDown() OVERRIDE
{
28 AuraTestBase::TearDown();
32 DISALLOW_COPY_AND_ASSIGN(WindowAnimationsTest
);
35 TEST_F(WindowAnimationsTest
, LayerTargetVisibility
) {
36 scoped_ptr
<aura::Window
> window(
37 aura::test::CreateTestWindowWithId(0, NULL
));
39 // Layer target visibility changes according to Show/Hide.
41 EXPECT_TRUE(window
->layer()->GetTargetVisibility());
43 EXPECT_FALSE(window
->layer()->GetTargetVisibility());
45 EXPECT_TRUE(window
->layer()->GetTargetVisibility());
48 TEST_F(WindowAnimationsTest
, LayerTargetVisibility_AnimateShow
) {
49 // Tests if opacity and transform are reset when only show animation is
50 // enabled. See also LayerTargetVisibility_AnimateHide.
51 // Since the window is not visible after Hide() is called, opacity and
52 // transform shouldn't matter in case of ANIMATE_SHOW, but we reset them
53 // to keep consistency.
55 scoped_ptr
<aura::Window
> window(aura::test::CreateTestWindowWithId(0, NULL
));
56 SetWindowVisibilityAnimationTransition(window
.get(), ANIMATE_SHOW
);
58 // Layer target visibility and opacity change according to Show/Hide.
60 AnimateOnChildWindowVisibilityChanged(window
.get(), true);
61 EXPECT_TRUE(window
->layer()->GetTargetVisibility());
62 EXPECT_EQ(1, window
->layer()->opacity());
65 AnimateOnChildWindowVisibilityChanged(window
.get(), false);
66 EXPECT_FALSE(window
->layer()->GetTargetVisibility());
67 EXPECT_EQ(0, window
->layer()->opacity());
68 EXPECT_EQ(gfx::Transform(), window
->layer()->transform());
71 AnimateOnChildWindowVisibilityChanged(window
.get(), true);
72 EXPECT_TRUE(window
->layer()->GetTargetVisibility());
73 EXPECT_EQ(1, window
->layer()->opacity());
76 TEST_F(WindowAnimationsTest
, LayerTargetVisibility_AnimateHide
) {
77 // Tests if opacity and transform are reset when only hide animation is
78 // enabled. Hide animation changes opacity and transform in addition to
79 // visibility, so we need to reset not only visibility but also opacity
80 // and transform to show the window.
82 scoped_ptr
<aura::Window
> window(aura::test::CreateTestWindowWithId(0, NULL
));
83 SetWindowVisibilityAnimationTransition(window
.get(), ANIMATE_HIDE
);
85 // Layer target visibility and opacity change according to Show/Hide.
87 AnimateOnChildWindowVisibilityChanged(window
.get(), true);
88 EXPECT_TRUE(window
->layer()->GetTargetVisibility());
89 EXPECT_EQ(1, window
->layer()->opacity());
90 EXPECT_EQ(gfx::Transform(), window
->layer()->transform());
93 AnimateOnChildWindowVisibilityChanged(window
.get(), false);
94 EXPECT_FALSE(window
->layer()->GetTargetVisibility());
95 EXPECT_EQ(0, window
->layer()->opacity());
98 AnimateOnChildWindowVisibilityChanged(window
.get(), true);
99 EXPECT_TRUE(window
->layer()->GetTargetVisibility());
100 EXPECT_EQ(1, window
->layer()->opacity());
101 EXPECT_EQ(gfx::Transform(), window
->layer()->transform());
104 // A simple AnimationHost implementation for the NotifyHideCompleted test.
105 class NotifyHideCompletedAnimationHost
: public aura::client::AnimationHost
{
107 NotifyHideCompletedAnimationHost() : hide_completed_(false) {}
108 virtual ~NotifyHideCompletedAnimationHost() {}
110 // Overridden from TestWindowDelegate:
111 virtual void OnWindowHidingAnimationCompleted() OVERRIDE
{
112 hide_completed_
= true;
115 virtual void SetHostTransitionBounds(const gfx::Rect
& bounds
) OVERRIDE
{}
117 bool hide_completed() const { return hide_completed_
; }
120 bool hide_completed_
;
122 DISALLOW_COPY_AND_ASSIGN(NotifyHideCompletedAnimationHost
);
125 TEST_F(WindowAnimationsTest
, NotifyHideCompleted
) {
126 NotifyHideCompletedAnimationHost animation_host
;
127 scoped_ptr
<aura::Window
> window(aura::test::CreateTestWindowWithId(0, NULL
));
128 aura::client::SetAnimationHost(window
.get(), &animation_host
);
129 views::corewm::SetWindowVisibilityAnimationType(
130 window
.get(), WINDOW_VISIBILITY_ANIMATION_TYPE_FADE
);
131 AnimateOnChildWindowVisibilityChanged(window
.get(), true);
132 EXPECT_TRUE(window
->layer()->visible());
134 EXPECT_FALSE(animation_host
.hide_completed());
135 AnimateOnChildWindowVisibilityChanged(window
.get(), false);
136 EXPECT_TRUE(animation_host
.hide_completed());
139 } // namespace corewm