OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/corewm/tooltip_controller.h" | 5 #include "ui/views/corewm/tooltip_controller.h" |
6 | 6 |
| 7 #include "base/run_loop.h" |
7 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
8 #include "ui/aura/client/cursor_client.h" | 9 #include "ui/aura/client/cursor_client.h" |
9 #include "ui/aura/client/screen_position_client.h" | 10 #include "ui/aura/client/screen_position_client.h" |
10 #include "ui/aura/env.h" | 11 #include "ui/aura/env.h" |
11 #include "ui/aura/test/aura_test_base.h" | 12 #include "ui/aura/test/aura_test_base.h" |
12 #include "ui/aura/test/event_generator.h" | 13 #include "ui/aura/test/event_generator.h" |
13 #include "ui/aura/test/test_screen.h" | 14 #include "ui/aura/test/test_screen.h" |
14 #include "ui/aura/test/test_window_delegate.h" | 15 #include "ui/aura/test/test_window_delegate.h" |
15 #include "ui/aura/window.h" | 16 #include "ui/aura/window.h" |
16 #include "ui/aura/window_event_dispatcher.h" | 17 #include "ui/aura/window_event_dispatcher.h" |
17 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
18 #include "ui/gfx/font.h" | 19 #include "ui/gfx/font.h" |
19 #include "ui/gfx/point.h" | 20 #include "ui/gfx/point.h" |
20 #include "ui/gfx/screen.h" | 21 #include "ui/gfx/screen.h" |
21 #include "ui/gfx/screen_type_delegate.h" | 22 #include "ui/gfx/screen_type_delegate.h" |
22 #include "ui/gfx/text_elider.h" | 23 #include "ui/gfx/text_elider.h" |
23 #include "ui/views/corewm/tooltip_aura.h" | 24 #include "ui/views/corewm/tooltip_aura.h" |
24 #include "ui/views/corewm/tooltip_controller_test_helper.h" | 25 #include "ui/views/corewm/tooltip_controller_test_helper.h" |
| 26 #include "ui/views/test/views_test_base.h" |
25 #include "ui/views/view.h" | 27 #include "ui/views/view.h" |
26 #include "ui/views/widget/tooltip_manager.h" | 28 #include "ui/views/widget/tooltip_manager.h" |
27 #include "ui/views/widget/widget.h" | 29 #include "ui/views/widget/widget.h" |
28 #include "ui/wm/core/default_activation_client.h" | 30 #include "ui/wm/core/default_activation_client.h" |
29 #include "ui/wm/core/wm_state.h" | 31 #include "ui/wm/core/wm_state.h" |
30 #include "ui/wm/public/tooltip_client.h" | 32 #include "ui/wm/public/tooltip_client.h" |
31 #include "ui/wm/public/window_types.h" | 33 #include "ui/wm/public/window_types.h" |
32 | 34 |
33 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
34 #include "ui/base/win/scoped_ole_initializer.h" | 36 #include "ui/base/win/scoped_ole_initializer.h" |
(...skipping 29 matching lines...) Expand all Loading... |
64 } | 66 } |
65 | 67 |
66 TooltipController* GetController(Widget* widget) { | 68 TooltipController* GetController(Widget* widget) { |
67 return static_cast<TooltipController*>( | 69 return static_cast<TooltipController*>( |
68 aura::client::GetTooltipClient( | 70 aura::client::GetTooltipClient( |
69 widget->GetNativeWindow()->GetRootWindow())); | 71 widget->GetNativeWindow()->GetRootWindow())); |
70 } | 72 } |
71 | 73 |
72 } // namespace | 74 } // namespace |
73 | 75 |
74 class TooltipControllerTest : public aura::test::AuraTestBase { | 76 class TooltipControllerTest : public ViewsTestBase { |
75 public: | 77 public: |
76 TooltipControllerTest() : view_(NULL) {} | 78 TooltipControllerTest() : view_(NULL) {} |
77 virtual ~TooltipControllerTest() {} | 79 virtual ~TooltipControllerTest() {} |
78 | 80 |
| 81 aura::Window* root_window() { |
| 82 return GetContext(); |
| 83 } |
| 84 |
79 virtual void SetUp() OVERRIDE { | 85 virtual void SetUp() OVERRIDE { |
80 wm_state_.reset(new wm::WMState); | 86 ViewsTestBase::SetUp(); |
81 aura::test::AuraTestBase::SetUp(); | |
82 new wm::DefaultActivationClient(root_window()); | |
83 #if defined(OS_CHROMEOS) | 87 #if defined(OS_CHROMEOS) |
84 controller_.reset(new TooltipController( | 88 controller_.reset(new TooltipController( |
85 scoped_ptr<views::corewm::Tooltip>( | 89 scoped_ptr<views::corewm::Tooltip>( |
86 new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE)))); | 90 new views::corewm::TooltipAura(gfx::SCREEN_TYPE_ALTERNATE)))); |
87 root_window()->AddPreTargetHandler(controller_.get()); | 91 root_window()->AddPreTargetHandler(controller_.get()); |
88 SetTooltipClient(root_window(), controller_.get()); | 92 SetTooltipClient(root_window(), controller_.get()); |
89 #endif | 93 #endif |
90 widget_.reset(CreateWidget(root_window())); | 94 widget_.reset(CreateWidget(GetContext())); |
91 widget_->SetContentsView(new View); | 95 widget_->SetContentsView(new View); |
92 view_ = new TooltipTestView; | 96 view_ = new TooltipTestView; |
93 widget_->GetContentsView()->AddChildView(view_); | 97 widget_->GetContentsView()->AddChildView(view_); |
94 view_->SetBoundsRect(widget_->GetContentsView()->GetLocalBounds()); | 98 view_->SetBoundsRect(widget_->GetContentsView()->GetLocalBounds()); |
95 helper_.reset(new TooltipControllerTestHelper( | 99 helper_.reset(new TooltipControllerTestHelper( |
96 GetController(widget_.get()))); | 100 GetController(widget_.get()))); |
97 generator_.reset(new aura::test::EventGenerator(GetRootWindow())); | 101 generator_.reset(new aura::test::EventGenerator(GetRootWindow())); |
98 } | 102 } |
99 | 103 |
100 virtual void TearDown() OVERRIDE { | 104 virtual void TearDown() OVERRIDE { |
101 #if defined(OS_CHROMEOS) | 105 #if defined(OS_CHROMEOS) |
102 root_window()->RemovePreTargetHandler(controller_.get()); | 106 root_window()->RemovePreTargetHandler(controller_.get()); |
103 aura::client::SetTooltipClient(root_window(), NULL); | 107 aura::client::SetTooltipClient(root_window(), NULL); |
104 controller_.reset(); | 108 controller_.reset(); |
105 #endif | 109 #endif |
106 generator_.reset(); | 110 generator_.reset(); |
107 helper_.reset(); | 111 helper_.reset(); |
108 widget_.reset(); | 112 widget_.reset(); |
109 aura::test::AuraTestBase::TearDown(); | 113 ViewsTestBase::TearDown(); |
110 wm_state_.reset(); | 114 wm_state_.reset(); |
111 } | 115 } |
112 | 116 |
113 protected: | 117 protected: |
114 aura::Window* GetWindow() { | 118 aura::Window* GetWindow() { |
115 return widget_->GetNativeWindow(); | 119 return widget_->GetNativeWindow(); |
116 } | 120 } |
117 | 121 |
118 aura::Window* GetRootWindow() { | 122 aura::Window* GetRootWindow() { |
119 return GetWindow()->GetRootWindow(); | 123 return GetWindow()->GetRootWindow(); |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 } | 432 } |
429 | 433 |
430 private: | 434 private: |
431 DISALLOW_COPY_AND_ASSIGN(TestScreenPositionClient); | 435 DISALLOW_COPY_AND_ASSIGN(TestScreenPositionClient); |
432 }; | 436 }; |
433 | 437 |
434 } // namespace | 438 } // namespace |
435 | 439 |
436 class TooltipControllerCaptureTest : public TooltipControllerTest { | 440 class TooltipControllerCaptureTest : public TooltipControllerTest { |
437 public: | 441 public: |
438 TooltipControllerCaptureTest() {} | 442 TooltipControllerCaptureTest() : original_screen_(NULL) {} |
439 virtual ~TooltipControllerCaptureTest() {} | 443 virtual ~TooltipControllerCaptureTest() {} |
440 | 444 |
441 virtual void SetUp() OVERRIDE { | 445 virtual void SetUp() OVERRIDE { |
442 TooltipControllerTest::SetUp(); | 446 TooltipControllerTest::SetUp(); |
| 447 original_screen_ = gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE); |
443 aura::client::SetScreenPositionClient(GetRootWindow(), | 448 aura::client::SetScreenPositionClient(GetRootWindow(), |
444 &screen_position_client_); | 449 &screen_position_client_); |
445 #if !defined(OS_CHROMEOS) | 450 #if !defined(OS_CHROMEOS) |
446 desktop_screen_.reset(CreateDesktopScreen()); | 451 desktop_screen_.reset(CreateDesktopScreen()); |
447 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, | 452 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, |
448 desktop_screen_.get()); | 453 desktop_screen_.get()); |
449 #endif | 454 #endif |
450 } | 455 } |
451 | 456 |
452 virtual void TearDown() OVERRIDE { | 457 virtual void TearDown() OVERRIDE { |
453 #if !defined(OS_CHROMEOS) | 458 #if !defined(OS_CHROMEOS) |
454 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen()); | 459 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, original_screen_); |
455 desktop_screen_.reset(); | 460 desktop_screen_.reset(); |
456 #endif | 461 #endif |
457 aura::client::SetScreenPositionClient(GetRootWindow(), NULL); | 462 aura::client::SetScreenPositionClient(GetRootWindow(), NULL); |
458 TooltipControllerTest::TearDown(); | 463 TooltipControllerTest::TearDown(); |
459 } | 464 } |
460 | 465 |
461 private: | 466 private: |
| 467 gfx::Screen* original_screen_; |
462 TestScreenPositionClient screen_position_client_; | 468 TestScreenPositionClient screen_position_client_; |
463 scoped_ptr<gfx::Screen> desktop_screen_; | 469 scoped_ptr<gfx::Screen> desktop_screen_; |
464 | 470 |
465 DISALLOW_COPY_AND_ASSIGN(TooltipControllerCaptureTest); | 471 DISALLOW_COPY_AND_ASSIGN(TooltipControllerCaptureTest); |
466 }; | 472 }; |
467 | 473 |
468 // Verifies when capture is released the TooltipController resets state. | 474 // Verifies when capture is released the TooltipController resets state. |
469 TEST_F(TooltipControllerCaptureTest, CloseOnCaptureLost) { | 475 TEST_F(TooltipControllerCaptureTest, CloseOnCaptureLost) { |
470 view_->GetWidget()->SetCapture(view_); | 476 view_->GetWidget()->SetCapture(view_); |
471 RunAllPendingInMessageLoop(); | 477 { |
| 478 base::RunLoop run_loop; |
| 479 run_loop.RunUntilIdle(); |
| 480 } |
472 view_->set_tooltip_text(ASCIIToUTF16("Tooltip Text")); | 481 view_->set_tooltip_text(ASCIIToUTF16("Tooltip Text")); |
473 generator_->MoveMouseToCenterOf(GetWindow()); | 482 generator_->MoveMouseToCenterOf(GetWindow()); |
474 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text"); | 483 base::string16 expected_tooltip = ASCIIToUTF16("Tooltip Text"); |
475 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(GetWindow())); | 484 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(GetWindow())); |
476 EXPECT_EQ(base::string16(), helper_->GetTooltipText()); | 485 EXPECT_EQ(base::string16(), helper_->GetTooltipText()); |
477 EXPECT_EQ(GetWindow(), helper_->GetTooltipWindow()); | 486 EXPECT_EQ(GetWindow(), helper_->GetTooltipWindow()); |
478 | 487 |
479 // Fire tooltip timer so tooltip becomes visible. | 488 // Fire tooltip timer so tooltip becomes visible. |
480 helper_->FireTooltipTimer(); | 489 helper_->FireTooltipTimer(); |
481 | 490 |
(...skipping 11 matching lines...) Expand all Loading... |
493 #define MAYBE_Capture Capture | 502 #define MAYBE_Capture Capture |
494 #endif | 503 #endif |
495 // Verifies the correct window is found for tooltips when there is a capture. | 504 // Verifies the correct window is found for tooltips when there is a capture. |
496 TEST_F(TooltipControllerCaptureTest, MAYBE_Capture) { | 505 TEST_F(TooltipControllerCaptureTest, MAYBE_Capture) { |
497 const base::string16 tooltip_text(ASCIIToUTF16("1")); | 506 const base::string16 tooltip_text(ASCIIToUTF16("1")); |
498 const base::string16 tooltip_text2(ASCIIToUTF16("2")); | 507 const base::string16 tooltip_text2(ASCIIToUTF16("2")); |
499 | 508 |
500 widget_->SetBounds(gfx::Rect(0, 0, 200, 200)); | 509 widget_->SetBounds(gfx::Rect(0, 0, 200, 200)); |
501 view_->set_tooltip_text(tooltip_text); | 510 view_->set_tooltip_text(tooltip_text); |
502 | 511 |
503 scoped_ptr<views::Widget> widget2(CreateWidget(root_window())); | 512 scoped_ptr<views::Widget> widget2(CreateWidget(GetContext())); |
504 widget2->SetContentsView(new View); | 513 widget2->SetContentsView(new View); |
505 TooltipTestView* view2 = new TooltipTestView; | 514 TooltipTestView* view2 = new TooltipTestView; |
506 widget2->GetContentsView()->AddChildView(view2); | 515 widget2->GetContentsView()->AddChildView(view2); |
507 view2->set_tooltip_text(tooltip_text2); | 516 view2->set_tooltip_text(tooltip_text2); |
508 widget2->SetBounds(gfx::Rect(0, 0, 200, 200)); | 517 widget2->SetBounds(gfx::Rect(0, 0, 200, 200)); |
509 view2->SetBoundsRect(widget2->GetContentsView()->GetLocalBounds()); | 518 view2->SetBoundsRect(widget2->GetContentsView()->GetLocalBounds()); |
510 | 519 |
511 widget_->SetCapture(view_); | 520 widget_->SetCapture(view_); |
512 EXPECT_TRUE(widget_->HasCapture()); | 521 EXPECT_TRUE(widget_->HasCapture()); |
513 widget2->Show(); | 522 widget2->Show(); |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 // the tooltip window is closed. | 771 // the tooltip window is closed. |
763 ui::CancelModeEvent event; | 772 ui::CancelModeEvent event; |
764 helper_->controller()->OnCancelMode(&event); | 773 helper_->controller()->OnCancelMode(&event); |
765 EXPECT_FALSE(helper_->IsTooltipVisible()); | 774 EXPECT_FALSE(helper_->IsTooltipVisible()); |
766 EXPECT_TRUE(helper_->GetTooltipWindow() == NULL); | 775 EXPECT_TRUE(helper_->GetTooltipWindow() == NULL); |
767 } | 776 } |
768 | 777 |
769 } // namespace test | 778 } // namespace test |
770 } // namespace corewm | 779 } // namespace corewm |
771 } // namespace views | 780 } // namespace views |
OLD | NEW |