Sort unlaunched apps on app list start page by apps grid order.
[chromium-blink-merge.git] / ui / views / corewm / tooltip_controller_unittest.cc
blobad341d965294a097eae95e0417988c549ff3295c
1 // Copyright (c) 2013 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 "ui/views/corewm/tooltip_controller.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/aura/client/cursor_client.h"
9 #include "ui/aura/client/screen_position_client.h"
10 #include "ui/aura/env.h"
11 #include "ui/aura/test/aura_test_base.h"
12 #include "ui/aura/test/test_screen.h"
13 #include "ui/aura/test/test_window_delegate.h"
14 #include "ui/aura/window.h"
15 #include "ui/aura/window_event_dispatcher.h"
16 #include "ui/events/test/event_generator.h"
17 #include "ui/gfx/font.h"
18 #include "ui/gfx/geometry/point.h"
19 #include "ui/gfx/screen.h"
20 #include "ui/gfx/screen_type_delegate.h"
21 #include "ui/gfx/text_elider.h"
22 #include "ui/views/corewm/tooltip_aura.h"
23 #include "ui/views/corewm/tooltip_controller_test_helper.h"
24 #include "ui/views/test/desktop_test_views_delegate.h"
25 #include "ui/views/test/test_views_delegate.h"
26 #include "ui/views/view.h"
27 #include "ui/views/widget/tooltip_manager.h"
28 #include "ui/views/widget/widget.h"
29 #include "ui/wm/core/default_activation_client.h"
30 #include "ui/wm/core/default_screen_position_client.h"
31 #include "ui/wm/core/wm_state.h"
32 #include "ui/wm/public/tooltip_client.h"
33 #include "ui/wm/public/window_types.h"
35 #if defined(OS_WIN)
36 #include "ui/base/win/scoped_ole_initializer.h"
37 #endif
39 #if !defined(OS_CHROMEOS)
40 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
41 #include "ui/views/widget/desktop_aura/desktop_screen.h"
42 #endif
44 using base::ASCIIToUTF16;
46 namespace views {
47 namespace corewm {
48 namespace test {
49 namespace {
51 views::Widget* CreateWidget(aura::Window* root) {
52 views::Widget* widget = new views::Widget;
53 views::Widget::InitParams params;
54 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
55 params.accept_events = true;
56 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
57 #if defined(OS_CHROMEOS)
58 params.parent = root;
59 #else
60 params.native_widget = new DesktopNativeWidgetAura(widget);
61 #endif
62 params.bounds = gfx::Rect(0, 0, 200, 100);
63 widget->Init(params);
64 widget->Show();
65 return widget;
68 TooltipController* GetController(Widget* widget) {
69 return static_cast<TooltipController*>(
70 aura::client::GetTooltipClient(
71 widget->GetNativeWindow()->GetRootWindow()));
74 } // namespace
76 class TooltipControllerTest : public aura::test::AuraTestBase {
77 public:
78 TooltipControllerTest() : view_(NULL) {}
79 ~TooltipControllerTest() override {}
81 void SetUp() override {
82 #if defined(OS_CHROMEOS)
83 views_delegate_.reset(new TestViewsDelegate);
84 #else
85 views_delegate_.reset(new DesktopTestViewsDelegate);
86 #endif
88 aura::test::AuraTestBase::SetUp();
89 new wm::DefaultActivationClient(root_window());
90 #if defined(OS_CHROMEOS)
91 controller_.reset(new TooltipController(
92 scoped_ptr<views::corewm::Tooltip>(
93 new views::corewm::TooltipAura)));
94 root_window()->AddPreTargetHandler(controller_.get());
95 SetTooltipClient(root_window(), controller_.get());
96 #endif
97 widget_.reset(CreateWidget(root_window()));
98 widget_->SetContentsView(new View);
99 view_ = new TooltipTestView;
100 widget_->GetContentsView()->AddChildView(view_);
101 view_->SetBoundsRect(widget_->GetContentsView()->GetLocalBounds());
102 helper_.reset(new TooltipControllerTestHelper(
103 GetController(widget_.get())));
104 generator_.reset(new ui::test::EventGenerator(GetRootWindow()));
107 void TearDown() override {
108 #if defined(OS_CHROMEOS)
109 root_window()->RemovePreTargetHandler(controller_.get());
110 aura::client::SetTooltipClient(root_window(), NULL);
111 controller_.reset();
112 #endif
113 generator_.reset();
114 helper_.reset();
115 widget_.reset();
116 aura::test::AuraTestBase::TearDown();
117 views_delegate_.reset();
120 protected:
121 aura::Window* GetWindow() {
122 return widget_->GetNativeWindow();
125 aura::Window* GetRootWindow() {
126 return GetWindow()->GetRootWindow();
129 TooltipTestView* PrepareSecondView() {
130 TooltipTestView* view2 = new TooltipTestView;
131 widget_->GetContentsView()->AddChildView(view2);
132 view_->SetBounds(0, 0, 100, 100);
133 view2->SetBounds(100, 0, 100, 100);
134 return view2;
137 scoped_ptr<views::Widget> widget_;
138 TooltipTestView* view_;
139 scoped_ptr<TooltipControllerTestHelper> helper_;
140 scoped_ptr<ui::test::EventGenerator> generator_;
142 private:
143 scoped_ptr<TooltipController> controller_;
145 scoped_ptr<views::TestViewsDelegate> views_delegate_;
147 #if defined(OS_WIN)
148 ui::ScopedOleInitializer ole_initializer_;
149 #endif
151 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest);
154 TEST_F(TooltipControllerTest, ViewTooltip) {
155 view_->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
156 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
157 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
158 generator_->MoveMouseToCenterOf(GetWindow());
160 EXPECT_EQ(GetWindow(), GetRootWindow()->GetEventHandlerForPoint(
161 generator_->current_location()));
162 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
163 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(GetWindow()));
164 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
165 EXPECT_EQ(GetWindow(), helper_->GetTooltipWindow());
167 // Fire tooltip timer so tooltip becomes visible.
168 helper_->FireTooltipTimer();
170 EXPECT_TRUE(helper_->IsTooltipVisible());
171 generator_->MoveMouseBy(1, 0);
173 EXPECT_TRUE(helper_->IsTooltipVisible());
174 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(GetWindow()));
175 EXPECT_EQ(expected_tooltip, helper_->GetTooltipText());
176 EXPECT_EQ(GetWindow(), helper_->GetTooltipWindow());
179 TEST_F(TooltipControllerTest, TooltipsInMultipleViews) {
180 view_->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
181 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
182 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
184 PrepareSecondView();
185 aura::Window* window = GetWindow();
186 aura::Window* root_window = GetRootWindow();
188 // Fire tooltip timer so tooltip becomes visible.
189 generator_->MoveMouseRelativeTo(window, view_->bounds().CenterPoint());
190 helper_->FireTooltipTimer();
191 EXPECT_TRUE(helper_->IsTooltipVisible());
192 for (int i = 0; i < 49; ++i) {
193 generator_->MoveMouseBy(1, 0);
194 EXPECT_TRUE(helper_->IsTooltipVisible());
195 EXPECT_EQ(window, root_window->GetEventHandlerForPoint(
196 generator_->current_location()));
197 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
198 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
199 EXPECT_EQ(expected_tooltip, helper_->GetTooltipText());
200 EXPECT_EQ(window, helper_->GetTooltipWindow());
202 for (int i = 0; i < 49; ++i) {
203 generator_->MoveMouseBy(1, 0);
204 EXPECT_FALSE(helper_->IsTooltipVisible());
205 EXPECT_EQ(window, root_window->GetEventHandlerForPoint(
206 generator_->current_location()));
207 base::string16 expected_tooltip; // = ""
208 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
209 EXPECT_EQ(expected_tooltip, helper_->GetTooltipText());
210 EXPECT_EQ(window, helper_->GetTooltipWindow());
214 TEST_F(TooltipControllerTest, EnableOrDisableTooltips) {
215 view_->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
216 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
217 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
219 generator_->MoveMouseRelativeTo(GetWindow(), view_->bounds().CenterPoint());
220 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
222 // Fire tooltip timer so tooltip becomes visible.
223 helper_->FireTooltipTimer();
224 EXPECT_TRUE(helper_->IsTooltipVisible());
226 // Disable tooltips and check again.
227 helper_->controller()->SetTooltipsEnabled(false);
228 EXPECT_FALSE(helper_->IsTooltipVisible());
229 helper_->FireTooltipTimer();
230 EXPECT_FALSE(helper_->IsTooltipVisible());
232 // Enable tooltips back and check again.
233 helper_->controller()->SetTooltipsEnabled(true);
234 EXPECT_FALSE(helper_->IsTooltipVisible());
235 helper_->FireTooltipTimer();
236 EXPECT_TRUE(helper_->IsTooltipVisible());
239 // Verifies tooltip isn't shown if tooltip text consists entirely of whitespace.
240 TEST_F(TooltipControllerTest, DontShowEmptyTooltips) {
241 view_->set_tooltip_text(ASCIIToUTF16(" "));
242 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
243 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
245 generator_->MoveMouseRelativeTo(GetWindow(), view_->bounds().CenterPoint());
247 helper_->FireTooltipTimer();
248 EXPECT_FALSE(helper_->IsTooltipVisible());
251 TEST_F(TooltipControllerTest, TooltipHidesOnKeyPressAndStaysHiddenUntilChange) {
252 view_->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 1"));
253 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
254 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
256 TooltipTestView* view2 = PrepareSecondView();
257 view2->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 2"));
259 aura::Window* window = GetWindow();
261 // Fire tooltip timer so tooltip becomes visible.
262 generator_->MoveMouseRelativeTo(window, view_->bounds().CenterPoint());
263 helper_->FireTooltipTimer();
264 EXPECT_TRUE(helper_->IsTooltipVisible());
265 EXPECT_TRUE(helper_->IsTooltipShownTimerRunning());
267 generator_->PressKey(ui::VKEY_1, 0);
268 EXPECT_FALSE(helper_->IsTooltipVisible());
269 EXPECT_FALSE(helper_->IsTooltipTimerRunning());
270 EXPECT_FALSE(helper_->IsTooltipShownTimerRunning());
272 // Moving the mouse inside |view1| should not change the state of the tooltip
273 // or the timers.
274 for (int i = 0; i < 49; i++) {
275 generator_->MoveMouseBy(1, 0);
276 EXPECT_FALSE(helper_->IsTooltipVisible());
277 EXPECT_FALSE(helper_->IsTooltipTimerRunning());
278 EXPECT_FALSE(helper_->IsTooltipShownTimerRunning());
279 EXPECT_EQ(window,
280 GetRootWindow()->GetEventHandlerForPoint(
281 generator_->current_location()));
282 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 1");
283 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
284 EXPECT_EQ(expected_tooltip, helper_->GetTooltipText());
285 EXPECT_EQ(window, helper_->GetTooltipWindow());
288 // Now we move the mouse on to |view2|. It should re-start the tooltip timer.
289 generator_->MoveMouseBy(1, 0);
290 EXPECT_TRUE(helper_->IsTooltipTimerRunning());
291 helper_->FireTooltipTimer();
292 EXPECT_TRUE(helper_->IsTooltipVisible());
293 EXPECT_TRUE(helper_->IsTooltipShownTimerRunning());
294 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 2");
295 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
296 EXPECT_EQ(expected_tooltip, helper_->GetTooltipText());
297 EXPECT_EQ(window, helper_->GetTooltipWindow());
300 TEST_F(TooltipControllerTest, TooltipHidesOnTimeoutAndStaysHiddenUntilChange) {
301 view_->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 1"));
302 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
303 EXPECT_EQ(NULL, helper_->GetTooltipWindow());
305 TooltipTestView* view2 = PrepareSecondView();
306 view2->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 2"));
308 aura::Window* window = GetWindow();
310 // Fire tooltip timer so tooltip becomes visible.
311 generator_->MoveMouseRelativeTo(window, view_->bounds().CenterPoint());
312 helper_->FireTooltipTimer();
313 EXPECT_TRUE(helper_->IsTooltipVisible());
314 EXPECT_TRUE(helper_->IsTooltipShownTimerRunning());
316 helper_->FireTooltipShownTimer();
317 EXPECT_FALSE(helper_->IsTooltipVisible());
318 EXPECT_FALSE(helper_->IsTooltipTimerRunning());
319 EXPECT_FALSE(helper_->IsTooltipShownTimerRunning());
321 // Moving the mouse inside |view1| should not change the state of the tooltip
322 // or the timers.
323 for (int i = 0; i < 49; ++i) {
324 generator_->MoveMouseBy(1, 0);
325 EXPECT_FALSE(helper_->IsTooltipVisible());
326 EXPECT_FALSE(helper_->IsTooltipTimerRunning());
327 EXPECT_FALSE(helper_->IsTooltipShownTimerRunning());
328 EXPECT_EQ(window, GetRootWindow()->GetEventHandlerForPoint(
329 generator_->current_location()));
330 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 1");
331 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
332 EXPECT_EQ(expected_tooltip, helper_->GetTooltipText());
333 EXPECT_EQ(window, helper_->GetTooltipWindow());
336 // Now we move the mouse on to |view2|. It should re-start the tooltip timer.
337 generator_->MoveMouseBy(1, 0);
338 EXPECT_TRUE(helper_->IsTooltipTimerRunning());
339 helper_->FireTooltipTimer();
340 EXPECT_TRUE(helper_->IsTooltipVisible());
341 EXPECT_TRUE(helper_->IsTooltipShownTimerRunning());
342 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 2");
343 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
344 EXPECT_EQ(expected_tooltip, helper_->GetTooltipText());
345 EXPECT_EQ(window, helper_->GetTooltipWindow());
348 // Verifies a mouse exit event hides the tooltips.
349 TEST_F(TooltipControllerTest, HideOnExit) {
350 view_->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
351 generator_->MoveMouseToCenterOf(GetWindow());
352 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
353 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(GetWindow()));
354 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
355 EXPECT_EQ(GetWindow(), helper_->GetTooltipWindow());
357 // Fire tooltip timer so tooltip becomes visible.
358 helper_->FireTooltipTimer();
360 EXPECT_TRUE(helper_->IsTooltipVisible());
361 generator_->SendMouseExit();
362 EXPECT_FALSE(helper_->IsTooltipVisible());
365 TEST_F(TooltipControllerTest, ReshowOnClickAfterEnterExit) {
366 // Owned by |view_|.
367 TooltipTestView* v1 = new TooltipTestView;
368 TooltipTestView* v2 = new TooltipTestView;
369 view_->AddChildView(v1);
370 view_->AddChildView(v2);
371 gfx::Rect view_bounds(view_->GetLocalBounds());
372 view_bounds.set_height(view_bounds.height() / 2);
373 v1->SetBoundsRect(view_bounds);
374 view_bounds.set_y(view_bounds.height());
375 v2->SetBoundsRect(view_bounds);
376 const base::string16 v1_tt(ASCIIToUTF16("v1"));
377 const base::string16 v2_tt(ASCIIToUTF16("v2"));
378 v1->set_tooltip_text(v1_tt);
379 v2->set_tooltip_text(v2_tt);
381 gfx::Point v1_point(1, 1);
382 View::ConvertPointToWidget(v1, &v1_point);
383 generator_->MoveMouseRelativeTo(GetWindow(), v1_point);
385 // Fire tooltip timer so tooltip becomes visible.
386 helper_->FireTooltipTimer();
387 EXPECT_TRUE(helper_->IsTooltipVisible());
388 EXPECT_EQ(v1_tt, helper_->GetTooltipText());
390 // Press the mouse, move to v2 and back to v1.
391 generator_->ClickLeftButton();
393 gfx::Point v2_point(1, 1);
394 View::ConvertPointToWidget(v2, &v2_point);
395 generator_->MoveMouseRelativeTo(GetWindow(), v2_point);
396 generator_->MoveMouseRelativeTo(GetWindow(), v1_point);
398 helper_->FireTooltipTimer();
399 EXPECT_TRUE(helper_->IsTooltipVisible());
400 EXPECT_EQ(v1_tt, helper_->GetTooltipText());
403 namespace {
405 // Returns the index of |window| in its parent's children.
406 int IndexInParent(const aura::Window* window) {
407 aura::Window::Windows::const_iterator i =
408 std::find(window->parent()->children().begin(),
409 window->parent()->children().end(),
410 window);
411 return i == window->parent()->children().end() ? -1 :
412 static_cast<int>(i - window->parent()->children().begin());
415 } // namespace
417 class TooltipControllerCaptureTest : public TooltipControllerTest {
418 public:
419 TooltipControllerCaptureTest() {}
420 ~TooltipControllerCaptureTest() override {}
422 void SetUp() override {
423 TooltipControllerTest::SetUp();
424 aura::client::SetScreenPositionClient(GetRootWindow(),
425 &screen_position_client_);
426 #if !defined(OS_CHROMEOS)
427 desktop_screen_.reset(CreateDesktopScreen());
428 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
429 desktop_screen_.get());
430 #endif
433 void TearDown() override {
434 #if !defined(OS_CHROMEOS)
435 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen());
436 desktop_screen_.reset();
437 #endif
438 aura::client::SetScreenPositionClient(GetRootWindow(), NULL);
439 TooltipControllerTest::TearDown();
442 private:
443 wm::DefaultScreenPositionClient screen_position_client_;
444 scoped_ptr<gfx::Screen> desktop_screen_;
446 DISALLOW_COPY_AND_ASSIGN(TooltipControllerCaptureTest);
449 // Verifies when capture is released the TooltipController resets state.
450 // Flaky on all builders. http://crbug.com/388268
451 TEST_F(TooltipControllerCaptureTest, DISABLED_CloseOnCaptureLost) {
452 view_->GetWidget()->SetCapture(view_);
453 RunAllPendingInMessageLoop();
454 view_->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
455 generator_->MoveMouseToCenterOf(GetWindow());
456 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
457 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(GetWindow()));
458 EXPECT_EQ(base::string16(), helper_->GetTooltipText());
459 EXPECT_EQ(GetWindow(), helper_->GetTooltipWindow());
461 // Fire tooltip timer so tooltip becomes visible.
462 helper_->FireTooltipTimer();
464 EXPECT_TRUE(helper_->IsTooltipVisible());
465 view_->GetWidget()->ReleaseCapture();
466 EXPECT_FALSE(helper_->IsTooltipVisible());
467 EXPECT_TRUE(helper_->GetTooltipWindow() == NULL);
470 // Disabled on linux as DesktopScreenX11::GetWindowAtScreenPoint() doesn't
471 // consider z-order.
472 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
473 #define MAYBE_Capture DISABLED_Capture
474 #else
475 #define MAYBE_Capture Capture
476 #endif
477 // Verifies the correct window is found for tooltips when there is a capture.
478 TEST_F(TooltipControllerCaptureTest, MAYBE_Capture) {
479 const base::string16 tooltip_text(ASCIIToUTF16("1"));
480 const base::string16 tooltip_text2(ASCIIToUTF16("2"));
482 widget_->SetBounds(gfx::Rect(0, 0, 200, 200));
483 view_->set_tooltip_text(tooltip_text);
485 scoped_ptr<views::Widget> widget2(CreateWidget(root_window()));
486 widget2->SetContentsView(new View);
487 TooltipTestView* view2 = new TooltipTestView;
488 widget2->GetContentsView()->AddChildView(view2);
489 view2->set_tooltip_text(tooltip_text2);
490 widget2->SetBounds(gfx::Rect(0, 0, 200, 200));
491 view2->SetBoundsRect(widget2->GetContentsView()->GetLocalBounds());
493 widget_->SetCapture(view_);
494 EXPECT_TRUE(widget_->HasCapture());
495 widget2->Show();
496 EXPECT_GE(IndexInParent(widget2->GetNativeWindow()),
497 IndexInParent(widget_->GetNativeWindow()));
499 generator_->MoveMouseRelativeTo(widget_->GetNativeWindow(),
500 view_->bounds().CenterPoint());
502 EXPECT_TRUE(helper_->IsTooltipTimerRunning());
503 helper_->FireTooltipTimer();
504 // Even though the mouse is over a window with a tooltip it shouldn't be
505 // picked up because the windows don't have the same value for
506 // |TooltipManager::kGroupingPropertyKey|.
507 EXPECT_TRUE(helper_->GetTooltipText().empty());
509 // Now make both the windows have same transient value for
510 // kGroupingPropertyKey. In this case the tooltip should be picked up from
511 // |widget2| (because the mouse is over it).
512 const int grouping_key = 1;
513 widget_->SetNativeWindowProperty(TooltipManager::kGroupingPropertyKey,
514 reinterpret_cast<void*>(grouping_key));
515 widget2->SetNativeWindowProperty(TooltipManager::kGroupingPropertyKey,
516 reinterpret_cast<void*>(grouping_key));
517 generator_->MoveMouseBy(1, 10);
518 EXPECT_TRUE(helper_->IsTooltipTimerRunning());
519 helper_->FireTooltipTimer();
520 EXPECT_EQ(tooltip_text2, helper_->GetTooltipText());
522 widget2.reset();
525 namespace {
527 class TestTooltip : public Tooltip {
528 public:
529 TestTooltip() : is_visible_(false) {}
530 ~TestTooltip() override {}
532 const base::string16& tooltip_text() const { return tooltip_text_; }
534 // Tooltip:
535 int GetMaxWidth(const gfx::Point& location,
536 aura::Window* context) const override {
537 return 100;
539 void SetText(aura::Window* window,
540 const base::string16& tooltip_text,
541 const gfx::Point& location) override {
542 tooltip_text_ = tooltip_text;
543 location_ = location;
545 void Show() override { is_visible_ = true; }
546 void Hide() override { is_visible_ = false; }
547 bool IsVisible() override { return is_visible_; }
548 const gfx::Point& location() { return location_; }
550 private:
551 bool is_visible_;
552 base::string16 tooltip_text_;
553 gfx::Point location_;
555 DISALLOW_COPY_AND_ASSIGN(TestTooltip);
558 } // namespace
560 // Use for tests that don't depend upon views.
561 class TooltipControllerTest2 : public aura::test::AuraTestBase {
562 public:
563 TooltipControllerTest2() : test_tooltip_(new TestTooltip) {}
564 ~TooltipControllerTest2() override {}
566 void SetUp() override {
567 wm_state_.reset(new wm::WMState);
568 aura::test::AuraTestBase::SetUp();
569 new wm::DefaultActivationClient(root_window());
570 controller_.reset(new TooltipController(
571 scoped_ptr<corewm::Tooltip>(test_tooltip_)));
572 root_window()->AddPreTargetHandler(controller_.get());
573 SetTooltipClient(root_window(), controller_.get());
574 helper_.reset(new TooltipControllerTestHelper(controller_.get()));
575 generator_.reset(new ui::test::EventGenerator(root_window()));
578 void TearDown() override {
579 root_window()->RemovePreTargetHandler(controller_.get());
580 aura::client::SetTooltipClient(root_window(), NULL);
581 controller_.reset();
582 generator_.reset();
583 helper_.reset();
584 aura::test::AuraTestBase::TearDown();
585 wm_state_.reset();
588 protected:
589 // Owned by |controller_|.
590 TestTooltip* test_tooltip_;
591 scoped_ptr<TooltipControllerTestHelper> helper_;
592 scoped_ptr<ui::test::EventGenerator> generator_;
594 private:
595 scoped_ptr<TooltipController> controller_;
596 scoped_ptr<wm::WMState> wm_state_;
598 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest2);
601 TEST_F(TooltipControllerTest2, VerifyLeadingTrailingWhitespaceStripped) {
602 aura::test::TestWindowDelegate test_delegate;
603 scoped_ptr<aura::Window> window(
604 CreateNormalWindow(100, root_window(), &test_delegate));
605 window->SetBounds(gfx::Rect(0, 0, 300, 300));
606 base::string16 tooltip_text(ASCIIToUTF16(" \nx "));
607 aura::client::SetTooltipText(window.get(), &tooltip_text);
608 generator_->MoveMouseToCenterOf(window.get());
609 helper_->FireTooltipTimer();
610 EXPECT_EQ(ASCIIToUTF16("x"), test_tooltip_->tooltip_text());
613 // Verifies that tooltip is hidden and tooltip window closed upon cancel mode.
614 TEST_F(TooltipControllerTest2, CloseOnCancelMode) {
615 aura::test::TestWindowDelegate test_delegate;
616 scoped_ptr<aura::Window> window(
617 CreateNormalWindow(100, root_window(), &test_delegate));
618 window->SetBounds(gfx::Rect(0, 0, 300, 300));
619 base::string16 tooltip_text(ASCIIToUTF16("Tooltip Text"));
620 aura::client::SetTooltipText(window.get(), &tooltip_text);
621 generator_->MoveMouseToCenterOf(window.get());
623 // Fire tooltip timer so tooltip becomes visible.
624 helper_->FireTooltipTimer();
625 EXPECT_TRUE(helper_->IsTooltipVisible());
627 // Send OnCancelMode event and verify that tooltip becomes invisible and
628 // the tooltip window is closed.
629 ui::CancelModeEvent event;
630 helper_->controller()->OnCancelMode(&event);
631 EXPECT_FALSE(helper_->IsTooltipVisible());
632 EXPECT_TRUE(helper_->GetTooltipWindow() == NULL);
635 // Use for tests that need both views and a TestTooltip.
636 class TooltipControllerTest3 : public aura::test::AuraTestBase {
637 public:
638 TooltipControllerTest3() : test_tooltip_(new TestTooltip) {}
639 ~TooltipControllerTest3() override {}
641 void SetUp() override {
642 wm_state_.reset(new wm::WMState);
643 aura::test::AuraTestBase::SetUp();
644 new wm::DefaultActivationClient(root_window());
646 widget_.reset(CreateWidget(root_window()));
647 widget_->SetContentsView(new View);
648 view_ = new TooltipTestView;
649 widget_->GetContentsView()->AddChildView(view_);
650 view_->SetBoundsRect(widget_->GetContentsView()->GetLocalBounds());
652 generator_.reset(new ui::test::EventGenerator(GetRootWindow()));
653 controller_.reset(new TooltipController(
654 scoped_ptr<views::corewm::Tooltip>(test_tooltip_)));
655 GetRootWindow()->RemovePreTargetHandler(
656 static_cast<TooltipController*>(aura::client::GetTooltipClient(
657 widget_->GetNativeWindow()->GetRootWindow())));
658 GetRootWindow()->AddPreTargetHandler(controller_.get());
659 helper_.reset(new TooltipControllerTestHelper(controller_.get()));
660 SetTooltipClient(GetRootWindow(), controller_.get());
663 void TearDown() override {
664 GetRootWindow()->RemovePreTargetHandler(controller_.get());
665 aura::client::SetTooltipClient(GetRootWindow(), NULL);
667 controller_.reset();
668 generator_.reset();
669 helper_.reset();
670 widget_.reset();
671 aura::test::AuraTestBase::TearDown();
672 wm_state_.reset();
675 aura::Window* GetWindow() { return widget_->GetNativeWindow(); }
677 protected:
678 // Owned by |controller_|.
679 TestTooltip* test_tooltip_;
680 scoped_ptr<TooltipControllerTestHelper> helper_;
681 scoped_ptr<ui::test::EventGenerator> generator_;
682 scoped_ptr<views::Widget> widget_;
683 TooltipTestView* view_;
685 private:
686 scoped_ptr<TooltipController> controller_;
687 scoped_ptr<wm::WMState> wm_state_;
689 #if defined(OS_WIN)
690 ui::ScopedOleInitializer ole_initializer_;
691 #endif
693 aura::Window* GetRootWindow() { return GetWindow()->GetRootWindow(); }
695 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest3);
698 TEST_F(TooltipControllerTest3, TooltipPositionChangesOnTwoViewsWithSameLabel) {
699 // Owned by |view_|.
700 // These two views have the same tooltip text
701 TooltipTestView* v1 = new TooltipTestView;
702 TooltipTestView* v2 = new TooltipTestView;
703 // v1_1 is a view inside v1 that has an identical tooltip text to that of v1
704 // and v2
705 TooltipTestView* v1_1 = new TooltipTestView;
706 // v2_1 is a view inside v2 that has an identical tooltip text to that of v1
707 // and v2
708 TooltipTestView* v2_1 = new TooltipTestView;
709 // v2_2 is a view inside v2 with the tooltip text different from all the
710 // others
711 TooltipTestView* v2_2 = new TooltipTestView;
713 // Setup all the views' relations
714 view_->AddChildView(v1);
715 view_->AddChildView(v2);
716 v1->AddChildView(v1_1);
717 v2->AddChildView(v2_1);
718 v2->AddChildView(v2_2);
719 const base::string16 reference_string(
720 base::ASCIIToUTF16("Identical Tooltip Text"));
721 const base::string16 alternative_string(
722 base::ASCIIToUTF16("Another Shrubbery"));
723 v1->set_tooltip_text(reference_string);
724 v2->set_tooltip_text(reference_string);
725 v1_1->set_tooltip_text(reference_string);
726 v2_1->set_tooltip_text(reference_string);
727 v2_2->set_tooltip_text(alternative_string);
729 // Set views' bounds
730 gfx::Rect view_bounds(view_->GetLocalBounds());
731 view_bounds.set_height(view_bounds.height() / 2);
732 v1->SetBoundsRect(view_bounds);
733 v1_1->SetBounds(0, 0, 3, 3);
734 view_bounds.set_y(view_bounds.height());
735 v2->SetBoundsRect(view_bounds);
736 v2_2->SetBounds(view_bounds.width() - 3, view_bounds.height() - 3, 3, 3);
737 v2_1->SetBounds(0, 0, 3, 3);
739 // Test whether a toolbar appears on v1
740 gfx::Point center = v1->bounds().CenterPoint();
741 generator_->MoveMouseRelativeTo(GetWindow(), center);
742 helper_->FireTooltipTimer();
743 EXPECT_TRUE(helper_->IsTooltipVisible());
744 EXPECT_EQ(reference_string, helper_->GetTooltipText());
745 gfx::Point tooltip_bounds1 = test_tooltip_->location();
747 // Test whether the toolbar changes position on mouse over v2
748 center = v2->bounds().CenterPoint();
749 generator_->MoveMouseRelativeTo(GetWindow(), center);
750 helper_->FireTooltipTimer();
751 EXPECT_TRUE(helper_->IsTooltipVisible());
752 EXPECT_EQ(reference_string, helper_->GetTooltipText());
753 gfx::Point tooltip_bounds2 = test_tooltip_->location();
755 EXPECT_NE(tooltip_bounds1, gfx::Point());
756 EXPECT_NE(tooltip_bounds2, gfx::Point());
757 EXPECT_NE(tooltip_bounds1, tooltip_bounds2);
759 // Test if the toolbar does not change position on encountering a contained
760 // view with the same tooltip text
761 center = v2_1->GetLocalBounds().CenterPoint();
762 views::View::ConvertPointToTarget(v2_1, view_, &center);
763 generator_->MoveMouseRelativeTo(GetWindow(), center);
764 helper_->FireTooltipTimer();
765 gfx::Point tooltip_bounds2_1 = test_tooltip_->location();
767 EXPECT_NE(tooltip_bounds2, tooltip_bounds2_1);
768 EXPECT_TRUE(helper_->IsTooltipVisible());
769 EXPECT_EQ(reference_string, helper_->GetTooltipText());
771 // Test if the toolbar changes position on encountering a contained
772 // view with a different tooltip text
773 center = v2_2->GetLocalBounds().CenterPoint();
774 views::View::ConvertPointToTarget(v2_2, view_, &center);
775 generator_->MoveMouseRelativeTo(GetWindow(), center);
776 helper_->FireTooltipTimer();
777 gfx::Point tooltip_bounds2_2 = test_tooltip_->location();
779 EXPECT_NE(tooltip_bounds2_1, tooltip_bounds2_2);
780 EXPECT_TRUE(helper_->IsTooltipVisible());
781 EXPECT_EQ(alternative_string, helper_->GetTooltipText());
783 // Test if moving from a view that is contained by a larger view, both with
784 // the same tooltip text, does not change tooltip's position.
785 center = v1_1->GetLocalBounds().CenterPoint();
786 views::View::ConvertPointToTarget(v1_1, view_, &center);
787 generator_->MoveMouseRelativeTo(GetWindow(), center);
788 helper_->FireTooltipTimer();
789 gfx::Point tooltip_bounds1_1 = test_tooltip_->location();
791 EXPECT_TRUE(helper_->IsTooltipVisible());
792 EXPECT_EQ(reference_string, helper_->GetTooltipText());
794 center = v1->bounds().CenterPoint();
795 generator_->MoveMouseRelativeTo(GetWindow(), center);
796 helper_->FireTooltipTimer();
797 tooltip_bounds1 = test_tooltip_->location();
799 EXPECT_NE(tooltip_bounds1_1, tooltip_bounds1);
800 EXPECT_EQ(reference_string, helper_->GetTooltipText());
803 } // namespace test
804 } // namespace corewm
805 } // namespace views