| 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" | |
| 15 | 14 |
| 16 namespace wm { | 15 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 | |
| 69 | 16 |
| 70 typedef aura::test::AuraTestBase WindowUtilTest; | 17 typedef aura::test::AuraTestBase WindowUtilTest; |
| 71 | 18 |
| 72 // Test if the recreate layers does not recreate layers that have | 19 // Test if the recreate layers does not recreate layers that have |
| 73 // already been acquired. | 20 // already been acquired. |
| 74 TEST_F(WindowUtilTest, RecreateLayers) { | 21 TEST_F(WindowUtilTest, RecreateLayers) { |
| 75 std::unique_ptr<aura::Window> window1( | 22 std::unique_ptr<aura::Window> window1( |
| 76 aura::test::CreateTestWindowWithId(0, NULL)); | 23 aura::test::CreateTestWindowWithId(0, NULL)); |
| 77 std::unique_ptr<aura::Window> window11( | 24 std::unique_ptr<aura::Window> window11( |
| 78 aura::test::CreateTestWindowWithId(1, window1.get())); | 25 aura::test::CreateTestWindowWithId(1, window1.get())); |
| 79 std::unique_ptr<aura::Window> window12( | 26 std::unique_ptr<aura::Window> window12( |
| 80 aura::test::CreateTestWindowWithId(2, window1.get())); | 27 aura::test::CreateTestWindowWithId(2, window1.get())); |
| 81 | 28 |
| 82 ASSERT_EQ(2u, window1->layer()->children().size()); | 29 ASSERT_EQ(2u, window1->layer()->children().size()); |
| 83 | 30 |
| 84 std::unique_ptr<ui::Layer> acquired(window11->AcquireLayer()); | 31 std::unique_ptr<ui::Layer> acquired(window11->AcquireLayer()); |
| 85 EXPECT_TRUE(acquired.get()); | 32 EXPECT_TRUE(acquired.get()); |
| 86 EXPECT_EQ(acquired.get(), window11->layer()); | 33 EXPECT_EQ(acquired.get(), window11->layer()); |
| 87 | 34 |
| 88 std::unique_ptr<ui::LayerTreeOwner> tree = | 35 std::unique_ptr<ui::LayerTreeOwner> tree = wm::RecreateLayers(window1.get()); |
| 89 wm::RecreateLayers(window1.get(), nullptr); | |
| 90 | 36 |
| 91 // The detached layer should not have the layer that has | 37 // The detached layer should not have the layer that has |
| 92 // already been detached. | 38 // already been detached. |
| 93 ASSERT_EQ(1u, tree->root()->children().size()); | 39 ASSERT_EQ(1u, tree->root()->children().size()); |
| 94 // Child layer is new instance. | 40 // Child layer is new instance. |
| 95 EXPECT_NE(window11->layer(), tree->root()->children()[0]); | 41 EXPECT_NE(window11->layer(), tree->root()->children()[0]); |
| 96 EXPECT_NE(window12->layer(), tree->root()->children()[0]); | 42 EXPECT_NE(window12->layer(), tree->root()->children()[0]); |
| 97 | 43 |
| 98 // The original window should have both. | 44 // The original window should have both. |
| 99 ASSERT_EQ(2u, window1->layer()->children().size()); | 45 ASSERT_EQ(2u, window1->layer()->children().size()); |
| 100 EXPECT_EQ(window11->layer(), window1->layer()->children()[0]); | 46 EXPECT_EQ(window11->layer(), window1->layer()->children()[0]); |
| 101 EXPECT_EQ(window12->layer(), window1->layer()->children()[1]); | 47 EXPECT_EQ(window12->layer(), window1->layer()->children()[1]); |
| 102 | 48 |
| 103 // Delete the window before the acquired layer is deleted. | 49 // Delete the window before the acquired layer is deleted. |
| 104 window11.reset(); | 50 window11.reset(); |
| 105 } | 51 } |
| 106 | 52 |
| 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 | |
| 138 } // namespace wm | 53 } // namespace wm |
| OLD | NEW |