[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / gfx / render_text_unittest.cc
blob3fc5aa3408abd1f9090e5103628d34c8f0ee4d2d
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"
7 #include <algorithm>
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"
19 #if defined(OS_WIN)
20 #include "base/win/windows_version.h"
21 #include "ui/gfx/render_text_win.h"
22 #endif
24 #if defined(OS_LINUX) && !defined(USE_OZONE)
25 #include "ui/gfx/render_text_pango.h"
26 #endif
28 using base::ASCIIToUTF16;
29 using base::UTF8ToUTF16;
30 using base::WideToUTF16;
32 namespace gfx {
34 namespace {
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";
45 #endif
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)
84 } // namespace
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.
117 if (i == 1)
118 render_text->SetStyle(STRIKE, true);
119 if (i >= 1)
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));
195 struct {
196 int start;
197 int end;
198 bool bold;
199 bool italic;
200 } cases[] = {
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);
232 #endif
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,
239 bool select) {
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.
407 struct {
408 const wchar_t* text;
409 const wchar_t* layout_text;
410 const bool elision_expected;
411 } cases[] = {
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) {
491 struct {
492 const wchar_t* text;
493 const wchar_t* layout_text;
494 } cases[] = {
495 // Strings shorter than the truncation length should be laid out in full.
496 { L"", L"" },
497 { kWeak, kWeak },
498 { kLtr, kLtr },
499 { kLtrRtl, kLtrRtl },
500 { kLtrRtlLtr, kLtrRtlLtr },
501 { kRtl, kRtl },
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);
563 expected.clear();
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);
591 expected.clear();
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) {
601 struct {
602 const wchar_t* text;
603 const base::i18n::TextDirection text_direction;
604 } cases[] = {
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());
654 // Pure LTR.
655 render_text->SetText(ASCIIToUTF16("abc"));
656 // |expected| saves the expected SelectionModel when moving cursor from left
657 // to right.
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);
666 expected.clear();
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());
677 // LTR-RTL
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);
691 expected.clear();
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());
705 // LTR-RTL-LTR.
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);
715 expected.clear();
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());
726 // Pure RTL.
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);
738 expected.clear();
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());
750 // RTL-LTR
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);
764 expected.clear();
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());
778 // RTL-LTR-RTL.
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);
789 expected.clear();
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());
825 #endif
827 TEST_F(RenderTextTest, MoveCursorLeftRight_MeiryoUILigatures) {
828 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
829 // Meiryo UI uses single-glyph ligatures for 'ff' and 'ffi', but each letter
830 // (code point) has unique bounds, so mid-glyph cursoring should be possible.
831 render_text->SetFontList(FontList("Meiryo UI, 12px"));
832 render_text->SetText(WideToUTF16(L"ff ffi"));
833 EXPECT_EQ(0U, render_text->cursor_position());
834 for (size_t i = 0; i < render_text->text().length(); ++i) {
835 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
836 EXPECT_EQ(i + 1, render_text->cursor_position());
838 EXPECT_EQ(6U, render_text->cursor_position());
841 TEST_F(RenderTextTest, GraphemePositions) {
842 // LTR 2-character grapheme, LTR abc, LTR 2-character grapheme.
843 const base::string16 kText1 =
844 WideToUTF16(L"\x0915\x093f" L"abc" L"\x0915\x093f");
846 // LTR ab, LTR 2-character grapheme, LTR cd.
847 const base::string16 kText2 = WideToUTF16(L"ab" L"\x0915\x093f" L"cd");
849 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
850 // two characters forming the surrogate pair 0x0001D11E.
851 const std::string kSurrogate = "\xF0\x9D\x84\x9E";
853 // LTR ab, UTF16 surrogate pair, LTR cd.
854 const base::string16 kText3 = UTF8ToUTF16("ab" + kSurrogate + "cd");
856 struct {
857 base::string16 text;
858 size_t index;
859 size_t expected_previous;
860 size_t expected_next;
861 } cases[] = {
862 { base::string16(), 0, 0, 0 },
863 { base::string16(), 1, 0, 0 },
864 { base::string16(), 50, 0, 0 },
865 { kText1, 0, 0, 2 },
866 { kText1, 1, 0, 2 },
867 { kText1, 2, 0, 3 },
868 { kText1, 3, 2, 4 },
869 { kText1, 4, 3, 5 },
870 { kText1, 5, 4, 7 },
871 { kText1, 6, 5, 7 },
872 { kText1, 7, 5, 7 },
873 { kText1, 8, 7, 7 },
874 { kText1, 50, 7, 7 },
875 { kText2, 0, 0, 1 },
876 { kText2, 1, 0, 2 },
877 { kText2, 2, 1, 4 },
878 { kText2, 3, 2, 4 },
879 { kText2, 4, 2, 5 },
880 { kText2, 5, 4, 6 },
881 { kText2, 6, 5, 6 },
882 { kText2, 7, 6, 6 },
883 { kText2, 50, 6, 6 },
884 { kText3, 0, 0, 1 },
885 { kText3, 1, 0, 2 },
886 { kText3, 2, 1, 4 },
887 { kText3, 3, 2, 4 },
888 { kText3, 4, 2, 5 },
889 { kText3, 5, 4, 6 },
890 { kText3, 6, 5, 6 },
891 { kText3, 7, 6, 6 },
892 { kText3, 50, 6, 6 },
895 #if defined(OS_WIN)
896 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
897 if (base::win::GetVersion() < base::win::VERSION_VISTA)
898 return;
899 #endif
901 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
902 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
903 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
904 render_text->SetText(cases[i].text);
906 size_t next = render_text->IndexOfAdjacentGrapheme(cases[i].index,
907 CURSOR_FORWARD);
908 EXPECT_EQ(cases[i].expected_next, next);
909 EXPECT_TRUE(render_text->IsValidCursorIndex(next));
911 size_t previous = render_text->IndexOfAdjacentGrapheme(cases[i].index,
912 CURSOR_BACKWARD);
913 EXPECT_EQ(cases[i].expected_previous, previous);
914 EXPECT_TRUE(render_text->IsValidCursorIndex(previous));
918 TEST_F(RenderTextTest, MidGraphemeSelectionBounds) {
919 #if defined(OS_WIN)
920 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
921 if (base::win::GetVersion() < base::win::VERSION_VISTA)
922 return;
923 #endif
925 // Test that selection bounds may be set amid multi-character graphemes.
926 const base::string16 kHindi = WideToUTF16(L"\x0915\x093f");
927 const base::string16 kThai = WideToUTF16(L"\x0e08\x0e33");
928 const base::string16 cases[] = { kHindi, kThai };
930 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
931 for (size_t i = 0; i < arraysize(cases); i++) {
932 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
933 render_text->SetText(cases[i]);
934 EXPECT_TRUE(render_text->IsValidLogicalIndex(1));
935 #if !defined(OS_MACOSX)
936 EXPECT_FALSE(render_text->IsValidCursorIndex(1));
937 #endif
938 EXPECT_TRUE(render_text->SelectRange(Range(2, 1)));
939 EXPECT_EQ(Range(2, 1), render_text->selection());
940 EXPECT_EQ(1U, render_text->cursor_position());
941 // Although selection bounds may be set within a multi-character grapheme,
942 // cursor movement (e.g. via arrow key) should avoid those indices.
943 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
944 EXPECT_EQ(0U, render_text->cursor_position());
945 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
946 EXPECT_EQ(2U, render_text->cursor_position());
950 TEST_F(RenderTextTest, FindCursorPosition) {
951 const wchar_t* kTestStrings[] = { kLtrRtl, kLtrRtlLtr, kRtlLtr, kRtlLtrRtl };
952 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
953 render_text->SetDisplayRect(Rect(0, 0, 100, 20));
954 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
955 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS "]", i));
956 render_text->SetText(WideToUTF16(kTestStrings[i]));
957 for(size_t j = 0; j < render_text->text().length(); ++j) {
958 const Range range(render_text->GetGlyphBounds(j));
959 // Test a point just inside the leading edge of the glyph bounds.
960 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1;
961 EXPECT_EQ(j, render_text->FindCursorPosition(Point(x, 0)).caret_pos());
966 TEST_F(RenderTextTest, EdgeSelectionModels) {
967 // Simple Latin text.
968 const base::string16 kLatin = WideToUTF16(L"abc");
969 // LTR 2-character grapheme.
970 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f");
971 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
972 const base::string16 kHindiLatin =
973 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f");
974 // RTL 2-character grapheme.
975 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8");
976 // RTL 2-character grapheme, LTR a, RTL 2-character grapheme.
977 const base::string16 kHebrewLatin =
978 WideToUTF16(L"\x05e0\x05b8" L"a" L"\x05e0\x05b8");
980 struct {
981 base::string16 text;
982 base::i18n::TextDirection expected_text_direction;
983 } cases[] = {
984 { base::string16(), base::i18n::LEFT_TO_RIGHT },
985 { kLatin, base::i18n::LEFT_TO_RIGHT },
986 { kLTRGrapheme, base::i18n::LEFT_TO_RIGHT },
987 { kHindiLatin, base::i18n::LEFT_TO_RIGHT },
988 { kRTLGrapheme, base::i18n::RIGHT_TO_LEFT },
989 { kHebrewLatin, base::i18n::RIGHT_TO_LEFT },
992 #if defined(OS_WIN)
993 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
994 if (base::win::GetVersion() < base::win::VERSION_VISTA)
995 return;
996 #endif
998 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
999 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
1000 render_text->SetText(cases[i].text);
1001 bool ltr = (cases[i].expected_text_direction == base::i18n::LEFT_TO_RIGHT);
1003 SelectionModel start_edge =
1004 render_text->EdgeSelectionModel(ltr ? CURSOR_LEFT : CURSOR_RIGHT);
1005 EXPECT_EQ(start_edge, SelectionModel(0, CURSOR_BACKWARD));
1007 SelectionModel end_edge =
1008 render_text->EdgeSelectionModel(ltr ? CURSOR_RIGHT : CURSOR_LEFT);
1009 EXPECT_EQ(end_edge, SelectionModel(cases[i].text.length(), CURSOR_FORWARD));
1013 TEST_F(RenderTextTest, SelectAll) {
1014 const wchar_t* const cases[] =
1015 { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl };
1017 // Ensure that SelectAll respects the |reversed| argument regardless of
1018 // application locale and text content directionality.
1019 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1020 const SelectionModel expected_reversed(Range(3, 0), CURSOR_FORWARD);
1021 const SelectionModel expected_forwards(Range(0, 3), CURSOR_BACKWARD);
1022 const bool was_rtl = base::i18n::IsRTL();
1024 for (size_t i = 0; i < 2; ++i) {
1025 SetRTL(!base::i18n::IsRTL());
1026 // Test that an empty string produces an empty selection model.
1027 render_text->SetText(base::string16());
1028 EXPECT_EQ(render_text->selection_model(), SelectionModel());
1030 // Test the weak, LTR, RTL, and Bidi string cases.
1031 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(cases); j++) {
1032 render_text->SetText(WideToUTF16(cases[j]));
1033 render_text->SelectAll(false);
1034 EXPECT_EQ(render_text->selection_model(), expected_forwards);
1035 render_text->SelectAll(true);
1036 EXPECT_EQ(render_text->selection_model(), expected_reversed);
1040 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1043 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) {
1044 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1045 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
1046 // Left arrow on select ranging (6, 4).
1047 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1048 EXPECT_EQ(Range(6), render_text->selection());
1049 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1050 EXPECT_EQ(Range(4), render_text->selection());
1051 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1052 EXPECT_EQ(Range(5), render_text->selection());
1053 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1054 EXPECT_EQ(Range(6), render_text->selection());
1055 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1056 EXPECT_EQ(Range(6, 5), render_text->selection());
1057 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1058 EXPECT_EQ(Range(6, 4), render_text->selection());
1059 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1060 EXPECT_EQ(Range(6), render_text->selection());
1062 // Right arrow on select ranging (4, 6).
1063 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1064 EXPECT_EQ(Range(0), render_text->selection());
1065 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1066 EXPECT_EQ(Range(1), render_text->selection());
1067 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1068 EXPECT_EQ(Range(2), render_text->selection());
1069 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1070 EXPECT_EQ(Range(3), render_text->selection());
1071 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1072 EXPECT_EQ(Range(5), render_text->selection());
1073 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1074 EXPECT_EQ(Range(4), render_text->selection());
1075 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1076 EXPECT_EQ(Range(4, 5), render_text->selection());
1077 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1078 EXPECT_EQ(Range(4, 6), render_text->selection());
1079 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1080 EXPECT_EQ(Range(4), render_text->selection());
1082 #endif // !defined(OS_MACOSX)
1084 // TODO(xji): Make these work on Windows.
1085 #if defined(OS_LINUX)
1086 void MoveLeftRightByWordVerifier(RenderText* render_text,
1087 const wchar_t* str) {
1088 render_text->SetText(WideToUTF16(str));
1090 // Test moving by word from left ro right.
1091 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1092 bool first_word = true;
1093 while (true) {
1094 // First, test moving by word from a word break position, such as from
1095 // "|abc def" to "abc| def".
1096 SelectionModel start = render_text->selection_model();
1097 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1098 SelectionModel end = render_text->selection_model();
1099 if (end == start) // reach the end.
1100 break;
1102 // For testing simplicity, each word is a 3-character word.
1103 int num_of_character_moves = first_word ? 3 : 4;
1104 first_word = false;
1105 render_text->MoveCursorTo(start);
1106 for (int j = 0; j < num_of_character_moves; ++j)
1107 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1108 EXPECT_EQ(end, render_text->selection_model());
1110 // Then, test moving by word from positions inside the word, such as from
1111 // "a|bc def" to "abc| def", and from "ab|c def" to "abc| def".
1112 for (int j = 1; j < num_of_character_moves; ++j) {
1113 render_text->MoveCursorTo(start);
1114 for (int k = 0; k < j; ++k)
1115 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1116 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1117 EXPECT_EQ(end, render_text->selection_model());
1121 // Test moving by word from right to left.
1122 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1123 first_word = true;
1124 while (true) {
1125 SelectionModel start = render_text->selection_model();
1126 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1127 SelectionModel end = render_text->selection_model();
1128 if (end == start) // reach the end.
1129 break;
1131 int num_of_character_moves = first_word ? 3 : 4;
1132 first_word = false;
1133 render_text->MoveCursorTo(start);
1134 for (int j = 0; j < num_of_character_moves; ++j)
1135 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1136 EXPECT_EQ(end, render_text->selection_model());
1138 for (int j = 1; j < num_of_character_moves; ++j) {
1139 render_text->MoveCursorTo(start);
1140 for (int k = 0; k < j; ++k)
1141 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1142 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1143 EXPECT_EQ(end, render_text->selection_model());
1148 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText) {
1149 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1151 // For testing simplicity, each word is a 3-character word.
1152 std::vector<const wchar_t*> test;
1153 test.push_back(L"abc");
1154 test.push_back(L"abc def");
1155 test.push_back(L"\x05E1\x05E2\x05E3");
1156 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1157 test.push_back(L"abc \x05E1\x05E2\x05E3");
1158 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1159 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1160 L" \x05E7\x05E8\x05E9");
1162 test.push_back(L"abc \x05E1\x05E2\x05E3 hij");
1163 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 hij opq");
1164 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1165 L" \x05E7\x05E8\x05E9" L" opq rst uvw");
1167 test.push_back(L"\x05E1\x05E2\x05E3 abc");
1168 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 abc def");
1169 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 \x05E7\x05E8\x05E9"
1170 L" abc def hij");
1172 test.push_back(L"\x05D1\x05D2\x05D3 abc \x05E1\x05E2\x05E3");
1173 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 abc def"
1174 L" \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1175 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 \x05D7\x05D8\x05D9"
1176 L" abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1177 L" \x05E7\x05E8\x05E9");
1179 for (size_t i = 0; i < test.size(); ++i)
1180 MoveLeftRightByWordVerifier(render_text.get(), test[i]);
1183 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText_TestEndOfText) {
1184 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1186 render_text->SetText(WideToUTF16(L"ab\x05E1"));
1187 // Moving the cursor by word from "abC|" to the left should return "|abC".
1188 // But since end of text is always treated as a word break, it returns
1189 // position "ab|C".
1190 // TODO(xji): Need to make it work as expected.
1191 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1192 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1193 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1195 // Moving the cursor by word from "|abC" to the right returns "abC|".
1196 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1197 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1198 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1200 render_text->SetText(WideToUTF16(L"\x05E1\x05E2" L"a"));
1201 // For logical text "BCa", moving the cursor by word from "aCB|" to the left
1202 // returns "|aCB".
1203 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1204 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1205 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1207 // Moving the cursor by word from "|aCB" to the right should return "aCB|".
1208 // But since end of text is always treated as a word break, it returns
1209 // position "a|CB".
1210 // TODO(xji): Need to make it work as expected.
1211 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1212 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1213 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1216 TEST_F(RenderTextTest, MoveLeftRightByWordInTextWithMultiSpaces) {
1217 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1218 render_text->SetText(WideToUTF16(L"abc def"));
1219 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1220 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1221 EXPECT_EQ(11U, render_text->cursor_position());
1223 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1224 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1225 EXPECT_EQ(0U, render_text->cursor_position());
1228 TEST_F(RenderTextTest, MoveLeftRightByWordInChineseText) {
1229 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1230 render_text->SetText(WideToUTF16(L"\x6211\x4EEC\x53BB\x516C\x56ED\x73A9"));
1231 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1232 EXPECT_EQ(0U, render_text->cursor_position());
1233 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1234 EXPECT_EQ(2U, render_text->cursor_position());
1235 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1236 EXPECT_EQ(3U, render_text->cursor_position());
1237 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1238 EXPECT_EQ(5U, render_text->cursor_position());
1239 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1240 EXPECT_EQ(6U, render_text->cursor_position());
1241 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1242 EXPECT_EQ(6U, render_text->cursor_position());
1244 #endif
1246 #if defined(OS_WIN)
1247 TEST_F(RenderTextTest, Win_LogicalClusters) {
1248 scoped_ptr<RenderTextWin> render_text(
1249 static_cast<RenderTextWin*>(RenderText::CreateInstance()));
1251 const base::string16 test_string =
1252 WideToUTF16(L"\x0930\x0930\x0930\x0930\x0930");
1253 render_text->SetText(test_string);
1254 render_text->EnsureLayout();
1255 ASSERT_EQ(1U, render_text->runs_.size());
1256 WORD* logical_clusters = render_text->runs_[0]->logical_clusters.get();
1257 for (size_t i = 0; i < test_string.length(); ++i)
1258 EXPECT_EQ(i, logical_clusters[i]);
1260 #endif // defined(OS_WIN)
1262 TEST_F(RenderTextTest, StringSizeSanity) {
1263 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1264 render_text->SetText(UTF8ToUTF16("Hello World"));
1265 const Size string_size = render_text->GetStringSize();
1266 EXPECT_GT(string_size.width(), 0);
1267 EXPECT_GT(string_size.height(), 0);
1270 TEST_F(RenderTextTest, StringSizeLongStrings) {
1271 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1272 Size previous_string_size;
1273 for (size_t length = 10; length < 1000000; length *= 10) {
1274 render_text->SetText(base::string16(length, 'a'));
1275 const Size string_size = render_text->GetStringSize();
1276 EXPECT_GT(string_size.width(), previous_string_size.width());
1277 EXPECT_GT(string_size.height(), 0);
1278 previous_string_size = string_size;
1282 // TODO(asvitkine): This test fails because PlatformFontMac uses point font
1283 // sizes instead of pixel sizes like other implementations.
1284 #if !defined(OS_MACOSX)
1285 TEST_F(RenderTextTest, StringSizeEmptyString) {
1286 // Ascent and descent of Arial and Symbol are different on most platforms.
1287 const FontList font_list("Arial,Symbol, 16px");
1288 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1289 render_text->SetFontList(font_list);
1290 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight()));
1292 // The empty string respects FontList metrics for non-zero height
1293 // and baseline.
1294 render_text->SetText(base::string16());
1295 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1296 EXPECT_EQ(0, render_text->GetStringSize().width());
1297 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1299 render_text->SetText(UTF8ToUTF16(" "));
1300 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1301 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1303 #endif // !defined(OS_MACOSX)
1305 TEST_F(RenderTextTest, StringSizeRespectsFontListMetrics) {
1306 // Check that Arial and Symbol have different font metrics.
1307 Font arial_font("Arial", 16);
1308 ASSERT_EQ("arial",
1309 StringToLowerASCII(arial_font.GetActualFontNameForTesting()));
1310 Font symbol_font("Symbol", 16);
1311 ASSERT_EQ("symbol",
1312 StringToLowerASCII(symbol_font.GetActualFontNameForTesting()));
1313 EXPECT_NE(arial_font.GetHeight(), symbol_font.GetHeight());
1314 EXPECT_NE(arial_font.GetBaseline(), symbol_font.GetBaseline());
1315 // "a" should be rendered with Arial, not with Symbol.
1316 const char* arial_font_text = "a";
1317 // "®" (registered trademark symbol) should be rendered with Symbol,
1318 // not with Arial.
1319 const char* symbol_font_text = "\xC2\xAE";
1321 Font smaller_font = arial_font;
1322 Font larger_font = symbol_font;
1323 const char* smaller_font_text = arial_font_text;
1324 const char* larger_font_text = symbol_font_text;
1325 if (symbol_font.GetHeight() < arial_font.GetHeight() &&
1326 symbol_font.GetBaseline() < arial_font.GetBaseline()) {
1327 std::swap(smaller_font, larger_font);
1328 std::swap(smaller_font_text, larger_font_text);
1330 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight());
1331 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline());
1333 // Check |smaller_font_text| is rendered with the smaller font.
1334 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1335 render_text->SetText(UTF8ToUTF16(smaller_font_text));
1336 render_text->SetFontList(FontList(smaller_font));
1337 render_text->SetDisplayRect(Rect(0, 0, 0,
1338 render_text->font_list().GetHeight()));
1339 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height());
1340 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline());
1342 // Layout the same text with mixed fonts. The text should be rendered with
1343 // the smaller font, but the height and baseline are determined with the
1344 // metrics of the font list, which is equal to the larger font.
1345 std::vector<Font> fonts;
1346 fonts.push_back(smaller_font); // The primary font is the smaller font.
1347 fonts.push_back(larger_font);
1348 const FontList font_list(fonts);
1349 render_text->SetFontList(font_list);
1350 render_text->SetDisplayRect(Rect(0, 0, 0,
1351 render_text->font_list().GetHeight()));
1352 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height());
1353 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline());
1354 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1355 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1358 TEST_F(RenderTextTest, SetFontList) {
1359 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1360 render_text->SetFontList(FontList("Arial,Symbol, 13px"));
1361 const std::vector<Font>& fonts = render_text->font_list().GetFonts();
1362 ASSERT_EQ(2U, fonts.size());
1363 EXPECT_EQ("Arial", fonts[0].GetFontName());
1364 EXPECT_EQ("Symbol", fonts[1].GetFontName());
1365 EXPECT_EQ(13, render_text->font_list().GetFontSize());
1368 TEST_F(RenderTextTest, StringSizeBoldWidth) {
1369 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1370 render_text->SetText(UTF8ToUTF16("Hello World"));
1372 const int plain_width = render_text->GetStringSize().width();
1373 EXPECT_GT(plain_width, 0);
1375 // Apply a bold style and check that the new width is greater.
1376 render_text->SetStyle(BOLD, true);
1377 const int bold_width = render_text->GetStringSize().width();
1378 EXPECT_GT(bold_width, plain_width);
1380 // Now, apply a plain style over the first word only.
1381 render_text->ApplyStyle(BOLD, false, Range(0, 5));
1382 const int plain_bold_width = render_text->GetStringSize().width();
1383 EXPECT_GT(plain_bold_width, plain_width);
1384 EXPECT_LT(plain_bold_width, bold_width);
1387 TEST_F(RenderTextTest, StringSizeHeight) {
1388 base::string16 cases[] = {
1389 WideToUTF16(L"Hello World!"), // English
1390 WideToUTF16(L"\x6328\x62f6"), // Japanese
1391 WideToUTF16(L"\x0915\x093f"), // Hindi
1392 WideToUTF16(L"\x05e0\x05b8"), // Hebrew
1395 const FontList default_font_list;
1396 const FontList& larger_font_list = default_font_list.DeriveWithSizeDelta(24);
1397 EXPECT_GT(larger_font_list.GetHeight(), default_font_list.GetHeight());
1399 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
1400 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1401 render_text->SetFontList(default_font_list);
1402 render_text->SetText(cases[i]);
1404 const int height1 = render_text->GetStringSize().height();
1405 EXPECT_GT(height1, 0);
1407 // Check that setting the larger font increases the height.
1408 render_text->SetFontList(larger_font_list);
1409 const int height2 = render_text->GetStringSize().height();
1410 EXPECT_GT(height2, height1);
1414 TEST_F(RenderTextTest, GetBaselineSanity) {
1415 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1416 render_text->SetText(UTF8ToUTF16("Hello World"));
1417 const int baseline = render_text->GetBaseline();
1418 EXPECT_GT(baseline, 0);
1421 TEST_F(RenderTextTest, CursorBoundsInReplacementMode) {
1422 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1423 render_text->SetText(ASCIIToUTF16("abcdefg"));
1424 render_text->SetDisplayRect(Rect(100, 17));
1425 SelectionModel sel_b(1, CURSOR_FORWARD);
1426 SelectionModel sel_c(2, CURSOR_FORWARD);
1427 Rect cursor_around_b = render_text->GetCursorBounds(sel_b, false);
1428 Rect cursor_before_b = render_text->GetCursorBounds(sel_b, true);
1429 Rect cursor_before_c = render_text->GetCursorBounds(sel_c, true);
1430 EXPECT_EQ(cursor_around_b.x(), cursor_before_b.x());
1431 EXPECT_EQ(cursor_around_b.right(), cursor_before_c.x());
1434 TEST_F(RenderTextTest, GetTextOffset) {
1435 // The default horizontal text offset differs for LTR and RTL, and is only set
1436 // when the RenderText object is created. This test will check the default in
1437 // LTR mode, and the next test will check the RTL default.
1438 const bool was_rtl = base::i18n::IsRTL();
1439 SetRTL(false);
1440 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1441 render_text->SetText(ASCIIToUTF16("abcdefg"));
1442 render_text->SetFontList(FontList("Arial, 13px"));
1444 // Set display area's size equal to the font size.
1445 const Size font_size(render_text->GetContentWidth(),
1446 render_text->font_list().GetHeight());
1447 Rect display_rect(font_size);
1448 render_text->SetDisplayRect(display_rect);
1450 Vector2d offset = render_text->GetLineOffset(0);
1451 EXPECT_TRUE(offset.IsZero());
1453 const int kEnlargementX = 2;
1454 display_rect.Inset(0, 0, -kEnlargementX, 0);
1455 render_text->SetDisplayRect(display_rect);
1457 // Check the default horizontal alignment.
1458 offset = render_text->GetLineOffset(0);
1459 EXPECT_EQ(0, offset.x());
1461 // Check explicitly setting the horizontal alignment.
1462 render_text->SetHorizontalAlignment(ALIGN_LEFT);
1463 offset = render_text->GetLineOffset(0);
1464 EXPECT_EQ(0, offset.x());
1465 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1466 offset = render_text->GetLineOffset(0);
1467 EXPECT_EQ(kEnlargementX / 2, offset.x());
1468 render_text->SetHorizontalAlignment(ALIGN_RIGHT);
1469 offset = render_text->GetLineOffset(0);
1470 EXPECT_EQ(kEnlargementX, offset.x());
1472 // Check that text is vertically centered within taller display rects.
1473 const int kEnlargementY = display_rect.height();
1474 display_rect.Inset(0, 0, 0, -kEnlargementY);
1475 render_text->SetDisplayRect(display_rect);
1476 const Vector2d prev_offset = render_text->GetLineOffset(0);
1477 display_rect.Inset(0, 0, 0, -2 * kEnlargementY);
1478 render_text->SetDisplayRect(display_rect);
1479 offset = render_text->GetLineOffset(0);
1480 EXPECT_EQ(prev_offset.y() + kEnlargementY, offset.y());
1482 SetRTL(was_rtl);
1485 TEST_F(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL) {
1486 // This only checks the default horizontal alignment in RTL mode; all other
1487 // GetLineOffset(0) attributes are checked by the test above.
1488 const bool was_rtl = base::i18n::IsRTL();
1489 SetRTL(true);
1490 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1491 render_text->SetText(ASCIIToUTF16("abcdefg"));
1492 render_text->SetFontList(FontList("Arial, 13px"));
1493 const int kEnlargement = 2;
1494 const Size font_size(render_text->GetContentWidth() + kEnlargement,
1495 render_text->GetStringSize().height());
1496 Rect display_rect(font_size);
1497 render_text->SetDisplayRect(display_rect);
1498 Vector2d offset = render_text->GetLineOffset(0);
1499 EXPECT_EQ(kEnlargement, offset.x());
1500 SetRTL(was_rtl);
1503 TEST_F(RenderTextTest, SetDisplayOffset) {
1504 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1505 render_text->SetText(ASCIIToUTF16("abcdefg"));
1506 render_text->SetFontList(FontList("Arial, 13px"));
1508 const Size font_size(render_text->GetContentWidth(),
1509 render_text->font_list().GetHeight());
1510 const int kEnlargement = 10;
1512 // Set display width |kEnlargement| pixels greater than content width and test
1513 // different possible situations. In this case the only possible display
1514 // offset is zero.
1515 Rect display_rect(font_size);
1516 display_rect.Inset(0, 0, -kEnlargement, 0);
1517 render_text->SetDisplayRect(display_rect);
1519 struct {
1520 HorizontalAlignment alignment;
1521 int offset;
1522 } small_content_cases[] = {
1523 { ALIGN_LEFT, -kEnlargement },
1524 { ALIGN_LEFT, 0 },
1525 { ALIGN_LEFT, kEnlargement },
1526 { ALIGN_RIGHT, -kEnlargement },
1527 { ALIGN_RIGHT, 0 },
1528 { ALIGN_RIGHT, kEnlargement },
1529 { ALIGN_CENTER, -kEnlargement },
1530 { ALIGN_CENTER, 0 },
1531 { ALIGN_CENTER, kEnlargement },
1534 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(small_content_cases); i++) {
1535 render_text->SetHorizontalAlignment(small_content_cases[i].alignment);
1536 render_text->SetDisplayOffset(small_content_cases[i].offset);
1537 EXPECT_EQ(0, render_text->GetUpdatedDisplayOffset().x());
1540 // Set display width |kEnlargement| pixels less than content width and test
1541 // different possible situations.
1542 display_rect = Rect(font_size);
1543 display_rect.Inset(0, 0, kEnlargement, 0);
1544 render_text->SetDisplayRect(display_rect);
1546 struct {
1547 HorizontalAlignment alignment;
1548 int offset;
1549 int expected_offset;
1550 } large_content_cases[] = {
1551 // When text is left-aligned, display offset can be in range
1552 // [-kEnlargement, 0].
1553 { ALIGN_LEFT, -2 * kEnlargement, -kEnlargement },
1554 { ALIGN_LEFT, -kEnlargement / 2, -kEnlargement / 2 },
1555 { ALIGN_LEFT, kEnlargement, 0 },
1556 // When text is right-aligned, display offset can be in range
1557 // [0, kEnlargement].
1558 { ALIGN_RIGHT, -kEnlargement, 0 },
1559 { ALIGN_RIGHT, kEnlargement / 2, kEnlargement / 2 },
1560 { ALIGN_RIGHT, 2 * kEnlargement, kEnlargement },
1561 // When text is center-aligned, display offset can be in range
1562 // [-kEnlargement / 2, kEnlargement / 2].
1563 { ALIGN_CENTER, -kEnlargement, -kEnlargement / 2 },
1564 { ALIGN_CENTER, -kEnlargement / 4, -kEnlargement / 4 },
1565 { ALIGN_CENTER, kEnlargement / 4, kEnlargement / 4 },
1566 { ALIGN_CENTER, kEnlargement, kEnlargement / 2 },
1569 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(large_content_cases); i++) {
1570 render_text->SetHorizontalAlignment(large_content_cases[i].alignment);
1571 render_text->SetDisplayOffset(large_content_cases[i].offset);
1572 EXPECT_EQ(large_content_cases[i].expected_offset,
1573 render_text->GetUpdatedDisplayOffset().x());
1577 TEST_F(RenderTextTest, SameFontForParentheses) {
1578 struct {
1579 const base::char16 left_char;
1580 const base::char16 right_char;
1581 } punctuation_pairs[] = {
1582 { '(', ')' },
1583 { '{', '}' },
1584 { '<', '>' },
1586 struct {
1587 base::string16 text;
1588 } cases[] = {
1589 // English(English)
1590 { WideToUTF16(L"Hello World(a)") },
1591 // English(English)English
1592 { WideToUTF16(L"Hello World(a)Hello World") },
1594 // Japanese(English)
1595 { WideToUTF16(L"\x6328\x62f6(a)") },
1596 // Japanese(English)Japanese
1597 { WideToUTF16(L"\x6328\x62f6(a)\x6328\x62f6") },
1598 // English(Japanese)English
1599 { WideToUTF16(L"Hello World(\x6328\x62f6)Hello World") },
1601 // Hindi(English)
1602 { WideToUTF16(L"\x0915\x093f(a)") },
1603 // Hindi(English)Hindi
1604 { WideToUTF16(L"\x0915\x093f(a)\x0915\x093f") },
1605 // English(Hindi)English
1606 { WideToUTF16(L"Hello World(\x0915\x093f)Hello World") },
1608 // Hebrew(English)
1609 { WideToUTF16(L"\x05e0\x05b8(a)") },
1610 // Hebrew(English)Hebrew
1611 { WideToUTF16(L"\x05e0\x05b8(a)\x05e0\x05b8") },
1612 // English(Hebrew)English
1613 { WideToUTF16(L"Hello World(\x05e0\x05b8)Hello World") },
1616 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1617 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
1618 base::string16 text = cases[i].text;
1619 const size_t start_paren_char_index = text.find('(');
1620 ASSERT_NE(base::string16::npos, start_paren_char_index);
1621 const size_t end_paren_char_index = text.find(')');
1622 ASSERT_NE(base::string16::npos, end_paren_char_index);
1624 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(punctuation_pairs); ++j) {
1625 text[start_paren_char_index] = punctuation_pairs[j].left_char;
1626 text[end_paren_char_index] = punctuation_pairs[j].right_char;
1627 render_text->SetText(text);
1629 const std::vector<RenderText::FontSpan> spans =
1630 render_text->GetFontSpansForTesting();
1632 int start_paren_span_index = -1;
1633 int end_paren_span_index = -1;
1634 for (size_t k = 0; k < spans.size(); ++k) {
1635 if (IndexInRange(spans[k].second, start_paren_char_index))
1636 start_paren_span_index = k;
1637 if (IndexInRange(spans[k].second, end_paren_char_index))
1638 end_paren_span_index = k;
1640 ASSERT_NE(-1, start_paren_span_index);
1641 ASSERT_NE(-1, end_paren_span_index);
1643 const Font& start_font = spans[start_paren_span_index].first;
1644 const Font& end_font = spans[end_paren_span_index].first;
1645 EXPECT_EQ(start_font.GetFontName(), end_font.GetFontName());
1646 EXPECT_EQ(start_font.GetFontSize(), end_font.GetFontSize());
1647 EXPECT_EQ(start_font.GetStyle(), end_font.GetStyle());
1652 // Make sure the caret width is always >=1 so that the correct
1653 // caret is drawn at high DPI. crbug.com/164100.
1654 TEST_F(RenderTextTest, CaretWidth) {
1655 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1656 render_text->SetText(ASCIIToUTF16("abcdefg"));
1657 EXPECT_GE(render_text->GetUpdatedCursorBounds().width(), 1);
1660 TEST_F(RenderTextTest, SelectWord) {
1661 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1662 render_text->SetText(ASCIIToUTF16(" foo a.bc.d bar"));
1664 struct {
1665 size_t cursor;
1666 size_t selection_start;
1667 size_t selection_end;
1668 } cases[] = {
1669 { 0, 0, 1 },
1670 { 1, 1, 4 },
1671 { 2, 1, 4 },
1672 { 3, 1, 4 },
1673 { 4, 4, 6 },
1674 { 5, 4, 6 },
1675 { 6, 6, 7 },
1676 { 7, 7, 8 },
1677 { 8, 8, 10 },
1678 { 9, 8, 10 },
1679 { 10, 10, 11 },
1680 { 11, 11, 12 },
1681 { 12, 12, 13 },
1682 { 13, 13, 16 },
1683 { 14, 13, 16 },
1684 { 15, 13, 16 },
1685 { 16, 13, 16 },
1688 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
1689 render_text->SetCursorPosition(cases[i].cursor);
1690 render_text->SelectWord();
1691 EXPECT_EQ(Range(cases[i].selection_start, cases[i].selection_end),
1692 render_text->selection());
1696 // Make sure the last word is selected when the cursor is at text.length().
1697 TEST_F(RenderTextTest, LastWordSelected) {
1698 const std::string kTestURL1 = "http://www.google.com";
1699 const std::string kTestURL2 = "http://www.google.com/something/";
1701 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1703 render_text->SetText(ASCIIToUTF16(kTestURL1));
1704 render_text->SetCursorPosition(kTestURL1.length());
1705 render_text->SelectWord();
1706 EXPECT_EQ(ASCIIToUTF16("com"), GetSelectedText(render_text.get()));
1707 EXPECT_FALSE(render_text->selection().is_reversed());
1709 render_text->SetText(ASCIIToUTF16(kTestURL2));
1710 render_text->SetCursorPosition(kTestURL2.length());
1711 render_text->SelectWord();
1712 EXPECT_EQ(ASCIIToUTF16("/"), GetSelectedText(render_text.get()));
1713 EXPECT_FALSE(render_text->selection().is_reversed());
1716 // When given a non-empty selection, SelectWord should expand the selection to
1717 // nearest word boundaries.
1718 TEST_F(RenderTextTest, SelectMultipleWords) {
1719 const std::string kTestURL = "http://www.google.com";
1721 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1723 render_text->SetText(ASCIIToUTF16(kTestURL));
1724 render_text->SelectRange(Range(16, 20));
1725 render_text->SelectWord();
1726 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1727 EXPECT_FALSE(render_text->selection().is_reversed());
1729 // SelectWord should preserve the selection direction.
1730 render_text->SelectRange(Range(20, 16));
1731 render_text->SelectWord();
1732 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1733 EXPECT_TRUE(render_text->selection().is_reversed());
1736 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
1737 // does not implement this yet. http://crbug.com/131618
1738 #if !defined(OS_MACOSX)
1739 TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) {
1740 ASSERT_FALSE(base::i18n::IsRTL());
1741 ASSERT_FALSE(base::i18n::ICUIsRTL());
1743 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1744 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1745 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1746 CURSOR_FORWARD));
1747 int width = render_text->GetStringSize().width();
1748 ASSERT_GT(width, 10);
1750 // Ensure that the cursor is placed at the width of its preceding text.
1751 render_text->SetDisplayRect(Rect(width + 10, 1));
1752 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1754 // Ensure that shrinking the display rectangle keeps the cursor in view.
1755 render_text->SetDisplayRect(Rect(width - 10, 1));
1756 EXPECT_EQ(render_text->display_rect().width(),
1757 render_text->GetUpdatedCursorBounds().right());
1759 // Ensure that the text will pan to fill its expanding display rectangle.
1760 render_text->SetDisplayRect(Rect(width - 5, 1));
1761 EXPECT_EQ(render_text->display_rect().width(),
1762 render_text->GetUpdatedCursorBounds().right());
1764 // Ensure that a sufficiently large display rectangle shows all the text.
1765 render_text->SetDisplayRect(Rect(width + 10, 1));
1766 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1768 // Repeat the test with RTL text.
1769 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1770 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1771 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1772 width = render_text->GetStringSize().width();
1773 ASSERT_GT(width, 10);
1775 // Ensure that the cursor is placed at the width of its preceding text.
1776 render_text->SetDisplayRect(Rect(width + 10, 1));
1777 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1779 // Ensure that shrinking the display rectangle keeps the cursor in view.
1780 render_text->SetDisplayRect(Rect(width - 10, 1));
1781 EXPECT_EQ(render_text->display_rect().width(),
1782 render_text->GetUpdatedCursorBounds().right());
1784 // Ensure that the text will pan to fill its expanding display rectangle.
1785 render_text->SetDisplayRect(Rect(width - 5, 1));
1786 EXPECT_EQ(render_text->display_rect().width(),
1787 render_text->GetUpdatedCursorBounds().right());
1789 // Ensure that a sufficiently large display rectangle shows all the text.
1790 render_text->SetDisplayRect(Rect(width + 10, 1));
1791 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1794 TEST_F(RenderTextTest, DisplayRectShowsCursorRTL) {
1795 // Set the application default text direction to RTL.
1796 const bool was_rtl = base::i18n::IsRTL();
1797 SetRTL(true);
1799 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1800 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1801 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1802 int width = render_text->GetStringSize().width();
1803 ASSERT_GT(width, 10);
1805 // Ensure that the cursor is placed at the width of its preceding text.
1806 render_text->SetDisplayRect(Rect(width + 10, 1));
1807 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1808 render_text->GetUpdatedCursorBounds().x());
1810 // Ensure that shrinking the display rectangle keeps the cursor in view.
1811 render_text->SetDisplayRect(Rect(width - 10, 1));
1812 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1814 // Ensure that the text will pan to fill its expanding display rectangle.
1815 render_text->SetDisplayRect(Rect(width - 5, 1));
1816 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1818 // Ensure that a sufficiently large display rectangle shows all the text.
1819 render_text->SetDisplayRect(Rect(width + 10, 1));
1820 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1821 render_text->GetUpdatedCursorBounds().x());
1823 // Repeat the test with RTL text.
1824 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1825 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1826 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1827 CURSOR_FORWARD));
1828 width = render_text->GetStringSize().width();
1829 ASSERT_GT(width, 10);
1831 // Ensure that the cursor is placed at the width of its preceding text.
1832 render_text->SetDisplayRect(Rect(width + 10, 1));
1833 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1834 render_text->GetUpdatedCursorBounds().x());
1836 // Ensure that shrinking the display rectangle keeps the cursor in view.
1837 render_text->SetDisplayRect(Rect(width - 10, 1));
1838 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1840 // Ensure that the text will pan to fill its expanding display rectangle.
1841 render_text->SetDisplayRect(Rect(width - 5, 1));
1842 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1844 // Ensure that a sufficiently large display rectangle shows all the 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 // Reset the application default text direction to LTR.
1850 SetRTL(was_rtl);
1851 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1853 #endif // !defined(OS_MACOSX)
1855 // Changing colors between or inside ligated glyphs should not break shaping.
1856 TEST_F(RenderTextTest, SelectionKeepsLigatures) {
1857 const wchar_t* kTestStrings[] = { L"\x644\x623", L"\x633\x627" };
1858 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1859 render_text->set_selection_color(SK_ColorRED);
1860 Canvas canvas;
1862 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1863 render_text->SetText(WideToUTF16(kTestStrings[i]));
1864 const int expected_width = render_text->GetStringSize().width();
1865 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD));
1866 EXPECT_EQ(expected_width, render_text->GetStringSize().width());
1867 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119
1868 render_text->Draw(&canvas);
1869 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1873 #if defined(OS_WIN)
1874 // Ensure strings wrap onto multiple lines for a small available width.
1875 TEST_F(RenderTextTest, Multiline_MinWidth) {
1876 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl,
1877 kRtlLtr, kRtlLtrRtl };
1879 scoped_ptr<RenderTextWin> render_text(
1880 static_cast<RenderTextWin*>(RenderText::CreateInstance()));
1881 render_text->SetDisplayRect(Rect(1, 1000));
1882 render_text->SetMultiline(true);
1883 Canvas canvas;
1885 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1886 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1887 render_text->SetText(WideToUTF16(kTestStrings[i]));
1888 render_text->Draw(&canvas);
1889 EXPECT_GT(render_text->lines_.size(), 1U);
1893 // Ensure strings wrap onto multiple lines for a normal available width.
1894 TEST_F(RenderTextTest, Multiline_NormalWidth) {
1895 const struct {
1896 const wchar_t* const text;
1897 const Range first_line_char_range;
1898 const Range second_line_char_range;
1899 } kTestStrings[] = {
1900 { L"abc defg hijkl", Range(0, 9), Range(9, 14) },
1901 { L"qwertyzxcvbn", Range(0, 8), Range(8, 12) },
1902 { L"\x062A\x0641\x0627\x062D\x05EA\x05E4\x05D5\x05D6\x05D9\x05DD",
1903 Range(4, 10), Range(0, 4) }
1906 scoped_ptr<RenderTextWin> render_text(
1907 static_cast<RenderTextWin*>(RenderText::CreateInstance()));
1908 render_text->SetDisplayRect(Rect(50, 1000));
1909 render_text->SetMultiline(true);
1910 Canvas canvas;
1912 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestStrings); ++i) {
1913 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1914 render_text->SetText(WideToUTF16(kTestStrings[i].text));
1915 render_text->Draw(&canvas);
1916 ASSERT_EQ(2U, render_text->lines_.size());
1917 ASSERT_EQ(1U, render_text->lines_[0].segments.size());
1918 EXPECT_EQ(kTestStrings[i].first_line_char_range,
1919 render_text->lines_[0].segments[0].char_range);
1920 ASSERT_EQ(1U, render_text->lines_[1].segments.size());
1921 EXPECT_EQ(kTestStrings[i].second_line_char_range,
1922 render_text->lines_[1].segments[0].char_range);
1926 // Ensure strings don't wrap onto multiple lines for a sufficient available
1927 // width.
1928 TEST_F(RenderTextTest, Multiline_SufficientWidth) {
1929 const wchar_t* kTestStrings[] = { L"", L" ", L".", L" . ", L"abc", L"a b c",
1930 L"\x62E\x628\x632", L"\x62E \x628 \x632" };
1932 scoped_ptr<RenderTextWin> render_text(
1933 static_cast<RenderTextWin*>(RenderText::CreateInstance()));
1934 render_text->SetDisplayRect(Rect(30, 1000));
1935 render_text->SetMultiline(true);
1936 Canvas canvas;
1938 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1939 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1940 render_text->SetText(WideToUTF16(kTestStrings[i]));
1941 render_text->Draw(&canvas);
1942 EXPECT_EQ(1U, render_text->lines_.size());
1946 TEST_F(RenderTextTest, Multiline_Newline) {
1947 const struct {
1948 const wchar_t* const text;
1949 // Ranges of the characters on each line preceding the newline.
1950 const Range first_line_char_range;
1951 const Range second_line_char_range;
1952 } kTestStrings[] = {
1953 { L"abc\ndef", Range(0, 3), Range(4, 7) },
1954 { L"a \n b ", Range(0, 2), Range(3, 6) },
1955 { L"\n" , Range::InvalidRange(), Range::InvalidRange() }
1958 scoped_ptr<RenderTextWin> render_text(
1959 static_cast<RenderTextWin*>(RenderText::CreateInstance()));
1960 render_text->SetDisplayRect(Rect(200, 1000));
1961 render_text->SetMultiline(true);
1962 Canvas canvas;
1964 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestStrings); ++i) {
1965 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1966 render_text->SetText(WideToUTF16(kTestStrings[i].text));
1967 render_text->Draw(&canvas);
1969 ASSERT_EQ(2U, render_text->lines_.size());
1971 const Range first_expected_range = kTestStrings[i].first_line_char_range;
1972 ASSERT_EQ(first_expected_range.IsValid() ? 2U : 1U,
1973 render_text->lines_[0].segments.size());
1974 if (first_expected_range.IsValid())
1975 EXPECT_EQ(first_expected_range,
1976 render_text->lines_[0].segments[0].char_range);
1978 const internal::LineSegment& newline_segment =
1979 render_text->lines_[0].segments[first_expected_range.IsValid() ? 1 : 0];
1980 ASSERT_EQ(1U, newline_segment.char_range.length());
1981 EXPECT_EQ(L'\n', kTestStrings[i].text[newline_segment.char_range.start()]);
1983 const Range second_expected_range = kTestStrings[i].second_line_char_range;
1984 ASSERT_EQ(second_expected_range.IsValid() ? 1U : 0U,
1985 render_text->lines_[1].segments.size());
1986 if (second_expected_range.IsValid())
1987 EXPECT_EQ(second_expected_range,
1988 render_text->lines_[1].segments[0].char_range);
1992 TEST_F(RenderTextTest, Win_BreakRunsByUnicodeBlocks) {
1993 scoped_ptr<RenderTextWin> render_text(
1994 static_cast<RenderTextWin*>(RenderText::CreateInstance()));
1996 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
1997 render_text->SetText(WideToUTF16(L"x\x25B6y"));
1998 render_text->EnsureLayout();
1999 ASSERT_EQ(3U, render_text->runs_.size());
2000 EXPECT_EQ(Range(0, 1), render_text->runs_[0]->range);
2001 EXPECT_EQ(Range(1, 2), render_text->runs_[1]->range);
2002 EXPECT_EQ(Range(2, 3), render_text->runs_[2]->range);
2004 render_text->SetText(WideToUTF16(L"x \x25B6 y"));
2005 render_text->EnsureLayout();
2006 ASSERT_EQ(3U, render_text->runs_.size());
2007 EXPECT_EQ(Range(0, 2), render_text->runs_[0]->range);
2008 EXPECT_EQ(Range(2, 3), render_text->runs_[1]->range);
2009 EXPECT_EQ(Range(3, 5), render_text->runs_[2]->range);
2011 #endif // defined(OS_WIN)
2013 TEST_F(RenderTextTest, HarfBuzz_CharToGlyph) {
2014 struct {
2015 uint32 glyph_to_char[4];
2016 size_t char_to_glyph_expected[4];
2017 Range char_range_to_glyph_range_expected[4];
2018 bool is_rtl;
2019 } cases[] = {
2020 { // From string "A B C D" to glyphs "a b c d".
2021 { 0, 1, 2, 3 },
2022 { 0, 1, 2, 3 },
2023 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2024 false
2026 { // From string "A B C D" to glyphs "d b c a".
2027 { 3, 2, 1, 0 },
2028 { 3, 2, 1, 0 },
2029 { Range(3, 4), Range(2, 3), Range(1, 2), Range(0, 1) },
2030 true
2032 { // From string "A B C D" to glyphs "ab c c d".
2033 { 0, 2, 2, 3 },
2034 { 0, 0, 1, 3 },
2035 { Range(0, 1), Range(0, 1), Range(1, 3), Range(3, 4) },
2036 false
2038 { // From string "A B C D" to glyphs "d c c ba".
2039 { 3, 2, 2, 0 },
2040 { 3, 3, 1, 0 },
2041 { Range(3, 4), Range(3, 4), Range(1, 3), Range(0, 1) },
2042 true
2046 internal::TextRunHarfBuzz run;
2047 run.range = Range(0, 4);
2048 run.glyph_count = 4;
2049 run.glyph_to_char.reset(new uint32[4]);
2051 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
2052 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 4,
2053 run.glyph_to_char.get());
2054 run.is_rtl = cases[i].is_rtl;
2055 for (size_t j = 0; j < 4; ++j) {
2056 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2057 EXPECT_EQ(cases[i].char_to_glyph_expected[j], run.CharToGlyph(j));
2058 EXPECT_EQ(cases[i].char_range_to_glyph_range_expected[j],
2059 run.CharRangeToGlyphRange(Range(j, j + 1)));
2064 TEST_F(RenderTextTest, HarfBuzz_RunDirection) {
2065 RenderTextHarfBuzz render_text;
2066 const base::string16 mixed =
2067 WideToUTF16(L"\x05D0\x05D1" L"1234" L"\x05D2\x05D3");
2068 render_text.SetText(mixed);
2069 render_text.EnsureLayout();
2070 ASSERT_EQ(3U, render_text.runs_.size());
2071 EXPECT_TRUE(render_text.runs_[0]->is_rtl);
2072 EXPECT_FALSE(render_text.runs_[1]->is_rtl);
2073 EXPECT_TRUE(render_text.runs_[2]->is_rtl);
2076 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks) {
2077 RenderTextHarfBuzz render_text;
2079 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2080 render_text.SetText(WideToUTF16(L"x\x25B6y"));
2081 render_text.EnsureLayout();
2082 ASSERT_EQ(3U, render_text.runs_.size());
2083 EXPECT_EQ(Range(0, 1), render_text.runs_[0]->range);
2084 EXPECT_EQ(Range(1, 2), render_text.runs_[1]->range);
2085 EXPECT_EQ(Range(2, 3), render_text.runs_[2]->range);
2087 render_text.SetText(WideToUTF16(L"x \x25B6 y"));
2088 render_text.EnsureLayout();
2089 ASSERT_EQ(3U, render_text.runs_.size());
2090 EXPECT_EQ(Range(0, 2), render_text.runs_[0]->range);
2091 EXPECT_EQ(Range(2, 3), render_text.runs_[1]->range);
2092 EXPECT_EQ(Range(3, 5), render_text.runs_[2]->range);
2095 // Disabled on Mac because RenderTextMac doesn't implement GetGlyphBounds.
2096 #if !defined(OS_MACOSX)
2097 TEST_F(RenderTextTest, GlyphBounds) {
2098 const wchar_t* kTestStrings[] = {
2099 L"asdf 1234 qwer", L"\x0647\x0654", L"\x0645\x0631\x062D\x0628\x0627"
2101 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2103 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2104 render_text->SetText(WideToUTF16(kTestStrings[i]));
2105 render_text->EnsureLayout();
2107 for (size_t j = 0; j < render_text->text().length(); ++j)
2108 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2111 #endif
2113 // Remove this after making RTHB default in favor of RenderTextTest.GlyphBounds.
2114 TEST_F(RenderTextTest, HarfBuzz_GlyphBounds) {
2115 const wchar_t* kTestStrings[] = {
2116 L"asdf 1234 qwer", L"\x0647\x0654", L"\x0645\x0631\x062D\x0628\x0627"
2118 scoped_ptr<RenderText> render_text(new RenderTextHarfBuzz);
2120 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2121 render_text->SetText(WideToUTF16(kTestStrings[i]));
2122 render_text->EnsureLayout();
2124 for (size_t j = 0; j < render_text->text().length(); ++j)
2125 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2129 } // namespace gfx