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" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 widget_->Init(NULL, gfx::Rect()); | 66 widget_->Init(NULL, gfx::Rect()); |
67 widget_->SetContentsView(textfield_); | 67 widget_->SetContentsView(textfield_); |
68 textfield_view_ | 68 textfield_view_ |
69 = static_cast<NativeTextfieldViews*>(textfield_->native_wrapper()); | 69 = static_cast<NativeTextfieldViews*>(textfield_->native_wrapper()); |
70 DCHECK(textfield_view_); | 70 DCHECK(textfield_view_); |
71 model_ = textfield_view_->model_.get(); | 71 model_ = textfield_view_->model_.get(); |
72 } | 72 } |
73 | 73 |
74 void SendKeyEventToTextfieldViews(app::KeyboardCode key_code, | 74 void SendKeyEventToTextfieldViews(app::KeyboardCode key_code, |
75 bool shift, | 75 bool shift, |
| 76 bool control, |
| 77 bool capslock) { |
| 78 int flags = (shift ? KeyEvent::EF_SHIFT_DOWN : 0) | |
| 79 (control ? KeyEvent::EF_CONTROL_DOWN : 0) | |
| 80 (capslock ? KeyEvent::EF_CAPS_LOCK_DOWN : 0); |
| 81 KeyEvent event(KeyEvent::ET_KEY_PRESSED, key_code, flags, 1, 0); |
| 82 textfield_view_->OnKeyPressed(event); |
| 83 } |
| 84 |
| 85 void SendKeyEventToTextfieldViews(app::KeyboardCode key_code, |
| 86 bool shift, |
76 bool control) { | 87 bool control) { |
77 int flags = (shift ? KeyEvent::EF_SHIFT_DOWN : 0) | | 88 SendKeyEventToTextfieldViews(key_code, shift, control, false); |
78 (control ? KeyEvent::EF_CONTROL_DOWN : 0); | |
79 KeyEvent event(KeyEvent::ET_KEY_PRESSED, key_code, flags, 1, 0); | |
80 textfield_view_->OnKeyPressed(event); | |
81 } | 89 } |
82 | |
83 void SendKeyEventToTextfieldViews(app::KeyboardCode key_code) { | 90 void SendKeyEventToTextfieldViews(app::KeyboardCode key_code) { |
84 SendKeyEventToTextfieldViews(key_code, false, false); | 91 SendKeyEventToTextfieldViews(key_code, false, false, false); |
85 } | 92 } |
86 | 93 |
87 protected: | 94 protected: |
88 // We need widget to populate wrapper class. | 95 // We need widget to populate wrapper class. |
89 Widget* widget_; | 96 Widget* widget_; |
90 | 97 |
91 Textfield* textfield_; | 98 Textfield* textfield_; |
92 NativeTextfieldViews* textfield_view_; | 99 NativeTextfieldViews* textfield_view_; |
93 TextfieldViewsModel* model_; | 100 TextfieldViewsModel* model_; |
94 | 101 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 TEST_F(NativeTextfieldViewsTest, KeyTest) { | 140 TEST_F(NativeTextfieldViewsTest, KeyTest) { |
134 InitTextfield(Textfield::STYLE_DEFAULT); | 141 InitTextfield(Textfield::STYLE_DEFAULT); |
135 SendKeyEventToTextfieldViews(app::VKEY_C, true, false); | 142 SendKeyEventToTextfieldViews(app::VKEY_C, true, false); |
136 EXPECT_STR_EQ("C", textfield_->text()); | 143 EXPECT_STR_EQ("C", textfield_->text()); |
137 EXPECT_STR_EQ("C", last_contents_); | 144 EXPECT_STR_EQ("C", last_contents_); |
138 last_contents_.clear(); | 145 last_contents_.clear(); |
139 | 146 |
140 SendKeyEventToTextfieldViews(app::VKEY_R, false, false); | 147 SendKeyEventToTextfieldViews(app::VKEY_R, false, false); |
141 EXPECT_STR_EQ("Cr", textfield_->text()); | 148 EXPECT_STR_EQ("Cr", textfield_->text()); |
142 EXPECT_STR_EQ("Cr", last_contents_); | 149 EXPECT_STR_EQ("Cr", last_contents_); |
| 150 |
| 151 textfield_->SetText(ASCIIToUTF16("")); |
| 152 SendKeyEventToTextfieldViews(app::VKEY_C, true, false, true); |
| 153 SendKeyEventToTextfieldViews(app::VKEY_C, false, false, true); |
| 154 SendKeyEventToTextfieldViews(app::VKEY_1, false, false, true); |
| 155 SendKeyEventToTextfieldViews(app::VKEY_1, true, false, true); |
| 156 SendKeyEventToTextfieldViews(app::VKEY_1, true, false, false); |
| 157 EXPECT_STR_EQ("cC1!!", textfield_->text()); |
| 158 EXPECT_STR_EQ("cC1!!", last_contents_); |
143 } | 159 } |
144 | 160 |
145 TEST_F(NativeTextfieldViewsTest, ControlAndSelectTest) { | 161 TEST_F(NativeTextfieldViewsTest, ControlAndSelectTest) { |
146 // Insert a test string in a textfield. | 162 // Insert a test string in a textfield. |
147 InitTextfield(Textfield::STYLE_DEFAULT); | 163 InitTextfield(Textfield::STYLE_DEFAULT); |
148 textfield_->SetText(ASCIIToUTF16("one two three")); | 164 textfield_->SetText(ASCIIToUTF16("one two three")); |
149 SendKeyEventToTextfieldViews(app::VKEY_RIGHT, | 165 SendKeyEventToTextfieldViews(app::VKEY_RIGHT, |
150 true /* shift */, false /* control */); | 166 true /* shift */, false /* control */); |
151 SendKeyEventToTextfieldViews(app::VKEY_RIGHT, true, false); | 167 SendKeyEventToTextfieldViews(app::VKEY_RIGHT, true, false); |
152 SendKeyEventToTextfieldViews(app::VKEY_RIGHT, true, false); | 168 SendKeyEventToTextfieldViews(app::VKEY_RIGHT, true, false); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 TEST_F(NativeTextfieldViewsTest, PasswordTest) { | 233 TEST_F(NativeTextfieldViewsTest, PasswordTest) { |
218 InitTextfield(Textfield::STYLE_PASSWORD); | 234 InitTextfield(Textfield::STYLE_PASSWORD); |
219 textfield_->SetText(ASCIIToUTF16("my password")); | 235 textfield_->SetText(ASCIIToUTF16("my password")); |
220 // Just to make sure the text() and callback returns | 236 // Just to make sure the text() and callback returns |
221 // the actual text instead of "*". | 237 // the actual text instead of "*". |
222 EXPECT_STR_EQ("my password", textfield_->text()); | 238 EXPECT_STR_EQ("my password", textfield_->text()); |
223 EXPECT_STR_EQ("my password", last_contents_); | 239 EXPECT_STR_EQ("my password", last_contents_); |
224 } | 240 } |
225 | 241 |
226 } // namespace views | 242 } // namespace views |
OLD | NEW |