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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/auto_reset.h" | 8 #include "base/auto_reset.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/command_line.h" |
12 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
13 #include "base/pickle.h" | 14 #include "base/pickle.h" |
14 #include "base/string16.h" | 15 #include "base/string16.h" |
15 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
16 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
17 #include "grit/ui_strings.h" | 18 #include "grit/ui_strings.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "ui/base/clipboard/clipboard.h" | 20 #include "ui/base/clipboard/clipboard.h" |
20 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 21 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
21 #include "ui/base/dragdrop/drag_drop_types.h" | 22 #include "ui/base/dragdrop/drag_drop_types.h" |
22 #include "ui/base/ime/text_input_client.h" | 23 #include "ui/base/ime/text_input_client.h" |
23 #include "ui/base/keycodes/keyboard_codes.h" | 24 #include "ui/base/keycodes/keyboard_codes.h" |
24 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 26 #include "ui/base/ui_base_switches.h" |
25 #include "ui/gfx/render_text.h" | 27 #include "ui/gfx/render_text.h" |
26 #include "ui/views/controls/textfield/native_textfield_views.h" | 28 #include "ui/views/controls/textfield/native_textfield_views.h" |
27 #include "ui/views/controls/textfield/textfield.h" | 29 #include "ui/views/controls/textfield/textfield.h" |
28 #include "ui/views/controls/textfield/textfield_controller.h" | 30 #include "ui/views/controls/textfield/textfield_controller.h" |
29 #include "ui/views/controls/textfield/textfield_views_model.h" | 31 #include "ui/views/controls/textfield/textfield_views_model.h" |
30 #include "ui/views/events/event.h" | 32 #include "ui/views/events/event.h" |
31 #include "ui/views/focus/focus_manager.h" | 33 #include "ui/views/focus/focus_manager.h" |
32 #include "ui/views/ime/mock_input_method.h" | 34 #include "ui/views/ime/mock_input_method.h" |
33 #include "ui/views/test/test_views_delegate.h" | 35 #include "ui/views/test/test_views_delegate.h" |
34 #include "ui/views/test/views_test_base.h" | 36 #include "ui/views/test/views_test_base.h" |
35 #include "ui/views/views_delegate.h" | 37 #include "ui/views/views_delegate.h" |
36 #include "ui/views/widget/native_widget_private.h" | 38 #include "ui/views/widget/native_widget_private.h" |
37 #include "ui/views/widget/widget.h" | 39 #include "ui/views/widget/widget.h" |
38 | 40 |
39 namespace { | 41 namespace { |
40 | 42 |
41 // A wrapper of Textfield to intercept the result of OnKeyPressed() and | 43 // A wrapper of Textfield to intercept the result of OnKeyPressed() and |
42 // OnKeyReleased() methods. | 44 // OnKeyReleased() methods. |
43 class TestTextfield : public views::Textfield { | 45 class TestTextfield : public views::Textfield { |
44 public: | 46 public: |
45 TestTextfield() | |
46 : key_handled_(false), | |
47 key_received_(false) { | |
48 } | |
49 | |
50 explicit TestTextfield(StyleFlags style) | 47 explicit TestTextfield(StyleFlags style) |
51 : Textfield(style), | 48 : Textfield(style), |
52 key_handled_(false), | 49 key_handled_(false), |
53 key_received_(false) { | 50 key_received_(false) { |
54 } | 51 } |
55 | 52 |
56 virtual bool OnKeyPressed(const views::KeyEvent& e) OVERRIDE { | 53 virtual bool OnKeyPressed(const views::KeyEvent& e) OVERRIDE { |
57 key_received_ = true; | 54 key_received_ = true; |
58 key_handled_ = views::Textfield::OnKeyPressed(e); | 55 key_handled_ = views::Textfield::OnKeyPressed(e); |
59 return key_handled_; | 56 return key_handled_; |
60 } | 57 } |
61 | 58 |
62 virtual bool OnKeyReleased(const views::KeyEvent& e) OVERRIDE { | 59 virtual bool OnKeyReleased(const views::KeyEvent& e) OVERRIDE { |
63 key_received_ = true; | 60 key_received_ = true; |
64 key_handled_ = views::Textfield::OnKeyReleased(e); | 61 key_handled_ = views::Textfield::OnKeyReleased(e); |
65 return key_handled_; | 62 return key_handled_; |
66 } | 63 } |
67 | 64 |
68 bool key_handled() const { return key_handled_; } | 65 bool key_handled() const { return key_handled_; } |
69 bool key_received() const { return key_received_; } | 66 bool key_received() const { return key_received_; } |
70 | 67 |
71 void clear() { | 68 void clear() { key_received_ = key_handled_ = false; } |
72 key_received_ = key_handled_ = false; | |
73 } | |
74 | 69 |
75 private: | 70 private: |
76 bool key_handled_; | 71 bool key_handled_; |
77 bool key_received_; | 72 bool key_received_; |
78 | 73 |
79 DISALLOW_COPY_AND_ASSIGN(TestTextfield); | 74 DISALLOW_COPY_AND_ASSIGN(TestTextfield); |
80 }; | 75 }; |
81 | 76 |
82 // A helper class for use with ui::TextInputClient::GetTextFromRange(). | 77 // A helper class for use with ui::TextInputClient::GetTextFromRange(). |
83 class GetTextHelper { | 78 class GetTextHelper { |
84 public: | 79 public: |
85 GetTextHelper() { | 80 GetTextHelper() {} |
86 } | |
87 | 81 |
88 void set_text(const string16& text) { text_ = text; } | 82 void set_text(const string16& text) { text_ = text; } |
89 const string16& text() const { return text_; } | 83 const string16& text() const { return text_; } |
90 | 84 |
91 private: | 85 private: |
92 string16 text_; | 86 string16 text_; |
93 | 87 |
94 DISALLOW_COPY_AND_ASSIGN(GetTextHelper); | 88 DISALLOW_COPY_AND_ASSIGN(GetTextHelper); |
95 }; | 89 }; |
96 | 90 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 ViewsTestBase::TearDown(); | 128 ViewsTestBase::TearDown(); |
135 } | 129 } |
136 | 130 |
137 // TextfieldController: | 131 // TextfieldController: |
138 virtual void ContentsChanged(Textfield* sender, | 132 virtual void ContentsChanged(Textfield* sender, |
139 const string16& new_contents) { | 133 const string16& new_contents) { |
140 ASSERT_NE(last_contents_, new_contents); | 134 ASSERT_NE(last_contents_, new_contents); |
141 last_contents_ = new_contents; | 135 last_contents_ = new_contents; |
142 } | 136 } |
143 | 137 |
144 virtual bool HandleKeyEvent(Textfield* sender, | 138 virtual bool HandleKeyEvent(Textfield* sender, const KeyEvent& key_event) { |
145 const KeyEvent& key_event) { | |
146 | |
147 // TODO(oshima): figure out how to test the keystroke. | 139 // TODO(oshima): figure out how to test the keystroke. |
148 return false; | 140 return false; |
149 } | 141 } |
150 | 142 |
151 virtual void OnBeforeUserAction(Textfield* sender) { | 143 virtual void OnBeforeUserAction(Textfield* sender) { |
152 ++on_before_user_action_; | 144 ++on_before_user_action_; |
153 } | 145 } |
154 | 146 |
155 virtual void OnAfterUserAction(Textfield* sender) { | 147 virtual void OnAfterUserAction(Textfield* sender) { |
156 ++on_after_user_action_; | 148 ++on_after_user_action_; |
157 } | 149 } |
158 | 150 |
159 void InitTextfield(Textfield::StyleFlags style) { | 151 void InitTextfield(Textfield::StyleFlags style) { |
160 InitTextfields(style, 1); | 152 InitTextfields(style, 1); |
161 } | 153 } |
162 | 154 |
163 void InitTextfields(Textfield::StyleFlags style, int count) { | 155 void InitTextfields(Textfield::StyleFlags style, int count) { |
| 156 // Append kEnableViewsTextfield to use NativeTextfieldViews on Windows. |
| 157 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 158 command_line->AppendSwitch(switches::kEnableViewsTextfield); |
| 159 |
164 ASSERT_FALSE(textfield_); | 160 ASSERT_FALSE(textfield_); |
165 textfield_ = new TestTextfield(style); | 161 textfield_ = new TestTextfield(style); |
166 textfield_->SetController(this); | 162 textfield_->SetController(this); |
167 widget_ = new Widget; | 163 widget_ = new Widget; |
168 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | 164 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); |
169 params.bounds = gfx::Rect(100, 100, 100, 100); | 165 params.bounds = gfx::Rect(100, 100, 100, 100); |
170 widget_->Init(params); | 166 widget_->Init(params); |
171 View* container = new View(); | 167 View* container = new View(); |
172 widget_->SetContentsView(container); | 168 widget_->SetContentsView(container); |
173 container->AddChildView(textfield_); | 169 container->AddChildView(textfield_); |
(...skipping 12 matching lines...) Expand all Loading... |
186 DCHECK(textfield_view_); | 182 DCHECK(textfield_view_); |
187 model_ = textfield_view_->model_.get(); | 183 model_ = textfield_view_->model_.get(); |
188 model_->ClearEditHistory(); | 184 model_->ClearEditHistory(); |
189 | 185 |
190 input_method_ = new MockInputMethod(); | 186 input_method_ = new MockInputMethod(); |
191 widget_->ReplaceInputMethod(input_method_); | 187 widget_->ReplaceInputMethod(input_method_); |
192 | 188 |
193 // Assumes the Widget is always focused. | 189 // Assumes the Widget is always focused. |
194 input_method_->OnFocus(); | 190 input_method_->OnFocus(); |
195 | 191 |
| 192 // TODO(msw): Determine why this requires two calls to work on Windows. |
| 193 textfield_->RequestFocus(); |
196 textfield_->RequestFocus(); | 194 textfield_->RequestFocus(); |
197 } | 195 } |
198 | 196 |
199 ui::MenuModel* GetContextMenuModel() { | 197 ui::MenuModel* GetContextMenuModel() { |
200 textfield_view_->UpdateContextMenu(); | 198 textfield_view_->UpdateContextMenu(); |
201 return textfield_view_->context_menu_contents_.get(); | 199 return textfield_view_->context_menu_contents_.get(); |
202 } | 200 } |
203 | 201 |
204 protected: | 202 protected: |
205 void SendKeyEvent(ui::KeyboardCode key_code, | 203 void SendKeyEvent(ui::KeyboardCode key_code, |
(...skipping 1487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1693 EXPECT_EQ(char_rect[i], actual_rect) << " i=" << i; | 1691 EXPECT_EQ(char_rect[i], actual_rect) << " i=" << i; |
1694 } | 1692 } |
1695 | 1693 |
1696 // Return false if the index is out of range. | 1694 // Return false if the index is out of range. |
1697 EXPECT_FALSE(client->GetCompositionCharacterBounds(char_count, &rect)); | 1695 EXPECT_FALSE(client->GetCompositionCharacterBounds(char_count, &rect)); |
1698 EXPECT_FALSE(client->GetCompositionCharacterBounds(char_count + 1, &rect)); | 1696 EXPECT_FALSE(client->GetCompositionCharacterBounds(char_count + 1, &rect)); |
1699 EXPECT_FALSE(client->GetCompositionCharacterBounds(char_count + 100, &rect)); | 1697 EXPECT_FALSE(client->GetCompositionCharacterBounds(char_count + 100, &rect)); |
1700 } | 1698 } |
1701 | 1699 |
1702 } // namespace views | 1700 } // namespace views |
OLD | NEW |