layer_animation_sequence_unittest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #include "ui/compositor/layer_animation_sequence.h"
  5. #include <memory>
  6. #include "base/compiler_specific.h"
  7. #include "base/strings/stringprintf.h"
  8. #include "base/time/time.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "ui/compositor/layer_animation_delegate.h"
  11. #include "ui/compositor/layer_animation_element.h"
  12. #include "ui/compositor/test/test_layer_animation_delegate.h"
  13. #include "ui/compositor/test/test_layer_animation_observer.h"
  14. #include "ui/compositor/test/test_utils.h"
  15. #include "ui/gfx/geometry/rect.h"
  16. #include "ui/gfx/geometry/transform.h"
  17. namespace ui {
  18. namespace {
  19. // Check that the sequence behaves sanely when it contains no elements.
  20. TEST(LayerAnimationSequenceTest, NoElement) {
  21. LayerAnimationSequence sequence;
  22. base::TimeTicks start_time;
  23. start_time += base::Seconds(1);
  24. sequence.set_start_time(start_time);
  25. EXPECT_TRUE(sequence.IsFinished(start_time));
  26. EXPECT_EQ(static_cast<LayerAnimationElement::AnimatableProperties>(
  27. LayerAnimationElement::UNKNOWN),
  28. sequence.properties());
  29. EXPECT_FALSE(sequence.HasConflictingProperty(LayerAnimationElement::UNKNOWN));
  30. }
  31. // Check that the sequences progresses the delegate as expected when it contains
  32. // a single non-threaded element.
  33. TEST(LayerAnimationSequenceTest, SingleElement) {
  34. LayerAnimationSequence sequence;
  35. TestLayerAnimationDelegate delegate;
  36. float start = 0.0f;
  37. float middle = 0.5f;
  38. float target = 1.0f;
  39. base::TimeTicks start_time;
  40. base::TimeDelta delta = base::Seconds(1);
  41. sequence.AddElement(
  42. LayerAnimationElement::CreateBrightnessElement(target, delta));
  43. for (int i = 0; i < 2; ++i) {
  44. start_time += delta;
  45. sequence.set_start_time(start_time);
  46. delegate.SetBrightnessFromAnimation(
  47. start, PropertyChangeReason::NOT_FROM_ANIMATION);
  48. sequence.Start(&delegate);
  49. sequence.Progress(start_time, &delegate);
  50. EXPECT_FLOAT_EQ(start, delegate.GetBrightnessForAnimation());
  51. sequence.Progress(start_time + base::Milliseconds(500), &delegate);
  52. EXPECT_FLOAT_EQ(middle, delegate.GetBrightnessForAnimation());
  53. EXPECT_TRUE(sequence.IsFinished(start_time + delta));
  54. sequence.Progress(start_time + base::Milliseconds(1000), &delegate);
  55. EXPECT_FLOAT_EQ(target, delegate.GetBrightnessForAnimation());
  56. }
  57. EXPECT_EQ(static_cast<LayerAnimationElement::AnimatableProperties>(
  58. LayerAnimationElement::BRIGHTNESS),
  59. sequence.properties());
  60. }
  61. // Check that the sequences progresses the delegate as expected when it contains
  62. // a single threaded element.
  63. TEST(LayerAnimationSequenceTest, SingleThreadedElement) {
  64. LayerAnimationSequence sequence;
  65. TestLayerAnimationDelegate delegate;
  66. float start = 0.0f;
  67. float middle = 0.5f;
  68. float target = 1.0f;
  69. base::TimeTicks start_time;
  70. base::TimeTicks effective_start;
  71. base::TimeDelta delta = base::Seconds(1);
  72. sequence.AddElement(
  73. LayerAnimationElement::CreateOpacityElement(target, delta));
  74. for (int i = 0; i < 2; ++i) {
  75. int starting_group_id = 1;
  76. sequence.set_animation_group_id(starting_group_id);
  77. start_time = effective_start + delta;
  78. sequence.set_start_time(start_time);
  79. delegate.SetOpacityFromAnimation(start,
  80. PropertyChangeReason::NOT_FROM_ANIMATION);
  81. sequence.Start(&delegate);
  82. sequence.Progress(start_time, &delegate);
  83. EXPECT_FLOAT_EQ(start, sequence.last_progressed_fraction());
  84. effective_start = start_time + delta;
  85. sequence.OnThreadedAnimationStarted(effective_start,
  86. cc::TargetProperty::OPACITY,
  87. sequence.animation_group_id());
  88. sequence.Progress(effective_start + delta/2, &delegate);
  89. EXPECT_FLOAT_EQ(middle, sequence.last_progressed_fraction());
  90. EXPECT_TRUE(sequence.IsFinished(effective_start + delta));
  91. sequence.Progress(effective_start + delta, &delegate);
  92. EXPECT_FLOAT_EQ(target, sequence.last_progressed_fraction());
  93. EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation());
  94. }
  95. EXPECT_EQ(static_cast<LayerAnimationElement::AnimatableProperties>(
  96. LayerAnimationElement::OPACITY),
  97. sequence.properties());
  98. }
  99. // Check that the sequences progresses the delegate as expected when it contains
  100. // multiple elements. Note, see the layer animator tests for repeating
  101. // sequences.
  102. TEST(LayerAnimationSequenceTest, MultipleElement) {
  103. LayerAnimationSequence sequence;
  104. TestLayerAnimationDelegate delegate;
  105. float start_opacity = 0.0f;
  106. float target_opacity = 1.0f;
  107. base::TimeTicks start_time;
  108. base::TimeTicks opacity_effective_start;
  109. base::TimeTicks transform_effective_start;
  110. base::TimeDelta delta = base::Seconds(1);
  111. sequence.AddElement(
  112. LayerAnimationElement::CreateOpacityElement(target_opacity, delta));
  113. // Pause bounds for a second.
  114. sequence.AddElement(LayerAnimationElement::CreatePauseElement(
  115. LayerAnimationElement::BOUNDS, delta));
  116. gfx::Transform start_transform, target_transform;
  117. start_transform.Rotate(-30.0);
  118. target_transform.Rotate(30.0);
  119. sequence.AddElement(
  120. LayerAnimationElement::CreateTransformElement(target_transform, delta));
  121. for (int i = 0; i < 2; ++i) {
  122. int starting_group_id = 1;
  123. sequence.set_animation_group_id(starting_group_id);
  124. start_time = opacity_effective_start + 4 * delta;
  125. sequence.set_start_time(start_time);
  126. delegate.SetOpacityFromAnimation(start_opacity,
  127. PropertyChangeReason::NOT_FROM_ANIMATION);
  128. delegate.SetTransformFromAnimation(
  129. start_transform, PropertyChangeReason::NOT_FROM_ANIMATION);
  130. sequence.Start(&delegate);
  131. sequence.Progress(start_time, &delegate);
  132. EXPECT_FLOAT_EQ(0.0, sequence.last_progressed_fraction());
  133. opacity_effective_start = start_time + delta;
  134. EXPECT_EQ(starting_group_id, sequence.animation_group_id());
  135. sequence.OnThreadedAnimationStarted(opacity_effective_start,
  136. cc::TargetProperty::OPACITY,
  137. sequence.animation_group_id());
  138. sequence.Progress(opacity_effective_start + delta/2, &delegate);
  139. EXPECT_FLOAT_EQ(0.5, sequence.last_progressed_fraction());
  140. sequence.Progress(opacity_effective_start + delta, &delegate);
  141. EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation());
  142. // Now at the start of the pause.
  143. EXPECT_FLOAT_EQ(0.0, sequence.last_progressed_fraction());
  144. TestLayerAnimationDelegate copy = delegate;
  145. // In the middle of the pause -- nothing should have changed.
  146. sequence.Progress(opacity_effective_start + delta + delta/2,
  147. &delegate);
  148. CheckApproximatelyEqual(delegate.GetBoundsForAnimation(),
  149. copy.GetBoundsForAnimation());
  150. CheckApproximatelyEqual(delegate.GetTransformForAnimation(),
  151. copy.GetTransformForAnimation());
  152. EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(),
  153. copy.GetOpacityForAnimation());
  154. sequence.Progress(opacity_effective_start + 2 * delta, &delegate);
  155. CheckApproximatelyEqual(start_transform,
  156. delegate.GetTransformForAnimation());
  157. EXPECT_FLOAT_EQ(0.0, sequence.last_progressed_fraction());
  158. transform_effective_start = opacity_effective_start + 3 * delta;
  159. EXPECT_NE(starting_group_id, sequence.animation_group_id());
  160. sequence.OnThreadedAnimationStarted(transform_effective_start,
  161. cc::TargetProperty::TRANSFORM,
  162. sequence.animation_group_id());
  163. sequence.Progress(transform_effective_start + delta/2, &delegate);
  164. EXPECT_FLOAT_EQ(0.5, sequence.last_progressed_fraction());
  165. EXPECT_TRUE(sequence.IsFinished(transform_effective_start + delta));
  166. sequence.Progress(transform_effective_start + delta, &delegate);
  167. CheckApproximatelyEqual(target_transform,
  168. delegate.GetTransformForAnimation());
  169. }
  170. EXPECT_EQ(
  171. static_cast<LayerAnimationElement::AnimatableProperties>(
  172. LayerAnimationElement::OPACITY | LayerAnimationElement::TRANSFORM |
  173. LayerAnimationElement::BOUNDS),
  174. sequence.properties());
  175. }
  176. // Check that a sequence can still be aborted if it has repeated many times.
  177. TEST(LayerAnimationSequenceTest, AbortingRepeatingSequence) {
  178. LayerAnimationSequence sequence;
  179. TestLayerAnimationDelegate delegate;
  180. float start_brightness = 0.0f;
  181. float target_brightness = 1.0f;
  182. base::TimeTicks start_time;
  183. base::TimeDelta delta = base::Seconds(1);
  184. sequence.AddElement(
  185. LayerAnimationElement::CreateBrightnessElement(target_brightness, delta));
  186. sequence.AddElement(
  187. LayerAnimationElement::CreateBrightnessElement(start_brightness, delta));
  188. sequence.set_is_repeating(true);
  189. delegate.SetBrightnessFromAnimation(start_brightness,
  190. PropertyChangeReason::NOT_FROM_ANIMATION);
  191. start_time += delta;
  192. sequence.set_start_time(start_time);
  193. sequence.Start(&delegate);
  194. sequence.Progress(start_time + base::Milliseconds(101000), &delegate);
  195. EXPECT_FLOAT_EQ(target_brightness, delegate.GetBrightnessForAnimation());
  196. sequence.Abort(&delegate);
  197. // Should be able to reuse the sequence after aborting.
  198. delegate.SetBrightnessFromAnimation(start_brightness,
  199. PropertyChangeReason::NOT_FROM_ANIMATION);
  200. start_time += base::Milliseconds(101000);
  201. sequence.set_start_time(start_time);
  202. sequence.Progress(start_time + base::Milliseconds(100000), &delegate);
  203. EXPECT_FLOAT_EQ(start_brightness, delegate.GetBrightnessForAnimation());
  204. }
  205. // Check that a sequence can be 'fast-forwarded' to the end and the target set.
  206. // Also check that this has no effect if the sequence is repeating.
  207. TEST(LayerAnimationSequenceTest, SetTarget) {
  208. LayerAnimationSequence sequence;
  209. TestLayerAnimationDelegate delegate;
  210. float start_opacity = 0.0f;
  211. float target_opacity = 1.0f;
  212. base::TimeDelta delta = base::Seconds(1);
  213. sequence.AddElement(
  214. LayerAnimationElement::CreateOpacityElement(target_opacity, delta));
  215. LayerAnimationElement::TargetValue target_value(&delegate);
  216. target_value.opacity = start_opacity;
  217. sequence.GetTargetValue(&target_value);
  218. EXPECT_FLOAT_EQ(target_opacity, target_value.opacity);
  219. sequence.set_is_repeating(true);
  220. target_value.opacity = start_opacity;
  221. sequence.GetTargetValue(&target_value);
  222. EXPECT_FLOAT_EQ(start_opacity, target_value.opacity);
  223. }
  224. TEST(LayerAnimationSequenceTest, AddObserver) {
  225. base::TimeTicks start_time;
  226. base::TimeDelta delta = base::Seconds(1);
  227. LayerAnimationSequence sequence;
  228. sequence.AddElement(
  229. LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
  230. for (int i = 0; i < 2; ++i) {
  231. start_time += delta;
  232. sequence.set_start_time(start_time);
  233. TestLayerAnimationObserver observer;
  234. TestLayerAnimationDelegate delegate;
  235. sequence.AddObserver(&observer);
  236. EXPECT_TRUE(!observer.last_ended_sequence());
  237. sequence.Progress(start_time + delta, &delegate);
  238. EXPECT_EQ(observer.last_ended_sequence(), &sequence);
  239. sequence.RemoveObserver(&observer);
  240. }
  241. }
  242. TEST(LayerAnimationSequenceTest, ToString) {
  243. base::TimeDelta delta = base::Seconds(1);
  244. LayerAnimationSequence sequence;
  245. EXPECT_EQ(
  246. "LayerAnimationSequence{size=0, properties=, elements=[], "
  247. "is_repeating=0, "
  248. "group_id=0}",
  249. sequence.ToString());
  250. std::unique_ptr<LayerAnimationElement> brightness =
  251. LayerAnimationElement::CreateBrightnessElement(1.0f, delta);
  252. int brightness_id = brightness->keyframe_model_id();
  253. sequence.AddElement(std::move(brightness));
  254. EXPECT_EQ(
  255. base::StringPrintf(
  256. "LayerAnimationSequence{size=1, properties=BRIGHTNESS, "
  257. "elements=[LayerAnimationElement{name=BrightnessTransition, id=%d, "
  258. "group=0, last_progressed_fraction=0.00}], "
  259. "is_repeating=0, group_id=0}",
  260. brightness_id),
  261. sequence.ToString());
  262. std::unique_ptr<LayerAnimationElement> opacity =
  263. LayerAnimationElement::CreateOpacityElement(1.0f, delta);
  264. int opacity_id = opacity->keyframe_model_id();
  265. sequence.AddElement(std::move(opacity));
  266. sequence.set_is_repeating(true);
  267. sequence.set_animation_group_id(1973);
  268. EXPECT_EQ(
  269. base::StringPrintf(
  270. "LayerAnimationSequence{size=2, properties=OPACITY|BRIGHTNESS, "
  271. "elements=[LayerAnimationElement{name=BrightnessTransition, id=%d, "
  272. "group=0, last_progressed_fraction=0.00}, "
  273. "LayerAnimationElement{name=ThreadedOpacityTransition, id=%d, "
  274. "group=0, "
  275. "last_progressed_fraction=0.00}], is_repeating=1, "
  276. "group_id=1973}",
  277. brightness_id, opacity_id),
  278. sequence.ToString());
  279. }
  280. } // namespace
  281. } // namespace ui