Removed unused icon resources from app_list.
[chromium-blink-merge.git] / ui / gfx / render_text_unittest.cc
blob859689b5febce093495cea438fc1b3a3dd0d523d
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/i18n/break_iterator.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/gfx/break_list.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/font.h"
19 #include "ui/gfx/render_text_harfbuzz.h"
21 #if defined(OS_WIN)
22 #include "base/win/windows_version.h"
23 #include "ui/gfx/platform_font_win.h"
24 #include "ui/gfx/render_text_win.h"
25 #endif
27 #if defined(OS_LINUX) && !defined(USE_OZONE)
28 #include "ui/gfx/render_text_pango.h"
29 #endif
31 using base::ASCIIToUTF16;
32 using base::UTF8ToUTF16;
33 using base::WideToUTF16;
34 using base::WideToUTF8;
36 namespace gfx {
38 namespace {
40 // Various weak, LTR, RTL, and Bidi string cases with three characters each.
41 const wchar_t kWeak[] = L" . ";
42 const wchar_t kLtr[] = L"abc";
43 const wchar_t kRtl[] = L"\x5d0\x5d1\x5d2";
44 #if !defined(OS_MACOSX)
45 const wchar_t kLtrRtl[] = L"a" L"\x5d0\x5d1";
46 const wchar_t kLtrRtlLtr[] = L"a" L"\x5d1" L"b";
47 const wchar_t kRtlLtr[] = L"\x5d0\x5d1" L"a";
48 const wchar_t kRtlLtrRtl[] = L"\x5d0" L"a" L"\x5d1";
49 #endif
51 // Checks whether |range| contains |index|. This is not the same as calling
52 // range.Contains(Range(index)), which returns true if |index| == |range.end()|.
53 bool IndexInRange(const Range& range, size_t index) {
54 return index >= range.start() && index < range.end();
57 base::string16 GetSelectedText(RenderText* render_text) {
58 return render_text->text().substr(render_text->selection().GetMin(),
59 render_text->selection().length());
62 // A test utility function to set the application default text direction.
63 void SetRTL(bool rtl) {
64 // Override the current locale/direction.
65 base::i18n::SetICUDefaultLocale(rtl ? "he" : "en");
66 EXPECT_EQ(rtl, base::i18n::IsRTL());
69 #if !defined(OS_MACOSX)
70 // Ensure cursor movement in the specified |direction| yields |expected| values.
71 void RunMoveCursorLeftRightTest(RenderText* render_text,
72 const std::vector<SelectionModel>& expected,
73 VisualCursorDirection direction) {
74 for (size_t i = 0; i < expected.size(); ++i) {
75 SCOPED_TRACE(base::StringPrintf("Going %s; expected value index %d.",
76 direction == CURSOR_LEFT ? "left" : "right", static_cast<int>(i)));
77 EXPECT_EQ(expected[i], render_text->selection_model());
78 render_text->MoveCursor(CHARACTER_BREAK, direction, false);
80 // Check that cursoring is clamped at the line edge.
81 EXPECT_EQ(expected.back(), render_text->selection_model());
82 // Check that it is the line edge.
83 render_text->MoveCursor(LINE_BREAK, direction, false);
84 EXPECT_EQ(expected.back(), render_text->selection_model());
86 #endif // !defined(OS_MACOSX)
88 } // namespace
90 class RenderTextTest : public testing::Test {
93 TEST_F(RenderTextTest, DefaultStyle) {
94 // Check the default styles applied to new instances and adjusted text.
95 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
96 EXPECT_TRUE(render_text->text().empty());
97 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
98 for (size_t i = 0; i < arraysize(cases); ++i) {
99 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLACK));
100 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
101 EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false));
102 render_text->SetText(WideToUTF16(cases[i]));
106 TEST_F(RenderTextTest, SetColorAndStyle) {
107 // Ensure custom default styles persist across setting and clearing text.
108 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
109 const SkColor color = SK_ColorRED;
110 render_text->SetColor(color);
111 render_text->SetStyle(BOLD, true);
112 render_text->SetStyle(UNDERLINE, false);
113 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
114 for (size_t i = 0; i < arraysize(cases); ++i) {
115 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color));
116 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true));
117 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false));
118 render_text->SetText(WideToUTF16(cases[i]));
120 // Ensure custom default styles can be applied after text has been set.
121 if (i == 1)
122 render_text->SetStyle(STRIKE, true);
123 if (i >= 1)
124 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true));
128 TEST_F(RenderTextTest, ApplyColorAndStyle) {
129 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
130 render_text->SetText(ASCIIToUTF16("012345678"));
132 // Apply a ranged color and style and check the resulting breaks.
133 render_text->ApplyColor(SK_ColorRED, Range(1, 4));
134 render_text->ApplyStyle(BOLD, true, Range(2, 5));
135 std::vector<std::pair<size_t, SkColor> > expected_color;
136 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK));
137 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED));
138 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK));
139 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
140 std::vector<std::pair<size_t, bool> > expected_style;
141 expected_style.push_back(std::pair<size_t, bool>(0, false));
142 expected_style.push_back(std::pair<size_t, bool>(2, true));
143 expected_style.push_back(std::pair<size_t, bool>(5, false));
144 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style));
146 // Ensure setting a color and style overrides the ranged colors and styles.
147 render_text->SetColor(SK_ColorBLUE);
148 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE));
149 render_text->SetStyle(BOLD, false);
150 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false));
152 // Apply a color and style over the text end and check the resulting breaks.
153 // (INT_MAX should be used instead of the text length for the range end)
154 const size_t text_length = render_text->text().length();
155 render_text->ApplyColor(SK_ColorRED, Range(0, text_length));
156 render_text->ApplyStyle(BOLD, true, Range(2, text_length));
157 std::vector<std::pair<size_t, SkColor> > expected_color_end;
158 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED));
159 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end));
160 std::vector<std::pair<size_t, bool> > expected_style_end;
161 expected_style_end.push_back(std::pair<size_t, bool>(0, false));
162 expected_style_end.push_back(std::pair<size_t, bool>(2, true));
163 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end));
165 // Ensure ranged values adjust to accommodate text length changes.
166 render_text->ApplyStyle(ITALIC, true, Range(0, 2));
167 render_text->ApplyStyle(ITALIC, true, Range(3, 6));
168 render_text->ApplyStyle(ITALIC, true, Range(7, text_length));
169 std::vector<std::pair<size_t, bool> > expected_italic;
170 expected_italic.push_back(std::pair<size_t, bool>(0, true));
171 expected_italic.push_back(std::pair<size_t, bool>(2, false));
172 expected_italic.push_back(std::pair<size_t, bool>(3, true));
173 expected_italic.push_back(std::pair<size_t, bool>(6, false));
174 expected_italic.push_back(std::pair<size_t, bool>(7, true));
175 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
177 // Changing the text should clear any breaks except for the first one.
178 render_text->SetText(ASCIIToUTF16("0123456"));
179 expected_italic.resize(1);
180 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
181 render_text->ApplyStyle(ITALIC, false, Range(2, 4));
182 render_text->SetText(ASCIIToUTF16("012345678"));
183 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
184 render_text->ApplyStyle(ITALIC, false, Range(0, 1));
185 render_text->SetText(ASCIIToUTF16("0123456"));
186 expected_italic.begin()->second = false;
187 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
188 render_text->ApplyStyle(ITALIC, true, Range(2, 4));
189 render_text->SetText(ASCIIToUTF16("012345678"));
190 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
192 // TODO(tmoniuszko): Enable when RenderTextMac::IsValidCursorIndex is
193 // implemented.
194 #if !defined(OS_MACOSX)
195 // Styles shouldn't be changed mid-grapheme.
196 render_text->SetText(WideToUTF16(
197 L"0" L"\x0915\x093f" L"1" L"\x0915\x093f" L"2"));
198 render_text->ApplyStyle(UNDERLINE, true, Range(2, 5));
199 std::vector<std::pair<size_t, bool> > expected_underline;
200 expected_underline.push_back(std::pair<size_t, bool>(0, false));
201 expected_underline.push_back(std::pair<size_t, bool>(1, true));
202 expected_underline.push_back(std::pair<size_t, bool>(6, false));
203 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsForTesting(
204 expected_underline));
205 #endif // OS_MACOSX
208 #if defined(OS_LINUX) && !defined(USE_OZONE)
209 TEST_F(RenderTextTest, PangoAttributes) {
210 scoped_ptr<RenderText> render_text(RenderText::CreateNativeInstance());
211 render_text->SetText(ASCIIToUTF16("012345678"));
213 // Apply ranged BOLD/ITALIC styles and check the resulting Pango attributes.
214 render_text->ApplyStyle(BOLD, true, Range(2, 4));
215 render_text->ApplyStyle(ITALIC, true, Range(1, 3));
217 struct {
218 int start;
219 int end;
220 bool bold;
221 bool italic;
222 } cases[] = {
223 { 0, 1, false, false },
224 { 1, 2, false, true },
225 { 2, 3, true, true },
226 { 3, 4, true, false },
227 { 4, INT_MAX, false, false },
230 int start = 0, end = 0;
231 RenderTextPango* rt_linux = static_cast<RenderTextPango*>(render_text.get());
232 rt_linux->EnsureLayout();
233 PangoAttrList* attributes = pango_layout_get_attributes(rt_linux->layout_);
234 PangoAttrIterator* iter = pango_attr_list_get_iterator(attributes);
235 for (size_t i = 0; i < arraysize(cases); ++i) {
236 pango_attr_iterator_range(iter, &start, &end);
237 EXPECT_EQ(cases[i].start, start);
238 EXPECT_EQ(cases[i].end, end);
239 PangoFontDescription* font = pango_font_description_new();
240 pango_attr_iterator_get_font(iter, font, NULL, NULL);
241 char* description_string = pango_font_description_to_string(font);
242 const base::string16 desc = ASCIIToUTF16(description_string);
243 const bool bold = desc.find(ASCIIToUTF16("Bold")) != std::string::npos;
244 EXPECT_EQ(cases[i].bold, bold);
245 const bool italic = desc.find(ASCIIToUTF16("Italic")) != std::string::npos;
246 EXPECT_EQ(cases[i].italic, italic);
247 pango_attr_iterator_next(iter);
248 pango_font_description_free(font);
249 g_free(description_string);
251 EXPECT_FALSE(pango_attr_iterator_next(iter));
252 pango_attr_iterator_destroy(iter);
254 #endif
256 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
257 // does not implement this yet. http://crbug.com/131618
258 #if !defined(OS_MACOSX)
259 void TestVisualCursorMotionInObscuredField(RenderText* render_text,
260 const base::string16& text,
261 bool select) {
262 ASSERT_TRUE(render_text->obscured());
263 render_text->SetText(text);
264 int len = text.length();
265 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, select);
266 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
267 render_text->selection_model());
268 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, select);
269 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
270 for (int j = 1; j <= len; ++j) {
271 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, select);
272 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_BACKWARD),
273 render_text->selection_model());
275 for (int j = len - 1; j >= 0; --j) {
276 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, select);
277 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_FORWARD),
278 render_text->selection_model());
280 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, select);
281 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
282 render_text->selection_model());
283 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, select);
284 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
287 TEST_F(RenderTextTest, ObscuredText) {
288 const base::string16 seuss = ASCIIToUTF16("hop on pop");
289 const base::string16 no_seuss = ASCIIToUTF16("**********");
290 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
292 // GetLayoutText() returns asterisks when the obscured bit is set.
293 render_text->SetText(seuss);
294 render_text->SetObscured(true);
295 EXPECT_EQ(seuss, render_text->text());
296 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
297 render_text->SetObscured(false);
298 EXPECT_EQ(seuss, render_text->text());
299 EXPECT_EQ(seuss, render_text->GetLayoutText());
301 render_text->SetObscured(true);
303 // Surrogate pairs are counted as one code point.
304 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 0};
305 render_text->SetText(invalid_surrogates);
306 EXPECT_EQ(ASCIIToUTF16("**"), render_text->GetLayoutText());
307 const base::char16 valid_surrogates[] = {0xD800, 0xDC00, 0};
308 render_text->SetText(valid_surrogates);
309 EXPECT_EQ(ASCIIToUTF16("*"), render_text->GetLayoutText());
310 EXPECT_EQ(0U, render_text->cursor_position());
311 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
312 EXPECT_EQ(2U, render_text->cursor_position());
314 // Test index conversion and cursor validity with a valid surrogate pair.
315 EXPECT_EQ(0U, render_text->TextIndexToLayoutIndex(0U));
316 EXPECT_EQ(1U, render_text->TextIndexToLayoutIndex(1U));
317 EXPECT_EQ(1U, render_text->TextIndexToLayoutIndex(2U));
318 EXPECT_EQ(0U, render_text->LayoutIndexToTextIndex(0U));
319 EXPECT_EQ(2U, render_text->LayoutIndexToTextIndex(1U));
320 EXPECT_TRUE(render_text->IsValidCursorIndex(0U));
321 EXPECT_FALSE(render_text->IsValidCursorIndex(1U));
322 EXPECT_TRUE(render_text->IsValidCursorIndex(2U));
324 // FindCursorPosition() should not return positions between a surrogate pair.
325 render_text->SetDisplayRect(Rect(0, 0, 20, 20));
326 EXPECT_EQ(render_text->FindCursorPosition(Point(0, 0)).caret_pos(), 0U);
327 EXPECT_EQ(render_text->FindCursorPosition(Point(20, 0)).caret_pos(), 2U);
328 for (int x = -1; x <= 20; ++x) {
329 SelectionModel selection = render_text->FindCursorPosition(Point(x, 0));
330 EXPECT_TRUE(selection.caret_pos() == 0U || selection.caret_pos() == 2U);
333 // GetGlyphBounds() should yield the entire string bounds for text index 0.
334 EXPECT_EQ(render_text->GetStringSize().width(),
335 static_cast<int>(render_text->GetGlyphBounds(0U).length()));
337 // Cursoring is independent of underlying characters when text is obscured.
338 const wchar_t* const texts[] = {
339 kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl,
340 L"hop on pop", // Check LTR word boundaries.
341 L"\x05d0\x05d1 \x05d0\x05d2 \x05d1\x05d2", // Check RTL word boundaries.
343 for (size_t i = 0; i < arraysize(texts); ++i) {
344 base::string16 text = WideToUTF16(texts[i]);
345 TestVisualCursorMotionInObscuredField(render_text.get(), text, false);
346 TestVisualCursorMotionInObscuredField(render_text.get(), text, true);
350 TEST_F(RenderTextTest, RevealObscuredText) {
351 const base::string16 seuss = ASCIIToUTF16("hop on pop");
352 const base::string16 no_seuss = ASCIIToUTF16("**********");
353 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
355 render_text->SetText(seuss);
356 render_text->SetObscured(true);
357 EXPECT_EQ(seuss, render_text->text());
358 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
360 // Valid reveal index and new revealed index clears previous one.
361 render_text->RenderText::SetObscuredRevealIndex(0);
362 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetLayoutText());
363 render_text->RenderText::SetObscuredRevealIndex(1);
364 EXPECT_EQ(ASCIIToUTF16("*o********"), render_text->GetLayoutText());
365 render_text->RenderText::SetObscuredRevealIndex(2);
366 EXPECT_EQ(ASCIIToUTF16("**p*******"), render_text->GetLayoutText());
368 // Invalid reveal index.
369 render_text->RenderText::SetObscuredRevealIndex(-1);
370 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
371 render_text->RenderText::SetObscuredRevealIndex(seuss.length() + 1);
372 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
374 // SetObscured clears the revealed index.
375 render_text->RenderText::SetObscuredRevealIndex(0);
376 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetLayoutText());
377 render_text->SetObscured(false);
378 EXPECT_EQ(seuss, render_text->GetLayoutText());
379 render_text->SetObscured(true);
380 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
382 // SetText clears the revealed index.
383 render_text->SetText(ASCIIToUTF16("new"));
384 EXPECT_EQ(ASCIIToUTF16("***"), render_text->GetLayoutText());
385 render_text->RenderText::SetObscuredRevealIndex(2);
386 EXPECT_EQ(ASCIIToUTF16("**w"), render_text->GetLayoutText());
387 render_text->SetText(ASCIIToUTF16("new longer"));
388 EXPECT_EQ(ASCIIToUTF16("**********"), render_text->GetLayoutText());
390 // Text with invalid surrogates.
391 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 'h', 'o', 'p', 0};
392 render_text->SetText(invalid_surrogates);
393 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetLayoutText());
394 render_text->RenderText::SetObscuredRevealIndex(0);
395 const base::char16 invalid_expect_0[] = {0xDC00, '*', '*', '*', '*', 0};
396 EXPECT_EQ(invalid_expect_0, render_text->GetLayoutText());
397 render_text->RenderText::SetObscuredRevealIndex(1);
398 const base::char16 invalid_expect_1[] = {'*', 0xD800, '*', '*', '*', 0};
399 EXPECT_EQ(invalid_expect_1, render_text->GetLayoutText());
400 render_text->RenderText::SetObscuredRevealIndex(2);
401 EXPECT_EQ(ASCIIToUTF16("**h**"), render_text->GetLayoutText());
403 // Text with valid surrogates before and after the reveal index.
404 const base::char16 valid_surrogates[] =
405 {0xD800, 0xDC00, 'h', 'o', 'p', 0xD800, 0xDC00, 0};
406 render_text->SetText(valid_surrogates);
407 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetLayoutText());
408 render_text->RenderText::SetObscuredRevealIndex(0);
409 const base::char16 valid_expect_0_and_1[] =
410 {0xD800, 0xDC00, '*', '*', '*', '*', 0};
411 EXPECT_EQ(valid_expect_0_and_1, render_text->GetLayoutText());
412 render_text->RenderText::SetObscuredRevealIndex(1);
413 EXPECT_EQ(valid_expect_0_and_1, render_text->GetLayoutText());
414 render_text->RenderText::SetObscuredRevealIndex(2);
415 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text->GetLayoutText());
416 render_text->RenderText::SetObscuredRevealIndex(5);
417 const base::char16 valid_expect_5_and_6[] =
418 {'*', '*', '*', '*', 0xD800, 0xDC00, 0};
419 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText());
420 render_text->RenderText::SetObscuredRevealIndex(6);
421 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText());
424 TEST_F(RenderTextTest, ElidedText) {
425 // TODO(skanuj) : Add more test cases for following
426 // - RenderText styles.
427 // - Cross interaction of truncate, elide and obscure.
428 // - ElideText tests from text_elider.cc.
429 struct {
430 const wchar_t* text;
431 const wchar_t* layout_text;
432 const bool elision_expected;
433 } cases[] = {
434 // Strings shorter than the elision width should be laid out in full.
435 { L"", L"" , false },
436 { L"M", L"" , false },
437 { L" . ", L" . " , false },
438 { kWeak, kWeak , false },
439 { kLtr, kLtr , false },
440 { kLtrRtl, kLtrRtl , false },
441 { kLtrRtlLtr, kLtrRtlLtr, false },
442 { kRtl, kRtl , false },
443 { kRtlLtr, kRtlLtr , false },
444 { kRtlLtrRtl, kRtlLtrRtl, false },
445 // Strings as long as the elision width should be laid out in full.
446 { L"012ab", L"012ab" , false },
447 // Long strings should be elided with an ellipsis appended at the end.
448 { L"012abc", L"012a\x2026", true },
449 { L"012ab" L"\x5d0\x5d1", L"012a\x2026", true },
450 { L"012a" L"\x5d1" L"b", L"012a\x2026", true },
451 // No RLM marker added as digits (012) have weak directionality.
452 { L"01" L"\x5d0\x5d1\x5d2", L"01\x5d0\x5d1\x2026", true },
453 // RLM marker added as "ab" have strong LTR directionality.
454 { L"ab" L"\x5d0\x5d1\x5d2", L"ab\x5d0\x5d1\x2026\x200f", true },
455 // Complex script is not handled. In this example, the "\x0915\x093f" is a
456 // compound glyph, but only half of it is elided.
457 { L"0123\x0915\x093f", L"0123\x0915\x2026", true },
458 // Surrogate pairs should be elided reasonably enough.
459 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8", false },
460 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x2026" , true },
461 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x2026" , true },
462 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x2026\x200E" , true },
463 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" , true },
466 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
467 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
468 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
470 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
471 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
472 render_text->SetElideBehavior(ELIDE_TAIL);
474 for (size_t i = 0; i < arraysize(cases); i++) {
475 // Compute expected width
476 expected_render_text->SetText(WideToUTF16(cases[i].layout_text));
477 int expected_width = expected_render_text->GetContentWidth();
479 base::string16 input = WideToUTF16(cases[i].text);
480 // Extend the input text to ensure that it is wider than the layout_text,
481 // and so it will get elided.
482 if (cases[i].elision_expected)
483 input.append(WideToUTF16(L" MMMMMMMMMMM"));
485 render_text->SetText(input);
486 render_text->SetDisplayRect(Rect(0, 0, expected_width, 100));
487 EXPECT_EQ(input, render_text->text())
488 << "->For case " << i << ": " << cases[i].text << "\n";
489 EXPECT_EQ(WideToUTF16(cases[i].layout_text), render_text->GetLayoutText())
490 << "->For case " << i << ": " << cases[i].text << "\n";
491 expected_render_text->SetText(base::string16());
495 TEST_F(RenderTextTest, ElidedObscuredText) {
496 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
497 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
498 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
499 expected_render_text->SetText(WideToUTF16(L"**\x2026"));
501 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
502 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
503 render_text->SetElideBehavior(ELIDE_TAIL);
504 render_text->SetDisplayRect(
505 Rect(0, 0, expected_render_text->GetContentWidth(), 100));
506 render_text->SetObscured(true);
507 render_text->SetText(WideToUTF16(L"abcdef"));
508 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
509 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetLayoutText());
512 TEST_F(RenderTextTest, TruncatedText) {
513 struct {
514 const wchar_t* text;
515 const wchar_t* layout_text;
516 } cases[] = {
517 // Strings shorter than the truncation length should be laid out in full.
518 { L"", L"" },
519 { kWeak, kWeak },
520 { kLtr, kLtr },
521 { kLtrRtl, kLtrRtl },
522 { kLtrRtlLtr, kLtrRtlLtr },
523 { kRtl, kRtl },
524 { kRtlLtr, kRtlLtr },
525 { kRtlLtrRtl, kRtlLtrRtl },
526 // Strings as long as the truncation length should be laid out in full.
527 { L"01234", L"01234" },
528 // Long strings should be truncated with an ellipsis appended at the end.
529 { L"012345", L"0123\x2026" },
530 { L"012" L" . ", L"012 \x2026" },
531 { L"012" L"abc", L"012a\x2026" },
532 { L"012" L"a" L"\x5d0\x5d1", L"012a\x2026" },
533 { L"012" L"a" L"\x5d1" L"b", L"012a\x2026" },
534 { L"012" L"\x5d0\x5d1\x5d2", L"012\x5d0\x2026" },
535 { L"012" L"\x5d0\x5d1" L"a", L"012\x5d0\x2026" },
536 { L"012" L"\x5d0" L"a" L"\x5d1", L"012\x5d0\x2026" },
537 // Surrogate pairs should be truncated reasonably enough.
538 { L"0123\x0915\x093f", L"0123\x2026" },
539 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8" },
540 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x05bc\x2026" },
541 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x05e9\x2026" },
542 { L"0123\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
543 { L"01234\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
544 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" },
547 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
548 render_text->set_truncate_length(5);
549 for (size_t i = 0; i < arraysize(cases); i++) {
550 render_text->SetText(WideToUTF16(cases[i].text));
551 EXPECT_EQ(WideToUTF16(cases[i].text), render_text->text());
552 EXPECT_EQ(WideToUTF16(cases[i].layout_text), render_text->GetLayoutText())
553 << "For case " << i << ": " << cases[i].text;
557 TEST_F(RenderTextTest, TruncatedObscuredText) {
558 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
559 render_text->set_truncate_length(3);
560 render_text->SetObscured(true);
561 render_text->SetText(WideToUTF16(L"abcdef"));
562 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
563 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetLayoutText());
566 TEST_F(RenderTextTest, TruncatedCursorMovementLTR) {
567 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
568 render_text->set_truncate_length(2);
569 render_text->SetText(WideToUTF16(L"abcd"));
571 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
572 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
573 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
574 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
575 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
577 std::vector<SelectionModel> expected;
578 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
579 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
580 // The cursor hops over the ellipsis and elided text to the line end.
581 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
582 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
583 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
585 expected.clear();
586 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
587 // The cursor hops over the elided text to preceeding text.
588 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
589 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
590 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
591 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
594 TEST_F(RenderTextTest, TruncatedCursorMovementRTL) {
595 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
596 render_text->set_truncate_length(2);
597 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3"));
599 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
600 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
601 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
602 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
603 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
605 std::vector<SelectionModel> expected;
606 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
607 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
608 // The cursor hops over the ellipsis and elided text to the line end.
609 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
610 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
611 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
613 expected.clear();
614 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
615 // The cursor hops over the elided text to preceeding text.
616 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
617 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
618 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
619 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
622 TEST_F(RenderTextTest, GetTextDirection) {
623 struct {
624 const wchar_t* text;
625 const base::i18n::TextDirection text_direction;
626 } cases[] = {
627 // Blank strings and those with no/weak directionality default to LTR.
628 { L"", base::i18n::LEFT_TO_RIGHT },
629 { kWeak, base::i18n::LEFT_TO_RIGHT },
630 // Strings that begin with strong LTR characters.
631 { kLtr, base::i18n::LEFT_TO_RIGHT },
632 { kLtrRtl, base::i18n::LEFT_TO_RIGHT },
633 { kLtrRtlLtr, base::i18n::LEFT_TO_RIGHT },
634 // Strings that begin with strong RTL characters.
635 { kRtl, base::i18n::RIGHT_TO_LEFT },
636 { kRtlLtr, base::i18n::RIGHT_TO_LEFT },
637 { kRtlLtrRtl, base::i18n::RIGHT_TO_LEFT },
640 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
641 const bool was_rtl = base::i18n::IsRTL();
643 for (size_t i = 0; i < 2; ++i) {
644 // Toggle the application default text direction (to try each direction).
645 SetRTL(!base::i18n::IsRTL());
646 const base::i18n::TextDirection ui_direction = base::i18n::IsRTL() ?
647 base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
649 // Ensure that directionality modes yield the correct text directions.
650 for (size_t j = 0; j < arraysize(cases); j++) {
651 render_text->SetText(WideToUTF16(cases[j].text));
652 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
653 EXPECT_EQ(render_text->GetTextDirection(), cases[j].text_direction);
654 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_UI);
655 EXPECT_EQ(render_text->GetTextDirection(), ui_direction);
656 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_LTR);
657 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::LEFT_TO_RIGHT);
658 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_RTL);
659 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::RIGHT_TO_LEFT);
663 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
665 // Ensure that text changes update the direction for DIRECTIONALITY_FROM_TEXT.
666 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
667 render_text->SetText(WideToUTF16(kLtr));
668 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::LEFT_TO_RIGHT);
669 render_text->SetText(WideToUTF16(kRtl));
670 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::RIGHT_TO_LEFT);
673 TEST_F(RenderTextTest, MoveCursorLeftRightInLtr) {
674 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
676 // Pure LTR.
677 render_text->SetText(ASCIIToUTF16("abc"));
678 // |expected| saves the expected SelectionModel when moving cursor from left
679 // to right.
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(3, CURSOR_FORWARD));
686 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
688 expected.clear();
689 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
690 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
691 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
692 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
693 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
694 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
697 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtl) {
698 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
699 // LTR-RTL
700 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
701 // The last one is the expected END position.
702 std::vector<SelectionModel> expected;
703 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
704 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
705 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
706 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
707 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
708 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
709 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
710 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
711 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
713 expected.clear();
714 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
715 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
716 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
717 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
718 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
719 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
720 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
721 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
722 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
725 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtlLtr) {
726 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
727 // LTR-RTL-LTR.
728 render_text->SetText(WideToUTF16(L"a" L"\x05d1" L"b"));
729 std::vector<SelectionModel> expected;
730 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
731 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
732 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
733 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
734 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
735 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
737 expected.clear();
738 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
739 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
740 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
741 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
742 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
743 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
746 TEST_F(RenderTextTest, MoveCursorLeftRightInRtl) {
747 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
748 // Pure RTL.
749 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2"));
750 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
751 std::vector<SelectionModel> expected;
753 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
754 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
755 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
756 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
757 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
758 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
760 expected.clear();
762 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
763 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
764 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
765 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
766 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
767 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
770 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtr) {
771 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
772 // RTL-LTR
773 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2" L"abc"));
774 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
775 std::vector<SelectionModel> expected;
776 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
777 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
778 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
779 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
780 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
781 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
782 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
783 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
784 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
786 expected.clear();
787 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
788 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
789 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
790 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
791 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
792 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
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 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtrRtl) {
799 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
800 // RTL-LTR-RTL.
801 render_text->SetText(WideToUTF16(L"\x05d0" L"a" L"\x05d1"));
802 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
803 std::vector<SelectionModel> expected;
804 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
805 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
806 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
807 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
808 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
809 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
811 expected.clear();
812 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
813 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
814 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
815 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
816 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
817 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
820 // TODO(xji): temporarily disable in platform Win since the complex script
821 // characters turned into empty square due to font regression. So, not able
822 // to test 2 characters belong to the same grapheme.
823 #if defined(OS_LINUX)
824 TEST_F(RenderTextTest, MoveCursorLeftRight_ComplexScript) {
825 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
827 render_text->SetText(WideToUTF16(L"\x0915\x093f\x0915\x094d\x0915"));
828 EXPECT_EQ(0U, render_text->cursor_position());
829 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
830 EXPECT_EQ(2U, render_text->cursor_position());
831 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
832 EXPECT_EQ(4U, render_text->cursor_position());
833 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
834 EXPECT_EQ(5U, render_text->cursor_position());
835 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
836 EXPECT_EQ(5U, render_text->cursor_position());
838 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
839 EXPECT_EQ(4U, render_text->cursor_position());
840 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
841 EXPECT_EQ(2U, render_text->cursor_position());
842 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
843 EXPECT_EQ(0U, render_text->cursor_position());
844 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
845 EXPECT_EQ(0U, render_text->cursor_position());
847 #endif
849 // TODO(ckocagil): Enable for RenderTextHarfBuzz. http://crbug.com/383265
850 TEST_F(RenderTextTest, MoveCursorLeftRight_MeiryoUILigatures) {
851 scoped_ptr<RenderText> render_text(RenderText::CreateNativeInstance());
852 // Meiryo UI uses single-glyph ligatures for 'ff' and 'ffi', but each letter
853 // (code point) has unique bounds, so mid-glyph cursoring should be possible.
854 render_text->SetFontList(FontList("Meiryo UI, 12px"));
855 render_text->SetText(WideToUTF16(L"ff ffi"));
856 EXPECT_EQ(0U, render_text->cursor_position());
857 for (size_t i = 0; i < render_text->text().length(); ++i) {
858 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
859 EXPECT_EQ(i + 1, render_text->cursor_position());
861 EXPECT_EQ(6U, render_text->cursor_position());
864 TEST_F(RenderTextTest, GraphemePositions) {
865 // LTR 2-character grapheme, LTR abc, LTR 2-character grapheme.
866 const base::string16 kText1 =
867 WideToUTF16(L"\x0915\x093f" L"abc" L"\x0915\x093f");
869 // LTR ab, LTR 2-character grapheme, LTR cd.
870 const base::string16 kText2 = WideToUTF16(L"ab" L"\x0915\x093f" L"cd");
872 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
873 // two characters forming the surrogate pair 0x0001D11E.
874 const std::string kSurrogate = "\xF0\x9D\x84\x9E";
876 // LTR ab, UTF16 surrogate pair, LTR cd.
877 const base::string16 kText3 = UTF8ToUTF16("ab" + kSurrogate + "cd");
879 struct {
880 base::string16 text;
881 size_t index;
882 size_t expected_previous;
883 size_t expected_next;
884 } cases[] = {
885 { base::string16(), 0, 0, 0 },
886 { base::string16(), 1, 0, 0 },
887 { base::string16(), 50, 0, 0 },
888 { kText1, 0, 0, 2 },
889 { kText1, 1, 0, 2 },
890 { kText1, 2, 0, 3 },
891 { kText1, 3, 2, 4 },
892 { kText1, 4, 3, 5 },
893 { kText1, 5, 4, 7 },
894 { kText1, 6, 5, 7 },
895 { kText1, 7, 5, 7 },
896 { kText1, 8, 7, 7 },
897 { kText1, 50, 7, 7 },
898 { kText2, 0, 0, 1 },
899 { kText2, 1, 0, 2 },
900 { kText2, 2, 1, 4 },
901 { kText2, 3, 2, 4 },
902 { kText2, 4, 2, 5 },
903 { kText2, 5, 4, 6 },
904 { kText2, 6, 5, 6 },
905 { kText2, 7, 6, 6 },
906 { kText2, 50, 6, 6 },
907 { kText3, 0, 0, 1 },
908 { kText3, 1, 0, 2 },
909 { kText3, 2, 1, 4 },
910 { kText3, 3, 2, 4 },
911 { kText3, 4, 2, 5 },
912 { kText3, 5, 4, 6 },
913 { kText3, 6, 5, 6 },
914 { kText3, 7, 6, 6 },
915 { kText3, 50, 6, 6 },
918 #if defined(OS_WIN)
919 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
920 if (base::win::GetVersion() < base::win::VERSION_VISTA)
921 return;
922 #endif
924 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
925 for (size_t i = 0; i < arraysize(cases); i++) {
926 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
927 render_text->SetText(cases[i].text);
929 size_t next = render_text->IndexOfAdjacentGrapheme(cases[i].index,
930 CURSOR_FORWARD);
931 EXPECT_EQ(cases[i].expected_next, next);
932 EXPECT_TRUE(render_text->IsValidCursorIndex(next));
934 size_t previous = render_text->IndexOfAdjacentGrapheme(cases[i].index,
935 CURSOR_BACKWARD);
936 EXPECT_EQ(cases[i].expected_previous, previous);
937 EXPECT_TRUE(render_text->IsValidCursorIndex(previous));
941 TEST_F(RenderTextTest, MidGraphemeSelectionBounds) {
942 #if defined(OS_WIN)
943 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
944 if (base::win::GetVersion() < base::win::VERSION_VISTA)
945 return;
946 #endif
948 // Test that selection bounds may be set amid multi-character graphemes.
949 const base::string16 kHindi = WideToUTF16(L"\x0915\x093f");
950 const base::string16 kThai = WideToUTF16(L"\x0e08\x0e33");
951 const base::string16 cases[] = { kHindi, kThai };
953 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
954 for (size_t i = 0; i < arraysize(cases); i++) {
955 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
956 render_text->SetText(cases[i]);
957 EXPECT_TRUE(render_text->IsValidLogicalIndex(1));
958 #if !defined(OS_MACOSX)
959 EXPECT_FALSE(render_text->IsValidCursorIndex(1));
960 #endif
961 EXPECT_TRUE(render_text->SelectRange(Range(2, 1)));
962 EXPECT_EQ(Range(2, 1), render_text->selection());
963 EXPECT_EQ(1U, render_text->cursor_position());
964 // Although selection bounds may be set within a multi-character grapheme,
965 // cursor movement (e.g. via arrow key) should avoid those indices.
966 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
967 EXPECT_EQ(0U, render_text->cursor_position());
968 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
969 EXPECT_EQ(2U, render_text->cursor_position());
973 TEST_F(RenderTextTest, FindCursorPosition) {
974 const wchar_t* kTestStrings[] = { kLtrRtl, kLtrRtlLtr, kRtlLtr, kRtlLtrRtl };
975 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
976 render_text->SetDisplayRect(Rect(0, 0, 100, 20));
977 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
978 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS "]", i));
979 render_text->SetText(WideToUTF16(kTestStrings[i]));
980 for(size_t j = 0; j < render_text->text().length(); ++j) {
981 const Range range(render_text->GetGlyphBounds(j));
982 // Test a point just inside the leading edge of the glyph bounds.
983 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1;
984 EXPECT_EQ(j, render_text->FindCursorPosition(Point(x, 0)).caret_pos());
989 TEST_F(RenderTextTest, EdgeSelectionModels) {
990 // Simple Latin text.
991 const base::string16 kLatin = WideToUTF16(L"abc");
992 // LTR 2-character grapheme.
993 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f");
994 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
995 const base::string16 kHindiLatin =
996 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f");
997 // RTL 2-character grapheme.
998 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8");
999 // RTL 2-character grapheme, LTR a, RTL 2-character grapheme.
1000 const base::string16 kHebrewLatin =
1001 WideToUTF16(L"\x05e0\x05b8" L"a" L"\x05e0\x05b8");
1003 struct {
1004 base::string16 text;
1005 base::i18n::TextDirection expected_text_direction;
1006 } cases[] = {
1007 { base::string16(), base::i18n::LEFT_TO_RIGHT },
1008 { kLatin, base::i18n::LEFT_TO_RIGHT },
1009 { kLTRGrapheme, base::i18n::LEFT_TO_RIGHT },
1010 { kHindiLatin, base::i18n::LEFT_TO_RIGHT },
1011 { kRTLGrapheme, base::i18n::RIGHT_TO_LEFT },
1012 { kHebrewLatin, base::i18n::RIGHT_TO_LEFT },
1015 #if defined(OS_WIN)
1016 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1017 if (base::win::GetVersion() < base::win::VERSION_VISTA)
1018 return;
1019 #endif
1021 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1022 for (size_t i = 0; i < arraysize(cases); i++) {
1023 render_text->SetText(cases[i].text);
1024 bool ltr = (cases[i].expected_text_direction == base::i18n::LEFT_TO_RIGHT);
1026 SelectionModel start_edge =
1027 render_text->EdgeSelectionModel(ltr ? CURSOR_LEFT : CURSOR_RIGHT);
1028 EXPECT_EQ(start_edge, SelectionModel(0, CURSOR_BACKWARD));
1030 SelectionModel end_edge =
1031 render_text->EdgeSelectionModel(ltr ? CURSOR_RIGHT : CURSOR_LEFT);
1032 EXPECT_EQ(end_edge, SelectionModel(cases[i].text.length(), CURSOR_FORWARD));
1036 TEST_F(RenderTextTest, SelectAll) {
1037 const wchar_t* const cases[] =
1038 { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl };
1040 // Ensure that SelectAll respects the |reversed| argument regardless of
1041 // application locale and text content directionality.
1042 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1043 const SelectionModel expected_reversed(Range(3, 0), CURSOR_FORWARD);
1044 const SelectionModel expected_forwards(Range(0, 3), CURSOR_BACKWARD);
1045 const bool was_rtl = base::i18n::IsRTL();
1047 for (size_t i = 0; i < 2; ++i) {
1048 SetRTL(!base::i18n::IsRTL());
1049 // Test that an empty string produces an empty selection model.
1050 render_text->SetText(base::string16());
1051 EXPECT_EQ(render_text->selection_model(), SelectionModel());
1053 // Test the weak, LTR, RTL, and Bidi string cases.
1054 for (size_t j = 0; j < arraysize(cases); j++) {
1055 render_text->SetText(WideToUTF16(cases[j]));
1056 render_text->SelectAll(false);
1057 EXPECT_EQ(render_text->selection_model(), expected_forwards);
1058 render_text->SelectAll(true);
1059 EXPECT_EQ(render_text->selection_model(), expected_reversed);
1063 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1066 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) {
1067 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1068 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
1069 // Left arrow on select ranging (6, 4).
1070 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1071 EXPECT_EQ(Range(6), render_text->selection());
1072 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1073 EXPECT_EQ(Range(4), render_text->selection());
1074 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1075 EXPECT_EQ(Range(5), render_text->selection());
1076 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1077 EXPECT_EQ(Range(6), render_text->selection());
1078 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1079 EXPECT_EQ(Range(6, 5), render_text->selection());
1080 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1081 EXPECT_EQ(Range(6, 4), render_text->selection());
1082 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1083 EXPECT_EQ(Range(6), render_text->selection());
1085 // Right arrow on select ranging (4, 6).
1086 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1087 EXPECT_EQ(Range(0), render_text->selection());
1088 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1089 EXPECT_EQ(Range(1), render_text->selection());
1090 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1091 EXPECT_EQ(Range(2), render_text->selection());
1092 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1093 EXPECT_EQ(Range(3), render_text->selection());
1094 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1095 EXPECT_EQ(Range(5), render_text->selection());
1096 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1097 EXPECT_EQ(Range(4), render_text->selection());
1098 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1099 EXPECT_EQ(Range(4, 5), render_text->selection());
1100 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1101 EXPECT_EQ(Range(4, 6), render_text->selection());
1102 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1103 EXPECT_EQ(Range(4), render_text->selection());
1106 TEST_F(RenderTextTest, CenteredDisplayOffset) {
1107 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1108 render_text->SetText(ASCIIToUTF16("abcdefghij"));
1109 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1111 const int kEnlargement = 10;
1112 const int content_width = render_text->GetContentWidth();
1113 Rect display_rect(0, 0, content_width / 2,
1114 render_text->font_list().GetHeight());
1115 render_text->SetDisplayRect(display_rect);
1117 // Move the cursor to the beginning of the text and, by checking the cursor
1118 // bounds, make sure no empty space is to the left of the text.
1119 render_text->SetCursorPosition(0);
1120 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1122 // Widen the display rect and, by checking the cursor bounds, make sure no
1123 // empty space is introduced to the left of the text.
1124 display_rect.Inset(0, 0, -kEnlargement, 0);
1125 render_text->SetDisplayRect(display_rect);
1126 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1128 // Move the cursor to the end of the text and, by checking the cursor bounds,
1129 // make sure no empty space is to the right of the text.
1130 render_text->SetCursorPosition(render_text->text().length());
1131 EXPECT_EQ(display_rect.right(),
1132 render_text->GetUpdatedCursorBounds().right());
1134 // Widen the display rect and, by checking the cursor bounds, make sure no
1135 // empty space is introduced to the right of the text.
1136 display_rect.Inset(0, 0, -kEnlargement, 0);
1137 render_text->SetDisplayRect(display_rect);
1138 EXPECT_EQ(display_rect.right(),
1139 render_text->GetUpdatedCursorBounds().right());
1141 #endif // !defined(OS_MACOSX)
1143 // TODO(xji): Make these work on Windows.
1144 #if defined(OS_LINUX)
1145 void MoveLeftRightByWordVerifier(RenderText* render_text,
1146 const wchar_t* str) {
1147 render_text->SetText(WideToUTF16(str));
1149 // Test moving by word from left ro right.
1150 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1151 bool first_word = true;
1152 while (true) {
1153 // First, test moving by word from a word break position, such as from
1154 // "|abc def" to "abc| def".
1155 SelectionModel start = render_text->selection_model();
1156 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1157 SelectionModel end = render_text->selection_model();
1158 if (end == start) // reach the end.
1159 break;
1161 // For testing simplicity, each word is a 3-character word.
1162 int num_of_character_moves = first_word ? 3 : 4;
1163 first_word = false;
1164 render_text->MoveCursorTo(start);
1165 for (int j = 0; j < num_of_character_moves; ++j)
1166 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1167 EXPECT_EQ(end, render_text->selection_model());
1169 // Then, test moving by word from positions inside the word, such as from
1170 // "a|bc def" to "abc| def", and from "ab|c def" to "abc| def".
1171 for (int j = 1; j < num_of_character_moves; ++j) {
1172 render_text->MoveCursorTo(start);
1173 for (int k = 0; k < j; ++k)
1174 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1175 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1176 EXPECT_EQ(end, render_text->selection_model());
1180 // Test moving by word from right to left.
1181 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1182 first_word = true;
1183 while (true) {
1184 SelectionModel start = render_text->selection_model();
1185 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1186 SelectionModel end = render_text->selection_model();
1187 if (end == start) // reach the end.
1188 break;
1190 int num_of_character_moves = first_word ? 3 : 4;
1191 first_word = false;
1192 render_text->MoveCursorTo(start);
1193 for (int j = 0; j < num_of_character_moves; ++j)
1194 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1195 EXPECT_EQ(end, render_text->selection_model());
1197 for (int j = 1; j < num_of_character_moves; ++j) {
1198 render_text->MoveCursorTo(start);
1199 for (int k = 0; k < j; ++k)
1200 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1201 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1202 EXPECT_EQ(end, render_text->selection_model());
1207 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText) {
1208 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1210 // For testing simplicity, each word is a 3-character word.
1211 std::vector<const wchar_t*> test;
1212 test.push_back(L"abc");
1213 test.push_back(L"abc def");
1214 test.push_back(L"\x05E1\x05E2\x05E3");
1215 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1216 test.push_back(L"abc \x05E1\x05E2\x05E3");
1217 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1218 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1219 L" \x05E7\x05E8\x05E9");
1221 test.push_back(L"abc \x05E1\x05E2\x05E3 hij");
1222 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 hij opq");
1223 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1224 L" \x05E7\x05E8\x05E9" L" opq rst uvw");
1226 test.push_back(L"\x05E1\x05E2\x05E3 abc");
1227 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 abc def");
1228 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 \x05E7\x05E8\x05E9"
1229 L" abc def hij");
1231 test.push_back(L"\x05D1\x05D2\x05D3 abc \x05E1\x05E2\x05E3");
1232 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 abc def"
1233 L" \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1234 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 \x05D7\x05D8\x05D9"
1235 L" abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1236 L" \x05E7\x05E8\x05E9");
1238 for (size_t i = 0; i < test.size(); ++i)
1239 MoveLeftRightByWordVerifier(render_text.get(), test[i]);
1242 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText_TestEndOfText) {
1243 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1245 render_text->SetText(WideToUTF16(L"ab\x05E1"));
1246 // Moving the cursor by word from "abC|" to the left should return "|abC".
1247 // But since end of text is always treated as a word break, it returns
1248 // position "ab|C".
1249 // TODO(xji): Need to make it work as expected.
1250 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1251 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1252 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1254 // Moving the cursor by word from "|abC" to the right returns "abC|".
1255 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1256 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1257 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1259 render_text->SetText(WideToUTF16(L"\x05E1\x05E2" L"a"));
1260 // For logical text "BCa", moving the cursor by word from "aCB|" to the left
1261 // returns "|aCB".
1262 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1263 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1264 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1266 // Moving the cursor by word from "|aCB" to the right should return "aCB|".
1267 // But since end of text is always treated as a word break, it returns
1268 // position "a|CB".
1269 // TODO(xji): Need to make it work as expected.
1270 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1271 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1272 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1275 TEST_F(RenderTextTest, MoveLeftRightByWordInTextWithMultiSpaces) {
1276 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1277 render_text->SetText(WideToUTF16(L"abc def"));
1278 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1279 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1280 EXPECT_EQ(11U, render_text->cursor_position());
1282 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1283 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1284 EXPECT_EQ(0U, render_text->cursor_position());
1287 TEST_F(RenderTextTest, MoveLeftRightByWordInChineseText) {
1288 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1289 render_text->SetText(WideToUTF16(L"\x6211\x4EEC\x53BB\x516C\x56ED\x73A9"));
1290 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1291 EXPECT_EQ(0U, render_text->cursor_position());
1292 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1293 EXPECT_EQ(2U, render_text->cursor_position());
1294 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1295 EXPECT_EQ(3U, render_text->cursor_position());
1296 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1297 EXPECT_EQ(5U, render_text->cursor_position());
1298 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1299 EXPECT_EQ(6U, render_text->cursor_position());
1300 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1301 EXPECT_EQ(6U, render_text->cursor_position());
1303 #endif
1305 // TODO(ckocagil): Remove when RenderTextWin goes away.
1306 #if defined(OS_WIN)
1307 TEST_F(RenderTextTest, Win_LogicalClusters) {
1308 scoped_ptr<RenderTextWin> render_text(
1309 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
1311 const base::string16 test_string =
1312 WideToUTF16(L"\x0930\x0930\x0930\x0930\x0930");
1313 render_text->SetText(test_string);
1314 render_text->EnsureLayout();
1315 ASSERT_EQ(1U, render_text->runs_.size());
1316 WORD* logical_clusters = render_text->runs_[0]->logical_clusters.get();
1317 for (size_t i = 0; i < test_string.length(); ++i)
1318 EXPECT_EQ(i, logical_clusters[i]);
1320 #endif // defined(OS_WIN)
1322 TEST_F(RenderTextTest, StringSizeSanity) {
1323 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1324 render_text->SetText(UTF8ToUTF16("Hello World"));
1325 const Size string_size = render_text->GetStringSize();
1326 EXPECT_GT(string_size.width(), 0);
1327 EXPECT_GT(string_size.height(), 0);
1330 TEST_F(RenderTextTest, StringSizeLongStrings) {
1331 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1332 Size previous_string_size;
1333 for (size_t length = 10; length < 1000000; length *= 10) {
1334 render_text->SetText(base::string16(length, 'a'));
1335 const Size string_size = render_text->GetStringSize();
1336 EXPECT_GT(string_size.width(), previous_string_size.width());
1337 EXPECT_GT(string_size.height(), 0);
1338 previous_string_size = string_size;
1342 // TODO(asvitkine): This test fails because PlatformFontMac uses point font
1343 // sizes instead of pixel sizes like other implementations.
1344 #if !defined(OS_MACOSX)
1345 TEST_F(RenderTextTest, StringSizeEmptyString) {
1346 // Ascent and descent of Arial and Symbol are different on most platforms.
1347 const FontList font_list("Arial,Symbol, 16px");
1348 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1349 render_text->SetFontList(font_list);
1350 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight()));
1352 // The empty string respects FontList metrics for non-zero height
1353 // and baseline.
1354 render_text->SetText(base::string16());
1355 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1356 EXPECT_EQ(0, render_text->GetStringSize().width());
1357 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1359 render_text->SetText(UTF8ToUTF16(" "));
1360 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1361 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1363 #endif // !defined(OS_MACOSX)
1365 TEST_F(RenderTextTest, StringSizeRespectsFontListMetrics) {
1366 // Check that Arial and Symbol have different font metrics.
1367 Font arial_font("Arial", 16);
1368 ASSERT_EQ("arial",
1369 base::StringToLowerASCII(arial_font.GetActualFontNameForTesting()));
1370 Font symbol_font("Symbol", 16);
1371 ASSERT_EQ("symbol",
1372 base::StringToLowerASCII(
1373 symbol_font.GetActualFontNameForTesting()));
1374 EXPECT_NE(arial_font.GetHeight(), symbol_font.GetHeight());
1375 EXPECT_NE(arial_font.GetBaseline(), symbol_font.GetBaseline());
1376 // "a" should be rendered with Arial, not with Symbol.
1377 const char* arial_font_text = "a";
1378 // "®" (registered trademark symbol) should be rendered with Symbol,
1379 // not with Arial.
1380 const char* symbol_font_text = "\xC2\xAE";
1382 Font smaller_font = arial_font;
1383 Font larger_font = symbol_font;
1384 const char* smaller_font_text = arial_font_text;
1385 const char* larger_font_text = symbol_font_text;
1386 if (symbol_font.GetHeight() < arial_font.GetHeight() &&
1387 symbol_font.GetBaseline() < arial_font.GetBaseline()) {
1388 std::swap(smaller_font, larger_font);
1389 std::swap(smaller_font_text, larger_font_text);
1391 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight());
1392 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline());
1394 // Check |smaller_font_text| is rendered with the smaller font.
1395 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1396 render_text->SetText(UTF8ToUTF16(smaller_font_text));
1397 render_text->SetFontList(FontList(smaller_font));
1398 render_text->SetDisplayRect(Rect(0, 0, 0,
1399 render_text->font_list().GetHeight()));
1400 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height());
1401 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline());
1403 // Layout the same text with mixed fonts. The text should be rendered with
1404 // the smaller font, but the height and baseline are determined with the
1405 // metrics of the font list, which is equal to the larger font.
1406 std::vector<Font> fonts;
1407 fonts.push_back(smaller_font); // The primary font is the smaller font.
1408 fonts.push_back(larger_font);
1409 const FontList font_list(fonts);
1410 render_text->SetFontList(font_list);
1411 render_text->SetDisplayRect(Rect(0, 0, 0,
1412 render_text->font_list().GetHeight()));
1413 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height());
1414 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline());
1415 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1416 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1419 TEST_F(RenderTextTest, SetFontList) {
1420 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1421 render_text->SetFontList(FontList("Arial,Symbol, 13px"));
1422 const std::vector<Font>& fonts = render_text->font_list().GetFonts();
1423 ASSERT_EQ(2U, fonts.size());
1424 EXPECT_EQ("Arial", fonts[0].GetFontName());
1425 EXPECT_EQ("Symbol", fonts[1].GetFontName());
1426 EXPECT_EQ(13, render_text->font_list().GetFontSize());
1429 TEST_F(RenderTextTest, StringSizeBoldWidth) {
1430 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1431 render_text->SetText(UTF8ToUTF16("Hello World"));
1433 const int plain_width = render_text->GetStringSize().width();
1434 EXPECT_GT(plain_width, 0);
1436 // Apply a bold style and check that the new width is greater.
1437 render_text->SetStyle(BOLD, true);
1438 const int bold_width = render_text->GetStringSize().width();
1439 EXPECT_GT(bold_width, plain_width);
1441 // Now, apply a plain style over the first word only.
1442 render_text->ApplyStyle(BOLD, false, Range(0, 5));
1443 const int plain_bold_width = render_text->GetStringSize().width();
1444 EXPECT_GT(plain_bold_width, plain_width);
1445 EXPECT_LT(plain_bold_width, bold_width);
1448 TEST_F(RenderTextTest, StringSizeHeight) {
1449 base::string16 cases[] = {
1450 WideToUTF16(L"Hello World!"), // English
1451 WideToUTF16(L"\x6328\x62f6"), // Japanese
1452 WideToUTF16(L"\x0915\x093f"), // Hindi
1453 WideToUTF16(L"\x05e0\x05b8"), // Hebrew
1456 const FontList default_font_list;
1457 const FontList& larger_font_list = default_font_list.DeriveWithSizeDelta(24);
1458 EXPECT_GT(larger_font_list.GetHeight(), default_font_list.GetHeight());
1460 for (size_t i = 0; i < arraysize(cases); i++) {
1461 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1462 render_text->SetFontList(default_font_list);
1463 render_text->SetText(cases[i]);
1465 const int height1 = render_text->GetStringSize().height();
1466 EXPECT_GT(height1, 0);
1468 // Check that setting the larger font increases the height.
1469 render_text->SetFontList(larger_font_list);
1470 const int height2 = render_text->GetStringSize().height();
1471 EXPECT_GT(height2, height1);
1475 TEST_F(RenderTextTest, GetBaselineSanity) {
1476 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1477 render_text->SetText(UTF8ToUTF16("Hello World"));
1478 const int baseline = render_text->GetBaseline();
1479 EXPECT_GT(baseline, 0);
1482 TEST_F(RenderTextTest, CursorBoundsInReplacementMode) {
1483 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1484 render_text->SetText(ASCIIToUTF16("abcdefg"));
1485 render_text->SetDisplayRect(Rect(100, 17));
1486 SelectionModel sel_b(1, CURSOR_FORWARD);
1487 SelectionModel sel_c(2, CURSOR_FORWARD);
1488 Rect cursor_around_b = render_text->GetCursorBounds(sel_b, false);
1489 Rect cursor_before_b = render_text->GetCursorBounds(sel_b, true);
1490 Rect cursor_before_c = render_text->GetCursorBounds(sel_c, true);
1491 EXPECT_EQ(cursor_around_b.x(), cursor_before_b.x());
1492 EXPECT_EQ(cursor_around_b.right(), cursor_before_c.x());
1495 TEST_F(RenderTextTest, GetTextOffset) {
1496 // The default horizontal text offset differs for LTR and RTL, and is only set
1497 // when the RenderText object is created. This test will check the default in
1498 // LTR mode, and the next test will check the RTL default.
1499 const bool was_rtl = base::i18n::IsRTL();
1500 SetRTL(false);
1501 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1502 render_text->SetText(ASCIIToUTF16("abcdefg"));
1503 render_text->SetFontList(FontList("Arial, 13px"));
1505 // Set display area's size equal to the font size.
1506 const Size font_size(render_text->GetContentWidth(),
1507 render_text->font_list().GetHeight());
1508 Rect display_rect(font_size);
1509 render_text->SetDisplayRect(display_rect);
1511 Vector2d offset = render_text->GetLineOffset(0);
1512 EXPECT_TRUE(offset.IsZero());
1514 const int kEnlargementX = 2;
1515 display_rect.Inset(0, 0, -kEnlargementX, 0);
1516 render_text->SetDisplayRect(display_rect);
1518 // Check the default horizontal alignment.
1519 offset = render_text->GetLineOffset(0);
1520 EXPECT_EQ(0, offset.x());
1522 // Check explicitly setting the horizontal alignment.
1523 render_text->SetHorizontalAlignment(ALIGN_LEFT);
1524 offset = render_text->GetLineOffset(0);
1525 EXPECT_EQ(0, offset.x());
1526 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1527 offset = render_text->GetLineOffset(0);
1528 EXPECT_EQ(kEnlargementX / 2, offset.x());
1529 render_text->SetHorizontalAlignment(ALIGN_RIGHT);
1530 offset = render_text->GetLineOffset(0);
1531 EXPECT_EQ(kEnlargementX, offset.x());
1533 // Check that text is vertically centered within taller display rects.
1534 const int kEnlargementY = display_rect.height();
1535 display_rect.Inset(0, 0, 0, -kEnlargementY);
1536 render_text->SetDisplayRect(display_rect);
1537 const Vector2d prev_offset = render_text->GetLineOffset(0);
1538 display_rect.Inset(0, 0, 0, -2 * kEnlargementY);
1539 render_text->SetDisplayRect(display_rect);
1540 offset = render_text->GetLineOffset(0);
1541 EXPECT_EQ(prev_offset.y() + kEnlargementY, offset.y());
1543 SetRTL(was_rtl);
1546 TEST_F(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL) {
1547 // This only checks the default horizontal alignment in RTL mode; all other
1548 // GetLineOffset(0) attributes are checked by the test above.
1549 const bool was_rtl = base::i18n::IsRTL();
1550 SetRTL(true);
1551 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1552 render_text->SetText(ASCIIToUTF16("abcdefg"));
1553 render_text->SetFontList(FontList("Arial, 13px"));
1554 const int kEnlargement = 2;
1555 const Size font_size(render_text->GetContentWidth() + kEnlargement,
1556 render_text->GetStringSize().height());
1557 Rect display_rect(font_size);
1558 render_text->SetDisplayRect(display_rect);
1559 Vector2d offset = render_text->GetLineOffset(0);
1560 EXPECT_EQ(kEnlargement, offset.x());
1561 SetRTL(was_rtl);
1564 TEST_F(RenderTextTest, SetDisplayOffset) {
1565 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1566 render_text->SetText(ASCIIToUTF16("abcdefg"));
1567 render_text->SetFontList(FontList("Arial, 13px"));
1569 const Size font_size(render_text->GetContentWidth(),
1570 render_text->font_list().GetHeight());
1571 const int kEnlargement = 10;
1573 // Set display width |kEnlargement| pixels greater than content width and test
1574 // different possible situations. In this case the only possible display
1575 // offset is zero.
1576 Rect display_rect(font_size);
1577 display_rect.Inset(0, 0, -kEnlargement, 0);
1578 render_text->SetDisplayRect(display_rect);
1580 struct {
1581 HorizontalAlignment alignment;
1582 int offset;
1583 } small_content_cases[] = {
1584 { ALIGN_LEFT, -kEnlargement },
1585 { ALIGN_LEFT, 0 },
1586 { ALIGN_LEFT, kEnlargement },
1587 { ALIGN_RIGHT, -kEnlargement },
1588 { ALIGN_RIGHT, 0 },
1589 { ALIGN_RIGHT, kEnlargement },
1590 { ALIGN_CENTER, -kEnlargement },
1591 { ALIGN_CENTER, 0 },
1592 { ALIGN_CENTER, kEnlargement },
1595 for (size_t i = 0; i < arraysize(small_content_cases); i++) {
1596 render_text->SetHorizontalAlignment(small_content_cases[i].alignment);
1597 render_text->SetDisplayOffset(small_content_cases[i].offset);
1598 EXPECT_EQ(0, render_text->GetUpdatedDisplayOffset().x());
1601 // Set display width |kEnlargement| pixels less than content width and test
1602 // different possible situations.
1603 display_rect = Rect(font_size);
1604 display_rect.Inset(0, 0, kEnlargement, 0);
1605 render_text->SetDisplayRect(display_rect);
1607 struct {
1608 HorizontalAlignment alignment;
1609 int offset;
1610 int expected_offset;
1611 } large_content_cases[] = {
1612 // When text is left-aligned, display offset can be in range
1613 // [-kEnlargement, 0].
1614 { ALIGN_LEFT, -2 * kEnlargement, -kEnlargement },
1615 { ALIGN_LEFT, -kEnlargement / 2, -kEnlargement / 2 },
1616 { ALIGN_LEFT, kEnlargement, 0 },
1617 // When text is right-aligned, display offset can be in range
1618 // [0, kEnlargement].
1619 { ALIGN_RIGHT, -kEnlargement, 0 },
1620 { ALIGN_RIGHT, kEnlargement / 2, kEnlargement / 2 },
1621 { ALIGN_RIGHT, 2 * kEnlargement, kEnlargement },
1622 // When text is center-aligned, display offset can be in range
1623 // [-kEnlargement / 2 - 1, (kEnlargement - 1) / 2].
1624 { ALIGN_CENTER, -kEnlargement, -kEnlargement / 2 - 1 },
1625 { ALIGN_CENTER, -kEnlargement / 4, -kEnlargement / 4 },
1626 { ALIGN_CENTER, kEnlargement / 4, kEnlargement / 4 },
1627 { ALIGN_CENTER, kEnlargement, (kEnlargement - 1) / 2 },
1630 for (size_t i = 0; i < arraysize(large_content_cases); i++) {
1631 render_text->SetHorizontalAlignment(large_content_cases[i].alignment);
1632 render_text->SetDisplayOffset(large_content_cases[i].offset);
1633 EXPECT_EQ(large_content_cases[i].expected_offset,
1634 render_text->GetUpdatedDisplayOffset().x());
1638 TEST_F(RenderTextTest, SameFontForParentheses) {
1639 struct {
1640 const base::char16 left_char;
1641 const base::char16 right_char;
1642 } punctuation_pairs[] = {
1643 { '(', ')' },
1644 { '{', '}' },
1645 { '<', '>' },
1647 struct {
1648 base::string16 text;
1649 } cases[] = {
1650 // English(English)
1651 { WideToUTF16(L"Hello World(a)") },
1652 // English(English)English
1653 { WideToUTF16(L"Hello World(a)Hello World") },
1655 // Japanese(English)
1656 { WideToUTF16(L"\x6328\x62f6(a)") },
1657 // Japanese(English)Japanese
1658 { WideToUTF16(L"\x6328\x62f6(a)\x6328\x62f6") },
1659 // English(Japanese)English
1660 { WideToUTF16(L"Hello World(\x6328\x62f6)Hello World") },
1662 // Hindi(English)
1663 { WideToUTF16(L"\x0915\x093f(a)") },
1664 // Hindi(English)Hindi
1665 { WideToUTF16(L"\x0915\x093f(a)\x0915\x093f") },
1666 // English(Hindi)English
1667 { WideToUTF16(L"Hello World(\x0915\x093f)Hello World") },
1669 // Hebrew(English)
1670 { WideToUTF16(L"\x05e0\x05b8(a)") },
1671 // Hebrew(English)Hebrew
1672 { WideToUTF16(L"\x05e0\x05b8(a)\x05e0\x05b8") },
1673 // English(Hebrew)English
1674 { WideToUTF16(L"Hello World(\x05e0\x05b8)Hello World") },
1677 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1678 for (size_t i = 0; i < arraysize(cases); ++i) {
1679 base::string16 text = cases[i].text;
1680 const size_t start_paren_char_index = text.find('(');
1681 ASSERT_NE(base::string16::npos, start_paren_char_index);
1682 const size_t end_paren_char_index = text.find(')');
1683 ASSERT_NE(base::string16::npos, end_paren_char_index);
1685 for (size_t j = 0; j < arraysize(punctuation_pairs); ++j) {
1686 text[start_paren_char_index] = punctuation_pairs[j].left_char;
1687 text[end_paren_char_index] = punctuation_pairs[j].right_char;
1688 render_text->SetText(text);
1690 const std::vector<RenderText::FontSpan> spans =
1691 render_text->GetFontSpansForTesting();
1693 int start_paren_span_index = -1;
1694 int end_paren_span_index = -1;
1695 for (size_t k = 0; k < spans.size(); ++k) {
1696 if (IndexInRange(spans[k].second, start_paren_char_index))
1697 start_paren_span_index = k;
1698 if (IndexInRange(spans[k].second, end_paren_char_index))
1699 end_paren_span_index = k;
1701 ASSERT_NE(-1, start_paren_span_index);
1702 ASSERT_NE(-1, end_paren_span_index);
1704 const Font& start_font = spans[start_paren_span_index].first;
1705 const Font& end_font = spans[end_paren_span_index].first;
1706 EXPECT_EQ(start_font.GetFontName(), end_font.GetFontName());
1707 EXPECT_EQ(start_font.GetFontSize(), end_font.GetFontSize());
1708 EXPECT_EQ(start_font.GetStyle(), end_font.GetStyle());
1713 // Make sure the caret width is always >=1 so that the correct
1714 // caret is drawn at high DPI. crbug.com/164100.
1715 TEST_F(RenderTextTest, CaretWidth) {
1716 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1717 render_text->SetText(ASCIIToUTF16("abcdefg"));
1718 EXPECT_GE(render_text->GetUpdatedCursorBounds().width(), 1);
1721 TEST_F(RenderTextTest, SelectWord) {
1722 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1723 render_text->SetText(ASCIIToUTF16(" foo a.bc.d bar"));
1725 struct {
1726 size_t cursor;
1727 size_t selection_start;
1728 size_t selection_end;
1729 } cases[] = {
1730 { 0, 0, 1 },
1731 { 1, 1, 4 },
1732 { 2, 1, 4 },
1733 { 3, 1, 4 },
1734 { 4, 4, 6 },
1735 { 5, 4, 6 },
1736 { 6, 6, 7 },
1737 { 7, 7, 8 },
1738 { 8, 8, 10 },
1739 { 9, 8, 10 },
1740 { 10, 10, 11 },
1741 { 11, 11, 12 },
1742 { 12, 12, 13 },
1743 { 13, 13, 16 },
1744 { 14, 13, 16 },
1745 { 15, 13, 16 },
1746 { 16, 13, 16 },
1749 for (size_t i = 0; i < arraysize(cases); ++i) {
1750 render_text->SetCursorPosition(cases[i].cursor);
1751 render_text->SelectWord();
1752 EXPECT_EQ(Range(cases[i].selection_start, cases[i].selection_end),
1753 render_text->selection());
1757 // Make sure the last word is selected when the cursor is at text.length().
1758 TEST_F(RenderTextTest, LastWordSelected) {
1759 const std::string kTestURL1 = "http://www.google.com";
1760 const std::string kTestURL2 = "http://www.google.com/something/";
1762 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1764 render_text->SetText(ASCIIToUTF16(kTestURL1));
1765 render_text->SetCursorPosition(kTestURL1.length());
1766 render_text->SelectWord();
1767 EXPECT_EQ(ASCIIToUTF16("com"), GetSelectedText(render_text.get()));
1768 EXPECT_FALSE(render_text->selection().is_reversed());
1770 render_text->SetText(ASCIIToUTF16(kTestURL2));
1771 render_text->SetCursorPosition(kTestURL2.length());
1772 render_text->SelectWord();
1773 EXPECT_EQ(ASCIIToUTF16("/"), GetSelectedText(render_text.get()));
1774 EXPECT_FALSE(render_text->selection().is_reversed());
1777 // When given a non-empty selection, SelectWord should expand the selection to
1778 // nearest word boundaries.
1779 TEST_F(RenderTextTest, SelectMultipleWords) {
1780 const std::string kTestURL = "http://www.google.com";
1782 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1784 render_text->SetText(ASCIIToUTF16(kTestURL));
1785 render_text->SelectRange(Range(16, 20));
1786 render_text->SelectWord();
1787 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1788 EXPECT_FALSE(render_text->selection().is_reversed());
1790 // SelectWord should preserve the selection direction.
1791 render_text->SelectRange(Range(20, 16));
1792 render_text->SelectWord();
1793 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1794 EXPECT_TRUE(render_text->selection().is_reversed());
1797 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
1798 // does not implement this yet. http://crbug.com/131618
1799 #if !defined(OS_MACOSX)
1800 TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) {
1801 ASSERT_FALSE(base::i18n::IsRTL());
1802 ASSERT_FALSE(base::i18n::ICUIsRTL());
1804 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1805 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1806 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1807 CURSOR_FORWARD));
1808 int width = render_text->GetStringSize().width();
1809 ASSERT_GT(width, 10);
1811 // Ensure that the cursor is placed at the width of its preceding text.
1812 render_text->SetDisplayRect(Rect(width + 10, 1));
1813 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1815 // Ensure that shrinking the display rectangle keeps the cursor in view.
1816 render_text->SetDisplayRect(Rect(width - 10, 1));
1817 EXPECT_EQ(render_text->display_rect().width(),
1818 render_text->GetUpdatedCursorBounds().right());
1820 // Ensure that the text will pan to fill its expanding display rectangle.
1821 render_text->SetDisplayRect(Rect(width - 5, 1));
1822 EXPECT_EQ(render_text->display_rect().width(),
1823 render_text->GetUpdatedCursorBounds().right());
1825 // Ensure that a sufficiently large display rectangle shows all the text.
1826 render_text->SetDisplayRect(Rect(width + 10, 1));
1827 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1829 // Repeat the test with RTL text.
1830 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1831 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1832 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1833 width = render_text->GetStringSize().width();
1834 ASSERT_GT(width, 10);
1836 // Ensure that the cursor is placed at the width of its preceding text.
1837 render_text->SetDisplayRect(Rect(width + 10, 1));
1838 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1840 // Ensure that shrinking the display rectangle keeps the cursor in view.
1841 render_text->SetDisplayRect(Rect(width - 10, 1));
1842 EXPECT_EQ(render_text->display_rect().width(),
1843 render_text->GetUpdatedCursorBounds().right());
1845 // Ensure that the text will pan to fill its expanding display rectangle.
1846 render_text->SetDisplayRect(Rect(width - 5, 1));
1847 EXPECT_EQ(render_text->display_rect().width(),
1848 render_text->GetUpdatedCursorBounds().right());
1850 // Ensure that a sufficiently large display rectangle shows all the text.
1851 render_text->SetDisplayRect(Rect(width + 10, 1));
1852 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1855 TEST_F(RenderTextTest, DisplayRectShowsCursorRTL) {
1856 // Set the application default text direction to RTL.
1857 const bool was_rtl = base::i18n::IsRTL();
1858 SetRTL(true);
1860 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1861 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1862 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1863 int width = render_text->GetStringSize().width();
1864 ASSERT_GT(width, 10);
1866 // Ensure that the cursor is placed at the width of its preceding text.
1867 render_text->SetDisplayRect(Rect(width + 10, 1));
1868 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1869 render_text->GetUpdatedCursorBounds().x());
1871 // Ensure that shrinking the display rectangle keeps the cursor in view.
1872 render_text->SetDisplayRect(Rect(width - 10, 1));
1873 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1875 // Ensure that the text will pan to fill its expanding display rectangle.
1876 render_text->SetDisplayRect(Rect(width - 5, 1));
1877 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1879 // Ensure that a sufficiently large display rectangle shows all the text.
1880 render_text->SetDisplayRect(Rect(width + 10, 1));
1881 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1882 render_text->GetUpdatedCursorBounds().x());
1884 // Repeat the test with RTL text.
1885 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1886 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1887 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1888 CURSOR_FORWARD));
1889 width = render_text->GetStringSize().width();
1890 ASSERT_GT(width, 10);
1892 // Ensure that the cursor is placed at the width of its preceding text.
1893 render_text->SetDisplayRect(Rect(width + 10, 1));
1894 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1895 render_text->GetUpdatedCursorBounds().x());
1897 // Ensure that shrinking the display rectangle keeps the cursor in view.
1898 render_text->SetDisplayRect(Rect(width - 10, 1));
1899 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1901 // Ensure that the text will pan to fill its expanding display rectangle.
1902 render_text->SetDisplayRect(Rect(width - 5, 1));
1903 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1905 // Ensure that a sufficiently large display rectangle shows all the text.
1906 render_text->SetDisplayRect(Rect(width + 10, 1));
1907 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1908 render_text->GetUpdatedCursorBounds().x());
1910 // Reset the application default text direction to LTR.
1911 SetRTL(was_rtl);
1912 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1914 #endif // !defined(OS_MACOSX)
1916 // Changing colors between or inside ligated glyphs should not break shaping.
1917 TEST_F(RenderTextTest, SelectionKeepsLigatures) {
1918 const wchar_t* kTestStrings[] = { L"\x644\x623", L"\x633\x627" };
1919 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1920 render_text->set_selection_color(SK_ColorRED);
1921 Canvas canvas;
1923 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1924 render_text->SetText(WideToUTF16(kTestStrings[i]));
1925 const int expected_width = render_text->GetStringSize().width();
1926 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD));
1927 EXPECT_EQ(expected_width, render_text->GetStringSize().width());
1928 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119
1929 render_text->Draw(&canvas);
1930 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1934 #if defined(OS_WIN)
1935 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1936 // Ensure strings wrap onto multiple lines for a small available width.
1937 TEST_F(RenderTextTest, Multiline_MinWidth) {
1938 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl,
1939 kRtlLtr, kRtlLtrRtl };
1941 scoped_ptr<RenderTextWin> render_text(
1942 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
1943 render_text->SetDisplayRect(Rect(1, 1000));
1944 render_text->SetMultiline(true);
1945 Canvas canvas;
1947 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1948 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1949 render_text->SetText(WideToUTF16(kTestStrings[i]));
1950 render_text->Draw(&canvas);
1951 EXPECT_GT(render_text->lines_.size(), 1U);
1955 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1956 // Ensure strings wrap onto multiple lines for a normal available width.
1957 TEST_F(RenderTextTest, Multiline_NormalWidth) {
1958 const struct {
1959 const wchar_t* const text;
1960 const Range first_line_char_range;
1961 const Range second_line_char_range;
1962 } kTestStrings[] = {
1963 { L"abc defg hijkl", Range(0, 9), Range(9, 14) },
1964 { L"qwertyzxcvbn", Range(0, 8), Range(8, 12) },
1965 { L"\x062A\x0641\x0627\x062D\x05EA\x05E4\x05D5\x05D6\x05D9\x05DD",
1966 Range(4, 10), Range(0, 4) }
1969 scoped_ptr<RenderTextWin> render_text(
1970 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
1971 render_text->SetDisplayRect(Rect(50, 1000));
1972 render_text->SetMultiline(true);
1973 Canvas canvas;
1975 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1976 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1977 render_text->SetText(WideToUTF16(kTestStrings[i].text));
1978 render_text->Draw(&canvas);
1979 ASSERT_EQ(2U, render_text->lines_.size());
1980 ASSERT_EQ(1U, render_text->lines_[0].segments.size());
1981 EXPECT_EQ(kTestStrings[i].first_line_char_range,
1982 render_text->lines_[0].segments[0].char_range);
1983 ASSERT_EQ(1U, render_text->lines_[1].segments.size());
1984 EXPECT_EQ(kTestStrings[i].second_line_char_range,
1985 render_text->lines_[1].segments[0].char_range);
1989 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1990 // Ensure strings don't wrap onto multiple lines for a sufficient available
1991 // width.
1992 TEST_F(RenderTextTest, Multiline_SufficientWidth) {
1993 const wchar_t* kTestStrings[] = { L"", L" ", L".", L" . ", L"abc", L"a b c",
1994 L"\x62E\x628\x632", L"\x62E \x628 \x632" };
1996 scoped_ptr<RenderTextWin> render_text(
1997 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
1998 render_text->SetDisplayRect(Rect(30, 1000));
1999 render_text->SetMultiline(true);
2000 Canvas canvas;
2002 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2003 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2004 render_text->SetText(WideToUTF16(kTestStrings[i]));
2005 render_text->Draw(&canvas);
2006 EXPECT_EQ(1U, render_text->lines_.size());
2010 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
2011 TEST_F(RenderTextTest, Multiline_Newline) {
2012 const struct {
2013 const wchar_t* const text;
2014 // Ranges of the characters on each line preceding the newline.
2015 const Range first_line_char_range;
2016 const Range second_line_char_range;
2017 } kTestStrings[] = {
2018 { L"abc\ndef", Range(0, 3), Range(4, 7) },
2019 { L"a \n b ", Range(0, 2), Range(3, 6) },
2020 { L"\n" , Range::InvalidRange(), Range::InvalidRange() }
2023 scoped_ptr<RenderTextWin> render_text(
2024 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
2025 render_text->SetDisplayRect(Rect(200, 1000));
2026 render_text->SetMultiline(true);
2027 Canvas canvas;
2029 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2030 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2031 render_text->SetText(WideToUTF16(kTestStrings[i].text));
2032 render_text->Draw(&canvas);
2034 ASSERT_EQ(2U, render_text->lines_.size());
2036 const Range first_expected_range = kTestStrings[i].first_line_char_range;
2037 ASSERT_EQ(first_expected_range.IsValid() ? 2U : 1U,
2038 render_text->lines_[0].segments.size());
2039 if (first_expected_range.IsValid())
2040 EXPECT_EQ(first_expected_range,
2041 render_text->lines_[0].segments[0].char_range);
2043 const internal::LineSegment& newline_segment =
2044 render_text->lines_[0].segments[first_expected_range.IsValid() ? 1 : 0];
2045 ASSERT_EQ(1U, newline_segment.char_range.length());
2046 EXPECT_EQ(L'\n', kTestStrings[i].text[newline_segment.char_range.start()]);
2048 const Range second_expected_range = kTestStrings[i].second_line_char_range;
2049 ASSERT_EQ(second_expected_range.IsValid() ? 1U : 0U,
2050 render_text->lines_[1].segments.size());
2051 if (second_expected_range.IsValid())
2052 EXPECT_EQ(second_expected_range,
2053 render_text->lines_[1].segments[0].char_range);
2057 // TODO(ckocagil): Remove when RenderTextWin goes away.
2058 TEST_F(RenderTextTest, BreakRunsByUnicodeBlocks) {
2059 scoped_ptr<RenderTextWin> render_text(
2060 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
2062 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2063 render_text->SetText(WideToUTF16(L"x\x25B6y"));
2064 render_text->EnsureLayout();
2065 ASSERT_EQ(3U, render_text->runs_.size());
2066 EXPECT_EQ(Range(0, 1), render_text->runs_[0]->range);
2067 EXPECT_EQ(Range(1, 2), render_text->runs_[1]->range);
2068 EXPECT_EQ(Range(2, 3), render_text->runs_[2]->range);
2070 render_text->SetText(WideToUTF16(L"x \x25B6 y"));
2071 render_text->EnsureLayout();
2072 ASSERT_EQ(3U, render_text->runs_.size());
2073 EXPECT_EQ(Range(0, 2), render_text->runs_[0]->range);
2074 EXPECT_EQ(Range(2, 3), render_text->runs_[1]->range);
2075 EXPECT_EQ(Range(3, 5), render_text->runs_[2]->range);
2077 #endif // defined(OS_WIN)
2079 // Test TextRunHarfBuzz's cluster finding logic.
2080 TEST_F(RenderTextTest, HarfBuzz_Clusters) {
2081 struct {
2082 uint32 glyph_to_char[4];
2083 Range chars[4];
2084 Range glyphs[4];
2085 bool is_rtl;
2086 } cases[] = {
2087 { // From string "A B C D" to glyphs "a b c d".
2088 { 0, 1, 2, 3 },
2089 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2090 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2091 false
2093 { // From string "A B C D" to glyphs "d c b a".
2094 { 3, 2, 1, 0 },
2095 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2096 { Range(3, 4), Range(2, 3), Range(1, 2), Range(0, 1) },
2097 true
2099 { // From string "A B C D" to glyphs "ab c c d".
2100 { 0, 2, 2, 3 },
2101 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2102 { Range(0, 1), Range(0, 1), Range(1, 3), Range(3, 4) },
2103 false
2105 { // From string "A B C D" to glyphs "d c c ba".
2106 { 3, 2, 2, 0 },
2107 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2108 { Range(3, 4), Range(3, 4), Range(1, 3), Range(0, 1) },
2109 true
2113 internal::TextRunHarfBuzz run;
2114 run.range = Range(0, 4);
2115 run.glyph_count = 4;
2116 run.glyph_to_char.resize(4);
2118 for (size_t i = 0; i < arraysize(cases); ++i) {
2119 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 4,
2120 run.glyph_to_char.begin());
2121 run.is_rtl = cases[i].is_rtl;
2123 for (size_t j = 0; j < 4; ++j) {
2124 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2125 Range chars;
2126 Range glyphs;
2127 run.GetClusterAt(j, &chars, &glyphs);
2128 EXPECT_EQ(cases[i].chars[j], chars);
2129 EXPECT_EQ(cases[i].glyphs[j], glyphs);
2130 EXPECT_EQ(cases[i].glyphs[j], run.CharRangeToGlyphRange(chars));
2135 // Ensure that graphemes with multiple code points do not get split.
2136 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemeCases) {
2137 const wchar_t* cases[] = {
2138 // "A" with a combining umlaut, followed by a "B".
2139 L"A\x0308" L"B",
2140 // Devanagari biconsonantal conjunct "ki", followed by an "a".
2141 L"\x0915\x093f\x0905",
2142 // Thai consonant and vowel pair "cho chan" + "sara am", followed by Thai
2143 // digit 0.
2144 L"\x0e08\x0e33\x0E50",
2147 RenderTextHarfBuzz render_text;
2149 for (size_t i = 0; i < arraysize(cases); ++i) {
2150 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS, i));
2152 base::string16 text = WideToUTF16(cases[i]);
2153 render_text.SetText(text);
2154 render_text.EnsureLayout();
2155 ASSERT_EQ(1U, render_text.runs_.size());
2156 internal::TextRunHarfBuzz* run = render_text.runs_[0];
2158 base::i18n::BreakIterator* iter = render_text.grapheme_iterator_.get();
2159 Range first_grapheme_bounds = run->GetGraphemeBounds(iter, 0);
2160 EXPECT_EQ(first_grapheme_bounds, run->GetGraphemeBounds(iter, 1));
2161 Range second_grapheme_bounds = run->GetGraphemeBounds(iter, 2);
2162 EXPECT_EQ(first_grapheme_bounds.end(), second_grapheme_bounds.start());
2166 // Test the partition of a multi-grapheme cluster into grapheme ranges.
2167 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemePartition) {
2168 struct {
2169 uint32 glyph_to_char[2];
2170 Range bounds[4];
2171 bool is_rtl;
2172 } cases[] = {
2173 { // From string "A B C D" to glyphs "a bcd".
2174 { 0, 1 },
2175 { Range(0, 10), Range(10, 13), Range(13, 17), Range(17, 20) },
2176 false
2178 { // From string "A B C D" to glyphs "ab cd".
2179 { 0, 2 },
2180 { Range(0, 5), Range(5, 10), Range(10, 15), Range(15, 20) },
2181 false
2183 { // From string "A B C D" to glyphs "dcb a".
2184 { 1, 0 },
2185 { Range(10, 20), Range(7, 10), Range(3, 7), Range(0, 3) },
2186 true
2188 { // From string "A B C D" to glyphs "dc ba".
2189 { 2, 0 },
2190 { Range(15, 20), Range(10, 15), Range(5, 10), Range(0, 5) },
2191 true
2195 internal::TextRunHarfBuzz run;
2196 run.range = Range(0, 4);
2197 run.glyph_count = 2;
2198 run.glyph_to_char.resize(2);
2199 run.positions.reset(new SkPoint[4]);
2200 run.width = 20;
2202 const base::string16 kString = ASCIIToUTF16("abcd");
2203 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2204 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2205 ASSERT_TRUE(iter->Init());
2207 for (size_t i = 0; i < arraysize(cases); ++i) {
2208 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 2,
2209 run.glyph_to_char.begin());
2210 run.is_rtl = cases[i].is_rtl;
2211 for (int j = 0; j < 2; ++j)
2212 run.positions[j].set(j * 10, 0);
2214 for (size_t j = 0; j < 4; ++j) {
2215 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2216 EXPECT_EQ(cases[i].bounds[j], run.GetGraphemeBounds(iter.get(), j));
2221 TEST_F(RenderTextTest, HarfBuzz_RunDirection) {
2222 RenderTextHarfBuzz render_text;
2223 const base::string16 mixed =
2224 WideToUTF16(L"\x05D0\x05D1" L"1234" L"\x05D2\x05D3");
2225 render_text.SetText(mixed);
2226 render_text.EnsureLayout();
2227 ASSERT_EQ(3U, render_text.runs_.size());
2228 EXPECT_TRUE(render_text.runs_[0]->is_rtl);
2229 EXPECT_FALSE(render_text.runs_[1]->is_rtl);
2230 EXPECT_TRUE(render_text.runs_[2]->is_rtl);
2233 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks) {
2234 RenderTextHarfBuzz render_text;
2236 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2237 render_text.SetText(WideToUTF16(L"x\x25B6y"));
2238 render_text.EnsureLayout();
2239 ASSERT_EQ(3U, render_text.runs_.size());
2240 EXPECT_EQ(Range(0, 1), render_text.runs_[0]->range);
2241 EXPECT_EQ(Range(1, 2), render_text.runs_[1]->range);
2242 EXPECT_EQ(Range(2, 3), render_text.runs_[2]->range);
2244 render_text.SetText(WideToUTF16(L"x \x25B6 y"));
2245 render_text.EnsureLayout();
2246 ASSERT_EQ(3U, render_text.runs_.size());
2247 EXPECT_EQ(Range(0, 2), render_text.runs_[0]->range);
2248 EXPECT_EQ(Range(2, 3), render_text.runs_[1]->range);
2249 EXPECT_EQ(Range(3, 5), render_text.runs_[2]->range);
2252 // Disabled on Mac because RenderTextMac doesn't implement GetGlyphBounds.
2253 #if !defined(OS_MACOSX)
2254 TEST_F(RenderTextTest, GlyphBounds) {
2255 const wchar_t* kTestStrings[] = {
2256 L"asdf 1234 qwer", L"\x0647\x0654", L"\x0645\x0631\x062D\x0628\x0627"
2258 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2260 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2261 render_text->SetText(WideToUTF16(kTestStrings[i]));
2262 render_text->EnsureLayout();
2264 for (size_t j = 0; j < render_text->text().length(); ++j)
2265 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2268 #endif
2270 // Remove this after making RTHB default in favor of RenderTextTest.GlyphBounds.
2271 TEST_F(RenderTextTest, HarfBuzz_GlyphBounds) {
2272 const wchar_t* kTestStrings[] = {
2273 L"asdf 1234 qwer", L"\x0647\x0654", L"\x0645\x0631\x062D\x0628\x0627"
2275 scoped_ptr<RenderText> render_text(new RenderTextHarfBuzz);
2277 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2278 render_text->SetText(WideToUTF16(kTestStrings[i]));
2279 render_text->EnsureLayout();
2281 for (size_t j = 0; j < render_text->text().length(); ++j)
2282 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2286 // Ensure that shaping with a non-existent font does not cause a crash.
2287 TEST_F(RenderTextTest, HarfBuzz_NonExistentFont) {
2288 RenderTextHarfBuzz render_text;
2289 render_text.SetText(ASCIIToUTF16("test"));
2290 render_text.EnsureLayout();
2291 ASSERT_EQ(1U, render_text.runs_.size());
2292 internal::TextRunHarfBuzz* run = render_text.runs_[0];
2293 render_text.ShapeRunWithFont(
2294 run, "TheFontThatDoesntExist", FontRenderParams());
2297 // Ensure an empty run returns sane values to queries.
2298 TEST_F(RenderTextTest, HarfBuzz_EmptyRun) {
2299 internal::TextRunHarfBuzz run;
2300 const base::string16 kString = ASCIIToUTF16("abcdefgh");
2301 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2302 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2303 ASSERT_TRUE(iter->Init());
2305 run.range = Range(3, 8);
2306 run.glyph_count = 0;
2307 EXPECT_EQ(Range(0, 0), run.CharRangeToGlyphRange(Range(4, 5)));
2308 EXPECT_EQ(Range(0, 0), run.GetGraphemeBounds(iter.get(), 4));
2309 Range chars;
2310 Range glyphs;
2311 run.GetClusterAt(4, &chars, &glyphs);
2312 EXPECT_EQ(Range(3, 8), chars);
2313 EXPECT_EQ(Range(0, 0), glyphs);
2316 // Ensure a string fits in a display rect with a width equal to the string's.
2317 TEST_F(RenderTextTest, StringFitsOwnWidth) {
2318 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2319 const base::string16 kString = ASCIIToUTF16("www.example.com");
2321 render_text->SetText(kString);
2322 render_text->ApplyStyle(BOLD, true, Range(0, 3));
2323 render_text->SetElideBehavior(ELIDE_TAIL);
2325 render_text->SetDisplayRect(Rect(0, 0, 500, 100));
2326 EXPECT_EQ(kString, render_text->GetLayoutText());
2327 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100));
2328 EXPECT_EQ(kString, render_text->GetLayoutText());
2331 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184
2332 #if !defined(OS_WIN)
2333 // Ensure that RenderText examines all of the fonts in its FontList before
2334 // falling back to other fonts.
2335 TEST_F(RenderTextTest, HarfBuzz_FontListFallback) {
2336 // Double-check that the requested fonts are present.
2337 FontList font_list("Arial, Symbol, 12px");
2338 const std::vector<Font>& fonts = font_list.GetFonts();
2339 ASSERT_EQ(2u, fonts.size());
2340 ASSERT_EQ("arial",
2341 base::StringToLowerASCII(fonts[0].GetActualFontNameForTesting()));
2342 ASSERT_EQ("symbol",
2343 base::StringToLowerASCII(fonts[1].GetActualFontNameForTesting()));
2345 // "⊕" (CIRCLED PLUS) should be rendered with Symbol rather than falling back
2346 // to some other font that's present on the system.
2347 RenderTextHarfBuzz render_text;
2348 render_text.SetFontList(font_list);
2349 render_text.SetText(UTF8ToUTF16("\xE2\x8A\x95"));
2350 const std::vector<RenderText::FontSpan> spans =
2351 render_text.GetFontSpansForTesting();
2352 ASSERT_EQ(static_cast<size_t>(1), spans.size());
2353 EXPECT_EQ("Symbol", spans[0].first.GetFontName());
2355 #endif // !defined(OS_WIN)
2357 // Ensure that the fallback fonts of the Uniscribe font are tried for shaping.
2358 #if defined(OS_WIN)
2359 TEST_F(RenderTextTest, HarfBuzz_UniscribeFallback) {
2360 RenderTextHarfBuzz render_text;
2361 PlatformFontWin* font_win = new PlatformFontWin("Meiryo", 12);
2362 // Japanese name for Meiryo. This name won't be found in the system's linked
2363 // fonts, forcing RTHB to try the Uniscribe font and its fallbacks.
2364 font_win->font_ref_->font_name_ = WideToUTF8(L"\x30e1\x30a4\x30ea\x30aa");
2365 FontList font_list((Font(font_win)));
2367 render_text.SetFontList(font_list);
2368 // Korean character "han".
2369 render_text.SetText(WideToUTF16(L"\xd55c"));
2370 render_text.EnsureLayout();
2371 ASSERT_EQ(1U, render_text.runs_.size());
2372 EXPECT_EQ(0U, render_text.runs_[0]->CountMissingGlyphs());
2374 #endif // defined(OS_WIN)
2376 } // namespace gfx