Chromium Code Reviews| Index: views/controls/textfield/textfield_view_unittest.cc |
| diff --git a/views/controls/textfield/textfield_view_unittest.cc b/views/controls/textfield/textfield_view_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6c44073c3cd5554ca7b090de41e98c51bad0aed3 |
| --- /dev/null |
| +++ b/views/controls/textfield/textfield_view_unittest.cc |
| @@ -0,0 +1,103 @@ |
| +// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "gfx/canvas_skia.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "views/controls/textfield/textfield_view.h" |
| +#include "views/event.h" |
| + |
| +namespace views { |
| + |
| +void SendKeyEventToTextfield(TextfieldView* tf, base::KeyboardCode key_code) { |
| + KeyEvent event(KeyEvent::ET_KEY_PRESSED, key_code, 0, 1, 0); |
| + tf->OnKeyPressed(event); |
| +} |
| + |
| +TEST(TextfieldViewTest, ModelChangesTest) { |
| + // Initialize textfield with a certain model. |
| + TextfieldModel* model = new TextfieldModel(L"this is a test"); |
| + scoped_ptr<TextfieldView> textfield(new TextfieldView(model)); |
| + EXPECT_EQ(textfield->GetText(), L"this is a test"); |
| + |
| + // Modify model and check the textfield. |
| + model->InsertBackspace(); |
| + EXPECT_EQ(textfield->GetText(), L"this is a tes"); |
| +} |
| + |
| +TEST(TextfieldViewTest, InsertionDeletionTest) { |
| + // Insert a test string in a textfield |
| + std::wstring test_str(L"thisisatest"); |
| + scoped_ptr<TextfieldView> textfield(new TextfieldView()); |
| + for (unsigned i = 0; i < test_str.length(); i++) { |
| + SendKeyEventToTextfield(textfield.get(), base::KeyboardCode( |
| + static_cast<int>(base::VKEY_A) + (test_str[i] - 'a'))); |
| + } |
| + EXPECT_EQ(textfield->GetText(), test_str); |
| + |
| + // Move the cursor around. |
| + for (int i = 0; i < 5; i++) { |
| + SendKeyEventToTextfield(textfield.get(), base::VKEY_LEFT); |
| + } |
| + SendKeyEventToTextfield(textfield.get(), base::VKEY_RIGHT); |
| + |
| + // Delete using backspace and check resulting string. |
| + SendKeyEventToTextfield(textfield.get(), base::VKEY_BACK); |
| + EXPECT_EQ(textfield->GetText(), L"thisistest"); |
| + |
| + // Delete using delete key and check resulting string. |
| + for (int i = 0; i < 4; i++) { |
| + SendKeyEventToTextfield(textfield.get(), base::VKEY_DELETE); |
| + } |
| + EXPECT_EQ(textfield->GetText(), L"thisis"); |
| +} |
| + |
| +TEST(TextfieldViewTest, PaintTest) { |
| + // Insert a test string in a textfield |
| + std::wstring test_str(L"thisisatest"); |
| + gfx::CanvasSkia canvas(100, 100, true); |
| + int tf_width = 20; |
| + int visible_width = tf_width - 2 /* border width */; |
| + scoped_ptr<TextfieldView> textfield(new TextfieldView()); |
| + textfield->SetBounds(0, 0, tf_width, 10); |
| + textfield->Paint(&canvas); |
| + |
| + for (unsigned i = 0; i < test_str.length(); i++) { |
| + SendKeyEventToTextfield(textfield.get(), base::KeyboardCode( |
| + static_cast<int>(base::VKEY_A) + (test_str[i] - 'a'))); |
| + } |
| + textfield->Paint(&canvas); |
| + |
| + EXPECT_EQ(textfield->GetText(), test_str); |
| + std::wstring visible_str = test_str; |
| + for (;textfield->font().GetStringWidth(visible_str) > visible_width;) { |
| + visible_str = visible_str.substr(1); |
| + } |
| + |
| + // The cursor must be at the end of the box. check paint. |
| + EXPECT_EQ(textfield->onscreen_cursor_pos(), visible_width); |
| + EXPECT_EQ(textfield->visible_text(), visible_str); |
| + |
| + // Move cursor left by one and check paint. |
| + SendKeyEventToTextfield(textfield.get(), base::VKEY_LEFT); |
| + EXPECT_EQ(textfield->onscreen_cursor_pos(), |
| + visible_width - textfield->font().GetStringWidth( |
| + visible_str.substr(visible_str.length() - 1))); |
| + EXPECT_EQ(textfield->visible_text(), visible_str); |
| + |
| + // Move cursor all the way to the begining and check paint. |
| + for (unsigned i = 0; i < visible_str.length() - 1; i++) { |
| + SendKeyEventToTextfield(textfield.get(), base::VKEY_LEFT); |
| + } |
| + EXPECT_EQ(textfield->onscreen_cursor_pos(), |
| + visible_width - textfield->font().GetStringWidth(visible_str)); |
| + EXPECT_EQ(textfield->visible_text(), visible_str); |
| + |
| + // Move cursor right by one and check paint. |
| + SendKeyEventToTextfield(textfield.get(), base::VKEY_RIGHT); |
| + EXPECT_EQ(textfield->onscreen_cursor_pos(), |
| + visible_width - textfield->font().GetStringWidth( |
| + visible_str.substr(1))); |
| + EXPECT_EQ(textfield->visible_text(), visible_str); |
| +} |
| +} |
|
tfarina
2010/11/18 11:55:31
// namespace views
|