OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/corewm/tooltip_controller.h" | 5 #include "ui/views/corewm/tooltip_controller.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "ui/aura/client/cursor_client.h" | 8 #include "ui/aura/client/cursor_client.h" |
9 #include "ui/aura/client/screen_position_client.h" | 9 #include "ui/aura/client/screen_position_client.h" |
10 #include "ui/aura/env.h" | 10 #include "ui/aura/env.h" |
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
662 TestTooltip() : is_visible_(false) {} | 662 TestTooltip() : is_visible_(false) {} |
663 virtual ~TestTooltip() {} | 663 virtual ~TestTooltip() {} |
664 | 664 |
665 const base::string16& tooltip_text() const { return tooltip_text_; } | 665 const base::string16& tooltip_text() const { return tooltip_text_; } |
666 | 666 |
667 // Tooltip: | 667 // Tooltip: |
668 virtual void SetText(aura::Window* window, | 668 virtual void SetText(aura::Window* window, |
669 const base::string16& tooltip_text, | 669 const base::string16& tooltip_text, |
670 const gfx::Point& location) OVERRIDE { | 670 const gfx::Point& location) OVERRIDE { |
671 tooltip_text_ = tooltip_text; | 671 tooltip_text_ = tooltip_text; |
672 location_ = location; | |
672 } | 673 } |
673 virtual void Show() OVERRIDE { | 674 virtual void Show() OVERRIDE { |
674 is_visible_ = true; | 675 is_visible_ = true; |
675 } | 676 } |
676 virtual void Hide() OVERRIDE { | 677 virtual void Hide() OVERRIDE { |
677 is_visible_ = false; | 678 is_visible_ = false; |
678 } | 679 } |
679 virtual bool IsVisible() OVERRIDE { | 680 virtual bool IsVisible() OVERRIDE { |
680 return is_visible_; | 681 return is_visible_; |
681 } | 682 } |
683 gfx::Point location() { return location_; } | |
sky
2014/04/23 20:08:46
const gfx::Point&
Mikus
2014/04/24 08:39:56
Done.
| |
682 | 684 |
683 private: | 685 private: |
684 bool is_visible_; | 686 bool is_visible_; |
685 base::string16 tooltip_text_; | 687 base::string16 tooltip_text_; |
688 gfx::Point location_; | |
686 | 689 |
687 DISALLOW_COPY_AND_ASSIGN(TestTooltip); | 690 DISALLOW_COPY_AND_ASSIGN(TestTooltip); |
688 }; | 691 }; |
689 | 692 |
690 } // namespace | 693 } // namespace |
691 | 694 |
692 // Use for tests that don't depend upon views. | 695 // Use for tests that don't depend upon views. |
693 class TooltipControllerTest2 : public aura::test::AuraTestBase { | 696 class TooltipControllerTest2 : public aura::test::AuraTestBase { |
694 public: | 697 public: |
695 TooltipControllerTest2() : test_tooltip_(new TestTooltip) {} | 698 TooltipControllerTest2() : test_tooltip_(new TestTooltip) {} |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
756 EXPECT_TRUE(helper_->IsTooltipVisible()); | 759 EXPECT_TRUE(helper_->IsTooltipVisible()); |
757 | 760 |
758 // Send OnCancelMode event and verify that tooltip becomes invisible and | 761 // Send OnCancelMode event and verify that tooltip becomes invisible and |
759 // the tooltip window is closed. | 762 // the tooltip window is closed. |
760 ui::CancelModeEvent event; | 763 ui::CancelModeEvent event; |
761 helper_->controller()->OnCancelMode(&event); | 764 helper_->controller()->OnCancelMode(&event); |
762 EXPECT_FALSE(helper_->IsTooltipVisible()); | 765 EXPECT_FALSE(helper_->IsTooltipVisible()); |
763 EXPECT_TRUE(helper_->GetTooltipWindow() == NULL); | 766 EXPECT_TRUE(helper_->GetTooltipWindow() == NULL); |
764 } | 767 } |
765 | 768 |
769 // Use for tests that need both views and a TestTooltip. | |
770 class TooltipControllerTest3 : public aura::test::AuraTestBase { | |
771 public: | |
772 TooltipControllerTest3() : test_tooltip_(new TestTooltip) {} | |
773 virtual ~TooltipControllerTest3() {} | |
774 | |
775 virtual void SetUp() OVERRIDE { | |
776 wm_state_.reset(new wm::WMState); | |
777 aura::test::AuraTestBase::SetUp(); | |
778 | |
779 widget_.reset(CreateWidget(root_window())); | |
780 widget_->SetContentsView(new View); | |
781 view_ = new TooltipTestView; | |
782 widget_->GetContentsView()->AddChildView(view_); | |
783 view_->SetBoundsRect(widget_->GetContentsView()->GetLocalBounds()); | |
784 | |
785 generator_.reset(new aura::test::EventGenerator(GetRootWindow())); | |
786 controller_.reset(new TooltipController( | |
787 scoped_ptr<views::corewm::Tooltip>(test_tooltip_))); | |
788 GetRootWindow()->RemovePreTargetHandler( | |
789 static_cast<TooltipController*>(aura::client::GetTooltipClient( | |
790 widget_->GetNativeWindow()->GetRootWindow()))); | |
791 GetRootWindow()->AddPreTargetHandler(controller_.get()); | |
792 helper_.reset(new TooltipControllerTestHelper(controller_.get())); | |
793 SetTooltipClient(GetRootWindow(), controller_.get()); | |
794 } | |
795 | |
796 virtual void TearDown() OVERRIDE { | |
797 GetRootWindow()->RemovePreTargetHandler(controller_.get()); | |
798 aura::client::SetTooltipClient(GetRootWindow(), NULL); | |
799 | |
800 controller_.reset(); | |
801 generator_.reset(); | |
802 helper_.reset(); | |
803 widget_.reset(); | |
804 aura::test::AuraTestBase::TearDown(); | |
805 wm_state_.reset(); | |
806 } | |
807 | |
808 aura::Window* GetWindow() { return widget_->GetNativeWindow(); } | |
809 | |
810 protected: | |
811 // Owned by |controller_|. | |
812 TestTooltip* test_tooltip_; | |
813 scoped_ptr<TooltipControllerTestHelper> helper_; | |
814 scoped_ptr<aura::test::EventGenerator> generator_; | |
815 scoped_ptr<views::Widget> widget_; | |
816 TooltipTestView* view_; | |
817 | |
818 private: | |
819 scoped_ptr<TooltipController> controller_; | |
820 scoped_ptr<wm::WMState> wm_state_; | |
821 | |
822 aura::Window* GetRootWindow() { return GetWindow()->GetRootWindow(); } | |
823 | |
824 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest3); | |
825 }; | |
826 | |
827 TEST_F(TooltipControllerTest3, TooltipPositionChangesOnTwoViewsWithSameLabel) { | |
828 // Owned by |view_|. | |
829 // These two views have the same tooltip text | |
830 TooltipTestView* v1 = new TooltipTestView; | |
831 TooltipTestView* v2 = new TooltipTestView; | |
832 // v1_1 is a view inside v1 that has an identical tooltip text to that of v1 | |
833 // and v2 | |
834 TooltipTestView* v1_1 = new TooltipTestView; | |
835 // v2_1 is a view inside v2 that has an identical tooltip text to that of v1 | |
836 // and v2 | |
837 TooltipTestView* v2_1 = new TooltipTestView; | |
838 // v2_2 is a view inside v2 with the tooltip text different from all the | |
839 // others | |
840 TooltipTestView* v2_2 = new TooltipTestView; | |
841 | |
842 // Setup all the views' relations | |
843 view_->AddChildView(v1); | |
844 view_->AddChildView(v2); | |
845 v1->AddChildView(v1_1); | |
846 v2->AddChildView(v2_1); | |
847 v2->AddChildView(v2_2); | |
848 const base::string16 reference_string( | |
849 base::ASCIIToUTF16("Identical Tooltip Text")); | |
850 const base::string16 alternative_string( | |
851 base::ASCIIToUTF16("Another Shrubbery")); | |
852 v1->set_tooltip_text(reference_string); | |
853 v2->set_tooltip_text(reference_string); | |
854 v1_1->set_tooltip_text(reference_string); | |
855 v2_1->set_tooltip_text(reference_string); | |
856 v2_2->set_tooltip_text(alternative_string); | |
857 | |
858 // Set views' bounds | |
859 gfx::Rect view_bounds(view_->GetLocalBounds()); | |
860 view_bounds.set_height(view_bounds.height() / 2); | |
861 v1->SetBoundsRect(view_bounds); | |
862 v1_1->SetBounds(0, 0, 3, 3); | |
863 view_bounds.set_y(view_bounds.height()); | |
864 v2->SetBoundsRect(view_bounds); | |
865 v2_2->SetBounds(view_bounds.width() - 3, view_bounds.height() - 3, 3, 3); | |
866 v2_1->SetBounds(0, 0, 3, 3); | |
867 | |
868 // Test whether a toolbar appears on v1 | |
869 gfx::Point center = v1->bounds().CenterPoint(); | |
870 generator_->MoveMouseRelativeTo(GetWindow(), center); | |
871 helper_->FireTooltipTimer(); | |
872 EXPECT_TRUE(helper_->IsTooltipVisible()); | |
873 EXPECT_EQ(reference_string, helper_->GetTooltipText()); | |
874 gfx::Point tooltip_bounds1 = test_tooltip_->location(); | |
875 | |
876 // Test whether the toolbar changes position on mouse over v2 | |
877 center = v2->bounds().CenterPoint(); | |
878 generator_->MoveMouseRelativeTo(GetWindow(), center); | |
879 helper_->FireTooltipTimer(); | |
880 EXPECT_TRUE(helper_->IsTooltipVisible()); | |
881 EXPECT_EQ(reference_string, helper_->GetTooltipText()); | |
882 gfx::Point tooltip_bounds2 = test_tooltip_->location(); | |
883 | |
884 EXPECT_NE(tooltip_bounds1, gfx::Point()); | |
885 EXPECT_NE(tooltip_bounds2, gfx::Point()); | |
886 EXPECT_NE(tooltip_bounds1, tooltip_bounds2); | |
887 | |
888 // Test if the toolbar does not change position on encountering a contained | |
889 // view with the same tooltip text | |
890 center = v2_1->GetLocalBounds().CenterPoint(); | |
891 views::View::ConvertPointToTarget(v2_1, view_, ¢er); | |
892 generator_->MoveMouseRelativeTo(GetWindow(), center); | |
893 helper_->FireTooltipTimer(); | |
894 gfx::Point tooltip_bounds2_1 = test_tooltip_->location(); | |
895 | |
896 EXPECT_EQ(tooltip_bounds2, tooltip_bounds2_1); | |
897 EXPECT_TRUE(helper_->IsTooltipVisible()); | |
898 EXPECT_EQ(reference_string, helper_->GetTooltipText()); | |
899 | |
900 // Test if the toolbar changes position on encountering a contained | |
901 // view with a different tooltip text | |
902 center = v2_2->GetLocalBounds().CenterPoint(); | |
903 views::View::ConvertPointToTarget(v2_2, view_, ¢er); | |
904 generator_->MoveMouseRelativeTo(GetWindow(), center); | |
905 helper_->FireTooltipTimer(); | |
906 gfx::Point tooltip_bounds2_2 = test_tooltip_->location(); | |
907 | |
908 EXPECT_NE(tooltip_bounds2_1, tooltip_bounds2_2); | |
909 EXPECT_TRUE(helper_->IsTooltipVisible()); | |
910 EXPECT_EQ(alternative_string, helper_->GetTooltipText()); | |
911 | |
912 // Test if moving from a view that is contained by a larger view, both with | |
913 // the same tooltip text, does not change tooltip's position. | |
914 center = v1_1->GetLocalBounds().CenterPoint(); | |
915 views::View::ConvertPointToTarget(v1_1, view_, ¢er); | |
916 generator_->MoveMouseRelativeTo(GetWindow(), center); | |
917 helper_->FireTooltipTimer(); | |
918 gfx::Point tooltip_bounds1_1 = test_tooltip_->location(); | |
919 | |
920 EXPECT_TRUE(helper_->IsTooltipVisible()); | |
921 EXPECT_EQ(reference_string, helper_->GetTooltipText()); | |
922 | |
923 center = v1->bounds().CenterPoint(); | |
924 generator_->MoveMouseRelativeTo(GetWindow(), center); | |
925 helper_->FireTooltipTimer(); | |
926 tooltip_bounds1 = test_tooltip_->location(); | |
927 | |
928 EXPECT_EQ(tooltip_bounds1_1, tooltip_bounds1); | |
929 EXPECT_EQ(reference_string, helper_->GetTooltipText()); | |
930 } | |
931 | |
766 } // namespace test | 932 } // namespace test |
767 } // namespace corewm | 933 } // namespace corewm |
768 } // namespace views | 934 } // namespace views |
OLD | NEW |