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