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 "gfx/canvas_skia.h" | |
6 #include "testing/gtest/include/gtest/gtest.h" | |
7 #include "views/controls/textfield/textfield_view.h" | |
8 #include "views/event.h" | |
9 | |
10 namespace views { | |
11 | |
12 void SendKeyEventToTextfield(TextfieldView* tf, base::KeyboardCode key_code) { | |
13 KeyEvent event(KeyEvent::ET_KEY_PRESSED, key_code, 0, 1, 0); | |
14 tf->OnKeyPressed(event); | |
15 } | |
16 | |
17 TEST(TextfieldViewTest, ModelChangesTest) { | |
18 // Initialize textfield with a certain model. | |
19 TextfieldModel* model = new TextfieldModel(L"this is a test"); | |
20 scoped_ptr<TextfieldView> textfield(new TextfieldView(model)); | |
21 EXPECT_EQ(textfield->GetText(), L"this is a test"); | |
22 | |
23 // Modify model and check the textfield. | |
24 model->InsertBackspace(); | |
25 EXPECT_EQ(textfield->GetText(), L"this is a tes"); | |
26 } | |
27 | |
28 TEST(TextfieldViewTest, InsertionDeletionTest) { | |
29 // Insert a test string in a textfield | |
30 std::wstring test_str(L"thisisatest"); | |
31 scoped_ptr<TextfieldView> textfield(new TextfieldView()); | |
32 for (unsigned i = 0; i < test_str.length(); i++) { | |
33 SendKeyEventToTextfield(textfield.get(), base::KeyboardCode( | |
34 static_cast<int>(base::VKEY_A) + (test_str[i] - 'a'))); | |
35 } | |
36 EXPECT_EQ(textfield->GetText(), test_str); | |
37 | |
38 // Move the cursor around. | |
39 for (int i = 0; i < 5; i++) { | |
40 SendKeyEventToTextfield(textfield.get(), base::VKEY_LEFT); | |
41 } | |
42 SendKeyEventToTextfield(textfield.get(), base::VKEY_RIGHT); | |
43 | |
44 // Delete using backspace and check resulting string. | |
45 SendKeyEventToTextfield(textfield.get(), base::VKEY_BACK); | |
46 EXPECT_EQ(textfield->GetText(), L"thisistest"); | |
47 | |
48 // Delete using delete key and check resulting string. | |
49 for (int i = 0; i < 4; i++) { | |
50 SendKeyEventToTextfield(textfield.get(), base::VKEY_DELETE); | |
51 } | |
52 EXPECT_EQ(textfield->GetText(), L"thisis"); | |
53 } | |
54 | |
55 TEST(TextfieldViewTest, PaintTest) { | |
56 // Insert a test string in a textfield | |
57 std::wstring test_str(L"thisisatest"); | |
58 gfx::CanvasSkia canvas(100, 100, true); | |
59 int tf_width = 20; | |
60 int visible_width = tf_width - 2 /* border width */; | |
61 scoped_ptr<TextfieldView> textfield(new TextfieldView()); | |
62 textfield->SetBounds(0, 0, tf_width, 10); | |
63 textfield->Paint(&canvas); | |
64 | |
65 for (unsigned i = 0; i < test_str.length(); i++) { | |
66 SendKeyEventToTextfield(textfield.get(), base::KeyboardCode( | |
67 static_cast<int>(base::VKEY_A) + (test_str[i] - 'a'))); | |
68 } | |
69 textfield->Paint(&canvas); | |
70 | |
71 EXPECT_EQ(textfield->GetText(), test_str); | |
72 std::wstring visible_str = test_str; | |
73 for (;textfield->font().GetStringWidth(visible_str) > visible_width;) { | |
74 visible_str = visible_str.substr(1); | |
75 } | |
76 | |
77 // The cursor must be at the end of the box. check paint. | |
78 EXPECT_EQ(textfield->onscreen_cursor_pos(), visible_width); | |
79 EXPECT_EQ(textfield->visible_text(), visible_str); | |
80 | |
81 // Move cursor left by one and check paint. | |
82 SendKeyEventToTextfield(textfield.get(), base::VKEY_LEFT); | |
83 EXPECT_EQ(textfield->onscreen_cursor_pos(), | |
84 visible_width - textfield->font().GetStringWidth( | |
85 visible_str.substr(visible_str.length() - 1))); | |
86 EXPECT_EQ(textfield->visible_text(), visible_str); | |
87 | |
88 // Move cursor all the way to the begining and check paint. | |
89 for (unsigned i = 0; i < visible_str.length() - 1; i++) { | |
90 SendKeyEventToTextfield(textfield.get(), base::VKEY_LEFT); | |
91 } | |
92 EXPECT_EQ(textfield->onscreen_cursor_pos(), | |
93 visible_width - textfield->font().GetStringWidth(visible_str)); | |
94 EXPECT_EQ(textfield->visible_text(), visible_str); | |
95 | |
96 // Move cursor right by one and check paint. | |
97 SendKeyEventToTextfield(textfield.get(), base::VKEY_RIGHT); | |
98 EXPECT_EQ(textfield->onscreen_cursor_pos(), | |
99 visible_width - textfield->font().GetStringWidth( | |
100 visible_str.substr(1))); | |
101 EXPECT_EQ(textfield->visible_text(), visible_str); | |
102 } | |
103 } | |
tfarina
2010/11/18 11:55:31
// namespace views
| |
OLD | NEW |