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 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); | 777 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); |
778 focus_manager->OnKeyEvent(left_key); | 778 focus_manager->OnKeyEvent(left_key); |
779 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); | 779 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); |
780 focus_manager->OnKeyEvent(down_key); | 780 focus_manager->OnKeyEvent(down_key); |
781 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); | 781 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); |
782 focus_manager->OnKeyEvent(up_key); | 782 focus_manager->OnKeyEvent(up_key); |
783 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); | 783 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); |
784 } | 784 } |
785 | 785 |
786 TEST_F(FocusManagerTest, StoreFocusedView) { | 786 TEST_F(FocusManagerTest, StoreFocusedView) { |
787 View view; | 787 View* view = new View; |
788 GetFocusManager()->SetFocusedView(&view); | 788 // Add view to the view hierarchy and make it focusable. |
| 789 GetWidget()->GetRootView()->AddChildView(view); |
| 790 view->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 791 |
| 792 GetFocusManager()->SetFocusedView(view); |
789 GetFocusManager()->StoreFocusedView(false); | 793 GetFocusManager()->StoreFocusedView(false); |
790 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); | 794 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); |
791 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); | 795 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
792 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); | 796 EXPECT_EQ(view, GetFocusManager()->GetStoredFocusView()); |
793 | 797 |
794 // Repeat with |true|. | 798 // Repeat with |true|. |
795 GetFocusManager()->SetFocusedView(&view); | 799 GetFocusManager()->SetFocusedView(view); |
796 GetFocusManager()->StoreFocusedView(true); | 800 GetFocusManager()->StoreFocusedView(true); |
797 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); | 801 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); |
798 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); | 802 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
799 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); | 803 EXPECT_EQ(view, GetFocusManager()->GetStoredFocusView()); |
800 } | 804 } |
801 | 805 |
| 806 #if defined(OS_MACOSX) |
| 807 // Test that the correct view is restored if full keyboard access is changed. |
| 808 TEST_F(FocusManagerTest, StoreFocusedViewFullKeyboardAccess) { |
| 809 View* view1 = new View; |
| 810 View* view2 = new View; |
| 811 View* view3 = new View; |
| 812 |
| 813 // Make view1 focusable in accessibility mode, view2 not focusable and view3 |
| 814 // always focusable. |
| 815 view1->SetFocusBehavior(View::FocusBehavior::ACCESSIBLE_ONLY); |
| 816 view2->SetFocusBehavior(View::FocusBehavior::NEVER); |
| 817 view3->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 818 |
| 819 // Add views to the view hierarchy |
| 820 GetWidget()->GetRootView()->AddChildView(view1); |
| 821 GetWidget()->GetRootView()->AddChildView(view2); |
| 822 GetWidget()->GetRootView()->AddChildView(view3); |
| 823 |
| 824 view1->RequestFocus(); |
| 825 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
| 826 GetFocusManager()->StoreFocusedView(true); |
| 827 EXPECT_EQ(nullptr, GetFocusManager()->GetFocusedView()); |
| 828 |
| 829 // Turn off full keyboard access mode and restore focused view. Since view1 is |
| 830 // no longer focusable, view3 should have focus. |
| 831 GetFocusManager()->SetKeyboardAccessible(false); |
| 832 EXPECT_FALSE(GetFocusManager()->RestoreFocusedView()); |
| 833 EXPECT_EQ(view3, GetFocusManager()->GetFocusedView()); |
| 834 |
| 835 GetFocusManager()->StoreFocusedView(false); |
| 836 EXPECT_EQ(nullptr, GetFocusManager()->GetFocusedView()); |
| 837 |
| 838 // Turn on full keyboard access mode and restore focused view. Since view3 is |
| 839 // still focusable, view3 should have focus. |
| 840 GetFocusManager()->SetKeyboardAccessible(true); |
| 841 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
| 842 EXPECT_EQ(view3, GetFocusManager()->GetFocusedView()); |
| 843 } |
| 844 #endif |
| 845 |
802 namespace { | 846 namespace { |
803 | 847 |
804 // Trivial WidgetDelegate implementation that allows setting return value of | 848 // Trivial WidgetDelegate implementation that allows setting return value of |
805 // ShouldAdvanceFocusToTopLevelWidget(). | 849 // ShouldAdvanceFocusToTopLevelWidget(). |
806 class AdvanceFocusWidgetDelegate : public WidgetDelegate { | 850 class AdvanceFocusWidgetDelegate : public WidgetDelegate { |
807 public: | 851 public: |
808 explicit AdvanceFocusWidgetDelegate(Widget* widget) | 852 explicit AdvanceFocusWidgetDelegate(Widget* widget) |
809 : widget_(widget), | 853 : widget_(widget), |
810 should_advance_focus_to_parent_(false) {} | 854 should_advance_focus_to_parent_(false) {} |
811 ~AdvanceFocusWidgetDelegate() override {} | 855 ~AdvanceFocusWidgetDelegate() override {} |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); | 918 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
875 | 919 |
876 // Allow focus to go to the parent, and focus backwards which should now move | 920 // Allow focus to go to the parent, and focus backwards which should now move |
877 // up |widget_view| (in the parent). | 921 // up |widget_view| (in the parent). |
878 delegate->set_should_advance_focus_to_parent(true); | 922 delegate->set_should_advance_focus_to_parent(true); |
879 GetFocusManager()->AdvanceFocus(true); | 923 GetFocusManager()->AdvanceFocus(true); |
880 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); | 924 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); |
881 } | 925 } |
882 | 926 |
883 } // namespace views | 927 } // namespace views |
OLD | NEW |