layer_animation_element_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.
4
5#include "ui/compositor/layer_animation_element.h"
6
7#include "base/basictypes.h"
8#include "base/compiler_specific.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/time.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "ui/compositor/layer_animation_delegate.h"
13#include "ui/compositor/test/test_layer_animation_delegate.h"
14#include "ui/compositor/test/test_utils.h"
15#include "ui/gfx/rect.h"
16#include "ui/gfx/transform.h"
17
18namespace ui {
19
20namespace {
21
22// Check that the transformation element progresses the delegate as expected and
23// that the element can be reused after it completes.
24TEST(LayerAnimationElementTest, TransformElement) {
25  TestLayerAnimationDelegate delegate;
26  gfx::Transform start_transform, target_transform, middle_transform;
27  start_transform.SetRotate(-90);
28  target_transform.SetRotate(90);
29  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
30
31  scoped_ptr<LayerAnimationElement> element(
32      LayerAnimationElement::CreateTransformElement(target_transform, delta));
33
34  for (int i = 0; i < 2; ++i) {
35    delegate.SetTransformFromAnimation(start_transform);
36    element->Progress(0.0, &delegate);
37    CheckApproximatelyEqual(start_transform,
38                            delegate.GetTransformForAnimation());
39    element->Progress(0.5, &delegate);
40    CheckApproximatelyEqual(middle_transform,
41                            delegate.GetTransformForAnimation());
42    element->Progress(1.0, &delegate);
43    CheckApproximatelyEqual(target_transform,
44                            delegate.GetTransformForAnimation());
45  }
46
47  LayerAnimationElement::TargetValue target_value(&delegate);
48  element->GetTargetValue(&target_value);
49  CheckApproximatelyEqual(target_transform, target_value.transform);
50
51  EXPECT_EQ(delta, element->duration());
52}
53
54// Check that the bounds element progresses the delegate as expected and
55// that the element can be reused after it completes.
56TEST(LayerAnimationElementTest, BoundsElement) {
57  TestLayerAnimationDelegate delegate;
58  gfx::Rect start, target, middle;
59  start = target = middle = gfx::Rect(0, 0, 50, 50);
60  start.set_x(-90);
61  target.set_x(90);
62  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
63
64  scoped_ptr<LayerAnimationElement> element(
65      LayerAnimationElement::CreateBoundsElement(target, delta));
66
67  for (int i = 0; i < 2; ++i) {
68    delegate.SetBoundsFromAnimation(start);
69    element->Progress(0.0, &delegate);
70    CheckApproximatelyEqual(start, delegate.GetBoundsForAnimation());
71    element->Progress(0.5, &delegate);
72    CheckApproximatelyEqual(middle, delegate.GetBoundsForAnimation());
73    element->Progress(1.0, &delegate);
74    CheckApproximatelyEqual(target, delegate.GetBoundsForAnimation());
75  }
76
77  LayerAnimationElement::TargetValue target_value(&delegate);
78  element->GetTargetValue(&target_value);
79  CheckApproximatelyEqual(target, target_value.bounds);
80
81  EXPECT_EQ(delta, element->duration());
82}
83
84// Check that the opacity element progresses the delegate as expected and
85// that the element can be reused after it completes.
86TEST(LayerAnimationElementTest, OpacityElement) {
87  TestLayerAnimationDelegate delegate;
88  float start = 0.0;
89  float middle = 0.5;
90  float target = 1.0;
91  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
92  scoped_ptr<LayerAnimationElement> element(
93      LayerAnimationElement::CreateOpacityElement(target, delta));
94
95  for (int i = 0; i < 2; ++i) {
96    delegate.SetOpacityFromAnimation(start);
97    element->Progress(0.0, &delegate);
98    EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation());
99    element->Progress(0.5, &delegate);
100    EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation());
101    element->Progress(1.0, &delegate);
102    EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation());
103  }
104
105  LayerAnimationElement::TargetValue target_value(&delegate);
106  element->GetTargetValue(&target_value);
107  EXPECT_FLOAT_EQ(target, target_value.opacity);
108
109  EXPECT_EQ(delta, element->duration());
110}
111
112// Check that the visibility element progresses the delegate as expected and
113// that the element can be reused after it completes.
114TEST(LayerAnimationElementTest, VisibilityElement) {
115  TestLayerAnimationDelegate delegate;
116  bool start = true;
117  bool target = false;
118  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
119  scoped_ptr<LayerAnimationElement> element(
120      LayerAnimationElement::CreateVisibilityElement(target, delta));
121
122  for (int i = 0; i < 2; ++i) {
123    delegate.SetVisibilityFromAnimation(start);
124    element->Progress(0.0, &delegate);
125    EXPECT_TRUE(delegate.GetVisibilityForAnimation());
126    element->Progress(0.5, &delegate);
127    EXPECT_TRUE(delegate.GetVisibilityForAnimation());
128    element->Progress(1.0, &delegate);
129    EXPECT_FALSE(delegate.GetVisibilityForAnimation());
130  }
131
132  LayerAnimationElement::TargetValue target_value(&delegate);
133  element->GetTargetValue(&target_value);
134  EXPECT_FALSE(target_value.visibility);
135
136  EXPECT_EQ(delta, element->duration());
137}
138
139// Check that the Brightness element progresses the delegate as expected and
140// that the element can be reused after it completes.
141TEST(LayerAnimationElementTest, BrightnessElement) {
142  TestLayerAnimationDelegate delegate;
143  float start = 0.0;
144  float middle = 0.5;
145  float target = 1.0;
146  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
147  scoped_ptr<LayerAnimationElement> element(
148      LayerAnimationElement::CreateBrightnessElement(target, delta));
149
150  for (int i = 0; i < 2; ++i) {
151    delegate.SetBrightnessFromAnimation(start);
152    element->Progress(0.0, &delegate);
153    EXPECT_FLOAT_EQ(start, delegate.GetBrightnessForAnimation());
154    element->Progress(0.5, &delegate);
155    EXPECT_FLOAT_EQ(middle, delegate.GetBrightnessForAnimation());
156    element->Progress(1.0, &delegate);
157    EXPECT_FLOAT_EQ(target, delegate.GetBrightnessForAnimation());
158  }
159
160  LayerAnimationElement::TargetValue target_value(&delegate);
161  element->GetTargetValue(&target_value);
162  EXPECT_FLOAT_EQ(target, target_value.brightness);
163
164  EXPECT_EQ(delta, element->duration());
165}
166
167// Check that the Grayscale element progresses the delegate as expected and
168// that the element can be reused after it completes.
169TEST(LayerAnimationElementTest, GrayscaleElement) {
170  TestLayerAnimationDelegate delegate;
171  float start = 0.0;
172  float middle = 0.5;
173  float target = 1.0;
174  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
175  scoped_ptr<LayerAnimationElement> element(
176      LayerAnimationElement::CreateGrayscaleElement(target, delta));
177
178  for (int i = 0; i < 2; ++i) {
179    delegate.SetGrayscaleFromAnimation(start);
180    element->Progress(0.0, &delegate);
181    EXPECT_FLOAT_EQ(start, delegate.GetGrayscaleForAnimation());
182    element->Progress(0.5, &delegate);
183    EXPECT_FLOAT_EQ(middle, delegate.GetGrayscaleForAnimation());
184    element->Progress(1.0, &delegate);
185    EXPECT_FLOAT_EQ(target, delegate.GetGrayscaleForAnimation());
186  }
187
188  LayerAnimationElement::TargetValue target_value(&delegate);
189  element->GetTargetValue(&target_value);
190  EXPECT_FLOAT_EQ(target, target_value.grayscale);
191
192  EXPECT_EQ(delta, element->duration());
193}
194
195// Check that the pause element progresses the delegate as expected and
196// that the element can be reused after it completes.
197TEST(LayerAnimationElementTest, PauseElement) {
198  LayerAnimationElement::AnimatableProperties properties;
199  properties.insert(LayerAnimationElement::TRANSFORM);
200  properties.insert(LayerAnimationElement::BOUNDS);
201  properties.insert(LayerAnimationElement::OPACITY);
202  properties.insert(LayerAnimationElement::BRIGHTNESS);
203  properties.insert(LayerAnimationElement::GRAYSCALE);
204  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
205
206  scoped_ptr<LayerAnimationElement> element(
207      LayerAnimationElement::CreatePauseElement(properties, delta));
208
209  TestLayerAnimationDelegate delegate;
210  TestLayerAnimationDelegate copy = delegate;
211
212  element->Progress(1.0, &delegate);
213
214  // Nothing should have changed.
215  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(),
216                          copy.GetBoundsForAnimation());
217  CheckApproximatelyEqual(delegate.GetTransformForAnimation(),
218                          copy.GetTransformForAnimation());
219  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(),
220                  copy.GetOpacityForAnimation());
221  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(),
222                  copy.GetBrightnessForAnimation());
223  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(),
224                  copy.GetGrayscaleForAnimation());
225
226  // Pause should last for |delta|.
227  EXPECT_EQ(delta, element->duration());
228}
229
230} // namespace
231
232} // namespace ui
233