OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/views/view.h" | 5 #include "ui/views/view.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
(...skipping 4651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4662 EXPECT_FALSE(view.layer()); | 4662 EXPECT_FALSE(view.layer()); |
4663 // Ensure the layer went away via VisibilityChanged(). | 4663 // Ensure the layer went away via VisibilityChanged(). |
4664 EXPECT_TRUE(view.was_hidden()); | 4664 EXPECT_TRUE(view.was_hidden()); |
4665 | 4665 |
4666 // Not removed from Widget until Close() completes. | 4666 // Not removed from Widget until Close() completes. |
4667 EXPECT_TRUE(view.GetWidget()); | 4667 EXPECT_TRUE(view.GetWidget()); |
4668 base::RunLoop().RunUntilIdle(); // Let the Close() complete. | 4668 base::RunLoop().RunUntilIdle(); // Let the Close() complete. |
4669 EXPECT_FALSE(view.GetWidget()); | 4669 EXPECT_FALSE(view.GetWidget()); |
4670 } | 4670 } |
4671 | 4671 |
| 4672 // A View that keeps the children with a special ID above other children. |
| 4673 class OrderableView : public View { |
| 4674 public: |
| 4675 // ID used by the children that are stacked above other children. |
| 4676 static constexpr int VIEW_ID_RAISED = 1000; |
| 4677 |
| 4678 OrderableView() : View() {} |
| 4679 ~OrderableView() override {} |
| 4680 |
| 4681 View::Views GetChildrenInZOrder() override { |
| 4682 View::Views children; |
| 4683 // Iterate over regular children and later over the raised children to |
| 4684 // create a custom Z-order. |
| 4685 for (int i = 0; i < child_count(); ++i) { |
| 4686 if (child_at(i)->id() != VIEW_ID_RAISED) |
| 4687 children.push_back(child_at(i)); |
| 4688 } |
| 4689 for (int i = 0; i < child_count(); ++i) { |
| 4690 if (child_at(i)->id() == VIEW_ID_RAISED) |
| 4691 children.push_back(child_at(i)); |
| 4692 } |
| 4693 DCHECK_EQ(child_count(), static_cast<int>(children.size())); |
| 4694 return children; |
| 4695 } |
| 4696 |
| 4697 private: |
| 4698 DISALLOW_COPY_AND_ASSIGN(OrderableView); |
| 4699 }; |
| 4700 |
| 4701 TEST_F(ViewTest, ChildViewZOrderChanged) { |
| 4702 const int kChildrenCount = 4; |
| 4703 std::unique_ptr<View> view(new OrderableView()); |
| 4704 view->SetPaintToLayer(true); |
| 4705 for (int i = 0; i < kChildrenCount; ++i) |
| 4706 AddViewWithChildLayer(view.get()); |
| 4707 View::Views children = view->GetChildrenInZOrder(); |
| 4708 const std::vector<ui::Layer*>& layers = view->layer()->children(); |
| 4709 EXPECT_EQ(kChildrenCount, static_cast<int>(layers.size())); |
| 4710 EXPECT_EQ(kChildrenCount, static_cast<int>(children.size())); |
| 4711 for (int i = 0; i < kChildrenCount; ++i) { |
| 4712 EXPECT_EQ(view->child_at(i)->layer(), layers[i]); |
| 4713 EXPECT_EQ(view->child_at(i), children[i]); |
| 4714 } |
| 4715 |
| 4716 // Raise one of the children in z-order and add another child to reorder. |
| 4717 view->child_at(2)->set_id(OrderableView::VIEW_ID_RAISED); |
| 4718 AddViewWithChildLayer(view.get()); |
| 4719 |
| 4720 // 2nd child should be now on top, i.e. the last element in the array returned |
| 4721 // by GetChildrenInZOrder(). Its layer should also be above the others. |
| 4722 // The rest of the children and layers order should be unchanged. |
| 4723 const int expected_order[] = {0, 1, 3, 4, 2}; |
| 4724 children = view->GetChildrenInZOrder(); |
| 4725 EXPECT_EQ(kChildrenCount + 1, static_cast<int>(children.size())); |
| 4726 EXPECT_EQ(kChildrenCount + 1, static_cast<int>(layers.size())); |
| 4727 for (size_t i = 0; i < kChildrenCount + 1; ++i) { |
| 4728 EXPECT_EQ(view->child_at(expected_order[i]), children[i]); |
| 4729 EXPECT_EQ(view->child_at(expected_order[i])->layer(), layers[i]); |
| 4730 } |
| 4731 } |
| 4732 |
4672 //////////////////////////////////////////////////////////////////////////////// | 4733 //////////////////////////////////////////////////////////////////////////////// |
4673 // Observer tests. | 4734 // Observer tests. |
4674 //////////////////////////////////////////////////////////////////////////////// | 4735 //////////////////////////////////////////////////////////////////////////////// |
4675 | 4736 |
4676 class ViewObserverTest : public ViewTest, public ViewObserver { | 4737 class ViewObserverTest : public ViewTest, public ViewObserver { |
4677 public: | 4738 public: |
4678 ViewObserverTest() | 4739 ViewObserverTest() |
4679 : child_view_added_times_(0), | 4740 : child_view_added_times_(0), |
4680 child_view_removed_times_(0), | 4741 child_view_removed_times_(0), |
4681 child_view_added_(nullptr), | 4742 child_view_added_(nullptr), |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4821 std::unique_ptr<View> view = NewView(); | 4882 std::unique_ptr<View> view = NewView(); |
4822 std::unique_ptr<View> child_view = NewView(); | 4883 std::unique_ptr<View> child_view = NewView(); |
4823 std::unique_ptr<View> child_view2 = NewView(); | 4884 std::unique_ptr<View> child_view2 = NewView(); |
4824 view->AddChildView(child_view.get()); | 4885 view->AddChildView(child_view.get()); |
4825 view->AddChildView(child_view2.get()); | 4886 view->AddChildView(child_view2.get()); |
4826 view->ReorderChildView(child_view2.get(), 0); | 4887 view->ReorderChildView(child_view2.get(), 0); |
4827 EXPECT_EQ(child_view2.get(), view_reordered()); | 4888 EXPECT_EQ(child_view2.get(), view_reordered()); |
4828 } | 4889 } |
4829 | 4890 |
4830 } // namespace views | 4891 } // namespace views |
OLD | NEW |