| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/callback_layer_animation_observer.h" | 5 #include "ui/compositor/callback_layer_animation_observer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 | 147 |
| 148 // A test specific CallbackLayerAnimationObserver that will set a bool when | 148 // A test specific CallbackLayerAnimationObserver that will set a bool when |
| 149 // destroyed. | 149 // destroyed. |
| 150 class TestCallbackLayerAnimationObserver | 150 class TestCallbackLayerAnimationObserver |
| 151 : public CallbackLayerAnimationObserver { | 151 : public CallbackLayerAnimationObserver { |
| 152 public: | 152 public: |
| 153 TestCallbackLayerAnimationObserver( | 153 TestCallbackLayerAnimationObserver( |
| 154 AnimationStartedCallback animation_started_callback, | 154 AnimationStartedCallback animation_started_callback, |
| 155 AnimationEndedCallback animation_ended_callback, | 155 AnimationEndedCallback animation_ended_callback, |
| 156 bool* destroyed); | 156 bool* destroyed); |
| 157 |
| 158 TestCallbackLayerAnimationObserver( |
| 159 AnimationStartedCallback animation_started_callback, |
| 160 bool should_delete_observer, |
| 161 bool* destroyed); |
| 162 |
| 163 TestCallbackLayerAnimationObserver( |
| 164 AnimationEndedCallback animation_ended_callback, |
| 165 bool* destroyed); |
| 166 |
| 157 ~TestCallbackLayerAnimationObserver() override; | 167 ~TestCallbackLayerAnimationObserver() override; |
| 158 | 168 |
| 159 private: | 169 private: |
| 160 bool* destroyed_; | 170 bool* destroyed_; |
| 161 | 171 |
| 162 DISALLOW_COPY_AND_ASSIGN(TestCallbackLayerAnimationObserver); | 172 DISALLOW_COPY_AND_ASSIGN(TestCallbackLayerAnimationObserver); |
| 163 }; | 173 }; |
| 164 | 174 |
| 165 TestCallbackLayerAnimationObserver::TestCallbackLayerAnimationObserver( | 175 TestCallbackLayerAnimationObserver::TestCallbackLayerAnimationObserver( |
| 166 AnimationStartedCallback animation_started_callback, | 176 AnimationStartedCallback animation_started_callback, |
| 167 AnimationEndedCallback animation_ended_callback, | 177 AnimationEndedCallback animation_ended_callback, |
| 168 bool* destroyed) | 178 bool* destroyed) |
| 169 : CallbackLayerAnimationObserver(animation_started_callback, | 179 : CallbackLayerAnimationObserver(animation_started_callback, |
| 170 animation_ended_callback), | 180 animation_ended_callback), |
| 171 destroyed_(destroyed) { | 181 destroyed_(destroyed) { |
| 172 (*destroyed_) = false; | 182 if (destroyed_) |
| 183 (*destroyed_) = false; |
| 184 } |
| 185 |
| 186 TestCallbackLayerAnimationObserver::TestCallbackLayerAnimationObserver( |
| 187 AnimationStartedCallback animation_started_callback, |
| 188 bool should_delete_observer, |
| 189 bool* destroyed) |
| 190 : CallbackLayerAnimationObserver(animation_started_callback, |
| 191 should_delete_observer), |
| 192 destroyed_(destroyed) { |
| 193 if (destroyed_) |
| 194 (*destroyed_) = false; |
| 195 } |
| 196 |
| 197 TestCallbackLayerAnimationObserver::TestCallbackLayerAnimationObserver( |
| 198 AnimationEndedCallback animation_ended_callback, |
| 199 bool* destroyed) |
| 200 : CallbackLayerAnimationObserver(animation_ended_callback), |
| 201 destroyed_(destroyed) { |
| 202 if (destroyed_) |
| 203 (*destroyed_) = false; |
| 173 } | 204 } |
| 174 | 205 |
| 175 TestCallbackLayerAnimationObserver::~TestCallbackLayerAnimationObserver() { | 206 TestCallbackLayerAnimationObserver::~TestCallbackLayerAnimationObserver() { |
| 176 (*destroyed_) = true; | 207 if (destroyed_) |
| 208 (*destroyed_) = true; |
| 177 } | 209 } |
| 178 | 210 |
| 179 class CallbackLayerAnimationObserverTest : public testing::Test { | 211 class CallbackLayerAnimationObserverTest : public testing::Test { |
| 180 public: | 212 public: |
| 181 CallbackLayerAnimationObserverTest(); | 213 CallbackLayerAnimationObserverTest(); |
| 182 ~CallbackLayerAnimationObserverTest() override; | 214 ~CallbackLayerAnimationObserverTest() override; |
| 183 | 215 |
| 184 protected: | 216 protected: |
| 185 // Creates a LayerAnimationSequence. The lifetime of the sequence will be | 217 // Creates a LayerAnimationSequence. The lifetime of the sequence will be |
| 186 // managed by this. | 218 // managed by this. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 217 observer_.reset(); | 249 observer_.reset(); |
| 218 } | 250 } |
| 219 | 251 |
| 220 LayerAnimationSequence* | 252 LayerAnimationSequence* |
| 221 CallbackLayerAnimationObserverTest::CreateLayerAnimationSequence() { | 253 CallbackLayerAnimationObserverTest::CreateLayerAnimationSequence() { |
| 222 LayerAnimationSequence* sequence = new LayerAnimationSequence(); | 254 LayerAnimationSequence* sequence = new LayerAnimationSequence(); |
| 223 sequences_.push_back(sequence); | 255 sequences_.push_back(sequence); |
| 224 return sequence; | 256 return sequence; |
| 225 } | 257 } |
| 226 | 258 |
| 227 TEST_F(CallbackLayerAnimationObserverTest, VerifyInitialState) { | 259 TEST(CallbackLayerAnimationObserverDestructionTest, VerifyFalseAutoDelete) { |
| 228 EXPECT_FALSE(observer_->active()); | 260 TestCallbacks callbacks; |
| 229 EXPECT_EQ(0, observer_->aborted_count()); | 261 callbacks.set_should_delete_observer_on_animations_ended(false); |
| 230 EXPECT_EQ(0, observer_->successful_count()); | |
| 231 | 262 |
| 232 EXPECT_FALSE(callbacks_->animations_started()); | 263 bool is_destroyed = false; |
| 233 EXPECT_FALSE(callbacks_->animations_ended()); | 264 |
| 265 TestCallbackLayerAnimationObserver* observer = |
| 266 new TestCallbackLayerAnimationObserver( |
| 267 base::Bind(&TestCallbacks::AnimationsStarted, |
| 268 base::Unretained(&callbacks)), |
| 269 false, &is_destroyed); |
| 270 observer->SetActive(); |
| 271 |
| 272 EXPECT_FALSE(is_destroyed); |
| 273 delete observer; |
| 274 } |
| 275 |
| 276 TEST(CallbackLayerAnimationObserverDestructionTest, VerifyTrueAutoDelete) { |
| 277 TestCallbacks callbacks; |
| 278 callbacks.set_should_delete_observer_on_animations_ended(false); |
| 279 |
| 280 bool is_destroyed = false; |
| 281 |
| 282 TestCallbackLayerAnimationObserver* observer = |
| 283 new TestCallbackLayerAnimationObserver( |
| 284 base::Bind(&TestCallbacks::AnimationsStarted, |
| 285 base::Unretained(&callbacks)), |
| 286 true, &is_destroyed); |
| 287 observer->SetActive(); |
| 288 |
| 289 EXPECT_TRUE(is_destroyed); |
| 234 } | 290 } |
| 235 | 291 |
| 236 TEST(CallbackLayerAnimationObserverDestructionTest, | 292 TEST(CallbackLayerAnimationObserverDestructionTest, |
| 237 AnimationEndedReturnsFalse) { | 293 AnimationEndedReturnsFalse) { |
| 238 TestCallbacks callbacks; | 294 TestCallbacks callbacks; |
| 239 callbacks.set_should_delete_observer_on_animations_ended(false); | 295 callbacks.set_should_delete_observer_on_animations_ended(false); |
| 240 | 296 |
| 241 bool is_destroyed = false; | 297 bool is_destroyed = false; |
| 242 | 298 |
| 243 TestCallbackLayerAnimationObserver* observer = | 299 TestCallbackLayerAnimationObserver* observer = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 264 base::Bind(&TestCallbacks::AnimationsStarted, | 320 base::Bind(&TestCallbacks::AnimationsStarted, |
| 265 base::Unretained(&callbacks)), | 321 base::Unretained(&callbacks)), |
| 266 base::Bind(&TestCallbacks::AnimationsEnded, | 322 base::Bind(&TestCallbacks::AnimationsEnded, |
| 267 base::Unretained(&callbacks)), | 323 base::Unretained(&callbacks)), |
| 268 &is_destroyed); | 324 &is_destroyed); |
| 269 observer->SetActive(); | 325 observer->SetActive(); |
| 270 | 326 |
| 271 EXPECT_TRUE(is_destroyed); | 327 EXPECT_TRUE(is_destroyed); |
| 272 } | 328 } |
| 273 | 329 |
| 330 TEST_F(CallbackLayerAnimationObserverTest, VerifyInitialState) { |
| 331 EXPECT_FALSE(observer_->active()); |
| 332 EXPECT_EQ(0, observer_->aborted_count()); |
| 333 EXPECT_EQ(0, observer_->successful_count()); |
| 334 |
| 335 EXPECT_FALSE(callbacks_->animations_started()); |
| 336 EXPECT_FALSE(callbacks_->animations_ended()); |
| 337 } |
| 338 |
| 274 // Verifies that the CallbackLayerAnimationObserver is robust to explicit | 339 // Verifies that the CallbackLayerAnimationObserver is robust to explicit |
| 275 // deletes caused as a side effect of calling the AnimationsStartedCallback() | 340 // deletes caused as a side effect of calling the AnimationsStartedCallback() |
| 276 // when there are no animation sequences attached. This test also guards against | 341 // when there are no animation sequences attached. This test also guards against |
| 277 // heap-use-after-free errors. | 342 // heap-use-after-free errors. |
| 278 TEST_F( | 343 TEST_F( |
| 279 CallbackLayerAnimationObserverTest, | 344 CallbackLayerAnimationObserverTest, |
| 280 ExplicitlyDeleteObserverInAnimationStartedCallbackWithNoSequencesAttached) { | 345 ExplicitlyDeleteObserverInAnimationStartedCallbackWithNoSequencesAttached) { |
| 281 TestCallbacksThatExplicitlyDeletesObserver callbacks; | 346 TestCallbacksThatExplicitlyDeletesObserver callbacks; |
| 282 callbacks.set_should_delete_observer_on_animations_ended(true); | 347 callbacks.set_should_delete_observer_on_animations_ended(true); |
| 283 | 348 |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 observer_->OnLayerAnimationEnded(sequence_4); | 578 observer_->OnLayerAnimationEnded(sequence_4); |
| 514 | 579 |
| 515 EXPECT_FALSE(observer_->active()); | 580 EXPECT_FALSE(observer_->active()); |
| 516 EXPECT_TRUE(callbacks_->animations_started()); | 581 EXPECT_TRUE(callbacks_->animations_started()); |
| 517 EXPECT_TRUE(callbacks_->animations_ended()); | 582 EXPECT_TRUE(callbacks_->animations_ended()); |
| 518 EXPECT_EQ(4, observer_->successful_count()); | 583 EXPECT_EQ(4, observer_->successful_count()); |
| 519 } | 584 } |
| 520 | 585 |
| 521 } // namespace test | 586 } // namespace test |
| 522 } // namespace ui | 587 } // namespace ui |
| OLD | NEW |