callback_layer_animation_observer_unittest.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. // Copyright 2015 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/callback_layer_animation_observer.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "ui/compositor/layer_animation_sequence.h"
  10. #include "ui/compositor/test/layer_animation_observer_test_api.h"
  11. namespace ui {
  12. namespace test {
  13. // Simple class that tracks whether callbacks were invoked and when.
  14. class TestCallbacks {
  15. public:
  16. TestCallbacks();
  17. TestCallbacks(const TestCallbacks&) = delete;
  18. TestCallbacks& operator=(const TestCallbacks&) = delete;
  19. virtual ~TestCallbacks();
  20. void ResetCallbackObservations();
  21. void set_should_delete_observer_on_animations_ended(
  22. bool should_delete_observer_on_animations_ended) {
  23. should_delete_observer_on_animations_ended_ =
  24. should_delete_observer_on_animations_ended;
  25. }
  26. bool animations_started() const { return animations_started_; }
  27. bool animations_ended() const { return animations_ended_; }
  28. virtual void AnimationsStarted(const CallbackLayerAnimationObserver&);
  29. virtual bool AnimationsEnded(const CallbackLayerAnimationObserver&);
  30. testing::AssertionResult StartedEpochIsBeforeEndedEpoch();
  31. private:
  32. // Monotonic counter that tracks the next time snapshot.
  33. int next_epoch_ = 0;
  34. // Is true when AnimationsStarted() has been called.
  35. bool animations_started_ = false;
  36. // Relative time snapshot of when AnimationsStarted() was last called.
  37. int animations_started_epoch_ = -1;
  38. // Is true when AnimationsEnded() has been called.
  39. bool animations_ended_ = false;
  40. // Relative time snapshot of when AnimationsEnded() was last called.
  41. int animations_ended_epoch_ = -1;
  42. // The return value for AnimationsEnded().
  43. bool should_delete_observer_on_animations_ended_ = false;
  44. };
  45. TestCallbacks::TestCallbacks() {}
  46. TestCallbacks::~TestCallbacks() {}
  47. void TestCallbacks::ResetCallbackObservations() {
  48. next_epoch_ = 0;
  49. animations_started_ = false;
  50. animations_started_epoch_ = -1;
  51. animations_ended_ = false;
  52. animations_ended_epoch_ = -1;
  53. should_delete_observer_on_animations_ended_ = false;
  54. }
  55. void TestCallbacks::AnimationsStarted(const CallbackLayerAnimationObserver&) {
  56. animations_started_ = true;
  57. animations_started_epoch_ = next_epoch_++;
  58. }
  59. bool TestCallbacks::AnimationsEnded(const CallbackLayerAnimationObserver&) {
  60. animations_ended_ = true;
  61. animations_ended_epoch_ = next_epoch_++;
  62. return should_delete_observer_on_animations_ended_;
  63. }
  64. testing::AssertionResult TestCallbacks::StartedEpochIsBeforeEndedEpoch() {
  65. if (animations_started_epoch_ < animations_ended_epoch_) {
  66. return testing::AssertionSuccess();
  67. } else {
  68. return testing::AssertionFailure()
  69. << "The started epoch=" << animations_started_epoch_
  70. << " is NOT before the ended epoch=" << animations_ended_epoch_;
  71. }
  72. }
  73. // A child of TestCallbacks that can explicitly delete a
  74. // CallbackLayerAnimationObserver in the AnimationsStarted() or
  75. // AnimationsEnded() callback.
  76. class TestCallbacksThatExplicitlyDeletesObserver : public TestCallbacks {
  77. public:
  78. TestCallbacksThatExplicitlyDeletesObserver();
  79. TestCallbacksThatExplicitlyDeletesObserver(
  80. const TestCallbacksThatExplicitlyDeletesObserver&) = delete;
  81. TestCallbacksThatExplicitlyDeletesObserver& operator=(
  82. const TestCallbacksThatExplicitlyDeletesObserver&) = delete;
  83. void set_observer_to_delete_in_animation_started(
  84. CallbackLayerAnimationObserver* observer) {
  85. observer_to_delete_in_animation_started_ = observer;
  86. }
  87. void set_observer_to_delete_in_animation_ended(
  88. CallbackLayerAnimationObserver* observer) {
  89. observer_to_delete_in_animation_ended_ = observer;
  90. }
  91. // TestCallbacks:
  92. void AnimationsStarted(
  93. const CallbackLayerAnimationObserver& observer) override;
  94. bool AnimationsEnded(const CallbackLayerAnimationObserver& observer) override;
  95. private:
  96. // The observer to delete, if non-NULL, in AnimationsStarted().
  97. raw_ptr<CallbackLayerAnimationObserver>
  98. observer_to_delete_in_animation_started_ = nullptr;
  99. // The observer to delete, if non-NULL, in AnimationsEnded().
  100. raw_ptr<CallbackLayerAnimationObserver>
  101. observer_to_delete_in_animation_ended_ = nullptr;
  102. };
  103. TestCallbacksThatExplicitlyDeletesObserver::
  104. TestCallbacksThatExplicitlyDeletesObserver() {}
  105. void TestCallbacksThatExplicitlyDeletesObserver::AnimationsStarted(
  106. const CallbackLayerAnimationObserver& observer) {
  107. if (observer_to_delete_in_animation_started_)
  108. delete observer_to_delete_in_animation_started_;
  109. TestCallbacks::AnimationsStarted(observer);
  110. }
  111. bool TestCallbacksThatExplicitlyDeletesObserver::AnimationsEnded(
  112. const CallbackLayerAnimationObserver& observer) {
  113. if (observer_to_delete_in_animation_ended_)
  114. delete observer_to_delete_in_animation_ended_;
  115. return TestCallbacks::AnimationsEnded(observer);
  116. }
  117. // A test specific CallbackLayerAnimationObserver that will set a bool when
  118. // destroyed.
  119. class TestCallbackLayerAnimationObserver
  120. : public CallbackLayerAnimationObserver {
  121. public:
  122. TestCallbackLayerAnimationObserver(
  123. AnimationStartedCallback animation_started_callback,
  124. AnimationEndedCallback animation_ended_callback,
  125. bool* destroyed);
  126. TestCallbackLayerAnimationObserver(
  127. AnimationStartedCallback animation_started_callback,
  128. bool should_delete_observer,
  129. bool* destroyed);
  130. TestCallbackLayerAnimationObserver(
  131. AnimationEndedCallback animation_ended_callback,
  132. bool* destroyed);
  133. TestCallbackLayerAnimationObserver(
  134. const TestCallbackLayerAnimationObserver&) = delete;
  135. TestCallbackLayerAnimationObserver& operator=(
  136. const TestCallbackLayerAnimationObserver&) = delete;
  137. ~TestCallbackLayerAnimationObserver() override;
  138. private:
  139. raw_ptr<bool> destroyed_;
  140. };
  141. TestCallbackLayerAnimationObserver::TestCallbackLayerAnimationObserver(
  142. AnimationStartedCallback animation_started_callback,
  143. AnimationEndedCallback animation_ended_callback,
  144. bool* destroyed)
  145. : CallbackLayerAnimationObserver(animation_started_callback,
  146. animation_ended_callback),
  147. destroyed_(destroyed) {
  148. if (destroyed_)
  149. (*destroyed_) = false;
  150. }
  151. TestCallbackLayerAnimationObserver::TestCallbackLayerAnimationObserver(
  152. AnimationStartedCallback animation_started_callback,
  153. bool should_delete_observer,
  154. bool* destroyed)
  155. : CallbackLayerAnimationObserver(animation_started_callback,
  156. should_delete_observer),
  157. destroyed_(destroyed) {
  158. if (destroyed_)
  159. (*destroyed_) = false;
  160. }
  161. TestCallbackLayerAnimationObserver::TestCallbackLayerAnimationObserver(
  162. AnimationEndedCallback animation_ended_callback,
  163. bool* destroyed)
  164. : CallbackLayerAnimationObserver(animation_ended_callback),
  165. destroyed_(destroyed) {
  166. if (destroyed_)
  167. (*destroyed_) = false;
  168. }
  169. TestCallbackLayerAnimationObserver::~TestCallbackLayerAnimationObserver() {
  170. if (destroyed_)
  171. (*destroyed_) = true;
  172. }
  173. class CallbackLayerAnimationObserverTest : public testing::Test {
  174. public:
  175. CallbackLayerAnimationObserverTest();
  176. CallbackLayerAnimationObserverTest(
  177. const CallbackLayerAnimationObserverTest&) = delete;
  178. CallbackLayerAnimationObserverTest& operator=(
  179. const CallbackLayerAnimationObserverTest&) = delete;
  180. ~CallbackLayerAnimationObserverTest() override;
  181. protected:
  182. // Creates a LayerAnimationSequence. The lifetime of the sequence will be
  183. // managed by this.
  184. LayerAnimationSequence* CreateLayerAnimationSequence();
  185. std::unique_ptr<TestCallbacks> callbacks_;
  186. std::unique_ptr<CallbackLayerAnimationObserver> observer_;
  187. std::unique_ptr<LayerAnimationObserverTestApi> observer_test_api_;
  188. // List of managaged sequences created by CreateLayerAnimationSequence() that
  189. // need to be destroyed.
  190. std::vector<std::unique_ptr<LayerAnimationSequence>> sequences_;
  191. };
  192. CallbackLayerAnimationObserverTest::CallbackLayerAnimationObserverTest()
  193. : callbacks_(new TestCallbacks()),
  194. observer_(new CallbackLayerAnimationObserver(
  195. base::BindRepeating(&TestCallbacks::AnimationsStarted,
  196. base::Unretained(callbacks_.get())),
  197. base::BindRepeating(&TestCallbacks::AnimationsEnded,
  198. base::Unretained(callbacks_.get())))),
  199. observer_test_api_(new LayerAnimationObserverTestApi(observer_.get())) {}
  200. CallbackLayerAnimationObserverTest::~CallbackLayerAnimationObserverTest() {
  201. observer_test_api_.reset();
  202. // The |observer_| will detach from all attached sequences upon destruction so
  203. // we need to explicitly delete the |observer_| before the |sequences_| and
  204. // |callbacks_|.
  205. observer_.reset();
  206. }
  207. LayerAnimationSequence*
  208. CallbackLayerAnimationObserverTest::CreateLayerAnimationSequence() {
  209. sequences_.emplace_back(new LayerAnimationSequence);
  210. return sequences_.back().get();
  211. }
  212. class CallbackLayerAnimationObserverTestOverwrite
  213. : public CallbackLayerAnimationObserverTest {
  214. public:
  215. CallbackLayerAnimationObserverTestOverwrite();
  216. CallbackLayerAnimationObserverTestOverwrite(
  217. const CallbackLayerAnimationObserverTestOverwrite&) = delete;
  218. CallbackLayerAnimationObserverTestOverwrite& operator=(
  219. const CallbackLayerAnimationObserverTestOverwrite&) = delete;
  220. protected:
  221. void AnimationStarted(const CallbackLayerAnimationObserver& observer);
  222. std::unique_ptr<CallbackLayerAnimationObserver> CreateAnimationObserver();
  223. };
  224. CallbackLayerAnimationObserverTestOverwrite::
  225. CallbackLayerAnimationObserverTestOverwrite() {
  226. observer_ = CreateAnimationObserver();
  227. observer_test_api_ =
  228. std::make_unique<LayerAnimationObserverTestApi>(observer_.get());
  229. }
  230. void CallbackLayerAnimationObserverTestOverwrite::AnimationStarted(
  231. const CallbackLayerAnimationObserver& observer) {
  232. observer_->OnLayerAnimationAborted(sequences_.front().get());
  233. observer_test_api_.reset();
  234. // Replace the current observer with a new observer so that the destructor
  235. // gets called on the current observer.
  236. observer_ = CreateAnimationObserver();
  237. }
  238. std::unique_ptr<CallbackLayerAnimationObserver>
  239. CallbackLayerAnimationObserverTestOverwrite::CreateAnimationObserver() {
  240. return std::make_unique<CallbackLayerAnimationObserver>(
  241. base::BindRepeating(
  242. &CallbackLayerAnimationObserverTestOverwrite::AnimationStarted,
  243. base::Unretained(this)),
  244. base::BindRepeating([](const CallbackLayerAnimationObserver& observer) {
  245. return false;
  246. }));
  247. }
  248. TEST(CallbackLayerAnimationObserverDestructionTest, VerifyFalseAutoDelete) {
  249. TestCallbacks callbacks;
  250. callbacks.set_should_delete_observer_on_animations_ended(false);
  251. bool is_destroyed = false;
  252. TestCallbackLayerAnimationObserver* observer =
  253. new TestCallbackLayerAnimationObserver(
  254. base::BindRepeating(&TestCallbacks::AnimationsStarted,
  255. base::Unretained(&callbacks)),
  256. false, &is_destroyed);
  257. observer->SetActive();
  258. EXPECT_FALSE(is_destroyed);
  259. delete observer;
  260. }
  261. TEST(CallbackLayerAnimationObserverDestructionTest, VerifyTrueAutoDelete) {
  262. TestCallbacks callbacks;
  263. callbacks.set_should_delete_observer_on_animations_ended(false);
  264. bool is_destroyed = false;
  265. TestCallbackLayerAnimationObserver* observer =
  266. new TestCallbackLayerAnimationObserver(
  267. base::BindRepeating(&TestCallbacks::AnimationsStarted,
  268. base::Unretained(&callbacks)),
  269. true, &is_destroyed);
  270. observer->SetActive();
  271. EXPECT_TRUE(is_destroyed);
  272. }
  273. TEST(CallbackLayerAnimationObserverDestructionTest,
  274. AnimationEndedReturnsFalse) {
  275. TestCallbacks callbacks;
  276. callbacks.set_should_delete_observer_on_animations_ended(false);
  277. bool is_destroyed = false;
  278. TestCallbackLayerAnimationObserver* observer =
  279. new TestCallbackLayerAnimationObserver(
  280. base::BindRepeating(&TestCallbacks::AnimationsStarted,
  281. base::Unretained(&callbacks)),
  282. base::BindRepeating(&TestCallbacks::AnimationsEnded,
  283. base::Unretained(&callbacks)),
  284. &is_destroyed);
  285. observer->SetActive();
  286. EXPECT_FALSE(is_destroyed);
  287. delete observer;
  288. }
  289. TEST(CallbackLayerAnimationObserverDestructionTest, AnimationEndedReturnsTrue) {
  290. TestCallbacks callbacks;
  291. callbacks.set_should_delete_observer_on_animations_ended(true);
  292. bool is_destroyed = false;
  293. TestCallbackLayerAnimationObserver* observer =
  294. new TestCallbackLayerAnimationObserver(
  295. base::BindRepeating(&TestCallbacks::AnimationsStarted,
  296. base::Unretained(&callbacks)),
  297. base::BindRepeating(&TestCallbacks::AnimationsEnded,
  298. base::Unretained(&callbacks)),
  299. &is_destroyed);
  300. observer->SetActive();
  301. EXPECT_TRUE(is_destroyed);
  302. }
  303. // Verifies that there are not heap-use-after-free errors when an observer has
  304. // its animation aborted and it gets destroyed due to a
  305. // unique_ptr<CallbackLayerAnimationObserver> being assigned a new value.
  306. TEST_F(CallbackLayerAnimationObserverTestOverwrite,
  307. VerifyOverwriteOnAnimationStart) {
  308. LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  309. LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  310. observer_test_api_->AttachedToSequence(sequence_1);
  311. observer_test_api_->AttachedToSequence(sequence_2);
  312. observer_->OnLayerAnimationStarted(sequence_1);
  313. observer_->OnLayerAnimationStarted(sequence_2);
  314. observer_->OnLayerAnimationEnded(sequence_1);
  315. observer_->SetActive();
  316. EXPECT_FALSE(observer_->active());
  317. }
  318. TEST_F(CallbackLayerAnimationObserverTest, VerifyInitialState) {
  319. EXPECT_FALSE(observer_->active());
  320. EXPECT_EQ(0, observer_->aborted_count());
  321. EXPECT_EQ(0, observer_->successful_count());
  322. EXPECT_FALSE(callbacks_->animations_started());
  323. EXPECT_FALSE(callbacks_->animations_ended());
  324. }
  325. // Verifies that the CallbackLayerAnimationObserver is robust to explicit
  326. // deletes caused as a side effect of calling the AnimationsStartedCallback()
  327. // when there are no animation sequences attached. This test also guards against
  328. // heap-use-after-free errors.
  329. TEST_F(
  330. CallbackLayerAnimationObserverTest,
  331. ExplicitlyDeleteObserverInAnimationStartedCallbackWithNoSequencesAttached) {
  332. TestCallbacksThatExplicitlyDeletesObserver callbacks;
  333. callbacks.set_should_delete_observer_on_animations_ended(true);
  334. bool is_destroyed = false;
  335. TestCallbackLayerAnimationObserver* observer =
  336. new TestCallbackLayerAnimationObserver(
  337. base::BindRepeating(&TestCallbacks::AnimationsStarted,
  338. base::Unretained(&callbacks)),
  339. base::BindRepeating(&TestCallbacks::AnimationsEnded,
  340. base::Unretained(&callbacks)),
  341. &is_destroyed);
  342. callbacks.set_observer_to_delete_in_animation_started(observer);
  343. observer->SetActive();
  344. EXPECT_TRUE(is_destroyed);
  345. }
  346. // Verifies that the CallbackLayerAnimationObserver is robust to explicit
  347. // deletes caused as a side effect of calling the AnimationsStartedCallback()
  348. // when there are some animation sequences attached. This test also guards
  349. // against heap-use-after-free errors.
  350. TEST_F(
  351. CallbackLayerAnimationObserverTest,
  352. ExplicitlyDeleteObserverInAnimationStartedCallbackWithSomeSequencesAttached) {
  353. LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  354. LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  355. TestCallbacksThatExplicitlyDeletesObserver callbacks;
  356. callbacks.set_should_delete_observer_on_animations_ended(true);
  357. bool is_destroyed = false;
  358. TestCallbackLayerAnimationObserver* observer =
  359. new TestCallbackLayerAnimationObserver(
  360. base::BindRepeating(&TestCallbacks::AnimationsStarted,
  361. base::Unretained(&callbacks)),
  362. base::BindRepeating(&TestCallbacks::AnimationsEnded,
  363. base::Unretained(&callbacks)),
  364. &is_destroyed);
  365. observer_test_api_->AttachedToSequence(sequence_1);
  366. observer_test_api_->AttachedToSequence(sequence_2);
  367. observer_->OnLayerAnimationStarted(sequence_1);
  368. observer_->OnLayerAnimationStarted(sequence_2);
  369. callbacks.set_observer_to_delete_in_animation_started(observer);
  370. observer->SetActive();
  371. EXPECT_TRUE(is_destroyed);
  372. }
  373. // Verifies that a 'true' return value for AnimationEndedCallback is ignored if
  374. // the CallbackLayerAnimationObserver is explicitly deleted as a side effect of
  375. // calling the AnimationEndedCallback. This test also guards against
  376. // heap-use-after-free errors.
  377. TEST_F(CallbackLayerAnimationObserverTest,
  378. IgnoreTrueReturnValueForAnimationEndedCallbackIfExplicitlyDeleted) {
  379. TestCallbacksThatExplicitlyDeletesObserver callbacks;
  380. callbacks.set_should_delete_observer_on_animations_ended(true);
  381. bool is_destroyed = false;
  382. TestCallbackLayerAnimationObserver* observer =
  383. new TestCallbackLayerAnimationObserver(
  384. base::BindRepeating(&TestCallbacks::AnimationsStarted,
  385. base::Unretained(&callbacks)),
  386. base::BindRepeating(&TestCallbacks::AnimationsEnded,
  387. base::Unretained(&callbacks)),
  388. &is_destroyed);
  389. callbacks.set_observer_to_delete_in_animation_ended(observer);
  390. observer->SetActive();
  391. EXPECT_TRUE(is_destroyed);
  392. }
  393. TEST_F(CallbackLayerAnimationObserverTest,
  394. SetActiveWhenNoSequencesWereAttached) {
  395. observer_->SetActive();
  396. EXPECT_FALSE(observer_->active());
  397. EXPECT_TRUE(callbacks_->animations_started());
  398. EXPECT_TRUE(callbacks_->animations_ended());
  399. EXPECT_TRUE(callbacks_->StartedEpochIsBeforeEndedEpoch());
  400. }
  401. TEST_F(CallbackLayerAnimationObserverTest,
  402. SetActiveWhenAllSequencesAreAttachedButNoneWereStarted) {
  403. LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  404. LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  405. observer_test_api_->AttachedToSequence(sequence_1);
  406. observer_test_api_->AttachedToSequence(sequence_2);
  407. observer_->SetActive();
  408. EXPECT_TRUE(observer_->active());
  409. EXPECT_FALSE(callbacks_->animations_started());
  410. EXPECT_FALSE(callbacks_->animations_ended());
  411. }
  412. TEST_F(CallbackLayerAnimationObserverTest,
  413. SetActiveWhenAllSequencesAreAttachedAndOnlySomeWereStarted) {
  414. LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  415. LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  416. observer_test_api_->AttachedToSequence(sequence_1);
  417. observer_test_api_->AttachedToSequence(sequence_2);
  418. observer_->OnLayerAnimationStarted(sequence_1);
  419. observer_->SetActive();
  420. EXPECT_TRUE(observer_->active());
  421. EXPECT_FALSE(callbacks_->animations_started());
  422. EXPECT_FALSE(callbacks_->animations_ended());
  423. }
  424. TEST_F(CallbackLayerAnimationObserverTest,
  425. SetActiveWhenAllSequencesAreAttachedAndOnlySomeWereCompleted) {
  426. LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  427. LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  428. observer_test_api_->AttachedToSequence(sequence_1);
  429. observer_test_api_->AttachedToSequence(sequence_2);
  430. observer_->OnLayerAnimationStarted(sequence_1);
  431. observer_->OnLayerAnimationEnded(sequence_1);
  432. observer_->SetActive();
  433. EXPECT_TRUE(observer_->active());
  434. EXPECT_FALSE(callbacks_->animations_started());
  435. EXPECT_FALSE(callbacks_->animations_ended());
  436. }
  437. TEST_F(CallbackLayerAnimationObserverTest,
  438. SetActiveAfterAllSequencesWereStartedButNoneWereCompleted) {
  439. LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  440. LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  441. observer_test_api_->AttachedToSequence(sequence_1);
  442. observer_test_api_->AttachedToSequence(sequence_2);
  443. observer_->OnLayerAnimationStarted(sequence_1);
  444. observer_->OnLayerAnimationStarted(sequence_2);
  445. observer_->SetActive();
  446. EXPECT_TRUE(observer_->active());
  447. EXPECT_TRUE(callbacks_->animations_started());
  448. EXPECT_FALSE(callbacks_->animations_ended());
  449. }
  450. TEST_F(CallbackLayerAnimationObserverTest,
  451. SetActiveWhenAllSequencesAreStartedAndOnlySomeWereCompleted) {
  452. LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  453. LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  454. observer_test_api_->AttachedToSequence(sequence_1);
  455. observer_test_api_->AttachedToSequence(sequence_2);
  456. observer_->OnLayerAnimationStarted(sequence_1);
  457. observer_->OnLayerAnimationStarted(sequence_2);
  458. observer_->OnLayerAnimationEnded(sequence_1);
  459. observer_->SetActive();
  460. EXPECT_TRUE(observer_->active());
  461. EXPECT_TRUE(callbacks_->animations_started());
  462. EXPECT_FALSE(callbacks_->animations_ended());
  463. }
  464. TEST_F(CallbackLayerAnimationObserverTest,
  465. SetActiveWhenAllSequencesWereCompleted) {
  466. LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  467. LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  468. observer_test_api_->AttachedToSequence(sequence_1);
  469. observer_test_api_->AttachedToSequence(sequence_2);
  470. observer_->OnLayerAnimationStarted(sequence_1);
  471. observer_->OnLayerAnimationStarted(sequence_2);
  472. observer_->OnLayerAnimationEnded(sequence_1);
  473. observer_->OnLayerAnimationEnded(sequence_2);
  474. observer_->SetActive();
  475. EXPECT_FALSE(observer_->active());
  476. EXPECT_TRUE(callbacks_->animations_started());
  477. EXPECT_TRUE(callbacks_->animations_ended());
  478. }
  479. TEST_F(CallbackLayerAnimationObserverTest,
  480. SetActiveAgainAfterAllSequencesWereCompleted) {
  481. LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  482. LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  483. LayerAnimationSequence* sequence_3 = CreateLayerAnimationSequence();
  484. LayerAnimationSequence* sequence_4 = CreateLayerAnimationSequence();
  485. observer_test_api_->AttachedToSequence(sequence_1);
  486. observer_test_api_->AttachedToSequence(sequence_2);
  487. observer_->OnLayerAnimationStarted(sequence_1);
  488. observer_->OnLayerAnimationStarted(sequence_2);
  489. observer_->OnLayerAnimationEnded(sequence_1);
  490. observer_->OnLayerAnimationEnded(sequence_2);
  491. observer_->SetActive();
  492. EXPECT_FALSE(observer_->active());
  493. observer_test_api_->AttachedToSequence(sequence_3);
  494. observer_test_api_->AttachedToSequence(sequence_4);
  495. callbacks_->ResetCallbackObservations();
  496. observer_->SetActive();
  497. EXPECT_TRUE(observer_->active());
  498. EXPECT_FALSE(callbacks_->animations_started());
  499. EXPECT_FALSE(callbacks_->animations_ended());
  500. EXPECT_EQ(2, observer_->successful_count());
  501. observer_->OnLayerAnimationStarted(sequence_3);
  502. observer_->OnLayerAnimationStarted(sequence_4);
  503. EXPECT_TRUE(observer_->active());
  504. EXPECT_TRUE(callbacks_->animations_started());
  505. EXPECT_FALSE(callbacks_->animations_ended());
  506. EXPECT_EQ(2, observer_->successful_count());
  507. observer_->OnLayerAnimationEnded(sequence_3);
  508. observer_->OnLayerAnimationEnded(sequence_4);
  509. EXPECT_FALSE(observer_->active());
  510. EXPECT_TRUE(callbacks_->animations_started());
  511. EXPECT_TRUE(callbacks_->animations_ended());
  512. EXPECT_EQ(4, observer_->successful_count());
  513. }
  514. TEST_F(CallbackLayerAnimationObserverTest, DetachBeforeActive) {
  515. LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  516. LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  517. observer_test_api_->AttachedToSequence(sequence_1);
  518. observer_test_api_->AttachedToSequence(sequence_2);
  519. observer_->OnLayerAnimationStarted(sequence_1);
  520. observer_->OnLayerAnimationEnded(sequence_1);
  521. observer_test_api_->DetachedFromSequence(sequence_1, true);
  522. observer_test_api_->DetachedFromSequence(sequence_2, true);
  523. observer_->SetActive();
  524. EXPECT_FALSE(observer_->active());
  525. EXPECT_TRUE(callbacks_->animations_started());
  526. EXPECT_TRUE(callbacks_->animations_ended());
  527. }
  528. TEST_F(CallbackLayerAnimationObserverTest, DetachAfterActive) {
  529. LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  530. LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  531. observer_test_api_->AttachedToSequence(sequence_1);
  532. observer_test_api_->AttachedToSequence(sequence_2);
  533. observer_->SetActive();
  534. observer_->OnLayerAnimationStarted(sequence_1);
  535. observer_->OnLayerAnimationEnded(sequence_1);
  536. observer_test_api_->DetachedFromSequence(sequence_1, true);
  537. observer_test_api_->DetachedFromSequence(sequence_2, true);
  538. EXPECT_FALSE(observer_->active());
  539. EXPECT_TRUE(callbacks_->animations_started());
  540. EXPECT_TRUE(callbacks_->animations_ended());
  541. }
  542. } // namespace test
  543. } // namespace ui