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 "base/utf_string_conversions.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "views/controls/textfield/textfield_view_model.h" |
| 8 |
| 9 namespace views { |
| 10 |
| 11 #define EXPECT_STR_EQ(ascii, utf16) EXPECT_EQ(ASCIIToUTF16(ascii), utf16) |
| 12 |
| 13 TEST(TextfieldViewModelTest, EditString) { |
| 14 TextfieldViewModel model; |
| 15 // append two strings |
| 16 model.Append(ASCIIToUTF16("HILL")); |
| 17 EXPECT_STR_EQ("HILL", model.text()); |
| 18 model.Append(ASCIIToUTF16("WORLD")); |
| 19 EXPECT_STR_EQ("HILLWORLD", model.text()); |
| 20 |
| 21 // Insert "E" to make hello |
| 22 model.MoveCursorRight(false); |
| 23 model.Insert('E'); |
| 24 EXPECT_STR_EQ("HEILLWORLD", model.text()); |
| 25 // Replace "I" with "L" |
| 26 model.Replace('L'); |
| 27 EXPECT_STR_EQ("HELLLWORLD", model.text()); |
| 28 model.Replace('L'); |
| 29 model.Replace('O'); |
| 30 EXPECT_STR_EQ("HELLOWORLD", model.text()); |
| 31 |
| 32 // Delete 6th char "W", then delete 5th char O" |
| 33 EXPECT_EQ(5, model.cursor_pos()); |
| 34 EXPECT_TRUE(model.Delete()); |
| 35 EXPECT_STR_EQ("HELLOORLD", model.text()); |
| 36 EXPECT_TRUE(model.Backspace()); |
| 37 EXPECT_EQ(4, model.cursor_pos()); |
| 38 EXPECT_STR_EQ("HELLORLD", model.text()); |
| 39 |
| 40 // Move the cursor to start. backspace should fail. |
| 41 model.MoveCursorToStart(false); |
| 42 EXPECT_FALSE(model.Backspace()); |
| 43 EXPECT_STR_EQ("HELLORLD", model.text()); |
| 44 // Move the cursor to the end. delete should fail. |
| 45 model.MoveCursorToEnd(false); |
| 46 EXPECT_FALSE(model.Delete()); |
| 47 EXPECT_STR_EQ("HELLORLD", model.text()); |
| 48 // but backspace should work. |
| 49 EXPECT_TRUE(model.Backspace()); |
| 50 EXPECT_STR_EQ("HELLORL", model.text()); |
| 51 } |
| 52 |
| 53 TEST(TextfieldViewModelTest, EmptyString) { |
| 54 TextfieldViewModel model; |
| 55 EXPECT_EQ(string16(), model.text()); |
| 56 EXPECT_EQ(string16(), model.GetSelectedText()); |
| 57 EXPECT_EQ(string16(), model.GetVisibleText()); |
| 58 |
| 59 model.MoveCursorLeft(true); |
| 60 EXPECT_EQ(0, model.cursor_pos()); |
| 61 model.MoveCursorRight(true); |
| 62 EXPECT_EQ(0, model.cursor_pos()); |
| 63 |
| 64 EXPECT_EQ(string16(), model.GetSelectedText()); |
| 65 |
| 66 EXPECT_FALSE(model.Delete()); |
| 67 EXPECT_FALSE(model.Backspace()); |
| 68 } |
| 69 |
| 70 TEST(TextfieldViewModelTest, Selection) { |
| 71 TextfieldViewModel model; |
| 72 model.Append(ASCIIToUTF16("HELLO")); |
| 73 model.MoveCursorRight(false); |
| 74 model.MoveCursorRight(true); |
| 75 EXPECT_STR_EQ("E", model.GetSelectedText()); |
| 76 model.MoveCursorRight(true); |
| 77 EXPECT_STR_EQ("EL", model.GetSelectedText()); |
| 78 |
| 79 model.MoveCursorToStart(true); |
| 80 EXPECT_STR_EQ("H", model.GetSelectedText()); |
| 81 model.MoveCursorToEnd(true); |
| 82 EXPECT_STR_EQ("ELLO", model.GetSelectedText()); |
| 83 model.ClearSelection(); |
| 84 EXPECT_EQ(string16(), model.GetSelectedText()); |
| 85 model.SelectAll(); |
| 86 EXPECT_STR_EQ("HELLO", model.GetSelectedText()); |
| 87 } |
| 88 |
| 89 TEST(TextfieldViewModelTest, SelectionAndEdit) { |
| 90 TextfieldViewModel model; |
| 91 model.Append(ASCIIToUTF16("HELLO")); |
| 92 model.MoveCursorRight(false); |
| 93 model.MoveCursorRight(true); |
| 94 model.MoveCursorRight(true); // select "EL" |
| 95 EXPECT_TRUE(model.Backspace()); |
| 96 EXPECT_STR_EQ("HLO", model.text()); |
| 97 |
| 98 model.Append(ASCIIToUTF16("ILL")); |
| 99 model.MoveCursorRight(true); |
| 100 model.MoveCursorRight(true); // select "LO" |
| 101 EXPECT_TRUE(model.Delete()); |
| 102 EXPECT_STR_EQ("HILL", model.text()); |
| 103 EXPECT_EQ(1, model.cursor_pos()); |
| 104 model.MoveCursorRight(true); // select "I" |
| 105 model.Insert('E'); |
| 106 EXPECT_STR_EQ("HELL", model.text()); |
| 107 model.MoveCursorToStart(false); |
| 108 model.MoveCursorRight(true); // select "H" |
| 109 model.Replace('B'); |
| 110 EXPECT_STR_EQ("BELL", model.text()); |
| 111 model.MoveCursorToEnd(false); |
| 112 model.MoveCursorLeft(true); |
| 113 model.MoveCursorLeft(true); // select ">LL" |
| 114 model.Replace('E'); |
| 115 EXPECT_STR_EQ("BEE", model.text()); |
| 116 } |
| 117 |
| 118 TEST(TextfieldViewModelTest, Password) { |
| 119 TextfieldViewModel model; |
| 120 model.set_is_password(true); |
| 121 model.Append(ASCIIToUTF16("HELLO")); |
| 122 EXPECT_STR_EQ("*****", model.GetVisibleText()); |
| 123 EXPECT_STR_EQ("HELLO", model.text()); |
| 124 EXPECT_TRUE(model.Delete()); |
| 125 |
| 126 EXPECT_STR_EQ("****", model.GetVisibleText()); |
| 127 EXPECT_STR_EQ("ELLO", model.text()); |
| 128 EXPECT_EQ(0, model.cursor_pos()); |
| 129 |
| 130 model.SelectAll(); |
| 131 EXPECT_STR_EQ("ELLO", model.GetSelectedText()); |
| 132 EXPECT_EQ(0, model.cursor_pos()); |
| 133 |
| 134 model.Insert('X'); |
| 135 EXPECT_STR_EQ("*", model.GetVisibleText()); |
| 136 EXPECT_STR_EQ("X", model.text()); |
| 137 } |
| 138 |
| 139 TEST(TextfieldViewModelTest, Word) { |
| 140 TextfieldViewModel model; |
| 141 model.Append( |
| 142 ASCIIToUTF16("The answer to Life, the Universe, and Everything")); |
| 143 model.MoveCursorToNextWord(false); |
| 144 EXPECT_EQ(3, model.cursor_pos()); |
| 145 model.MoveCursorToNextWord(false); |
| 146 EXPECT_EQ(10, model.cursor_pos()); |
| 147 model.MoveCursorToNextWord(false); |
| 148 model.MoveCursorToNextWord(false); |
| 149 EXPECT_EQ(18, model.cursor_pos()); |
| 150 |
| 151 // Should passes the non word char ',' |
| 152 model.MoveCursorToNextWord(true); |
| 153 EXPECT_EQ(23, model.cursor_pos()); |
| 154 EXPECT_STR_EQ(", the", model.GetSelectedText()); |
| 155 |
| 156 // Move to the end. |
| 157 model.MoveCursorToNextWord(true); |
| 158 model.MoveCursorToNextWord(true); |
| 159 model.MoveCursorToNextWord(true); |
| 160 EXPECT_STR_EQ(", the Universe, and Everything", model.GetSelectedText()); |
| 161 // Should be safe to go next word at the end. |
| 162 model.MoveCursorToNextWord(true); |
| 163 EXPECT_STR_EQ(", the Universe, and Everything", model.GetSelectedText()); |
| 164 model.Insert('2'); |
| 165 EXPECT_EQ(19, model.cursor_pos()); |
| 166 |
| 167 // Now backwards. |
| 168 model.MoveCursorLeft(false); // leave 2. |
| 169 model.MoveCursorToPreviousWord(true); |
| 170 EXPECT_EQ(14, model.cursor_pos()); |
| 171 EXPECT_STR_EQ("Life", model.GetSelectedText()); |
| 172 model.MoveCursorToPreviousWord(true); |
| 173 EXPECT_STR_EQ("to Life", model.GetSelectedText()); |
| 174 model.MoveCursorToPreviousWord(true); |
| 175 model.MoveCursorToPreviousWord(true); |
| 176 model.MoveCursorToPreviousWord(true); // Select to the begining. |
| 177 EXPECT_STR_EQ("The answer to Life", model.GetSelectedText()); |
| 178 // Should be safe to go pervious word at the begining. |
| 179 model.MoveCursorToPreviousWord(true); |
| 180 EXPECT_STR_EQ("The answer to Life", model.GetSelectedText()); |
| 181 model.Replace('4'); |
| 182 EXPECT_EQ(string16(), model.GetSelectedText()); |
| 183 EXPECT_STR_EQ("42", model.GetVisibleText()); |
| 184 } |
| 185 |
| 186 TEST(TextfieldViewModelTest, TextFragment) { |
| 187 TextfieldViewModel model; |
| 188 TextfieldViewModel::TextFragments fragments; |
| 189 // Empty string |
| 190 model.PopulateFragments(&fragments); |
| 191 EXPECT_EQ(1U, fragments.size()); |
| 192 fragments.clear(); |
| 193 EXPECT_EQ(0U, fragments[0].begin); |
| 194 EXPECT_EQ(0U, fragments[0].end); |
| 195 EXPECT_FALSE(fragments[0].selected); |
| 196 |
| 197 // Some string |
| 198 model.Append(ASCIIToUTF16("Hello world")); |
| 199 model.PopulateFragments(&fragments); |
| 200 EXPECT_EQ(1U, fragments.size()); |
| 201 EXPECT_EQ(0U, fragments[0].begin); |
| 202 EXPECT_EQ(11U, fragments[0].end); |
| 203 EXPECT_FALSE(fragments[0].selected); |
| 204 |
| 205 // Select 1st word |
| 206 model.MoveCursorToNextWord(true); |
| 207 model.PopulateFragments(&fragments); |
| 208 EXPECT_EQ(2U, fragments.size()); |
| 209 EXPECT_EQ(0U, fragments[0].begin); |
| 210 EXPECT_EQ(5U, fragments[0].end); |
| 211 EXPECT_TRUE(fragments[0].selected); |
| 212 EXPECT_EQ(5U, fragments[1].begin); |
| 213 EXPECT_EQ(11U, fragments[1].end); |
| 214 EXPECT_FALSE(fragments[1].selected); |
| 215 |
| 216 // Select empty string |
| 217 model.ClearSelection(); |
| 218 model.MoveCursorRight(true); |
| 219 model.PopulateFragments(&fragments); |
| 220 EXPECT_EQ(3U, fragments.size()); |
| 221 EXPECT_EQ(0U, fragments[0].begin); |
| 222 EXPECT_EQ(5U, fragments[0].end); |
| 223 EXPECT_FALSE(fragments[0].selected); |
| 224 EXPECT_EQ(5U, fragments[1].begin); |
| 225 EXPECT_EQ(6U, fragments[1].end); |
| 226 EXPECT_TRUE(fragments[1].selected); |
| 227 |
| 228 EXPECT_EQ(6U, fragments[2].begin); |
| 229 EXPECT_EQ(11U, fragments[2].end); |
| 230 EXPECT_FALSE(fragments[2].selected); |
| 231 |
| 232 // Select to the end. |
| 233 model.MoveCursorToEnd(true); |
| 234 model.PopulateFragments(&fragments); |
| 235 EXPECT_EQ(2U, fragments.size()); |
| 236 EXPECT_EQ(0U, fragments[0].begin); |
| 237 EXPECT_EQ(5U, fragments[0].end); |
| 238 EXPECT_FALSE(fragments[0].selected); |
| 239 EXPECT_EQ(5U, fragments[1].begin); |
| 240 EXPECT_EQ(11U, fragments[1].end); |
| 241 EXPECT_TRUE(fragments[1].selected); |
| 242 } |
| 243 |
| 244 TEST(TextfieldViewModelTest, SetText) { |
| 245 TextfieldViewModel model; |
| 246 model.Append(ASCIIToUTF16("HELLO")); |
| 247 model.MoveCursorToEnd(false); |
| 248 model.SetText(ASCIIToUTF16("GOODBYE")); |
| 249 EXPECT_STR_EQ("GOODBYE", model.text()); |
| 250 EXPECT_EQ(5, model.cursor_pos()); |
| 251 model.SelectAll(); |
| 252 EXPECT_STR_EQ("GOODBYE", model.GetSelectedText()); |
| 253 EXPECT_EQ(5, model.cursor_pos()); |
| 254 |
| 255 model.SetText(ASCIIToUTF16("BYE")); |
| 256 EXPECT_EQ(3, model.cursor_pos()); |
| 257 EXPECT_EQ(string16(), model.GetSelectedText()); |
| 258 model.SetText(ASCIIToUTF16("")); |
| 259 EXPECT_EQ(0, model.cursor_pos()); |
| 260 } |
| 261 |
| 262 } // namespace views |
OLD | NEW |