| 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::CreatePopupWidget(params); | 159 widget_ = Widget::CreatePopupWidget(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_->OnFocusIn(); |
| 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_EQ(string16(), last_contents_); |
| 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(); | |
| 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_); | 258 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_EQ(string16(), last_contents_); |
| 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); |
| 305 textfield_->SetText(ASCIIToUTF16("my password")); | 393 textfield_->SetText(ASCIIToUTF16("my password")); |
| 306 // Just to make sure the text() and callback returns | 394 // Just to make sure the text() and callback returns |
| 307 // the actual text instead of "*". | 395 // the actual text instead of "*". |
| 308 EXPECT_STR_EQ("my password", textfield_->text()); | 396 EXPECT_STR_EQ("my password", textfield_->text()); |
| 309 EXPECT_STR_EQ("my password", last_contents_); | |
| 310 } | 397 } |
| 311 | 398 |
| 312 TEST_F(NativeTextfieldViewsTest, OnKeyPressReturnValueTest) { | 399 TEST_F(NativeTextfieldViewsTest, OnKeyPressReturnValueTest) { |
| 313 InitTextfield(Textfield::STYLE_DEFAULT); | 400 InitTextfield(Textfield::STYLE_DEFAULT); |
| 314 EXPECT_TRUE(SendKeyEventToTextfieldViews(ui::VKEY_A)); | 401 |
| 402 // Character keys will be handled by input method. |
| 403 SendKeyEvent(ui::VKEY_A); |
| 404 EXPECT_TRUE(textfield_->key_received()); |
| 405 EXPECT_FALSE(textfield_->key_handled()); |
| 406 textfield_->clear(); |
| 407 |
| 408 // Home will be handled. |
| 409 SendKeyEvent(ui::VKEY_HOME); |
| 410 EXPECT_TRUE(textfield_->key_received()); |
| 411 EXPECT_TRUE(textfield_->key_handled()); |
| 412 textfield_->clear(); |
| 413 |
| 315 // F24, up/down key won't be handled. | 414 // F24, up/down key won't be handled. |
| 316 EXPECT_FALSE(SendKeyEventToTextfieldViews(ui::VKEY_F24)); | 415 SendKeyEvent(ui::VKEY_F24); |
| 317 EXPECT_FALSE(SendKeyEventToTextfieldViews(ui::VKEY_UP)); | 416 EXPECT_TRUE(textfield_->key_received()); |
| 318 EXPECT_FALSE(SendKeyEventToTextfieldViews(ui::VKEY_DOWN)); | 417 EXPECT_FALSE(textfield_->key_handled()); |
| 418 textfield_->clear(); |
| 419 |
| 420 SendKeyEvent(ui::VKEY_UP); |
| 421 EXPECT_TRUE(textfield_->key_received()); |
| 422 EXPECT_FALSE(textfield_->key_handled()); |
| 423 textfield_->clear(); |
| 424 |
| 425 SendKeyEvent(ui::VKEY_DOWN); |
| 426 EXPECT_TRUE(textfield_->key_received()); |
| 427 EXPECT_FALSE(textfield_->key_handled()); |
| 319 } | 428 } |
| 320 | 429 |
| 321 TEST_F(NativeTextfieldViewsTest, CursorMovement) { | 430 TEST_F(NativeTextfieldViewsTest, CursorMovement) { |
| 322 InitTextfield(Textfield::STYLE_DEFAULT); | 431 InitTextfield(Textfield::STYLE_DEFAULT); |
| 323 | 432 |
| 324 // Test with trailing whitespace. | 433 // Test with trailing whitespace. |
| 325 textfield_->SetText(ASCIIToUTF16("one two hre ")); | 434 textfield_->SetText(ASCIIToUTF16("one two hre ")); |
| 326 | 435 |
| 327 // Send the cursor at the end. | 436 // Send the cursor at the end. |
| 328 SendKeyEventToTextfieldViews(ui::VKEY_END); | 437 SendKeyEvent(ui::VKEY_END); |
| 329 | 438 |
| 330 // Ctrl+Left should move the cursor just before the last word. | 439 // Ctrl+Left should move the cursor just before the last word. |
| 331 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true); | 440 SendKeyEvent(ui::VKEY_LEFT, false, true); |
| 332 SendKeyEventToTextfieldViews(ui::VKEY_T); | 441 SendKeyEvent(ui::VKEY_T); |
| 333 EXPECT_STR_EQ("one two thre ", textfield_->text()); | 442 EXPECT_STR_EQ("one two thre ", textfield_->text()); |
| 334 EXPECT_STR_EQ("one two thre ", last_contents_); | 443 EXPECT_STR_EQ("one two thre ", last_contents_); |
| 335 | 444 |
| 336 // Ctrl+Right should move the cursor to the end of the last word. | 445 // Ctrl+Right should move the cursor to the end of the last word. |
| 337 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, false, true); | 446 SendKeyEvent(ui::VKEY_RIGHT, false, true); |
| 338 SendKeyEventToTextfieldViews(ui::VKEY_E); | 447 SendKeyEvent(ui::VKEY_E); |
| 339 EXPECT_STR_EQ("one two three ", textfield_->text()); | 448 EXPECT_STR_EQ("one two three ", textfield_->text()); |
| 340 EXPECT_STR_EQ("one two three ", last_contents_); | 449 EXPECT_STR_EQ("one two three ", last_contents_); |
| 341 | 450 |
| 342 // Ctrl+Right again should move the cursor to the end. | 451 // Ctrl+Right again should move the cursor to the end. |
| 343 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, false, true); | 452 SendKeyEvent(ui::VKEY_RIGHT, false, true); |
| 344 SendKeyEventToTextfieldViews(ui::VKEY_BACK); | 453 SendKeyEvent(ui::VKEY_BACK); |
| 345 EXPECT_STR_EQ("one two three", textfield_->text()); | 454 EXPECT_STR_EQ("one two three", textfield_->text()); |
| 346 EXPECT_STR_EQ("one two three", last_contents_); | 455 EXPECT_STR_EQ("one two three", last_contents_); |
| 347 | 456 |
| 348 // Test with leading whitespace. | 457 // Test with leading whitespace. |
| 349 textfield_->SetText(ASCIIToUTF16(" ne two")); | 458 textfield_->SetText(ASCIIToUTF16(" ne two")); |
| 350 | 459 |
| 351 // Send the cursor at the beginning. | 460 // Send the cursor at the beginning. |
| 352 SendKeyEventToTextfieldViews(ui::VKEY_HOME); | 461 SendKeyEvent(ui::VKEY_HOME); |
| 353 | 462 |
| 354 // Ctrl+Right, then Ctrl+Left should move the cursor to the beginning of the | 463 // Ctrl+Right, then Ctrl+Left should move the cursor to the beginning of the |
| 355 // first word. | 464 // first word. |
| 356 SendKeyEventToTextfieldViews(ui::VKEY_RIGHT, false, true); | 465 SendKeyEvent(ui::VKEY_RIGHT, false, true); |
| 357 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true); | 466 SendKeyEvent(ui::VKEY_LEFT, false, true); |
| 358 SendKeyEventToTextfieldViews(ui::VKEY_O); | 467 SendKeyEvent(ui::VKEY_O); |
| 359 EXPECT_STR_EQ(" one two", textfield_->text()); | 468 EXPECT_STR_EQ(" one two", textfield_->text()); |
| 360 EXPECT_STR_EQ(" one two", last_contents_); | 469 EXPECT_STR_EQ(" one two", last_contents_); |
| 361 | 470 |
| 362 // Ctrl+Left to move the cursor to the beginning of the first word. | 471 // Ctrl+Left to move the cursor to the beginning of the first word. |
| 363 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true); | 472 SendKeyEvent(ui::VKEY_LEFT, false, true); |
| 364 // Ctrl+Left again should move the cursor back to the very beginning. | 473 // Ctrl+Left again should move the cursor back to the very beginning. |
| 365 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true); | 474 SendKeyEvent(ui::VKEY_LEFT, false, true); |
| 366 SendKeyEventToTextfieldViews(ui::VKEY_DELETE); | 475 SendKeyEvent(ui::VKEY_DELETE); |
| 367 EXPECT_STR_EQ("one two", textfield_->text()); | 476 EXPECT_STR_EQ("one two", textfield_->text()); |
| 368 EXPECT_STR_EQ("one two", last_contents_); | 477 EXPECT_STR_EQ("one two", last_contents_); |
| 369 } | 478 } |
| 370 | 479 |
| 371 #if defined(OS_WIN) | 480 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); | 481 InitTextfields(Textfield::STYLE_DEFAULT, 3); |
| 381 textfield_->RequestFocus(); | 482 textfield_->RequestFocus(); |
| 382 | 483 |
| 383 EXPECT_EQ(1, GetFocusedView()->GetID()); | 484 EXPECT_EQ(1, GetFocusedView()->GetID()); |
| 384 widget_->GetFocusManager()->AdvanceFocus(false); | 485 widget_->GetFocusManager()->AdvanceFocus(false); |
| 385 EXPECT_EQ(2, GetFocusedView()->GetID()); | 486 EXPECT_EQ(2, GetFocusedView()->GetID()); |
| 386 widget_->GetFocusManager()->AdvanceFocus(false); | 487 widget_->GetFocusManager()->AdvanceFocus(false); |
| 387 EXPECT_EQ(3, GetFocusedView()->GetID()); | 488 EXPECT_EQ(3, GetFocusedView()->GetID()); |
| 388 // Cycle back to the first textfield. | 489 // Cycle back to the first textfield. |
| 389 widget_->GetFocusManager()->AdvanceFocus(false); | 490 widget_->GetFocusManager()->AdvanceFocus(false); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 } | 558 } |
| 458 | 559 |
| 459 TEST_F(NativeTextfieldViewsTest, ReadOnlyTest) { | 560 TEST_F(NativeTextfieldViewsTest, ReadOnlyTest) { |
| 460 scoped_ptr<TestViewsDelegate> test_views_delegate(new TestViewsDelegate()); | 561 scoped_ptr<TestViewsDelegate> test_views_delegate(new TestViewsDelegate()); |
| 461 AutoReset<views::ViewsDelegate*> auto_reset( | 562 AutoReset<views::ViewsDelegate*> auto_reset( |
| 462 &views::ViewsDelegate::views_delegate, test_views_delegate.get()); | 563 &views::ViewsDelegate::views_delegate, test_views_delegate.get()); |
| 463 | 564 |
| 464 InitTextfield(Textfield::STYLE_DEFAULT); | 565 InitTextfield(Textfield::STYLE_DEFAULT); |
| 465 textfield_->SetText(ASCIIToUTF16(" one two three ")); | 566 textfield_->SetText(ASCIIToUTF16(" one two three ")); |
| 466 textfield_->SetReadOnly(true); | 567 textfield_->SetReadOnly(true); |
| 467 SendKeyEventToTextfieldViews(ui::VKEY_HOME); | 568 SendKeyEvent(ui::VKEY_HOME); |
| 468 EXPECT_EQ(0U, textfield_->GetCursorPosition()); | 569 EXPECT_EQ(0U, textfield_->GetCursorPosition()); |
| 469 | 570 |
| 470 SendKeyEventToTextfieldViews(ui::VKEY_END); | 571 SendKeyEvent(ui::VKEY_END); |
| 471 EXPECT_EQ(15U, textfield_->GetCursorPosition()); | 572 EXPECT_EQ(15U, textfield_->GetCursorPosition()); |
| 472 | 573 |
| 473 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, false); | 574 SendKeyEvent(ui::VKEY_LEFT, false, false); |
| 474 EXPECT_EQ(14U, textfield_->GetCursorPosition()); | 575 EXPECT_EQ(14U, textfield_->GetCursorPosition()); |
| 475 | 576 |
| 476 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, false, true); | 577 SendKeyEvent(ui::VKEY_LEFT, false, true); |
| 477 EXPECT_EQ(9U, textfield_->GetCursorPosition()); | 578 EXPECT_EQ(9U, textfield_->GetCursorPosition()); |
| 478 | 579 |
| 479 SendKeyEventToTextfieldViews(ui::VKEY_LEFT, true, true); | 580 SendKeyEvent(ui::VKEY_LEFT, true, true); |
| 480 EXPECT_EQ(5U, textfield_->GetCursorPosition()); | 581 EXPECT_EQ(5U, textfield_->GetCursorPosition()); |
| 481 EXPECT_STR_EQ("two ", textfield_->GetSelectedText()); | 582 EXPECT_STR_EQ("two ", textfield_->GetSelectedText()); |
| 482 | 583 |
| 483 textfield_->SelectAll(); | 584 textfield_->SelectAll(); |
| 484 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); | 585 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); |
| 485 | 586 |
| 486 // CUT&PASTE does not work, but COPY works | 587 // CUT&PASTE does not work, but COPY works |
| 487 SendKeyEventToTextfieldViews(ui::VKEY_X, false, true); | 588 SendKeyEvent(ui::VKEY_X, false, true); |
| 488 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); | 589 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); |
| 489 string16 str; | 590 string16 str; |
| 490 views::ViewsDelegate::views_delegate->GetClipboard()-> | 591 views::ViewsDelegate::views_delegate->GetClipboard()-> |
| 491 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); | 592 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); |
| 492 EXPECT_STR_NE(" one two three ", str); | 593 EXPECT_STR_NE(" one two three ", str); |
| 493 | 594 |
| 494 SendKeyEventToTextfieldViews(ui::VKEY_C, false, true); | 595 SendKeyEvent(ui::VKEY_C, false, true); |
| 495 views::ViewsDelegate::views_delegate->GetClipboard()-> | 596 views::ViewsDelegate::views_delegate->GetClipboard()-> |
| 496 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); | 597 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); |
| 497 EXPECT_STR_EQ(" one two three ", str); | 598 EXPECT_STR_EQ(" one two three ", str); |
| 498 | 599 |
| 499 // SetText should work even in read only mode. | 600 // SetText should work even in read only mode. |
| 500 textfield_->SetText(ASCIIToUTF16(" four five six ")); | 601 textfield_->SetText(ASCIIToUTF16(" four five six ")); |
| 501 EXPECT_STR_EQ(" four five six ", textfield_->text()); | 602 EXPECT_STR_EQ(" four five six ", textfield_->text()); |
| 502 | 603 |
| 503 // Paste shouldn't work. | 604 // Paste shouldn't work. |
| 504 SendKeyEventToTextfieldViews(ui::VKEY_V, false, true); | 605 SendKeyEvent(ui::VKEY_V, false, true); |
| 505 EXPECT_STR_EQ(" four five six ", textfield_->text()); | 606 EXPECT_STR_EQ(" four five six ", textfield_->text()); |
| 506 EXPECT_TRUE(textfield_->GetSelectedText().empty()); | 607 EXPECT_TRUE(textfield_->GetSelectedText().empty()); |
| 507 | 608 |
| 508 textfield_->SelectAll(); | 609 textfield_->SelectAll(); |
| 509 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); | 610 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); |
| 510 | 611 |
| 511 // Text field is unmodifiable and selection shouldn't change. | 612 // Text field is unmodifiable and selection shouldn't change. |
| 512 SendKeyEventToTextfieldViews(ui::VKEY_DELETE); | 613 SendKeyEvent(ui::VKEY_DELETE); |
| 513 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); | 614 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); |
| 514 SendKeyEventToTextfieldViews(ui::VKEY_BACK); | 615 SendKeyEvent(ui::VKEY_BACK); |
| 515 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); | 616 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); |
| 516 SendKeyEventToTextfieldViews(ui::VKEY_T); | 617 SendKeyEvent(ui::VKEY_T); |
| 517 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); | 618 EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); |
| 518 } | 619 } |
| 519 | 620 |
| 621 TEST_F(NativeTextfieldViewsTest, TextInputClientTest) { |
| 622 InitTextfield(Textfield::STYLE_DEFAULT); |
| 623 TextInputClient* client = textfield_->GetTextInputClient(); |
| 624 EXPECT_TRUE(client); |
| 625 EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT, client->GetTextInputType()); |
| 626 |
| 627 textfield_->SetText(ASCIIToUTF16("0123456789")); |
| 628 ui::Range range; |
| 629 EXPECT_TRUE(client->GetTextRange(&range)); |
| 630 EXPECT_EQ(0U, range.start()); |
| 631 EXPECT_EQ(10U, range.end()); |
| 632 |
| 633 EXPECT_TRUE(client->SetSelectionRange(ui::Range(1, 4))); |
| 634 EXPECT_TRUE(client->GetSelectionRange(&range)); |
| 635 EXPECT_EQ(ui::Range(1,4), range); |
| 636 |
| 637 // This code can't be compiled because of a bug in base::Callback. |
| 638 #if 0 |
| 639 GetTextHelper helper; |
| 640 base::Callback<void(string16)> callback = |
| 641 base::Bind(&GetTextHelper::set_text, base::Unretained(&helper)); |
| 642 |
| 643 EXPECT_TRUE(client->GetTextFromRange(range, callback)); |
| 644 EXPECT_STR_EQ("123", helper.text()); |
| 645 #endif |
| 646 |
| 647 EXPECT_TRUE(client->DeleteRange(range)); |
| 648 EXPECT_STR_EQ("0456789", textfield_->text()); |
| 649 |
| 650 ui::CompositionText composition; |
| 651 composition.text = UTF8ToUTF16("321"); |
| 652 // Set composition through input method. |
| 653 input_method_->Clear(); |
| 654 input_method_->SetCompositionTextForNextKey(composition); |
| 655 textfield_->clear(); |
| 656 |
| 657 on_before_user_action_ = on_after_user_action_ = 0; |
| 658 SendKeyEvent(ui::VKEY_A); |
| 659 EXPECT_TRUE(textfield_->key_received()); |
| 660 EXPECT_FALSE(textfield_->key_handled()); |
| 661 EXPECT_TRUE(client->HasCompositionText()); |
| 662 EXPECT_TRUE(client->GetCompositionTextRange(&range)); |
| 663 EXPECT_STR_EQ("0321456789", textfield_->text()); |
| 664 EXPECT_EQ(ui::Range(1,4), range); |
| 665 EXPECT_EQ(2, on_before_user_action_); |
| 666 EXPECT_EQ(2, on_after_user_action_); |
| 667 |
| 668 input_method_->SetResultTextForNextKey(UTF8ToUTF16("123")); |
| 669 on_before_user_action_ = on_after_user_action_ = 0; |
| 670 textfield_->clear(); |
| 671 SendKeyEvent(ui::VKEY_A); |
| 672 EXPECT_TRUE(textfield_->key_received()); |
| 673 EXPECT_FALSE(textfield_->key_handled()); |
| 674 EXPECT_FALSE(client->HasCompositionText()); |
| 675 EXPECT_FALSE(input_method_->cancel_composition_called()); |
| 676 EXPECT_STR_EQ("0123456789", textfield_->text()); |
| 677 EXPECT_EQ(2, on_before_user_action_); |
| 678 EXPECT_EQ(2, on_after_user_action_); |
| 679 |
| 680 input_method_->Clear(); |
| 681 input_method_->SetCompositionTextForNextKey(composition); |
| 682 textfield_->clear(); |
| 683 SendKeyEvent(ui::VKEY_A); |
| 684 EXPECT_TRUE(client->HasCompositionText()); |
| 685 EXPECT_STR_EQ("0123321456789", textfield_->text()); |
| 686 |
| 687 on_before_user_action_ = on_after_user_action_ = 0; |
| 688 textfield_->clear(); |
| 689 SendKeyEvent(ui::VKEY_RIGHT); |
| 690 EXPECT_FALSE(client->HasCompositionText()); |
| 691 EXPECT_TRUE(input_method_->cancel_composition_called()); |
| 692 EXPECT_TRUE(textfield_->key_received()); |
| 693 EXPECT_TRUE(textfield_->key_handled()); |
| 694 EXPECT_STR_EQ("0123321456789", textfield_->text()); |
| 695 EXPECT_EQ(8U, textfield_->GetCursorPosition()); |
| 696 EXPECT_EQ(1, on_before_user_action_); |
| 697 EXPECT_EQ(1, on_after_user_action_); |
| 698 |
| 699 input_method_->Clear(); |
| 700 textfield_->SetReadOnly(true); |
| 701 EXPECT_TRUE(input_method_->text_input_type_changed()); |
| 702 EXPECT_FALSE(textfield_->GetTextInputClient()); |
| 703 |
| 704 textfield_->SetReadOnly(false); |
| 705 input_method_->Clear(); |
| 706 textfield_->SetPassword(true); |
| 707 EXPECT_TRUE(input_method_->text_input_type_changed()); |
| 708 EXPECT_TRUE(textfield_->GetTextInputClient()); |
| 709 } |
| 710 |
| 711 |
| 520 } // namespace views | 712 } // namespace views |
| OLD | NEW |