OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "views/controls/textfield/native_textfield_views.h" | 9 #include "views/controls/textfield/native_textfield_views.h" |
10 #include "views/controls/textfield/textfield.h" | 10 #include "views/controls/textfield/textfield.h" |
11 #include "views/controls/textfield/textfield_views_model.h" | 11 #include "views/controls/textfield/textfield_views_model.h" |
12 #include "views/event.h" | 12 #include "views/event.h" |
| 13 #include "views/focus/focus_manager.h" |
13 #include "views/widget/widget.h" | 14 #include "views/widget/widget.h" |
14 | 15 |
15 namespace views { | 16 namespace views { |
16 | 17 |
17 #define EXPECT_STR_EQ(ascii, utf16) \ | 18 #define EXPECT_STR_EQ(ascii, utf16) \ |
18 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16)) | 19 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16)) |
19 | 20 |
20 // TODO(oshima): Move tests that are independent of TextfieldViews to | 21 // TODO(oshima): Move tests that are independent of TextfieldViews to |
21 // textfield_unittests.cc once we move the test utility functions | 22 // textfield_unittests.cc once we move the test utility functions |
22 // from chrome/browser/automation/ to app/test/. | 23 // from chrome/browser/automation/ to app/test/. |
(...skipping 26 matching lines...) Expand all Loading... |
49 } | 50 } |
50 | 51 |
51 virtual bool HandleKeyEvent(Textfield* sender, | 52 virtual bool HandleKeyEvent(Textfield* sender, |
52 const KeyEvent& key_event) { | 53 const KeyEvent& key_event) { |
53 | 54 |
54 // TODO(oshima): figure out how to test the keystroke. | 55 // TODO(oshima): figure out how to test the keystroke. |
55 return false; | 56 return false; |
56 } | 57 } |
57 | 58 |
58 void InitTextfield(Textfield::StyleFlags style) { | 59 void InitTextfield(Textfield::StyleFlags style) { |
| 60 InitTextfields(style, 1); |
| 61 } |
| 62 |
| 63 void InitTextfields(Textfield::StyleFlags style, int count) { |
59 ASSERT_FALSE(textfield_); | 64 ASSERT_FALSE(textfield_); |
60 textfield_ = new Textfield(style); | 65 textfield_ = new Textfield(style); |
61 textfield_->SetController(this); | 66 textfield_->SetController(this); |
62 widget_ = Widget::CreatePopupWidget( | 67 widget_ = Widget::CreatePopupWidget( |
63 Widget::NotTransparent, | 68 Widget::NotTransparent, |
64 Widget::AcceptEvents, | 69 Widget::AcceptEvents, |
65 Widget::DeleteOnDestroy, | 70 Widget::DeleteOnDestroy, |
66 Widget::DontMirrorOriginInRTL); | 71 Widget::DontMirrorOriginInRTL); |
67 widget_->Init(NULL, gfx::Rect()); | 72 widget_->Init(NULL, gfx::Rect()); |
68 widget_->SetContentsView(textfield_); | 73 |
| 74 View* container = new View(); |
| 75 widget_->SetContentsView(container); |
| 76 container->AddChildView(textfield_); |
69 textfield_view_ | 77 textfield_view_ |
70 = static_cast<NativeTextfieldViews*>(textfield_->native_wrapper()); | 78 = static_cast<NativeTextfieldViews*>(textfield_->native_wrapper()); |
| 79 textfield_->SetID(1); |
| 80 |
| 81 for (int i = 1; i < count; i++) { |
| 82 Textfield* textfield = new Textfield(style); |
| 83 container->AddChildView(textfield); |
| 84 textfield->SetID(i + 1); |
| 85 } |
| 86 |
71 DCHECK(textfield_view_); | 87 DCHECK(textfield_view_); |
72 model_ = textfield_view_->model_.get(); | 88 model_ = textfield_view_->model_.get(); |
73 } | 89 } |
74 | 90 |
| 91 protected: |
75 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code, | 92 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code, |
76 bool shift, | 93 bool shift, |
77 bool control, | 94 bool control, |
78 bool capslock) { | 95 bool capslock) { |
79 int flags = (shift ? KeyEvent::EF_SHIFT_DOWN : 0) | | 96 int flags = (shift ? KeyEvent::EF_SHIFT_DOWN : 0) | |
80 (control ? KeyEvent::EF_CONTROL_DOWN : 0) | | 97 (control ? KeyEvent::EF_CONTROL_DOWN : 0) | |
81 (capslock ? KeyEvent::EF_CAPS_LOCK_DOWN : 0); | 98 (capslock ? KeyEvent::EF_CAPS_LOCK_DOWN : 0); |
82 KeyEvent event(KeyEvent::ET_KEY_PRESSED, key_code, flags, 1, 0); | 99 KeyEvent event(KeyEvent::ET_KEY_PRESSED, key_code, flags, 1, 0); |
83 return textfield_view_->OnKeyPressed(event); | 100 return textfield_->OnKeyPressed(event); |
84 } | 101 } |
85 | 102 |
86 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code, | 103 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code, |
87 bool shift, | 104 bool shift, |
88 bool control) { | 105 bool control) { |
89 return SendKeyEventToTextfieldViews(key_code, shift, control, false); | 106 return SendKeyEventToTextfieldViews(key_code, shift, control, false); |
90 } | 107 } |
91 | 108 |
92 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code) { | 109 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code) { |
93 return SendKeyEventToTextfieldViews(key_code, false, false); | 110 return SendKeyEventToTextfieldViews(key_code, false, false); |
94 } | 111 } |
95 | 112 |
96 protected: | 113 View* GetFocusedView() { |
| 114 return widget_->GetFocusManager()->GetFocusedView(); |
| 115 } |
| 116 |
97 // We need widget to populate wrapper class. | 117 // We need widget to populate wrapper class. |
98 Widget* widget_; | 118 Widget* widget_; |
99 | 119 |
100 Textfield* textfield_; | 120 Textfield* textfield_; |
101 NativeTextfieldViews* textfield_view_; | 121 NativeTextfieldViews* textfield_view_; |
102 TextfieldViewsModel* model_; | 122 TextfieldViewsModel* model_; |
103 | 123 |
104 // A fake message loop for view's drawing events. | 124 // A fake message loop for view's drawing events. |
105 MessageLoop message_loop_; | 125 MessageLoop message_loop_; |
106 | 126 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 | 254 |
235 TEST_F(NativeTextfieldViewsTest, PasswordTest) { | 255 TEST_F(NativeTextfieldViewsTest, PasswordTest) { |
236 InitTextfield(Textfield::STYLE_PASSWORD); | 256 InitTextfield(Textfield::STYLE_PASSWORD); |
237 textfield_->SetText(ASCIIToUTF16("my password")); | 257 textfield_->SetText(ASCIIToUTF16("my password")); |
238 // Just to make sure the text() and callback returns | 258 // Just to make sure the text() and callback returns |
239 // the actual text instead of "*". | 259 // the actual text instead of "*". |
240 EXPECT_STR_EQ("my password", textfield_->text()); | 260 EXPECT_STR_EQ("my password", textfield_->text()); |
241 EXPECT_STR_EQ("my password", last_contents_); | 261 EXPECT_STR_EQ("my password", last_contents_); |
242 } | 262 } |
243 | 263 |
244 TEST_F(NativeTextfieldViewsTest, TestOnKeyPressReturnValue) { | 264 TEST_F(NativeTextfieldViewsTest, OnKeyPressReturnValueTest) { |
245 InitTextfield(Textfield::STYLE_DEFAULT); | 265 InitTextfield(Textfield::STYLE_DEFAULT); |
246 EXPECT_TRUE(SendKeyEventToTextfieldViews(app::VKEY_A)); | 266 EXPECT_TRUE(SendKeyEventToTextfieldViews(app::VKEY_A)); |
247 // F24, up/down key won't be handled. | 267 // F24, up/down key won't be handled. |
248 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_F24)); | 268 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_F24)); |
249 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_UP)); | 269 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_UP)); |
250 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_DOWN)); | 270 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_DOWN)); |
251 } | 271 } |
252 | 272 |
253 TEST_F(NativeTextfieldViewsTest, CursorMovement) { | 273 TEST_F(NativeTextfieldViewsTest, CursorMovement) { |
254 InitTextfield(Textfield::STYLE_DEFAULT); | 274 InitTextfield(Textfield::STYLE_DEFAULT); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 | 313 |
294 // Ctrl+Left to move the cursor to the beginning of the first word. | 314 // Ctrl+Left to move the cursor to the beginning of the first word. |
295 SendKeyEventToTextfieldViews(app::VKEY_LEFT, false, true); | 315 SendKeyEventToTextfieldViews(app::VKEY_LEFT, false, true); |
296 // Ctrl+Left again should move the cursor back to the very beginning. | 316 // Ctrl+Left again should move the cursor back to the very beginning. |
297 SendKeyEventToTextfieldViews(app::VKEY_LEFT, false, true); | 317 SendKeyEventToTextfieldViews(app::VKEY_LEFT, false, true); |
298 SendKeyEventToTextfieldViews(app::VKEY_DELETE); | 318 SendKeyEventToTextfieldViews(app::VKEY_DELETE); |
299 EXPECT_STR_EQ("one two", textfield_->text()); | 319 EXPECT_STR_EQ("one two", textfield_->text()); |
300 EXPECT_STR_EQ("one two", last_contents_); | 320 EXPECT_STR_EQ("one two", last_contents_); |
301 } | 321 } |
302 | 322 |
| 323 TEST_F(NativeTextfieldViewsTest, FocusTraversalTest) { |
| 324 InitTextfields(Textfield::STYLE_DEFAULT, 3); |
| 325 textfield_->RequestFocus(); |
| 326 |
| 327 EXPECT_EQ(1, GetFocusedView()->GetID()); |
| 328 widget_->GetFocusManager()->AdvanceFocus(false); |
| 329 EXPECT_EQ(2, GetFocusedView()->GetID()); |
| 330 widget_->GetFocusManager()->AdvanceFocus(false); |
| 331 EXPECT_EQ(3, GetFocusedView()->GetID()); |
| 332 // Cycle back to the first textfield. |
| 333 widget_->GetFocusManager()->AdvanceFocus(false); |
| 334 EXPECT_EQ(1, GetFocusedView()->GetID()); |
| 335 |
| 336 widget_->GetFocusManager()->AdvanceFocus(true); |
| 337 EXPECT_EQ(3, GetFocusedView()->GetID()); |
| 338 widget_->GetFocusManager()->AdvanceFocus(true); |
| 339 EXPECT_EQ(2, GetFocusedView()->GetID()); |
| 340 widget_->GetFocusManager()->AdvanceFocus(true); |
| 341 EXPECT_EQ(1, GetFocusedView()->GetID()); |
| 342 // Cycle back to the last textfield. |
| 343 widget_->GetFocusManager()->AdvanceFocus(true); |
| 344 EXPECT_EQ(3, GetFocusedView()->GetID()); |
| 345 |
| 346 // Request focus should still work. |
| 347 textfield_->RequestFocus(); |
| 348 EXPECT_EQ(1, GetFocusedView()->GetID()); |
| 349 } |
| 350 |
303 } // namespace views | 351 } // namespace views |
OLD | NEW |