Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: ui/views/focus/focus_manager_unittest.cc

Issue 108063004: Give up focus if the focused view becomes unfocusable (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Try to advance focus first Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <utility> 5 #include <utility>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/base/accelerators/accelerator.h" 9 #include "ui/base/accelerators/accelerator.h"
10 #include "ui/events/keycodes/keyboard_codes.h" 10 #include "ui/events/keycodes/keyboard_codes.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 } 46 }
47 47
48 FocusTestEventType type; 48 FocusTestEventType type;
49 int view_id; 49 int view_id;
50 }; 50 };
51 51
52 class SimpleTestView : public View { 52 class SimpleTestView : public View {
53 public: 53 public:
54 SimpleTestView(std::vector<FocusTestEvent>* event_list, int view_id) 54 SimpleTestView(std::vector<FocusTestEvent>* event_list, int view_id)
55 : event_list_(event_list) { 55 : event_list_(event_list) {
56 set_focusable(true); 56 SetFocusable(true);
57 set_id(view_id); 57 set_id(view_id);
58 } 58 }
59 59
60 virtual void OnFocus() OVERRIDE { 60 virtual void OnFocus() OVERRIDE {
61 event_list_->push_back(FocusTestEvent(ON_FOCUS, id())); 61 event_list_->push_back(FocusTestEvent(ON_FOCUS, id()));
62 } 62 }
63 63
64 virtual void OnBlur() OVERRIDE { 64 virtual void OnBlur() OVERRIDE {
65 event_list_->push_back(FocusTestEvent(ON_BLUR, id())); 65 event_list_->push_back(FocusTestEvent(ON_BLUR, id()));
66 } 66 }
(...skipping 29 matching lines...) Expand all
96 96
97 event_list.clear(); 97 event_list.clear();
98 GetFocusManager()->ClearFocus(); 98 GetFocusManager()->ClearFocus();
99 ASSERT_EQ(1, static_cast<int>(event_list.size())); 99 ASSERT_EQ(1, static_cast<int>(event_list.size()));
100 EXPECT_EQ(ON_BLUR, event_list[0].type); 100 EXPECT_EQ(ON_BLUR, event_list[0].type);
101 EXPECT_EQ(kView2ID, event_list[0].view_id); 101 EXPECT_EQ(kView2ID, event_list[0].view_id);
102 } 102 }
103 103
104 TEST_F(FocusManagerTest, FocusChangeListener) { 104 TEST_F(FocusManagerTest, FocusChangeListener) {
105 View* view1 = new View(); 105 View* view1 = new View();
106 view1->set_focusable(true); 106 view1->SetFocusable(true);
107 View* view2 = new View(); 107 View* view2 = new View();
108 view2->set_focusable(true); 108 view2->SetFocusable(true);
109 GetContentsView()->AddChildView(view1); 109 GetContentsView()->AddChildView(view1);
110 GetContentsView()->AddChildView(view2); 110 GetContentsView()->AddChildView(view2);
111 111
112 TestFocusChangeListener listener; 112 TestFocusChangeListener listener;
113 AddFocusChangeListener(&listener); 113 AddFocusChangeListener(&listener);
114 114
115 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair 115 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/det ails/520043/error-converting-from-null-to-a-pointer-type-in-std-pair
116 views::View* null_view = NULL; 116 views::View* null_view = NULL;
117 117
118 view1->RequestFocus(); 118 view1->RequestFocus();
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 DISALLOW_COPY_AND_ASSIGN(FocusInAboutToRequestFocusFromTabTraversalView); 623 DISALLOW_COPY_AND_ASSIGN(FocusInAboutToRequestFocusFromTabTraversalView);
624 }; 624 };
625 } // namespace 625 } // namespace
626 626
627 // Verifies a focus change done during a call to 627 // Verifies a focus change done during a call to
628 // AboutToRequestFocusFromTabTraversal() is honored. 628 // AboutToRequestFocusFromTabTraversal() is honored.
629 TEST_F(FocusManagerTest, FocusInAboutToRequestFocusFromTabTraversal) { 629 TEST_F(FocusManagerTest, FocusInAboutToRequestFocusFromTabTraversal) {
630 // Create 3 views focuses the 3 and advances to the second. The 2nd views 630 // Create 3 views focuses the 3 and advances to the second. The 2nd views
631 // implementation of AboutToRequestFocusFromTabTraversal() focuses the first. 631 // implementation of AboutToRequestFocusFromTabTraversal() focuses the first.
632 views::View* v1 = new View; 632 views::View* v1 = new View;
633 v1->set_focusable(true); 633 v1->SetFocusable(true);
634 GetContentsView()->AddChildView(v1); 634 GetContentsView()->AddChildView(v1);
635 635
636 FocusInAboutToRequestFocusFromTabTraversalView* v2 = 636 FocusInAboutToRequestFocusFromTabTraversalView* v2 =
637 new FocusInAboutToRequestFocusFromTabTraversalView; 637 new FocusInAboutToRequestFocusFromTabTraversalView;
638 v2->set_focusable(true); 638 v2->SetFocusable(true);
639 v2->set_view_to_focus(v1); 639 v2->set_view_to_focus(v1);
640 GetContentsView()->AddChildView(v2); 640 GetContentsView()->AddChildView(v2);
641 641
642 views::View* v3 = new View; 642 views::View* v3 = new View;
643 v3->set_focusable(true); 643 v3->SetFocusable(true);
644 GetContentsView()->AddChildView(v3); 644 GetContentsView()->AddChildView(v3);
645 645
646 v3->RequestFocus(); 646 v3->RequestFocus();
647 GetWidget()->GetFocusManager()->AdvanceFocus(true); 647 GetWidget()->GetFocusManager()->AdvanceFocus(true);
648 EXPECT_TRUE(v1->HasFocus()); 648 EXPECT_TRUE(v1->HasFocus());
649 } 649 }
650 650
651 TEST_F(FocusManagerTest, RotatePaneFocus) { 651 TEST_F(FocusManagerTest, RotatePaneFocus) {
652 views::AccessiblePaneView* pane1 = new AccessiblePaneView(); 652 views::AccessiblePaneView* pane1 = new AccessiblePaneView();
653 GetContentsView()->AddChildView(pane1); 653 GetContentsView()->AddChildView(pane1);
654 654
655 views::View* v1 = new View; 655 views::View* v1 = new View;
656 v1->set_focusable(true); 656 v1->SetFocusable(true);
657 pane1->AddChildView(v1); 657 pane1->AddChildView(v1);
658 658
659 views::View* v2 = new View; 659 views::View* v2 = new View;
660 v2->set_focusable(true); 660 v2->SetFocusable(true);
661 pane1->AddChildView(v2); 661 pane1->AddChildView(v2);
662 662
663 views::AccessiblePaneView* pane2 = new AccessiblePaneView(); 663 views::AccessiblePaneView* pane2 = new AccessiblePaneView();
664 GetContentsView()->AddChildView(pane2); 664 GetContentsView()->AddChildView(pane2);
665 665
666 views::View* v3 = new View; 666 views::View* v3 = new View;
667 v3->set_focusable(true); 667 v3->SetFocusable(true);
668 pane2->AddChildView(v3); 668 pane2->AddChildView(v3);
669 669
670 views::View* v4 = new View; 670 views::View* v4 = new View;
671 v4->set_focusable(true); 671 v4->SetFocusable(true);
672 pane2->AddChildView(v4); 672 pane2->AddChildView(v4);
673 673
674 std::vector<views::View*> panes; 674 std::vector<views::View*> panes;
675 panes.push_back(pane1); 675 panes.push_back(pane1);
676 panes.push_back(pane2); 676 panes.push_back(pane2);
677 SetAccessiblePanes(panes); 677 SetAccessiblePanes(panes);
678 678
679 FocusManager* focus_manager = GetWidget()->GetFocusManager(); 679 FocusManager* focus_manager = GetWidget()->GetFocusManager();
680 680
681 // Advance forwards. Focus should stay trapped within each pane. 681 // Advance forwards. Focus should stay trapped within each pane.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
716 EXPECT_EQ(v3, focus_manager->GetFocusedView()); 716 EXPECT_EQ(v3, focus_manager->GetFocusedView());
717 717
718 EXPECT_FALSE(focus_manager->RotatePaneFocus( 718 EXPECT_FALSE(focus_manager->RotatePaneFocus(
719 FocusManager::kForward, FocusManager::kNoWrap)); 719 FocusManager::kForward, FocusManager::kNoWrap));
720 EXPECT_EQ(v3, focus_manager->GetFocusedView()); 720 EXPECT_EQ(v3, focus_manager->GetFocusedView());
721 } 721 }
722 722
723 // Verifies the stored focus view tracks the focused view. 723 // Verifies the stored focus view tracks the focused view.
724 TEST_F(FocusManagerTest, ImplicitlyStoresFocus) { 724 TEST_F(FocusManagerTest, ImplicitlyStoresFocus) {
725 views::View* v1 = new View; 725 views::View* v1 = new View;
726 v1->set_focusable(true); 726 v1->SetFocusable(true);
727 GetContentsView()->AddChildView(v1); 727 GetContentsView()->AddChildView(v1);
728 728
729 views::View* v2 = new View; 729 views::View* v2 = new View;
730 v2->set_focusable(true); 730 v2->SetFocusable(true);
731 GetContentsView()->AddChildView(v2); 731 GetContentsView()->AddChildView(v2);
732 732
733 // Verify a focus request on |v1| implicitly updates the stored focus view. 733 // Verify a focus request on |v1| implicitly updates the stored focus view.
734 v1->RequestFocus(); 734 v1->RequestFocus();
735 EXPECT_TRUE(v1->HasFocus()); 735 EXPECT_TRUE(v1->HasFocus());
736 EXPECT_EQ(v1, GetWidget()->GetFocusManager()->GetStoredFocusView()); 736 EXPECT_EQ(v1, GetWidget()->GetFocusManager()->GetStoredFocusView());
737 737
738 // Verify a focus request on |v2| implicitly updates the stored focus view. 738 // Verify a focus request on |v2| implicitly updates the stored focus view.
739 v2->RequestFocus(); 739 v2->RequestFocus();
740 EXPECT_TRUE(v2->HasFocus()); 740 EXPECT_TRUE(v2->HasFocus());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
778 const ui::KeyEvent right_key( 778 const ui::KeyEvent right_key(
779 ui::ET_KEY_PRESSED, ui::VKEY_RIGHT, ui::EF_NONE, false); 779 ui::ET_KEY_PRESSED, ui::VKEY_RIGHT, ui::EF_NONE, false);
780 const ui::KeyEvent up_key( 780 const ui::KeyEvent up_key(
781 ui::ET_KEY_PRESSED, ui::VKEY_UP, ui::EF_NONE, false); 781 ui::ET_KEY_PRESSED, ui::VKEY_UP, ui::EF_NONE, false);
782 const ui::KeyEvent down_key( 782 const ui::KeyEvent down_key(
783 ui::ET_KEY_PRESSED, ui::VKEY_DOWN, ui::EF_NONE, false); 783 ui::ET_KEY_PRESSED, ui::VKEY_DOWN, ui::EF_NONE, false);
784 784
785 std::vector<views::View*> v; 785 std::vector<views::View*> v;
786 for (size_t i = 0; i < 2; ++i) { 786 for (size_t i = 0; i < 2; ++i) {
787 views::View* view = new View; 787 views::View* view = new View;
788 view->set_focusable(true); 788 view->SetFocusable(true);
789 GetContentsView()->AddChildView(view); 789 GetContentsView()->AddChildView(view);
790 v.push_back(view); 790 v.push_back(view);
791 } 791 }
792 792
793 // Arrow key traversal is off and arrow key does not change focus. 793 // Arrow key traversal is off and arrow key does not change focus.
794 FocusManager::set_arrow_key_traversal_enabled(false); 794 FocusManager::set_arrow_key_traversal_enabled(false);
795 v[0]->RequestFocus(); 795 v[0]->RequestFocus();
796 focus_manager->OnKeyEvent(right_key); 796 focus_manager->OnKeyEvent(right_key);
797 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); 797 EXPECT_EQ(v[0], focus_manager->GetFocusedView());
798 focus_manager->OnKeyEvent(left_key); 798 focus_manager->OnKeyEvent(left_key);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 857
858 DISALLOW_COPY_AND_ASSIGN(AdvanceFocusWidgetDelegate); 858 DISALLOW_COPY_AND_ASSIGN(AdvanceFocusWidgetDelegate);
859 }; 859 };
860 860
861 } // namespace 861 } // namespace
862 862
863 // Verifies focus wrapping happens in the same widget. 863 // Verifies focus wrapping happens in the same widget.
864 TEST_F(FocusManagerTest, AdvanceFocusStaysInWidget) { 864 TEST_F(FocusManagerTest, AdvanceFocusStaysInWidget) {
865 // Add |widget_view| as a child of the Widget. 865 // Add |widget_view| as a child of the Widget.
866 View* widget_view = new View; 866 View* widget_view = new View;
867 widget_view->set_focusable(true); 867 widget_view->SetFocusable(true);
868 widget_view->SetBounds(20, 0, 20, 20); 868 widget_view->SetBounds(20, 0, 20, 20);
869 GetContentsView()->AddChildView(widget_view); 869 GetContentsView()->AddChildView(widget_view);
870 870
871 // Create a widget with two views, focus the second. 871 // Create a widget with two views, focus the second.
872 scoped_ptr<AdvanceFocusWidgetDelegate> delegate; 872 scoped_ptr<AdvanceFocusWidgetDelegate> delegate;
873 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); 873 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
874 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 874 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
875 params.child = true; 875 params.child = true;
876 params.bounds = gfx::Rect(10, 10, 100, 100); 876 params.bounds = gfx::Rect(10, 10, 100, 100);
877 params.parent = GetWidget()->GetNativeView(); 877 params.parent = GetWidget()->GetNativeView();
878 Widget child_widget; 878 Widget child_widget;
879 delegate.reset(new AdvanceFocusWidgetDelegate(&child_widget)); 879 delegate.reset(new AdvanceFocusWidgetDelegate(&child_widget));
880 params.delegate = delegate.get(); 880 params.delegate = delegate.get();
881 child_widget.Init(params); 881 child_widget.Init(params);
882 View* view1 = new View; 882 View* view1 = new View;
883 view1->set_focusable(true); 883 view1->SetFocusable(true);
884 view1->SetBounds(0, 0, 20, 20); 884 view1->SetBounds(0, 0, 20, 20);
885 View* view2 = new View; 885 View* view2 = new View;
886 view2->set_focusable(true); 886 view2->SetFocusable(true);
887 view2->SetBounds(20, 0, 20, 20); 887 view2->SetBounds(20, 0, 20, 20);
888 child_widget.client_view()->AddChildView(view1); 888 child_widget.client_view()->AddChildView(view1);
889 child_widget.client_view()->AddChildView(view2); 889 child_widget.client_view()->AddChildView(view2);
890 child_widget.Show(); 890 child_widget.Show();
891 view2->RequestFocus(); 891 view2->RequestFocus();
892 EXPECT_EQ(view2, GetFocusManager()->GetFocusedView()); 892 EXPECT_EQ(view2, GetFocusManager()->GetFocusedView());
893 893
894 // Advance focus backwards, which should focus the first. 894 // Advance focus backwards, which should focus the first.
895 GetFocusManager()->AdvanceFocus(false); 895 GetFocusManager()->AdvanceFocus(false);
896 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); 896 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView());
897 897
898 // Focus forward to |view2|. 898 // Focus forward to |view2|.
899 GetFocusManager()->AdvanceFocus(true); 899 GetFocusManager()->AdvanceFocus(true);
900 EXPECT_EQ(view2, GetFocusManager()->GetFocusedView()); 900 EXPECT_EQ(view2, GetFocusManager()->GetFocusedView());
901 901
902 // And forward again, wrapping back to |view1|. 902 // And forward again, wrapping back to |view1|.
903 GetFocusManager()->AdvanceFocus(true); 903 GetFocusManager()->AdvanceFocus(true);
904 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); 904 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView());
905 905
906 // Allow focus to go to the parent, and focus backwards which should now move 906 // Allow focus to go to the parent, and focus backwards which should now move
907 // up |widget_view| (in the parent). 907 // up |widget_view| (in the parent).
908 delegate->set_should_advance_focus_to_parent(true); 908 delegate->set_should_advance_focus_to_parent(true);
909 GetFocusManager()->AdvanceFocus(true); 909 GetFocusManager()->AdvanceFocus(true);
910 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); 910 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView());
911 } 911 }
912 912
913 } // namespace views 913 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698