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 25 matching lines...) Expand all Loading... | |
48 last_contents_ = new_contents; | 49 last_contents_ = new_contents; |
49 } | 50 } |
50 | 51 |
51 virtual bool HandleKeystroke(Textfield* sender, | 52 virtual bool HandleKeystroke(Textfield* sender, |
52 const Textfield::Keystroke& keystroke) { | 53 const Textfield::Keystroke& keystroke) { |
53 // TODO(oshima): figure out how to test the keystroke. | 54 // TODO(oshima): figure out how to test the keystroke. |
54 return false; | 55 return false; |
55 } | 56 } |
56 | 57 |
57 void InitTextfield(Textfield::StyleFlags style) { | 58 void InitTextfield(Textfield::StyleFlags style) { |
59 InitTextfield(style, 1); | |
60 } | |
61 | |
62 void InitTextfield(Textfield::StyleFlags style, int count) { | |
Jay Civelli
2011/01/06 00:55:54
May be it should be named: InitMultipleTextfields?
oshima
2011/01/06 01:31:22
renamed to InitTextfiels (Multiple sounded a bit v
| |
58 ASSERT_FALSE(textfield_); | 63 ASSERT_FALSE(textfield_); |
59 textfield_ = new Textfield(style); | 64 textfield_ = new Textfield(style); |
60 textfield_->SetController(this); | 65 textfield_->SetController(this); |
61 widget_ = Widget::CreatePopupWidget( | 66 widget_ = Widget::CreatePopupWidget( |
62 Widget::NotTransparent, | 67 Widget::NotTransparent, |
63 Widget::AcceptEvents, | 68 Widget::AcceptEvents, |
64 Widget::DeleteOnDestroy, | 69 Widget::DeleteOnDestroy, |
65 Widget::DontMirrorOriginInRTL); | 70 Widget::DontMirrorOriginInRTL); |
66 widget_->Init(NULL, gfx::Rect()); | 71 widget_->Init(NULL, gfx::Rect()); |
67 widget_->SetContentsView(textfield_); | 72 |
73 View* container = new View(); | |
74 widget_->SetContentsView(container); | |
75 container->AddChildView(textfield_); | |
68 textfield_view_ | 76 textfield_view_ |
69 = static_cast<NativeTextfieldViews*>(textfield_->native_wrapper()); | 77 = static_cast<NativeTextfieldViews*>(textfield_->native_wrapper()); |
78 textfield_->SetID(1); | |
79 | |
80 for (int i = 1; i < count; i++) { | |
81 Textfield* textfield = new Textfield(style); | |
82 container->AddChildView(textfield); | |
83 textfield->SetID(i + 1); | |
84 } | |
85 | |
70 DCHECK(textfield_view_); | 86 DCHECK(textfield_view_); |
71 model_ = textfield_view_->model_.get(); | 87 model_ = textfield_view_->model_.get(); |
72 } | 88 } |
73 | 89 |
90 protected: | |
74 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code, | 91 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code, |
75 bool shift, | 92 bool shift, |
76 bool control, | 93 bool control, |
77 bool capslock) { | 94 bool capslock) { |
78 int flags = (shift ? KeyEvent::EF_SHIFT_DOWN : 0) | | 95 int flags = (shift ? KeyEvent::EF_SHIFT_DOWN : 0) | |
79 (control ? KeyEvent::EF_CONTROL_DOWN : 0) | | 96 (control ? KeyEvent::EF_CONTROL_DOWN : 0) | |
80 (capslock ? KeyEvent::EF_CAPS_LOCK_DOWN : 0); | 97 (capslock ? KeyEvent::EF_CAPS_LOCK_DOWN : 0); |
81 KeyEvent event(KeyEvent::ET_KEY_PRESSED, key_code, flags, 1, 0); | 98 KeyEvent event(KeyEvent::ET_KEY_PRESSED, key_code, flags, 1, 0); |
82 return textfield_view_->OnKeyPressed(event); | 99 return textfield_->OnKeyPressed(event); |
83 } | 100 } |
84 | 101 |
85 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code, | 102 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code, |
86 bool shift, | 103 bool shift, |
87 bool control) { | 104 bool control) { |
88 return SendKeyEventToTextfieldViews(key_code, shift, control, false); | 105 return SendKeyEventToTextfieldViews(key_code, shift, control, false); |
89 } | 106 } |
90 | 107 |
91 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code) { | 108 bool SendKeyEventToTextfieldViews(app::KeyboardCode key_code) { |
92 return SendKeyEventToTextfieldViews(key_code, false, false); | 109 return SendKeyEventToTextfieldViews(key_code, false, false); |
93 } | 110 } |
94 | 111 |
95 protected: | 112 View* GetFocusedView() { |
113 return widget_->GetFocusManager()->GetFocusedView(); | |
114 } | |
115 | |
96 // We need widget to populate wrapper class. | 116 // We need widget to populate wrapper class. |
97 Widget* widget_; | 117 Widget* widget_; |
98 | 118 |
99 Textfield* textfield_; | 119 Textfield* textfield_; |
100 NativeTextfieldViews* textfield_view_; | 120 NativeTextfieldViews* textfield_view_; |
101 TextfieldViewsModel* model_; | 121 TextfieldViewsModel* model_; |
102 | 122 |
103 // A fake message loop for view's drawing events. | 123 // A fake message loop for view's drawing events. |
104 MessageLoop message_loop_; | 124 MessageLoop message_loop_; |
105 | 125 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 | 253 |
234 TEST_F(NativeTextfieldViewsTest, PasswordTest) { | 254 TEST_F(NativeTextfieldViewsTest, PasswordTest) { |
235 InitTextfield(Textfield::STYLE_PASSWORD); | 255 InitTextfield(Textfield::STYLE_PASSWORD); |
236 textfield_->SetText(ASCIIToUTF16("my password")); | 256 textfield_->SetText(ASCIIToUTF16("my password")); |
237 // Just to make sure the text() and callback returns | 257 // Just to make sure the text() and callback returns |
238 // the actual text instead of "*". | 258 // the actual text instead of "*". |
239 EXPECT_STR_EQ("my password", textfield_->text()); | 259 EXPECT_STR_EQ("my password", textfield_->text()); |
240 EXPECT_STR_EQ("my password", last_contents_); | 260 EXPECT_STR_EQ("my password", last_contents_); |
241 } | 261 } |
242 | 262 |
243 TEST_F(NativeTextfieldViewsTest, TestOnKeyPressReturnValue) { | 263 TEST_F(NativeTextfieldViewsTest, OnKeyPressReturnValueTest) { |
244 InitTextfield(Textfield::STYLE_DEFAULT); | 264 InitTextfield(Textfield::STYLE_DEFAULT); |
245 EXPECT_TRUE(SendKeyEventToTextfieldViews(app::VKEY_A)); | 265 EXPECT_TRUE(SendKeyEventToTextfieldViews(app::VKEY_A)); |
246 // F24, up/down key won't be handled. | 266 // F24, up/down key won't be handled. |
247 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_F24)); | 267 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_F24)); |
248 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_UP)); | 268 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_UP)); |
249 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_DOWN)); | 269 EXPECT_FALSE(SendKeyEventToTextfieldViews(app::VKEY_DOWN)); |
250 } | 270 } |
251 | 271 |
252 TEST_F(NativeTextfieldViewsTest, CursorMovement) { | 272 TEST_F(NativeTextfieldViewsTest, CursorMovement) { |
253 InitTextfield(Textfield::STYLE_DEFAULT); | 273 InitTextfield(Textfield::STYLE_DEFAULT); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 | 312 |
293 // Ctrl+Left to move the cursor to the beginning of the first word. | 313 // Ctrl+Left to move the cursor to the beginning of the first word. |
294 SendKeyEventToTextfieldViews(app::VKEY_LEFT, false, true); | 314 SendKeyEventToTextfieldViews(app::VKEY_LEFT, false, true); |
295 // Ctrl+Left again should move the cursor back to the very beginning. | 315 // Ctrl+Left again should move the cursor back to the very beginning. |
296 SendKeyEventToTextfieldViews(app::VKEY_LEFT, false, true); | 316 SendKeyEventToTextfieldViews(app::VKEY_LEFT, false, true); |
297 SendKeyEventToTextfieldViews(app::VKEY_DELETE); | 317 SendKeyEventToTextfieldViews(app::VKEY_DELETE); |
298 EXPECT_STR_EQ("one two", textfield_->text()); | 318 EXPECT_STR_EQ("one two", textfield_->text()); |
299 EXPECT_STR_EQ("one two", last_contents_); | 319 EXPECT_STR_EQ("one two", last_contents_); |
300 } | 320 } |
301 | 321 |
322 TEST_F(NativeTextfieldViewsTest, FocusTraversalTest) { | |
323 InitTextfield(Textfield::STYLE_DEFAULT, 3); | |
324 textfield_->RequestFocus(); | |
325 | |
326 EXPECT_EQ(1, GetFocusedView()->GetID()); | |
327 widget_->GetFocusManager()->AdvanceFocus(false); | |
328 EXPECT_EQ(2, GetFocusedView()->GetID()); | |
329 widget_->GetFocusManager()->AdvanceFocus(false); | |
330 EXPECT_EQ(3, GetFocusedView()->GetID()); | |
Jay Civelli
2011/01/06 00:55:54
May be you could advance focus one more time to ma
oshima
2011/01/06 01:31:22
Thank you for good suggestion. Added a few more ca
| |
331 | |
332 widget_->GetFocusManager()->AdvanceFocus(true); | |
333 EXPECT_EQ(2, GetFocusedView()->GetID()); | |
334 widget_->GetFocusManager()->AdvanceFocus(true); | |
335 EXPECT_EQ(1, GetFocusedView()->GetID()); | |
336 } | |
337 | |
302 } // namespace views | 338 } // namespace views |
OLD | NEW |