| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "services/window_manager/focus_controller.h" | 5 #include "services/window_manager/focus_controller.h" |
| 6 | 6 |
| 7 #include "mojo/converters/geometry/geometry_type_converters.h" | 7 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 8 #include "services/window_manager/basic_focus_rules.h" | 8 #include "services/window_manager/basic_focus_rules.h" |
| 9 #include "services/window_manager/capture_controller.h" |
| 9 #include "services/window_manager/focus_controller_observer.h" | 10 #include "services/window_manager/focus_controller_observer.h" |
| 10 #include "services/window_manager/view_event_dispatcher.h" | 11 #include "services/window_manager/view_event_dispatcher.h" |
| 11 #include "services/window_manager/view_targeter.h" | 12 #include "services/window_manager/view_targeter.h" |
| 12 #include "services/window_manager/window_manager_test_util.h" | 13 #include "services/window_manager/window_manager_test_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/events/test/event_generator.h" | 15 #include "ui/events/test/event_generator.h" |
| 15 #include "ui/gfx/geometry/rect.h" | 16 #include "ui/gfx/geometry/rect.h" |
| 16 | 17 |
| 17 using mojo::View; | 18 using mojo::View; |
| 18 | 19 |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 v211(TestView::Build(211, gfx::Rect(1, 1, 5, 5), v21)), | 341 v211(TestView::Build(211, gfx::Rect(1, 1, 5, 5), v21)), |
| 341 v3(TestView::Build(3, gfx::Rect(125, 125, 50, 50), root_view())) {} | 342 v3(TestView::Build(3, gfx::Rect(125, 125, 50, 50), root_view())) {} |
| 342 | 343 |
| 343 // Overridden from testing::Test: | 344 // Overridden from testing::Test: |
| 344 void SetUp() override { | 345 void SetUp() override { |
| 345 testing::Test::SetUp(); | 346 testing::Test::SetUp(); |
| 346 | 347 |
| 347 test_focus_rules_ = new TestFocusRules(root_view()); | 348 test_focus_rules_ = new TestFocusRules(root_view()); |
| 348 focus_controller_.reset( | 349 focus_controller_.reset( |
| 349 new FocusController(scoped_ptr<FocusRules>(test_focus_rules_))); | 350 new FocusController(scoped_ptr<FocusRules>(test_focus_rules_))); |
| 351 SetFocusController(root_view(), focus_controller_.get()); |
| 352 |
| 353 capture_controller_.reset(new CaptureController); |
| 354 SetCaptureController(root_view(), capture_controller_.get()); |
| 350 | 355 |
| 351 ViewTarget* root_target = root_view_->target(); | 356 ViewTarget* root_target = root_view_->target(); |
| 352 root_target->SetEventTargeter(scoped_ptr<ViewTargeter>(new ViewTargeter())); | 357 root_target->SetEventTargeter(scoped_ptr<ViewTargeter>(new ViewTargeter())); |
| 353 view_event_dispatcher_.reset(new ViewEventDispatcher()); | 358 view_event_dispatcher_.reset(new ViewEventDispatcher()); |
| 354 view_event_dispatcher_->SetRootViewTarget(root_target); | 359 view_event_dispatcher_->SetRootViewTarget(root_target); |
| 355 | 360 |
| 356 GetRootViewTarget()->AddPreTargetHandler(focus_controller_.get()); | 361 GetRootViewTarget()->AddPreTargetHandler(focus_controller_.get()); |
| 357 } | 362 } |
| 358 | 363 |
| 359 void TearDown() override { | 364 void TearDown() override { |
| 360 GetRootViewTarget()->RemovePreTargetHandler(focus_controller_.get()); | 365 GetRootViewTarget()->RemovePreTargetHandler(focus_controller_.get()); |
| 361 view_event_dispatcher_.reset(); | 366 view_event_dispatcher_.reset(); |
| 362 | 367 |
| 363 root_view_->Destroy(); | 368 root_view_->Destroy(); |
| 364 | 369 |
| 370 capture_controller_.reset(); |
| 365 test_focus_rules_ = nullptr; // Owned by FocusController. | 371 test_focus_rules_ = nullptr; // Owned by FocusController. |
| 366 focus_controller_.reset(); | 372 focus_controller_.reset(); |
| 367 | 373 |
| 368 testing::Test::TearDown(); | 374 testing::Test::TearDown(); |
| 369 } | 375 } |
| 370 | 376 |
| 371 void FocusView(View* view) { focus_controller_->FocusView(view); } | 377 void FocusView(View* view) { focus_controller_->FocusView(view); } |
| 372 View* GetFocusedView() { return focus_controller_->GetFocusedView(); } | 378 View* GetFocusedView() { return focus_controller_->GetFocusedView(); } |
| 373 int GetFocusedViewId() { | 379 int GetFocusedViewId() { |
| 374 View* focused_view = GetFocusedView(); | 380 View* focused_view = GetFocusedView(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 403 CHECK(!details.dispatcher_destroyed); | 409 CHECK(!details.dispatcher_destroyed); |
| 404 } | 410 } |
| 405 | 411 |
| 406 ViewTarget* GetRootViewTarget() { | 412 ViewTarget* GetRootViewTarget() { |
| 407 return ViewTarget::TargetFromView(root_view()); | 413 return ViewTarget::TargetFromView(root_view()); |
| 408 } | 414 } |
| 409 | 415 |
| 410 View* root_view() { return root_view_; } | 416 View* root_view() { return root_view_; } |
| 411 TestFocusRules* test_focus_rules() { return test_focus_rules_; } | 417 TestFocusRules* test_focus_rules() { return test_focus_rules_; } |
| 412 FocusController* focus_controller() { return focus_controller_.get(); } | 418 FocusController* focus_controller() { return focus_controller_.get(); } |
| 419 CaptureController* capture_controller() { return capture_controller_.get(); } |
| 413 | 420 |
| 414 // Test functions. | 421 // Test functions. |
| 415 virtual void BasicFocus() = 0; | 422 virtual void BasicFocus() = 0; |
| 416 virtual void BasicActivation() = 0; | 423 virtual void BasicActivation() = 0; |
| 417 virtual void FocusEvents() = 0; | 424 virtual void FocusEvents() = 0; |
| 418 virtual void DuplicateFocusEvents() {} | 425 virtual void DuplicateFocusEvents() {} |
| 419 virtual void ActivationEvents() = 0; | 426 virtual void ActivationEvents() = 0; |
| 420 virtual void ReactivationEvents() {} | 427 virtual void ReactivationEvents() {} |
| 421 virtual void DuplicateActivationEvents() {} | 428 virtual void DuplicateActivationEvents() {} |
| 422 virtual void ShiftFocusWithinActiveView() {} | 429 virtual void ShiftFocusWithinActiveView() {} |
| 423 virtual void ShiftFocusToChildOfInactiveView() {} | 430 virtual void ShiftFocusToChildOfInactiveView() {} |
| 424 virtual void ShiftFocusToParentOfFocusedView() {} | 431 virtual void ShiftFocusToParentOfFocusedView() {} |
| 425 virtual void FocusRulesOverride() = 0; | 432 virtual void FocusRulesOverride() = 0; |
| 426 virtual void ActivationRulesOverride() = 0; | 433 virtual void ActivationRulesOverride() = 0; |
| 427 virtual void ShiftFocusOnActivation() {} | 434 virtual void ShiftFocusOnActivation() {} |
| 428 virtual void ShiftFocusOnActivationDueToHide() {} | 435 virtual void ShiftFocusOnActivationDueToHide() {} |
| 429 virtual void NoShiftActiveOnActivation() {} | 436 virtual void NoShiftActiveOnActivation() {} |
| 430 // TODO(erg): void NoFocusChangeOnClickOnCaptureWindow() once we have a | 437 virtual void NoFocusChangeOnClickOnCaptureWindow() {} |
| 431 // system of capture. | 438 virtual void ChangeFocusWhenNothingFocusedAndCaptured() {} |
| 432 // TODO(erg): Also, void ChangeFocusWhenNothingFocusedAndCaptured(). | |
| 433 virtual void DontPassDestroyedView() {} | 439 virtual void DontPassDestroyedView() {} |
| 434 // TODO(erg): Also, void FocusedTextInputClient() once we build the IME. | 440 // TODO(erg): Also, void FocusedTextInputClient() once we build the IME. |
| 435 | 441 |
| 436 private: | 442 private: |
| 437 TestView* root_view_; | 443 TestView* root_view_; |
| 438 scoped_ptr<FocusController> focus_controller_; | 444 scoped_ptr<FocusController> focus_controller_; |
| 439 TestFocusRules* test_focus_rules_; | 445 TestFocusRules* test_focus_rules_; |
| 446 scoped_ptr<CaptureController> capture_controller_; |
| 440 // TODO(erg): The aura version of this class also keeps track of WMState. Do | 447 // TODO(erg): The aura version of this class also keeps track of WMState. Do |
| 441 // we need something analogous here? | 448 // we need something analogous here? |
| 442 | 449 |
| 443 scoped_ptr<ViewEventDispatcher> view_event_dispatcher_; | 450 scoped_ptr<ViewEventDispatcher> view_event_dispatcher_; |
| 444 | 451 |
| 445 TestView* v1; | 452 TestView* v1; |
| 446 TestView* v11; | 453 TestView* v11; |
| 447 TestView* v12; | 454 TestView* v12; |
| 448 TestView* v2; | 455 TestView* v2; |
| 449 TestView* v21; | 456 TestView* v21; |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 | 723 |
| 717 EXPECT_EQ(21, GetFocusedViewId()); | 724 EXPECT_EQ(21, GetFocusedViewId()); |
| 718 | 725 |
| 719 focus_controller()->RemoveObserver(observer.get()); | 726 focus_controller()->RemoveObserver(observer.get()); |
| 720 } | 727 } |
| 721 void NoShiftActiveOnActivation() override { | 728 void NoShiftActiveOnActivation() override { |
| 722 // When a view is activated, we need to prevent any change to activation | 729 // When a view is activated, we need to prevent any change to activation |
| 723 // from being made in response to an activation change notification. | 730 // from being made in response to an activation change notification. |
| 724 } | 731 } |
| 725 | 732 |
| 726 // TODO(erg): Port the capture tests here, once we have a capture mechanism. | 733 void NoFocusChangeOnClickOnCaptureWindow() override { |
| 734 // Clicking on a view which has capture should not cause a focus change |
| 735 // to the view. This test verifies whether that is indeed the case. |
| 736 ActivateViewById(1); |
| 737 |
| 738 EXPECT_EQ(1, GetActiveViewId()); |
| 739 EXPECT_EQ(1, GetFocusedViewId()); |
| 740 |
| 741 View* v2 = root_view()->GetChildById(2); |
| 742 capture_controller()->SetCapture(v2); |
| 743 ClickLeftButton(v2); |
| 744 |
| 745 EXPECT_EQ(1, GetActiveViewId()); |
| 746 EXPECT_EQ(1, GetFocusedViewId()); |
| 747 |
| 748 capture_controller()->ReleaseCapture(v2); |
| 749 } |
| 750 |
| 751 // Verifies focus change is honored while capture held. |
| 752 void ChangeFocusWhenNothingFocusedAndCaptured() override { |
| 753 View* v1 = root_view()->GetChildById(1); |
| 754 capture_controller()->SetCapture(v1); |
| 755 |
| 756 EXPECT_EQ(-1, GetActiveViewId()); |
| 757 EXPECT_EQ(-1, GetFocusedViewId()); |
| 758 |
| 759 FocusViewById(1); |
| 760 |
| 761 EXPECT_EQ(1, GetActiveViewId()); |
| 762 EXPECT_EQ(1, GetFocusedViewId()); |
| 763 |
| 764 capture_controller()->ReleaseCapture(v1); |
| 765 } |
| 727 | 766 |
| 728 // Verifies if a view that loses activation or focus is destroyed during | 767 // Verifies if a view that loses activation or focus is destroyed during |
| 729 // observer notification we don't pass the destroyed view to other observers. | 768 // observer notification we don't pass the destroyed view to other observers. |
| 730 void DontPassDestroyedView() override { | 769 void DontPassDestroyedView() override { |
| 731 FocusViewById(1); | 770 FocusViewById(1); |
| 732 | 771 |
| 733 EXPECT_EQ(1, GetActiveViewId()); | 772 EXPECT_EQ(1, GetActiveViewId()); |
| 734 EXPECT_EQ(1, GetFocusedViewId()); | 773 EXPECT_EQ(1, GetFocusedViewId()); |
| 735 | 774 |
| 736 { | 775 { |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1115 | 1154 |
| 1116 // - Verifies that FocusRules determine what can be activated. | 1155 // - Verifies that FocusRules determine what can be activated. |
| 1117 TARGET_FOCUS_TESTS(ActivationRulesOverride); | 1156 TARGET_FOCUS_TESTS(ActivationRulesOverride); |
| 1118 | 1157 |
| 1119 // - Verifies that attempts to change focus or activation from a focus or | 1158 // - Verifies that attempts to change focus or activation from a focus or |
| 1120 // activation change observer are ignored. | 1159 // activation change observer are ignored. |
| 1121 DIRECT_FOCUS_CHANGE_TESTS(ShiftFocusOnActivation); | 1160 DIRECT_FOCUS_CHANGE_TESTS(ShiftFocusOnActivation); |
| 1122 DIRECT_FOCUS_CHANGE_TESTS(ShiftFocusOnActivationDueToHide); | 1161 DIRECT_FOCUS_CHANGE_TESTS(ShiftFocusOnActivationDueToHide); |
| 1123 DIRECT_FOCUS_CHANGE_TESTS(NoShiftActiveOnActivation); | 1162 DIRECT_FOCUS_CHANGE_TESTS(NoShiftActiveOnActivation); |
| 1124 | 1163 |
| 1125 // TODO(erg): Add the capture tests here. | 1164 // Clicking on a window which has capture should not result in a focus change. |
| 1165 DIRECT_FOCUS_CHANGE_TESTS(NoFocusChangeOnClickOnCaptureWindow); |
| 1166 |
| 1167 FOCUS_CONTROLLER_TEST(FocusControllerApiTest, |
| 1168 ChangeFocusWhenNothingFocusedAndCaptured); |
| 1126 | 1169 |
| 1127 // See description above DontPassDestroyedView() for details. | 1170 // See description above DontPassDestroyedView() for details. |
| 1128 FOCUS_CONTROLLER_TEST(FocusControllerApiTest, DontPassDestroyedView); | 1171 FOCUS_CONTROLLER_TEST(FocusControllerApiTest, DontPassDestroyedView); |
| 1129 | 1172 |
| 1130 // TODO(erg): Add the TextInputClient tests here. | 1173 // TODO(erg): Add the TextInputClient tests here. |
| 1131 | 1174 |
| 1132 // If a mouse event was handled, it should not activate a view. | 1175 // If a mouse event was handled, it should not activate a view. |
| 1133 FOCUS_CONTROLLER_TEST(FocusControllerMouseEventTest, IgnoreHandledEvent); | 1176 FOCUS_CONTROLLER_TEST(FocusControllerMouseEventTest, IgnoreHandledEvent); |
| 1134 | 1177 |
| 1135 } // namespace window_manager | 1178 } // namespace window_manager |
| OLD | NEW |