animation_builder_unittest.cc 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. // Copyright 2021 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/views/animation/animation_builder.h"
  5. #include "base/bind.h"
  6. #include "base/test/gtest_util.h"
  7. #include "base/time/time.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. #include "ui/compositor/layer.h"
  11. #include "ui/compositor/layer_animator.h"
  12. #include "ui/compositor/layer_owner.h"
  13. #include "ui/compositor/property_change_reason.h"
  14. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  15. #include "ui/compositor/test/layer_animator_test_controller.h"
  16. #include "ui/compositor/test/test_layer_animation_delegate.h"
  17. #include "ui/gfx/geometry/rounded_corners_f.h"
  18. #include "ui/views/animation/animation_abort_handle.h"
  19. namespace views {
  20. namespace {
  21. class TestAnimatibleLayerOwner : public ui::LayerOwner {
  22. public:
  23. TestAnimatibleLayerOwner() : ui::LayerOwner(std::make_unique<ui::Layer>()) {
  24. layer()->GetAnimator()->set_disable_timer_for_test(true);
  25. layer()->GetAnimator()->SetDelegate(&delegate_);
  26. }
  27. ui::LayerAnimationDelegate* delegate() { return &delegate_; }
  28. private:
  29. ui::TestLayerAnimationDelegate delegate_;
  30. };
  31. // Configures the layer animation on `layer_owner` and returns the builder.
  32. AnimationBuilder BuildLayerOpacityAnimationAndReturnBuilder(
  33. ui::LayerOwner* layer_owner,
  34. const base::TimeDelta& duration) {
  35. EXPECT_NE(0.f, layer_owner->layer()->opacity());
  36. AnimationBuilder builder;
  37. builder.Once().SetDuration(duration).SetOpacity(layer_owner, 0.f);
  38. return builder;
  39. }
  40. } // namespace
  41. class AnimationBuilderTest : public testing::Test {
  42. public:
  43. AnimationBuilderTest() = default;
  44. TestAnimatibleLayerOwner* CreateTestLayerOwner() {
  45. layer_owners_.push_back(std::make_unique<TestAnimatibleLayerOwner>());
  46. animator_controllers_.push_back(
  47. std::make_unique<ui::LayerAnimatorTestController>(
  48. layer_owners_.back()->layer()->GetAnimator()));
  49. return layer_owners_.back().get();
  50. }
  51. void Step(const base::TimeDelta& duration) {
  52. DCHECK_GT(duration, base::TimeDelta());
  53. for (const auto& controller : animator_controllers_) {
  54. controller->StartThreadedAnimationsIfNeeded(
  55. controller->animator()->last_step_time());
  56. controller->Step(duration);
  57. }
  58. elapsed_ += duration;
  59. }
  60. protected:
  61. void SetUp() override {
  62. testing::Test::SetUp();
  63. AnimationBuilder::SetObserverDeletedCallbackForTesting(base::BindRepeating(
  64. [](int* deleted_count) { ++(*deleted_count); }, &deleted_observers_));
  65. }
  66. void TearDown() override {
  67. testing::Test::TearDown();
  68. // Delete the layer owners and animator controllers here to ensure any
  69. // lingering animations are aborted and all the observers are destroyed.
  70. layer_owners_.clear();
  71. animator_controllers_.clear();
  72. AnimationBuilder::SetObserverDeletedCallbackForTesting(
  73. base::NullCallback());
  74. if (expected_observers_deleted_)
  75. EXPECT_EQ(expected_observers_deleted_.value(), deleted_observers_);
  76. }
  77. // Call this function to also ensure any implicitly created observers have
  78. // also been properly cleaned up. One observer is created per
  79. // AnimationSequenceBlock which sets callbacks.
  80. void set_expected_observers_deleted(int expected_observers_deleted) {
  81. expected_observers_deleted_ = expected_observers_deleted;
  82. }
  83. private:
  84. std::vector<std::unique_ptr<TestAnimatibleLayerOwner>> layer_owners_;
  85. std::vector<std::unique_ptr<ui::LayerAnimatorTestController>>
  86. animator_controllers_;
  87. base::TimeDelta elapsed_;
  88. absl::optional<int> expected_observers_deleted_;
  89. int deleted_observers_ = 0;
  90. };
  91. // This test builds two animation sequences and checks that the properties are
  92. // animated in the specified durations.
  93. TEST_F(AnimationBuilderTest, SimpleAnimation) {
  94. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  95. TestAnimatibleLayerOwner* second_animating_view = CreateTestLayerOwner();
  96. ui::LayerAnimationDelegate* first_delegate = first_animating_view->delegate();
  97. ui::LayerAnimationDelegate* second_delegate =
  98. second_animating_view->delegate();
  99. gfx::RoundedCornersF rounded_corners(12.0f, 12.0f, 12.0f, 12.0f);
  100. constexpr auto kDelay = base::Seconds(3);
  101. {
  102. AnimationBuilder()
  103. .Once()
  104. .SetDuration(kDelay)
  105. .SetOpacity(first_animating_view, 0.4f)
  106. .SetRoundedCorners(first_animating_view, rounded_corners)
  107. .Offset(base::TimeDelta())
  108. .SetDuration(kDelay * 2)
  109. .SetOpacity(second_animating_view, 0.9f);
  110. }
  111. // Original value before the animation steps.
  112. EXPECT_TRUE(first_animating_view->layer()->GetAnimator()->is_animating());
  113. EXPECT_TRUE(second_animating_view->layer()->GetAnimator()->is_animating());
  114. EXPECT_FLOAT_EQ(first_delegate->GetOpacityForAnimation(), 1.0);
  115. EXPECT_FLOAT_EQ(first_delegate->GetRoundedCornersForAnimation().upper_left(),
  116. 0.0);
  117. EXPECT_FLOAT_EQ(second_delegate->GetOpacityForAnimation(), 1.0);
  118. Step(kDelay);
  119. EXPECT_FLOAT_EQ(first_delegate->GetOpacityForAnimation(), 0.4f);
  120. // Sanity check one of the corners.
  121. EXPECT_FLOAT_EQ(first_delegate->GetRoundedCornersForAnimation().upper_left(),
  122. 12.0f);
  123. // This animation should not be finished yet.
  124. EXPECT_NE(second_delegate->GetOpacityForAnimation(), 0.9f);
  125. Step(kDelay);
  126. EXPECT_FLOAT_EQ(second_delegate->GetOpacityForAnimation(), 0.9f);
  127. }
  128. // This test checks that after setting the animation duration scale to be larger
  129. // than 1, animations behave as expected of that scale.
  130. TEST_F(AnimationBuilderTest, ModifiedSlowAnimationDuration) {
  131. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  132. ui::ScopedAnimationDurationScaleMode::SLOW_DURATION);
  133. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  134. TestAnimatibleLayerOwner* second_animating_view = CreateTestLayerOwner();
  135. ui::LayerAnimationDelegate* first_delegate = first_animating_view->delegate();
  136. ui::LayerAnimationDelegate* second_delegate =
  137. second_animating_view->delegate();
  138. gfx::RoundedCornersF rounded_corners(12.0f, 12.0f, 12.0f, 12.0f);
  139. constexpr auto kDelay = base::Seconds(3);
  140. {
  141. AnimationBuilder()
  142. .Once()
  143. .SetDuration(kDelay)
  144. .SetOpacity(first_animating_view, 0.4f)
  145. .SetRoundedCorners(first_animating_view, rounded_corners)
  146. .Offset(base::TimeDelta())
  147. .SetDuration(kDelay * 2)
  148. .SetOpacity(second_animating_view, 0.9f)
  149. .Then()
  150. .SetDuration(kDelay)
  151. .Then()
  152. .SetDuration(kDelay)
  153. .SetOpacity(second_animating_view, 0.4f);
  154. }
  155. Step(kDelay * ui::ScopedAnimationDurationScaleMode::SLOW_DURATION);
  156. EXPECT_FLOAT_EQ(first_delegate->GetOpacityForAnimation(), 0.4f);
  157. // Sanity check one of the corners.
  158. EXPECT_FLOAT_EQ(first_delegate->GetRoundedCornersForAnimation().upper_left(),
  159. 12.0f);
  160. // This animation should not be finished yet.
  161. EXPECT_NE(second_delegate->GetOpacityForAnimation(), 0.9f);
  162. Step(kDelay * ui::ScopedAnimationDurationScaleMode::SLOW_DURATION);
  163. EXPECT_FLOAT_EQ(second_delegate->GetOpacityForAnimation(), 0.9f);
  164. Step(kDelay * 2 * ui::ScopedAnimationDurationScaleMode::SLOW_DURATION);
  165. EXPECT_FLOAT_EQ(second_delegate->GetOpacityForAnimation(), 0.4f);
  166. }
  167. // This test checks that after setting the animation duration scale to be
  168. // between 0 and 1, animations behave as expected of that scale.
  169. TEST_F(AnimationBuilderTest, ModifiedFastAnimationDuration) {
  170. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  171. ui::ScopedAnimationDurationScaleMode::FAST_DURATION);
  172. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  173. TestAnimatibleLayerOwner* second_animating_view = CreateTestLayerOwner();
  174. ui::LayerAnimationDelegate* first_delegate = first_animating_view->delegate();
  175. ui::LayerAnimationDelegate* second_delegate =
  176. second_animating_view->delegate();
  177. gfx::RoundedCornersF rounded_corners(12.0f, 12.0f, 12.0f, 12.0f);
  178. constexpr auto kDelay = base::Seconds(3);
  179. {
  180. AnimationBuilder()
  181. .Once()
  182. .SetDuration(kDelay)
  183. .SetOpacity(first_animating_view, 0.4f)
  184. .SetRoundedCorners(first_animating_view, rounded_corners)
  185. .Offset(base::TimeDelta())
  186. .SetDuration(kDelay * 2)
  187. .SetOpacity(second_animating_view, 0.9f)
  188. .Then()
  189. .SetDuration(kDelay)
  190. .Then()
  191. .SetDuration(kDelay)
  192. .SetOpacity(second_animating_view, 0.4f);
  193. }
  194. Step(kDelay * ui::ScopedAnimationDurationScaleMode::FAST_DURATION);
  195. EXPECT_FLOAT_EQ(first_delegate->GetOpacityForAnimation(), 0.4f);
  196. // Sanity check one of the corners.
  197. EXPECT_FLOAT_EQ(first_delegate->GetRoundedCornersForAnimation().upper_left(),
  198. 12.0f);
  199. // This animation should not be finished yet.
  200. EXPECT_NE(second_delegate->GetOpacityForAnimation(), 0.9f);
  201. Step(kDelay * ui::ScopedAnimationDurationScaleMode::FAST_DURATION);
  202. EXPECT_FLOAT_EQ(second_delegate->GetOpacityForAnimation(), 0.9f);
  203. Step(kDelay * 2 * ui::ScopedAnimationDurationScaleMode::FAST_DURATION);
  204. EXPECT_FLOAT_EQ(second_delegate->GetOpacityForAnimation(), 0.4f);
  205. }
  206. // This test checks that after setting the animation duration scale to be 0,
  207. // animations behave as expected of that scale.
  208. TEST_F(AnimationBuilderTest, ModifiedZeroAnimationDuration) {
  209. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  210. ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
  211. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  212. TestAnimatibleLayerOwner* second_animating_view = CreateTestLayerOwner();
  213. ui::LayerAnimationDelegate* first_delegate = first_animating_view->delegate();
  214. ui::LayerAnimationDelegate* second_delegate =
  215. second_animating_view->delegate();
  216. gfx::RoundedCornersF rounded_corners(12.0f, 12.0f, 12.0f, 12.0f);
  217. constexpr auto kDelay = base::Seconds(3);
  218. {
  219. AnimationBuilder()
  220. .Once()
  221. .SetDuration(kDelay)
  222. .SetOpacity(first_animating_view, 0.4f)
  223. .SetRoundedCorners(first_animating_view, rounded_corners)
  224. .Offset(base::TimeDelta())
  225. .SetDuration(kDelay * 2)
  226. .SetOpacity(second_animating_view, 0.9f)
  227. .Then()
  228. .SetDuration(kDelay)
  229. .Then()
  230. .SetDuration(kDelay)
  231. .SetOpacity(second_animating_view, 0.4f);
  232. }
  233. EXPECT_FLOAT_EQ(first_delegate->GetOpacityForAnimation(), 0.4f);
  234. // Sanity check one of the corners.
  235. EXPECT_FLOAT_EQ(first_delegate->GetRoundedCornersForAnimation().upper_left(),
  236. 12.0f);
  237. EXPECT_FLOAT_EQ(second_delegate->GetOpacityForAnimation(), 0.4f);
  238. }
  239. // This test checks that the callback supplied to .OnEnded is not called before
  240. // all sequences have finished running. This test will crash if .OnEnded is
  241. // called prematurely.
  242. TEST_F(AnimationBuilderTest, ModifiedZeroAnimationDurationWithOnEndedCallback) {
  243. ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
  244. ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
  245. auto first_animating_view = std::make_unique<TestAnimatibleLayerOwner>();
  246. auto second_animating_view = std::make_unique<TestAnimatibleLayerOwner>();
  247. views::AnimationBuilder b;
  248. b.OnEnded(base::BindRepeating(
  249. [](TestAnimatibleLayerOwner* layer_owner,
  250. TestAnimatibleLayerOwner* second_layer_owner) {
  251. delete layer_owner;
  252. delete second_layer_owner;
  253. },
  254. first_animating_view.get(), second_animating_view.get()))
  255. .Once()
  256. .SetDuration(base::Seconds(3))
  257. .SetOpacity(first_animating_view.get(), 0.4f)
  258. .SetOpacity(second_animating_view.get(), 0.9f);
  259. first_animating_view.release();
  260. second_animating_view.release();
  261. }
  262. TEST_F(AnimationBuilderTest, ZeroDurationBlock) {
  263. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  264. ui::LayerAnimationDelegate* first_delegate = first_animating_view->delegate();
  265. gfx::RoundedCornersF first_corners(6.0f, 6.0f, 6.0f, 6.0f);
  266. gfx::RoundedCornersF second_corners(12.0f, 12.0f, 12.0f, 12.0f);
  267. constexpr auto kDelay = base::Seconds(3);
  268. {
  269. AnimationBuilder()
  270. .Once()
  271. .SetDuration(base::TimeDelta())
  272. .SetRoundedCorners(first_animating_view, first_corners)
  273. .Then()
  274. .SetDuration(kDelay)
  275. .SetRoundedCorners(first_animating_view, second_corners)
  276. .Then()
  277. .SetDuration(base::TimeDelta())
  278. .SetRoundedCorners(first_animating_view, first_corners);
  279. }
  280. EXPECT_FLOAT_EQ(first_delegate->GetRoundedCornersForAnimation().upper_left(),
  281. 6.0f);
  282. Step(kDelay / 2);
  283. EXPECT_FLOAT_EQ(first_delegate->GetRoundedCornersForAnimation().upper_left(),
  284. 9.0f);
  285. Step(kDelay / 2);
  286. EXPECT_FLOAT_EQ(first_delegate->GetRoundedCornersForAnimation().upper_left(),
  287. 6.0f);
  288. }
  289. TEST_F(AnimationBuilderTest, CheckTweenType) {
  290. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  291. gfx::Tween::Type tween_type = gfx::Tween::EASE_IN;
  292. constexpr auto kDelay = base::Seconds(4);
  293. // Set initial opacity.
  294. first_animating_view->delegate()->SetOpacityFromAnimation(
  295. 0.0f, ui::PropertyChangeReason::NOT_FROM_ANIMATION);
  296. constexpr float opacity_end_val = 0.5f;
  297. {
  298. AnimationBuilder().Once().SetDuration(kDelay).SetOpacity(
  299. first_animating_view, opacity_end_val, tween_type);
  300. }
  301. EXPECT_TRUE(first_animating_view->layer()->GetAnimator()->is_animating());
  302. Step(kDelay / 2);
  303. // Force an update to the delegate by aborting the animation.
  304. first_animating_view->layer()->GetAnimator()->AbortAllAnimations();
  305. // Values at intermediate steps may not be exact.
  306. EXPECT_NEAR(gfx::Tween::CalculateValue(tween_type, 0.5) * opacity_end_val,
  307. first_animating_view->delegate()->GetOpacityForAnimation(),
  308. 0.001f);
  309. }
  310. // Verify that destroying the layers tracked by the animation abort handle
  311. // before the animation ends should not cause any crash.
  312. TEST_F(AnimationBuilderTest, DestroyLayerBeforeAnimationEnd) {
  313. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  314. TestAnimatibleLayerOwner* second_animating_view = CreateTestLayerOwner();
  315. std::unique_ptr<AnimationAbortHandle> abort_handle;
  316. {
  317. AnimationBuilder builder;
  318. abort_handle = builder.GetAbortHandle();
  319. builder.Once()
  320. .SetDuration(base::Seconds(3))
  321. .SetOpacity(first_animating_view, 0.5f)
  322. .SetOpacity(second_animating_view, 0.5f);
  323. }
  324. EXPECT_TRUE(first_animating_view->layer()->GetAnimator()->is_animating());
  325. EXPECT_TRUE(second_animating_view->layer()->GetAnimator()->is_animating());
  326. first_animating_view->ReleaseLayer();
  327. second_animating_view->ReleaseLayer();
  328. }
  329. // Verify that destroying layers tracked by the animation abort handle when
  330. // the animation ends should not cause any crash.
  331. TEST_F(AnimationBuilderTest, DestroyLayerWhenAnimationEnd) {
  332. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  333. TestAnimatibleLayerOwner* second_animating_view = CreateTestLayerOwner();
  334. auto end_callback = [](TestAnimatibleLayerOwner* first_animating_view,
  335. TestAnimatibleLayerOwner* second_animating_view) {
  336. first_animating_view->ReleaseLayer();
  337. second_animating_view->ReleaseLayer();
  338. };
  339. constexpr auto kDelay = base::Seconds(3);
  340. std::unique_ptr<AnimationAbortHandle> abort_handle;
  341. {
  342. AnimationBuilder builder;
  343. abort_handle = builder.GetAbortHandle();
  344. builder
  345. .OnEnded(base::BindOnce(end_callback, first_animating_view,
  346. second_animating_view))
  347. .Once()
  348. .SetDuration(kDelay)
  349. .SetOpacity(first_animating_view, 0.5f)
  350. .SetOpacity(second_animating_view, 0.5f);
  351. }
  352. EXPECT_TRUE(first_animating_view->layer()->GetAnimator()->is_animating());
  353. EXPECT_TRUE(second_animating_view->layer()->GetAnimator()->is_animating());
  354. Step(kDelay * 2);
  355. // Verify that layers are destroyed when the animation ends.
  356. EXPECT_FALSE(first_animating_view->layer());
  357. EXPECT_FALSE(second_animating_view->layer());
  358. }
  359. // Verify that destroying layers tracked by the animation abort handle when
  360. // the animation is aborted should not cause any crash.
  361. TEST_F(AnimationBuilderTest, DestroyLayerWhenAnimationAborted) {
  362. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  363. TestAnimatibleLayerOwner* second_animating_view = CreateTestLayerOwner();
  364. auto abort_callback = [](TestAnimatibleLayerOwner* first_animating_view,
  365. TestAnimatibleLayerOwner* second_animating_view) {
  366. first_animating_view->ReleaseLayer();
  367. second_animating_view->ReleaseLayer();
  368. };
  369. constexpr auto kDelay = base::Seconds(3);
  370. std::unique_ptr<AnimationAbortHandle> abort_handle;
  371. {
  372. AnimationBuilder builder;
  373. abort_handle = builder.GetAbortHandle();
  374. builder
  375. .OnAborted(base::BindOnce(abort_callback, first_animating_view,
  376. second_animating_view))
  377. .Once()
  378. .SetDuration(kDelay)
  379. .SetOpacity(first_animating_view, 0.5f)
  380. .SetOpacity(second_animating_view, 0.5f);
  381. }
  382. Step(0.5 * kDelay);
  383. EXPECT_TRUE(first_animating_view->layer()->GetAnimator()->is_animating());
  384. EXPECT_TRUE(second_animating_view->layer()->GetAnimator()->is_animating());
  385. // Abort the animation in the half way.
  386. first_animating_view->layer()->GetAnimator()->AbortAllAnimations();
  387. // Verify that layers are destroyed by the animation abortion callback.
  388. EXPECT_FALSE(first_animating_view->layer());
  389. EXPECT_FALSE(second_animating_view->layer());
  390. }
  391. TEST_F(AnimationBuilderTest, CheckStartEndCallbacks) {
  392. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  393. TestAnimatibleLayerOwner* second_animating_view = CreateTestLayerOwner();
  394. constexpr auto kDelay = base::Seconds(3);
  395. bool started = false;
  396. bool ended = false;
  397. {
  398. AnimationBuilder()
  399. .OnStarted(
  400. base::BindOnce([](bool* started) { *started = true; }, &started))
  401. .OnEnded(base::BindOnce([](bool* ended) { *ended = true; }, &ended))
  402. .Once()
  403. .SetDuration(kDelay)
  404. .SetOpacity(first_animating_view, 0.4f)
  405. .Offset(base::TimeDelta())
  406. .SetDuration(kDelay * 2)
  407. .SetOpacity(second_animating_view, 0.9f)
  408. .Then()
  409. .SetDuration(kDelay)
  410. .SetOpacity(second_animating_view, 0.4f);
  411. }
  412. // Only one Observer should have been created in the above block. Make sure
  413. // it has been cleaned up.
  414. set_expected_observers_deleted(1);
  415. EXPECT_TRUE(first_animating_view->layer()->GetAnimator()->is_animating());
  416. EXPECT_TRUE(started);
  417. Step(kDelay * 2);
  418. EXPECT_FALSE(ended);
  419. Step(kDelay);
  420. EXPECT_TRUE(ended);
  421. }
  422. // This test checks that repeat callbacks are called after each sequence
  423. // repetition and callbacks from one sequence do not affect calls from another
  424. // sequence.
  425. TEST_F(AnimationBuilderTest, CheckOnWillRepeatCallbacks) {
  426. int first_repeat_count = 0;
  427. int second_repeat_count = 0;
  428. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  429. constexpr auto kDelay = base::Seconds(3);
  430. gfx::RoundedCornersF first_rounded_corners(12.0f, 12.0f, 12.0f, 12.0f);
  431. gfx::RoundedCornersF second_rounded_corners(5.0f, 5.0f, 5.0f, 5.0f);
  432. {
  433. AnimationBuilder b;
  434. b.OnWillRepeat(base::BindRepeating([](int& repeat) { repeat = repeat + 1; },
  435. std::ref(first_repeat_count)))
  436. .Repeatedly()
  437. .SetDuration(kDelay)
  438. .SetOpacity(first_animating_view, 0.4f)
  439. .Then()
  440. .SetDuration(kDelay)
  441. .SetOpacity(first_animating_view, 0.9f);
  442. b.OnWillRepeat(base::BindRepeating([](int& repeat) { repeat = repeat + 1; },
  443. std::ref(second_repeat_count)))
  444. .Repeatedly()
  445. .SetDuration(kDelay)
  446. .SetRoundedCorners(first_animating_view, first_rounded_corners)
  447. .Then()
  448. .SetDuration(kDelay)
  449. .SetRoundedCorners(first_animating_view, second_rounded_corners);
  450. }
  451. set_expected_observers_deleted(2);
  452. Step(kDelay * 2);
  453. EXPECT_EQ(first_repeat_count, 1);
  454. EXPECT_EQ(second_repeat_count, 1);
  455. Step(kDelay * 2);
  456. EXPECT_EQ(first_repeat_count, 2);
  457. EXPECT_EQ(second_repeat_count, 2);
  458. }
  459. // We use these notations to illustrate the tested timeline,
  460. // Pause: ---|
  461. // KeyFrame: -->|
  462. // Repeat: [...]
  463. //
  464. // Opacity ---|-->|
  465. TEST_F(AnimationBuilderTest, DelayedStart) {
  466. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  467. ui::LayerAnimationDelegate* delegate = view->delegate();
  468. constexpr auto kDelay = base::Seconds(1);
  469. constexpr auto kDuration = base::Seconds(1);
  470. {
  471. // clang-format off
  472. AnimationBuilder()
  473. .Once()
  474. .At(kDelay)
  475. .SetDuration(kDuration)
  476. .SetOpacity(view, 0.4f);
  477. // clang-format on
  478. }
  479. Step(kDelay);
  480. // The animation on opacity is not yet started.
  481. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 1.0);
  482. Step(kDuration);
  483. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  484. }
  485. // Opacity -->|-->|
  486. TEST_F(AnimationBuilderTest, TwoKeyFrame) {
  487. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  488. ui::LayerAnimationDelegate* delegate = view->delegate();
  489. constexpr auto kDuration = base::Seconds(1);
  490. {
  491. AnimationBuilder()
  492. .Once()
  493. .SetDuration(kDuration)
  494. .SetOpacity(view, 0.4f)
  495. .Then()
  496. .SetDuration(kDuration)
  497. .SetOpacity(view, 0.9f);
  498. }
  499. Step(kDuration);
  500. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  501. Step(kDuration);
  502. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  503. }
  504. // Opacity -->|---|-->|
  505. TEST_F(AnimationBuilderTest, PauseInTheMiddle) {
  506. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  507. ui::LayerAnimationDelegate* delegate = view->delegate();
  508. constexpr auto kDuration = base::Seconds(1);
  509. {
  510. AnimationBuilder()
  511. .Once()
  512. .SetDuration(kDuration)
  513. .SetOpacity(view, 0.4f)
  514. .Then()
  515. .Offset(kDuration)
  516. .SetDuration(kDuration)
  517. .SetOpacity(view, 0.9f);
  518. }
  519. Step(kDuration);
  520. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  521. Step(kDuration);
  522. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  523. Step(kDuration);
  524. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  525. }
  526. // Opacity -->|
  527. // RoundedCorners ----->|
  528. TEST_F(AnimationBuilderTest, TwoPropertiesOfDifferentDuration) {
  529. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  530. ui::LayerAnimationDelegate* delegate = view->delegate();
  531. gfx::RoundedCornersF rounded_corners(12.0f, 12.0f, 12.0f, 12.0f);
  532. // Make sure that the opacity keyframe finishes at the middle of the rounded
  533. // corners keyframe.
  534. constexpr auto kDurationShort = base::Seconds(1);
  535. constexpr auto kDurationLong = kDurationShort * 2;
  536. {
  537. AnimationBuilder()
  538. .Once()
  539. .SetDuration(kDurationShort)
  540. .SetOpacity(view, 0.4f)
  541. .At(base::TimeDelta())
  542. .SetDuration(kDurationLong)
  543. .SetRoundedCorners(view, rounded_corners);
  544. }
  545. Step(kDurationShort);
  546. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  547. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 6.0f);
  548. Step(kDurationLong - kDurationShort);
  549. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  550. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(),
  551. 12.0f);
  552. }
  553. // Opacity ----->|
  554. // RoundedCorners ----->|
  555. TEST_F(AnimationBuilderTest, TwoPropertiesOfDifferentStartTime) {
  556. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  557. ui::LayerAnimationDelegate* delegate = view->delegate();
  558. gfx::RoundedCornersF rounded_corners(12.0f, 12.0f, 12.0f, 12.0f);
  559. // Make sure that the opacity keyframe finishes at the middle of the rounded
  560. // corners keyframe.
  561. constexpr auto kDelay = base::Seconds(1);
  562. constexpr auto kDuration = kDelay * 2;
  563. {
  564. AnimationBuilder()
  565. .Once()
  566. .SetDuration(kDuration)
  567. .SetOpacity(view, 0.4f)
  568. .At(kDelay)
  569. .SetDuration(kDuration)
  570. .SetRoundedCorners(view, rounded_corners);
  571. }
  572. Step(kDelay);
  573. // Unfortunately, we can't test threaded animations in the midst of a frame
  574. // because they don't update LayerAnimationDelegate in OnProgress().
  575. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 0.0);
  576. Step(kDuration - kDelay);
  577. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  578. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 6.0f);
  579. Step(kDelay);
  580. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  581. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(),
  582. 12.0f);
  583. }
  584. // Opacity ----->|---|-->|
  585. // RoundedCorners ----->|-->|
  586. TEST_F(AnimationBuilderTest, ThenAddsImplicitPause) {
  587. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  588. ui::LayerAnimationDelegate* delegate = view->delegate();
  589. gfx::RoundedCornersF rounded_corners1(12.0f, 12.0f, 12.0f, 12.0f);
  590. gfx::RoundedCornersF rounded_corners2(5.0f, 5.0f, 5.0f, 5.0f);
  591. // Make sure that the first opacity keyframe finishes at the middle of the
  592. // first rounded corners keyframe.
  593. constexpr auto kDelay = base::Seconds(1);
  594. constexpr auto kDuration = kDelay * 2;
  595. {
  596. AnimationBuilder()
  597. .Once()
  598. .SetDuration(kDuration)
  599. .SetOpacity(view, 0.4f)
  600. .At(kDelay)
  601. .SetDuration(kDuration)
  602. .SetRoundedCorners(view, rounded_corners1)
  603. .Then()
  604. .SetDuration(kDuration)
  605. .SetOpacity(view, 0.9f)
  606. .SetRoundedCorners(view, rounded_corners2);
  607. }
  608. Step(kDelay);
  609. // Unfortunately, we can't test threaded animations in the midst of a frame
  610. // because they don't update LayerAnimationDelegate in OnProgress().
  611. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 0.0);
  612. Step(kDuration - kDelay);
  613. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  614. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 6.0f);
  615. Step(kDelay);
  616. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  617. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(),
  618. 12.0f);
  619. Step(kDuration);
  620. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  621. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 5.0f);
  622. }
  623. // Opacity [-->|-->]
  624. TEST_F(AnimationBuilderTest, Repeat) {
  625. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  626. ui::LayerAnimationDelegate* delegate = view->delegate();
  627. constexpr auto kDuration = base::Seconds(1);
  628. {
  629. AnimationBuilder()
  630. .Repeatedly()
  631. .SetDuration(kDuration)
  632. .SetOpacity(view, 0.4f)
  633. .Then()
  634. .SetDuration(kDuration)
  635. .SetOpacity(view, 0.9f);
  636. }
  637. Step(kDuration);
  638. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  639. Step(kDuration);
  640. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  641. Step(kDuration);
  642. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  643. Step(kDuration);
  644. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  645. }
  646. // Opacity [-->|-->| ]
  647. TEST_F(AnimationBuilderTest, RepeatWithExplicitTrailingPause) {
  648. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  649. ui::LayerAnimationDelegate* delegate = view->delegate();
  650. constexpr auto kDuration = base::Seconds(1);
  651. {
  652. AnimationBuilder()
  653. .Repeatedly()
  654. .SetDuration(kDuration)
  655. .SetOpacity(view, 0.4f)
  656. .Then()
  657. .SetDuration(kDuration)
  658. .SetOpacity(view, 0.9f)
  659. .Then()
  660. .SetDuration(kDuration);
  661. }
  662. Step(kDuration);
  663. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  664. Step(kDuration);
  665. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  666. Step(kDuration);
  667. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  668. Step(kDuration);
  669. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  670. Step(kDuration);
  671. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  672. Step(kDuration);
  673. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  674. }
  675. // Opacity [-->|-->]
  676. // RoundedCorners [-->|-->]
  677. TEST_F(AnimationBuilderTest, RepeatTwoProperties) {
  678. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  679. ui::LayerAnimationDelegate* delegate = view->delegate();
  680. gfx::RoundedCornersF rounded_corners1(12.0f, 12.0f, 12.0f, 12.0f);
  681. gfx::RoundedCornersF rounded_corners2(5.0f, 5.0f, 5.0f, 5.0f);
  682. constexpr auto kDuration = base::Seconds(1);
  683. {
  684. AnimationBuilder()
  685. .Repeatedly()
  686. .SetDuration(kDuration)
  687. .SetOpacity(view, 0.4f)
  688. .SetRoundedCorners(view, rounded_corners1)
  689. .Then()
  690. .SetDuration(kDuration)
  691. .SetOpacity(view, 0.9f)
  692. .SetRoundedCorners(view, rounded_corners2);
  693. }
  694. Step(kDuration);
  695. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  696. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 12.0);
  697. Step(kDuration);
  698. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  699. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 5.0);
  700. Step(kDuration);
  701. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  702. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 12.0);
  703. Step(kDuration);
  704. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  705. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 5.0);
  706. }
  707. // Opacity -->|-->|
  708. // RoundedCorners -->|-->|
  709. TEST_F(AnimationBuilderTest, AtCanSkipThenBlock) {
  710. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  711. ui::LayerAnimationDelegate* delegate = view->delegate();
  712. gfx::RoundedCornersF rounded_corners1(12.0f, 12.0f, 12.0f, 12.0f);
  713. gfx::RoundedCornersF rounded_corners2(4.0f, 4.0f, 4.0f, 4.0f);
  714. // Make sure that the first opacity keyframe finishes at the middle of the
  715. // first rounded corners keyframe.
  716. constexpr auto kDelay = base::Seconds(1);
  717. constexpr auto kDuration = kDelay * 2;
  718. {
  719. AnimationBuilder()
  720. .Once()
  721. .SetDuration(kDuration)
  722. .SetOpacity(view, 0.4f)
  723. .Then()
  724. .SetDuration(kDuration)
  725. .SetOpacity(view, 0.9f)
  726. .At(kDelay)
  727. .SetDuration(kDuration)
  728. .SetRoundedCorners(view, rounded_corners1)
  729. .Then()
  730. .SetDuration(kDuration)
  731. .SetRoundedCorners(view, rounded_corners2);
  732. }
  733. Step(kDelay);
  734. // Unfortunately, we can't test threaded animations in the midst of a frame
  735. // because they don't update LayerAnimationDelegate in OnProgress().
  736. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 0.0);
  737. Step(kDuration - kDelay);
  738. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  739. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 6.0);
  740. Step(kDelay);
  741. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 12.0);
  742. Step(kDuration - kDelay);
  743. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  744. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 8.0);
  745. Step(kDelay);
  746. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  747. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 4.0);
  748. }
  749. // Opacity -->|-->|
  750. // RoundedCorners -->|
  751. TEST_F(AnimationBuilderTest, OffsetCanRewindTime) {
  752. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  753. ui::LayerAnimationDelegate* delegate = view->delegate();
  754. gfx::RoundedCornersF rounded_corners(12.0f, 12.0f, 12.0f, 12.0f);
  755. // Make sure that the first opacity keyframe finishes at the middle of the
  756. // first rounded corners keyframe.
  757. constexpr auto kDelay = base::Seconds(1);
  758. constexpr auto kDuration = kDelay * 2;
  759. {
  760. AnimationBuilder()
  761. .Once()
  762. .SetDuration(kDuration)
  763. .SetOpacity(view, 0.4f)
  764. .Then()
  765. .SetDuration(kDuration)
  766. .SetOpacity(view, 0.9f)
  767. .Offset(kDelay - kDuration)
  768. .SetDuration(kDuration)
  769. .SetRoundedCorners(view, rounded_corners);
  770. }
  771. Step(kDelay);
  772. // Unfortunately, we can't test threaded animations in the midst of a frame
  773. // because they don't update LayerAnimationDelegate in OnProgress().
  774. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 0.0);
  775. Step(kDuration - kDelay);
  776. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  777. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 6.0);
  778. Step(kDelay);
  779. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 12.0);
  780. Step(kDuration - kDelay);
  781. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  782. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 12.0);
  783. }
  784. // Opacity [-->|--> ]
  785. // RoundedCorners [-->|---->]
  786. TEST_F(AnimationBuilderTest, RepeatedlyImplicitlyAppendsTrailingPause) {
  787. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  788. ui::LayerAnimationDelegate* delegate = view->delegate();
  789. gfx::RoundedCornersF rounded_corners1(12.0f, 12.0f, 12.0f, 12.0f);
  790. gfx::RoundedCornersF rounded_corners2(4.0f, 4.0f, 4.0f, 4.0f);
  791. // Make sure that the second opacity keyframe finishes at the middle of the
  792. // second rounded corners keyframe.
  793. constexpr auto kDurationShort = base::Seconds(1);
  794. constexpr auto kDurationLong = kDurationShort * 2;
  795. {
  796. AnimationBuilder()
  797. .Repeatedly()
  798. .SetDuration(kDurationShort)
  799. .SetOpacity(view, 0.4f)
  800. .SetRoundedCorners(view, rounded_corners1)
  801. .Then()
  802. .SetDuration(kDurationShort)
  803. .SetOpacity(view, 0.9f)
  804. .Offset(base::TimeDelta())
  805. .SetDuration(kDurationLong)
  806. .SetRoundedCorners(view, rounded_corners2);
  807. }
  808. Step(kDurationShort);
  809. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  810. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 12.0);
  811. Step(kDurationShort);
  812. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  813. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 8.0);
  814. Step(kDurationLong - kDurationShort);
  815. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  816. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 4.0);
  817. // Repeat
  818. Step(kDurationShort);
  819. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.4f);
  820. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 12.0);
  821. Step(kDurationShort);
  822. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  823. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 8.0);
  824. Step(kDurationLong - kDurationShort);
  825. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.9f);
  826. EXPECT_FLOAT_EQ(delegate->GetRoundedCornersForAnimation().upper_left(), 4.0);
  827. }
  828. // Opacity -->|-->|--> with a loop for setting these blocks.
  829. TEST_F(AnimationBuilderTest, RepeatedBlocks) {
  830. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  831. ui::LayerAnimationDelegate* delegate = view->delegate();
  832. constexpr auto kDuration = base::Seconds(1);
  833. constexpr float kOpacity[] = {0.4f, 0.9f, 0.6f};
  834. {
  835. AnimationBuilder builder;
  836. builder.Repeatedly();
  837. for (const auto& opacity : kOpacity) {
  838. builder.GetCurrentSequence()
  839. .SetDuration(kDuration)
  840. .SetOpacity(view, opacity)
  841. .Then();
  842. }
  843. }
  844. for (const auto& opacity : kOpacity) {
  845. Step(kDuration);
  846. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), opacity);
  847. }
  848. }
  849. TEST_F(AnimationBuilderTest, PreemptionStrategyTest) {
  850. using ps = ui::LayerAnimator::PreemptionStrategy;
  851. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  852. ui::LayerAnimationDelegate* delegate = view->delegate();
  853. constexpr auto kStepSize = base::Seconds(1);
  854. constexpr auto kDuration = base::Seconds(5);
  855. // Set the initial value to animate.
  856. delegate->SetBrightnessFromAnimation(
  857. 1.0f, ui::PropertyChangeReason::NOT_FROM_ANIMATION);
  858. {
  859. AnimationBuilder().Once().SetDuration(kDuration).SetBrightness(view, 0.0f);
  860. }
  861. // The animation hasn't started.
  862. EXPECT_FLOAT_EQ(delegate->GetBrightnessForAnimation(), 1.0f);
  863. // Step the animation, but don't complete it.
  864. Step(kStepSize);
  865. // Make sure the animation is progressing.
  866. EXPECT_FLOAT_EQ(delegate->GetBrightnessForAnimation(), 0.8f);
  867. // Make sure we're still animatiing.
  868. EXPECT_TRUE(view->layer()->GetAnimator()->is_animating());
  869. // Now start a new animation to a different target.
  870. {
  871. AnimationBuilder()
  872. .SetPreemptionStrategy(ps::IMMEDIATELY_SET_NEW_TARGET)
  873. .Once()
  874. .SetDuration(
  875. kStepSize) // We only moved previous animation by kStepSize
  876. .SetBrightness(view, 1.0f);
  877. }
  878. Step(kStepSize);
  879. // The above animation should have been aborted, and set the brightness to the
  880. // new target immediately.
  881. EXPECT_FLOAT_EQ(delegate->GetBrightnessForAnimation(), 1.0f);
  882. EXPECT_FALSE(view->layer()->GetAnimator()->is_animating());
  883. // Start another animation which we'll preemtp to test another strategy.
  884. {
  885. AnimationBuilder().Once().SetDuration(kDuration).SetBrightness(view, 0.0f);
  886. }
  887. // This should start out like the one above.
  888. EXPECT_FLOAT_EQ(delegate->GetBrightnessForAnimation(), 1.0f);
  889. Step(kStepSize);
  890. EXPECT_FLOAT_EQ(delegate->GetBrightnessForAnimation(), 0.8f);
  891. EXPECT_TRUE(view->layer()->GetAnimator()->is_animating());
  892. {
  893. AnimationBuilder()
  894. .SetPreemptionStrategy(ps::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
  895. .Once()
  896. .SetDuration(kStepSize)
  897. .SetBrightness(view, 1.0f);
  898. }
  899. // The new animation should pick up where the last one left off.
  900. EXPECT_FLOAT_EQ(delegate->GetBrightnessForAnimation(), 0.8f);
  901. Step(kStepSize);
  902. // The new animation is in force if it steps toward the new target.
  903. EXPECT_FLOAT_EQ(delegate->GetBrightnessForAnimation(), 1.0f);
  904. // Make sure the animation is fully complete.
  905. Step(kStepSize);
  906. // The animation should be done now.
  907. EXPECT_FALSE(view->layer()->GetAnimator()->is_animating());
  908. }
  909. TEST_F(AnimationBuilderTest, AbortHandle) {
  910. TestAnimatibleLayerOwner* view = CreateTestLayerOwner();
  911. ui::LayerAnimationDelegate* delegate = view->delegate();
  912. std::unique_ptr<AnimationAbortHandle> abort_handle;
  913. constexpr auto kStepSize = base::Seconds(1);
  914. constexpr auto kDuration = kStepSize * 2;
  915. {
  916. delegate->SetBrightnessFromAnimation(
  917. 1.0f, ui::PropertyChangeReason::NOT_FROM_ANIMATION);
  918. delegate->SetOpacityFromAnimation(
  919. 1.0f, ui::PropertyChangeReason::NOT_FROM_ANIMATION);
  920. {
  921. AnimationBuilder b;
  922. abort_handle = b.GetAbortHandle();
  923. b.Once()
  924. .SetDuration(kDuration)
  925. .SetOpacity(view, 0.4f)
  926. .At(base::TimeDelta())
  927. .SetDuration(kDuration)
  928. .SetBrightness(view, 0.4f);
  929. }
  930. Step(kStepSize);
  931. // Destroy abort handle should stop all animations.
  932. abort_handle.reset();
  933. EXPECT_FLOAT_EQ(delegate->GetBrightnessForAnimation(), 0.7f);
  934. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.7f);
  935. Step(kStepSize);
  936. EXPECT_FLOAT_EQ(delegate->GetBrightnessForAnimation(), 0.7f);
  937. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.7f);
  938. }
  939. // The builder crashes if the handle is destroyed before animation starts.
  940. {
  941. delegate->SetBrightnessFromAnimation(
  942. 1.0f, ui::PropertyChangeReason::NOT_FROM_ANIMATION);
  943. delegate->SetOpacityFromAnimation(
  944. 1.0f, ui::PropertyChangeReason::NOT_FROM_ANIMATION);
  945. {
  946. AnimationBuilder b;
  947. abort_handle = b.GetAbortHandle();
  948. b.Once()
  949. .SetDuration(kDuration)
  950. .SetOpacity(view, 0.4f)
  951. .At(base::TimeDelta())
  952. .SetDuration(kDuration)
  953. .SetBrightness(view, 0.4f);
  954. // Early destroy should crash the builder.
  955. EXPECT_DCHECK_DEATH(abort_handle.reset());
  956. }
  957. }
  958. // The handle shouldn't abort animations subsequent to the builder.
  959. {
  960. delegate->SetBrightnessFromAnimation(
  961. 1.0f, ui::PropertyChangeReason::NOT_FROM_ANIMATION);
  962. delegate->SetOpacityFromAnimation(
  963. 1.0f, ui::PropertyChangeReason::NOT_FROM_ANIMATION);
  964. {
  965. AnimationBuilder b;
  966. abort_handle = b.GetAbortHandle();
  967. b.Once()
  968. .SetDuration(kDuration)
  969. .SetOpacity(view, 0.4f)
  970. .At(base::TimeDelta())
  971. .SetDuration(kDuration)
  972. .SetBrightness(view, 0.4f);
  973. }
  974. EXPECT_EQ(abort_handle->animation_state(),
  975. AnimationAbortHandle::AnimationState::kRunning);
  976. // Step to the end of the animation.
  977. Step(kDuration);
  978. EXPECT_EQ(abort_handle->animation_state(),
  979. AnimationAbortHandle::AnimationState::kEnded);
  980. {
  981. AnimationBuilder b;
  982. b.Once()
  983. .SetDuration(kDuration)
  984. .SetOpacity(view, 0.8f)
  985. .At(base::TimeDelta())
  986. .SetDuration(kDuration)
  987. .SetBrightness(view, 0.8f);
  988. }
  989. // Destroy the handle on the finihsed animation shouldn't affect other
  990. // unfinihsed animations.
  991. abort_handle.reset();
  992. Step(kDuration);
  993. EXPECT_FLOAT_EQ(delegate->GetBrightnessForAnimation(), 0.8f);
  994. EXPECT_FLOAT_EQ(delegate->GetOpacityForAnimation(), 0.8f);
  995. }
  996. }
  997. // Verifies that configuring layer animations with an animation builder returned
  998. // from a function works as expected.
  999. TEST_F(AnimationBuilderTest, BuildAnimationWithBuilderFromScope) {
  1000. TestAnimatibleLayerOwner* first_animating_view = CreateTestLayerOwner();
  1001. TestAnimatibleLayerOwner* second_animating_view = CreateTestLayerOwner();
  1002. EXPECT_EQ(1.f, first_animating_view->layer()->opacity());
  1003. EXPECT_EQ(1.f, second_animating_view->layer()->opacity());
  1004. constexpr auto kDuration = base::Seconds(3);
  1005. {
  1006. // Build a layer animation on `second_animating_view` with a builder
  1007. // returned from a function.
  1008. AnimationBuilder builder = BuildLayerOpacityAnimationAndReturnBuilder(
  1009. first_animating_view, kDuration);
  1010. builder.GetCurrentSequence().SetOpacity(second_animating_view, 0.f);
  1011. }
  1012. // Verify that both views are under animation.
  1013. EXPECT_TRUE(first_animating_view->layer()->GetAnimator()->is_animating());
  1014. EXPECT_TRUE(second_animating_view->layer()->GetAnimator()->is_animating());
  1015. Step(kDuration);
  1016. // Verify that after `kDuration` time, both layer animations end. In addition,
  1017. // both layers are set with the target opacity.
  1018. EXPECT_FALSE(first_animating_view->layer()->GetAnimator()->is_animating());
  1019. EXPECT_FALSE(second_animating_view->layer()->GetAnimator()->is_animating());
  1020. EXPECT_EQ(0.f, first_animating_view->delegate()->GetOpacityForAnimation());
  1021. EXPECT_EQ(0.f, second_animating_view->delegate()->GetOpacityForAnimation());
  1022. }
  1023. } // namespace views