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 780 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 | 791 |
792 GetFocusManager()->StoreFocusedView(false); | 792 GetFocusManager()->StoreFocusedView(false); |
793 EXPECT_EQ(nullptr, GetFocusManager()->GetFocusedView()); | 793 EXPECT_EQ(nullptr, GetFocusManager()->GetFocusedView()); |
794 | 794 |
795 // Turn on full keyboard access mode and restore focused view. Since view3 is | 795 // Turn on full keyboard access mode and restore focused view. Since view3 is |
796 // still focusable, view3 should have focus. | 796 // still focusable, view3 should have focus. |
797 GetFocusManager()->SetKeyboardAccessible(true); | 797 GetFocusManager()->SetKeyboardAccessible(true); |
798 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); | 798 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
799 EXPECT_EQ(view3, GetFocusManager()->GetFocusedView()); | 799 EXPECT_EQ(view3, GetFocusManager()->GetFocusedView()); |
800 } | 800 } |
| 801 |
| 802 // Test that View::RequestFocus() respects full keyboard access mode. |
| 803 TEST_F(FocusManagerTest, RequestFocus) { |
| 804 View* view1 = new View(); |
| 805 View* view2 = new View(); |
| 806 |
| 807 // Make view1 always focusable, view2 only focusable in accessibility mode. |
| 808 view1->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 809 view2->SetFocusBehavior(View::FocusBehavior::ACCESSIBLE_ONLY); |
| 810 |
| 811 // Adds views to the view hierarchy. |
| 812 GetWidget()->GetRootView()->AddChildView(view1); |
| 813 GetWidget()->GetRootView()->AddChildView(view2); |
| 814 |
| 815 // Verify view1 can always get focus via View::RequestFocus, while view2 can |
| 816 // only get focus in full keyboard accessibility mode. |
| 817 EXPECT_TRUE(GetFocusManager()->keyboard_accessible()); |
| 818 view1->RequestFocus(); |
| 819 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
| 820 view2->RequestFocus(); |
| 821 EXPECT_EQ(view2, GetFocusManager()->GetFocusedView()); |
| 822 |
| 823 // Toggle full keyboard accessibility. |
| 824 GetFocusManager()->SetKeyboardAccessible(false); |
| 825 |
| 826 GetFocusManager()->ClearFocus(); |
| 827 EXPECT_NE(view1, GetFocusManager()->GetFocusedView()); |
| 828 view1->RequestFocus(); |
| 829 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
| 830 view2->RequestFocus(); |
| 831 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
| 832 } |
| 833 |
801 #endif | 834 #endif |
802 | 835 |
803 namespace { | 836 namespace { |
804 | 837 |
805 // Trivial WidgetDelegate implementation that allows setting return value of | 838 // Trivial WidgetDelegate implementation that allows setting return value of |
806 // ShouldAdvanceFocusToTopLevelWidget(). | 839 // ShouldAdvanceFocusToTopLevelWidget(). |
807 class AdvanceFocusWidgetDelegate : public WidgetDelegate { | 840 class AdvanceFocusWidgetDelegate : public WidgetDelegate { |
808 public: | 841 public: |
809 explicit AdvanceFocusWidgetDelegate(Widget* widget) | 842 explicit AdvanceFocusWidgetDelegate(Widget* widget) |
810 : widget_(widget), | 843 : widget_(widget), |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
875 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); | 908 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
876 | 909 |
877 // Allow focus to go to the parent, and focus backwards which should now move | 910 // Allow focus to go to the parent, and focus backwards which should now move |
878 // up |widget_view| (in the parent). | 911 // up |widget_view| (in the parent). |
879 delegate->set_should_advance_focus_to_parent(true); | 912 delegate->set_should_advance_focus_to_parent(true); |
880 GetFocusManager()->AdvanceFocus(true); | 913 GetFocusManager()->AdvanceFocus(true); |
881 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); | 914 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); |
882 } | 915 } |
883 | 916 |
884 } // namespace views | 917 } // namespace views |
OLD | NEW |