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
3 // found in the LICENSE file.
6 #include "ash/test/ash_test_base.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/aura/client/tooltip_client.h"
9 #include "ui/aura/env.h"
10 #include "ui/aura/root_window.h"
11 #include "ui/aura/test/event_generator.h"
12 #include "ui/aura/window.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/gfx/font.h"
15 #include "ui/gfx/point.h"
16 #include "ui/views/corewm/tooltip_controller.h"
17 #include "ui/views/corewm/tooltip_controller_test_helper.h"
18 #include "ui/views/view.h"
19 #include "ui/views/widget/widget.h"
21 using views::corewm::TooltipController
;
22 using views::corewm::test::TooltipTestView
;
23 using views::corewm::test::TooltipControllerTestHelper
;
25 // The tests in this file exercise bits of TooltipController that are hard to
26 // test outside of ash. Meaning these tests require the shell and related things
34 views::Widget
* CreateNewWidgetWithBoundsOn(int display
,
35 const gfx::Rect
& bounds
) {
36 views::Widget
* widget
= new views::Widget
;
37 views::Widget::InitParams params
;
38 params
.type
= views::Widget::InitParams::TYPE_WINDOW_FRAMELESS
;
39 params
.accept_events
= true;
40 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
41 params
.context
= Shell::GetAllRootWindows().at(display
);
43 params
.bounds
= bounds
;
49 views::Widget
* CreateNewWidgetOn(int display
) {
50 return CreateNewWidgetWithBoundsOn(display
, gfx::Rect());
53 void AddViewToWidgetAndResize(views::Widget
* widget
, views::View
* view
) {
54 if (!widget
->GetContentsView()) {
55 views::View
* contents_view
= new views::View
;
56 widget
->SetContentsView(contents_view
);
59 views::View
* contents_view
= widget
->GetContentsView();
60 contents_view
->AddChildView(view
);
61 view
->SetBounds(contents_view
->width(), 0, 100, 100);
62 gfx::Rect contents_view_bounds
= contents_view
->bounds();
63 contents_view_bounds
.Union(view
->bounds());
64 contents_view
->SetBoundsRect(contents_view_bounds
);
65 widget
->SetBounds(gfx::Rect(widget
->GetWindowBoundsInScreen().origin(),
66 contents_view_bounds
.size()));
69 TooltipController
* GetController() {
70 return static_cast<TooltipController
*>(
71 aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow()));
76 class TooltipControllerTest
: public AshTestBase
{
78 TooltipControllerTest() {}
79 virtual ~TooltipControllerTest() {}
81 virtual void SetUp() OVERRIDE
{
83 helper_
.reset(new TooltipControllerTestHelper(GetController()));
87 scoped_ptr
<TooltipControllerTestHelper
> helper_
;
90 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest
);
93 TEST_F(TooltipControllerTest
, NonNullTooltipClient
) {
94 EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow())
96 EXPECT_EQ(base::string16(), helper_
->GetTooltipText());
97 EXPECT_EQ(NULL
, helper_
->GetTooltipWindow());
98 EXPECT_FALSE(helper_
->IsTooltipVisible());
101 TEST_F(TooltipControllerTest
, HideTooltipWhenCursorHidden
) {
102 scoped_ptr
<views::Widget
> widget(CreateNewWidgetOn(0));
103 TooltipTestView
* view
= new TooltipTestView
;
104 AddViewToWidgetAndResize(widget
.get(), view
);
105 view
->set_tooltip_text(base::ASCIIToUTF16("Tooltip Text"));
106 EXPECT_EQ(base::string16(), helper_
->GetTooltipText());
107 EXPECT_EQ(NULL
, helper_
->GetTooltipWindow());
109 aura::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
110 generator
.MoveMouseRelativeTo(widget
->GetNativeView(),
111 view
->bounds().CenterPoint());
112 base::string16 expected_tooltip
= base::ASCIIToUTF16("Tooltip Text");
114 // Fire tooltip timer so tooltip becomes visible.
115 helper_
->FireTooltipTimer();
116 EXPECT_TRUE(helper_
->IsTooltipVisible());
118 // Hide the cursor and check again.
119 ash::Shell::GetInstance()->cursor_manager()->DisableMouseEvents();
120 helper_
->FireTooltipTimer();
121 EXPECT_FALSE(helper_
->IsTooltipVisible());
123 // Show the cursor and re-check.
124 RunAllPendingInMessageLoop();
125 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
126 RunAllPendingInMessageLoop();
127 helper_
->FireTooltipTimer();
128 EXPECT_TRUE(helper_
->IsTooltipVisible());
131 TEST_F(TooltipControllerTest
, TooltipsOnMultiDisplayShouldNotCrash
) {
132 if (!SupportsMultipleDisplays())
135 UpdateDisplay("1000x600,600x400");
136 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
137 scoped_ptr
<views::Widget
> widget1(CreateNewWidgetWithBoundsOn(
138 0, gfx::Rect(10, 10, 100, 100)));
139 TooltipTestView
* view1
= new TooltipTestView
;
140 AddViewToWidgetAndResize(widget1
.get(), view1
);
141 view1
->set_tooltip_text(base::ASCIIToUTF16("Tooltip Text for view 1"));
142 EXPECT_EQ(widget1
->GetNativeView()->GetRootWindow(), root_windows
[0]);
144 scoped_ptr
<views::Widget
> widget2(CreateNewWidgetWithBoundsOn(
145 1, gfx::Rect(1200, 10, 100, 100)));
146 TooltipTestView
* view2
= new TooltipTestView
;
147 AddViewToWidgetAndResize(widget2
.get(), view2
);
148 view2
->set_tooltip_text(base::ASCIIToUTF16("Tooltip Text for view 2"));
149 EXPECT_EQ(widget2
->GetNativeView()->GetRootWindow(), root_windows
[1]);
151 // Show tooltip on second display.
152 aura::test::EventGenerator
generator(root_windows
[1]);
153 generator
.MoveMouseRelativeTo(widget2
->GetNativeView(),
154 view2
->bounds().CenterPoint());
155 helper_
->FireTooltipTimer();
156 EXPECT_TRUE(helper_
->IsTooltipVisible());
158 // Get rid of secondary display. This destroy's the tooltip's aura window. If
159 // we have handled this case, we will not crash in the following statement.
160 UpdateDisplay("1000x600");
162 // TODO(cpu): Detangle the window destruction notification. Currently
163 // the TooltipController::OnWindowDestroyed is not being called then the
164 // display is torn down so the tooltip is is still there.
165 EXPECT_FALSE(helper_
->IsTooltipVisible());
167 EXPECT_EQ(widget2
->GetNativeView()->GetRootWindow(), root_windows
[0]);
169 // The tooltip should create a new aura window for itself, so we should still
170 // be able to show tooltips on the primary display.
171 aura::test::EventGenerator
generator1(root_windows
[0]);
172 generator1
.MoveMouseRelativeTo(widget1
->GetNativeView(),
173 view1
->bounds().CenterPoint());
174 helper_
->FireTooltipTimer();
175 EXPECT_TRUE(helper_
->IsTooltipVisible());