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