Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Side by Side Diff: ui/compositor/callback_layer_animation_observer_unittest.cc

Issue 1369393002: Added a CallbackLayerAnimationObserver. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed the diff delta to be based off the correct branch. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
5 #include "ui/compositor/callback_layer_animation_observer.h"
6
7 #include "base/bind.h"
8 #include "base/macros.h"
9 #include "base/memory/scoped_vector.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/compositor/layer_animation_sequence.h"
12 #include "ui/compositor/test/layer_animation_observer_test_api.h"
13
14 namespace ui {
15 namespace test {
16
17 // Simple class that tracks whether callbacks were invoked and when.
18 class TestCallbacks {
19 public:
20 TestCallbacks();
21
22 void ResetCallbackObservations();
23
24 void set_should_delete_observer_on_animations_ended(
25 bool should_delete_observer_on_animations_ended) {
26 should_delete_observer_on_animations_ended_ =
27 should_delete_observer_on_animations_ended;
28 }
29
30 bool animations_started() const { return animations_started_; }
31
32 bool animations_ended() const { return animations_ended_; }
33
34 void AnimationsStarted(const CallbackLayerAnimationObserver&);
35
36 bool AnimationsEnded(const CallbackLayerAnimationObserver&);
37
38 testing::AssertionResult StartedEpochIsBeforeEndedEpoch();
39
40 private:
41 // Monotonic counter that tracks the next time snapshot.
42 int next_epoch_ = 0;
43
44 // Is true when AnimationsStarted() has been called.
45 bool animations_started_ = false;
46
47 // Relative time snapshot of when AnimationsStarted() was last called.
48 int animations_started_epoch_ = -1;
49
50 // Is true when AnimationsEnded() has been called.
51 bool animations_ended_ = false;
52
53 // Relative time snapshot of when AnimationsEnded() was last called.
54 int animations_ended_epoch_ = -1;
55
56 // The return value for AnimationsEnded().
57 bool should_delete_observer_on_animations_ended_ = false;
58
59 DISALLOW_COPY_AND_ASSIGN(TestCallbacks);
60 };
61
62 TestCallbacks::TestCallbacks() {}
63
64 void TestCallbacks::ResetCallbackObservations() {
65 next_epoch_ = 0;
66 animations_started_ = false;
67 animations_started_epoch_ = -1;
68 animations_ended_ = false;
69 animations_ended_epoch_ = -1;
70 should_delete_observer_on_animations_ended_ = false;
71 }
72
73 void TestCallbacks::AnimationsStarted(const CallbackLayerAnimationObserver&) {
74 animations_started_ = true;
75 animations_started_epoch_ = next_epoch_++;
76 }
77
78 bool TestCallbacks::AnimationsEnded(const CallbackLayerAnimationObserver&) {
79 animations_ended_ = true;
80 animations_ended_epoch_ = next_epoch_++;
81 return should_delete_observer_on_animations_ended_;
82 }
83
84 testing::AssertionResult TestCallbacks::StartedEpochIsBeforeEndedEpoch() {
85 if (animations_started_epoch_ < animations_ended_epoch_) {
86 return testing::AssertionSuccess();
87 } else {
88 return testing::AssertionFailure()
89 << "The started epoch=" << animations_started_epoch_
90 << " is NOT before the ended epoch=" << animations_ended_epoch_;
91 }
92 }
93
94 // A test specific CallbackLayerAnimationObserver that will set a bool when
95 // destroyed.
96 class TestCallbackLayerAnimationObserver
97 : public CallbackLayerAnimationObserver {
98 public:
99 TestCallbackLayerAnimationObserver(
100 AnimationStartedCallback animation_started_callback,
101 AnimationEndedCallback animation_ended_callback,
102 bool* destroyed);
103 ~TestCallbackLayerAnimationObserver() override;
104
105 private:
106 bool* destroyed_;
107
108 DISALLOW_COPY_AND_ASSIGN(TestCallbackLayerAnimationObserver);
109 };
110
111 TestCallbackLayerAnimationObserver::TestCallbackLayerAnimationObserver(
112 AnimationStartedCallback animation_started_callback,
113 AnimationEndedCallback animation_ended_callback,
114 bool* destroyed)
115 : CallbackLayerAnimationObserver(animation_started_callback,
116 animation_ended_callback),
117 destroyed_(destroyed) {
118 (*destroyed_) = false;
119 }
120
121 TestCallbackLayerAnimationObserver::~TestCallbackLayerAnimationObserver() {
122 (*destroyed_) = true;
123 }
124
125 class CallbackLayerAnimationObserverTest : public testing::Test {
126 public:
127 CallbackLayerAnimationObserverTest();
128 ~CallbackLayerAnimationObserverTest() override;
129
130 protected:
131 // Creates a LayerAnimationSequence. The lifetime of the sequence will be
132 // managed by this.
133 LayerAnimationSequence* CreateLayerAnimationSequence();
134
135 scoped_ptr<TestCallbacks> callbacks_;
136
137 scoped_ptr<CallbackLayerAnimationObserver> observer_;
138
139 scoped_ptr<LayerAnimationObserverTestApi> observer_test_api_;
140
141 // List of managaged sequences created by CreateLayerAnimationSequence() that
142 // need to be destroyed.
143 ScopedVector<LayerAnimationSequence> sequences_;
144
145 private:
146 DISALLOW_COPY_AND_ASSIGN(CallbackLayerAnimationObserverTest);
147 };
148
149 CallbackLayerAnimationObserverTest::CallbackLayerAnimationObserverTest()
150 : callbacks_(new TestCallbacks()),
151 observer_(new CallbackLayerAnimationObserver(
152 base::Bind(&TestCallbacks::AnimationsStarted,
153 base::Unretained(callbacks_.get())),
154 base::Bind(&TestCallbacks::AnimationsEnded,
155 base::Unretained(callbacks_.get())))),
156 observer_test_api_(new LayerAnimationObserverTestApi(observer_.get())) {}
157
158 CallbackLayerAnimationObserverTest::~CallbackLayerAnimationObserverTest() {
159 observer_test_api_.reset();
160 // The |observer_| will detach from all attached sequences upon destruction so
161 // we need to explicitly delete the |observer_| before the |sequences_| and
162 // |callbacks_|.
ajuma 2015/10/07 20:14:55 I'm not sure I follow this part. The observer's de
bruthig 2015/10/07 21:37:29 Observers are detached from in LayerAnimationObser
ajuma 2015/10/07 21:50:38 Ah, yes, that's what I was missing. Thanks!
163 observer_.reset();
164 }
165
166 LayerAnimationSequence*
167 CallbackLayerAnimationObserverTest::CreateLayerAnimationSequence() {
168 LayerAnimationSequence* sequence = new LayerAnimationSequence();
169 sequences_.push_back(sequence);
170 return sequence;
171 }
172
173 TEST(CallbackLayerAnimationObserverDestructionTest,
174 AnimationEndedReturnsFalse) {
175 TestCallbacks callbacks;
176 callbacks.set_should_delete_observer_on_animations_ended(false);
177
178 bool is_destroyed = false;
179
180 TestCallbackLayerAnimationObserver* observer =
181 new TestCallbackLayerAnimationObserver(
182 base::Bind(&TestCallbacks::AnimationsStarted,
183 base::Unretained(&callbacks)),
184 base::Bind(&TestCallbacks::AnimationsEnded,
185 base::Unretained(&callbacks)),
186 &is_destroyed);
187 observer->SetActive();
188
189 EXPECT_FALSE(is_destroyed);
190 delete observer;
191 }
192
193 TEST(CallbackLayerAnimationObserverDestructionTest, AnimationEndedReturnsTrue) {
194 TestCallbacks callbacks;
195 callbacks.set_should_delete_observer_on_animations_ended(true);
196
197 bool is_destroyed = false;
198
199 TestCallbackLayerAnimationObserver* observer =
200 new TestCallbackLayerAnimationObserver(
201 base::Bind(&TestCallbacks::AnimationsStarted,
202 base::Unretained(&callbacks)),
203 base::Bind(&TestCallbacks::AnimationsEnded,
204 base::Unretained(&callbacks)),
205 &is_destroyed);
206 observer->SetActive();
207
208 EXPECT_TRUE(is_destroyed);
209 }
210
211 TEST_F(CallbackLayerAnimationObserverTest, VerifyInitialState) {
212 EXPECT_EQ(false, observer_->active());
213 EXPECT_EQ(0, observer_->aborted_count());
214 EXPECT_EQ(0, observer_->successful_count());
215
216 EXPECT_FALSE(callbacks_->animations_started());
217 EXPECT_FALSE(callbacks_->animations_ended());
218 }
219
220 TEST_F(CallbackLayerAnimationObserverTest,
221 SetActiveWhenNoSequencesWereAttached) {
222 observer_->SetActive();
223
224 EXPECT_FALSE(observer_->active());
225 EXPECT_TRUE(callbacks_->animations_started());
226 EXPECT_TRUE(callbacks_->animations_ended());
227 EXPECT_TRUE(callbacks_->StartedEpochIsBeforeEndedEpoch());
228 }
229
230 TEST_F(CallbackLayerAnimationObserverTest,
231 SetActiveWhenAllSequencesAreAttachedButNoneWereStarted) {
232 LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
233 LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
234
235 observer_test_api_->AttachedToSequence(sequence_1);
236 observer_test_api_->AttachedToSequence(sequence_2);
237
238 observer_->SetActive();
239
240 EXPECT_TRUE(observer_->active());
241 EXPECT_FALSE(callbacks_->animations_started());
242 EXPECT_FALSE(callbacks_->animations_ended());
243 }
244
245 TEST_F(CallbackLayerAnimationObserverTest,
246 SetActiveWhenAllSequencesAreAttachedAndOnlySomeWereStarted) {
247 LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
248 LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
249
250 observer_test_api_->AttachedToSequence(sequence_1);
251 observer_test_api_->AttachedToSequence(sequence_2);
252 observer_->OnLayerAnimationStarted(sequence_1);
253
254 observer_->SetActive();
255
256 EXPECT_TRUE(observer_->active());
257 EXPECT_FALSE(callbacks_->animations_started());
258 EXPECT_FALSE(callbacks_->animations_ended());
259 }
260
261 TEST_F(CallbackLayerAnimationObserverTest,
262 SetActiveWhenAllSequencesAreAttachedAndOnlySomeWereCompleted) {
263 LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
264 LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
265
266 observer_test_api_->AttachedToSequence(sequence_1);
267 observer_test_api_->AttachedToSequence(sequence_2);
268 observer_->OnLayerAnimationStarted(sequence_1);
269 observer_->OnLayerAnimationEnded(sequence_1);
270
271 observer_->SetActive();
272
273 EXPECT_TRUE(observer_->active());
274 EXPECT_FALSE(callbacks_->animations_started());
275 EXPECT_FALSE(callbacks_->animations_ended());
276 }
277
278 TEST_F(CallbackLayerAnimationObserverTest,
279 SetActiveAfterAllSequencesWereStartedButNoneWereCompleted) {
280 LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
281 LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
282
283 observer_test_api_->AttachedToSequence(sequence_1);
284 observer_test_api_->AttachedToSequence(sequence_2);
285 observer_->OnLayerAnimationStarted(sequence_1);
286 observer_->OnLayerAnimationStarted(sequence_2);
287
288 observer_->SetActive();
289
290 EXPECT_TRUE(observer_->active());
291 EXPECT_TRUE(callbacks_->animations_started());
292 EXPECT_FALSE(callbacks_->animations_ended());
293 }
294
295 TEST_F(CallbackLayerAnimationObserverTest,
296 SetActiveWhenAllSequencesAreStartedAndOnlySomeWereCompleted) {
297 LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
298 LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
299
300 observer_test_api_->AttachedToSequence(sequence_1);
301 observer_test_api_->AttachedToSequence(sequence_2);
302 observer_->OnLayerAnimationStarted(sequence_1);
303 observer_->OnLayerAnimationStarted(sequence_2);
304 observer_->OnLayerAnimationEnded(sequence_1);
305
306 observer_->SetActive();
307
308 EXPECT_TRUE(observer_->active());
309 EXPECT_TRUE(callbacks_->animations_started());
310 EXPECT_FALSE(callbacks_->animations_ended());
311 }
312
313 TEST_F(CallbackLayerAnimationObserverTest,
314 SetActiveWhenAllSequencesWereCompleted) {
315 LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
316 LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
317
318 observer_test_api_->AttachedToSequence(sequence_1);
319 observer_test_api_->AttachedToSequence(sequence_2);
320 observer_->OnLayerAnimationStarted(sequence_1);
321 observer_->OnLayerAnimationStarted(sequence_2);
322 observer_->OnLayerAnimationEnded(sequence_1);
323 observer_->OnLayerAnimationEnded(sequence_2);
324
325 observer_->SetActive();
326
327 EXPECT_FALSE(observer_->active());
328 EXPECT_TRUE(callbacks_->animations_started());
329 EXPECT_TRUE(callbacks_->animations_ended());
330 }
331
332 TEST_F(CallbackLayerAnimationObserverTest,
333 SetActiveAgainAfterAllSequencesWereCompleted) {
334 LayerAnimationSequence* sequence_1 = CreateLayerAnimationSequence();
335 LayerAnimationSequence* sequence_2 = CreateLayerAnimationSequence();
336 LayerAnimationSequence* sequence_3 = CreateLayerAnimationSequence();
337 LayerAnimationSequence* sequence_4 = CreateLayerAnimationSequence();
338
339 observer_test_api_->AttachedToSequence(sequence_1);
340 observer_test_api_->AttachedToSequence(sequence_2);
341 observer_->OnLayerAnimationStarted(sequence_1);
342 observer_->OnLayerAnimationStarted(sequence_2);
343 observer_->OnLayerAnimationEnded(sequence_1);
344 observer_->OnLayerAnimationEnded(sequence_2);
345
346 observer_->SetActive();
347
348 EXPECT_FALSE(observer_->active());
349
350 observer_test_api_->AttachedToSequence(sequence_3);
351 observer_test_api_->AttachedToSequence(sequence_4);
352
353 callbacks_->ResetCallbackObservations();
354
355 observer_->SetActive();
356
357 EXPECT_TRUE(observer_->active());
358 EXPECT_FALSE(callbacks_->animations_started());
359 EXPECT_FALSE(callbacks_->animations_ended());
360 EXPECT_EQ(2, observer_->successful_count());
361
362 observer_->OnLayerAnimationStarted(sequence_3);
363 observer_->OnLayerAnimationStarted(sequence_4);
364
365 EXPECT_TRUE(observer_->active());
366 EXPECT_TRUE(callbacks_->animations_started());
367 EXPECT_FALSE(callbacks_->animations_ended());
368 EXPECT_EQ(2, observer_->successful_count());
369
370 observer_->OnLayerAnimationEnded(sequence_3);
371 observer_->OnLayerAnimationEnded(sequence_4);
372
373 EXPECT_FALSE(observer_->active());
374 EXPECT_TRUE(callbacks_->animations_started());
375 EXPECT_TRUE(callbacks_->animations_ended());
376 EXPECT_EQ(4, observer_->successful_count());
377 }
378
379 } // namespace test
380 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698