blob: 4e560e4b83105036f01f8186860871da590eb748 [file] [log] [blame]
[email protected]5139f1e2012-01-18 23:58:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a7eb0182011-12-21 12:33:312// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]a4f3d052012-08-08 02:37:205#include "ash/display/display_controller.h"
[email protected]e73bd7802012-02-17 20:10:346#include "ash/shell.h"
[email protected]1dd1c1b2012-02-17 22:04:477#include "ash/test/ash_test_base.h"
[email protected]a4ea1f12013-06-07 18:37:078#include "base/strings/utf_string_conversions.h"
[email protected]a7eb0182011-12-21 12:33:319#include "ui/aura/client/tooltip_client.h"
[email protected]1aad3322012-06-06 06:37:0910#include "ui/aura/env.h"
[email protected]a7eb0182011-12-21 12:33:3111#include "ui/aura/root_window.h"
[email protected]68a8e062012-01-26 01:39:4912#include "ui/aura/test/event_generator.h"
[email protected]a7eb0182011-12-21 12:33:3113#include "ui/aura/window.h"
[email protected]1eea6142012-03-22 09:30:0114#include "ui/base/resource/resource_bundle.h"
[email protected]1eea6142012-03-22 09:30:0115#include "ui/gfx/font.h"
[email protected]a7eb0182011-12-21 12:33:3116#include "ui/gfx/point.h"
[email protected]a1b7a822013-02-23 19:08:0417#include "ui/views/corewm/tooltip_controller.h"
18#include "ui/views/corewm/tooltip_controller_test_helper.h"
[email protected]a7eb0182011-12-21 12:33:3119#include "ui/views/view.h"
20#include "ui/views/widget/widget.h"
21
[email protected]a1b7a822013-02-23 19:08:0422using views::corewm::TooltipController;
23using views::corewm::test::TooltipTestView;
24using 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]55f593352011-12-24 05:42:4630namespace ash {
[email protected]a7eb0182011-12-21 12:33:3131namespace test {
32
33namespace {
34
[email protected]5ebe6102012-11-28 21:00:0335views::Widget* CreateNewWidgetWithBoundsOn(int display,
36 const gfx::Rect& bounds) {
[email protected]a7eb0182011-12-21 12:33:3137 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]5ebe6102012-11-28 21:00:0342 params.context = Shell::GetAllRootWindows().at(display);
[email protected]a7eb0182011-12-21 12:33:3143 params.child = true;
[email protected]a4f3d052012-08-08 02:37:2044 params.bounds = bounds;
[email protected]a7eb0182011-12-21 12:33:3145 widget->Init(params);
46 widget->Show();
47 return widget;
48}
49
[email protected]5ebe6102012-11-28 21:00:0350views::Widget* CreateNewWidgetOn(int display) {
51 return CreateNewWidgetWithBoundsOn(display, gfx::Rect());
[email protected]a4f3d052012-08-08 02:37:2052}
53
[email protected]a7eb0182011-12-21 12:33:3154void 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]d4030502012-10-23 16:51:4764 contents_view_bounds.Union(view->bounds());
[email protected]a7eb0182011-12-21 12:33:3165 contents_view->SetBoundsRect(contents_view_bounds);
[email protected]a4f3d052012-08-08 02:37:2066 widget->SetBounds(gfx::Rect(widget->GetWindowBoundsInScreen().origin(),
67 contents_view_bounds.size()));
[email protected]a7eb0182011-12-21 12:33:3168}
69
[email protected]a1b7a822013-02-23 19:08:0470TooltipController* GetController() {
71 return static_cast<TooltipController*>(
[email protected]42713f72012-05-25 00:41:5072 aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow()));
[email protected]a7eb0182011-12-21 12:33:3173}
74
[email protected]1eea6142012-03-22 09:30:0175gfx::Font GetDefaultFont() {
76 return ui::ResourceBundle::GetSharedInstance().GetFont(
77 ui::ResourceBundle::BaseFont);
78}
79
[email protected]a7eb0182011-12-21 12:33:3180} // namespace
81
[email protected]1dd1c1b2012-02-17 22:04:4782class TooltipControllerTest : public AshTestBase {
[email protected]a7eb0182011-12-21 12:33:3183 public:
84 TooltipControllerTest() {}
85 virtual ~TooltipControllerTest() {}
86
[email protected]a1b7a822013-02-23 19:08:0487 virtual void SetUp() OVERRIDE {
88 AshTestBase::SetUp();
89 helper_.reset(new TooltipControllerTestHelper(GetController()));
[email protected]a7eb0182011-12-21 12:33:3190 }
91
[email protected]a1b7a822013-02-23 19:08:0492 protected:
93 scoped_ptr<TooltipControllerTestHelper> helper_;
[email protected]1eea6142012-03-22 09:30:0194
[email protected]a7eb0182011-12-21 12:33:3195 private:
96 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest);
97};
98
99TEST_F(TooltipControllerTest, NonNullTooltipClient) {
[email protected]42713f72012-05-25 00:41:50100 EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow())
101 != NULL);
[email protected]ed95e022013-04-11 04:03:32102 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
[email protected]a1b7a822013-02-23 19:08:04103 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
104 EXPECT_FALSE(helper_->IsTooltipVisible());
[email protected]5139f1e2012-01-18 23:58:02105}
106
[email protected]3341c652012-04-24 02:02:28107TEST_F(TooltipControllerTest, HideTooltipWhenCursorHidden) {
[email protected]5ebe6102012-11-28 21:00:03108 scoped_ptr<views::Widget> widget(CreateNewWidgetOn(0));
[email protected]3341c652012-04-24 02:02:28109 TooltipTestView* view = new TooltipTestView;
110 AddViewToWidgetAndResize(widget.get(), view);
111 view->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
[email protected]ed95e022013-04-11 04:03:32112 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
[email protected]a1b7a822013-02-23 19:08:04113 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
[email protected]3341c652012-04-24 02:02:28114
[email protected]42713f72012-05-25 00:41:50115 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
[email protected]3341c652012-04-24 02:02:28116 generator.MoveMouseRelativeTo(widget->GetNativeView(),
117 view->bounds().CenterPoint());
[email protected]ed95e022013-04-11 04:03:32118 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
[email protected]3341c652012-04-24 02:02:28119
120 // Fire tooltip timer so tooltip becomes visible.
[email protected]a1b7a822013-02-23 19:08:04121 helper_->FireTooltipTimer();
122 EXPECT_TRUE(helper_->IsTooltipVisible());
[email protected]3341c652012-04-24 02:02:28123
124 // Hide the cursor and check again.
[email protected]166ccde2012-12-19 16:43:53125 ash::Shell::GetInstance()->cursor_manager()->DisableMouseEvents();
[email protected]a1b7a822013-02-23 19:08:04126 helper_->FireTooltipTimer();
127 EXPECT_FALSE(helper_->IsTooltipVisible());
[email protected]3341c652012-04-24 02:02:28128
129 // Show the cursor and re-check.
[email protected]166ccde2012-12-19 16:43:53130 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
[email protected]a1b7a822013-02-23 19:08:04131 helper_->FireTooltipTimer();
132 EXPECT_TRUE(helper_->IsTooltipVisible());
[email protected]7109ce12012-07-24 01:20:33133}
134
[email protected]1c3f7002013-01-21 18:46:05135#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
144TEST_F(TooltipControllerTest, MAYBE_TooltipsOnMultiDisplayShouldNotCrash) {
[email protected]a4f3d052012-08-08 02:37:20145 UpdateDisplay("1000x600,600x400");
146 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
[email protected]5ebe6102012-11-28 21:00:03147 scoped_ptr<views::Widget> widget1(CreateNewWidgetWithBoundsOn(
148 0, gfx::Rect(10, 10, 100, 100)));
[email protected]a4f3d052012-08-08 02:37:20149 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]5ebe6102012-11-28 21:00:03154 scoped_ptr<views::Widget> widget2(CreateNewWidgetWithBoundsOn(
155 1, gfx::Rect(1200, 10, 100, 100)));
[email protected]a4f3d052012-08-08 02:37:20156 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]a1b7a822013-02-23 19:08:04165 helper_->FireTooltipTimer();
166 EXPECT_TRUE(helper_->IsTooltipVisible());
[email protected]a4f3d052012-08-08 02:37:20167
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]dbc7b182012-11-02 18:18:16171#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]a1b7a822013-02-23 19:08:04175 EXPECT_FALSE(helper_->IsTooltipVisible());
[email protected]dbc7b182012-11-02 18:18:16176#endif
[email protected]a4f3d052012-08-08 02:37:20177 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]a1b7a822013-02-23 19:08:04184 helper_->FireTooltipTimer();
185 EXPECT_TRUE(helper_->IsTooltipVisible());
[email protected]a4f3d052012-08-08 02:37:20186}
187
[email protected]a7eb0182011-12-21 12:33:31188} // namespace test
[email protected]55f593352011-12-24 05:42:46189} // namespace ash