OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_owner.h" | 5 #include "ui/compositor/layer_owner.h" |
6 | 6 |
7 #include "base/test/null_task_runner.h" | 7 #include "base/test/null_task_runner.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "ui/compositor/compositor.h" | 9 #include "ui/compositor/compositor.h" |
10 #include "ui/compositor/layer.h" | 10 #include "ui/compositor/layer.h" |
| 11 #include "ui/compositor/layer_animation_observer.h" |
11 #include "ui/compositor/layer_animator.h" | 12 #include "ui/compositor/layer_animator.h" |
| 13 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
12 #include "ui/compositor/scoped_layer_animation_settings.h" | 14 #include "ui/compositor/scoped_layer_animation_settings.h" |
13 #include "ui/compositor/test/context_factories_for_test.h" | 15 #include "ui/compositor/test/context_factories_for_test.h" |
14 #include "ui/gfx/native_widget_types.h" | 16 #include "ui/gfx/native_widget_types.h" |
15 | 17 |
16 namespace ui { | 18 namespace ui { |
17 namespace { | 19 namespace { |
18 | 20 |
| 21 // An animation observer that confirms upon animation completion, that the |
| 22 // compositor is not null. |
| 23 class TestLayerAnimationObserver : public ImplicitAnimationObserver { |
| 24 public: |
| 25 TestLayerAnimationObserver(Layer* layer) : layer_(layer) {} |
| 26 ~TestLayerAnimationObserver() override {} |
| 27 |
| 28 // ImplicitAnimationObserver: |
| 29 void OnImplicitAnimationsCompleted() override { |
| 30 EXPECT_NE(nullptr, layer_->GetCompositor()); |
| 31 } |
| 32 |
| 33 private: |
| 34 Layer* layer_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationObserver); |
| 37 }; |
| 38 |
19 // Test fixture for LayerOwner tests that require a ui::Compositor. | 39 // Test fixture for LayerOwner tests that require a ui::Compositor. |
20 class LayerOwnerTestWithCompositor : public testing::Test { | 40 class LayerOwnerTestWithCompositor : public testing::Test { |
21 public: | 41 public: |
22 LayerOwnerTestWithCompositor(); | 42 LayerOwnerTestWithCompositor(); |
23 ~LayerOwnerTestWithCompositor() override; | 43 ~LayerOwnerTestWithCompositor() override; |
24 | 44 |
25 void SetUp() override; | 45 void SetUp() override; |
26 void TearDown() override; | 46 void TearDown() override; |
27 | 47 |
28 protected: | 48 protected: |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 | 119 |
100 compositor()->SetRootLayer(layer); | 120 compositor()->SetRootLayer(layer); |
101 | 121 |
102 scoped_ptr<Layer> layer_copy = owner.RecreateLayer(); | 122 scoped_ptr<Layer> layer_copy = owner.RecreateLayer(); |
103 | 123 |
104 EXPECT_EQ(compositor(), owner.layer()->GetCompositor()); | 124 EXPECT_EQ(compositor(), owner.layer()->GetCompositor()); |
105 EXPECT_EQ(owner.layer(), compositor()->root_layer()); | 125 EXPECT_EQ(owner.layer(), compositor()->root_layer()); |
106 EXPECT_EQ(nullptr, layer_copy->GetCompositor()); | 126 EXPECT_EQ(nullptr, layer_copy->GetCompositor()); |
107 } | 127 } |
108 | 128 |
| 129 // Tests that recreating the root layer, while one of its children is animating, |
| 130 // properly updates the compositor. So that compositor is not null for observers |
| 131 // of animations being cancelled. |
| 132 TEST_F(LayerOwnerTestWithCompositor, RecreateRootLayerDuringAnimation) { |
| 133 LayerOwner owner; |
| 134 Layer* layer = new Layer; |
| 135 owner.SetLayer(layer); |
| 136 compositor()->SetRootLayer(layer); |
| 137 |
| 138 scoped_ptr<Layer> child(new Layer); |
| 139 child->SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 140 layer->Add(child.get()); |
| 141 |
| 142 // This observer checks that the compositor of |child| is not null upon |
| 143 // animation completion. |
| 144 scoped_ptr<TestLayerAnimationObserver> observer( |
| 145 new TestLayerAnimationObserver(child.get())); |
| 146 scoped_ptr<ui::ScopedAnimationDurationScaleMode> long_duration_animation( |
| 147 new ui::ScopedAnimationDurationScaleMode( |
| 148 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION)); |
| 149 { |
| 150 ui::ScopedLayerAnimationSettings animation(child->GetAnimator()); |
| 151 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(1000)); |
| 152 animation.AddObserver(observer.get()); |
| 153 gfx::Transform transform; |
| 154 transform.Scale(0.5f, 0.5f); |
| 155 child->SetTransform(transform); |
| 156 } |
| 157 |
| 158 scoped_ptr<Layer> layer_copy = owner.RecreateLayer(); |
| 159 } |
| 160 |
| 161 // Tests that recreating a non-root layer, while one of its children is |
| 162 // animating, properly updates the compositor. So that compositor is not null |
| 163 // for observers of animations being cancelled. |
| 164 TEST_F(LayerOwnerTestWithCompositor, RecreateNonRootLayerDuringAnimation) { |
| 165 scoped_ptr<Layer> root_layer(new Layer); |
| 166 compositor()->SetRootLayer(root_layer.get()); |
| 167 |
| 168 LayerOwner owner; |
| 169 Layer* layer = new Layer; |
| 170 owner.SetLayer(layer); |
| 171 root_layer->Add(layer); |
| 172 |
| 173 scoped_ptr<Layer> child(new Layer); |
| 174 child->SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 175 layer->Add(child.get()); |
| 176 |
| 177 // This observer checks that the compositor of |child| is not null upon |
| 178 // animation completion. |
| 179 scoped_ptr<TestLayerAnimationObserver> observer( |
| 180 new TestLayerAnimationObserver(child.get())); |
| 181 scoped_ptr<ui::ScopedAnimationDurationScaleMode> long_duration_animation( |
| 182 new ui::ScopedAnimationDurationScaleMode( |
| 183 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION)); |
| 184 { |
| 185 ui::ScopedLayerAnimationSettings animation(child->GetAnimator()); |
| 186 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(1000)); |
| 187 animation.AddObserver(observer.get()); |
| 188 gfx::Transform transform; |
| 189 transform.Scale(0.5f, 0.5f); |
| 190 child->SetTransform(transform); |
| 191 } |
| 192 |
| 193 scoped_ptr<Layer> layer_copy = owner.RecreateLayer(); |
| 194 } |
| 195 |
109 } // namespace ui | 196 } // namespace ui |
OLD | NEW |