OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/auto_reset.h" | 5 #include "base/auto_reset.h" |
| 6 #include "base/bind.h" |
| 7 #include "base/bind_helpers.h" |
| 8 #include "base/callback.h" |
6 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
7 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "ui/base/clipboard/clipboard.h" | 12 #include "ui/base/clipboard/clipboard.h" |
10 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 13 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
11 #include "ui/base/keycodes/keyboard_codes.h" | 14 #include "ui/base/keycodes/keyboard_codes.h" |
12 #include "views/controls/menu/menu_2.h" | 15 #include "views/controls/menu/menu_2.h" |
13 #include "views/controls/textfield/native_textfield_views.h" | 16 #include "views/controls/textfield/native_textfield_views.h" |
14 #include "views/controls/textfield/textfield.h" | 17 #include "views/controls/textfield/textfield.h" |
15 #include "views/controls/textfield/textfield_controller.h" | 18 #include "views/controls/textfield/textfield_controller.h" |
16 #include "views/controls/textfield/textfield_views_model.h" | 19 #include "views/controls/textfield/textfield_views_model.h" |
17 #include "views/events/event.h" | 20 #include "views/events/event.h" |
18 #include "views/focus/focus_manager.h" | 21 #include "views/focus/focus_manager.h" |
| 22 #include "views/ime/mock_input_method.h" |
| 23 #include "views/ime/text_input_client.h" |
19 #include "views/test/test_views_delegate.h" | 24 #include "views/test/test_views_delegate.h" |
20 #include "views/test/views_test_base.h" | 25 #include "views/test/views_test_base.h" |
21 #include "views/views_delegate.h" | 26 #include "views/views_delegate.h" |
| 27 #include "views/widget/native_widget.h" |
22 #include "views/widget/widget.h" | 28 #include "views/widget/widget.h" |
23 | 29 |
| 30 namespace { |
| 31 |
| 32 // A wrapper of Textfield to intercept the result of OnKeyPressed() and |
| 33 // OnKeyReleased() methods. |
| 34 class TestTextfield : public views::Textfield { |
| 35 public: |
| 36 TestTextfield() |
| 37 : key_handled_(false), |
| 38 key_received_(false) { |
| 39 } |
| 40 |
| 41 explicit TestTextfield(StyleFlags style) |
| 42 : Textfield(style), |
| 43 key_handled_(false), |
| 44 key_received_(false) { |
| 45 } |
| 46 |
| 47 virtual bool OnKeyPressed(const views::KeyEvent& e) OVERRIDE { |
| 48 key_received_ = true; |
| 49 key_handled_ = views::Textfield::OnKeyPressed(e); |
| 50 return key_handled_; |
| 51 } |
| 52 |
| 53 virtual bool OnKeyReleased(const views::KeyEvent& e) OVERRIDE { |
| 54 key_received_ = true; |
| 55 key_handled_ = views::Textfield::OnKeyReleased(e); |
| 56 return key_handled_; |
| 57 } |
| 58 |
| 59 bool key_handled() const { return key_handled_; } |
| 60 bool key_received() const { return key_received_; } |
| 61 |
| 62 void clear() { |
| 63 key_received_ = key_handled_ = false; |
| 64 } |
| 65 |
| 66 private: |
| 67 bool key_handled_; |
| 68 bool key_received_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(TestTextfield); |
| 71 }; |
| 72 |
| 73 // A helper class for use with TextInputClient::GetTextFromRange(). |
| 74 class GetTextHelper { |
| 75 public: |
| 76 GetTextHelper() { |
| 77 } |
| 78 |
| 79 void set_text(const string16& text) { text_ = text; } |
| 80 const string16& text() const { return text_; } |
| 81 |
| 82 private: |
| 83 string16 text_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(GetTextHelper); |
| 86 }; |
| 87 |
| 88 } // namespace |
| 89 |
24 namespace views { | 90 namespace views { |
25 | 91 |
26 // Convert to Wide so that the printed string will be readable when | 92 // Convert to Wide so that the printed string will be readable when |
27 // check fails. | 93 // check fails. |
28 #define EXPECT_STR_EQ(ascii, utf16) \ | 94 #define EXPECT_STR_EQ(ascii, utf16) \ |
29 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16)) | 95 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16)) |
30 #define EXPECT_STR_NE(ascii, utf16) \ | 96 #define EXPECT_STR_NE(ascii, utf16) \ |
31 EXPECT_NE(ASCIIToWide(ascii), UTF16ToWide(utf16)) | 97 EXPECT_NE(ASCIIToWide(ascii), UTF16ToWide(utf16)) |
32 | 98 |
33 // TODO(oshima): Move tests that are independent of TextfieldViews to | 99 // TODO(oshima): Move tests that are independent of TextfieldViews to |
34 // textfield_unittests.cc once we move the test utility functions | 100 // textfield_unittests.cc once we move the test utility functions |
35 // from chrome/browser/automation/ to app/test/. | 101 // from chrome/browser/automation/ to app/test/. |
36 class NativeTextfieldViewsTest : public ViewsTestBase, | 102 class NativeTextfieldViewsTest : public ViewsTestBase, |
37 public TextfieldController { | 103 public TextfieldController { |
38 public: | 104 public: |
39 NativeTextfieldViewsTest() | 105 NativeTextfieldViewsTest() |
40 : widget_(NULL), | 106 : widget_(NULL), |
41 textfield_(NULL), | 107 textfield_(NULL), |
42 textfield_view_(NULL), | 108 textfield_view_(NULL), |
43 model_(NULL) { | 109 model_(NULL), |
| 110 input_method_(NULL), |
| 111 on_before_user_action_(0), |
| 112 on_after_user_action_(0) { |
44 } | 113 } |
45 | 114 |
46 // ::testing::Test: | 115 // ::testing::Test: |
47 virtual void SetUp() { | 116 virtual void SetUp() { |
48 NativeTextfieldViews::SetEnableTextfieldViews(true); | 117 NativeTextfieldViews::SetEnableTextfieldViews(true); |
49 } | 118 } |
50 | 119 |
51 virtual void TearDown() { | 120 virtual void TearDown() { |
52 NativeTextfieldViews::SetEnableTextfieldViews(false); | 121 NativeTextfieldViews::SetEnableTextfieldViews(false); |
53 if (widget_) | 122 if (widget_) |
54 widget_->Close(); | 123 widget_->Close(); |
55 ViewsTestBase::TearDown(); | 124 ViewsTestBase::TearDown(); |
56 } | 125 } |
57 | 126 |
58 // TextfieldController: | 127 // TextfieldController: |
59 virtual void ContentsChanged(Textfield* sender, | 128 virtual void ContentsChanged(Textfield* sender, |
60 const string16& new_contents){ | 129 const string16& new_contents) { |
| 130 ASSERT_NE(last_contents_, new_contents); |
61 last_contents_ = new_contents; | 131 last_contents_ = new_contents; |
62 } | 132 } |
63 | 133 |
64 virtual bool HandleKeyEvent(Textfield* sender, | 134 virtual bool HandleKeyEvent(Textfield* sender, |
65 const KeyEvent& key_event) { | 135 const KeyEvent& key_event) { |
66 | 136 |
67 // TODO(oshima): figure out how to test the keystroke. | 137 // TODO(oshima): figure out how to test the keystroke. |
68 return false; | 138 return false; |
69 } | 139 } |
70 | 140 |
| 141 virtual void OnBeforeUserAction(Textfield* sender) { |
| 142 ++on_before_user_action_; |
| 143 } |
| 144 |
| 145 virtual void OnAfterUserAction(Textfield* sender) { |
| 146 ++on_after_user_action_; |
| 147 } |
| 148 |
71 void InitTextfield(Textfield::StyleFlags style) { | 149 void InitTextfield(Textfield::StyleFlags style) { |
72 InitTextfields(style, 1); | 150 InitTextfields(style, 1); |
73 } | 151 } |
74 | 152 |
75 void InitTextfields(Textfield::StyleFlags style, int count) { | 153 void InitTextfields(Textfield::StyleFlags style, int count) { |
76 ASSERT_FALSE(textfield_); | 154 ASSERT_FALSE(textfield_); |
77 textfield_ = new Textfield(style); | 155 textfield_ = new TestTextfield(style); |
78 textfield_->SetController(this); | 156 textfield_->SetController(this); |
79 Widget::CreateParams params(Widget::CreateParams::TYPE_POPUP); | 157 Widget::CreateParams params(Widget::CreateParams::TYPE_POPUP); |
80 params.mirror_origin_in_rtl = false; | 158 params.mirror_origin_in_rtl = false; |
81 widget_ = Widget::CreateWidget(params); | 159 widget_ = Widget::CreateWidget(params); |
82 widget_->Init(NULL, gfx::Rect(100, 100, 100, 100)); | 160 widget_->Init(NULL, gfx::Rect(100, 100, 100, 100)); |
83 View* container = new View(); | 161 View* container = new View(); |
84 widget_->SetContentsView(container); | 162 widget_->SetContentsView(container); |
85 container->AddChildView(textfield_); | 163 container->AddChildView(textfield_); |
86 | 164 |
87 textfield_view_ | 165 textfield_view_ |
88 = static_cast<NativeTextfieldViews*>(textfield_->native_wrapper()); | 166 = static_cast<NativeTextfieldViews*>(textfield_->native_wrapper()); |
89 textfield_->SetID(1); | 167 textfield_->SetID(1); |
90 | 168 |
91 for (int i = 1; i < count; i++) { | 169 for (int i = 1; i < count; i++) { |
92 Textfield* textfield = new Textfield(style); | 170 Textfield* textfield = new Textfield(style); |
93 container->AddChildView(textfield); | 171 container->AddChildView(textfield); |
94 textfield->SetID(i + 1); | 172 textfield->SetID(i + 1); |
95 } | 173 } |
96 | 174 |
97 DCHECK(textfield_view_); | 175 DCHECK(textfield_view_); |
98 model_ = textfield_view_->model_.get(); | 176 model_ = textfield_view_->model_.get(); |
| 177 |
| 178 input_method_ = new MockInputMethod(); |
| 179 widget_->native_widget()->ReplaceInputMethod(input_method_); |
| 180 |
| 181 // Assumes the Widget is always focused. |
| 182 input_method_->OnFocus(); |
| 183 |
| 184 textfield_->RequestFocus(); |
99 } | 185 } |
100 | 186 |
101 views::Menu2* GetContextMenu() { | 187 views::Menu2* GetContextMenu() { |
102 textfield_view_->InitContextMenuIfRequired(); | 188 textfield_view_->InitContextMenuIfRequired(); |
103 return textfield_view_->context_menu_menu_.get(); | 189 return textfield_view_->context_menu_menu_.get(); |
104 } | 190 } |
105 | 191 |
106 NativeTextfieldViews::ClickState GetClickState() { | 192 NativeTextfieldViews::ClickState GetClickState() { |
107 return textfield_view_->click_state_; | 193 return textfield_view_->click_state_; |
108 } | 194 } |
109 | 195 |
110 protected: | 196 protected: |
111 bool SendKeyEventToTextfieldViews(ui::KeyboardCode key_code, | 197 void SendKeyEvent(ui::KeyboardCode key_code, |
112 bool shift, | 198 bool shift, |
113 bool control, | 199 bool control, |
114 bool capslock) { | 200 bool capslock) { |
115 int flags = (shift ? ui::EF_SHIFT_DOWN : 0) | | 201 int flags = (shift ? ui::EF_SHIFT_DOWN : 0) | |
116 (control ? ui::EF_CONTROL_DOWN : 0) | | 202 (control ? ui::EF_CONTROL_DOWN : 0) | |
117 (capslock ? ui::EF_CAPS_LOCK_DOWN : 0); | 203 (capslock ? ui::EF_CAPS_LOCK_DOWN : 0); |
118 KeyEvent event(ui::ET_KEY_PRESSED, key_code, flags); | 204 KeyEvent event(ui::ET_KEY_PRESSED, key_code, flags); |
119 return textfield_->OnKeyPressed(event); | 205 input_method_->DispatchKeyEvent(event); |
120 } | 206 } |
121 | 207 |
122 bool SendKeyEventToTextfieldViews(ui::KeyboardCode key_code, | 208 void SendKeyEvent(ui::KeyboardCode key_code, bool shift, bool control) { |
123 bool shift, | 209 SendKeyEvent(key_code, shift, control, false); |
124 bool control) { | |
125 return SendKeyEventToTextfieldViews(key_code, shift, control, false); | |
126 } | 210 } |
127 | 211 |
128 bool SendKeyEventToTextfieldViews(ui::KeyboardCode key_code) { | 212 void SendKeyEvent(ui::KeyboardCode key_code) { |
129 return SendKeyEventToTextfieldViews(key_code, false, false); | 213 SendKeyEvent(key_code, false, false); |
130 } | 214 } |
131 | 215 |
132 View* GetFocusedView() { | 216 View* GetFocusedView() { |
133 return widget_->GetFocusManager()->GetFocusedView(); | 217 return widget_->GetFocusManager()->GetFocusedView(); |
134 } | 218 } |
135 | 219 |
136 // We need widget to populate wrapper class. | 220 // We need widget to populate wrapper class. |
137 Widget* widget_; | 221 Widget* widget_; |
138 | 222 |
139 Textfield* textfield_; | 223 TestTextfield* textfield_; |
140 NativeTextfieldViews* textfield_view_; | 224 NativeTextfieldViews* textfield_view_; |
141 TextfieldViewsModel* model_; | 225 TextfieldViewsModel* model_; |
142 | 226 |
143 // The string from Controller::ContentsChanged callback. | 227 // The string from Controller::ContentsChanged callback. |
144 string16 last_contents_; | 228 string16 last_contents_; |
145 | 229 |
| 230 // For testing input method related behaviors. |
| 231 MockInputMethod* input_method_; |
| 232 |
| 233 // Indicates how many times OnBeforeUserAction() is called. |
| 234 int on_before_user_action_; |
| 235 |
| 236 // Indicates how many times OnAfterUserAction() is called. |
| 237 int on_after_user_action_; |
| 238 |
146 private: | 239 private: |
147 DISALLOW_COPY_AND_ASSIGN(NativeTextfieldViewsTest); | 240 DISALLOW_COPY_AND_ASSIGN(NativeTextfieldViewsTest); |
148 }; | 241 }; |
149 | 242 |
150 TEST_F(NativeTextfieldViewsTest, ModelChangesTeset) { | 243 TEST_F(NativeTextfieldViewsTest, ModelChangesTest) { |
151 InitTextfield(Textfield::STYLE_DEFAULT); | 244 InitTextfield(Textfield::STYLE_DEFAULT); |
| 245 |
| 246 // TextfieldController::ContentsChanged() shouldn't be called when changing |
| 247 // text programmatically. |
| 248 last_contents_.clear(); |
152 textfield_->SetText(ASCIIToUTF16("this is")); | 249 textfield_->SetText(ASCIIToUTF16("this is")); |
153 | 250 |
154 EXPECT_STR_EQ("this is", model_->text()); | 251 EXPECT_STR_EQ("this is", model_->text()); |
155 EXPECT_STR_EQ("this is", last_contents_); | 252 EXPECT_STR_EQ("this is", textfield_->text()); |
156 last_contents_.clear(); | 253 EXPECT_TRUE(last_contents_.empty()); |
157 | 254 |
158 textfield_->AppendText(ASCIIToUTF16(" a test")); | 255 textfield_->AppendText(ASCIIToUTF16(" a test")); |
159 EXPECT_STR_EQ("this is a test", model_->text()); | 256 EXPECT_STR_EQ("this is a test", model_->text()); |
160 EXPECT_STR_EQ("this is a test", last_contents_); | 257 EXPECT_STR_EQ("this is a test", textfield_->text()); |
161 last_contents_.clear(); | 258 EXPECT_TRUE(last_contents_.empty()); |
162 | |
163 // Cases where the callback should not be called. | |
164 textfield_->SetText(ASCIIToUTF16("this is a test")); | |
165 EXPECT_STR_EQ("this is a test", model_->text()); | |
166 EXPECT_EQ(string16(), last_contents_); | |
167 | |
168 textfield_->AppendText(string16()); | |
169 EXPECT_STR_EQ("this is a test", model_->text()); | |
170 EXPECT_EQ(string16(), last_contents_); | |
171 | 259 |
172 EXPECT_EQ(string16(), textfield_->GetSelectedText()); | 260 EXPECT_EQ(string16(), textfield_->GetSelectedText()); |
173 textfield_->SelectAll(); | 261 textfield_->SelectAll(); |
174 EXPECT_STR_EQ("this is a test", textfield_->GetSelectedText()); | 262 EXPECT_STR_EQ("this is a test", textfield_->GetSelectedText()); |
175 EXPECT_EQ(string16(), last_contents_); | 263 EXPECT_TRUE(last_contents_.empty()); |
176 } | 264 } |
177 | 265 |
178 TEST_F(NativeTextfieldViewsTest, KeyTest) { | 266 TEST_F(NativeTextfieldViewsTest, KeyTest) { |
179 InitTextfield(Textfield::STYLE_DEFAULT); | 267 InitTextfield(Textfield::STYLE_DEFAULT); |
180 SendKeyEventToTextfieldViews(ui::VKEY_C, true, false); | 268 SendKeyEvent(ui::VKEY_C, true, false); |
181 EXPECT_STR_EQ("C", textfield_->text()); | 269 EXPECT_STR_EQ("C", textfield_->text()); |
182 EXPECT_STR_EQ("C", last_contents_); | 270 EXPECT_STR_EQ("C", last_contents_); |
183 last_contents_.clear(); | 271 last_contents_.clear(); |
184 | 272 |
185 SendKeyEventToTextfieldViews(ui::VKEY_R, false, false); | 273 SendKeyEvent(ui::VKEY_R, false, false); |
186 EXPECT_STR_EQ("Cr", textfield_->text()); | 274 EXPECT_STR_EQ("Cr", textfield_->text()); |
187 EXPECT_STR_EQ("Cr", last_contents_); | 275 EXPECT_STR_EQ("Cr", last_contents_); |
188 | 276 |
189 textfield_->SetText(ASCIIToUTF16("")); | 277 textfield_->SetText(ASCIIToUTF16("")); |
190 SendKeyEventToTextfieldViews(ui::VKEY_C, true, false, true); | 278 SendKeyEvent(ui::VKEY_C, true, false, true); |
191 SendKeyEventToTextfieldViews(ui::VKEY_C, false, false, true); | 279 SendKeyEvent(ui::VKEY_C, false, false, true); |
192 SendKeyEventToTextfieldViews(ui::VKEY_1, false, false, true); | 280 SendKeyEvent(ui::VKEY_1, false, false, true); |
193 SendKeyEventToTextfieldViews(ui::VKEY_1, true, false, true); | 281 SendKeyEvent(ui::VKEY_1, true, false, true); |
194 SendKeyEventToTextfieldViews(ui::VKEY_1, true, false, false); | 282 SendKeyEvent(ui::VKEY_1, true, false, false); |
195 EXPECT_STR_EQ("cC1!!", textfield_->text()); | 283 EXPECT_STR_EQ("cC1!!", textfield_->text()); |
196 EXPECT_STR_EQ("cC1!!", last_contents_); | 284 EXPECT_STR_EQ("cC1!!", last_contents_); |
197 } | 285 } |
198 | 286 |
199 TEST_F(NativeTextfieldViewsTest, ControlAndSelectTest) { | 287 TEST_F(NativeTextfieldViewsTest, ControlAndSelectTest) { |
200 // Insert a test string in a textfield. | 288 // Insert a test string in a textfield. |
201 InitTextfield(Textfield::STYLE_DEFAULT); | 289 InitTextfield(Textfield::STYLE_DEFAULT); |
202 textfield_->SetText(ASCIIToUTF16("one two three")); | 290 textfield_->SetText(ASCIIToUTF16("one two three")); |
203 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, | 291 SendKeyEvent(ui::VKEY_RIGHT, |
204 true /* shift */, false /* control */); | 292 true /* shift */, false /* control */); |
205 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, true, false); | 293 SendKeyEvent(ui::VKEY_RIGHT, true, false); |
206 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, true, false); | 294 SendKeyEvent(ui::VKEY_RIGHT, true, false); |
207 | 295 |
208 EXPECT_STR_EQ("one", textfield_->GetSelectedText()); | 296 EXPECT_STR_EQ("one", textfield_->GetSelectedText()); |
209 | 297 |
210 // Test word select. | 298 // Test word select. |
211 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, true, true); | 299 SendKeyEvent(ui::VKEY_RIGHT, true, true); |
212 EXPECT_STR_EQ("one two", textfield_->GetSelectedText()); | 300 EXPECT_STR_EQ("one two", textfield_->GetSelectedText()); |
213 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, true, true); | 301 SendKeyEvent(ui::VKEY_RIGHT, true, true); |
214 EXPECT_STR_EQ("one two three", textfield_->GetSelectedText()); | 302 EXPECT_STR_EQ("one two three", textfield_->GetSelectedText()); |
215 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, true, true); | 303 SendKeyEvent(ui::VKEY_LEFT, true, true); |
216 EXPECT_STR_EQ("one two ", textfield_->GetSelectedText()); | 304 EXPECT_STR_EQ("one two ", textfield_->GetSelectedText()); |
217 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, true, true); | 305 SendKeyEvent(ui::VKEY_LEFT, true, true); |
218 EXPECT_STR_EQ("one ", textfield_->GetSelectedText()); | 306 EXPECT_STR_EQ("one ", textfield_->GetSelectedText()); |
219 | 307 |
220 // Replace the selected text. | 308 // Replace the selected text. |
221 SendKeyEventToTextfieldViews(ui::VKEY_Z, true, false); | 309 SendKeyEvent(ui::VKEY_Z, true, false); |
222 SendKeyEventToTextfieldViews(ui::VKEY_E, true, false); | 310 SendKeyEvent(ui::VKEY_E, true, false); |
223 SendKeyEventToTextfieldViews(ui::VKEY_R, true, false); | 311 SendKeyEvent(ui::VKEY_R, true, false); |
224 SendKeyEventToTextfieldViews(ui::VKEY_O, true, false); | 312 SendKeyEvent(ui::VKEY_O, true, false); |
225 SendKeyEventToTextfieldViews(ui::VKEY_SPACE, false, false); | 313 SendKeyEvent(ui::VKEY_SPACE, false, false); |
226 EXPECT_STR_EQ("ZERO two three", textfield_->text()); | 314 EXPECT_STR_EQ("ZERO two three", textfield_->text()); |
227 | 315 |
228 SendKeyEventToTextfieldViews(ui::VKEY_END, true, false); | 316 SendKeyEvent(ui::VKEY_END, true, false); |
229 EXPECT_STR_EQ("two three", textfield_->GetSelectedText()); | 317 EXPECT_STR_EQ("two three", textfield_->GetSelectedText()); |
230 SendKeyEventToTextfieldViews(ui::VKEY_HOME, true, false); | 318 SendKeyEvent(ui::VKEY_HOME, true, false); |
231 EXPECT_STR_EQ("ZERO ", textfield_->GetSelectedText()); | 319 EXPECT_STR_EQ("ZERO ", textfield_->GetSelectedText()); |
232 } | 320 } |
233 | 321 |
234 TEST_F(NativeTextfieldViewsTest, InsertionDeletionTest) { | 322 TEST_F(NativeTextfieldViewsTest, InsertionDeletionTest) { |
235 // Insert a test string in a textfield. | 323 // Insert a test string in a textfield. |
236 InitTextfield(Textfield::STYLE_DEFAULT); | 324 InitTextfield(Textfield::STYLE_DEFAULT); |
237 char test_str[] = "this is a test"; | 325 char test_str[] = "this is a test"; |
238 for (size_t i = 0; i < sizeof(test_str); i++) { | 326 for (size_t i = 0; i < sizeof(test_str); i++) { |
239 // This is ugly and should be replaced by a utility standard function. | 327 // This is ugly and should be replaced by a utility standard function. |
240 // See comment in NativeTextfieldViews::GetPrintableChar. | 328 // See comment in NativeTextfieldViews::GetPrintableChar. |
241 char c = test_str[i]; | 329 char c = test_str[i]; |
242 ui::KeyboardCode code = | 330 ui::KeyboardCode code = |
243 c == ' ' ? ui::VKEY_SPACE : | 331 c == ' ' ? ui::VKEY_SPACE : |
244 static_cast<ui::KeyboardCode>(ui::VKEY_A + c - 'a'); | 332 static_cast<ui::KeyboardCode>(ui::VKEY_A + c - 'a'); |
245 SendKeyEventToTextfieldViews(code); | 333 SendKeyEvent(code); |
246 } | 334 } |
247 EXPECT_STR_EQ(test_str, textfield_->text()); | 335 EXPECT_STR_EQ(test_str, textfield_->text()); |
248 | 336 |
249 // Move the cursor around. | 337 // Move the cursor around. |
250 for (int i = 0; i < 6; i++) { | 338 for (int i = 0; i < 6; i++) { |
251 SendKeyEventToTextfieldViews(ui::VKEY_LEFT); | 339 SendKeyEvent(ui::VKEY_LEFT); |
252 } | 340 } |
253 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT); | 341 SendKeyEvent(ui::VKEY_RIGHT); |
254 | 342 |
255 // Delete using backspace and check resulting string. | 343 // Delete using backspace and check resulting string. |
256 SendKeyEventToTextfieldViews(ui::VKEY_BACK); | 344 SendKeyEvent(ui::VKEY_BACK); |
257 EXPECT_STR_EQ("this is test", textfield_->text()); | 345 EXPECT_STR_EQ("this is test", textfield_->text()); |
258 | 346 |
259 // Delete using delete key and check resulting string. | 347 // Delete using delete key and check resulting string. |
260 for (int i = 0; i < 5; i++) { | 348 for (int i = 0; i < 5; i++) { |
261 SendKeyEventToTextfieldViews(ui::VKEY_DELETE); | 349 SendKeyEvent(ui::VKEY_DELETE); |
262 } | 350 } |
263 EXPECT_STR_EQ("this is ", textfield_->text()); | 351 EXPECT_STR_EQ("this is ", textfield_->text()); |
264 | 352 |
265 // Select all and replace with "k". | 353 // Select all and replace with "k". |
266 textfield_->SelectAll(); | 354 textfield_->SelectAll(); |
267 SendKeyEventToTextfieldViews(ui::VKEY_K); | 355 SendKeyEvent(ui::VKEY_K); |
268 EXPECT_STR_EQ("k", textfield_->text()); | 356 EXPECT_STR_EQ("k", textfield_->text()); |
269 | 357 |
270 // Delete the previous word from cursor. | 358 // Delete the previous word from cursor. |
271 textfield_->SetText(ASCIIToUTF16("one two three four")); | 359 textfield_->SetText(ASCIIToUTF16("one two three four")); |
272 SendKeyEventToTextfieldViews(ui::VKEY_END); | 360 SendKeyEvent(ui::VKEY_END); |
273 SendKeyEventToTextfieldViews(ui::VKEY_BACK, false, true, false); | 361 SendKeyEvent(ui::VKEY_BACK, false, true, false); |
274 EXPECT_STR_EQ("one two three ", textfield_->text()); | 362 EXPECT_STR_EQ("one two three ", textfield_->text()); |
275 | 363 |
276 // Delete upto the beginning of the buffer from cursor in chromeos, do nothing | 364 // Delete upto the beginning of the buffer from cursor in chromeos, do nothing |
277 // in windows. | 365 // in windows. |
278 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true, false); | 366 SendKeyEvent(ui::VKEY_LEFT, false, true, false); |
279 SendKeyEventToTextfieldViews(ui::VKEY_BACK, true, true, false); | 367 SendKeyEvent(ui::VKEY_BACK, true, true, false); |
280 #if defined(OS_WIN) | 368 #if defined(OS_WIN) |
281 EXPECT_STR_EQ("one two three ", textfield_->text()); | 369 EXPECT_STR_EQ("one two three ", textfield_->text()); |
282 #else | 370 #else |
283 EXPECT_STR_EQ("three ", textfield_->text()); | 371 EXPECT_STR_EQ("three ", textfield_->text()); |
284 #endif | 372 #endif |
285 | 373 |
286 // Delete the next word from cursor. | 374 // Delete the next word from cursor. |
287 textfield_->SetText(ASCIIToUTF16("one two three four")); | 375 textfield_->SetText(ASCIIToUTF16("one two three four")); |
288 SendKeyEventToTextfieldViews(ui::VKEY_HOME); | 376 SendKeyEvent(ui::VKEY_HOME); |
289 SendKeyEventToTextfieldViews(ui::VKEY_DELETE, false, true, false); | 377 SendKeyEvent(ui::VKEY_DELETE, false, true, false); |
290 EXPECT_STR_EQ(" two three four", textfield_->text()); | 378 EXPECT_STR_EQ(" two three four", textfield_->text()); |
291 | 379 |
292 // Delete upto the end of the buffer from cursor in chromeos, do nothing | 380 // Delete upto the end of the buffer from cursor in chromeos, do nothing |
293 // in windows. | 381 // in windows. |
294 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, false, true, false); | 382 SendKeyEvent(ui::VKEY_RIGHT, false, true, false); |
295 SendKeyEventToTextfieldViews(ui::VKEY_DELETE, true, true, false); | 383 SendKeyEvent(ui::VKEY_DELETE, true, true, false); |
296 #if defined(OS_WIN) | 384 #if defined(OS_WIN) |
297 EXPECT_STR_EQ(" two three four", textfield_->text()); | 385 EXPECT_STR_EQ(" two three four", textfield_->text()); |
298 #else | 386 #else |
299 EXPECT_STR_EQ(" two", textfield_->text()); | 387 EXPECT_STR_EQ(" two", textfield_->text()); |
300 #endif | 388 #endif |
301 } | 389 } |
302 | 390 |
303 TEST_F(NativeTextfieldViewsTest, PasswordTest) { | 391 TEST_F(NativeTextfieldViewsTest, PasswordTest) { |
304 InitTextfield(Textfield::STYLE_PASSWORD); | 392 InitTextfield(Textfield::STYLE_PASSWORD); |
| 393 |
| 394 last_contents_.clear(); |
305 textfield_->SetText(ASCIIToUTF16("my password")); | 395 textfield_->SetText(ASCIIToUTF16("my password")); |
306 // Just to make sure the text() and callback returns | 396 // Just to make sure the text() and callback returns |
307 // the actual text instead of "*". | 397 // the actual text instead of "*". |
308 EXPECT_STR_EQ("my password", textfield_->text()); | 398 EXPECT_STR_EQ("my password", textfield_->text()); |
309 EXPECT_STR_EQ("my password", last_contents_); | 399 EXPECT_TRUE(last_contents_.empty()); |
310 } | 400 } |
311 | 401 |
312 TEST_F(NativeTextfieldViewsTest, OnKeyPressReturnValueTest) { | 402 TEST_F(NativeTextfieldViewsTest, OnKeyPressReturnValueTest) { |
313 InitTextfield(Textfield::STYLE_DEFAULT); | 403 InitTextfield(Textfield::STYLE_DEFAULT); |
314 EXPECT_TRUE(SendKeyEventToTextfieldViews(ui::VKEY_A)); | 404 |
| 405 // Character keys will be handled by input method. |
| 406 SendKeyEvent(ui::VKEY_A); |
| 407 EXPECT_TRUE(textfield_->key_received()); |
| 408 EXPECT_FALSE(textfield_->key_handled()); |
| 409 textfield_->clear(); |
| 410 |
| 411 // Home will be handled. |
| 412 SendKeyEvent(ui::VKEY_HOME); |
| 413 EXPECT_TRUE(textfield_->key_received()); |
| 414 EXPECT_TRUE(textfield_->key_handled()); |
| 415 textfield_->clear(); |
| 416 |
315 // F24, up/down key won't be handled. | 417 // F24, up/down key won't be handled. |
316 EXPECT_FALSE(SendKeyEventToTextfieldViews(ui::VKEY_F24)); | 418 SendKeyEvent(ui::VKEY_F24); |
317 EXPECT_FALSE(SendKeyEventToTextfieldViews(ui::VKEY_UP)); | 419 EXPECT_TRUE(textfield_->key_received()); |
318 EXPECT_FALSE(SendKeyEventToTextfieldViews(ui::VKEY_DOWN)); | 420 EXPECT_FALSE(textfield_->key_handled()); |
| 421 textfield_->clear(); |
| 422 |
| 423 SendKeyEvent(ui::VKEY_UP); |
| 424 EXPECT_TRUE(textfield_->key_received()); |
| 425 EXPECT_FALSE(textfield_->key_handled()); |
| 426 textfield_->clear(); |
| 427 |
| 428 SendKeyEvent(ui::VKEY_DOWN); |
| 429 EXPECT_TRUE(textfield_->key_received()); |
| 430 EXPECT_FALSE(textfield_->key_handled()); |
319 } | 431 } |
320 | 432 |
321 TEST_F(NativeTextfieldViewsTest, CursorMovement) { | 433 TEST_F(NativeTextfieldViewsTest, CursorMovement) { |
322 InitTextfield(Textfield::STYLE_DEFAULT); | 434 InitTextfield(Textfield::STYLE_DEFAULT); |
323 | 435 |
324 // Test with trailing whitespace. | 436 // Test with trailing whitespace. |
325 textfield_->SetText(ASCIIToUTF16("one two hre ")); | 437 textfield_->SetText(ASCIIToUTF16("one two hre ")); |
326 | 438 |
327 // Send the cursor at the end. | 439 // Send the cursor at the end. |
328 SendKeyEventToTextfieldViews(ui::VKEY_END); | 440 SendKeyEvent(ui::VKEY_END); |
329 | 441 |
330 // Ctrl+Left should move the cursor just before the last word. | 442 // Ctrl+Left should move the cursor just before the last word. |
331 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true); | 443 SendKeyEvent(ui::VKEY_LEFT, false, true); |
332 SendKeyEventToTextfieldViews(ui::VKEY_T); | 444 SendKeyEvent(ui::VKEY_T); |
333 EXPECT_STR_EQ("one two thre ", textfield_->text()); | 445 EXPECT_STR_EQ("one two thre ", textfield_->text()); |
334 EXPECT_STR_EQ("one two thre ", last_contents_); | 446 EXPECT_STR_EQ("one two thre ", last_contents_); |
335 | 447 |
336 // Ctrl+Right should move the cursor to the end of the last word. | 448 // Ctrl+Right should move the cursor to the end of the last word. |
337 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, false, true); | 449 SendKeyEvent(ui::VKEY_RIGHT, false, true); |
338 SendKeyEventToTextfieldViews(ui::VKEY_E); | 450 SendKeyEvent(ui::VKEY_E); |
339 EXPECT_STR_EQ("one two three ", textfield_->text()); | 451 EXPECT_STR_EQ("one two three ", textfield_->text()); |
340 EXPECT_STR_EQ("one two three ", last_contents_); | 452 EXPECT_STR_EQ("one two three ", last_contents_); |
341 | 453 |
342 // Ctrl+Right again should move the cursor to the end. | 454 // Ctrl+Right again should move the cursor to the end. |
343 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, false, true); | 455 SendKeyEvent(ui::VKEY_RIGHT, false, true); |
344 SendKeyEventToTextfieldViews(ui::VKEY_BACK); | 456 SendKeyEvent(ui::VKEY_BACK); |
345 EXPECT_STR_EQ("one two three", textfield_->text()); | 457 EXPECT_STR_EQ("one two three", textfield_->text()); |
346 EXPECT_STR_EQ("one two three", last_contents_); | 458 EXPECT_STR_EQ("one two three", last_contents_); |
347 | 459 |
348 // Test with leading whitespace. | 460 // Test with leading whitespace. |
349 textfield_->SetText(ASCIIToUTF16(" ne two")); | 461 textfield_->SetText(ASCIIToUTF16(" ne two")); |
350 | 462 |
351 // Send the cursor at the beginning. | 463 // Send the cursor at the beginning. |
352 SendKeyEventToTextfieldViews(ui::VKEY_HOME); | 464 SendKeyEvent(ui::VKEY_HOME); |
353 | 465 |
354 // Ctrl+Right, then Ctrl+Left should move the cursor to the beginning of the | 466 // Ctrl+Right, then Ctrl+Left should move the cursor to the beginning of the |
355 // first word. | 467 // first word. |
356 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, false, true); | 468 SendKeyEvent(ui::VKEY_RIGHT, false, true); |
357 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true); | 469 SendKeyEvent(ui::VKEY_LEFT, false, true); |
358 SendKeyEventToTextfieldViews(ui::VKEY_O); | 470 SendKeyEvent(ui::VKEY_O); |
359 EXPECT_STR_EQ(" one two", textfield_->text()); | 471 EXPECT_STR_EQ(" one two", textfield_->text()); |
360 EXPECT_STR_EQ(" one two", last_contents_); | 472 EXPECT_STR_EQ(" one two", last_contents_); |
361 | 473 |
362 // Ctrl+Left to move the cursor to the beginning of the first word. | 474 // Ctrl+Left to move the cursor to the beginning of the first word. |
363 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true); | 475 SendKeyEvent(ui::VKEY_LEFT, false, true); |
364 // Ctrl+Left again should move the cursor back to the very beginning. | 476 // Ctrl+Left again should move the cursor back to the very beginning. |
365 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true); | 477 SendKeyEvent(ui::VKEY_LEFT, false, true); |
366 SendKeyEventToTextfieldViews(ui::VKEY_DELETE); | 478 SendKeyEvent(ui::VKEY_DELETE); |
367 EXPECT_STR_EQ("one two", textfield_->text()); | 479 EXPECT_STR_EQ("one two", textfield_->text()); |
368 EXPECT_STR_EQ("one two", last_contents_); | 480 EXPECT_STR_EQ("one two", last_contents_); |
369 } | 481 } |
370 | 482 |
371 #if defined(OS_WIN) | 483 TEST_F(NativeTextfieldViewsTest, FocusTraversalTest) { |
372 // TODO(oshima): Windows' FocusManager::ClearNativeFocus() resets the | |
373 // focused view to NULL, which causes crash in this test. Figure out | |
374 // why and fix this. | |
375 #define MAYBE_FocusTraversalTest DISABLED_FocusTraversalTest | |
376 #else | |
377 #define MAYBE_FocusTraversalTest FocusTraversalTest | |
378 #endif | |
379 TEST_F(NativeTextfieldViewsTest, MAYBE_FocusTraversalTest) { | |
380 InitTextfields(Textfield::STYLE_DEFAULT, 3); | 484 InitTextfields(Textfield::STYLE_DEFAULT, 3); |
381 textfield_->RequestFocus(); | 485 textfield_->RequestFocus(); |
382 | 486 |
383 EXPECT_EQ(1, GetFocusedView()->GetID()); | 487 EXPECT_EQ(1, GetFocusedView()->GetID()); |
384 widget_->GetFocusManager()->AdvanceFocus(false); | 488 widget_->GetFocusManager()->AdvanceFocus(false); |
385 EXPECT_EQ(2, GetFocusedView()->GetID()); | 489 EXPECT_EQ(2, GetFocusedView()->GetID()); |
386 widget_->GetFocusManager()->AdvanceFocus(false); | 490 widget_->GetFocusManager()->AdvanceFocus(false); |
387 EXPECT_EQ(3, GetFocusedView()->GetID()); | 491 EXPECT_EQ(3, GetFocusedView()->GetID()); |
388 // Cycle back to the first textfield. | 492 // Cycle back to the first textfield. |
389 widget_->GetFocusManager()->AdvanceFocus(false); | 493 widget_->GetFocusManager()->AdvanceFocus(false); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 } | 561 } |
458 | 562 |
459 TEST_F(NativeTextfieldViewsTest, ReadOnlyTest) { | 563 TEST_F(NativeTextfieldViewsTest, ReadOnlyTest) { |
460 scoped_ptr<TestViewsDelegate> test_views_delegate(new TestViewsDelegate()); | 564 scoped_ptr<TestViewsDelegate> test_views_delegate(new TestViewsDelegate()); |
461 AutoReset<views::ViewsDelegate*> auto_reset( | 565 AutoReset<views::ViewsDelegate*> auto_reset( |
462 &views::ViewsDelegate::views_delegate, test_views_delegate.get()); | 566 &views::ViewsDelegate::views_delegate, test_views_delegate.get()); |
463 | 567 |
464 InitTextfield(Textfield::STYLE_DEFAULT); | 568 InitTextfield(Textfield::STYLE_DEFAULT); |
465 textfield_->SetText(ASCIIToUTF16(" one two three ")); | 569 textfield_->SetText(ASCIIToUTF16(" one two three ")); |
466 textfield_->SetReadOnly(true); | 570 textfield_->SetReadOnly(true); |
467 SendKeyEventToTextfieldViews(ui::VKEY_HOME); | 571 SendKeyEvent(ui::VKEY_HOME); |
468 EXPECT_EQ(0U, textfield_->GetCursorPosition()); | 572 EXPECT_EQ(0U, textfield_->GetCursorPosition()); |
469 | 573 |
470 SendKeyEventToTextfieldViews(ui::VKEY_END); | 574 SendKeyEvent(ui::VKEY_END); |
471 EXPECT_EQ(15U, textfield_->GetCursorPosition()); | 575 EXPECT_EQ(15U, textfield_->GetCursorPosition()); |
472 | 576 |
473 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, false); | 577 SendKeyEvent(ui::VKEY_LEFT, false, false); |
474 EXPECT_EQ(14U, textfield_->GetCursorPosition()); | 578 EXPECT_EQ(14U, textfield_->GetCursorPosition()); |
475 | 579 |
476 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true); | 580 SendKeyEvent(ui::VKEY_LEFT, false, true); |
477 EXPECT_EQ(9U, textfield_->GetCursorPosition()); | 581 EXPECT_EQ(9U, textfield_->GetCursorPosition()); |
478 | 582 |
479 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, true, true); | 583 SendKeyEvent(ui::VKEY_LEFT, true, true); |
480 EXPECT_EQ(5U, textfield_->GetCursorPosition()); | 584 EXPECT_EQ(5U, textfield_->GetCursorPosition()); |
481 EXPECT_STR_EQ("two ", textfield_->GetSelectedText()); | 585 EXPECT_STR_EQ("two ", textfield_->GetSelectedText()); |
482 | 586 |
483 textfield_->SelectAll(); | 587 textfield_->SelectAll(); |
484 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); | 588 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); |
485 | 589 |
486 // CUT&PASTE does not work, but COPY works | 590 // CUT&PASTE does not work, but COPY works |
487 SendKeyEventToTextfieldViews(ui::VKEY_X, false, true); | 591 SendKeyEvent(ui::VKEY_X, false, true); |
488 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); | 592 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); |
489 string16 str; | 593 string16 str; |
490 views::ViewsDelegate::views_delegate->GetClipboard()-> | 594 views::ViewsDelegate::views_delegate->GetClipboard()-> |
491 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); | 595 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); |
492 EXPECT_STR_NE(" one two three ", str); | 596 EXPECT_STR_NE(" one two three ", str); |
493 | 597 |
494 SendKeyEventToTextfieldViews(ui::VKEY_C, false, true); | 598 SendKeyEvent(ui::VKEY_C, false, true); |
495 views::ViewsDelegate::views_delegate->GetClipboard()-> | 599 views::ViewsDelegate::views_delegate->GetClipboard()-> |
496 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); | 600 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); |
497 EXPECT_STR_EQ(" one two three ", str); | 601 EXPECT_STR_EQ(" one two three ", str); |
498 | 602 |
499 // SetText should work even in read only mode. | 603 // SetText should work even in read only mode. |
500 textfield_->SetText(ASCIIToUTF16(" four five six ")); | 604 textfield_->SetText(ASCIIToUTF16(" four five six ")); |
501 EXPECT_STR_EQ(" four five six ", textfield_->text()); | 605 EXPECT_STR_EQ(" four five six ", textfield_->text()); |
502 | 606 |
503 // Paste shouldn't work. | 607 // Paste shouldn't work. |
504 SendKeyEventToTextfieldViews(ui::VKEY_V, false, true); | 608 SendKeyEvent(ui::VKEY_V, false, true); |
505 EXPECT_STR_EQ(" four five six ", textfield_->text()); | 609 EXPECT_STR_EQ(" four five six ", textfield_->text()); |
506 EXPECT_TRUE(textfield_->GetSelectedText().empty()); | 610 EXPECT_TRUE(textfield_->GetSelectedText().empty()); |
507 | 611 |
508 textfield_->SelectAll(); | 612 textfield_->SelectAll(); |
509 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); | 613 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); |
510 | 614 |
511 // Text field is unmodifiable and selection shouldn't change. | 615 // Text field is unmodifiable and selection shouldn't change. |
512 SendKeyEventToTextfieldViews(ui::VKEY_DELETE); | 616 SendKeyEvent(ui::VKEY_DELETE); |
513 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); | 617 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); |
514 SendKeyEventToTextfieldViews(ui::VKEY_BACK); | 618 SendKeyEvent(ui::VKEY_BACK); |
515 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); | 619 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); |
516 SendKeyEventToTextfieldViews(ui::VKEY_T); | 620 SendKeyEvent(ui::VKEY_T); |
517 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); | 621 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); |
518 } | 622 } |
519 | 623 |
| 624 TEST_F(NativeTextfieldViewsTest, TextInputClientTest) { |
| 625 InitTextfield(Textfield::STYLE_DEFAULT); |
| 626 TextInputClient* client = textfield_->GetTextInputClient(); |
| 627 EXPECT_TRUE(client); |
| 628 EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT, client->GetTextInputType()); |
| 629 |
| 630 textfield_->SetText(ASCIIToUTF16("0123456789")); |
| 631 ui::Range range; |
| 632 EXPECT_TRUE(client->GetTextRange(&range)); |
| 633 EXPECT_EQ(0U, range.start()); |
| 634 EXPECT_EQ(10U, range.end()); |
| 635 |
| 636 EXPECT_TRUE(client->SetSelectionRange(ui::Range(1, 4))); |
| 637 EXPECT_TRUE(client->GetSelectionRange(&range)); |
| 638 EXPECT_EQ(ui::Range(1,4), range); |
| 639 |
| 640 // This code can't be compiled because of a bug in base::Callback. |
| 641 #if 0 |
| 642 GetTextHelper helper; |
| 643 base::Callback<void(string16)> callback = |
| 644 base::Bind(&GetTextHelper::set_text, base::Unretained(&helper)); |
| 645 |
| 646 EXPECT_TRUE(client->GetTextFromRange(range, callback)); |
| 647 EXPECT_STR_EQ("123", helper.text()); |
| 648 #endif |
| 649 |
| 650 EXPECT_TRUE(client->DeleteRange(range)); |
| 651 EXPECT_STR_EQ("0456789", textfield_->text()); |
| 652 |
| 653 ui::CompositionText composition; |
| 654 composition.text = UTF8ToUTF16("321"); |
| 655 // Set composition through input method. |
| 656 input_method_->Clear(); |
| 657 input_method_->SetCompositionTextForNextKey(composition); |
| 658 textfield_->clear(); |
| 659 |
| 660 on_before_user_action_ = on_after_user_action_ = 0; |
| 661 SendKeyEvent(ui::VKEY_A); |
| 662 EXPECT_TRUE(textfield_->key_received()); |
| 663 EXPECT_FALSE(textfield_->key_handled()); |
| 664 EXPECT_TRUE(client->HasCompositionText()); |
| 665 EXPECT_TRUE(client->GetCompositionTextRange(&range)); |
| 666 EXPECT_STR_EQ("0321456789", textfield_->text()); |
| 667 EXPECT_EQ(ui::Range(1,4), range); |
| 668 EXPECT_EQ(2, on_before_user_action_); |
| 669 EXPECT_EQ(2, on_after_user_action_); |
| 670 |
| 671 input_method_->SetResultTextForNextKey(UTF8ToUTF16("123")); |
| 672 on_before_user_action_ = on_after_user_action_ = 0; |
| 673 textfield_->clear(); |
| 674 SendKeyEvent(ui::VKEY_A); |
| 675 EXPECT_TRUE(textfield_->key_received()); |
| 676 EXPECT_FALSE(textfield_->key_handled()); |
| 677 EXPECT_FALSE(client->HasCompositionText()); |
| 678 EXPECT_FALSE(input_method_->cancel_composition_called()); |
| 679 EXPECT_STR_EQ("0123456789", textfield_->text()); |
| 680 EXPECT_EQ(2, on_before_user_action_); |
| 681 EXPECT_EQ(2, on_after_user_action_); |
| 682 |
| 683 input_method_->Clear(); |
| 684 input_method_->SetCompositionTextForNextKey(composition); |
| 685 textfield_->clear(); |
| 686 SendKeyEvent(ui::VKEY_A); |
| 687 EXPECT_TRUE(client->HasCompositionText()); |
| 688 EXPECT_STR_EQ("0123321456789", textfield_->text()); |
| 689 |
| 690 on_before_user_action_ = on_after_user_action_ = 0; |
| 691 textfield_->clear(); |
| 692 SendKeyEvent(ui::VKEY_RIGHT); |
| 693 EXPECT_FALSE(client->HasCompositionText()); |
| 694 EXPECT_TRUE(input_method_->cancel_composition_called()); |
| 695 EXPECT_TRUE(textfield_->key_received()); |
| 696 EXPECT_TRUE(textfield_->key_handled()); |
| 697 EXPECT_STR_EQ("0123321456789", textfield_->text()); |
| 698 EXPECT_EQ(8U, textfield_->GetCursorPosition()); |
| 699 EXPECT_EQ(1, on_before_user_action_); |
| 700 EXPECT_EQ(1, on_after_user_action_); |
| 701 |
| 702 input_method_->Clear(); |
| 703 textfield_->SetReadOnly(true); |
| 704 EXPECT_TRUE(input_method_->text_input_type_changed()); |
| 705 EXPECT_FALSE(textfield_->GetTextInputClient()); |
| 706 |
| 707 textfield_->SetReadOnly(false); |
| 708 input_method_->Clear(); |
| 709 textfield_->SetPassword(true); |
| 710 EXPECT_TRUE(input_method_->text_input_type_changed()); |
| 711 EXPECT_TRUE(textfield_->GetTextInputClient()); |
| 712 } |
| 713 |
| 714 |
520 } // namespace views | 715 } // namespace views |
OLD | NEW |