1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/render_text.h"
9 #include "base/format_macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gfx/break_list.h"
16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/render_text_harfbuzz.h"
20 #include "base/win/windows_version.h"
21 #include "ui/gfx/render_text_win.h"
24 #if defined(OS_LINUX) && !defined(USE_OZONE)
25 #include "ui/gfx/render_text_pango.h"
28 using base::ASCIIToUTF16
;
29 using base::UTF8ToUTF16
;
30 using base::WideToUTF16
;
36 // Various weak, LTR, RTL, and Bidi string cases with three characters each.
37 const wchar_t kWeak
[] = L
" . ";
38 const wchar_t kLtr
[] = L
"abc";
39 const wchar_t kRtl
[] = L
"\x5d0\x5d1\x5d2";
40 #if !defined(OS_MACOSX)
41 const wchar_t kLtrRtl
[] = L
"a" L
"\x5d0\x5d1";
42 const wchar_t kLtrRtlLtr
[] = L
"a" L
"\x5d1" L
"b";
43 const wchar_t kRtlLtr
[] = L
"\x5d0\x5d1" L
"a";
44 const wchar_t kRtlLtrRtl
[] = L
"\x5d0" L
"a" L
"\x5d1";
47 // Checks whether |range| contains |index|. This is not the same as calling
48 // range.Contains(Range(index)), which returns true if |index| == |range.end()|.
49 bool IndexInRange(const Range
& range
, size_t index
) {
50 return index
>= range
.start() && index
< range
.end();
53 base::string16
GetSelectedText(RenderText
* render_text
) {
54 return render_text
->text().substr(render_text
->selection().GetMin(),
55 render_text
->selection().length());
58 // A test utility function to set the application default text direction.
59 void SetRTL(bool rtl
) {
60 // Override the current locale/direction.
61 base::i18n::SetICUDefaultLocale(rtl
? "he" : "en");
62 EXPECT_EQ(rtl
, base::i18n::IsRTL());
65 #if !defined(OS_MACOSX)
66 // Ensure cursor movement in the specified |direction| yields |expected| values.
67 void RunMoveCursorLeftRightTest(RenderText
* render_text
,
68 const std::vector
<SelectionModel
>& expected
,
69 VisualCursorDirection direction
) {
70 for (size_t i
= 0; i
< expected
.size(); ++i
) {
71 SCOPED_TRACE(base::StringPrintf("Going %s; expected value index %d.",
72 direction
== CURSOR_LEFT
? "left" : "right", static_cast<int>(i
)));
73 EXPECT_EQ(expected
[i
], render_text
->selection_model());
74 render_text
->MoveCursor(CHARACTER_BREAK
, direction
, false);
76 // Check that cursoring is clamped at the line edge.
77 EXPECT_EQ(expected
.back(), render_text
->selection_model());
78 // Check that it is the line edge.
79 render_text
->MoveCursor(LINE_BREAK
, direction
, false);
80 EXPECT_EQ(expected
.back(), render_text
->selection_model());
82 #endif // !defined(OS_MACOSX)
86 class RenderTextTest
: public testing::Test
{
89 TEST_F(RenderTextTest
, DefaultStyle
) {
90 // Check the default styles applied to new instances and adjusted text.
91 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
92 EXPECT_TRUE(render_text
->text().empty());
93 const wchar_t* const cases
[] = { kWeak
, kLtr
, L
"Hello", kRtl
, L
"", L
"" };
94 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
95 EXPECT_TRUE(render_text
->colors().EqualsValueForTesting(SK_ColorBLACK
));
96 for (size_t style
= 0; style
< NUM_TEXT_STYLES
; ++style
)
97 EXPECT_TRUE(render_text
->styles()[style
].EqualsValueForTesting(false));
98 render_text
->SetText(WideToUTF16(cases
[i
]));
102 TEST_F(RenderTextTest
, SetColorAndStyle
) {
103 // Ensure custom default styles persist across setting and clearing text.
104 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
105 const SkColor color
= SK_ColorRED
;
106 render_text
->SetColor(color
);
107 render_text
->SetStyle(BOLD
, true);
108 render_text
->SetStyle(UNDERLINE
, false);
109 const wchar_t* const cases
[] = { kWeak
, kLtr
, L
"Hello", kRtl
, L
"", L
"" };
110 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
111 EXPECT_TRUE(render_text
->colors().EqualsValueForTesting(color
));
112 EXPECT_TRUE(render_text
->styles()[BOLD
].EqualsValueForTesting(true));
113 EXPECT_TRUE(render_text
->styles()[UNDERLINE
].EqualsValueForTesting(false));
114 render_text
->SetText(WideToUTF16(cases
[i
]));
116 // Ensure custom default styles can be applied after text has been set.
118 render_text
->SetStyle(STRIKE
, true);
120 EXPECT_TRUE(render_text
->styles()[STRIKE
].EqualsValueForTesting(true));
124 TEST_F(RenderTextTest
, ApplyColorAndStyle
) {
125 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
126 render_text
->SetText(ASCIIToUTF16("012345678"));
128 // Apply a ranged color and style and check the resulting breaks.
129 render_text
->ApplyColor(SK_ColorRED
, Range(1, 4));
130 render_text
->ApplyStyle(BOLD
, true, Range(2, 5));
131 std::vector
<std::pair
<size_t, SkColor
> > expected_color
;
132 expected_color
.push_back(std::pair
<size_t, SkColor
>(0, SK_ColorBLACK
));
133 expected_color
.push_back(std::pair
<size_t, SkColor
>(1, SK_ColorRED
));
134 expected_color
.push_back(std::pair
<size_t, SkColor
>(4, SK_ColorBLACK
));
135 EXPECT_TRUE(render_text
->colors().EqualsForTesting(expected_color
));
136 std::vector
<std::pair
<size_t, bool> > expected_style
;
137 expected_style
.push_back(std::pair
<size_t, bool>(0, false));
138 expected_style
.push_back(std::pair
<size_t, bool>(2, true));
139 expected_style
.push_back(std::pair
<size_t, bool>(5, false));
140 EXPECT_TRUE(render_text
->styles()[BOLD
].EqualsForTesting(expected_style
));
142 // Ensure setting a color and style overrides the ranged colors and styles.
143 render_text
->SetColor(SK_ColorBLUE
);
144 EXPECT_TRUE(render_text
->colors().EqualsValueForTesting(SK_ColorBLUE
));
145 render_text
->SetStyle(BOLD
, false);
146 EXPECT_TRUE(render_text
->styles()[BOLD
].EqualsValueForTesting(false));
148 // Apply a color and style over the text end and check the resulting breaks.
149 // (INT_MAX should be used instead of the text length for the range end)
150 const size_t text_length
= render_text
->text().length();
151 render_text
->ApplyColor(SK_ColorRED
, Range(0, text_length
));
152 render_text
->ApplyStyle(BOLD
, true, Range(2, text_length
));
153 std::vector
<std::pair
<size_t, SkColor
> > expected_color_end
;
154 expected_color_end
.push_back(std::pair
<size_t, SkColor
>(0, SK_ColorRED
));
155 EXPECT_TRUE(render_text
->colors().EqualsForTesting(expected_color_end
));
156 std::vector
<std::pair
<size_t, bool> > expected_style_end
;
157 expected_style_end
.push_back(std::pair
<size_t, bool>(0, false));
158 expected_style_end
.push_back(std::pair
<size_t, bool>(2, true));
159 EXPECT_TRUE(render_text
->styles()[BOLD
].EqualsForTesting(expected_style_end
));
161 // Ensure ranged values adjust to accommodate text length changes.
162 render_text
->ApplyStyle(ITALIC
, true, Range(0, 2));
163 render_text
->ApplyStyle(ITALIC
, true, Range(3, 6));
164 render_text
->ApplyStyle(ITALIC
, true, Range(7, text_length
));
165 std::vector
<std::pair
<size_t, bool> > expected_italic
;
166 expected_italic
.push_back(std::pair
<size_t, bool>(0, true));
167 expected_italic
.push_back(std::pair
<size_t, bool>(2, false));
168 expected_italic
.push_back(std::pair
<size_t, bool>(3, true));
169 expected_italic
.push_back(std::pair
<size_t, bool>(6, false));
170 expected_italic
.push_back(std::pair
<size_t, bool>(7, true));
171 EXPECT_TRUE(render_text
->styles()[ITALIC
].EqualsForTesting(expected_italic
));
173 // Truncating the text should trim any corresponding breaks.
174 render_text
->SetText(ASCIIToUTF16("0123456"));
175 expected_italic
.resize(4);
176 EXPECT_TRUE(render_text
->styles()[ITALIC
].EqualsForTesting(expected_italic
));
177 render_text
->SetText(ASCIIToUTF16("01234"));
178 expected_italic
.resize(3);
179 EXPECT_TRUE(render_text
->styles()[ITALIC
].EqualsForTesting(expected_italic
));
181 // Appending text should extend the terminal styles without changing breaks.
182 render_text
->SetText(ASCIIToUTF16("012345678"));
183 EXPECT_TRUE(render_text
->styles()[ITALIC
].EqualsForTesting(expected_italic
));
186 #if defined(OS_LINUX) && !defined(USE_OZONE)
187 TEST_F(RenderTextTest
, PangoAttributes
) {
188 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
189 render_text
->SetText(ASCIIToUTF16("012345678"));
191 // Apply ranged BOLD/ITALIC styles and check the resulting Pango attributes.
192 render_text
->ApplyStyle(BOLD
, true, Range(2, 4));
193 render_text
->ApplyStyle(ITALIC
, true, Range(1, 3));
201 { 0, 1, false, false },
202 { 1, 2, false, true },
203 { 2, 3, true, true },
204 { 3, 4, true, false },
205 { 4, INT_MAX
, false, false },
208 int start
= 0, end
= 0;
209 RenderTextPango
* rt_linux
= static_cast<RenderTextPango
*>(render_text
.get());
210 rt_linux
->EnsureLayout();
211 PangoAttrList
* attributes
= pango_layout_get_attributes(rt_linux
->layout_
);
212 PangoAttrIterator
* iter
= pango_attr_list_get_iterator(attributes
);
213 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
214 pango_attr_iterator_range(iter
, &start
, &end
);
215 EXPECT_EQ(cases
[i
].start
, start
);
216 EXPECT_EQ(cases
[i
].end
, end
);
217 PangoFontDescription
* font
= pango_font_description_new();
218 pango_attr_iterator_get_font(iter
, font
, NULL
, NULL
);
219 char* description_string
= pango_font_description_to_string(font
);
220 const base::string16 desc
= ASCIIToUTF16(description_string
);
221 const bool bold
= desc
.find(ASCIIToUTF16("Bold")) != std::string::npos
;
222 EXPECT_EQ(cases
[i
].bold
, bold
);
223 const bool italic
= desc
.find(ASCIIToUTF16("Italic")) != std::string::npos
;
224 EXPECT_EQ(cases
[i
].italic
, italic
);
225 pango_attr_iterator_next(iter
);
226 pango_font_description_free(font
);
227 g_free(description_string
);
229 EXPECT_FALSE(pango_attr_iterator_next(iter
));
230 pango_attr_iterator_destroy(iter
);
234 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
235 // does not implement this yet. http://crbug.com/131618
236 #if !defined(OS_MACOSX)
237 void TestVisualCursorMotionInObscuredField(RenderText
* render_text
,
238 const base::string16
& text
,
240 ASSERT_TRUE(render_text
->obscured());
241 render_text
->SetText(text
);
242 int len
= text
.length();
243 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, select
);
244 EXPECT_EQ(SelectionModel(Range(select
? 0 : len
, len
), CURSOR_FORWARD
),
245 render_text
->selection_model());
246 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, select
);
247 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
248 for (int j
= 1; j
<= len
; ++j
) {
249 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, select
);
250 EXPECT_EQ(SelectionModel(Range(select
? 0 : j
, j
), CURSOR_BACKWARD
),
251 render_text
->selection_model());
253 for (int j
= len
- 1; j
>= 0; --j
) {
254 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, select
);
255 EXPECT_EQ(SelectionModel(Range(select
? 0 : j
, j
), CURSOR_FORWARD
),
256 render_text
->selection_model());
258 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, select
);
259 EXPECT_EQ(SelectionModel(Range(select
? 0 : len
, len
), CURSOR_FORWARD
),
260 render_text
->selection_model());
261 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, select
);
262 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
265 TEST_F(RenderTextTest
, ObscuredText
) {
266 const base::string16 seuss
= ASCIIToUTF16("hop on pop");
267 const base::string16 no_seuss
= ASCIIToUTF16("**********");
268 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
270 // GetLayoutText() returns asterisks when the obscured bit is set.
271 render_text
->SetText(seuss
);
272 render_text
->SetObscured(true);
273 EXPECT_EQ(seuss
, render_text
->text());
274 EXPECT_EQ(no_seuss
, render_text
->GetLayoutText());
275 render_text
->SetObscured(false);
276 EXPECT_EQ(seuss
, render_text
->text());
277 EXPECT_EQ(seuss
, render_text
->GetLayoutText());
279 render_text
->SetObscured(true);
281 // Surrogate pairs are counted as one code point.
282 const base::char16 invalid_surrogates
[] = {0xDC00, 0xD800, 0};
283 render_text
->SetText(invalid_surrogates
);
284 EXPECT_EQ(ASCIIToUTF16("**"), render_text
->GetLayoutText());
285 const base::char16 valid_surrogates
[] = {0xD800, 0xDC00, 0};
286 render_text
->SetText(valid_surrogates
);
287 EXPECT_EQ(ASCIIToUTF16("*"), render_text
->GetLayoutText());
288 EXPECT_EQ(0U, render_text
->cursor_position());
289 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
290 EXPECT_EQ(2U, render_text
->cursor_position());
292 // Test index conversion and cursor validity with a valid surrogate pair.
293 EXPECT_EQ(0U, render_text
->TextIndexToLayoutIndex(0U));
294 EXPECT_EQ(1U, render_text
->TextIndexToLayoutIndex(1U));
295 EXPECT_EQ(1U, render_text
->TextIndexToLayoutIndex(2U));
296 EXPECT_EQ(0U, render_text
->LayoutIndexToTextIndex(0U));
297 EXPECT_EQ(2U, render_text
->LayoutIndexToTextIndex(1U));
298 EXPECT_TRUE(render_text
->IsValidCursorIndex(0U));
299 EXPECT_FALSE(render_text
->IsValidCursorIndex(1U));
300 EXPECT_TRUE(render_text
->IsValidCursorIndex(2U));
302 // FindCursorPosition() should not return positions between a surrogate pair.
303 render_text
->SetDisplayRect(Rect(0, 0, 20, 20));
304 EXPECT_EQ(render_text
->FindCursorPosition(Point(0, 0)).caret_pos(), 0U);
305 EXPECT_EQ(render_text
->FindCursorPosition(Point(20, 0)).caret_pos(), 2U);
306 for (int x
= -1; x
<= 20; ++x
) {
307 SelectionModel selection
= render_text
->FindCursorPosition(Point(x
, 0));
308 EXPECT_TRUE(selection
.caret_pos() == 0U || selection
.caret_pos() == 2U);
311 // GetGlyphBounds() should yield the entire string bounds for text index 0.
312 EXPECT_EQ(render_text
->GetStringSize().width(),
313 static_cast<int>(render_text
->GetGlyphBounds(0U).length()));
315 // Cursoring is independent of underlying characters when text is obscured.
316 const wchar_t* const texts
[] = {
317 kWeak
, kLtr
, kLtrRtl
, kLtrRtlLtr
, kRtl
, kRtlLtr
, kRtlLtrRtl
,
318 L
"hop on pop", // Check LTR word boundaries.
319 L
"\x05d0\x05d1 \x05d0\x05d2 \x05d1\x05d2", // Check RTL word boundaries.
321 for (size_t i
= 0; i
< arraysize(texts
); ++i
) {
322 base::string16 text
= WideToUTF16(texts
[i
]);
323 TestVisualCursorMotionInObscuredField(render_text
.get(), text
, false);
324 TestVisualCursorMotionInObscuredField(render_text
.get(), text
, true);
328 TEST_F(RenderTextTest
, RevealObscuredText
) {
329 const base::string16 seuss
= ASCIIToUTF16("hop on pop");
330 const base::string16 no_seuss
= ASCIIToUTF16("**********");
331 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
333 render_text
->SetText(seuss
);
334 render_text
->SetObscured(true);
335 EXPECT_EQ(seuss
, render_text
->text());
336 EXPECT_EQ(no_seuss
, render_text
->GetLayoutText());
338 // Valid reveal index and new revealed index clears previous one.
339 render_text
->RenderText::SetObscuredRevealIndex(0);
340 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text
->GetLayoutText());
341 render_text
->RenderText::SetObscuredRevealIndex(1);
342 EXPECT_EQ(ASCIIToUTF16("*o********"), render_text
->GetLayoutText());
343 render_text
->RenderText::SetObscuredRevealIndex(2);
344 EXPECT_EQ(ASCIIToUTF16("**p*******"), render_text
->GetLayoutText());
346 // Invalid reveal index.
347 render_text
->RenderText::SetObscuredRevealIndex(-1);
348 EXPECT_EQ(no_seuss
, render_text
->GetLayoutText());
349 render_text
->RenderText::SetObscuredRevealIndex(seuss
.length() + 1);
350 EXPECT_EQ(no_seuss
, render_text
->GetLayoutText());
352 // SetObscured clears the revealed index.
353 render_text
->RenderText::SetObscuredRevealIndex(0);
354 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text
->GetLayoutText());
355 render_text
->SetObscured(false);
356 EXPECT_EQ(seuss
, render_text
->GetLayoutText());
357 render_text
->SetObscured(true);
358 EXPECT_EQ(no_seuss
, render_text
->GetLayoutText());
360 // SetText clears the revealed index.
361 render_text
->SetText(ASCIIToUTF16("new"));
362 EXPECT_EQ(ASCIIToUTF16("***"), render_text
->GetLayoutText());
363 render_text
->RenderText::SetObscuredRevealIndex(2);
364 EXPECT_EQ(ASCIIToUTF16("**w"), render_text
->GetLayoutText());
365 render_text
->SetText(ASCIIToUTF16("new longer"));
366 EXPECT_EQ(ASCIIToUTF16("**********"), render_text
->GetLayoutText());
368 // Text with invalid surrogates.
369 const base::char16 invalid_surrogates
[] = {0xDC00, 0xD800, 'h', 'o', 'p', 0};
370 render_text
->SetText(invalid_surrogates
);
371 EXPECT_EQ(ASCIIToUTF16("*****"), render_text
->GetLayoutText());
372 render_text
->RenderText::SetObscuredRevealIndex(0);
373 const base::char16 invalid_expect_0
[] = {0xDC00, '*', '*', '*', '*', 0};
374 EXPECT_EQ(invalid_expect_0
, render_text
->GetLayoutText());
375 render_text
->RenderText::SetObscuredRevealIndex(1);
376 const base::char16 invalid_expect_1
[] = {'*', 0xD800, '*', '*', '*', 0};
377 EXPECT_EQ(invalid_expect_1
, render_text
->GetLayoutText());
378 render_text
->RenderText::SetObscuredRevealIndex(2);
379 EXPECT_EQ(ASCIIToUTF16("**h**"), render_text
->GetLayoutText());
381 // Text with valid surrogates before and after the reveal index.
382 const base::char16 valid_surrogates
[] =
383 {0xD800, 0xDC00, 'h', 'o', 'p', 0xD800, 0xDC00, 0};
384 render_text
->SetText(valid_surrogates
);
385 EXPECT_EQ(ASCIIToUTF16("*****"), render_text
->GetLayoutText());
386 render_text
->RenderText::SetObscuredRevealIndex(0);
387 const base::char16 valid_expect_0_and_1
[] =
388 {0xD800, 0xDC00, '*', '*', '*', '*', 0};
389 EXPECT_EQ(valid_expect_0_and_1
, render_text
->GetLayoutText());
390 render_text
->RenderText::SetObscuredRevealIndex(1);
391 EXPECT_EQ(valid_expect_0_and_1
, render_text
->GetLayoutText());
392 render_text
->RenderText::SetObscuredRevealIndex(2);
393 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text
->GetLayoutText());
394 render_text
->RenderText::SetObscuredRevealIndex(5);
395 const base::char16 valid_expect_5_and_6
[] =
396 {'*', '*', '*', '*', 0xD800, 0xDC00, 0};
397 EXPECT_EQ(valid_expect_5_and_6
, render_text
->GetLayoutText());
398 render_text
->RenderText::SetObscuredRevealIndex(6);
399 EXPECT_EQ(valid_expect_5_and_6
, render_text
->GetLayoutText());
402 TEST_F(RenderTextTest
, ElidedText
) {
403 // TODO(skanuj) : Add more test cases for following
404 // - RenderText styles.
405 // - Cross interaction of truncate, elide and obscure.
406 // - ElideText tests from text_elider.cc.
409 const wchar_t* layout_text
;
410 const bool elision_expected
;
412 // Strings shorter than the elision width should be laid out in full.
413 { L
"", L
"" , false },
414 { L
"M", L
"" , false },
415 { L
" . ", L
" . " , false },
416 { kWeak
, kWeak
, false },
417 { kLtr
, kLtr
, false },
418 { kLtrRtl
, kLtrRtl
, false },
419 { kLtrRtlLtr
, kLtrRtlLtr
, false },
420 { kRtl
, kRtl
, false },
421 { kRtlLtr
, kRtlLtr
, false },
422 { kRtlLtrRtl
, kRtlLtrRtl
, false },
423 // Strings as long as the elision width should be laid out in full.
424 { L
"012ab", L
"012ab" , false },
425 // Long strings should be elided with an ellipsis appended at the end.
426 { L
"012abc", L
"012a\x2026", true },
427 { L
"012ab" L
"\x5d0\x5d1", L
"012a\x2026", true },
428 { L
"012a" L
"\x5d1" L
"b", L
"012a\x2026", true },
429 // No RLM marker added as digits (012) have weak directionality.
430 { L
"01" L
"\x5d0\x5d1\x5d2", L
"01\x5d0\x5d1\x2026", true },
431 // RLM marker added as "ab" have strong LTR directionality.
432 { L
"ab" L
"\x5d0\x5d1\x5d2", L
"ab\x5d0\x5d1\x2026\x200f", true },
433 // Complex script is not handled. In this example, the "\x0915\x093f" is a
434 // compound glyph, but only half of it is elided.
435 { L
"0123\x0915\x093f", L
"0123\x0915\x2026", true },
436 // Surrogate pairs should be elided reasonably enough.
437 { L
"0\x05e9\x05bc\x05c1\x05b8", L
"0\x05e9\x05bc\x05c1\x05b8", false },
438 { L
"0\x05e9\x05bc\x05c1\x05b8", L
"0\x05e9\x05bc\x2026" , true },
439 { L
"01\x05e9\x05bc\x05c1\x05b8", L
"01\x05e9\x2026" , true },
440 { L
"012\x05e9\x05bc\x05c1\x05b8", L
"012\x2026\x200E" , true },
441 { L
"012\xF0\x9D\x84\x9E", L
"012\xF0\x2026" , true },
444 scoped_ptr
<RenderText
> expected_render_text(RenderText::CreateInstance());
445 expected_render_text
->SetFontList(FontList("serif, Sans serif, 12px"));
446 expected_render_text
->SetDisplayRect(Rect(0, 0, 9999, 100));
448 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
449 render_text
->SetFontList(FontList("serif, Sans serif, 12px"));
450 render_text
->SetElideBehavior(ELIDE_TAIL
);
452 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); i
++) {
453 // Compute expected width
454 expected_render_text
->SetText(WideToUTF16(cases
[i
].layout_text
));
455 int expected_width
= expected_render_text
->GetContentWidth();
457 base::string16 input
= WideToUTF16(cases
[i
].text
);
458 // Extend the input text to ensure that it is wider than the layout_text,
459 // and so it will get elided.
460 if (cases
[i
].elision_expected
)
461 input
.append(WideToUTF16(L
" MMMMMMMMMMM"));
463 render_text
->SetText(input
);
464 render_text
->SetDisplayRect(Rect(0, 0, expected_width
, 100));
465 EXPECT_EQ(input
, render_text
->text())
466 << "->For case " << i
<< ": " << cases
[i
].text
<< "\n";
467 EXPECT_EQ(WideToUTF16(cases
[i
].layout_text
), render_text
->GetLayoutText())
468 << "->For case " << i
<< ": " << cases
[i
].text
<< "\n";
469 expected_render_text
->SetText(base::string16());
473 TEST_F(RenderTextTest
, ElidedObscuredText
) {
474 scoped_ptr
<RenderText
> expected_render_text(RenderText::CreateInstance());
475 expected_render_text
->SetFontList(FontList("serif, Sans serif, 12px"));
476 expected_render_text
->SetDisplayRect(Rect(0, 0, 9999, 100));
477 expected_render_text
->SetText(WideToUTF16(L
"**\x2026"));
479 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
480 render_text
->SetFontList(FontList("serif, Sans serif, 12px"));
481 render_text
->SetElideBehavior(ELIDE_TAIL
);
482 render_text
->SetDisplayRect(
483 Rect(0, 0, expected_render_text
->GetContentWidth(), 100));
484 render_text
->SetObscured(true);
485 render_text
->SetText(WideToUTF16(L
"abcdef"));
486 EXPECT_EQ(WideToUTF16(L
"abcdef"), render_text
->text());
487 EXPECT_EQ(WideToUTF16(L
"**\x2026"), render_text
->GetLayoutText());
490 TEST_F(RenderTextTest
, TruncatedText
) {
493 const wchar_t* layout_text
;
495 // Strings shorter than the truncation length should be laid out in full.
499 { kLtrRtl
, kLtrRtl
},
500 { kLtrRtlLtr
, kLtrRtlLtr
},
502 { kRtlLtr
, kRtlLtr
},
503 { kRtlLtrRtl
, kRtlLtrRtl
},
504 // Strings as long as the truncation length should be laid out in full.
505 { L
"01234", L
"01234" },
506 // Long strings should be truncated with an ellipsis appended at the end.
507 { L
"012345", L
"0123\x2026" },
508 { L
"012" L
" . ", L
"012 \x2026" },
509 { L
"012" L
"abc", L
"012a\x2026" },
510 { L
"012" L
"a" L
"\x5d0\x5d1", L
"012a\x2026" },
511 { L
"012" L
"a" L
"\x5d1" L
"b", L
"012a\x2026" },
512 { L
"012" L
"\x5d0\x5d1\x5d2", L
"012\x5d0\x2026" },
513 { L
"012" L
"\x5d0\x5d1" L
"a", L
"012\x5d0\x2026" },
514 { L
"012" L
"\x5d0" L
"a" L
"\x5d1", L
"012\x5d0\x2026" },
515 // Surrogate pairs should be truncated reasonably enough.
516 { L
"0123\x0915\x093f", L
"0123\x2026" },
517 { L
"0\x05e9\x05bc\x05c1\x05b8", L
"0\x05e9\x05bc\x05c1\x05b8" },
518 { L
"01\x05e9\x05bc\x05c1\x05b8", L
"01\x05e9\x05bc\x2026" },
519 { L
"012\x05e9\x05bc\x05c1\x05b8", L
"012\x05e9\x2026" },
520 { L
"0123\x05e9\x05bc\x05c1\x05b8", L
"0123\x2026" },
521 { L
"01234\x05e9\x05bc\x05c1\x05b8", L
"0123\x2026" },
522 { L
"012\xF0\x9D\x84\x9E", L
"012\xF0\x2026" },
525 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
526 render_text
->set_truncate_length(5);
527 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); i
++) {
528 render_text
->SetText(WideToUTF16(cases
[i
].text
));
529 EXPECT_EQ(WideToUTF16(cases
[i
].text
), render_text
->text());
530 EXPECT_EQ(WideToUTF16(cases
[i
].layout_text
), render_text
->GetLayoutText())
531 << "For case " << i
<< ": " << cases
[i
].text
;
535 TEST_F(RenderTextTest
, TruncatedObscuredText
) {
536 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
537 render_text
->set_truncate_length(3);
538 render_text
->SetObscured(true);
539 render_text
->SetText(WideToUTF16(L
"abcdef"));
540 EXPECT_EQ(WideToUTF16(L
"abcdef"), render_text
->text());
541 EXPECT_EQ(WideToUTF16(L
"**\x2026"), render_text
->GetLayoutText());
544 TEST_F(RenderTextTest
, TruncatedCursorMovementLTR
) {
545 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
546 render_text
->set_truncate_length(2);
547 render_text
->SetText(WideToUTF16(L
"abcd"));
549 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
550 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
551 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD
), render_text
->selection_model());
552 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
553 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
555 std::vector
<SelectionModel
> expected
;
556 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
557 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
558 // The cursor hops over the ellipsis and elided text to the line end.
559 expected
.push_back(SelectionModel(4, CURSOR_BACKWARD
));
560 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
561 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
564 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
565 // The cursor hops over the elided text to preceeding text.
566 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
567 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
568 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
569 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
572 TEST_F(RenderTextTest
, TruncatedCursorMovementRTL
) {
573 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
574 render_text
->set_truncate_length(2);
575 render_text
->SetText(WideToUTF16(L
"\x5d0\x5d1\x5d2\x5d3"));
577 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
578 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
579 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD
), render_text
->selection_model());
580 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
581 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
583 std::vector
<SelectionModel
> expected
;
584 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
585 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
586 // The cursor hops over the ellipsis and elided text to the line end.
587 expected
.push_back(SelectionModel(4, CURSOR_BACKWARD
));
588 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
589 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
592 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
593 // The cursor hops over the elided text to preceeding text.
594 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
595 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
596 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
597 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
600 TEST_F(RenderTextTest
, GetTextDirection
) {
603 const base::i18n::TextDirection text_direction
;
605 // Blank strings and those with no/weak directionality default to LTR.
606 { L
"", base::i18n::LEFT_TO_RIGHT
},
607 { kWeak
, base::i18n::LEFT_TO_RIGHT
},
608 // Strings that begin with strong LTR characters.
609 { kLtr
, base::i18n::LEFT_TO_RIGHT
},
610 { kLtrRtl
, base::i18n::LEFT_TO_RIGHT
},
611 { kLtrRtlLtr
, base::i18n::LEFT_TO_RIGHT
},
612 // Strings that begin with strong RTL characters.
613 { kRtl
, base::i18n::RIGHT_TO_LEFT
},
614 { kRtlLtr
, base::i18n::RIGHT_TO_LEFT
},
615 { kRtlLtrRtl
, base::i18n::RIGHT_TO_LEFT
},
618 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
619 const bool was_rtl
= base::i18n::IsRTL();
621 for (size_t i
= 0; i
< 2; ++i
) {
622 // Toggle the application default text direction (to try each direction).
623 SetRTL(!base::i18n::IsRTL());
624 const base::i18n::TextDirection ui_direction
= base::i18n::IsRTL() ?
625 base::i18n::RIGHT_TO_LEFT
: base::i18n::LEFT_TO_RIGHT
;
627 // Ensure that directionality modes yield the correct text directions.
628 for (size_t j
= 0; j
< ARRAYSIZE_UNSAFE(cases
); j
++) {
629 render_text
->SetText(WideToUTF16(cases
[j
].text
));
630 render_text
->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT
);
631 EXPECT_EQ(render_text
->GetTextDirection(), cases
[j
].text_direction
);
632 render_text
->SetDirectionalityMode(DIRECTIONALITY_FROM_UI
);
633 EXPECT_EQ(render_text
->GetTextDirection(), ui_direction
);
634 render_text
->SetDirectionalityMode(DIRECTIONALITY_FORCE_LTR
);
635 EXPECT_EQ(render_text
->GetTextDirection(), base::i18n::LEFT_TO_RIGHT
);
636 render_text
->SetDirectionalityMode(DIRECTIONALITY_FORCE_RTL
);
637 EXPECT_EQ(render_text
->GetTextDirection(), base::i18n::RIGHT_TO_LEFT
);
641 EXPECT_EQ(was_rtl
, base::i18n::IsRTL());
643 // Ensure that text changes update the direction for DIRECTIONALITY_FROM_TEXT.
644 render_text
->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT
);
645 render_text
->SetText(WideToUTF16(kLtr
));
646 EXPECT_EQ(render_text
->GetTextDirection(), base::i18n::LEFT_TO_RIGHT
);
647 render_text
->SetText(WideToUTF16(kRtl
));
648 EXPECT_EQ(render_text
->GetTextDirection(), base::i18n::RIGHT_TO_LEFT
);
651 TEST_F(RenderTextTest
, MoveCursorLeftRightInLtr
) {
652 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
655 render_text
->SetText(ASCIIToUTF16("abc"));
656 // |expected| saves the expected SelectionModel when moving cursor from left
658 std::vector
<SelectionModel
> expected
;
659 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
660 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
661 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
662 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
663 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
664 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
667 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
668 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
669 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
670 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
671 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
672 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
675 TEST_F(RenderTextTest
, MoveCursorLeftRightInLtrRtl
) {
676 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
678 render_text
->SetText(WideToUTF16(L
"abc\x05d0\x05d1\x05d2"));
679 // The last one is the expected END position.
680 std::vector
<SelectionModel
> expected
;
681 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
682 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
683 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
684 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
685 expected
.push_back(SelectionModel(5, CURSOR_FORWARD
));
686 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
687 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
688 expected
.push_back(SelectionModel(6, CURSOR_FORWARD
));
689 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
692 expected
.push_back(SelectionModel(6, CURSOR_FORWARD
));
693 expected
.push_back(SelectionModel(4, CURSOR_BACKWARD
));
694 expected
.push_back(SelectionModel(5, CURSOR_BACKWARD
));
695 expected
.push_back(SelectionModel(6, CURSOR_BACKWARD
));
696 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
697 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
698 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
699 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
700 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
703 TEST_F(RenderTextTest
, MoveCursorLeftRightInLtrRtlLtr
) {
704 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
706 render_text
->SetText(WideToUTF16(L
"a" L
"\x05d1" L
"b"));
707 std::vector
<SelectionModel
> expected
;
708 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
709 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
710 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
711 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
712 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
713 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
716 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
717 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
718 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
719 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
720 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
721 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
724 TEST_F(RenderTextTest
, MoveCursorLeftRightInRtl
) {
725 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
727 render_text
->SetText(WideToUTF16(L
"\x05d0\x05d1\x05d2"));
728 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
729 std::vector
<SelectionModel
> expected
;
731 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
732 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
733 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
734 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
735 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
736 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
740 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
741 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
742 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
743 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
744 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
745 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
748 TEST_F(RenderTextTest
, MoveCursorLeftRightInRtlLtr
) {
749 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
751 render_text
->SetText(WideToUTF16(L
"\x05d0\x05d1\x05d2" L
"abc"));
752 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
753 std::vector
<SelectionModel
> expected
;
754 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
755 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
756 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
757 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
758 expected
.push_back(SelectionModel(5, CURSOR_FORWARD
));
759 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
760 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
761 expected
.push_back(SelectionModel(6, CURSOR_FORWARD
));
762 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
765 expected
.push_back(SelectionModel(6, CURSOR_FORWARD
));
766 expected
.push_back(SelectionModel(4, CURSOR_BACKWARD
));
767 expected
.push_back(SelectionModel(5, CURSOR_BACKWARD
));
768 expected
.push_back(SelectionModel(6, CURSOR_BACKWARD
));
769 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
770 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
771 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
772 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
773 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
776 TEST_F(RenderTextTest
, MoveCursorLeftRightInRtlLtrRtl
) {
777 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
779 render_text
->SetText(WideToUTF16(L
"\x05d0" L
"a" L
"\x05d1"));
780 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
781 std::vector
<SelectionModel
> expected
;
782 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
783 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
784 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
785 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
786 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
787 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
790 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
791 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
792 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
793 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
794 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
795 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
798 // TODO(xji): temporarily disable in platform Win since the complex script
799 // characters turned into empty square due to font regression. So, not able
800 // to test 2 characters belong to the same grapheme.
801 #if defined(OS_LINUX)
802 TEST_F(RenderTextTest
, MoveCursorLeftRight_ComplexScript
) {
803 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
805 render_text
->SetText(WideToUTF16(L
"\x0915\x093f\x0915\x094d\x0915"));
806 EXPECT_EQ(0U, render_text
->cursor_position());
807 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
808 EXPECT_EQ(2U, render_text
->cursor_position());
809 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
810 EXPECT_EQ(4U, render_text
->cursor_position());
811 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
812 EXPECT_EQ(5U, render_text
->cursor_position());
813 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
814 EXPECT_EQ(5U, render_text
->cursor_position());
816 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
817 EXPECT_EQ(4U, render_text
->cursor_position());
818 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
819 EXPECT_EQ(2U, render_text
->cursor_position());
820 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
821 EXPECT_EQ(0U, render_text
->cursor_position());
822 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
823 EXPECT_EQ(0U, render_text
->cursor_position());
827 // TODO(ckocagil): Enable for RenderTextHarfBuzz. http://crbug.com/383265
828 TEST_F(RenderTextTest
, MoveCursorLeftRight_MeiryoUILigatures
) {
829 scoped_ptr
<RenderText
> render_text(RenderText::CreateNativeInstance());
830 // Meiryo UI uses single-glyph ligatures for 'ff' and 'ffi', but each letter
831 // (code point) has unique bounds, so mid-glyph cursoring should be possible.
832 render_text
->SetFontList(FontList("Meiryo UI, 12px"));
833 render_text
->SetText(WideToUTF16(L
"ff ffi"));
834 EXPECT_EQ(0U, render_text
->cursor_position());
835 for (size_t i
= 0; i
< render_text
->text().length(); ++i
) {
836 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
837 EXPECT_EQ(i
+ 1, render_text
->cursor_position());
839 EXPECT_EQ(6U, render_text
->cursor_position());
842 TEST_F(RenderTextTest
, GraphemePositions
) {
843 // LTR 2-character grapheme, LTR abc, LTR 2-character grapheme.
844 const base::string16 kText1
=
845 WideToUTF16(L
"\x0915\x093f" L
"abc" L
"\x0915\x093f");
847 // LTR ab, LTR 2-character grapheme, LTR cd.
848 const base::string16 kText2
= WideToUTF16(L
"ab" L
"\x0915\x093f" L
"cd");
850 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
851 // two characters forming the surrogate pair 0x0001D11E.
852 const std::string kSurrogate
= "\xF0\x9D\x84\x9E";
854 // LTR ab, UTF16 surrogate pair, LTR cd.
855 const base::string16 kText3
= UTF8ToUTF16("ab" + kSurrogate
+ "cd");
860 size_t expected_previous
;
861 size_t expected_next
;
863 { base::string16(), 0, 0, 0 },
864 { base::string16(), 1, 0, 0 },
865 { base::string16(), 50, 0, 0 },
875 { kText1
, 50, 7, 7 },
884 { kText2
, 50, 6, 6 },
893 { kText3
, 50, 6, 6 },
897 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
898 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
902 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
903 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); i
++) {
904 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS
"]", i
));
905 render_text
->SetText(cases
[i
].text
);
907 size_t next
= render_text
->IndexOfAdjacentGrapheme(cases
[i
].index
,
909 EXPECT_EQ(cases
[i
].expected_next
, next
);
910 EXPECT_TRUE(render_text
->IsValidCursorIndex(next
));
912 size_t previous
= render_text
->IndexOfAdjacentGrapheme(cases
[i
].index
,
914 EXPECT_EQ(cases
[i
].expected_previous
, previous
);
915 EXPECT_TRUE(render_text
->IsValidCursorIndex(previous
));
919 TEST_F(RenderTextTest
, MidGraphemeSelectionBounds
) {
921 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
922 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
926 // Test that selection bounds may be set amid multi-character graphemes.
927 const base::string16 kHindi
= WideToUTF16(L
"\x0915\x093f");
928 const base::string16 kThai
= WideToUTF16(L
"\x0e08\x0e33");
929 const base::string16 cases
[] = { kHindi
, kThai
};
931 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
932 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
933 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS
"]", i
));
934 render_text
->SetText(cases
[i
]);
935 EXPECT_TRUE(render_text
->IsValidLogicalIndex(1));
936 #if !defined(OS_MACOSX)
937 EXPECT_FALSE(render_text
->IsValidCursorIndex(1));
939 EXPECT_TRUE(render_text
->SelectRange(Range(2, 1)));
940 EXPECT_EQ(Range(2, 1), render_text
->selection());
941 EXPECT_EQ(1U, render_text
->cursor_position());
942 // Although selection bounds may be set within a multi-character grapheme,
943 // cursor movement (e.g. via arrow key) should avoid those indices.
944 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
945 EXPECT_EQ(0U, render_text
->cursor_position());
946 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
947 EXPECT_EQ(2U, render_text
->cursor_position());
951 TEST_F(RenderTextTest
, FindCursorPosition
) {
952 const wchar_t* kTestStrings
[] = { kLtrRtl
, kLtrRtlLtr
, kRtlLtr
, kRtlLtrRtl
};
953 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
954 render_text
->SetDisplayRect(Rect(0, 0, 100, 20));
955 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
956 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS
"]", i
));
957 render_text
->SetText(WideToUTF16(kTestStrings
[i
]));
958 for(size_t j
= 0; j
< render_text
->text().length(); ++j
) {
959 const Range
range(render_text
->GetGlyphBounds(j
));
960 // Test a point just inside the leading edge of the glyph bounds.
961 int x
= range
.is_reversed() ? range
.GetMax() - 1 : range
.GetMin() + 1;
962 EXPECT_EQ(j
, render_text
->FindCursorPosition(Point(x
, 0)).caret_pos());
967 TEST_F(RenderTextTest
, EdgeSelectionModels
) {
968 // Simple Latin text.
969 const base::string16 kLatin
= WideToUTF16(L
"abc");
970 // LTR 2-character grapheme.
971 const base::string16 kLTRGrapheme
= WideToUTF16(L
"\x0915\x093f");
972 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
973 const base::string16 kHindiLatin
=
974 WideToUTF16(L
"\x0915\x093f" L
"a" L
"\x0915\x093f");
975 // RTL 2-character grapheme.
976 const base::string16 kRTLGrapheme
= WideToUTF16(L
"\x05e0\x05b8");
977 // RTL 2-character grapheme, LTR a, RTL 2-character grapheme.
978 const base::string16 kHebrewLatin
=
979 WideToUTF16(L
"\x05e0\x05b8" L
"a" L
"\x05e0\x05b8");
983 base::i18n::TextDirection expected_text_direction
;
985 { base::string16(), base::i18n::LEFT_TO_RIGHT
},
986 { kLatin
, base::i18n::LEFT_TO_RIGHT
},
987 { kLTRGrapheme
, base::i18n::LEFT_TO_RIGHT
},
988 { kHindiLatin
, base::i18n::LEFT_TO_RIGHT
},
989 { kRTLGrapheme
, base::i18n::RIGHT_TO_LEFT
},
990 { kHebrewLatin
, base::i18n::RIGHT_TO_LEFT
},
994 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
995 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
999 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1000 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); i
++) {
1001 render_text
->SetText(cases
[i
].text
);
1002 bool ltr
= (cases
[i
].expected_text_direction
== base::i18n::LEFT_TO_RIGHT
);
1004 SelectionModel start_edge
=
1005 render_text
->EdgeSelectionModel(ltr
? CURSOR_LEFT
: CURSOR_RIGHT
);
1006 EXPECT_EQ(start_edge
, SelectionModel(0, CURSOR_BACKWARD
));
1008 SelectionModel end_edge
=
1009 render_text
->EdgeSelectionModel(ltr
? CURSOR_RIGHT
: CURSOR_LEFT
);
1010 EXPECT_EQ(end_edge
, SelectionModel(cases
[i
].text
.length(), CURSOR_FORWARD
));
1014 TEST_F(RenderTextTest
, SelectAll
) {
1015 const wchar_t* const cases
[] =
1016 { kWeak
, kLtr
, kLtrRtl
, kLtrRtlLtr
, kRtl
, kRtlLtr
, kRtlLtrRtl
};
1018 // Ensure that SelectAll respects the |reversed| argument regardless of
1019 // application locale and text content directionality.
1020 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1021 const SelectionModel
expected_reversed(Range(3, 0), CURSOR_FORWARD
);
1022 const SelectionModel
expected_forwards(Range(0, 3), CURSOR_BACKWARD
);
1023 const bool was_rtl
= base::i18n::IsRTL();
1025 for (size_t i
= 0; i
< 2; ++i
) {
1026 SetRTL(!base::i18n::IsRTL());
1027 // Test that an empty string produces an empty selection model.
1028 render_text
->SetText(base::string16());
1029 EXPECT_EQ(render_text
->selection_model(), SelectionModel());
1031 // Test the weak, LTR, RTL, and Bidi string cases.
1032 for (size_t j
= 0; j
< ARRAYSIZE_UNSAFE(cases
); j
++) {
1033 render_text
->SetText(WideToUTF16(cases
[j
]));
1034 render_text
->SelectAll(false);
1035 EXPECT_EQ(render_text
->selection_model(), expected_forwards
);
1036 render_text
->SelectAll(true);
1037 EXPECT_EQ(render_text
->selection_model(), expected_reversed
);
1041 EXPECT_EQ(was_rtl
, base::i18n::IsRTL());
1044 TEST_F(RenderTextTest
, MoveCursorLeftRightWithSelection
) {
1045 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1046 render_text
->SetText(WideToUTF16(L
"abc\x05d0\x05d1\x05d2"));
1047 // Left arrow on select ranging (6, 4).
1048 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
1049 EXPECT_EQ(Range(6), render_text
->selection());
1050 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1051 EXPECT_EQ(Range(4), render_text
->selection());
1052 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1053 EXPECT_EQ(Range(5), render_text
->selection());
1054 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1055 EXPECT_EQ(Range(6), render_text
->selection());
1056 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, true);
1057 EXPECT_EQ(Range(6, 5), render_text
->selection());
1058 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, true);
1059 EXPECT_EQ(Range(6, 4), render_text
->selection());
1060 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1061 EXPECT_EQ(Range(6), render_text
->selection());
1063 // Right arrow on select ranging (4, 6).
1064 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
1065 EXPECT_EQ(Range(0), render_text
->selection());
1066 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1067 EXPECT_EQ(Range(1), render_text
->selection());
1068 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1069 EXPECT_EQ(Range(2), render_text
->selection());
1070 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1071 EXPECT_EQ(Range(3), render_text
->selection());
1072 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1073 EXPECT_EQ(Range(5), render_text
->selection());
1074 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1075 EXPECT_EQ(Range(4), render_text
->selection());
1076 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, true);
1077 EXPECT_EQ(Range(4, 5), render_text
->selection());
1078 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, true);
1079 EXPECT_EQ(Range(4, 6), render_text
->selection());
1080 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1081 EXPECT_EQ(Range(4), render_text
->selection());
1084 TEST_F(RenderTextTest
, CenteredDisplayOffset
) {
1085 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1086 render_text
->SetText(ASCIIToUTF16("abcdefghij"));
1087 render_text
->SetHorizontalAlignment(ALIGN_CENTER
);
1089 const int kEnlargement
= 10;
1090 const int content_width
= render_text
->GetContentWidth();
1091 Rect
display_rect(0, 0, content_width
/ 2,
1092 render_text
->font_list().GetHeight());
1093 render_text
->SetDisplayRect(display_rect
);
1095 // Move the cursor to the beginning of the text and, by checking the cursor
1096 // bounds, make sure no empty space is to the left of the text.
1097 render_text
->SetCursorPosition(0);
1098 EXPECT_EQ(display_rect
.x(), render_text
->GetUpdatedCursorBounds().x());
1100 // Widen the display rect and, by checking the cursor bounds, make sure no
1101 // empty space is introduced to the left of the text.
1102 display_rect
.Inset(0, 0, -kEnlargement
, 0);
1103 render_text
->SetDisplayRect(display_rect
);
1104 EXPECT_EQ(display_rect
.x(), render_text
->GetUpdatedCursorBounds().x());
1106 // Move the cursor to the end of the text and, by checking the cursor bounds,
1107 // make sure no empty space is to the right of the text.
1108 render_text
->SetCursorPosition(render_text
->text().length());
1109 EXPECT_EQ(display_rect
.right(),
1110 render_text
->GetUpdatedCursorBounds().right());
1112 // Widen the display rect and, by checking the cursor bounds, make sure no
1113 // empty space is introduced to the right of the text.
1114 display_rect
.Inset(0, 0, -kEnlargement
, 0);
1115 render_text
->SetDisplayRect(display_rect
);
1116 EXPECT_EQ(display_rect
.right(),
1117 render_text
->GetUpdatedCursorBounds().right());
1119 #endif // !defined(OS_MACOSX)
1121 // TODO(xji): Make these work on Windows.
1122 #if defined(OS_LINUX)
1123 void MoveLeftRightByWordVerifier(RenderText
* render_text
,
1124 const wchar_t* str
) {
1125 render_text
->SetText(WideToUTF16(str
));
1127 // Test moving by word from left ro right.
1128 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
1129 bool first_word
= true;
1131 // First, test moving by word from a word break position, such as from
1132 // "|abc def" to "abc| def".
1133 SelectionModel start
= render_text
->selection_model();
1134 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1135 SelectionModel end
= render_text
->selection_model();
1136 if (end
== start
) // reach the end.
1139 // For testing simplicity, each word is a 3-character word.
1140 int num_of_character_moves
= first_word
? 3 : 4;
1142 render_text
->MoveCursorTo(start
);
1143 for (int j
= 0; j
< num_of_character_moves
; ++j
)
1144 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1145 EXPECT_EQ(end
, render_text
->selection_model());
1147 // Then, test moving by word from positions inside the word, such as from
1148 // "a|bc def" to "abc| def", and from "ab|c def" to "abc| def".
1149 for (int j
= 1; j
< num_of_character_moves
; ++j
) {
1150 render_text
->MoveCursorTo(start
);
1151 for (int k
= 0; k
< j
; ++k
)
1152 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1153 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1154 EXPECT_EQ(end
, render_text
->selection_model());
1158 // Test moving by word from right to left.
1159 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
1162 SelectionModel start
= render_text
->selection_model();
1163 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, false);
1164 SelectionModel end
= render_text
->selection_model();
1165 if (end
== start
) // reach the end.
1168 int num_of_character_moves
= first_word
? 3 : 4;
1170 render_text
->MoveCursorTo(start
);
1171 for (int j
= 0; j
< num_of_character_moves
; ++j
)
1172 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1173 EXPECT_EQ(end
, render_text
->selection_model());
1175 for (int j
= 1; j
< num_of_character_moves
; ++j
) {
1176 render_text
->MoveCursorTo(start
);
1177 for (int k
= 0; k
< j
; ++k
)
1178 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1179 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, false);
1180 EXPECT_EQ(end
, render_text
->selection_model());
1185 TEST_F(RenderTextTest
, MoveLeftRightByWordInBidiText
) {
1186 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1188 // For testing simplicity, each word is a 3-character word.
1189 std::vector
<const wchar_t*> test
;
1190 test
.push_back(L
"abc");
1191 test
.push_back(L
"abc def");
1192 test
.push_back(L
"\x05E1\x05E2\x05E3");
1193 test
.push_back(L
"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1194 test
.push_back(L
"abc \x05E1\x05E2\x05E3");
1195 test
.push_back(L
"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1196 test
.push_back(L
"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1197 L
" \x05E7\x05E8\x05E9");
1199 test
.push_back(L
"abc \x05E1\x05E2\x05E3 hij");
1200 test
.push_back(L
"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 hij opq");
1201 test
.push_back(L
"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1202 L
" \x05E7\x05E8\x05E9" L
" opq rst uvw");
1204 test
.push_back(L
"\x05E1\x05E2\x05E3 abc");
1205 test
.push_back(L
"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 abc def");
1206 test
.push_back(L
"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 \x05E7\x05E8\x05E9"
1209 test
.push_back(L
"\x05D1\x05D2\x05D3 abc \x05E1\x05E2\x05E3");
1210 test
.push_back(L
"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 abc def"
1211 L
" \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1212 test
.push_back(L
"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 \x05D7\x05D8\x05D9"
1213 L
" abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1214 L
" \x05E7\x05E8\x05E9");
1216 for (size_t i
= 0; i
< test
.size(); ++i
)
1217 MoveLeftRightByWordVerifier(render_text
.get(), test
[i
]);
1220 TEST_F(RenderTextTest
, MoveLeftRightByWordInBidiText_TestEndOfText
) {
1221 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1223 render_text
->SetText(WideToUTF16(L
"ab\x05E1"));
1224 // Moving the cursor by word from "abC|" to the left should return "|abC".
1225 // But since end of text is always treated as a word break, it returns
1227 // TODO(xji): Need to make it work as expected.
1228 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
1229 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, false);
1230 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1232 // Moving the cursor by word from "|abC" to the right returns "abC|".
1233 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
1234 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1235 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD
), render_text
->selection_model());
1237 render_text
->SetText(WideToUTF16(L
"\x05E1\x05E2" L
"a"));
1238 // For logical text "BCa", moving the cursor by word from "aCB|" to the left
1240 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
1241 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, false);
1242 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD
), render_text
->selection_model());
1244 // Moving the cursor by word from "|aCB" to the right should return "aCB|".
1245 // But since end of text is always treated as a word break, it returns
1247 // TODO(xji): Need to make it work as expected.
1248 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
1249 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1250 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1253 TEST_F(RenderTextTest
, MoveLeftRightByWordInTextWithMultiSpaces
) {
1254 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1255 render_text
->SetText(WideToUTF16(L
"abc def"));
1256 render_text
->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD
));
1257 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1258 EXPECT_EQ(11U, render_text
->cursor_position());
1260 render_text
->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD
));
1261 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, false);
1262 EXPECT_EQ(0U, render_text
->cursor_position());
1265 TEST_F(RenderTextTest
, MoveLeftRightByWordInChineseText
) {
1266 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1267 render_text
->SetText(WideToUTF16(L
"\x6211\x4EEC\x53BB\x516C\x56ED\x73A9"));
1268 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
1269 EXPECT_EQ(0U, render_text
->cursor_position());
1270 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1271 EXPECT_EQ(2U, render_text
->cursor_position());
1272 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1273 EXPECT_EQ(3U, render_text
->cursor_position());
1274 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1275 EXPECT_EQ(5U, render_text
->cursor_position());
1276 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1277 EXPECT_EQ(6U, render_text
->cursor_position());
1278 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1279 EXPECT_EQ(6U, render_text
->cursor_position());
1283 // TODO(ckocagil): Remove when RenderTextWin goes away.
1285 TEST_F(RenderTextTest
, Win_LogicalClusters
) {
1286 scoped_ptr
<RenderTextWin
> render_text(
1287 static_cast<RenderTextWin
*>(RenderText::CreateNativeInstance()));
1289 const base::string16 test_string
=
1290 WideToUTF16(L
"\x0930\x0930\x0930\x0930\x0930");
1291 render_text
->SetText(test_string
);
1292 render_text
->EnsureLayout();
1293 ASSERT_EQ(1U, render_text
->runs_
.size());
1294 WORD
* logical_clusters
= render_text
->runs_
[0]->logical_clusters
.get();
1295 for (size_t i
= 0; i
< test_string
.length(); ++i
)
1296 EXPECT_EQ(i
, logical_clusters
[i
]);
1298 #endif // defined(OS_WIN)
1300 TEST_F(RenderTextTest
, StringSizeSanity
) {
1301 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1302 render_text
->SetText(UTF8ToUTF16("Hello World"));
1303 const Size string_size
= render_text
->GetStringSize();
1304 EXPECT_GT(string_size
.width(), 0);
1305 EXPECT_GT(string_size
.height(), 0);
1308 TEST_F(RenderTextTest
, StringSizeLongStrings
) {
1309 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1310 Size previous_string_size
;
1311 for (size_t length
= 10; length
< 1000000; length
*= 10) {
1312 render_text
->SetText(base::string16(length
, 'a'));
1313 const Size string_size
= render_text
->GetStringSize();
1314 EXPECT_GT(string_size
.width(), previous_string_size
.width());
1315 EXPECT_GT(string_size
.height(), 0);
1316 previous_string_size
= string_size
;
1320 // TODO(asvitkine): This test fails because PlatformFontMac uses point font
1321 // sizes instead of pixel sizes like other implementations.
1322 #if !defined(OS_MACOSX)
1323 TEST_F(RenderTextTest
, StringSizeEmptyString
) {
1324 // Ascent and descent of Arial and Symbol are different on most platforms.
1325 const FontList
font_list("Arial,Symbol, 16px");
1326 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1327 render_text
->SetFontList(font_list
);
1328 render_text
->SetDisplayRect(Rect(0, 0, 0, font_list
.GetHeight()));
1330 // The empty string respects FontList metrics for non-zero height
1332 render_text
->SetText(base::string16());
1333 EXPECT_EQ(font_list
.GetHeight(), render_text
->GetStringSize().height());
1334 EXPECT_EQ(0, render_text
->GetStringSize().width());
1335 EXPECT_EQ(font_list
.GetBaseline(), render_text
->GetBaseline());
1337 render_text
->SetText(UTF8ToUTF16(" "));
1338 EXPECT_EQ(font_list
.GetHeight(), render_text
->GetStringSize().height());
1339 EXPECT_EQ(font_list
.GetBaseline(), render_text
->GetBaseline());
1341 #endif // !defined(OS_MACOSX)
1343 TEST_F(RenderTextTest
, StringSizeRespectsFontListMetrics
) {
1344 // Check that Arial and Symbol have different font metrics.
1345 Font
arial_font("Arial", 16);
1347 StringToLowerASCII(arial_font
.GetActualFontNameForTesting()));
1348 Font
symbol_font("Symbol", 16);
1350 StringToLowerASCII(symbol_font
.GetActualFontNameForTesting()));
1351 EXPECT_NE(arial_font
.GetHeight(), symbol_font
.GetHeight());
1352 EXPECT_NE(arial_font
.GetBaseline(), symbol_font
.GetBaseline());
1353 // "a" should be rendered with Arial, not with Symbol.
1354 const char* arial_font_text
= "a";
1355 // "®" (registered trademark symbol) should be rendered with Symbol,
1357 const char* symbol_font_text
= "\xC2\xAE";
1359 Font smaller_font
= arial_font
;
1360 Font larger_font
= symbol_font
;
1361 const char* smaller_font_text
= arial_font_text
;
1362 const char* larger_font_text
= symbol_font_text
;
1363 if (symbol_font
.GetHeight() < arial_font
.GetHeight() &&
1364 symbol_font
.GetBaseline() < arial_font
.GetBaseline()) {
1365 std::swap(smaller_font
, larger_font
);
1366 std::swap(smaller_font_text
, larger_font_text
);
1368 ASSERT_LT(smaller_font
.GetHeight(), larger_font
.GetHeight());
1369 ASSERT_LT(smaller_font
.GetBaseline(), larger_font
.GetBaseline());
1371 // Check |smaller_font_text| is rendered with the smaller font.
1372 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1373 render_text
->SetText(UTF8ToUTF16(smaller_font_text
));
1374 render_text
->SetFontList(FontList(smaller_font
));
1375 render_text
->SetDisplayRect(Rect(0, 0, 0,
1376 render_text
->font_list().GetHeight()));
1377 EXPECT_EQ(smaller_font
.GetHeight(), render_text
->GetStringSize().height());
1378 EXPECT_EQ(smaller_font
.GetBaseline(), render_text
->GetBaseline());
1380 // Layout the same text with mixed fonts. The text should be rendered with
1381 // the smaller font, but the height and baseline are determined with the
1382 // metrics of the font list, which is equal to the larger font.
1383 std::vector
<Font
> fonts
;
1384 fonts
.push_back(smaller_font
); // The primary font is the smaller font.
1385 fonts
.push_back(larger_font
);
1386 const FontList
font_list(fonts
);
1387 render_text
->SetFontList(font_list
);
1388 render_text
->SetDisplayRect(Rect(0, 0, 0,
1389 render_text
->font_list().GetHeight()));
1390 EXPECT_LT(smaller_font
.GetHeight(), render_text
->GetStringSize().height());
1391 EXPECT_LT(smaller_font
.GetBaseline(), render_text
->GetBaseline());
1392 EXPECT_EQ(font_list
.GetHeight(), render_text
->GetStringSize().height());
1393 EXPECT_EQ(font_list
.GetBaseline(), render_text
->GetBaseline());
1396 TEST_F(RenderTextTest
, SetFontList
) {
1397 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1398 render_text
->SetFontList(FontList("Arial,Symbol, 13px"));
1399 const std::vector
<Font
>& fonts
= render_text
->font_list().GetFonts();
1400 ASSERT_EQ(2U, fonts
.size());
1401 EXPECT_EQ("Arial", fonts
[0].GetFontName());
1402 EXPECT_EQ("Symbol", fonts
[1].GetFontName());
1403 EXPECT_EQ(13, render_text
->font_list().GetFontSize());
1406 TEST_F(RenderTextTest
, StringSizeBoldWidth
) {
1407 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1408 render_text
->SetText(UTF8ToUTF16("Hello World"));
1410 const int plain_width
= render_text
->GetStringSize().width();
1411 EXPECT_GT(plain_width
, 0);
1413 // Apply a bold style and check that the new width is greater.
1414 render_text
->SetStyle(BOLD
, true);
1415 const int bold_width
= render_text
->GetStringSize().width();
1416 EXPECT_GT(bold_width
, plain_width
);
1418 // Now, apply a plain style over the first word only.
1419 render_text
->ApplyStyle(BOLD
, false, Range(0, 5));
1420 const int plain_bold_width
= render_text
->GetStringSize().width();
1421 EXPECT_GT(plain_bold_width
, plain_width
);
1422 EXPECT_LT(plain_bold_width
, bold_width
);
1425 TEST_F(RenderTextTest
, StringSizeHeight
) {
1426 base::string16 cases
[] = {
1427 WideToUTF16(L
"Hello World!"), // English
1428 WideToUTF16(L
"\x6328\x62f6"), // Japanese
1429 WideToUTF16(L
"\x0915\x093f"), // Hindi
1430 WideToUTF16(L
"\x05e0\x05b8"), // Hebrew
1433 const FontList default_font_list
;
1434 const FontList
& larger_font_list
= default_font_list
.DeriveWithSizeDelta(24);
1435 EXPECT_GT(larger_font_list
.GetHeight(), default_font_list
.GetHeight());
1437 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); i
++) {
1438 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1439 render_text
->SetFontList(default_font_list
);
1440 render_text
->SetText(cases
[i
]);
1442 const int height1
= render_text
->GetStringSize().height();
1443 EXPECT_GT(height1
, 0);
1445 // Check that setting the larger font increases the height.
1446 render_text
->SetFontList(larger_font_list
);
1447 const int height2
= render_text
->GetStringSize().height();
1448 EXPECT_GT(height2
, height1
);
1452 TEST_F(RenderTextTest
, GetBaselineSanity
) {
1453 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1454 render_text
->SetText(UTF8ToUTF16("Hello World"));
1455 const int baseline
= render_text
->GetBaseline();
1456 EXPECT_GT(baseline
, 0);
1459 TEST_F(RenderTextTest
, CursorBoundsInReplacementMode
) {
1460 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1461 render_text
->SetText(ASCIIToUTF16("abcdefg"));
1462 render_text
->SetDisplayRect(Rect(100, 17));
1463 SelectionModel
sel_b(1, CURSOR_FORWARD
);
1464 SelectionModel
sel_c(2, CURSOR_FORWARD
);
1465 Rect cursor_around_b
= render_text
->GetCursorBounds(sel_b
, false);
1466 Rect cursor_before_b
= render_text
->GetCursorBounds(sel_b
, true);
1467 Rect cursor_before_c
= render_text
->GetCursorBounds(sel_c
, true);
1468 EXPECT_EQ(cursor_around_b
.x(), cursor_before_b
.x());
1469 EXPECT_EQ(cursor_around_b
.right(), cursor_before_c
.x());
1472 TEST_F(RenderTextTest
, GetTextOffset
) {
1473 // The default horizontal text offset differs for LTR and RTL, and is only set
1474 // when the RenderText object is created. This test will check the default in
1475 // LTR mode, and the next test will check the RTL default.
1476 const bool was_rtl
= base::i18n::IsRTL();
1478 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1479 render_text
->SetText(ASCIIToUTF16("abcdefg"));
1480 render_text
->SetFontList(FontList("Arial, 13px"));
1482 // Set display area's size equal to the font size.
1483 const Size
font_size(render_text
->GetContentWidth(),
1484 render_text
->font_list().GetHeight());
1485 Rect
display_rect(font_size
);
1486 render_text
->SetDisplayRect(display_rect
);
1488 Vector2d offset
= render_text
->GetLineOffset(0);
1489 EXPECT_TRUE(offset
.IsZero());
1491 const int kEnlargementX
= 2;
1492 display_rect
.Inset(0, 0, -kEnlargementX
, 0);
1493 render_text
->SetDisplayRect(display_rect
);
1495 // Check the default horizontal alignment.
1496 offset
= render_text
->GetLineOffset(0);
1497 EXPECT_EQ(0, offset
.x());
1499 // Check explicitly setting the horizontal alignment.
1500 render_text
->SetHorizontalAlignment(ALIGN_LEFT
);
1501 offset
= render_text
->GetLineOffset(0);
1502 EXPECT_EQ(0, offset
.x());
1503 render_text
->SetHorizontalAlignment(ALIGN_CENTER
);
1504 offset
= render_text
->GetLineOffset(0);
1505 EXPECT_EQ(kEnlargementX
/ 2, offset
.x());
1506 render_text
->SetHorizontalAlignment(ALIGN_RIGHT
);
1507 offset
= render_text
->GetLineOffset(0);
1508 EXPECT_EQ(kEnlargementX
, offset
.x());
1510 // Check that text is vertically centered within taller display rects.
1511 const int kEnlargementY
= display_rect
.height();
1512 display_rect
.Inset(0, 0, 0, -kEnlargementY
);
1513 render_text
->SetDisplayRect(display_rect
);
1514 const Vector2d prev_offset
= render_text
->GetLineOffset(0);
1515 display_rect
.Inset(0, 0, 0, -2 * kEnlargementY
);
1516 render_text
->SetDisplayRect(display_rect
);
1517 offset
= render_text
->GetLineOffset(0);
1518 EXPECT_EQ(prev_offset
.y() + kEnlargementY
, offset
.y());
1523 TEST_F(RenderTextTest
, GetTextOffsetHorizontalDefaultInRTL
) {
1524 // This only checks the default horizontal alignment in RTL mode; all other
1525 // GetLineOffset(0) attributes are checked by the test above.
1526 const bool was_rtl
= base::i18n::IsRTL();
1528 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1529 render_text
->SetText(ASCIIToUTF16("abcdefg"));
1530 render_text
->SetFontList(FontList("Arial, 13px"));
1531 const int kEnlargement
= 2;
1532 const Size
font_size(render_text
->GetContentWidth() + kEnlargement
,
1533 render_text
->GetStringSize().height());
1534 Rect
display_rect(font_size
);
1535 render_text
->SetDisplayRect(display_rect
);
1536 Vector2d offset
= render_text
->GetLineOffset(0);
1537 EXPECT_EQ(kEnlargement
, offset
.x());
1541 TEST_F(RenderTextTest
, SetDisplayOffset
) {
1542 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1543 render_text
->SetText(ASCIIToUTF16("abcdefg"));
1544 render_text
->SetFontList(FontList("Arial, 13px"));
1546 const Size
font_size(render_text
->GetContentWidth(),
1547 render_text
->font_list().GetHeight());
1548 const int kEnlargement
= 10;
1550 // Set display width |kEnlargement| pixels greater than content width and test
1551 // different possible situations. In this case the only possible display
1553 Rect
display_rect(font_size
);
1554 display_rect
.Inset(0, 0, -kEnlargement
, 0);
1555 render_text
->SetDisplayRect(display_rect
);
1558 HorizontalAlignment alignment
;
1560 } small_content_cases
[] = {
1561 { ALIGN_LEFT
, -kEnlargement
},
1563 { ALIGN_LEFT
, kEnlargement
},
1564 { ALIGN_RIGHT
, -kEnlargement
},
1566 { ALIGN_RIGHT
, kEnlargement
},
1567 { ALIGN_CENTER
, -kEnlargement
},
1568 { ALIGN_CENTER
, 0 },
1569 { ALIGN_CENTER
, kEnlargement
},
1572 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(small_content_cases
); i
++) {
1573 render_text
->SetHorizontalAlignment(small_content_cases
[i
].alignment
);
1574 render_text
->SetDisplayOffset(small_content_cases
[i
].offset
);
1575 EXPECT_EQ(0, render_text
->GetUpdatedDisplayOffset().x());
1578 // Set display width |kEnlargement| pixels less than content width and test
1579 // different possible situations.
1580 display_rect
= Rect(font_size
);
1581 display_rect
.Inset(0, 0, kEnlargement
, 0);
1582 render_text
->SetDisplayRect(display_rect
);
1585 HorizontalAlignment alignment
;
1587 int expected_offset
;
1588 } large_content_cases
[] = {
1589 // When text is left-aligned, display offset can be in range
1590 // [-kEnlargement, 0].
1591 { ALIGN_LEFT
, -2 * kEnlargement
, -kEnlargement
},
1592 { ALIGN_LEFT
, -kEnlargement
/ 2, -kEnlargement
/ 2 },
1593 { ALIGN_LEFT
, kEnlargement
, 0 },
1594 // When text is right-aligned, display offset can be in range
1595 // [0, kEnlargement].
1596 { ALIGN_RIGHT
, -kEnlargement
, 0 },
1597 { ALIGN_RIGHT
, kEnlargement
/ 2, kEnlargement
/ 2 },
1598 { ALIGN_RIGHT
, 2 * kEnlargement
, kEnlargement
},
1599 // When text is center-aligned, display offset can be in range
1600 // [-kEnlargement / 2 - 1, (kEnlargement - 1) / 2].
1601 { ALIGN_CENTER
, -kEnlargement
, -kEnlargement
/ 2 - 1 },
1602 { ALIGN_CENTER
, -kEnlargement
/ 4, -kEnlargement
/ 4 },
1603 { ALIGN_CENTER
, kEnlargement
/ 4, kEnlargement
/ 4 },
1604 { ALIGN_CENTER
, kEnlargement
, (kEnlargement
- 1) / 2 },
1607 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(large_content_cases
); i
++) {
1608 render_text
->SetHorizontalAlignment(large_content_cases
[i
].alignment
);
1609 render_text
->SetDisplayOffset(large_content_cases
[i
].offset
);
1610 EXPECT_EQ(large_content_cases
[i
].expected_offset
,
1611 render_text
->GetUpdatedDisplayOffset().x());
1615 // TODO(ckocagil): Enable for RenderTextHarfBuzz. http://crbug.com/396776
1616 TEST_F(RenderTextTest
, SameFontForParentheses
) {
1618 const base::char16 left_char
;
1619 const base::char16 right_char
;
1620 } punctuation_pairs
[] = {
1626 base::string16 text
;
1629 { WideToUTF16(L
"Hello World(a)") },
1630 // English(English)English
1631 { WideToUTF16(L
"Hello World(a)Hello World") },
1633 // Japanese(English)
1634 { WideToUTF16(L
"\x6328\x62f6(a)") },
1635 // Japanese(English)Japanese
1636 { WideToUTF16(L
"\x6328\x62f6(a)\x6328\x62f6") },
1637 // English(Japanese)English
1638 { WideToUTF16(L
"Hello World(\x6328\x62f6)Hello World") },
1641 { WideToUTF16(L
"\x0915\x093f(a)") },
1642 // Hindi(English)Hindi
1643 { WideToUTF16(L
"\x0915\x093f(a)\x0915\x093f") },
1644 // English(Hindi)English
1645 { WideToUTF16(L
"Hello World(\x0915\x093f)Hello World") },
1648 { WideToUTF16(L
"\x05e0\x05b8(a)") },
1649 // Hebrew(English)Hebrew
1650 { WideToUTF16(L
"\x05e0\x05b8(a)\x05e0\x05b8") },
1651 // English(Hebrew)English
1652 { WideToUTF16(L
"Hello World(\x05e0\x05b8)Hello World") },
1655 scoped_ptr
<RenderText
> render_text(RenderText::CreateNativeInstance());
1656 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
1657 base::string16 text
= cases
[i
].text
;
1658 const size_t start_paren_char_index
= text
.find('(');
1659 ASSERT_NE(base::string16::npos
, start_paren_char_index
);
1660 const size_t end_paren_char_index
= text
.find(')');
1661 ASSERT_NE(base::string16::npos
, end_paren_char_index
);
1663 for (size_t j
= 0; j
< ARRAYSIZE_UNSAFE(punctuation_pairs
); ++j
) {
1664 text
[start_paren_char_index
] = punctuation_pairs
[j
].left_char
;
1665 text
[end_paren_char_index
] = punctuation_pairs
[j
].right_char
;
1666 render_text
->SetText(text
);
1668 const std::vector
<RenderText::FontSpan
> spans
=
1669 render_text
->GetFontSpansForTesting();
1671 int start_paren_span_index
= -1;
1672 int end_paren_span_index
= -1;
1673 for (size_t k
= 0; k
< spans
.size(); ++k
) {
1674 if (IndexInRange(spans
[k
].second
, start_paren_char_index
))
1675 start_paren_span_index
= k
;
1676 if (IndexInRange(spans
[k
].second
, end_paren_char_index
))
1677 end_paren_span_index
= k
;
1679 ASSERT_NE(-1, start_paren_span_index
);
1680 ASSERT_NE(-1, end_paren_span_index
);
1682 const Font
& start_font
= spans
[start_paren_span_index
].first
;
1683 const Font
& end_font
= spans
[end_paren_span_index
].first
;
1684 EXPECT_EQ(start_font
.GetFontName(), end_font
.GetFontName());
1685 EXPECT_EQ(start_font
.GetFontSize(), end_font
.GetFontSize());
1686 EXPECT_EQ(start_font
.GetStyle(), end_font
.GetStyle());
1691 // Make sure the caret width is always >=1 so that the correct
1692 // caret is drawn at high DPI. crbug.com/164100.
1693 TEST_F(RenderTextTest
, CaretWidth
) {
1694 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1695 render_text
->SetText(ASCIIToUTF16("abcdefg"));
1696 EXPECT_GE(render_text
->GetUpdatedCursorBounds().width(), 1);
1699 TEST_F(RenderTextTest
, SelectWord
) {
1700 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1701 render_text
->SetText(ASCIIToUTF16(" foo a.bc.d bar"));
1705 size_t selection_start
;
1706 size_t selection_end
;
1727 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
1728 render_text
->SetCursorPosition(cases
[i
].cursor
);
1729 render_text
->SelectWord();
1730 EXPECT_EQ(Range(cases
[i
].selection_start
, cases
[i
].selection_end
),
1731 render_text
->selection());
1735 // Make sure the last word is selected when the cursor is at text.length().
1736 TEST_F(RenderTextTest
, LastWordSelected
) {
1737 const std::string kTestURL1
= "http://www.google.com";
1738 const std::string kTestURL2
= "http://www.google.com/something/";
1740 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1742 render_text
->SetText(ASCIIToUTF16(kTestURL1
));
1743 render_text
->SetCursorPosition(kTestURL1
.length());
1744 render_text
->SelectWord();
1745 EXPECT_EQ(ASCIIToUTF16("com"), GetSelectedText(render_text
.get()));
1746 EXPECT_FALSE(render_text
->selection().is_reversed());
1748 render_text
->SetText(ASCIIToUTF16(kTestURL2
));
1749 render_text
->SetCursorPosition(kTestURL2
.length());
1750 render_text
->SelectWord();
1751 EXPECT_EQ(ASCIIToUTF16("/"), GetSelectedText(render_text
.get()));
1752 EXPECT_FALSE(render_text
->selection().is_reversed());
1755 // When given a non-empty selection, SelectWord should expand the selection to
1756 // nearest word boundaries.
1757 TEST_F(RenderTextTest
, SelectMultipleWords
) {
1758 const std::string kTestURL
= "http://www.google.com";
1760 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1762 render_text
->SetText(ASCIIToUTF16(kTestURL
));
1763 render_text
->SelectRange(Range(16, 20));
1764 render_text
->SelectWord();
1765 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text
.get()));
1766 EXPECT_FALSE(render_text
->selection().is_reversed());
1768 // SelectWord should preserve the selection direction.
1769 render_text
->SelectRange(Range(20, 16));
1770 render_text
->SelectWord();
1771 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text
.get()));
1772 EXPECT_TRUE(render_text
->selection().is_reversed());
1775 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
1776 // does not implement this yet. http://crbug.com/131618
1777 #if !defined(OS_MACOSX)
1778 TEST_F(RenderTextTest
, DisplayRectShowsCursorLTR
) {
1779 ASSERT_FALSE(base::i18n::IsRTL());
1780 ASSERT_FALSE(base::i18n::ICUIsRTL());
1782 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1783 render_text
->SetText(WideToUTF16(L
"abcdefghijklmnopqrstuvwxzyabcdefg"));
1784 render_text
->MoveCursorTo(SelectionModel(render_text
->text().length(),
1786 int width
= render_text
->GetStringSize().width();
1787 ASSERT_GT(width
, 10);
1789 // Ensure that the cursor is placed at the width of its preceding text.
1790 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1791 EXPECT_EQ(width
, render_text
->GetUpdatedCursorBounds().x());
1793 // Ensure that shrinking the display rectangle keeps the cursor in view.
1794 render_text
->SetDisplayRect(Rect(width
- 10, 1));
1795 EXPECT_EQ(render_text
->display_rect().width(),
1796 render_text
->GetUpdatedCursorBounds().right());
1798 // Ensure that the text will pan to fill its expanding display rectangle.
1799 render_text
->SetDisplayRect(Rect(width
- 5, 1));
1800 EXPECT_EQ(render_text
->display_rect().width(),
1801 render_text
->GetUpdatedCursorBounds().right());
1803 // Ensure that a sufficiently large display rectangle shows all the text.
1804 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1805 EXPECT_EQ(width
, render_text
->GetUpdatedCursorBounds().x());
1807 // Repeat the test with RTL text.
1808 render_text
->SetText(WideToUTF16(L
"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1809 L
"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1810 render_text
->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD
));
1811 width
= render_text
->GetStringSize().width();
1812 ASSERT_GT(width
, 10);
1814 // Ensure that the cursor is placed at the width of its preceding text.
1815 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1816 EXPECT_EQ(width
, render_text
->GetUpdatedCursorBounds().x());
1818 // Ensure that shrinking the display rectangle keeps the cursor in view.
1819 render_text
->SetDisplayRect(Rect(width
- 10, 1));
1820 EXPECT_EQ(render_text
->display_rect().width(),
1821 render_text
->GetUpdatedCursorBounds().right());
1823 // Ensure that the text will pan to fill its expanding display rectangle.
1824 render_text
->SetDisplayRect(Rect(width
- 5, 1));
1825 EXPECT_EQ(render_text
->display_rect().width(),
1826 render_text
->GetUpdatedCursorBounds().right());
1828 // Ensure that a sufficiently large display rectangle shows all the text.
1829 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1830 EXPECT_EQ(width
, render_text
->GetUpdatedCursorBounds().x());
1833 TEST_F(RenderTextTest
, DisplayRectShowsCursorRTL
) {
1834 // Set the application default text direction to RTL.
1835 const bool was_rtl
= base::i18n::IsRTL();
1838 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1839 render_text
->SetText(WideToUTF16(L
"abcdefghijklmnopqrstuvwxzyabcdefg"));
1840 render_text
->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD
));
1841 int width
= render_text
->GetStringSize().width();
1842 ASSERT_GT(width
, 10);
1844 // Ensure that the cursor is placed at the width of its preceding text.
1845 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1846 EXPECT_EQ(render_text
->display_rect().width() - width
- 1,
1847 render_text
->GetUpdatedCursorBounds().x());
1849 // Ensure that shrinking the display rectangle keeps the cursor in view.
1850 render_text
->SetDisplayRect(Rect(width
- 10, 1));
1851 EXPECT_EQ(0, render_text
->GetUpdatedCursorBounds().x());
1853 // Ensure that the text will pan to fill its expanding display rectangle.
1854 render_text
->SetDisplayRect(Rect(width
- 5, 1));
1855 EXPECT_EQ(0, render_text
->GetUpdatedCursorBounds().x());
1857 // Ensure that a sufficiently large display rectangle shows all the text.
1858 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1859 EXPECT_EQ(render_text
->display_rect().width() - width
- 1,
1860 render_text
->GetUpdatedCursorBounds().x());
1862 // Repeat the test with RTL text.
1863 render_text
->SetText(WideToUTF16(L
"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1864 L
"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1865 render_text
->MoveCursorTo(SelectionModel(render_text
->text().length(),
1867 width
= render_text
->GetStringSize().width();
1868 ASSERT_GT(width
, 10);
1870 // Ensure that the cursor is placed at the width of its preceding text.
1871 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1872 EXPECT_EQ(render_text
->display_rect().width() - width
- 1,
1873 render_text
->GetUpdatedCursorBounds().x());
1875 // Ensure that shrinking the display rectangle keeps the cursor in view.
1876 render_text
->SetDisplayRect(Rect(width
- 10, 1));
1877 EXPECT_EQ(0, render_text
->GetUpdatedCursorBounds().x());
1879 // Ensure that the text will pan to fill its expanding display rectangle.
1880 render_text
->SetDisplayRect(Rect(width
- 5, 1));
1881 EXPECT_EQ(0, render_text
->GetUpdatedCursorBounds().x());
1883 // Ensure that a sufficiently large display rectangle shows all the text.
1884 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1885 EXPECT_EQ(render_text
->display_rect().width() - width
- 1,
1886 render_text
->GetUpdatedCursorBounds().x());
1888 // Reset the application default text direction to LTR.
1890 EXPECT_EQ(was_rtl
, base::i18n::IsRTL());
1892 #endif // !defined(OS_MACOSX)
1894 // Changing colors between or inside ligated glyphs should not break shaping.
1895 TEST_F(RenderTextTest
, SelectionKeepsLigatures
) {
1896 const wchar_t* kTestStrings
[] = { L
"\x644\x623", L
"\x633\x627" };
1897 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1898 render_text
->set_selection_color(SK_ColorRED
);
1901 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
1902 render_text
->SetText(WideToUTF16(kTestStrings
[i
]));
1903 const int expected_width
= render_text
->GetStringSize().width();
1904 render_text
->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD
));
1905 EXPECT_EQ(expected_width
, render_text
->GetStringSize().width());
1906 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119
1907 render_text
->Draw(&canvas
);
1908 render_text
->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD
));
1913 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1914 // Ensure strings wrap onto multiple lines for a small available width.
1915 TEST_F(RenderTextTest
, Multiline_MinWidth
) {
1916 const wchar_t* kTestStrings
[] = { kWeak
, kLtr
, kLtrRtl
, kLtrRtlLtr
, kRtl
,
1917 kRtlLtr
, kRtlLtrRtl
};
1919 scoped_ptr
<RenderTextWin
> render_text(
1920 static_cast<RenderTextWin
*>(RenderText::CreateNativeInstance()));
1921 render_text
->SetDisplayRect(Rect(1, 1000));
1922 render_text
->SetMultiline(true);
1925 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
1926 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"]", i
));
1927 render_text
->SetText(WideToUTF16(kTestStrings
[i
]));
1928 render_text
->Draw(&canvas
);
1929 EXPECT_GT(render_text
->lines_
.size(), 1U);
1933 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1934 // Ensure strings wrap onto multiple lines for a normal available width.
1935 TEST_F(RenderTextTest
, Multiline_NormalWidth
) {
1937 const wchar_t* const text
;
1938 const Range first_line_char_range
;
1939 const Range second_line_char_range
;
1940 } kTestStrings
[] = {
1941 { L
"abc defg hijkl", Range(0, 9), Range(9, 14) },
1942 { L
"qwertyzxcvbn", Range(0, 8), Range(8, 12) },
1943 { L
"\x062A\x0641\x0627\x062D\x05EA\x05E4\x05D5\x05D6\x05D9\x05DD",
1944 Range(4, 10), Range(0, 4) }
1947 scoped_ptr
<RenderTextWin
> render_text(
1948 static_cast<RenderTextWin
*>(RenderText::CreateNativeInstance()));
1949 render_text
->SetDisplayRect(Rect(50, 1000));
1950 render_text
->SetMultiline(true);
1953 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kTestStrings
); ++i
) {
1954 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"]", i
));
1955 render_text
->SetText(WideToUTF16(kTestStrings
[i
].text
));
1956 render_text
->Draw(&canvas
);
1957 ASSERT_EQ(2U, render_text
->lines_
.size());
1958 ASSERT_EQ(1U, render_text
->lines_
[0].segments
.size());
1959 EXPECT_EQ(kTestStrings
[i
].first_line_char_range
,
1960 render_text
->lines_
[0].segments
[0].char_range
);
1961 ASSERT_EQ(1U, render_text
->lines_
[1].segments
.size());
1962 EXPECT_EQ(kTestStrings
[i
].second_line_char_range
,
1963 render_text
->lines_
[1].segments
[0].char_range
);
1967 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1968 // Ensure strings don't wrap onto multiple lines for a sufficient available
1970 TEST_F(RenderTextTest
, Multiline_SufficientWidth
) {
1971 const wchar_t* kTestStrings
[] = { L
"", L
" ", L
".", L
" . ", L
"abc", L
"a b c",
1972 L
"\x62E\x628\x632", L
"\x62E \x628 \x632" };
1974 scoped_ptr
<RenderTextWin
> render_text(
1975 static_cast<RenderTextWin
*>(RenderText::CreateNativeInstance()));
1976 render_text
->SetDisplayRect(Rect(30, 1000));
1977 render_text
->SetMultiline(true);
1980 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
1981 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"]", i
));
1982 render_text
->SetText(WideToUTF16(kTestStrings
[i
]));
1983 render_text
->Draw(&canvas
);
1984 EXPECT_EQ(1U, render_text
->lines_
.size());
1988 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1989 TEST_F(RenderTextTest
, Multiline_Newline
) {
1991 const wchar_t* const text
;
1992 // Ranges of the characters on each line preceding the newline.
1993 const Range first_line_char_range
;
1994 const Range second_line_char_range
;
1995 } kTestStrings
[] = {
1996 { L
"abc\ndef", Range(0, 3), Range(4, 7) },
1997 { L
"a \n b ", Range(0, 2), Range(3, 6) },
1998 { L
"\n" , Range::InvalidRange(), Range::InvalidRange() }
2001 scoped_ptr
<RenderTextWin
> render_text(
2002 static_cast<RenderTextWin
*>(RenderText::CreateNativeInstance()));
2003 render_text
->SetDisplayRect(Rect(200, 1000));
2004 render_text
->SetMultiline(true);
2007 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(kTestStrings
); ++i
) {
2008 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"]", i
));
2009 render_text
->SetText(WideToUTF16(kTestStrings
[i
].text
));
2010 render_text
->Draw(&canvas
);
2012 ASSERT_EQ(2U, render_text
->lines_
.size());
2014 const Range first_expected_range
= kTestStrings
[i
].first_line_char_range
;
2015 ASSERT_EQ(first_expected_range
.IsValid() ? 2U : 1U,
2016 render_text
->lines_
[0].segments
.size());
2017 if (first_expected_range
.IsValid())
2018 EXPECT_EQ(first_expected_range
,
2019 render_text
->lines_
[0].segments
[0].char_range
);
2021 const internal::LineSegment
& newline_segment
=
2022 render_text
->lines_
[0].segments
[first_expected_range
.IsValid() ? 1 : 0];
2023 ASSERT_EQ(1U, newline_segment
.char_range
.length());
2024 EXPECT_EQ(L
'\n', kTestStrings
[i
].text
[newline_segment
.char_range
.start()]);
2026 const Range second_expected_range
= kTestStrings
[i
].second_line_char_range
;
2027 ASSERT_EQ(second_expected_range
.IsValid() ? 1U : 0U,
2028 render_text
->lines_
[1].segments
.size());
2029 if (second_expected_range
.IsValid())
2030 EXPECT_EQ(second_expected_range
,
2031 render_text
->lines_
[1].segments
[0].char_range
);
2035 // TODO(ckocagil): Remove when RenderTextWin goes away.
2036 TEST_F(RenderTextTest
, BreakRunsByUnicodeBlocks
) {
2037 scoped_ptr
<RenderTextWin
> render_text(
2038 static_cast<RenderTextWin
*>(RenderText::CreateNativeInstance()));
2040 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2041 render_text
->SetText(WideToUTF16(L
"x\x25B6y"));
2042 render_text
->EnsureLayout();
2043 ASSERT_EQ(3U, render_text
->runs_
.size());
2044 EXPECT_EQ(Range(0, 1), render_text
->runs_
[0]->range
);
2045 EXPECT_EQ(Range(1, 2), render_text
->runs_
[1]->range
);
2046 EXPECT_EQ(Range(2, 3), render_text
->runs_
[2]->range
);
2048 render_text
->SetText(WideToUTF16(L
"x \x25B6 y"));
2049 render_text
->EnsureLayout();
2050 ASSERT_EQ(3U, render_text
->runs_
.size());
2051 EXPECT_EQ(Range(0, 2), render_text
->runs_
[0]->range
);
2052 EXPECT_EQ(Range(2, 3), render_text
->runs_
[1]->range
);
2053 EXPECT_EQ(Range(3, 5), render_text
->runs_
[2]->range
);
2055 #endif // defined(OS_WIN)
2057 TEST_F(RenderTextTest
, HarfBuzz_CharToGlyph
) {
2059 uint32 glyph_to_char
[4];
2060 size_t char_to_glyph_expected
[4];
2061 Range char_range_to_glyph_range_expected
[4];
2064 { // From string "A B C D" to glyphs "a b c d".
2067 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2070 { // From string "A B C D" to glyphs "d b c a".
2073 { Range(3, 4), Range(2, 3), Range(1, 2), Range(0, 1) },
2076 { // From string "A B C D" to glyphs "ab c c d".
2079 { Range(0, 1), Range(0, 1), Range(1, 3), Range(3, 4) },
2082 { // From string "A B C D" to glyphs "d c c ba".
2085 { Range(3, 4), Range(3, 4), Range(1, 3), Range(0, 1) },
2090 internal::TextRunHarfBuzz run
;
2091 run
.range
= Range(0, 4);
2092 run
.glyph_count
= 4;
2093 run
.glyph_to_char
.reset(new uint32
[4]);
2095 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
2096 std::copy(cases
[i
].glyph_to_char
, cases
[i
].glyph_to_char
+ 4,
2097 run
.glyph_to_char
.get());
2098 run
.is_rtl
= cases
[i
].is_rtl
;
2099 for (size_t j
= 0; j
< 4; ++j
) {
2100 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS
", char %" PRIuS
, i
, j
));
2101 EXPECT_EQ(cases
[i
].char_to_glyph_expected
[j
], run
.CharToGlyph(j
));
2102 EXPECT_EQ(cases
[i
].char_range_to_glyph_range_expected
[j
],
2103 run
.CharRangeToGlyphRange(Range(j
, j
+ 1)));
2108 TEST_F(RenderTextTest
, HarfBuzz_RunDirection
) {
2109 RenderTextHarfBuzz render_text
;
2110 const base::string16 mixed
=
2111 WideToUTF16(L
"\x05D0\x05D1" L
"1234" L
"\x05D2\x05D3");
2112 render_text
.SetText(mixed
);
2113 render_text
.EnsureLayout();
2114 ASSERT_EQ(3U, render_text
.runs_
.size());
2115 EXPECT_TRUE(render_text
.runs_
[0]->is_rtl
);
2116 EXPECT_FALSE(render_text
.runs_
[1]->is_rtl
);
2117 EXPECT_TRUE(render_text
.runs_
[2]->is_rtl
);
2120 TEST_F(RenderTextTest
, HarfBuzz_BreakRunsByUnicodeBlocks
) {
2121 RenderTextHarfBuzz render_text
;
2123 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2124 render_text
.SetText(WideToUTF16(L
"x\x25B6y"));
2125 render_text
.EnsureLayout();
2126 ASSERT_EQ(3U, render_text
.runs_
.size());
2127 EXPECT_EQ(Range(0, 1), render_text
.runs_
[0]->range
);
2128 EXPECT_EQ(Range(1, 2), render_text
.runs_
[1]->range
);
2129 EXPECT_EQ(Range(2, 3), render_text
.runs_
[2]->range
);
2131 render_text
.SetText(WideToUTF16(L
"x \x25B6 y"));
2132 render_text
.EnsureLayout();
2133 ASSERT_EQ(3U, render_text
.runs_
.size());
2134 EXPECT_EQ(Range(0, 2), render_text
.runs_
[0]->range
);
2135 EXPECT_EQ(Range(2, 3), render_text
.runs_
[1]->range
);
2136 EXPECT_EQ(Range(3, 5), render_text
.runs_
[2]->range
);
2139 // Disabled on Mac because RenderTextMac doesn't implement GetGlyphBounds.
2140 #if !defined(OS_MACOSX)
2141 TEST_F(RenderTextTest
, GlyphBounds
) {
2142 const wchar_t* kTestStrings
[] = {
2143 L
"asdf 1234 qwer", L
"\x0647\x0654", L
"\x0645\x0631\x062D\x0628\x0627"
2145 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
2147 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2148 render_text
->SetText(WideToUTF16(kTestStrings
[i
]));
2149 render_text
->EnsureLayout();
2151 for (size_t j
= 0; j
< render_text
->text().length(); ++j
)
2152 EXPECT_FALSE(render_text
->GetGlyphBounds(j
).is_empty());
2157 // Remove this after making RTHB default in favor of RenderTextTest.GlyphBounds.
2158 TEST_F(RenderTextTest
, HarfBuzz_GlyphBounds
) {
2159 const wchar_t* kTestStrings
[] = {
2160 L
"asdf 1234 qwer", L
"\x0647\x0654", L
"\x0645\x0631\x062D\x0628\x0627"
2162 scoped_ptr
<RenderText
> render_text(new RenderTextHarfBuzz
);
2164 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2165 render_text
->SetText(WideToUTF16(kTestStrings
[i
]));
2166 render_text
->EnsureLayout();
2168 for (size_t j
= 0; j
< render_text
->text().length(); ++j
)
2169 EXPECT_FALSE(render_text
->GetGlyphBounds(j
).is_empty());