OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "app/keyboard_codes.h" | |
6 #include "base/message_loop.h" | |
7 #include "base/utf_string_conversions.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 #include "views/controls/textfield/native_textfield_view.h" | |
10 #include "views/controls/textfield/textfield.h" | |
11 #include "views/controls/textfield/textfield_view_model.h" | |
12 #include "views/event.h" | |
13 #include "views/widget/widget.h" | |
14 | |
15 namespace views { | |
16 | |
17 #define EXPECT_STR_EQ(ascii, utf16) EXPECT_EQ(ASCIIToUTF16(ascii), utf16) | |
sky
2010/12/15 20:31:27
If this fails the output isn't very helpful (meani
oshima
2010/12/16 01:15:19
Done.
| |
18 | |
19 // TODO(oshima): Move tests that are independent of TextfieldView to | |
20 // textfield_unittests.cc once we move the test utility functions | |
21 // from chrome/browser/automation/ to app/test/. | |
22 class NativeTextfieldViewTest : public ::testing::Test, | |
23 public Textfield::Controller { | |
24 public: | |
25 NativeTextfieldViewTest() | |
26 : widget_(NULL), | |
27 textfield_(NULL), | |
28 textfield_view_(NULL), | |
29 model_(NULL), | |
30 message_loop_(MessageLoop::TYPE_UI) { | |
31 } | |
32 | |
33 // ::testing::Test overrides. | |
34 virtual void SetUp() { | |
35 NativeTextfieldView::SetEnableTextfieldView(true); | |
36 } | |
37 | |
38 virtual void TearDown() { | |
39 NativeTextfieldView::SetEnableTextfieldView(false); | |
40 if (widget_) | |
41 widget_->Close(); | |
42 } | |
43 | |
44 // Textfield::Controller implementation: | |
45 virtual void ContentsChanged(Textfield* sender, | |
46 const string16& new_contents){ | |
47 last_contents_ = new_contents; | |
48 } | |
49 | |
50 virtual bool HandleKeystroke(Textfield* sender, | |
51 const Textfield::Keystroke& keystroke) { | |
52 // TODO(oshima): figure out how to test the keystroke. | |
53 return false; | |
54 } | |
55 | |
56 void InitTextfield(Textfield::StyleFlags style) { | |
57 DCHECK(!textfield_); | |
58 textfield_ = new Textfield(style); | |
59 textfield_->SetController(this); | |
60 widget_ = Widget::CreatePopupWidget( | |
61 Widget::NotTransparent, | |
62 Widget::AcceptEvents, | |
63 Widget::DeleteOnDestroy, | |
64 Widget::DontMirrorOriginInRTL); | |
65 widget_->Init(NULL, gfx::Rect()); | |
66 widget_->SetContentsView(textfield_); | |
67 textfield_view_ | |
68 = static_cast<NativeTextfieldView*>(textfield_->native_wrapper()); | |
69 DCHECK(textfield_view_); | |
70 model_ = textfield_view_->model_.get(); | |
71 } | |
72 | |
73 void SendKeyEventToTextfieldView(app::KeyboardCode key_code, | |
74 bool shift, | |
75 bool control) { | |
76 int flags = (shift ? KeyEvent::EF_SHIFT_DOWN : 0) | | |
77 (control ? KeyEvent::EF_CONTROL_DOWN : 0); | |
78 KeyEvent event(KeyEvent::ET_KEY_PRESSED, key_code, flags, 1, 0); | |
79 textfield_view_->OnKeyPressed(event); | |
80 } | |
81 | |
82 void SendKeyEventToTextfieldView(app::KeyboardCode key_code) { | |
83 SendKeyEventToTextfieldView(key_code, false, false); | |
84 } | |
85 | |
86 protected: | |
87 // We need widget to populate wrapper class. | |
88 Widget* widget_; | |
89 | |
90 Textfield* textfield_; | |
91 NativeTextfieldView* textfield_view_; | |
92 TextfieldViewModel* model_; | |
93 | |
94 // A fake message loop for view's drawing events. | |
95 MessageLoop message_loop_; | |
96 | |
97 // The string from Controller::ContentsChanged callback. | |
98 string16 last_contents_; | |
99 | |
100 private: | |
101 DISALLOW_COPY_AND_ASSIGN(NativeTextfieldViewTest); | |
102 }; | |
103 | |
104 TEST_F(NativeTextfieldViewTest, ModelChangesTeset) { | |
105 InitTextfield(Textfield::STYLE_DEFAULT); | |
106 textfield_->SetText(ASCIIToUTF16("this is")); | |
107 | |
108 EXPECT_STR_EQ("this is", model_->text()); | |
109 EXPECT_STR_EQ("this is", last_contents_); | |
110 last_contents_.clear(); | |
111 | |
112 textfield_->AppendText(ASCIIToUTF16(" a test")); | |
113 EXPECT_STR_EQ("this is a test", model_->text()); | |
114 EXPECT_STR_EQ("this is a test", last_contents_); | |
115 last_contents_.clear(); | |
116 | |
117 // Cases where the callback should not be called. | |
118 textfield_->SetText(ASCIIToUTF16("this is a test")); | |
119 EXPECT_STR_EQ("this is a test", model_->text()); | |
120 EXPECT_EQ(string16(), last_contents_); | |
121 | |
122 textfield_->AppendText(string16()); | |
123 EXPECT_STR_EQ("this is a test", model_->text()); | |
124 EXPECT_EQ(string16(), last_contents_); | |
125 | |
126 EXPECT_EQ(string16(), textfield_->GetSelectedText()); | |
127 textfield_->SelectAll(); | |
128 EXPECT_STR_EQ("this is a test", textfield_->GetSelectedText()); | |
129 EXPECT_EQ(string16(), last_contents_); | |
130 } | |
131 | |
132 TEST_F(NativeTextfieldViewTest, KeyTest) { | |
133 InitTextfield(Textfield::STYLE_DEFAULT); | |
134 SendKeyEventToTextfieldView(app::VKEY_C, true, false); | |
135 EXPECT_STR_EQ("C", textfield_->text()); | |
136 EXPECT_STR_EQ("C", last_contents_); | |
137 last_contents_.clear(); | |
138 | |
139 SendKeyEventToTextfieldView(app::VKEY_R, false, false); | |
140 EXPECT_STR_EQ("Cr", textfield_->text()); | |
141 EXPECT_STR_EQ("Cr", last_contents_); | |
142 } | |
143 | |
144 TEST_F(NativeTextfieldViewTest, ControlAndSelectTest) { | |
145 // Insert a test string in a textfield. | |
146 InitTextfield(Textfield::STYLE_DEFAULT); | |
147 textfield_->SetText(ASCIIToUTF16("one two three")); | |
148 SendKeyEventToTextfieldView(app::VKEY_RIGHT, | |
149 true /* shift */, false /* control */); | |
150 SendKeyEventToTextfieldView(app::VKEY_RIGHT, true, false); | |
151 SendKeyEventToTextfieldView(app::VKEY_RIGHT, true, false); | |
152 | |
153 EXPECT_STR_EQ("one", textfield_->GetSelectedText()); | |
154 | |
155 // Test word select. | |
156 SendKeyEventToTextfieldView(app::VKEY_RIGHT, true, true); | |
157 EXPECT_STR_EQ("one two", textfield_->GetSelectedText()); | |
158 SendKeyEventToTextfieldView(app::VKEY_RIGHT, true, true); | |
159 EXPECT_STR_EQ("one two three", textfield_->GetSelectedText()); | |
160 SendKeyEventToTextfieldView(app::VKEY_LEFT, true, true); | |
161 EXPECT_STR_EQ("one two ", textfield_->GetSelectedText()); | |
162 SendKeyEventToTextfieldView(app::VKEY_LEFT, true, true); | |
163 EXPECT_STR_EQ("one ", textfield_->GetSelectedText()); | |
164 | |
165 // Replace the selected text. | |
166 SendKeyEventToTextfieldView(app::VKEY_Z, true, false); | |
167 SendKeyEventToTextfieldView(app::VKEY_E, true, false); | |
168 SendKeyEventToTextfieldView(app::VKEY_R, true, false); | |
169 SendKeyEventToTextfieldView(app::VKEY_O, true, false); | |
170 SendKeyEventToTextfieldView(app::VKEY_SPACE, false, false); | |
171 EXPECT_STR_EQ("ZERO two three", textfield_->text()); | |
172 | |
173 SendKeyEventToTextfieldView(app::VKEY_END, true, false); | |
174 EXPECT_STR_EQ("two three", textfield_->GetSelectedText()); | |
175 SendKeyEventToTextfieldView(app::VKEY_HOME, true, false); | |
176 EXPECT_STR_EQ("ZERO ", textfield_->GetSelectedText()); | |
177 } | |
178 | |
179 TEST_F(NativeTextfieldViewTest, InsertionDeletionTest) { | |
180 // Insert a test string in a textfield. | |
181 InitTextfield(Textfield::STYLE_DEFAULT); | |
182 char test_str[] = "this is a test"; | |
183 for (size_t i = 0; i < sizeof(test_str); i++) { | |
184 // This is ugly and should be replaced by a utility standard function. | |
185 // See comment in NativeTextfieldView::GetPrintableChar. | |
186 char c = test_str[i]; | |
187 app::KeyboardCode code = | |
188 c == ' ' ? app::VKEY_SPACE : | |
189 static_cast<app::KeyboardCode>(app::VKEY_A + c - 'a'); | |
190 SendKeyEventToTextfieldView(code); | |
191 } | |
192 EXPECT_STR_EQ(test_str, textfield_->text()); | |
193 | |
194 // Move the cursor around. | |
195 for (int i = 0; i < 6; i++) { | |
196 SendKeyEventToTextfieldView(app::VKEY_LEFT); | |
197 } | |
198 SendKeyEventToTextfieldView(app::VKEY_RIGHT); | |
199 | |
200 // Delete using backspace and check resulting string. | |
201 SendKeyEventToTextfieldView(app::VKEY_BACK); | |
202 EXPECT_STR_EQ("this is test", textfield_->text()); | |
203 | |
204 // Delete using delete key and check resulting string. | |
205 for (int i = 0; i < 5; i++) { | |
206 SendKeyEventToTextfieldView(app::VKEY_DELETE); | |
207 } | |
208 EXPECT_STR_EQ("this is ", textfield_->text()); | |
209 | |
210 // Select all and replace with "k". | |
211 textfield_->SelectAll(); | |
212 SendKeyEventToTextfieldView(app::VKEY_K); | |
213 EXPECT_STR_EQ("k", textfield_->text()); | |
214 } | |
215 | |
216 TEST_F(NativeTextfieldViewTest, PasswordTest) { | |
217 InitTextfield(Textfield::STYLE_PASSWORD); | |
218 textfield_->SetText(ASCIIToUTF16("my password")); | |
219 // Just to make sure the text() and callback returns | |
220 // the actual text instead of "*". | |
221 EXPECT_STR_EQ("my password", textfield_->text()); | |
222 EXPECT_STR_EQ("my password", last_contents_); | |
223 } | |
224 | |
225 } // namespace views | |
OLD | NEW |