File: callback_layer_animation_observer_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (738 lines) | stat: -rw-r--r-- 26,801 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/compositor/callback_layer_animation_observer.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/compositor/layer_animation_sequence.h"
#include "ui/compositor/test/layer_animation_observer_test_api.h"

namespace ui {
namespace test {

// Simple class that tracks whether callbacks were invoked and when.
class TestCallbacks {
 public:
  TestCallbacks();

  TestCallbacks(const TestCallbacks&) = delete;
  TestCallbacks& operator=(const TestCallbacks&) = delete;

  virtual ~TestCallbacks();

  void ResetCallbackObservations();

  void set_should_delete_observer_on_animations_ended(
      bool should_delete_observer_on_animations_ended) {
    should_delete_observer_on_animations_ended_ =
        should_delete_observer_on_animations_ended;
  }

  bool animations_started() const { return animations_started_; }

  bool animations_ended() const { return animations_ended_; }

  virtual void AnimationsStarted(const CallbackLayerAnimationObserver&);

  virtual bool AnimationsEnded(const CallbackLayerAnimationObserver&);

  testing::AssertionResult StartedEpochIsBeforeEndedEpoch();

 private:
  // Monotonic counter that tracks the next time snapshot.
  int next_epoch_ = 0;

  // Is true when AnimationsStarted() has been called.
  bool animations_started_ = false;

  // Relative time snapshot of when AnimationsStarted() was last called.
  int animations_started_epoch_ = -1;

  // Is true when AnimationsEnded() has been called.
  bool animations_ended_ = false;

  // Relative time snapshot of when AnimationsEnded() was last called.
  int animations_ended_epoch_ = -1;

  // The return value for AnimationsEnded().
  bool should_delete_observer_on_animations_ended_ = false;
};

TestCallbacks::TestCallbacks() {}

TestCallbacks::~TestCallbacks() {}

void TestCallbacks::ResetCallbackObservations() {
  next_epoch_ = 0;
  animations_started_ = false;
  animations_started_epoch_ = -1;
  animations_ended_ = false;
  animations_ended_epoch_ = -1;
  should_delete_observer_on_animations_ended_ = false;
}

void TestCallbacks::AnimationsStarted(const CallbackLayerAnimationObserver&) {
  animations_started_ = true;
  animations_started_epoch_ = next_epoch_++;
}

bool TestCallbacks::AnimationsEnded(const CallbackLayerAnimationObserver&) {
  animations_ended_ = true;
  animations_ended_epoch_ = next_epoch_++;
  return should_delete_observer_on_animations_ended_;
}

testing::AssertionResult TestCallbacks::StartedEpochIsBeforeEndedEpoch() {
  if (animations_started_epoch_ < animations_ended_epoch_) {
    return testing::AssertionSuccess();
  } else {
    return testing::AssertionFailure()
           << "The started epoch=" << animations_started_epoch_
           << " is NOT before the ended epoch=" << animations_ended_epoch_;
  }
}

// A child of TestCallbacks that can explicitly delete a
// CallbackLayerAnimationObserver in the AnimationsStarted() or
// AnimationsEnded() callback.
class TestCallbacksThatExplicitlyDeletesObserver : public TestCallbacks {
 public:
  TestCallbacksThatExplicitlyDeletesObserver();

  TestCallbacksThatExplicitlyDeletesObserver(
      const TestCallbacksThatExplicitlyDeletesObserver&) = delete;
  TestCallbacksThatExplicitlyDeletesObserver& operator=(
      const TestCallbacksThatExplicitlyDeletesObserver&) = delete;

  void set_observer_to_delete_in_animation_ended(
      std::unique_ptr<CallbackLayerAnimationObserver> observer) {
    observer_to_delete_in_animation_ended_ = std::move(observer);
  }

  void set_observer_to_delete_in_animation_started(
      std::unique_ptr<CallbackLayerAnimationObserver> observer) {
    observer_to_delete_in_animation_started_ = std::move(observer);
  }

  CallbackLayerAnimationObserver* observer_to_delete_in_animation_started() {
    return observer_to_delete_in_animation_started_.get();
  }

  CallbackLayerAnimationObserver* observer_to_delete_in_animation_ended() {
    return observer_to_delete_in_animation_ended_.get();
  }

  // TestCallbacks:
  void AnimationsStarted(
      const CallbackLayerAnimationObserver& observer) override;
  bool AnimationsEnded(const CallbackLayerAnimationObserver& observer) override;

 private:
  // The observer to delete, if non-NULL, in AnimationsStarted().
  std::unique_ptr<CallbackLayerAnimationObserver>
      observer_to_delete_in_animation_started_;

  // The observer to delete, if non-NULL, in AnimationsEnded().
  std::unique_ptr<CallbackLayerAnimationObserver>
      observer_to_delete_in_animation_ended_;
};

TestCallbacksThatExplicitlyDeletesObserver::
    TestCallbacksThatExplicitlyDeletesObserver() {}

void TestCallbacksThatExplicitlyDeletesObserver::AnimationsStarted(
    const CallbackLayerAnimationObserver& observer) {
  observer_to_delete_in_animation_started_.reset();
  TestCallbacks::AnimationsStarted(observer);
}

bool TestCallbacksThatExplicitlyDeletesObserver::AnimationsEnded(
    const CallbackLayerAnimationObserver& observer) {
  observer_to_delete_in_animation_ended_.reset();
  return TestCallbacks::AnimationsEnded(observer);
}

// A test specific CallbackLayerAnimationObserver that will set a bool when
// destroyed.
class TestCallbackLayerAnimationObserver
    : public CallbackLayerAnimationObserver {
 public:
  TestCallbackLayerAnimationObserver(
      AnimationStartedCallback animation_started_callback,
      AnimationEndedCallback animation_ended_callback,
      bool* destroyed);

  TestCallbackLayerAnimationObserver(
      AnimationStartedCallback animation_started_callback,
      bool should_delete_observer,
      bool* destroyed);

  TestCallbackLayerAnimationObserver(
      AnimationEndedCallback animation_ended_callback,
      bool* destroyed);

  TestCallbackLayerAnimationObserver(
      const TestCallbackLayerAnimationObserver&) = delete;
  TestCallbackLayerAnimationObserver& operator=(
      const TestCallbackLayerAnimationObserver&) = delete;

  ~TestCallbackLayerAnimationObserver() override;

 private:
  raw_ptr<bool> destroyed_;
};

TestCallbackLayerAnimationObserver::TestCallbackLayerAnimationObserver(
    AnimationStartedCallback animation_started_callback,
    AnimationEndedCallback animation_ended_callback,
    bool* destroyed)
    : CallbackLayerAnimationObserver(animation_started_callback,
                                     animation_ended_callback),
      destroyed_(destroyed) {
  if (destroyed_)
    (*destroyed_) = false;
}

TestCallbackLayerAnimationObserver::TestCallbackLayerAnimationObserver(
    AnimationStartedCallback animation_started_callback,
    bool should_delete_observer,
    bool* destroyed)
    : CallbackLayerAnimationObserver(animation_started_callback,
                                     should_delete_observer),
      destroyed_(destroyed) {
  if (destroyed_)
    (*destroyed_) = false;
}

TestCallbackLayerAnimationObserver::TestCallbackLayerAnimationObserver(
    AnimationEndedCallback animation_ended_callback,
    bool* destroyed)
    : CallbackLayerAnimationObserver(animation_ended_callback),
      destroyed_(destroyed) {
  if (destroyed_)
    (*destroyed_) = false;
}

TestCallbackLayerAnimationObserver::~TestCallbackLayerAnimationObserver() {
  if (destroyed_)
    (*destroyed_) = true;
}

class CallbackLayerAnimationObserverTest : public testing::Test {
 public:
  CallbackLayerAnimationObserverTest();

  CallbackLayerAnimationObserverTest(
      const CallbackLayerAnimationObserverTest&) = delete;
  CallbackLayerAnimationObserverTest& operator=(
      const CallbackLayerAnimationObserverTest&) = delete;

  ~CallbackLayerAnimationObserverTest() override;

 protected:
  // Creates a LayerAnimationSequence.  The lifetime of the sequence will be
  // managed by this.
  LayerAnimationSequence* CreateLayerAnimationSequence();

  std::unique_ptr<TestCallbacks> callbacks_;

  std::unique_ptr<CallbackLayerAnimationObserver> observer_;

  std::unique_ptr<LayerAnimationObserverTestApi> observer_test_api_;

  // List of managaged sequences created by CreateLayerAnimationSequence() that
  // need to be destroyed.
  std::vector<std::unique_ptr<LayerAnimationSequence>> sequences_;
};

CallbackLayerAnimationObserverTest::CallbackLayerAnimationObserverTest()
    : callbacks_(new TestCallbacks()),
      observer_(new CallbackLayerAnimationObserver(
          base::BindRepeating(&TestCallbacks::AnimationsStarted,
                              base::Unretained(callbacks_.get())),
          base::BindRepeating(&TestCallbacks::AnimationsEnded,
                              base::Unretained(callbacks_.get())))),
      observer_test_api_(new LayerAnimationObserverTestApi(observer_.get())) {}

CallbackLayerAnimationObserverTest::~CallbackLayerAnimationObserverTest() {
  observer_test_api_.reset();
  // The |observer_| will detach from all attached sequences upon destruction so
  // we need to explicitly delete the |observer_| before the |sequences_| and
  // |callbacks_|.
  observer_.reset();
}

LayerAnimationSequence*
CallbackLayerAnimationObserverTest::CreateLayerAnimationSequence() {
  sequences_.emplace_back(new LayerAnimationSequence);
  return sequences_.back().get();
}

class CallbackLayerAnimationObserverTestOverwrite
    : public CallbackLayerAnimationObserverTest {
 public:
  CallbackLayerAnimationObserverTestOverwrite();

  CallbackLayerAnimationObserverTestOverwrite(
      const CallbackLayerAnimationObserverTestOverwrite&) = delete;
  CallbackLayerAnimationObserverTestOverwrite& operator=(
      const CallbackLayerAnimationObserverTestOverwrite&) = delete;

 protected:
  void AnimationStarted(const CallbackLayerAnimationObserver& observer);

  std::unique_ptr<CallbackLayerAnimationObserver> CreateAnimationObserver();
};

CallbackLayerAnimationObserverTestOverwrite::
    CallbackLayerAnimationObserverTestOverwrite() {
  observer_ = CreateAnimationObserver();
  observer_test_api_ =
      std::make_unique<LayerAnimationObserverTestApi>(observer_.get());
}

void CallbackLayerAnimationObserverTestOverwrite::AnimationStarted(
    const CallbackLayerAnimationObserver& observer) {
  observer_->OnLayerAnimationAborted(sequences_.front().get());
  observer_test_api_.reset();
  // Replace the current observer with a new observer so that the destructor
  // gets called on the current observer.
  observer_ = CreateAnimationObserver();
}

std::unique_ptr<CallbackLayerAnimationObserver>
CallbackLayerAnimationObserverTestOverwrite::CreateAnimationObserver() {
  return std::make_unique<CallbackLayerAnimationObserver>(
      base::BindRepeating(
          &CallbackLayerAnimationObserverTestOverwrite::AnimationStarted,
          base::Unretained(this)),
      base::BindRepeating([](const CallbackLayerAnimationObserver& observer) {
        return false;
      }));
}

TEST(CallbackLayerAnimationObserverDestructionTest, VerifyFalseAutoDelete) {
  TestCallbacks callbacks;
  callbacks.set_should_delete_observer_on_animations_ended(false);

  bool is_destroyed = false;

  TestCallbackLayerAnimationObserver* observer =
      new TestCallbackLayerAnimationObserver(
          base::BindRepeating(&TestCallbacks::AnimationsStarted,
                              base::Unretained(&callbacks)),
          false, &is_destroyed);
  observer->SetActive();

  EXPECT_FALSE(is_destroyed);
  delete observer;
}

TEST(CallbackLayerAnimationObserverDestructionTest, VerifyTrueAutoDelete) {
  TestCallbacks callbacks;
  callbacks.set_should_delete_observer_on_animations_ended(false);

  bool is_destroyed = false;

  TestCallbackLayerAnimationObserver* observer =
      new TestCallbackLayerAnimationObserver(
          base::BindRepeating(&TestCallbacks::AnimationsStarted,
                              base::Unretained(&callbacks)),
          true, &is_destroyed);
  observer->SetActive();

  EXPECT_TRUE(is_destroyed);
}

TEST(CallbackLayerAnimationObserverDestructionTest,
     AnimationEndedReturnsFalse) {
  TestCallbacks callbacks;
  callbacks.set_should_delete_observer_on_animations_ended(false);

  bool is_destroyed = false;

  TestCallbackLayerAnimationObserver* observer =
      new TestCallbackLayerAnimationObserver(
          base::BindRepeating(&TestCallbacks::AnimationsStarted,
                              base::Unretained(&callbacks)),
          base::BindRepeating(&TestCallbacks::AnimationsEnded,
                              base::Unretained(&callbacks)),
          &is_destroyed);
  observer->SetActive();

  EXPECT_FALSE(is_destroyed);
  delete observer;
}

TEST(CallbackLayerAnimationObserverDestructionTest, AnimationEndedReturnsTrue) {
  TestCallbacks callbacks;
  callbacks.set_should_delete_observer_on_animations_ended(true);

  bool is_destroyed = false;

  TestCallbackLayerAnimationObserver* observer =
      new TestCallbackLayerAnimationObserver(
          base::BindRepeating(&TestCallbacks::AnimationsStarted,
                              base::Unretained(&callbacks)),
          base::BindRepeating(&TestCallbacks::AnimationsEnded,
                              base::Unretained(&callbacks)),
          &is_destroyed);
  observer->SetActive();

  EXPECT_TRUE(is_destroyed);
}

// Verifies that there are not heap-use-after-free errors when an observer has
// its animation aborted and it gets destroyed due to a
// unique_ptr<CallbackLayerAnimationObserver> being assigned a new value.
TEST_F(CallbackLayerAnimationObserverTestOverwrite,
       VerifyOverwriteOnAnimationStart) {
  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();

  observer_test_api_->AttachedToSequence(sequence_1);
  observer_test_api_->AttachedToSequence(sequence_2);
  observer_->OnLayerAnimationStarted(sequence_1);
  observer_->OnLayerAnimationStarted(sequence_2);
  observer_->OnLayerAnimationEnded(sequence_1);
  observer_->SetActive();
  EXPECT_FALSE(observer_->active());
}

TEST_F(CallbackLayerAnimationObserverTest, VerifyInitialState) {
  EXPECT_FALSE(observer_->active());
  EXPECT_EQ(0, observer_->aborted_count());
  EXPECT_EQ(0, observer_->successful_count());

  EXPECT_FALSE(callbacks_->animations_started());
  EXPECT_FALSE(callbacks_->animations_ended());
}

// Verifies that the CallbackLayerAnimationObserver is robust to explicit
// deletes caused as a side effect of calling the AnimationsStartedCallback()
// when there are no animation sequences attached. This test also guards against
// heap-use-after-free errors.
TEST_F(
    CallbackLayerAnimationObserverTest,
    ExplicitlyDeleteObserverInAnimationStartedCallbackWithNoSequencesAttached) {
  TestCallbacksThatExplicitlyDeletesObserver callbacks;
  callbacks.set_should_delete_observer_on_animations_ended(true);

  bool is_destroyed = false;

  std::unique_ptr<TestCallbackLayerAnimationObserver> observer =
      std::make_unique<TestCallbackLayerAnimationObserver>(
          base::BindRepeating(&TestCallbacks::AnimationsStarted,
                              base::Unretained(&callbacks)),
          base::BindRepeating(&TestCallbacks::AnimationsEnded,
                              base::Unretained(&callbacks)),
          &is_destroyed);

  callbacks.set_observer_to_delete_in_animation_started(std::move(observer));

  callbacks.observer_to_delete_in_animation_started()->SetActive();

  EXPECT_TRUE(is_destroyed);
}

// Verifies that the CallbackLayerAnimationObserver is robust to explicit
// deletes caused as a side effect of calling the AnimationsStartedCallback()
// when all attached animation sequences detach. This test also guards against
// heap-use-after-free errors.
TEST_F(CallbackLayerAnimationObserverTest,
       ExplicitlyDeleteObserverInAnimationStartedCallbackSequencesDetached) {
  TestCallbacksThatExplicitlyDeletesObserver callbacks;
  callbacks.set_should_delete_observer_on_animations_ended(true);

  bool is_destroyed = false;

  std::unique_ptr<TestCallbackLayerAnimationObserver> observer =
      std::make_unique<TestCallbackLayerAnimationObserver>(
          base::BindRepeating(&TestCallbacks::AnimationsStarted,
                              base::Unretained(&callbacks)),
          base::BindRepeating(&TestCallbacks::AnimationsEnded,
                              base::Unretained(&callbacks)),
          &is_destroyed);

  callbacks.set_observer_to_delete_in_animation_started(std::move(observer));
  CallbackLayerAnimationObserver* observer_ptr =
      callbacks.observer_to_delete_in_animation_started();
  LayerAnimationObserverTestApi test_api(observer_ptr);

  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();

  test_api.AttachedToSequence(sequence_1);
  observer_ptr->SetActive();
  test_api.DetachedFromSequence(sequence_1, true);

  EXPECT_TRUE(is_destroyed);
  EXPECT_TRUE(callbacks.animations_started());

  // The observer was destroyed before the end notification could be dispatched.
  EXPECT_FALSE(callbacks.animations_ended());
}

// Verifies that the CallbackLayerAnimationObserver is robust to explicit
// deletes caused as a side effect of calling the AnimationsStartedCallback()
// when there are some animation sequences attached. This test also guards
// against heap-use-after-free errors.
TEST_F(
    CallbackLayerAnimationObserverTest,
    ExplicitlyDeleteObserverInAnimationStartedCallbackWithSomeSequencesAttached) {
  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();

  TestCallbacksThatExplicitlyDeletesObserver callbacks;
  callbacks.set_should_delete_observer_on_animations_ended(true);

  bool is_destroyed = false;

  std::unique_ptr<TestCallbackLayerAnimationObserver> observer =
      std::make_unique<TestCallbackLayerAnimationObserver>(
          base::BindRepeating(&TestCallbacks::AnimationsStarted,
                              base::Unretained(&callbacks)),
          base::BindRepeating(&TestCallbacks::AnimationsEnded,
                              base::Unretained(&callbacks)),
          &is_destroyed);

  observer_test_api_->AttachedToSequence(sequence_1);
  observer_test_api_->AttachedToSequence(sequence_2);
  observer_->OnLayerAnimationStarted(sequence_1);
  observer_->OnLayerAnimationStarted(sequence_2);

  callbacks.set_observer_to_delete_in_animation_started(std::move(observer));

  callbacks.observer_to_delete_in_animation_started()->SetActive();

  EXPECT_TRUE(is_destroyed);
}

// Verifies that a 'true' return value for AnimationEndedCallback is ignored if
// the CallbackLayerAnimationObserver is explicitly deleted as a side effect of
// calling the AnimationEndedCallback. This test also guards against
// heap-use-after-free errors.
TEST_F(CallbackLayerAnimationObserverTest,
       IgnoreTrueReturnValueForAnimationEndedCallbackIfExplicitlyDeleted) {
  TestCallbacksThatExplicitlyDeletesObserver callbacks;
  callbacks.set_should_delete_observer_on_animations_ended(true);

  bool is_destroyed = false;

  std::unique_ptr<TestCallbackLayerAnimationObserver> observer =
      std::make_unique<TestCallbackLayerAnimationObserver>(
          base::BindRepeating(&TestCallbacks::AnimationsStarted,
                              base::Unretained(&callbacks)),
          base::BindRepeating(&TestCallbacks::AnimationsEnded,
                              base::Unretained(&callbacks)),
          &is_destroyed);

  callbacks.set_observer_to_delete_in_animation_ended(std::move(observer));

  callbacks.observer_to_delete_in_animation_ended()->SetActive();

  EXPECT_TRUE(is_destroyed);
}

TEST_F(CallbackLayerAnimationObserverTest,
       SetActiveWhenNoSequencesWereAttached) {
  observer_->SetActive();

  EXPECT_FALSE(observer_->active());
  EXPECT_TRUE(callbacks_->animations_started());
  EXPECT_TRUE(callbacks_->animations_ended());
  EXPECT_TRUE(callbacks_->StartedEpochIsBeforeEndedEpoch());
}

TEST_F(CallbackLayerAnimationObserverTest,
       SetActiveWhenAllSequencesAreAttachedButNoneWereStarted) {
  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();

  observer_test_api_->AttachedToSequence(sequence_1);
  observer_test_api_->AttachedToSequence(sequence_2);

  observer_->SetActive();

  EXPECT_TRUE(observer_->active());
  EXPECT_FALSE(callbacks_->animations_started());
  EXPECT_FALSE(callbacks_->animations_ended());
}

TEST_F(CallbackLayerAnimationObserverTest,
       SetActiveWhenAllSequencesAreAttachedAndOnlySomeWereStarted) {
  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();

  observer_test_api_->AttachedToSequence(sequence_1);
  observer_test_api_->AttachedToSequence(sequence_2);
  observer_->OnLayerAnimationStarted(sequence_1);

  observer_->SetActive();

  EXPECT_TRUE(observer_->active());
  EXPECT_FALSE(callbacks_->animations_started());
  EXPECT_FALSE(callbacks_->animations_ended());
}

TEST_F(CallbackLayerAnimationObserverTest,
       SetActiveWhenAllSequencesAreAttachedAndOnlySomeWereCompleted) {
  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();

  observer_test_api_->AttachedToSequence(sequence_1);
  observer_test_api_->AttachedToSequence(sequence_2);
  observer_->OnLayerAnimationStarted(sequence_1);
  observer_->OnLayerAnimationEnded(sequence_1);

  observer_->SetActive();

  EXPECT_TRUE(observer_->active());
  EXPECT_FALSE(callbacks_->animations_started());
  EXPECT_FALSE(callbacks_->animations_ended());
}

TEST_F(CallbackLayerAnimationObserverTest,
       SetActiveAfterAllSequencesWereStartedButNoneWereCompleted) {
  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();

  observer_test_api_->AttachedToSequence(sequence_1);
  observer_test_api_->AttachedToSequence(sequence_2);
  observer_->OnLayerAnimationStarted(sequence_1);
  observer_->OnLayerAnimationStarted(sequence_2);

  observer_->SetActive();

  EXPECT_TRUE(observer_->active());
  EXPECT_TRUE(callbacks_->animations_started());
  EXPECT_FALSE(callbacks_->animations_ended());
}

TEST_F(CallbackLayerAnimationObserverTest,
       SetActiveWhenAllSequencesAreStartedAndOnlySomeWereCompleted) {
  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();

  observer_test_api_->AttachedToSequence(sequence_1);
  observer_test_api_->AttachedToSequence(sequence_2);
  observer_->OnLayerAnimationStarted(sequence_1);
  observer_->OnLayerAnimationStarted(sequence_2);
  observer_->OnLayerAnimationEnded(sequence_1);

  observer_->SetActive();

  EXPECT_TRUE(observer_->active());
  EXPECT_TRUE(callbacks_->animations_started());
  EXPECT_FALSE(callbacks_->animations_ended());
}

TEST_F(CallbackLayerAnimationObserverTest,
       SetActiveWhenAllSequencesWereCompleted) {
  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();

  observer_test_api_->AttachedToSequence(sequence_1);
  observer_test_api_->AttachedToSequence(sequence_2);
  observer_->OnLayerAnimationStarted(sequence_1);
  observer_->OnLayerAnimationStarted(sequence_2);
  observer_->OnLayerAnimationEnded(sequence_1);
  observer_->OnLayerAnimationEnded(sequence_2);

  observer_->SetActive();

  EXPECT_FALSE(observer_->active());
  EXPECT_TRUE(callbacks_->animations_started());
  EXPECT_TRUE(callbacks_->animations_ended());
}

TEST_F(CallbackLayerAnimationObserverTest,
       SetActiveAgainAfterAllSequencesWereCompleted) {
  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_3 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_4 = CreateLayerAnimationSequence();

  observer_test_api_->AttachedToSequence(sequence_1);
  observer_test_api_->AttachedToSequence(sequence_2);
  observer_->OnLayerAnimationStarted(sequence_1);
  observer_->OnLayerAnimationStarted(sequence_2);
  observer_->OnLayerAnimationEnded(sequence_1);
  observer_->OnLayerAnimationEnded(sequence_2);

  observer_->SetActive();

  EXPECT_FALSE(observer_->active());

  observer_test_api_->AttachedToSequence(sequence_3);
  observer_test_api_->AttachedToSequence(sequence_4);

  callbacks_->ResetCallbackObservations();

  observer_->SetActive();

  EXPECT_TRUE(observer_->active());
  EXPECT_FALSE(callbacks_->animations_started());
  EXPECT_FALSE(callbacks_->animations_ended());
  EXPECT_EQ(2, observer_->successful_count());

  observer_->OnLayerAnimationStarted(sequence_3);
  observer_->OnLayerAnimationStarted(sequence_4);

  EXPECT_TRUE(observer_->active());
  EXPECT_TRUE(callbacks_->animations_started());
  EXPECT_FALSE(callbacks_->animations_ended());
  EXPECT_EQ(2, observer_->successful_count());

  observer_->OnLayerAnimationEnded(sequence_3);
  observer_->OnLayerAnimationEnded(sequence_4);

  EXPECT_FALSE(observer_->active());
  EXPECT_TRUE(callbacks_->animations_started());
  EXPECT_TRUE(callbacks_->animations_ended());
  EXPECT_EQ(4, observer_->successful_count());
}

TEST_F(CallbackLayerAnimationObserverTest, DetachBeforeActive) {
  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();

  observer_test_api_->AttachedToSequence(sequence_1);
  observer_test_api_->AttachedToSequence(sequence_2);
  observer_->OnLayerAnimationStarted(sequence_1);
  observer_->OnLayerAnimationEnded(sequence_1);
  observer_test_api_->DetachedFromSequence(sequence_1, true);
  observer_test_api_->DetachedFromSequence(sequence_2, true);

  observer_->SetActive();

  EXPECT_FALSE(observer_->active());
  EXPECT_TRUE(callbacks_->animations_started());
  EXPECT_TRUE(callbacks_->animations_ended());
}

TEST_F(CallbackLayerAnimationObserverTest, DetachAfterActive) {
  LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
  LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();

  observer_test_api_->AttachedToSequence(sequence_1);
  observer_test_api_->AttachedToSequence(sequence_2);

  observer_->SetActive();

  observer_->OnLayerAnimationStarted(sequence_1);
  observer_->OnLayerAnimationEnded(sequence_1);
  observer_test_api_->DetachedFromSequence(sequence_1, true);
  observer_test_api_->DetachedFromSequence(sequence_2, true);

  EXPECT_FALSE(observer_->active());
  EXPECT_TRUE(callbacks_->animations_started());
  EXPECT_TRUE(callbacks_->animations_ended());
}

}  // namespace test
}  // namespace ui