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/env.h"
9 #include "ui/aura/test/event_generator.h"
10 #include "ui/aura/window.h"
11 #include "ui/aura/window_event_dispatcher.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/gfx/font.h"
14 #include "ui/gfx/point.h"
15 #include "ui/views/corewm/tooltip_controller.h"
16 #include "ui/views/corewm/tooltip_controller_test_helper.h"
17 #include "ui/views/view.h"
18 #include "ui/views/widget/widget.h"
19 #include "ui/wm/public/tooltip_client.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
);
42 params
.bounds
= bounds
;
48 views::Widget
* CreateNewWidgetOn(int display
) {
49 return CreateNewWidgetWithBoundsOn(display
, gfx::Rect());
52 void AddViewToWidgetAndResize(views::Widget
* widget
, views::View
* view
) {
53 if (!widget
->GetContentsView()) {
54 views::View
* contents_view
= new views::View
;
55 widget
->SetContentsView(contents_view
);
58 views::View
* contents_view
= widget
->GetContentsView();
59 contents_view
->AddChildView(view
);
60 view
->SetBounds(contents_view
->width(), 0, 100, 100);
61 gfx::Rect contents_view_bounds
= contents_view
->bounds();
62 contents_view_bounds
.Union(view
->bounds());
63 contents_view
->SetBoundsRect(contents_view_bounds
);
64 widget
->SetBounds(gfx::Rect(widget
->GetWindowBoundsInScreen().origin(),
65 contents_view_bounds
.size()));
68 TooltipController
* GetController() {
69 return static_cast<TooltipController
*>(
70 aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow()));
75 class TooltipControllerTest
: public AshTestBase
{
77 TooltipControllerTest() {}
78 virtual ~TooltipControllerTest() {}
80 virtual void SetUp() OVERRIDE
{
82 helper_
.reset(new TooltipControllerTestHelper(GetController()));
86 scoped_ptr
<TooltipControllerTestHelper
> helper_
;
89 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest
);
92 TEST_F(TooltipControllerTest
, NonNullTooltipClient
) {
93 EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow())
95 EXPECT_EQ(base::string16(), helper_
->GetTooltipText());
96 EXPECT_EQ(NULL
, helper_
->GetTooltipWindow());
97 EXPECT_FALSE(helper_
->IsTooltipVisible());
100 TEST_F(TooltipControllerTest
, HideTooltipWhenCursorHidden
) {
101 scoped_ptr
<views::Widget
> widget(CreateNewWidgetOn(0));
102 TooltipTestView
* view
= new TooltipTestView
;
103 AddViewToWidgetAndResize(widget
.get(), view
);
104 view
->set_tooltip_text(base::ASCIIToUTF16("Tooltip Text"));
105 EXPECT_EQ(base::string16(), helper_
->GetTooltipText());
106 EXPECT_EQ(NULL
, helper_
->GetTooltipWindow());
108 aura::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
109 generator
.MoveMouseRelativeTo(widget
->GetNativeView(),
110 view
->bounds().CenterPoint());
111 base::string16 expected_tooltip
= base::ASCIIToUTF16("Tooltip Text");
113 // Fire tooltip timer so tooltip becomes visible.
114 helper_
->FireTooltipTimer();
115 EXPECT_TRUE(helper_
->IsTooltipVisible());
117 // Hide the cursor and check again.
118 ash::Shell::GetInstance()->cursor_manager()->DisableMouseEvents();
119 helper_
->FireTooltipTimer();
120 EXPECT_FALSE(helper_
->IsTooltipVisible());
122 // Show the cursor and re-check.
123 RunAllPendingInMessageLoop();
124 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
125 RunAllPendingInMessageLoop();
126 helper_
->FireTooltipTimer();
127 EXPECT_TRUE(helper_
->IsTooltipVisible());
130 TEST_F(TooltipControllerTest
, TooltipsOnMultiDisplayShouldNotCrash
) {
131 if (!SupportsMultipleDisplays())
134 UpdateDisplay("1000x600,600x400");
135 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
136 scoped_ptr
<views::Widget
> widget1(CreateNewWidgetWithBoundsOn(
137 0, gfx::Rect(10, 10, 100, 100)));
138 TooltipTestView
* view1
= new TooltipTestView
;
139 AddViewToWidgetAndResize(widget1
.get(), view1
);
140 view1
->set_tooltip_text(base::ASCIIToUTF16("Tooltip Text for view 1"));
141 EXPECT_EQ(widget1
->GetNativeView()->GetRootWindow(), root_windows
[0]);
143 scoped_ptr
<views::Widget
> widget2(CreateNewWidgetWithBoundsOn(
144 1, gfx::Rect(1200, 10, 100, 100)));
145 TooltipTestView
* view2
= new TooltipTestView
;
146 AddViewToWidgetAndResize(widget2
.get(), view2
);
147 view2
->set_tooltip_text(base::ASCIIToUTF16("Tooltip Text for view 2"));
148 EXPECT_EQ(widget2
->GetNativeView()->GetRootWindow(), root_windows
[1]);
150 // Show tooltip on second display.
151 aura::test::EventGenerator
generator(root_windows
[1]);
152 generator
.MoveMouseRelativeTo(widget2
->GetNativeView(),
153 view2
->bounds().CenterPoint());
154 helper_
->FireTooltipTimer();
155 EXPECT_TRUE(helper_
->IsTooltipVisible());
157 // Get rid of secondary display. This destroy's the tooltip's aura window. If
158 // we have handled this case, we will not crash in the following statement.
159 UpdateDisplay("1000x600");
161 // TODO(cpu): Detangle the window destruction notification. Currently
162 // the TooltipController::OnWindowDestroyed is not being called then the
163 // display is torn down so the tooltip is is still there.
164 EXPECT_FALSE(helper_
->IsTooltipVisible());
166 EXPECT_EQ(widget2
->GetNativeView()->GetRootWindow(), root_windows
[0]);
168 // The tooltip should create a new aura window for itself, so we should still
169 // be able to show tooltips on the primary display.
170 aura::test::EventGenerator
generator1(root_windows
[0]);
171 generator1
.MoveMouseRelativeTo(widget1
->GetNativeView(),
172 view1
->bounds().CenterPoint());
173 helper_
->FireTooltipTimer();
174 EXPECT_TRUE(helper_
->IsTooltipVisible());