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 <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" |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 ch == ' ' ? ui::VKEY_SPACE : | 241 ch == ' ' ? ui::VKEY_SPACE : |
242 static_cast<ui::KeyboardCode>(ui::VKEY_A + ch - 'a'); | 242 static_cast<ui::KeyboardCode>(ui::VKEY_A + ch - 'a'); |
243 SendKeyEvent(code); | 243 SendKeyEvent(code); |
244 } else { | 244 } else { |
245 KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, 0); | 245 KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, 0); |
246 event.set_character(ch); | 246 event.set_character(ch); |
247 input_method_->DispatchKeyEvent(event); | 247 input_method_->DispatchKeyEvent(event); |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
| 251 string16 GetClipboardText() const { |
| 252 string16 text; |
| 253 views::ViewsDelegate::views_delegate->GetClipboard()-> |
| 254 ReadText(ui::Clipboard::BUFFER_STANDARD, &text); |
| 255 return text; |
| 256 } |
| 257 |
| 258 void SetClipboardText(const std::string& text) { |
| 259 ui::ScopedClipboardWriter clipboard_writer( |
| 260 views::ViewsDelegate::views_delegate->GetClipboard()); |
| 261 clipboard_writer.WriteText(ASCIIToUTF16(text)); |
| 262 } |
| 263 |
251 View* GetFocusedView() { | 264 View* GetFocusedView() { |
252 return widget_->GetFocusManager()->GetFocusedView(); | 265 return widget_->GetFocusManager()->GetFocusedView(); |
253 } | 266 } |
254 | 267 |
255 int GetCursorPositionX(int cursor_pos) { | 268 int GetCursorPositionX(int cursor_pos) { |
256 gfx::RenderText* render_text = textfield_view_->GetRenderText(); | 269 gfx::RenderText* render_text = textfield_view_->GetRenderText(); |
257 return render_text->GetCursorBounds( | 270 return render_text->GetCursorBounds( |
258 gfx::SelectionModel(cursor_pos), false).x(); | 271 gfx::SelectionModel(cursor_pos), false).x(); |
259 } | 272 } |
260 | 273 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 MouseEvent release(ui::ET_MOUSE_RELEASED, 0, 0, | 308 MouseEvent release(ui::ET_MOUSE_RELEASED, 0, 0, |
296 ui::EF_LEFT_BUTTON_DOWN | ui::EF_IS_NON_CLIENT); | 309 ui::EF_LEFT_BUTTON_DOWN | ui::EF_IS_NON_CLIENT); |
297 textfield_view_->OnMouseReleased(release); | 310 textfield_view_->OnMouseReleased(release); |
298 } | 311 } |
299 | 312 |
300 // Wrap for visibility in test classes. | 313 // Wrap for visibility in test classes. |
301 ui::TextInputType GetTextInputType() { | 314 ui::TextInputType GetTextInputType() { |
302 return textfield_view_->GetTextInputType(); | 315 return textfield_view_->GetTextInputType(); |
303 } | 316 } |
304 | 317 |
| 318 void VerifyTextfieldContextMenuContents(bool textfield_has_selection, |
| 319 ui::MenuModel* menu_model) { |
| 320 EXPECT_TRUE(menu_model->IsEnabledAt(4 /* Separator */)); |
| 321 EXPECT_TRUE(menu_model->IsEnabledAt(5 /* SELECT ALL */)); |
| 322 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(0 /* CUT */)); |
| 323 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(1 /* COPY */)); |
| 324 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(3 /* DELETE */)); |
| 325 string16 str(GetClipboardText()); |
| 326 EXPECT_NE(str.empty(), menu_model->IsEnabledAt(2 /* PASTE */)); |
| 327 } |
| 328 |
305 // We need widget to populate wrapper class. | 329 // We need widget to populate wrapper class. |
306 Widget* widget_; | 330 Widget* widget_; |
307 | 331 |
308 TestTextfield* textfield_; | 332 TestTextfield* textfield_; |
309 NativeTextfieldViews* textfield_view_; | 333 NativeTextfieldViews* textfield_view_; |
310 TextfieldViewsModel* model_; | 334 TextfieldViewsModel* model_; |
311 | 335 |
312 // The string from Controller::ContentsChanged callback. | 336 // The string from Controller::ContentsChanged callback. |
313 string16 last_contents_; | 337 string16 last_contents_; |
314 | 338 |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 EXPECT_EQ(1, GetFocusedView()->id()); | 666 EXPECT_EQ(1, GetFocusedView()->id()); |
643 | 667 |
644 // Test if clicking on textfield view sets the focus to textfield_. | 668 // Test if clicking on textfield view sets the focus to textfield_. |
645 widget_->GetFocusManager()->AdvanceFocus(true); | 669 widget_->GetFocusManager()->AdvanceFocus(true); |
646 EXPECT_EQ(3, GetFocusedView()->id()); | 670 EXPECT_EQ(3, GetFocusedView()->id()); |
647 MouseEvent click(ui::ET_MOUSE_PRESSED, 0, 0, ui::EF_LEFT_BUTTON_DOWN); | 671 MouseEvent click(ui::ET_MOUSE_PRESSED, 0, 0, ui::EF_LEFT_BUTTON_DOWN); |
648 textfield_view_->OnMousePressed(click); | 672 textfield_view_->OnMousePressed(click); |
649 EXPECT_EQ(1, GetFocusedView()->id()); | 673 EXPECT_EQ(1, GetFocusedView()->id()); |
650 } | 674 } |
651 | 675 |
652 void VerifyTextfieldContextMenuContents(bool textfield_has_selection, | |
653 ui::MenuModel* menu_model) { | |
654 EXPECT_TRUE(menu_model->IsEnabledAt(4 /* Separator */)); | |
655 EXPECT_TRUE(menu_model->IsEnabledAt(5 /* SELECT ALL */)); | |
656 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(0 /* CUT */)); | |
657 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(1 /* COPY */)); | |
658 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(3 /* DELETE */)); | |
659 string16 str; | |
660 views::ViewsDelegate::views_delegate->GetClipboard()-> | |
661 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); | |
662 EXPECT_NE(str.empty(), menu_model->IsEnabledAt(2 /* PASTE */)); | |
663 } | |
664 | |
665 TEST_F(NativeTextfieldViewsTest, ContextMenuDisplayTest) { | 676 TEST_F(NativeTextfieldViewsTest, ContextMenuDisplayTest) { |
666 InitTextfield(Textfield::STYLE_DEFAULT); | 677 InitTextfield(Textfield::STYLE_DEFAULT); |
667 textfield_->SetText(ASCIIToUTF16("hello world")); | 678 textfield_->SetText(ASCIIToUTF16("hello world")); |
668 EXPECT_TRUE(GetContextMenuModel()); | 679 EXPECT_TRUE(GetContextMenuModel()); |
669 VerifyTextfieldContextMenuContents(false, GetContextMenuModel()); | 680 VerifyTextfieldContextMenuContents(false, GetContextMenuModel()); |
670 | 681 |
671 textfield_->SelectAll(); | 682 textfield_->SelectAll(); |
672 VerifyTextfieldContextMenuContents(true, GetContextMenuModel()); | 683 VerifyTextfieldContextMenuContents(true, GetContextMenuModel()); |
673 } | 684 } |
674 | 685 |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 EXPECT_EQ(9U, textfield_->GetCursorPosition()); | 988 EXPECT_EQ(9U, textfield_->GetCursorPosition()); |
978 | 989 |
979 SendKeyEvent(ui::VKEY_LEFT, true, true); | 990 SendKeyEvent(ui::VKEY_LEFT, true, true); |
980 EXPECT_EQ(5U, textfield_->GetCursorPosition()); | 991 EXPECT_EQ(5U, textfield_->GetCursorPosition()); |
981 EXPECT_STR_EQ("two ", textfield_->GetSelectedText()); | 992 EXPECT_STR_EQ("two ", textfield_->GetSelectedText()); |
982 | 993 |
983 textfield_->SelectAll(); | 994 textfield_->SelectAll(); |
984 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); | 995 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); |
985 | 996 |
986 // CUT&PASTE does not work, but COPY works | 997 // CUT&PASTE does not work, but COPY works |
| 998 SetClipboardText("Test"); |
987 SendKeyEvent(ui::VKEY_X, false, true); | 999 SendKeyEvent(ui::VKEY_X, false, true); |
988 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); | 1000 EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); |
989 string16 str; | 1001 string16 str(GetClipboardText()); |
990 views::ViewsDelegate::views_delegate->GetClipboard()-> | |
991 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); | |
992 EXPECT_STR_NE(" one two three ", str); | 1002 EXPECT_STR_NE(" one two three ", str); |
993 | 1003 |
994 SendKeyEvent(ui::VKEY_C, false, true); | 1004 SendKeyEvent(ui::VKEY_C, false, true); |
995 views::ViewsDelegate::views_delegate->GetClipboard()-> | 1005 views::ViewsDelegate::views_delegate->GetClipboard()-> |
996 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); | 1006 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); |
997 EXPECT_STR_EQ(" one two three ", str); | 1007 EXPECT_STR_EQ(" one two three ", str); |
998 | 1008 |
999 // SetText should work even in read only mode. | 1009 // SetText should work even in read only mode. |
1000 textfield_->SetText(ASCIIToUTF16(" four five six ")); | 1010 textfield_->SetText(ASCIIToUTF16(" four five six ")); |
1001 EXPECT_STR_EQ(" four five six ", textfield_->text()); | 1011 EXPECT_STR_EQ(" four five six ", textfield_->text()); |
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1545 EXPECT_EQ(0U, textfield_->GetCursorPosition()); | 1555 EXPECT_EQ(0U, textfield_->GetCursorPosition()); |
1546 #else | 1556 #else |
1547 EXPECT_EQ(500U, textfield_->GetCursorPosition()); | 1557 EXPECT_EQ(500U, textfield_->GetCursorPosition()); |
1548 #endif | 1558 #endif |
1549 | 1559 |
1550 // Reset locale. | 1560 // Reset locale. |
1551 base::i18n::SetICUDefaultLocale(locale); | 1561 base::i18n::SetICUDefaultLocale(locale); |
1552 } | 1562 } |
1553 | 1563 |
1554 } // namespace views | 1564 } // namespace views |
OLD | NEW |