Chromium Code Reviews| 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 23 matching lines...) Expand all Loading... | |
| 34 #include "ui/gfx/path.h" | 34 #include "ui/gfx/path.h" |
| 35 #include "ui/gfx/transform.h" | 35 #include "ui/gfx/transform.h" |
| 36 #include "ui/native_theme/native_theme.h" | 36 #include "ui/native_theme/native_theme.h" |
| 37 #include "ui/strings/grit/ui_strings.h" | 37 #include "ui/strings/grit/ui_strings.h" |
| 38 #include "ui/views/background.h" | 38 #include "ui/views/background.h" |
| 39 #include "ui/views/controls/native/native_view_host.h" | 39 #include "ui/views/controls/native/native_view_host.h" |
| 40 #include "ui/views/controls/scroll_view.h" | 40 #include "ui/views/controls/scroll_view.h" |
| 41 #include "ui/views/controls/textfield/textfield.h" | 41 #include "ui/views/controls/textfield/textfield.h" |
| 42 #include "ui/views/focus/view_storage.h" | 42 #include "ui/views/focus/view_storage.h" |
| 43 #include "ui/views/test/views_test_base.h" | 43 #include "ui/views/test/views_test_base.h" |
| 44 #include "ui/views/view_observer.h" | |
| 44 #include "ui/views/widget/native_widget.h" | 45 #include "ui/views/widget/native_widget.h" |
| 45 #include "ui/views/widget/root_view.h" | 46 #include "ui/views/widget/root_view.h" |
| 46 #include "ui/views/window/dialog_client_view.h" | 47 #include "ui/views/window/dialog_client_view.h" |
| 47 #include "ui/views/window/dialog_delegate.h" | 48 #include "ui/views/window/dialog_delegate.h" |
| 48 | 49 |
| 49 using base::ASCIIToUTF16; | 50 using base::ASCIIToUTF16; |
| 50 | 51 |
| 51 namespace { | 52 namespace { |
| 52 | 53 |
| 53 // Returns true if |ancestor| is an ancestor of |layer|. | 54 // Returns true if |ancestor| is an ancestor of |layer|. |
| (...skipping 4516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4570 params.bounds = gfx::Rect(50, 50, 350, 350); | 4571 params.bounds = gfx::Rect(50, 50, 350, 350); |
| 4571 widget.Init(params); | 4572 widget.Init(params); |
| 4572 | 4573 |
| 4573 AddViewWithChildLayer(widget.GetRootView()); | 4574 AddViewWithChildLayer(widget.GetRootView()); |
| 4574 ViewThatAddsViewInOnNativeThemeChanged* v = | 4575 ViewThatAddsViewInOnNativeThemeChanged* v = |
| 4575 new ViewThatAddsViewInOnNativeThemeChanged; | 4576 new ViewThatAddsViewInOnNativeThemeChanged; |
| 4576 widget.GetRootView()->AddChildView(v); | 4577 widget.GetRootView()->AddChildView(v); |
| 4577 EXPECT_TRUE(v->on_native_theme_changed_called()); | 4578 EXPECT_TRUE(v->on_native_theme_changed_called()); |
| 4578 } | 4579 } |
| 4579 | 4580 |
| 4581 //////////////////////////////////////////////////////////////////////////////// | |
| 4582 // Observer tests. | |
| 4583 //////////////////////////////////////////////////////////////////////////////// | |
| 4584 | |
| 4585 class ViewObserverTest : public ViewTest, public ViewObserver { | |
| 4586 public: | |
| 4587 ViewObserverTest() | |
| 4588 : view_visibility_changed_(nullptr), | |
| 4589 view_enabled_changed_(nullptr), | |
| 4590 view_bounds_changed_(nullptr), | |
| 4591 view_reordered_(nullptr) {} | |
| 4592 | |
| 4593 ~ViewObserverTest() override {} | |
| 4594 | |
| 4595 void OnViewParentChanged(View* target, | |
|
sky
2016/11/21 16:33:37
Generally we prefix overrides with the class they
Sarmad Hashmi
2016/11/21 17:09:54
Done.
| |
| 4596 View* old_parent, | |
| 4597 View* new_parent) override { | |
| 4598 ViewParentChangedDetails details; | |
| 4599 details.target = target; | |
| 4600 details.old_parent = old_parent; | |
| 4601 details.new_parent = new_parent; | |
| 4602 views_parent_changed_details_.push_back(details); | |
| 4603 } | |
| 4604 | |
| 4605 void OnViewVisibilityChanged(View* view) override { | |
| 4606 view_visibility_changed_ = view; | |
| 4607 } | |
| 4608 | |
| 4609 void OnViewEnabledChanged(View* view) override { | |
| 4610 view_enabled_changed_ = view; | |
| 4611 } | |
| 4612 | |
| 4613 void OnViewBoundsChanged(View* view) override { view_bounds_changed_ = view; } | |
| 4614 | |
| 4615 void OnChildViewReordered(View* view, int index) override { | |
| 4616 view_reordered_ = view; | |
| 4617 } | |
| 4618 | |
| 4619 bool DidChangeParent(View* target, View* old_parent, View* new_parent) { | |
| 4620 for (auto& details : views_parent_changed_details_) { | |
| 4621 if (details.target == target && details.old_parent == old_parent && | |
| 4622 details.new_parent == new_parent) | |
| 4623 return true; | |
| 4624 } | |
| 4625 return false; | |
| 4626 } | |
| 4627 | |
| 4628 void reset() { | |
| 4629 views_parent_changed_details_.clear(); | |
| 4630 view_visibility_changed_ = nullptr; | |
| 4631 view_enabled_changed_ = nullptr; | |
| 4632 view_bounds_changed_ = nullptr; | |
| 4633 view_reordered_ = nullptr; | |
| 4634 } | |
| 4635 | |
| 4636 std::unique_ptr<View> NewView() { | |
| 4637 auto view = base::MakeUnique<View>(); | |
| 4638 view->AddObserver(this); | |
| 4639 return view; | |
| 4640 } | |
| 4641 | |
| 4642 int views_parent_changed_times() const { | |
| 4643 return static_cast<int>(views_parent_changed_details_.size()); | |
| 4644 } | |
| 4645 const View* view_visibility_changed() const { | |
| 4646 return view_visibility_changed_; | |
| 4647 } | |
| 4648 const View* view_enabled_changed() const { return view_enabled_changed_; } | |
| 4649 const View* view_bounds_changed() const { return view_bounds_changed_; } | |
| 4650 const View* view_reordered() const { return view_reordered_; } | |
| 4651 | |
| 4652 private: | |
| 4653 struct ViewParentChangedDetails { | |
| 4654 View* target; | |
| 4655 View* old_parent; | |
| 4656 View* new_parent; | |
| 4657 }; | |
| 4658 std::vector<ViewParentChangedDetails> views_parent_changed_details_; | |
| 4659 View* view_visibility_changed_; | |
| 4660 View* view_enabled_changed_; | |
| 4661 View* view_bounds_changed_; | |
| 4662 View* view_reordered_; | |
| 4663 }; | |
|
sky
2016/11/21 16:33:37
DISALLOW...
Sarmad Hashmi
2016/11/21 17:09:53
Done.
| |
| 4664 | |
| 4665 TEST_F(ViewObserverTest, ViewParentChanged) { | |
| 4666 std::unique_ptr<View> parent1 = NewView(); | |
| 4667 std::unique_ptr<View> parent2 = NewView(); | |
| 4668 std::unique_ptr<View> child_view = NewView(); | |
| 4669 | |
| 4670 parent1->AddChildView(child_view.get()); | |
| 4671 EXPECT_EQ(1, views_parent_changed_times()); | |
| 4672 EXPECT_TRUE(DidChangeParent(child_view.get(), nullptr, parent1.get())); | |
| 4673 reset(); | |
| 4674 | |
| 4675 // Removed from parent1, added to parent2 | |
| 4676 parent2->AddChildView(child_view.get()); | |
| 4677 EXPECT_EQ(2, views_parent_changed_times()); | |
| 4678 EXPECT_TRUE(DidChangeParent(child_view.get(), parent1.get(), nullptr)); | |
| 4679 EXPECT_TRUE(DidChangeParent(child_view.get(), nullptr, parent2.get())); | |
| 4680 | |
| 4681 reset(); | |
| 4682 | |
| 4683 parent2->RemoveChildView(child_view.get()); | |
| 4684 EXPECT_EQ(1, views_parent_changed_times()); | |
| 4685 EXPECT_TRUE(DidChangeParent(child_view.get(), parent2.get(), nullptr)); | |
| 4686 } | |
| 4687 | |
| 4688 TEST_F(ViewObserverTest, ViewVisibilityChanged) { | |
| 4689 std::unique_ptr<View> view = NewView(); | |
| 4690 view->SetVisible(false); | |
| 4691 EXPECT_EQ(view.get(), view_visibility_changed()); | |
| 4692 EXPECT_EQ(false, view->visible()); | |
| 4693 } | |
| 4694 | |
| 4695 TEST_F(ViewObserverTest, ViewEnabledChanged) { | |
| 4696 std::unique_ptr<View> view = NewView(); | |
| 4697 view->SetEnabled(false); | |
| 4698 EXPECT_EQ(view.get(), view_enabled_changed()); | |
| 4699 EXPECT_EQ(false, view->enabled()); | |
| 4700 } | |
| 4701 | |
| 4702 TEST_F(ViewObserverTest, ViewBoundsChanged) { | |
| 4703 std::unique_ptr<View> view = NewView(); | |
| 4704 gfx::Rect bounds(2, 2, 2, 2); | |
| 4705 view->SetBoundsRect(bounds); | |
| 4706 EXPECT_EQ(view.get(), view_bounds_changed()); | |
| 4707 EXPECT_EQ(bounds, view->bounds()); | |
| 4708 | |
| 4709 reset(); | |
| 4710 | |
| 4711 gfx::Rect new_bounds(1, 1, 1, 1); | |
| 4712 view->SetBoundsRect(new_bounds); | |
| 4713 EXPECT_EQ(view.get(), view_bounds_changed()); | |
| 4714 EXPECT_EQ(new_bounds, view->bounds()); | |
| 4715 } | |
| 4716 | |
| 4717 TEST_F(ViewObserverTest, ChildViewReordered) { | |
| 4718 std::unique_ptr<View> view = NewView(); | |
| 4719 std::unique_ptr<View> child_view = NewView(); | |
| 4720 std::unique_ptr<View> child_view2 = NewView(); | |
| 4721 view->AddChildView(child_view.get()); | |
| 4722 view->AddChildView(child_view2.get()); | |
| 4723 view->ReorderChildView(child_view2.get(), 0); | |
| 4724 EXPECT_EQ(child_view2.get(), view_reordered()); | |
| 4725 } | |
| 4726 | |
| 4580 } // namespace views | 4727 } // namespace views |
| OLD | NEW |