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 "ash/wm/window_animations.h"
7 #include "ash/shell_window_ids.h"
8 #include "ash/test/ash_test_base.h"
9 #include "ash/wm/window_state.h"
10 #include "ash/wm/workspace_controller.h"
11 #include "base/time/time.h"
12 #include "ui/aura/test/test_windows.h"
13 #include "ui/aura/window.h"
14 #include "ui/compositor/layer.h"
15 #include "ui/compositor/layer_animation_observer.h"
16 #include "ui/compositor/layer_animator.h"
17 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
18 #include "ui/compositor/scoped_layer_animation_settings.h"
19 #include "ui/gfx/animation/animation_container_element.h"
25 class WindowAnimationsTest
: public ash::test::AshTestBase
{
27 WindowAnimationsTest() {}
29 virtual void TearDown() OVERRIDE
{
30 AshTestBase::TearDown();
34 DISALLOW_COPY_AND_ASSIGN(WindowAnimationsTest
);
37 // Listens to animation scheduled notifications. Remembers the transition
38 // duration of the first sequence.
39 class MinimizeAnimationObserver
: public ui::LayerAnimationObserver
{
41 explicit MinimizeAnimationObserver(ui::LayerAnimator
* animator
)
42 : animator_(animator
) {
43 animator_
->AddObserver(this);
44 // RemoveObserver is called when the first animation is scheduled and so
45 // there should be no need for now to remove it in destructor.
47 base::TimeDelta
duration() { return duration_
; }
50 // ui::LayerAnimationObserver:
51 virtual void OnLayerAnimationScheduled(
52 ui::LayerAnimationSequence
* sequence
) OVERRIDE
{
53 duration_
= animator_
->GetTransitionDuration();
54 animator_
->RemoveObserver(this);
56 virtual void OnLayerAnimationEnded(
57 ui::LayerAnimationSequence
* sequence
) OVERRIDE
{}
58 virtual void OnLayerAnimationAborted(
59 ui::LayerAnimationSequence
* sequence
) OVERRIDE
{}
62 ui::LayerAnimator
* animator_
;
63 base::TimeDelta duration_
;
65 DISALLOW_COPY_AND_ASSIGN(MinimizeAnimationObserver
);
68 TEST_F(WindowAnimationsTest
, HideShowBrightnessGrayscaleAnimation
) {
69 scoped_ptr
<aura::Window
> window(CreateTestWindowInShellWithId(0));
71 EXPECT_TRUE(window
->layer()->visible());
74 views::corewm::SetWindowVisibilityAnimationType(
76 WINDOW_VISIBILITY_ANIMATION_TYPE_BRIGHTNESS_GRAYSCALE
);
77 AnimateOnChildWindowVisibilityChanged(window
.get(), false);
78 EXPECT_EQ(0.0f
, window
->layer()->GetTargetOpacity());
79 EXPECT_FALSE(window
->layer()->GetTargetVisibility());
80 EXPECT_FALSE(window
->layer()->visible());
83 views::corewm::SetWindowVisibilityAnimationType(
85 WINDOW_VISIBILITY_ANIMATION_TYPE_BRIGHTNESS_GRAYSCALE
);
86 AnimateOnChildWindowVisibilityChanged(window
.get(), true);
87 EXPECT_EQ(0.0f
, window
->layer()->GetTargetBrightness());
88 EXPECT_EQ(0.0f
, window
->layer()->GetTargetGrayscale());
89 EXPECT_TRUE(window
->layer()->visible());
92 gfx::AnimationContainerElement
* element
=
93 static_cast<gfx::AnimationContainerElement
*>(
94 window
->layer()->GetAnimator());
95 element
->Step(base::TimeTicks::Now() +
96 base::TimeDelta::FromSeconds(5));
97 EXPECT_EQ(0.0f
, window
->layer()->GetTargetBrightness());
98 EXPECT_EQ(0.0f
, window
->layer()->GetTargetGrayscale());
99 EXPECT_TRUE(window
->layer()->visible());
102 TEST_F(WindowAnimationsTest
, LayerTargetVisibility
) {
103 scoped_ptr
<aura::Window
> window(CreateTestWindowInShellWithId(0));
105 // Layer target visibility changes according to Show/Hide.
107 EXPECT_TRUE(window
->layer()->GetTargetVisibility());
109 EXPECT_FALSE(window
->layer()->GetTargetVisibility());
111 EXPECT_TRUE(window
->layer()->GetTargetVisibility());
116 TEST_F(WindowAnimationsTest
, CrossFadeToBounds
) {
117 ui::ScopedAnimationDurationScaleMode
normal_duration_mode(
118 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
120 scoped_ptr
<Window
> window(CreateTestWindowInShellWithId(0));
121 window
->SetBounds(gfx::Rect(5, 10, 320, 240));
124 Layer
* old_layer
= window
->layer();
125 EXPECT_EQ(1.0f
, old_layer
->GetTargetOpacity());
127 // Cross fade to a larger size, as in a maximize animation.
128 GetWindowState(window
.get())->SetBoundsDirectCrossFade(
129 gfx::Rect(0, 0, 640, 480));
130 // Window's layer has been replaced.
131 EXPECT_NE(old_layer
, window
->layer());
132 // Original layer stays opaque and stretches to new size.
133 EXPECT_EQ(1.0f
, old_layer
->GetTargetOpacity());
134 EXPECT_EQ("5,10 320x240", old_layer
->bounds().ToString());
135 gfx::Transform grow_transform
;
136 grow_transform
.Translate(-5.f
, -10.f
);
137 grow_transform
.Scale(640.f
/ 320.f
, 480.f
/ 240.f
);
138 EXPECT_EQ(grow_transform
, old_layer
->GetTargetTransform());
139 // New layer animates in to the identity transform.
140 EXPECT_EQ(1.0f
, window
->layer()->GetTargetOpacity());
141 EXPECT_EQ(gfx::Transform(), window
->layer()->GetTargetTransform());
143 // Run the animations to completion.
144 static_cast<gfx::AnimationContainerElement
*>(old_layer
->GetAnimator())->Step(
145 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(1));
146 static_cast<gfx::AnimationContainerElement
*>(window
->layer()->GetAnimator())->
147 Step(base::TimeTicks::Now() + base::TimeDelta::FromSeconds(1));
149 // Cross fade to a smaller size, as in a restore animation.
150 old_layer
= window
->layer();
151 GetWindowState(window
.get())->SetBoundsDirectCrossFade(
152 gfx::Rect(5, 10, 320, 240));
153 // Again, window layer has been replaced.
154 EXPECT_NE(old_layer
, window
->layer());
155 // Original layer fades out and stretches down to new size.
156 EXPECT_EQ(0.0f
, old_layer
->GetTargetOpacity());
157 EXPECT_EQ("0,0 640x480", old_layer
->bounds().ToString());
158 gfx::Transform shrink_transform
;
159 shrink_transform
.Translate(5.f
, 10.f
);
160 shrink_transform
.Scale(320.f
/ 640.f
, 240.f
/ 480.f
);
161 EXPECT_EQ(shrink_transform
, old_layer
->GetTargetTransform());
162 // New layer animates in to the identity transform.
163 EXPECT_EQ(1.0f
, window
->layer()->GetTargetOpacity());
164 EXPECT_EQ(gfx::Transform(), window
->layer()->GetTargetTransform());
166 static_cast<gfx::AnimationContainerElement
*>(old_layer
->GetAnimator())->Step(
167 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(1));
168 static_cast<gfx::AnimationContainerElement
*>(window
->layer()->GetAnimator())->
169 Step(base::TimeTicks::Now() + base::TimeDelta::FromSeconds(1));
174 TEST_F(WindowAnimationsTest
, LockAnimationDuration
) {
175 ui::ScopedAnimationDurationScaleMode
normal_duration_mode(
176 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
178 scoped_ptr
<Window
> window(CreateTestWindowInShellWithId(0));
179 Layer
* layer
= window
->layer();
180 window
->SetBounds(gfx::Rect(5, 10, 320, 240));
183 // Test that it is possible to override transition duration when it is not
186 ui::ScopedLayerAnimationSettings
settings1(layer
->GetAnimator());
187 settings1
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(1000));
189 ui::ScopedLayerAnimationSettings
settings2(layer
->GetAnimator());
190 // Duration is not locked so it gets overridden.
191 settings2
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(50));
192 wm::GetWindowState(window
.get())->Minimize();
193 EXPECT_TRUE(layer
->GetAnimator()->is_animating());
194 // Expect duration from the inner scope
196 layer
->GetAnimator()->GetTransitionDuration().InMilliseconds());
199 layer
->GetAnimator()->StopAnimating();
202 // Test that it is possible to lock transition duration
204 ui::ScopedLayerAnimationSettings
settings1(layer
->GetAnimator());
205 settings1
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(1000));
206 // Duration is locked in outer scope.
207 settings1
.LockTransitionDuration();
209 ui::ScopedLayerAnimationSettings
settings2(layer
->GetAnimator());
210 // Transition duration setting is ignored.
211 settings2
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(50));
212 wm::GetWindowState(window
.get())->Minimize();
213 EXPECT_TRUE(layer
->GetAnimator()->is_animating());
214 // Expect duration from the outer scope
216 layer
->GetAnimator()->GetTransitionDuration().InMilliseconds());
219 layer
->GetAnimator()->StopAnimating();
222 // Test that duration respects default.
224 // Query default duration.
225 MinimizeAnimationObserver
observer(layer
->GetAnimator());
226 wm::GetWindowState(window
.get())->Minimize();
227 EXPECT_TRUE(layer
->GetAnimator()->is_animating());
228 base::TimeDelta
default_duration(observer
.duration());
230 layer
->GetAnimator()->StopAnimating();
232 ui::ScopedLayerAnimationSettings
settings(layer
->GetAnimator());
233 settings
.LockTransitionDuration();
234 // Setting transition duration is ignored since duration is locked
235 settings
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(1000));
236 wm::GetWindowState(window
.get())->Minimize();
237 EXPECT_TRUE(layer
->GetAnimator()->is_animating());
238 // Expect default duration (200ms for stock ash minimizing animation).
239 EXPECT_EQ(default_duration
.InMilliseconds(),
240 layer
->GetAnimator()->GetTransitionDuration().InMilliseconds());
242 layer
->GetAnimator()->StopAnimating();