ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / gfx / render_text_unittest.cc
blob542939ea3081369af9a50fdf080f3490eaa7d3d8
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_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/core/SkSurface.h"
18 #include "ui/gfx/break_list.h"
19 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/color_utils.h"
21 #include "ui/gfx/font.h"
22 #include "ui/gfx/range/range.h"
23 #include "ui/gfx/range/range_f.h"
24 #include "ui/gfx/render_text_harfbuzz.h"
26 #if defined(OS_WIN)
27 #include "base/win/windows_version.h"
28 #include "ui/gfx/platform_font_win.h"
29 #endif
31 using base::ASCIIToUTF16;
32 using base::UTF8ToUTF16;
33 using base::WideToUTF16;
34 using base::WideToUTF8;
36 namespace gfx {
38 namespace {
40 // Various weak, LTR, RTL, and Bidi string cases with three characters each.
41 const wchar_t kWeak[] = L" . ";
42 const wchar_t kLtr[] = L"abc";
43 const wchar_t kRtl[] = L"\x5d0\x5d1\x5d2";
44 const wchar_t kLtrRtl[] = L"a" L"\x5d0\x5d1";
45 const wchar_t kLtrRtlLtr[] = L"a" L"\x5d1" L"b";
46 const wchar_t kRtlLtr[] = L"\x5d0\x5d1" L"a";
47 const wchar_t kRtlLtrRtl[] = L"\x5d0" L"a" L"\x5d1";
49 // Checks whether |range| contains |index|. This is not the same as calling
50 // range.Contains(Range(index)), which returns true if |index| == |range.end()|.
51 bool IndexInRange(const Range& range, size_t index) {
52 return index >= range.start() && index < range.end();
55 base::string16 GetSelectedText(RenderText* render_text) {
56 return render_text->text().substr(render_text->selection().GetMin(),
57 render_text->selection().length());
60 // A test utility function to set the application default text direction.
61 void SetRTL(bool rtl) {
62 // Override the current locale/direction.
63 base::i18n::SetICUDefaultLocale(rtl ? "he" : "en");
64 EXPECT_EQ(rtl, base::i18n::IsRTL());
67 #if !defined(OS_MACOSX)
68 // Ensure cursor movement in the specified |direction| yields |expected| values.
69 void RunMoveCursorLeftRightTest(RenderText* render_text,
70 const std::vector<SelectionModel>& expected,
71 VisualCursorDirection direction) {
72 for (size_t i = 0; i < expected.size(); ++i) {
73 SCOPED_TRACE(base::StringPrintf("Going %s; expected value index %d.",
74 direction == CURSOR_LEFT ? "left" : "right", static_cast<int>(i)));
75 EXPECT_EQ(expected[i], render_text->selection_model());
76 render_text->MoveCursor(CHARACTER_BREAK, direction, false);
78 // Check that cursoring is clamped at the line edge.
79 EXPECT_EQ(expected.back(), render_text->selection_model());
80 // Check that it is the line edge.
81 render_text->MoveCursor(LINE_BREAK, direction, false);
82 EXPECT_EQ(expected.back(), render_text->selection_model());
84 #endif // !defined(OS_MACOSX)
86 // Test utility for Multiline_Newline test case. Empty |expected_range| means
87 // the blank line which has no segments. Otherwise |segments| should contain
88 // exactly one line segment whose range equals to |expected_range|.
89 void VerifyLineSegments(const Range& expected_range,
90 const std::vector<internal::LineSegment>& segments) {
91 EXPECT_EQ(expected_range.is_empty() ? 0ul : 1ul, segments.size());
92 if (!expected_range.is_empty())
93 EXPECT_EQ(expected_range, segments[0].char_range);
96 // The class which records the drawing operations so that the test case can
97 // verify where exactly the glyphs are drawn.
98 class TestSkiaTextRenderer : public internal::SkiaTextRenderer {
99 public:
100 struct TextLog {
101 TextLog() : glyph_count(0u) {}
102 PointF origin;
103 size_t glyph_count;
106 struct DecorationLog {
107 DecorationLog(int x, int y, int width, bool underline, bool strike,
108 bool diagonal_strike)
109 : x(x), y(y), width(width), underline(underline), strike(strike),
110 diagonal_strike(diagonal_strike) {}
111 int x;
112 int y;
113 int width;
114 bool underline;
115 bool strike;
116 bool diagonal_strike;
119 explicit TestSkiaTextRenderer(Canvas* canvas)
120 : internal::SkiaTextRenderer(canvas) {}
121 ~TestSkiaTextRenderer() override {}
123 void GetTextLogAndReset(std::vector<TextLog>* text_log) {
124 text_log_.swap(*text_log);
125 text_log_.clear();
128 void GetDecorationLogAndReset(std::vector<DecorationLog>* decoration_log) {
129 decoration_log_.swap(*decoration_log);
130 decoration_log_.clear();
133 private:
134 // internal::SkiaTextRenderer:
135 void DrawPosText(const SkPoint* pos,
136 const uint16* glyphs,
137 size_t glyph_count) override {
138 TextLog log_entry;
139 log_entry.glyph_count = glyph_count;
140 if (glyph_count > 0) {
141 log_entry.origin =
142 PointF(SkScalarToFloat(pos[0].x()), SkScalarToFloat(pos[0].y()));
143 for (size_t i = 1U; i < glyph_count; ++i) {
144 log_entry.origin.SetToMin(
145 PointF(SkScalarToFloat(pos[i].x()), SkScalarToFloat(pos[i].y())));
148 text_log_.push_back(log_entry);
149 internal::SkiaTextRenderer::DrawPosText(pos, glyphs, glyph_count);
152 void DrawDecorations(int x, int y, int width, bool underline, bool strike,
153 bool diagonal_strike) override {
154 decoration_log_.push_back(
155 DecorationLog(x, y, width, underline, strike, diagonal_strike));
156 internal::SkiaTextRenderer::DrawDecorations(
157 x, y, width, underline, strike, diagonal_strike);
160 std::vector<TextLog> text_log_;
161 std::vector<DecorationLog> decoration_log_;
163 DISALLOW_COPY_AND_ASSIGN(TestSkiaTextRenderer);
166 } // namespace
168 class RenderTextTest : public testing::Test {
171 TEST_F(RenderTextTest, DefaultStyle) {
172 // Check the default styles applied to new instances and adjusted text.
173 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
174 EXPECT_TRUE(render_text->text().empty());
175 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
176 for (size_t i = 0; i < arraysize(cases); ++i) {
177 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLACK));
178 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
179 EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false));
180 render_text->SetText(WideToUTF16(cases[i]));
184 TEST_F(RenderTextTest, SetColorAndStyle) {
185 // Ensure custom default styles persist across setting and clearing text.
186 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
187 const SkColor color = SK_ColorRED;
188 render_text->SetColor(color);
189 render_text->SetStyle(BOLD, true);
190 render_text->SetStyle(UNDERLINE, false);
191 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
192 for (size_t i = 0; i < arraysize(cases); ++i) {
193 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color));
194 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true));
195 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false));
196 render_text->SetText(WideToUTF16(cases[i]));
198 // Ensure custom default styles can be applied after text has been set.
199 if (i == 1)
200 render_text->SetStyle(STRIKE, true);
201 if (i >= 1)
202 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true));
206 TEST_F(RenderTextTest, ApplyColorAndStyle) {
207 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
208 render_text->SetText(ASCIIToUTF16("012345678"));
210 // Apply a ranged color and style and check the resulting breaks.
211 render_text->ApplyColor(SK_ColorRED, Range(1, 4));
212 render_text->ApplyStyle(BOLD, true, Range(2, 5));
213 std::vector<std::pair<size_t, SkColor> > expected_color;
214 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK));
215 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED));
216 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK));
217 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
218 std::vector<std::pair<size_t, bool> > expected_style;
219 expected_style.push_back(std::pair<size_t, bool>(0, false));
220 expected_style.push_back(std::pair<size_t, bool>(2, true));
221 expected_style.push_back(std::pair<size_t, bool>(5, false));
222 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style));
224 // Ensure setting a color and style overrides the ranged colors and styles.
225 render_text->SetColor(SK_ColorBLUE);
226 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE));
227 render_text->SetStyle(BOLD, false);
228 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false));
230 // Apply a color and style over the text end and check the resulting breaks.
231 // (INT_MAX should be used instead of the text length for the range end)
232 const size_t text_length = render_text->text().length();
233 render_text->ApplyColor(SK_ColorRED, Range(0, text_length));
234 render_text->ApplyStyle(BOLD, true, Range(2, text_length));
235 std::vector<std::pair<size_t, SkColor> > expected_color_end;
236 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED));
237 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end));
238 std::vector<std::pair<size_t, bool> > expected_style_end;
239 expected_style_end.push_back(std::pair<size_t, bool>(0, false));
240 expected_style_end.push_back(std::pair<size_t, bool>(2, true));
241 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end));
243 // Ensure ranged values adjust to accommodate text length changes.
244 render_text->ApplyStyle(ITALIC, true, Range(0, 2));
245 render_text->ApplyStyle(ITALIC, true, Range(3, 6));
246 render_text->ApplyStyle(ITALIC, true, Range(7, text_length));
247 std::vector<std::pair<size_t, bool> > expected_italic;
248 expected_italic.push_back(std::pair<size_t, bool>(0, true));
249 expected_italic.push_back(std::pair<size_t, bool>(2, false));
250 expected_italic.push_back(std::pair<size_t, bool>(3, true));
251 expected_italic.push_back(std::pair<size_t, bool>(6, false));
252 expected_italic.push_back(std::pair<size_t, bool>(7, true));
253 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
255 // Changing the text should clear any breaks except for the first one.
256 render_text->SetText(ASCIIToUTF16("0123456"));
257 expected_italic.resize(1);
258 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
259 render_text->ApplyStyle(ITALIC, false, Range(2, 4));
260 render_text->SetText(ASCIIToUTF16("012345678"));
261 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
262 render_text->ApplyStyle(ITALIC, false, Range(0, 1));
263 render_text->SetText(ASCIIToUTF16("0123456"));
264 expected_italic.begin()->second = false;
265 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
266 render_text->ApplyStyle(ITALIC, true, Range(2, 4));
267 render_text->SetText(ASCIIToUTF16("012345678"));
268 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
270 // TODO(tmoniuszko): Enable when RenderTextMac::IsValidCursorIndex is
271 // implemented.
272 #if !defined(OS_MACOSX)
273 // Styles shouldn't be changed mid-grapheme.
274 render_text->SetText(WideToUTF16(
275 L"0" L"\x0915\x093f" L"1" L"\x0915\x093f" L"2"));
276 render_text->ApplyStyle(UNDERLINE, true, Range(2, 5));
277 std::vector<std::pair<size_t, bool> > expected_underline;
278 expected_underline.push_back(std::pair<size_t, bool>(0, false));
279 expected_underline.push_back(std::pair<size_t, bool>(1, true));
280 expected_underline.push_back(std::pair<size_t, bool>(6, false));
281 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsForTesting(
282 expected_underline));
283 #endif // OS_MACOSX
286 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
287 // does not implement this yet. http://crbug.com/131618
288 #if !defined(OS_MACOSX)
289 void TestVisualCursorMotionInObscuredField(RenderText* render_text,
290 const base::string16& text,
291 bool select) {
292 ASSERT_TRUE(render_text->obscured());
293 render_text->SetText(text);
294 int len = text.length();
295 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, select);
296 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
297 render_text->selection_model());
298 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, select);
299 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
300 for (int j = 1; j <= len; ++j) {
301 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, select);
302 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_BACKWARD),
303 render_text->selection_model());
305 for (int j = len - 1; j >= 0; --j) {
306 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, select);
307 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_FORWARD),
308 render_text->selection_model());
310 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, select);
311 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
312 render_text->selection_model());
313 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, select);
314 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
317 TEST_F(RenderTextTest, ObscuredText) {
318 const base::string16 seuss = ASCIIToUTF16("hop on pop");
319 const base::string16 no_seuss = ASCIIToUTF16("**********");
320 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
322 // GetLayoutText() returns asterisks when the obscured bit is set.
323 render_text->SetText(seuss);
324 render_text->SetObscured(true);
325 EXPECT_EQ(seuss, render_text->text());
326 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
327 render_text->SetObscured(false);
328 EXPECT_EQ(seuss, render_text->text());
329 EXPECT_EQ(seuss, render_text->GetDisplayText());
331 render_text->SetObscured(true);
333 // Surrogate pairs are counted as one code point.
334 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 0};
335 render_text->SetText(invalid_surrogates);
336 EXPECT_EQ(ASCIIToUTF16("**"), render_text->GetDisplayText());
337 const base::char16 valid_surrogates[] = {0xD800, 0xDC00, 0};
338 render_text->SetText(valid_surrogates);
339 EXPECT_EQ(ASCIIToUTF16("*"), render_text->GetDisplayText());
340 EXPECT_EQ(0U, render_text->cursor_position());
341 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
342 EXPECT_EQ(2U, render_text->cursor_position());
344 // Test index conversion and cursor validity with a valid surrogate pair.
345 EXPECT_EQ(0U, render_text->TextIndexToDisplayIndex(0U));
346 EXPECT_EQ(1U, render_text->TextIndexToDisplayIndex(1U));
347 EXPECT_EQ(1U, render_text->TextIndexToDisplayIndex(2U));
348 EXPECT_EQ(0U, render_text->DisplayIndexToTextIndex(0U));
349 EXPECT_EQ(2U, render_text->DisplayIndexToTextIndex(1U));
350 EXPECT_TRUE(render_text->IsValidCursorIndex(0U));
351 EXPECT_FALSE(render_text->IsValidCursorIndex(1U));
352 EXPECT_TRUE(render_text->IsValidCursorIndex(2U));
354 // FindCursorPosition() should not return positions between a surrogate pair.
355 render_text->SetDisplayRect(Rect(0, 0, 20, 20));
356 EXPECT_EQ(render_text->FindCursorPosition(Point(0, 0)).caret_pos(), 0U);
357 EXPECT_EQ(render_text->FindCursorPosition(Point(20, 0)).caret_pos(), 2U);
358 for (int x = -1; x <= 20; ++x) {
359 SelectionModel selection = render_text->FindCursorPosition(Point(x, 0));
360 EXPECT_TRUE(selection.caret_pos() == 0U || selection.caret_pos() == 2U);
363 // GetGlyphBounds() should yield the entire string bounds for text index 0.
364 EXPECT_EQ(render_text->GetStringSize().width(),
365 static_cast<int>(render_text->GetGlyphBounds(0U).length()));
367 // Cursoring is independent of underlying characters when text is obscured.
368 const wchar_t* const texts[] = {
369 kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl,
370 L"hop on pop", // Check LTR word boundaries.
371 L"\x05d0\x05d1 \x05d0\x05d2 \x05d1\x05d2", // Check RTL word boundaries.
373 for (size_t i = 0; i < arraysize(texts); ++i) {
374 base::string16 text = WideToUTF16(texts[i]);
375 TestVisualCursorMotionInObscuredField(render_text.get(), text, false);
376 TestVisualCursorMotionInObscuredField(render_text.get(), text, true);
380 TEST_F(RenderTextTest, RevealObscuredText) {
381 const base::string16 seuss = ASCIIToUTF16("hop on pop");
382 const base::string16 no_seuss = ASCIIToUTF16("**********");
383 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
385 render_text->SetText(seuss);
386 render_text->SetObscured(true);
387 EXPECT_EQ(seuss, render_text->text());
388 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
390 // Valid reveal index and new revealed index clears previous one.
391 render_text->RenderText::SetObscuredRevealIndex(0);
392 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetDisplayText());
393 render_text->RenderText::SetObscuredRevealIndex(1);
394 EXPECT_EQ(ASCIIToUTF16("*o********"), render_text->GetDisplayText());
395 render_text->RenderText::SetObscuredRevealIndex(2);
396 EXPECT_EQ(ASCIIToUTF16("**p*******"), render_text->GetDisplayText());
398 // Invalid reveal index.
399 render_text->RenderText::SetObscuredRevealIndex(-1);
400 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
401 render_text->RenderText::SetObscuredRevealIndex(seuss.length() + 1);
402 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
404 // SetObscured clears the revealed index.
405 render_text->RenderText::SetObscuredRevealIndex(0);
406 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetDisplayText());
407 render_text->SetObscured(false);
408 EXPECT_EQ(seuss, render_text->GetDisplayText());
409 render_text->SetObscured(true);
410 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
412 // SetText clears the revealed index.
413 render_text->SetText(ASCIIToUTF16("new"));
414 EXPECT_EQ(ASCIIToUTF16("***"), render_text->GetDisplayText());
415 render_text->RenderText::SetObscuredRevealIndex(2);
416 EXPECT_EQ(ASCIIToUTF16("**w"), render_text->GetDisplayText());
417 render_text->SetText(ASCIIToUTF16("new longer"));
418 EXPECT_EQ(ASCIIToUTF16("**********"), render_text->GetDisplayText());
420 // Text with invalid surrogates.
421 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 'h', 'o', 'p', 0};
422 render_text->SetText(invalid_surrogates);
423 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetDisplayText());
424 render_text->RenderText::SetObscuredRevealIndex(0);
425 const base::char16 invalid_expect_0[] = {0xDC00, '*', '*', '*', '*', 0};
426 EXPECT_EQ(invalid_expect_0, render_text->GetDisplayText());
427 render_text->RenderText::SetObscuredRevealIndex(1);
428 const base::char16 invalid_expect_1[] = {'*', 0xD800, '*', '*', '*', 0};
429 EXPECT_EQ(invalid_expect_1, render_text->GetDisplayText());
430 render_text->RenderText::SetObscuredRevealIndex(2);
431 EXPECT_EQ(ASCIIToUTF16("**h**"), render_text->GetDisplayText());
433 // Text with valid surrogates before and after the reveal index.
434 const base::char16 valid_surrogates[] =
435 {0xD800, 0xDC00, 'h', 'o', 'p', 0xD800, 0xDC00, 0};
436 render_text->SetText(valid_surrogates);
437 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetDisplayText());
438 render_text->RenderText::SetObscuredRevealIndex(0);
439 const base::char16 valid_expect_0_and_1[] =
440 {0xD800, 0xDC00, '*', '*', '*', '*', 0};
441 EXPECT_EQ(valid_expect_0_and_1, render_text->GetDisplayText());
442 render_text->RenderText::SetObscuredRevealIndex(1);
443 EXPECT_EQ(valid_expect_0_and_1, render_text->GetDisplayText());
444 render_text->RenderText::SetObscuredRevealIndex(2);
445 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text->GetDisplayText());
446 render_text->RenderText::SetObscuredRevealIndex(5);
447 const base::char16 valid_expect_5_and_6[] =
448 {'*', '*', '*', '*', 0xD800, 0xDC00, 0};
449 EXPECT_EQ(valid_expect_5_and_6, render_text->GetDisplayText());
450 render_text->RenderText::SetObscuredRevealIndex(6);
451 EXPECT_EQ(valid_expect_5_and_6, render_text->GetDisplayText());
454 TEST_F(RenderTextTest, ElidedText) {
455 // TODO(skanuj) : Add more test cases for following
456 // - RenderText styles.
457 // - Cross interaction of truncate, elide and obscure.
458 // - ElideText tests from text_elider.cc.
459 struct {
460 const wchar_t* text;
461 const wchar_t* display_text;
462 const bool elision_expected;
463 } cases[] = {
464 // Strings shorter than the elision width should be laid out in full.
465 { L"", L"" , false },
466 { L"M", L"" , false },
467 { L" . ", L" . " , false },
468 { kWeak, kWeak , false },
469 { kLtr, kLtr , false },
470 { kLtrRtl, kLtrRtl , false },
471 { kLtrRtlLtr, kLtrRtlLtr, false },
472 { kRtl, kRtl , false },
473 { kRtlLtr, kRtlLtr , false },
474 { kRtlLtrRtl, kRtlLtrRtl, false },
475 // Strings as long as the elision width should be laid out in full.
476 { L"012ab", L"012ab" , false },
477 // Long strings should be elided with an ellipsis appended at the end.
478 { L"012abc", L"012a\x2026", true },
479 { L"012ab" L"\x5d0\x5d1", L"012a\x2026", true },
480 { L"012a" L"\x5d1" L"b", L"012a\x2026", true },
481 // No RLM marker added as digits (012) have weak directionality.
482 { L"01" L"\x5d0\x5d1\x5d2", L"01\x5d0\x5d1\x2026", true },
483 // RLM marker added as "ab" have strong LTR directionality.
484 { L"ab" L"\x5d0\x5d1\x5d2", L"ab\x5d0\x5d1\x2026\x200f", true },
485 // Complex script is not handled. In this example, the "\x0915\x093f" is a
486 // compound glyph, but only half of it is elided.
487 { L"0123\x0915\x093f", L"0123\x0915\x2026", true },
488 // Surrogate pairs should be elided reasonably enough.
489 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8", false },
490 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x2026" , true },
491 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x2026" , true },
492 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x2026\x200E" , true },
493 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" , true },
496 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
497 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
498 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
500 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
501 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
502 render_text->SetElideBehavior(ELIDE_TAIL);
504 for (size_t i = 0; i < arraysize(cases); i++) {
505 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "] '%ls'", i,
506 cases[i].text));
508 // Compute expected width
509 expected_render_text->SetText(WideToUTF16(cases[i].display_text));
510 int expected_width = expected_render_text->GetContentWidth();
512 base::string16 input = WideToUTF16(cases[i].text);
513 // Extend the input text to ensure that it is wider than the display_text,
514 // and so it will get elided.
515 if (cases[i].elision_expected)
516 input.append(WideToUTF16(L" MMMMMMMMMMM"));
517 render_text->SetText(input);
518 render_text->SetDisplayRect(Rect(0, 0, expected_width, 100));
519 EXPECT_EQ(input, render_text->text());
520 EXPECT_EQ(WideToUTF16(cases[i].display_text),
521 render_text->GetDisplayText());
522 expected_render_text->SetText(base::string16());
526 TEST_F(RenderTextTest, ElidedObscuredText) {
527 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
528 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
529 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
530 expected_render_text->SetText(WideToUTF16(L"**\x2026"));
532 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
533 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
534 render_text->SetElideBehavior(ELIDE_TAIL);
535 render_text->SetDisplayRect(
536 Rect(0, 0, expected_render_text->GetContentWidth(), 100));
537 render_text->SetObscured(true);
538 render_text->SetText(WideToUTF16(L"abcdef"));
539 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
540 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetDisplayText());
543 TEST_F(RenderTextTest, TruncatedText) {
544 struct {
545 const wchar_t* text;
546 const wchar_t* display_text;
547 } cases[] = {
548 // Strings shorter than the truncation length should be laid out in full.
549 { L"", L"" },
550 { kWeak, kWeak },
551 { kLtr, kLtr },
552 { kLtrRtl, kLtrRtl },
553 { kLtrRtlLtr, kLtrRtlLtr },
554 { kRtl, kRtl },
555 { kRtlLtr, kRtlLtr },
556 { kRtlLtrRtl, kRtlLtrRtl },
557 // Strings as long as the truncation length should be laid out in full.
558 { L"01234", L"01234" },
559 // Long strings should be truncated with an ellipsis appended at the end.
560 { L"012345", L"0123\x2026" },
561 { L"012" L" . ", L"012 \x2026" },
562 { L"012" L"abc", L"012a\x2026" },
563 { L"012" L"a" L"\x5d0\x5d1", L"012a\x2026" },
564 { L"012" L"a" L"\x5d1" L"b", L"012a\x2026" },
565 { L"012" L"\x5d0\x5d1\x5d2", L"012\x5d0\x2026" },
566 { L"012" L"\x5d0\x5d1" L"a", L"012\x5d0\x2026" },
567 { L"012" L"\x5d0" L"a" L"\x5d1", L"012\x5d0\x2026" },
568 // Surrogate pairs should be truncated reasonably enough.
569 { L"0123\x0915\x093f", L"0123\x2026" },
570 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8" },
571 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x05bc\x2026" },
572 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x05e9\x2026" },
573 { L"0123\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
574 { L"01234\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
575 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" },
578 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
579 render_text->set_truncate_length(5);
580 for (size_t i = 0; i < arraysize(cases); i++) {
581 render_text->SetText(WideToUTF16(cases[i].text));
582 EXPECT_EQ(WideToUTF16(cases[i].text), render_text->text());
583 EXPECT_EQ(WideToUTF16(cases[i].display_text), render_text->GetDisplayText())
584 << "For case " << i << ": " << cases[i].text;
588 TEST_F(RenderTextTest, TruncatedObscuredText) {
589 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
590 render_text->set_truncate_length(3);
591 render_text->SetObscured(true);
592 render_text->SetText(WideToUTF16(L"abcdef"));
593 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
594 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetDisplayText());
597 TEST_F(RenderTextTest, TruncatedCursorMovementLTR) {
598 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
599 render_text->set_truncate_length(2);
600 render_text->SetText(WideToUTF16(L"abcd"));
602 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
603 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
604 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
605 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
606 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
608 std::vector<SelectionModel> expected;
609 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
610 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
611 // The cursor hops over the ellipsis and elided text to the line end.
612 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
613 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
614 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
616 expected.clear();
617 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
618 // The cursor hops over the elided text to preceeding text.
619 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
620 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
621 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
622 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
625 TEST_F(RenderTextTest, TruncatedCursorMovementRTL) {
626 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
627 render_text->set_truncate_length(2);
628 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3"));
630 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
631 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
632 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
633 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
634 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
636 std::vector<SelectionModel> expected;
637 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
638 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
639 // The cursor hops over the ellipsis and elided text to the line end.
640 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
641 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
642 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
644 expected.clear();
645 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
646 // The cursor hops over the elided text to preceeding text.
647 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
648 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
649 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
650 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
653 TEST_F(RenderTextTest, GetDisplayTextDirection) {
654 struct {
655 const wchar_t* text;
656 const base::i18n::TextDirection text_direction;
657 } cases[] = {
658 // Blank strings and those with no/weak directionality default to LTR.
659 { L"", base::i18n::LEFT_TO_RIGHT },
660 { kWeak, base::i18n::LEFT_TO_RIGHT },
661 // Strings that begin with strong LTR characters.
662 { kLtr, base::i18n::LEFT_TO_RIGHT },
663 { kLtrRtl, base::i18n::LEFT_TO_RIGHT },
664 { kLtrRtlLtr, base::i18n::LEFT_TO_RIGHT },
665 // Strings that begin with strong RTL characters.
666 { kRtl, base::i18n::RIGHT_TO_LEFT },
667 { kRtlLtr, base::i18n::RIGHT_TO_LEFT },
668 { kRtlLtrRtl, base::i18n::RIGHT_TO_LEFT },
671 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
672 const bool was_rtl = base::i18n::IsRTL();
674 for (size_t i = 0; i < 2; ++i) {
675 // Toggle the application default text direction (to try each direction).
676 SetRTL(!base::i18n::IsRTL());
677 const base::i18n::TextDirection ui_direction = base::i18n::IsRTL() ?
678 base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
680 // Ensure that directionality modes yield the correct text directions.
681 for (size_t j = 0; j < arraysize(cases); j++) {
682 render_text->SetText(WideToUTF16(cases[j].text));
683 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
684 EXPECT_EQ(render_text->GetDisplayTextDirection(),cases[j].text_direction);
685 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_UI);
686 EXPECT_EQ(render_text->GetDisplayTextDirection(), ui_direction);
687 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_LTR);
688 EXPECT_EQ(render_text->GetDisplayTextDirection(),
689 base::i18n::LEFT_TO_RIGHT);
690 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_RTL);
691 EXPECT_EQ(render_text->GetDisplayTextDirection(),
692 base::i18n::RIGHT_TO_LEFT);
696 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
698 // Ensure that text changes update the direction for DIRECTIONALITY_FROM_TEXT.
699 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
700 render_text->SetText(WideToUTF16(kLtr));
701 EXPECT_EQ(render_text->GetDisplayTextDirection(), base::i18n::LEFT_TO_RIGHT);
702 render_text->SetText(WideToUTF16(kRtl));
703 EXPECT_EQ(render_text->GetDisplayTextDirection(), base::i18n::RIGHT_TO_LEFT);
706 TEST_F(RenderTextTest, MoveCursorLeftRightInLtr) {
707 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
709 // Pure LTR.
710 render_text->SetText(ASCIIToUTF16("abc"));
711 // |expected| saves the expected SelectionModel when moving cursor from left
712 // to right.
713 std::vector<SelectionModel> expected;
714 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
715 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
716 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
717 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
718 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
719 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
721 expected.clear();
722 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
723 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
724 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
725 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
726 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
727 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
730 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtl) {
731 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
732 // LTR-RTL
733 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
734 // The last one is the expected END position.
735 std::vector<SelectionModel> expected;
736 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
737 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
738 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
739 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
740 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
741 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
742 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
743 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
744 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
746 expected.clear();
747 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
748 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
749 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
750 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
751 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
752 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
753 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
754 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
755 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
758 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtlLtr) {
759 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
760 // LTR-RTL-LTR.
761 render_text->SetText(WideToUTF16(L"a" L"\x05d1" L"b"));
762 std::vector<SelectionModel> expected;
763 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
764 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
765 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
766 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
767 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
768 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
770 expected.clear();
771 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
772 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
773 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
774 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
775 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
776 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
779 TEST_F(RenderTextTest, MoveCursorLeftRightInRtl) {
780 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
781 // Pure RTL.
782 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2"));
783 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
784 std::vector<SelectionModel> expected;
786 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
787 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
788 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
789 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
790 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
791 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
793 expected.clear();
795 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
796 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
797 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
798 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
799 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
800 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
803 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtr) {
804 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
805 // RTL-LTR
806 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2" L"abc"));
807 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
808 std::vector<SelectionModel> expected;
809 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
810 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
811 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
812 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
813 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
814 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
815 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
816 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
817 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
819 expected.clear();
820 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
821 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
822 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
823 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
824 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
825 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
826 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
827 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
828 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
831 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtrRtl) {
832 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
833 // RTL-LTR-RTL.
834 render_text->SetText(WideToUTF16(L"\x05d0" L"a" L"\x05d1"));
835 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
836 std::vector<SelectionModel> expected;
837 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
838 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
839 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
840 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
841 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
842 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
844 expected.clear();
845 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
846 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
847 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
848 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
849 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
850 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
853 // TODO(xji): temporarily disable in platform Win since the complex script
854 // characters turned into empty square due to font regression. So, not able
855 // to test 2 characters belong to the same grapheme.
856 #if defined(OS_LINUX)
857 TEST_F(RenderTextTest, MoveCursorLeftRight_ComplexScript) {
858 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
860 render_text->SetText(WideToUTF16(L"\x0915\x093f\x0915\x094d\x0915"));
861 EXPECT_EQ(0U, render_text->cursor_position());
862 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
863 EXPECT_EQ(2U, render_text->cursor_position());
864 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
865 EXPECT_EQ(4U, render_text->cursor_position());
866 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
867 EXPECT_EQ(5U, render_text->cursor_position());
868 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
869 EXPECT_EQ(5U, render_text->cursor_position());
871 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
872 EXPECT_EQ(4U, render_text->cursor_position());
873 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
874 EXPECT_EQ(2U, render_text->cursor_position());
875 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
876 EXPECT_EQ(0U, render_text->cursor_position());
877 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
878 EXPECT_EQ(0U, render_text->cursor_position());
880 #endif
882 // TODO(ckocagil): Enable for RenderTextHarfBuzz. http://crbug.com/383265
883 #if defined(OS_MACOSX)
884 TEST_F(RenderTextTest, MoveCursorLeftRight_MeiryoUILigatures) {
885 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
886 // Meiryo UI uses single-glyph ligatures for 'ff' and 'ffi', but each letter
887 // (code point) has unique bounds, so mid-glyph cursoring should be possible.
888 render_text->SetFontList(FontList("Meiryo UI, 12px"));
889 render_text->SetText(WideToUTF16(L"ff ffi"));
890 EXPECT_EQ(0U, render_text->cursor_position());
891 for (size_t i = 0; i < render_text->text().length(); ++i) {
892 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
893 EXPECT_EQ(i + 1, render_text->cursor_position());
895 EXPECT_EQ(6U, render_text->cursor_position());
897 #endif // defined(OS_MACOSX)
899 TEST_F(RenderTextTest, GraphemePositions) {
900 // LTR 2-character grapheme, LTR abc, LTR 2-character grapheme.
901 const base::string16 kText1 =
902 WideToUTF16(L"\x0915\x093f" L"abc" L"\x0915\x093f");
904 // LTR ab, LTR 2-character grapheme, LTR cd.
905 const base::string16 kText2 = WideToUTF16(L"ab" L"\x0915\x093f" L"cd");
907 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
908 // two characters forming the surrogate pair 0x0001D11E.
909 const std::string kSurrogate = "\xF0\x9D\x84\x9E";
911 // LTR ab, UTF16 surrogate pair, LTR cd.
912 const base::string16 kText3 = UTF8ToUTF16("ab" + kSurrogate + "cd");
914 struct {
915 base::string16 text;
916 size_t index;
917 size_t expected_previous;
918 size_t expected_next;
919 } cases[] = {
920 { base::string16(), 0, 0, 0 },
921 { base::string16(), 1, 0, 0 },
922 { base::string16(), 50, 0, 0 },
923 { kText1, 0, 0, 2 },
924 { kText1, 1, 0, 2 },
925 { kText1, 2, 0, 3 },
926 { kText1, 3, 2, 4 },
927 { kText1, 4, 3, 5 },
928 { kText1, 5, 4, 7 },
929 { kText1, 6, 5, 7 },
930 { kText1, 7, 5, 7 },
931 { kText1, 8, 7, 7 },
932 { kText1, 50, 7, 7 },
933 { kText2, 0, 0, 1 },
934 { kText2, 1, 0, 2 },
935 { kText2, 2, 1, 4 },
936 { kText2, 3, 2, 4 },
937 { kText2, 4, 2, 5 },
938 { kText2, 5, 4, 6 },
939 { kText2, 6, 5, 6 },
940 { kText2, 7, 6, 6 },
941 { kText2, 50, 6, 6 },
942 { kText3, 0, 0, 1 },
943 { kText3, 1, 0, 2 },
944 { kText3, 2, 1, 4 },
945 { kText3, 3, 2, 4 },
946 { kText3, 4, 2, 5 },
947 { kText3, 5, 4, 6 },
948 { kText3, 6, 5, 6 },
949 { kText3, 7, 6, 6 },
950 { kText3, 50, 6, 6 },
953 #if defined(OS_WIN)
954 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
955 if (base::win::GetVersion() < base::win::VERSION_VISTA)
956 return;
957 #endif
959 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
960 for (size_t i = 0; i < arraysize(cases); i++) {
961 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
962 render_text->SetText(cases[i].text);
964 size_t next = render_text->IndexOfAdjacentGrapheme(cases[i].index,
965 CURSOR_FORWARD);
966 EXPECT_EQ(cases[i].expected_next, next);
967 EXPECT_TRUE(render_text->IsValidCursorIndex(next));
969 size_t previous = render_text->IndexOfAdjacentGrapheme(cases[i].index,
970 CURSOR_BACKWARD);
971 EXPECT_EQ(cases[i].expected_previous, previous);
972 EXPECT_TRUE(render_text->IsValidCursorIndex(previous));
976 TEST_F(RenderTextTest, MidGraphemeSelectionBounds) {
977 #if defined(OS_WIN)
978 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
979 if (base::win::GetVersion() < base::win::VERSION_VISTA)
980 return;
981 #endif
983 // Test that selection bounds may be set amid multi-character graphemes.
984 const base::string16 kHindi = WideToUTF16(L"\x0915\x093f");
985 const base::string16 kThai = WideToUTF16(L"\x0e08\x0e33");
986 const base::string16 cases[] = { kHindi, kThai };
988 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
989 for (size_t i = 0; i < arraysize(cases); i++) {
990 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
991 render_text->SetText(cases[i]);
992 EXPECT_TRUE(render_text->IsValidLogicalIndex(1));
993 #if !defined(OS_MACOSX)
994 EXPECT_FALSE(render_text->IsValidCursorIndex(1));
995 #endif
996 EXPECT_TRUE(render_text->SelectRange(Range(2, 1)));
997 EXPECT_EQ(Range(2, 1), render_text->selection());
998 EXPECT_EQ(1U, render_text->cursor_position());
999 // Although selection bounds may be set within a multi-character grapheme,
1000 // cursor movement (e.g. via arrow key) should avoid those indices.
1001 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1002 EXPECT_EQ(0U, render_text->cursor_position());
1003 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1004 EXPECT_EQ(2U, render_text->cursor_position());
1008 TEST_F(RenderTextTest, FindCursorPosition) {
1009 const wchar_t* kTestStrings[] = { kLtrRtl, kLtrRtlLtr, kRtlLtr, kRtlLtrRtl };
1010 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1011 render_text->SetDisplayRect(Rect(0, 0, 100, 20));
1012 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1013 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS "]", i));
1014 render_text->SetText(WideToUTF16(kTestStrings[i]));
1015 for(size_t j = 0; j < render_text->text().length(); ++j) {
1016 const Range range(render_text->GetGlyphBounds(j));
1017 // Test a point just inside the leading edge of the glyph bounds.
1018 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1;
1019 EXPECT_EQ(j, render_text->FindCursorPosition(Point(x, 0)).caret_pos());
1024 TEST_F(RenderTextTest, EdgeSelectionModels) {
1025 // Simple Latin text.
1026 const base::string16 kLatin = WideToUTF16(L"abc");
1027 // LTR 2-character grapheme.
1028 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f");
1029 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
1030 const base::string16 kHindiLatin =
1031 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f");
1032 // RTL 2-character grapheme.
1033 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8");
1034 // RTL 2-character grapheme, LTR a, RTL 2-character grapheme.
1035 const base::string16 kHebrewLatin =
1036 WideToUTF16(L"\x05e0\x05b8" L"a" L"\x05e0\x05b8");
1038 struct {
1039 base::string16 text;
1040 base::i18n::TextDirection expected_text_direction;
1041 } cases[] = {
1042 { base::string16(), base::i18n::LEFT_TO_RIGHT },
1043 { kLatin, base::i18n::LEFT_TO_RIGHT },
1044 { kLTRGrapheme, base::i18n::LEFT_TO_RIGHT },
1045 { kHindiLatin, base::i18n::LEFT_TO_RIGHT },
1046 { kRTLGrapheme, base::i18n::RIGHT_TO_LEFT },
1047 { kHebrewLatin, base::i18n::RIGHT_TO_LEFT },
1050 #if defined(OS_WIN)
1051 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1052 if (base::win::GetVersion() < base::win::VERSION_VISTA)
1053 return;
1054 #endif
1056 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1057 for (size_t i = 0; i < arraysize(cases); i++) {
1058 render_text->SetText(cases[i].text);
1059 bool ltr = (cases[i].expected_text_direction == base::i18n::LEFT_TO_RIGHT);
1061 SelectionModel start_edge =
1062 render_text->EdgeSelectionModel(ltr ? CURSOR_LEFT : CURSOR_RIGHT);
1063 EXPECT_EQ(start_edge, SelectionModel(0, CURSOR_BACKWARD));
1065 SelectionModel end_edge =
1066 render_text->EdgeSelectionModel(ltr ? CURSOR_RIGHT : CURSOR_LEFT);
1067 EXPECT_EQ(end_edge, SelectionModel(cases[i].text.length(), CURSOR_FORWARD));
1071 TEST_F(RenderTextTest, SelectAll) {
1072 const wchar_t* const cases[] =
1073 { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl };
1075 // Ensure that SelectAll respects the |reversed| argument regardless of
1076 // application locale and text content directionality.
1077 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1078 const SelectionModel expected_reversed(Range(3, 0), CURSOR_FORWARD);
1079 const SelectionModel expected_forwards(Range(0, 3), CURSOR_BACKWARD);
1080 const bool was_rtl = base::i18n::IsRTL();
1082 for (size_t i = 0; i < 2; ++i) {
1083 SetRTL(!base::i18n::IsRTL());
1084 // Test that an empty string produces an empty selection model.
1085 render_text->SetText(base::string16());
1086 EXPECT_EQ(render_text->selection_model(), SelectionModel());
1088 // Test the weak, LTR, RTL, and Bidi string cases.
1089 for (size_t j = 0; j < arraysize(cases); j++) {
1090 render_text->SetText(WideToUTF16(cases[j]));
1091 render_text->SelectAll(false);
1092 EXPECT_EQ(render_text->selection_model(), expected_forwards);
1093 render_text->SelectAll(true);
1094 EXPECT_EQ(render_text->selection_model(), expected_reversed);
1098 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1101 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) {
1102 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1103 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
1104 // Left arrow on select ranging (6, 4).
1105 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1106 EXPECT_EQ(Range(6), render_text->selection());
1107 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1108 EXPECT_EQ(Range(4), render_text->selection());
1109 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1110 EXPECT_EQ(Range(5), render_text->selection());
1111 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1112 EXPECT_EQ(Range(6), render_text->selection());
1113 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1114 EXPECT_EQ(Range(6, 5), render_text->selection());
1115 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1116 EXPECT_EQ(Range(6, 4), render_text->selection());
1117 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1118 EXPECT_EQ(Range(6), render_text->selection());
1120 // Right arrow on select ranging (4, 6).
1121 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1122 EXPECT_EQ(Range(0), render_text->selection());
1123 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1124 EXPECT_EQ(Range(1), render_text->selection());
1125 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1126 EXPECT_EQ(Range(2), render_text->selection());
1127 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1128 EXPECT_EQ(Range(3), render_text->selection());
1129 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1130 EXPECT_EQ(Range(5), render_text->selection());
1131 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1132 EXPECT_EQ(Range(4), render_text->selection());
1133 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1134 EXPECT_EQ(Range(4, 5), render_text->selection());
1135 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1136 EXPECT_EQ(Range(4, 6), render_text->selection());
1137 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1138 EXPECT_EQ(Range(4), render_text->selection());
1141 TEST_F(RenderTextTest, CenteredDisplayOffset) {
1142 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1143 render_text->SetText(ASCIIToUTF16("abcdefghij"));
1144 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1146 const int kEnlargement = 10;
1147 const int content_width = render_text->GetContentWidth();
1148 Rect display_rect(0, 0, content_width / 2,
1149 render_text->font_list().GetHeight());
1150 render_text->SetDisplayRect(display_rect);
1152 // Move the cursor to the beginning of the text and, by checking the cursor
1153 // bounds, make sure no empty space is to the left of the text.
1154 render_text->SetCursorPosition(0);
1155 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1157 // Widen the display rect and, by checking the cursor bounds, make sure no
1158 // empty space is introduced to the left of the text.
1159 display_rect.Inset(0, 0, -kEnlargement, 0);
1160 render_text->SetDisplayRect(display_rect);
1161 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1163 // Move the cursor to the end of the text and, by checking the cursor bounds,
1164 // make sure no empty space is to the right of the text.
1165 render_text->SetCursorPosition(render_text->text().length());
1166 EXPECT_EQ(display_rect.right(),
1167 render_text->GetUpdatedCursorBounds().right());
1169 // Widen the display rect and, by checking the cursor bounds, make sure no
1170 // empty space is introduced to the right of the text.
1171 display_rect.Inset(0, 0, -kEnlargement, 0);
1172 render_text->SetDisplayRect(display_rect);
1173 EXPECT_EQ(display_rect.right(),
1174 render_text->GetUpdatedCursorBounds().right());
1176 #endif // !defined(OS_MACOSX)
1178 // TODO(xji): Make these work on Windows.
1179 #if defined(OS_LINUX)
1180 void MoveLeftRightByWordVerifier(RenderText* render_text,
1181 const wchar_t* str) {
1182 render_text->SetText(WideToUTF16(str));
1184 // Test moving by word from left ro right.
1185 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1186 bool first_word = true;
1187 while (true) {
1188 // First, test moving by word from a word break position, such as from
1189 // "|abc def" to "abc| def".
1190 SelectionModel start = render_text->selection_model();
1191 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1192 SelectionModel end = render_text->selection_model();
1193 if (end == start) // reach the end.
1194 break;
1196 // For testing simplicity, each word is a 3-character word.
1197 int num_of_character_moves = first_word ? 3 : 4;
1198 first_word = false;
1199 render_text->MoveCursorTo(start);
1200 for (int j = 0; j < num_of_character_moves; ++j)
1201 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1202 EXPECT_EQ(end, render_text->selection_model());
1204 // Then, test moving by word from positions inside the word, such as from
1205 // "a|bc def" to "abc| def", and from "ab|c def" to "abc| def".
1206 for (int j = 1; j < num_of_character_moves; ++j) {
1207 render_text->MoveCursorTo(start);
1208 for (int k = 0; k < j; ++k)
1209 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1210 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1211 EXPECT_EQ(end, render_text->selection_model());
1215 // Test moving by word from right to left.
1216 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1217 first_word = true;
1218 while (true) {
1219 SelectionModel start = render_text->selection_model();
1220 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1221 SelectionModel end = render_text->selection_model();
1222 if (end == start) // reach the end.
1223 break;
1225 int num_of_character_moves = first_word ? 3 : 4;
1226 first_word = false;
1227 render_text->MoveCursorTo(start);
1228 for (int j = 0; j < num_of_character_moves; ++j)
1229 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1230 EXPECT_EQ(end, render_text->selection_model());
1232 for (int j = 1; j < num_of_character_moves; ++j) {
1233 render_text->MoveCursorTo(start);
1234 for (int k = 0; k < j; ++k)
1235 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1236 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1237 EXPECT_EQ(end, render_text->selection_model());
1242 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText) {
1243 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1245 // For testing simplicity, each word is a 3-character word.
1246 std::vector<const wchar_t*> test;
1247 test.push_back(L"abc");
1248 test.push_back(L"abc def");
1249 test.push_back(L"\x05E1\x05E2\x05E3");
1250 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1251 test.push_back(L"abc \x05E1\x05E2\x05E3");
1252 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1253 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1254 L" \x05E7\x05E8\x05E9");
1256 test.push_back(L"abc \x05E1\x05E2\x05E3 hij");
1257 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 hij opq");
1258 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1259 L" \x05E7\x05E8\x05E9" L" opq rst uvw");
1261 test.push_back(L"\x05E1\x05E2\x05E3 abc");
1262 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 abc def");
1263 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 \x05E7\x05E8\x05E9"
1264 L" abc def hij");
1266 test.push_back(L"\x05D1\x05D2\x05D3 abc \x05E1\x05E2\x05E3");
1267 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 abc def"
1268 L" \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1269 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 \x05D7\x05D8\x05D9"
1270 L" abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1271 L" \x05E7\x05E8\x05E9");
1273 for (size_t i = 0; i < test.size(); ++i)
1274 MoveLeftRightByWordVerifier(render_text.get(), test[i]);
1277 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText_TestEndOfText) {
1278 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1280 render_text->SetText(WideToUTF16(L"ab\x05E1"));
1281 // Moving the cursor by word from "abC|" to the left should return "|abC".
1282 // But since end of text is always treated as a word break, it returns
1283 // position "ab|C".
1284 // TODO(xji): Need to make it work as expected.
1285 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1286 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1287 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1289 // Moving the cursor by word from "|abC" to the right returns "abC|".
1290 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1291 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1292 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1294 render_text->SetText(WideToUTF16(L"\x05E1\x05E2" L"a"));
1295 // For logical text "BCa", moving the cursor by word from "aCB|" to the left
1296 // returns "|aCB".
1297 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1298 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1299 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1301 // Moving the cursor by word from "|aCB" to the right should return "aCB|".
1302 // But since end of text is always treated as a word break, it returns
1303 // position "a|CB".
1304 // TODO(xji): Need to make it work as expected.
1305 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1306 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1307 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1310 TEST_F(RenderTextTest, MoveLeftRightByWordInTextWithMultiSpaces) {
1311 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1312 render_text->SetText(WideToUTF16(L"abc def"));
1313 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1314 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1315 EXPECT_EQ(11U, render_text->cursor_position());
1317 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1318 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1319 EXPECT_EQ(0U, render_text->cursor_position());
1322 TEST_F(RenderTextTest, MoveLeftRightByWordInChineseText) {
1323 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1324 render_text->SetText(WideToUTF16(L"\x6211\x4EEC\x53BB\x516C\x56ED\x73A9"));
1325 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1326 EXPECT_EQ(0U, render_text->cursor_position());
1327 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1328 EXPECT_EQ(2U, render_text->cursor_position());
1329 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1330 EXPECT_EQ(3U, render_text->cursor_position());
1331 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1332 EXPECT_EQ(5U, render_text->cursor_position());
1333 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1334 EXPECT_EQ(6U, render_text->cursor_position());
1335 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1336 EXPECT_EQ(6U, render_text->cursor_position());
1338 #endif
1340 TEST_F(RenderTextTest, StringSizeSanity) {
1341 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1342 render_text->SetText(UTF8ToUTF16("Hello World"));
1343 const Size string_size = render_text->GetStringSize();
1344 EXPECT_GT(string_size.width(), 0);
1345 EXPECT_GT(string_size.height(), 0);
1348 TEST_F(RenderTextTest, StringSizeLongStrings) {
1349 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1350 Size previous_string_size;
1351 for (size_t length = 10; length < 1000000; length *= 10) {
1352 render_text->SetText(base::string16(length, 'a'));
1353 const Size string_size = render_text->GetStringSize();
1354 EXPECT_GT(string_size.width(), previous_string_size.width());
1355 EXPECT_GT(string_size.height(), 0);
1356 previous_string_size = string_size;
1360 // TODO(asvitkine): This test fails because PlatformFontMac uses point font
1361 // sizes instead of pixel sizes like other implementations.
1362 #if !defined(OS_MACOSX)
1363 TEST_F(RenderTextTest, StringSizeEmptyString) {
1364 // Ascent and descent of Arial and Symbol are different on most platforms.
1365 const FontList font_list("Arial,Symbol, 16px");
1366 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1367 render_text->SetFontList(font_list);
1368 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight()));
1370 // The empty string respects FontList metrics for non-zero height
1371 // and baseline.
1372 render_text->SetText(base::string16());
1373 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1374 EXPECT_EQ(0, render_text->GetStringSize().width());
1375 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1377 render_text->SetText(UTF8ToUTF16(" "));
1378 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1379 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1381 #endif // !defined(OS_MACOSX)
1383 TEST_F(RenderTextTest, StringSizeRespectsFontListMetrics) {
1384 // Check that Arial and Symbol have different font metrics.
1385 Font arial_font("Arial", 16);
1386 ASSERT_EQ("arial",
1387 base::StringToLowerASCII(arial_font.GetActualFontNameForTesting()));
1388 Font symbol_font("Symbol", 16);
1389 ASSERT_EQ("symbol",
1390 base::StringToLowerASCII(
1391 symbol_font.GetActualFontNameForTesting()));
1392 EXPECT_NE(arial_font.GetHeight(), symbol_font.GetHeight());
1393 EXPECT_NE(arial_font.GetBaseline(), symbol_font.GetBaseline());
1394 // "a" should be rendered with Arial, not with Symbol.
1395 const char* arial_font_text = "a";
1396 // "®" (registered trademark symbol) should be rendered with Symbol,
1397 // not with Arial.
1398 const char* symbol_font_text = "\xC2\xAE";
1400 Font smaller_font = arial_font;
1401 Font larger_font = symbol_font;
1402 const char* smaller_font_text = arial_font_text;
1403 const char* larger_font_text = symbol_font_text;
1404 if (symbol_font.GetHeight() < arial_font.GetHeight() &&
1405 symbol_font.GetBaseline() < arial_font.GetBaseline()) {
1406 std::swap(smaller_font, larger_font);
1407 std::swap(smaller_font_text, larger_font_text);
1409 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight());
1410 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline());
1412 // Check |smaller_font_text| is rendered with the smaller font.
1413 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1414 render_text->SetText(UTF8ToUTF16(smaller_font_text));
1415 render_text->SetFontList(FontList(smaller_font));
1416 render_text->SetDisplayRect(Rect(0, 0, 0,
1417 render_text->font_list().GetHeight()));
1418 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height());
1419 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline());
1421 // Layout the same text with mixed fonts. The text should be rendered with
1422 // the smaller font, but the height and baseline are determined with the
1423 // metrics of the font list, which is equal to the larger font.
1424 std::vector<Font> fonts;
1425 fonts.push_back(smaller_font); // The primary font is the smaller font.
1426 fonts.push_back(larger_font);
1427 const FontList font_list(fonts);
1428 render_text->SetFontList(font_list);
1429 render_text->SetDisplayRect(Rect(0, 0, 0,
1430 render_text->font_list().GetHeight()));
1431 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height());
1432 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline());
1433 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1434 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1437 TEST_F(RenderTextTest, MinLineHeight) {
1438 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1440 render_text->SetText(ASCIIToUTF16("Hello!"));
1441 SizeF default_size = render_text->GetStringSizeF();
1442 ASSERT_NE(0, default_size.height());
1443 ASSERT_NE(0, default_size.width());
1445 render_text->SetMinLineHeight(default_size.height() / 2);
1446 EXPECT_EQ(default_size.ToString(), render_text->GetStringSizeF().ToString());
1448 render_text->SetMinLineHeight(default_size.height() * 2);
1449 SizeF taller_size = render_text->GetStringSizeF();
1450 EXPECT_EQ(default_size.height() * 2, taller_size.height());
1451 EXPECT_EQ(default_size.width(), taller_size.width());
1454 TEST_F(RenderTextTest, SetFontList) {
1455 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1456 render_text->SetFontList(FontList("Arial,Symbol, 13px"));
1457 const std::vector<Font>& fonts = render_text->font_list().GetFonts();
1458 ASSERT_EQ(2U, fonts.size());
1459 EXPECT_EQ("Arial", fonts[0].GetFontName());
1460 EXPECT_EQ("Symbol", fonts[1].GetFontName());
1461 EXPECT_EQ(13, render_text->font_list().GetFontSize());
1464 TEST_F(RenderTextTest, StringSizeBoldWidth) {
1465 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1466 render_text->SetText(UTF8ToUTF16("Hello World"));
1468 const int plain_width = render_text->GetStringSize().width();
1469 EXPECT_GT(plain_width, 0);
1471 // Apply a bold style and check that the new width is greater.
1472 render_text->SetStyle(BOLD, true);
1473 const int bold_width = render_text->GetStringSize().width();
1474 EXPECT_GT(bold_width, plain_width);
1476 // Now, apply a plain style over the first word only.
1477 render_text->ApplyStyle(BOLD, false, Range(0, 5));
1478 const int plain_bold_width = render_text->GetStringSize().width();
1479 EXPECT_GT(plain_bold_width, plain_width);
1480 EXPECT_LT(plain_bold_width, bold_width);
1483 TEST_F(RenderTextTest, StringSizeHeight) {
1484 base::string16 cases[] = {
1485 WideToUTF16(L"Hello World!"), // English
1486 WideToUTF16(L"\x6328\x62f6"), // Japanese
1487 WideToUTF16(L"\x0915\x093f"), // Hindi
1488 WideToUTF16(L"\x05e0\x05b8"), // Hebrew
1491 const FontList default_font_list;
1492 const FontList& larger_font_list = default_font_list.DeriveWithSizeDelta(24);
1493 EXPECT_GT(larger_font_list.GetHeight(), default_font_list.GetHeight());
1495 for (size_t i = 0; i < arraysize(cases); i++) {
1496 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1497 render_text->SetFontList(default_font_list);
1498 render_text->SetText(cases[i]);
1500 const int height1 = render_text->GetStringSize().height();
1501 EXPECT_GT(height1, 0);
1503 // Check that setting the larger font increases the height.
1504 render_text->SetFontList(larger_font_list);
1505 const int height2 = render_text->GetStringSize().height();
1506 EXPECT_GT(height2, height1);
1510 TEST_F(RenderTextTest, GetBaselineSanity) {
1511 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1512 render_text->SetText(UTF8ToUTF16("Hello World"));
1513 const int baseline = render_text->GetBaseline();
1514 EXPECT_GT(baseline, 0);
1517 TEST_F(RenderTextTest, CursorBoundsInReplacementMode) {
1518 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1519 render_text->SetText(ASCIIToUTF16("abcdefg"));
1520 render_text->SetDisplayRect(Rect(100, 17));
1521 SelectionModel sel_b(1, CURSOR_FORWARD);
1522 SelectionModel sel_c(2, CURSOR_FORWARD);
1523 Rect cursor_around_b = render_text->GetCursorBounds(sel_b, false);
1524 Rect cursor_before_b = render_text->GetCursorBounds(sel_b, true);
1525 Rect cursor_before_c = render_text->GetCursorBounds(sel_c, true);
1526 EXPECT_EQ(cursor_around_b.x(), cursor_before_b.x());
1527 EXPECT_EQ(cursor_around_b.right(), cursor_before_c.x());
1530 TEST_F(RenderTextTest, GetTextOffset) {
1531 // The default horizontal text offset differs for LTR and RTL, and is only set
1532 // when the RenderText object is created. This test will check the default in
1533 // LTR mode, and the next test will check the RTL default.
1534 const bool was_rtl = base::i18n::IsRTL();
1535 SetRTL(false);
1536 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1537 render_text->SetText(ASCIIToUTF16("abcdefg"));
1538 render_text->SetFontList(FontList("Arial, 13px"));
1540 // Set display area's size equal to the font size.
1541 const Size font_size(render_text->GetContentWidth(),
1542 render_text->font_list().GetHeight());
1543 Rect display_rect(font_size);
1544 render_text->SetDisplayRect(display_rect);
1546 Vector2d offset = render_text->GetLineOffset(0);
1547 EXPECT_TRUE(offset.IsZero());
1549 const int kEnlargementX = 2;
1550 display_rect.Inset(0, 0, -kEnlargementX, 0);
1551 render_text->SetDisplayRect(display_rect);
1553 // Check the default horizontal alignment.
1554 offset = render_text->GetLineOffset(0);
1555 EXPECT_EQ(0, offset.x());
1557 // Check explicitly setting the horizontal alignment.
1558 render_text->SetHorizontalAlignment(ALIGN_LEFT);
1559 offset = render_text->GetLineOffset(0);
1560 EXPECT_EQ(0, offset.x());
1561 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1562 offset = render_text->GetLineOffset(0);
1563 EXPECT_EQ(kEnlargementX / 2, offset.x());
1564 render_text->SetHorizontalAlignment(ALIGN_RIGHT);
1565 offset = render_text->GetLineOffset(0);
1566 EXPECT_EQ(kEnlargementX, offset.x());
1568 // Check that text is vertically centered within taller display rects.
1569 const int kEnlargementY = display_rect.height();
1570 display_rect.Inset(0, 0, 0, -kEnlargementY);
1571 render_text->SetDisplayRect(display_rect);
1572 const Vector2d prev_offset = render_text->GetLineOffset(0);
1573 display_rect.Inset(0, 0, 0, -2 * kEnlargementY);
1574 render_text->SetDisplayRect(display_rect);
1575 offset = render_text->GetLineOffset(0);
1576 EXPECT_EQ(prev_offset.y() + kEnlargementY, offset.y());
1578 SetRTL(was_rtl);
1581 TEST_F(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL) {
1582 // This only checks the default horizontal alignment in RTL mode; all other
1583 // GetLineOffset(0) attributes are checked by the test above.
1584 const bool was_rtl = base::i18n::IsRTL();
1585 SetRTL(true);
1586 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1587 render_text->SetText(ASCIIToUTF16("abcdefg"));
1588 render_text->SetFontList(FontList("Arial, 13px"));
1589 const int kEnlargement = 2;
1590 const Size font_size(render_text->GetContentWidth() + kEnlargement,
1591 render_text->GetStringSize().height());
1592 Rect display_rect(font_size);
1593 render_text->SetDisplayRect(display_rect);
1594 Vector2d offset = render_text->GetLineOffset(0);
1595 EXPECT_EQ(kEnlargement, offset.x());
1596 SetRTL(was_rtl);
1599 TEST_F(RenderTextTest, SetDisplayOffset) {
1600 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1601 render_text->SetText(ASCIIToUTF16("abcdefg"));
1602 render_text->SetFontList(FontList("Arial, 13px"));
1604 const Size font_size(render_text->GetContentWidth(),
1605 render_text->font_list().GetHeight());
1606 const int kEnlargement = 10;
1608 // Set display width |kEnlargement| pixels greater than content width and test
1609 // different possible situations. In this case the only possible display
1610 // offset is zero.
1611 Rect display_rect(font_size);
1612 display_rect.Inset(0, 0, -kEnlargement, 0);
1613 render_text->SetDisplayRect(display_rect);
1615 struct {
1616 HorizontalAlignment alignment;
1617 int offset;
1618 } small_content_cases[] = {
1619 { ALIGN_LEFT, -kEnlargement },
1620 { ALIGN_LEFT, 0 },
1621 { ALIGN_LEFT, kEnlargement },
1622 { ALIGN_RIGHT, -kEnlargement },
1623 { ALIGN_RIGHT, 0 },
1624 { ALIGN_RIGHT, kEnlargement },
1625 { ALIGN_CENTER, -kEnlargement },
1626 { ALIGN_CENTER, 0 },
1627 { ALIGN_CENTER, kEnlargement },
1630 for (size_t i = 0; i < arraysize(small_content_cases); i++) {
1631 render_text->SetHorizontalAlignment(small_content_cases[i].alignment);
1632 render_text->SetDisplayOffset(small_content_cases[i].offset);
1633 EXPECT_EQ(0, render_text->GetUpdatedDisplayOffset().x());
1636 // Set display width |kEnlargement| pixels less than content width and test
1637 // different possible situations.
1638 display_rect = Rect(font_size);
1639 display_rect.Inset(0, 0, kEnlargement, 0);
1640 render_text->SetDisplayRect(display_rect);
1642 struct {
1643 HorizontalAlignment alignment;
1644 int offset;
1645 int expected_offset;
1646 } large_content_cases[] = {
1647 // When text is left-aligned, display offset can be in range
1648 // [-kEnlargement, 0].
1649 { ALIGN_LEFT, -2 * kEnlargement, -kEnlargement },
1650 { ALIGN_LEFT, -kEnlargement / 2, -kEnlargement / 2 },
1651 { ALIGN_LEFT, kEnlargement, 0 },
1652 // When text is right-aligned, display offset can be in range
1653 // [0, kEnlargement].
1654 { ALIGN_RIGHT, -kEnlargement, 0 },
1655 { ALIGN_RIGHT, kEnlargement / 2, kEnlargement / 2 },
1656 { ALIGN_RIGHT, 2 * kEnlargement, kEnlargement },
1657 // When text is center-aligned, display offset can be in range
1658 // [-kEnlargement / 2 - 1, (kEnlargement - 1) / 2].
1659 { ALIGN_CENTER, -kEnlargement, -kEnlargement / 2 - 1 },
1660 { ALIGN_CENTER, -kEnlargement / 4, -kEnlargement / 4 },
1661 { ALIGN_CENTER, kEnlargement / 4, kEnlargement / 4 },
1662 { ALIGN_CENTER, kEnlargement, (kEnlargement - 1) / 2 },
1665 for (size_t i = 0; i < arraysize(large_content_cases); i++) {
1666 render_text->SetHorizontalAlignment(large_content_cases[i].alignment);
1667 render_text->SetDisplayOffset(large_content_cases[i].offset);
1668 EXPECT_EQ(large_content_cases[i].expected_offset,
1669 render_text->GetUpdatedDisplayOffset().x());
1673 TEST_F(RenderTextTest, SameFontForParentheses) {
1674 struct {
1675 const base::char16 left_char;
1676 const base::char16 right_char;
1677 } punctuation_pairs[] = {
1678 { '(', ')' },
1679 { '{', '}' },
1680 { '<', '>' },
1682 struct {
1683 base::string16 text;
1684 } cases[] = {
1685 // English(English)
1686 { WideToUTF16(L"Hello World(a)") },
1687 // English(English)English
1688 { WideToUTF16(L"Hello World(a)Hello World") },
1690 // Japanese(English)
1691 { WideToUTF16(L"\x6328\x62f6(a)") },
1692 // Japanese(English)Japanese
1693 { WideToUTF16(L"\x6328\x62f6(a)\x6328\x62f6") },
1694 // English(Japanese)English
1695 { WideToUTF16(L"Hello World(\x6328\x62f6)Hello World") },
1697 // Hindi(English)
1698 { WideToUTF16(L"\x0915\x093f(a)") },
1699 // Hindi(English)Hindi
1700 { WideToUTF16(L"\x0915\x093f(a)\x0915\x093f") },
1701 // English(Hindi)English
1702 { WideToUTF16(L"Hello World(\x0915\x093f)Hello World") },
1704 // Hebrew(English)
1705 { WideToUTF16(L"\x05e0\x05b8(a)") },
1706 // Hebrew(English)Hebrew
1707 { WideToUTF16(L"\x05e0\x05b8(a)\x05e0\x05b8") },
1708 // English(Hebrew)English
1709 { WideToUTF16(L"Hello World(\x05e0\x05b8)Hello World") },
1712 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1713 for (size_t i = 0; i < arraysize(cases); ++i) {
1714 base::string16 text = cases[i].text;
1715 const size_t start_paren_char_index = text.find('(');
1716 ASSERT_NE(base::string16::npos, start_paren_char_index);
1717 const size_t end_paren_char_index = text.find(')');
1718 ASSERT_NE(base::string16::npos, end_paren_char_index);
1720 for (size_t j = 0; j < arraysize(punctuation_pairs); ++j) {
1721 text[start_paren_char_index] = punctuation_pairs[j].left_char;
1722 text[end_paren_char_index] = punctuation_pairs[j].right_char;
1723 render_text->SetText(text);
1725 const std::vector<RenderText::FontSpan> spans =
1726 render_text->GetFontSpansForTesting();
1728 int start_paren_span_index = -1;
1729 int end_paren_span_index = -1;
1730 for (size_t k = 0; k < spans.size(); ++k) {
1731 if (IndexInRange(spans[k].second, start_paren_char_index))
1732 start_paren_span_index = k;
1733 if (IndexInRange(spans[k].second, end_paren_char_index))
1734 end_paren_span_index = k;
1736 ASSERT_NE(-1, start_paren_span_index);
1737 ASSERT_NE(-1, end_paren_span_index);
1739 const Font& start_font = spans[start_paren_span_index].first;
1740 const Font& end_font = spans[end_paren_span_index].first;
1741 EXPECT_EQ(start_font.GetFontName(), end_font.GetFontName());
1742 EXPECT_EQ(start_font.GetFontSize(), end_font.GetFontSize());
1743 EXPECT_EQ(start_font.GetStyle(), end_font.GetStyle());
1748 // Make sure the caret width is always >=1 so that the correct
1749 // caret is drawn at high DPI. crbug.com/164100.
1750 TEST_F(RenderTextTest, CaretWidth) {
1751 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1752 render_text->SetText(ASCIIToUTF16("abcdefg"));
1753 EXPECT_GE(render_text->GetUpdatedCursorBounds().width(), 1);
1756 TEST_F(RenderTextTest, SelectWord) {
1757 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1758 render_text->SetText(ASCIIToUTF16(" foo a.bc.d bar"));
1760 struct {
1761 size_t cursor;
1762 size_t selection_start;
1763 size_t selection_end;
1764 } cases[] = {
1765 { 0, 0, 1 },
1766 { 1, 1, 4 },
1767 { 2, 1, 4 },
1768 { 3, 1, 4 },
1769 { 4, 4, 6 },
1770 { 5, 4, 6 },
1771 { 6, 6, 7 },
1772 { 7, 7, 8 },
1773 { 8, 8, 10 },
1774 { 9, 8, 10 },
1775 { 10, 10, 11 },
1776 { 11, 11, 12 },
1777 { 12, 12, 13 },
1778 { 13, 13, 16 },
1779 { 14, 13, 16 },
1780 { 15, 13, 16 },
1781 { 16, 13, 16 },
1784 for (size_t i = 0; i < arraysize(cases); ++i) {
1785 render_text->SetCursorPosition(cases[i].cursor);
1786 render_text->SelectWord();
1787 EXPECT_EQ(Range(cases[i].selection_start, cases[i].selection_end),
1788 render_text->selection());
1792 // Make sure the last word is selected when the cursor is at text.length().
1793 TEST_F(RenderTextTest, LastWordSelected) {
1794 const std::string kTestURL1 = "http://www.google.com";
1795 const std::string kTestURL2 = "http://www.google.com/something/";
1797 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1799 render_text->SetText(ASCIIToUTF16(kTestURL1));
1800 render_text->SetCursorPosition(kTestURL1.length());
1801 render_text->SelectWord();
1802 EXPECT_EQ(ASCIIToUTF16("com"), GetSelectedText(render_text.get()));
1803 EXPECT_FALSE(render_text->selection().is_reversed());
1805 render_text->SetText(ASCIIToUTF16(kTestURL2));
1806 render_text->SetCursorPosition(kTestURL2.length());
1807 render_text->SelectWord();
1808 EXPECT_EQ(ASCIIToUTF16("/"), GetSelectedText(render_text.get()));
1809 EXPECT_FALSE(render_text->selection().is_reversed());
1812 // When given a non-empty selection, SelectWord should expand the selection to
1813 // nearest word boundaries.
1814 TEST_F(RenderTextTest, SelectMultipleWords) {
1815 const std::string kTestURL = "http://www.google.com";
1817 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1819 render_text->SetText(ASCIIToUTF16(kTestURL));
1820 render_text->SelectRange(Range(16, 20));
1821 render_text->SelectWord();
1822 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1823 EXPECT_FALSE(render_text->selection().is_reversed());
1825 // SelectWord should preserve the selection direction.
1826 render_text->SelectRange(Range(20, 16));
1827 render_text->SelectWord();
1828 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1829 EXPECT_TRUE(render_text->selection().is_reversed());
1832 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
1833 // does not implement this yet. http://crbug.com/131618
1834 #if !defined(OS_MACOSX)
1835 TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) {
1836 ASSERT_FALSE(base::i18n::IsRTL());
1837 ASSERT_FALSE(base::i18n::ICUIsRTL());
1839 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1840 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1841 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1842 CURSOR_FORWARD));
1843 int width = render_text->GetStringSize().width();
1844 ASSERT_GT(width, 10);
1846 // Ensure that the cursor is placed at the width of its preceding text.
1847 render_text->SetDisplayRect(Rect(width + 10, 1));
1848 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1850 // Ensure that shrinking the display rectangle keeps the cursor in view.
1851 render_text->SetDisplayRect(Rect(width - 10, 1));
1852 EXPECT_EQ(render_text->display_rect().width(),
1853 render_text->GetUpdatedCursorBounds().right());
1855 // Ensure that the text will pan to fill its expanding display rectangle.
1856 render_text->SetDisplayRect(Rect(width - 5, 1));
1857 EXPECT_EQ(render_text->display_rect().width(),
1858 render_text->GetUpdatedCursorBounds().right());
1860 // Ensure that a sufficiently large display rectangle shows all the text.
1861 render_text->SetDisplayRect(Rect(width + 10, 1));
1862 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1864 // Repeat the test with RTL text.
1865 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1866 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1867 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1868 width = render_text->GetStringSize().width();
1869 ASSERT_GT(width, 10);
1871 // Ensure that the cursor is placed at the width of its preceding text.
1872 render_text->SetDisplayRect(Rect(width + 10, 1));
1873 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1875 // Ensure that shrinking the display rectangle keeps the cursor in view.
1876 render_text->SetDisplayRect(Rect(width - 10, 1));
1877 EXPECT_EQ(render_text->display_rect().width(),
1878 render_text->GetUpdatedCursorBounds().right());
1880 // Ensure that the text will pan to fill its expanding display rectangle.
1881 render_text->SetDisplayRect(Rect(width - 5, 1));
1882 EXPECT_EQ(render_text->display_rect().width(),
1883 render_text->GetUpdatedCursorBounds().right());
1885 // Ensure that a sufficiently large display rectangle shows all the text.
1886 render_text->SetDisplayRect(Rect(width + 10, 1));
1887 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1890 TEST_F(RenderTextTest, DisplayRectShowsCursorRTL) {
1891 // Set the application default text direction to RTL.
1892 const bool was_rtl = base::i18n::IsRTL();
1893 SetRTL(true);
1895 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1896 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1897 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1898 int width = render_text->GetStringSize().width();
1899 ASSERT_GT(width, 10);
1901 // Ensure that the cursor is placed at the width of its preceding text.
1902 render_text->SetDisplayRect(Rect(width + 10, 1));
1903 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1904 render_text->GetUpdatedCursorBounds().x());
1906 // Ensure that shrinking the display rectangle keeps the cursor in view.
1907 render_text->SetDisplayRect(Rect(width - 10, 1));
1908 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1910 // Ensure that the text will pan to fill its expanding display rectangle.
1911 render_text->SetDisplayRect(Rect(width - 5, 1));
1912 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1914 // Ensure that a sufficiently large display rectangle shows all the text.
1915 render_text->SetDisplayRect(Rect(width + 10, 1));
1916 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1917 render_text->GetUpdatedCursorBounds().x());
1919 // Repeat the test with RTL text.
1920 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1921 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1922 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1923 CURSOR_FORWARD));
1924 width = render_text->GetStringSize().width();
1925 ASSERT_GT(width, 10);
1927 // Ensure that the cursor is placed at the width of its preceding text.
1928 render_text->SetDisplayRect(Rect(width + 10, 1));
1929 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1930 render_text->GetUpdatedCursorBounds().x());
1932 // Ensure that shrinking the display rectangle keeps the cursor in view.
1933 render_text->SetDisplayRect(Rect(width - 10, 1));
1934 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1936 // Ensure that the text will pan to fill its expanding display rectangle.
1937 render_text->SetDisplayRect(Rect(width - 5, 1));
1938 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
1940 // Ensure that a sufficiently large display rectangle shows all the text.
1941 render_text->SetDisplayRect(Rect(width + 10, 1));
1942 EXPECT_EQ(render_text->display_rect().width() - width - 1,
1943 render_text->GetUpdatedCursorBounds().x());
1945 // Reset the application default text direction to LTR.
1946 SetRTL(was_rtl);
1947 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1949 #endif // !defined(OS_MACOSX)
1951 // Changing colors between or inside ligated glyphs should not break shaping.
1952 TEST_F(RenderTextTest, SelectionKeepsLigatures) {
1953 const wchar_t* kTestStrings[] = { L"\x644\x623", L"\x633\x627" };
1954 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1955 render_text->set_selection_color(SK_ColorRED);
1956 Canvas canvas;
1958 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1959 render_text->SetText(WideToUTF16(kTestStrings[i]));
1960 const int expected_width = render_text->GetStringSize().width();
1961 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD));
1962 EXPECT_EQ(expected_width, render_text->GetStringSize().width());
1963 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119
1964 render_text->Draw(&canvas);
1965 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1969 // Ensure strings wrap onto multiple lines for a small available width.
1970 TEST_F(RenderTextTest, Multiline_MinWidth) {
1971 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl,
1972 kRtlLtr, kRtlLtrRtl };
1974 RenderTextHarfBuzz render_text;
1975 render_text.SetDisplayRect(Rect(1, 1000));
1976 render_text.SetMultiline(true);
1977 Canvas canvas;
1979 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1980 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1981 render_text.SetText(WideToUTF16(kTestStrings[i]));
1982 render_text.Draw(&canvas);
1983 EXPECT_GT(render_text.lines_.size(), 1U);
1987 // Ensure strings wrap onto multiple lines for a normal available width.
1988 TEST_F(RenderTextTest, Multiline_NormalWidth) {
1989 const struct {
1990 const wchar_t* const text;
1991 const Range first_line_char_range;
1992 const Range second_line_char_range;
1993 bool is_ltr;
1994 } kTestStrings[] = {
1995 { L"abc defg hijkl", Range(0, 9), Range(9, 14), true },
1996 { L"qwertyzxcvbn", Range(0, 10), Range(10, 12), true },
1997 { L"\x0627\x0644\x0644\x063A\x0629 "
1998 L"\x0627\x0644\x0639\x0631\x0628\x064A\x0629",
1999 Range(0, 6), Range(6, 13), false },
2000 { L"\x062A\x0641\x0627\x062D\x05EA\x05E4\x05D5\x05D6\x05D9"
2001 L"\x05DA\x05DB\x05DD", Range(0, 4), Range(4, 12), false }
2004 RenderTextHarfBuzz render_text;
2005 // Specify the fixed width for characters to suppress the possible variations
2006 // of linebreak results.
2007 render_text.set_glyph_width_for_test(5);
2008 render_text.SetDisplayRect(Rect(50, 1000));
2009 render_text.SetMultiline(true);
2010 render_text.SetHorizontalAlignment(ALIGN_TO_HEAD);
2012 Canvas canvas;
2013 TestSkiaTextRenderer renderer(&canvas);
2015 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2016 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2017 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2018 render_text.EnsureLayout();
2019 render_text.DrawVisualTextInternal(&renderer);
2021 ASSERT_EQ(2U, render_text.lines_.size());
2022 ASSERT_EQ(1U, render_text.lines_[0].segments.size());
2023 EXPECT_EQ(kTestStrings[i].first_line_char_range,
2024 render_text.lines_[0].segments[0].char_range);
2025 ASSERT_EQ(1U, render_text.lines_[1].segments.size());
2026 EXPECT_EQ(kTestStrings[i].second_line_char_range,
2027 render_text.lines_[1].segments[0].char_range);
2029 std::vector<TestSkiaTextRenderer::TextLog> text_log;
2030 renderer.GetTextLogAndReset(&text_log);
2031 ASSERT_EQ(2U, text_log.size());
2032 // NOTE: this expectation compares the character length and glyph counts,
2033 // which isn't always equal. This is okay only because all the test
2034 // strings are simple (like, no compound characters nor UTF16-surrogate
2035 // pairs). Be careful in case more complicated test strings are added.
2036 EXPECT_EQ(kTestStrings[i].first_line_char_range.length(),
2037 text_log[0].glyph_count);
2038 EXPECT_EQ(kTestStrings[i].second_line_char_range.length(),
2039 text_log[1].glyph_count);
2040 EXPECT_LT(text_log[0].origin.y(), text_log[1].origin.y());
2041 if (kTestStrings[i].is_ltr) {
2042 EXPECT_EQ(0, text_log[0].origin.x());
2043 EXPECT_EQ(0, text_log[1].origin.x());
2044 } else {
2045 EXPECT_LT(0, text_log[0].origin.x());
2046 EXPECT_LT(0, text_log[1].origin.x());
2051 // Ensure strings don't wrap onto multiple lines for a sufficient available
2052 // width.
2053 TEST_F(RenderTextTest, Multiline_SufficientWidth) {
2054 const wchar_t* kTestStrings[] = { L"", L" ", L".", L" . ", L"abc", L"a b c",
2055 L"\x62E\x628\x632", L"\x62E \x628 \x632" };
2057 RenderTextHarfBuzz render_text;
2058 render_text.SetDisplayRect(Rect(1000, 1000));
2059 render_text.SetMultiline(true);
2060 Canvas canvas;
2062 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2063 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2064 render_text.SetText(WideToUTF16(kTestStrings[i]));
2065 render_text.Draw(&canvas);
2066 EXPECT_EQ(1U, render_text.lines_.size());
2070 TEST_F(RenderTextTest, Multiline_Newline) {
2071 const struct {
2072 const wchar_t* const text;
2073 const size_t lines_count;
2074 // Ranges of the characters on each line preceding the newline.
2075 const Range line_char_ranges[3];
2076 } kTestStrings[] = {
2077 {L"abc\ndef", 2ul, { Range(0, 3), Range(4, 7), Range::InvalidRange() } },
2078 {L"a \n b ", 2ul, { Range(0, 2), Range(3, 6), Range::InvalidRange() } },
2079 {L"ab\n", 2ul, { Range(0, 2), Range(), Range::InvalidRange() } },
2080 {L"a\n\nb", 3ul, { Range(0, 1), Range(), Range(3, 4) } },
2081 {L"\nab", 2ul, { Range(), Range(1, 3), Range::InvalidRange() } },
2082 {L"\n", 2ul, { Range(), Range(), Range::InvalidRange() } },
2085 RenderTextHarfBuzz render_text;
2086 render_text.SetDisplayRect(Rect(200, 1000));
2087 render_text.SetMultiline(true);
2088 Canvas canvas;
2090 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2091 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2092 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2093 render_text.Draw(&canvas);
2094 EXPECT_EQ(kTestStrings[i].lines_count, render_text.lines_.size());
2095 if (kTestStrings[i].lines_count != render_text.lines_.size())
2096 continue;
2098 for (size_t j = 0; j < kTestStrings[i].lines_count; ++j) {
2099 SCOPED_TRACE(base::StringPrintf("Line %" PRIuS "", j));
2100 VerifyLineSegments(kTestStrings[i].line_char_ranges[j],
2101 render_text.lines_[j].segments);
2106 // Make sure that multiline mode ignores elide behavior.
2107 TEST_F(RenderTextTest, Multiline_IgnoreElide) {
2108 const wchar_t kTestString[] =
2109 L"very very very long string xxxxxxxxxxxxxxxxxxxxxxxxxx";
2110 const wchar_t kEllipsis[] = L"\x2026";
2112 RenderTextHarfBuzz render_text;
2113 render_text.SetElideBehavior(ELIDE_TAIL);
2114 render_text.SetDisplayRect(Rect(20, 1000));
2115 render_text.SetText(base::WideToUTF16(kTestString));
2116 EXPECT_NE(base::string16::npos,
2117 render_text.GetDisplayText().find(base::WideToUTF16(kEllipsis)));
2119 render_text.SetMultiline(true);
2120 EXPECT_EQ(base::string16::npos,
2121 render_text.GetDisplayText().find(base::WideToUTF16(kEllipsis)));
2124 TEST_F(RenderTextTest, Multiline_NewlineCharacterReplacement) {
2125 const wchar_t* kTestStrings[] = {
2126 L"abc\ndef", L"a \n b ", L"ab\n", L"a\n\nb", L"\nab", L"\n",
2129 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2130 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2131 RenderTextHarfBuzz render_text;
2132 render_text.SetDisplayRect(Rect(200, 1000));
2133 render_text.SetText(WideToUTF16(kTestStrings[i]));
2135 base::string16 display_text = render_text.GetDisplayText();
2136 // If RenderText is not multiline, the newline characters are replaced
2137 // by symbols, therefore the character should be changed.
2138 EXPECT_NE(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText());
2140 // Setting multiline will fix this, the newline characters will be back
2141 // to the original text.
2142 render_text.SetMultiline(true);
2143 EXPECT_EQ(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText());
2147 // Ensure horizontal alignment works in multiline mode.
2148 TEST_F(RenderTextTest, Multiline_HorizontalAlignment) {
2149 const struct {
2150 const wchar_t* const text;
2151 const gfx::HorizontalAlignment alignment;
2152 } kTestStrings[] = {
2153 { L"abcdefghij\nhijkl", gfx::ALIGN_LEFT },
2154 { L"nhijkl\nabcdefghij", gfx::ALIGN_LEFT },
2155 // hebrew, 2nd line shorter
2156 { L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7\n\x5d0\x5d1\x5d2\x5d3",
2157 gfx::ALIGN_RIGHT },
2158 // hebrew, 2nd line longer
2159 { L"\x5d0\x5d1\x5d2\x5d3\n\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7",
2160 gfx::ALIGN_RIGHT },
2161 // arabic, 2nd line shorter
2162 { L"\x62a\x62b\x62c\x62d\x62e\x62f\x630\n\x660\x661\x662\x663\x664",
2163 gfx::ALIGN_RIGHT },
2164 // arabic, 2nd line longer
2165 { L"\x660\x661\x662\x663\x664\n\x62a\x62b\x62c\x62d\x62e\x62f\x630",
2166 gfx::ALIGN_RIGHT },
2168 const int kGlyphSize = 5;
2169 RenderTextHarfBuzz render_text;
2170 render_text.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
2171 render_text.set_glyph_width_for_test(kGlyphSize);
2172 render_text.SetDisplayRect(Rect(100, 1000));
2173 render_text.SetMultiline(true);
2175 Canvas canvas;
2176 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2177 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "] %ls", i,
2178 kTestStrings[i].text));
2179 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2180 render_text.Draw(&canvas);
2181 ASSERT_LE(2u, render_text.lines().size());
2182 if (kTestStrings[i].alignment == gfx::ALIGN_LEFT) {
2183 EXPECT_EQ(0, render_text.GetAlignmentOffset(0).x());
2184 EXPECT_EQ(0, render_text.GetAlignmentOffset(1).x());
2185 } else {
2186 std::vector<base::string16> lines;
2187 base::SplitString(base::WideToUTF16(kTestStrings[i].text), '\n', &lines);
2188 ASSERT_EQ(2u, lines.size());
2189 int difference = (lines[0].length() - lines[1].length()) * kGlyphSize;
2190 EXPECT_EQ(render_text.GetAlignmentOffset(0).x() + difference,
2191 render_text.GetAlignmentOffset(1).x());
2196 TEST_F(RenderTextTest, NewlineWithoutMultilineFlag) {
2197 const wchar_t* kTestStrings[] = {
2198 L"abc\ndef", L"a \n b ", L"ab\n", L"a\n\nb", L"\nab", L"\n",
2201 RenderTextHarfBuzz render_text;
2202 render_text.SetDisplayRect(Rect(200, 1000));
2203 Canvas canvas;
2205 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2206 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2207 render_text.SetText(WideToUTF16(kTestStrings[i]));
2208 render_text.Draw(&canvas);
2210 EXPECT_EQ(1U, render_text.lines_.size());
2214 // Make sure the horizontal positions of runs in a line (left-to-right for
2215 // LTR languages and right-to-left for RTL languages).
2216 TEST_F(RenderTextTest, HarfBuzz_HorizontalPositions) {
2217 const struct {
2218 const wchar_t* const text;
2219 const Range first_run_char_range;
2220 const Range second_run_char_range;
2221 bool is_rtl;
2222 } kTestStrings[] = {
2223 { L"abc\x3042\x3044\x3046\x3048\x304A", Range(0, 3), Range(3, 8), false },
2224 { L"\x062A\x0641\x0627\x062D"
2225 L"\x05EA\x05E4\x05D5\x05D6\x05D9\x05DA\x05DB\x05DD",
2226 Range(0, 4), Range(4, 12), true },
2229 RenderTextHarfBuzz render_text;
2230 Canvas canvas;
2231 TestSkiaTextRenderer renderer(&canvas);
2233 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2234 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2235 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2237 render_text.EnsureLayout();
2238 const internal::TextRunList* run_list = render_text.GetRunList();
2239 ASSERT_EQ(2U, run_list->runs().size());
2240 EXPECT_EQ(kTestStrings[i].first_run_char_range, run_list->runs()[0]->range);
2241 EXPECT_EQ(kTestStrings[i].second_run_char_range,
2242 run_list->runs()[1]->range);
2243 // If it's RTL, the visual order is reversed.
2244 if (kTestStrings[i].is_rtl) {
2245 EXPECT_EQ(1U, run_list->logical_to_visual(0));
2246 EXPECT_EQ(0U, run_list->logical_to_visual(1));
2247 } else {
2248 EXPECT_EQ(0U, run_list->logical_to_visual(0));
2249 EXPECT_EQ(1U, run_list->logical_to_visual(1));
2252 render_text.DrawVisualTextInternal(&renderer);
2254 std::vector<TestSkiaTextRenderer::TextLog> text_log;
2255 renderer.GetTextLogAndReset(&text_log);
2257 EXPECT_EQ(2U, text_log.size());
2259 // Verifies the DrawText happens in the visual order and left-to-right.
2260 // If the text is RTL, the logically first run should be drawn at last.
2261 EXPECT_EQ(run_list->runs()[run_list->logical_to_visual(0)]->glyph_count,
2262 text_log[0].glyph_count);
2263 EXPECT_EQ(run_list->runs()[run_list->logical_to_visual(1)]->glyph_count,
2264 text_log[1].glyph_count);
2265 EXPECT_LT(text_log[0].origin.x(), text_log[1].origin.x());
2269 // Test TextRunHarfBuzz's cluster finding logic.
2270 TEST_F(RenderTextTest, HarfBuzz_Clusters) {
2271 struct {
2272 uint32 glyph_to_char[4];
2273 Range chars[4];
2274 Range glyphs[4];
2275 bool is_rtl;
2276 } cases[] = {
2277 { // From string "A B C D" to glyphs "a b c d".
2278 { 0, 1, 2, 3 },
2279 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2280 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2281 false
2283 { // From string "A B C D" to glyphs "d c b a".
2284 { 3, 2, 1, 0 },
2285 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2286 { Range(3, 4), Range(2, 3), Range(1, 2), Range(0, 1) },
2287 true
2289 { // From string "A B C D" to glyphs "ab c c d".
2290 { 0, 2, 2, 3 },
2291 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2292 { Range(0, 1), Range(0, 1), Range(1, 3), Range(3, 4) },
2293 false
2295 { // From string "A B C D" to glyphs "d c c ba".
2296 { 3, 2, 2, 0 },
2297 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2298 { Range(3, 4), Range(3, 4), Range(1, 3), Range(0, 1) },
2299 true
2303 internal::TextRunHarfBuzz run;
2304 run.range = Range(0, 4);
2305 run.glyph_count = 4;
2306 run.glyph_to_char.resize(4);
2308 for (size_t i = 0; i < arraysize(cases); ++i) {
2309 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 4,
2310 run.glyph_to_char.begin());
2311 run.is_rtl = cases[i].is_rtl;
2313 for (size_t j = 0; j < 4; ++j) {
2314 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2315 Range chars;
2316 Range glyphs;
2317 run.GetClusterAt(j, &chars, &glyphs);
2318 EXPECT_EQ(cases[i].chars[j], chars);
2319 EXPECT_EQ(cases[i].glyphs[j], glyphs);
2320 EXPECT_EQ(cases[i].glyphs[j], run.CharRangeToGlyphRange(chars));
2325 // Ensure that graphemes with multiple code points do not get split.
2326 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemeCases) {
2327 const wchar_t* cases[] = {
2328 // "A" with a combining umlaut, followed by a "B".
2329 L"A\x0308" L"B",
2330 // Devanagari biconsonantal conjunct "ki", followed by an "a".
2331 L"\x0915\x093f\x0905",
2332 // Thai consonant and vowel pair "cho chan" + "sara am", followed by Thai
2333 // digit 0.
2334 L"\x0e08\x0e33\x0E50",
2337 RenderTextHarfBuzz render_text;
2339 for (size_t i = 0; i < arraysize(cases); ++i) {
2340 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS, i));
2342 base::string16 text = WideToUTF16(cases[i]);
2343 render_text.SetText(text);
2344 render_text.EnsureLayout();
2345 internal::TextRunList* run_list = render_text.GetRunList();
2346 ASSERT_EQ(1U, run_list->size());
2347 internal::TextRunHarfBuzz* run = run_list->runs()[0];
2349 base::i18n::BreakIterator* iter = render_text.grapheme_iterator_.get();
2350 auto first_grapheme_bounds = run->GetGraphemeBounds(iter, 0);
2351 EXPECT_EQ(first_grapheme_bounds, run->GetGraphemeBounds(iter, 1));
2352 auto second_grapheme_bounds = run->GetGraphemeBounds(iter, 2);
2353 EXPECT_EQ(first_grapheme_bounds.end(), second_grapheme_bounds.start());
2357 // Test the partition of a multi-grapheme cluster into grapheme ranges.
2358 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemePartition) {
2359 struct {
2360 uint32 glyph_to_char[2];
2361 Range bounds[4];
2362 bool is_rtl;
2363 } cases[] = {
2364 { // From string "A B C D" to glyphs "a bcd".
2365 { 0, 1 },
2366 { Range(0, 10), Range(10, 13), Range(13, 17), Range(17, 20) },
2367 false
2369 { // From string "A B C D" to glyphs "ab cd".
2370 { 0, 2 },
2371 { Range(0, 5), Range(5, 10), Range(10, 15), Range(15, 20) },
2372 false
2374 { // From string "A B C D" to glyphs "dcb a".
2375 { 1, 0 },
2376 { Range(10, 20), Range(7, 10), Range(3, 7), Range(0, 3) },
2377 true
2379 { // From string "A B C D" to glyphs "dc ba".
2380 { 2, 0 },
2381 { Range(15, 20), Range(10, 15), Range(5, 10), Range(0, 5) },
2382 true
2386 internal::TextRunHarfBuzz run;
2387 run.range = Range(0, 4);
2388 run.glyph_count = 2;
2389 run.glyph_to_char.resize(2);
2390 run.positions.reset(new SkPoint[4]);
2391 run.width = 20;
2393 const base::string16 kString = ASCIIToUTF16("abcd");
2394 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2395 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2396 ASSERT_TRUE(iter->Init());
2398 for (size_t i = 0; i < arraysize(cases); ++i) {
2399 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 2,
2400 run.glyph_to_char.begin());
2401 run.is_rtl = cases[i].is_rtl;
2402 for (int j = 0; j < 2; ++j)
2403 run.positions[j].set(j * 10, 0);
2405 for (size_t j = 0; j < 4; ++j) {
2406 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2407 EXPECT_EQ(cases[i].bounds[j],
2408 internal::RoundRangeF(run.GetGraphemeBounds(iter.get(), j)));
2413 TEST_F(RenderTextTest, HarfBuzz_RunDirection) {
2414 RenderTextHarfBuzz render_text;
2415 const base::string16 mixed =
2416 WideToUTF16(L"\x05D0\x05D1" L"1234" L"\x05D2\x05D3");
2417 render_text.SetText(mixed);
2418 render_text.EnsureLayout();
2419 internal::TextRunList* run_list = render_text.GetRunList();
2420 ASSERT_EQ(3U, run_list->size());
2421 EXPECT_TRUE(run_list->runs()[0]->is_rtl);
2422 EXPECT_FALSE(run_list->runs()[1]->is_rtl);
2423 EXPECT_TRUE(run_list->runs()[2]->is_rtl);
2426 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks) {
2427 RenderTextHarfBuzz render_text;
2429 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2430 render_text.SetText(WideToUTF16(L"x\x25B6y"));
2431 render_text.EnsureLayout();
2432 internal::TextRunList* run_list = render_text.GetRunList();
2433 ASSERT_EQ(3U, run_list->size());
2434 EXPECT_EQ(Range(0, 1), run_list->runs()[0]->range);
2435 EXPECT_EQ(Range(1, 2), run_list->runs()[1]->range);
2436 EXPECT_EQ(Range(2, 3), run_list->runs()[2]->range);
2438 render_text.SetText(WideToUTF16(L"x \x25B6 y"));
2439 render_text.EnsureLayout();
2440 run_list = render_text.GetRunList();
2441 ASSERT_EQ(3U, run_list->size());
2442 EXPECT_EQ(Range(0, 2), run_list->runs()[0]->range);
2443 EXPECT_EQ(Range(2, 3), run_list->runs()[1]->range);
2444 EXPECT_EQ(Range(3, 5), run_list->runs()[2]->range);
2447 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByEmoji) {
2448 RenderTextHarfBuzz render_text;
2450 // \xF0\x9F\x98\x81 (U+1F601) is smile icon emoji. \xE2\x9C\xA8 (U+2728) is
2451 // a sparkle icon. Both can be drawn with color emoji fonts, so runs should be
2452 // separated. See crbug.com/448909
2453 render_text.SetText(UTF8ToUTF16("x\xF0\x9F\x98\x81y\xE2\x9C\xA8"));
2454 render_text.EnsureLayout();
2455 internal::TextRunList* run_list = render_text.GetRunList();
2456 ASSERT_EQ(4U, run_list->size());
2457 EXPECT_EQ(Range(0, 1), run_list->runs()[0]->range);
2458 // The length is 2 since U+1F601 is represented as a surrogate pair in UTF16.
2459 EXPECT_EQ(Range(1, 3), run_list->runs()[1]->range);
2460 EXPECT_EQ(Range(3, 4), run_list->runs()[2]->range);
2461 EXPECT_EQ(Range(4, 5), run_list->runs()[3]->range);
2464 TEST_F(RenderTextTest, GlyphBounds) {
2465 const wchar_t* kTestStrings[] = {
2466 L"asdf 1234 qwer", L"\x0647\x0654", L"\x0645\x0631\x062D\x0628\x0627"
2468 scoped_ptr<RenderText> render_text(new RenderTextHarfBuzz);
2470 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2471 render_text->SetText(WideToUTF16(kTestStrings[i]));
2472 render_text->EnsureLayout();
2474 for (size_t j = 0; j < render_text->text().length(); ++j)
2475 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2479 // Ensure that shaping with a non-existent font does not cause a crash.
2480 TEST_F(RenderTextTest, HarfBuzz_NonExistentFont) {
2481 RenderTextHarfBuzz render_text;
2482 render_text.SetText(ASCIIToUTF16("test"));
2483 render_text.EnsureLayout();
2484 internal::TextRunList* run_list = render_text.GetRunList();
2485 ASSERT_EQ(1U, run_list->size());
2486 internal::TextRunHarfBuzz* run = run_list->runs()[0];
2487 render_text.ShapeRunWithFont(
2488 render_text.text(), "TheFontThatDoesntExist", FontRenderParams(), run);
2491 // Ensure an empty run returns sane values to queries.
2492 TEST_F(RenderTextTest, HarfBuzz_EmptyRun) {
2493 internal::TextRunHarfBuzz run;
2494 const base::string16 kString = ASCIIToUTF16("abcdefgh");
2495 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2496 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2497 ASSERT_TRUE(iter->Init());
2499 run.range = Range(3, 8);
2500 run.glyph_count = 0;
2501 EXPECT_EQ(Range(0, 0), run.CharRangeToGlyphRange(Range(4, 5)));
2502 EXPECT_EQ(Range(0, 0),
2503 internal::RoundRangeF(run.GetGraphemeBounds(iter.get(), 4)));
2504 Range chars;
2505 Range glyphs;
2506 run.GetClusterAt(4, &chars, &glyphs);
2507 EXPECT_EQ(Range(3, 8), chars);
2508 EXPECT_EQ(Range(0, 0), glyphs);
2511 // Ensure a string fits in a display rect with a width equal to the string's.
2512 TEST_F(RenderTextTest, StringFitsOwnWidth) {
2513 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2514 const base::string16 kString = ASCIIToUTF16("www.example.com");
2516 render_text->SetText(kString);
2517 render_text->ApplyStyle(BOLD, true, Range(0, 3));
2518 render_text->SetElideBehavior(ELIDE_TAIL);
2520 render_text->SetDisplayRect(Rect(0, 0, 500, 100));
2521 EXPECT_EQ(kString, render_text->GetDisplayText());
2522 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100));
2523 EXPECT_EQ(kString, render_text->GetDisplayText());
2526 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184
2527 #if !defined(OS_WIN)
2528 // Ensure that RenderText examines all of the fonts in its FontList before
2529 // falling back to other fonts.
2530 TEST_F(RenderTextTest, HarfBuzz_FontListFallback) {
2531 // Double-check that the requested fonts are present.
2532 FontList font_list("Arial, Symbol, 12px");
2533 const std::vector<Font>& fonts = font_list.GetFonts();
2534 ASSERT_EQ(2u, fonts.size());
2535 ASSERT_EQ("arial",
2536 base::StringToLowerASCII(fonts[0].GetActualFontNameForTesting()));
2537 ASSERT_EQ("symbol",
2538 base::StringToLowerASCII(fonts[1].GetActualFontNameForTesting()));
2540 // "⊕" (CIRCLED PLUS) should be rendered with Symbol rather than falling back
2541 // to some other font that's present on the system.
2542 RenderTextHarfBuzz render_text;
2543 render_text.SetFontList(font_list);
2544 render_text.SetText(UTF8ToUTF16("\xE2\x8A\x95"));
2545 const std::vector<RenderText::FontSpan> spans =
2546 render_text.GetFontSpansForTesting();
2547 ASSERT_EQ(static_cast<size_t>(1), spans.size());
2548 EXPECT_EQ("Symbol", spans[0].first.GetFontName());
2550 #endif // !defined(OS_WIN)
2552 // Ensure that the fallback fonts of the Uniscribe font are tried for shaping.
2553 #if defined(OS_WIN)
2554 TEST_F(RenderTextTest, HarfBuzz_UniscribeFallback) {
2555 RenderTextHarfBuzz render_text;
2556 PlatformFontWin* font_win = new PlatformFontWin("Meiryo", 12);
2557 // Japanese name for Meiryo. This name won't be found in the system's linked
2558 // fonts, forcing RTHB to try the Uniscribe font and its fallbacks.
2559 font_win->font_ref_->font_name_ = WideToUTF8(L"\x30e1\x30a4\x30ea\x30aa");
2560 FontList font_list((Font(font_win)));
2562 render_text.SetFontList(font_list);
2563 // Korean character "han".
2564 render_text.SetText(WideToUTF16(L"\xd55c"));
2565 render_text.EnsureLayout();
2566 internal::TextRunList* run_list = render_text.GetRunList();
2567 ASSERT_EQ(1U, run_list->size());
2568 EXPECT_EQ(0U, run_list->runs()[0]->CountMissingGlyphs());
2570 #endif // defined(OS_WIN)
2572 // Ensure that the width reported by RenderText is sufficient for drawing. Draws
2573 // to a canvas and checks whether any pixel beyond the width is colored.
2574 TEST_F(RenderTextTest, TextDoesntClip) {
2575 const wchar_t* kTestStrings[] = { L"Save", L"Remove", L"TEST", L"W", L"WWW" };
2576 const Size kCanvasSize(300, 50);
2577 const int kTestWidth = 10;
2579 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
2580 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2581 scoped_ptr<Canvas> canvas(
2582 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
2583 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2584 render_text->SetDisplayRect(Rect(kCanvasSize));
2585 render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT);
2586 render_text->SetColor(SK_ColorBLACK);
2588 for (auto string : kTestStrings) {
2589 surface->getCanvas()->clear(SK_ColorWHITE);
2590 render_text->SetText(WideToUTF16(string));
2591 render_text->SetStyle(BOLD, true);
2592 render_text->Draw(canvas.get());
2593 int width = render_text->GetStringSize().width();
2594 ASSERT_LT(width + kTestWidth, kCanvasSize.width());
2595 const uint32* buffer = static_cast<const uint32*>(
2596 surface->peekPixels(NULL, NULL));
2597 ASSERT_NE(nullptr, buffer);
2599 for (int y = 0; y < kCanvasSize.height(); ++y) {
2600 // Allow one column of anti-aliased pixels past the expected width.
2601 SkColor color = buffer[width + y * kCanvasSize.width()];
2602 EXPECT_LT(220U, color_utils::GetLuminanceForColor(color)) << string;
2603 for (int x = 1; x < kTestWidth; ++x) {
2604 color = buffer[width + x + y * kCanvasSize.width()];
2605 EXPECT_EQ(SK_ColorWHITE, color) << string;
2611 } // namespace gfx