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