Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(923)

Side by Side Diff: views/controls/textfield/textfield_views_model_unittest.cc

Issue 7265011: RenderText API Outline. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Almost at parity with the current implementation. Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/auto_reset.h" 5 #include "base/auto_reset.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/base/clipboard/clipboard.h" 10 #include "ui/base/clipboard/clipboard.h"
11 #include "ui/base/clipboard/scoped_clipboard_writer.h" 11 #include "ui/base/clipboard/scoped_clipboard_writer.h"
12 #include "ui/base/range/range.h" 12 #include "ui/base/range/range.h"
13 #include "views/controls/textfield/text_style.h"
14 #include "views/controls/textfield/textfield.h" 13 #include "views/controls/textfield/textfield.h"
15 #include "views/controls/textfield/textfield_views_model.h" 14 #include "views/controls/textfield/textfield_views_model.h"
16 #include "views/test/test_views_delegate.h" 15 #include "views/test/test_views_delegate.h"
17 #include "views/test/views_test_base.h" 16 #include "views/test/views_test_base.h"
18 #include "views/views_delegate.h" 17 #include "views/views_delegate.h"
19 18
20 namespace views { 19 namespace views {
21 20
22 #include "views/test/views_test_base.h"
23
24 class TextfieldViewsModelTest : public ViewsTestBase, 21 class TextfieldViewsModelTest : public ViewsTestBase,
25 public TextfieldViewsModel::Delegate { 22 public TextfieldViewsModel::Delegate {
26 public: 23 public:
27 TextfieldViewsModelTest() 24 TextfieldViewsModelTest()
28 : ViewsTestBase(), 25 : ViewsTestBase(),
29 composition_text_confirmed_or_cleared_(false) { 26 composition_text_confirmed_or_cleared_(false) {
30 } 27 }
31 28
32 virtual void OnCompositionTextConfirmedOrCleared() { 29 virtual void OnCompositionTextConfirmedOrCleared() {
33 composition_text_confirmed_or_cleared_ = true; 30 composition_text_confirmed_or_cleared_ = true;
34 } 31 }
35 32
36 protected: 33 protected:
37 void ResetModel(TextfieldViewsModel* model) const { 34 void ResetModel(TextfieldViewsModel* model) const {
38 model->SetText(ASCIIToUTF16("")); 35 model->SetText(ASCIIToUTF16(""));
39 model->ClearEditHistory(); 36 model->ClearEditHistory();
40 } 37 }
41 38
42 bool composition_text_confirmed_or_cleared_; 39 bool composition_text_confirmed_or_cleared_;
43 40
44 private: 41 private:
45 DISALLOW_COPY_AND_ASSIGN(TextfieldViewsModelTest); 42 DISALLOW_COPY_AND_ASSIGN(TextfieldViewsModelTest);
46 }; 43 };
47 44
48 #define EXPECT_STR_EQ(ascii, utf16) \ 45 #define EXPECT_STR_EQ(ascii, utf16) \
49 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16)) 46 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16))
50
51 TEST_F(TextfieldViewsModelTest, EditString) { 47 TEST_F(TextfieldViewsModelTest, EditString) {
52 TextfieldViewsModel model(NULL); 48 TextfieldViewsModel model(NULL);
53 // append two strings 49 // append two strings
54 model.Append(ASCIIToUTF16("HILL")); 50 model.Append(ASCIIToUTF16("HILL"));
55 EXPECT_STR_EQ("HILL", model.text()); 51 EXPECT_STR_EQ("HILL", model.GetText());
56 model.Append(ASCIIToUTF16("WORLD")); 52 model.Append(ASCIIToUTF16("WORLD"));
57 EXPECT_STR_EQ("HILLWORLD", model.text()); 53 EXPECT_STR_EQ("HILLWORLD", model.GetText());
58 54
59 // Insert "E" to make hello 55 // Insert "E" to make hello
60 model.MoveCursorRight(false); 56 model.MoveCursorRight(false);
61 model.InsertChar('E'); 57 model.InsertChar('E');
62 EXPECT_STR_EQ("HEILLWORLD", model.text()); 58 EXPECT_STR_EQ("HEILLWORLD", model.GetText());
63 // Replace "I" with "L" 59 // Replace "I" with "L"
64 model.ReplaceChar('L'); 60 model.ReplaceChar('L');
65 EXPECT_STR_EQ("HELLLWORLD", model.text()); 61 EXPECT_STR_EQ("HELLLWORLD", model.GetText());
66 model.ReplaceChar('L'); 62 model.ReplaceChar('L');
67 model.ReplaceChar('O'); 63 model.ReplaceChar('O');
68 EXPECT_STR_EQ("HELLOWORLD", model.text()); 64 EXPECT_STR_EQ("HELLOWORLD", model.GetText());
69 65
70 // Delete 6th char "W", then delete 5th char O" 66 // Delete 6th char "W", then delete 5th char O"
71 EXPECT_EQ(5U, model.cursor_pos()); 67 EXPECT_EQ(5U, model.GetCursorPosition());
72 EXPECT_TRUE(model.Delete()); 68 EXPECT_TRUE(model.Delete());
73 EXPECT_STR_EQ("HELLOORLD", model.text()); 69 EXPECT_STR_EQ("HELLOORLD", model.GetText());
74 EXPECT_TRUE(model.Backspace()); 70 EXPECT_TRUE(model.Backspace());
75 EXPECT_EQ(4U, model.cursor_pos()); 71 EXPECT_EQ(4U, model.GetCursorPosition());
76 EXPECT_STR_EQ("HELLORLD", model.text()); 72 EXPECT_STR_EQ("HELLORLD", model.GetText());
77 73
78 // Move the cursor to start. backspace should fail. 74 // Move the cursor to start. backspace should fail.
79 model.MoveCursorToHome(false); 75 model.MoveCursorToLeftEnd(false);
80 EXPECT_FALSE(model.Backspace()); 76 EXPECT_FALSE(model.Backspace());
81 EXPECT_STR_EQ("HELLORLD", model.text()); 77 EXPECT_STR_EQ("HELLORLD", model.GetText());
82 // Move the cursor to the end. delete should fail. 78 // Move the cursor to the end. delete should fail.
83 model.MoveCursorToEnd(false); 79 model.MoveCursorToRightEnd(false);
84 EXPECT_FALSE(model.Delete()); 80 EXPECT_FALSE(model.Delete());
85 EXPECT_STR_EQ("HELLORLD", model.text()); 81 EXPECT_STR_EQ("HELLORLD", model.GetText());
86 // but backspace should work. 82 // but backspace should work.
87 EXPECT_TRUE(model.Backspace()); 83 EXPECT_TRUE(model.Backspace());
88 EXPECT_STR_EQ("HELLORL", model.text()); 84 EXPECT_STR_EQ("HELLORL", model.GetText());
89 } 85 }
90 86
91 TEST_F(TextfieldViewsModelTest, EmptyString) { 87 TEST_F(TextfieldViewsModelTest, EmptyString) {
92 TextfieldViewsModel model(NULL); 88 TextfieldViewsModel model(NULL);
93 EXPECT_EQ(string16(), model.text()); 89 EXPECT_EQ(string16(), model.GetText());
94 EXPECT_EQ(string16(), model.GetSelectedText()); 90 EXPECT_EQ(string16(), model.GetSelectedText());
95 EXPECT_EQ(string16(), model.GetVisibleText()); 91 EXPECT_EQ(string16(), model.GetVisibleText());
96 92
97 model.MoveCursorLeft(true); 93 model.MoveCursorLeft(true);
98 EXPECT_EQ(0U, model.cursor_pos()); 94 EXPECT_EQ(0U, model.GetCursorPosition());
99 model.MoveCursorRight(true); 95 model.MoveCursorRight(true);
100 EXPECT_EQ(0U, model.cursor_pos()); 96 EXPECT_EQ(0U, model.GetCursorPosition());
101 97
102 EXPECT_EQ(string16(), model.GetSelectedText()); 98 EXPECT_EQ(string16(), model.GetSelectedText());
103 99
104 EXPECT_FALSE(model.Delete()); 100 EXPECT_FALSE(model.Delete());
105 EXPECT_FALSE(model.Backspace()); 101 EXPECT_FALSE(model.Backspace());
106 } 102 }
107 103
108 TEST_F(TextfieldViewsModelTest, Selection) { 104 TEST_F(TextfieldViewsModelTest, Selection) {
109 TextfieldViewsModel model(NULL); 105 TextfieldViewsModel model(NULL);
110 model.Append(ASCIIToUTF16("HELLO")); 106 model.Append(ASCIIToUTF16("HELLO"));
111 model.MoveCursorRight(false); 107 model.MoveCursorRight(false);
112 model.MoveCursorRight(true); 108 model.MoveCursorRight(true);
113 EXPECT_STR_EQ("E", model.GetSelectedText()); 109 EXPECT_STR_EQ("E", model.GetSelectedText());
114 model.MoveCursorRight(true); 110 model.MoveCursorRight(true);
115 EXPECT_STR_EQ("EL", model.GetSelectedText()); 111 EXPECT_STR_EQ("EL", model.GetSelectedText());
116 112
117 model.MoveCursorToHome(true); 113 model.MoveCursorToLeftEnd(true);
118 EXPECT_STR_EQ("H", model.GetSelectedText()); 114 EXPECT_STR_EQ("H", model.GetSelectedText());
119 model.MoveCursorToEnd(true); 115 model.MoveCursorToRightEnd(true);
120 EXPECT_STR_EQ("ELLO", model.GetSelectedText()); 116 EXPECT_STR_EQ("ELLO", model.GetSelectedText());
121 model.ClearSelection(); 117 model.ClearSelection();
122 EXPECT_EQ(string16(), model.GetSelectedText()); 118 EXPECT_EQ(string16(), model.GetSelectedText());
123 model.SelectAll(); 119 model.SelectAll();
124 EXPECT_STR_EQ("HELLO", model.GetSelectedText()); 120 EXPECT_STR_EQ("HELLO", model.GetSelectedText());
125 // SelectAll should select towards the end. 121 // SelectAll should select towards the end.
126 ui::Range range; 122 ui::Range range;
127 model.GetSelectedRange(&range); 123 model.GetSelectedRange(&range);
128 EXPECT_EQ(0U, range.start()); 124 EXPECT_EQ(0U, range.start());
129 EXPECT_EQ(5U, range.end()); 125 EXPECT_EQ(5U, range.end());
130 126
131 // Select and move cursor 127 // Select and move cursor
132 model.MoveCursorTo(1U, false); 128 model.MoveCursorTo(1U, false);
133 model.MoveCursorTo(3U, true); 129 model.MoveCursorTo(3U, true);
134 EXPECT_STR_EQ("EL", model.GetSelectedText()); 130 EXPECT_STR_EQ("EL", model.GetSelectedText());
135 model.MoveCursorLeft(false); 131 model.MoveCursorLeft(false);
136 EXPECT_EQ(1U, model.cursor_pos()); 132 EXPECT_EQ(1U, model.GetCursorPosition());
137 model.MoveCursorTo(1U, false); 133 model.MoveCursorTo(1U, false);
138 model.MoveCursorTo(3U, true); 134 model.MoveCursorTo(3U, true);
139 model.MoveCursorRight(false); 135 model.MoveCursorRight(false);
140 EXPECT_EQ(3U, model.cursor_pos()); 136 EXPECT_EQ(3U, model.GetCursorPosition());
141 137
142 // Select all and move cursor 138 // Select all and move cursor
143 model.SelectAll(); 139 model.SelectAll();
144 model.MoveCursorLeft(false); 140 model.MoveCursorLeft(false);
145 EXPECT_EQ(0U, model.cursor_pos()); 141 EXPECT_EQ(0U, model.GetCursorPosition());
146 model.SelectAll(); 142 model.SelectAll();
147 model.MoveCursorRight(false); 143 model.MoveCursorRight(false);
148 EXPECT_EQ(5U, model.cursor_pos()); 144 EXPECT_EQ(5U, model.GetCursorPosition());
149 } 145 }
150 146
151 TEST_F(TextfieldViewsModelTest, SelectionAndEdit) { 147 TEST_F(TextfieldViewsModelTest, SelectionAndEdit) {
152 TextfieldViewsModel model(NULL); 148 TextfieldViewsModel model(NULL);
153 model.Append(ASCIIToUTF16("HELLO")); 149 model.Append(ASCIIToUTF16("HELLO"));
154 model.MoveCursorRight(false); 150 model.MoveCursorRight(false);
155 model.MoveCursorRight(true); 151 model.MoveCursorRight(true);
156 model.MoveCursorRight(true); // select "EL" 152 model.MoveCursorRight(true); // select "EL"
157 EXPECT_TRUE(model.Backspace()); 153 EXPECT_TRUE(model.Backspace());
158 EXPECT_STR_EQ("HLO", model.text()); 154 EXPECT_STR_EQ("HLO", model.GetText());
159 155
160 model.Append(ASCIIToUTF16("ILL")); 156 model.Append(ASCIIToUTF16("ILL"));
161 model.MoveCursorRight(true); 157 model.MoveCursorRight(true);
162 model.MoveCursorRight(true); // select "LO" 158 model.MoveCursorRight(true); // select "LO"
163 EXPECT_TRUE(model.Delete()); 159 EXPECT_TRUE(model.Delete());
164 EXPECT_STR_EQ("HILL", model.text()); 160 EXPECT_STR_EQ("HILL", model.GetText());
165 EXPECT_EQ(1U, model.cursor_pos()); 161 EXPECT_EQ(1U, model.GetCursorPosition());
166 model.MoveCursorRight(true); // select "I" 162 model.MoveCursorRight(true); // select "I"
167 model.InsertChar('E'); 163 model.InsertChar('E');
168 EXPECT_STR_EQ("HELL", model.text()); 164 EXPECT_STR_EQ("HELL", model.GetText());
169 model.MoveCursorToHome(false); 165 model.MoveCursorToLeftEnd(false);
170 model.MoveCursorRight(true); // select "H" 166 model.MoveCursorRight(true); // select "H"
171 model.ReplaceChar('B'); 167 model.ReplaceChar('B');
172 EXPECT_STR_EQ("BELL", model.text()); 168 EXPECT_STR_EQ("BELL", model.GetText());
173 model.MoveCursorToEnd(false); 169 model.MoveCursorToRightEnd(false);
174 model.MoveCursorLeft(true); 170 model.MoveCursorLeft(true);
175 model.MoveCursorLeft(true); // select ">LL" 171 model.MoveCursorLeft(true); // select ">LL"
176 model.ReplaceChar('E'); 172 model.ReplaceChar('E');
177 EXPECT_STR_EQ("BEE", model.text()); 173 EXPECT_STR_EQ("BEE", model.GetText());
178 } 174 }
179 175
180 TEST_F(TextfieldViewsModelTest, Password) { 176 TEST_F(TextfieldViewsModelTest, Password) {
181 TextfieldViewsModel model(NULL); 177 TextfieldViewsModel model(NULL);
182 model.set_is_password(true); 178 model.set_is_password(true);
183 model.Append(ASCIIToUTF16("HELLO")); 179 model.Append(ASCIIToUTF16("HELLO"));
184 EXPECT_STR_EQ("*****", model.GetVisibleText()); 180 EXPECT_STR_EQ("*****", model.GetVisibleText());
185 EXPECT_STR_EQ("HELLO", model.text()); 181 EXPECT_STR_EQ("HELLO", model.GetText());
186 EXPECT_TRUE(model.Delete()); 182 EXPECT_TRUE(model.Delete());
187 183
188 EXPECT_STR_EQ("****", model.GetVisibleText()); 184 EXPECT_STR_EQ("****", model.GetVisibleText());
189 EXPECT_STR_EQ("ELLO", model.text()); 185 EXPECT_STR_EQ("ELLO", model.GetText());
190 EXPECT_EQ(0U, model.cursor_pos()); 186 EXPECT_EQ(0U, model.GetCursorPosition());
191 187
192 model.SelectAll(); 188 model.SelectAll();
193 EXPECT_STR_EQ("ELLO", model.GetSelectedText()); 189 EXPECT_STR_EQ("ELLO", model.GetSelectedText());
194 EXPECT_EQ(4U, model.cursor_pos()); 190 EXPECT_EQ(4U, model.GetCursorPosition());
195 191
196 model.InsertChar('X'); 192 model.InsertChar('X');
197 EXPECT_STR_EQ("*", model.GetVisibleText()); 193 EXPECT_STR_EQ("*", model.GetVisibleText());
198 EXPECT_STR_EQ("X", model.text()); 194 EXPECT_STR_EQ("X", model.GetText());
199 } 195 }
200 196
201 TEST_F(TextfieldViewsModelTest, Word) { 197 TEST_F(TextfieldViewsModelTest, Word) {
202 TextfieldViewsModel model(NULL); 198 TextfieldViewsModel model(NULL);
203 model.Append( 199 model.Append(
204 ASCIIToUTF16("The answer to Life, the Universe, and Everything")); 200 ASCIIToUTF16("The answer to Life, the Universe, and Everything"));
205 model.MoveCursorToNextWord(false); 201 model.MoveCursorRightByWord(false);
206 EXPECT_EQ(3U, model.cursor_pos()); 202 EXPECT_EQ(3U, model.GetCursorPosition());
207 model.MoveCursorToNextWord(false); 203 model.MoveCursorRightByWord(false);
208 EXPECT_EQ(10U, model.cursor_pos()); 204 EXPECT_EQ(10U, model.GetCursorPosition());
209 model.MoveCursorToNextWord(false); 205 model.MoveCursorRightByWord(false);
210 model.MoveCursorToNextWord(false); 206 model.MoveCursorRightByWord(false);
211 EXPECT_EQ(18U, model.cursor_pos()); 207 EXPECT_EQ(18U, model.GetCursorPosition());
212 208
213 // Should passes the non word char ',' 209 // Should passes the non word char ','
214 model.MoveCursorToNextWord(true); 210 model.MoveCursorRightByWord(true);
215 EXPECT_EQ(23U, model.cursor_pos()); 211 EXPECT_EQ(23U, model.GetCursorPosition());
216 EXPECT_STR_EQ(", the", model.GetSelectedText()); 212 EXPECT_STR_EQ(", the", model.GetSelectedText());
217 213
218 // Move to the end. 214 // Move to the end.
219 model.MoveCursorToNextWord(true); 215 model.MoveCursorRightByWord(true);
220 model.MoveCursorToNextWord(true); 216 model.MoveCursorRightByWord(true);
221 model.MoveCursorToNextWord(true); 217 model.MoveCursorRightByWord(true);
222 EXPECT_STR_EQ(", the Universe, and Everything", model.GetSelectedText()); 218 EXPECT_STR_EQ(", the Universe, and Everything", model.GetSelectedText());
223 // Should be safe to go next word at the end. 219 // Should be safe to go next word at the end.
224 model.MoveCursorToNextWord(true); 220 model.MoveCursorRightByWord(true);
225 EXPECT_STR_EQ(", the Universe, and Everything", model.GetSelectedText()); 221 EXPECT_STR_EQ(", the Universe, and Everything", model.GetSelectedText());
226 model.InsertChar('2'); 222 model.InsertChar('2');
227 EXPECT_EQ(19U, model.cursor_pos()); 223 EXPECT_EQ(19U, model.GetCursorPosition());
228 224
229 // Now backwards. 225 // Now backwards.
230 model.MoveCursorLeft(false); // leave 2. 226 model.MoveCursorLeft(false); // leave 2.
231 model.MoveCursorToPreviousWord(true); 227 model.MoveCursorLeftByWord(true);
232 EXPECT_EQ(14U, model.cursor_pos()); 228 EXPECT_EQ(14U, model.GetCursorPosition());
233 EXPECT_STR_EQ("Life", model.GetSelectedText()); 229 EXPECT_STR_EQ("Life", model.GetSelectedText());
234 model.MoveCursorToPreviousWord(true); 230 model.MoveCursorLeftByWord(true);
235 EXPECT_STR_EQ("to Life", model.GetSelectedText()); 231 EXPECT_STR_EQ("to Life", model.GetSelectedText());
236 model.MoveCursorToPreviousWord(true); 232 model.MoveCursorLeftByWord(true);
237 model.MoveCursorToPreviousWord(true); 233 model.MoveCursorLeftByWord(true);
238 model.MoveCursorToPreviousWord(true); // Select to the begining. 234 model.MoveCursorLeftByWord(true); // Select to the begining.
239 EXPECT_STR_EQ("The answer to Life", model.GetSelectedText()); 235 EXPECT_STR_EQ("The answer to Life", model.GetSelectedText());
240 // Should be safe to go pervious word at the begining. 236 // Should be safe to go pervious word at the begining.
241 model.MoveCursorToPreviousWord(true); 237 model.MoveCursorLeftByWord(true);
242 EXPECT_STR_EQ("The answer to Life", model.GetSelectedText()); 238 EXPECT_STR_EQ("The answer to Life", model.GetSelectedText());
243 model.ReplaceChar('4'); 239 model.ReplaceChar('4');
244 EXPECT_EQ(string16(), model.GetSelectedText()); 240 EXPECT_EQ(string16(), model.GetSelectedText());
245 EXPECT_STR_EQ("42", model.GetVisibleText()); 241 EXPECT_STR_EQ("42", model.GetVisibleText());
246 } 242 }
247 243
248 TEST_F(TextfieldViewsModelTest, TextFragment) {
249 TextfieldViewsModel model(NULL);
250 TextfieldViewsModel::TextFragments fragments;
251 // Empty string has no fragment.
252 model.GetFragments(&fragments);
253 EXPECT_EQ(0U, fragments.size());
254
255 // Some string
256 model.Append(ASCIIToUTF16("Hello world"));
257 model.GetFragments(&fragments);
258 EXPECT_EQ(1U, fragments.size());
259 EXPECT_EQ(0U, fragments[0].range.start());
260 EXPECT_EQ(11U, fragments[0].range.end());
261
262 // Selection won't change fragment.
263 model.MoveCursorToNextWord(true);
264 model.GetFragments(&fragments);
265 EXPECT_EQ(1U, fragments.size());
266 EXPECT_EQ(0U, fragments[0].range.start());
267 EXPECT_EQ(11U, fragments[0].range.end());
268 }
269
270 TEST_F(TextfieldViewsModelTest, SetText) { 244 TEST_F(TextfieldViewsModelTest, SetText) {
271 TextfieldViewsModel model(NULL); 245 TextfieldViewsModel model(NULL);
272 model.Append(ASCIIToUTF16("HELLO")); 246 model.Append(ASCIIToUTF16("HELLO"));
273 model.MoveCursorToEnd(false); 247 model.MoveCursorToRightEnd(false);
274 model.SetText(ASCIIToUTF16("GOODBYE")); 248 model.SetText(ASCIIToUTF16("GOODBYE"));
275 EXPECT_STR_EQ("GOODBYE", model.text()); 249 EXPECT_STR_EQ("GOODBYE", model.GetText());
276 // SetText won't reset the cursor posistion. 250 // SetText won't reset the cursor posistion.
277 EXPECT_EQ(5U, model.cursor_pos()); 251 EXPECT_EQ(5U, model.GetCursorPosition());
278 model.SelectAll(); 252 model.SelectAll();
279 EXPECT_STR_EQ("GOODBYE", model.GetSelectedText()); 253 EXPECT_STR_EQ("GOODBYE", model.GetSelectedText());
280 model.MoveCursorToEnd(false); 254 model.MoveCursorToRightEnd(false);
281 EXPECT_EQ(7U, model.cursor_pos()); 255 EXPECT_EQ(7U, model.GetCursorPosition());
282 256
283 model.SetText(ASCIIToUTF16("BYE")); 257 model.SetText(ASCIIToUTF16("BYE"));
284 // Setting shorter string moves the cursor to the end of the new string. 258 // Setting shorter string moves the cursor to the end of the new string.
285 EXPECT_EQ(3U, model.cursor_pos()); 259 EXPECT_EQ(3U, model.GetCursorPosition());
286 EXPECT_EQ(string16(), model.GetSelectedText()); 260 EXPECT_EQ(string16(), model.GetSelectedText());
287 model.SetText(ASCIIToUTF16("")); 261 model.SetText(ASCIIToUTF16(""));
288 EXPECT_EQ(0U, model.cursor_pos()); 262 EXPECT_EQ(0U, model.GetCursorPosition());
289 } 263 }
290 264
291 TEST_F(TextfieldViewsModelTest, Clipboard) { 265 TEST_F(TextfieldViewsModelTest, Clipboard) {
292 ui::Clipboard* clipboard 266 ui::Clipboard* clipboard
293 = views::ViewsDelegate::views_delegate->GetClipboard(); 267 = views::ViewsDelegate::views_delegate->GetClipboard();
294 string16 initial_clipboard_text; 268 string16 initial_clipboard_text;
295 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &initial_clipboard_text); 269 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &initial_clipboard_text);
296 string16 clipboard_text; 270 string16 clipboard_text;
297 TextfieldViewsModel model(NULL); 271 TextfieldViewsModel model(NULL);
298 model.Append(ASCIIToUTF16("HELLO WORLD")); 272 model.Append(ASCIIToUTF16("HELLO WORLD"));
299 model.MoveCursorToEnd(false); 273 model.MoveCursorToRightEnd(false);
300 274
301 // Test for cut: Empty selection. 275 // Test for cut: Empty selection.
302 EXPECT_FALSE(model.Cut()); 276 EXPECT_FALSE(model.Cut());
303 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text); 277 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
304 EXPECT_STR_EQ(UTF16ToUTF8(initial_clipboard_text), clipboard_text); 278 EXPECT_STR_EQ(UTF16ToUTF8(initial_clipboard_text), clipboard_text);
305 EXPECT_STR_EQ("HELLO WORLD", model.text()); 279 EXPECT_STR_EQ("HELLO WORLD", model.GetText());
306 EXPECT_EQ(11U, model.cursor_pos()); 280 EXPECT_EQ(11U, model.GetCursorPosition());
307 281
308 // Test for cut: Non-empty selection. 282 // Test for cut: Non-empty selection.
309 model.MoveCursorToPreviousWord(true); 283 model.MoveCursorLeftByWord(true);
310 EXPECT_TRUE(model.Cut()); 284 EXPECT_TRUE(model.Cut());
311 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text); 285 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
312 EXPECT_STR_EQ("WORLD", clipboard_text); 286 EXPECT_STR_EQ("WORLD", clipboard_text);
313 EXPECT_STR_EQ("HELLO ", model.text()); 287 EXPECT_STR_EQ("HELLO ", model.GetText());
314 EXPECT_EQ(6U, model.cursor_pos()); 288 EXPECT_EQ(6U, model.GetCursorPosition());
315 289
316 // Test for copy: Empty selection. 290 // Test for copy: Empty selection.
317 model.Copy(); 291 model.Copy();
318 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text); 292 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
319 EXPECT_STR_EQ("WORLD", clipboard_text); 293 EXPECT_STR_EQ("WORLD", clipboard_text);
320 EXPECT_STR_EQ("HELLO ", model.text()); 294 EXPECT_STR_EQ("HELLO ", model.GetText());
321 EXPECT_EQ(6U, model.cursor_pos()); 295 EXPECT_EQ(6U, model.GetCursorPosition());
322 296
323 // Test for copy: Non-empty selection. 297 // Test for copy: Non-empty selection.
324 model.Append(ASCIIToUTF16("HELLO WORLD")); 298 model.Append(ASCIIToUTF16("HELLO WORLD"));
325 model.SelectAll(); 299 model.SelectAll();
326 model.Copy(); 300 model.Copy();
327 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text); 301 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
328 EXPECT_STR_EQ("HELLO HELLO WORLD", clipboard_text); 302 EXPECT_STR_EQ("HELLO HELLO WORLD", clipboard_text);
329 EXPECT_STR_EQ("HELLO HELLO WORLD", model.text()); 303 EXPECT_STR_EQ("HELLO HELLO WORLD", model.GetText());
330 EXPECT_EQ(17U, model.cursor_pos()); 304 EXPECT_EQ(17U, model.GetCursorPosition());
331 305
332 // Test for paste. 306 // Test for paste.
333 model.ClearSelection(); 307 model.ClearSelection();
334 model.MoveCursorToEnd(false); 308 model.MoveCursorToRightEnd(false);
335 model.MoveCursorToPreviousWord(true); 309 model.MoveCursorLeftByWord(true);
336 EXPECT_TRUE(model.Paste()); 310 EXPECT_TRUE(model.Paste());
337 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text); 311 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &clipboard_text);
338 EXPECT_STR_EQ("HELLO HELLO WORLD", clipboard_text); 312 EXPECT_STR_EQ("HELLO HELLO WORLD", clipboard_text);
339 EXPECT_STR_EQ("HELLO HELLO HELLO HELLO WORLD", model.text()); 313 EXPECT_STR_EQ("HELLO HELLO HELLO HELLO WORLD", model.GetText());
340 EXPECT_EQ(29U, model.cursor_pos()); 314 EXPECT_EQ(29U, model.GetCursorPosition());
341 } 315 }
342 316
343 void SelectWordTestVerifier(TextfieldViewsModel &model, 317 void SelectWordTestVerifier(TextfieldViewsModel &model,
344 const std::string &expected_selected_string, size_t expected_cursor_pos) { 318 const std::string &expected_selected_string, size_t expected_cursor_pos) {
345 EXPECT_STR_EQ(expected_selected_string, model.GetSelectedText()); 319 EXPECT_STR_EQ(expected_selected_string, model.GetSelectedText());
346 EXPECT_EQ(expected_cursor_pos, model.cursor_pos()); 320 EXPECT_EQ(expected_cursor_pos, model.GetCursorPosition());
347 } 321 }
348 322
349 TEST_F(TextfieldViewsModelTest, SelectWordTest) { 323 TEST_F(TextfieldViewsModelTest, SelectWordTest) {
350 TextfieldViewsModel model(NULL); 324 TextfieldViewsModel model(NULL);
351 model.Append(ASCIIToUTF16(" HELLO !! WO RLD ")); 325 model.Append(ASCIIToUTF16(" HELLO !! WO RLD "));
352 326
353 // Test when cursor is at the beginning. 327 // Test when cursor is at the beginning.
354 model.MoveCursorToHome(false); 328 model.MoveCursorToLeftEnd(false);
355 model.SelectWord(); 329 model.SelectWord();
356 SelectWordTestVerifier(model, " ", 2U); 330 SelectWordTestVerifier(model, " ", 2U);
357 331
358 // Test when cursor is at the beginning of a word. 332 // Test when cursor is at the beginning of a word.
359 model.MoveCursorTo(2U, false); 333 model.MoveCursorTo(2U, false);
360 model.SelectWord(); 334 model.SelectWord();
361 SelectWordTestVerifier(model, "HELLO", 7U); 335 SelectWordTestVerifier(model, "HELLO", 7U);
362 336
363 // Test when cursor is at the end of a word. 337 // Test when cursor is at the end of a word.
364 model.MoveCursorTo(15U, false); 338 model.MoveCursorTo(15U, false);
365 model.SelectWord(); 339 model.SelectWord();
366 SelectWordTestVerifier(model, "WO", 15U); 340 SelectWordTestVerifier(model, "WO", 15U);
367 341
368 // Test when cursor is somewhere in a non-alph-numeric fragment. 342 // Test when cursor is somewhere in a non-alph-numeric fragment.
369 for (size_t cursor_pos = 8; cursor_pos < 13U; cursor_pos++) { 343 for (size_t cursor_pos = 8; cursor_pos < 13U; cursor_pos++) {
370 model.MoveCursorTo(cursor_pos, false); 344 model.MoveCursorTo(cursor_pos, false);
371 model.SelectWord(); 345 model.SelectWord();
372 SelectWordTestVerifier(model, " !! ", 13U); 346 SelectWordTestVerifier(model, " !! ", 13U);
373 } 347 }
374 348
375 // Test when cursor is somewhere in a whitespace fragment. 349 // Test when cursor is somewhere in a whitespace fragment.
376 model.MoveCursorTo(17U, false); 350 model.MoveCursorTo(17U, false);
377 model.SelectWord(); 351 model.SelectWord();
378 SelectWordTestVerifier(model, " ", 20U); 352 SelectWordTestVerifier(model, " ", 20U);
379 353
380 // Test when cursor is at the end. 354 // Test when cursor is at the end.
381 model.MoveCursorToEnd(false); 355 model.MoveCursorToRightEnd(false);
382 model.SelectWord(); 356 model.SelectWord();
383 SelectWordTestVerifier(model, " ", 24U); 357 SelectWordTestVerifier(model, " ", 24U);
384 } 358 }
385 359
386 TEST_F(TextfieldViewsModelTest, RangeTest) { 360 TEST_F(TextfieldViewsModelTest, RangeTest) {
387 TextfieldViewsModel model(NULL); 361 TextfieldViewsModel model(NULL);
388 model.Append(ASCIIToUTF16("HELLO WORLD")); 362 model.Append(ASCIIToUTF16("HELLO WORLD"));
389 model.MoveCursorToHome(false); 363 model.MoveCursorToLeftEnd(false);
390 ui::Range range; 364 ui::Range range;
391 model.GetSelectedRange(&range); 365 model.GetSelectedRange(&range);
392 EXPECT_TRUE(range.is_empty()); 366 EXPECT_TRUE(range.is_empty());
393 EXPECT_EQ(0U, range.start()); 367 EXPECT_EQ(0U, range.start());
394 EXPECT_EQ(0U, range.end()); 368 EXPECT_EQ(0U, range.end());
395 369
396 model.MoveCursorToNextWord(true); 370 model.MoveCursorRightByWord(true);
397 model.GetSelectedRange(&range); 371 model.GetSelectedRange(&range);
398 EXPECT_FALSE(range.is_empty()); 372 EXPECT_FALSE(range.is_empty());
399 EXPECT_FALSE(range.is_reversed()); 373 EXPECT_FALSE(range.is_reversed());
400 EXPECT_EQ(0U, range.start()); 374 EXPECT_EQ(0U, range.start());
401 EXPECT_EQ(5U, range.end()); 375 EXPECT_EQ(5U, range.end());
402 376
403 model.MoveCursorLeft(true); 377 model.MoveCursorLeft(true);
404 model.GetSelectedRange(&range); 378 model.GetSelectedRange(&range);
405 EXPECT_FALSE(range.is_empty()); 379 EXPECT_FALSE(range.is_empty());
406 EXPECT_EQ(0U, range.start()); 380 EXPECT_EQ(0U, range.start());
407 EXPECT_EQ(4U, range.end()); 381 EXPECT_EQ(4U, range.end());
408 382
409 model.MoveCursorToPreviousWord(true); 383 model.MoveCursorLeftByWord(true);
410 model.GetSelectedRange(&range); 384 model.GetSelectedRange(&range);
411 EXPECT_TRUE(range.is_empty()); 385 EXPECT_TRUE(range.is_empty());
412 EXPECT_EQ(0U, range.start()); 386 EXPECT_EQ(0U, range.start());
413 EXPECT_EQ(0U, range.end()); 387 EXPECT_EQ(0U, range.end());
414 388
415 // now from the end. 389 // now from the end.
416 model.MoveCursorToEnd(false); 390 model.MoveCursorToRightEnd(false);
417 model.GetSelectedRange(&range); 391 model.GetSelectedRange(&range);
418 EXPECT_TRUE(range.is_empty()); 392 EXPECT_TRUE(range.is_empty());
419 EXPECT_EQ(11U, range.start()); 393 EXPECT_EQ(11U, range.start());
420 EXPECT_EQ(11U, range.end()); 394 EXPECT_EQ(11U, range.end());
421 395
422 model.MoveCursorToPreviousWord(true); 396 model.MoveCursorLeftByWord(true);
423 model.GetSelectedRange(&range); 397 model.GetSelectedRange(&range);
424 EXPECT_FALSE(range.is_empty()); 398 EXPECT_FALSE(range.is_empty());
425 EXPECT_TRUE(range.is_reversed()); 399 EXPECT_TRUE(range.is_reversed());
426 EXPECT_EQ(11U, range.start()); 400 EXPECT_EQ(11U, range.start());
427 EXPECT_EQ(6U, range.end()); 401 EXPECT_EQ(6U, range.end());
428 402
429 model.MoveCursorRight(true); 403 model.MoveCursorRight(true);
430 model.GetSelectedRange(&range); 404 model.GetSelectedRange(&range);
431 EXPECT_FALSE(range.is_empty()); 405 EXPECT_FALSE(range.is_empty());
432 EXPECT_TRUE(range.is_reversed()); 406 EXPECT_TRUE(range.is_reversed());
433 EXPECT_EQ(11U, range.start()); 407 EXPECT_EQ(11U, range.start());
434 EXPECT_EQ(7U, range.end()); 408 EXPECT_EQ(7U, range.end());
435 409
436 model.MoveCursorToNextWord(true); 410 model.MoveCursorRightByWord(true);
437 model.GetSelectedRange(&range); 411 model.GetSelectedRange(&range);
438 EXPECT_TRUE(range.is_empty()); 412 EXPECT_TRUE(range.is_empty());
439 EXPECT_EQ(11U, range.start()); 413 EXPECT_EQ(11U, range.start());
440 EXPECT_EQ(11U, range.end()); 414 EXPECT_EQ(11U, range.end());
441 415
442 // Select All 416 // Select All
443 model.MoveCursorToHome(true); 417 model.MoveCursorToLeftEnd(true);
444 model.GetSelectedRange(&range); 418 model.GetSelectedRange(&range);
445 EXPECT_FALSE(range.is_empty()); 419 EXPECT_FALSE(range.is_empty());
446 EXPECT_TRUE(range.is_reversed()); 420 EXPECT_TRUE(range.is_reversed());
447 EXPECT_EQ(11U, range.start()); 421 EXPECT_EQ(11U, range.start());
448 EXPECT_EQ(0U, range.end()); 422 EXPECT_EQ(0U, range.end());
449 } 423 }
450 424
451 TEST_F(TextfieldViewsModelTest, SelectRangeTest) { 425 TEST_F(TextfieldViewsModelTest, SelectRangeTest) {
452 TextfieldViewsModel model(NULL); 426 TextfieldViewsModel model(NULL);
453 model.Append(ASCIIToUTF16("HELLO WORLD")); 427 model.Append(ASCIIToUTF16("HELLO WORLD"));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 EXPECT_TRUE(range.is_empty()); 464 EXPECT_TRUE(range.is_empty());
491 model.SelectRange(range); 465 model.SelectRange(range);
492 EXPECT_TRUE(model.GetSelectedText().empty()); 466 EXPECT_TRUE(model.GetSelectedText().empty());
493 } 467 }
494 468
495 TEST_F(TextfieldViewsModelTest, CompositionTextTest) { 469 TEST_F(TextfieldViewsModelTest, CompositionTextTest) {
496 TextfieldViewsModel model(this); 470 TextfieldViewsModel model(this);
497 model.Append(ASCIIToUTF16("1234590")); 471 model.Append(ASCIIToUTF16("1234590"));
498 model.SelectRange(ui::Range(5, 5)); 472 model.SelectRange(ui::Range(5, 5));
499 EXPECT_FALSE(model.HasSelection()); 473 EXPECT_FALSE(model.HasSelection());
500 EXPECT_EQ(5U, model.cursor_pos()); 474 EXPECT_EQ(5U, model.GetCursorPosition());
501 475
502 ui::Range range; 476 ui::Range range;
503 model.GetTextRange(&range); 477 model.GetTextRange(&range);
504 EXPECT_EQ(0U, range.start()); 478 EXPECT_EQ(0U, range.start());
505 EXPECT_EQ(7U, range.end()); 479 EXPECT_EQ(7U, range.end());
506 480
507 ui::CompositionText composition; 481 ui::CompositionText composition;
508 composition.text = ASCIIToUTF16("678"); 482 composition.text = ASCIIToUTF16("678");
509 composition.underlines.push_back(ui::CompositionUnderline(0, 3, 0, false)); 483 composition.underlines.push_back(ui::CompositionUnderline(0, 3, 0, false));
510 composition.selection = ui::Range(2, 3); 484 composition.selection = ui::Range(2, 3);
511 model.SetCompositionText(composition); 485 model.SetCompositionText(composition);
512 EXPECT_TRUE(model.HasCompositionText()); 486 EXPECT_TRUE(model.HasCompositionText());
513 EXPECT_TRUE(model.HasSelection()); 487 EXPECT_TRUE(model.HasSelection());
514 488
515 model.GetTextRange(&range); 489 model.GetTextRange(&range);
516 EXPECT_EQ(10U, range.end()); 490 EXPECT_EQ(10U, range.end());
517 EXPECT_STR_EQ("1234567890", model.text()); 491 EXPECT_STR_EQ("1234567890", model.GetText());
518 492
519 model.GetCompositionTextRange(&range); 493 model.GetCompositionTextRange(&range);
520 EXPECT_EQ(5U, range.start()); 494 EXPECT_EQ(5U, range.start());
521 EXPECT_EQ(8U, range.end()); 495 EXPECT_EQ(8U, range.end());
522 // composition text 496 // composition text
523 EXPECT_STR_EQ("456", model.GetTextFromRange(ui::Range(3, 6))); 497 EXPECT_STR_EQ("456", model.GetTextFromRange(ui::Range(3, 6)));
524 498
525 model.GetSelectedRange(&range); 499 model.GetSelectedRange(&range);
526 EXPECT_EQ(7U, range.start()); 500 EXPECT_EQ(7U, range.start());
527 EXPECT_EQ(8U, range.end()); 501 EXPECT_EQ(8U, range.end());
528 EXPECT_STR_EQ("8", model.GetSelectedText()); 502 EXPECT_STR_EQ("8", model.GetSelectedText());
529 503
530 TextfieldViewsModel::TextFragments fragments; 504 // TODO(msw): Test composition style?
531 model.GetFragments(&fragments);
532 EXPECT_EQ(3U, fragments.size());
533 EXPECT_EQ(0U, fragments[0].range.start());
534 EXPECT_EQ(5U, fragments[0].range.end());
535 EXPECT_FALSE(fragments[0].style->underline());
536
537 EXPECT_EQ(5U, fragments[1].range.start());
538 EXPECT_EQ(8U, fragments[1].range.end());
539 EXPECT_TRUE(fragments[1].style->underline());
540
541 EXPECT_EQ(8U, fragments[2].range.start());
542 EXPECT_EQ(10U, fragments[2].range.end());
543 EXPECT_FALSE(fragments[2].style->underline());
544 505
545 EXPECT_FALSE(composition_text_confirmed_or_cleared_); 506 EXPECT_FALSE(composition_text_confirmed_or_cleared_);
546 model.CancelCompositionText(); 507 model.CancelCompositionText();
547 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 508 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
548 composition_text_confirmed_or_cleared_ = false; 509 composition_text_confirmed_or_cleared_ = false;
549 EXPECT_FALSE(model.HasCompositionText()); 510 EXPECT_FALSE(model.HasCompositionText());
550 EXPECT_FALSE(model.HasSelection()); 511 EXPECT_FALSE(model.HasSelection());
551 EXPECT_EQ(5U, model.cursor_pos()); 512 EXPECT_EQ(5U, model.GetCursorPosition());
552 513
553 model.SetCompositionText(composition); 514 model.SetCompositionText(composition);
554 EXPECT_STR_EQ("1234567890", model.text()); 515 EXPECT_STR_EQ("1234567890", model.GetText());
555 EXPECT_TRUE(model.SetText(ASCIIToUTF16("1234567890"))); 516 EXPECT_TRUE(model.SetText(ASCIIToUTF16("1234567890")));
556 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 517 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
557 composition_text_confirmed_or_cleared_ = false; 518 composition_text_confirmed_or_cleared_ = false;
558 model.MoveCursorToEnd(false); 519 model.MoveCursorToRightEnd(false);
559 520
560 model.SetCompositionText(composition); 521 model.SetCompositionText(composition);
561 EXPECT_STR_EQ("1234567890678", model.text()); 522 EXPECT_STR_EQ("1234567890678", model.GetText());
562 523
563 model.InsertText(UTF8ToUTF16("-")); 524 model.InsertText(UTF8ToUTF16("-"));
564 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 525 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
565 composition_text_confirmed_or_cleared_ = false; 526 composition_text_confirmed_or_cleared_ = false;
566 EXPECT_STR_EQ("1234567890-", model.text()); 527 EXPECT_STR_EQ("1234567890-", model.GetText());
567 EXPECT_FALSE(model.HasCompositionText()); 528 EXPECT_FALSE(model.HasCompositionText());
568 EXPECT_FALSE(model.HasSelection()); 529 EXPECT_FALSE(model.HasSelection());
569 530
570 model.MoveCursorLeft(true); 531 model.MoveCursorLeft(true);
571 EXPECT_STR_EQ("-", model.GetSelectedText()); 532 EXPECT_STR_EQ("-", model.GetSelectedText());
572 model.SetCompositionText(composition); 533 model.SetCompositionText(composition);
573 EXPECT_STR_EQ("1234567890678", model.text()); 534 EXPECT_STR_EQ("1234567890678", model.GetText());
574 535
575 model.ReplaceText(UTF8ToUTF16("-")); 536 model.ReplaceText(UTF8ToUTF16("-"));
576 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 537 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
577 composition_text_confirmed_or_cleared_ = false; 538 composition_text_confirmed_or_cleared_ = false;
578 EXPECT_STR_EQ("1234567890-", model.text()); 539 EXPECT_STR_EQ("1234567890-", model.GetText());
579 EXPECT_FALSE(model.HasCompositionText()); 540 EXPECT_FALSE(model.HasCompositionText());
580 EXPECT_FALSE(model.HasSelection()); 541 EXPECT_FALSE(model.HasSelection());
581 542
582 model.SetCompositionText(composition); 543 model.SetCompositionText(composition);
583 model.Append(UTF8ToUTF16("-")); 544 model.Append(UTF8ToUTF16("-"));
584 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 545 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
585 composition_text_confirmed_or_cleared_ = false; 546 composition_text_confirmed_or_cleared_ = false;
586 EXPECT_STR_EQ("1234567890-678-", model.text()); 547 EXPECT_STR_EQ("1234567890-678-", model.GetText());
587 548
588 model.SetCompositionText(composition); 549 model.SetCompositionText(composition);
589 model.Delete(); 550 model.Delete();
590 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 551 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
591 composition_text_confirmed_or_cleared_ = false; 552 composition_text_confirmed_or_cleared_ = false;
592 EXPECT_STR_EQ("1234567890-678-", model.text()); 553 EXPECT_STR_EQ("1234567890-678-", model.GetText());
593 554
594 model.SetCompositionText(composition); 555 model.SetCompositionText(composition);
595 model.Backspace(); 556 model.Backspace();
596 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 557 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
597 composition_text_confirmed_or_cleared_ = false; 558 composition_text_confirmed_or_cleared_ = false;
598 EXPECT_STR_EQ("1234567890-678-", model.text()); 559 EXPECT_STR_EQ("1234567890-678-", model.GetText());
599 560
600 model.SetText(string16()); 561 model.SetText(string16());
601 model.SetCompositionText(composition); 562 model.SetCompositionText(composition);
602 model.MoveCursorLeft(false); 563 model.MoveCursorLeft(false);
603 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 564 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
604 composition_text_confirmed_or_cleared_ = false; 565 composition_text_confirmed_or_cleared_ = false;
605 EXPECT_STR_EQ("678", model.text()); 566 EXPECT_STR_EQ("678", model.GetText());
606 EXPECT_EQ(2U, model.cursor_pos()); 567 EXPECT_EQ(2U, model.GetCursorPosition());
607 568
608 model.SetCompositionText(composition); 569 model.SetCompositionText(composition);
609 model.MoveCursorRight(false); 570 model.MoveCursorRight(false);
610 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 571 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
611 composition_text_confirmed_or_cleared_ = false; 572 composition_text_confirmed_or_cleared_ = false;
612 EXPECT_STR_EQ("676788", model.text()); 573 EXPECT_STR_EQ("676788", model.GetText());
613 EXPECT_EQ(6U, model.cursor_pos()); 574 EXPECT_EQ(6U, model.GetCursorPosition());
614 575
615 model.SetCompositionText(composition); 576 model.SetCompositionText(composition);
616 model.MoveCursorToPreviousWord(false); 577 model.MoveCursorLeftByWord(false);
617 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 578 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
618 composition_text_confirmed_or_cleared_ = false; 579 composition_text_confirmed_or_cleared_ = false;
619 EXPECT_STR_EQ("676788678", model.text()); 580 EXPECT_STR_EQ("676788678", model.GetText());
620 581
621 model.SetText(string16()); 582 model.SetText(string16());
622 model.SetCompositionText(composition); 583 model.SetCompositionText(composition);
623 model.MoveCursorToNextWord(false); 584 model.MoveCursorRightByWord(false);
624 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 585 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
625 composition_text_confirmed_or_cleared_ = false; 586 composition_text_confirmed_or_cleared_ = false;
626 587
627 model.SetCompositionText(composition); 588 model.SetCompositionText(composition);
628 model.MoveCursorToHome(true); 589 model.MoveCursorToLeftEnd(true);
629 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 590 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
630 composition_text_confirmed_or_cleared_ = false; 591 composition_text_confirmed_or_cleared_ = false;
631 EXPECT_STR_EQ("678678", model.text()); 592 EXPECT_STR_EQ("678678", model.GetText());
632 593
633 model.SetCompositionText(composition); 594 model.SetCompositionText(composition);
634 model.MoveCursorToEnd(false); 595 model.MoveCursorToRightEnd(false);
635 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 596 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
636 composition_text_confirmed_or_cleared_ = false; 597 composition_text_confirmed_or_cleared_ = false;
637 EXPECT_STR_EQ("678", model.text()); 598 EXPECT_STR_EQ("678", model.GetText());
638 599
639 model.SetCompositionText(composition); 600 model.SetCompositionText(composition);
640 model.MoveCursorTo(0, true); 601 model.MoveCursorTo(0, true);
641 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 602 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
642 composition_text_confirmed_or_cleared_ = false; 603 composition_text_confirmed_or_cleared_ = false;
643 EXPECT_STR_EQ("678678", model.text()); 604 EXPECT_STR_EQ("678678", model.GetText());
644 605
645 model.SetCompositionText(composition); 606 model.SetCompositionText(composition);
646 model.SelectRange(ui::Range(0, 3)); 607 model.SelectRange(ui::Range(0, 3));
647 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 608 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
648 composition_text_confirmed_or_cleared_ = false; 609 composition_text_confirmed_or_cleared_ = false;
649 EXPECT_STR_EQ("678", model.text()); 610 EXPECT_STR_EQ("678", model.GetText());
650 611
651 model.SetCompositionText(composition); 612 model.SetCompositionText(composition);
652 model.SelectAll(); 613 model.SelectAll();
653 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 614 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
654 composition_text_confirmed_or_cleared_ = false; 615 composition_text_confirmed_or_cleared_ = false;
655 EXPECT_STR_EQ("678", model.text()); 616 EXPECT_STR_EQ("678", model.GetText());
656 617
657 model.SetCompositionText(composition); 618 model.SetCompositionText(composition);
658 model.SelectWord(); 619 model.SelectWord();
659 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 620 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
660 composition_text_confirmed_or_cleared_ = false; 621 composition_text_confirmed_or_cleared_ = false;
661 EXPECT_STR_EQ("678", model.text()); 622 EXPECT_STR_EQ("678", model.GetText());
662 623
663 model.SetCompositionText(composition); 624 model.SetCompositionText(composition);
664 model.ClearSelection(); 625 model.ClearSelection();
665 EXPECT_TRUE(composition_text_confirmed_or_cleared_); 626 EXPECT_TRUE(composition_text_confirmed_or_cleared_);
666 composition_text_confirmed_or_cleared_ = false; 627 composition_text_confirmed_or_cleared_ = false;
667 628
668 model.SetCompositionText(composition); 629 model.SetCompositionText(composition);
669 EXPECT_FALSE(model.Cut()); 630 EXPECT_FALSE(model.Cut());
670 EXPECT_FALSE(composition_text_confirmed_or_cleared_); 631 EXPECT_FALSE(composition_text_confirmed_or_cleared_);
671 } 632 }
672 633
673 TEST_F(TextfieldViewsModelTest, UndoRedo_BasicTest) { 634 TEST_F(TextfieldViewsModelTest, UndoRedo_BasicTest) {
674 TextfieldViewsModel model(NULL); 635 TextfieldViewsModel model(NULL);
675 model.InsertChar('a'); 636 model.InsertChar('a');
676 EXPECT_FALSE(model.Redo()); // nothing to redo 637 EXPECT_FALSE(model.Redo()); // nothing to redo
677 EXPECT_TRUE(model.Undo()); 638 EXPECT_TRUE(model.Undo());
678 EXPECT_STR_EQ("", model.text()); 639 EXPECT_STR_EQ("", model.GetText());
679 EXPECT_TRUE(model.Redo()); 640 EXPECT_TRUE(model.Redo());
680 EXPECT_STR_EQ("a", model.text()); 641 EXPECT_STR_EQ("a", model.GetText());
681 642
682 // Continuous inserts are treated as one edit. 643 // Continuous inserts are treated as one edit.
683 model.InsertChar('b'); 644 model.InsertChar('b');
684 model.InsertChar('c'); 645 model.InsertChar('c');
685 EXPECT_STR_EQ("abc", model.text()); 646 EXPECT_STR_EQ("abc", model.GetText());
686 EXPECT_TRUE(model.Undo()); 647 EXPECT_TRUE(model.Undo());
687 EXPECT_STR_EQ("a", model.text()); 648 EXPECT_STR_EQ("a", model.GetText());
688 EXPECT_EQ(1U, model.cursor_pos()); 649 EXPECT_EQ(1U, model.GetCursorPosition());
689 EXPECT_TRUE(model.Undo()); 650 EXPECT_TRUE(model.Undo());
690 EXPECT_STR_EQ("", model.text()); 651 EXPECT_STR_EQ("", model.GetText());
691 EXPECT_EQ(0U, model.cursor_pos()); 652 EXPECT_EQ(0U, model.GetCursorPosition());
692 653
693 // Undoing further shouldn't change the text. 654 // Undoing further shouldn't change the text.
694 EXPECT_FALSE(model.Undo()); 655 EXPECT_FALSE(model.Undo());
695 EXPECT_STR_EQ("", model.text()); 656 EXPECT_STR_EQ("", model.GetText());
696 EXPECT_FALSE(model.Undo()); 657 EXPECT_FALSE(model.Undo());
697 EXPECT_STR_EQ("", model.text()); 658 EXPECT_STR_EQ("", model.GetText());
698 EXPECT_EQ(0U, model.cursor_pos()); 659 EXPECT_EQ(0U, model.GetCursorPosition());
699 660
700 // Redoing to the latest text. 661 // Redoing to the latest text.
701 EXPECT_TRUE(model.Redo()); 662 EXPECT_TRUE(model.Redo());
702 EXPECT_STR_EQ("a", model.text()); 663 EXPECT_STR_EQ("a", model.GetText());
703 EXPECT_EQ(1U, model.cursor_pos()); 664 EXPECT_EQ(1U, model.GetCursorPosition());
704 EXPECT_TRUE(model.Redo()); 665 EXPECT_TRUE(model.Redo());
705 EXPECT_STR_EQ("abc", model.text()); 666 EXPECT_STR_EQ("abc", model.GetText());
706 EXPECT_EQ(3U, model.cursor_pos()); 667 EXPECT_EQ(3U, model.GetCursorPosition());
707 668
708 // Backspace =============================== 669 // Backspace ===============================
709 EXPECT_TRUE(model.Backspace()); 670 EXPECT_TRUE(model.Backspace());
710 EXPECT_STR_EQ("ab", model.text()); 671 EXPECT_STR_EQ("ab", model.GetText());
711 EXPECT_TRUE(model.Undo()); 672 EXPECT_TRUE(model.Undo());
712 EXPECT_STR_EQ("abc", model.text()); 673 EXPECT_STR_EQ("abc", model.GetText());
713 EXPECT_EQ(3U, model.cursor_pos()); 674 EXPECT_EQ(3U, model.GetCursorPosition());
714 EXPECT_TRUE(model.Redo()); 675 EXPECT_TRUE(model.Redo());
715 EXPECT_STR_EQ("ab", model.text()); 676 EXPECT_STR_EQ("ab", model.GetText());
716 EXPECT_EQ(2U, model.cursor_pos()); 677 EXPECT_EQ(2U, model.GetCursorPosition());
717 // Continous backspaces are treated as one edit. 678 // Continous backspaces are treated as one edit.
718 EXPECT_TRUE(model.Backspace()); 679 EXPECT_TRUE(model.Backspace());
719 EXPECT_TRUE(model.Backspace()); 680 EXPECT_TRUE(model.Backspace());
720 EXPECT_STR_EQ("", model.text()); 681 EXPECT_STR_EQ("", model.GetText());
721 // Extra backspace shouldn't affect the history. 682 // Extra backspace shouldn't affect the history.
722 EXPECT_FALSE(model.Backspace()); 683 EXPECT_FALSE(model.Backspace());
723 EXPECT_TRUE(model.Undo()); 684 EXPECT_TRUE(model.Undo());
724 EXPECT_STR_EQ("ab", model.text()); 685 EXPECT_STR_EQ("ab", model.GetText());
725 EXPECT_EQ(2U, model.cursor_pos()); 686 EXPECT_EQ(2U, model.GetCursorPosition());
726 EXPECT_TRUE(model.Undo()); 687 EXPECT_TRUE(model.Undo());
727 EXPECT_STR_EQ("abc", model.text()); 688 EXPECT_STR_EQ("abc", model.GetText());
728 EXPECT_EQ(3U, model.cursor_pos()); 689 EXPECT_EQ(3U, model.GetCursorPosition());
729 EXPECT_TRUE(model.Undo()); 690 EXPECT_TRUE(model.Undo());
730 EXPECT_STR_EQ("a", model.text()); 691 EXPECT_STR_EQ("a", model.GetText());
731 EXPECT_EQ(1U, model.cursor_pos()); 692 EXPECT_EQ(1U, model.GetCursorPosition());
732 693
733 // Clear history 694 // Clear history
734 model.ClearEditHistory(); 695 model.ClearEditHistory();
735 EXPECT_FALSE(model.Undo()); 696 EXPECT_FALSE(model.Undo());
736 EXPECT_FALSE(model.Redo()); 697 EXPECT_FALSE(model.Redo());
737 EXPECT_STR_EQ("a", model.text()); 698 EXPECT_STR_EQ("a", model.GetText());
738 EXPECT_EQ(1U, model.cursor_pos()); 699 EXPECT_EQ(1U, model.GetCursorPosition());
739 700
740 // Delete =============================== 701 // Delete ===============================
741 model.SetText(ASCIIToUTF16("ABCDE")); 702 model.SetText(ASCIIToUTF16("ABCDE"));
742 model.ClearEditHistory(); 703 model.ClearEditHistory();
743 model.MoveCursorTo(2, false); 704 model.MoveCursorTo(2, false);
744 EXPECT_TRUE(model.Delete()); 705 EXPECT_TRUE(model.Delete());
745 EXPECT_STR_EQ("ABDE", model.text()); 706 EXPECT_STR_EQ("ABDE", model.GetText());
746 model.MoveCursorToHome(false); 707 model.MoveCursorToLeftEnd(false);
747 EXPECT_TRUE(model.Delete()); 708 EXPECT_TRUE(model.Delete());
748 EXPECT_STR_EQ("BDE", model.text()); 709 EXPECT_STR_EQ("BDE", model.GetText());
749 EXPECT_TRUE(model.Undo()); 710 EXPECT_TRUE(model.Undo());
750 EXPECT_STR_EQ("ABDE", model.text()); 711 EXPECT_STR_EQ("ABDE", model.GetText());
751 EXPECT_EQ(0U, model.cursor_pos()); 712 EXPECT_EQ(0U, model.GetCursorPosition());
752 EXPECT_TRUE(model.Undo()); 713 EXPECT_TRUE(model.Undo());
753 EXPECT_STR_EQ("ABCDE", model.text()); 714 EXPECT_STR_EQ("ABCDE", model.GetText());
754 EXPECT_EQ(2U, model.cursor_pos()); 715 EXPECT_EQ(2U, model.GetCursorPosition());
755 EXPECT_TRUE(model.Redo()); 716 EXPECT_TRUE(model.Redo());
756 EXPECT_STR_EQ("ABDE", model.text()); 717 EXPECT_STR_EQ("ABDE", model.GetText());
757 EXPECT_EQ(2U, model.cursor_pos()); 718 EXPECT_EQ(2U, model.GetCursorPosition());
758 // Continous deletes are treated as one edit. 719 // Continous deletes are treated as one edit.
759 EXPECT_TRUE(model.Delete()); 720 EXPECT_TRUE(model.Delete());
760 EXPECT_TRUE(model.Delete()); 721 EXPECT_TRUE(model.Delete());
761 EXPECT_STR_EQ("AB", model.text()); 722 EXPECT_STR_EQ("AB", model.GetText());
762 EXPECT_TRUE(model.Undo()); 723 EXPECT_TRUE(model.Undo());
763 EXPECT_STR_EQ("ABDE", model.text()); 724 EXPECT_STR_EQ("ABDE", model.GetText());
764 EXPECT_EQ(2U, model.cursor_pos()); 725 EXPECT_EQ(2U, model.GetCursorPosition());
765 EXPECT_TRUE(model.Redo()); 726 EXPECT_TRUE(model.Redo());
766 EXPECT_STR_EQ("AB", model.text()); 727 EXPECT_STR_EQ("AB", model.GetText());
767 EXPECT_EQ(2U, model.cursor_pos()); 728 EXPECT_EQ(2U, model.GetCursorPosition());
768 } 729 }
769 730
770 TEST_F(TextfieldViewsModelTest, UndoRedo_SetText) { 731 TEST_F(TextfieldViewsModelTest, UndoRedo_SetText) {
771 // This is to test the undo/redo behavior of omnibox. 732 // This is to test the undo/redo behavior of omnibox.
772 TextfieldViewsModel model(NULL); 733 TextfieldViewsModel model(NULL);
773 model.InsertChar('w'); 734 model.InsertChar('w');
774 EXPECT_STR_EQ("w", model.text()); 735 EXPECT_STR_EQ("w", model.GetText());
775 EXPECT_EQ(1U, model.cursor_pos()); 736 EXPECT_EQ(1U, model.GetCursorPosition());
776 model.SetText(ASCIIToUTF16("www.google.com")); 737 model.SetText(ASCIIToUTF16("www.google.com"));
777 EXPECT_EQ(1U, model.cursor_pos()); 738 EXPECT_EQ(1U, model.GetCursorPosition());
778 EXPECT_STR_EQ("www.google.com", model.text()); 739 EXPECT_STR_EQ("www.google.com", model.GetText());
779 model.SelectRange(ui::Range(14, 1)); 740 model.SelectRange(ui::Range(14, 1));
780 model.InsertChar('w'); 741 model.InsertChar('w');
781 EXPECT_STR_EQ("ww", model.text()); 742 EXPECT_STR_EQ("ww", model.GetText());
782 model.SetText(ASCIIToUTF16("www.google.com")); 743 model.SetText(ASCIIToUTF16("www.google.com"));
783 model.SelectRange(ui::Range(14, 2)); 744 model.SelectRange(ui::Range(14, 2));
784 model.InsertChar('w'); 745 model.InsertChar('w');
785 EXPECT_STR_EQ("www", model.text()); 746 EXPECT_STR_EQ("www", model.GetText());
786 model.SetText(ASCIIToUTF16("www.google.com")); 747 model.SetText(ASCIIToUTF16("www.google.com"));
787 model.SelectRange(ui::Range(14, 3)); 748 model.SelectRange(ui::Range(14, 3));
788 model.InsertChar('.'); 749 model.InsertChar('.');
789 EXPECT_STR_EQ("www.", model.text()); 750 EXPECT_STR_EQ("www.", model.GetText());
790 model.SetText(ASCIIToUTF16("www.google.com")); 751 model.SetText(ASCIIToUTF16("www.google.com"));
791 model.SelectRange(ui::Range(14, 4)); 752 model.SelectRange(ui::Range(14, 4));
792 model.InsertChar('y'); 753 model.InsertChar('y');
793 EXPECT_STR_EQ("www.y", model.text()); 754 EXPECT_STR_EQ("www.y", model.GetText());
794 model.SetText(ASCIIToUTF16("www.youtube.com")); 755 model.SetText(ASCIIToUTF16("www.youtube.com"));
795 EXPECT_STR_EQ("www.youtube.com", model.text()); 756 EXPECT_STR_EQ("www.youtube.com", model.GetText());
796 EXPECT_EQ(5U, model.cursor_pos()); 757 EXPECT_EQ(5U, model.GetCursorPosition());
797 758
798 EXPECT_TRUE(model.Undo()); 759 EXPECT_TRUE(model.Undo());
799 EXPECT_STR_EQ("www.google.com", model.text()); 760 EXPECT_STR_EQ("www.google.com", model.GetText());
800 EXPECT_EQ(4U, model.cursor_pos()); 761 EXPECT_EQ(4U, model.GetCursorPosition());
801 EXPECT_TRUE(model.Undo()); 762 EXPECT_TRUE(model.Undo());
802 EXPECT_STR_EQ("www.google.com", model.text()); 763 EXPECT_STR_EQ("www.google.com", model.GetText());
803 EXPECT_EQ(3U, model.cursor_pos()); 764 EXPECT_EQ(3U, model.GetCursorPosition());
804 EXPECT_TRUE(model.Undo()); 765 EXPECT_TRUE(model.Undo());
805 EXPECT_STR_EQ("www.google.com", model.text()); 766 EXPECT_STR_EQ("www.google.com", model.GetText());
806 EXPECT_EQ(2U, model.cursor_pos()); 767 EXPECT_EQ(2U, model.GetCursorPosition());
807 EXPECT_TRUE(model.Undo()); 768 EXPECT_TRUE(model.Undo());
808 EXPECT_STR_EQ("www.google.com", model.text()); 769 EXPECT_STR_EQ("www.google.com", model.GetText());
809 EXPECT_EQ(1U, model.cursor_pos()); 770 EXPECT_EQ(1U, model.GetCursorPosition());
810 EXPECT_TRUE(model.Undo()); 771 EXPECT_TRUE(model.Undo());
811 EXPECT_STR_EQ("", model.text()); 772 EXPECT_STR_EQ("", model.GetText());
812 EXPECT_EQ(0U, model.cursor_pos()); 773 EXPECT_EQ(0U, model.GetCursorPosition());
813 EXPECT_FALSE(model.Undo()); 774 EXPECT_FALSE(model.Undo());
814 EXPECT_TRUE(model.Redo()); 775 EXPECT_TRUE(model.Redo());
815 EXPECT_STR_EQ("www.google.com", model.text()); 776 EXPECT_STR_EQ("www.google.com", model.GetText());
816 EXPECT_EQ(1U, model.cursor_pos()); 777 EXPECT_EQ(1U, model.GetCursorPosition());
817 EXPECT_TRUE(model.Redo()); 778 EXPECT_TRUE(model.Redo());
818 EXPECT_STR_EQ("www.google.com", model.text()); 779 EXPECT_STR_EQ("www.google.com", model.GetText());
819 EXPECT_EQ(2U, model.cursor_pos()); 780 EXPECT_EQ(2U, model.GetCursorPosition());
820 EXPECT_TRUE(model.Redo()); 781 EXPECT_TRUE(model.Redo());
821 EXPECT_STR_EQ("www.google.com", model.text()); 782 EXPECT_STR_EQ("www.google.com", model.GetText());
822 EXPECT_EQ(3U, model.cursor_pos()); 783 EXPECT_EQ(3U, model.GetCursorPosition());
823 EXPECT_TRUE(model.Redo()); 784 EXPECT_TRUE(model.Redo());
824 EXPECT_STR_EQ("www.google.com", model.text()); 785 EXPECT_STR_EQ("www.google.com", model.GetText());
825 EXPECT_EQ(4U, model.cursor_pos()); 786 EXPECT_EQ(4U, model.GetCursorPosition());
826 EXPECT_TRUE(model.Redo()); 787 EXPECT_TRUE(model.Redo());
827 EXPECT_STR_EQ("www.youtube.com", model.text()); 788 EXPECT_STR_EQ("www.youtube.com", model.GetText());
828 EXPECT_EQ(5U, model.cursor_pos()); 789 EXPECT_EQ(5U, model.GetCursorPosition());
829 EXPECT_FALSE(model.Redo()); 790 EXPECT_FALSE(model.Redo());
830 } 791 }
831 792
832 TEST_F(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest) { 793 TEST_F(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest) {
833 TextfieldViewsModel model(NULL); 794 TextfieldViewsModel model(NULL);
834 model.SetText(ASCIIToUTF16("ABCDE")); 795 model.SetText(ASCIIToUTF16("ABCDE"));
835 EXPECT_FALSE(model.Redo()); // nothing to redo 796 EXPECT_FALSE(model.Redo()); // nothing to redo
836 // Cut 797 // Cut
837 model.MoveCursorTo(1, false); 798 model.MoveCursorTo(1, false);
838 model.MoveCursorTo(3, true); 799 model.MoveCursorTo(3, true);
839 model.Cut(); 800 model.Cut();
840 EXPECT_STR_EQ("ADE", model.text()); 801 EXPECT_STR_EQ("ADE", model.GetText());
841 EXPECT_EQ(1U, model.cursor_pos()); 802 EXPECT_EQ(1U, model.GetCursorPosition());
842 EXPECT_TRUE(model.Undo()); 803 EXPECT_TRUE(model.Undo());
843 EXPECT_STR_EQ("ABCDE", model.text()); 804 EXPECT_STR_EQ("ABCDE", model.GetText());
844 EXPECT_EQ(3U, model.cursor_pos()); 805 EXPECT_EQ(3U, model.GetCursorPosition());
845 EXPECT_TRUE(model.Undo()); 806 EXPECT_TRUE(model.Undo());
846 EXPECT_STR_EQ("", model.text()); 807 EXPECT_STR_EQ("", model.GetText());
847 EXPECT_EQ(0U, model.cursor_pos()); 808 EXPECT_EQ(0U, model.GetCursorPosition());
848 EXPECT_FALSE(model.Undo()); // no more undo 809 EXPECT_FALSE(model.Undo()); // no more undo
849 EXPECT_STR_EQ("", model.text()); 810 EXPECT_STR_EQ("", model.GetText());
850 EXPECT_TRUE(model.Redo()); 811 EXPECT_TRUE(model.Redo());
851 EXPECT_STR_EQ("ABCDE", model.text()); 812 EXPECT_STR_EQ("ABCDE", model.GetText());
852 EXPECT_EQ(0U, model.cursor_pos()); 813 EXPECT_EQ(0U, model.GetCursorPosition());
853 EXPECT_TRUE(model.Redo()); 814 EXPECT_TRUE(model.Redo());
854 EXPECT_STR_EQ("ADE", model.text()); 815 EXPECT_STR_EQ("ADE", model.GetText());
855 EXPECT_EQ(1U, model.cursor_pos()); 816 EXPECT_EQ(1U, model.GetCursorPosition());
856 EXPECT_FALSE(model.Redo()); // no more redo 817 EXPECT_FALSE(model.Redo()); // no more redo
857 EXPECT_STR_EQ("ADE", model.text()); 818 EXPECT_STR_EQ("ADE", model.GetText());
858 819
859 model.Paste(); 820 model.Paste();
860 model.Paste(); 821 model.Paste();
861 model.Paste(); 822 model.Paste();
862 EXPECT_STR_EQ("ABCBCBCDE", model.text()); 823 EXPECT_STR_EQ("ABCBCBCDE", model.GetText());
863 EXPECT_EQ(7U, model.cursor_pos()); 824 EXPECT_EQ(7U, model.GetCursorPosition());
864 EXPECT_TRUE(model.Undo()); 825 EXPECT_TRUE(model.Undo());
865 EXPECT_STR_EQ("ABCBCDE", model.text()); 826 EXPECT_STR_EQ("ABCBCDE", model.GetText());
866 EXPECT_EQ(5U, model.cursor_pos()); 827 EXPECT_EQ(5U, model.GetCursorPosition());
867 EXPECT_TRUE(model.Undo()); 828 EXPECT_TRUE(model.Undo());
868 EXPECT_STR_EQ("ABCDE", model.text()); 829 EXPECT_STR_EQ("ABCDE", model.GetText());
869 EXPECT_EQ(3U, model.cursor_pos()); 830 EXPECT_EQ(3U, model.GetCursorPosition());
870 EXPECT_TRUE(model.Undo()); 831 EXPECT_TRUE(model.Undo());
871 EXPECT_STR_EQ("ADE", model.text()); 832 EXPECT_STR_EQ("ADE", model.GetText());
872 EXPECT_EQ(1U, model.cursor_pos()); 833 EXPECT_EQ(1U, model.GetCursorPosition());
873 EXPECT_TRUE(model.Undo()); 834 EXPECT_TRUE(model.Undo());
874 EXPECT_STR_EQ("ABCDE", model.text()); 835 EXPECT_STR_EQ("ABCDE", model.GetText());
875 EXPECT_EQ(3U, model.cursor_pos()); 836 EXPECT_EQ(3U, model.GetCursorPosition());
876 EXPECT_TRUE(model.Undo()); 837 EXPECT_TRUE(model.Undo());
877 EXPECT_STR_EQ("", model.text()); 838 EXPECT_STR_EQ("", model.GetText());
878 EXPECT_EQ(0U, model.cursor_pos()); 839 EXPECT_EQ(0U, model.GetCursorPosition());
879 EXPECT_FALSE(model.Undo()); 840 EXPECT_FALSE(model.Undo());
880 EXPECT_STR_EQ("", model.text()); 841 EXPECT_STR_EQ("", model.GetText());
881 EXPECT_TRUE(model.Redo()); 842 EXPECT_TRUE(model.Redo());
882 EXPECT_STR_EQ("ABCDE", model.text()); // Redoing SetText 843 EXPECT_STR_EQ("ABCDE", model.GetText()); // Redoing SetText
883 EXPECT_EQ(0U, model.cursor_pos()); 844 EXPECT_EQ(0U, model.GetCursorPosition());
884 845
885 // Redo 846 // Redo
886 EXPECT_TRUE(model.Redo()); 847 EXPECT_TRUE(model.Redo());
887 EXPECT_STR_EQ("ADE", model.text()); 848 EXPECT_STR_EQ("ADE", model.GetText());
888 EXPECT_EQ(1U, model.cursor_pos()); 849 EXPECT_EQ(1U, model.GetCursorPosition());
889 EXPECT_TRUE(model.Redo()); 850 EXPECT_TRUE(model.Redo());
890 EXPECT_STR_EQ("ABCDE", model.text()); 851 EXPECT_STR_EQ("ABCDE", model.GetText());
891 EXPECT_EQ(3U, model.cursor_pos()); 852 EXPECT_EQ(3U, model.GetCursorPosition());
892 EXPECT_TRUE(model.Redo()); 853 EXPECT_TRUE(model.Redo());
893 EXPECT_STR_EQ("ABCBCDE", model.text()); 854 EXPECT_STR_EQ("ABCBCDE", model.GetText());
894 EXPECT_EQ(5U, model.cursor_pos()); 855 EXPECT_EQ(5U, model.GetCursorPosition());
895 EXPECT_TRUE(model.Redo()); 856 EXPECT_TRUE(model.Redo());
896 EXPECT_STR_EQ("ABCBCBCDE", model.text()); 857 EXPECT_STR_EQ("ABCBCBCDE", model.GetText());
897 EXPECT_EQ(7U, model.cursor_pos()); 858 EXPECT_EQ(7U, model.GetCursorPosition());
898 EXPECT_FALSE(model.Redo()); 859 EXPECT_FALSE(model.Redo());
899 860
900 // with SelectRange 861 // with SelectRange
901 model.SelectRange(ui::Range(1, 3)); 862 model.SelectRange(ui::Range(1, 3));
902 EXPECT_TRUE(model.Cut()); 863 EXPECT_TRUE(model.Cut());
903 EXPECT_STR_EQ("ABCBCDE", model.text()); 864 EXPECT_STR_EQ("ABCBCDE", model.GetText());
904 EXPECT_EQ(1U, model.cursor_pos()); 865 EXPECT_EQ(1U, model.GetCursorPosition());
905 model.SelectRange(ui::Range(1, 1)); 866 model.SelectRange(ui::Range(1, 1));
906 EXPECT_FALSE(model.Cut()); 867 EXPECT_FALSE(model.Cut());
907 model.MoveCursorToEnd(false); 868 model.MoveCursorToRightEnd(false);
908 EXPECT_TRUE(model.Paste()); 869 EXPECT_TRUE(model.Paste());
909 EXPECT_STR_EQ("ABCBCDEBC", model.text()); 870 EXPECT_STR_EQ("ABCBCDEBC", model.GetText());
910 EXPECT_EQ(9U, model.cursor_pos()); 871 EXPECT_EQ(9U, model.GetCursorPosition());
911 EXPECT_TRUE(model.Undo()); 872 EXPECT_TRUE(model.Undo());
912 EXPECT_STR_EQ("ABCBCDE", model.text()); 873 EXPECT_STR_EQ("ABCBCDE", model.GetText());
913 EXPECT_EQ(7U, model.cursor_pos()); 874 EXPECT_EQ(7U, model.GetCursorPosition());
914 // empty cut shouldn't create an edit. 875 // empty cut shouldn't create an edit.
915 EXPECT_TRUE(model.Undo()); 876 EXPECT_TRUE(model.Undo());
916 EXPECT_STR_EQ("ABCBCBCDE", model.text()); 877 EXPECT_STR_EQ("ABCBCBCDE", model.GetText());
917 EXPECT_EQ(3U, model.cursor_pos()); 878 EXPECT_EQ(3U, model.GetCursorPosition());
918 879
919 // Copy 880 // Copy
920 ResetModel(&model); 881 ResetModel(&model);
921 model.SetText(ASCIIToUTF16("12345")); 882 model.SetText(ASCIIToUTF16("12345"));
922 EXPECT_STR_EQ("12345", model.text()); 883 EXPECT_STR_EQ("12345", model.GetText());
923 EXPECT_EQ(0U, model.cursor_pos()); 884 EXPECT_EQ(0U, model.GetCursorPosition());
924 model.MoveCursorTo(1, false); 885 model.MoveCursorTo(1, false);
925 model.MoveCursorTo(3, true); 886 model.MoveCursorTo(3, true);
926 model.Copy(); // Copy "23" 887 model.Copy(); // Copy "23"
927 EXPECT_STR_EQ("12345", model.text()); 888 EXPECT_STR_EQ("12345", model.GetText());
928 EXPECT_EQ(3U, model.cursor_pos()); 889 EXPECT_EQ(3U, model.GetCursorPosition());
929 model.Paste(); // Paste "23" into "23". 890 model.Paste(); // Paste "23" into "23".
930 EXPECT_STR_EQ("12345", model.text()); 891 EXPECT_STR_EQ("12345", model.GetText());
931 EXPECT_EQ(3U, model.cursor_pos()); 892 EXPECT_EQ(3U, model.GetCursorPosition());
932 model.Paste(); 893 model.Paste();
933 EXPECT_STR_EQ("1232345", model.text()); 894 EXPECT_STR_EQ("1232345", model.GetText());
934 EXPECT_EQ(5U, model.cursor_pos()); 895 EXPECT_EQ(5U, model.GetCursorPosition());
935 EXPECT_TRUE(model.Undo()); 896 EXPECT_TRUE(model.Undo());
936 EXPECT_STR_EQ("12345", model.text()); 897 EXPECT_STR_EQ("12345", model.GetText());
937 EXPECT_EQ(3U, model.cursor_pos()); 898 EXPECT_EQ(3U, model.GetCursorPosition());
938 // TODO(oshima): We need to change the return type from bool to enum. 899 // TODO(oshima): We need to change the return type from bool to enum.
939 EXPECT_FALSE(model.Undo()); // No text change. 900 EXPECT_FALSE(model.Undo()); // No text change.
940 EXPECT_STR_EQ("12345", model.text()); 901 EXPECT_STR_EQ("12345", model.GetText());
941 EXPECT_EQ(3U, model.cursor_pos()); 902 EXPECT_EQ(3U, model.GetCursorPosition());
942 EXPECT_TRUE(model.Undo()); 903 EXPECT_TRUE(model.Undo());
943 EXPECT_STR_EQ("", model.text()); 904 EXPECT_STR_EQ("", model.GetText());
944 EXPECT_FALSE(model.Undo()); 905 EXPECT_FALSE(model.Undo());
945 // Redo 906 // Redo
946 EXPECT_TRUE(model.Redo()); 907 EXPECT_TRUE(model.Redo());
947 EXPECT_STR_EQ("12345", model.text()); 908 EXPECT_STR_EQ("12345", model.GetText());
948 EXPECT_EQ(0U, model.cursor_pos()); 909 EXPECT_EQ(0U, model.GetCursorPosition());
949 EXPECT_TRUE(model.Redo()); 910 EXPECT_TRUE(model.Redo());
950 EXPECT_STR_EQ("12345", model.text()); // For 1st paste 911 EXPECT_STR_EQ("12345", model.GetText()); // For 1st paste
951 EXPECT_EQ(3U, model.cursor_pos()); 912 EXPECT_EQ(3U, model.GetCursorPosition());
952 EXPECT_TRUE(model.Redo()); 913 EXPECT_TRUE(model.Redo());
953 EXPECT_STR_EQ("1232345", model.text()); 914 EXPECT_STR_EQ("1232345", model.GetText());
954 EXPECT_EQ(5U, model.cursor_pos()); 915 EXPECT_EQ(5U, model.GetCursorPosition());
955 EXPECT_FALSE(model.Redo()); 916 EXPECT_FALSE(model.Redo());
956 EXPECT_STR_EQ("1232345", model.text()); 917 EXPECT_STR_EQ("1232345", model.GetText());
957 918
958 // with SelectRange 919 // with SelectRange
959 model.SelectRange(ui::Range(1, 3)); 920 model.SelectRange(ui::Range(1, 3));
960 model.Copy(); 921 model.Copy();
961 EXPECT_STR_EQ("1232345", model.text()); 922 EXPECT_STR_EQ("1232345", model.GetText());
962 model.MoveCursorToEnd(false); 923 model.MoveCursorToRightEnd(false);
963 EXPECT_TRUE(model.Paste()); 924 EXPECT_TRUE(model.Paste());
964 EXPECT_STR_EQ("123234523", model.text()); 925 EXPECT_STR_EQ("123234523", model.GetText());
965 EXPECT_EQ(9U, model.cursor_pos()); 926 EXPECT_EQ(9U, model.GetCursorPosition());
966 EXPECT_TRUE(model.Undo()); 927 EXPECT_TRUE(model.Undo());
967 EXPECT_STR_EQ("1232345", model.text()); 928 EXPECT_STR_EQ("1232345", model.GetText());
968 EXPECT_EQ(7U, model.cursor_pos()); 929 EXPECT_EQ(7U, model.GetCursorPosition());
969 } 930 }
970 931
971 TEST_F(TextfieldViewsModelTest, UndoRedo_CursorTest) { 932 TEST_F(TextfieldViewsModelTest, UndoRedo_CursorTest) {
972 TextfieldViewsModel model(NULL); 933 TextfieldViewsModel model(NULL);
973 model.InsertChar('a'); 934 model.InsertChar('a');
974 model.MoveCursorLeft(false); 935 model.MoveCursorLeft(false);
975 model.MoveCursorRight(false); 936 model.MoveCursorRight(false);
976 model.InsertChar('b'); 937 model.InsertChar('b');
977 // Moving cursor shoudln't create a new edit. 938 // Moving cursor shoudln't create a new edit.
978 EXPECT_STR_EQ("ab", model.text()); 939 EXPECT_STR_EQ("ab", model.GetText());
979 EXPECT_FALSE(model.Redo()); 940 EXPECT_FALSE(model.Redo());
980 EXPECT_TRUE(model.Undo()); 941 EXPECT_TRUE(model.Undo());
981 EXPECT_STR_EQ("", model.text()); 942 EXPECT_STR_EQ("", model.GetText());
982 EXPECT_FALSE(model.Undo()); 943 EXPECT_FALSE(model.Undo());
983 EXPECT_STR_EQ("", model.text()); 944 EXPECT_STR_EQ("", model.GetText());
984 EXPECT_TRUE(model.Redo()); 945 EXPECT_TRUE(model.Redo());
985 EXPECT_STR_EQ("ab", model.text()); 946 EXPECT_STR_EQ("ab", model.GetText());
986 EXPECT_EQ(2U, model.cursor_pos()); 947 EXPECT_EQ(2U, model.GetCursorPosition());
987 EXPECT_FALSE(model.Redo()); 948 EXPECT_FALSE(model.Redo());
988 } 949 }
989 950
990 void RunInsertReplaceTest(TextfieldViewsModel& model) { 951 void RunInsertReplaceTest(TextfieldViewsModel& model) {
991 ui::Range r; 952 ui::Range r;
992 model.GetSelectedRange(&r); 953 model.GetSelectedRange(&r);
993 bool reverse = r.is_reversed(); 954 bool reverse = r.is_reversed();
994 955
995 model.InsertChar('1'); 956 model.InsertChar('1');
996 model.InsertChar('2'); 957 model.InsertChar('2');
997 model.InsertChar('3'); 958 model.InsertChar('3');
998 EXPECT_STR_EQ("a123d", model.text()); 959 EXPECT_STR_EQ("a123d", model.GetText());
999 EXPECT_EQ(4U, model.cursor_pos()); 960 EXPECT_EQ(4U, model.GetCursorPosition());
1000 EXPECT_TRUE(model.Undo()); 961 EXPECT_TRUE(model.Undo());
1001 EXPECT_STR_EQ("abcd", model.text()); 962 EXPECT_STR_EQ("abcd", model.GetText());
1002 EXPECT_EQ(reverse ? 1U : 3U, model.cursor_pos()); 963 EXPECT_EQ(reverse ? 1U : 3U, model.GetCursorPosition());
1003 EXPECT_TRUE(model.Undo()); 964 EXPECT_TRUE(model.Undo());
1004 EXPECT_STR_EQ("", model.text()); 965 EXPECT_STR_EQ("", model.GetText());
1005 EXPECT_EQ(0U, model.cursor_pos()); 966 EXPECT_EQ(0U, model.GetCursorPosition());
1006 EXPECT_FALSE(model.Undo()); 967 EXPECT_FALSE(model.Undo());
1007 EXPECT_TRUE(model.Redo()); 968 EXPECT_TRUE(model.Redo());
1008 EXPECT_STR_EQ("abcd", model.text()); 969 EXPECT_STR_EQ("abcd", model.GetText());
1009 EXPECT_EQ(0U, model.cursor_pos()); // By SetText 970 EXPECT_EQ(0U, model.GetCursorPosition()); // By SetText
1010 EXPECT_TRUE(model.Redo()); 971 EXPECT_TRUE(model.Redo());
1011 EXPECT_STR_EQ("a123d", model.text()); 972 EXPECT_STR_EQ("a123d", model.GetText());
1012 EXPECT_EQ(4U, model.cursor_pos()); 973 EXPECT_EQ(4U, model.GetCursorPosition());
1013 EXPECT_FALSE(model.Redo()); 974 EXPECT_FALSE(model.Redo());
1014 } 975 }
1015 976
1016 void RunOverwriteReplaceTest(TextfieldViewsModel& model) { 977 void RunOverwriteReplaceTest(TextfieldViewsModel& model) {
1017 ui::Range r; 978 ui::Range r;
1018 model.GetSelectedRange(&r); 979 model.GetSelectedRange(&r);
1019 bool reverse = r.is_reversed(); 980 bool reverse = r.is_reversed();
1020 981
1021 model.ReplaceChar('1'); 982 model.ReplaceChar('1');
1022 model.ReplaceChar('2'); 983 model.ReplaceChar('2');
1023 model.ReplaceChar('3'); 984 model.ReplaceChar('3');
1024 model.ReplaceChar('4'); 985 model.ReplaceChar('4');
1025 EXPECT_STR_EQ("a1234", model.text()); 986 EXPECT_STR_EQ("a1234", model.GetText());
1026 EXPECT_EQ(5U, model.cursor_pos()); 987 EXPECT_EQ(5U, model.GetCursorPosition());
1027 EXPECT_TRUE(model.Undo()); 988 EXPECT_TRUE(model.Undo());
1028 EXPECT_STR_EQ("abcd", model.text()); 989 EXPECT_STR_EQ("abcd", model.GetText());
1029 EXPECT_EQ(reverse ? 1U : 3U, model.cursor_pos()); 990 EXPECT_EQ(reverse ? 1U : 3U, model.GetCursorPosition());
1030 EXPECT_TRUE(model.Undo()); 991 EXPECT_TRUE(model.Undo());
1031 EXPECT_STR_EQ("", model.text()); 992 EXPECT_STR_EQ("", model.GetText());
1032 EXPECT_EQ(0U, model.cursor_pos()); 993 EXPECT_EQ(0U, model.GetCursorPosition());
1033 EXPECT_FALSE(model.Undo()); 994 EXPECT_FALSE(model.Undo());
1034 EXPECT_TRUE(model.Redo()); 995 EXPECT_TRUE(model.Redo());
1035 EXPECT_STR_EQ("abcd", model.text()); 996 EXPECT_STR_EQ("abcd", model.GetText());
1036 EXPECT_EQ(0U, model.cursor_pos()); 997 EXPECT_EQ(0U, model.GetCursorPosition());
1037 EXPECT_TRUE(model.Redo()); 998 EXPECT_TRUE(model.Redo());
1038 EXPECT_STR_EQ("a1234", model.text()); 999 EXPECT_STR_EQ("a1234", model.GetText());
1039 EXPECT_EQ(5U, model.cursor_pos()); 1000 EXPECT_EQ(5U, model.GetCursorPosition());
1040 EXPECT_FALSE(model.Redo()); 1001 EXPECT_FALSE(model.Redo());
1041 } 1002 }
1042 1003
1043 TEST_F(TextfieldViewsModelTest, UndoRedo_ReplaceTest) { 1004 TEST_F(TextfieldViewsModelTest, UndoRedo_ReplaceTest) {
1044 // By Cursor 1005 // By Cursor
1045 { 1006 {
1046 SCOPED_TRACE("forward & insert by cursor"); 1007 SCOPED_TRACE("forward & insert by cursor");
1047 TextfieldViewsModel model(NULL); 1008 TextfieldViewsModel model(NULL);
1048 model.SetText(ASCIIToUTF16("abcd")); 1009 model.SetText(ASCIIToUTF16("abcd"));
1049 model.MoveCursorTo(1, false); 1010 model.MoveCursorTo(1, false);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1107 1068
1108 TEST_F(TextfieldViewsModelTest, UndoRedo_CompositionText) { 1069 TEST_F(TextfieldViewsModelTest, UndoRedo_CompositionText) {
1109 TextfieldViewsModel model(NULL); 1070 TextfieldViewsModel model(NULL);
1110 1071
1111 ui::CompositionText composition; 1072 ui::CompositionText composition;
1112 composition.text = ASCIIToUTF16("abc"); 1073 composition.text = ASCIIToUTF16("abc");
1113 composition.underlines.push_back(ui::CompositionUnderline(0, 3, 0, false)); 1074 composition.underlines.push_back(ui::CompositionUnderline(0, 3, 0, false));
1114 composition.selection = ui::Range(2, 3); 1075 composition.selection = ui::Range(2, 3);
1115 1076
1116 model.SetText(ASCIIToUTF16("ABCDE")); 1077 model.SetText(ASCIIToUTF16("ABCDE"));
1117 model.MoveCursorToEnd(false); 1078 model.MoveCursorToRightEnd(false);
1118 model.InsertChar('x'); 1079 model.InsertChar('x');
1119 EXPECT_STR_EQ("ABCDEx", model.text()); 1080 EXPECT_STR_EQ("ABCDEx", model.GetText());
1120 EXPECT_TRUE(model.Undo()); // set composition should forget undone edit. 1081 EXPECT_TRUE(model.Undo()); // set composition should forget undone edit.
1121 model.SetCompositionText(composition); 1082 model.SetCompositionText(composition);
1122 EXPECT_TRUE(model.HasCompositionText()); 1083 EXPECT_TRUE(model.HasCompositionText());
1123 EXPECT_TRUE(model.HasSelection()); 1084 EXPECT_TRUE(model.HasSelection());
1124 EXPECT_STR_EQ("ABCDEabc", model.text()); 1085 EXPECT_STR_EQ("ABCDEabc", model.GetText());
1125 1086
1126 // Accepting composition 1087 // Accepting composition
1127 model.ConfirmCompositionText(); 1088 model.ConfirmCompositionText();
1128 EXPECT_STR_EQ("ABCDEabc", model.text()); 1089 EXPECT_STR_EQ("ABCDEabc", model.GetText());
1129 EXPECT_TRUE(model.Undo()); 1090 EXPECT_TRUE(model.Undo());
1130 EXPECT_STR_EQ("ABCDE", model.text()); 1091 EXPECT_STR_EQ("ABCDE", model.GetText());
1131 EXPECT_TRUE(model.Undo()); 1092 EXPECT_TRUE(model.Undo());
1132 EXPECT_STR_EQ("", model.text()); 1093 EXPECT_STR_EQ("", model.GetText());
1133 EXPECT_TRUE(model.Redo()); 1094 EXPECT_TRUE(model.Redo());
1134 EXPECT_STR_EQ("ABCDE", model.text()); 1095 EXPECT_STR_EQ("ABCDE", model.GetText());
1135 EXPECT_TRUE(model.Redo()); 1096 EXPECT_TRUE(model.Redo());
1136 EXPECT_STR_EQ("ABCDEabc", model.text()); 1097 EXPECT_STR_EQ("ABCDEabc", model.GetText());
1137 EXPECT_FALSE(model.Redo()); 1098 EXPECT_FALSE(model.Redo());
1138 1099
1139 // Canceling composition 1100 // Canceling composition
1140 model.MoveCursorToHome(false); 1101 model.MoveCursorToLeftEnd(false);
1141 model.SetCompositionText(composition); 1102 model.SetCompositionText(composition);
1142 EXPECT_STR_EQ("abcABCDEabc", model.text()); 1103 EXPECT_STR_EQ("abcABCDEabc", model.GetText());
1143 model.CancelCompositionText(); 1104 model.CancelCompositionText();
1144 EXPECT_STR_EQ("ABCDEabc", model.text()); 1105 EXPECT_STR_EQ("ABCDEabc", model.GetText());
1145 EXPECT_FALSE(model.Redo()); 1106 EXPECT_FALSE(model.Redo());
1146 EXPECT_STR_EQ("ABCDEabc", model.text()); 1107 EXPECT_STR_EQ("ABCDEabc", model.GetText());
1147 EXPECT_TRUE(model.Undo()); 1108 EXPECT_TRUE(model.Undo());
1148 EXPECT_STR_EQ("ABCDE", model.text()); 1109 EXPECT_STR_EQ("ABCDE", model.GetText());
1149 EXPECT_TRUE(model.Redo()); 1110 EXPECT_TRUE(model.Redo());
1150 EXPECT_STR_EQ("ABCDEabc", model.text()); 1111 EXPECT_STR_EQ("ABCDEabc", model.GetText());
1151 EXPECT_FALSE(model.Redo()); 1112 EXPECT_FALSE(model.Redo());
1152 1113
1153 // SetText with the same text as the result. 1114 // SetText with the same text as the result.
1154 ResetModel(&model); 1115 ResetModel(&model);
1155 model.SetText(ASCIIToUTF16("ABCDE")); 1116 model.SetText(ASCIIToUTF16("ABCDE"));
1156 model.MoveCursorToEnd(false); 1117 model.MoveCursorToRightEnd(false);
1157 model.SetCompositionText(composition); 1118 model.SetCompositionText(composition);
1158 EXPECT_STR_EQ("ABCDEabc", model.text()); 1119 EXPECT_STR_EQ("ABCDEabc", model.GetText());
1159 model.SetText(ASCIIToUTF16("ABCDEabc")); 1120 model.SetText(ASCIIToUTF16("ABCDEabc"));
1160 EXPECT_STR_EQ("ABCDEabc", model.text()); 1121 EXPECT_STR_EQ("ABCDEabc", model.GetText());
1161 EXPECT_TRUE(model.Undo()); 1122 EXPECT_TRUE(model.Undo());
1162 EXPECT_STR_EQ("ABCDE", model.text()); 1123 EXPECT_STR_EQ("ABCDE", model.GetText());
1163 EXPECT_TRUE(model.Redo()); 1124 EXPECT_TRUE(model.Redo());
1164 EXPECT_STR_EQ("ABCDEabc", model.text()); 1125 EXPECT_STR_EQ("ABCDEabc", model.GetText());
1165 EXPECT_FALSE(model.Redo()); 1126 EXPECT_FALSE(model.Redo());
1166 1127
1167 // SetText with the different text than the result should not 1128 // SetText with the different text than the result should not
1168 // remember composition text. 1129 // remember composition text.
1169 ResetModel(&model); 1130 ResetModel(&model);
1170 model.SetText(ASCIIToUTF16("ABCDE")); 1131 model.SetText(ASCIIToUTF16("ABCDE"));
1171 model.MoveCursorToEnd(false); 1132 model.MoveCursorToRightEnd(false);
1172 model.SetCompositionText(composition); 1133 model.SetCompositionText(composition);
1173 EXPECT_STR_EQ("ABCDEabc", model.text()); 1134 EXPECT_STR_EQ("ABCDEabc", model.GetText());
1174 model.SetText(ASCIIToUTF16("1234")); 1135 model.SetText(ASCIIToUTF16("1234"));
1175 EXPECT_STR_EQ("1234", model.text()); 1136 EXPECT_STR_EQ("1234", model.GetText());
1176 EXPECT_TRUE(model.Undo()); 1137 EXPECT_TRUE(model.Undo());
1177 EXPECT_STR_EQ("ABCDE", model.text()); 1138 EXPECT_STR_EQ("ABCDE", model.GetText());
1178 EXPECT_TRUE(model.Redo()); 1139 EXPECT_TRUE(model.Redo());
1179 EXPECT_STR_EQ("1234", model.text()); 1140 EXPECT_STR_EQ("1234", model.GetText());
1180 EXPECT_FALSE(model.Redo()); 1141 EXPECT_FALSE(model.Redo());
1181 1142
1182 // TODO(oshima): We need MockInputMethod to test the behavior with IME. 1143 // TODO(oshima): We need MockInputMethod to test the behavior with IME.
1183 } 1144 }
1184 1145
1185 TEST_F(TextfieldViewsModelTest, TextStyleTest) {
1186 const SkColor black = 0xFF000000; // black is default text color.
1187 const SkColor white = 0xFFFFFFFF;
1188 TextfieldViewsModel model(NULL);
1189 TextStyle* color = model.CreateTextStyle();
1190 color->set_foreground(white);
1191 TextStyle* underline = model.CreateTextStyle();
1192 underline->set_underline(true);
1193 underline->set_foreground(white);
1194 TextStyle* strike = model.CreateTextStyle();
1195 strike->set_strike(true);
1196 strike->set_foreground(white);
1197
1198 // Case 1: No overlaps
1199 model.ApplyTextStyle(color, ui::Range(1, 3));
1200 model.ApplyTextStyle(underline, ui::Range(5, 6));
1201
1202 TextfieldViewsModel::TextFragments fragments;
1203 model.GetFragments(&fragments);
1204 // Styles with empty string simply returns an empty fragments.
1205 EXPECT_EQ(0U, fragments.size());
1206
1207 // 1st style only.
1208 model.SetText(ASCIIToUTF16("01234")); // SetText doesn't change styles.
1209 model.GetFragments(&fragments);
1210 EXPECT_EQ(3U, fragments.size());
1211 EXPECT_EQ(0U, fragments[0].range.start());
1212 EXPECT_EQ(1U, fragments[0].range.end());
1213 EXPECT_EQ(black, fragments[0].style->foreground());
1214
1215 EXPECT_EQ(1U, fragments[1].range.start());
1216 EXPECT_EQ(3U, fragments[1].range.end());
1217 EXPECT_EQ(color, fragments[1].style);
1218
1219 EXPECT_EQ(3U, fragments[2].range.start());
1220 EXPECT_EQ(5U, fragments[2].range.end());
1221 EXPECT_EQ(black, fragments[2].style->foreground());
1222
1223 // Clear styles
1224 model.ClearAllTextStyles();
1225 model.GetFragments(&fragments);
1226 EXPECT_EQ(1U, fragments.size());
1227 EXPECT_EQ(0U, fragments[0].range.start());
1228 EXPECT_EQ(5U, fragments[0].range.end());
1229 EXPECT_EQ(black, fragments[0].style->foreground());
1230
1231 // Case 2: Overlaps on left and on right
1232 model.ApplyTextStyle(color, ui::Range(1, 3));
1233 model.ApplyTextStyle(strike, ui::Range(6, 8));
1234 model.ApplyTextStyle(underline, ui::Range(2, 7));
1235
1236 // With short string
1237 model.SetText(ASCIIToUTF16("0"));
1238 model.GetFragments(&fragments);
1239 EXPECT_EQ(1U, fragments.size());
1240 EXPECT_EQ(0U, fragments[0].range.start());
1241 EXPECT_EQ(1U, fragments[0].range.end());
1242 EXPECT_EQ(black, fragments[0].style->foreground());
1243
1244 // With mid-length string
1245 model.SetText(ASCIIToUTF16("0123"));
1246 model.GetFragments(&fragments);
1247 EXPECT_EQ(3U, fragments.size());
1248 EXPECT_EQ(0U, fragments[0].range.start());
1249 EXPECT_EQ(1U, fragments[0].range.end());
1250 EXPECT_EQ(black, fragments[0].style->foreground());
1251
1252 EXPECT_EQ(1U, fragments[1].range.start());
1253 EXPECT_EQ(2U, fragments[1].range.end());
1254 EXPECT_EQ(color, fragments[1].style);
1255
1256 EXPECT_EQ(2U, fragments[2].range.start());
1257 EXPECT_EQ(4U, fragments[2].range.end());
1258 EXPECT_EQ(underline, fragments[2].style);
1259
1260 // With long (longer than styles) string
1261 model.SetText(ASCIIToUTF16("0123456789"));
1262 model.GetFragments(&fragments);
1263 EXPECT_EQ(5U, fragments.size());
1264 EXPECT_EQ(0U, fragments[0].range.start());
1265 EXPECT_EQ(1U, fragments[0].range.end());
1266 EXPECT_EQ(black, fragments[0].style->foreground());
1267
1268 EXPECT_EQ(1U, fragments[1].range.start());
1269 EXPECT_EQ(2U, fragments[1].range.end());
1270 EXPECT_EQ(color, fragments[1].style);
1271
1272 EXPECT_EQ(2U, fragments[2].range.start());
1273 EXPECT_EQ(7U, fragments[2].range.end());
1274 EXPECT_EQ(underline, fragments[2].style);
1275
1276 EXPECT_EQ(7U, fragments[3].range.start());
1277 EXPECT_EQ(8U, fragments[3].range.end());
1278 EXPECT_EQ(strike, fragments[3].style);
1279
1280 EXPECT_EQ(8U, fragments[4].range.start());
1281 EXPECT_EQ(10U, fragments[4].range.end());
1282 EXPECT_EQ(black, fragments[4].style->foreground());
1283
1284 model.ClearAllTextStyles();
1285
1286 // Case 3: The underline style splits the color style underneath.
1287 model.ApplyTextStyle(color, ui::Range(0, 15));
1288 model.ApplyTextStyle(underline, ui::Range(5, 6));
1289 model.GetFragments(&fragments);
1290 EXPECT_EQ(3U, fragments.size());
1291 EXPECT_EQ(0U, fragments[0].range.start());
1292 EXPECT_EQ(5U, fragments[0].range.end());
1293 EXPECT_EQ(color, fragments[0].style);
1294
1295 EXPECT_EQ(5U, fragments[1].range.start());
1296 EXPECT_EQ(6U, fragments[1].range.end());
1297 EXPECT_EQ(underline, fragments[1].style);
1298
1299 EXPECT_EQ(6U, fragments[2].range.start());
1300 EXPECT_EQ(10U, fragments[2].range.end());
1301 EXPECT_EQ(color, fragments[2].style);
1302
1303 model.ClearAllTextStyles();
1304
1305 // Case 4: The underline style moves the color style underneath.
1306 model.ApplyTextStyle(color, ui::Range(0, 15));
1307 model.ApplyTextStyle(underline, ui::Range(0, 6));
1308 model.GetFragments(&fragments);
1309 EXPECT_EQ(2U, fragments.size());
1310 EXPECT_EQ(0U, fragments[0].range.start());
1311 EXPECT_EQ(6U, fragments[0].range.end());
1312 EXPECT_EQ(underline, fragments[0].style);
1313
1314 EXPECT_EQ(6U, fragments[1].range.start());
1315 EXPECT_EQ(10U, fragments[1].range.end());
1316 EXPECT_EQ(color, fragments[1].style);
1317
1318 model.ClearAllTextStyles();
1319
1320 model.ApplyTextStyle(color, ui::Range(0, 10));
1321 model.ApplyTextStyle(underline, ui::Range(6, 10));
1322 model.GetFragments(&fragments);
1323 EXPECT_EQ(2U, fragments.size());
1324 EXPECT_EQ(0U, fragments[0].range.start());
1325 EXPECT_EQ(6U, fragments[0].range.end());
1326 EXPECT_EQ(color, fragments[0].style);
1327
1328 EXPECT_EQ(6U, fragments[1].range.start());
1329 EXPECT_EQ(10U, fragments[1].range.end());
1330 EXPECT_EQ(underline, fragments[1].style);
1331
1332 model.ClearAllTextStyles();
1333 // Case 5: The strike style hides the unerline style underneath.
1334 model.ApplyTextStyle(color, ui::Range(0, 15));
1335 model.ApplyTextStyle(underline, ui::Range(0, 6));
1336 model.ApplyTextStyle(strike, ui::Range(4, 7));
1337 model.GetFragments(&fragments);
1338 EXPECT_EQ(3U, fragments.size());
1339 EXPECT_EQ(0U, fragments[0].range.start());
1340 EXPECT_EQ(4U, fragments[0].range.end());
1341 EXPECT_EQ(underline, fragments[0].style);
1342
1343 EXPECT_EQ(4U, fragments[1].range.start());
1344 EXPECT_EQ(7U, fragments[1].range.end());
1345 EXPECT_EQ(strike, fragments[1].style);
1346
1347 EXPECT_EQ(7U, fragments[2].range.start());
1348 EXPECT_EQ(10U, fragments[2].range.end());
1349 EXPECT_EQ(color, fragments[2].style);
1350
1351 // Case 6: Reversed range.
1352 model.ClearAllTextStyles();
1353 model.ApplyTextStyle(color, ui::Range(3, 1));
1354 model.ApplyTextStyle(underline, ui::Range(6, 4));
1355 model.ApplyTextStyle(strike, ui::Range(5, 2));
1356 model.GetFragments(&fragments);
1357 EXPECT_EQ(5U, fragments.size());
1358
1359 EXPECT_EQ(0U, fragments[0].range.start());
1360 EXPECT_EQ(1U, fragments[0].range.end());
1361 EXPECT_EQ(black, fragments[0].style->foreground());
1362
1363 EXPECT_EQ(1U, fragments[1].range.start());
1364 EXPECT_EQ(2U, fragments[1].range.end());
1365 EXPECT_EQ(color, fragments[1].style);
1366
1367 EXPECT_EQ(2U, fragments[2].range.start());
1368 EXPECT_EQ(5U, fragments[2].range.end());
1369 EXPECT_EQ(strike, fragments[2].style);
1370
1371 EXPECT_EQ(5U, fragments[3].range.start());
1372 EXPECT_EQ(6U, fragments[3].range.end());
1373 EXPECT_EQ(underline, fragments[3].style);
1374
1375 EXPECT_EQ(6U, fragments[4].range.start());
1376 EXPECT_EQ(10U, fragments[4].range.end());
1377 EXPECT_EQ(black, fragments[4].style->foreground());
1378
1379 // Case 7: empty / invald range
1380 model.ClearAllTextStyles();
1381 model.ApplyTextStyle(color, ui::Range(0, 0));
1382 model.ApplyTextStyle(underline, ui::Range(4, 4));
1383 ui::Range invalid = ui::Range(0, 2).Intersect(ui::Range(3, 4));
1384 ASSERT_FALSE(invalid.IsValid());
1385
1386 model.ApplyTextStyle(strike, invalid);
1387 model.GetFragments(&fragments);
1388 EXPECT_EQ(1U, fragments.size());
1389
1390 EXPECT_EQ(0U, fragments[0].range.start());
1391 EXPECT_EQ(10U, fragments[0].range.end());
1392 EXPECT_EQ(black, fragments[0].style->foreground());
1393 }
1394
1395 } // namespace views 1146 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698