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 "ui/views/focus/focus_manager.h" | 5 #include "ui/views/focus/focus_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); | 734 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); |
735 focus_manager->OnKeyEvent(left_key); | 735 focus_manager->OnKeyEvent(left_key); |
736 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); | 736 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); |
737 focus_manager->OnKeyEvent(down_key); | 737 focus_manager->OnKeyEvent(down_key); |
738 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); | 738 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); |
739 focus_manager->OnKeyEvent(up_key); | 739 focus_manager->OnKeyEvent(up_key); |
740 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); | 740 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); |
741 } | 741 } |
742 | 742 |
743 TEST_F(FocusManagerTest, StoreFocusedView) { | 743 TEST_F(FocusManagerTest, StoreFocusedView) { |
744 View view; | 744 View* view = new View; |
745 GetFocusManager()->SetFocusedView(&view); | 745 // Add view to the view hierarchy and make it focusable. |
| 746 GetWidget()->GetRootView()->AddChildView(view); |
| 747 view->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 748 |
| 749 GetFocusManager()->SetFocusedView(view); |
746 GetFocusManager()->StoreFocusedView(false); | 750 GetFocusManager()->StoreFocusedView(false); |
747 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); | 751 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); |
748 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); | 752 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
749 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); | 753 EXPECT_EQ(view, GetFocusManager()->GetStoredFocusView()); |
750 | 754 |
751 // Repeat with |true|. | 755 // Repeat with |true|. |
752 GetFocusManager()->SetFocusedView(&view); | 756 GetFocusManager()->SetFocusedView(view); |
753 GetFocusManager()->StoreFocusedView(true); | 757 GetFocusManager()->StoreFocusedView(true); |
754 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); | 758 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); |
755 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); | 759 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
756 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); | 760 EXPECT_EQ(view, GetFocusManager()->GetStoredFocusView()); |
757 } | 761 } |
758 | 762 |
| 763 #if defined(OS_MACOSX) |
| 764 // Test that the correct view is restored if full keyboard access is changed. |
| 765 TEST_F(FocusManagerTest, StoreFocusedViewFullKeyboardAccess) { |
| 766 View* view1 = new View; |
| 767 View* view2 = new View; |
| 768 View* view3 = new View; |
| 769 |
| 770 // Make view1 focusable in accessibility mode, view2 not focusable and view3 |
| 771 // always focusable. |
| 772 view1->SetFocusBehavior(View::FocusBehavior::ACCESSIBLE_ONLY); |
| 773 view2->SetFocusBehavior(View::FocusBehavior::NEVER); |
| 774 view3->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 775 |
| 776 // Add views to the view hierarchy |
| 777 GetWidget()->GetRootView()->AddChildView(view1); |
| 778 GetWidget()->GetRootView()->AddChildView(view2); |
| 779 GetWidget()->GetRootView()->AddChildView(view3); |
| 780 |
| 781 view1->RequestFocus(); |
| 782 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
| 783 GetFocusManager()->StoreFocusedView(true); |
| 784 EXPECT_EQ(nullptr, GetFocusManager()->GetFocusedView()); |
| 785 |
| 786 // Turn off full keyboard access mode and restore focused view. Since view1 is |
| 787 // no longer focusable, view3 should have focus. |
| 788 GetFocusManager()->SetKeyboardAccessible(false); |
| 789 EXPECT_FALSE(GetFocusManager()->RestoreFocusedView()); |
| 790 EXPECT_EQ(view3, GetFocusManager()->GetFocusedView()); |
| 791 |
| 792 GetFocusManager()->StoreFocusedView(false); |
| 793 EXPECT_EQ(nullptr, GetFocusManager()->GetFocusedView()); |
| 794 |
| 795 // Turn on full keyboard access mode and restore focused view. Since view3 is |
| 796 // still focusable, view3 should have focus. |
| 797 GetFocusManager()->SetKeyboardAccessible(true); |
| 798 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
| 799 EXPECT_EQ(view3, GetFocusManager()->GetFocusedView()); |
| 800 } |
| 801 #endif |
| 802 |
759 namespace { | 803 namespace { |
760 | 804 |
761 // Trivial WidgetDelegate implementation that allows setting return value of | 805 // Trivial WidgetDelegate implementation that allows setting return value of |
762 // ShouldAdvanceFocusToTopLevelWidget(). | 806 // ShouldAdvanceFocusToTopLevelWidget(). |
763 class AdvanceFocusWidgetDelegate : public WidgetDelegate { | 807 class AdvanceFocusWidgetDelegate : public WidgetDelegate { |
764 public: | 808 public: |
765 explicit AdvanceFocusWidgetDelegate(Widget* widget) | 809 explicit AdvanceFocusWidgetDelegate(Widget* widget) |
766 : widget_(widget), | 810 : widget_(widget), |
767 should_advance_focus_to_parent_(false) {} | 811 should_advance_focus_to_parent_(false) {} |
768 ~AdvanceFocusWidgetDelegate() override {} | 812 ~AdvanceFocusWidgetDelegate() override {} |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
831 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); | 875 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
832 | 876 |
833 // Allow focus to go to the parent, and focus backwards which should now move | 877 // Allow focus to go to the parent, and focus backwards which should now move |
834 // up |widget_view| (in the parent). | 878 // up |widget_view| (in the parent). |
835 delegate->set_should_advance_focus_to_parent(true); | 879 delegate->set_should_advance_focus_to_parent(true); |
836 GetFocusManager()->AdvanceFocus(true); | 880 GetFocusManager()->AdvanceFocus(true); |
837 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); | 881 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); |
838 } | 882 } |
839 | 883 |
840 } // namespace views | 884 } // namespace views |
OLD | NEW |