| 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/wm/core/window_util.h" | 5 #include "ui/wm/core/window_util.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "ui/aura/test/aura_test_base.h" | 9 #include "ui/aura/test/aura_test_base.h" |
| 10 #include "ui/aura/test/test_windows.h" | 10 #include "ui/aura/test/test_windows.h" |
| 11 #include "ui/aura/window.h" | 11 #include "ui/aura/window.h" |
| 12 #include "ui/compositor/layer.h" | 12 #include "ui/compositor/layer.h" |
| 13 #include "ui/compositor/layer_tree_owner.h" | 13 #include "ui/compositor/layer_tree_owner.h" |
| 14 #include "ui/compositor/paint_context.h" |
| 14 | 15 |
| 15 namespace wm { | 16 namespace wm { |
| 17 namespace { |
| 18 |
| 19 // Used to check if the delegate for recreated layer is created for |
| 20 // the correct delegate. |
| 21 class TestLayerDelegate : public ui::LayerDelegate { |
| 22 public: |
| 23 explicit TestLayerDelegate(ui::Layer* original_layer) |
| 24 : original_layer_(original_layer) {} |
| 25 ~TestLayerDelegate() override = default; |
| 26 |
| 27 // ui::LayerDelegate: |
| 28 void OnPaintLayer(const ui::PaintContext& context) override {} |
| 29 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {} |
| 30 void OnDeviceScaleFactorChanged(float device_scale_factor) override {} |
| 31 base::Closure PrepareForLayerBoundsChange() override { |
| 32 return base::Closure(); |
| 33 } |
| 34 |
| 35 ui::Layer* original_layer() { return original_layer_; } |
| 36 |
| 37 private: |
| 38 ui::Layer* original_layer_; |
| 39 |
| 40 DISALLOW_COPY_AND_ASSIGN(TestLayerDelegate); |
| 41 }; |
| 42 |
| 43 // Used to create a TestLayerDelegate for recreated layers. |
| 44 class TestLayerDelegateFactory : public ::wm::LayerDelegateFactory { |
| 45 public: |
| 46 TestLayerDelegateFactory() = default; |
| 47 ~TestLayerDelegateFactory() override = default; |
| 48 |
| 49 // ::wm::LayerDelegateFactory: |
| 50 ui::LayerDelegate* CreateDelegate(ui::Layer* new_layer, |
| 51 ui::Layer* original_layer) override { |
| 52 delegates_.push_back(base::MakeUnique<TestLayerDelegate>(original_layer)); |
| 53 return delegates_.back().get(); |
| 54 } |
| 55 |
| 56 size_t delegate_count() const { return delegates_.size(); } |
| 57 |
| 58 TestLayerDelegate* GetDelegateAt(int index) { |
| 59 return delegates_[index].get(); |
| 60 } |
| 61 |
| 62 private: |
| 63 std::vector<std::unique_ptr<TestLayerDelegate>> delegates_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(TestLayerDelegateFactory); |
| 66 }; |
| 67 |
| 68 } // namespace |
| 16 | 69 |
| 17 typedef aura::test::AuraTestBase WindowUtilTest; | 70 typedef aura::test::AuraTestBase WindowUtilTest; |
| 18 | 71 |
| 19 // Test if the recreate layers does not recreate layers that have | 72 // Test if the recreate layers does not recreate layers that have |
| 20 // already been acquired. | 73 // already been acquired. |
| 21 TEST_F(WindowUtilTest, RecreateLayers) { | 74 TEST_F(WindowUtilTest, RecreateLayers) { |
| 22 std::unique_ptr<aura::Window> window1( | 75 std::unique_ptr<aura::Window> window1( |
| 23 aura::test::CreateTestWindowWithId(0, NULL)); | 76 aura::test::CreateTestWindowWithId(0, NULL)); |
| 24 std::unique_ptr<aura::Window> window11( | 77 std::unique_ptr<aura::Window> window11( |
| 25 aura::test::CreateTestWindowWithId(1, window1.get())); | 78 aura::test::CreateTestWindowWithId(1, window1.get())); |
| 26 std::unique_ptr<aura::Window> window12( | 79 std::unique_ptr<aura::Window> window12( |
| 27 aura::test::CreateTestWindowWithId(2, window1.get())); | 80 aura::test::CreateTestWindowWithId(2, window1.get())); |
| 28 | 81 |
| 29 ASSERT_EQ(2u, window1->layer()->children().size()); | 82 ASSERT_EQ(2u, window1->layer()->children().size()); |
| 30 | 83 |
| 31 std::unique_ptr<ui::Layer> acquired(window11->AcquireLayer()); | 84 std::unique_ptr<ui::Layer> acquired(window11->AcquireLayer()); |
| 32 EXPECT_TRUE(acquired.get()); | 85 EXPECT_TRUE(acquired.get()); |
| 33 EXPECT_EQ(acquired.get(), window11->layer()); | 86 EXPECT_EQ(acquired.get(), window11->layer()); |
| 34 | 87 |
| 35 std::unique_ptr<ui::LayerTreeOwner> tree = wm::RecreateLayers(window1.get()); | 88 std::unique_ptr<ui::LayerTreeOwner> tree = |
| 89 wm::RecreateLayers(window1.get(), nullptr); |
| 36 | 90 |
| 37 // The detached layer should not have the layer that has | 91 // The detached layer should not have the layer that has |
| 38 // already been detached. | 92 // already been detached. |
| 39 ASSERT_EQ(1u, tree->root()->children().size()); | 93 ASSERT_EQ(1u, tree->root()->children().size()); |
| 40 // Child layer is new instance. | 94 // Child layer is new instance. |
| 41 EXPECT_NE(window11->layer(), tree->root()->children()[0]); | 95 EXPECT_NE(window11->layer(), tree->root()->children()[0]); |
| 42 EXPECT_NE(window12->layer(), tree->root()->children()[0]); | 96 EXPECT_NE(window12->layer(), tree->root()->children()[0]); |
| 43 | 97 |
| 44 // The original window should have both. | 98 // The original window should have both. |
| 45 ASSERT_EQ(2u, window1->layer()->children().size()); | 99 ASSERT_EQ(2u, window1->layer()->children().size()); |
| 46 EXPECT_EQ(window11->layer(), window1->layer()->children()[0]); | 100 EXPECT_EQ(window11->layer(), window1->layer()->children()[0]); |
| 47 EXPECT_EQ(window12->layer(), window1->layer()->children()[1]); | 101 EXPECT_EQ(window12->layer(), window1->layer()->children()[1]); |
| 48 | 102 |
| 49 // Delete the window before the acquired layer is deleted. | 103 // Delete the window before the acquired layer is deleted. |
| 50 window11.reset(); | 104 window11.reset(); |
| 51 } | 105 } |
| 52 | 106 |
| 107 // Test if the LayerDelegateFactory creates new delegates for |
| 108 // recreated layers correctly. |
| 109 TEST_F(WindowUtilTest, RecreateLayersWithDelegate) { |
| 110 TestLayerDelegateFactory factory; |
| 111 std::unique_ptr<aura::Window> window1( |
| 112 aura::test::CreateTestWindowWithId(0, NULL)); |
| 113 std::unique_ptr<aura::Window> window11( |
| 114 aura::test::CreateTestWindowWithId(1, window1.get())); |
| 115 std::unique_ptr<aura::Window> window12( |
| 116 aura::test::CreateTestWindowWithId(2, window1.get())); |
| 117 |
| 118 std::unique_ptr<ui::LayerTreeOwner> tree = |
| 119 wm::RecreateLayers(window1.get(), &factory); |
| 120 |
| 121 ASSERT_EQ(3u, factory.delegate_count()); |
| 122 |
| 123 TestLayerDelegate* new_delegate_1 = factory.GetDelegateAt(0); |
| 124 TestLayerDelegate* new_delegate_11 = factory.GetDelegateAt(1); |
| 125 TestLayerDelegate* new_delegate_12 = factory.GetDelegateAt(2); |
| 126 |
| 127 EXPECT_EQ(window1->layer(), new_delegate_1->original_layer()); |
| 128 EXPECT_EQ(window11->layer(), |
| 129 new_delegate_11->original_layer()); |
| 130 EXPECT_EQ(window12->layer(), |
| 131 new_delegate_12->original_layer()); |
| 132 |
| 133 EXPECT_EQ(tree->root()->delegate(), new_delegate_1); |
| 134 EXPECT_EQ(tree->root()->children()[0]->delegate(), new_delegate_11); |
| 135 EXPECT_EQ(tree->root()->children()[1]->delegate(), new_delegate_12); |
| 136 } |
| 137 |
| 53 } // namespace wm | 138 } // namespace wm |
| OLD | NEW |