Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / render_text_unittest.cc
blob44ecd7d51ada02180784c043392c6601e48091a1
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"
25 #include "ui/gfx/text_utils.h"
27 #if defined(OS_WIN)
28 #include "base/win/windows_version.h"
29 #include "ui/gfx/platform_font_win.h"
30 #endif
32 #if defined(OS_MACOSX)
33 #include <ApplicationServices/ApplicationServices.h>
35 #include "ui/gfx/render_text_mac.h"
36 #endif
38 using base::ASCIIToUTF16;
39 using base::UTF8ToUTF16;
40 using base::WideToUTF16;
41 using base::WideToUTF8;
43 namespace gfx {
45 namespace {
47 // Various weak, LTR, RTL, and Bidi string cases with three characters each.
48 const wchar_t kWeak[] = L" . ";
49 const wchar_t kLtr[] = L"abc";
50 const wchar_t kRtl[] = L"\x5d0\x5d1\x5d2";
51 const wchar_t kLtrRtl[] = L"a" L"\x5d0\x5d1";
52 const wchar_t kLtrRtlLtr[] = L"a" L"\x5d1" L"b";
53 const wchar_t kRtlLtr[] = L"\x5d0\x5d1" L"a";
54 const wchar_t kRtlLtrRtl[] = L"\x5d0" L"a" L"\x5d1";
56 // Checks whether |range| contains |index|. This is not the same as calling
57 // range.Contains(Range(index)), which returns true if |index| == |range.end()|.
58 bool IndexInRange(const Range& range, size_t index) {
59 return index >= range.start() && index < range.end();
62 base::string16 GetSelectedText(RenderText* render_text) {
63 return render_text->text().substr(render_text->selection().GetMin(),
64 render_text->selection().length());
67 // A test utility function to set the application default text direction.
68 void SetRTL(bool rtl) {
69 // Override the current locale/direction.
70 base::i18n::SetICUDefaultLocale(rtl ? "he" : "en");
71 EXPECT_EQ(rtl, base::i18n::IsRTL());
74 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
75 #if !defined(OS_MACOSX)
76 // Ensure cursor movement in the specified |direction| yields |expected| values.
77 void RunMoveCursorLeftRightTest(RenderText* render_text,
78 const std::vector<SelectionModel>& expected,
79 VisualCursorDirection direction) {
80 for (size_t i = 0; i < expected.size(); ++i) {
81 SCOPED_TRACE(base::StringPrintf("Going %s; expected value index %d.",
82 direction == CURSOR_LEFT ? "left" : "right", static_cast<int>(i)));
83 EXPECT_EQ(expected[i], render_text->selection_model());
84 render_text->MoveCursor(CHARACTER_BREAK, direction, false);
86 // Check that cursoring is clamped at the line edge.
87 EXPECT_EQ(expected.back(), render_text->selection_model());
88 // Check that it is the line edge.
89 render_text->MoveCursor(LINE_BREAK, direction, false);
90 EXPECT_EQ(expected.back(), render_text->selection_model());
92 #endif // !defined(OS_MACOSX)
94 // The class which records the drawing operations so that the test case can
95 // verify where exactly the glyphs are drawn.
96 class TestSkiaTextRenderer : public internal::SkiaTextRenderer {
97 public:
98 struct TextLog {
99 TextLog() : glyph_count(0u) {}
100 PointF origin;
101 size_t glyph_count;
104 struct DecorationLog {
105 DecorationLog(int x, int y, int width, bool underline, bool strike,
106 bool diagonal_strike)
107 : x(x), y(y), width(width), underline(underline), strike(strike),
108 diagonal_strike(diagonal_strike) {}
109 int x;
110 int y;
111 int width;
112 bool underline;
113 bool strike;
114 bool diagonal_strike;
117 explicit TestSkiaTextRenderer(Canvas* canvas)
118 : internal::SkiaTextRenderer(canvas) {}
119 ~TestSkiaTextRenderer() override {}
121 void GetTextLogAndReset(std::vector<TextLog>* text_log) {
122 text_log_.swap(*text_log);
123 text_log_.clear();
126 void GetDecorationLogAndReset(std::vector<DecorationLog>* decoration_log) {
127 decoration_log_.swap(*decoration_log);
128 decoration_log_.clear();
131 private:
132 // internal::SkiaTextRenderer:
133 void DrawPosText(const SkPoint* pos,
134 const uint16* glyphs,
135 size_t glyph_count) override {
136 TextLog log_entry;
137 log_entry.glyph_count = glyph_count;
138 if (glyph_count > 0) {
139 log_entry.origin =
140 PointF(SkScalarToFloat(pos[0].x()), SkScalarToFloat(pos[0].y()));
141 for (size_t i = 1U; i < glyph_count; ++i) {
142 log_entry.origin.SetToMin(
143 PointF(SkScalarToFloat(pos[i].x()), SkScalarToFloat(pos[i].y())));
146 text_log_.push_back(log_entry);
147 internal::SkiaTextRenderer::DrawPosText(pos, glyphs, glyph_count);
150 void DrawDecorations(int x, int y, int width, bool underline, bool strike,
151 bool diagonal_strike) override {
152 decoration_log_.push_back(
153 DecorationLog(x, y, width, underline, strike, diagonal_strike));
154 internal::SkiaTextRenderer::DrawDecorations(
155 x, y, width, underline, strike, diagonal_strike);
158 std::vector<TextLog> text_log_;
159 std::vector<DecorationLog> decoration_log_;
161 DISALLOW_COPY_AND_ASSIGN(TestSkiaTextRenderer);
164 // Given a buffer to test against, this can be used to test various areas of the
165 // rectangular buffer against a specific color value.
166 class TestRectangleBuffer {
167 public:
168 TestRectangleBuffer(const wchar_t* string,
169 const SkColor* buffer,
170 uint32_t stride,
171 uint32_t row_count)
172 : string_(string),
173 buffer_(buffer),
174 stride_(stride),
175 row_count_(row_count) {}
177 // Test if any values in the rectangular area are anything other than |color|.
178 void EnsureSolidRect(SkColor color,
179 int left,
180 int top,
181 int width,
182 int height) const {
183 ASSERT_LT(top, row_count_) << string_;
184 ASSERT_LE(top + height, row_count_) << string_;
185 ASSERT_LT(left, stride_) << string_;
186 ASSERT_LE(left + width, stride_) << string_ << ", left " << left
187 << ", width " << width << ", stride_ "
188 << stride_;
189 for (int y = top; y < top + height; ++y) {
190 for (int x = left; x < left + width; ++x) {
191 SkColor buffer_color = buffer_[x + y * stride_];
192 EXPECT_EQ(color, buffer_color) << string_ << " at " << x << ", " << y;
197 private:
198 const wchar_t* string_;
199 const SkColor* buffer_;
200 int stride_;
201 int row_count_;
203 DISALLOW_COPY_AND_ASSIGN(TestRectangleBuffer);
206 } // namespace
208 class RenderTextTest : public testing::Test {
211 TEST_F(RenderTextTest, DefaultStyles) {
212 // Check the default styles applied to new instances and adjusted text.
213 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
214 EXPECT_TRUE(render_text->text().empty());
215 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
216 for (size_t i = 0; i < arraysize(cases); ++i) {
217 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLACK));
218 EXPECT_TRUE(
219 render_text->baselines().EqualsValueForTesting(NORMAL_BASELINE));
220 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
221 EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false));
222 render_text->SetText(WideToUTF16(cases[i]));
226 TEST_F(RenderTextTest, SetStyles) {
227 // Ensure custom default styles persist across setting and clearing text.
228 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
229 const SkColor color = SK_ColorRED;
230 render_text->SetColor(color);
231 render_text->SetBaselineStyle(SUPERSCRIPT);
232 render_text->SetStyle(BOLD, true);
233 render_text->SetStyle(UNDERLINE, false);
234 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
235 for (size_t i = 0; i < arraysize(cases); ++i) {
236 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color));
237 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUPERSCRIPT));
238 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true));
239 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false));
240 render_text->SetText(WideToUTF16(cases[i]));
242 // Ensure custom default styles can be applied after text has been set.
243 if (i == 1)
244 render_text->SetStyle(STRIKE, true);
245 if (i >= 1)
246 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true));
250 TEST_F(RenderTextTest, ApplyStyles) {
251 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
252 render_text->SetText(ASCIIToUTF16("012345678"));
254 // Apply a ranged color and style and check the resulting breaks.
255 render_text->ApplyColor(SK_ColorRED, Range(1, 4));
256 render_text->ApplyBaselineStyle(SUPERIOR, Range(2, 4));
257 render_text->ApplyStyle(BOLD, true, Range(2, 5));
258 std::vector<std::pair<size_t, SkColor> > expected_color;
259 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK));
260 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED));
261 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK));
262 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
263 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_style;
264 expected_baseline_style.push_back(
265 std::pair<size_t, BaselineStyle>(0, NORMAL_BASELINE));
266 expected_baseline_style.push_back(
267 std::pair<size_t, BaselineStyle>(2, SUPERIOR));
268 expected_baseline_style.push_back(
269 std::pair<size_t, BaselineStyle>(4, NORMAL_BASELINE));
270 EXPECT_TRUE(
271 render_text->baselines().EqualsForTesting(expected_baseline_style));
272 std::vector<std::pair<size_t, bool> > expected_style;
273 expected_style.push_back(std::pair<size_t, bool>(0, false));
274 expected_style.push_back(std::pair<size_t, bool>(2, true));
275 expected_style.push_back(std::pair<size_t, bool>(5, false));
276 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style));
278 // Ensure that setting a value overrides the ranged values.
279 render_text->SetColor(SK_ColorBLUE);
280 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE));
281 render_text->SetBaselineStyle(SUBSCRIPT);
282 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUBSCRIPT));
283 render_text->SetStyle(BOLD, false);
284 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false));
286 // Apply a value over the text end and check the resulting breaks (INT_MAX
287 // should be used instead of the text length for the range end)
288 const size_t text_length = render_text->text().length();
289 render_text->ApplyColor(SK_ColorRED, Range(0, text_length));
290 render_text->ApplyBaselineStyle(SUPERIOR, Range(0, text_length));
291 render_text->ApplyStyle(BOLD, true, Range(2, text_length));
292 std::vector<std::pair<size_t, SkColor> > expected_color_end;
293 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED));
294 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end));
295 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_end;
296 expected_baseline_end.push_back(
297 std::pair<size_t, BaselineStyle>(0, SUPERIOR));
298 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline_end));
299 std::vector<std::pair<size_t, bool> > expected_style_end;
300 expected_style_end.push_back(std::pair<size_t, bool>(0, false));
301 expected_style_end.push_back(std::pair<size_t, bool>(2, true));
302 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end));
304 // Ensure ranged values adjust to accommodate text length changes.
305 render_text->ApplyStyle(ITALIC, true, Range(0, 2));
306 render_text->ApplyStyle(ITALIC, true, Range(3, 6));
307 render_text->ApplyStyle(ITALIC, true, Range(7, text_length));
308 std::vector<std::pair<size_t, bool> > expected_italic;
309 expected_italic.push_back(std::pair<size_t, bool>(0, true));
310 expected_italic.push_back(std::pair<size_t, bool>(2, false));
311 expected_italic.push_back(std::pair<size_t, bool>(3, true));
312 expected_italic.push_back(std::pair<size_t, bool>(6, false));
313 expected_italic.push_back(std::pair<size_t, bool>(7, true));
314 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
316 // Changing the text should clear any breaks except for the first one.
317 render_text->SetText(ASCIIToUTF16("0123456"));
318 expected_italic.resize(1);
319 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
320 render_text->ApplyStyle(ITALIC, false, Range(2, 4));
321 render_text->SetText(ASCIIToUTF16("012345678"));
322 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
323 render_text->ApplyStyle(ITALIC, false, Range(0, 1));
324 render_text->SetText(ASCIIToUTF16("0123456"));
325 expected_italic.begin()->second = false;
326 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
327 render_text->ApplyStyle(ITALIC, true, Range(2, 4));
328 render_text->SetText(ASCIIToUTF16("012345678"));
329 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
331 // TODO(tmoniuszko): Enable when RenderTextMac::IsValidCursorIndex is
332 // implemented.
333 #if !defined(OS_MACOSX)
334 // Styles shouldn't be changed mid-grapheme.
335 render_text->SetText(WideToUTF16(
336 L"0" L"\x0915\x093f" L"1" L"\x0915\x093f" L"2"));
337 render_text->ApplyStyle(UNDERLINE, true, Range(2, 5));
338 std::vector<std::pair<size_t, bool> > expected_underline;
339 expected_underline.push_back(std::pair<size_t, bool>(0, false));
340 expected_underline.push_back(std::pair<size_t, bool>(1, true));
341 expected_underline.push_back(std::pair<size_t, bool>(6, false));
342 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsForTesting(
343 expected_underline));
344 #endif // !defined(OS_MACOSX)
347 TEST_F(RenderTextTest, AppendTextKeepsStyles) {
348 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
349 // Setup basic functionality.
350 render_text->SetText(ASCIIToUTF16("abc"));
351 render_text->ApplyColor(SK_ColorRED, Range(0, 1));
352 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2));
353 render_text->ApplyStyle(UNDERLINE, true, Range(2, 3));
354 // Verify basic functionality.
355 std::vector<std::pair<size_t, SkColor>> expected_color;
356 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED));
357 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorBLACK));
358 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
359 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline;
360 expected_baseline.push_back(
361 std::pair<size_t, BaselineStyle>(0, NORMAL_BASELINE));
362 expected_baseline.push_back(std::pair<size_t, BaselineStyle>(1, SUPERSCRIPT));
363 expected_baseline.push_back(
364 std::pair<size_t, BaselineStyle>(2, NORMAL_BASELINE));
365 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline));
366 std::vector<std::pair<size_t, bool>> expected_style;
367 expected_style.push_back(std::pair<size_t, bool>(0, false));
368 expected_style.push_back(std::pair<size_t, bool>(2, true));
369 EXPECT_TRUE(
370 render_text->styles()[UNDERLINE].EqualsForTesting(expected_style));
371 // Ensure AppendText maintains current text styles.
372 render_text->AppendText(ASCIIToUTF16("def"));
373 EXPECT_EQ(render_text->GetDisplayText(), ASCIIToUTF16("abcdef"));
374 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
375 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline));
376 EXPECT_TRUE(
377 render_text->styles()[UNDERLINE].EqualsForTesting(expected_style));
380 void TestVisualCursorMotionInObscuredField(RenderText* render_text,
381 const base::string16& text,
382 bool select) {
383 ASSERT_TRUE(render_text->obscured());
384 render_text->SetText(text);
385 int len = text.length();
386 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, select);
387 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
388 render_text->selection_model());
389 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, select);
390 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
391 for (int j = 1; j <= len; ++j) {
392 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, select);
393 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_BACKWARD),
394 render_text->selection_model());
396 for (int j = len - 1; j >= 0; --j) {
397 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, select);
398 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_FORWARD),
399 render_text->selection_model());
401 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, select);
402 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
403 render_text->selection_model());
404 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, select);
405 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
408 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
409 #if !defined(OS_MACOSX)
410 TEST_F(RenderTextTest, ObscuredText) {
411 const base::string16 seuss = ASCIIToUTF16("hop on pop");
412 const base::string16 no_seuss = ASCIIToUTF16("**********");
413 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
415 // GetLayoutText() returns asterisks when the obscured bit is set.
416 render_text->SetText(seuss);
417 render_text->SetObscured(true);
418 EXPECT_EQ(seuss, render_text->text());
419 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
420 render_text->SetObscured(false);
421 EXPECT_EQ(seuss, render_text->text());
422 EXPECT_EQ(seuss, render_text->GetDisplayText());
424 render_text->SetObscured(true);
426 // Surrogate pairs are counted as one code point.
427 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 0};
428 render_text->SetText(invalid_surrogates);
429 EXPECT_EQ(ASCIIToUTF16("**"), render_text->GetDisplayText());
430 const base::char16 valid_surrogates[] = {0xD800, 0xDC00, 0};
431 render_text->SetText(valid_surrogates);
432 EXPECT_EQ(ASCIIToUTF16("*"), render_text->GetDisplayText());
433 EXPECT_EQ(0U, render_text->cursor_position());
434 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
435 EXPECT_EQ(2U, render_text->cursor_position());
437 // Test index conversion and cursor validity with a valid surrogate pair.
438 EXPECT_EQ(0U, render_text->TextIndexToDisplayIndex(0U));
439 EXPECT_EQ(1U, render_text->TextIndexToDisplayIndex(1U));
440 EXPECT_EQ(1U, render_text->TextIndexToDisplayIndex(2U));
441 EXPECT_EQ(0U, render_text->DisplayIndexToTextIndex(0U));
442 EXPECT_EQ(2U, render_text->DisplayIndexToTextIndex(1U));
443 EXPECT_TRUE(render_text->IsValidCursorIndex(0U));
444 EXPECT_FALSE(render_text->IsValidCursorIndex(1U));
445 EXPECT_TRUE(render_text->IsValidCursorIndex(2U));
447 // FindCursorPosition() should not return positions between a surrogate pair.
448 render_text->SetDisplayRect(Rect(0, 0, 20, 20));
449 EXPECT_EQ(render_text->FindCursorPosition(Point(0, 0)).caret_pos(), 0U);
450 EXPECT_EQ(render_text->FindCursorPosition(Point(20, 0)).caret_pos(), 2U);
451 for (int x = -1; x <= 20; ++x) {
452 SelectionModel selection = render_text->FindCursorPosition(Point(x, 0));
453 EXPECT_TRUE(selection.caret_pos() == 0U || selection.caret_pos() == 2U);
456 // GetGlyphBounds() should yield the entire string bounds for text index 0.
457 EXPECT_EQ(render_text->GetStringSize().width(),
458 static_cast<int>(render_text->GetGlyphBounds(0U).length()));
460 // Cursoring is independent of underlying characters when text is obscured.
461 const wchar_t* const texts[] = {
462 kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl,
463 L"hop on pop", // Check LTR word boundaries.
464 L"\x05d0\x05d1 \x05d0\x05d2 \x05d1\x05d2", // Check RTL word boundaries.
466 for (size_t i = 0; i < arraysize(texts); ++i) {
467 base::string16 text = WideToUTF16(texts[i]);
468 TestVisualCursorMotionInObscuredField(render_text.get(), text, false);
469 TestVisualCursorMotionInObscuredField(render_text.get(), text, true);
472 #endif // !defined(OS_MACOSX)
474 TEST_F(RenderTextTest, RevealObscuredText) {
475 const base::string16 seuss = ASCIIToUTF16("hop on pop");
476 const base::string16 no_seuss = ASCIIToUTF16("**********");
477 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
479 render_text->SetText(seuss);
480 render_text->SetObscured(true);
481 EXPECT_EQ(seuss, render_text->text());
482 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
484 // Valid reveal index and new revealed index clears previous one.
485 render_text->RenderText::SetObscuredRevealIndex(0);
486 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetDisplayText());
487 render_text->RenderText::SetObscuredRevealIndex(1);
488 EXPECT_EQ(ASCIIToUTF16("*o********"), render_text->GetDisplayText());
489 render_text->RenderText::SetObscuredRevealIndex(2);
490 EXPECT_EQ(ASCIIToUTF16("**p*******"), render_text->GetDisplayText());
492 // Invalid reveal index.
493 render_text->RenderText::SetObscuredRevealIndex(-1);
494 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
495 render_text->RenderText::SetObscuredRevealIndex(seuss.length() + 1);
496 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
498 // SetObscured clears the revealed index.
499 render_text->RenderText::SetObscuredRevealIndex(0);
500 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetDisplayText());
501 render_text->SetObscured(false);
502 EXPECT_EQ(seuss, render_text->GetDisplayText());
503 render_text->SetObscured(true);
504 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
506 // SetText clears the revealed index.
507 render_text->SetText(ASCIIToUTF16("new"));
508 EXPECT_EQ(ASCIIToUTF16("***"), render_text->GetDisplayText());
509 render_text->RenderText::SetObscuredRevealIndex(2);
510 EXPECT_EQ(ASCIIToUTF16("**w"), render_text->GetDisplayText());
511 render_text->SetText(ASCIIToUTF16("new longer"));
512 EXPECT_EQ(ASCIIToUTF16("**********"), render_text->GetDisplayText());
514 // Text with invalid surrogates.
515 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 'h', 'o', 'p', 0};
516 render_text->SetText(invalid_surrogates);
517 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetDisplayText());
518 render_text->RenderText::SetObscuredRevealIndex(0);
519 const base::char16 invalid_expect_0[] = {0xDC00, '*', '*', '*', '*', 0};
520 EXPECT_EQ(invalid_expect_0, render_text->GetDisplayText());
521 render_text->RenderText::SetObscuredRevealIndex(1);
522 const base::char16 invalid_expect_1[] = {'*', 0xD800, '*', '*', '*', 0};
523 EXPECT_EQ(invalid_expect_1, render_text->GetDisplayText());
524 render_text->RenderText::SetObscuredRevealIndex(2);
525 EXPECT_EQ(ASCIIToUTF16("**h**"), render_text->GetDisplayText());
527 // Text with valid surrogates before and after the reveal index.
528 const base::char16 valid_surrogates[] =
529 {0xD800, 0xDC00, 'h', 'o', 'p', 0xD800, 0xDC00, 0};
530 render_text->SetText(valid_surrogates);
531 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetDisplayText());
532 render_text->RenderText::SetObscuredRevealIndex(0);
533 const base::char16 valid_expect_0_and_1[] =
534 {0xD800, 0xDC00, '*', '*', '*', '*', 0};
535 EXPECT_EQ(valid_expect_0_and_1, render_text->GetDisplayText());
536 render_text->RenderText::SetObscuredRevealIndex(1);
537 EXPECT_EQ(valid_expect_0_and_1, render_text->GetDisplayText());
538 render_text->RenderText::SetObscuredRevealIndex(2);
539 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text->GetDisplayText());
540 render_text->RenderText::SetObscuredRevealIndex(5);
541 const base::char16 valid_expect_5_and_6[] =
542 {'*', '*', '*', '*', 0xD800, 0xDC00, 0};
543 EXPECT_EQ(valid_expect_5_and_6, render_text->GetDisplayText());
544 render_text->RenderText::SetObscuredRevealIndex(6);
545 EXPECT_EQ(valid_expect_5_and_6, render_text->GetDisplayText());
548 // TODO(PORT): Fails for RenderTextMac.
549 #if !defined(OS_MACOSX)
550 TEST_F(RenderTextTest, ElidedText) {
551 // TODO(skanuj) : Add more test cases for following
552 // - RenderText styles.
553 // - Cross interaction of truncate, elide and obscure.
554 // - ElideText tests from text_elider.cc.
555 struct {
556 const wchar_t* text;
557 const wchar_t* display_text;
558 const bool elision_expected;
559 } cases[] = {
560 // Strings shorter than the elision width should be laid out in full.
561 { L"", L"" , false },
562 { L"M", L"" , false },
563 { L" . ", L" . " , false },
564 { kWeak, kWeak , false },
565 { kLtr, kLtr , false },
566 { kLtrRtl, kLtrRtl , false },
567 { kLtrRtlLtr, kLtrRtlLtr, false },
568 { kRtl, kRtl , false },
569 { kRtlLtr, kRtlLtr , false },
570 { kRtlLtrRtl, kRtlLtrRtl, false },
571 // Strings as long as the elision width should be laid out in full.
572 { L"012ab", L"012ab" , false },
573 // Long strings should be elided with an ellipsis appended at the end.
574 { L"012abc", L"012a\x2026", true },
575 { L"012ab" L"\x5d0\x5d1", L"012a\x2026", true },
576 { L"012a" L"\x5d1" L"b", L"012a\x2026", true },
577 // No RLM marker added as digits (012) have weak directionality.
578 { L"01" L"\x5d0\x5d1\x5d2", L"01\x5d0\x5d1\x2026", true },
579 // RLM marker added as "ab" have strong LTR directionality.
580 { L"ab" L"\x5d0\x5d1\x5d2", L"ab\x5d0\x5d1\x2026\x200f", true },
581 // Test surrogate pairs. \xd834\xdd1e forms a single code point U+1D11E;
582 // \xd834\xdd22 forms a second code point U+1D122. The first should be kept;
583 // the second removed (no surrogate pair should be partially elided).
584 { L"0123\xd834\xdd1e\xd834\xdd22", L"0123\xd834\xdd1e\x2026", true },
585 // Test combining character sequences. U+0915 U+093F forms a compound glyph;
586 // U+0915 U+0942 forms a second compound glyph. The first should be kept;
587 // the second removed (no combining sequence should be partially elided).
588 { L"0123\x0915\x093f\x0915\x0942", L"0123\x0915\x093f\x2026", true },
589 // U+05E9 U+05BC U+05C1 U+05B8 forms a four-character compound glyph. Again,
590 // it should be either fully elided, or not elided at all. If completely
591 // elided, an LTR Mark (U+200E) should be added.
592 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8", false },
593 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x2026\x200E" , true },
594 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x2026\x200E" , true },
595 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x2026\x200E" , true },
596 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" , true },
599 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
600 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
601 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
603 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
604 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
605 render_text->SetElideBehavior(ELIDE_TAIL);
607 for (size_t i = 0; i < arraysize(cases); i++) {
608 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "] '%ls'", i,
609 cases[i].text));
611 // Compute expected width
612 expected_render_text->SetText(WideToUTF16(cases[i].display_text));
613 int expected_width = expected_render_text->GetContentWidth();
615 base::string16 input = WideToUTF16(cases[i].text);
616 // Extend the input text to ensure that it is wider than the display_text,
617 // and so it will get elided.
618 if (cases[i].elision_expected)
619 input.append(WideToUTF16(L" MMMMMMMMMMM"));
620 render_text->SetText(input);
621 render_text->SetDisplayRect(Rect(0, 0, expected_width, 100));
622 EXPECT_EQ(input, render_text->text());
623 EXPECT_EQ(WideToUTF16(cases[i].display_text),
624 render_text->GetDisplayText());
625 expected_render_text->SetText(base::string16());
629 TEST_F(RenderTextTest, ElidedObscuredText) {
630 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
631 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
632 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
633 expected_render_text->SetText(WideToUTF16(L"**\x2026"));
635 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
636 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
637 render_text->SetElideBehavior(ELIDE_TAIL);
638 render_text->SetDisplayRect(
639 Rect(0, 0, expected_render_text->GetContentWidth(), 100));
640 render_text->SetObscured(true);
641 render_text->SetText(WideToUTF16(L"abcdef"));
642 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
643 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetDisplayText());
645 #endif // !defined(OS_MACOSX)
647 TEST_F(RenderTextTest, ElidedEmail) {
648 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
649 render_text->SetText(ASCIIToUTF16("test@example.com"));
650 const gfx::Size size = render_text->GetStringSize();
652 const base::string16 long_email =
653 ASCIIToUTF16("longemailaddresstest@example.com");
654 render_text->SetText(long_email);
655 render_text->SetElideBehavior(ELIDE_EMAIL);
656 render_text->SetDisplayRect(gfx::Rect(size));
657 EXPECT_GE(size.width(), render_text->GetStringSize().width());
658 EXPECT_GT(long_email.size(), render_text->GetDisplayText().size());
661 TEST_F(RenderTextTest, TruncatedText) {
662 struct {
663 const wchar_t* text;
664 const wchar_t* display_text;
665 } cases[] = {
666 // Strings shorter than the truncation length should be laid out in full.
667 { L"", L"" },
668 { kWeak, kWeak },
669 { kLtr, kLtr },
670 { kLtrRtl, kLtrRtl },
671 { kLtrRtlLtr, kLtrRtlLtr },
672 { kRtl, kRtl },
673 { kRtlLtr, kRtlLtr },
674 { kRtlLtrRtl, kRtlLtrRtl },
675 // Strings as long as the truncation length should be laid out in full.
676 { L"01234", L"01234" },
677 // Long strings should be truncated with an ellipsis appended at the end.
678 { L"012345", L"0123\x2026" },
679 { L"012" L" . ", L"012 \x2026" },
680 { L"012" L"abc", L"012a\x2026" },
681 { L"012" L"a" L"\x5d0\x5d1", L"012a\x2026" },
682 { L"012" L"a" L"\x5d1" L"b", L"012a\x2026" },
683 { L"012" L"\x5d0\x5d1\x5d2", L"012\x5d0\x2026" },
684 { L"012" L"\x5d0\x5d1" L"a", L"012\x5d0\x2026" },
685 { L"012" L"\x5d0" L"a" L"\x5d1", L"012\x5d0\x2026" },
686 // Surrogate pairs should be truncated reasonably enough.
687 { L"0123\x0915\x093f", L"0123\x2026" },
688 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8" },
689 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x05bc\x2026" },
690 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x05e9\x2026" },
691 { L"0123\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
692 { L"01234\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
693 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" },
696 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
697 render_text->set_truncate_length(5);
698 for (size_t i = 0; i < arraysize(cases); i++) {
699 render_text->SetText(WideToUTF16(cases[i].text));
700 EXPECT_EQ(WideToUTF16(cases[i].text), render_text->text());
701 EXPECT_EQ(WideToUTF16(cases[i].display_text), render_text->GetDisplayText())
702 << "For case " << i << ": " << cases[i].text;
706 TEST_F(RenderTextTest, TruncatedObscuredText) {
707 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
708 render_text->set_truncate_length(3);
709 render_text->SetObscured(true);
710 render_text->SetText(WideToUTF16(L"abcdef"));
711 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
712 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetDisplayText());
715 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
716 #if !defined(OS_MACOSX)
717 TEST_F(RenderTextTest, TruncatedCursorMovementLTR) {
718 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
719 render_text->set_truncate_length(2);
720 render_text->SetText(WideToUTF16(L"abcd"));
722 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
723 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
724 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
725 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
726 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
728 std::vector<SelectionModel> expected;
729 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
730 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
731 // The cursor hops over the ellipsis and elided text to the line end.
732 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
733 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
734 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
736 expected.clear();
737 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
738 // The cursor hops over the elided text to preceeding text.
739 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
740 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
741 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
742 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
745 TEST_F(RenderTextTest, TruncatedCursorMovementRTL) {
746 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
747 render_text->set_truncate_length(2);
748 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3"));
750 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
751 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
752 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
753 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
754 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
756 std::vector<SelectionModel> expected;
757 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
758 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
759 // The cursor hops over the ellipsis and elided text to the line end.
760 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
761 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
762 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
764 expected.clear();
765 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
766 // The cursor hops over the elided text to preceeding text.
767 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
768 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
769 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
770 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
772 #endif // !defined(OS_MACOSX)
774 TEST_F(RenderTextTest, GetDisplayTextDirection) {
775 struct {
776 const wchar_t* text;
777 const base::i18n::TextDirection text_direction;
778 } cases[] = {
779 // Blank strings and those with no/weak directionality default to LTR.
780 { L"", base::i18n::LEFT_TO_RIGHT },
781 { kWeak, base::i18n::LEFT_TO_RIGHT },
782 // Strings that begin with strong LTR characters.
783 { kLtr, base::i18n::LEFT_TO_RIGHT },
784 { kLtrRtl, base::i18n::LEFT_TO_RIGHT },
785 { kLtrRtlLtr, base::i18n::LEFT_TO_RIGHT },
786 // Strings that begin with strong RTL characters.
787 { kRtl, base::i18n::RIGHT_TO_LEFT },
788 { kRtlLtr, base::i18n::RIGHT_TO_LEFT },
789 { kRtlLtrRtl, base::i18n::RIGHT_TO_LEFT },
792 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
793 const bool was_rtl = base::i18n::IsRTL();
795 for (size_t i = 0; i < 2; ++i) {
796 // Toggle the application default text direction (to try each direction).
797 SetRTL(!base::i18n::IsRTL());
798 const base::i18n::TextDirection ui_direction = base::i18n::IsRTL() ?
799 base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
801 // Ensure that directionality modes yield the correct text directions.
802 for (size_t j = 0; j < arraysize(cases); j++) {
803 render_text->SetText(WideToUTF16(cases[j].text));
804 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
805 EXPECT_EQ(render_text->GetDisplayTextDirection(),cases[j].text_direction);
806 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_UI);
807 EXPECT_EQ(render_text->GetDisplayTextDirection(), ui_direction);
808 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_LTR);
809 EXPECT_EQ(render_text->GetDisplayTextDirection(),
810 base::i18n::LEFT_TO_RIGHT);
811 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_RTL);
812 EXPECT_EQ(render_text->GetDisplayTextDirection(),
813 base::i18n::RIGHT_TO_LEFT);
817 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
819 // Ensure that text changes update the direction for DIRECTIONALITY_FROM_TEXT.
820 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
821 render_text->SetText(WideToUTF16(kLtr));
822 EXPECT_EQ(render_text->GetDisplayTextDirection(), base::i18n::LEFT_TO_RIGHT);
823 render_text->SetText(WideToUTF16(kRtl));
824 EXPECT_EQ(render_text->GetDisplayTextDirection(), base::i18n::RIGHT_TO_LEFT);
827 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
828 #if !defined(OS_MACOSX)
829 TEST_F(RenderTextTest, MoveCursorLeftRightInLtr) {
830 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
832 // Pure LTR.
833 render_text->SetText(ASCIIToUTF16("abc"));
834 // |expected| saves the expected SelectionModel when moving cursor from left
835 // to right.
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(2, CURSOR_BACKWARD));
840 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
841 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
842 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
844 expected.clear();
845 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
846 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
847 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
848 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
849 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
850 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
853 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtl) {
854 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
855 // LTR-RTL
856 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
857 // The last one is the expected END position.
858 std::vector<SelectionModel> expected;
859 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
860 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
861 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
862 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
863 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
864 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
865 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
866 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
867 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
869 expected.clear();
870 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
871 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
872 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
873 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
874 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
875 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
876 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
877 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
878 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
881 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtlLtr) {
882 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
883 // LTR-RTL-LTR.
884 render_text->SetText(WideToUTF16(L"a" L"\x05d1" L"b"));
885 std::vector<SelectionModel> expected;
886 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
887 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
888 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
889 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
890 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
891 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
893 expected.clear();
894 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
895 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
896 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
897 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
898 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
899 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
902 TEST_F(RenderTextTest, MoveCursorLeftRightInRtl) {
903 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
904 // Pure RTL.
905 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2"));
906 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
907 std::vector<SelectionModel> expected;
909 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
910 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
911 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
912 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
913 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
914 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
916 expected.clear();
918 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
919 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
920 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
921 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
922 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
923 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
926 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtr) {
927 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
928 // RTL-LTR
929 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2" L"abc"));
930 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
931 std::vector<SelectionModel> expected;
932 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
933 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
934 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
935 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
936 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
937 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
938 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
939 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
940 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
942 expected.clear();
943 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
944 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
945 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
946 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
947 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
948 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
949 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
950 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
951 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
954 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtrRtl) {
955 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
956 // RTL-LTR-RTL.
957 render_text->SetText(WideToUTF16(L"\x05d0" L"a" L"\x05d1"));
958 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
959 std::vector<SelectionModel> expected;
960 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
961 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
962 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
963 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
964 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
965 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
967 expected.clear();
968 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
969 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
970 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
971 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
972 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
973 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
976 TEST_F(RenderTextTest, MoveCursorLeftRight_ComplexScript) {
977 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
979 render_text->SetText(WideToUTF16(L"\x0915\x093f\x0915\x094d\x0915"));
980 EXPECT_EQ(0U, render_text->cursor_position());
981 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
982 EXPECT_EQ(2U, render_text->cursor_position());
983 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
984 EXPECT_EQ(4U, render_text->cursor_position());
985 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
986 EXPECT_EQ(5U, render_text->cursor_position());
987 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
988 EXPECT_EQ(5U, render_text->cursor_position());
990 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
991 EXPECT_EQ(4U, render_text->cursor_position());
992 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
993 EXPECT_EQ(2U, render_text->cursor_position());
994 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
995 EXPECT_EQ(0U, render_text->cursor_position());
996 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
997 EXPECT_EQ(0U, render_text->cursor_position());
1000 TEST_F(RenderTextTest, MoveCursorLeftRight_MeiryoUILigatures) {
1001 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1002 // Meiryo UI uses single-glyph ligatures for 'ff' and 'ffi', but each letter
1003 // (code point) has unique bounds, so mid-glyph cursoring should be possible.
1004 render_text->SetFontList(FontList("Meiryo UI, 12px"));
1005 render_text->SetText(WideToUTF16(L"ff ffi"));
1006 EXPECT_EQ(0U, render_text->cursor_position());
1007 for (size_t i = 0; i < render_text->text().length(); ++i) {
1008 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1009 EXPECT_EQ(i + 1, render_text->cursor_position());
1011 EXPECT_EQ(6U, render_text->cursor_position());
1014 TEST_F(RenderTextTest, GraphemePositions) {
1015 // LTR 2-character grapheme, LTR abc, LTR 2-character grapheme.
1016 const base::string16 kText1 =
1017 WideToUTF16(L"\x0915\x093f" L"abc" L"\x0915\x093f");
1019 // LTR ab, LTR 2-character grapheme, LTR cd.
1020 const base::string16 kText2 = WideToUTF16(L"ab" L"\x0915\x093f" L"cd");
1022 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
1023 // two characters forming the surrogate pair 0x0001D11E.
1024 const std::string kSurrogate = "\xF0\x9D\x84\x9E";
1026 // LTR ab, UTF16 surrogate pair, LTR cd.
1027 const base::string16 kText3 = UTF8ToUTF16("ab" + kSurrogate + "cd");
1029 struct {
1030 base::string16 text;
1031 size_t index;
1032 size_t expected_previous;
1033 size_t expected_next;
1034 } cases[] = {
1035 { base::string16(), 0, 0, 0 },
1036 { base::string16(), 1, 0, 0 },
1037 { base::string16(), 50, 0, 0 },
1038 { kText1, 0, 0, 2 },
1039 { kText1, 1, 0, 2 },
1040 { kText1, 2, 0, 3 },
1041 { kText1, 3, 2, 4 },
1042 { kText1, 4, 3, 5 },
1043 { kText1, 5, 4, 7 },
1044 { kText1, 6, 5, 7 },
1045 { kText1, 7, 5, 7 },
1046 { kText1, 8, 7, 7 },
1047 { kText1, 50, 7, 7 },
1048 { kText2, 0, 0, 1 },
1049 { kText2, 1, 0, 2 },
1050 { kText2, 2, 1, 4 },
1051 { kText2, 3, 2, 4 },
1052 { kText2, 4, 2, 5 },
1053 { kText2, 5, 4, 6 },
1054 { kText2, 6, 5, 6 },
1055 { kText2, 7, 6, 6 },
1056 { kText2, 50, 6, 6 },
1057 { kText3, 0, 0, 1 },
1058 { kText3, 1, 0, 2 },
1059 { kText3, 2, 1, 4 },
1060 { kText3, 3, 2, 4 },
1061 { kText3, 4, 2, 5 },
1062 { kText3, 5, 4, 6 },
1063 { kText3, 6, 5, 6 },
1064 { kText3, 7, 6, 6 },
1065 { kText3, 50, 6, 6 },
1068 #if defined(OS_WIN)
1069 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1070 if (base::win::GetVersion() < base::win::VERSION_VISTA)
1071 return;
1072 #endif
1074 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1075 for (size_t i = 0; i < arraysize(cases); i++) {
1076 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
1077 render_text->SetText(cases[i].text);
1079 size_t next = render_text->IndexOfAdjacentGrapheme(cases[i].index,
1080 CURSOR_FORWARD);
1081 EXPECT_EQ(cases[i].expected_next, next);
1082 EXPECT_TRUE(render_text->IsValidCursorIndex(next));
1084 size_t previous = render_text->IndexOfAdjacentGrapheme(cases[i].index,
1085 CURSOR_BACKWARD);
1086 EXPECT_EQ(cases[i].expected_previous, previous);
1087 EXPECT_TRUE(render_text->IsValidCursorIndex(previous));
1091 TEST_F(RenderTextTest, MidGraphemeSelectionBounds) {
1092 #if defined(OS_WIN)
1093 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1094 if (base::win::GetVersion() < base::win::VERSION_VISTA)
1095 return;
1096 #endif
1098 // Test that selection bounds may be set amid multi-character graphemes.
1099 const base::string16 kHindi = WideToUTF16(L"\x0915\x093f");
1100 const base::string16 kThai = WideToUTF16(L"\x0e08\x0e33");
1101 const base::string16 cases[] = { kHindi, kThai };
1103 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1104 for (size_t i = 0; i < arraysize(cases); i++) {
1105 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
1106 render_text->SetText(cases[i]);
1107 EXPECT_TRUE(render_text->IsValidLogicalIndex(1));
1108 EXPECT_FALSE(render_text->IsValidCursorIndex(1));
1109 EXPECT_TRUE(render_text->SelectRange(Range(2, 1)));
1110 EXPECT_EQ(Range(2, 1), render_text->selection());
1111 EXPECT_EQ(1U, render_text->cursor_position());
1112 // Although selection bounds may be set within a multi-character grapheme,
1113 // cursor movement (e.g. via arrow key) should avoid those indices.
1114 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1115 EXPECT_EQ(0U, render_text->cursor_position());
1116 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1117 EXPECT_EQ(2U, render_text->cursor_position());
1121 TEST_F(RenderTextTest, FindCursorPosition) {
1122 const wchar_t* kTestStrings[] = { kLtrRtl, kLtrRtlLtr, kRtlLtr, kRtlLtrRtl };
1123 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1124 render_text->SetDisplayRect(Rect(0, 0, 100, 20));
1125 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1126 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS "]", i));
1127 render_text->SetText(WideToUTF16(kTestStrings[i]));
1128 for(size_t j = 0; j < render_text->text().length(); ++j) {
1129 const Range range(render_text->GetGlyphBounds(j));
1130 // Test a point just inside the leading edge of the glyph bounds.
1131 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1;
1132 EXPECT_EQ(j, render_text->FindCursorPosition(Point(x, 0)).caret_pos());
1136 #endif // !defined(OS_MACOSX)
1138 TEST_F(RenderTextTest, EdgeSelectionModels) {
1139 // Simple Latin text.
1140 const base::string16 kLatin = WideToUTF16(L"abc");
1141 // LTR 2-character grapheme.
1142 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f");
1143 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
1144 const base::string16 kHindiLatin =
1145 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f");
1146 // RTL 2-character grapheme.
1147 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8");
1148 // RTL 2-character grapheme, LTR a, RTL 2-character grapheme.
1149 const base::string16 kHebrewLatin =
1150 WideToUTF16(L"\x05e0\x05b8" L"a" L"\x05e0\x05b8");
1152 struct {
1153 base::string16 text;
1154 base::i18n::TextDirection expected_text_direction;
1155 } cases[] = {
1156 { base::string16(), base::i18n::LEFT_TO_RIGHT },
1157 { kLatin, base::i18n::LEFT_TO_RIGHT },
1158 { kLTRGrapheme, base::i18n::LEFT_TO_RIGHT },
1159 { kHindiLatin, base::i18n::LEFT_TO_RIGHT },
1160 { kRTLGrapheme, base::i18n::RIGHT_TO_LEFT },
1161 { kHebrewLatin, base::i18n::RIGHT_TO_LEFT },
1164 #if defined(OS_WIN)
1165 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1166 if (base::win::GetVersion() < base::win::VERSION_VISTA)
1167 return;
1168 #endif
1170 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1171 for (size_t i = 0; i < arraysize(cases); i++) {
1172 render_text->SetText(cases[i].text);
1173 bool ltr = (cases[i].expected_text_direction == base::i18n::LEFT_TO_RIGHT);
1175 SelectionModel start_edge =
1176 render_text->EdgeSelectionModel(ltr ? CURSOR_LEFT : CURSOR_RIGHT);
1177 EXPECT_EQ(start_edge, SelectionModel(0, CURSOR_BACKWARD));
1179 SelectionModel end_edge =
1180 render_text->EdgeSelectionModel(ltr ? CURSOR_RIGHT : CURSOR_LEFT);
1181 EXPECT_EQ(end_edge, SelectionModel(cases[i].text.length(), CURSOR_FORWARD));
1185 TEST_F(RenderTextTest, SelectAll) {
1186 const wchar_t* const cases[] =
1187 { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl };
1189 // Ensure that SelectAll respects the |reversed| argument regardless of
1190 // application locale and text content directionality.
1191 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1192 const SelectionModel expected_reversed(Range(3, 0), CURSOR_FORWARD);
1193 const SelectionModel expected_forwards(Range(0, 3), CURSOR_BACKWARD);
1194 const bool was_rtl = base::i18n::IsRTL();
1196 for (size_t i = 0; i < 2; ++i) {
1197 SetRTL(!base::i18n::IsRTL());
1198 // Test that an empty string produces an empty selection model.
1199 render_text->SetText(base::string16());
1200 EXPECT_EQ(render_text->selection_model(), SelectionModel());
1202 // Test the weak, LTR, RTL, and Bidi string cases.
1203 for (size_t j = 0; j < arraysize(cases); j++) {
1204 render_text->SetText(WideToUTF16(cases[j]));
1205 render_text->SelectAll(false);
1206 EXPECT_EQ(render_text->selection_model(), expected_forwards);
1207 render_text->SelectAll(true);
1208 EXPECT_EQ(render_text->selection_model(), expected_reversed);
1212 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1215 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
1216 #if !defined(OS_MACOSX)
1217 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) {
1218 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1219 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
1220 // Left arrow on select ranging (6, 4).
1221 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1222 EXPECT_EQ(Range(6), render_text->selection());
1223 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1224 EXPECT_EQ(Range(4), render_text->selection());
1225 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1226 EXPECT_EQ(Range(5), render_text->selection());
1227 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1228 EXPECT_EQ(Range(6), render_text->selection());
1229 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1230 EXPECT_EQ(Range(6, 5), render_text->selection());
1231 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1232 EXPECT_EQ(Range(6, 4), render_text->selection());
1233 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1234 EXPECT_EQ(Range(6), render_text->selection());
1236 // Right arrow on select ranging (4, 6).
1237 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1238 EXPECT_EQ(Range(0), render_text->selection());
1239 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1240 EXPECT_EQ(Range(1), render_text->selection());
1241 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1242 EXPECT_EQ(Range(2), render_text->selection());
1243 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1244 EXPECT_EQ(Range(3), render_text->selection());
1245 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1246 EXPECT_EQ(Range(5), render_text->selection());
1247 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1248 EXPECT_EQ(Range(4), render_text->selection());
1249 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1250 EXPECT_EQ(Range(4, 5), render_text->selection());
1251 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1252 EXPECT_EQ(Range(4, 6), render_text->selection());
1253 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1254 EXPECT_EQ(Range(4), render_text->selection());
1256 #endif // !defined(OS_MACOSX)
1258 TEST_F(RenderTextTest, CenteredDisplayOffset) {
1259 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1260 render_text->SetText(ASCIIToUTF16("abcdefghij"));
1261 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1263 const int kEnlargement = 10;
1264 const int content_width = render_text->GetContentWidth();
1265 Rect display_rect(0, 0, content_width / 2,
1266 render_text->font_list().GetHeight());
1267 render_text->SetDisplayRect(display_rect);
1269 // Move the cursor to the beginning of the text and, by checking the cursor
1270 // bounds, make sure no empty space is to the left of the text.
1271 render_text->SetCursorPosition(0);
1272 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1274 // Widen the display rect and, by checking the cursor bounds, make sure no
1275 // empty space is introduced to the left of the text.
1276 display_rect.Inset(0, 0, -kEnlargement, 0);
1277 render_text->SetDisplayRect(display_rect);
1278 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1280 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
1281 #if !defined(OS_MACOSX)
1282 // Move the cursor to the end of the text and, by checking the cursor bounds,
1283 // make sure no empty space is to the right of the text.
1284 render_text->SetCursorPosition(render_text->text().length());
1285 EXPECT_EQ(display_rect.right(),
1286 render_text->GetUpdatedCursorBounds().right());
1288 // Widen the display rect and, by checking the cursor bounds, make sure no
1289 // empty space is introduced to the right of the text.
1290 display_rect.Inset(0, 0, -kEnlargement, 0);
1291 render_text->SetDisplayRect(display_rect);
1292 EXPECT_EQ(display_rect.right(),
1293 render_text->GetUpdatedCursorBounds().right());
1294 #endif // !defined(OS_MACOSX)
1297 void MoveLeftRightByWordVerifier(RenderText* render_text,
1298 const wchar_t* str) {
1299 render_text->SetText(WideToUTF16(str));
1301 // Test moving by word from left ro right.
1302 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1303 bool first_word = true;
1304 while (true) {
1305 // First, test moving by word from a word break position, such as from
1306 // "|abc def" to "abc| def".
1307 SelectionModel start = render_text->selection_model();
1308 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1309 SelectionModel end = render_text->selection_model();
1310 if (end == start) // reach the end.
1311 break;
1313 // For testing simplicity, each word is a 3-character word.
1314 int num_of_character_moves = first_word ? 3 : 4;
1315 first_word = false;
1316 render_text->MoveCursorTo(start);
1317 for (int j = 0; j < num_of_character_moves; ++j)
1318 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1319 EXPECT_EQ(end, render_text->selection_model());
1321 // Then, test moving by word from positions inside the word, such as from
1322 // "a|bc def" to "abc| def", and from "ab|c def" to "abc| def".
1323 for (int j = 1; j < num_of_character_moves; ++j) {
1324 render_text->MoveCursorTo(start);
1325 for (int k = 0; k < j; ++k)
1326 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1327 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1328 EXPECT_EQ(end, render_text->selection_model());
1332 // Test moving by word from right to left.
1333 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1334 first_word = true;
1335 while (true) {
1336 SelectionModel start = render_text->selection_model();
1337 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1338 SelectionModel end = render_text->selection_model();
1339 if (end == start) // reach the end.
1340 break;
1342 int num_of_character_moves = first_word ? 3 : 4;
1343 first_word = false;
1344 render_text->MoveCursorTo(start);
1345 for (int j = 0; j < num_of_character_moves; ++j)
1346 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1347 EXPECT_EQ(end, render_text->selection_model());
1349 for (int j = 1; j < num_of_character_moves; ++j) {
1350 render_text->MoveCursorTo(start);
1351 for (int k = 0; k < j; ++k)
1352 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1353 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1354 EXPECT_EQ(end, render_text->selection_model());
1359 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
1360 #if !defined(OS_MACOSX)
1361 // TODO(msw): Make these work on Windows.
1362 #if !defined(OS_WIN)
1363 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText) {
1364 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1366 // For testing simplicity, each word is a 3-character word.
1367 std::vector<const wchar_t*> test;
1368 test.push_back(L"abc");
1369 test.push_back(L"abc def");
1370 test.push_back(L"\x05E1\x05E2\x05E3");
1371 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1372 test.push_back(L"abc \x05E1\x05E2\x05E3");
1373 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1374 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1375 L" \x05E7\x05E8\x05E9");
1377 test.push_back(L"abc \x05E1\x05E2\x05E3 hij");
1378 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 hij opq");
1379 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1380 L" \x05E7\x05E8\x05E9" L" opq rst uvw");
1382 test.push_back(L"\x05E1\x05E2\x05E3 abc");
1383 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 abc def");
1384 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 \x05E7\x05E8\x05E9"
1385 L" abc def hij");
1387 test.push_back(L"\x05D1\x05D2\x05D3 abc \x05E1\x05E2\x05E3");
1388 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 abc def"
1389 L" \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1390 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 \x05D7\x05D8\x05D9"
1391 L" abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1392 L" \x05E7\x05E8\x05E9");
1394 for (size_t i = 0; i < test.size(); ++i)
1395 MoveLeftRightByWordVerifier(render_text.get(), test[i]);
1398 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText_TestEndOfText) {
1399 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1401 render_text->SetText(WideToUTF16(L"ab\x05E1"));
1402 // Moving the cursor by word from "abC|" to the left should return "|abC".
1403 // But since end of text is always treated as a word break, it returns
1404 // position "ab|C".
1405 // TODO(xji): Need to make it work as expected.
1406 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1407 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1408 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1410 // Moving the cursor by word from "|abC" to the right returns "abC|".
1411 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1412 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1413 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1415 render_text->SetText(WideToUTF16(L"\x05E1\x05E2" L"a"));
1416 // For logical text "BCa", moving the cursor by word from "aCB|" to the left
1417 // returns "|aCB".
1418 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1419 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1420 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1422 // Moving the cursor by word from "|aCB" to the right should return "aCB|".
1423 // But since end of text is always treated as a word break, it returns
1424 // position "a|CB".
1425 // TODO(xji): Need to make it work as expected.
1426 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1427 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1428 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1431 TEST_F(RenderTextTest, MoveLeftRightByWordInTextWithMultiSpaces) {
1432 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1433 render_text->SetText(WideToUTF16(L"abc def"));
1434 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1435 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1436 EXPECT_EQ(11U, render_text->cursor_position());
1438 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1439 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1440 EXPECT_EQ(0U, render_text->cursor_position());
1442 #endif // !defined(OS_WIN)
1444 TEST_F(RenderTextTest, MoveLeftRightByWordInChineseText) {
1445 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1446 render_text->SetText(WideToUTF16(L"\x6211\x4EEC\x53BB\x516C\x56ED\x73A9"));
1447 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1448 EXPECT_EQ(0U, render_text->cursor_position());
1449 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1450 EXPECT_EQ(2U, render_text->cursor_position());
1451 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1452 EXPECT_EQ(3U, render_text->cursor_position());
1453 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1454 EXPECT_EQ(5U, render_text->cursor_position());
1455 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1456 EXPECT_EQ(6U, render_text->cursor_position());
1457 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1458 EXPECT_EQ(6U, render_text->cursor_position());
1460 #endif // !defined(OS_MACOSX)
1462 TEST_F(RenderTextTest, StringSizeSanity) {
1463 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1464 render_text->SetText(UTF8ToUTF16("Hello World"));
1465 const Size string_size = render_text->GetStringSize();
1466 EXPECT_GT(string_size.width(), 0);
1467 EXPECT_GT(string_size.height(), 0);
1470 TEST_F(RenderTextTest, StringSizeLongStrings) {
1471 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1472 Size previous_string_size;
1473 for (size_t length = 10; length < 1000000; length *= 10) {
1474 render_text->SetText(base::string16(length, 'a'));
1475 const Size string_size = render_text->GetStringSize();
1476 EXPECT_GT(string_size.width(), previous_string_size.width());
1477 EXPECT_GT(string_size.height(), 0);
1478 previous_string_size = string_size;
1482 // TODO(asvitkine): This test fails because PlatformFontMac uses point font
1483 // sizes instead of pixel sizes like other implementations.
1484 #if !defined(OS_MACOSX)
1485 TEST_F(RenderTextTest, StringSizeEmptyString) {
1486 // Ascent and descent of Arial and Symbol are different on most platforms.
1487 const FontList font_list("Arial,Symbol, 16px");
1488 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1489 render_text->SetFontList(font_list);
1490 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight()));
1492 // The empty string respects FontList metrics for non-zero height
1493 // and baseline.
1494 render_text->SetText(base::string16());
1495 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1496 EXPECT_EQ(0, render_text->GetStringSize().width());
1497 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1499 render_text->SetText(UTF8ToUTF16(" "));
1500 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1501 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1503 #endif // !defined(OS_MACOSX)
1505 TEST_F(RenderTextTest, StringSizeRespectsFontListMetrics) {
1506 // Check that Arial and Symbol have different font metrics.
1507 Font arial_font("Arial", 16);
1508 ASSERT_EQ("arial",
1509 base::ToLowerASCII(arial_font.GetActualFontNameForTesting()));
1510 Font symbol_font("Symbol", 16);
1511 ASSERT_EQ("symbol",
1512 base::ToLowerASCII(symbol_font.GetActualFontNameForTesting()));
1513 EXPECT_NE(arial_font.GetHeight(), symbol_font.GetHeight());
1514 EXPECT_NE(arial_font.GetBaseline(), symbol_font.GetBaseline());
1515 // "a" should be rendered with Arial, not with Symbol.
1516 const char* arial_font_text = "a";
1517 // "®" (registered trademark symbol) should be rendered with Symbol,
1518 // not with Arial.
1519 const char* symbol_font_text = "\xC2\xAE";
1521 Font smaller_font = arial_font;
1522 Font larger_font = symbol_font;
1523 const char* smaller_font_text = arial_font_text;
1524 const char* larger_font_text = symbol_font_text;
1525 if (symbol_font.GetHeight() < arial_font.GetHeight() &&
1526 symbol_font.GetBaseline() < arial_font.GetBaseline()) {
1527 std::swap(smaller_font, larger_font);
1528 std::swap(smaller_font_text, larger_font_text);
1530 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight());
1531 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline());
1533 // Check |smaller_font_text| is rendered with the smaller font.
1534 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1535 render_text->SetText(UTF8ToUTF16(smaller_font_text));
1536 render_text->SetFontList(FontList(smaller_font));
1537 render_text->SetDisplayRect(Rect(0, 0, 0,
1538 render_text->font_list().GetHeight()));
1539 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height());
1540 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline());
1542 // Layout the same text with mixed fonts. The text should be rendered with
1543 // the smaller font, but the height and baseline are determined with the
1544 // metrics of the font list, which is equal to the larger font.
1545 std::vector<Font> fonts;
1546 fonts.push_back(smaller_font); // The primary font is the smaller font.
1547 fonts.push_back(larger_font);
1548 const FontList font_list(fonts);
1549 render_text->SetFontList(font_list);
1550 render_text->SetDisplayRect(Rect(0, 0, 0,
1551 render_text->font_list().GetHeight()));
1552 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height());
1553 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline());
1554 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1555 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1558 TEST_F(RenderTextTest, MinLineHeight) {
1559 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1561 render_text->SetText(ASCIIToUTF16("Hello!"));
1562 SizeF default_size = render_text->GetStringSizeF();
1563 ASSERT_NE(0, default_size.height());
1564 ASSERT_NE(0, default_size.width());
1566 render_text->SetMinLineHeight(default_size.height() / 2);
1567 EXPECT_EQ(default_size.ToString(), render_text->GetStringSizeF().ToString());
1569 render_text->SetMinLineHeight(default_size.height() * 2);
1570 SizeF taller_size = render_text->GetStringSizeF();
1571 EXPECT_EQ(default_size.height() * 2, taller_size.height());
1572 EXPECT_EQ(default_size.width(), taller_size.width());
1575 TEST_F(RenderTextTest, SetFontList) {
1576 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1577 render_text->SetFontList(FontList("Arial,Symbol, 13px"));
1578 const std::vector<Font>& fonts = render_text->font_list().GetFonts();
1579 ASSERT_EQ(2U, fonts.size());
1580 EXPECT_EQ("Arial", fonts[0].GetFontName());
1581 EXPECT_EQ("Symbol", fonts[1].GetFontName());
1582 EXPECT_EQ(13, render_text->font_list().GetFontSize());
1585 TEST_F(RenderTextTest, StringSizeBoldWidth) {
1586 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1587 render_text->SetText(UTF8ToUTF16("Hello World"));
1589 const int plain_width = render_text->GetStringSize().width();
1590 EXPECT_GT(plain_width, 0);
1592 // Apply a bold style and check that the new width is greater.
1593 render_text->SetStyle(BOLD, true);
1594 const int bold_width = render_text->GetStringSize().width();
1595 EXPECT_GT(bold_width, plain_width);
1597 // Now, apply a plain style over the first word only.
1598 render_text->ApplyStyle(BOLD, false, Range(0, 5));
1599 const int plain_bold_width = render_text->GetStringSize().width();
1600 EXPECT_GT(plain_bold_width, plain_width);
1601 EXPECT_LT(plain_bold_width, bold_width);
1604 TEST_F(RenderTextTest, StringSizeHeight) {
1605 base::string16 cases[] = {
1606 WideToUTF16(L"Hello World!"), // English
1607 WideToUTF16(L"\x6328\x62f6"), // Japanese
1608 WideToUTF16(L"\x0915\x093f"), // Hindi
1609 WideToUTF16(L"\x05e0\x05b8"), // Hebrew
1612 const FontList default_font_list;
1613 const FontList& larger_font_list = default_font_list.DeriveWithSizeDelta(24);
1614 EXPECT_GT(larger_font_list.GetHeight(), default_font_list.GetHeight());
1616 for (size_t i = 0; i < arraysize(cases); i++) {
1617 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1618 render_text->SetFontList(default_font_list);
1619 render_text->SetText(cases[i]);
1621 const int height1 = render_text->GetStringSize().height();
1622 EXPECT_GT(height1, 0);
1624 // Check that setting the larger font increases the height.
1625 render_text->SetFontList(larger_font_list);
1626 const int height2 = render_text->GetStringSize().height();
1627 EXPECT_GT(height2, height1);
1631 TEST_F(RenderTextTest, GetBaselineSanity) {
1632 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1633 render_text->SetText(UTF8ToUTF16("Hello World"));
1634 const int baseline = render_text->GetBaseline();
1635 EXPECT_GT(baseline, 0);
1638 TEST_F(RenderTextTest, CursorBoundsInReplacementMode) {
1639 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1640 render_text->SetText(ASCIIToUTF16("abcdefg"));
1641 render_text->SetDisplayRect(Rect(100, 17));
1642 SelectionModel sel_b(1, CURSOR_FORWARD);
1643 SelectionModel sel_c(2, CURSOR_FORWARD);
1644 Rect cursor_around_b = render_text->GetCursorBounds(sel_b, false);
1645 Rect cursor_before_b = render_text->GetCursorBounds(sel_b, true);
1646 Rect cursor_before_c = render_text->GetCursorBounds(sel_c, true);
1647 EXPECT_EQ(cursor_around_b.x(), cursor_before_b.x());
1648 EXPECT_EQ(cursor_around_b.right(), cursor_before_c.x());
1651 TEST_F(RenderTextTest, GetTextOffset) {
1652 // The default horizontal text offset differs for LTR and RTL, and is only set
1653 // when the RenderText object is created. This test will check the default in
1654 // LTR mode, and the next test will check the RTL default.
1655 const bool was_rtl = base::i18n::IsRTL();
1656 SetRTL(false);
1657 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1658 render_text->SetText(ASCIIToUTF16("abcdefg"));
1659 render_text->SetFontList(FontList("Arial, 13px"));
1661 // Set display area's size equal to the font size.
1662 const Size font_size(render_text->GetContentWidth(),
1663 render_text->font_list().GetHeight());
1664 Rect display_rect(font_size);
1665 render_text->SetDisplayRect(display_rect);
1667 Vector2d offset = render_text->GetLineOffset(0);
1668 EXPECT_TRUE(offset.IsZero());
1670 const int kEnlargementX = 2;
1671 display_rect.Inset(0, 0, -kEnlargementX, 0);
1672 render_text->SetDisplayRect(display_rect);
1674 // Check the default horizontal alignment.
1675 offset = render_text->GetLineOffset(0);
1676 EXPECT_EQ(0, offset.x());
1678 // Check explicitly setting the horizontal alignment.
1679 render_text->SetHorizontalAlignment(ALIGN_LEFT);
1680 offset = render_text->GetLineOffset(0);
1681 EXPECT_EQ(0, offset.x());
1682 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1683 offset = render_text->GetLineOffset(0);
1684 EXPECT_EQ(kEnlargementX / 2, offset.x());
1685 render_text->SetHorizontalAlignment(ALIGN_RIGHT);
1686 offset = render_text->GetLineOffset(0);
1687 EXPECT_EQ(kEnlargementX, offset.x());
1689 // Check that text is vertically centered within taller display rects.
1690 const int kEnlargementY = display_rect.height();
1691 display_rect.Inset(0, 0, 0, -kEnlargementY);
1692 render_text->SetDisplayRect(display_rect);
1693 const Vector2d prev_offset = render_text->GetLineOffset(0);
1694 display_rect.Inset(0, 0, 0, -2 * kEnlargementY);
1695 render_text->SetDisplayRect(display_rect);
1696 offset = render_text->GetLineOffset(0);
1697 EXPECT_EQ(prev_offset.y() + kEnlargementY, offset.y());
1699 SetRTL(was_rtl);
1702 TEST_F(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL) {
1703 // This only checks the default horizontal alignment in RTL mode; all other
1704 // GetLineOffset(0) attributes are checked by the test above.
1705 const bool was_rtl = base::i18n::IsRTL();
1706 SetRTL(true);
1707 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1708 render_text->SetText(ASCIIToUTF16("abcdefg"));
1709 render_text->SetFontList(FontList("Arial, 13px"));
1710 const int kEnlargement = 2;
1711 const Size font_size(render_text->GetContentWidth() + kEnlargement,
1712 render_text->GetStringSize().height());
1713 Rect display_rect(font_size);
1714 render_text->SetDisplayRect(display_rect);
1715 Vector2d offset = render_text->GetLineOffset(0);
1716 EXPECT_EQ(kEnlargement, offset.x());
1717 SetRTL(was_rtl);
1720 TEST_F(RenderTextTest, SetDisplayOffset) {
1721 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1722 render_text->SetText(ASCIIToUTF16("abcdefg"));
1723 render_text->SetFontList(FontList("Arial, 13px"));
1725 const Size font_size(render_text->GetContentWidth(),
1726 render_text->font_list().GetHeight());
1727 const int kEnlargement = 10;
1729 // Set display width |kEnlargement| pixels greater than content width and test
1730 // different possible situations. In this case the only possible display
1731 // offset is zero.
1732 Rect display_rect(font_size);
1733 display_rect.Inset(0, 0, -kEnlargement, 0);
1734 render_text->SetDisplayRect(display_rect);
1736 struct {
1737 HorizontalAlignment alignment;
1738 int offset;
1739 } small_content_cases[] = {
1740 { ALIGN_LEFT, -kEnlargement },
1741 { ALIGN_LEFT, 0 },
1742 { ALIGN_LEFT, kEnlargement },
1743 { ALIGN_RIGHT, -kEnlargement },
1744 { ALIGN_RIGHT, 0 },
1745 { ALIGN_RIGHT, kEnlargement },
1746 { ALIGN_CENTER, -kEnlargement },
1747 { ALIGN_CENTER, 0 },
1748 { ALIGN_CENTER, kEnlargement },
1751 for (size_t i = 0; i < arraysize(small_content_cases); i++) {
1752 render_text->SetHorizontalAlignment(small_content_cases[i].alignment);
1753 render_text->SetDisplayOffset(small_content_cases[i].offset);
1754 EXPECT_EQ(0, render_text->GetUpdatedDisplayOffset().x());
1757 // Set display width |kEnlargement| pixels less than content width and test
1758 // different possible situations.
1759 display_rect = Rect(font_size);
1760 display_rect.Inset(0, 0, kEnlargement, 0);
1761 render_text->SetDisplayRect(display_rect);
1763 struct {
1764 HorizontalAlignment alignment;
1765 int offset;
1766 int expected_offset;
1767 } large_content_cases[] = {
1768 // When text is left-aligned, display offset can be in range
1769 // [-kEnlargement, 0].
1770 { ALIGN_LEFT, -2 * kEnlargement, -kEnlargement },
1771 { ALIGN_LEFT, -kEnlargement / 2, -kEnlargement / 2 },
1772 { ALIGN_LEFT, kEnlargement, 0 },
1773 // When text is right-aligned, display offset can be in range
1774 // [0, kEnlargement].
1775 { ALIGN_RIGHT, -kEnlargement, 0 },
1776 { ALIGN_RIGHT, kEnlargement / 2, kEnlargement / 2 },
1777 { ALIGN_RIGHT, 2 * kEnlargement, kEnlargement },
1778 // When text is center-aligned, display offset can be in range
1779 // [-kEnlargement / 2 - 1, (kEnlargement - 1) / 2].
1780 { ALIGN_CENTER, -kEnlargement, -kEnlargement / 2 - 1 },
1781 { ALIGN_CENTER, -kEnlargement / 4, -kEnlargement / 4 },
1782 { ALIGN_CENTER, kEnlargement / 4, kEnlargement / 4 },
1783 { ALIGN_CENTER, kEnlargement, (kEnlargement - 1) / 2 },
1786 for (size_t i = 0; i < arraysize(large_content_cases); i++) {
1787 render_text->SetHorizontalAlignment(large_content_cases[i].alignment);
1788 render_text->SetDisplayOffset(large_content_cases[i].offset);
1789 EXPECT_EQ(large_content_cases[i].expected_offset,
1790 render_text->GetUpdatedDisplayOffset().x());
1794 TEST_F(RenderTextTest, SameFontForParentheses) {
1795 struct {
1796 const base::char16 left_char;
1797 const base::char16 right_char;
1798 } punctuation_pairs[] = {
1799 { '(', ')' },
1800 { '{', '}' },
1801 { '<', '>' },
1803 struct {
1804 base::string16 text;
1805 } cases[] = {
1806 // English(English)
1807 { WideToUTF16(L"Hello World(a)") },
1808 // English(English)English
1809 { WideToUTF16(L"Hello World(a)Hello World") },
1811 // Japanese(English)
1812 { WideToUTF16(L"\x6328\x62f6(a)") },
1813 // Japanese(English)Japanese
1814 { WideToUTF16(L"\x6328\x62f6(a)\x6328\x62f6") },
1815 // English(Japanese)English
1816 { WideToUTF16(L"Hello World(\x6328\x62f6)Hello World") },
1818 // Hindi(English)
1819 { WideToUTF16(L"\x0915\x093f(a)") },
1820 // Hindi(English)Hindi
1821 { WideToUTF16(L"\x0915\x093f(a)\x0915\x093f") },
1822 // English(Hindi)English
1823 { WideToUTF16(L"Hello World(\x0915\x093f)Hello World") },
1825 // Hebrew(English)
1826 { WideToUTF16(L"\x05e0\x05b8(a)") },
1827 // Hebrew(English)Hebrew
1828 { WideToUTF16(L"\x05e0\x05b8(a)\x05e0\x05b8") },
1829 // English(Hebrew)English
1830 { WideToUTF16(L"Hello World(\x05e0\x05b8)Hello World") },
1833 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1834 for (size_t i = 0; i < arraysize(cases); ++i) {
1835 base::string16 text = cases[i].text;
1836 const size_t start_paren_char_index = text.find('(');
1837 ASSERT_NE(base::string16::npos, start_paren_char_index);
1838 const size_t end_paren_char_index = text.find(')');
1839 ASSERT_NE(base::string16::npos, end_paren_char_index);
1841 for (size_t j = 0; j < arraysize(punctuation_pairs); ++j) {
1842 text[start_paren_char_index] = punctuation_pairs[j].left_char;
1843 text[end_paren_char_index] = punctuation_pairs[j].right_char;
1844 render_text->SetText(text);
1846 const std::vector<RenderText::FontSpan> spans =
1847 render_text->GetFontSpansForTesting();
1849 int start_paren_span_index = -1;
1850 int end_paren_span_index = -1;
1851 for (size_t k = 0; k < spans.size(); ++k) {
1852 if (IndexInRange(spans[k].second, start_paren_char_index))
1853 start_paren_span_index = k;
1854 if (IndexInRange(spans[k].second, end_paren_char_index))
1855 end_paren_span_index = k;
1857 ASSERT_NE(-1, start_paren_span_index);
1858 ASSERT_NE(-1, end_paren_span_index);
1860 const Font& start_font = spans[start_paren_span_index].first;
1861 const Font& end_font = spans[end_paren_span_index].first;
1862 EXPECT_EQ(start_font.GetFontName(), end_font.GetFontName());
1863 EXPECT_EQ(start_font.GetFontSize(), end_font.GetFontSize());
1864 EXPECT_EQ(start_font.GetStyle(), end_font.GetStyle());
1869 // Make sure the caret width is always >=1 so that the correct
1870 // caret is drawn at high DPI. crbug.com/164100.
1871 TEST_F(RenderTextTest, CaretWidth) {
1872 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1873 render_text->SetText(ASCIIToUTF16("abcdefg"));
1874 EXPECT_GE(render_text->GetUpdatedCursorBounds().width(), 1);
1877 TEST_F(RenderTextTest, SelectWord) {
1878 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1879 render_text->SetText(ASCIIToUTF16(" foo a.bc.d bar"));
1881 struct {
1882 size_t cursor;
1883 size_t selection_start;
1884 size_t selection_end;
1885 } cases[] = {
1886 { 0, 0, 1 },
1887 { 1, 1, 4 },
1888 { 2, 1, 4 },
1889 { 3, 1, 4 },
1890 { 4, 4, 6 },
1891 { 5, 4, 6 },
1892 { 6, 6, 7 },
1893 { 7, 7, 8 },
1894 { 8, 8, 10 },
1895 { 9, 8, 10 },
1896 { 10, 10, 11 },
1897 { 11, 11, 12 },
1898 { 12, 12, 13 },
1899 { 13, 13, 16 },
1900 { 14, 13, 16 },
1901 { 15, 13, 16 },
1902 { 16, 13, 16 },
1905 for (size_t i = 0; i < arraysize(cases); ++i) {
1906 render_text->SetCursorPosition(cases[i].cursor);
1907 render_text->SelectWord();
1908 EXPECT_EQ(Range(cases[i].selection_start, cases[i].selection_end),
1909 render_text->selection());
1913 // Make sure the last word is selected when the cursor is at text.length().
1914 TEST_F(RenderTextTest, LastWordSelected) {
1915 const std::string kTestURL1 = "http://www.google.com";
1916 const std::string kTestURL2 = "http://www.google.com/something/";
1918 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1920 render_text->SetText(ASCIIToUTF16(kTestURL1));
1921 render_text->SetCursorPosition(kTestURL1.length());
1922 render_text->SelectWord();
1923 EXPECT_EQ(ASCIIToUTF16("com"), GetSelectedText(render_text.get()));
1924 EXPECT_FALSE(render_text->selection().is_reversed());
1926 render_text->SetText(ASCIIToUTF16(kTestURL2));
1927 render_text->SetCursorPosition(kTestURL2.length());
1928 render_text->SelectWord();
1929 EXPECT_EQ(ASCIIToUTF16("/"), GetSelectedText(render_text.get()));
1930 EXPECT_FALSE(render_text->selection().is_reversed());
1933 // When given a non-empty selection, SelectWord should expand the selection to
1934 // nearest word boundaries.
1935 TEST_F(RenderTextTest, SelectMultipleWords) {
1936 const std::string kTestURL = "http://www.google.com";
1938 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1940 render_text->SetText(ASCIIToUTF16(kTestURL));
1941 render_text->SelectRange(Range(16, 20));
1942 render_text->SelectWord();
1943 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1944 EXPECT_FALSE(render_text->selection().is_reversed());
1946 // SelectWord should preserve the selection direction.
1947 render_text->SelectRange(Range(20, 16));
1948 render_text->SelectWord();
1949 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1950 EXPECT_TRUE(render_text->selection().is_reversed());
1953 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
1954 #if !defined(OS_MACOSX)
1955 TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) {
1956 ASSERT_FALSE(base::i18n::IsRTL());
1957 ASSERT_FALSE(base::i18n::ICUIsRTL());
1959 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1960 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1961 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1962 CURSOR_FORWARD));
1963 int width = render_text->GetStringSize().width();
1964 ASSERT_GT(width, 10);
1966 // Ensure that the cursor is placed at the width of its preceding text.
1967 render_text->SetDisplayRect(Rect(width + 10, 1));
1968 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1970 // Ensure that shrinking the display rectangle keeps the cursor in view.
1971 render_text->SetDisplayRect(Rect(width - 10, 1));
1972 EXPECT_EQ(render_text->display_rect().width(),
1973 render_text->GetUpdatedCursorBounds().right());
1975 // Ensure that the text will pan to fill its expanding display rectangle.
1976 render_text->SetDisplayRect(Rect(width - 5, 1));
1977 EXPECT_EQ(render_text->display_rect().width(),
1978 render_text->GetUpdatedCursorBounds().right());
1980 // Ensure that a sufficiently large display rectangle shows all the text.
1981 render_text->SetDisplayRect(Rect(width + 10, 1));
1982 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1984 // Repeat the test with RTL text.
1985 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1986 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1987 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1988 width = render_text->GetStringSize().width();
1989 ASSERT_GT(width, 10);
1991 // Ensure that the cursor is placed at the width of its preceding text.
1992 render_text->SetDisplayRect(Rect(width + 10, 1));
1993 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1995 // Ensure that shrinking the display rectangle keeps the cursor in view.
1996 render_text->SetDisplayRect(Rect(width - 10, 1));
1997 EXPECT_EQ(render_text->display_rect().width(),
1998 render_text->GetUpdatedCursorBounds().right());
2000 // Ensure that the text will pan to fill its expanding display rectangle.
2001 render_text->SetDisplayRect(Rect(width - 5, 1));
2002 EXPECT_EQ(render_text->display_rect().width(),
2003 render_text->GetUpdatedCursorBounds().right());
2005 // Ensure that a sufficiently large display rectangle shows all the text.
2006 render_text->SetDisplayRect(Rect(width + 10, 1));
2007 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
2009 #endif // !defined(OS_MACOSX)
2011 TEST_F(RenderTextTest, DisplayRectShowsCursorRTL) {
2012 // Set the application default text direction to RTL.
2013 const bool was_rtl = base::i18n::IsRTL();
2014 SetRTL(true);
2016 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2017 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
2018 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
2019 int width = render_text->GetStringSize().width();
2020 ASSERT_GT(width, 10);
2022 // Ensure that the cursor is placed at the width of its preceding text.
2023 render_text->SetDisplayRect(Rect(width + 10, 1));
2024 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2025 render_text->GetUpdatedCursorBounds().x());
2027 // Ensure that shrinking the display rectangle keeps the cursor in view.
2028 render_text->SetDisplayRect(Rect(width - 10, 1));
2029 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2031 // Ensure that the text will pan to fill its expanding display rectangle.
2032 render_text->SetDisplayRect(Rect(width - 5, 1));
2033 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2035 // Ensure that a sufficiently large display rectangle shows all the text.
2036 render_text->SetDisplayRect(Rect(width + 10, 1));
2037 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2038 render_text->GetUpdatedCursorBounds().x());
2040 // Repeat the test with RTL text.
2041 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
2042 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
2043 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
2044 CURSOR_FORWARD));
2045 width = render_text->GetStringSize().width();
2046 ASSERT_GT(width, 10);
2048 // Ensure that the cursor is placed at the width of its preceding text.
2049 render_text->SetDisplayRect(Rect(width + 10, 1));
2050 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2051 render_text->GetUpdatedCursorBounds().x());
2053 // Ensure that shrinking the display rectangle keeps the cursor in view.
2054 render_text->SetDisplayRect(Rect(width - 10, 1));
2055 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2057 // Ensure that the text will pan to fill its expanding display rectangle.
2058 render_text->SetDisplayRect(Rect(width - 5, 1));
2059 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2061 // Ensure that a sufficiently large display rectangle shows all the text.
2062 render_text->SetDisplayRect(Rect(width + 10, 1));
2063 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2064 render_text->GetUpdatedCursorBounds().x());
2066 // Reset the application default text direction to LTR.
2067 SetRTL(was_rtl);
2068 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
2071 // Changing colors between or inside ligated glyphs should not break shaping.
2072 TEST_F(RenderTextTest, SelectionKeepsLigatures) {
2073 const wchar_t* kTestStrings[] = { L"\x644\x623", L"\x633\x627" };
2074 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2075 render_text->set_selection_color(SK_ColorRED);
2076 Canvas canvas;
2078 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2079 render_text->SetText(WideToUTF16(kTestStrings[i]));
2080 const int expected_width = render_text->GetStringSize().width();
2081 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD));
2082 EXPECT_EQ(expected_width, render_text->GetStringSize().width());
2083 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119
2084 render_text->Draw(&canvas);
2085 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
2089 // Ensure strings wrap onto multiple lines for a small available width.
2090 TEST_F(RenderTextTest, Multiline_MinWidth) {
2091 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl,
2092 kRtlLtr, kRtlLtrRtl };
2094 RenderTextHarfBuzz render_text;
2095 render_text.SetDisplayRect(Rect(1, 1000));
2096 render_text.SetMultiline(true);
2097 render_text.SetWordWrapBehavior(WRAP_LONG_WORDS);
2098 Canvas canvas;
2100 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2101 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2102 render_text.SetText(WideToUTF16(kTestStrings[i]));
2103 render_text.Draw(&canvas);
2104 EXPECT_GT(render_text.lines_.size(), 1U);
2108 // Ensure strings wrap onto multiple lines for a normal available width.
2109 TEST_F(RenderTextTest, Multiline_NormalWidth) {
2110 const struct {
2111 const wchar_t* const text;
2112 const Range first_line_char_range;
2113 const Range second_line_char_range;
2114 bool is_ltr;
2115 } kTestStrings[] = {
2116 { L"abc defg hijkl", Range(0, 9), Range(9, 14), true },
2117 { L"qwertyzxcvbn", Range(0, 10), Range(10, 12), true },
2118 { L"\x0627\x0644\x0644\x063A\x0629 "
2119 L"\x0627\x0644\x0639\x0631\x0628\x064A\x0629",
2120 Range(0, 6), Range(6, 13), false },
2121 { L"\x062A\x0641\x0627\x062D \x05EA\x05E4\x05D5\x05D6\x05D9"
2122 L"\x05DA\x05DB\x05DD", Range(0, 5), Range(5, 13), false }
2125 RenderTextHarfBuzz render_text;
2126 // Specify the fixed width for characters to suppress the possible variations
2127 // of linebreak results.
2128 render_text.set_glyph_width_for_test(5);
2129 render_text.SetDisplayRect(Rect(50, 1000));
2130 render_text.SetMultiline(true);
2131 render_text.SetWordWrapBehavior(WRAP_LONG_WORDS);
2132 render_text.SetHorizontalAlignment(ALIGN_TO_HEAD);
2134 Canvas canvas;
2135 TestSkiaTextRenderer renderer(&canvas);
2137 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2138 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2139 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2140 render_text.EnsureLayout();
2141 render_text.DrawVisualTextInternal(&renderer);
2143 ASSERT_EQ(2U, render_text.lines_.size());
2144 ASSERT_EQ(1U, render_text.lines_[0].segments.size());
2145 EXPECT_EQ(kTestStrings[i].first_line_char_range,
2146 render_text.lines_[0].segments[0].char_range);
2147 ASSERT_EQ(1U, render_text.lines_[1].segments.size());
2148 EXPECT_EQ(kTestStrings[i].second_line_char_range,
2149 render_text.lines_[1].segments[0].char_range);
2151 std::vector<TestSkiaTextRenderer::TextLog> text_log;
2152 renderer.GetTextLogAndReset(&text_log);
2153 ASSERT_EQ(2U, text_log.size());
2154 // NOTE: this expectation compares the character length and glyph counts,
2155 // which isn't always equal. This is okay only because all the test
2156 // strings are simple (like, no compound characters nor UTF16-surrogate
2157 // pairs). Be careful in case more complicated test strings are added.
2158 EXPECT_EQ(kTestStrings[i].first_line_char_range.length(),
2159 text_log[0].glyph_count);
2160 EXPECT_EQ(kTestStrings[i].second_line_char_range.length(),
2161 text_log[1].glyph_count);
2162 EXPECT_LT(text_log[0].origin.y(), text_log[1].origin.y());
2163 if (kTestStrings[i].is_ltr) {
2164 EXPECT_EQ(0, text_log[0].origin.x());
2165 EXPECT_EQ(0, text_log[1].origin.x());
2166 } else {
2167 EXPECT_LT(0, text_log[0].origin.x());
2168 EXPECT_LT(0, text_log[1].origin.x());
2173 // Ensure strings don't wrap onto multiple lines for a sufficient available
2174 // width.
2175 TEST_F(RenderTextTest, Multiline_SufficientWidth) {
2176 const wchar_t* kTestStrings[] = { L"", L" ", L".", L" . ", L"abc", L"a b c",
2177 L"\x62E\x628\x632", L"\x62E \x628 \x632" };
2179 RenderTextHarfBuzz render_text;
2180 render_text.SetDisplayRect(Rect(1000, 1000));
2181 render_text.SetMultiline(true);
2182 Canvas canvas;
2184 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2185 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2186 render_text.SetText(WideToUTF16(kTestStrings[i]));
2187 render_text.Draw(&canvas);
2188 EXPECT_EQ(1U, render_text.lines_.size());
2192 TEST_F(RenderTextTest, Multiline_Newline) {
2193 const struct {
2194 const wchar_t* const text;
2195 const size_t lines_count;
2196 // Ranges of the characters on each line preceding the newline.
2197 const Range line_char_ranges[3];
2198 } kTestStrings[] = {
2199 {L"abc\ndef", 2ul, { Range(0, 3), Range(4, 7), Range::InvalidRange() } },
2200 {L"a \n b ", 2ul, { Range(0, 2), Range(3, 6), Range::InvalidRange() } },
2201 {L"ab\n", 2ul, { Range(0, 2), Range(), Range::InvalidRange() } },
2202 {L"a\n\nb", 3ul, { Range(0, 1), Range(), Range(3, 4) } },
2203 {L"\nab", 2ul, { Range(), Range(1, 3), Range::InvalidRange() } },
2204 {L"\n", 2ul, { Range(), Range(), Range::InvalidRange() } },
2207 RenderTextHarfBuzz render_text;
2208 render_text.SetDisplayRect(Rect(200, 1000));
2209 render_text.SetMultiline(true);
2210 Canvas canvas;
2212 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2213 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2214 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2215 render_text.Draw(&canvas);
2216 EXPECT_EQ(kTestStrings[i].lines_count, render_text.lines_.size());
2217 if (kTestStrings[i].lines_count != render_text.lines_.size())
2218 continue;
2220 for (size_t j = 0; j < kTestStrings[i].lines_count; ++j) {
2221 SCOPED_TRACE(base::StringPrintf("Line %" PRIuS "", j));
2222 // There might be multiple segments in one line. Merge all the segments
2223 // ranges in the same line.
2224 const size_t segment_size = render_text.lines()[j].segments.size();
2225 Range line_range;
2226 if (segment_size > 0)
2227 line_range = Range(
2228 render_text.lines()[j].segments[0].char_range.start(),
2229 render_text.lines()[j].segments[segment_size - 1].char_range.end());
2230 EXPECT_EQ(kTestStrings[i].line_char_ranges[j], line_range);
2235 // Make sure that multiline mode ignores elide behavior.
2236 TEST_F(RenderTextTest, Multiline_IgnoreElide) {
2237 const wchar_t kTestString[] =
2238 L"very very very long string xxxxxxxxxxxxxxxxxxxxxxxxxx";
2239 const wchar_t kEllipsis[] = L"\x2026";
2241 RenderTextHarfBuzz render_text;
2242 render_text.SetElideBehavior(ELIDE_TAIL);
2243 render_text.SetDisplayRect(Rect(20, 1000));
2244 render_text.SetText(base::WideToUTF16(kTestString));
2245 EXPECT_NE(base::string16::npos,
2246 render_text.GetDisplayText().find(base::WideToUTF16(kEllipsis)));
2248 render_text.SetMultiline(true);
2249 EXPECT_EQ(base::string16::npos,
2250 render_text.GetDisplayText().find(base::WideToUTF16(kEllipsis)));
2253 TEST_F(RenderTextTest, Multiline_NewlineCharacterReplacement) {
2254 const wchar_t* kTestStrings[] = {
2255 L"abc\ndef", L"a \n b ", L"ab\n", L"a\n\nb", L"\nab", L"\n",
2258 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2259 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2260 RenderTextHarfBuzz render_text;
2261 render_text.SetDisplayRect(Rect(200, 1000));
2262 render_text.SetText(WideToUTF16(kTestStrings[i]));
2264 base::string16 display_text = render_text.GetDisplayText();
2265 // If RenderText is not multiline, the newline characters are replaced
2266 // by symbols, therefore the character should be changed.
2267 EXPECT_NE(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText());
2269 // Setting multiline will fix this, the newline characters will be back
2270 // to the original text.
2271 render_text.SetMultiline(true);
2272 EXPECT_EQ(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText());
2276 // Ensure horizontal alignment works in multiline mode.
2277 TEST_F(RenderTextTest, Multiline_HorizontalAlignment) {
2278 const struct {
2279 const wchar_t* const text;
2280 const gfx::HorizontalAlignment alignment;
2281 } kTestStrings[] = {
2282 { L"abcdefghij\nhijkl", gfx::ALIGN_LEFT },
2283 { L"nhijkl\nabcdefghij", gfx::ALIGN_LEFT },
2284 // hebrew, 2nd line shorter
2285 { L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7\n\x5d0\x5d1\x5d2\x5d3",
2286 gfx::ALIGN_RIGHT },
2287 // hebrew, 2nd line longer
2288 { L"\x5d0\x5d1\x5d2\x5d3\n\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7",
2289 gfx::ALIGN_RIGHT },
2290 // arabic, 2nd line shorter
2291 { L"\x62a\x62b\x62c\x62d\x62e\x62f\x630\n\x660\x661\x662\x663\x664",
2292 gfx::ALIGN_RIGHT },
2293 // arabic, 2nd line longer
2294 { L"\x660\x661\x662\x663\x664\n\x62a\x62b\x62c\x62d\x62e\x62f\x630",
2295 gfx::ALIGN_RIGHT },
2297 const int kGlyphSize = 5;
2298 RenderTextHarfBuzz render_text;
2299 render_text.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
2300 render_text.set_glyph_width_for_test(kGlyphSize);
2301 render_text.SetDisplayRect(Rect(100, 1000));
2302 render_text.SetMultiline(true);
2304 Canvas canvas;
2305 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2306 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "] %ls", i,
2307 kTestStrings[i].text));
2308 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2309 render_text.Draw(&canvas);
2310 ASSERT_LE(2u, render_text.lines().size());
2311 if (kTestStrings[i].alignment == gfx::ALIGN_LEFT) {
2312 EXPECT_EQ(0, render_text.GetAlignmentOffset(0).x());
2313 EXPECT_EQ(0, render_text.GetAlignmentOffset(1).x());
2314 } else {
2315 std::vector<base::string16> lines = base::SplitString(
2316 base::WideToUTF16(kTestStrings[i].text),
2317 base::string16(1, '\n'), base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
2318 ASSERT_EQ(2u, lines.size());
2319 int difference = (lines[0].length() - lines[1].length()) * kGlyphSize;
2320 EXPECT_EQ(render_text.GetAlignmentOffset(0).x() + difference,
2321 render_text.GetAlignmentOffset(1).x());
2326 TEST_F(RenderTextTest, Multiline_WordWrapBehavior) {
2327 const int kGlyphSize = 5;
2328 const struct {
2329 const WordWrapBehavior behavior;
2330 const size_t num_lines;
2331 const Range char_ranges[4];
2332 } kTestScenarios[] = {
2333 { IGNORE_LONG_WORDS, 3u,
2334 { Range(0, 4), Range(4, 11), Range(11, 14), Range::InvalidRange() } },
2335 { TRUNCATE_LONG_WORDS, 3u,
2336 { Range(0, 4), Range(4, 8), Range(11, 14), Range::InvalidRange() } },
2337 { WRAP_LONG_WORDS, 4u,
2338 { Range(0, 4), Range(4, 8), Range(8, 11), Range(11, 14) } },
2339 // TODO(mukai): implement ELIDE_LONG_WORDS. It's not used right now.
2341 RenderTextHarfBuzz render_text;
2342 render_text.SetMultiline(true);
2343 render_text.SetText(ASCIIToUTF16("foo fooooo foo"));
2344 render_text.set_glyph_width_for_test(kGlyphSize);
2345 render_text.SetDisplayRect(Rect(0, 0, kGlyphSize * 4, 0));
2347 Canvas canvas;
2349 for (size_t i = 0; i < arraysize(kTestScenarios); ++i) {
2350 SCOPED_TRACE(base::StringPrintf(
2351 "kTestScenarios[%" PRIuS "] %d", i, kTestScenarios[i].behavior));
2352 render_text.SetWordWrapBehavior(kTestScenarios[i].behavior);
2353 render_text.Draw(&canvas);
2355 ASSERT_EQ(kTestScenarios[i].num_lines, render_text.lines().size());
2356 for (size_t j = 0; j < render_text.lines().size(); ++j) {
2357 SCOPED_TRACE(base::StringPrintf("%" PRIuS "-th line", j));
2358 EXPECT_EQ(kTestScenarios[i].char_ranges[j],
2359 render_text.lines()[j].segments[0].char_range);
2360 EXPECT_EQ(kTestScenarios[i].char_ranges[j].length() * kGlyphSize,
2361 render_text.lines()[j].size.width());
2366 TEST_F(RenderTextTest, Multiline_LineBreakerBehavior) {
2367 const int kGlyphSize = 5;
2368 const struct {
2369 const wchar_t* const text;
2370 const WordWrapBehavior behavior;
2371 const Range char_ranges[3];
2372 } kTestScenarios[] = {
2373 { L"a single run", IGNORE_LONG_WORDS,
2374 {Range(0, 2), Range(2, 9), Range(9, 12) } },
2375 // 3 words: "That's ", ""good". ", "aaa" and 7 runs: "That", "'", "s ",
2376 // """, "good", "". ", "aaa". They all mixed together.
2377 { L"That's \"good\". aaa", IGNORE_LONG_WORDS,
2378 {Range(0, 7), Range(7, 15), Range(15, 18) } },
2379 // Test "\"" should be put into a new line correctly.
2380 { L"a \"good\" one.", IGNORE_LONG_WORDS,
2381 {Range(0, 2), Range(2, 9), Range(9, 13) } },
2382 // Test for full-width space.
2383 { L"That's\x3000good.\x3000yyy", IGNORE_LONG_WORDS,
2384 {Range(0, 7), Range(7, 13), Range(13, 16) } },
2385 { L"a single run", TRUNCATE_LONG_WORDS,
2386 {Range(0, 2), Range(2, 6), Range(9, 12) } },
2387 { L"That's \"good\". aaa", TRUNCATE_LONG_WORDS,
2388 {Range(0, 4), Range(7, 11), Range(15, 18) } },
2389 { L"That's good. aaa", TRUNCATE_LONG_WORDS,
2390 {Range(0, 4), Range(7, 11), Range(13, 16) } },
2391 { L"a \"good\" one.", TRUNCATE_LONG_WORDS,
2392 {Range(0, 2), Range(2, 6), Range(9, 13) } },
2393 { L"asingleword", WRAP_LONG_WORDS,
2394 {Range(0, 4), Range(4, 8), Range(8, 11) } },
2395 { L"That's good", WRAP_LONG_WORDS,
2396 {Range(0, 4), Range(4, 7), Range(7, 11) } },
2397 { L"That's \"g\".", WRAP_LONG_WORDS,
2398 {Range(0, 4), Range(4, 7), Range(7, 11) } },
2401 RenderTextHarfBuzz render_text;
2402 render_text.SetMultiline(true);
2403 render_text.set_glyph_width_for_test(kGlyphSize);
2404 render_text.SetDisplayRect(Rect(0, 0, kGlyphSize * 4, 0));
2406 Canvas canvas;
2408 for (size_t i = 0; i < arraysize(kTestScenarios); ++i) {
2409 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2410 render_text.SetText(WideToUTF16(kTestScenarios[i].text));
2411 render_text.SetWordWrapBehavior(kTestScenarios[i].behavior);
2412 render_text.Draw(&canvas);
2414 ASSERT_EQ(3u, render_text.lines().size());
2415 for (size_t j = 0; j < render_text.lines().size(); ++j) {
2416 SCOPED_TRACE(base::StringPrintf("%" PRIuS "-th line", j));
2417 // Merge all the segments ranges in the same line.
2418 size_t segment_size = render_text.lines()[j].segments.size();
2419 Range line_range;
2420 if (segment_size > 0)
2421 line_range = Range(
2422 render_text.lines()[j].segments[0].char_range.start(),
2423 render_text.lines()[j].segments[segment_size - 1].char_range.end());
2424 EXPECT_EQ(kTestScenarios[i].char_ranges[j], line_range);
2425 EXPECT_EQ(kTestScenarios[i].char_ranges[j].length() * kGlyphSize,
2426 render_text.lines()[j].size.width());
2431 // Test that Surrogate pairs or combining character sequences do not get
2432 // separated by line breaking.
2433 TEST_F(RenderTextTest, Multiline_SurrogatePairsOrCombiningChars) {
2434 RenderTextHarfBuzz render_text;
2435 render_text.SetMultiline(true);
2436 render_text.SetWordWrapBehavior(WRAP_LONG_WORDS);
2438 // Below is 'MUSICAL SYMBOL G CLEF' (U+1D11E), which is represented in UTF-16
2439 // as two code units forming a surrogate pair: 0xD834 0xDD1E.
2440 const base::char16 kSurrogate[] = {0xD834, 0xDD1E, 0};
2441 const base::string16 text_surrogate(kSurrogate);
2442 const int kSurrogateWidth =
2443 GetStringWidth(kSurrogate, render_text.font_list());
2445 // Below is a Devanagari two-character combining sequence U+0921 U+093F. The
2446 // sequence forms a single display character and should not be separated.
2447 const base::char16 kCombiningChars[] = {0x921, 0x93F, 0};
2448 const base::string16 text_combining(kCombiningChars);
2449 const int kCombiningCharsWidth =
2450 GetStringWidth(kCombiningChars, render_text.font_list());
2452 const struct {
2453 const base::string16 text;
2454 const int display_width;
2455 const Range char_ranges[3];
2456 } kTestScenarios[] = {
2457 { text_surrogate + text_surrogate + text_surrogate,
2458 kSurrogateWidth / 2 * 3,
2459 { Range(0, 2), Range(2, 4), Range(4, 6) } },
2460 { text_surrogate + UTF8ToUTF16(" ") + kCombiningChars,
2461 std::min(kSurrogateWidth, kCombiningCharsWidth) / 2,
2462 { Range(0, 2), Range(2, 3), Range(3, 5) } },
2465 Canvas canvas;
2467 for (size_t i = 0; i < arraysize(kTestScenarios); ++i) {
2468 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2469 render_text.SetText(kTestScenarios[i].text);
2470 render_text.SetDisplayRect(Rect(0, 0, kTestScenarios[i].display_width, 0));
2471 render_text.Draw(&canvas);
2473 ASSERT_EQ(3u, render_text.lines().size());
2474 for (size_t j = 0; j < render_text.lines().size(); ++j) {
2475 SCOPED_TRACE(base::StringPrintf("%" PRIuS "-th line", j));
2476 // There is only one segment in each line.
2477 EXPECT_EQ(kTestScenarios[i].char_ranges[j],
2478 render_text.lines()[j].segments[0].char_range);
2483 // Test that Zero width characters have the correct line breaking behavior.
2484 TEST_F(RenderTextTest, Multiline_ZeroWidthChars) {
2485 RenderTextHarfBuzz render_text;
2486 render_text.SetMultiline(true);
2487 render_text.SetWordWrapBehavior(WRAP_LONG_WORDS);
2489 const base::char16 kZeroWidthSpace = {0x200B};
2490 const base::string16 text(UTF8ToUTF16("test") + kZeroWidthSpace +
2491 UTF8ToUTF16("\n") + kZeroWidthSpace +
2492 UTF8ToUTF16("test."));
2493 const int kTestWidth =
2494 GetStringWidth(UTF8ToUTF16("test"), render_text.font_list());
2495 const Range char_ranges[3] = {Range(0, 5), Range(6, 11), Range(11, 12)};
2497 Canvas canvas;
2498 render_text.SetText(text);
2499 render_text.SetDisplayRect(Rect(0, 0, kTestWidth, 0));
2500 render_text.Draw(&canvas);
2502 ASSERT_EQ(3u, render_text.lines().size());
2503 for (size_t j = 0; j < render_text.lines().size(); ++j) {
2504 SCOPED_TRACE(base::StringPrintf("%" PRIuS "-th line", j));
2505 int segment_size = render_text.lines()[j].segments.size();
2506 ASSERT_GT(segment_size, 0);
2507 Range line_range(
2508 render_text.lines()[j].segments[0].char_range.start(),
2509 render_text.lines()[j].segments[segment_size - 1].char_range.end());
2510 EXPECT_EQ(char_ranges[j], line_range);
2514 TEST_F(RenderTextTest, NewlineWithoutMultilineFlag) {
2515 const wchar_t* kTestStrings[] = {
2516 L"abc\ndef", L"a \n b ", L"ab\n", L"a\n\nb", L"\nab", L"\n",
2519 RenderTextHarfBuzz render_text;
2520 render_text.SetDisplayRect(Rect(200, 1000));
2521 Canvas canvas;
2523 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2524 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2525 render_text.SetText(WideToUTF16(kTestStrings[i]));
2526 render_text.Draw(&canvas);
2528 EXPECT_EQ(1U, render_text.lines_.size());
2532 // Make sure the horizontal positions of runs in a line (left-to-right for
2533 // LTR languages and right-to-left for RTL languages).
2534 TEST_F(RenderTextTest, HarfBuzz_HorizontalPositions) {
2535 const struct {
2536 const wchar_t* const text;
2537 const Range first_run_char_range;
2538 const Range second_run_char_range;
2539 bool is_rtl;
2540 } kTestStrings[] = {
2541 { L"abc\x3042\x3044\x3046\x3048\x304A", Range(0, 3), Range(3, 8), false },
2542 { L"\x062A\x0641\x0627\x062D"
2543 L"\x05EA\x05E4\x05D5\x05D6\x05D9\x05DA\x05DB\x05DD",
2544 Range(0, 4), Range(4, 12), true },
2547 RenderTextHarfBuzz render_text;
2548 Canvas canvas;
2549 TestSkiaTextRenderer renderer(&canvas);
2551 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2552 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2553 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2555 render_text.EnsureLayout();
2556 const internal::TextRunList* run_list = render_text.GetRunList();
2557 ASSERT_EQ(2U, run_list->runs().size());
2558 EXPECT_EQ(kTestStrings[i].first_run_char_range, run_list->runs()[0]->range);
2559 EXPECT_EQ(kTestStrings[i].second_run_char_range,
2560 run_list->runs()[1]->range);
2561 // If it's RTL, the visual order is reversed.
2562 if (kTestStrings[i].is_rtl) {
2563 EXPECT_EQ(1U, run_list->logical_to_visual(0));
2564 EXPECT_EQ(0U, run_list->logical_to_visual(1));
2565 } else {
2566 EXPECT_EQ(0U, run_list->logical_to_visual(0));
2567 EXPECT_EQ(1U, run_list->logical_to_visual(1));
2570 render_text.DrawVisualTextInternal(&renderer);
2572 std::vector<TestSkiaTextRenderer::TextLog> text_log;
2573 renderer.GetTextLogAndReset(&text_log);
2575 EXPECT_EQ(2U, text_log.size());
2577 // Verifies the DrawText happens in the visual order and left-to-right.
2578 // If the text is RTL, the logically first run should be drawn at last.
2579 EXPECT_EQ(run_list->runs()[run_list->logical_to_visual(0)]->glyph_count,
2580 text_log[0].glyph_count);
2581 EXPECT_EQ(run_list->runs()[run_list->logical_to_visual(1)]->glyph_count,
2582 text_log[1].glyph_count);
2583 EXPECT_LT(text_log[0].origin.x(), text_log[1].origin.x());
2587 // Test TextRunHarfBuzz's cluster finding logic.
2588 TEST_F(RenderTextTest, HarfBuzz_Clusters) {
2589 struct {
2590 uint32 glyph_to_char[4];
2591 Range chars[4];
2592 Range glyphs[4];
2593 bool is_rtl;
2594 } cases[] = {
2595 { // From string "A B C D" to glyphs "a b c d".
2596 { 0, 1, 2, 3 },
2597 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2598 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2599 false
2601 { // From string "A B C D" to glyphs "d c b a".
2602 { 3, 2, 1, 0 },
2603 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2604 { Range(3, 4), Range(2, 3), Range(1, 2), Range(0, 1) },
2605 true
2607 { // From string "A B C D" to glyphs "ab c c d".
2608 { 0, 2, 2, 3 },
2609 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2610 { Range(0, 1), Range(0, 1), Range(1, 3), Range(3, 4) },
2611 false
2613 { // From string "A B C D" to glyphs "d c c ba".
2614 { 3, 2, 2, 0 },
2615 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2616 { Range(3, 4), Range(3, 4), Range(1, 3), Range(0, 1) },
2617 true
2621 internal::TextRunHarfBuzz run;
2622 run.range = Range(0, 4);
2623 run.glyph_count = 4;
2624 run.glyph_to_char.resize(4);
2626 for (size_t i = 0; i < arraysize(cases); ++i) {
2627 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 4,
2628 run.glyph_to_char.begin());
2629 run.is_rtl = cases[i].is_rtl;
2631 for (size_t j = 0; j < 4; ++j) {
2632 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2633 Range chars;
2634 Range glyphs;
2635 run.GetClusterAt(j, &chars, &glyphs);
2636 EXPECT_EQ(cases[i].chars[j], chars);
2637 EXPECT_EQ(cases[i].glyphs[j], glyphs);
2638 EXPECT_EQ(cases[i].glyphs[j], run.CharRangeToGlyphRange(chars));
2643 // Ensure that graphemes with multiple code points do not get split.
2644 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemeCases) {
2645 const wchar_t* cases[] = {
2646 // "A" with a combining umlaut, followed by a "B".
2647 L"A\x0308" L"B",
2648 // Devanagari biconsonantal conjunct "ki", followed by an "a".
2649 L"\x0915\x093f\x0905",
2650 // Thai consonant and vowel pair "cho chan" + "sara am", followed by Thai
2651 // digit 0.
2652 L"\x0e08\x0e33\x0E50",
2655 RenderTextHarfBuzz render_text;
2657 for (size_t i = 0; i < arraysize(cases); ++i) {
2658 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS, i));
2660 base::string16 text = WideToUTF16(cases[i]);
2661 render_text.SetText(text);
2662 render_text.EnsureLayout();
2663 internal::TextRunList* run_list = render_text.GetRunList();
2664 ASSERT_EQ(1U, run_list->size());
2665 internal::TextRunHarfBuzz* run = run_list->runs()[0];
2667 base::i18n::BreakIterator* iter = render_text.grapheme_iterator_.get();
2668 auto first_grapheme_bounds = run->GetGraphemeBounds(iter, 0);
2669 EXPECT_EQ(first_grapheme_bounds, run->GetGraphemeBounds(iter, 1));
2670 auto second_grapheme_bounds = run->GetGraphemeBounds(iter, 2);
2671 EXPECT_EQ(first_grapheme_bounds.end(), second_grapheme_bounds.start());
2675 // Test the partition of a multi-grapheme cluster into grapheme ranges.
2676 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemePartition) {
2677 struct {
2678 uint32 glyph_to_char[2];
2679 Range bounds[4];
2680 bool is_rtl;
2681 } cases[] = {
2682 { // From string "A B C D" to glyphs "a bcd".
2683 { 0, 1 },
2684 { Range(0, 10), Range(10, 13), Range(13, 17), Range(17, 20) },
2685 false
2687 { // From string "A B C D" to glyphs "ab cd".
2688 { 0, 2 },
2689 { Range(0, 5), Range(5, 10), Range(10, 15), Range(15, 20) },
2690 false
2692 { // From string "A B C D" to glyphs "dcb a".
2693 { 1, 0 },
2694 { Range(10, 20), Range(7, 10), Range(3, 7), Range(0, 3) },
2695 true
2697 { // From string "A B C D" to glyphs "dc ba".
2698 { 2, 0 },
2699 { Range(15, 20), Range(10, 15), Range(5, 10), Range(0, 5) },
2700 true
2704 internal::TextRunHarfBuzz run;
2705 run.range = Range(0, 4);
2706 run.glyph_count = 2;
2707 run.glyph_to_char.resize(2);
2708 run.positions.reset(new SkPoint[4]);
2709 run.width = 20;
2711 const base::string16 kString = ASCIIToUTF16("abcd");
2712 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2713 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2714 ASSERT_TRUE(iter->Init());
2716 for (size_t i = 0; i < arraysize(cases); ++i) {
2717 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 2,
2718 run.glyph_to_char.begin());
2719 run.is_rtl = cases[i].is_rtl;
2720 for (int j = 0; j < 2; ++j)
2721 run.positions[j].set(j * 10, 0);
2723 for (size_t j = 0; j < 4; ++j) {
2724 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2725 EXPECT_EQ(cases[i].bounds[j],
2726 run.GetGraphemeBounds(iter.get(), j).Round());
2731 TEST_F(RenderTextTest, HarfBuzz_RunDirection) {
2732 RenderTextHarfBuzz render_text;
2733 const base::string16 mixed = WideToUTF16(
2734 L"\x05D0\x05D1"
2735 L"1234"
2736 L"\x05D2\x05D3"
2737 L"abc");
2738 render_text.SetText(mixed);
2740 // Get the run list for both display directions.
2741 render_text.SetDirectionalityMode(DIRECTIONALITY_FORCE_LTR);
2742 render_text.EnsureLayout();
2743 internal::TextRunList* run_list = render_text.GetRunList();
2744 ASSERT_EQ(4U, run_list->size());
2745 EXPECT_TRUE(run_list->runs()[0]->is_rtl);
2746 EXPECT_FALSE(run_list->runs()[1]->is_rtl);
2747 EXPECT_TRUE(run_list->runs()[2]->is_rtl);
2748 EXPECT_FALSE(run_list->runs()[3]->is_rtl);
2750 // The Latin letters should appear to the right of the other runs.
2751 EXPECT_EQ(2U, run_list->logical_to_visual(0));
2752 EXPECT_EQ(1U, run_list->logical_to_visual(1));
2753 EXPECT_EQ(0U, run_list->logical_to_visual(2));
2754 EXPECT_EQ(3U, run_list->logical_to_visual(3));
2756 render_text.SetDirectionalityMode(DIRECTIONALITY_FORCE_RTL);
2757 render_text.EnsureLayout();
2758 run_list = render_text.GetRunList();
2759 ASSERT_EQ(4U, run_list->size());
2760 EXPECT_TRUE(run_list->runs()[0]->is_rtl);
2761 EXPECT_FALSE(run_list->runs()[1]->is_rtl);
2762 EXPECT_TRUE(run_list->runs()[2]->is_rtl);
2763 EXPECT_FALSE(run_list->runs()[3]->is_rtl);
2765 // The Latin letters should appear to the left of the other runs.
2766 EXPECT_EQ(3U, run_list->logical_to_visual(0));
2767 EXPECT_EQ(2U, run_list->logical_to_visual(1));
2768 EXPECT_EQ(1U, run_list->logical_to_visual(2));
2769 EXPECT_EQ(0U, run_list->logical_to_visual(3));
2772 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks) {
2773 RenderTextHarfBuzz render_text;
2775 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2776 render_text.SetText(WideToUTF16(L"x\x25B6y"));
2777 render_text.EnsureLayout();
2778 internal::TextRunList* run_list = render_text.GetRunList();
2779 ASSERT_EQ(3U, run_list->size());
2780 EXPECT_EQ(Range(0, 1), run_list->runs()[0]->range);
2781 EXPECT_EQ(Range(1, 2), run_list->runs()[1]->range);
2782 EXPECT_EQ(Range(2, 3), run_list->runs()[2]->range);
2784 render_text.SetText(WideToUTF16(L"x \x25B6 y"));
2785 render_text.EnsureLayout();
2786 run_list = render_text.GetRunList();
2787 ASSERT_EQ(4U, run_list->size());
2788 EXPECT_EQ(Range(0, 2), run_list->runs()[0]->range);
2789 EXPECT_EQ(Range(2, 3), run_list->runs()[1]->range);
2790 EXPECT_EQ(Range(3, 4), run_list->runs()[2]->range);
2791 EXPECT_EQ(Range(4, 5), run_list->runs()[3]->range);
2794 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByEmoji) {
2795 RenderTextHarfBuzz render_text;
2797 // \xF0\x9F\x98\x81 (U+1F601) is smile icon emoji. \xE2\x9C\xA8 (U+2728) is
2798 // a sparkle icon. Both can be drawn with color emoji fonts, so runs should be
2799 // separated. See crbug.com/448909
2800 render_text.SetText(UTF8ToUTF16("x\xF0\x9F\x98\x81y\xE2\x9C\xA8"));
2801 render_text.EnsureLayout();
2802 internal::TextRunList* run_list = render_text.GetRunList();
2803 ASSERT_EQ(4U, run_list->size());
2804 EXPECT_EQ(Range(0, 1), run_list->runs()[0]->range);
2805 // The length is 2 since U+1F601 is represented as a surrogate pair in UTF16.
2806 EXPECT_EQ(Range(1, 3), run_list->runs()[1]->range);
2807 EXPECT_EQ(Range(3, 4), run_list->runs()[2]->range);
2808 EXPECT_EQ(Range(4, 5), run_list->runs()[3]->range);
2811 TEST_F(RenderTextTest, GlyphBounds) {
2812 const wchar_t* kTestStrings[] = {
2813 L"asdf 1234 qwer", L"\x0647\x0654", L"\x0645\x0631\x062D\x0628\x0627"
2815 scoped_ptr<RenderText> render_text(new RenderTextHarfBuzz);
2817 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2818 render_text->SetText(WideToUTF16(kTestStrings[i]));
2819 render_text->EnsureLayout();
2821 for (size_t j = 0; j < render_text->text().length(); ++j)
2822 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2826 // Ensure that shaping with a non-existent font does not cause a crash.
2827 TEST_F(RenderTextTest, HarfBuzz_NonExistentFont) {
2828 RenderTextHarfBuzz render_text;
2829 render_text.SetText(ASCIIToUTF16("test"));
2830 render_text.EnsureLayout();
2831 internal::TextRunList* run_list = render_text.GetRunList();
2832 ASSERT_EQ(1U, run_list->size());
2833 internal::TextRunHarfBuzz* run = run_list->runs()[0];
2834 render_text.ShapeRunWithFont(
2835 render_text.text(), "TheFontThatDoesntExist", FontRenderParams(), run);
2838 // Ensure an empty run returns sane values to queries.
2839 TEST_F(RenderTextTest, HarfBuzz_EmptyRun) {
2840 internal::TextRunHarfBuzz run;
2841 const base::string16 kString = ASCIIToUTF16("abcdefgh");
2842 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2843 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2844 ASSERT_TRUE(iter->Init());
2846 run.range = Range(3, 8);
2847 run.glyph_count = 0;
2848 EXPECT_EQ(Range(0, 0), run.CharRangeToGlyphRange(Range(4, 5)));
2849 EXPECT_EQ(Range(0, 0), run.GetGraphemeBounds(iter.get(), 4).Round());
2850 Range chars;
2851 Range glyphs;
2852 run.GetClusterAt(4, &chars, &glyphs);
2853 EXPECT_EQ(Range(3, 8), chars);
2854 EXPECT_EQ(Range(0, 0), glyphs);
2857 // Ensure the line breaker doesn't compute the word's width bigger than the
2858 // actual size. See http://crbug.com/470073
2859 TEST_F(RenderTextTest, HarfBuzz_WordWidthWithDiacritics) {
2860 RenderTextHarfBuzz render_text;
2861 const base::string16 kWord = WideToUTF16(L"\u0906\u092A\u0915\u0947 ");
2862 render_text.SetText(kWord);
2863 const gfx::SizeF text_size = render_text.GetStringSizeF();
2865 render_text.SetText(kWord + kWord);
2866 render_text.SetMultiline(true);
2867 EXPECT_EQ(text_size.width() * 2, render_text.GetStringSizeF().width());
2868 EXPECT_EQ(text_size.height(), render_text.GetStringSizeF().height());
2869 render_text.SetDisplayRect(gfx::Rect(0, 0, std::ceil(text_size.width()), 0));
2870 EXPECT_NEAR(text_size.width(), render_text.GetStringSizeF().width(), 1.0f);
2871 EXPECT_EQ(text_size.height() * 2, render_text.GetStringSizeF().height());
2874 // Ensure a string fits in a display rect with a width equal to the string's.
2875 TEST_F(RenderTextTest, StringFitsOwnWidth) {
2876 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2877 const base::string16 kString = ASCIIToUTF16("www.example.com");
2879 render_text->SetText(kString);
2880 render_text->ApplyStyle(BOLD, true, Range(0, 3));
2881 render_text->SetElideBehavior(ELIDE_TAIL);
2883 render_text->SetDisplayRect(Rect(0, 0, 500, 100));
2884 EXPECT_EQ(kString, render_text->GetDisplayText());
2885 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100));
2886 EXPECT_EQ(kString, render_text->GetDisplayText());
2889 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184
2890 #if !defined(OS_WIN)
2891 // Ensure that RenderText examines all of the fonts in its FontList before
2892 // falling back to other fonts.
2893 TEST_F(RenderTextTest, HarfBuzz_FontListFallback) {
2894 // Double-check that the requested fonts are present.
2895 FontList font_list("Arial, Symbol, 12px");
2896 const std::vector<Font>& fonts = font_list.GetFonts();
2897 ASSERT_EQ(2u, fonts.size());
2898 ASSERT_EQ("arial",
2899 base::ToLowerASCII(fonts[0].GetActualFontNameForTesting()));
2900 ASSERT_EQ("symbol",
2901 base::ToLowerASCII(fonts[1].GetActualFontNameForTesting()));
2903 // "⊕" (CIRCLED PLUS) should be rendered with Symbol rather than falling back
2904 // to some other font that's present on the system.
2905 RenderTextHarfBuzz render_text;
2906 render_text.SetFontList(font_list);
2907 render_text.SetText(UTF8ToUTF16("\xE2\x8A\x95"));
2908 const std::vector<RenderText::FontSpan> spans =
2909 render_text.GetFontSpansForTesting();
2910 ASSERT_EQ(static_cast<size_t>(1), spans.size());
2911 EXPECT_EQ("Symbol", spans[0].first.GetFontName());
2913 #endif // !defined(OS_WIN)
2915 // Ensure that the fallback fonts of the Uniscribe font are tried for shaping.
2916 #if defined(OS_WIN)
2917 TEST_F(RenderTextTest, HarfBuzz_UniscribeFallback) {
2918 RenderTextHarfBuzz render_text;
2919 PlatformFontWin* font_win = new PlatformFontWin("Meiryo", 12);
2920 // Japanese name for Meiryo. This name won't be found in the system's linked
2921 // fonts, forcing RTHB to try the Uniscribe font and its fallbacks.
2922 font_win->font_ref_->font_name_ = WideToUTF8(L"\x30e1\x30a4\x30ea\x30aa");
2923 FontList font_list((Font(font_win)));
2925 render_text.SetFontList(font_list);
2926 // Korean character "han".
2927 render_text.SetText(WideToUTF16(L"\xd55c"));
2928 render_text.EnsureLayout();
2929 internal::TextRunList* run_list = render_text.GetRunList();
2930 ASSERT_EQ(1U, run_list->size());
2931 EXPECT_EQ(0U, run_list->runs()[0]->CountMissingGlyphs());
2933 #endif // defined(OS_WIN)
2935 // Ensure that the fallback fonts offered by gfx::GetFallbackFontFamilies() are
2936 // tried. Note this test assumes the font "Arial" doesn't provide a unicode
2937 // glyph for a particular character, and that there exists a system fallback
2938 // font which does.
2939 // TODO(msw): Fallback doesn't find a glyph on Linux.
2940 #if !defined(OS_LINUX)
2941 TEST_F(RenderTextTest, HarfBuzz_UnicodeFallback) {
2942 RenderTextHarfBuzz render_text;
2943 render_text.SetFontList(FontList("Arial, 12px"));
2945 // Korean character "han".
2946 render_text.SetText(WideToUTF16(L"\xd55c"));
2947 render_text.EnsureLayout();
2948 internal::TextRunList* run_list = render_text.GetRunList();
2949 ASSERT_EQ(1U, run_list->size());
2950 EXPECT_EQ(0U, run_list->runs()[0]->CountMissingGlyphs());
2952 #endif // !defined(OS_LINUX)
2954 // Ensure that the width reported by RenderText is sufficient for drawing. Draws
2955 // to a canvas and checks if any pixel beyond the bounding rectangle is colored.
2956 TEST_F(RenderTextTest, TextDoesntClip) {
2957 const wchar_t* kTestStrings[] = {
2958 L" ",
2959 // TODO(dschuyler): Underscores draw outside GetStringSize;
2960 // crbug.com/459812. This appears to be a preexisting issue that wasn't
2961 // revealed by the prior unit tests.
2962 // L"TEST_______",
2963 L"TEST some stuff",
2964 L"WWWWWWWWWW",
2965 L"gAXAXAXAXAXAXA",
2966 // TODO(dschuyler): A-Ring draws outside GetStringSize; crbug.com/459812.
2967 // L"g\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5",
2968 L"\x0647\x0654\x0647\x0654\x0647\x0654\x0647\x0654\x0645\x0631\x062D"
2969 L"\x0628\x0627"};
2970 const Size kCanvasSize(300, 50);
2971 const int kTestSize = 10;
2973 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
2974 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2975 Canvas canvas(surface->getCanvas(), 1.0f);
2976 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2977 render_text->SetHorizontalAlignment(ALIGN_LEFT);
2978 render_text->SetColor(SK_ColorBLACK);
2980 for (auto string : kTestStrings) {
2981 surface->getCanvas()->clear(SK_ColorWHITE);
2982 render_text->SetText(WideToUTF16(string));
2983 const Size string_size = render_text->GetStringSize();
2984 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2));
2985 render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4));
2986 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6));
2987 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8));
2988 render_text->SetStyle(BOLD, true);
2989 render_text->SetDisplayRect(
2990 Rect(kTestSize, kTestSize, string_size.width(), string_size.height()));
2991 // Allow the RenderText to paint outside of its display rect.
2992 render_text->set_clip_to_display_rect(false);
2993 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width());
2995 render_text->Draw(&canvas);
2996 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width());
2997 const uint32* buffer =
2998 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
2999 ASSERT_NE(nullptr, buffer);
3000 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
3001 kCanvasSize.height());
3003 #if !defined(OS_CHROMEOS)
3004 // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect.
3005 SCOPED_TRACE("TextDoesntClip Top Side");
3006 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(),
3007 kTestSize);
3008 #endif
3011 SCOPED_TRACE("TextDoesntClip Bottom Side");
3012 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0,
3013 kTestSize + string_size.height(),
3014 kCanvasSize.width(), kTestSize);
3017 SCOPED_TRACE("TextDoesntClip Left Side");
3018 #if defined(OS_WIN)
3019 // TODO(dschuyler): On Windows XP the Unicode test draws to the left edge
3020 // as if it is ignoring the SetDisplayRect shift by kTestSize. This
3021 // appears to be a preexisting issue that wasn't revealed by the prior
3022 // unit tests.
3023 #elif defined(OS_MACOSX) || defined(OS_CHROMEOS)
3024 // TODO(dschuyler): On Windows (non-XP), Chrome OS and Mac smoothing draws
3025 // left of text. This appears to be a preexisting issue that wasn't
3026 // revealed by the prior unit tests.
3027 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize - 1,
3028 string_size.height());
3029 #else
3030 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize,
3031 string_size.height());
3032 #endif
3035 SCOPED_TRACE("TextDoesntClip Right Side");
3036 #if !defined(OS_MACOSX)
3037 // TODO(dschuyler): On Mac text draws to right of GetStringSize. This
3038 // appears to be a preexisting issue that wasn't revealed by the prior
3039 // unit tests.
3040 rect_buffer.EnsureSolidRect(SK_ColorWHITE,
3041 kTestSize + string_size.width(), kTestSize,
3042 kTestSize, string_size.height());
3043 #endif
3048 // Ensure that the text will clip to the display rect. Draws to a canvas and
3049 // checks whether any pixel beyond the bounding rectangle is colored.
3050 TEST_F(RenderTextTest, TextDoesClip) {
3051 const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"};
3052 const Size kCanvasSize(300, 50);
3053 const int kTestSize = 10;
3055 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
3056 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
3057 Canvas canvas(surface->getCanvas(), 1.0f);
3058 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
3059 render_text->SetHorizontalAlignment(ALIGN_LEFT);
3060 render_text->SetColor(SK_ColorBLACK);
3062 for (auto string : kTestStrings) {
3063 surface->getCanvas()->clear(SK_ColorWHITE);
3064 render_text->SetText(WideToUTF16(string));
3065 const Size string_size = render_text->GetStringSize();
3066 int fake_width = string_size.width() / 2;
3067 int fake_height = string_size.height() / 2;
3068 render_text->SetDisplayRect(
3069 Rect(kTestSize, kTestSize, fake_width, fake_height));
3070 render_text->set_clip_to_display_rect(true);
3071 render_text->Draw(&canvas);
3072 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width());
3073 const uint32* buffer =
3074 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
3075 ASSERT_NE(nullptr, buffer);
3076 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
3077 kCanvasSize.height());
3079 SCOPED_TRACE("TextDoesClip Top Side");
3080 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(),
3081 kTestSize);
3085 SCOPED_TRACE("TextDoesClip Bottom Side");
3086 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize + fake_height,
3087 kCanvasSize.width(), kTestSize);
3090 SCOPED_TRACE("TextDoesClip Left Side");
3091 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize,
3092 fake_height);
3095 SCOPED_TRACE("TextDoesClip Right Side");
3096 rect_buffer.EnsureSolidRect(SK_ColorWHITE, kTestSize + fake_width,
3097 kTestSize, kTestSize, fake_height);
3102 #if defined(OS_MACOSX)
3103 TEST_F(RenderTextTest, Mac_ElidedText) {
3104 RenderTextMac render_text;
3105 base::string16 text(ASCIIToUTF16("This is an example."));
3106 render_text.SetText(text);
3107 gfx::Size string_size = render_text.GetStringSize();
3108 render_text.SetDisplayRect(gfx::Rect(string_size));
3109 render_text.EnsureLayout();
3110 // NOTE: Character and glyph counts are only comparable for simple text.
3111 EXPECT_EQ(text.size(),
3112 static_cast<size_t>(CTLineGetGlyphCount(render_text.line_)));
3114 render_text.SetElideBehavior(ELIDE_TAIL);
3115 string_size.set_width(string_size.width() / 2);
3116 render_text.SetDisplayRect(gfx::Rect(string_size));
3117 render_text.EnsureLayout();
3118 CFIndex glyph_count = CTLineGetGlyphCount(render_text.line_);
3119 EXPECT_GT(text.size(), static_cast<size_t>(glyph_count));
3120 EXPECT_NE(0, glyph_count);
3122 #endif
3124 } // namespace gfx