Report errors from ChromiumEnv::GetChildren in Posix.
[chromium-blink-merge.git] / ash / tooltips / tooltip_controller_unittest.cc
blob2b510a978aebf88451d057ccc53edfb4fcdcccfc
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/shell.h"
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
27 // to be installed.
29 namespace ash {
30 namespace test {
32 namespace {
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.child = true;
43 params.bounds = bounds;
44 widget->Init(params);
45 widget->Show();
46 return widget;
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()));
74 gfx::Font GetDefaultFont() {
75 return ui::ResourceBundle::GetSharedInstance().GetFont(
76 ui::ResourceBundle::BaseFont);
79 } // namespace
81 class TooltipControllerTest : public AshTestBase {
82 public:
83 TooltipControllerTest() {}
84 virtual ~TooltipControllerTest() {}
86 virtual void SetUp() OVERRIDE {
87 AshTestBase::SetUp();
88 helper_.reset(new TooltipControllerTestHelper(GetController()));
91 protected:
92 scoped_ptr<TooltipControllerTestHelper> helper_;
94 private:
95 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest);
98 TEST_F(TooltipControllerTest, NonNullTooltipClient) {
99 EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow())
100 != NULL);
101 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
102 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
103 EXPECT_FALSE(helper_->IsTooltipVisible());
106 TEST_F(TooltipControllerTest, HideTooltipWhenCursorHidden) {
107 scoped_ptr<views::Widget> widget(CreateNewWidgetOn(0));
108 TooltipTestView* view = new TooltipTestView;
109 AddViewToWidgetAndResize(widget.get(), view);
110 view->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
111 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
112 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
114 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
115 generator.MoveMouseRelativeTo(widget->GetNativeView(),
116 view->bounds().CenterPoint());
117 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
119 // Fire tooltip timer so tooltip becomes visible.
120 helper_->FireTooltipTimer();
121 EXPECT_TRUE(helper_->IsTooltipVisible());
123 // Hide the cursor and check again.
124 ash::Shell::GetInstance()->cursor_manager()->DisableMouseEvents();
125 helper_->FireTooltipTimer();
126 EXPECT_FALSE(helper_->IsTooltipVisible());
128 // Show the cursor and re-check.
129 RunAllPendingInMessageLoop();
130 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
131 RunAllPendingInMessageLoop();
132 helper_->FireTooltipTimer();
133 EXPECT_TRUE(helper_->IsTooltipVisible());
136 TEST_F(TooltipControllerTest, TooltipsOnMultiDisplayShouldNotCrash) {
137 if (!SupportsMultipleDisplays())
138 return;
140 UpdateDisplay("1000x600,600x400");
141 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
142 scoped_ptr<views::Widget> widget1(CreateNewWidgetWithBoundsOn(
143 0, gfx::Rect(10, 10, 100, 100)));
144 TooltipTestView* view1 = new TooltipTestView;
145 AddViewToWidgetAndResize(widget1.get(), view1);
146 view1->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 1"));
147 EXPECT_EQ(widget1->GetNativeView()->GetRootWindow(), root_windows[0]);
149 scoped_ptr<views::Widget> widget2(CreateNewWidgetWithBoundsOn(
150 1, gfx::Rect(1200, 10, 100, 100)));
151 TooltipTestView* view2 = new TooltipTestView;
152 AddViewToWidgetAndResize(widget2.get(), view2);
153 view2->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 2"));
154 EXPECT_EQ(widget2->GetNativeView()->GetRootWindow(), root_windows[1]);
156 // Show tooltip on second display.
157 aura::test::EventGenerator generator(root_windows[1]);
158 generator.MoveMouseRelativeTo(widget2->GetNativeView(),
159 view2->bounds().CenterPoint());
160 helper_->FireTooltipTimer();
161 EXPECT_TRUE(helper_->IsTooltipVisible());
163 // Get rid of secondary display. This destroy's the tooltip's aura window. If
164 // we have handled this case, we will not crash in the following statement.
165 UpdateDisplay("1000x600");
166 #if !defined(OS_WIN)
167 // TODO(cpu): Detangle the window destruction notification. Currently
168 // the TooltipController::OnWindowDestroyed is not being called then the
169 // display is torn down so the tooltip is is still there.
170 EXPECT_FALSE(helper_->IsTooltipVisible());
171 #endif
172 EXPECT_EQ(widget2->GetNativeView()->GetRootWindow(), root_windows[0]);
174 // The tooltip should create a new aura window for itself, so we should still
175 // be able to show tooltips on the primary display.
176 aura::test::EventGenerator generator1(root_windows[0]);
177 generator1.MoveMouseRelativeTo(widget1->GetNativeView(),
178 view1->bounds().CenterPoint());
179 helper_->FireTooltipTimer();
180 EXPECT_TRUE(helper_->IsTooltipVisible());
183 } // namespace test
184 } // namespace ash