[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | a4f3d05 | 2012-08-08 02:37:20 | [diff] [blame] | 5 | #include "ash/display/display_controller.h" |
[email protected] | e73bd780 | 2012-02-17 20:10:34 | [diff] [blame] | 6 | #include "ash/shell.h" |
[email protected] | 1dd1c1b | 2012-02-17 22:04:47 | [diff] [blame] | 7 | #include "ash/test/ash_test_base.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame^] | 8 | #include "base/strings/utf_string_conversions.h" |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 9 | #include "ui/aura/client/tooltip_client.h" |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame] | 10 | #include "ui/aura/env.h" |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 11 | #include "ui/aura/root_window.h" |
[email protected] | 68a8e06 | 2012-01-26 01:39:49 | [diff] [blame] | 12 | #include "ui/aura/test/event_generator.h" |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 13 | #include "ui/aura/window.h" |
[email protected] | 1eea614 | 2012-03-22 09:30:01 | [diff] [blame] | 14 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 1eea614 | 2012-03-22 09:30:01 | [diff] [blame] | 15 | #include "ui/gfx/font.h" |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 16 | #include "ui/gfx/point.h" |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 17 | #include "ui/views/corewm/tooltip_controller.h" |
| 18 | #include "ui/views/corewm/tooltip_controller_test_helper.h" |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 19 | #include "ui/views/view.h" |
| 20 | #include "ui/views/widget/widget.h" |
| 21 | |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 22 | using views::corewm::TooltipController; |
| 23 | using views::corewm::test::TooltipTestView; |
| 24 | using views::corewm::test::TooltipControllerTestHelper; |
| 25 | |
| 26 | // The tests in this file exercise bits of TooltipController that are hard to |
| 27 | // test outside of ash. Meaning these tests require the shell and related things |
| 28 | // to be installed. |
| 29 | |
[email protected] | 55f59335 | 2011-12-24 05:42:46 | [diff] [blame] | 30 | namespace ash { |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 31 | namespace test { |
| 32 | |
| 33 | namespace { |
| 34 | |
[email protected] | 5ebe610 | 2012-11-28 21:00:03 | [diff] [blame] | 35 | views::Widget* CreateNewWidgetWithBoundsOn(int display, |
| 36 | const gfx::Rect& bounds) { |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 37 | views::Widget* widget = new views::Widget; |
| 38 | views::Widget::InitParams params; |
| 39 | params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
| 40 | params.accept_events = true; |
| 41 | params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
[email protected] | 5ebe610 | 2012-11-28 21:00:03 | [diff] [blame] | 42 | params.context = Shell::GetAllRootWindows().at(display); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 43 | params.child = true; |
[email protected] | a4f3d05 | 2012-08-08 02:37:20 | [diff] [blame] | 44 | params.bounds = bounds; |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 45 | widget->Init(params); |
| 46 | widget->Show(); |
| 47 | return widget; |
| 48 | } |
| 49 | |
[email protected] | 5ebe610 | 2012-11-28 21:00:03 | [diff] [blame] | 50 | views::Widget* CreateNewWidgetOn(int display) { |
| 51 | return CreateNewWidgetWithBoundsOn(display, gfx::Rect()); |
[email protected] | a4f3d05 | 2012-08-08 02:37:20 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 54 | void AddViewToWidgetAndResize(views::Widget* widget, views::View* view) { |
| 55 | if (!widget->GetContentsView()) { |
| 56 | views::View* contents_view = new views::View; |
| 57 | widget->SetContentsView(contents_view); |
| 58 | } |
| 59 | |
| 60 | views::View* contents_view = widget->GetContentsView(); |
| 61 | contents_view->AddChildView(view); |
| 62 | view->SetBounds(contents_view->width(), 0, 100, 100); |
| 63 | gfx::Rect contents_view_bounds = contents_view->bounds(); |
[email protected] | d403050 | 2012-10-23 16:51:47 | [diff] [blame] | 64 | contents_view_bounds.Union(view->bounds()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 65 | contents_view->SetBoundsRect(contents_view_bounds); |
[email protected] | a4f3d05 | 2012-08-08 02:37:20 | [diff] [blame] | 66 | widget->SetBounds(gfx::Rect(widget->GetWindowBoundsInScreen().origin(), |
| 67 | contents_view_bounds.size())); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 70 | TooltipController* GetController() { |
| 71 | return static_cast<TooltipController*>( |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 72 | aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow())); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | 1eea614 | 2012-03-22 09:30:01 | [diff] [blame] | 75 | gfx::Font GetDefaultFont() { |
| 76 | return ui::ResourceBundle::GetSharedInstance().GetFont( |
| 77 | ui::ResourceBundle::BaseFont); |
| 78 | } |
| 79 | |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 80 | } // namespace |
| 81 | |
[email protected] | 1dd1c1b | 2012-02-17 22:04:47 | [diff] [blame] | 82 | class TooltipControllerTest : public AshTestBase { |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 83 | public: |
| 84 | TooltipControllerTest() {} |
| 85 | virtual ~TooltipControllerTest() {} |
| 86 | |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 87 | virtual void SetUp() OVERRIDE { |
| 88 | AshTestBase::SetUp(); |
| 89 | helper_.reset(new TooltipControllerTestHelper(GetController())); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 92 | protected: |
| 93 | scoped_ptr<TooltipControllerTestHelper> helper_; |
[email protected] | 1eea614 | 2012-03-22 09:30:01 | [diff] [blame] | 94 | |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 95 | private: |
| 96 | DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest); |
| 97 | }; |
| 98 | |
| 99 | TEST_F(TooltipControllerTest, NonNullTooltipClient) { |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 100 | EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow()) |
| 101 | != NULL); |
[email protected] | ed95e02 | 2013-04-11 04:03:32 | [diff] [blame] | 102 | EXPECT_EQ(base::string16(), helper_->GetTooltipText()); |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 103 | EXPECT_EQ(NULL, helper_->GetTooltipWindow()); |
| 104 | EXPECT_FALSE(helper_->IsTooltipVisible()); |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 107 | TEST_F(TooltipControllerTest, HideTooltipWhenCursorHidden) { |
[email protected] | 5ebe610 | 2012-11-28 21:00:03 | [diff] [blame] | 108 | scoped_ptr<views::Widget> widget(CreateNewWidgetOn(0)); |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 109 | TooltipTestView* view = new TooltipTestView; |
| 110 | AddViewToWidgetAndResize(widget.get(), view); |
| 111 | view->set_tooltip_text(ASCIIToUTF16("Tooltip Text")); |
[email protected] | ed95e02 | 2013-04-11 04:03:32 | [diff] [blame] | 112 | EXPECT_EQ(base::string16(), helper_->GetTooltipText()); |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 113 | EXPECT_EQ(NULL, helper_->GetTooltipWindow()); |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 114 | |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 115 | aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 116 | generator.MoveMouseRelativeTo(widget->GetNativeView(), |
| 117 | view->bounds().CenterPoint()); |
[email protected] | ed95e02 | 2013-04-11 04:03:32 | [diff] [blame] | 118 | base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text"); |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 119 | |
| 120 | // Fire tooltip timer so tooltip becomes visible. |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 121 | helper_->FireTooltipTimer(); |
| 122 | EXPECT_TRUE(helper_->IsTooltipVisible()); |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 123 | |
| 124 | // Hide the cursor and check again. |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame] | 125 | ash::Shell::GetInstance()->cursor_manager()->DisableMouseEvents(); |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 126 | helper_->FireTooltipTimer(); |
| 127 | EXPECT_FALSE(helper_->IsTooltipVisible()); |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 128 | |
| 129 | // Show the cursor and re-check. |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame] | 130 | ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents(); |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 131 | helper_->FireTooltipTimer(); |
| 132 | EXPECT_TRUE(helper_->IsTooltipVisible()); |
[email protected] | 7109ce1 | 2012-07-24 01:20:33 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | 1c3f700 | 2013-01-21 18:46:05 | [diff] [blame] | 135 | #if defined(OS_WIN) |
| 136 | // Multiple displays are not supported on Windows Ash. http://crbug.com/165962 |
| 137 | #define MAYBE_TooltipsOnMultiDisplayShouldNotCrash \ |
| 138 | DISABLED_TooltipsOnMultiDisplayShouldNotCrash |
| 139 | #else |
| 140 | #define MAYBE_TooltipsOnMultiDisplayShouldNotCrash \ |
| 141 | TooltipsOnMultiDisplayShouldNotCrash |
| 142 | #endif |
| 143 | |
| 144 | TEST_F(TooltipControllerTest, MAYBE_TooltipsOnMultiDisplayShouldNotCrash) { |
[email protected] | a4f3d05 | 2012-08-08 02:37:20 | [diff] [blame] | 145 | UpdateDisplay("1000x600,600x400"); |
| 146 | Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); |
[email protected] | 5ebe610 | 2012-11-28 21:00:03 | [diff] [blame] | 147 | scoped_ptr<views::Widget> widget1(CreateNewWidgetWithBoundsOn( |
| 148 | 0, gfx::Rect(10, 10, 100, 100))); |
[email protected] | a4f3d05 | 2012-08-08 02:37:20 | [diff] [blame] | 149 | TooltipTestView* view1 = new TooltipTestView; |
| 150 | AddViewToWidgetAndResize(widget1.get(), view1); |
| 151 | view1->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 1")); |
| 152 | EXPECT_EQ(widget1->GetNativeView()->GetRootWindow(), root_windows[0]); |
| 153 | |
[email protected] | 5ebe610 | 2012-11-28 21:00:03 | [diff] [blame] | 154 | scoped_ptr<views::Widget> widget2(CreateNewWidgetWithBoundsOn( |
| 155 | 1, gfx::Rect(1200, 10, 100, 100))); |
[email protected] | a4f3d05 | 2012-08-08 02:37:20 | [diff] [blame] | 156 | TooltipTestView* view2 = new TooltipTestView; |
| 157 | AddViewToWidgetAndResize(widget2.get(), view2); |
| 158 | view2->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 2")); |
| 159 | EXPECT_EQ(widget2->GetNativeView()->GetRootWindow(), root_windows[1]); |
| 160 | |
| 161 | // Show tooltip on second display. |
| 162 | aura::test::EventGenerator generator(root_windows[1]); |
| 163 | generator.MoveMouseRelativeTo(widget2->GetNativeView(), |
| 164 | view2->bounds().CenterPoint()); |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 165 | helper_->FireTooltipTimer(); |
| 166 | EXPECT_TRUE(helper_->IsTooltipVisible()); |
[email protected] | a4f3d05 | 2012-08-08 02:37:20 | [diff] [blame] | 167 | |
| 168 | // Get rid of secondary display. This destroy's the tooltip's aura window. If |
| 169 | // we have handled this case, we will not crash in the following statement. |
| 170 | UpdateDisplay("1000x600"); |
[email protected] | dbc7b18 | 2012-11-02 18:18:16 | [diff] [blame] | 171 | #if !defined(OS_WIN) |
| 172 | // TODO(cpu): Detangle the window destruction notification. Currently |
| 173 | // the TooltipController::OnWindowDestroyed is not being called then the |
| 174 | // display is torn down so the tooltip is is still there. |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 175 | EXPECT_FALSE(helper_->IsTooltipVisible()); |
[email protected] | dbc7b18 | 2012-11-02 18:18:16 | [diff] [blame] | 176 | #endif |
[email protected] | a4f3d05 | 2012-08-08 02:37:20 | [diff] [blame] | 177 | EXPECT_EQ(widget2->GetNativeView()->GetRootWindow(), root_windows[0]); |
| 178 | |
| 179 | // The tooltip should create a new aura window for itself, so we should still |
| 180 | // be able to show tooltips on the primary display. |
| 181 | aura::test::EventGenerator generator1(root_windows[0]); |
| 182 | generator1.MoveMouseRelativeTo(widget1->GetNativeView(), |
| 183 | view1->bounds().CenterPoint()); |
[email protected] | a1b7a82 | 2013-02-23 19:08:04 | [diff] [blame] | 184 | helper_->FireTooltipTimer(); |
| 185 | EXPECT_TRUE(helper_->IsTooltipVisible()); |
[email protected] | a4f3d05 | 2012-08-08 02:37:20 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 188 | } // namespace test |
[email protected] | 55f59335 | 2011-12-24 05:42:46 | [diff] [blame] | 189 | } // namespace ash |