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" |
| 6 |
5 #include <utility> | 7 #include <utility> |
6 #include <vector> | 8 #include <vector> |
7 | 9 |
| 10 #include "base/command_line.h" |
8 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
9 #include "ui/aura/client/focus_client.h" | 12 #include "ui/aura/client/focus_client.h" |
10 #include "ui/aura/window.h" | 13 #include "ui/aura/window.h" |
11 #include "ui/base/accelerators/accelerator.h" | 14 #include "ui/base/accelerators/accelerator.h" |
| 15 #include "ui/base/ime/dummy_text_input_client.h" |
| 16 #include "ui/base/ime/text_input_focus_manager.h" |
| 17 #include "ui/base/ui_base_switches.h" |
12 #include "ui/events/keycodes/keyboard_codes.h" | 18 #include "ui/events/keycodes/keyboard_codes.h" |
13 #include "ui/views/accessible_pane_view.h" | 19 #include "ui/views/accessible_pane_view.h" |
14 #include "ui/views/controls/button/label_button.h" | 20 #include "ui/views/controls/button/label_button.h" |
15 #include "ui/views/controls/textfield/textfield.h" | |
16 #include "ui/views/focus/focus_manager_factory.h" | 21 #include "ui/views/focus/focus_manager_factory.h" |
17 #include "ui/views/focus/focus_manager_test.h" | 22 #include "ui/views/focus/focus_manager_test.h" |
18 #include "ui/views/focus/widget_focus_manager.h" | 23 #include "ui/views/focus/widget_focus_manager.h" |
19 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
20 | 25 |
21 namespace views { | 26 namespace views { |
22 | 27 |
23 enum FocusTestEventType { | 28 enum FocusTestEventType { |
24 ON_FOCUS = 0, | 29 ON_FOCUS = 0, |
25 ON_BLUR | 30 ON_BLUR |
(...skipping 20 matching lines...) Expand all Loading... |
46 virtual void OnFocus() OVERRIDE { | 51 virtual void OnFocus() OVERRIDE { |
47 event_list_->push_back(FocusTestEvent(ON_FOCUS, id())); | 52 event_list_->push_back(FocusTestEvent(ON_FOCUS, id())); |
48 } | 53 } |
49 | 54 |
50 virtual void OnBlur() OVERRIDE { | 55 virtual void OnBlur() OVERRIDE { |
51 event_list_->push_back(FocusTestEvent(ON_BLUR, id())); | 56 event_list_->push_back(FocusTestEvent(ON_BLUR, id())); |
52 } | 57 } |
53 | 58 |
54 private: | 59 private: |
55 std::vector<FocusTestEvent>* event_list_; | 60 std::vector<FocusTestEvent>* event_list_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(SimpleTestView); |
56 }; | 63 }; |
57 | 64 |
58 // Tests that the appropriate Focus related methods are called when a View | 65 // Tests that the appropriate Focus related methods are called when a View |
59 // gets/loses focus. | 66 // gets/loses focus. |
60 TEST_F(FocusManagerTest, ViewFocusCallbacks) { | 67 TEST_F(FocusManagerTest, ViewFocusCallbacks) { |
61 std::vector<FocusTestEvent> event_list; | 68 std::vector<FocusTestEvent> event_list; |
62 const int kView1ID = 1; | 69 const int kView1ID = 1; |
63 const int kView2ID = 2; | 70 const int kView2ID = 2; |
64 | 71 |
65 SimpleTestView* view1 = new SimpleTestView(&event_list, kView1ID); | 72 SimpleTestView* view1 = new SimpleTestView(&event_list, kView1ID); |
(...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
760 focus_manager->OnKeyEvent(down_key); | 767 focus_manager->OnKeyEvent(down_key); |
761 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); | 768 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); |
762 focus_manager->OnKeyEvent(up_key); | 769 focus_manager->OnKeyEvent(up_key); |
763 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); | 770 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); |
764 } | 771 } |
765 | 772 |
766 TEST_F(FocusManagerTest, StoreFocusedView) { | 773 TEST_F(FocusManagerTest, StoreFocusedView) { |
767 View view; | 774 View view; |
768 GetFocusManager()->SetFocusedView(&view); | 775 GetFocusManager()->SetFocusedView(&view); |
769 GetFocusManager()->StoreFocusedView(false); | 776 GetFocusManager()->StoreFocusedView(false); |
| 777 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); |
770 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); | 778 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
771 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); | 779 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); |
772 | 780 |
773 // Repeat with |true|. | 781 // Repeat with |true|. |
774 GetFocusManager()->SetFocusedView(&view); | 782 GetFocusManager()->SetFocusedView(&view); |
775 GetFocusManager()->StoreFocusedView(true); | 783 GetFocusManager()->StoreFocusedView(true); |
| 784 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView()); |
776 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); | 785 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
777 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); | 786 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); |
778 } | 787 } |
779 | 788 |
| 789 class TextInputTestView : public View { |
| 790 public: |
| 791 TextInputTestView() {} |
| 792 |
| 793 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE { |
| 794 return &text_input_client_; |
| 795 } |
| 796 |
| 797 private: |
| 798 ui::DummyTextInputClient text_input_client_; |
| 799 |
| 800 DISALLOW_COPY_AND_ASSIGN(TextInputTestView); |
| 801 }; |
| 802 |
| 803 TEST_F(FocusManagerTest, TextInputClient) { |
| 804 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 805 cmd_line->AppendSwitch(switches::kEnableTextInputFocusManager); |
| 806 |
| 807 View* view = new TextInputTestView; |
| 808 ui::TextInputClient* text_input_client = view->GetTextInputClient(); |
| 809 view->SetFocusable(true); |
| 810 GetContentsView()->AddChildView(view); |
| 811 ui::TextInputFocusManager* text_input_focus_manager = |
| 812 ui::TextInputFocusManager::GetInstance(); |
| 813 |
| 814 GetFocusManager()->SetFocusedView(view); |
| 815 EXPECT_EQ(view, GetFocusManager()->GetFocusedView()); |
| 816 EXPECT_EQ(text_input_client, |
| 817 text_input_focus_manager->GetFocusedTextInputClient()); |
| 818 GetFocusManager()->StoreFocusedView(false); |
| 819 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
| 820 EXPECT_EQ(text_input_client, |
| 821 text_input_focus_manager->GetFocusedTextInputClient()); |
| 822 |
| 823 // Repeat with |true|. |
| 824 GetFocusManager()->SetFocusedView(view); |
| 825 EXPECT_EQ(view, GetFocusManager()->GetFocusedView()); |
| 826 EXPECT_EQ(text_input_client, |
| 827 text_input_focus_manager->GetFocusedTextInputClient()); |
| 828 GetFocusManager()->StoreFocusedView(true); |
| 829 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
| 830 EXPECT_EQ(text_input_client, |
| 831 text_input_focus_manager->GetFocusedTextInputClient()); |
| 832 |
| 833 // Focus the view twice in a row. |
| 834 GetFocusManager()->SetFocusedView(view); |
| 835 EXPECT_EQ(text_input_client, |
| 836 text_input_focus_manager->GetFocusedTextInputClient()); |
| 837 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(NULL); |
| 838 GetFocusManager()->SetFocusedView(view); |
| 839 EXPECT_EQ(text_input_client, |
| 840 text_input_focus_manager->GetFocusedTextInputClient()); |
| 841 } |
| 842 |
780 namespace { | 843 namespace { |
781 | 844 |
782 // Trivial WidgetDelegate implementation that allows setting return value of | 845 // Trivial WidgetDelegate implementation that allows setting return value of |
783 // ShouldAdvanceFocusToTopLevelWidget(). | 846 // ShouldAdvanceFocusToTopLevelWidget(). |
784 class AdvanceFocusWidgetDelegate : public WidgetDelegate { | 847 class AdvanceFocusWidgetDelegate : public WidgetDelegate { |
785 public: | 848 public: |
786 explicit AdvanceFocusWidgetDelegate(Widget* widget) | 849 explicit AdvanceFocusWidgetDelegate(Widget* widget) |
787 : widget_(widget), | 850 : widget_(widget), |
788 should_advance_focus_to_parent_(false) {} | 851 should_advance_focus_to_parent_(false) {} |
789 virtual ~AdvanceFocusWidgetDelegate() {} | 852 virtual ~AdvanceFocusWidgetDelegate() {} |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); | 915 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
853 | 916 |
854 // Allow focus to go to the parent, and focus backwards which should now move | 917 // Allow focus to go to the parent, and focus backwards which should now move |
855 // up |widget_view| (in the parent). | 918 // up |widget_view| (in the parent). |
856 delegate->set_should_advance_focus_to_parent(true); | 919 delegate->set_should_advance_focus_to_parent(true); |
857 GetFocusManager()->AdvanceFocus(true); | 920 GetFocusManager()->AdvanceFocus(true); |
858 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); | 921 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); |
859 } | 922 } |
860 | 923 |
861 } // namespace views | 924 } // namespace views |
OLD | NEW |