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

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

Powered by Google App Engine
This is Rietveld 408576698