Commit e5575e16 authored by Sammie Quon's avatar Sammie Quon Committed by Commit Bot

overview: Add some DelayedObserverImpl tests.

These were part of [1], but made the diff difficult to read so moved
them to a separate cl. Also renamed StartAnimationObserver to
EnterAnimationObserver as it makes more sense in the context.

[1] https://chromium-review.googlesource.com/c/chromium/src/+/1564881

Test: added tests
Bug: 914147
Change-Id: Iaf878bf2255d3c5ac585d52fc32cdf9f15cf8029
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1566361
Commit-Queue: Sammie Quon <sammiequon@chromium.org>
Reviewed-by: default avatarMitsuru Oshima <oshima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#650504}
parent f2e882f3
...@@ -45,9 +45,9 @@ class TestOverviewDelegate : public OverviewDelegate { ...@@ -45,9 +45,9 @@ class TestOverviewDelegate : public OverviewDelegate {
base::EraseIf(observers_, base::MatchesUniquePtr(animation_observer)); base::EraseIf(observers_, base::MatchesUniquePtr(animation_observer));
} }
void AddStartAnimationObserver( void AddEnterAnimationObserver(
std::unique_ptr<DelayedAnimationObserver> animation_observer) override {} std::unique_ptr<DelayedAnimationObserver> animation_observer) override {}
void RemoveAndDestroyStartAnimationObserver( void RemoveAndDestroyEnterAnimationObserver(
DelayedAnimationObserver* animation_observer) override {} DelayedAnimationObserver* animation_observer) override {}
private: private:
......
...@@ -30,24 +30,24 @@ void ForceDelayObserver::Shutdown() { ...@@ -30,24 +30,24 @@ void ForceDelayObserver::Shutdown() {
void ForceDelayObserver::Finish() { void ForceDelayObserver::Finish() {
if (owner_) if (owner_)
owner_->RemoveAndDestroyStartAnimationObserver(this); owner_->RemoveAndDestroyEnterAnimationObserver(this);
} }
StartAnimationObserver::StartAnimationObserver() = default; EnterAnimationObserver::EnterAnimationObserver() = default;
StartAnimationObserver::~StartAnimationObserver() = default; EnterAnimationObserver::~EnterAnimationObserver() = default;
void StartAnimationObserver::OnImplicitAnimationsCompleted() { void EnterAnimationObserver::OnImplicitAnimationsCompleted() {
if (owner_) if (owner_)
owner_->RemoveAndDestroyStartAnimationObserver(this); owner_->RemoveAndDestroyEnterAnimationObserver(this);
} }
void StartAnimationObserver::SetOwner(OverviewDelegate* owner) { void EnterAnimationObserver::SetOwner(OverviewDelegate* owner) {
DCHECK(!owner_); DCHECK(!owner_);
owner_ = owner; owner_ = owner;
} }
void StartAnimationObserver::Shutdown() { void EnterAnimationObserver::Shutdown() {
owner_ = nullptr; owner_ = nullptr;
} }
......
...@@ -13,6 +13,9 @@ ...@@ -13,6 +13,9 @@
namespace ash { namespace ash {
class OverviewDelegate; class OverviewDelegate;
// An observer that does not watch any animation, but instead has a timeout
// before telling its owner to destroy it. It is used when entering overview
// without any animations but we still want to delay some tasks.
class ASH_EXPORT ForceDelayObserver : public DelayedAnimationObserver { class ASH_EXPORT ForceDelayObserver : public DelayedAnimationObserver {
public: public:
explicit ForceDelayObserver(base::TimeDelta delay); explicit ForceDelayObserver(base::TimeDelta delay);
...@@ -33,11 +36,11 @@ class ASH_EXPORT ForceDelayObserver : public DelayedAnimationObserver { ...@@ -33,11 +36,11 @@ class ASH_EXPORT ForceDelayObserver : public DelayedAnimationObserver {
// An observer which watches a overview enter animation and signals its owner // An observer which watches a overview enter animation and signals its owner
// when the animation it is watching finishes. // when the animation it is watching finishes.
class ASH_EXPORT StartAnimationObserver : public ui::ImplicitAnimationObserver, class ASH_EXPORT EnterAnimationObserver : public ui::ImplicitAnimationObserver,
public DelayedAnimationObserver { public DelayedAnimationObserver {
public: public:
StartAnimationObserver(); EnterAnimationObserver();
~StartAnimationObserver() override; ~EnterAnimationObserver() override;
// ui::ImplicitAnimationObserver: // ui::ImplicitAnimationObserver:
void OnImplicitAnimationsCompleted() override; void OnImplicitAnimationsCompleted() override;
...@@ -49,9 +52,11 @@ class ASH_EXPORT StartAnimationObserver : public ui::ImplicitAnimationObserver, ...@@ -49,9 +52,11 @@ class ASH_EXPORT StartAnimationObserver : public ui::ImplicitAnimationObserver,
private: private:
OverviewDelegate* owner_ = nullptr; OverviewDelegate* owner_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(StartAnimationObserver); DISALLOW_COPY_AND_ASSIGN(EnterAnimationObserver);
}; };
// An observer which watches a overview exit animation and signals its owner
// when the animation it is watching finishes.
class ASH_EXPORT ExitAnimationObserver : public ui::ImplicitAnimationObserver, class ASH_EXPORT ExitAnimationObserver : public ui::ImplicitAnimationObserver,
public DelayedAnimationObserver { public DelayedAnimationObserver {
public: public:
......
...@@ -27,22 +27,29 @@ class TestOverviewDelegate : public OverviewDelegate { ...@@ -27,22 +27,29 @@ class TestOverviewDelegate : public OverviewDelegate {
// OverviewDelegate: // OverviewDelegate:
void OnSelectionEnded() override {} void OnSelectionEnded() override {}
void AddExitAnimationObserver( void AddExitAnimationObserver(
std::unique_ptr<DelayedAnimationObserver> animation_observer) override {} std::unique_ptr<DelayedAnimationObserver> animation_observer) override {
animation_observer->SetOwner(this);
exit_observers_.push_back(std::move(animation_observer));
}
void RemoveAndDestroyExitAnimationObserver( void RemoveAndDestroyExitAnimationObserver(
DelayedAnimationObserver* animation_observer) override {} DelayedAnimationObserver* animation_observer) override {
void AddStartAnimationObserver( base::EraseIf(exit_observers_, base::MatchesUniquePtr(animation_observer));
}
void AddEnterAnimationObserver(
std::unique_ptr<DelayedAnimationObserver> animation_observer) override { std::unique_ptr<DelayedAnimationObserver> animation_observer) override {
animation_observer->SetOwner(this); animation_observer->SetOwner(this);
enter_observers_.push_back(std::move(animation_observer)); enter_observers_.push_back(std::move(animation_observer));
} }
void RemoveAndDestroyStartAnimationObserver( void RemoveAndDestroyEnterAnimationObserver(
DelayedAnimationObserver* animation_observer) override { DelayedAnimationObserver* animation_observer) override {
base::EraseIf(enter_observers_, base::MatchesUniquePtr(animation_observer)); base::EraseIf(enter_observers_, base::MatchesUniquePtr(animation_observer));
} }
size_t num_exit_observers() const { return exit_observers_.size(); }
size_t num_enter_observers() const { return enter_observers_.size(); } size_t num_enter_observers() const { return enter_observers_.size(); }
private: private:
std::vector<std::unique_ptr<DelayedAnimationObserver>> exit_observers_;
std::vector<std::unique_ptr<DelayedAnimationObserver>> enter_observers_; std::vector<std::unique_ptr<DelayedAnimationObserver>> enter_observers_;
DISALLOW_COPY_AND_ASSIGN(TestOverviewDelegate); DISALLOW_COPY_AND_ASSIGN(TestOverviewDelegate);
...@@ -50,10 +57,45 @@ class TestOverviewDelegate : public OverviewDelegate { ...@@ -50,10 +57,45 @@ class TestOverviewDelegate : public OverviewDelegate {
} // namespace } // namespace
using StartAnimationObserverTest = AshTestBase; class ForceDelayObserverTest : public AshTestBase {
public:
ForceDelayObserverTest() {
DestroyScopedTaskEnvironment();
scoped_task_environment_ =
std::make_unique<base::test::ScopedTaskEnvironment>(
base::test::ScopedTaskEnvironment::MainThreadType::UI_MOCK_TIME);
}
~ForceDelayObserverTest() override = default;
protected:
std::unique_ptr<base::test::ScopedTaskEnvironment> scoped_task_environment_;
// Tests that adding a StartAnimationObserver works as intended. private:
TEST_F(StartAnimationObserverTest, Basic) { DISALLOW_COPY_AND_ASSIGN(ForceDelayObserverTest);
};
TEST_F(ForceDelayObserverTest, Basic) {
TestOverviewDelegate delegate;
auto observer = std::make_unique<ForceDelayObserver>(
base::TimeDelta::FromMilliseconds(100));
delegate.AddEnterAnimationObserver(std::move(observer));
EXPECT_EQ(1u, delegate.num_enter_observers());
scoped_task_environment_->FastForwardBy(
base::TimeDelta::FromMilliseconds(50));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, delegate.num_enter_observers());
scoped_task_environment_->FastForwardBy(
base::TimeDelta::FromMilliseconds(55));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, delegate.num_enter_observers());
}
using EnterAnimationObserverTest = AshTestBase;
// Tests that adding a EnterAnimationObserver works as intended.
TEST_F(EnterAnimationObserverTest, Basic) {
TestOverviewDelegate delegate; TestOverviewDelegate delegate;
std::unique_ptr<aura::Window> window = CreateTestWindow(); std::unique_ptr<aura::Window> window = CreateTestWindow();
...@@ -65,10 +107,11 @@ TEST_F(StartAnimationObserverTest, Basic) { ...@@ -65,10 +107,11 @@ TEST_F(StartAnimationObserverTest, Basic) {
animation_settings.SetPreemptionStrategy( animation_settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
auto observer = std::make_unique<StartAnimationObserver>(); auto observer = std::make_unique<EnterAnimationObserver>();
animation_settings.AddObserver(observer.get()); animation_settings.AddObserver(observer.get());
delegate.AddStartAnimationObserver(std::move(observer)); delegate.AddEnterAnimationObserver(std::move(observer));
window->SetTransform(gfx::Transform(1.f, 0.f, 0.f, 1.f, 100.f, 0.f)); window->SetTransform(gfx::Transform(1.f, 0.f, 0.f, 1.f, 100.f, 0.f));
EXPECT_EQ(0u, delegate.num_exit_observers());
EXPECT_EQ(1u, delegate.num_enter_observers()); EXPECT_EQ(1u, delegate.num_enter_observers());
} }
...@@ -77,4 +120,32 @@ TEST_F(StartAnimationObserverTest, Basic) { ...@@ -77,4 +120,32 @@ TEST_F(StartAnimationObserverTest, Basic) {
EXPECT_EQ(0u, delegate.num_enter_observers()); EXPECT_EQ(0u, delegate.num_enter_observers());
} }
using ExitAnimationObserverTest = AshTestBase;
// Tests that adding a ExitAnimationObserver works as intended.
TEST_F(ExitAnimationObserverTest, Basic) {
TestOverviewDelegate delegate;
std::unique_ptr<aura::Window> window = CreateTestWindow();
{
ui::ScopedLayerAnimationSettings animation_settings(
window->layer()->GetAnimator());
animation_settings.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(1000));
animation_settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
auto observer = std::make_unique<ExitAnimationObserver>();
animation_settings.AddObserver(observer.get());
delegate.AddExitAnimationObserver(std::move(observer));
window->SetTransform(gfx::Transform(1.f, 0.f, 0.f, 1.f, 100.f, 0.f));
EXPECT_EQ(1u, delegate.num_exit_observers());
EXPECT_EQ(0u, delegate.num_enter_observers());
}
// Tests that when done animating, the observer count is zero.
window->layer()->GetAnimator()->StopAnimating();
EXPECT_EQ(0u, delegate.num_exit_observers());
}
} // namespace ash } // namespace ash
...@@ -389,7 +389,7 @@ bool OverviewController::ToggleOverview( ...@@ -389,7 +389,7 @@ bool OverviewController::ToggleOverview(
!delayed_animation_task_delay_.is_zero()) { !delayed_animation_task_delay_.is_zero()) {
auto force_delay_observer = auto force_delay_observer =
std::make_unique<ForceDelayObserver>(delayed_animation_task_delay_); std::make_unique<ForceDelayObserver>(delayed_animation_task_delay_);
AddStartAnimationObserver(std::move(force_delay_observer)); AddEnterAnimationObserver(std::move(force_delay_observer));
} }
if (start_animations_.empty()) if (start_animations_.empty())
...@@ -665,13 +665,13 @@ OverviewController::GetWindowsListInOverviewGridsForTest() { ...@@ -665,13 +665,13 @@ OverviewController::GetWindowsListInOverviewGridsForTest() {
return windows; return windows;
} }
void OverviewController::AddStartAnimationObserver( void OverviewController::AddEnterAnimationObserver(
std::unique_ptr<DelayedAnimationObserver> animation_observer) { std::unique_ptr<DelayedAnimationObserver> animation_observer) {
animation_observer->SetOwner(this); animation_observer->SetOwner(this);
start_animations_.push_back(std::move(animation_observer)); start_animations_.push_back(std::move(animation_observer));
} }
void OverviewController::RemoveAndDestroyStartAnimationObserver( void OverviewController::RemoveAndDestroyEnterAnimationObserver(
DelayedAnimationObserver* animation_observer) { DelayedAnimationObserver* animation_observer) {
const bool previous_empty = start_animations_.empty(); const bool previous_empty = start_animations_.empty();
base::EraseIf(start_animations_, base::MatchesUniquePtr(animation_observer)); base::EraseIf(start_animations_, base::MatchesUniquePtr(animation_observer));
......
...@@ -85,9 +85,9 @@ class ASH_EXPORT OverviewController : public OverviewDelegate, ...@@ -85,9 +85,9 @@ class ASH_EXPORT OverviewController : public OverviewDelegate,
std::unique_ptr<DelayedAnimationObserver> animation) override; std::unique_ptr<DelayedAnimationObserver> animation) override;
void RemoveAndDestroyExitAnimationObserver( void RemoveAndDestroyExitAnimationObserver(
DelayedAnimationObserver* animation) override; DelayedAnimationObserver* animation) override;
void AddStartAnimationObserver( void AddEnterAnimationObserver(
std::unique_ptr<DelayedAnimationObserver> animation_observer) override; std::unique_ptr<DelayedAnimationObserver> animation_observer) override;
void RemoveAndDestroyStartAnimationObserver( void RemoveAndDestroyEnterAnimationObserver(
DelayedAnimationObserver* animation_observer) override; DelayedAnimationObserver* animation_observer) override;
// ::wm::ActivationChangeObserver: // ::wm::ActivationChangeObserver:
......
...@@ -32,13 +32,13 @@ class ASH_EXPORT OverviewDelegate { ...@@ -32,13 +32,13 @@ class ASH_EXPORT OverviewDelegate {
DelayedAnimationObserver* animation_observer) = 0; DelayedAnimationObserver* animation_observer) = 0;
// Passes ownership of |animation_observer| to |this| delegate. // Passes ownership of |animation_observer| to |this| delegate.
virtual void AddStartAnimationObserver( virtual void AddEnterAnimationObserver(
std::unique_ptr<DelayedAnimationObserver> animation_observer) = 0; std::unique_ptr<DelayedAnimationObserver> animation_observer) = 0;
// Finds and erases |animation_observer| from the list which tracks the start // Finds and erases |animation_observer| from the list which tracks the start
// animations. This method should be called when a scheduled start overview // animations. This method should be called when a scheduled start overview
// animation completes. // animation completes.
virtual void RemoveAndDestroyStartAnimationObserver( virtual void RemoveAndDestroyEnterAnimationObserver(
DelayedAnimationObserver* animation_observer) = 0; DelayedAnimationObserver* animation_observer) = 0;
protected: protected:
......
...@@ -824,10 +824,10 @@ void OverviewItem::UpdateHeaderLayout(OverviewAnimationType animation_type) { ...@@ -824,10 +824,10 @@ void OverviewItem::UpdateHeaderLayout(OverviewAnimationType animation_type) {
// Create a start animation observer if this is an enter overview layout // Create a start animation observer if this is an enter overview layout
// animation. // animation.
if (animation_type == OVERVIEW_ANIMATION_LAYOUT_OVERVIEW_ITEMS_ON_ENTER) { if (animation_type == OVERVIEW_ANIMATION_LAYOUT_OVERVIEW_ITEMS_ON_ENTER) {
auto start_observer = std::make_unique<StartAnimationObserver>(); auto enter_observer = std::make_unique<EnterAnimationObserver>();
animation_settings.AddObserver(start_observer.get()); animation_settings.AddObserver(enter_observer.get());
Shell::Get()->overview_controller()->AddStartAnimationObserver( Shell::Get()->overview_controller()->AddEnterAnimationObserver(
std::move(start_observer)); std::move(enter_observer));
} }
// |widget_window| is sized to the same bounds as the original window plus // |widget_window| is sized to the same bounds as the original window plus
......
...@@ -79,10 +79,10 @@ void FadeInWidgetAndMaybeSlideOnEnter(views::Widget* widget, ...@@ -79,10 +79,10 @@ void FadeInWidgetAndMaybeSlideOnEnter(views::Widget* widget,
if (slide) { if (slide) {
window->SetTransform(original_transform); window->SetTransform(original_transform);
auto start_observer = std::make_unique<StartAnimationObserver>(); auto enter_observer = std::make_unique<EnterAnimationObserver>();
scoped_overview_animation_settings.AddObserver(start_observer.get()); scoped_overview_animation_settings.AddObserver(enter_observer.get());
Shell::Get()->overview_controller()->AddStartAnimationObserver( Shell::Get()->overview_controller()->AddEnterAnimationObserver(
std::move(start_observer)); std::move(enter_observer));
} }
} }
......
...@@ -283,13 +283,13 @@ void ScopedOverviewTransformWindow::BeginScopedAnimation( ...@@ -283,13 +283,13 @@ void ScopedOverviewTransformWindow::BeginScopedAnimation(
animation_type, window); animation_type, window);
settings->DeferPaint(); settings->DeferPaint();
// Create a start animation observer if this is an enter overview layout // Create an EnterAnimationObserver if this is an enter overview layout
// animation. // animation.
if (animation_type == OVERVIEW_ANIMATION_LAYOUT_OVERVIEW_ITEMS_ON_ENTER) { if (animation_type == OVERVIEW_ANIMATION_LAYOUT_OVERVIEW_ITEMS_ON_ENTER) {
auto start_observer = std::make_unique<StartAnimationObserver>(); auto enter_observer = std::make_unique<EnterAnimationObserver>();
settings->AddObserver(start_observer.get()); settings->AddObserver(enter_observer.get());
Shell::Get()->overview_controller()->AddStartAnimationObserver( Shell::Get()->overview_controller()->AddEnterAnimationObserver(
std::move(start_observer)); std::move(enter_observer));
} }
animation_settings->push_back(std::move(settings)); animation_settings->push_back(std::move(settings));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment