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