Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / gfx / render_text_unittest.cc
blob5140d9e4fdf6e40880b9c8d91dbb0092a62467a5
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/render_text_harfbuzz.h"
20 #if defined(OS_WIN)
21 #include "base/win/windows_version.h"
22 #include "ui/gfx/render_text_win.h"
23 #endif
25 #if defined(OS_LINUX) && !defined(USE_OZONE)
26 #include "ui/gfx/render_text_pango.h"
27 #endif
29 using base::ASCIIToUTF16;
30 using base::UTF8ToUTF16;
31 using base::WideToUTF16;
33 namespace gfx {
35 namespace {
37 // Various weak, LTR, RTL, and Bidi string cases with three characters each.
38 const wchar_t kWeak[] = L" . ";
39 const wchar_t kLtr[] = L"abc";
40 const wchar_t kRtl[] = L"\x5d0\x5d1\x5d2";
41 #if !defined(OS_MACOSX)
42 const wchar_t kLtrRtl[] = L"a" L"\x5d0\x5d1";
43 const wchar_t kLtrRtlLtr[] = L"a" L"\x5d1" L"b";
44 const wchar_t kRtlLtr[] = L"\x5d0\x5d1" L"a";
45 const wchar_t kRtlLtrRtl[] = L"\x5d0" L"a" L"\x5d1";
46 #endif
48 // Checks whether |range| contains |index|. This is not the same as calling
49 // range.Contains(Range(index)), which returns true if |index| == |range.end()|.
50 bool IndexInRange(const Range& range, size_t index) {
51 return index >= range.start() && index < range.end();
54 base::string16 GetSelectedText(RenderText* render_text) {
55 return render_text->text().substr(render_text->selection().GetMin(),
56 render_text->selection().length());
59 // A test utility function to set the application default text direction.
60 void SetRTL(bool rtl) {
61 // Override the current locale/direction.
62 base::i18n::SetICUDefaultLocale(rtl ? "he" : "en");
63 EXPECT_EQ(rtl, base::i18n::IsRTL());
66 #if !defined(OS_MACOSX)
67 // Ensure cursor movement in the specified |direction| yields |expected| values.
68 void RunMoveCursorLeftRightTest(RenderText* render_text,
69 const std::vector<SelectionModel>& expected,
70 VisualCursorDirection direction) {
71 for (size_t i = 0; i < expected.size(); ++i) {
72 SCOPED_TRACE(base::StringPrintf("Going %s; expected value index %d.",
73 direction == CURSOR_LEFT ? "left" : "right", static_cast<int>(i)));
74 EXPECT_EQ(expected[i], render_text->selection_model());
75 render_text->MoveCursor(CHARACTER_BREAK, direction, false);
77 // Check that cursoring is clamped at the line edge.
78 EXPECT_EQ(expected.back(), render_text->selection_model());
79 // Check that it is the line edge.
80 render_text->MoveCursor(LINE_BREAK, direction, false);
81 EXPECT_EQ(expected.back(), render_text->selection_model());
83 #endif // !defined(OS_MACOSX)
85 } // namespace
87 class RenderTextTest : public testing::Test {
90 TEST_F(RenderTextTest, DefaultStyle) {
91 // Check the default styles applied to new instances and adjusted text.
92 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
93 EXPECT_TRUE(render_text->text().empty());
94 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
95 for (size_t i = 0; i < arraysize(cases); ++i) {
96 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLACK));
97 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
98 EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false));
99 render_text->SetText(WideToUTF16(cases[i]));
103 TEST_F(RenderTextTest, SetColorAndStyle) {
104 // Ensure custom default styles persist across setting and clearing text.
105 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
106 const SkColor color = SK_ColorRED;
107 render_text->SetColor(color);
108 render_text->SetStyle(BOLD, true);
109 render_text->SetStyle(UNDERLINE, false);
110 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
111 for (size_t i = 0; i < arraysize(cases); ++i) {
112 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color));
113 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true));
114 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false));
115 render_text->SetText(WideToUTF16(cases[i]));
117 // Ensure custom default styles can be applied after text has been set.
118 if (i == 1)
119 render_text->SetStyle(STRIKE, true);
120 if (i >= 1)
121 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true));
125 TEST_F(RenderTextTest, ApplyColorAndStyle) {
126 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
127 render_text->SetText(ASCIIToUTF16("012345678"));
129 // Apply a ranged color and style and check the resulting breaks.
130 render_text->ApplyColor(SK_ColorRED, Range(1, 4));
131 render_text->ApplyStyle(BOLD, true, Range(2, 5));
132 std::vector<std::pair<size_t, SkColor> > expected_color;
133 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK));
134 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED));
135 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK));
136 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
137 std::vector<std::pair<size_t, bool> > expected_style;
138 expected_style.push_back(std::pair<size_t, bool>(0, false));
139 expected_style.push_back(std::pair<size_t, bool>(2, true));
140 expected_style.push_back(std::pair<size_t, bool>(5, false));
141 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style));
143 // Ensure setting a color and style overrides the ranged colors and styles.
144 render_text->SetColor(SK_ColorBLUE);
145 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE));
146 render_text->SetStyle(BOLD, false);
147 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false));
149 // Apply a color and style over the text end and check the resulting breaks.
150 // (INT_MAX should be used instead of the text length for the range end)
151 const size_t text_length = render_text->text().length();
152 render_text->ApplyColor(SK_ColorRED, Range(0, text_length));
153 render_text->ApplyStyle(BOLD, true, Range(2, text_length));
154 std::vector<std::pair<size_t, SkColor> > expected_color_end;
155 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED));
156 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end));
157 std::vector<std::pair<size_t, bool> > expected_style_end;
158 expected_style_end.push_back(std::pair<size_t, bool>(0, false));
159 expected_style_end.push_back(std::pair<size_t, bool>(2, true));
160 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end));
162 // Ensure ranged values adjust to accommodate text length changes.
163 render_text->ApplyStyle(ITALIC, true, Range(0, 2));
164 render_text->ApplyStyle(ITALIC, true, Range(3, 6));
165 render_text->ApplyStyle(ITALIC, true, Range(7, text_length));
166 std::vector<std::pair<size_t, bool> > expected_italic;
167 expected_italic.push_back(std::pair<size_t, bool>(0, true));
168 expected_italic.push_back(std::pair<size_t, bool>(2, false));
169 expected_italic.push_back(std::pair<size_t, bool>(3, true));
170 expected_italic.push_back(std::pair<size_t, bool>(6, false));
171 expected_italic.push_back(std::pair<size_t, bool>(7, true));
172 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
174 // Truncating the text should trim any corresponding breaks.
175 render_text->SetText(ASCIIToUTF16("0123456"));
176 expected_italic.resize(4);
177 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
178 render_text->SetText(ASCIIToUTF16("01234"));
179 expected_italic.resize(3);
180 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
182 // Appending text should extend the terminal styles without changing breaks.
183 render_text->SetText(ASCIIToUTF16("012345678"));
184 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
187 #if defined(OS_LINUX) && !defined(USE_OZONE)
188 TEST_F(RenderTextTest, PangoAttributes) {
189 scoped_ptr<RenderText> render_text(RenderText::CreateNativeInstance());
190 render_text->SetText(ASCIIToUTF16("012345678"));
192 // Apply ranged BOLD/ITALIC styles and check the resulting Pango attributes.
193 render_text->ApplyStyle(BOLD, true, Range(2, 4));
194 render_text->ApplyStyle(ITALIC, true, Range(1, 3));
196 struct {
197 int start;
198 int end;
199 bool bold;
200 bool italic;
201 } cases[] = {
202 { 0, 1, false, false },
203 { 1, 2, false, true },
204 { 2, 3, true, true },
205 { 3, 4, true, false },
206 { 4, INT_MAX, false, false },
209 int start = 0, end = 0;
210 RenderTextPango* rt_linux = static_cast<RenderTextPango*>(render_text.get());
211 rt_linux->EnsureLayout();
212 PangoAttrList* attributes = pango_layout_get_attributes(rt_linux->layout_);
213 PangoAttrIterator* iter = pango_attr_list_get_iterator(attributes);
214 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
215 pango_attr_iterator_range(iter, &start, &end);
216 EXPECT_EQ(cases[i].start, start);
217 EXPECT_EQ(cases[i].end, end);
218 PangoFontDescription* font = pango_font_description_new();
219 pango_attr_iterator_get_font(iter, font, NULL, NULL);
220 char* description_string = pango_font_description_to_string(font);
221 const base::string16 desc = ASCIIToUTF16(description_string);
222 const bool bold = desc.find(ASCIIToUTF16("Bold")) != std::string::npos;
223 EXPECT_EQ(cases[i].bold, bold);
224 const bool italic = desc.find(ASCIIToUTF16("Italic")) != std::string::npos;
225 EXPECT_EQ(cases[i].italic, italic);
226 pango_attr_iterator_next(iter);
227 pango_font_description_free(font);
228 g_free(description_string);
230 EXPECT_FALSE(pango_attr_iterator_next(iter));
231 pango_attr_iterator_destroy(iter);
233 #endif
235 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
236 // does not implement this yet. http://crbug.com/131618
237 #if !defined(OS_MACOSX)
238 void TestVisualCursorMotionInObscuredField(RenderText* render_text,
239 const base::string16& text,
240 bool select) {
241 ASSERT_TRUE(render_text->obscured());
242 render_text->SetText(text);
243 int len = text.length();
244 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, select);
245 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
246 render_text->selection_model());
247 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, select);
248 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
249 for (int j = 1; j <= len; ++j) {
250 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, select);
251 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_BACKWARD),
252 render_text->selection_model());
254 for (int j = len - 1; j >= 0; --j) {
255 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, select);
256 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_FORWARD),
257 render_text->selection_model());
259 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, select);
260 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
261 render_text->selection_model());
262 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, select);
263 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
266 TEST_F(RenderTextTest, ObscuredText) {
267 const base::string16 seuss = ASCIIToUTF16("hop on pop");
268 const base::string16 no_seuss = ASCIIToUTF16("**********");
269 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
271 // GetLayoutText() returns asterisks when the obscured bit is set.
272 render_text->SetText(seuss);
273 render_text->SetObscured(true);
274 EXPECT_EQ(seuss, render_text->text());
275 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
276 render_text->SetObscured(false);
277 EXPECT_EQ(seuss, render_text->text());
278 EXPECT_EQ(seuss, render_text->GetLayoutText());
280 render_text->SetObscured(true);
282 // Surrogate pairs are counted as one code point.
283 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 0};
284 render_text->SetText(invalid_surrogates);
285 EXPECT_EQ(ASCIIToUTF16("**"), render_text->GetLayoutText());
286 const base::char16 valid_surrogates[] = {0xD800, 0xDC00, 0};
287 render_text->SetText(valid_surrogates);
288 EXPECT_EQ(ASCIIToUTF16("*"), render_text->GetLayoutText());
289 EXPECT_EQ(0U, render_text->cursor_position());
290 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
291 EXPECT_EQ(2U, render_text->cursor_position());
293 // Test index conversion and cursor validity with a valid surrogate pair.
294 EXPECT_EQ(0U, render_text->TextIndexToLayoutIndex(0U));
295 EXPECT_EQ(1U, render_text->TextIndexToLayoutIndex(1U));
296 EXPECT_EQ(1U, render_text->TextIndexToLayoutIndex(2U));
297 EXPECT_EQ(0U, render_text->LayoutIndexToTextIndex(0U));
298 EXPECT_EQ(2U, render_text->LayoutIndexToTextIndex(1U));
299 EXPECT_TRUE(render_text->IsValidCursorIndex(0U));
300 EXPECT_FALSE(render_text->IsValidCursorIndex(1U));
301 EXPECT_TRUE(render_text->IsValidCursorIndex(2U));
303 // FindCursorPosition() should not return positions between a surrogate pair.
304 render_text->SetDisplayRect(Rect(0, 0, 20, 20));
305 EXPECT_EQ(render_text->FindCursorPosition(Point(0, 0)).caret_pos(), 0U);
306 EXPECT_EQ(render_text->FindCursorPosition(Point(20, 0)).caret_pos(), 2U);
307 for (int x = -1; x <= 20; ++x) {
308 SelectionModel selection = render_text->FindCursorPosition(Point(x, 0));
309 EXPECT_TRUE(selection.caret_pos() == 0U || selection.caret_pos() == 2U);
312 // GetGlyphBounds() should yield the entire string bounds for text index 0.
313 EXPECT_EQ(render_text->GetStringSize().width(),
314 static_cast<int>(render_text->GetGlyphBounds(0U).length()));
316 // Cursoring is independent of underlying characters when text is obscured.
317 const wchar_t* const texts[] = {
318 kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl,
319 L"hop on pop", // Check LTR word boundaries.
320 L"\x05d0\x05d1 \x05d0\x05d2 \x05d1\x05d2", // Check RTL word boundaries.
322 for (size_t i = 0; i < arraysize(texts); ++i) {
323 base::string16 text = WideToUTF16(texts[i]);
324 TestVisualCursorMotionInObscuredField(render_text.get(), text, false);
325 TestVisualCursorMotionInObscuredField(render_text.get(), text, true);
329 TEST_F(RenderTextTest, RevealObscuredText) {
330 const base::string16 seuss = ASCIIToUTF16("hop on pop");
331 const base::string16 no_seuss = ASCIIToUTF16("**********");
332 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
334 render_text->SetText(seuss);
335 render_text->SetObscured(true);
336 EXPECT_EQ(seuss, render_text->text());
337 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
339 // Valid reveal index and new revealed index clears previous one.
340 render_text->RenderText::SetObscuredRevealIndex(0);
341 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetLayoutText());
342 render_text->RenderText::SetObscuredRevealIndex(1);
343 EXPECT_EQ(ASCIIToUTF16("*o********"), render_text->GetLayoutText());
344 render_text->RenderText::SetObscuredRevealIndex(2);
345 EXPECT_EQ(ASCIIToUTF16("**p*******"), render_text->GetLayoutText());
347 // Invalid reveal index.
348 render_text->RenderText::SetObscuredRevealIndex(-1);
349 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
350 render_text->RenderText::SetObscuredRevealIndex(seuss.length() + 1);
351 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
353 // SetObscured clears the revealed index.
354 render_text->RenderText::SetObscuredRevealIndex(0);
355 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetLayoutText());
356 render_text->SetObscured(false);
357 EXPECT_EQ(seuss, render_text->GetLayoutText());
358 render_text->SetObscured(true);
359 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
361 // SetText clears the revealed index.
362 render_text->SetText(ASCIIToUTF16("new"));
363 EXPECT_EQ(ASCIIToUTF16("***"), render_text->GetLayoutText());
364 render_text->RenderText::SetObscuredRevealIndex(2);
365 EXPECT_EQ(ASCIIToUTF16("**w"), render_text->GetLayoutText());
366 render_text->SetText(ASCIIToUTF16("new longer"));
367 EXPECT_EQ(ASCIIToUTF16("**********"), render_text->GetLayoutText());
369 // Text with invalid surrogates.
370 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 'h', 'o', 'p', 0};
371 render_text->SetText(invalid_surrogates);
372 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetLayoutText());
373 render_text->RenderText::SetObscuredRevealIndex(0);
374 const base::char16 invalid_expect_0[] = {0xDC00, '*', '*', '*', '*', 0};
375 EXPECT_EQ(invalid_expect_0, render_text->GetLayoutText());
376 render_text->RenderText::SetObscuredRevealIndex(1);
377 const base::char16 invalid_expect_1[] = {'*', 0xD800, '*', '*', '*', 0};
378 EXPECT_EQ(invalid_expect_1, render_text->GetLayoutText());
379 render_text->RenderText::SetObscuredRevealIndex(2);
380 EXPECT_EQ(ASCIIToUTF16("**h**"), render_text->GetLayoutText());
382 // Text with valid surrogates before and after the reveal index.
383 const base::char16 valid_surrogates[] =
384 {0xD800, 0xDC00, 'h', 'o', 'p', 0xD800, 0xDC00, 0};
385 render_text->SetText(valid_surrogates);
386 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetLayoutText());
387 render_text->RenderText::SetObscuredRevealIndex(0);
388 const base::char16 valid_expect_0_and_1[] =
389 {0xD800, 0xDC00, '*', '*', '*', '*', 0};
390 EXPECT_EQ(valid_expect_0_and_1, render_text->GetLayoutText());
391 render_text->RenderText::SetObscuredRevealIndex(1);
392 EXPECT_EQ(valid_expect_0_and_1, render_text->GetLayoutText());
393 render_text->RenderText::SetObscuredRevealIndex(2);
394 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text->GetLayoutText());
395 render_text->RenderText::SetObscuredRevealIndex(5);
396 const base::char16 valid_expect_5_and_6[] =
397 {'*', '*', '*', '*', 0xD800, 0xDC00, 0};
398 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText());
399 render_text->RenderText::SetObscuredRevealIndex(6);
400 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText());
403 TEST_F(RenderTextTest, ElidedText) {
404 // TODO(skanuj) : Add more test cases for following
405 // - RenderText styles.
406 // - Cross interaction of truncate, elide and obscure.
407 // - ElideText tests from text_elider.cc.
408 struct {
409 const wchar_t* text;
410 const wchar_t* layout_text;
411 const bool elision_expected;
412 } cases[] = {
413 // Strings shorter than the elision width should be laid out in full.
414 { L"", L"" , false },
415 { L"M", L"" , false },
416 { L" . ", L" . " , false },
417 { kWeak, kWeak , false },
418 { kLtr, kLtr , false },
419 { kLtrRtl, kLtrRtl , false },
420 { kLtrRtlLtr, kLtrRtlLtr, false },
421 { kRtl, kRtl , false },
422 { kRtlLtr, kRtlLtr , false },
423 { kRtlLtrRtl, kRtlLtrRtl, false },
424 // Strings as long as the elision width should be laid out in full.
425 { L"012ab", L"012ab" , false },
426 // Long strings should be elided with an ellipsis appended at the end.
427 { L"012abc", L"012a\x2026", true },
428 { L"012ab" L"\x5d0\x5d1", L"012a\x2026", true },
429 { L"012a" L"\x5d1" L"b", L"012a\x2026", true },
430 // No RLM marker added as digits (012) have weak directionality.
431 { L"01" L"\x5d0\x5d1\x5d2", L"01\x5d0\x5d1\x2026", true },
432 // RLM marker added as "ab" have strong LTR directionality.
433 { L"ab" L"\x5d0\x5d1\x5d2", L"ab\x5d0\x5d1\x2026\x200f", true },
434 // Complex script is not handled. In this example, the "\x0915\x093f" is a
435 // compound glyph, but only half of it is elided.
436 { L"0123\x0915\x093f", L"0123\x0915\x2026", true },
437 // Surrogate pairs should be elided reasonably enough.
438 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8", false },
439 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x2026" , true },
440 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x2026" , true },
441 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x2026\x200E" , true },
442 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" , true },
445 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
446 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
447 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
449 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
450 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
451 render_text->SetElideBehavior(ELIDE_TAIL);
453 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
454 // Compute expected width
455 expected_render_text->SetText(WideToUTF16(cases[i].layout_text));
456 int expected_width = expected_render_text->GetContentWidth();
458 base::string16 input = WideToUTF16(cases[i].text);
459 // Extend the input text to ensure that it is wider than the layout_text,
460 // and so it will get elided.
461 if (cases[i].elision_expected)
462 input.append(WideToUTF16(L" MMMMMMMMMMM"));
464 render_text->SetText(input);
465 render_text->SetDisplayRect(Rect(0, 0, expected_width, 100));
466 EXPECT_EQ(input, render_text->text())
467 << "->For case " << i << ": " << cases[i].text << "\n";
468 EXPECT_EQ(WideToUTF16(cases[i].layout_text), render_text->GetLayoutText())
469 << "->For case " << i << ": " << cases[i].text << "\n";
470 expected_render_text->SetText(base::string16());
474 TEST_F(RenderTextTest, ElidedObscuredText) {
475 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
476 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
477 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
478 expected_render_text->SetText(WideToUTF16(L"**\x2026"));
480 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
481 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
482 render_text->SetElideBehavior(ELIDE_TAIL);
483 render_text->SetDisplayRect(
484 Rect(0, 0, expected_render_text->GetContentWidth(), 100));
485 render_text->SetObscured(true);
486 render_text->SetText(WideToUTF16(L"abcdef"));
487 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
488 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetLayoutText());
491 TEST_F(RenderTextTest, TruncatedText) {
492 struct {
493 const wchar_t* text;
494 const wchar_t* layout_text;
495 } cases[] = {
496 // Strings shorter than the truncation length should be laid out in full.
497 { L"", L"" },
498 { kWeak, kWeak },
499 { kLtr, kLtr },
500 { kLtrRtl, kLtrRtl },
501 { kLtrRtlLtr, kLtrRtlLtr },
502 { kRtl, kRtl },
503 { kRtlLtr, kRtlLtr },
504 { kRtlLtrRtl, kRtlLtrRtl },
505 // Strings as long as the truncation length should be laid out in full.
506 { L"01234", L"01234" },
507 // Long strings should be truncated with an ellipsis appended at the end.
508 { L"012345", L"0123\x2026" },
509 { L"012" L" . ", L"012 \x2026" },
510 { L"012" L"abc", L"012a\x2026" },
511 { L"012" L"a" L"\x5d0\x5d1", L"012a\x2026" },
512 { L"012" L"a" L"\x5d1" L"b", L"012a\x2026" },
513 { L"012" L"\x5d0\x5d1\x5d2", L"012\x5d0\x2026" },
514 { L"012" L"\x5d0\x5d1" L"a", L"012\x5d0\x2026" },
515 { L"012" L"\x5d0" L"a" L"\x5d1", L"012\x5d0\x2026" },
516 // Surrogate pairs should be truncated reasonably enough.
517 { L"0123\x0915\x093f", L"0123\x2026" },
518 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8" },
519 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x05bc\x2026" },
520 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x05e9\x2026" },
521 { L"0123\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
522 { L"01234\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
523 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" },
526 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
527 render_text->set_truncate_length(5);
528 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
529 render_text->SetText(WideToUTF16(cases[i].text));
530 EXPECT_EQ(WideToUTF16(cases[i].text), render_text->text());
531 EXPECT_EQ(WideToUTF16(cases[i].layout_text), render_text->GetLayoutText())
532 << "For case " << i << ": " << cases[i].text;
536 TEST_F(RenderTextTest, TruncatedObscuredText) {
537 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
538 render_text->set_truncate_length(3);
539 render_text->SetObscured(true);
540 render_text->SetText(WideToUTF16(L"abcdef"));
541 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
542 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetLayoutText());
545 TEST_F(RenderTextTest, TruncatedCursorMovementLTR) {
546 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
547 render_text->set_truncate_length(2);
548 render_text->SetText(WideToUTF16(L"abcd"));
550 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
551 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
552 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
553 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
554 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
556 std::vector<SelectionModel> expected;
557 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
558 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
559 // The cursor hops over the ellipsis and elided text to the line end.
560 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
561 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
562 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
564 expected.clear();
565 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
566 // The cursor hops over the elided text to preceeding text.
567 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
568 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
569 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
570 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
573 TEST_F(RenderTextTest, TruncatedCursorMovementRTL) {
574 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
575 render_text->set_truncate_length(2);
576 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3"));
578 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
579 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
580 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
581 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
582 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
584 std::vector<SelectionModel> expected;
585 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
586 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
587 // The cursor hops over the ellipsis and elided text to the line end.
588 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
589 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
590 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
592 expected.clear();
593 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
594 // The cursor hops over the elided text to preceeding text.
595 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
596 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
597 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
598 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
601 TEST_F(RenderTextTest, GetTextDirection) {
602 struct {
603 const wchar_t* text;
604 const base::i18n::TextDirection text_direction;
605 } cases[] = {
606 // Blank strings and those with no/weak directionality default to LTR.
607 { L"", base::i18n::LEFT_TO_RIGHT },
608 { kWeak, base::i18n::LEFT_TO_RIGHT },
609 // Strings that begin with strong LTR characters.
610 { kLtr, base::i18n::LEFT_TO_RIGHT },
611 { kLtrRtl, base::i18n::LEFT_TO_RIGHT },
612 { kLtrRtlLtr, base::i18n::LEFT_TO_RIGHT },
613 // Strings that begin with strong RTL characters.
614 { kRtl, base::i18n::RIGHT_TO_LEFT },
615 { kRtlLtr, base::i18n::RIGHT_TO_LEFT },
616 { kRtlLtrRtl, base::i18n::RIGHT_TO_LEFT },
619 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
620 const bool was_rtl = base::i18n::IsRTL();
622 for (size_t i = 0; i < 2; ++i) {
623 // Toggle the application default text direction (to try each direction).
624 SetRTL(!base::i18n::IsRTL());
625 const base::i18n::TextDirection ui_direction = base::i18n::IsRTL() ?
626 base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
628 // Ensure that directionality modes yield the correct text directions.
629 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(cases); j++) {
630 render_text->SetText(WideToUTF16(cases[j].text));
631 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
632 EXPECT_EQ(render_text->GetTextDirection(), cases[j].text_direction);
633 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_UI);
634 EXPECT_EQ(render_text->GetTextDirection(), ui_direction);
635 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_LTR);
636 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::LEFT_TO_RIGHT);
637 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_RTL);
638 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::RIGHT_TO_LEFT);
642 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
644 // Ensure that text changes update the direction for DIRECTIONALITY_FROM_TEXT.
645 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
646 render_text->SetText(WideToUTF16(kLtr));
647 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::LEFT_TO_RIGHT);
648 render_text->SetText(WideToUTF16(kRtl));
649 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::RIGHT_TO_LEFT);
652 TEST_F(RenderTextTest, MoveCursorLeftRightInLtr) {
653 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
655 // Pure LTR.
656 render_text->SetText(ASCIIToUTF16("abc"));
657 // |expected| saves the expected SelectionModel when moving cursor from left
658 // to right.
659 std::vector<SelectionModel> expected;
660 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
661 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
662 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
663 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
664 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
665 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
667 expected.clear();
668 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
669 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
670 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
671 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
672 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
673 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
676 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtl) {
677 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
678 // LTR-RTL
679 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
680 // The last one is the expected END position.
681 std::vector<SelectionModel> expected;
682 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
683 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
684 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
685 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
686 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
687 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
688 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
689 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
690 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
692 expected.clear();
693 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
694 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
695 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
696 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
697 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
698 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
699 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
700 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
701 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
704 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtlLtr) {
705 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
706 // LTR-RTL-LTR.
707 render_text->SetText(WideToUTF16(L"a" L"\x05d1" L"b"));
708 std::vector<SelectionModel> expected;
709 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
710 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
711 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
712 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
713 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
714 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
716 expected.clear();
717 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
718 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
719 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
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, MoveCursorLeftRightInRtl) {
726 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
727 // Pure RTL.
728 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2"));
729 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
730 std::vector<SelectionModel> expected;
732 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
733 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
734 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
735 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
736 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
737 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
739 expected.clear();
741 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
742 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
743 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
744 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
745 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
746 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
749 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtr) {
750 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
751 // RTL-LTR
752 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2" L"abc"));
753 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
754 std::vector<SelectionModel> expected;
755 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
756 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
757 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
758 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
759 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
760 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
761 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
762 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
763 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
765 expected.clear();
766 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
767 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
768 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
769 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
770 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
771 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
772 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
773 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
774 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
777 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtrRtl) {
778 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
779 // RTL-LTR-RTL.
780 render_text->SetText(WideToUTF16(L"\x05d0" L"a" L"\x05d1"));
781 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
782 std::vector<SelectionModel> expected;
783 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
784 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
785 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
786 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
787 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
788 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
790 expected.clear();
791 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
792 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
793 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
794 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
795 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
796 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
799 // TODO(xji): temporarily disable in platform Win since the complex script
800 // characters turned into empty square due to font regression. So, not able
801 // to test 2 characters belong to the same grapheme.
802 #if defined(OS_LINUX)
803 TEST_F(RenderTextTest, MoveCursorLeftRight_ComplexScript) {
804 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
806 render_text->SetText(WideToUTF16(L"\x0915\x093f\x0915\x094d\x0915"));
807 EXPECT_EQ(0U, render_text->cursor_position());
808 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
809 EXPECT_EQ(2U, render_text->cursor_position());
810 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
811 EXPECT_EQ(4U, render_text->cursor_position());
812 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
813 EXPECT_EQ(5U, render_text->cursor_position());
814 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
815 EXPECT_EQ(5U, render_text->cursor_position());
817 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
818 EXPECT_EQ(4U, render_text->cursor_position());
819 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
820 EXPECT_EQ(2U, render_text->cursor_position());
821 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
822 EXPECT_EQ(0U, render_text->cursor_position());
823 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
824 EXPECT_EQ(0U, render_text->cursor_position());
826 #endif
828 // TODO(ckocagil): Enable for RenderTextHarfBuzz. http://crbug.com/383265
829 TEST_F(RenderTextTest, MoveCursorLeftRight_MeiryoUILigatures) {
830 scoped_ptr<RenderText> render_text(RenderText::CreateNativeInstance());
831 // Meiryo UI uses single-glyph ligatures for 'ff' and 'ffi', but each letter
832 // (code point) has unique bounds, so mid-glyph cursoring should be possible.
833 render_text->SetFontList(FontList("Meiryo UI, 12px"));
834 render_text->SetText(WideToUTF16(L"ff ffi"));
835 EXPECT_EQ(0U, render_text->cursor_position());
836 for (size_t i = 0; i < render_text->text().length(); ++i) {
837 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
838 EXPECT_EQ(i + 1, render_text->cursor_position());
840 EXPECT_EQ(6U, render_text->cursor_position());
843 TEST_F(RenderTextTest, GraphemePositions) {
844 // LTR 2-character grapheme, LTR abc, LTR 2-character grapheme.
845 const base::string16 kText1 =
846 WideToUTF16(L"\x0915\x093f" L"abc" L"\x0915\x093f");
848 // LTR ab, LTR 2-character grapheme, LTR cd.
849 const base::string16 kText2 = WideToUTF16(L"ab" L"\x0915\x093f" L"cd");
851 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
852 // two characters forming the surrogate pair 0x0001D11E.
853 const std::string kSurrogate = "\xF0\x9D\x84\x9E";
855 // LTR ab, UTF16 surrogate pair, LTR cd.
856 const base::string16 kText3 = UTF8ToUTF16("ab" + kSurrogate + "cd");
858 struct {
859 base::string16 text;
860 size_t index;
861 size_t expected_previous;
862 size_t expected_next;
863 } cases[] = {
864 { base::string16(), 0, 0, 0 },
865 { base::string16(), 1, 0, 0 },
866 { base::string16(), 50, 0, 0 },
867 { kText1, 0, 0, 2 },
868 { kText1, 1, 0, 2 },
869 { kText1, 2, 0, 3 },
870 { kText1, 3, 2, 4 },
871 { kText1, 4, 3, 5 },
872 { kText1, 5, 4, 7 },
873 { kText1, 6, 5, 7 },
874 { kText1, 7, 5, 7 },
875 { kText1, 8, 7, 7 },
876 { kText1, 50, 7, 7 },
877 { kText2, 0, 0, 1 },
878 { kText2, 1, 0, 2 },
879 { kText2, 2, 1, 4 },
880 { kText2, 3, 2, 4 },
881 { kText2, 4, 2, 5 },
882 { kText2, 5, 4, 6 },
883 { kText2, 6, 5, 6 },
884 { kText2, 7, 6, 6 },
885 { kText2, 50, 6, 6 },
886 { kText3, 0, 0, 1 },
887 { kText3, 1, 0, 2 },
888 { kText3, 2, 1, 4 },
889 { kText3, 3, 2, 4 },
890 { kText3, 4, 2, 5 },
891 { kText3, 5, 4, 6 },
892 { kText3, 6, 5, 6 },
893 { kText3, 7, 6, 6 },
894 { kText3, 50, 6, 6 },
897 #if defined(OS_WIN)
898 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
899 if (base::win::GetVersion() < base::win::VERSION_VISTA)
900 return;
901 #endif
903 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
904 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
905 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
906 render_text->SetText(cases[i].text);
908 size_t next = render_text->IndexOfAdjacentGrapheme(cases[i].index,
909 CURSOR_FORWARD);
910 EXPECT_EQ(cases[i].expected_next, next);
911 EXPECT_TRUE(render_text->IsValidCursorIndex(next));
913 size_t previous = render_text->IndexOfAdjacentGrapheme(cases[i].index,
914 CURSOR_BACKWARD);
915 EXPECT_EQ(cases[i].expected_previous, previous);
916 EXPECT_TRUE(render_text->IsValidCursorIndex(previous));
920 TEST_F(RenderTextTest, MidGraphemeSelectionBounds) {
921 #if defined(OS_WIN)
922 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
923 if (base::win::GetVersion() < base::win::VERSION_VISTA)
924 return;
925 #endif
927 // Test that selection bounds may be set amid multi-character graphemes.
928 const base::string16 kHindi = WideToUTF16(L"\x0915\x093f");
929 const base::string16 kThai = WideToUTF16(L"\x0e08\x0e33");
930 const base::string16 cases[] = { kHindi, kThai };
932 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
933 for (size_t i = 0; i < arraysize(cases); i++) {
934 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
935 render_text->SetText(cases[i]);
936 EXPECT_TRUE(render_text->IsValidLogicalIndex(1));
937 #if !defined(OS_MACOSX)
938 EXPECT_FALSE(render_text->IsValidCursorIndex(1));
939 #endif
940 EXPECT_TRUE(render_text->SelectRange(Range(2, 1)));
941 EXPECT_EQ(Range(2, 1), render_text->selection());
942 EXPECT_EQ(1U, render_text->cursor_position());
943 // Although selection bounds may be set within a multi-character grapheme,
944 // cursor movement (e.g. via arrow key) should avoid those indices.
945 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
946 EXPECT_EQ(0U, render_text->cursor_position());
947 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
948 EXPECT_EQ(2U, render_text->cursor_position());
952 TEST_F(RenderTextTest, FindCursorPosition) {
953 const wchar_t* kTestStrings[] = { kLtrRtl, kLtrRtlLtr, kRtlLtr, kRtlLtrRtl };
954 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
955 render_text->SetDisplayRect(Rect(0, 0, 100, 20));
956 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
957 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS "]", i));
958 render_text->SetText(WideToUTF16(kTestStrings[i]));
959 for(size_t j = 0; j < render_text->text().length(); ++j) {
960 const Range range(render_text->GetGlyphBounds(j));
961 // Test a point just inside the leading edge of the glyph bounds.
962 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1;
963 EXPECT_EQ(j, render_text->FindCursorPosition(Point(x, 0)).caret_pos());
968 TEST_F(RenderTextTest, EdgeSelectionModels) {
969 // Simple Latin text.
970 const base::string16 kLatin = WideToUTF16(L"abc");
971 // LTR 2-character grapheme.
972 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f");
973 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
974 const base::string16 kHindiLatin =
975 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f");
976 // RTL 2-character grapheme.
977 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8");
978 // RTL 2-character grapheme, LTR a, RTL 2-character grapheme.
979 const base::string16 kHebrewLatin =
980 WideToUTF16(L"\x05e0\x05b8" L"a" L"\x05e0\x05b8");
982 struct {
983 base::string16 text;
984 base::i18n::TextDirection expected_text_direction;
985 } cases[] = {
986 { base::string16(), base::i18n::LEFT_TO_RIGHT },
987 { kLatin, base::i18n::LEFT_TO_RIGHT },
988 { kLTRGrapheme, base::i18n::LEFT_TO_RIGHT },
989 { kHindiLatin, base::i18n::LEFT_TO_RIGHT },
990 { kRTLGrapheme, base::i18n::RIGHT_TO_LEFT },
991 { kHebrewLatin, base::i18n::RIGHT_TO_LEFT },
994 #if defined(OS_WIN)
995 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
996 if (base::win::GetVersion() < base::win::VERSION_VISTA)
997 return;
998 #endif
1000 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1001 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
1002 render_text->SetText(cases[i].text);
1003 bool ltr = (cases[i].expected_text_direction == base::i18n::LEFT_TO_RIGHT);
1005 SelectionModel start_edge =
1006 render_text->EdgeSelectionModel(ltr ? CURSOR_LEFT : CURSOR_RIGHT);
1007 EXPECT_EQ(start_edge, SelectionModel(0, CURSOR_BACKWARD));
1009 SelectionModel end_edge =
1010 render_text->EdgeSelectionModel(ltr ? CURSOR_RIGHT : CURSOR_LEFT);
1011 EXPECT_EQ(end_edge, SelectionModel(cases[i].text.length(), CURSOR_FORWARD));
1015 TEST_F(RenderTextTest, SelectAll) {
1016 const wchar_t* const cases[] =
1017 { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl };
1019 // Ensure that SelectAll respects the |reversed| argument regardless of
1020 // application locale and text content directionality.
1021 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1022 const SelectionModel expected_reversed(Range(3, 0), CURSOR_FORWARD);
1023 const SelectionModel expected_forwards(Range(0, 3), CURSOR_BACKWARD);
1024 const bool was_rtl = base::i18n::IsRTL();
1026 for (size_t i = 0; i < 2; ++i) {
1027 SetRTL(!base::i18n::IsRTL());
1028 // Test that an empty string produces an empty selection model.
1029 render_text->SetText(base::string16());
1030 EXPECT_EQ(render_text->selection_model(), SelectionModel());
1032 // Test the weak, LTR, RTL, and Bidi string cases.
1033 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(cases); j++) {
1034 render_text->SetText(WideToUTF16(cases[j]));
1035 render_text->SelectAll(false);
1036 EXPECT_EQ(render_text->selection_model(), expected_forwards);
1037 render_text->SelectAll(true);
1038 EXPECT_EQ(render_text->selection_model(), expected_reversed);
1042 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1045 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) {
1046 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1047 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
1048 // Left arrow on select ranging (6, 4).
1049 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1050 EXPECT_EQ(Range(6), render_text->selection());
1051 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1052 EXPECT_EQ(Range(4), render_text->selection());
1053 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1054 EXPECT_EQ(Range(5), render_text->selection());
1055 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1056 EXPECT_EQ(Range(6), render_text->selection());
1057 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1058 EXPECT_EQ(Range(6, 5), render_text->selection());
1059 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1060 EXPECT_EQ(Range(6, 4), render_text->selection());
1061 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1062 EXPECT_EQ(Range(6), render_text->selection());
1064 // Right arrow on select ranging (4, 6).
1065 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1066 EXPECT_EQ(Range(0), render_text->selection());
1067 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1068 EXPECT_EQ(Range(1), render_text->selection());
1069 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1070 EXPECT_EQ(Range(2), render_text->selection());
1071 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1072 EXPECT_EQ(Range(3), render_text->selection());
1073 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1074 EXPECT_EQ(Range(5), render_text->selection());
1075 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1076 EXPECT_EQ(Range(4), render_text->selection());
1077 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1078 EXPECT_EQ(Range(4, 5), render_text->selection());
1079 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1080 EXPECT_EQ(Range(4, 6), render_text->selection());
1081 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1082 EXPECT_EQ(Range(4), render_text->selection());
1085 TEST_F(RenderTextTest, CenteredDisplayOffset) {
1086 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1087 render_text->SetText(ASCIIToUTF16("abcdefghij"));
1088 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1090 const int kEnlargement = 10;
1091 const int content_width = render_text->GetContentWidth();
1092 Rect display_rect(0, 0, content_width / 2,
1093 render_text->font_list().GetHeight());
1094 render_text->SetDisplayRect(display_rect);
1096 // Move the cursor to the beginning of the text and, by checking the cursor
1097 // bounds, make sure no empty space is to the left of the text.
1098 render_text->SetCursorPosition(0);
1099 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1101 // Widen the display rect and, by checking the cursor bounds, make sure no
1102 // empty space is introduced to the left of the text.
1103 display_rect.Inset(0, 0, -kEnlargement, 0);
1104 render_text->SetDisplayRect(display_rect);
1105 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1107 // Move the cursor to the end of the text and, by checking the cursor bounds,
1108 // make sure no empty space is to the right of the text.
1109 render_text->SetCursorPosition(render_text->text().length());
1110 EXPECT_EQ(display_rect.right(),
1111 render_text->GetUpdatedCursorBounds().right());
1113 // Widen the display rect and, by checking the cursor bounds, make sure no
1114 // empty space is introduced to the right of the text.
1115 display_rect.Inset(0, 0, -kEnlargement, 0);
1116 render_text->SetDisplayRect(display_rect);
1117 EXPECT_EQ(display_rect.right(),
1118 render_text->GetUpdatedCursorBounds().right());
1120 #endif // !defined(OS_MACOSX)
1122 // TODO(xji): Make these work on Windows.
1123 #if defined(OS_LINUX)
1124 void MoveLeftRightByWordVerifier(RenderText* render_text,
1125 const wchar_t* str) {
1126 render_text->SetText(WideToUTF16(str));
1128 // Test moving by word from left ro right.
1129 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1130 bool first_word = true;
1131 while (true) {
1132 // First, test moving by word from a word break position, such as from
1133 // "|abc def" to "abc| def".
1134 SelectionModel start = render_text->selection_model();
1135 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1136 SelectionModel end = render_text->selection_model();
1137 if (end == start) // reach the end.
1138 break;
1140 // For testing simplicity, each word is a 3-character word.
1141 int num_of_character_moves = first_word ? 3 : 4;
1142 first_word = false;
1143 render_text->MoveCursorTo(start);
1144 for (int j = 0; j < num_of_character_moves; ++j)
1145 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1146 EXPECT_EQ(end, render_text->selection_model());
1148 // Then, test moving by word from positions inside the word, such as from
1149 // "a|bc def" to "abc| def", and from "ab|c def" to "abc| def".
1150 for (int j = 1; j < num_of_character_moves; ++j) {
1151 render_text->MoveCursorTo(start);
1152 for (int k = 0; k < j; ++k)
1153 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1154 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1155 EXPECT_EQ(end, render_text->selection_model());
1159 // Test moving by word from right to left.
1160 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1161 first_word = true;
1162 while (true) {
1163 SelectionModel start = render_text->selection_model();
1164 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1165 SelectionModel end = render_text->selection_model();
1166 if (end == start) // reach the end.
1167 break;
1169 int num_of_character_moves = first_word ? 3 : 4;
1170 first_word = false;
1171 render_text->MoveCursorTo(start);
1172 for (int j = 0; j < num_of_character_moves; ++j)
1173 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1174 EXPECT_EQ(end, render_text->selection_model());
1176 for (int j = 1; j < num_of_character_moves; ++j) {
1177 render_text->MoveCursorTo(start);
1178 for (int k = 0; k < j; ++k)
1179 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1180 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1181 EXPECT_EQ(end, render_text->selection_model());
1186 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText) {
1187 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1189 // For testing simplicity, each word is a 3-character word.
1190 std::vector<const wchar_t*> test;
1191 test.push_back(L"abc");
1192 test.push_back(L"abc def");
1193 test.push_back(L"\x05E1\x05E2\x05E3");
1194 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1195 test.push_back(L"abc \x05E1\x05E2\x05E3");
1196 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1197 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1198 L" \x05E7\x05E8\x05E9");
1200 test.push_back(L"abc \x05E1\x05E2\x05E3 hij");
1201 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 hij opq");
1202 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1203 L" \x05E7\x05E8\x05E9" L" opq rst uvw");
1205 test.push_back(L"\x05E1\x05E2\x05E3 abc");
1206 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 abc def");
1207 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 \x05E7\x05E8\x05E9"
1208 L" abc def hij");
1210 test.push_back(L"\x05D1\x05D2\x05D3 abc \x05E1\x05E2\x05E3");
1211 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 abc def"
1212 L" \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1213 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 \x05D7\x05D8\x05D9"
1214 L" abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1215 L" \x05E7\x05E8\x05E9");
1217 for (size_t i = 0; i < test.size(); ++i)
1218 MoveLeftRightByWordVerifier(render_text.get(), test[i]);
1221 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText_TestEndOfText) {
1222 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1224 render_text->SetText(WideToUTF16(L"ab\x05E1"));
1225 // Moving the cursor by word from "abC|" to the left should return "|abC".
1226 // But since end of text is always treated as a word break, it returns
1227 // position "ab|C".
1228 // TODO(xji): Need to make it work as expected.
1229 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1230 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1231 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1233 // Moving the cursor by word from "|abC" to the right returns "abC|".
1234 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1235 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1236 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1238 render_text->SetText(WideToUTF16(L"\x05E1\x05E2" L"a"));
1239 // For logical text "BCa", moving the cursor by word from "aCB|" to the left
1240 // returns "|aCB".
1241 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1242 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1243 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1245 // Moving the cursor by word from "|aCB" to the right should return "aCB|".
1246 // But since end of text is always treated as a word break, it returns
1247 // position "a|CB".
1248 // TODO(xji): Need to make it work as expected.
1249 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1250 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1251 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1254 TEST_F(RenderTextTest, MoveLeftRightByWordInTextWithMultiSpaces) {
1255 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1256 render_text->SetText(WideToUTF16(L"abc def"));
1257 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1258 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1259 EXPECT_EQ(11U, render_text->cursor_position());
1261 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1262 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1263 EXPECT_EQ(0U, render_text->cursor_position());
1266 TEST_F(RenderTextTest, MoveLeftRightByWordInChineseText) {
1267 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1268 render_text->SetText(WideToUTF16(L"\x6211\x4EEC\x53BB\x516C\x56ED\x73A9"));
1269 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1270 EXPECT_EQ(0U, render_text->cursor_position());
1271 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1272 EXPECT_EQ(2U, render_text->cursor_position());
1273 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1274 EXPECT_EQ(3U, render_text->cursor_position());
1275 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1276 EXPECT_EQ(5U, render_text->cursor_position());
1277 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1278 EXPECT_EQ(6U, render_text->cursor_position());
1279 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1280 EXPECT_EQ(6U, render_text->cursor_position());
1282 #endif
1284 // TODO(ckocagil): Remove when RenderTextWin goes away.
1285 #if defined(OS_WIN)
1286 TEST_F(RenderTextTest, Win_LogicalClusters) {
1287 scoped_ptr<RenderTextWin> render_text(
1288 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
1290 const base::string16 test_string =
1291 WideToUTF16(L"\x0930\x0930\x0930\x0930\x0930");
1292 render_text->SetText(test_string);
1293 render_text->EnsureLayout();
1294 ASSERT_EQ(1U, render_text->runs_.size());
1295 WORD* logical_clusters = render_text->runs_[0]->logical_clusters.get();
1296 for (size_t i = 0; i < test_string.length(); ++i)
1297 EXPECT_EQ(i, logical_clusters[i]);
1299 #endif // defined(OS_WIN)
1301 TEST_F(RenderTextTest, StringSizeSanity) {
1302 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1303 render_text->SetText(UTF8ToUTF16("Hello World"));
1304 const Size string_size = render_text->GetStringSize();
1305 EXPECT_GT(string_size.width(), 0);
1306 EXPECT_GT(string_size.height(), 0);
1309 TEST_F(RenderTextTest, StringSizeLongStrings) {
1310 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1311 Size previous_string_size;
1312 for (size_t length = 10; length < 1000000; length *= 10) {
1313 render_text->SetText(base::string16(length, 'a'));
1314 const Size string_size = render_text->GetStringSize();
1315 EXPECT_GT(string_size.width(), previous_string_size.width());
1316 EXPECT_GT(string_size.height(), 0);
1317 previous_string_size = string_size;
1321 // TODO(asvitkine): This test fails because PlatformFontMac uses point font
1322 // sizes instead of pixel sizes like other implementations.
1323 #if !defined(OS_MACOSX)
1324 TEST_F(RenderTextTest, StringSizeEmptyString) {
1325 // Ascent and descent of Arial and Symbol are different on most platforms.
1326 const FontList font_list("Arial,Symbol, 16px");
1327 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1328 render_text->SetFontList(font_list);
1329 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight()));
1331 // The empty string respects FontList metrics for non-zero height
1332 // and baseline.
1333 render_text->SetText(base::string16());
1334 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1335 EXPECT_EQ(0, render_text->GetStringSize().width());
1336 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1338 render_text->SetText(UTF8ToUTF16(" "));
1339 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1340 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1342 #endif // !defined(OS_MACOSX)
1344 TEST_F(RenderTextTest, StringSizeRespectsFontListMetrics) {
1345 // Check that Arial and Symbol have different font metrics.
1346 Font arial_font("Arial", 16);
1347 ASSERT_EQ("arial",
1348 base::StringToLowerASCII(arial_font.GetActualFontNameForTesting()));
1349 Font symbol_font("Symbol", 16);
1350 ASSERT_EQ("symbol",
1351 base::StringToLowerASCII(
1352 symbol_font.GetActualFontNameForTesting()));
1353 EXPECT_NE(arial_font.GetHeight(), symbol_font.GetHeight());
1354 EXPECT_NE(arial_font.GetBaseline(), symbol_font.GetBaseline());
1355 // "a" should be rendered with Arial, not with Symbol.
1356 const char* arial_font_text = "a";
1357 // "®" (registered trademark symbol) should be rendered with Symbol,
1358 // not with Arial.
1359 const char* symbol_font_text = "\xC2\xAE";
1361 Font smaller_font = arial_font;
1362 Font larger_font = symbol_font;
1363 const char* smaller_font_text = arial_font_text;
1364 const char* larger_font_text = symbol_font_text;
1365 if (symbol_font.GetHeight() < arial_font.GetHeight() &&
1366 symbol_font.GetBaseline() < arial_font.GetBaseline()) {
1367 std::swap(smaller_font, larger_font);
1368 std::swap(smaller_font_text, larger_font_text);
1370 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight());
1371 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline());
1373 // Check |smaller_font_text| is rendered with the smaller font.
1374 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1375 render_text->SetText(UTF8ToUTF16(smaller_font_text));
1376 render_text->SetFontList(FontList(smaller_font));
1377 render_text->SetDisplayRect(Rect(0, 0, 0,
1378 render_text->font_list().GetHeight()));
1379 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height());
1380 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline());
1382 // Layout the same text with mixed fonts. The text should be rendered with
1383 // the smaller font, but the height and baseline are determined with the
1384 // metrics of the font list, which is equal to the larger font.
1385 std::vector<Font> fonts;
1386 fonts.push_back(smaller_font); // The primary font is the smaller font.
1387 fonts.push_back(larger_font);
1388 const FontList font_list(fonts);
1389 render_text->SetFontList(font_list);
1390 render_text->SetDisplayRect(Rect(0, 0, 0,
1391 render_text->font_list().GetHeight()));
1392 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height());
1393 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline());
1394 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1395 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1398 TEST_F(RenderTextTest, SetFontList) {
1399 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1400 render_text->SetFontList(FontList("Arial,Symbol, 13px"));
1401 const std::vector<Font>& fonts = render_text->font_list().GetFonts();
1402 ASSERT_EQ(2U, fonts.size());
1403 EXPECT_EQ("Arial", fonts[0].GetFontName());
1404 EXPECT_EQ("Symbol", fonts[1].GetFontName());
1405 EXPECT_EQ(13, render_text->font_list().GetFontSize());
1408 TEST_F(RenderTextTest, StringSizeBoldWidth) {
1409 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1410 render_text->SetText(UTF8ToUTF16("Hello World"));
1412 const int plain_width = render_text->GetStringSize().width();
1413 EXPECT_GT(plain_width, 0);
1415 // Apply a bold style and check that the new width is greater.
1416 render_text->SetStyle(BOLD, true);
1417 const int bold_width = render_text->GetStringSize().width();
1418 EXPECT_GT(bold_width, plain_width);
1420 // Now, apply a plain style over the first word only.
1421 render_text->ApplyStyle(BOLD, false, Range(0, 5));
1422 const int plain_bold_width = render_text->GetStringSize().width();
1423 EXPECT_GT(plain_bold_width, plain_width);
1424 EXPECT_LT(plain_bold_width, bold_width);
1427 TEST_F(RenderTextTest, StringSizeHeight) {
1428 base::string16 cases[] = {
1429 WideToUTF16(L"Hello World!"), // English
1430 WideToUTF16(L"\x6328\x62f6"), // Japanese
1431 WideToUTF16(L"\x0915\x093f"), // Hindi
1432 WideToUTF16(L"\x05e0\x05b8"), // Hebrew
1435 const FontList default_font_list;
1436 const FontList& larger_font_list = default_font_list.DeriveWithSizeDelta(24);
1437 EXPECT_GT(larger_font_list.GetHeight(), default_font_list.GetHeight());
1439 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
1440 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1441 render_text->SetFontList(default_font_list);
1442 render_text->SetText(cases[i]);
1444 const int height1 = render_text->GetStringSize().height();
1445 EXPECT_GT(height1, 0);
1447 // Check that setting the larger font increases the height.
1448 render_text->SetFontList(larger_font_list);
1449 const int height2 = render_text->GetStringSize().height();
1450 EXPECT_GT(height2, height1);
1454 TEST_F(RenderTextTest, GetBaselineSanity) {
1455 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1456 render_text->SetText(UTF8ToUTF16("Hello World"));
1457 const int baseline = render_text->GetBaseline();
1458 EXPECT_GT(baseline, 0);
1461 TEST_F(RenderTextTest, CursorBoundsInReplacementMode) {
1462 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1463 render_text->SetText(ASCIIToUTF16("abcdefg"));
1464 render_text->SetDisplayRect(Rect(100, 17));
1465 SelectionModel sel_b(1, CURSOR_FORWARD);
1466 SelectionModel sel_c(2, CURSOR_FORWARD);
1467 Rect cursor_around_b = render_text->GetCursorBounds(sel_b, false);
1468 Rect cursor_before_b = render_text->GetCursorBounds(sel_b, true);
1469 Rect cursor_before_c = render_text->GetCursorBounds(sel_c, true);
1470 EXPECT_EQ(cursor_around_b.x(), cursor_before_b.x());
1471 EXPECT_EQ(cursor_around_b.right(), cursor_before_c.x());
1474 TEST_F(RenderTextTest, GetTextOffset) {
1475 // The default horizontal text offset differs for LTR and RTL, and is only set
1476 // when the RenderText object is created. This test will check the default in
1477 // LTR mode, and the next test will check the RTL default.
1478 const bool was_rtl = base::i18n::IsRTL();
1479 SetRTL(false);
1480 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1481 render_text->SetText(ASCIIToUTF16("abcdefg"));
1482 render_text->SetFontList(FontList("Arial, 13px"));
1484 // Set display area's size equal to the font size.
1485 const Size font_size(render_text->GetContentWidth(),
1486 render_text->font_list().GetHeight());
1487 Rect display_rect(font_size);
1488 render_text->SetDisplayRect(display_rect);
1490 Vector2d offset = render_text->GetLineOffset(0);
1491 EXPECT_TRUE(offset.IsZero());
1493 const int kEnlargementX = 2;
1494 display_rect.Inset(0, 0, -kEnlargementX, 0);
1495 render_text->SetDisplayRect(display_rect);
1497 // Check the default horizontal alignment.
1498 offset = render_text->GetLineOffset(0);
1499 EXPECT_EQ(0, offset.x());
1501 // Check explicitly setting the horizontal alignment.
1502 render_text->SetHorizontalAlignment(ALIGN_LEFT);
1503 offset = render_text->GetLineOffset(0);
1504 EXPECT_EQ(0, offset.x());
1505 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1506 offset = render_text->GetLineOffset(0);
1507 EXPECT_EQ(kEnlargementX / 2, offset.x());
1508 render_text->SetHorizontalAlignment(ALIGN_RIGHT);
1509 offset = render_text->GetLineOffset(0);
1510 EXPECT_EQ(kEnlargementX, offset.x());
1512 // Check that text is vertically centered within taller display rects.
1513 const int kEnlargementY = display_rect.height();
1514 display_rect.Inset(0, 0, 0, -kEnlargementY);
1515 render_text->SetDisplayRect(display_rect);
1516 const Vector2d prev_offset = render_text->GetLineOffset(0);
1517 display_rect.Inset(0, 0, 0, -2 * kEnlargementY);
1518 render_text->SetDisplayRect(display_rect);
1519 offset = render_text->GetLineOffset(0);
1520 EXPECT_EQ(prev_offset.y() + kEnlargementY, offset.y());
1522 SetRTL(was_rtl);
1525 TEST_F(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL) {
1526 // This only checks the default horizontal alignment in RTL mode; all other
1527 // GetLineOffset(0) attributes are checked by the test above.
1528 const bool was_rtl = base::i18n::IsRTL();
1529 SetRTL(true);
1530 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1531 render_text->SetText(ASCIIToUTF16("abcdefg"));
1532 render_text->SetFontList(FontList("Arial, 13px"));
1533 const int kEnlargement = 2;
1534 const Size font_size(render_text->GetContentWidth() + kEnlargement,
1535 render_text->GetStringSize().height());
1536 Rect display_rect(font_size);
1537 render_text->SetDisplayRect(display_rect);
1538 Vector2d offset = render_text->GetLineOffset(0);
1539 EXPECT_EQ(kEnlargement, offset.x());
1540 SetRTL(was_rtl);
1543 TEST_F(RenderTextTest, SetDisplayOffset) {
1544 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1545 render_text->SetText(ASCIIToUTF16("abcdefg"));
1546 render_text->SetFontList(FontList("Arial, 13px"));
1548 const Size font_size(render_text->GetContentWidth(),
1549 render_text->font_list().GetHeight());
1550 const int kEnlargement = 10;
1552 // Set display width |kEnlargement| pixels greater than content width and test
1553 // different possible situations. In this case the only possible display
1554 // offset is zero.
1555 Rect display_rect(font_size);
1556 display_rect.Inset(0, 0, -kEnlargement, 0);
1557 render_text->SetDisplayRect(display_rect);
1559 struct {
1560 HorizontalAlignment alignment;
1561 int offset;
1562 } small_content_cases[] = {
1563 { ALIGN_LEFT, -kEnlargement },
1564 { ALIGN_LEFT, 0 },
1565 { ALIGN_LEFT, kEnlargement },
1566 { ALIGN_RIGHT, -kEnlargement },
1567 { ALIGN_RIGHT, 0 },
1568 { ALIGN_RIGHT, kEnlargement },
1569 { ALIGN_CENTER, -kEnlargement },
1570 { ALIGN_CENTER, 0 },
1571 { ALIGN_CENTER, kEnlargement },
1574 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(small_content_cases); i++) {
1575 render_text->SetHorizontalAlignment(small_content_cases[i].alignment);
1576 render_text->SetDisplayOffset(small_content_cases[i].offset);
1577 EXPECT_EQ(0, render_text->GetUpdatedDisplayOffset().x());
1580 // Set display width |kEnlargement| pixels less than content width and test
1581 // different possible situations.
1582 display_rect = Rect(font_size);
1583 display_rect.Inset(0, 0, kEnlargement, 0);
1584 render_text->SetDisplayRect(display_rect);
1586 struct {
1587 HorizontalAlignment alignment;
1588 int offset;
1589 int expected_offset;
1590 } large_content_cases[] = {
1591 // When text is left-aligned, display offset can be in range
1592 // [-kEnlargement, 0].
1593 { ALIGN_LEFT, -2 * kEnlargement, -kEnlargement },
1594 { ALIGN_LEFT, -kEnlargement / 2, -kEnlargement / 2 },
1595 { ALIGN_LEFT, kEnlargement, 0 },
1596 // When text is right-aligned, display offset can be in range
1597 // [0, kEnlargement].
1598 { ALIGN_RIGHT, -kEnlargement, 0 },
1599 { ALIGN_RIGHT, kEnlargement / 2, kEnlargement / 2 },
1600 { ALIGN_RIGHT, 2 * kEnlargement, kEnlargement },
1601 // When text is center-aligned, display offset can be in range
1602 // [-kEnlargement / 2 - 1, (kEnlargement - 1) / 2].
1603 { ALIGN_CENTER, -kEnlargement, -kEnlargement / 2 - 1 },
1604 { ALIGN_CENTER, -kEnlargement / 4, -kEnlargement / 4 },
1605 { ALIGN_CENTER, kEnlargement / 4, kEnlargement / 4 },
1606 { ALIGN_CENTER, kEnlargement, (kEnlargement - 1) / 2 },
1609 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(large_content_cases); i++) {
1610 render_text->SetHorizontalAlignment(large_content_cases[i].alignment);
1611 render_text->SetDisplayOffset(large_content_cases[i].offset);
1612 EXPECT_EQ(large_content_cases[i].expected_offset,
1613 render_text->GetUpdatedDisplayOffset().x());
1617 // TODO(ckocagil): Enable for RenderTextHarfBuzz. http://crbug.com/396776
1618 TEST_F(RenderTextTest, SameFontForParentheses) {
1619 struct {
1620 const base::char16 left_char;
1621 const base::char16 right_char;
1622 } punctuation_pairs[] = {
1623 { '(', ')' },
1624 { '{', '}' },
1625 { '<', '>' },
1627 struct {
1628 base::string16 text;
1629 } cases[] = {
1630 // English(English)
1631 { WideToUTF16(L"Hello World(a)") },
1632 // English(English)English
1633 { WideToUTF16(L"Hello World(a)Hello World") },
1635 // Japanese(English)
1636 { WideToUTF16(L"\x6328\x62f6(a)") },
1637 // Japanese(English)Japanese
1638 { WideToUTF16(L"\x6328\x62f6(a)\x6328\x62f6") },
1639 // English(Japanese)English
1640 { WideToUTF16(L"Hello World(\x6328\x62f6)Hello World") },
1642 // Hindi(English)
1643 { WideToUTF16(L"\x0915\x093f(a)") },
1644 // Hindi(English)Hindi
1645 { WideToUTF16(L"\x0915\x093f(a)\x0915\x093f") },
1646 // English(Hindi)English
1647 { WideToUTF16(L"Hello World(\x0915\x093f)Hello World") },
1649 // Hebrew(English)
1650 { WideToUTF16(L"\x05e0\x05b8(a)") },
1651 // Hebrew(English)Hebrew
1652 { WideToUTF16(L"\x05e0\x05b8(a)\x05e0\x05b8") },
1653 // English(Hebrew)English
1654 { WideToUTF16(L"Hello World(\x05e0\x05b8)Hello World") },
1657 scoped_ptr<RenderText> render_text(RenderText::CreateNativeInstance());
1658 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
1659 base::string16 text = cases[i].text;
1660 const size_t start_paren_char_index = text.find('(');
1661 ASSERT_NE(base::string16::npos, start_paren_char_index);
1662 const size_t end_paren_char_index = text.find(')');
1663 ASSERT_NE(base::string16::npos, end_paren_char_index);
1665 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(punctuation_pairs); ++j) {
1666 text[start_paren_char_index] = punctuation_pairs[j].left_char;
1667 text[end_paren_char_index] = punctuation_pairs[j].right_char;
1668 render_text->SetText(text);
1670 const std::vector<RenderText::FontSpan> spans =
1671 render_text->GetFontSpansForTesting();
1673 int start_paren_span_index = -1;
1674 int end_paren_span_index = -1;
1675 for (size_t k = 0; k < spans.size(); ++k) {
1676 if (IndexInRange(spans[k].second, start_paren_char_index))
1677 start_paren_span_index = k;
1678 if (IndexInRange(spans[k].second, end_paren_char_index))
1679 end_paren_span_index = k;
1681 ASSERT_NE(-1, start_paren_span_index);
1682 ASSERT_NE(-1, end_paren_span_index);
1684 const Font& start_font = spans[start_paren_span_index].first;
1685 const Font& end_font = spans[end_paren_span_index].first;
1686 EXPECT_EQ(start_font.GetFontName(), end_font.GetFontName());
1687 EXPECT_EQ(start_font.GetFontSize(), end_font.GetFontSize());
1688 EXPECT_EQ(start_font.GetStyle(), end_font.GetStyle());
1693 // Make sure the caret width is always >=1 so that the correct
1694 // caret is drawn at high DPI. crbug.com/164100.
1695 TEST_F(RenderTextTest, CaretWidth) {
1696 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1697 render_text->SetText(ASCIIToUTF16("abcdefg"));
1698 EXPECT_GE(render_text->GetUpdatedCursorBounds().width(), 1);
1701 TEST_F(RenderTextTest, SelectWord) {
1702 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1703 render_text->SetText(ASCIIToUTF16(" foo a.bc.d bar"));
1705 struct {
1706 size_t cursor;
1707 size_t selection_start;
1708 size_t selection_end;
1709 } cases[] = {
1710 { 0, 0, 1 },
1711 { 1, 1, 4 },
1712 { 2, 1, 4 },
1713 { 3, 1, 4 },
1714 { 4, 4, 6 },
1715 { 5, 4, 6 },
1716 { 6, 6, 7 },
1717 { 7, 7, 8 },
1718 { 8, 8, 10 },
1719 { 9, 8, 10 },
1720 { 10, 10, 11 },
1721 { 11, 11, 12 },
1722 { 12, 12, 13 },
1723 { 13, 13, 16 },
1724 { 14, 13, 16 },
1725 { 15, 13, 16 },
1726 { 16, 13, 16 },
1729 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
1730 render_text->SetCursorPosition(cases[i].cursor);
1731 render_text->SelectWord();
1732 EXPECT_EQ(Range(cases[i].selection_start, cases[i].selection_end),
1733 render_text->selection());
1737 // Make sure the last word is selected when the cursor is at text.length().
1738 TEST_F(RenderTextTest, LastWordSelected) {
1739 const std::string kTestURL1 = "http://www.google.com";
1740 const std::string kTestURL2 = "http://www.google.com/something/";
1742 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1744 render_text->SetText(ASCIIToUTF16(kTestURL1));
1745 render_text->SetCursorPosition(kTestURL1.length());
1746 render_text->SelectWord();
1747 EXPECT_EQ(ASCIIToUTF16("com"), GetSelectedText(render_text.get()));
1748 EXPECT_FALSE(render_text->selection().is_reversed());
1750 render_text->SetText(ASCIIToUTF16(kTestURL2));
1751 render_text->SetCursorPosition(kTestURL2.length());
1752 render_text->SelectWord();
1753 EXPECT_EQ(ASCIIToUTF16("/"), GetSelectedText(render_text.get()));
1754 EXPECT_FALSE(render_text->selection().is_reversed());
1757 // When given a non-empty selection, SelectWord should expand the selection to
1758 // nearest word boundaries.
1759 TEST_F(RenderTextTest, SelectMultipleWords) {
1760 const std::string kTestURL = "http://www.google.com";
1762 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1764 render_text->SetText(ASCIIToUTF16(kTestURL));
1765 render_text->SelectRange(Range(16, 20));
1766 render_text->SelectWord();
1767 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1768 EXPECT_FALSE(render_text->selection().is_reversed());
1770 // SelectWord should preserve the selection direction.
1771 render_text->SelectRange(Range(20, 16));
1772 render_text->SelectWord();
1773 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1774 EXPECT_TRUE(render_text->selection().is_reversed());
1777 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
1778 // does not implement this yet. http://crbug.com/131618
1779 #if !defined(OS_MACOSX)
1780 TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) {
1781 ASSERT_FALSE(base::i18n::IsRTL());
1782 ASSERT_FALSE(base::i18n::ICUIsRTL());
1784 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1785 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1786 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1787 CURSOR_FORWARD));
1788 int width = render_text->GetStringSize().width();
1789 ASSERT_GT(width, 10);
1791 // Ensure that the cursor is placed at the width of its preceding text.
1792 render_text->SetDisplayRect(Rect(width + 10, 1));
1793 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1795 // Ensure that shrinking the display rectangle keeps the cursor in view.
1796 render_text->SetDisplayRect(Rect(width - 10, 1));
1797 EXPECT_EQ(render_text->display_rect().width(),
1798 render_text->GetUpdatedCursorBounds().right());
1800 // Ensure that the text will pan to fill its expanding display rectangle.
1801 render_text->SetDisplayRect(Rect(width - 5, 1));
1802 EXPECT_EQ(render_text->display_rect().width(),
1803 render_text->GetUpdatedCursorBounds().right());
1805 // Ensure that a sufficiently large display rectangle shows all the text.
1806 render_text->SetDisplayRect(Rect(width + 10, 1));
1807 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1809 // Repeat the test with RTL text.
1810 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1811 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1812 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1813 width = render_text->GetStringSize().width();
1814 ASSERT_GT(width, 10);
1816 // Ensure that the cursor is placed at the width of its preceding text.
1817 render_text->SetDisplayRect(Rect(width + 10, 1));
1818 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1820 // Ensure that shrinking the display rectangle keeps the cursor in view.
1821 render_text->SetDisplayRect(Rect(width - 10, 1));
1822 EXPECT_EQ(render_text->display_rect().width(),
1823 render_text->GetUpdatedCursorBounds().right());
1825 // Ensure that the text will pan to fill its expanding display rectangle.
1826 render_text->SetDisplayRect(Rect(width - 5, 1));
1827 EXPECT_EQ(render_text->display_rect().width(),
1828 render_text->GetUpdatedCursorBounds().right());
1830 // Ensure that a sufficiently large display rectangle shows all the text.
1831 render_text->SetDisplayRect(Rect(width + 10, 1));
1832 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1835 TEST_F(RenderTextTest, DisplayRectShowsCursorRTL) {
1836 // Set the application default text direction to RTL.
1837 const bool was_rtl = base::i18n::IsRTL();
1838 SetRTL(true);
1840 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1841 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1842 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1843 int width = render_text->GetStringSize().width();
1844 ASSERT_GT(width, 10);
1846 // Ensure that the cursor is placed at the width of its preceding text.
1847 render_text->SetDisplayRect(Rect(width + 10, 1));
1848 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1849 render_text->GetUpdatedCursorBounds().x());
1851 // Ensure that shrinking the display rectangle keeps the cursor in view.
1852 render_text->SetDisplayRect(Rect(width - 10, 1));
1853 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1855 // Ensure that the text will pan to fill its expanding display rectangle.
1856 render_text->SetDisplayRect(Rect(width - 5, 1));
1857 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1859 // Ensure that a sufficiently large display rectangle shows all the text.
1860 render_text->SetDisplayRect(Rect(width + 10, 1));
1861 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1862 render_text->GetUpdatedCursorBounds().x());
1864 // Repeat the test with RTL text.
1865 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1866 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1867 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1868 CURSOR_FORWARD));
1869 width = render_text->GetStringSize().width();
1870 ASSERT_GT(width, 10);
1872 // Ensure that the cursor is placed at the width of its preceding text.
1873 render_text->SetDisplayRect(Rect(width + 10, 1));
1874 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1875 render_text->GetUpdatedCursorBounds().x());
1877 // Ensure that shrinking the display rectangle keeps the cursor in view.
1878 render_text->SetDisplayRect(Rect(width - 10, 1));
1879 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1881 // Ensure that the text will pan to fill its expanding display rectangle.
1882 render_text->SetDisplayRect(Rect(width - 5, 1));
1883 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1885 // Ensure that a sufficiently large display rectangle shows all the text.
1886 render_text->SetDisplayRect(Rect(width + 10, 1));
1887 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1888 render_text->GetUpdatedCursorBounds().x());
1890 // Reset the application default text direction to LTR.
1891 SetRTL(was_rtl);
1892 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1894 #endif // !defined(OS_MACOSX)
1896 // Changing colors between or inside ligated glyphs should not break shaping.
1897 TEST_F(RenderTextTest, SelectionKeepsLigatures) {
1898 const wchar_t* kTestStrings[] = { L"\x644\x623", L"\x633\x627" };
1899 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1900 render_text->set_selection_color(SK_ColorRED);
1901 Canvas canvas;
1903 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1904 render_text->SetText(WideToUTF16(kTestStrings[i]));
1905 const int expected_width = render_text->GetStringSize().width();
1906 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD));
1907 EXPECT_EQ(expected_width, render_text->GetStringSize().width());
1908 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119
1909 render_text->Draw(&canvas);
1910 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1914 #if defined(OS_WIN)
1915 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1916 // Ensure strings wrap onto multiple lines for a small available width.
1917 TEST_F(RenderTextTest, Multiline_MinWidth) {
1918 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl,
1919 kRtlLtr, kRtlLtrRtl };
1921 scoped_ptr<RenderTextWin> render_text(
1922 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
1923 render_text->SetDisplayRect(Rect(1, 1000));
1924 render_text->SetMultiline(true);
1925 Canvas canvas;
1927 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1928 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1929 render_text->SetText(WideToUTF16(kTestStrings[i]));
1930 render_text->Draw(&canvas);
1931 EXPECT_GT(render_text->lines_.size(), 1U);
1935 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1936 // Ensure strings wrap onto multiple lines for a normal available width.
1937 TEST_F(RenderTextTest, Multiline_NormalWidth) {
1938 const struct {
1939 const wchar_t* const text;
1940 const Range first_line_char_range;
1941 const Range second_line_char_range;
1942 } kTestStrings[] = {
1943 { L"abc defg hijkl", Range(0, 9), Range(9, 14) },
1944 { L"qwertyzxcvbn", Range(0, 8), Range(8, 12) },
1945 { L"\x062A\x0641\x0627\x062D\x05EA\x05E4\x05D5\x05D6\x05D9\x05DD",
1946 Range(4, 10), Range(0, 4) }
1949 scoped_ptr<RenderTextWin> render_text(
1950 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
1951 render_text->SetDisplayRect(Rect(50, 1000));
1952 render_text->SetMultiline(true);
1953 Canvas canvas;
1955 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestStrings); ++i) {
1956 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1957 render_text->SetText(WideToUTF16(kTestStrings[i].text));
1958 render_text->Draw(&canvas);
1959 ASSERT_EQ(2U, render_text->lines_.size());
1960 ASSERT_EQ(1U, render_text->lines_[0].segments.size());
1961 EXPECT_EQ(kTestStrings[i].first_line_char_range,
1962 render_text->lines_[0].segments[0].char_range);
1963 ASSERT_EQ(1U, render_text->lines_[1].segments.size());
1964 EXPECT_EQ(kTestStrings[i].second_line_char_range,
1965 render_text->lines_[1].segments[0].char_range);
1969 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1970 // Ensure strings don't wrap onto multiple lines for a sufficient available
1971 // width.
1972 TEST_F(RenderTextTest, Multiline_SufficientWidth) {
1973 const wchar_t* kTestStrings[] = { L"", L" ", L".", L" . ", L"abc", L"a b c",
1974 L"\x62E\x628\x632", L"\x62E \x628 \x632" };
1976 scoped_ptr<RenderTextWin> render_text(
1977 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
1978 render_text->SetDisplayRect(Rect(30, 1000));
1979 render_text->SetMultiline(true);
1980 Canvas canvas;
1982 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1983 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1984 render_text->SetText(WideToUTF16(kTestStrings[i]));
1985 render_text->Draw(&canvas);
1986 EXPECT_EQ(1U, render_text->lines_.size());
1990 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1991 TEST_F(RenderTextTest, Multiline_Newline) {
1992 const struct {
1993 const wchar_t* const text;
1994 // Ranges of the characters on each line preceding the newline.
1995 const Range first_line_char_range;
1996 const Range second_line_char_range;
1997 } kTestStrings[] = {
1998 { L"abc\ndef", Range(0, 3), Range(4, 7) },
1999 { L"a \n b ", Range(0, 2), Range(3, 6) },
2000 { L"\n" , Range::InvalidRange(), Range::InvalidRange() }
2003 scoped_ptr<RenderTextWin> render_text(
2004 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
2005 render_text->SetDisplayRect(Rect(200, 1000));
2006 render_text->SetMultiline(true);
2007 Canvas canvas;
2009 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestStrings); ++i) {
2010 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2011 render_text->SetText(WideToUTF16(kTestStrings[i].text));
2012 render_text->Draw(&canvas);
2014 ASSERT_EQ(2U, render_text->lines_.size());
2016 const Range first_expected_range = kTestStrings[i].first_line_char_range;
2017 ASSERT_EQ(first_expected_range.IsValid() ? 2U : 1U,
2018 render_text->lines_[0].segments.size());
2019 if (first_expected_range.IsValid())
2020 EXPECT_EQ(first_expected_range,
2021 render_text->lines_[0].segments[0].char_range);
2023 const internal::LineSegment& newline_segment =
2024 render_text->lines_[0].segments[first_expected_range.IsValid() ? 1 : 0];
2025 ASSERT_EQ(1U, newline_segment.char_range.length());
2026 EXPECT_EQ(L'\n', kTestStrings[i].text[newline_segment.char_range.start()]);
2028 const Range second_expected_range = kTestStrings[i].second_line_char_range;
2029 ASSERT_EQ(second_expected_range.IsValid() ? 1U : 0U,
2030 render_text->lines_[1].segments.size());
2031 if (second_expected_range.IsValid())
2032 EXPECT_EQ(second_expected_range,
2033 render_text->lines_[1].segments[0].char_range);
2037 // TODO(ckocagil): Remove when RenderTextWin goes away.
2038 TEST_F(RenderTextTest, BreakRunsByUnicodeBlocks) {
2039 scoped_ptr<RenderTextWin> render_text(
2040 static_cast<RenderTextWin*>(RenderText::CreateNativeInstance()));
2042 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2043 render_text->SetText(WideToUTF16(L"x\x25B6y"));
2044 render_text->EnsureLayout();
2045 ASSERT_EQ(3U, render_text->runs_.size());
2046 EXPECT_EQ(Range(0, 1), render_text->runs_[0]->range);
2047 EXPECT_EQ(Range(1, 2), render_text->runs_[1]->range);
2048 EXPECT_EQ(Range(2, 3), render_text->runs_[2]->range);
2050 render_text->SetText(WideToUTF16(L"x \x25B6 y"));
2051 render_text->EnsureLayout();
2052 ASSERT_EQ(3U, render_text->runs_.size());
2053 EXPECT_EQ(Range(0, 2), render_text->runs_[0]->range);
2054 EXPECT_EQ(Range(2, 3), render_text->runs_[1]->range);
2055 EXPECT_EQ(Range(3, 5), render_text->runs_[2]->range);
2057 #endif // defined(OS_WIN)
2059 // Test TextRunHarfBuzz's cluster finding logic.
2060 TEST_F(RenderTextTest, HarfBuzz_Clusters) {
2061 struct {
2062 uint32 glyph_to_char[4];
2063 Range chars[4];
2064 Range glyphs[4];
2065 bool is_rtl;
2066 } cases[] = {
2067 { // From string "A B C D" to glyphs "a b c d".
2068 { 0, 1, 2, 3 },
2069 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2070 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2071 false
2073 { // From string "A B C D" to glyphs "d c b a".
2074 { 3, 2, 1, 0 },
2075 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2076 { Range(3, 4), Range(2, 3), Range(1, 2), Range(0, 1) },
2077 true
2079 { // From string "A B C D" to glyphs "ab c c d".
2080 { 0, 2, 2, 3 },
2081 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2082 { Range(0, 1), Range(0, 1), Range(1, 3), Range(3, 4) },
2083 false
2085 { // From string "A B C D" to glyphs "d c c ba".
2086 { 3, 2, 2, 0 },
2087 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2088 { Range(3, 4), Range(3, 4), Range(1, 3), Range(0, 1) },
2089 true
2093 internal::TextRunHarfBuzz run;
2094 run.range = Range(0, 4);
2095 run.glyph_count = 4;
2096 run.glyph_to_char.resize(4);
2098 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
2099 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 4,
2100 run.glyph_to_char.begin());
2101 run.is_rtl = cases[i].is_rtl;
2103 for (size_t j = 0; j < 4; ++j) {
2104 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2105 Range chars;
2106 Range glyphs;
2107 run.GetClusterAt(j, &chars, &glyphs);
2108 EXPECT_EQ(cases[i].chars[j], chars);
2109 EXPECT_EQ(cases[i].glyphs[j], glyphs);
2110 EXPECT_EQ(cases[i].glyphs[j], run.CharRangeToGlyphRange(chars));
2115 // Ensure that graphemes with multiple code points do not get split.
2116 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemeCases) {
2117 const wchar_t* cases[] = {
2118 // "A" with a combining umlaut, followed by a "B".
2119 L"A\x0308" L"B",
2120 // Devanagari biconsonantal conjunct "ki", followed by an "a".
2121 L"\x0915\x093f\x0905",
2122 // Thai consonant and vowel pair "cho chan" + "sara am", followed by Thai
2123 // digit 0.
2124 L"\x0e08\x0e33\x0E50",
2127 RenderTextHarfBuzz render_text;
2129 for (size_t i = 0; i < arraysize(cases); ++i) {
2130 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS, i));
2132 base::string16 text = WideToUTF16(cases[i]);
2133 render_text.SetText(text);
2134 render_text.EnsureLayout();
2135 ASSERT_EQ(1U, render_text.runs_.size());
2136 internal::TextRunHarfBuzz* run = render_text.runs_[0];
2138 base::i18n::BreakIterator* iter = render_text.grapheme_iterator_.get();
2139 Range first_grapheme_bounds = run->GetGraphemeBounds(iter, 0);
2140 EXPECT_EQ(first_grapheme_bounds, run->GetGraphemeBounds(iter, 1));
2141 Range second_grapheme_bounds = run->GetGraphemeBounds(iter, 2);
2142 EXPECT_EQ(first_grapheme_bounds.end(), second_grapheme_bounds.start());
2146 // Test the partition of a multi-grapheme cluster into grapheme ranges.
2147 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemePartition) {
2148 struct {
2149 uint32 glyph_to_char[2];
2150 Range bounds[4];
2151 bool is_rtl;
2152 } cases[] = {
2153 { // From string "A B C D" to glyphs "a bcd".
2154 { 0, 1 },
2155 { Range(0, 10), Range(10, 13), Range(13, 17), Range(17, 20) },
2156 false
2158 { // From string "A B C D" to glyphs "ab cd".
2159 { 0, 2 },
2160 { Range(0, 5), Range(5, 10), Range(10, 15), Range(15, 20) },
2161 false
2163 { // From string "A B C D" to glyphs "dcb a".
2164 { 1, 0 },
2165 { Range(10, 20), Range(7, 10), Range(3, 7), Range(0, 3) },
2166 true
2168 { // From string "A B C D" to glyphs "dc ba".
2169 { 2, 0 },
2170 { Range(15, 20), Range(10, 15), Range(5, 10), Range(0, 5) },
2171 true
2175 internal::TextRunHarfBuzz run;
2176 run.range = Range(0, 4);
2177 run.glyph_count = 2;
2178 run.glyph_to_char.resize(2);
2179 run.positions.reset(new SkPoint[4]);
2180 run.width = 20;
2182 const base::string16 kString = ASCIIToUTF16("abcd");
2183 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2184 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2185 ASSERT_TRUE(iter->Init());
2187 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
2188 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 2,
2189 run.glyph_to_char.begin());
2190 run.is_rtl = cases[i].is_rtl;
2191 for (int j = 0; j < 2; ++j)
2192 run.positions[j].set(j * 10, 0);
2194 for (size_t j = 0; j < 4; ++j) {
2195 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2196 EXPECT_EQ(cases[i].bounds[j], run.GetGraphemeBounds(iter.get(), j));
2201 TEST_F(RenderTextTest, HarfBuzz_RunDirection) {
2202 RenderTextHarfBuzz render_text;
2203 const base::string16 mixed =
2204 WideToUTF16(L"\x05D0\x05D1" L"1234" L"\x05D2\x05D3");
2205 render_text.SetText(mixed);
2206 render_text.EnsureLayout();
2207 ASSERT_EQ(3U, render_text.runs_.size());
2208 EXPECT_TRUE(render_text.runs_[0]->is_rtl);
2209 EXPECT_FALSE(render_text.runs_[1]->is_rtl);
2210 EXPECT_TRUE(render_text.runs_[2]->is_rtl);
2213 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks) {
2214 RenderTextHarfBuzz render_text;
2216 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2217 render_text.SetText(WideToUTF16(L"x\x25B6y"));
2218 render_text.EnsureLayout();
2219 ASSERT_EQ(3U, render_text.runs_.size());
2220 EXPECT_EQ(Range(0, 1), render_text.runs_[0]->range);
2221 EXPECT_EQ(Range(1, 2), render_text.runs_[1]->range);
2222 EXPECT_EQ(Range(2, 3), render_text.runs_[2]->range);
2224 render_text.SetText(WideToUTF16(L"x \x25B6 y"));
2225 render_text.EnsureLayout();
2226 ASSERT_EQ(3U, render_text.runs_.size());
2227 EXPECT_EQ(Range(0, 2), render_text.runs_[0]->range);
2228 EXPECT_EQ(Range(2, 3), render_text.runs_[1]->range);
2229 EXPECT_EQ(Range(3, 5), render_text.runs_[2]->range);
2232 // Disabled on Mac because RenderTextMac doesn't implement GetGlyphBounds.
2233 #if !defined(OS_MACOSX)
2234 TEST_F(RenderTextTest, GlyphBounds) {
2235 const wchar_t* kTestStrings[] = {
2236 L"asdf 1234 qwer", L"\x0647\x0654", L"\x0645\x0631\x062D\x0628\x0627"
2238 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2240 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2241 render_text->SetText(WideToUTF16(kTestStrings[i]));
2242 render_text->EnsureLayout();
2244 for (size_t j = 0; j < render_text->text().length(); ++j)
2245 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2248 #endif
2250 // Remove this after making RTHB default in favor of RenderTextTest.GlyphBounds.
2251 TEST_F(RenderTextTest, HarfBuzz_GlyphBounds) {
2252 const wchar_t* kTestStrings[] = {
2253 L"asdf 1234 qwer", L"\x0647\x0654", L"\x0645\x0631\x062D\x0628\x0627"
2255 scoped_ptr<RenderText> render_text(new RenderTextHarfBuzz);
2257 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2258 render_text->SetText(WideToUTF16(kTestStrings[i]));
2259 render_text->EnsureLayout();
2261 for (size_t j = 0; j < render_text->text().length(); ++j)
2262 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2266 // Ensure that shaping with a non-existent font does not cause a crash.
2267 TEST_F(RenderTextTest, HarfBuzz_NonExistentFont) {
2268 RenderTextHarfBuzz render_text;
2269 render_text.SetText(ASCIIToUTF16("test"));
2270 render_text.EnsureLayout();
2271 ASSERT_EQ(1U, render_text.runs_.size());
2272 internal::TextRunHarfBuzz* run = render_text.runs_[0];
2273 render_text.ShapeRunWithFont(run, "TheFontThatDoesntExist");
2276 // Ensure an empty run returns sane values to queries.
2277 TEST_F(RenderTextTest, HarfBuzz_EmptyRun) {
2278 internal::TextRunHarfBuzz run;
2279 const base::string16 kString = ASCIIToUTF16("abcdefgh");
2280 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2281 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2282 ASSERT_TRUE(iter->Init());
2284 run.range = Range(3, 8);
2285 run.glyph_count = 0;
2286 EXPECT_EQ(Range(0, 0), run.CharRangeToGlyphRange(Range(4, 5)));
2287 EXPECT_EQ(Range(0, 0), run.GetGraphemeBounds(iter.get(), 4));
2288 Range chars;
2289 Range glyphs;
2290 run.GetClusterAt(4, &chars, &glyphs);
2291 EXPECT_EQ(Range(3, 8), chars);
2292 EXPECT_EQ(Range(0, 0), glyphs);
2295 } // namespace gfx