Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: ui/gfx/compositor/layer_animation_sequence_unittest.cc

Issue 8395046: Allows observers to be notified when layer animations complete. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address reviewer comments. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/compositor/layer_animation_sequence.h" 5 #include "ui/gfx/compositor/layer_animation_sequence.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/gfx/rect.h" 12 #include "ui/gfx/rect.h"
13 #include "ui/gfx/transform.h" 13 #include "ui/gfx/transform.h"
14 #include "ui/gfx/compositor/dummy_layer_animation_delegate.h"
15 #include "ui/gfx/compositor/layer_animation_delegate.h" 14 #include "ui/gfx/compositor/layer_animation_delegate.h"
16 #include "ui/gfx/compositor/layer_animation_element.h" 15 #include "ui/gfx/compositor/layer_animation_element.h"
16 #include "ui/gfx/compositor/test_layer_animation_delegate.h"
17 #include "ui/gfx/compositor/test_layer_animation_observer.h"
17 #include "ui/gfx/compositor/test_utils.h" 18 #include "ui/gfx/compositor/test_utils.h"
18 19
19 namespace ui { 20 namespace ui {
20 21
21 namespace { 22 namespace {
22 23
23 // Check that the sequence behaves sanely when it contains no elements. 24 // Check that the sequence behaves sanely when it contains no elements.
24 TEST(LayerAnimationSequenceTest, NoElement) { 25 TEST(LayerAnimationSequenceTest, NoElement) {
25 LayerAnimationSequence sequence; 26 LayerAnimationSequence sequence;
26 EXPECT_EQ(sequence.duration(), base::TimeDelta()); 27 EXPECT_EQ(sequence.duration(), base::TimeDelta());
27 EXPECT_TRUE(sequence.properties().size() == 0); 28 EXPECT_TRUE(sequence.properties().size() == 0);
28 LayerAnimationElement::AnimatableProperties properties; 29 LayerAnimationElement::AnimatableProperties properties;
29 EXPECT_FALSE(sequence.HasCommonProperty(properties)); 30 EXPECT_FALSE(sequence.HasCommonProperty(properties));
30 } 31 }
31 32
32 // Check that the sequences progresses the delegate as expected when it contains 33 // Check that the sequences progresses the delegate as expected when it contains
33 // a single element. 34 // a single element.
34 TEST(LayerAnimationSequenceTest, SingleElement) { 35 TEST(LayerAnimationSequenceTest, SingleElement) {
35 LayerAnimationSequence sequence; 36 LayerAnimationSequence sequence;
36 DummyLayerAnimationDelegate delegate; 37 TestLayerAnimationDelegate delegate;
37 float start = 0.0; 38 float start = 0.0f;
38 float middle = 0.5; 39 float middle = 0.5f;
39 float target = 1.0; 40 float target = 1.0f;
40 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 41 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
41 sequence.AddElement( 42 sequence.AddElement(
42 LayerAnimationElement::CreateOpacityElement(target, delta)); 43 LayerAnimationElement::CreateOpacityElement(target, delta));
43 44
44 for (int i = 0; i < 2; ++i) { 45 for (int i = 0; i < 2; ++i) {
45 delegate.SetOpacityFromAnimation(start); 46 delegate.SetOpacityFromAnimation(start);
46 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate); 47 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate);
47 EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation()); 48 EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation());
48 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate); 49 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate);
49 EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation()); 50 EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation());
50 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate); 51 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate);
51 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); 52 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation());
52 } 53 }
53 54
54 EXPECT_TRUE(sequence.properties().size() == 1); 55 EXPECT_TRUE(sequence.properties().size() == 1);
55 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != 56 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) !=
56 sequence.properties().end()); 57 sequence.properties().end());
57 EXPECT_EQ(delta, sequence.duration()); 58 EXPECT_EQ(delta, sequence.duration());
58 } 59 }
59 60
60 // Check that the sequences progresses the delegate as expected when it contains 61 // Check that the sequences progresses the delegate as expected when it contains
61 // multiple elements. Note, see the layer animator tests for cyclic sequences. 62 // multiple elements. Note, see the layer animator tests for cyclic sequences.
62 TEST(LayerAnimationSequenceTest, MultipleElement) { 63 TEST(LayerAnimationSequenceTest, MultipleElement) {
63 LayerAnimationSequence sequence; 64 LayerAnimationSequence sequence;
64 DummyLayerAnimationDelegate delegate; 65 TestLayerAnimationDelegate delegate;
65 float start_opacity = 0.0; 66 float start_opacity = 0.0f;
66 float middle_opacity = 0.5; 67 float middle_opacity = 0.5f;
67 float target_opacity = 1.0; 68 float target_opacity = 1.0f;
68 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 69 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
69 sequence.AddElement( 70 sequence.AddElement(
70 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); 71 LayerAnimationElement::CreateOpacityElement(target_opacity, delta));
71 72
72 // Pause bounds for a second. 73 // Pause bounds for a second.
73 LayerAnimationElement::AnimatableProperties properties; 74 LayerAnimationElement::AnimatableProperties properties;
74 properties.insert(LayerAnimationElement::BOUNDS); 75 properties.insert(LayerAnimationElement::BOUNDS);
75 76
76 sequence.AddElement( 77 sequence.AddElement(
77 LayerAnimationElement::CreatePauseElement(properties, delta)); 78 LayerAnimationElement::CreatePauseElement(properties, delta));
78 79
79 Transform start_transform, target_transform, middle_transform; 80 Transform start_transform, target_transform, middle_transform;
80 start_transform.SetRotate(-90); 81 start_transform.SetRotate(-90);
81 target_transform.SetRotate(90); 82 target_transform.SetRotate(90);
82 83
83 sequence.AddElement( 84 sequence.AddElement(
84 LayerAnimationElement::CreateTransformElement(target_transform, delta)); 85 LayerAnimationElement::CreateTransformElement(target_transform, delta));
85 86
86 for (int i = 0; i < 2; ++i) { 87 for (int i = 0; i < 2; ++i) {
87 delegate.SetOpacityFromAnimation(start_opacity); 88 delegate.SetOpacityFromAnimation(start_opacity);
88 delegate.SetTransformFromAnimation(start_transform); 89 delegate.SetTransformFromAnimation(start_transform);
89 90
90 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate); 91 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate);
91 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); 92 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation());
92 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate); 93 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate);
93 EXPECT_FLOAT_EQ(middle_opacity, delegate.GetOpacityForAnimation()); 94 EXPECT_FLOAT_EQ(middle_opacity, delegate.GetOpacityForAnimation());
94 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate); 95 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate);
95 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); 96 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation());
96 DummyLayerAnimationDelegate copy = delegate; 97 TestLayerAnimationDelegate copy = delegate;
97 98
98 // In the middle of the pause -- nothing should have changed. 99 // In the middle of the pause -- nothing should have changed.
99 sequence.Progress(base::TimeDelta::FromMilliseconds(1500), &delegate); 100 sequence.Progress(base::TimeDelta::FromMilliseconds(1500), &delegate);
100 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), 101 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(),
101 copy.GetBoundsForAnimation()); 102 copy.GetBoundsForAnimation());
102 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), 103 CheckApproximatelyEqual(delegate.GetTransformForAnimation(),
103 copy.GetTransformForAnimation()); 104 copy.GetTransformForAnimation());
104 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 105 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(),
105 copy.GetOpacityForAnimation()); 106 copy.GetOpacityForAnimation());
106 107
(...skipping 15 matching lines...) Expand all
122 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) != 123 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) !=
123 sequence.properties().end()); 124 sequence.properties().end());
124 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) != 125 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) !=
125 sequence.properties().end()); 126 sequence.properties().end());
126 EXPECT_EQ(delta + delta + delta, sequence.duration()); 127 EXPECT_EQ(delta + delta + delta, sequence.duration());
127 } 128 }
128 129
129 // Check that a sequence can still be aborted if it has cycled many times. 130 // Check that a sequence can still be aborted if it has cycled many times.
130 TEST(LayerAnimationSequenceTest, AbortingCyclicSequence) { 131 TEST(LayerAnimationSequenceTest, AbortingCyclicSequence) {
131 LayerAnimationSequence sequence; 132 LayerAnimationSequence sequence;
132 DummyLayerAnimationDelegate delegate; 133 TestLayerAnimationDelegate delegate;
133 float start_opacity = 0.0; 134 float start_opacity = 0.0f;
134 float target_opacity = 1.0; 135 float target_opacity = 1.0f;
135 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 136 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
136 sequence.AddElement( 137 sequence.AddElement(
137 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); 138 LayerAnimationElement::CreateOpacityElement(target_opacity, delta));
138 139
139 sequence.AddElement( 140 sequence.AddElement(
140 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)); 141 LayerAnimationElement::CreateOpacityElement(start_opacity, delta));
141 142
142 sequence.set_is_cyclic(true); 143 sequence.set_is_cyclic(true);
143 144
144 delegate.SetOpacityFromAnimation(start_opacity); 145 delegate.SetOpacityFromAnimation(start_opacity);
145 146
146 sequence.Progress(base::TimeDelta::FromMilliseconds(101000), &delegate); 147 sequence.Progress(base::TimeDelta::FromMilliseconds(101000), &delegate);
147 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); 148 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation());
148 sequence.Abort(); 149 sequence.Abort();
149 150
150 // Should be able to reuse the sequence after aborting. 151 // Should be able to reuse the sequence after aborting.
151 delegate.SetOpacityFromAnimation(start_opacity); 152 delegate.SetOpacityFromAnimation(start_opacity);
152 sequence.Progress(base::TimeDelta::FromMilliseconds(100000), &delegate); 153 sequence.Progress(base::TimeDelta::FromMilliseconds(100000), &delegate);
153 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); 154 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation());
154 } 155 }
155 156
156 // Check that a sequence can be 'fast-forwarded' to the end and the target set. 157 // Check that a sequence can be 'fast-forwarded' to the end and the target set.
157 // Also check that this has no effect if the sequence is cyclic. 158 // Also check that this has no effect if the sequence is cyclic.
158 TEST(LayerAnimationSequenceTest, SetTarget) { 159 TEST(LayerAnimationSequenceTest, SetTarget) {
159 LayerAnimationSequence sequence; 160 LayerAnimationSequence sequence;
160 DummyLayerAnimationDelegate delegate; 161 TestLayerAnimationDelegate delegate;
161 float start_opacity = 0.0; 162 float start_opacity = 0.0f;
162 float target_opacity = 1.0; 163 float target_opacity = 1.0f;
163 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); 164 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
164 sequence.AddElement( 165 sequence.AddElement(
165 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); 166 LayerAnimationElement::CreateOpacityElement(target_opacity, delta));
166 167
167 LayerAnimationElement::TargetValue target_value; 168 LayerAnimationElement::TargetValue target_value;
168 target_value.opacity = start_opacity; 169 target_value.opacity = start_opacity;
169 sequence.GetTargetValue(&target_value); 170 sequence.GetTargetValue(&target_value);
170 EXPECT_FLOAT_EQ(target_opacity, target_value.opacity); 171 EXPECT_FLOAT_EQ(target_opacity, target_value.opacity);
171 172
172 sequence.set_is_cyclic(true); 173 sequence.set_is_cyclic(true);
173 target_value.opacity = start_opacity; 174 target_value.opacity = start_opacity;
174 sequence.GetTargetValue(&target_value); 175 sequence.GetTargetValue(&target_value);
175 EXPECT_FLOAT_EQ(start_opacity, target_value.opacity); 176 EXPECT_FLOAT_EQ(start_opacity, target_value.opacity);
176 } 177 }
177 178
179 TEST(LayerAnimationSequenceTest, AddObserver) {
180 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
181 LayerAnimationSequence sequence;
182 sequence.AddElement(
183 LayerAnimationElement::CreateOpacityElement(1.0f, delta));
184 for (int i = 0; i < 2; ++i) {
185 TestLayerAnimationObserver observer;
186 TestLayerAnimationDelegate delegate;
187 sequence.AddObserver(&observer);
188 EXPECT_TRUE(!observer.last_ended_sequence());
189 sequence.Progress(delta, &delegate);
190 EXPECT_EQ(observer.last_ended_sequence(), &sequence);
191 sequence.RemoveObserver(&observer);
192 }
193 }
194
178 } // namespace 195 } // namespace
179 196
180 } // namespace ui 197 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698