OLD | NEW |
1 // Copyright (c) 2010 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" |
| 6 #include "base/message_loop.h" |
5 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/base/clipboard/clipboard.h" |
6 #include "ui/base/keycodes/keyboard_codes.h" | 10 #include "ui/base/keycodes/keyboard_codes.h" |
| 11 #include "views/controls/menu/menu_2.h" |
7 #include "views/controls/textfield/native_textfield_views.h" | 12 #include "views/controls/textfield/native_textfield_views.h" |
8 #include "views/controls/textfield/textfield.h" | 13 #include "views/controls/textfield/textfield.h" |
9 #include "views/controls/textfield/textfield_views_model.h" | 14 #include "views/controls/textfield/textfield_views_model.h" |
10 #include "views/event.h" | 15 #include "views/event.h" |
11 #include "views/focus/focus_manager.h" | 16 #include "views/focus/focus_manager.h" |
| 17 #include "views/test/test_views_delegate.h" |
12 #include "views/test/views_test_base.h" | 18 #include "views/test/views_test_base.h" |
13 #include "views/widget/widget.h" | 19 #include "views/widget/widget.h" |
| 20 #include "views/views_delegate.h" |
14 | 21 |
15 namespace views { | 22 namespace views { |
16 | 23 |
17 #define EXPECT_STR_EQ(ascii, utf16) \ | 24 #define EXPECT_STR_EQ(ascii, utf16) \ |
18 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16)) | 25 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16)) |
19 | 26 |
20 // TODO(oshima): Move tests that are independent of TextfieldViews to | 27 // TODO(oshima): Move tests that are independent of TextfieldViews to |
21 // textfield_unittests.cc once we move the test utility functions | 28 // textfield_unittests.cc once we move the test utility functions |
22 // from chrome/browser/automation/ to app/test/. | 29 // from chrome/browser/automation/ to app/test/. |
23 class NativeTextfieldViewsTest : public ViewsTestBase, | 30 class NativeTextfieldViewsTest : public ViewsTestBase, |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 for (int i = 1; i < count; i++) { | 87 for (int i = 1; i < count; i++) { |
81 Textfield* textfield = new Textfield(style); | 88 Textfield* textfield = new Textfield(style); |
82 container->AddChildView(textfield); | 89 container->AddChildView(textfield); |
83 textfield->SetID(i + 1); | 90 textfield->SetID(i + 1); |
84 } | 91 } |
85 | 92 |
86 DCHECK(textfield_view_); | 93 DCHECK(textfield_view_); |
87 model_ = textfield_view_->model_.get(); | 94 model_ = textfield_view_->model_.get(); |
88 } | 95 } |
89 | 96 |
| 97 views::Menu2* GetContextMenu() { |
| 98 textfield_view_->InitContextMenuIfRequired(); |
| 99 return textfield_view_->context_menu_menu_.get(); |
| 100 } |
| 101 |
90 protected: | 102 protected: |
91 bool SendKeyEventToTextfieldViews(ui::KeyboardCode key_code, | 103 bool SendKeyEventToTextfieldViews(ui::KeyboardCode key_code, |
92 bool shift, | 104 bool shift, |
93 bool control, | 105 bool control, |
94 bool capslock) { | 106 bool capslock) { |
95 int flags = (shift ? KeyEvent::EF_SHIFT_DOWN : 0) | | 107 int flags = (shift ? KeyEvent::EF_SHIFT_DOWN : 0) | |
96 (control ? KeyEvent::EF_CONTROL_DOWN : 0) | | 108 (control ? KeyEvent::EF_CONTROL_DOWN : 0) | |
97 (capslock ? KeyEvent::EF_CAPS_LOCK_DOWN : 0); | 109 (capslock ? KeyEvent::EF_CAPS_LOCK_DOWN : 0); |
98 KeyEvent event(KeyEvent::ET_KEY_PRESSED, key_code, flags, 1, 0); | 110 KeyEvent event(KeyEvent::ET_KEY_PRESSED, key_code, flags, 1, 0); |
99 return textfield_->OnKeyPressed(event); | 111 return textfield_->OnKeyPressed(event); |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 EXPECT_EQ(1, GetFocusedView()->GetID()); | 389 EXPECT_EQ(1, GetFocusedView()->GetID()); |
378 // Cycle back to the last textfield. | 390 // Cycle back to the last textfield. |
379 widget_->GetFocusManager()->AdvanceFocus(true); | 391 widget_->GetFocusManager()->AdvanceFocus(true); |
380 EXPECT_EQ(3, GetFocusedView()->GetID()); | 392 EXPECT_EQ(3, GetFocusedView()->GetID()); |
381 | 393 |
382 // Request focus should still work. | 394 // Request focus should still work. |
383 textfield_->RequestFocus(); | 395 textfield_->RequestFocus(); |
384 EXPECT_EQ(1, GetFocusedView()->GetID()); | 396 EXPECT_EQ(1, GetFocusedView()->GetID()); |
385 } | 397 } |
386 | 398 |
| 399 void VerifyTextfieldContextMenuContents(bool textfield_has_selection, |
| 400 menus::MenuModel* menu_model) { |
| 401 EXPECT_TRUE(menu_model->IsEnabledAt(4 /* Separator */)); |
| 402 EXPECT_TRUE(menu_model->IsEnabledAt(5 /* SELECT ALL */)); |
| 403 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(0 /* CUT */)); |
| 404 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(1 /* COPY */)); |
| 405 EXPECT_EQ(textfield_has_selection, menu_model->IsEnabledAt(3 /* DELETE */)); |
| 406 string16 str; |
| 407 views::ViewsDelegate::views_delegate->GetClipboard() |
| 408 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &str); |
| 409 EXPECT_NE(str.empty(), menu_model->IsEnabledAt(2 /* PASTE */)); |
| 410 } |
| 411 |
| 412 TEST_F(NativeTextfieldViewsTest, ContextMenuDisplayTest) { |
| 413 scoped_ptr<TestViewsDelegate> test_views_delegate(new TestViewsDelegate()); |
| 414 AutoReset<views::ViewsDelegate*> auto_reset( |
| 415 &views::ViewsDelegate::views_delegate, test_views_delegate.get()); |
| 416 views::ViewsDelegate::views_delegate = test_views_delegate.get(); |
| 417 InitTextfield(Textfield::STYLE_DEFAULT); |
| 418 textfield_->SetText(ASCIIToUTF16("hello world")); |
| 419 EXPECT_TRUE(GetContextMenu()); |
| 420 VerifyTextfieldContextMenuContents(false, GetContextMenu()->model()); |
| 421 |
| 422 textfield_->SelectAll(); |
| 423 VerifyTextfieldContextMenuContents(true, GetContextMenu()->model()); |
| 424 } |
| 425 |
387 } // namespace views | 426 } // namespace views |
OLD | NEW |