blob: 824018568207a8a2fb79f856d5e416a116a58fca [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]e73bd7802012-02-17 20:10:345#include "ash/shell.h"
[email protected]1dd1c1b2012-02-17 22:04:476#include "ash/test/ash_test_base.h"
[email protected]a4ea1f12013-06-07 18:37:077#include "base/strings/utf_string_conversions.h"
[email protected]1aad3322012-06-06 06:37:098#include "ui/aura/env.h"
[email protected]a7eb0182011-12-21 12:33:319#include "ui/aura/window.h"
[email protected]fcc51c952014-02-21 21:31:2610#include "ui/aura/window_event_dispatcher.h"
[email protected]73c9fd02014-07-28 01:48:5211#include "ui/events/test/event_generator.h"
[email protected]1eea6142012-03-22 09:30:0112#include "ui/gfx/font.h"
[email protected]a7eb0182011-12-21 12:33:3113#include "ui/gfx/point.h"
[email protected]a1b7a822013-02-23 19:08:0414#include "ui/views/corewm/tooltip_controller.h"
15#include "ui/views/corewm/tooltip_controller_test_helper.h"
[email protected]a7eb0182011-12-21 12:33:3116#include "ui/views/view.h"
17#include "ui/views/widget/widget.h"
[email protected]af4552b22014-03-21 19:45:0118#include "ui/wm/public/tooltip_client.h"
[email protected]a7eb0182011-12-21 12:33:3119
[email protected]a1b7a822013-02-23 19:08:0420using views::corewm::TooltipController;
21using views::corewm::test::TooltipTestView;
22using 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]55f593352011-12-24 05:42:4628namespace ash {
[email protected]a7eb0182011-12-21 12:33:3129namespace test {
30
31namespace {
32
[email protected]5ebe6102012-11-28 21:00:0333views::Widget* CreateNewWidgetWithBoundsOn(int display,
34 const gfx::Rect& bounds) {
[email protected]a7eb0182011-12-21 12:33:3135 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]5ebe6102012-11-28 21:00:0340 params.context = Shell::GetAllRootWindows().at(display);
[email protected]a4f3d052012-08-08 02:37:2041 params.bounds = bounds;
[email protected]a7eb0182011-12-21 12:33:3142 widget->Init(params);
43 widget->Show();
44 return widget;
45}
46
[email protected]5ebe6102012-11-28 21:00:0347views::Widget* CreateNewWidgetOn(int display) {
48 return CreateNewWidgetWithBoundsOn(display, gfx::Rect());
[email protected]a4f3d052012-08-08 02:37:2049}
50
[email protected]a7eb0182011-12-21 12:33:3151void 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]d4030502012-10-23 16:51:4761 contents_view_bounds.Union(view->bounds());
[email protected]a7eb0182011-12-21 12:33:3162 contents_view->SetBoundsRect(contents_view_bounds);
[email protected]a4f3d052012-08-08 02:37:2063 widget->SetBounds(gfx::Rect(widget->GetWindowBoundsInScreen().origin(),
64 contents_view_bounds.size()));
[email protected]a7eb0182011-12-21 12:33:3165}
66
[email protected]a1b7a822013-02-23 19:08:0467TooltipController* GetController() {
68 return static_cast<TooltipController*>(
[email protected]42713f72012-05-25 00:41:5069 aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow()));
[email protected]a7eb0182011-12-21 12:33:3170}
71
[email protected]a7eb0182011-12-21 12:33:3172} // namespace
73
[email protected]1dd1c1b2012-02-17 22:04:4774class TooltipControllerTest : public AshTestBase {
[email protected]a7eb0182011-12-21 12:33:3175 public:
76 TooltipControllerTest() {}
dcheng1f4538e2014-10-27 23:57:0577 ~TooltipControllerTest() override {}
[email protected]a7eb0182011-12-21 12:33:3178
dcheng1f4538e2014-10-27 23:57:0579 void SetUp() override {
[email protected]a1b7a822013-02-23 19:08:0480 AshTestBase::SetUp();
81 helper_.reset(new TooltipControllerTestHelper(GetController()));
[email protected]a7eb0182011-12-21 12:33:3182 }
83
[email protected]a1b7a822013-02-23 19:08:0484 protected:
85 scoped_ptr<TooltipControllerTestHelper> helper_;
[email protected]1eea6142012-03-22 09:30:0186
[email protected]a7eb0182011-12-21 12:33:3187 private:
88 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest);
89};
90
91TEST_F(TooltipControllerTest, NonNullTooltipClient) {
[email protected]42713f72012-05-25 00:41:5092 EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow())
93 != NULL);
[email protected]ed95e022013-04-11 04:03:3294 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
[email protected]a1b7a822013-02-23 19:08:0495 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
96 EXPECT_FALSE(helper_->IsTooltipVisible());
[email protected]5139f1e2012-01-18 23:58:0297}
98
[email protected]3341c652012-04-24 02:02:2899TEST_F(TooltipControllerTest, HideTooltipWhenCursorHidden) {
[email protected]5ebe6102012-11-28 21:00:03100 scoped_ptr<views::Widget> widget(CreateNewWidgetOn(0));
[email protected]3341c652012-04-24 02:02:28101 TooltipTestView* view = new TooltipTestView;
102 AddViewToWidgetAndResize(widget.get(), view);
[email protected]b57c9d52013-12-24 16:23:25103 view->set_tooltip_text(base::ASCIIToUTF16("Tooltip Text"));
[email protected]ed95e022013-04-11 04:03:32104 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
[email protected]a1b7a822013-02-23 19:08:04105 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
[email protected]3341c652012-04-24 02:02:28106
[email protected]73c9fd02014-07-28 01:48:52107 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
[email protected]3341c652012-04-24 02:02:28108 generator.MoveMouseRelativeTo(widget->GetNativeView(),
109 view->bounds().CenterPoint());
[email protected]b57c9d52013-12-24 16:23:25110 base::string16 expected_tooltip = base::ASCIIToUTF16("Tooltip Text");
[email protected]3341c652012-04-24 02:02:28111
112 // Fire tooltip timer so tooltip becomes visible.
[email protected]a1b7a822013-02-23 19:08:04113 helper_->FireTooltipTimer();
114 EXPECT_TRUE(helper_->IsTooltipVisible());
[email protected]3341c652012-04-24 02:02:28115
116 // Hide the cursor and check again.
[email protected]166ccde2012-12-19 16:43:53117 ash::Shell::GetInstance()->cursor_manager()->DisableMouseEvents();
[email protected]a1b7a822013-02-23 19:08:04118 helper_->FireTooltipTimer();
119 EXPECT_FALSE(helper_->IsTooltipVisible());
[email protected]3341c652012-04-24 02:02:28120
121 // Show the cursor and re-check.
[email protected]450edc672013-09-20 15:29:59122 RunAllPendingInMessageLoop();
[email protected]166ccde2012-12-19 16:43:53123 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
[email protected]450edc672013-09-20 15:29:59124 RunAllPendingInMessageLoop();
[email protected]a1b7a822013-02-23 19:08:04125 helper_->FireTooltipTimer();
126 EXPECT_TRUE(helper_->IsTooltipVisible());
[email protected]7109ce12012-07-24 01:20:33127}
128
[email protected]e75642a2013-06-12 17:21:18129TEST_F(TooltipControllerTest, TooltipsOnMultiDisplayShouldNotCrash) {
130 if (!SupportsMultipleDisplays())
131 return;
[email protected]1c3f7002013-01-21 18:46:05132
[email protected]a4f3d052012-08-08 02:37:20133 UpdateDisplay("1000x600,600x400");
[email protected]c9390bd2013-11-08 20:33:13134 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
[email protected]5ebe6102012-11-28 21:00:03135 scoped_ptr<views::Widget> widget1(CreateNewWidgetWithBoundsOn(
136 0, gfx::Rect(10, 10, 100, 100)));
[email protected]a4f3d052012-08-08 02:37:20137 TooltipTestView* view1 = new TooltipTestView;
138 AddViewToWidgetAndResize(widget1.get(), view1);
[email protected]b57c9d52013-12-24 16:23:25139 view1->set_tooltip_text(base::ASCIIToUTF16("Tooltip Text for view 1"));
[email protected]a4f3d052012-08-08 02:37:20140 EXPECT_EQ(widget1->GetNativeView()->GetRootWindow(), root_windows[0]);
141
[email protected]5ebe6102012-11-28 21:00:03142 scoped_ptr<views::Widget> widget2(CreateNewWidgetWithBoundsOn(
143 1, gfx::Rect(1200, 10, 100, 100)));
[email protected]a4f3d052012-08-08 02:37:20144 TooltipTestView* view2 = new TooltipTestView;
145 AddViewToWidgetAndResize(widget2.get(), view2);
[email protected]b57c9d52013-12-24 16:23:25146 view2->set_tooltip_text(base::ASCIIToUTF16("Tooltip Text for view 2"));
[email protected]a4f3d052012-08-08 02:37:20147 EXPECT_EQ(widget2->GetNativeView()->GetRootWindow(), root_windows[1]);
148
149 // Show tooltip on second display.
[email protected]73c9fd02014-07-28 01:48:52150 ui::test::EventGenerator generator(root_windows[1]);
[email protected]a4f3d052012-08-08 02:37:20151 generator.MoveMouseRelativeTo(widget2->GetNativeView(),
152 view2->bounds().CenterPoint());
[email protected]a1b7a822013-02-23 19:08:04153 helper_->FireTooltipTimer();
154 EXPECT_TRUE(helper_->IsTooltipVisible());
[email protected]a4f3d052012-08-08 02:37:20155
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]dbc7b182012-11-02 18:18:16159#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]a1b7a822013-02-23 19:08:04163 EXPECT_FALSE(helper_->IsTooltipVisible());
[email protected]dbc7b182012-11-02 18:18:16164#endif
[email protected]a4f3d052012-08-08 02:37:20165 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]73c9fd02014-07-28 01:48:52169 ui::test::EventGenerator generator1(root_windows[0]);
[email protected]a4f3d052012-08-08 02:37:20170 generator1.MoveMouseRelativeTo(widget1->GetNativeView(),
171 view1->bounds().CenterPoint());
[email protected]a1b7a822013-02-23 19:08:04172 helper_->FireTooltipTimer();
173 EXPECT_TRUE(helper_->IsTooltipVisible());
[email protected]a4f3d052012-08-08 02:37:20174}
175
[email protected]a7eb0182011-12-21 12:33:31176} // namespace test
[email protected]55f593352011-12-24 05:42:46177} // namespace ash