layer_copy_animator_unittest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // Copyright 2021 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. #include "ash/utility/layer_copy_animator.h"
  5. #include "base/bind.h"
  6. #include "base/cancelable_callback.h"
  7. #include "base/run_loop.h"
  8. #include "base/test/bind.h"
  9. #include "base/test/task_environment.h"
  10. #include "base/timer/timer.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "ui/aura/window.h"
  13. #include "ui/compositor/compositor.h"
  14. #include "ui/compositor/layer.h"
  15. #include "ui/compositor/layer_animation_sequence.h"
  16. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  17. #include "ui/compositor/scoped_layer_animation_settings.h"
  18. #include "ui/compositor/test/test_compositor_host.h"
  19. #include "ui/compositor/test/test_context_factories.h"
  20. #include "ui/compositor/test/test_layer_animation_observer.h"
  21. #include "ui/gfx/geometry/rect.h"
  22. namespace ash {
  23. namespace {
  24. class TestLayerCopyAnimator final : public LayerCopyAnimator {
  25. public:
  26. explicit TestLayerCopyAnimator(aura::Window* window)
  27. : LayerCopyAnimator(window) {}
  28. TestLayerCopyAnimator(const TestLayerCopyAnimator& animator) = delete;
  29. TestLayerCopyAnimator& operator=(const TestLayerCopyAnimator& animator) =
  30. delete;
  31. ~TestLayerCopyAnimator() final = default;
  32. // LayerCopyAnimator:
  33. void OnLayerCopied(std::unique_ptr<ui::Layer> new_layer) override {
  34. DCHECK(!copied_);
  35. LayerCopyAnimator::OnLayerCopied(std::move(new_layer));
  36. copied_ = true;
  37. if (run_loop_.running())
  38. run_loop_.Quit();
  39. }
  40. ui::Layer* WaitForCopy() {
  41. if (!copied_)
  42. run_loop_.Run();
  43. return copied_layer_for_test();
  44. }
  45. private:
  46. base::RunLoop run_loop_;
  47. bool copied_ = false;
  48. };
  49. class LayerCopyAnimatorTest : public testing::Test {
  50. public:
  51. LayerCopyAnimatorTest() = default;
  52. LayerCopyAnimatorTest(const LayerCopyAnimatorTest&) = delete;
  53. LayerCopyAnimatorTest& operator=(const LayerCopyAnimatorTest&) = delete;
  54. ~LayerCopyAnimatorTest() override = default;
  55. // testing::Test:
  56. void SetUp() override {
  57. context_factories_ = std::make_unique<ui::TestContextFactories>(false);
  58. const gfx::Rect bounds(300, 300);
  59. host_.reset(ui::TestCompositorHost::Create(
  60. bounds, context_factories_->GetContextFactory()));
  61. host_->Show();
  62. root_.Init(ui::LAYER_NOT_DRAWN);
  63. root_.SetBounds(gfx::Rect(200, 200));
  64. compositor()->SetRootLayer(root_.layer());
  65. anim_root_ = std::make_unique<aura::Window>(nullptr);
  66. anim_root_->Init(ui::LAYER_NOT_DRAWN);
  67. anim_root_->SetBounds(gfx::Rect(100, 100));
  68. anim_leaf_.Init(ui::LAYER_TEXTURED);
  69. anim_leaf_.SetBounds(gfx::Rect(50, 50));
  70. root_.AddChild(anim_root_.get());
  71. anim_root_->AddChild(&anim_leaf_);
  72. }
  73. void TearDown() override {
  74. DeleteAnimRoot();
  75. host_.reset();
  76. context_factories_.reset();
  77. }
  78. void Advance(const base::TimeDelta& delta) {
  79. task_environment_.FastForwardBy(delta);
  80. }
  81. void GenerateOneFrame() { compositor()->ScheduleFullRedraw(); }
  82. ui::Compositor* compositor() { return host_->GetCompositor(); }
  83. aura::Window* root() { return &root_; }
  84. aura::Window* anim_root() { return anim_root_.get(); }
  85. void DeleteAnimRoot() {
  86. if (anim_root_) {
  87. anim_root_->RemoveChild(&anim_leaf_);
  88. anim_root_.reset();
  89. }
  90. }
  91. private:
  92. base::test::TaskEnvironment task_environment_{
  93. base::test::TaskEnvironment::TimeSource::MOCK_TIME,
  94. base::test::TaskEnvironment::MainThreadType::UI};
  95. std::unique_ptr<ui::TestContextFactories> context_factories_;
  96. std::unique_ptr<ui::TestCompositorHost> host_;
  97. aura::Window root_{nullptr};
  98. std::unique_ptr<aura::Window> anim_root_;
  99. aura::Window anim_leaf_{nullptr};
  100. };
  101. } // namespace
  102. TEST_F(LayerCopyAnimatorTest, Basic) {
  103. ui::ScopedAnimationDurationScaleMode non_zero(
  104. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  105. auto* root_layer = root()->layer();
  106. auto* anim_layer = anim_root()->layer();
  107. auto* animator = new TestLayerCopyAnimator(anim_root());
  108. EXPECT_FALSE(animator->animation_requested());
  109. EXPECT_EQ(2u, anim_layer->children().size());
  110. EXPECT_EQ(1u, root_layer->children().size());
  111. GenerateOneFrame();
  112. auto* copied_layer = animator->WaitForCopy();
  113. EXPECT_TRUE(copied_layer);
  114. EXPECT_EQ(ui::LAYER_SOLID_COLOR, copied_layer->type());
  115. EXPECT_EQ(gfx::Size(100, 100), copied_layer->size());
  116. ui::TestLayerAnimationObserver observer;
  117. animator->MaybeStartAnimation(
  118. &observer, base::BindOnce([](ui::Layer* layer,
  119. ui::LayerAnimationObserver* observer) {
  120. DCHECK(observer);
  121. layer->SetOpacity(0.f);
  122. ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
  123. settings.SetTransitionDuration(base::Milliseconds(1));
  124. layer->SetOpacity(1.f);
  125. ui::LayerAnimationSequence* sequence = new ui::LayerAnimationSequence(
  126. ui::LayerAnimationElement::CreateOpacityElement(1.0,
  127. base::TimeDelta()));
  128. sequence->AddObserver(observer);
  129. layer->GetAnimator()->ScheduleAnimation(sequence);
  130. }));
  131. ASSERT_EQ(2u, root_layer->children().size());
  132. EXPECT_EQ(copied_layer, root_layer->children()[1]);
  133. EXPECT_TRUE(copied_layer->GetAnimator()->is_animating());
  134. EXPECT_EQ(0.f, anim_layer->GetTargetOpacity());
  135. Advance(base::Milliseconds(1000));
  136. EXPECT_EQ(3, observer.last_ended_sequence_epoch());
  137. EXPECT_EQ(1u, root_layer->children().size());
  138. EXPECT_EQ(1.f, anim_layer->GetTargetOpacity());
  139. }
  140. TEST_F(LayerCopyAnimatorTest, CopyAfterAnimationRequest) {
  141. ui::ScopedAnimationDurationScaleMode non_zero(
  142. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  143. auto* root_layer = root()->layer();
  144. auto* anim_layer = anim_root()->layer();
  145. auto* animator = new TestLayerCopyAnimator(anim_root());
  146. EXPECT_FALSE(animator->animation_requested());
  147. EXPECT_EQ(2u, anim_layer->children().size());
  148. EXPECT_EQ(1u, root_layer->children().size());
  149. ui::TestLayerAnimationObserver observer;
  150. animator->MaybeStartAnimation(
  151. &observer, base::BindOnce([](ui::Layer* layer,
  152. ui::LayerAnimationObserver* observer) {
  153. DCHECK(observer);
  154. layer->SetOpacity(0.f);
  155. ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
  156. // Longer duration so that animation doesn't end after copy.
  157. settings.SetTransitionDuration(base::Milliseconds(100));
  158. layer->SetOpacity(1.f);
  159. ui::LayerAnimationSequence* sequence = new ui::LayerAnimationSequence(
  160. ui::LayerAnimationElement::CreateOpacityElement(1.0,
  161. base::TimeDelta()));
  162. sequence->AddObserver(observer);
  163. layer->GetAnimator()->ScheduleAnimation(sequence);
  164. }));
  165. EXPECT_FALSE(animator->copied_layer_for_test());
  166. GenerateOneFrame();
  167. auto* copied_layer = animator->WaitForCopy();
  168. ASSERT_TRUE(copied_layer);
  169. EXPECT_EQ(ui::LAYER_SOLID_COLOR, copied_layer->type());
  170. EXPECT_EQ(gfx::Size(100, 100), copied_layer->size());
  171. ASSERT_EQ(2u, root_layer->children().size());
  172. EXPECT_EQ(copied_layer, root_layer->children()[1]);
  173. EXPECT_TRUE(copied_layer->GetAnimator()->is_animating());
  174. EXPECT_EQ(0.f, anim_layer->GetTargetOpacity());
  175. Advance(base::Milliseconds(1000));
  176. // When animation starts before copy, it registers the observer to fake
  177. // sequecne, hence become 6.
  178. EXPECT_EQ(6, observer.last_ended_sequence_epoch());
  179. EXPECT_EQ(1u, root_layer->children().size());
  180. EXPECT_EQ(1.f, anim_layer->GetTargetOpacity());
  181. }
  182. TEST_F(LayerCopyAnimatorTest, CancelByResize) {
  183. ui::ScopedAnimationDurationScaleMode non_zero(
  184. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  185. auto* root_layer = root()->layer();
  186. auto* anim_layer = anim_root()->layer();
  187. auto* animator = new TestLayerCopyAnimator(anim_root());
  188. EXPECT_FALSE(animator->animation_requested());
  189. EXPECT_EQ(2u, anim_layer->children().size());
  190. EXPECT_EQ(1u, root_layer->children().size());
  191. anim_layer->SetBounds(gfx::Rect(210, 210));
  192. GenerateOneFrame();
  193. auto* copied_layer = animator->WaitForCopy();
  194. ASSERT_FALSE(copied_layer);
  195. ui::TestLayerAnimationObserver observer;
  196. EXPECT_EQ(-1, observer.last_aborted_sequence_epoch());
  197. animator->MaybeStartAnimation(
  198. &observer, base::BindOnce([](ui::Layer* layer,
  199. ui::LayerAnimationObserver* observer) {
  200. EXPECT_FALSE(true) << "Callback should not be called";
  201. }));
  202. EXPECT_EQ(1.f, anim_layer->GetTargetOpacity());
  203. EXPECT_EQ(1, observer.last_aborted_sequence_epoch());
  204. }
  205. TEST_F(LayerCopyAnimatorTest, CancelByDelete) {
  206. ui::ScopedAnimationDurationScaleMode non_zero(
  207. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  208. auto* root_layer = root()->layer();
  209. auto* anim_layer = anim_root()->layer();
  210. auto* animator = new LayerCopyAnimator(anim_root());
  211. EXPECT_FALSE(animator->animation_requested());
  212. EXPECT_EQ(2u, anim_layer->children().size());
  213. EXPECT_EQ(1u, root_layer->children().size());
  214. GenerateOneFrame();
  215. DeleteAnimRoot();
  216. }
  217. TEST_F(LayerCopyAnimatorTest, CancelByStop) {
  218. ui::ScopedAnimationDurationScaleMode non_zero(
  219. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  220. auto* root_layer = root()->layer();
  221. auto* anim_layer = anim_root()->layer();
  222. auto* animator = new TestLayerCopyAnimator(anim_root());
  223. EXPECT_FALSE(animator->animation_requested());
  224. EXPECT_EQ(2u, anim_layer->children().size());
  225. EXPECT_EQ(1u, root_layer->children().size());
  226. GenerateOneFrame();
  227. auto* copied_layer = animator->WaitForCopy();
  228. ASSERT_TRUE(copied_layer);
  229. EXPECT_EQ(ui::LAYER_SOLID_COLOR, copied_layer->type());
  230. EXPECT_EQ(gfx::Size(100, 100), copied_layer->size());
  231. ui::TestLayerAnimationObserver observer;
  232. animator->MaybeStartAnimation(
  233. &observer, base::BindOnce([](ui::Layer* layer,
  234. ui::LayerAnimationObserver* observer) {
  235. DCHECK(observer);
  236. layer->SetOpacity(0.f);
  237. ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
  238. settings.SetTransitionDuration(base::Milliseconds(100));
  239. layer->SetOpacity(1.f);
  240. ui::LayerAnimationSequence* sequence = new ui::LayerAnimationSequence(
  241. ui::LayerAnimationElement::CreateOpacityElement(1.0,
  242. base::TimeDelta()));
  243. sequence->AddObserver(observer);
  244. layer->GetAnimator()->ScheduleAnimation(sequence);
  245. }));
  246. ASSERT_EQ(2u, root_layer->children().size());
  247. EXPECT_EQ(copied_layer, root_layer->children()[1]);
  248. EXPECT_TRUE(copied_layer->GetAnimator()->is_animating());
  249. EXPECT_EQ(0.f, anim_layer->GetTargetOpacity());
  250. copied_layer->GetAnimator()->StopAnimating();
  251. Advance(base::Milliseconds(1000));
  252. EXPECT_EQ(3, observer.last_ended_sequence_epoch());
  253. EXPECT_EQ(1u, root_layer->children().size());
  254. EXPECT_EQ(1.f, anim_layer->GetTargetOpacity());
  255. }
  256. TEST_F(LayerCopyAnimatorTest, NoAnimationStopImmediately) {
  257. ui::ScopedAnimationDurationScaleMode non_zero(
  258. ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
  259. auto* root_layer = root()->layer();
  260. auto* anim_layer = anim_root()->layer();
  261. auto* animator = new TestLayerCopyAnimator(anim_root());
  262. EXPECT_FALSE(animator->animation_requested());
  263. EXPECT_EQ(2u, anim_layer->children().size());
  264. EXPECT_EQ(1u, root_layer->children().size());
  265. GenerateOneFrame();
  266. auto* copied_layer = animator->WaitForCopy();
  267. ASSERT_TRUE(copied_layer);
  268. EXPECT_EQ(ui::LAYER_SOLID_COLOR, copied_layer->type());
  269. EXPECT_EQ(gfx::Size(100, 100), copied_layer->size());
  270. ui::TestLayerAnimationObserver observer;
  271. animator->MaybeStartAnimation(
  272. &observer, base::BindOnce([](ui::Layer* layer,
  273. ui::LayerAnimationObserver* observer) {}));
  274. EXPECT_EQ(1u, root_layer->children().size());
  275. EXPECT_EQ(1.f, anim_layer->GetTargetOpacity());
  276. EXPECT_EQ(1, observer.last_ended_sequence_epoch());
  277. }
  278. } // namespace ash