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 690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
756 focus_manager->OnKeyEvent(right_key); | 763 focus_manager->OnKeyEvent(right_key); |
757 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); | 764 EXPECT_EQ(v[1], focus_manager->GetFocusedView()); |
758 focus_manager->OnKeyEvent(left_key); | 765 focus_manager->OnKeyEvent(left_key); |
759 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); | 766 EXPECT_EQ(v[0], focus_manager->GetFocusedView()); |
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 |
773 class TextInputTestView : public View { | |
774 public: | |
775 TextInputTestView() {} | |
776 | |
777 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE { | |
778 return &text_input_client_; | |
779 } | |
780 | |
781 private: | |
782 ui::DummyTextInputClient text_input_client_; | |
783 | |
784 DISALLOW_COPY_AND_ASSIGN(TextInputTestView); | |
785 }; | |
786 | |
766 TEST_F(FocusManagerTest, StoreFocusedView) { | 787 TEST_F(FocusManagerTest, StoreFocusedView) { |
msw
2014/06/02 17:17:04
Make a separate test for TextInputClient checks, t
Yuki
2014/06/08 11:08:16
Done.
| |
767 View view; | 788 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
768 GetFocusManager()->SetFocusedView(&view); | 789 cmd_line->AppendSwitch(switches::kEnableTextInputFocusManager); |
790 | |
791 View* view = new TextInputTestView; | |
792 ui::TextInputClient* text_input_client = view->GetTextInputClient(); | |
793 view->SetFocusable(true); | |
794 GetContentsView()->AddChildView(view); | |
795 ui::TextInputFocusManager* text_input_focus_manager = | |
796 ui::TextInputFocusManager::GetInstance(); | |
797 | |
798 GetFocusManager()->SetFocusedView(view); | |
799 EXPECT_EQ(view, GetFocusManager()->GetFocusedView()); | |
800 EXPECT_EQ(text_input_client, | |
801 text_input_focus_manager->GetFocusedTextInputClient()); | |
769 GetFocusManager()->StoreFocusedView(false); | 802 GetFocusManager()->StoreFocusedView(false); |
msw
2014/06/02 17:17:04
nit: EXPECT_EQ(NULL, GetFocusManager()->GetFocused
Yuki
2014/06/08 11:08:16
Done.
| |
770 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); | 803 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
771 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); | 804 EXPECT_EQ(view, GetFocusManager()->GetStoredFocusView()); |
805 EXPECT_EQ(view, GetFocusManager()->GetFocusedView()); | |
806 EXPECT_EQ(text_input_client, | |
807 text_input_focus_manager->GetFocusedTextInputClient()); | |
772 | 808 |
773 // Repeat with |true|. | 809 // Repeat with |true|. |
774 GetFocusManager()->SetFocusedView(&view); | 810 GetFocusManager()->SetFocusedView(view); |
811 EXPECT_EQ(view, GetFocusManager()->GetFocusedView()); | |
812 EXPECT_EQ(text_input_client, | |
813 text_input_focus_manager->GetFocusedTextInputClient()); | |
775 GetFocusManager()->StoreFocusedView(true); | 814 GetFocusManager()->StoreFocusedView(true); |
776 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); | 815 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView()); |
777 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView()); | 816 EXPECT_EQ(view, GetFocusManager()->GetStoredFocusView()); |
817 EXPECT_EQ(view, GetFocusManager()->GetFocusedView()); | |
818 EXPECT_EQ(text_input_client, | |
819 text_input_focus_manager->GetFocusedTextInputClient()); | |
820 | |
821 // Focus the view twice in a row. | |
822 GetFocusManager()->SetFocusedView(view); | |
823 EXPECT_EQ(text_input_client, | |
824 text_input_focus_manager->GetFocusedTextInputClient()); | |
825 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(NULL); | |
826 GetFocusManager()->SetFocusedView(view); | |
827 EXPECT_EQ(text_input_client, | |
828 text_input_focus_manager->GetFocusedTextInputClient()); | |
778 } | 829 } |
779 | 830 |
780 namespace { | 831 namespace { |
781 | 832 |
782 // Trivial WidgetDelegate implementation that allows setting return value of | 833 // Trivial WidgetDelegate implementation that allows setting return value of |
783 // ShouldAdvanceFocusToTopLevelWidget(). | 834 // ShouldAdvanceFocusToTopLevelWidget(). |
784 class AdvanceFocusWidgetDelegate : public WidgetDelegate { | 835 class AdvanceFocusWidgetDelegate : public WidgetDelegate { |
785 public: | 836 public: |
786 explicit AdvanceFocusWidgetDelegate(Widget* widget) | 837 explicit AdvanceFocusWidgetDelegate(Widget* widget) |
787 : widget_(widget), | 838 : widget_(widget), |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
852 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); | 903 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView()); |
853 | 904 |
854 // Allow focus to go to the parent, and focus backwards which should now move | 905 // Allow focus to go to the parent, and focus backwards which should now move |
855 // up |widget_view| (in the parent). | 906 // up |widget_view| (in the parent). |
856 delegate->set_should_advance_focus_to_parent(true); | 907 delegate->set_should_advance_focus_to_parent(true); |
857 GetFocusManager()->AdvanceFocus(true); | 908 GetFocusManager()->AdvanceFocus(true); |
858 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); | 909 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView()); |
859 } | 910 } |
860 | 911 |
861 } // namespace views | 912 } // namespace views |
OLD | NEW |