OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "ash/common/wm/overview/cleanup_animation_observer.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "ash/common/wm/overview/window_selector_delegate.h" | |
10 #include "ash/common/wm_window.h" | |
11 #include "ash/test/ash_test_base.h" | |
12 #include "ui/compositor/layer_animation_observer.h" | |
13 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | |
14 #include "ui/compositor/scoped_layer_animation_settings.h" | |
15 #include "ui/gfx/geometry/rect.h" | |
16 #include "ui/views/widget/widget.h" | |
17 #include "ui/views/widget/widget_observer.h" | |
18 | |
19 namespace ash { | |
20 namespace { | |
21 | |
22 class TestWindowSelectorDelegate : public WindowSelectorDelegate { | |
23 public: | |
24 TestWindowSelectorDelegate() = default; | |
25 | |
26 ~TestWindowSelectorDelegate() override { | |
27 // Destroy widgets that may be still animating if shell shuts down soon | |
28 // after exiting overview mode. | |
29 for (std::unique_ptr<DelayedAnimationObserver>& observer : observers_) | |
30 observer->Shutdown(); | |
31 } | |
32 | |
33 // WindowSelectorDelegate: | |
34 void OnSelectionEnded() override {} | |
35 | |
36 void AddDelayedAnimationObserver( | |
37 std::unique_ptr<DelayedAnimationObserver> animation_observer) override { | |
38 animation_observer->SetOwner(this); | |
39 observers_.push_back(std::move(animation_observer)); | |
40 } | |
41 | |
42 void RemoveAndDestroyAnimationObserver( | |
43 DelayedAnimationObserver* animation_observer) override { | |
44 class IsEqual { | |
45 public: | |
46 explicit IsEqual(DelayedAnimationObserver* animation_observer) | |
47 : animation_observer_(animation_observer) {} | |
48 bool operator()(const std::unique_ptr<DelayedAnimationObserver>& other) { | |
49 return (other.get() == animation_observer_); | |
50 } | |
51 | |
52 private: | |
53 const DelayedAnimationObserver* animation_observer_; | |
54 }; | |
55 observers_.erase(std::remove_if(observers_.begin(), observers_.end(), | |
56 IsEqual(animation_observer)), | |
57 observers_.end()); | |
58 } | |
59 | |
60 private: | |
61 std::vector<std::unique_ptr<DelayedAnimationObserver>> observers_; | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(TestWindowSelectorDelegate); | |
64 }; | |
65 | |
66 class CleanupAnimationObserverTest : public test::AshTestBase, | |
67 public views::WidgetObserver { | |
68 public: | |
69 CleanupAnimationObserverTest() = default; | |
70 | |
71 ~CleanupAnimationObserverTest() override { | |
72 if (widget_) | |
73 widget_->RemoveObserver(this); | |
74 } | |
75 | |
76 // Creates a Widget containing a Window with the given |bounds|. This should | |
77 // be used when the test requires a Widget. For example any test that will | |
78 // cause a window to be closed via | |
79 // views::Widget::GetWidgetForNativeView(window)->Close(). | |
80 std::unique_ptr<views::Widget> CreateWindowWidget(const gfx::Rect& bounds) { | |
81 std::unique_ptr<views::Widget> widget(new views::Widget); | |
82 views::Widget::InitParams params; | |
83 params.bounds = bounds; | |
84 params.type = views::Widget::InitParams::TYPE_WINDOW; | |
85 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
86 widget->Init(params); | |
87 widget->Show(); | |
88 ParentWindowInPrimaryRootWindow(widget->GetNativeWindow()); | |
89 widget->AddObserver(this); | |
90 widget_ = widget.get(); | |
91 return widget; | |
92 } | |
93 | |
94 protected: | |
95 bool widget_destroyed() { return !widget_; } | |
96 | |
97 private: | |
98 void OnWidgetDestroyed(views::Widget* widget) override { | |
99 if (widget_ == widget) | |
100 widget_ = nullptr; | |
101 } | |
102 | |
103 views::Widget* widget_ = nullptr; | |
104 | |
105 DISALLOW_COPY_AND_ASSIGN(CleanupAnimationObserverTest); | |
106 }; | |
107 | |
108 } // namespace | |
109 | |
110 // Tests that basic create-destroy sequence does not crash. | |
111 TEST_F(CleanupAnimationObserverTest, CreateDestroy) { | |
112 TestWindowSelectorDelegate delegate; | |
113 std::unique_ptr<views::Widget> widget( | |
114 CreateWindowWidget(gfx::Rect(0, 0, 40, 40))); | |
115 std::unique_ptr<CleanupAnimationObserver> observer( | |
116 new CleanupAnimationObserver(std::move(widget))); | |
117 delegate.AddDelayedAnimationObserver(std::move(observer)); | |
118 } | |
119 | |
120 // Tests that completing animation deletes the animation observer and the | |
121 // test widget and that deleting the WindowSelectorDelegate instance which | |
122 // owns the observer does not crash. | |
123 TEST_F(CleanupAnimationObserverTest, CreateAnimateComplete) { | |
124 TestWindowSelectorDelegate delegate; | |
125 std::unique_ptr<views::Widget> widget( | |
126 CreateWindowWidget(gfx::Rect(0, 0, 40, 40))); | |
127 WmWindow* widget_window = WmWindow::Get(widget->GetNativeWindow()); | |
128 { | |
129 ui::ScopedLayerAnimationSettings animation_settings( | |
130 widget_window->GetLayer()->GetAnimator()); | |
131 animation_settings.SetTransitionDuration( | |
132 base::TimeDelta::FromMilliseconds(1000)); | |
133 animation_settings.SetPreemptionStrategy( | |
134 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
135 | |
136 std::unique_ptr<CleanupAnimationObserver> observer( | |
137 new CleanupAnimationObserver(std::move(widget))); | |
138 animation_settings.AddObserver(observer.get()); | |
139 delegate.AddDelayedAnimationObserver(std::move(observer)); | |
140 | |
141 widget_window->SetBounds(gfx::Rect(50, 50, 60, 60)); | |
142 } | |
143 // The widget should be destroyed when |animation_settings| gets out of scope | |
144 // which in absence of NON_ZERO_DURATION animation duration mode completes | |
145 // the animation and calls OnImplicitAnimationsCompleted() on the cleanup | |
146 // observer and auto-deletes the owned widget. | |
147 EXPECT_TRUE(widget_destroyed()); | |
148 // TestWindowSelectorDelegate going out of scope should not crash. | |
149 } | |
150 | |
151 // Tests that starting an animation and exiting doesn't crash. If not for | |
152 // TestWindowSelectorDelegate calling Shutdown() on a CleanupAnimationObserver | |
153 // instance in destructor, this test would have crashed. | |
154 TEST_F(CleanupAnimationObserverTest, CreateAnimateShutdown) { | |
155 TestWindowSelectorDelegate delegate; | |
156 std::unique_ptr<views::Widget> widget( | |
157 CreateWindowWidget(gfx::Rect(0, 0, 40, 40))); | |
158 WmWindow* widget_window = WmWindow::Get(widget->GetNativeWindow()); | |
159 { | |
160 // Normal animations for tests have ZERO_DURATION, make sure we are actually | |
161 // animating the movement. | |
162 ui::ScopedAnimationDurationScaleMode animation_scale_mode( | |
163 ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION); | |
164 ui::ScopedLayerAnimationSettings animation_settings( | |
165 widget_window->GetLayer()->GetAnimator()); | |
166 animation_settings.SetTransitionDuration( | |
167 base::TimeDelta::FromMilliseconds(1000)); | |
168 animation_settings.SetPreemptionStrategy( | |
169 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
170 | |
171 std::unique_ptr<CleanupAnimationObserver> observer( | |
172 new CleanupAnimationObserver(std::move(widget))); | |
173 animation_settings.AddObserver(observer.get()); | |
174 delegate.AddDelayedAnimationObserver(std::move(observer)); | |
175 | |
176 widget_window->SetBounds(gfx::Rect(50, 50, 60, 60)); | |
177 } | |
178 // The widget still exists. | |
179 EXPECT_FALSE(widget_destroyed()); | |
180 // The test widget is auto-deleted when |delegate| that owns it goes out of | |
181 // scope. The animation is still active when this happens which should not | |
182 // crash. | |
183 } | |
184 | |
185 } // namespace ash | |
OLD | NEW |