Add a basic settings page with a Polymer paper element which loads at chrome://md...
[chromium-blink-merge.git] / ui / gfx / render_text_unittest.cc
blobf947d754d385a0436167d5b8f8f6a4c83bb4b60e
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 "third_party/skia/include/core/SkSurface.h"
17 #include "ui/gfx/break_list.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/color_utils.h"
20 #include "ui/gfx/font.h"
21 #include "ui/gfx/render_text_harfbuzz.h"
23 #if defined(OS_WIN)
24 #include "base/win/windows_version.h"
25 #include "ui/gfx/platform_font_win.h"
26 #endif
28 using base::ASCIIToUTF16;
29 using base::UTF8ToUTF16;
30 using base::WideToUTF16;
31 using base::WideToUTF8;
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 const wchar_t kLtrRtl[] = L"a" L"\x5d0\x5d1";
42 const wchar_t kLtrRtlLtr[] = L"a" L"\x5d1" L"b";
43 const wchar_t kRtlLtr[] = L"\x5d0\x5d1" L"a";
44 const wchar_t kRtlLtrRtl[] = L"\x5d0" L"a" L"\x5d1";
46 // Checks whether |range| contains |index|. This is not the same as calling
47 // range.Contains(Range(index)), which returns true if |index| == |range.end()|.
48 bool IndexInRange(const Range& range, size_t index) {
49 return index >= range.start() && index < range.end();
52 base::string16 GetSelectedText(RenderText* render_text) {
53 return render_text->text().substr(render_text->selection().GetMin(),
54 render_text->selection().length());
57 // A test utility function to set the application default text direction.
58 void SetRTL(bool rtl) {
59 // Override the current locale/direction.
60 base::i18n::SetICUDefaultLocale(rtl ? "he" : "en");
61 EXPECT_EQ(rtl, base::i18n::IsRTL());
64 #if !defined(OS_MACOSX)
65 // Ensure cursor movement in the specified |direction| yields |expected| values.
66 void RunMoveCursorLeftRightTest(RenderText* render_text,
67 const std::vector<SelectionModel>& expected,
68 VisualCursorDirection direction) {
69 for (size_t i = 0; i < expected.size(); ++i) {
70 SCOPED_TRACE(base::StringPrintf("Going %s; expected value index %d.",
71 direction == CURSOR_LEFT ? "left" : "right", static_cast<int>(i)));
72 EXPECT_EQ(expected[i], render_text->selection_model());
73 render_text->MoveCursor(CHARACTER_BREAK, direction, false);
75 // Check that cursoring is clamped at the line edge.
76 EXPECT_EQ(expected.back(), render_text->selection_model());
77 // Check that it is the line edge.
78 render_text->MoveCursor(LINE_BREAK, direction, false);
79 EXPECT_EQ(expected.back(), render_text->selection_model());
81 #endif // !defined(OS_MACOSX)
83 } // namespace
85 class RenderTextTest : public testing::Test {
88 TEST_F(RenderTextTest, DefaultStyle) {
89 // Check the default styles applied to new instances and adjusted text.
90 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
91 EXPECT_TRUE(render_text->text().empty());
92 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
93 for (size_t i = 0; i < arraysize(cases); ++i) {
94 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLACK));
95 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
96 EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false));
97 render_text->SetText(WideToUTF16(cases[i]));
101 TEST_F(RenderTextTest, SetColorAndStyle) {
102 // Ensure custom default styles persist across setting and clearing text.
103 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
104 const SkColor color = SK_ColorRED;
105 render_text->SetColor(color);
106 render_text->SetStyle(BOLD, true);
107 render_text->SetStyle(UNDERLINE, false);
108 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
109 for (size_t i = 0; i < arraysize(cases); ++i) {
110 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color));
111 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true));
112 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false));
113 render_text->SetText(WideToUTF16(cases[i]));
115 // Ensure custom default styles can be applied after text has been set.
116 if (i == 1)
117 render_text->SetStyle(STRIKE, true);
118 if (i >= 1)
119 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true));
123 TEST_F(RenderTextTest, ApplyColorAndStyle) {
124 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
125 render_text->SetText(ASCIIToUTF16("012345678"));
127 // Apply a ranged color and style and check the resulting breaks.
128 render_text->ApplyColor(SK_ColorRED, Range(1, 4));
129 render_text->ApplyStyle(BOLD, true, Range(2, 5));
130 std::vector<std::pair<size_t, SkColor> > expected_color;
131 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK));
132 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED));
133 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK));
134 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
135 std::vector<std::pair<size_t, bool> > expected_style;
136 expected_style.push_back(std::pair<size_t, bool>(0, false));
137 expected_style.push_back(std::pair<size_t, bool>(2, true));
138 expected_style.push_back(std::pair<size_t, bool>(5, false));
139 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style));
141 // Ensure setting a color and style overrides the ranged colors and styles.
142 render_text->SetColor(SK_ColorBLUE);
143 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE));
144 render_text->SetStyle(BOLD, false);
145 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false));
147 // Apply a color and style over the text end and check the resulting breaks.
148 // (INT_MAX should be used instead of the text length for the range end)
149 const size_t text_length = render_text->text().length();
150 render_text->ApplyColor(SK_ColorRED, Range(0, text_length));
151 render_text->ApplyStyle(BOLD, true, Range(2, text_length));
152 std::vector<std::pair<size_t, SkColor> > expected_color_end;
153 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED));
154 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end));
155 std::vector<std::pair<size_t, bool> > expected_style_end;
156 expected_style_end.push_back(std::pair<size_t, bool>(0, false));
157 expected_style_end.push_back(std::pair<size_t, bool>(2, true));
158 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end));
160 // Ensure ranged values adjust to accommodate text length changes.
161 render_text->ApplyStyle(ITALIC, true, Range(0, 2));
162 render_text->ApplyStyle(ITALIC, true, Range(3, 6));
163 render_text->ApplyStyle(ITALIC, true, Range(7, text_length));
164 std::vector<std::pair<size_t, bool> > expected_italic;
165 expected_italic.push_back(std::pair<size_t, bool>(0, true));
166 expected_italic.push_back(std::pair<size_t, bool>(2, false));
167 expected_italic.push_back(std::pair<size_t, bool>(3, true));
168 expected_italic.push_back(std::pair<size_t, bool>(6, false));
169 expected_italic.push_back(std::pair<size_t, bool>(7, true));
170 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
172 // Changing the text should clear any breaks except for the first one.
173 render_text->SetText(ASCIIToUTF16("0123456"));
174 expected_italic.resize(1);
175 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
176 render_text->ApplyStyle(ITALIC, false, Range(2, 4));
177 render_text->SetText(ASCIIToUTF16("012345678"));
178 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
179 render_text->ApplyStyle(ITALIC, false, Range(0, 1));
180 render_text->SetText(ASCIIToUTF16("0123456"));
181 expected_italic.begin()->second = false;
182 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
183 render_text->ApplyStyle(ITALIC, true, Range(2, 4));
184 render_text->SetText(ASCIIToUTF16("012345678"));
185 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
187 // TODO(tmoniuszko): Enable when RenderTextMac::IsValidCursorIndex is
188 // implemented.
189 #if !defined(OS_MACOSX)
190 // Styles shouldn't be changed mid-grapheme.
191 render_text->SetText(WideToUTF16(
192 L"0" L"\x0915\x093f" L"1" L"\x0915\x093f" L"2"));
193 render_text->ApplyStyle(UNDERLINE, true, Range(2, 5));
194 std::vector<std::pair<size_t, bool> > expected_underline;
195 expected_underline.push_back(std::pair<size_t, bool>(0, false));
196 expected_underline.push_back(std::pair<size_t, bool>(1, true));
197 expected_underline.push_back(std::pair<size_t, bool>(6, false));
198 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsForTesting(
199 expected_underline));
200 #endif // OS_MACOSX
203 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
204 // does not implement this yet. http://crbug.com/131618
205 #if !defined(OS_MACOSX)
206 void TestVisualCursorMotionInObscuredField(RenderText* render_text,
207 const base::string16& text,
208 bool select) {
209 ASSERT_TRUE(render_text->obscured());
210 render_text->SetText(text);
211 int len = text.length();
212 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, select);
213 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
214 render_text->selection_model());
215 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, select);
216 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
217 for (int j = 1; j <= len; ++j) {
218 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, select);
219 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_BACKWARD),
220 render_text->selection_model());
222 for (int j = len - 1; j >= 0; --j) {
223 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, select);
224 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_FORWARD),
225 render_text->selection_model());
227 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, select);
228 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
229 render_text->selection_model());
230 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, select);
231 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
234 TEST_F(RenderTextTest, ObscuredText) {
235 const base::string16 seuss = ASCIIToUTF16("hop on pop");
236 const base::string16 no_seuss = ASCIIToUTF16("**********");
237 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
239 // GetLayoutText() returns asterisks when the obscured bit is set.
240 render_text->SetText(seuss);
241 render_text->SetObscured(true);
242 EXPECT_EQ(seuss, render_text->text());
243 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
244 render_text->SetObscured(false);
245 EXPECT_EQ(seuss, render_text->text());
246 EXPECT_EQ(seuss, render_text->GetLayoutText());
248 render_text->SetObscured(true);
250 // Surrogate pairs are counted as one code point.
251 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 0};
252 render_text->SetText(invalid_surrogates);
253 EXPECT_EQ(ASCIIToUTF16("**"), render_text->GetLayoutText());
254 const base::char16 valid_surrogates[] = {0xD800, 0xDC00, 0};
255 render_text->SetText(valid_surrogates);
256 EXPECT_EQ(ASCIIToUTF16("*"), render_text->GetLayoutText());
257 EXPECT_EQ(0U, render_text->cursor_position());
258 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
259 EXPECT_EQ(2U, render_text->cursor_position());
261 // Test index conversion and cursor validity with a valid surrogate pair.
262 EXPECT_EQ(0U, render_text->TextIndexToLayoutIndex(0U));
263 EXPECT_EQ(1U, render_text->TextIndexToLayoutIndex(1U));
264 EXPECT_EQ(1U, render_text->TextIndexToLayoutIndex(2U));
265 EXPECT_EQ(0U, render_text->LayoutIndexToTextIndex(0U));
266 EXPECT_EQ(2U, render_text->LayoutIndexToTextIndex(1U));
267 EXPECT_TRUE(render_text->IsValidCursorIndex(0U));
268 EXPECT_FALSE(render_text->IsValidCursorIndex(1U));
269 EXPECT_TRUE(render_text->IsValidCursorIndex(2U));
271 // FindCursorPosition() should not return positions between a surrogate pair.
272 render_text->SetDisplayRect(Rect(0, 0, 20, 20));
273 EXPECT_EQ(render_text->FindCursorPosition(Point(0, 0)).caret_pos(), 0U);
274 EXPECT_EQ(render_text->FindCursorPosition(Point(20, 0)).caret_pos(), 2U);
275 for (int x = -1; x <= 20; ++x) {
276 SelectionModel selection = render_text->FindCursorPosition(Point(x, 0));
277 EXPECT_TRUE(selection.caret_pos() == 0U || selection.caret_pos() == 2U);
280 // GetGlyphBounds() should yield the entire string bounds for text index 0.
281 EXPECT_EQ(render_text->GetStringSize().width(),
282 static_cast<int>(render_text->GetGlyphBounds(0U).length()));
284 // Cursoring is independent of underlying characters when text is obscured.
285 const wchar_t* const texts[] = {
286 kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl,
287 L"hop on pop", // Check LTR word boundaries.
288 L"\x05d0\x05d1 \x05d0\x05d2 \x05d1\x05d2", // Check RTL word boundaries.
290 for (size_t i = 0; i < arraysize(texts); ++i) {
291 base::string16 text = WideToUTF16(texts[i]);
292 TestVisualCursorMotionInObscuredField(render_text.get(), text, false);
293 TestVisualCursorMotionInObscuredField(render_text.get(), text, true);
297 TEST_F(RenderTextTest, RevealObscuredText) {
298 const base::string16 seuss = ASCIIToUTF16("hop on pop");
299 const base::string16 no_seuss = ASCIIToUTF16("**********");
300 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
302 render_text->SetText(seuss);
303 render_text->SetObscured(true);
304 EXPECT_EQ(seuss, render_text->text());
305 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
307 // Valid reveal index and new revealed index clears previous one.
308 render_text->RenderText::SetObscuredRevealIndex(0);
309 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetLayoutText());
310 render_text->RenderText::SetObscuredRevealIndex(1);
311 EXPECT_EQ(ASCIIToUTF16("*o********"), render_text->GetLayoutText());
312 render_text->RenderText::SetObscuredRevealIndex(2);
313 EXPECT_EQ(ASCIIToUTF16("**p*******"), render_text->GetLayoutText());
315 // Invalid reveal index.
316 render_text->RenderText::SetObscuredRevealIndex(-1);
317 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
318 render_text->RenderText::SetObscuredRevealIndex(seuss.length() + 1);
319 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
321 // SetObscured clears the revealed index.
322 render_text->RenderText::SetObscuredRevealIndex(0);
323 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetLayoutText());
324 render_text->SetObscured(false);
325 EXPECT_EQ(seuss, render_text->GetLayoutText());
326 render_text->SetObscured(true);
327 EXPECT_EQ(no_seuss, render_text->GetLayoutText());
329 // SetText clears the revealed index.
330 render_text->SetText(ASCIIToUTF16("new"));
331 EXPECT_EQ(ASCIIToUTF16("***"), render_text->GetLayoutText());
332 render_text->RenderText::SetObscuredRevealIndex(2);
333 EXPECT_EQ(ASCIIToUTF16("**w"), render_text->GetLayoutText());
334 render_text->SetText(ASCIIToUTF16("new longer"));
335 EXPECT_EQ(ASCIIToUTF16("**********"), render_text->GetLayoutText());
337 // Text with invalid surrogates.
338 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 'h', 'o', 'p', 0};
339 render_text->SetText(invalid_surrogates);
340 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetLayoutText());
341 render_text->RenderText::SetObscuredRevealIndex(0);
342 const base::char16 invalid_expect_0[] = {0xDC00, '*', '*', '*', '*', 0};
343 EXPECT_EQ(invalid_expect_0, render_text->GetLayoutText());
344 render_text->RenderText::SetObscuredRevealIndex(1);
345 const base::char16 invalid_expect_1[] = {'*', 0xD800, '*', '*', '*', 0};
346 EXPECT_EQ(invalid_expect_1, render_text->GetLayoutText());
347 render_text->RenderText::SetObscuredRevealIndex(2);
348 EXPECT_EQ(ASCIIToUTF16("**h**"), render_text->GetLayoutText());
350 // Text with valid surrogates before and after the reveal index.
351 const base::char16 valid_surrogates[] =
352 {0xD800, 0xDC00, 'h', 'o', 'p', 0xD800, 0xDC00, 0};
353 render_text->SetText(valid_surrogates);
354 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetLayoutText());
355 render_text->RenderText::SetObscuredRevealIndex(0);
356 const base::char16 valid_expect_0_and_1[] =
357 {0xD800, 0xDC00, '*', '*', '*', '*', 0};
358 EXPECT_EQ(valid_expect_0_and_1, render_text->GetLayoutText());
359 render_text->RenderText::SetObscuredRevealIndex(1);
360 EXPECT_EQ(valid_expect_0_and_1, render_text->GetLayoutText());
361 render_text->RenderText::SetObscuredRevealIndex(2);
362 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text->GetLayoutText());
363 render_text->RenderText::SetObscuredRevealIndex(5);
364 const base::char16 valid_expect_5_and_6[] =
365 {'*', '*', '*', '*', 0xD800, 0xDC00, 0};
366 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText());
367 render_text->RenderText::SetObscuredRevealIndex(6);
368 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText());
371 TEST_F(RenderTextTest, ElidedText) {
372 // TODO(skanuj) : Add more test cases for following
373 // - RenderText styles.
374 // - Cross interaction of truncate, elide and obscure.
375 // - ElideText tests from text_elider.cc.
376 struct {
377 const wchar_t* text;
378 const wchar_t* layout_text;
379 const bool elision_expected;
380 } cases[] = {
381 // Strings shorter than the elision width should be laid out in full.
382 { L"", L"" , false },
383 { L"M", L"" , false },
384 { L" . ", L" . " , false },
385 { kWeak, kWeak , false },
386 { kLtr, kLtr , false },
387 { kLtrRtl, kLtrRtl , false },
388 { kLtrRtlLtr, kLtrRtlLtr, false },
389 { kRtl, kRtl , false },
390 { kRtlLtr, kRtlLtr , false },
391 { kRtlLtrRtl, kRtlLtrRtl, false },
392 // Strings as long as the elision width should be laid out in full.
393 { L"012ab", L"012ab" , false },
394 // Long strings should be elided with an ellipsis appended at the end.
395 { L"012abc", L"012a\x2026", true },
396 { L"012ab" L"\x5d0\x5d1", L"012a\x2026", true },
397 { L"012a" L"\x5d1" L"b", L"012a\x2026", true },
398 // No RLM marker added as digits (012) have weak directionality.
399 { L"01" L"\x5d0\x5d1\x5d2", L"01\x5d0\x5d1\x2026", true },
400 // RLM marker added as "ab" have strong LTR directionality.
401 { L"ab" L"\x5d0\x5d1\x5d2", L"ab\x5d0\x5d1\x2026\x200f", true },
402 // Complex script is not handled. In this example, the "\x0915\x093f" is a
403 // compound glyph, but only half of it is elided.
404 { L"0123\x0915\x093f", L"0123\x0915\x2026", true },
405 // Surrogate pairs should be elided reasonably enough.
406 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8", false },
407 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x2026" , true },
408 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x2026" , true },
409 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x2026\x200E" , true },
410 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" , true },
413 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
414 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
415 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
417 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
418 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
419 render_text->SetElideBehavior(ELIDE_TAIL);
421 for (size_t i = 0; i < arraysize(cases); i++) {
422 // Compute expected width
423 expected_render_text->SetText(WideToUTF16(cases[i].layout_text));
424 int expected_width = expected_render_text->GetContentWidth();
426 base::string16 input = WideToUTF16(cases[i].text);
427 // Extend the input text to ensure that it is wider than the layout_text,
428 // and so it will get elided.
429 if (cases[i].elision_expected)
430 input.append(WideToUTF16(L" MMMMMMMMMMM"));
432 render_text->SetText(input);
433 render_text->SetDisplayRect(Rect(0, 0, expected_width, 100));
434 EXPECT_EQ(input, render_text->text())
435 << "->For case " << i << ": " << cases[i].text << "\n";
436 EXPECT_EQ(WideToUTF16(cases[i].layout_text), render_text->GetLayoutText())
437 << "->For case " << i << ": " << cases[i].text << "\n";
438 expected_render_text->SetText(base::string16());
442 TEST_F(RenderTextTest, ElidedObscuredText) {
443 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
444 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
445 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
446 expected_render_text->SetText(WideToUTF16(L"**\x2026"));
448 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
449 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
450 render_text->SetElideBehavior(ELIDE_TAIL);
451 render_text->SetDisplayRect(
452 Rect(0, 0, expected_render_text->GetContentWidth(), 100));
453 render_text->SetObscured(true);
454 render_text->SetText(WideToUTF16(L"abcdef"));
455 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
456 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetLayoutText());
459 TEST_F(RenderTextTest, TruncatedText) {
460 struct {
461 const wchar_t* text;
462 const wchar_t* layout_text;
463 } cases[] = {
464 // Strings shorter than the truncation length should be laid out in full.
465 { L"", L"" },
466 { kWeak, kWeak },
467 { kLtr, kLtr },
468 { kLtrRtl, kLtrRtl },
469 { kLtrRtlLtr, kLtrRtlLtr },
470 { kRtl, kRtl },
471 { kRtlLtr, kRtlLtr },
472 { kRtlLtrRtl, kRtlLtrRtl },
473 // Strings as long as the truncation length should be laid out in full.
474 { L"01234", L"01234" },
475 // Long strings should be truncated with an ellipsis appended at the end.
476 { L"012345", L"0123\x2026" },
477 { L"012" L" . ", L"012 \x2026" },
478 { L"012" L"abc", L"012a\x2026" },
479 { L"012" L"a" L"\x5d0\x5d1", L"012a\x2026" },
480 { L"012" L"a" L"\x5d1" L"b", L"012a\x2026" },
481 { L"012" L"\x5d0\x5d1\x5d2", L"012\x5d0\x2026" },
482 { L"012" L"\x5d0\x5d1" L"a", L"012\x5d0\x2026" },
483 { L"012" L"\x5d0" L"a" L"\x5d1", L"012\x5d0\x2026" },
484 // Surrogate pairs should be truncated reasonably enough.
485 { L"0123\x0915\x093f", L"0123\x2026" },
486 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8" },
487 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x05bc\x2026" },
488 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x05e9\x2026" },
489 { L"0123\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
490 { L"01234\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
491 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" },
494 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
495 render_text->set_truncate_length(5);
496 for (size_t i = 0; i < arraysize(cases); i++) {
497 render_text->SetText(WideToUTF16(cases[i].text));
498 EXPECT_EQ(WideToUTF16(cases[i].text), render_text->text());
499 EXPECT_EQ(WideToUTF16(cases[i].layout_text), render_text->GetLayoutText())
500 << "For case " << i << ": " << cases[i].text;
504 TEST_F(RenderTextTest, TruncatedObscuredText) {
505 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
506 render_text->set_truncate_length(3);
507 render_text->SetObscured(true);
508 render_text->SetText(WideToUTF16(L"abcdef"));
509 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
510 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetLayoutText());
513 TEST_F(RenderTextTest, TruncatedCursorMovementLTR) {
514 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
515 render_text->set_truncate_length(2);
516 render_text->SetText(WideToUTF16(L"abcd"));
518 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
519 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
520 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
521 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
522 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
524 std::vector<SelectionModel> expected;
525 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
526 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
527 // The cursor hops over the ellipsis and elided text to the line end.
528 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
529 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
530 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
532 expected.clear();
533 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
534 // The cursor hops over the elided text to preceeding text.
535 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
536 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
537 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
538 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
541 TEST_F(RenderTextTest, TruncatedCursorMovementRTL) {
542 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
543 render_text->set_truncate_length(2);
544 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3"));
546 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
547 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
548 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
549 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
550 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
552 std::vector<SelectionModel> expected;
553 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
554 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
555 // The cursor hops over the ellipsis and elided text to the line end.
556 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
557 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
558 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
560 expected.clear();
561 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
562 // The cursor hops over the elided text to preceeding text.
563 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
564 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
565 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
566 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
569 TEST_F(RenderTextTest, GetTextDirection) {
570 struct {
571 const wchar_t* text;
572 const base::i18n::TextDirection text_direction;
573 } cases[] = {
574 // Blank strings and those with no/weak directionality default to LTR.
575 { L"", base::i18n::LEFT_TO_RIGHT },
576 { kWeak, base::i18n::LEFT_TO_RIGHT },
577 // Strings that begin with strong LTR characters.
578 { kLtr, base::i18n::LEFT_TO_RIGHT },
579 { kLtrRtl, base::i18n::LEFT_TO_RIGHT },
580 { kLtrRtlLtr, base::i18n::LEFT_TO_RIGHT },
581 // Strings that begin with strong RTL characters.
582 { kRtl, base::i18n::RIGHT_TO_LEFT },
583 { kRtlLtr, base::i18n::RIGHT_TO_LEFT },
584 { kRtlLtrRtl, base::i18n::RIGHT_TO_LEFT },
587 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
588 const bool was_rtl = base::i18n::IsRTL();
590 for (size_t i = 0; i < 2; ++i) {
591 // Toggle the application default text direction (to try each direction).
592 SetRTL(!base::i18n::IsRTL());
593 const base::i18n::TextDirection ui_direction = base::i18n::IsRTL() ?
594 base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
596 // Ensure that directionality modes yield the correct text directions.
597 for (size_t j = 0; j < arraysize(cases); j++) {
598 render_text->SetText(WideToUTF16(cases[j].text));
599 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
600 EXPECT_EQ(render_text->GetTextDirection(), cases[j].text_direction);
601 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_UI);
602 EXPECT_EQ(render_text->GetTextDirection(), ui_direction);
603 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_LTR);
604 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::LEFT_TO_RIGHT);
605 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_RTL);
606 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::RIGHT_TO_LEFT);
610 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
612 // Ensure that text changes update the direction for DIRECTIONALITY_FROM_TEXT.
613 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
614 render_text->SetText(WideToUTF16(kLtr));
615 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::LEFT_TO_RIGHT);
616 render_text->SetText(WideToUTF16(kRtl));
617 EXPECT_EQ(render_text->GetTextDirection(), base::i18n::RIGHT_TO_LEFT);
620 TEST_F(RenderTextTest, MoveCursorLeftRightInLtr) {
621 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
623 // Pure LTR.
624 render_text->SetText(ASCIIToUTF16("abc"));
625 // |expected| saves the expected SelectionModel when moving cursor from left
626 // to right.
627 std::vector<SelectionModel> expected;
628 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
629 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
630 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
631 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
632 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
633 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
635 expected.clear();
636 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
637 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
638 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
639 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
640 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
641 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
644 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtl) {
645 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
646 // LTR-RTL
647 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
648 // The last one is the expected END position.
649 std::vector<SelectionModel> expected;
650 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
651 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
652 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
653 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
654 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
655 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
656 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
657 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
658 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
660 expected.clear();
661 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
662 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
663 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
664 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
665 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
666 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
667 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
668 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
669 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
672 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtlLtr) {
673 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
674 // LTR-RTL-LTR.
675 render_text->SetText(WideToUTF16(L"a" L"\x05d1" L"b"));
676 std::vector<SelectionModel> expected;
677 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
678 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
679 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
680 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
681 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
682 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
684 expected.clear();
685 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
686 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
687 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
688 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
689 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
690 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
693 TEST_F(RenderTextTest, MoveCursorLeftRightInRtl) {
694 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
695 // Pure RTL.
696 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2"));
697 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
698 std::vector<SelectionModel> expected;
700 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
701 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
702 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
703 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
704 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
705 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
707 expected.clear();
709 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
710 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
711 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
712 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
713 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
714 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
717 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtr) {
718 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
719 // RTL-LTR
720 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2" L"abc"));
721 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
722 std::vector<SelectionModel> expected;
723 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
724 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
725 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
726 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
727 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
728 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
729 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
730 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
731 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
733 expected.clear();
734 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
735 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
736 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
737 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
738 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
739 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
740 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
741 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
742 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
745 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtrRtl) {
746 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
747 // RTL-LTR-RTL.
748 render_text->SetText(WideToUTF16(L"\x05d0" L"a" L"\x05d1"));
749 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
750 std::vector<SelectionModel> expected;
751 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
752 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
753 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
754 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
755 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
756 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
758 expected.clear();
759 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
760 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
761 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
762 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
763 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
764 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
767 // TODO(xji): temporarily disable in platform Win since the complex script
768 // characters turned into empty square due to font regression. So, not able
769 // to test 2 characters belong to the same grapheme.
770 #if defined(OS_LINUX)
771 TEST_F(RenderTextTest, MoveCursorLeftRight_ComplexScript) {
772 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
774 render_text->SetText(WideToUTF16(L"\x0915\x093f\x0915\x094d\x0915"));
775 EXPECT_EQ(0U, render_text->cursor_position());
776 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
777 EXPECT_EQ(2U, render_text->cursor_position());
778 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
779 EXPECT_EQ(4U, render_text->cursor_position());
780 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
781 EXPECT_EQ(5U, render_text->cursor_position());
782 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
783 EXPECT_EQ(5U, render_text->cursor_position());
785 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
786 EXPECT_EQ(4U, render_text->cursor_position());
787 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
788 EXPECT_EQ(2U, render_text->cursor_position());
789 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
790 EXPECT_EQ(0U, render_text->cursor_position());
791 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
792 EXPECT_EQ(0U, render_text->cursor_position());
794 #endif
796 // TODO(ckocagil): Enable for RenderTextHarfBuzz. http://crbug.com/383265
797 #if defined(OS_MACOSX)
798 TEST_F(RenderTextTest, MoveCursorLeftRight_MeiryoUILigatures) {
799 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
800 // Meiryo UI uses single-glyph ligatures for 'ff' and 'ffi', but each letter
801 // (code point) has unique bounds, so mid-glyph cursoring should be possible.
802 render_text->SetFontList(FontList("Meiryo UI, 12px"));
803 render_text->SetText(WideToUTF16(L"ff ffi"));
804 EXPECT_EQ(0U, render_text->cursor_position());
805 for (size_t i = 0; i < render_text->text().length(); ++i) {
806 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
807 EXPECT_EQ(i + 1, render_text->cursor_position());
809 EXPECT_EQ(6U, render_text->cursor_position());
811 #endif // defined(OS_MACOSX)
813 TEST_F(RenderTextTest, GraphemePositions) {
814 // LTR 2-character grapheme, LTR abc, LTR 2-character grapheme.
815 const base::string16 kText1 =
816 WideToUTF16(L"\x0915\x093f" L"abc" L"\x0915\x093f");
818 // LTR ab, LTR 2-character grapheme, LTR cd.
819 const base::string16 kText2 = WideToUTF16(L"ab" L"\x0915\x093f" L"cd");
821 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
822 // two characters forming the surrogate pair 0x0001D11E.
823 const std::string kSurrogate = "\xF0\x9D\x84\x9E";
825 // LTR ab, UTF16 surrogate pair, LTR cd.
826 const base::string16 kText3 = UTF8ToUTF16("ab" + kSurrogate + "cd");
828 struct {
829 base::string16 text;
830 size_t index;
831 size_t expected_previous;
832 size_t expected_next;
833 } cases[] = {
834 { base::string16(), 0, 0, 0 },
835 { base::string16(), 1, 0, 0 },
836 { base::string16(), 50, 0, 0 },
837 { kText1, 0, 0, 2 },
838 { kText1, 1, 0, 2 },
839 { kText1, 2, 0, 3 },
840 { kText1, 3, 2, 4 },
841 { kText1, 4, 3, 5 },
842 { kText1, 5, 4, 7 },
843 { kText1, 6, 5, 7 },
844 { kText1, 7, 5, 7 },
845 { kText1, 8, 7, 7 },
846 { kText1, 50, 7, 7 },
847 { kText2, 0, 0, 1 },
848 { kText2, 1, 0, 2 },
849 { kText2, 2, 1, 4 },
850 { kText2, 3, 2, 4 },
851 { kText2, 4, 2, 5 },
852 { kText2, 5, 4, 6 },
853 { kText2, 6, 5, 6 },
854 { kText2, 7, 6, 6 },
855 { kText2, 50, 6, 6 },
856 { kText3, 0, 0, 1 },
857 { kText3, 1, 0, 2 },
858 { kText3, 2, 1, 4 },
859 { kText3, 3, 2, 4 },
860 { kText3, 4, 2, 5 },
861 { kText3, 5, 4, 6 },
862 { kText3, 6, 5, 6 },
863 { kText3, 7, 6, 6 },
864 { kText3, 50, 6, 6 },
867 #if defined(OS_WIN)
868 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
869 if (base::win::GetVersion() < base::win::VERSION_VISTA)
870 return;
871 #endif
873 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
874 for (size_t i = 0; i < arraysize(cases); i++) {
875 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
876 render_text->SetText(cases[i].text);
878 size_t next = render_text->IndexOfAdjacentGrapheme(cases[i].index,
879 CURSOR_FORWARD);
880 EXPECT_EQ(cases[i].expected_next, next);
881 EXPECT_TRUE(render_text->IsValidCursorIndex(next));
883 size_t previous = render_text->IndexOfAdjacentGrapheme(cases[i].index,
884 CURSOR_BACKWARD);
885 EXPECT_EQ(cases[i].expected_previous, previous);
886 EXPECT_TRUE(render_text->IsValidCursorIndex(previous));
890 TEST_F(RenderTextTest, MidGraphemeSelectionBounds) {
891 #if defined(OS_WIN)
892 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
893 if (base::win::GetVersion() < base::win::VERSION_VISTA)
894 return;
895 #endif
897 // Test that selection bounds may be set amid multi-character graphemes.
898 const base::string16 kHindi = WideToUTF16(L"\x0915\x093f");
899 const base::string16 kThai = WideToUTF16(L"\x0e08\x0e33");
900 const base::string16 cases[] = { kHindi, kThai };
902 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
903 for (size_t i = 0; i < arraysize(cases); i++) {
904 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
905 render_text->SetText(cases[i]);
906 EXPECT_TRUE(render_text->IsValidLogicalIndex(1));
907 #if !defined(OS_MACOSX)
908 EXPECT_FALSE(render_text->IsValidCursorIndex(1));
909 #endif
910 EXPECT_TRUE(render_text->SelectRange(Range(2, 1)));
911 EXPECT_EQ(Range(2, 1), render_text->selection());
912 EXPECT_EQ(1U, render_text->cursor_position());
913 // Although selection bounds may be set within a multi-character grapheme,
914 // cursor movement (e.g. via arrow key) should avoid those indices.
915 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
916 EXPECT_EQ(0U, render_text->cursor_position());
917 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
918 EXPECT_EQ(2U, render_text->cursor_position());
922 TEST_F(RenderTextTest, FindCursorPosition) {
923 const wchar_t* kTestStrings[] = { kLtrRtl, kLtrRtlLtr, kRtlLtr, kRtlLtrRtl };
924 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
925 render_text->SetDisplayRect(Rect(0, 0, 100, 20));
926 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
927 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS "]", i));
928 render_text->SetText(WideToUTF16(kTestStrings[i]));
929 for(size_t j = 0; j < render_text->text().length(); ++j) {
930 const Range range(render_text->GetGlyphBounds(j));
931 // Test a point just inside the leading edge of the glyph bounds.
932 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1;
933 EXPECT_EQ(j, render_text->FindCursorPosition(Point(x, 0)).caret_pos());
938 TEST_F(RenderTextTest, EdgeSelectionModels) {
939 // Simple Latin text.
940 const base::string16 kLatin = WideToUTF16(L"abc");
941 // LTR 2-character grapheme.
942 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f");
943 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
944 const base::string16 kHindiLatin =
945 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f");
946 // RTL 2-character grapheme.
947 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8");
948 // RTL 2-character grapheme, LTR a, RTL 2-character grapheme.
949 const base::string16 kHebrewLatin =
950 WideToUTF16(L"\x05e0\x05b8" L"a" L"\x05e0\x05b8");
952 struct {
953 base::string16 text;
954 base::i18n::TextDirection expected_text_direction;
955 } cases[] = {
956 { base::string16(), base::i18n::LEFT_TO_RIGHT },
957 { kLatin, base::i18n::LEFT_TO_RIGHT },
958 { kLTRGrapheme, base::i18n::LEFT_TO_RIGHT },
959 { kHindiLatin, base::i18n::LEFT_TO_RIGHT },
960 { kRTLGrapheme, base::i18n::RIGHT_TO_LEFT },
961 { kHebrewLatin, base::i18n::RIGHT_TO_LEFT },
964 #if defined(OS_WIN)
965 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
966 if (base::win::GetVersion() < base::win::VERSION_VISTA)
967 return;
968 #endif
970 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
971 for (size_t i = 0; i < arraysize(cases); i++) {
972 render_text->SetText(cases[i].text);
973 bool ltr = (cases[i].expected_text_direction == base::i18n::LEFT_TO_RIGHT);
975 SelectionModel start_edge =
976 render_text->EdgeSelectionModel(ltr ? CURSOR_LEFT : CURSOR_RIGHT);
977 EXPECT_EQ(start_edge, SelectionModel(0, CURSOR_BACKWARD));
979 SelectionModel end_edge =
980 render_text->EdgeSelectionModel(ltr ? CURSOR_RIGHT : CURSOR_LEFT);
981 EXPECT_EQ(end_edge, SelectionModel(cases[i].text.length(), CURSOR_FORWARD));
985 TEST_F(RenderTextTest, SelectAll) {
986 const wchar_t* const cases[] =
987 { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl };
989 // Ensure that SelectAll respects the |reversed| argument regardless of
990 // application locale and text content directionality.
991 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
992 const SelectionModel expected_reversed(Range(3, 0), CURSOR_FORWARD);
993 const SelectionModel expected_forwards(Range(0, 3), CURSOR_BACKWARD);
994 const bool was_rtl = base::i18n::IsRTL();
996 for (size_t i = 0; i < 2; ++i) {
997 SetRTL(!base::i18n::IsRTL());
998 // Test that an empty string produces an empty selection model.
999 render_text->SetText(base::string16());
1000 EXPECT_EQ(render_text->selection_model(), SelectionModel());
1002 // Test the weak, LTR, RTL, and Bidi string cases.
1003 for (size_t j = 0; j < arraysize(cases); j++) {
1004 render_text->SetText(WideToUTF16(cases[j]));
1005 render_text->SelectAll(false);
1006 EXPECT_EQ(render_text->selection_model(), expected_forwards);
1007 render_text->SelectAll(true);
1008 EXPECT_EQ(render_text->selection_model(), expected_reversed);
1012 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1015 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) {
1016 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1017 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
1018 // Left arrow on select ranging (6, 4).
1019 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1020 EXPECT_EQ(Range(6), render_text->selection());
1021 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1022 EXPECT_EQ(Range(4), render_text->selection());
1023 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1024 EXPECT_EQ(Range(5), render_text->selection());
1025 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1026 EXPECT_EQ(Range(6), render_text->selection());
1027 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1028 EXPECT_EQ(Range(6, 5), render_text->selection());
1029 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1030 EXPECT_EQ(Range(6, 4), render_text->selection());
1031 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1032 EXPECT_EQ(Range(6), render_text->selection());
1034 // Right arrow on select ranging (4, 6).
1035 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1036 EXPECT_EQ(Range(0), render_text->selection());
1037 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1038 EXPECT_EQ(Range(1), render_text->selection());
1039 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1040 EXPECT_EQ(Range(2), render_text->selection());
1041 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1042 EXPECT_EQ(Range(3), render_text->selection());
1043 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1044 EXPECT_EQ(Range(5), render_text->selection());
1045 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1046 EXPECT_EQ(Range(4), render_text->selection());
1047 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1048 EXPECT_EQ(Range(4, 5), render_text->selection());
1049 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1050 EXPECT_EQ(Range(4, 6), render_text->selection());
1051 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1052 EXPECT_EQ(Range(4), render_text->selection());
1055 TEST_F(RenderTextTest, CenteredDisplayOffset) {
1056 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1057 render_text->SetText(ASCIIToUTF16("abcdefghij"));
1058 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1060 const int kEnlargement = 10;
1061 const int content_width = render_text->GetContentWidth();
1062 Rect display_rect(0, 0, content_width / 2,
1063 render_text->font_list().GetHeight());
1064 render_text->SetDisplayRect(display_rect);
1066 // Move the cursor to the beginning of the text and, by checking the cursor
1067 // bounds, make sure no empty space is to the left of the text.
1068 render_text->SetCursorPosition(0);
1069 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1071 // Widen the display rect and, by checking the cursor bounds, make sure no
1072 // empty space is introduced to the left of the text.
1073 display_rect.Inset(0, 0, -kEnlargement, 0);
1074 render_text->SetDisplayRect(display_rect);
1075 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1077 // Move the cursor to the end of the text and, by checking the cursor bounds,
1078 // make sure no empty space is to the right of the text.
1079 render_text->SetCursorPosition(render_text->text().length());
1080 EXPECT_EQ(display_rect.right(),
1081 render_text->GetUpdatedCursorBounds().right());
1083 // Widen the display rect and, by checking the cursor bounds, make sure no
1084 // empty space is introduced to the right of the text.
1085 display_rect.Inset(0, 0, -kEnlargement, 0);
1086 render_text->SetDisplayRect(display_rect);
1087 EXPECT_EQ(display_rect.right(),
1088 render_text->GetUpdatedCursorBounds().right());
1090 #endif // !defined(OS_MACOSX)
1092 // TODO(xji): Make these work on Windows.
1093 #if defined(OS_LINUX)
1094 void MoveLeftRightByWordVerifier(RenderText* render_text,
1095 const wchar_t* str) {
1096 render_text->SetText(WideToUTF16(str));
1098 // Test moving by word from left ro right.
1099 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1100 bool first_word = true;
1101 while (true) {
1102 // First, test moving by word from a word break position, such as from
1103 // "|abc def" to "abc| def".
1104 SelectionModel start = render_text->selection_model();
1105 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1106 SelectionModel end = render_text->selection_model();
1107 if (end == start) // reach the end.
1108 break;
1110 // For testing simplicity, each word is a 3-character word.
1111 int num_of_character_moves = first_word ? 3 : 4;
1112 first_word = false;
1113 render_text->MoveCursorTo(start);
1114 for (int j = 0; j < num_of_character_moves; ++j)
1115 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1116 EXPECT_EQ(end, render_text->selection_model());
1118 // Then, test moving by word from positions inside the word, such as from
1119 // "a|bc def" to "abc| def", and from "ab|c def" to "abc| def".
1120 for (int j = 1; j < num_of_character_moves; ++j) {
1121 render_text->MoveCursorTo(start);
1122 for (int k = 0; k < j; ++k)
1123 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1124 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1125 EXPECT_EQ(end, render_text->selection_model());
1129 // Test moving by word from right to left.
1130 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1131 first_word = true;
1132 while (true) {
1133 SelectionModel start = render_text->selection_model();
1134 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1135 SelectionModel end = render_text->selection_model();
1136 if (end == start) // reach the end.
1137 break;
1139 int num_of_character_moves = first_word ? 3 : 4;
1140 first_word = false;
1141 render_text->MoveCursorTo(start);
1142 for (int j = 0; j < num_of_character_moves; ++j)
1143 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1144 EXPECT_EQ(end, render_text->selection_model());
1146 for (int j = 1; j < num_of_character_moves; ++j) {
1147 render_text->MoveCursorTo(start);
1148 for (int k = 0; k < j; ++k)
1149 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1150 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1151 EXPECT_EQ(end, render_text->selection_model());
1156 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText) {
1157 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1159 // For testing simplicity, each word is a 3-character word.
1160 std::vector<const wchar_t*> test;
1161 test.push_back(L"abc");
1162 test.push_back(L"abc def");
1163 test.push_back(L"\x05E1\x05E2\x05E3");
1164 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1165 test.push_back(L"abc \x05E1\x05E2\x05E3");
1166 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1167 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1168 L" \x05E7\x05E8\x05E9");
1170 test.push_back(L"abc \x05E1\x05E2\x05E3 hij");
1171 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 hij opq");
1172 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1173 L" \x05E7\x05E8\x05E9" L" opq rst uvw");
1175 test.push_back(L"\x05E1\x05E2\x05E3 abc");
1176 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 abc def");
1177 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 \x05E7\x05E8\x05E9"
1178 L" abc def hij");
1180 test.push_back(L"\x05D1\x05D2\x05D3 abc \x05E1\x05E2\x05E3");
1181 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 abc def"
1182 L" \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1183 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 \x05D7\x05D8\x05D9"
1184 L" abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1185 L" \x05E7\x05E8\x05E9");
1187 for (size_t i = 0; i < test.size(); ++i)
1188 MoveLeftRightByWordVerifier(render_text.get(), test[i]);
1191 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText_TestEndOfText) {
1192 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1194 render_text->SetText(WideToUTF16(L"ab\x05E1"));
1195 // Moving the cursor by word from "abC|" to the left should return "|abC".
1196 // But since end of text is always treated as a word break, it returns
1197 // position "ab|C".
1198 // TODO(xji): Need to make it work as expected.
1199 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1200 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1201 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1203 // Moving the cursor by word from "|abC" to the right returns "abC|".
1204 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1205 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1206 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1208 render_text->SetText(WideToUTF16(L"\x05E1\x05E2" L"a"));
1209 // For logical text "BCa", moving the cursor by word from "aCB|" to the left
1210 // returns "|aCB".
1211 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1212 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1213 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1215 // Moving the cursor by word from "|aCB" to the right should return "aCB|".
1216 // But since end of text is always treated as a word break, it returns
1217 // position "a|CB".
1218 // TODO(xji): Need to make it work as expected.
1219 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1220 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1221 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1224 TEST_F(RenderTextTest, MoveLeftRightByWordInTextWithMultiSpaces) {
1225 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1226 render_text->SetText(WideToUTF16(L"abc def"));
1227 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1228 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1229 EXPECT_EQ(11U, render_text->cursor_position());
1231 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1232 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1233 EXPECT_EQ(0U, render_text->cursor_position());
1236 TEST_F(RenderTextTest, MoveLeftRightByWordInChineseText) {
1237 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1238 render_text->SetText(WideToUTF16(L"\x6211\x4EEC\x53BB\x516C\x56ED\x73A9"));
1239 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1240 EXPECT_EQ(0U, render_text->cursor_position());
1241 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1242 EXPECT_EQ(2U, render_text->cursor_position());
1243 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1244 EXPECT_EQ(3U, render_text->cursor_position());
1245 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1246 EXPECT_EQ(5U, render_text->cursor_position());
1247 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1248 EXPECT_EQ(6U, render_text->cursor_position());
1249 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1250 EXPECT_EQ(6U, render_text->cursor_position());
1252 #endif
1254 TEST_F(RenderTextTest, StringSizeSanity) {
1255 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1256 render_text->SetText(UTF8ToUTF16("Hello World"));
1257 const Size string_size = render_text->GetStringSize();
1258 EXPECT_GT(string_size.width(), 0);
1259 EXPECT_GT(string_size.height(), 0);
1262 TEST_F(RenderTextTest, StringSizeLongStrings) {
1263 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1264 Size previous_string_size;
1265 for (size_t length = 10; length < 1000000; length *= 10) {
1266 render_text->SetText(base::string16(length, 'a'));
1267 const Size string_size = render_text->GetStringSize();
1268 EXPECT_GT(string_size.width(), previous_string_size.width());
1269 EXPECT_GT(string_size.height(), 0);
1270 previous_string_size = string_size;
1274 // TODO(asvitkine): This test fails because PlatformFontMac uses point font
1275 // sizes instead of pixel sizes like other implementations.
1276 #if !defined(OS_MACOSX)
1277 TEST_F(RenderTextTest, StringSizeEmptyString) {
1278 // Ascent and descent of Arial and Symbol are different on most platforms.
1279 const FontList font_list("Arial,Symbol, 16px");
1280 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1281 render_text->SetFontList(font_list);
1282 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight()));
1284 // The empty string respects FontList metrics for non-zero height
1285 // and baseline.
1286 render_text->SetText(base::string16());
1287 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1288 EXPECT_EQ(0, render_text->GetStringSize().width());
1289 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1291 render_text->SetText(UTF8ToUTF16(" "));
1292 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1293 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1295 #endif // !defined(OS_MACOSX)
1297 TEST_F(RenderTextTest, StringSizeRespectsFontListMetrics) {
1298 // Check that Arial and Symbol have different font metrics.
1299 Font arial_font("Arial", 16);
1300 ASSERT_EQ("arial",
1301 base::StringToLowerASCII(arial_font.GetActualFontNameForTesting()));
1302 Font symbol_font("Symbol", 16);
1303 ASSERT_EQ("symbol",
1304 base::StringToLowerASCII(
1305 symbol_font.GetActualFontNameForTesting()));
1306 EXPECT_NE(arial_font.GetHeight(), symbol_font.GetHeight());
1307 EXPECT_NE(arial_font.GetBaseline(), symbol_font.GetBaseline());
1308 // "a" should be rendered with Arial, not with Symbol.
1309 const char* arial_font_text = "a";
1310 // "®" (registered trademark symbol) should be rendered with Symbol,
1311 // not with Arial.
1312 const char* symbol_font_text = "\xC2\xAE";
1314 Font smaller_font = arial_font;
1315 Font larger_font = symbol_font;
1316 const char* smaller_font_text = arial_font_text;
1317 const char* larger_font_text = symbol_font_text;
1318 if (symbol_font.GetHeight() < arial_font.GetHeight() &&
1319 symbol_font.GetBaseline() < arial_font.GetBaseline()) {
1320 std::swap(smaller_font, larger_font);
1321 std::swap(smaller_font_text, larger_font_text);
1323 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight());
1324 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline());
1326 // Check |smaller_font_text| is rendered with the smaller font.
1327 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1328 render_text->SetText(UTF8ToUTF16(smaller_font_text));
1329 render_text->SetFontList(FontList(smaller_font));
1330 render_text->SetDisplayRect(Rect(0, 0, 0,
1331 render_text->font_list().GetHeight()));
1332 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height());
1333 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline());
1335 // Layout the same text with mixed fonts. The text should be rendered with
1336 // the smaller font, but the height and baseline are determined with the
1337 // metrics of the font list, which is equal to the larger font.
1338 std::vector<Font> fonts;
1339 fonts.push_back(smaller_font); // The primary font is the smaller font.
1340 fonts.push_back(larger_font);
1341 const FontList font_list(fonts);
1342 render_text->SetFontList(font_list);
1343 render_text->SetDisplayRect(Rect(0, 0, 0,
1344 render_text->font_list().GetHeight()));
1345 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height());
1346 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline());
1347 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1348 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1351 TEST_F(RenderTextTest, SetFontList) {
1352 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1353 render_text->SetFontList(FontList("Arial,Symbol, 13px"));
1354 const std::vector<Font>& fonts = render_text->font_list().GetFonts();
1355 ASSERT_EQ(2U, fonts.size());
1356 EXPECT_EQ("Arial", fonts[0].GetFontName());
1357 EXPECT_EQ("Symbol", fonts[1].GetFontName());
1358 EXPECT_EQ(13, render_text->font_list().GetFontSize());
1361 TEST_F(RenderTextTest, StringSizeBoldWidth) {
1362 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1363 render_text->SetText(UTF8ToUTF16("Hello World"));
1365 const int plain_width = render_text->GetStringSize().width();
1366 EXPECT_GT(plain_width, 0);
1368 // Apply a bold style and check that the new width is greater.
1369 render_text->SetStyle(BOLD, true);
1370 const int bold_width = render_text->GetStringSize().width();
1371 EXPECT_GT(bold_width, plain_width);
1373 // Now, apply a plain style over the first word only.
1374 render_text->ApplyStyle(BOLD, false, Range(0, 5));
1375 const int plain_bold_width = render_text->GetStringSize().width();
1376 EXPECT_GT(plain_bold_width, plain_width);
1377 EXPECT_LT(plain_bold_width, bold_width);
1380 TEST_F(RenderTextTest, StringSizeHeight) {
1381 base::string16 cases[] = {
1382 WideToUTF16(L"Hello World!"), // English
1383 WideToUTF16(L"\x6328\x62f6"), // Japanese
1384 WideToUTF16(L"\x0915\x093f"), // Hindi
1385 WideToUTF16(L"\x05e0\x05b8"), // Hebrew
1388 const FontList default_font_list;
1389 const FontList& larger_font_list = default_font_list.DeriveWithSizeDelta(24);
1390 EXPECT_GT(larger_font_list.GetHeight(), default_font_list.GetHeight());
1392 for (size_t i = 0; i < arraysize(cases); i++) {
1393 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1394 render_text->SetFontList(default_font_list);
1395 render_text->SetText(cases[i]);
1397 const int height1 = render_text->GetStringSize().height();
1398 EXPECT_GT(height1, 0);
1400 // Check that setting the larger font increases the height.
1401 render_text->SetFontList(larger_font_list);
1402 const int height2 = render_text->GetStringSize().height();
1403 EXPECT_GT(height2, height1);
1407 TEST_F(RenderTextTest, GetBaselineSanity) {
1408 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1409 render_text->SetText(UTF8ToUTF16("Hello World"));
1410 const int baseline = render_text->GetBaseline();
1411 EXPECT_GT(baseline, 0);
1414 TEST_F(RenderTextTest, CursorBoundsInReplacementMode) {
1415 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1416 render_text->SetText(ASCIIToUTF16("abcdefg"));
1417 render_text->SetDisplayRect(Rect(100, 17));
1418 SelectionModel sel_b(1, CURSOR_FORWARD);
1419 SelectionModel sel_c(2, CURSOR_FORWARD);
1420 Rect cursor_around_b = render_text->GetCursorBounds(sel_b, false);
1421 Rect cursor_before_b = render_text->GetCursorBounds(sel_b, true);
1422 Rect cursor_before_c = render_text->GetCursorBounds(sel_c, true);
1423 EXPECT_EQ(cursor_around_b.x(), cursor_before_b.x());
1424 EXPECT_EQ(cursor_around_b.right(), cursor_before_c.x());
1427 TEST_F(RenderTextTest, GetTextOffset) {
1428 // The default horizontal text offset differs for LTR and RTL, and is only set
1429 // when the RenderText object is created. This test will check the default in
1430 // LTR mode, and the next test will check the RTL default.
1431 const bool was_rtl = base::i18n::IsRTL();
1432 SetRTL(false);
1433 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1434 render_text->SetText(ASCIIToUTF16("abcdefg"));
1435 render_text->SetFontList(FontList("Arial, 13px"));
1437 // Set display area's size equal to the font size.
1438 const Size font_size(render_text->GetContentWidth(),
1439 render_text->font_list().GetHeight());
1440 Rect display_rect(font_size);
1441 render_text->SetDisplayRect(display_rect);
1443 Vector2d offset = render_text->GetLineOffset(0);
1444 EXPECT_TRUE(offset.IsZero());
1446 const int kEnlargementX = 2;
1447 display_rect.Inset(0, 0, -kEnlargementX, 0);
1448 render_text->SetDisplayRect(display_rect);
1450 // Check the default horizontal alignment.
1451 offset = render_text->GetLineOffset(0);
1452 EXPECT_EQ(0, offset.x());
1454 // Check explicitly setting the horizontal alignment.
1455 render_text->SetHorizontalAlignment(ALIGN_LEFT);
1456 offset = render_text->GetLineOffset(0);
1457 EXPECT_EQ(0, offset.x());
1458 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1459 offset = render_text->GetLineOffset(0);
1460 EXPECT_EQ(kEnlargementX / 2, offset.x());
1461 render_text->SetHorizontalAlignment(ALIGN_RIGHT);
1462 offset = render_text->GetLineOffset(0);
1463 EXPECT_EQ(kEnlargementX, offset.x());
1465 // Check that text is vertically centered within taller display rects.
1466 const int kEnlargementY = display_rect.height();
1467 display_rect.Inset(0, 0, 0, -kEnlargementY);
1468 render_text->SetDisplayRect(display_rect);
1469 const Vector2d prev_offset = render_text->GetLineOffset(0);
1470 display_rect.Inset(0, 0, 0, -2 * kEnlargementY);
1471 render_text->SetDisplayRect(display_rect);
1472 offset = render_text->GetLineOffset(0);
1473 EXPECT_EQ(prev_offset.y() + kEnlargementY, offset.y());
1475 SetRTL(was_rtl);
1478 TEST_F(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL) {
1479 // This only checks the default horizontal alignment in RTL mode; all other
1480 // GetLineOffset(0) attributes are checked by the test above.
1481 const bool was_rtl = base::i18n::IsRTL();
1482 SetRTL(true);
1483 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1484 render_text->SetText(ASCIIToUTF16("abcdefg"));
1485 render_text->SetFontList(FontList("Arial, 13px"));
1486 const int kEnlargement = 2;
1487 const Size font_size(render_text->GetContentWidth() + kEnlargement,
1488 render_text->GetStringSize().height());
1489 Rect display_rect(font_size);
1490 render_text->SetDisplayRect(display_rect);
1491 Vector2d offset = render_text->GetLineOffset(0);
1492 EXPECT_EQ(kEnlargement, offset.x());
1493 SetRTL(was_rtl);
1496 TEST_F(RenderTextTest, SetDisplayOffset) {
1497 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1498 render_text->SetText(ASCIIToUTF16("abcdefg"));
1499 render_text->SetFontList(FontList("Arial, 13px"));
1501 const Size font_size(render_text->GetContentWidth(),
1502 render_text->font_list().GetHeight());
1503 const int kEnlargement = 10;
1505 // Set display width |kEnlargement| pixels greater than content width and test
1506 // different possible situations. In this case the only possible display
1507 // offset is zero.
1508 Rect display_rect(font_size);
1509 display_rect.Inset(0, 0, -kEnlargement, 0);
1510 render_text->SetDisplayRect(display_rect);
1512 struct {
1513 HorizontalAlignment alignment;
1514 int offset;
1515 } small_content_cases[] = {
1516 { ALIGN_LEFT, -kEnlargement },
1517 { ALIGN_LEFT, 0 },
1518 { ALIGN_LEFT, kEnlargement },
1519 { ALIGN_RIGHT, -kEnlargement },
1520 { ALIGN_RIGHT, 0 },
1521 { ALIGN_RIGHT, kEnlargement },
1522 { ALIGN_CENTER, -kEnlargement },
1523 { ALIGN_CENTER, 0 },
1524 { ALIGN_CENTER, kEnlargement },
1527 for (size_t i = 0; i < arraysize(small_content_cases); i++) {
1528 render_text->SetHorizontalAlignment(small_content_cases[i].alignment);
1529 render_text->SetDisplayOffset(small_content_cases[i].offset);
1530 EXPECT_EQ(0, render_text->GetUpdatedDisplayOffset().x());
1533 // Set display width |kEnlargement| pixels less than content width and test
1534 // different possible situations.
1535 display_rect = Rect(font_size);
1536 display_rect.Inset(0, 0, kEnlargement, 0);
1537 render_text->SetDisplayRect(display_rect);
1539 struct {
1540 HorizontalAlignment alignment;
1541 int offset;
1542 int expected_offset;
1543 } large_content_cases[] = {
1544 // When text is left-aligned, display offset can be in range
1545 // [-kEnlargement, 0].
1546 { ALIGN_LEFT, -2 * kEnlargement, -kEnlargement },
1547 { ALIGN_LEFT, -kEnlargement / 2, -kEnlargement / 2 },
1548 { ALIGN_LEFT, kEnlargement, 0 },
1549 // When text is right-aligned, display offset can be in range
1550 // [0, kEnlargement].
1551 { ALIGN_RIGHT, -kEnlargement, 0 },
1552 { ALIGN_RIGHT, kEnlargement / 2, kEnlargement / 2 },
1553 { ALIGN_RIGHT, 2 * kEnlargement, kEnlargement },
1554 // When text is center-aligned, display offset can be in range
1555 // [-kEnlargement / 2 - 1, (kEnlargement - 1) / 2].
1556 { ALIGN_CENTER, -kEnlargement, -kEnlargement / 2 - 1 },
1557 { ALIGN_CENTER, -kEnlargement / 4, -kEnlargement / 4 },
1558 { ALIGN_CENTER, kEnlargement / 4, kEnlargement / 4 },
1559 { ALIGN_CENTER, kEnlargement, (kEnlargement - 1) / 2 },
1562 for (size_t i = 0; i < arraysize(large_content_cases); i++) {
1563 render_text->SetHorizontalAlignment(large_content_cases[i].alignment);
1564 render_text->SetDisplayOffset(large_content_cases[i].offset);
1565 EXPECT_EQ(large_content_cases[i].expected_offset,
1566 render_text->GetUpdatedDisplayOffset().x());
1570 TEST_F(RenderTextTest, SameFontForParentheses) {
1571 struct {
1572 const base::char16 left_char;
1573 const base::char16 right_char;
1574 } punctuation_pairs[] = {
1575 { '(', ')' },
1576 { '{', '}' },
1577 { '<', '>' },
1579 struct {
1580 base::string16 text;
1581 } cases[] = {
1582 // English(English)
1583 { WideToUTF16(L"Hello World(a)") },
1584 // English(English)English
1585 { WideToUTF16(L"Hello World(a)Hello World") },
1587 // Japanese(English)
1588 { WideToUTF16(L"\x6328\x62f6(a)") },
1589 // Japanese(English)Japanese
1590 { WideToUTF16(L"\x6328\x62f6(a)\x6328\x62f6") },
1591 // English(Japanese)English
1592 { WideToUTF16(L"Hello World(\x6328\x62f6)Hello World") },
1594 // Hindi(English)
1595 { WideToUTF16(L"\x0915\x093f(a)") },
1596 // Hindi(English)Hindi
1597 { WideToUTF16(L"\x0915\x093f(a)\x0915\x093f") },
1598 // English(Hindi)English
1599 { WideToUTF16(L"Hello World(\x0915\x093f)Hello World") },
1601 // Hebrew(English)
1602 { WideToUTF16(L"\x05e0\x05b8(a)") },
1603 // Hebrew(English)Hebrew
1604 { WideToUTF16(L"\x05e0\x05b8(a)\x05e0\x05b8") },
1605 // English(Hebrew)English
1606 { WideToUTF16(L"Hello World(\x05e0\x05b8)Hello World") },
1609 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1610 for (size_t i = 0; i < arraysize(cases); ++i) {
1611 base::string16 text = cases[i].text;
1612 const size_t start_paren_char_index = text.find('(');
1613 ASSERT_NE(base::string16::npos, start_paren_char_index);
1614 const size_t end_paren_char_index = text.find(')');
1615 ASSERT_NE(base::string16::npos, end_paren_char_index);
1617 for (size_t j = 0; j < arraysize(punctuation_pairs); ++j) {
1618 text[start_paren_char_index] = punctuation_pairs[j].left_char;
1619 text[end_paren_char_index] = punctuation_pairs[j].right_char;
1620 render_text->SetText(text);
1622 const std::vector<RenderText::FontSpan> spans =
1623 render_text->GetFontSpansForTesting();
1625 int start_paren_span_index = -1;
1626 int end_paren_span_index = -1;
1627 for (size_t k = 0; k < spans.size(); ++k) {
1628 if (IndexInRange(spans[k].second, start_paren_char_index))
1629 start_paren_span_index = k;
1630 if (IndexInRange(spans[k].second, end_paren_char_index))
1631 end_paren_span_index = k;
1633 ASSERT_NE(-1, start_paren_span_index);
1634 ASSERT_NE(-1, end_paren_span_index);
1636 const Font& start_font = spans[start_paren_span_index].first;
1637 const Font& end_font = spans[end_paren_span_index].first;
1638 EXPECT_EQ(start_font.GetFontName(), end_font.GetFontName());
1639 EXPECT_EQ(start_font.GetFontSize(), end_font.GetFontSize());
1640 EXPECT_EQ(start_font.GetStyle(), end_font.GetStyle());
1645 // Make sure the caret width is always >=1 so that the correct
1646 // caret is drawn at high DPI. crbug.com/164100.
1647 TEST_F(RenderTextTest, CaretWidth) {
1648 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1649 render_text->SetText(ASCIIToUTF16("abcdefg"));
1650 EXPECT_GE(render_text->GetUpdatedCursorBounds().width(), 1);
1653 TEST_F(RenderTextTest, SelectWord) {
1654 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1655 render_text->SetText(ASCIIToUTF16(" foo a.bc.d bar"));
1657 struct {
1658 size_t cursor;
1659 size_t selection_start;
1660 size_t selection_end;
1661 } cases[] = {
1662 { 0, 0, 1 },
1663 { 1, 1, 4 },
1664 { 2, 1, 4 },
1665 { 3, 1, 4 },
1666 { 4, 4, 6 },
1667 { 5, 4, 6 },
1668 { 6, 6, 7 },
1669 { 7, 7, 8 },
1670 { 8, 8, 10 },
1671 { 9, 8, 10 },
1672 { 10, 10, 11 },
1673 { 11, 11, 12 },
1674 { 12, 12, 13 },
1675 { 13, 13, 16 },
1676 { 14, 13, 16 },
1677 { 15, 13, 16 },
1678 { 16, 13, 16 },
1681 for (size_t i = 0; i < arraysize(cases); ++i) {
1682 render_text->SetCursorPosition(cases[i].cursor);
1683 render_text->SelectWord();
1684 EXPECT_EQ(Range(cases[i].selection_start, cases[i].selection_end),
1685 render_text->selection());
1689 // Make sure the last word is selected when the cursor is at text.length().
1690 TEST_F(RenderTextTest, LastWordSelected) {
1691 const std::string kTestURL1 = "http://www.google.com";
1692 const std::string kTestURL2 = "http://www.google.com/something/";
1694 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1696 render_text->SetText(ASCIIToUTF16(kTestURL1));
1697 render_text->SetCursorPosition(kTestURL1.length());
1698 render_text->SelectWord();
1699 EXPECT_EQ(ASCIIToUTF16("com"), GetSelectedText(render_text.get()));
1700 EXPECT_FALSE(render_text->selection().is_reversed());
1702 render_text->SetText(ASCIIToUTF16(kTestURL2));
1703 render_text->SetCursorPosition(kTestURL2.length());
1704 render_text->SelectWord();
1705 EXPECT_EQ(ASCIIToUTF16("/"), GetSelectedText(render_text.get()));
1706 EXPECT_FALSE(render_text->selection().is_reversed());
1709 // When given a non-empty selection, SelectWord should expand the selection to
1710 // nearest word boundaries.
1711 TEST_F(RenderTextTest, SelectMultipleWords) {
1712 const std::string kTestURL = "http://www.google.com";
1714 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1716 render_text->SetText(ASCIIToUTF16(kTestURL));
1717 render_text->SelectRange(Range(16, 20));
1718 render_text->SelectWord();
1719 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1720 EXPECT_FALSE(render_text->selection().is_reversed());
1722 // SelectWord should preserve the selection direction.
1723 render_text->SelectRange(Range(20, 16));
1724 render_text->SelectWord();
1725 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1726 EXPECT_TRUE(render_text->selection().is_reversed());
1729 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
1730 // does not implement this yet. http://crbug.com/131618
1731 #if !defined(OS_MACOSX)
1732 TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) {
1733 ASSERT_FALSE(base::i18n::IsRTL());
1734 ASSERT_FALSE(base::i18n::ICUIsRTL());
1736 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1737 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1738 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1739 CURSOR_FORWARD));
1740 int width = render_text->GetStringSize().width();
1741 ASSERT_GT(width, 10);
1743 // Ensure that the cursor is placed at the width of its preceding text.
1744 render_text->SetDisplayRect(Rect(width + 10, 1));
1745 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1747 // Ensure that shrinking the display rectangle keeps the cursor in view.
1748 render_text->SetDisplayRect(Rect(width - 10, 1));
1749 EXPECT_EQ(render_text->display_rect().width(),
1750 render_text->GetUpdatedCursorBounds().right());
1752 // Ensure that the text will pan to fill its expanding display rectangle.
1753 render_text->SetDisplayRect(Rect(width - 5, 1));
1754 EXPECT_EQ(render_text->display_rect().width(),
1755 render_text->GetUpdatedCursorBounds().right());
1757 // Ensure that a sufficiently large display rectangle shows all the text.
1758 render_text->SetDisplayRect(Rect(width + 10, 1));
1759 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1761 // Repeat the test with RTL text.
1762 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1763 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1764 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1765 width = render_text->GetStringSize().width();
1766 ASSERT_GT(width, 10);
1768 // Ensure that the cursor is placed at the width of its preceding text.
1769 render_text->SetDisplayRect(Rect(width + 10, 1));
1770 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1772 // Ensure that shrinking the display rectangle keeps the cursor in view.
1773 render_text->SetDisplayRect(Rect(width - 10, 1));
1774 EXPECT_EQ(render_text->display_rect().width(),
1775 render_text->GetUpdatedCursorBounds().right());
1777 // Ensure that the text will pan to fill its expanding display rectangle.
1778 render_text->SetDisplayRect(Rect(width - 5, 1));
1779 EXPECT_EQ(render_text->display_rect().width(),
1780 render_text->GetUpdatedCursorBounds().right());
1782 // Ensure that a sufficiently large display rectangle shows all the text.
1783 render_text->SetDisplayRect(Rect(width + 10, 1));
1784 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1787 TEST_F(RenderTextTest, DisplayRectShowsCursorRTL) {
1788 // Set the application default text direction to RTL.
1789 const bool was_rtl = base::i18n::IsRTL();
1790 SetRTL(true);
1792 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1793 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1794 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1795 int width = render_text->GetStringSize().width();
1796 ASSERT_GT(width, 10);
1798 // Ensure that the cursor is placed at the width of its preceding text.
1799 render_text->SetDisplayRect(Rect(width + 10, 1));
1800 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1801 render_text->GetUpdatedCursorBounds().x());
1803 // Ensure that shrinking the display rectangle keeps the cursor in view.
1804 render_text->SetDisplayRect(Rect(width - 10, 1));
1805 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1807 // Ensure that the text will pan to fill its expanding display rectangle.
1808 render_text->SetDisplayRect(Rect(width - 5, 1));
1809 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1811 // Ensure that a sufficiently large display rectangle shows all the text.
1812 render_text->SetDisplayRect(Rect(width + 10, 1));
1813 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1814 render_text->GetUpdatedCursorBounds().x());
1816 // Repeat the test with RTL text.
1817 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1818 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1819 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1820 CURSOR_FORWARD));
1821 width = render_text->GetStringSize().width();
1822 ASSERT_GT(width, 10);
1824 // Ensure that the cursor is placed at the width of its preceding text.
1825 render_text->SetDisplayRect(Rect(width + 10, 1));
1826 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1827 render_text->GetUpdatedCursorBounds().x());
1829 // Ensure that shrinking the display rectangle keeps the cursor in view.
1830 render_text->SetDisplayRect(Rect(width - 10, 1));
1831 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1833 // Ensure that the text will pan to fill its expanding display rectangle.
1834 render_text->SetDisplayRect(Rect(width - 5, 1));
1835 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1837 // Ensure that a sufficiently large display rectangle shows all the text.
1838 render_text->SetDisplayRect(Rect(width + 10, 1));
1839 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1840 render_text->GetUpdatedCursorBounds().x());
1842 // Reset the application default text direction to LTR.
1843 SetRTL(was_rtl);
1844 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1846 #endif // !defined(OS_MACOSX)
1848 // Changing colors between or inside ligated glyphs should not break shaping.
1849 TEST_F(RenderTextTest, SelectionKeepsLigatures) {
1850 const wchar_t* kTestStrings[] = { L"\x644\x623", L"\x633\x627" };
1851 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1852 render_text->set_selection_color(SK_ColorRED);
1853 Canvas canvas;
1855 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1856 render_text->SetText(WideToUTF16(kTestStrings[i]));
1857 const int expected_width = render_text->GetStringSize().width();
1858 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD));
1859 EXPECT_EQ(expected_width, render_text->GetStringSize().width());
1860 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119
1861 render_text->Draw(&canvas);
1862 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1866 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1867 // Ensure strings wrap onto multiple lines for a small available width.
1868 TEST_F(RenderTextTest, DISABLED_Multiline_MinWidth) {
1869 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl,
1870 kRtlLtr, kRtlLtrRtl };
1872 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1873 render_text->SetDisplayRect(Rect(1, 1000));
1874 render_text->SetMultiline(true);
1875 Canvas canvas;
1877 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1878 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1879 render_text->SetText(WideToUTF16(kTestStrings[i]));
1880 render_text->Draw(&canvas);
1881 EXPECT_GT(render_text->lines_.size(), 1U);
1885 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1886 // Ensure strings wrap onto multiple lines for a normal available width.
1887 TEST_F(RenderTextTest, DISABLED_Multiline_NormalWidth) {
1888 const struct {
1889 const wchar_t* const text;
1890 const Range first_line_char_range;
1891 const Range second_line_char_range;
1892 } kTestStrings[] = {
1893 { L"abc defg hijkl", Range(0, 9), Range(9, 14) },
1894 { L"qwertyzxcvbn", Range(0, 8), Range(8, 12) },
1895 { L"\x062A\x0641\x0627\x062D\x05EA\x05E4\x05D5\x05D6\x05D9\x05DD",
1896 Range(4, 10), Range(0, 4) }
1899 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1900 render_text->SetDisplayRect(Rect(50, 1000));
1901 render_text->SetMultiline(true);
1902 Canvas canvas;
1904 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1905 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1906 render_text->SetText(WideToUTF16(kTestStrings[i].text));
1907 render_text->Draw(&canvas);
1908 ASSERT_EQ(2U, render_text->lines_.size());
1909 ASSERT_EQ(1U, render_text->lines_[0].segments.size());
1910 EXPECT_EQ(kTestStrings[i].first_line_char_range,
1911 render_text->lines_[0].segments[0].char_range);
1912 ASSERT_EQ(1U, render_text->lines_[1].segments.size());
1913 EXPECT_EQ(kTestStrings[i].second_line_char_range,
1914 render_text->lines_[1].segments[0].char_range);
1918 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1919 // Ensure strings don't wrap onto multiple lines for a sufficient available
1920 // width.
1921 TEST_F(RenderTextTest, DISABLED_Multiline_SufficientWidth) {
1922 const wchar_t* kTestStrings[] = { L"", L" ", L".", L" . ", L"abc", L"a b c",
1923 L"\x62E\x628\x632", L"\x62E \x628 \x632" };
1925 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1926 render_text->SetDisplayRect(Rect(30, 1000));
1927 render_text->SetMultiline(true);
1928 Canvas canvas;
1930 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1931 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1932 render_text->SetText(WideToUTF16(kTestStrings[i]));
1933 render_text->Draw(&canvas);
1934 EXPECT_EQ(1U, render_text->lines_.size());
1938 // TODO(ckocagil): Enable for RenderTextHarfBuzz after implementing multiline.
1939 TEST_F(RenderTextTest, DISABLED_Multiline_Newline) {
1940 const struct {
1941 const wchar_t* const text;
1942 // Ranges of the characters on each line preceding the newline.
1943 const Range first_line_char_range;
1944 const Range second_line_char_range;
1945 } kTestStrings[] = {
1946 { L"abc\ndef", Range(0, 3), Range(4, 7) },
1947 { L"a \n b ", Range(0, 2), Range(3, 6) },
1948 { L"\n" , Range::InvalidRange(), Range::InvalidRange() }
1951 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1952 render_text->SetDisplayRect(Rect(200, 1000));
1953 render_text->SetMultiline(true);
1954 Canvas canvas;
1956 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1957 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1958 render_text->SetText(WideToUTF16(kTestStrings[i].text));
1959 render_text->Draw(&canvas);
1961 ASSERT_EQ(2U, render_text->lines_.size());
1963 const Range first_expected_range = kTestStrings[i].first_line_char_range;
1964 ASSERT_EQ(first_expected_range.IsValid() ? 2U : 1U,
1965 render_text->lines_[0].segments.size());
1966 if (first_expected_range.IsValid())
1967 EXPECT_EQ(first_expected_range,
1968 render_text->lines_[0].segments[0].char_range);
1970 const internal::LineSegment& newline_segment =
1971 render_text->lines_[0].segments[first_expected_range.IsValid() ? 1 : 0];
1972 ASSERT_EQ(1U, newline_segment.char_range.length());
1973 EXPECT_EQ(L'\n', kTestStrings[i].text[newline_segment.char_range.start()]);
1975 const Range second_expected_range = kTestStrings[i].second_line_char_range;
1976 ASSERT_EQ(second_expected_range.IsValid() ? 1U : 0U,
1977 render_text->lines_[1].segments.size());
1978 if (second_expected_range.IsValid())
1979 EXPECT_EQ(second_expected_range,
1980 render_text->lines_[1].segments[0].char_range);
1984 // Test TextRunHarfBuzz's cluster finding logic.
1985 TEST_F(RenderTextTest, HarfBuzz_Clusters) {
1986 struct {
1987 uint32 glyph_to_char[4];
1988 Range chars[4];
1989 Range glyphs[4];
1990 bool is_rtl;
1991 } cases[] = {
1992 { // From string "A B C D" to glyphs "a b c d".
1993 { 0, 1, 2, 3 },
1994 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
1995 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
1996 false
1998 { // From string "A B C D" to glyphs "d c b a".
1999 { 3, 2, 1, 0 },
2000 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2001 { Range(3, 4), Range(2, 3), Range(1, 2), Range(0, 1) },
2002 true
2004 { // From string "A B C D" to glyphs "ab c c d".
2005 { 0, 2, 2, 3 },
2006 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2007 { Range(0, 1), Range(0, 1), Range(1, 3), Range(3, 4) },
2008 false
2010 { // From string "A B C D" to glyphs "d c c ba".
2011 { 3, 2, 2, 0 },
2012 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2013 { Range(3, 4), Range(3, 4), Range(1, 3), Range(0, 1) },
2014 true
2018 internal::TextRunHarfBuzz run;
2019 run.range = Range(0, 4);
2020 run.glyph_count = 4;
2021 run.glyph_to_char.resize(4);
2023 for (size_t i = 0; i < arraysize(cases); ++i) {
2024 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 4,
2025 run.glyph_to_char.begin());
2026 run.is_rtl = cases[i].is_rtl;
2028 for (size_t j = 0; j < 4; ++j) {
2029 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2030 Range chars;
2031 Range glyphs;
2032 run.GetClusterAt(j, &chars, &glyphs);
2033 EXPECT_EQ(cases[i].chars[j], chars);
2034 EXPECT_EQ(cases[i].glyphs[j], glyphs);
2035 EXPECT_EQ(cases[i].glyphs[j], run.CharRangeToGlyphRange(chars));
2040 // Ensure that graphemes with multiple code points do not get split.
2041 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemeCases) {
2042 const wchar_t* cases[] = {
2043 // "A" with a combining umlaut, followed by a "B".
2044 L"A\x0308" L"B",
2045 // Devanagari biconsonantal conjunct "ki", followed by an "a".
2046 L"\x0915\x093f\x0905",
2047 // Thai consonant and vowel pair "cho chan" + "sara am", followed by Thai
2048 // digit 0.
2049 L"\x0e08\x0e33\x0E50",
2052 RenderTextHarfBuzz render_text;
2054 for (size_t i = 0; i < arraysize(cases); ++i) {
2055 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS, i));
2057 base::string16 text = WideToUTF16(cases[i]);
2058 render_text.SetText(text);
2059 render_text.EnsureLayout();
2060 ASSERT_EQ(1U, render_text.runs_.size());
2061 internal::TextRunHarfBuzz* run = render_text.runs_[0];
2063 base::i18n::BreakIterator* iter = render_text.grapheme_iterator_.get();
2064 auto first_grapheme_bounds = run->GetGraphemeBounds(iter, 0);
2065 EXPECT_EQ(first_grapheme_bounds, run->GetGraphemeBounds(iter, 1));
2066 auto second_grapheme_bounds = run->GetGraphemeBounds(iter, 2);
2067 EXPECT_EQ(first_grapheme_bounds.second, second_grapheme_bounds.first);
2071 // Test the partition of a multi-grapheme cluster into grapheme ranges.
2072 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemePartition) {
2073 struct {
2074 uint32 glyph_to_char[2];
2075 Range bounds[4];
2076 bool is_rtl;
2077 } cases[] = {
2078 { // From string "A B C D" to glyphs "a bcd".
2079 { 0, 1 },
2080 { Range(0, 10), Range(10, 13), Range(13, 17), Range(17, 20) },
2081 false
2083 { // From string "A B C D" to glyphs "ab cd".
2084 { 0, 2 },
2085 { Range(0, 5), Range(5, 10), Range(10, 15), Range(15, 20) },
2086 false
2088 { // From string "A B C D" to glyphs "dcb a".
2089 { 1, 0 },
2090 { Range(10, 20), Range(7, 10), Range(3, 7), Range(0, 3) },
2091 true
2093 { // From string "A B C D" to glyphs "dc ba".
2094 { 2, 0 },
2095 { Range(15, 20), Range(10, 15), Range(5, 10), Range(0, 5) },
2096 true
2100 internal::TextRunHarfBuzz run;
2101 run.range = Range(0, 4);
2102 run.glyph_count = 2;
2103 run.glyph_to_char.resize(2);
2104 run.positions.reset(new SkPoint[4]);
2105 run.width = 20;
2107 const base::string16 kString = ASCIIToUTF16("abcd");
2108 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2109 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2110 ASSERT_TRUE(iter->Init());
2112 for (size_t i = 0; i < arraysize(cases); ++i) {
2113 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 2,
2114 run.glyph_to_char.begin());
2115 run.is_rtl = cases[i].is_rtl;
2116 for (int j = 0; j < 2; ++j)
2117 run.positions[j].set(j * 10, 0);
2119 for (size_t j = 0; j < 4; ++j) {
2120 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2121 EXPECT_EQ(cases[i].bounds[j],
2122 internal::RoundRangeF(run.GetGraphemeBounds(iter.get(), j)));
2127 TEST_F(RenderTextTest, HarfBuzz_RunDirection) {
2128 RenderTextHarfBuzz render_text;
2129 const base::string16 mixed =
2130 WideToUTF16(L"\x05D0\x05D1" L"1234" L"\x05D2\x05D3");
2131 render_text.SetText(mixed);
2132 render_text.EnsureLayout();
2133 ASSERT_EQ(3U, render_text.runs_.size());
2134 EXPECT_TRUE(render_text.runs_[0]->is_rtl);
2135 EXPECT_FALSE(render_text.runs_[1]->is_rtl);
2136 EXPECT_TRUE(render_text.runs_[2]->is_rtl);
2139 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks) {
2140 RenderTextHarfBuzz render_text;
2142 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2143 render_text.SetText(WideToUTF16(L"x\x25B6y"));
2144 render_text.EnsureLayout();
2145 ASSERT_EQ(3U, render_text.runs_.size());
2146 EXPECT_EQ(Range(0, 1), render_text.runs_[0]->range);
2147 EXPECT_EQ(Range(1, 2), render_text.runs_[1]->range);
2148 EXPECT_EQ(Range(2, 3), render_text.runs_[2]->range);
2150 render_text.SetText(WideToUTF16(L"x \x25B6 y"));
2151 render_text.EnsureLayout();
2152 ASSERT_EQ(3U, render_text.runs_.size());
2153 EXPECT_EQ(Range(0, 2), render_text.runs_[0]->range);
2154 EXPECT_EQ(Range(2, 3), render_text.runs_[1]->range);
2155 EXPECT_EQ(Range(3, 5), render_text.runs_[2]->range);
2158 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByEmoji) {
2159 RenderTextHarfBuzz render_text;
2161 // \xF0\x9F\x98\x81 (U+1F601) is smile icon emoji. \xE2\x9C\xA8 (U+2728) is
2162 // a sparkle icon. Both can be drawn with color emoji fonts, so runs should be
2163 // separated. See crbug.com/448909
2164 render_text.SetText(UTF8ToUTF16("x\xF0\x9F\x98\x81y\xE2\x9C\xA8"));
2165 render_text.EnsureLayout();
2166 ASSERT_EQ(4U, render_text.runs_.size());
2167 EXPECT_EQ(Range(0, 1), render_text.runs_[0]->range);
2168 // The length is 2 since U+1F601 is represented as a surrogate pair in UTF16.
2169 EXPECT_EQ(Range(1, 3), render_text.runs_[1]->range);
2170 EXPECT_EQ(Range(3, 4), render_text.runs_[2]->range);
2171 EXPECT_EQ(Range(4, 5), render_text.runs_[3]->range);
2174 TEST_F(RenderTextTest, GlyphBounds) {
2175 const wchar_t* kTestStrings[] = {
2176 L"asdf 1234 qwer", L"\x0647\x0654", L"\x0645\x0631\x062D\x0628\x0627"
2178 scoped_ptr<RenderText> render_text(new RenderTextHarfBuzz);
2180 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2181 render_text->SetText(WideToUTF16(kTestStrings[i]));
2182 render_text->EnsureLayout();
2184 for (size_t j = 0; j < render_text->text().length(); ++j)
2185 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2189 // Ensure that shaping with a non-existent font does not cause a crash.
2190 TEST_F(RenderTextTest, HarfBuzz_NonExistentFont) {
2191 RenderTextHarfBuzz render_text;
2192 render_text.SetText(ASCIIToUTF16("test"));
2193 render_text.EnsureLayout();
2194 ASSERT_EQ(1U, render_text.runs_.size());
2195 internal::TextRunHarfBuzz* run = render_text.runs_[0];
2196 render_text.ShapeRunWithFont(
2197 run, "TheFontThatDoesntExist", FontRenderParams());
2200 // Ensure an empty run returns sane values to queries.
2201 TEST_F(RenderTextTest, HarfBuzz_EmptyRun) {
2202 internal::TextRunHarfBuzz run;
2203 const base::string16 kString = ASCIIToUTF16("abcdefgh");
2204 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2205 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2206 ASSERT_TRUE(iter->Init());
2208 run.range = Range(3, 8);
2209 run.glyph_count = 0;
2210 EXPECT_EQ(Range(0, 0), run.CharRangeToGlyphRange(Range(4, 5)));
2211 EXPECT_EQ(Range(0, 0),
2212 internal::RoundRangeF(run.GetGraphemeBounds(iter.get(), 4)));
2213 Range chars;
2214 Range glyphs;
2215 run.GetClusterAt(4, &chars, &glyphs);
2216 EXPECT_EQ(Range(3, 8), chars);
2217 EXPECT_EQ(Range(0, 0), glyphs);
2220 // Ensure a string fits in a display rect with a width equal to the string's.
2221 TEST_F(RenderTextTest, StringFitsOwnWidth) {
2222 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2223 const base::string16 kString = ASCIIToUTF16("www.example.com");
2225 render_text->SetText(kString);
2226 render_text->ApplyStyle(BOLD, true, Range(0, 3));
2227 render_text->SetElideBehavior(ELIDE_TAIL);
2229 render_text->SetDisplayRect(Rect(0, 0, 500, 100));
2230 EXPECT_EQ(kString, render_text->GetLayoutText());
2231 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100));
2232 EXPECT_EQ(kString, render_text->GetLayoutText());
2235 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184
2236 #if !defined(OS_WIN)
2237 // Ensure that RenderText examines all of the fonts in its FontList before
2238 // falling back to other fonts.
2239 TEST_F(RenderTextTest, HarfBuzz_FontListFallback) {
2240 // Double-check that the requested fonts are present.
2241 FontList font_list("Arial, Symbol, 12px");
2242 const std::vector<Font>& fonts = font_list.GetFonts();
2243 ASSERT_EQ(2u, fonts.size());
2244 ASSERT_EQ("arial",
2245 base::StringToLowerASCII(fonts[0].GetActualFontNameForTesting()));
2246 ASSERT_EQ("symbol",
2247 base::StringToLowerASCII(fonts[1].GetActualFontNameForTesting()));
2249 // "⊕" (CIRCLED PLUS) should be rendered with Symbol rather than falling back
2250 // to some other font that's present on the system.
2251 RenderTextHarfBuzz render_text;
2252 render_text.SetFontList(font_list);
2253 render_text.SetText(UTF8ToUTF16("\xE2\x8A\x95"));
2254 const std::vector<RenderText::FontSpan> spans =
2255 render_text.GetFontSpansForTesting();
2256 ASSERT_EQ(static_cast<size_t>(1), spans.size());
2257 EXPECT_EQ("Symbol", spans[0].first.GetFontName());
2259 #endif // !defined(OS_WIN)
2261 // Ensure that the fallback fonts of the Uniscribe font are tried for shaping.
2262 #if defined(OS_WIN)
2263 TEST_F(RenderTextTest, HarfBuzz_UniscribeFallback) {
2264 RenderTextHarfBuzz render_text;
2265 PlatformFontWin* font_win = new PlatformFontWin("Meiryo", 12);
2266 // Japanese name for Meiryo. This name won't be found in the system's linked
2267 // fonts, forcing RTHB to try the Uniscribe font and its fallbacks.
2268 font_win->font_ref_->font_name_ = WideToUTF8(L"\x30e1\x30a4\x30ea\x30aa");
2269 FontList font_list((Font(font_win)));
2271 render_text.SetFontList(font_list);
2272 // Korean character "han".
2273 render_text.SetText(WideToUTF16(L"\xd55c"));
2274 render_text.EnsureLayout();
2275 ASSERT_EQ(1U, render_text.runs_.size());
2276 EXPECT_EQ(0U, render_text.runs_[0]->CountMissingGlyphs());
2278 #endif // defined(OS_WIN)
2280 // Ensure that the width reported by RenderText is sufficient for drawing. Draws
2281 // to a canvas and checks whether any pixel beyond the width is colored.
2282 TEST_F(RenderTextTest, TextDoesntClip) {
2283 const wchar_t* kTestStrings[] = { L"Save", L"Remove", L"TEST", L"W", L"WWW" };
2284 const Size kCanvasSize(300, 50);
2285 const int kTestWidth = 10;
2287 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
2288 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2289 scoped_ptr<Canvas> canvas(
2290 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
2291 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2292 render_text->SetDisplayRect(Rect(kCanvasSize));
2293 render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT);
2294 render_text->SetColor(SK_ColorBLACK);
2296 for (auto string : kTestStrings) {
2297 surface->getCanvas()->clear(SK_ColorWHITE);
2298 render_text->SetText(WideToUTF16(string));
2299 render_text->SetStyle(BOLD, true);
2300 render_text->Draw(canvas.get());
2301 int width = render_text->GetStringSize().width();
2302 ASSERT_LT(width + kTestWidth, kCanvasSize.width());
2303 const uint32* buffer = static_cast<const uint32*>(
2304 surface->peekPixels(NULL, NULL));
2305 ASSERT_NE(nullptr, buffer);
2307 for (int y = 0; y < kCanvasSize.height(); ++y) {
2308 // Allow one column of anti-aliased pixels past the expected width.
2309 SkColor color = buffer[width + y * kCanvasSize.width()];
2310 EXPECT_LT(220U, color_utils::GetLuminanceForColor(color)) << string;
2311 for (int x = 1; x < kTestWidth; ++x) {
2312 color = buffer[width + x + y * kCanvasSize.width()];
2313 EXPECT_EQ(SK_ColorWHITE, color) << string;
2319 // This test validates that the RenderText centering baseline is greater than
2320 // the font list baseline for a valid display rect.
2321 TEST_F(RenderTextTest, CenteringBaselineTest) {
2322 FontList font_list("Arial, 12px");
2323 const std::vector<Font>& fonts = font_list.GetFonts();
2324 ASSERT_EQ(1U, fonts.size());
2325 ASSERT_EQ("arial",
2326 base::StringToLowerASCII(fonts[0].GetActualFontNameForTesting()));
2328 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2329 render_text->SetDisplayRect(Rect(0, 0, 25, 25));
2330 render_text->SetFontList(font_list);
2331 EXPECT_GT(render_text->GetBaseline(), font_list.GetBaseline());
2334 #if defined(OS_WIN)
2335 // This test validates that the RenderText centering baseline for uniscribe
2336 // fonts like Devnagri fonts is greater than the font list baseline for a valid
2337 // display rect.
2338 TEST_F(RenderTextTest, CenteringBaselineTestForUniscribeFonts) {
2339 FontList font_list("Madhav, 12px");
2340 const std::vector<Font>& fonts = font_list.GetFonts();
2341 ASSERT_EQ(1U, fonts.size());
2342 ASSERT_EQ("madhav",
2343 base::StringToLowerASCII(fonts[0].GetActualFontNameForTesting()));
2345 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2346 render_text->SetDisplayRect(Rect(0, 0, 25, 25));
2347 render_text->SetFontList(font_list);
2348 EXPECT_GT(render_text->GetBaseline(), font_list.GetBaseline());
2350 #endif
2352 } // namespace gfx