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

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

Issue 2473483013: [ash-md] Enhanced LayerAnimator to allow animations to be aborted/started in OnLayerAnimationStarte… (Closed)
Patch Set: Added test to expose bug and fixed bug. Created 4 years, 1 month 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
« no previous file with comments | « ui/compositor/layer_animator.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/compositor/layer_animator.h" 5 #include "ui/compositor/layer_animator.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string>
9 #include <vector>
8 10
9 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
10 #include "base/macros.h" 12 #include "base/macros.h"
11 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
12 #include "base/time/time.h" 14 #include "base/time/time.h"
13 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/compositor/layer.h" 16 #include "ui/compositor/layer.h"
15 #include "ui/compositor/layer_animation_delegate.h" 17 #include "ui/compositor/layer_animation_delegate.h"
16 #include "ui/compositor/layer_animation_element.h" 18 #include "ui/compositor/layer_animation_element.h"
17 #include "ui/compositor/layer_animation_sequence.h" 19 #include "ui/compositor/layer_animation_sequence.h"
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 140
139 bool animations_completed_; 141 bool animations_completed_;
140 bool notify_when_animator_destructed_; 142 bool notify_when_animator_destructed_;
141 143
142 DISALLOW_COPY_AND_ASSIGN(TestImplicitAnimationObserver); 144 DISALLOW_COPY_AND_ASSIGN(TestImplicitAnimationObserver);
143 }; 145 };
144 146
145 // When notified that an animation has ended, stops all other animations. 147 // When notified that an animation has ended, stops all other animations.
146 class DeletingLayerAnimationObserver : public LayerAnimationObserver { 148 class DeletingLayerAnimationObserver : public LayerAnimationObserver {
147 public: 149 public:
148 DeletingLayerAnimationObserver(LayerAnimator* animator) 150 explicit DeletingLayerAnimationObserver(LayerAnimator* animator)
149 : animator_(animator) { 151 : animator_(animator) {}
150 }
151 152
152 void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override { 153 void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override {
153 animator_->StopAnimating(); 154 animator_->StopAnimating();
154 } 155 }
155 156
156 void OnLayerAnimationAborted(LayerAnimationSequence* sequence) override { 157 void OnLayerAnimationAborted(LayerAnimationSequence* sequence) override {
157 animator_->StopAnimating(); 158 animator_->StopAnimating();
158 } 159 }
159 160
160 void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) override {} 161 void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) override {}
161 162
162 private: 163 private:
163 LayerAnimator* animator_; 164 LayerAnimator* animator_;
164 165
165 DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver); 166 DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver);
166 }; 167 };
167 168
169 // When notified that an animation has started, aborts all animations.
170 class AbortAnimationsOnStartedLayerAnimationObserver
171 : public LayerAnimationObserver {
172 public:
173 explicit AbortAnimationsOnStartedLayerAnimationObserver(
174 LayerAnimator* animator)
175 : animator_(animator) {}
176
177 void OnLayerAnimationStarted(LayerAnimationSequence* sequence) override {
178 animator_->AbortAllAnimations();
179 }
180
181 void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override {}
182
183 void OnLayerAnimationAborted(LayerAnimationSequence* sequence) override {}
184
185 void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) override {}
186
187 private:
188 LayerAnimator* animator_;
189
190 DISALLOW_COPY_AND_ASSIGN(AbortAnimationsOnStartedLayerAnimationObserver);
191 };
192
168 class LayerAnimatorDestructionObserver { 193 class LayerAnimatorDestructionObserver {
169 public: 194 public:
170 LayerAnimatorDestructionObserver() : animator_deleted_(false) {} 195 LayerAnimatorDestructionObserver() : animator_deleted_(false) {}
171 virtual ~LayerAnimatorDestructionObserver() {} 196 virtual ~LayerAnimatorDestructionObserver() {}
172 197
173 void NotifyAnimatorDeleted() { 198 void NotifyAnimatorDeleted() {
174 animator_deleted_ = true; 199 animator_deleted_ = true;
175 } 200 }
176 201
177 bool IsAnimatorDeleted() { 202 bool IsAnimatorDeleted() {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 } 250 }
226 251
227 ~TestLayerAnimationSequence() override { (*num_live_instances_)--; } 252 ~TestLayerAnimationSequence() override { (*num_live_instances_)--; }
228 253
229 private: 254 private:
230 int* num_live_instances_; 255 int* num_live_instances_;
231 256
232 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence); 257 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence);
233 }; 258 };
234 259
235 } // namespace 260 } // namespace
261
262 namespace test {
263
264 // Tracks the number of calls to the LayerAnimationObserver overrides and fails
265 // CHECKS if the counts are inconsistent, e.g. the number of started sequences
266 // is greater than the number of attached sequences.
267 class CountCheckingLayerAnimationObserver : public LayerAnimationObserver {
268 public:
269 explicit CountCheckingLayerAnimationObserver(LayerAnimationObserver* observer)
270 : observer_(observer) {}
271
272 void OnLayerAnimationStarted(LayerAnimationSequence* sequence) override {
273 ++started_count_;
274 CHECK_LE(started_count_, attached_sequence_count_);
ajuma 2016/11/04 22:49:40 Given that this is happening inside a test, should
bruthig 2016/11/04 22:58:20 Done.
275 CHECK_LE(started_count_, sequences_scheduled_);
276 if (observer_)
277 observer_->OnLayerAnimationStarted(sequence);
278 }
279
280 void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override {
281 ++successful_count_;
282 CHECK_LE(GetNumSequencesCompleted(), sequences_scheduled_)
283 << " aborted_count_=" << aborted_count_
284 << " successful_count_=" << successful_count_;
285 CHECK_LE(GetNumSequencesCompleted(), attached_sequence_count_)
286 << " aborted_count_=" << aborted_count_
287 << " successful_count_=" << successful_count_;
288 if (observer_)
289 observer_->OnLayerAnimationEnded(sequence);
290 }
291
292 void OnLayerAnimationAborted(LayerAnimationSequence* sequence) override {
293 ++aborted_count_;
294 CHECK_LE(GetNumSequencesCompleted(), sequences_scheduled_)
295 << " aborted_count_=" << aborted_count_
296 << " successful_count_=" << successful_count_;
297 CHECK_LE(GetNumSequencesCompleted(), attached_sequence_count_)
298 << " aborted_count_=" << aborted_count_
299 << " successful_count_=" << successful_count_;
300 if (observer_)
301 observer_->OnLayerAnimationAborted(sequence);
302 }
303
304 void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) override {
305 ++sequences_scheduled_;
306 CHECK_LE(sequences_scheduled_, attached_sequence_count_);
307 if (observer_)
308 observer_->OnLayerAnimationScheduled(sequence);
309 }
310
311 int GetNumSequencesCompleted() { return aborted_count_ + successful_count_; }
312
313 protected:
314 void OnAttachedToSequence(LayerAnimationSequence* sequence) override {
315 ++attached_sequence_count_;
316 if (observer_)
317 observer_->OnAttachedToSequence(sequence);
318 }
319
320 void OnDetachedFromSequence(LayerAnimationSequence* sequence) override {
321 ++detached_sequence_count_;
322 CHECK_LE(detached_sequence_count_, attached_sequence_count_);
323 CHECK_LE(detached_sequence_count_, GetNumSequencesCompleted())
324 << " aborted_count_=" << aborted_count_
325 << " successful_count_=" << successful_count_;
326 if (observer_)
327 observer_->OnDetachedFromSequence(sequence);
328 }
329
330 private:
331 // Observer to delegate calls to.
ajuma 2016/11/04 22:49:40 nit: "the delegate"
bruthig 2016/11/04 22:58:20 Not exactly sure what you are suggesting. I've up
332 LayerAnimationObserver* observer_;
333
334 // The total number of animation sequences that have been attached.
335 int attached_sequence_count_ = 0;
336
337 // The total number of animation sequences that have been detached.
338 int detached_sequence_count_ = 0;
339
340 // The total number of animation sequences that have been scheduled.
341 int sequences_scheduled_ = 0;
342
343 // The number of animation sequences that have been started.
344 int started_count_ = 0;
345
346 // The number of animation sequences that were aborted.
347 int aborted_count_ = 0;
348
349 // The number of animation sequences that completed successfully.
350 int successful_count_ = 0;
351
352 DISALLOW_COPY_AND_ASSIGN(CountCheckingLayerAnimationObserver);
353 };
354
355 } // namespace test
236 356
237 // Checks that setting a property on an implicit animator causes an animation to 357 // Checks that setting a property on an implicit animator causes an animation to
238 // happen. 358 // happen.
239 TEST(LayerAnimatorTest, ImplicitAnimation) { 359 TEST(LayerAnimatorTest, ImplicitAnimation) {
240 TestLayerAnimationDelegate delegate; 360 TestLayerAnimationDelegate delegate;
241 scoped_refptr<LayerAnimator> animator(CreateImplicitTestAnimator(&delegate)); 361 scoped_refptr<LayerAnimator> animator(CreateImplicitTestAnimator(&delegate));
242 base::TimeTicks now = base::TimeTicks::Now(); 362 base::TimeTicks now = base::TimeTicks::Now();
243 animator->SetBrightness(0.5); 363 animator->SetBrightness(0.5);
244 EXPECT_TRUE(animator->is_animating()); 364 EXPECT_TRUE(animator->is_animating());
245 animator->Step(now + base::TimeDelta::FromSeconds(1)); 365 animator->Step(now + base::TimeDelta::FromSeconds(1));
(...skipping 1890 matching lines...) Expand 10 before | Expand all | Expand 10 after
2136 animator->SchedulePauseForProperties( 2256 animator->SchedulePauseForProperties(
2137 base::TimeDelta::FromMilliseconds(100), 2257 base::TimeDelta::FromMilliseconds(100),
2138 LayerAnimationElement::TRANSFORM | LayerAnimationElement::BOUNDS); 2258 LayerAnimationElement::TRANSFORM | LayerAnimationElement::BOUNDS);
2139 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::TRANSFORM)); 2259 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::TRANSFORM));
2140 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS)); 2260 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2141 EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::OPACITY)); 2261 EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::OPACITY));
2142 } 2262 }
2143 2263
2144 2264
2145 class AnimatorOwner { 2265 class AnimatorOwner {
2146 public: 2266 public:
2147 AnimatorOwner() : animator_(CreateDefaultTestAnimator()) {} 2267 AnimatorOwner() : animator_(CreateDefaultTestAnimator()) {}
2148 2268
2149 LayerAnimator* animator() { return animator_.get(); } 2269 LayerAnimator* animator() { return animator_.get(); }
2150 2270
2151 private: 2271 private:
2152 scoped_refptr<LayerAnimator> animator_; 2272 scoped_refptr<LayerAnimator> animator_;
2153 2273
2154 DISALLOW_COPY_AND_ASSIGN(AnimatorOwner); 2274 DISALLOW_COPY_AND_ASSIGN(AnimatorOwner);
2155 }; 2275 };
2156 2276
2157 class DeletingObserver : public LayerAnimationObserver { 2277 class DeletingObserver : public LayerAnimationObserver {
2158 public: 2278 public:
2159 DeletingObserver(bool* was_deleted) 2279 explicit DeletingObserver(bool* was_deleted)
2160 : animator_owner_(new AnimatorOwner), 2280 : animator_owner_(new AnimatorOwner),
2161 delete_on_animation_ended_(false), 2281 delete_on_animation_ended_(false),
2162 delete_on_animation_aborted_(false), 2282 delete_on_animation_aborted_(false),
2163 delete_on_animation_scheduled_(false), 2283 delete_on_animation_scheduled_(false),
2164 was_deleted_(was_deleted) { 2284 was_deleted_(was_deleted) {
2165 animator()->AddObserver(this); 2285 animator()->AddObserver(this);
2166 } 2286 }
2167 2287
2168 ~DeletingObserver() override { 2288 ~DeletingObserver() override {
2169 animator()->RemoveObserver(this); 2289 animator()->RemoveObserver(this);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2202 void OnLayerAnimationAborted(LayerAnimationSequence* sequence) override { 2322 void OnLayerAnimationAborted(LayerAnimationSequence* sequence) override {
2203 if (delete_on_animation_aborted_) 2323 if (delete_on_animation_aborted_)
2204 delete this; 2324 delete this;
2205 } 2325 }
2206 2326
2207 void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) override { 2327 void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) override {
2208 if (delete_on_animation_scheduled_) 2328 if (delete_on_animation_scheduled_)
2209 delete this; 2329 delete this;
2210 } 2330 }
2211 2331
2212 private: 2332 private:
2213 std::unique_ptr<AnimatorOwner> animator_owner_; 2333 std::unique_ptr<AnimatorOwner> animator_owner_;
2214 bool delete_on_animation_ended_; 2334 bool delete_on_animation_ended_;
2215 bool delete_on_animation_aborted_; 2335 bool delete_on_animation_aborted_;
2216 bool delete_on_animation_scheduled_; 2336 bool delete_on_animation_scheduled_;
2217 bool* was_deleted_; 2337 bool* was_deleted_;
2218 2338
2219 DISALLOW_COPY_AND_ASSIGN(DeletingObserver); 2339 DISALLOW_COPY_AND_ASSIGN(DeletingObserver);
2220 }; 2340 };
2221 2341
2222 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterFinishingAnimation) { 2342 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterFinishingAnimation) {
2223 bool observer_was_deleted = false; 2343 bool observer_was_deleted = false;
2224 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted); 2344 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2225 observer->set_delete_on_animation_ended(true); 2345 observer->set_delete_on_animation_ended(true);
2226 observer->set_delete_on_animation_aborted(true); 2346 observer->set_delete_on_animation_aborted(true);
2227 LayerAnimator* animator = observer->animator(); 2347 LayerAnimator* animator = observer->animator();
2228 TestLayerAnimationDelegate delegate; 2348 TestLayerAnimationDelegate delegate;
2229 animator->SetDelegate(&delegate); 2349 animator->SetDelegate(&delegate);
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
2405 collection->StartAnimator(layer.GetAnimator()); 2525 collection->StartAnimator(layer.GetAnimator());
2406 EXPECT_TRUE(collection->HasActiveAnimators()); 2526 EXPECT_TRUE(collection->HasActiveAnimators());
2407 EXPECT_FALSE(collection->last_tick_time().is_null()); 2527 EXPECT_FALSE(collection->last_tick_time().is_null());
2408 2528
2409 collection->StopAnimator(layer.GetAnimator()); 2529 collection->StopAnimator(layer.GetAnimator());
2410 EXPECT_FALSE(collection->HasActiveAnimators()); 2530 EXPECT_FALSE(collection->HasActiveAnimators());
2411 2531
2412 animator->SetDelegate(nullptr); 2532 animator->SetDelegate(nullptr);
2413 } 2533 }
2414 2534
2535 // Verifies that sequences are removed from the animation queue prior to be
2536 // notified of being started. This will allow observers to abort animations in
2537 // the OnLayerAnimationStarted() callback without triggering a sequence to be
2538 // started a second time.
2539 TEST(LayerAnimatorTest,
2540 SequencesAreRemovedFromQueueBeforeBeingNotifiedOfStarted) {
2541 TestLayerAnimationDelegate delegate;
2542 scoped_refptr<LayerAnimator> animator(CreateDefaultTestAnimator(&delegate));
2543
2544 AbortAnimationsOnStartedLayerAnimationObserver aborting_observer(
2545 animator.get());
2546 test::CountCheckingLayerAnimationObserver counting_observer(
2547 &aborting_observer);
2548
2549 const base::TimeDelta sequence_duration = base::TimeDelta::FromSeconds(1);
2550
2551 animator->ScheduleAnimation(new LayerAnimationSequence(
2552 LayerAnimationElement::CreateBrightnessElement(1.f, sequence_duration)));
2553
2554 LayerAnimationSequence* observed_sequence = new LayerAnimationSequence(
2555 LayerAnimationElement::CreateBrightnessElement(0.f, sequence_duration));
2556 observed_sequence->AddObserver(&counting_observer);
2557 animator->ScheduleAnimation(observed_sequence);
2558
2559 EXPECT_TRUE(animator->is_animating());
2560 const base::TimeTicks start_time = animator->last_step_time();
2561
2562 animator->Step(start_time + sequence_duration * 3);
ajuma 2016/11/04 22:49:40 Please add an EXPECT_FALSE(animator->is_animating(
bruthig 2016/11/04 22:58:20 Done.
2563 }
2564
2415 TEST(LayerAnimatorTest, AnimatorStartedCorrectly) { 2565 TEST(LayerAnimatorTest, AnimatorStartedCorrectly) {
2416 Layer layer; 2566 Layer layer;
2417 LayerAnimatorTestController test_controller(layer.GetAnimator()); 2567 LayerAnimatorTestController test_controller(layer.GetAnimator());
2418 LayerAnimator* animator = test_controller.animator(); 2568 LayerAnimator* animator = test_controller.animator();
2419 ASSERT_FALSE(animator->is_started_); 2569 ASSERT_FALSE(animator->is_started_);
2420 2570
2421 TestLayerAnimationDelegate test_delegate; 2571 TestLayerAnimationDelegate test_delegate;
2422 animator->SetDelegate(&test_delegate); 2572 animator->SetDelegate(&test_delegate);
2423 double target_opacity = 1.0; 2573 double target_opacity = 1.0;
2424 base::TimeDelta time_delta = base::TimeDelta::FromSeconds(1); 2574 base::TimeDelta time_delta = base::TimeDelta::FromSeconds(1);
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
2532 2682
2533 root.Add(&layer); 2683 root.Add(&layer);
2534 EXPECT_TRUE(layer.cc_layer_for_testing()->HasActiveAnimationForTesting()); 2684 EXPECT_TRUE(layer.cc_layer_for_testing()->HasActiveAnimationForTesting());
2535 2685
2536 host.reset(); 2686 host.reset();
2537 TerminateContextFactoryForTests(); 2687 TerminateContextFactoryForTests();
2538 } 2688 }
2539 2689
2540 class LayerOwnerAnimationObserver : public LayerAnimationObserver { 2690 class LayerOwnerAnimationObserver : public LayerAnimationObserver {
2541 public: 2691 public:
2542 LayerOwnerAnimationObserver(LayerAnimator* animator) 2692 explicit LayerOwnerAnimationObserver(LayerAnimator* animator)
2543 : animator_layer_(new Layer(LAYER_TEXTURED)) { 2693 : animator_layer_(new Layer(LAYER_TEXTURED)) {
2544 animator_layer_->SetAnimator(animator); 2694 animator_layer_->SetAnimator(animator);
2545 } 2695 }
2546 2696
2547 ~LayerOwnerAnimationObserver() override {} 2697 ~LayerOwnerAnimationObserver() override {}
2548 2698
2549 void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override { 2699 void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override {
2550 ASSERT_TRUE(sequence); 2700 ASSERT_TRUE(sequence);
2551 animator_layer_.reset(); 2701 animator_layer_.reset();
2552 } 2702 }
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
2794 EXPECT_EQ(observer.last_ended_sequence(), nullptr); 2944 EXPECT_EQ(observer.last_ended_sequence(), nullptr);
2795 EXPECT_EQ(observer.last_detached_sequence(), first_sequence); 2945 EXPECT_EQ(observer.last_detached_sequence(), first_sequence);
2796 2946
2797 EXPECT_TRUE(observer.AbortedEpochIsBeforeDetachedEpoch()); 2947 EXPECT_TRUE(observer.AbortedEpochIsBeforeDetachedEpoch());
2798 EXPECT_TRUE(observer.AbortedEpochIsBeforeStartedEpoch()); 2948 EXPECT_TRUE(observer.AbortedEpochIsBeforeStartedEpoch());
2799 EXPECT_TRUE(observer.AttachedEpochIsBeforeScheduledEpoch()); 2949 EXPECT_TRUE(observer.AttachedEpochIsBeforeScheduledEpoch());
2800 EXPECT_TRUE(observer.ScheduledEpochIsBeforeStartedEpoch()); 2950 EXPECT_TRUE(observer.ScheduledEpochIsBeforeStartedEpoch());
2801 } 2951 }
2802 2952
2803 } // namespace ui 2953 } // namespace ui
OLDNEW
« no previous file with comments | « ui/compositor/layer_animator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698