| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/compositor/layer_animation_sequence.h" | 5 #include "ui/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" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 base::TimeTicks start_time; | 27 base::TimeTicks start_time; |
| 28 start_time += base::TimeDelta::FromSeconds(1); | 28 start_time += base::TimeDelta::FromSeconds(1); |
| 29 sequence.set_start_time(start_time); | 29 sequence.set_start_time(start_time); |
| 30 EXPECT_TRUE(sequence.IsFinished(start_time)); | 30 EXPECT_TRUE(sequence.IsFinished(start_time)); |
| 31 EXPECT_TRUE(sequence.properties().size() == 0); | 31 EXPECT_TRUE(sequence.properties().size() == 0); |
| 32 LayerAnimationElement::AnimatableProperties properties; | 32 LayerAnimationElement::AnimatableProperties properties; |
| 33 EXPECT_FALSE(sequence.HasCommonProperty(properties)); | 33 EXPECT_FALSE(sequence.HasCommonProperty(properties)); |
| 34 } | 34 } |
| 35 | 35 |
| 36 // Check that the sequences progresses the delegate as expected when it contains | 36 // Check that the sequences progresses the delegate as expected when it contains |
| 37 // a single element. | 37 // a single non-threaded element. |
| 38 TEST(LayerAnimationSequenceTest, SingleElement) { | 38 TEST(LayerAnimationSequenceTest, SingleElement) { |
| 39 LayerAnimationSequence sequence; | 39 LayerAnimationSequence sequence; |
| 40 TestLayerAnimationDelegate delegate; | 40 TestLayerAnimationDelegate delegate; |
| 41 float start = 0.0f; | 41 float start = 0.0f; |
| 42 float middle = 0.5f; | 42 float middle = 0.5f; |
| 43 float target = 1.0f; | 43 float target = 1.0f; |
| 44 base::TimeTicks start_time; | 44 base::TimeTicks start_time; |
| 45 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 45 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 46 sequence.AddElement( | 46 sequence.AddElement( |
| 47 LayerAnimationElement::CreateBrightnessElement(target, delta)); |
| 48 |
| 49 for (int i = 0; i < 2; ++i) { |
| 50 start_time += delta; |
| 51 sequence.set_start_time(start_time); |
| 52 delegate.SetBrightnessFromAnimation(start); |
| 53 sequence.Start(&delegate); |
| 54 sequence.Progress(start_time, &delegate); |
| 55 EXPECT_FLOAT_EQ(start, delegate.GetBrightnessForAnimation()); |
| 56 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(500), |
| 57 &delegate); |
| 58 EXPECT_FLOAT_EQ(middle, delegate.GetBrightnessForAnimation()); |
| 59 EXPECT_TRUE(sequence.IsFinished(start_time + delta)); |
| 60 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1000), |
| 61 &delegate); |
| 62 EXPECT_FLOAT_EQ(target, delegate.GetBrightnessForAnimation()); |
| 63 } |
| 64 |
| 65 EXPECT_TRUE(sequence.properties().size() == 1); |
| 66 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BRIGHTNESS) != |
| 67 sequence.properties().end()); |
| 68 } |
| 69 |
| 70 // Check that the sequences progresses the delegate as expected when it contains |
| 71 // a single threaded element. |
| 72 TEST(LayerAnimationSequenceTest, SingleThreadedElement) { |
| 73 LayerAnimationSequence sequence; |
| 74 TestLayerAnimationDelegate delegate; |
| 75 float start = 0.0f; |
| 76 float middle = 0.5f; |
| 77 float target = 1.0f; |
| 78 base::TimeTicks start_time; |
| 79 base::TimeTicks effective_start; |
| 80 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 81 sequence.AddElement( |
| 47 LayerAnimationElement::CreateOpacityElement(target, delta)); | 82 LayerAnimationElement::CreateOpacityElement(target, delta)); |
| 48 | 83 |
| 49 for (int i = 0; i < 2; ++i) { | 84 for (int i = 0; i < 2; ++i) { |
| 50 start_time += delta; | 85 int group_id = 1; |
| 86 sequence.set_animation_group_id(group_id); |
| 87 start_time = effective_start + delta; |
| 51 sequence.set_start_time(start_time); | 88 sequence.set_start_time(start_time); |
| 52 delegate.SetOpacityFromAnimation(start); | 89 delegate.SetOpacityFromAnimation(start); |
| 90 sequence.Start(&delegate); |
| 53 sequence.Progress(start_time, &delegate); | 91 sequence.Progress(start_time, &delegate); |
| 54 EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation()); | 92 EXPECT_FLOAT_EQ(start, sequence.last_progressed_fraction()); |
| 55 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(500), | 93 effective_start = start_time + delta; |
| 56 &delegate); | 94 sequence.OnThreadedAnimationStarted(cc::AnimationEvent( |
| 57 EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation()); | 95 cc::AnimationEvent::Started, |
| 58 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1000), | 96 0, |
| 59 &delegate); | 97 group_id, |
| 98 cc::Animation::Opacity, |
| 99 (effective_start - base::TimeTicks()).InSecondsF())); |
| 100 sequence.Progress(effective_start + delta/2, &delegate); |
| 101 EXPECT_FLOAT_EQ(middle, sequence.last_progressed_fraction()); |
| 102 EXPECT_TRUE(sequence.IsFinished(effective_start + delta)); |
| 103 sequence.Progress(effective_start + delta, &delegate); |
| 104 EXPECT_FLOAT_EQ(target, sequence.last_progressed_fraction()); |
| 60 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); | 105 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); |
| 61 } | 106 } |
| 62 | 107 |
| 63 EXPECT_TRUE(sequence.properties().size() == 1); | 108 EXPECT_TRUE(sequence.properties().size() == 1); |
| 64 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != | 109 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != |
| 65 sequence.properties().end()); | 110 sequence.properties().end()); |
| 66 EXPECT_TRUE(sequence.IsFinished(start_time + delta)); | |
| 67 } | 111 } |
| 68 | 112 |
| 69 // Check that the sequences progresses the delegate as expected when it contains | 113 // Check that the sequences progresses the delegate as expected when it contains |
| 70 // multiple elements. Note, see the layer animator tests for cyclic sequences. | 114 // multiple elements. Note, see the layer animator tests for cyclic sequences. |
| 71 TEST(LayerAnimationSequenceTest, MultipleElement) { | 115 TEST(LayerAnimationSequenceTest, MultipleElement) { |
| 72 LayerAnimationSequence sequence; | 116 LayerAnimationSequence sequence; |
| 73 TestLayerAnimationDelegate delegate; | 117 TestLayerAnimationDelegate delegate; |
| 74 float start_opacity = 0.0f; | 118 float start_opacity = 0.0f; |
| 75 float middle_opacity = 0.5f; | |
| 76 float target_opacity = 1.0f; | 119 float target_opacity = 1.0f; |
| 77 base::TimeTicks start_time; | 120 base::TimeTicks start_time; |
| 121 base::TimeTicks effective_start; |
| 78 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 122 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 79 sequence.AddElement( | 123 sequence.AddElement( |
| 80 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | 124 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); |
| 81 | 125 |
| 82 // Pause bounds for a second. | 126 // Pause bounds for a second. |
| 83 LayerAnimationElement::AnimatableProperties properties; | 127 LayerAnimationElement::AnimatableProperties properties; |
| 84 properties.insert(LayerAnimationElement::BOUNDS); | 128 properties.insert(LayerAnimationElement::BOUNDS); |
| 85 | 129 |
| 86 sequence.AddElement( | 130 sequence.AddElement( |
| 87 LayerAnimationElement::CreatePauseElement(properties, delta)); | 131 LayerAnimationElement::CreatePauseElement(properties, delta)); |
| 88 | 132 |
| 89 gfx::Transform start_transform, target_transform, middle_transform; | 133 gfx::Transform start_transform, target_transform, middle_transform; |
| 90 start_transform.Rotate(-30.0); | 134 start_transform.Rotate(-30.0); |
| 91 target_transform.Rotate(30.0); | 135 target_transform.Rotate(30.0); |
| 92 | 136 |
| 93 sequence.AddElement( | 137 sequence.AddElement( |
| 94 LayerAnimationElement::CreateTransformElement(target_transform, delta)); | 138 LayerAnimationElement::CreateTransformElement(target_transform, delta)); |
| 95 | 139 |
| 96 for (int i = 0; i < 2; ++i) { | 140 for (int i = 0; i < 2; ++i) { |
| 97 start_time += delta + delta + delta; | 141 int group_id = 1; |
| 142 sequence.set_animation_group_id(group_id); |
| 143 start_time = effective_start + 3 * delta; |
| 98 sequence.set_start_time(start_time); | 144 sequence.set_start_time(start_time); |
| 99 delegate.SetOpacityFromAnimation(start_opacity); | 145 delegate.SetOpacityFromAnimation(start_opacity); |
| 100 delegate.SetTransformFromAnimation(start_transform); | 146 delegate.SetTransformFromAnimation(start_transform); |
| 101 | 147 |
| 148 sequence.Start(&delegate); |
| 102 sequence.Progress(start_time, &delegate); | 149 sequence.Progress(start_time, &delegate); |
| 103 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); | 150 EXPECT_FLOAT_EQ(0.0, sequence.last_progressed_fraction()); |
| 104 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(500), | 151 effective_start = start_time + delta; |
| 105 &delegate); | 152 sequence.OnThreadedAnimationStarted(cc::AnimationEvent( |
| 106 EXPECT_FLOAT_EQ(middle_opacity, delegate.GetOpacityForAnimation()); | 153 cc::AnimationEvent::Started, |
| 107 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1000), | 154 0, |
| 108 &delegate); | 155 group_id, |
| 156 cc::Animation::Opacity, |
| 157 (effective_start - base::TimeTicks()).InSecondsF())); |
| 158 sequence.Progress(effective_start + delta/2, &delegate); |
| 159 EXPECT_FLOAT_EQ(0.5, sequence.last_progressed_fraction()); |
| 160 sequence.Progress(effective_start + delta, &delegate); |
| 109 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); | 161 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); |
| 162 |
| 163 // Now at the start of the pause. |
| 164 EXPECT_FLOAT_EQ(0.0, sequence.last_progressed_fraction()); |
| 110 TestLayerAnimationDelegate copy = delegate; | 165 TestLayerAnimationDelegate copy = delegate; |
| 111 | 166 |
| 112 // In the middle of the pause -- nothing should have changed. | 167 // In the middle of the pause -- nothing should have changed. |
| 113 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(1500), | 168 sequence.Progress(effective_start + delta + delta/2, |
| 114 &delegate); | 169 &delegate); |
| 115 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), | 170 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), |
| 116 copy.GetBoundsForAnimation()); | 171 copy.GetBoundsForAnimation()); |
| 117 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), | 172 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), |
| 118 copy.GetTransformForAnimation()); | 173 copy.GetTransformForAnimation()); |
| 119 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), | 174 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), |
| 120 copy.GetOpacityForAnimation()); | 175 copy.GetOpacityForAnimation()); |
| 121 | 176 |
| 122 | 177 |
| 123 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(2000), | 178 sequence.Progress(effective_start + 2 * delta, &delegate); |
| 124 &delegate); | |
| 125 CheckApproximatelyEqual(start_transform, | 179 CheckApproximatelyEqual(start_transform, |
| 126 delegate.GetTransformForAnimation()); | 180 delegate.GetTransformForAnimation()); |
| 127 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(2500), | 181 sequence.Progress(effective_start + 2 * delta + delta/2, &delegate); |
| 128 &delegate); | |
| 129 CheckApproximatelyEqual(middle_transform, | 182 CheckApproximatelyEqual(middle_transform, |
| 130 delegate.GetTransformForAnimation()); | 183 delegate.GetTransformForAnimation()); |
| 131 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(3000), | 184 EXPECT_TRUE(sequence.IsFinished(effective_start + 3 * delta)); |
| 132 &delegate); | 185 sequence.Progress(effective_start + 3 * delta, &delegate); |
| 133 CheckApproximatelyEqual(target_transform, | 186 CheckApproximatelyEqual(target_transform, |
| 134 delegate.GetTransformForAnimation()); | 187 delegate.GetTransformForAnimation()); |
| 135 } | 188 } |
| 136 | 189 |
| 137 EXPECT_TRUE(sequence.properties().size() == 3); | 190 EXPECT_TRUE(sequence.properties().size() == 3); |
| 138 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != | 191 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != |
| 139 sequence.properties().end()); | 192 sequence.properties().end()); |
| 140 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) != | 193 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) != |
| 141 sequence.properties().end()); | 194 sequence.properties().end()); |
| 142 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) != | 195 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) != |
| 143 sequence.properties().end()); | 196 sequence.properties().end()); |
| 144 EXPECT_TRUE(sequence.IsFinished(start_time + delta + delta + delta)); | |
| 145 } | 197 } |
| 146 | 198 |
| 147 // Check that a sequence can still be aborted if it has cycled many times. | 199 // Check that a sequence can still be aborted if it has cycled many times. |
| 148 TEST(LayerAnimationSequenceTest, AbortingCyclicSequence) { | 200 TEST(LayerAnimationSequenceTest, AbortingCyclicSequence) { |
| 149 LayerAnimationSequence sequence; | 201 LayerAnimationSequence sequence; |
| 150 TestLayerAnimationDelegate delegate; | 202 TestLayerAnimationDelegate delegate; |
| 151 float start_opacity = 0.0f; | 203 float start_brightness = 0.0f; |
| 152 float target_opacity = 1.0f; | 204 float target_brightness = 1.0f; |
| 153 base::TimeTicks start_time; | 205 base::TimeTicks start_time; |
| 154 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 206 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 155 sequence.AddElement( | 207 sequence.AddElement( |
| 156 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | 208 LayerAnimationElement::CreateBrightnessElement(target_brightness, delta)); |
| 157 | 209 |
| 158 sequence.AddElement( | 210 sequence.AddElement( |
| 159 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)); | 211 LayerAnimationElement::CreateBrightnessElement(start_brightness, delta)); |
| 160 | 212 |
| 161 sequence.set_is_cyclic(true); | 213 sequence.set_is_cyclic(true); |
| 162 | 214 |
| 163 delegate.SetOpacityFromAnimation(start_opacity); | 215 delegate.SetBrightnessFromAnimation(start_brightness); |
| 164 | 216 |
| 165 start_time += delta; | 217 start_time += delta; |
| 166 sequence.set_start_time(start_time); | 218 sequence.set_start_time(start_time); |
| 219 sequence.Start(&delegate); |
| 167 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(101000), | 220 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(101000), |
| 168 &delegate); | 221 &delegate); |
| 169 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); | 222 EXPECT_FLOAT_EQ(target_brightness, delegate.GetBrightnessForAnimation()); |
| 170 sequence.Abort(); | 223 sequence.Abort(&delegate); |
| 171 | 224 |
| 172 // Should be able to reuse the sequence after aborting. | 225 // Should be able to reuse the sequence after aborting. |
| 173 delegate.SetOpacityFromAnimation(start_opacity); | 226 delegate.SetBrightnessFromAnimation(start_brightness); |
| 174 start_time += base::TimeDelta::FromMilliseconds(101000); | 227 start_time += base::TimeDelta::FromMilliseconds(101000); |
| 175 sequence.set_start_time(start_time); | 228 sequence.set_start_time(start_time); |
| 176 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(100000), | 229 sequence.Progress(start_time + base::TimeDelta::FromMilliseconds(100000), |
| 177 &delegate); | 230 &delegate); |
| 178 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); | 231 EXPECT_FLOAT_EQ(start_brightness, delegate.GetBrightnessForAnimation()); |
| 179 } | 232 } |
| 180 | 233 |
| 181 // Check that a sequence can be 'fast-forwarded' to the end and the target set. | 234 // Check that a sequence can be 'fast-forwarded' to the end and the target set. |
| 182 // Also check that this has no effect if the sequence is cyclic. | 235 // Also check that this has no effect if the sequence is cyclic. |
| 183 TEST(LayerAnimationSequenceTest, SetTarget) { | 236 TEST(LayerAnimationSequenceTest, SetTarget) { |
| 184 LayerAnimationSequence sequence; | 237 LayerAnimationSequence sequence; |
| 185 TestLayerAnimationDelegate delegate; | 238 TestLayerAnimationDelegate delegate; |
| 186 float start_opacity = 0.0f; | 239 float start_opacity = 0.0f; |
| 187 float target_opacity = 1.0f; | 240 float target_opacity = 1.0f; |
| 188 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 241 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 189 sequence.AddElement( | 242 sequence.AddElement( |
| 190 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | 243 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); |
| 191 | 244 |
| 192 LayerAnimationElement::TargetValue target_value(&delegate); | 245 LayerAnimationElement::TargetValue target_value(&delegate); |
| 193 target_value.opacity = start_opacity; | 246 target_value.opacity = start_opacity; |
| 194 sequence.GetTargetValue(&target_value); | 247 sequence.GetTargetValue(&target_value); |
| 195 EXPECT_FLOAT_EQ(target_opacity, target_value.opacity); | 248 EXPECT_FLOAT_EQ(target_opacity, target_value.opacity); |
| 196 | 249 |
| 197 sequence.set_is_cyclic(true); | 250 sequence.set_is_cyclic(true); |
| 198 target_value.opacity = start_opacity; | 251 target_value.opacity = start_opacity; |
| 199 sequence.GetTargetValue(&target_value); | 252 sequence.GetTargetValue(&target_value); |
| 200 EXPECT_FLOAT_EQ(start_opacity, target_value.opacity); | 253 EXPECT_FLOAT_EQ(start_opacity, target_value.opacity); |
| 201 } | 254 } |
| 202 | 255 |
| 203 TEST(LayerAnimationSequenceTest, AddObserver) { | 256 TEST(LayerAnimationSequenceTest, AddObserver) { |
| 204 base::TimeTicks start_time; | 257 base::TimeTicks start_time; |
| 205 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 258 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 206 LayerAnimationSequence sequence; | 259 LayerAnimationSequence sequence; |
| 207 sequence.AddElement( | 260 sequence.AddElement( |
| 208 LayerAnimationElement::CreateOpacityElement(1.0f, delta)); | 261 LayerAnimationElement::CreateBrightnessElement(1.0f, delta)); |
| 209 for (int i = 0; i < 2; ++i) { | 262 for (int i = 0; i < 2; ++i) { |
| 210 start_time += delta; | 263 start_time += delta; |
| 211 sequence.set_start_time(start_time); | 264 sequence.set_start_time(start_time); |
| 212 TestLayerAnimationObserver observer; | 265 TestLayerAnimationObserver observer; |
| 213 TestLayerAnimationDelegate delegate; | 266 TestLayerAnimationDelegate delegate; |
| 214 sequence.AddObserver(&observer); | 267 sequence.AddObserver(&observer); |
| 215 EXPECT_TRUE(!observer.last_ended_sequence()); | 268 EXPECT_TRUE(!observer.last_ended_sequence()); |
| 216 sequence.Progress(start_time + delta, &delegate); | 269 sequence.Progress(start_time + delta, &delegate); |
| 217 EXPECT_EQ(observer.last_ended_sequence(), &sequence); | 270 EXPECT_EQ(observer.last_ended_sequence(), &sequence); |
| 218 sequence.RemoveObserver(&observer); | 271 sequence.RemoveObserver(&observer); |
| 219 } | 272 } |
| 220 } | 273 } |
| 221 | 274 |
| 222 } // namespace | 275 } // namespace |
| 223 | 276 |
| 224 } // namespace ui | 277 } // namespace ui |
| OLD | NEW |