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.
5 #include "ash/display/display_controller.h"
7 #include "ash/test/ash_test_base.h"
8 #include "base/utf_string_conversions.h"
9 #include "ui/aura/client/tooltip_client.h"
10 #include "ui/aura/env.h"
11 #include "ui/aura/root_window.h"
12 #include "ui/aura/test/event_generator.h"
13 #include "ui/aura/window.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/gfx/font.h"
16 #include "ui/gfx/point.h"
17 #include "ui/views/corewm/tooltip_controller.h"
18 #include "ui/views/corewm/tooltip_controller_test_helper.h"
19 #include "ui/views/view.h"
20 #include "ui/views/widget/widget.h"
22 using views::corewm::TooltipController
;
23 using views::corewm::test::TooltipTestView
;
24 using views::corewm::test::TooltipControllerTestHelper
;
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
35 views::Widget
* CreateNewWidgetWithBoundsOn(int display
,
36 const gfx::Rect
& bounds
) {
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
;
42 params
.context
= Shell::GetAllRootWindows().at(display
);
44 params
.bounds
= bounds
;
50 views::Widget
* CreateNewWidgetOn(int display
) {
51 return CreateNewWidgetWithBoundsOn(display
, gfx::Rect());
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
);
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();
64 contents_view_bounds
.Union(view
->bounds());
65 contents_view
->SetBoundsRect(contents_view_bounds
);
66 widget
->SetBounds(gfx::Rect(widget
->GetWindowBoundsInScreen().origin(),
67 contents_view_bounds
.size()));
70 TooltipController
* GetController() {
71 return static_cast<TooltipController
*>(
72 aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow()));
75 gfx::Font
GetDefaultFont() {
76 return ui::ResourceBundle::GetSharedInstance().GetFont(
77 ui::ResourceBundle::BaseFont
);
82 class TooltipControllerTest
: public AshTestBase
{
84 TooltipControllerTest() {}
85 virtual ~TooltipControllerTest() {}
87 virtual void SetUp() OVERRIDE
{
89 helper_
.reset(new TooltipControllerTestHelper(GetController()));
93 scoped_ptr
<TooltipControllerTestHelper
> helper_
;
96 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest
);
99 TEST_F(TooltipControllerTest
, NonNullTooltipClient
) {
100 EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow())
102 EXPECT_EQ(base::string16(), helper_
->GetTooltipText());
103 EXPECT_EQ(NULL
, helper_
->GetTooltipWindow());
104 EXPECT_FALSE(helper_
->IsTooltipVisible());
107 TEST_F(TooltipControllerTest
, HideTooltipWhenCursorHidden
) {
108 scoped_ptr
<views::Widget
> widget(CreateNewWidgetOn(0));
109 TooltipTestView
* view
= new TooltipTestView
;
110 AddViewToWidgetAndResize(widget
.get(), view
);
111 view
->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
112 EXPECT_EQ(base::string16(), helper_
->GetTooltipText());
113 EXPECT_EQ(NULL
, helper_
->GetTooltipWindow());
115 aura::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
116 generator
.MoveMouseRelativeTo(widget
->GetNativeView(),
117 view
->bounds().CenterPoint());
118 base::string16 expected_tooltip
= ASCIIToUTF16("Tooltip Text");
120 // Fire tooltip timer so tooltip becomes visible.
121 helper_
->FireTooltipTimer();
122 EXPECT_TRUE(helper_
->IsTooltipVisible());
124 // Hide the cursor and check again.
125 ash::Shell::GetInstance()->cursor_manager()->DisableMouseEvents();
126 helper_
->FireTooltipTimer();
127 EXPECT_FALSE(helper_
->IsTooltipVisible());
129 // Show the cursor and re-check.
130 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
131 helper_
->FireTooltipTimer();
132 EXPECT_TRUE(helper_
->IsTooltipVisible());
136 // Multiple displays are not supported on Windows Ash. http://crbug.com/165962
137 #define MAYBE_TooltipsOnMultiDisplayShouldNotCrash \
138 DISABLED_TooltipsOnMultiDisplayShouldNotCrash
140 #define MAYBE_TooltipsOnMultiDisplayShouldNotCrash \
141 TooltipsOnMultiDisplayShouldNotCrash
144 TEST_F(TooltipControllerTest
, MAYBE_TooltipsOnMultiDisplayShouldNotCrash
) {
145 UpdateDisplay("1000x600,600x400");
146 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
147 scoped_ptr
<views::Widget
> widget1(CreateNewWidgetWithBoundsOn(
148 0, gfx::Rect(10, 10, 100, 100)));
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]);
154 scoped_ptr
<views::Widget
> widget2(CreateNewWidgetWithBoundsOn(
155 1, gfx::Rect(1200, 10, 100, 100)));
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]);
161 // Show tooltip on second display.
162 aura::test::EventGenerator
generator(root_windows
[1]);
163 generator
.MoveMouseRelativeTo(widget2
->GetNativeView(),
164 view2
->bounds().CenterPoint());
165 helper_
->FireTooltipTimer();
166 EXPECT_TRUE(helper_
->IsTooltipVisible());
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");
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.
175 EXPECT_FALSE(helper_
->IsTooltipVisible());
177 EXPECT_EQ(widget2
->GetNativeView()->GetRootWindow(), root_windows
[0]);
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());
184 helper_
->FireTooltipTimer();
185 EXPECT_TRUE(helper_
->IsTooltipVisible());