| OLD | NEW |
| 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" |
| 14 #include "ui/gfx/compositor/layer_animation_delegate.h" | 15 #include "ui/gfx/compositor/layer_animation_delegate.h" |
| 15 #include "ui/gfx/compositor/layer_animation_element.h" | 16 #include "ui/gfx/compositor/layer_animation_element.h" |
| 16 #include "ui/gfx/compositor/test_utils.h" | 17 #include "ui/gfx/compositor/test_utils.h" |
| 17 #include "ui/gfx/compositor/test_layer_animation_delegate.h" | |
| 18 | 18 |
| 19 namespace ui { | 19 namespace ui { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Check that the sequence behaves sanely when it contains no elements. | 23 // Check that the sequence behaves sanely when it contains no elements. |
| 24 TEST(LayerAnimationSequenceTest, NoElement) { | 24 TEST(LayerAnimationSequenceTest, NoElement) { |
| 25 LayerAnimationSequence sequence; | 25 LayerAnimationSequence sequence; |
| 26 EXPECT_EQ(sequence.duration(), base::TimeDelta()); | 26 EXPECT_EQ(sequence.duration(), base::TimeDelta()); |
| 27 EXPECT_TRUE(sequence.properties().size() == 0); | 27 EXPECT_TRUE(sequence.properties().size() == 0); |
| 28 LayerAnimationElement::AnimatableProperties properties; | 28 LayerAnimationElement::AnimatableProperties properties; |
| 29 EXPECT_FALSE(sequence.HasCommonProperty(properties)); | 29 EXPECT_FALSE(sequence.HasCommonProperty(properties)); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // Check that the sequences progresses the delegate as expected when it contains | 32 // Check that the sequences progresses the delegate as expected when it contains |
| 33 // a single element. | 33 // a single element. |
| 34 TEST(LayerAnimationSequenceTest, SingleElement) { | 34 TEST(LayerAnimationSequenceTest, SingleElement) { |
| 35 LayerAnimationSequence sequence; | 35 LayerAnimationSequence sequence; |
| 36 TestLayerAnimationDelegate delegate; | 36 DummyLayerAnimationDelegate delegate; |
| 37 float start = 0.0; | 37 float start = 0.0; |
| 38 float middle = 0.5; | 38 float middle = 0.5; |
| 39 float target = 1.0; | 39 float target = 1.0; |
| 40 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 40 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 41 sequence.AddElement( | 41 sequence.AddElement( |
| 42 LayerAnimationElement::CreateOpacityElement(target, delta)); | 42 LayerAnimationElement::CreateOpacityElement(target, delta)); |
| 43 | 43 |
| 44 for (int i = 0; i < 2; ++i) { | 44 for (int i = 0; i < 2; ++i) { |
| 45 delegate.SetOpacityFromAnimation(start); | 45 delegate.SetOpacityFromAnimation(start); |
| 46 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate); | 46 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate); |
| 47 EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation()); | 47 EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation()); |
| 48 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate); | 48 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate); |
| 49 EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation()); | 49 EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation()); |
| 50 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate); | 50 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate); |
| 51 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); | 51 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); |
| 52 } | 52 } |
| 53 | 53 |
| 54 EXPECT_TRUE(sequence.properties().size() == 1); | 54 EXPECT_TRUE(sequence.properties().size() == 1); |
| 55 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != | 55 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != |
| 56 sequence.properties().end()); | 56 sequence.properties().end()); |
| 57 EXPECT_EQ(delta, sequence.duration()); | 57 EXPECT_EQ(delta, sequence.duration()); |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Check that the sequences progresses the delegate as expected when it contains | 60 // Check that the sequences progresses the delegate as expected when it contains |
| 61 // multiple elements. Note, see the layer animator tests for cyclic sequences. | 61 // multiple elements. Note, see the layer animator tests for cyclic sequences. |
| 62 TEST(LayerAnimationSequenceTest, MultipleElement) { | 62 TEST(LayerAnimationSequenceTest, MultipleElement) { |
| 63 LayerAnimationSequence sequence; | 63 LayerAnimationSequence sequence; |
| 64 TestLayerAnimationDelegate delegate; | 64 DummyLayerAnimationDelegate delegate; |
| 65 float start_opacity = 0.0; | 65 float start_opacity = 0.0; |
| 66 float middle_opacity = 0.5; | 66 float middle_opacity = 0.5; |
| 67 float target_opacity = 1.0; | 67 float target_opacity = 1.0; |
| 68 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 68 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 69 sequence.AddElement( | 69 sequence.AddElement( |
| 70 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | 70 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); |
| 71 | 71 |
| 72 // Pause bounds for a second. | 72 // Pause bounds for a second. |
| 73 LayerAnimationElement::AnimatableProperties properties; | 73 LayerAnimationElement::AnimatableProperties properties; |
| 74 properties.insert(LayerAnimationElement::BOUNDS); | 74 properties.insert(LayerAnimationElement::BOUNDS); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 86 for (int i = 0; i < 2; ++i) { | 86 for (int i = 0; i < 2; ++i) { |
| 87 delegate.SetOpacityFromAnimation(start_opacity); | 87 delegate.SetOpacityFromAnimation(start_opacity); |
| 88 delegate.SetTransformFromAnimation(start_transform); | 88 delegate.SetTransformFromAnimation(start_transform); |
| 89 | 89 |
| 90 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate); | 90 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate); |
| 91 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); | 91 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); |
| 92 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate); | 92 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate); |
| 93 EXPECT_FLOAT_EQ(middle_opacity, delegate.GetOpacityForAnimation()); | 93 EXPECT_FLOAT_EQ(middle_opacity, delegate.GetOpacityForAnimation()); |
| 94 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate); | 94 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate); |
| 95 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); | 95 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); |
| 96 TestLayerAnimationDelegate copy = delegate; | 96 DummyLayerAnimationDelegate copy = delegate; |
| 97 | 97 |
| 98 // In the middle of the pause -- nothing should have changed. | 98 // In the middle of the pause -- nothing should have changed. |
| 99 sequence.Progress(base::TimeDelta::FromMilliseconds(1500), &delegate); | 99 sequence.Progress(base::TimeDelta::FromMilliseconds(1500), &delegate); |
| 100 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), | 100 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), |
| 101 copy.GetBoundsForAnimation()); | 101 copy.GetBoundsForAnimation()); |
| 102 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), | 102 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), |
| 103 copy.GetTransformForAnimation()); | 103 copy.GetTransformForAnimation()); |
| 104 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), | 104 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), |
| 105 copy.GetOpacityForAnimation()); | 105 copy.GetOpacityForAnimation()); |
| 106 | 106 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 122 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) != | 122 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) != |
| 123 sequence.properties().end()); | 123 sequence.properties().end()); |
| 124 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) != | 124 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) != |
| 125 sequence.properties().end()); | 125 sequence.properties().end()); |
| 126 EXPECT_EQ(delta + delta + delta, sequence.duration()); | 126 EXPECT_EQ(delta + delta + delta, sequence.duration()); |
| 127 } | 127 } |
| 128 | 128 |
| 129 // Check that a sequence can still be aborted if it has cycled many times. | 129 // Check that a sequence can still be aborted if it has cycled many times. |
| 130 TEST(LayerAnimationSequenceTest, AbortingCyclicSequence) { | 130 TEST(LayerAnimationSequenceTest, AbortingCyclicSequence) { |
| 131 LayerAnimationSequence sequence; | 131 LayerAnimationSequence sequence; |
| 132 TestLayerAnimationDelegate delegate; | 132 DummyLayerAnimationDelegate delegate; |
| 133 float start_opacity = 0.0; | 133 float start_opacity = 0.0; |
| 134 float target_opacity = 1.0; | 134 float target_opacity = 1.0; |
| 135 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | 135 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 136 sequence.AddElement( | 136 sequence.AddElement( |
| 137 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | 137 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); |
| 138 | 138 |
| 139 sequence.AddElement( | 139 sequence.AddElement( |
| 140 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)); | 140 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)); |
| 141 | 141 |
| 142 sequence.set_is_cyclic(true); | 142 sequence.set_is_cyclic(true); |
| 143 | 143 |
| 144 delegate.SetOpacityFromAnimation(start_opacity); | 144 delegate.SetOpacityFromAnimation(start_opacity); |
| 145 | 145 |
| 146 sequence.Progress(base::TimeDelta::FromMilliseconds(101000), &delegate); | 146 sequence.Progress(base::TimeDelta::FromMilliseconds(101000), &delegate); |
| 147 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); | 147 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); |
| 148 sequence.Abort(); | 148 sequence.Abort(); |
| 149 | 149 |
| 150 // Should be able to reuse the sequence after aborting. | 150 // Should be able to reuse the sequence after aborting. |
| 151 delegate.SetOpacityFromAnimation(start_opacity); | 151 delegate.SetOpacityFromAnimation(start_opacity); |
| 152 sequence.Progress(base::TimeDelta::FromMilliseconds(100000), &delegate); | 152 sequence.Progress(base::TimeDelta::FromMilliseconds(100000), &delegate); |
| 153 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); | 153 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); |
| 154 } | 154 } |
| 155 | 155 |
| 156 // 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 TEST(LayerAnimationSequenceTest, SetTarget) { |
| 159 LayerAnimationSequence sequence; |
| 160 DummyLayerAnimationDelegate delegate; |
| 161 float start_opacity = 0.0; |
| 162 float target_opacity = 1.0; |
| 163 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); |
| 164 sequence.AddElement( |
| 165 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); |
| 166 |
| 167 LayerAnimationElement::TargetValue target_value; |
| 168 target_value.opacity = start_opacity; |
| 169 sequence.GetTargetValue(&target_value); |
| 170 EXPECT_FLOAT_EQ(target_opacity, target_value.opacity); |
| 171 |
| 172 sequence.set_is_cyclic(true); |
| 173 target_value.opacity = start_opacity; |
| 174 sequence.GetTargetValue(&target_value); |
| 175 EXPECT_FLOAT_EQ(start_opacity, target_value.opacity); |
| 176 } |
| 177 |
| 156 } // namespace | 178 } // namespace |
| 157 | 179 |
| 158 } // namespace ui | 180 } // namespace ui |
| OLD | NEW |