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 <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/base/keycodes/keyboard_codes.h" | 10 #include "ui/base/keycodes/keyboard_codes.h" |
(...skipping 811 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); | 822 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
823 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); | 823 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); |
824 | 824 |
825 // Repeat with |true|. | 825 // Repeat with |true|. |
826 GetFocusManager()->SetFocusedView(&view); | 826 GetFocusManager()->SetFocusedView(&view); |
827 GetFocusManager()->StoreFocusedView(true); | 827 GetFocusManager()->StoreFocusedView(true); |
828 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); | 828 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
829 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); | 829 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); |
830 } | 830 } |
831 | 831 |
| 832 namespace { |
| 833 |
| 834 // Trivial WidgetDelegate implementation that allows setting return value of |
| 835 // ShouldAdvanceFocusToParent(). |
| 836 class AdvanceFocusWidgetDelegate : public WidgetDelegate { |
| 837 public: |
| 838 explicit AdvanceFocusWidgetDelegate(Widget* widget) |
| 839 : widget_(widget), |
| 840 should_advance_focus_to_parent_(false) {} |
| 841 virtual ~AdvanceFocusWidgetDelegate() {} |
| 842 |
| 843 void set_should_advance_focus_to_parent(bool value) { |
| 844 should_advance_focus_to_parent_ = value; |
| 845 } |
| 846 |
| 847 // WidgetDelegate overrides: |
| 848 virtual bool ShouldAdvanceFocusToParent() const OVERRIDE { |
| 849 return should_advance_focus_to_parent_; |
| 850 } |
| 851 virtual Widget* GetWidget() OVERRIDE { return widget_; } |
| 852 virtual const Widget* GetWidget() const { return widget_; } |
| 853 |
| 854 private: |
| 855 Widget* widget_; |
| 856 bool should_advance_focus_to_parent_; |
| 857 |
| 858 DISALLOW_COPY_AND_ASSIGN(AdvanceFocusWidgetDelegate); |
| 859 }; |
| 860 |
| 861 } // namespace |
| 862 |
| 863 // Verifies focus wrapping happens in the same widget. |
| 864 TEST_F(FocusManagerTest, AdvanceFocusStaysInWidget) { |
| 865 // Add |widget_view| as a child of the Widget. |
| 866 View* widget_view = new View; |
| 867 widget_view->set_focusable(true); |
| 868 widget_view->SetBounds(20, 0, 20, 20); |
| 869 GetContentsView()->AddChildView(widget_view); |
| 870 |
| 871 // Create a widget with two views, focus the second. |
| 872 scoped_ptr<AdvanceFocusWidgetDelegate> delegate; |
| 873 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 874 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 875 params.child = true; |
| 876 params.bounds = gfx::Rect(10, 10, 100, 100); |
| 877 params.parent = GetWidget()->GetNativeView(); |
| 878 Widget child_widget; |
| 879 delegate.reset(new AdvanceFocusWidgetDelegate(&child_widget)); |
| 880 params.delegate = delegate.get(); |
| 881 child_widget.Init(params); |
| 882 View* view1 = new View; |
| 883 view1->set_focusable(true); |
| 884 view1->SetBounds(0, 0, 20, 20); |
| 885 View* view2 = new View; |
| 886 view2->set_focusable(true); |
| 887 view2->SetBounds(20, 0, 20, 20); |
| 888 child_widget.client_view()->AddChildView(view1); |
| 889 child_widget.client_view()->AddChildView(view2); |
| 890 child_widget.Show(); |
| 891 view2->RequestFocus(); |
| 892 EXPECT_EQ(view2, GetFocusManager()->GetFocusedView()); |
| 893 |
| 894 // Advance focus backwards, which should focus the first. |
| 895 GetFocusManager()->AdvanceFocus(false); |
| 896 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
| 897 |
| 898 // Focus forward to |view2|. |
| 899 GetFocusManager()->AdvanceFocus(true); |
| 900 EXPECT_EQ(view2, GetFocusManager()->GetFocusedView()); |
| 901 |
| 902 // And forward again, wrapping back to |view1|. |
| 903 GetFocusManager()->AdvanceFocus(true); |
| 904 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
| 905 |
| 906 // Allow focus to go to the parent, and focus backwards which should now move |
| 907 // up |widget_view| (in the parent). |
| 908 delegate->set_should_advance_focus_to_parent(true); |
| 909 GetFocusManager()->AdvanceFocus(true); |
| 910 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); |
| 911 } |
| 912 |
832 } // namespace views | 913 } // namespace views |
OLD | NEW |