OLD | NEW |
---|---|
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 "ui/gfx/render_text.h" | 5 #include "ui/gfx/render_text.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.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 | 10 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 EXPECT_EQ(ui::Range(0, 2), render_text->style_ranges()[0].range); | 171 EXPECT_EQ(ui::Range(0, 2), render_text->style_ranges()[0].range); |
172 EXPECT_FALSE(render_text->style_ranges()[0].strike); | 172 EXPECT_FALSE(render_text->style_ranges()[0].strike); |
173 | 173 |
174 // Test that previously removed ranges don't return. | 174 // Test that previously removed ranges don't return. |
175 render_text->SetText(ASCIIToUTF16("abcdef")); | 175 render_text->SetText(ASCIIToUTF16("abcdef")); |
176 EXPECT_EQ(1U, render_text->style_ranges().size()); | 176 EXPECT_EQ(1U, render_text->style_ranges().size()); |
177 EXPECT_EQ(ui::Range(0, 6), render_text->style_ranges()[0].range); | 177 EXPECT_EQ(ui::Range(0, 6), render_text->style_ranges()[0].range); |
178 EXPECT_FALSE(render_text->style_ranges()[0].strike); | 178 EXPECT_FALSE(render_text->style_ranges()[0].strike); |
179 } | 179 } |
180 | 180 |
181 #if defined(OS_LINUX) | |
msw
2011/09/15 01:26:00
Add a TODO to make these work on Windows.
xji
2011/09/15 22:58:09
Done.
| |
182 void MoveLeftRightByWordVerifier(RenderText* render_text, | |
msw
2011/09/15 01:26:00
This function is rather complex (difficult to debu
xji
2011/09/15 22:58:09
hm... I agree with you that this function is not e
| |
183 const wchar_t* str) { | |
184 render_text->SetText(WideToUTF16(str)); | |
185 | |
186 // Test moving by word from left ro right. | |
187 render_text->MoveCursorLeft(LINE_BREAK, false); | |
188 bool first_word = true; | |
189 while (true) { | |
190 // First, test moving by word from a word break position, such as from | |
191 // "|abc def" to "abc| def". | |
192 SelectionModel start = render_text->selection_model(); | |
193 render_text->MoveCursorRight(WORD_BREAK, false); | |
194 SelectionModel end = render_text->selection_model(); | |
195 if (end.Equals(start)) // reach the end. | |
196 break; | |
197 | |
198 // For testing simplicity, each word is a 3-character word. | |
199 int num_of_character_moves = first_word ? 3 : 4; | |
200 first_word = false; | |
201 render_text->MoveCursorTo(start); | |
202 for (int j = 0; j < num_of_character_moves; ++j) | |
203 render_text->MoveCursorRight(CHARACTER_BREAK, false); | |
204 EXPECT_TRUE(render_text->selection_model().Equals(end)); | |
205 | |
206 // Then, test moving by word from positions inside the word, such as from | |
207 // "a|bc def" to "abc| def", and from "ab|c def" to "abc| def". | |
208 for (int j = 1; j < num_of_character_moves; ++j) { | |
209 render_text->MoveCursorTo(start); | |
210 for (int k = 0; k < j; ++k) | |
211 render_text->MoveCursorRight(CHARACTER_BREAK, false); | |
212 render_text->MoveCursorRight(WORD_BREAK, false); | |
213 EXPECT_TRUE(render_text->selection_model().Equals(end)); | |
214 } | |
215 } | |
216 | |
217 // Test moving by word from right to left. | |
218 render_text->MoveCursorRight(LINE_BREAK, false); | |
219 first_word = true; | |
220 while (true) { | |
221 SelectionModel start = render_text->selection_model(); | |
222 render_text->MoveCursorLeft(WORD_BREAK, false); | |
223 SelectionModel end = render_text->selection_model(); | |
224 if (end.Equals(start)) // reach the end. | |
225 break; | |
226 | |
227 int num_of_character_moves = first_word ? 3 : 4; | |
228 first_word = false; | |
229 render_text->MoveCursorTo(start); | |
230 for (int j = 0; j < num_of_character_moves; ++j) | |
231 render_text->MoveCursorLeft(CHARACTER_BREAK, false); | |
232 EXPECT_TRUE(render_text->selection_model().Equals(end)); | |
233 | |
234 for (int j = 1; j < num_of_character_moves - 1; ++j) { | |
msw
2011/09/15 01:26:00
Why |num_of_character_moves - 1| here but not - 1
xji
2011/09/15 22:58:09
Ah, should not "-1", forgot to change this part.
| |
235 render_text->MoveCursorTo(start); | |
236 for (int k = 0; k < j; ++k) | |
237 render_text->MoveCursorLeft(CHARACTER_BREAK, false); | |
238 render_text->MoveCursorLeft(WORD_BREAK, false); | |
239 EXPECT_TRUE(render_text->selection_model().Equals(end)); | |
240 } | |
241 } | |
242 } | |
243 | |
244 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText) { | |
245 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | |
246 | |
247 // For testing simplicity, each word is a 3-character word. | |
248 std::vector<const wchar_t*> test; | |
249 test.push_back(L"abc"); | |
250 test.push_back(L"abc def"); | |
251 test.push_back(L"\x05E1\x05E2\x05E3"); | |
252 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"); | |
253 test.push_back(L"abc \x05E1\x05E2\x05E3"); | |
254 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"); | |
255 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6" | |
256 L" \x05E7\x05E8\x05E9"); | |
257 | |
258 test.push_back(L"abc \x05E1\x05E2\x05E3 hij"); | |
259 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 hij opq"); | |
260 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6" | |
261 L" \x05E7\x05E8\x05E9"L" opq rst uvw"); | |
262 | |
263 test.push_back(L"\x05E1\x05E2\x05E3 abc"); | |
264 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 abc def"); | |
265 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 \x05E7\x05E8\x05E9" | |
266 L" abc def hij"); | |
267 | |
268 test.push_back(L"\x05D1\x05D2\x05D3 abc \x05E1\x05E2\x05E3"); | |
269 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 abc def" | |
270 L" \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"); | |
271 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 \x05D7\x05D8\x05D9" | |
272 L" abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6" | |
273 L" \x05E7\x05E8\x05E9"); | |
274 | |
275 for (size_t i = 0; i < test.size(); ++i) | |
276 MoveLeftRightByWordVerifier(render_text.get(), test[i]); | |
277 } | |
278 | |
279 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText_TestEndOfText) { | |
msw
2011/09/15 01:26:00
Can you add comments as to what each of these bloc
xji
2011/09/15 22:58:09
Done.
I tried to do move by word the other way, f
| |
280 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | |
281 | |
282 render_text->SetText(WideToUTF16(L"ab\x05E1")); | |
283 render_text->MoveCursorRight(LINE_BREAK, false); | |
284 render_text->MoveCursorLeft(WORD_BREAK, false); | |
285 // TODO(xji): end of text is always treated as a word boundary. | |
286 // EXPECT_TRUE(render_text->selection_model().Equals(SelectionModel(0))); | |
msw
2011/09/15 01:26:00
Why isn't this working now? Can you add "abC" comm
xji
2011/09/15 22:58:09
Done.
| |
287 | |
288 render_text->MoveCursorLeft(LINE_BREAK, false); | |
289 render_text->MoveCursorRight(WORD_BREAK, false); | |
290 EXPECT_TRUE(render_text->selection_model().Equals( | |
291 SelectionModel(3, 2, SelectionModel::LEADING))); | |
292 | |
293 render_text->SetText(WideToUTF16(L"\x05E1\x05E2"L"a")); | |
294 render_text->MoveCursorRight(LINE_BREAK, false); | |
295 render_text->MoveCursorLeft(WORD_BREAK, false); | |
296 EXPECT_TRUE(render_text->selection_model().Equals( | |
297 SelectionModel(3, 2, SelectionModel::LEADING))); | |
298 | |
299 render_text->MoveCursorLeft(LINE_BREAK, false); | |
300 render_text->MoveCursorRight(WORD_BREAK, false); | |
301 // TODO(xji): end of text is always treated as a word boundary. | |
302 // EXPECT_TRUE(render_text->selection_model().Equals(SelectionModel(0))); | |
msw
2011/09/15 01:26:00
Same here, I don't get what's broken.
xji
2011/09/15 22:58:09
explained.
| |
303 } | |
304 | |
305 TEST_F(RenderTextTest, MoveLeftRightByWordInTextWithMultiSpaces) { | |
306 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | |
307 render_text->SetText(WideToUTF16(L"abc def")); | |
308 render_text->MoveCursorTo(SelectionModel(5)); | |
309 render_text->MoveCursorRight(WORD_BREAK, false); | |
310 EXPECT_EQ(11U, render_text->GetCursorPosition()); | |
311 | |
312 render_text->MoveCursorTo(SelectionModel(5)); | |
313 render_text->MoveCursorLeft(WORD_BREAK, false); | |
314 EXPECT_EQ(0U, render_text->GetCursorPosition()); | |
315 } | |
316 #endif | |
317 | |
181 } // namespace gfx | 318 } // namespace gfx |
OLD | NEW |