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"
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"
27 #include "base/win/windows_version.h"
28 #include "ui/gfx/platform_font_win.h"
31 #if defined(OS_MACOSX)
32 #include <ApplicationServices/ApplicationServices.h>
34 #include "ui/gfx/render_text_mac.h"
37 using base::ASCIIToUTF16
;
38 using base::UTF8ToUTF16
;
39 using base::WideToUTF16
;
40 using base::WideToUTF8
;
46 // Various weak, LTR, RTL, and Bidi string cases with three characters each.
47 const wchar_t kWeak
[] = L
" . ";
48 const wchar_t kLtr
[] = L
"abc";
49 const wchar_t kRtl
[] = L
"\x5d0\x5d1\x5d2";
50 const wchar_t kLtrRtl
[] = L
"a" L
"\x5d0\x5d1";
51 const wchar_t kLtrRtlLtr
[] = L
"a" L
"\x5d1" L
"b";
52 const wchar_t kRtlLtr
[] = L
"\x5d0\x5d1" L
"a";
53 const wchar_t kRtlLtrRtl
[] = L
"\x5d0" L
"a" L
"\x5d1";
55 // Checks whether |range| contains |index|. This is not the same as calling
56 // range.Contains(Range(index)), which returns true if |index| == |range.end()|.
57 bool IndexInRange(const Range
& range
, size_t index
) {
58 return index
>= range
.start() && index
< range
.end();
61 base::string16
GetSelectedText(RenderText
* render_text
) {
62 return render_text
->text().substr(render_text
->selection().GetMin(),
63 render_text
->selection().length());
66 // A test utility function to set the application default text direction.
67 void SetRTL(bool rtl
) {
68 // Override the current locale/direction.
69 base::i18n::SetICUDefaultLocale(rtl
? "he" : "en");
70 EXPECT_EQ(rtl
, base::i18n::IsRTL());
73 #if !defined(OS_MACOSX)
74 // Ensure cursor movement in the specified |direction| yields |expected| values.
75 void RunMoveCursorLeftRightTest(RenderText
* render_text
,
76 const std::vector
<SelectionModel
>& expected
,
77 VisualCursorDirection direction
) {
78 for (size_t i
= 0; i
< expected
.size(); ++i
) {
79 SCOPED_TRACE(base::StringPrintf("Going %s; expected value index %d.",
80 direction
== CURSOR_LEFT
? "left" : "right", static_cast<int>(i
)));
81 EXPECT_EQ(expected
[i
], render_text
->selection_model());
82 render_text
->MoveCursor(CHARACTER_BREAK
, direction
, false);
84 // Check that cursoring is clamped at the line edge.
85 EXPECT_EQ(expected
.back(), render_text
->selection_model());
86 // Check that it is the line edge.
87 render_text
->MoveCursor(LINE_BREAK
, direction
, false);
88 EXPECT_EQ(expected
.back(), render_text
->selection_model());
90 #endif // !defined(OS_MACOSX)
92 // Test utility for Multiline_Newline test case. Empty |expected_range| means
93 // the blank line which has no segments. Otherwise |segments| should contain
94 // exactly one line segment whose range equals to |expected_range|.
95 void VerifyLineSegments(const Range
& expected_range
,
96 const std::vector
<internal::LineSegment
>& segments
) {
97 EXPECT_EQ(expected_range
.is_empty() ? 0ul : 1ul, segments
.size());
98 if (!expected_range
.is_empty())
99 EXPECT_EQ(expected_range
, segments
[0].char_range
);
102 // The class which records the drawing operations so that the test case can
103 // verify where exactly the glyphs are drawn.
104 class TestSkiaTextRenderer
: public internal::SkiaTextRenderer
{
107 TextLog() : glyph_count(0u) {}
112 struct DecorationLog
{
113 DecorationLog(int x
, int y
, int width
, bool underline
, bool strike
,
114 bool diagonal_strike
)
115 : x(x
), y(y
), width(width
), underline(underline
), strike(strike
),
116 diagonal_strike(diagonal_strike
) {}
122 bool diagonal_strike
;
125 explicit TestSkiaTextRenderer(Canvas
* canvas
)
126 : internal::SkiaTextRenderer(canvas
) {}
127 ~TestSkiaTextRenderer() override
{}
129 void GetTextLogAndReset(std::vector
<TextLog
>* text_log
) {
130 text_log_
.swap(*text_log
);
134 void GetDecorationLogAndReset(std::vector
<DecorationLog
>* decoration_log
) {
135 decoration_log_
.swap(*decoration_log
);
136 decoration_log_
.clear();
140 // internal::SkiaTextRenderer:
141 void DrawPosText(const SkPoint
* pos
,
142 const uint16
* glyphs
,
143 size_t glyph_count
) override
{
145 log_entry
.glyph_count
= glyph_count
;
146 if (glyph_count
> 0) {
148 PointF(SkScalarToFloat(pos
[0].x()), SkScalarToFloat(pos
[0].y()));
149 for (size_t i
= 1U; i
< glyph_count
; ++i
) {
150 log_entry
.origin
.SetToMin(
151 PointF(SkScalarToFloat(pos
[i
].x()), SkScalarToFloat(pos
[i
].y())));
154 text_log_
.push_back(log_entry
);
155 internal::SkiaTextRenderer::DrawPosText(pos
, glyphs
, glyph_count
);
158 void DrawDecorations(int x
, int y
, int width
, bool underline
, bool strike
,
159 bool diagonal_strike
) override
{
160 decoration_log_
.push_back(
161 DecorationLog(x
, y
, width
, underline
, strike
, diagonal_strike
));
162 internal::SkiaTextRenderer::DrawDecorations(
163 x
, y
, width
, underline
, strike
, diagonal_strike
);
166 std::vector
<TextLog
> text_log_
;
167 std::vector
<DecorationLog
> decoration_log_
;
169 DISALLOW_COPY_AND_ASSIGN(TestSkiaTextRenderer
);
172 // Given a buffer to test against, this can be used to test various areas of the
173 // rectangular buffer against a specific color value.
174 class TestRectangleBuffer
{
176 TestRectangleBuffer(const wchar_t* string
,
177 const SkColor
* buffer
,
183 row_count_(row_count
) {}
185 // Test if any values in the rectangular area are anything other than |color|.
186 void EnsureSolidRect(SkColor color
,
191 ASSERT_LT(top
, row_count_
) << string_
;
192 ASSERT_LE(top
+ height
, row_count_
) << string_
;
193 ASSERT_LT(left
, stride_
) << string_
;
194 ASSERT_LE(left
+ width
, stride_
) << string_
<< ", left " << left
195 << ", width " << width
<< ", stride_ "
197 for (int y
= top
; y
< top
+ height
; ++y
) {
198 for (int x
= left
; x
< left
+ width
; ++x
) {
199 SkColor buffer_color
= buffer_
[x
+ y
* stride_
];
200 EXPECT_EQ(color
, buffer_color
) << string_
<< " at " << x
<< ", " << y
;
206 const wchar_t* string_
;
207 const SkColor
* buffer_
;
211 DISALLOW_COPY_AND_ASSIGN(TestRectangleBuffer
);
216 class RenderTextTest
: public testing::Test
{
219 TEST_F(RenderTextTest
, DefaultStyles
) {
220 // Check the default styles applied to new instances and adjusted text.
221 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
222 EXPECT_TRUE(render_text
->text().empty());
223 const wchar_t* const cases
[] = { kWeak
, kLtr
, L
"Hello", kRtl
, L
"", L
"" };
224 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
225 EXPECT_TRUE(render_text
->colors().EqualsValueForTesting(SK_ColorBLACK
));
227 render_text
->baselines().EqualsValueForTesting(NORMAL_BASELINE
));
228 for (size_t style
= 0; style
< NUM_TEXT_STYLES
; ++style
)
229 EXPECT_TRUE(render_text
->styles()[style
].EqualsValueForTesting(false));
230 render_text
->SetText(WideToUTF16(cases
[i
]));
234 TEST_F(RenderTextTest
, SetStyles
) {
235 // Ensure custom default styles persist across setting and clearing text.
236 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
237 const SkColor color
= SK_ColorRED
;
238 render_text
->SetColor(color
);
239 render_text
->SetBaselineStyle(SUPERSCRIPT
);
240 render_text
->SetStyle(BOLD
, true);
241 render_text
->SetStyle(UNDERLINE
, false);
242 const wchar_t* const cases
[] = { kWeak
, kLtr
, L
"Hello", kRtl
, L
"", L
"" };
243 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
244 EXPECT_TRUE(render_text
->colors().EqualsValueForTesting(color
));
245 EXPECT_TRUE(render_text
->baselines().EqualsValueForTesting(SUPERSCRIPT
));
246 EXPECT_TRUE(render_text
->styles()[BOLD
].EqualsValueForTesting(true));
247 EXPECT_TRUE(render_text
->styles()[UNDERLINE
].EqualsValueForTesting(false));
248 render_text
->SetText(WideToUTF16(cases
[i
]));
250 // Ensure custom default styles can be applied after text has been set.
252 render_text
->SetStyle(STRIKE
, true);
254 EXPECT_TRUE(render_text
->styles()[STRIKE
].EqualsValueForTesting(true));
258 TEST_F(RenderTextTest
, ApplyStyles
) {
259 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
260 render_text
->SetText(ASCIIToUTF16("012345678"));
262 // Apply a ranged color and style and check the resulting breaks.
263 render_text
->ApplyColor(SK_ColorRED
, Range(1, 4));
264 render_text
->ApplyBaselineStyle(SUPERIOR
, Range(2, 4));
265 render_text
->ApplyStyle(BOLD
, true, Range(2, 5));
266 std::vector
<std::pair
<size_t, SkColor
> > expected_color
;
267 expected_color
.push_back(std::pair
<size_t, SkColor
>(0, SK_ColorBLACK
));
268 expected_color
.push_back(std::pair
<size_t, SkColor
>(1, SK_ColorRED
));
269 expected_color
.push_back(std::pair
<size_t, SkColor
>(4, SK_ColorBLACK
));
270 EXPECT_TRUE(render_text
->colors().EqualsForTesting(expected_color
));
271 std::vector
<std::pair
<size_t, BaselineStyle
>> expected_baseline_style
;
272 expected_baseline_style
.push_back(
273 std::pair
<size_t, BaselineStyle
>(0, NORMAL_BASELINE
));
274 expected_baseline_style
.push_back(
275 std::pair
<size_t, BaselineStyle
>(2, SUPERIOR
));
276 expected_baseline_style
.push_back(
277 std::pair
<size_t, BaselineStyle
>(4, NORMAL_BASELINE
));
279 render_text
->baselines().EqualsForTesting(expected_baseline_style
));
280 std::vector
<std::pair
<size_t, bool> > expected_style
;
281 expected_style
.push_back(std::pair
<size_t, bool>(0, false));
282 expected_style
.push_back(std::pair
<size_t, bool>(2, true));
283 expected_style
.push_back(std::pair
<size_t, bool>(5, false));
284 EXPECT_TRUE(render_text
->styles()[BOLD
].EqualsForTesting(expected_style
));
286 // Ensure that setting a value overrides the ranged values.
287 render_text
->SetColor(SK_ColorBLUE
);
288 EXPECT_TRUE(render_text
->colors().EqualsValueForTesting(SK_ColorBLUE
));
289 render_text
->SetBaselineStyle(SUBSCRIPT
);
290 EXPECT_TRUE(render_text
->baselines().EqualsValueForTesting(SUBSCRIPT
));
291 render_text
->SetStyle(BOLD
, false);
292 EXPECT_TRUE(render_text
->styles()[BOLD
].EqualsValueForTesting(false));
294 // Apply a value over the text end and check the resulting breaks (INT_MAX
295 // should be used instead of the text length for the range end)
296 const size_t text_length
= render_text
->text().length();
297 render_text
->ApplyColor(SK_ColorRED
, Range(0, text_length
));
298 render_text
->ApplyBaselineStyle(SUPERIOR
, Range(0, text_length
));
299 render_text
->ApplyStyle(BOLD
, true, Range(2, text_length
));
300 std::vector
<std::pair
<size_t, SkColor
> > expected_color_end
;
301 expected_color_end
.push_back(std::pair
<size_t, SkColor
>(0, SK_ColorRED
));
302 EXPECT_TRUE(render_text
->colors().EqualsForTesting(expected_color_end
));
303 std::vector
<std::pair
<size_t, BaselineStyle
>> expected_baseline_end
;
304 expected_baseline_end
.push_back(
305 std::pair
<size_t, BaselineStyle
>(0, SUPERIOR
));
306 EXPECT_TRUE(render_text
->baselines().EqualsForTesting(expected_baseline_end
));
307 std::vector
<std::pair
<size_t, bool> > expected_style_end
;
308 expected_style_end
.push_back(std::pair
<size_t, bool>(0, false));
309 expected_style_end
.push_back(std::pair
<size_t, bool>(2, true));
310 EXPECT_TRUE(render_text
->styles()[BOLD
].EqualsForTesting(expected_style_end
));
312 // Ensure ranged values adjust to accommodate text length changes.
313 render_text
->ApplyStyle(ITALIC
, true, Range(0, 2));
314 render_text
->ApplyStyle(ITALIC
, true, Range(3, 6));
315 render_text
->ApplyStyle(ITALIC
, true, Range(7, text_length
));
316 std::vector
<std::pair
<size_t, bool> > expected_italic
;
317 expected_italic
.push_back(std::pair
<size_t, bool>(0, true));
318 expected_italic
.push_back(std::pair
<size_t, bool>(2, false));
319 expected_italic
.push_back(std::pair
<size_t, bool>(3, true));
320 expected_italic
.push_back(std::pair
<size_t, bool>(6, false));
321 expected_italic
.push_back(std::pair
<size_t, bool>(7, true));
322 EXPECT_TRUE(render_text
->styles()[ITALIC
].EqualsForTesting(expected_italic
));
324 // Changing the text should clear any breaks except for the first one.
325 render_text
->SetText(ASCIIToUTF16("0123456"));
326 expected_italic
.resize(1);
327 EXPECT_TRUE(render_text
->styles()[ITALIC
].EqualsForTesting(expected_italic
));
328 render_text
->ApplyStyle(ITALIC
, false, Range(2, 4));
329 render_text
->SetText(ASCIIToUTF16("012345678"));
330 EXPECT_TRUE(render_text
->styles()[ITALIC
].EqualsForTesting(expected_italic
));
331 render_text
->ApplyStyle(ITALIC
, false, Range(0, 1));
332 render_text
->SetText(ASCIIToUTF16("0123456"));
333 expected_italic
.begin()->second
= false;
334 EXPECT_TRUE(render_text
->styles()[ITALIC
].EqualsForTesting(expected_italic
));
335 render_text
->ApplyStyle(ITALIC
, true, Range(2, 4));
336 render_text
->SetText(ASCIIToUTF16("012345678"));
337 EXPECT_TRUE(render_text
->styles()[ITALIC
].EqualsForTesting(expected_italic
));
339 // TODO(tmoniuszko): Enable when RenderTextMac::IsValidCursorIndex is
341 #if !defined(OS_MACOSX)
342 // Styles shouldn't be changed mid-grapheme.
343 render_text
->SetText(WideToUTF16(
344 L
"0" L
"\x0915\x093f" L
"1" L
"\x0915\x093f" L
"2"));
345 render_text
->ApplyStyle(UNDERLINE
, true, Range(2, 5));
346 std::vector
<std::pair
<size_t, bool> > expected_underline
;
347 expected_underline
.push_back(std::pair
<size_t, bool>(0, false));
348 expected_underline
.push_back(std::pair
<size_t, bool>(1, true));
349 expected_underline
.push_back(std::pair
<size_t, bool>(6, false));
350 EXPECT_TRUE(render_text
->styles()[UNDERLINE
].EqualsForTesting(
351 expected_underline
));
355 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
356 // does not implement this yet. http://crbug.com/131618
357 #if !defined(OS_MACOSX)
358 void TestVisualCursorMotionInObscuredField(RenderText
* render_text
,
359 const base::string16
& text
,
361 ASSERT_TRUE(render_text
->obscured());
362 render_text
->SetText(text
);
363 int len
= text
.length();
364 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, select
);
365 EXPECT_EQ(SelectionModel(Range(select
? 0 : len
, len
), CURSOR_FORWARD
),
366 render_text
->selection_model());
367 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, select
);
368 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
369 for (int j
= 1; j
<= len
; ++j
) {
370 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, select
);
371 EXPECT_EQ(SelectionModel(Range(select
? 0 : j
, j
), CURSOR_BACKWARD
),
372 render_text
->selection_model());
374 for (int j
= len
- 1; j
>= 0; --j
) {
375 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, select
);
376 EXPECT_EQ(SelectionModel(Range(select
? 0 : j
, j
), CURSOR_FORWARD
),
377 render_text
->selection_model());
379 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, select
);
380 EXPECT_EQ(SelectionModel(Range(select
? 0 : len
, len
), CURSOR_FORWARD
),
381 render_text
->selection_model());
382 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, select
);
383 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
386 TEST_F(RenderTextTest
, ObscuredText
) {
387 const base::string16 seuss
= ASCIIToUTF16("hop on pop");
388 const base::string16 no_seuss
= ASCIIToUTF16("**********");
389 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
391 // GetLayoutText() returns asterisks when the obscured bit is set.
392 render_text
->SetText(seuss
);
393 render_text
->SetObscured(true);
394 EXPECT_EQ(seuss
, render_text
->text());
395 EXPECT_EQ(no_seuss
, render_text
->GetDisplayText());
396 render_text
->SetObscured(false);
397 EXPECT_EQ(seuss
, render_text
->text());
398 EXPECT_EQ(seuss
, render_text
->GetDisplayText());
400 render_text
->SetObscured(true);
402 // Surrogate pairs are counted as one code point.
403 const base::char16 invalid_surrogates
[] = {0xDC00, 0xD800, 0};
404 render_text
->SetText(invalid_surrogates
);
405 EXPECT_EQ(ASCIIToUTF16("**"), render_text
->GetDisplayText());
406 const base::char16 valid_surrogates
[] = {0xD800, 0xDC00, 0};
407 render_text
->SetText(valid_surrogates
);
408 EXPECT_EQ(ASCIIToUTF16("*"), render_text
->GetDisplayText());
409 EXPECT_EQ(0U, render_text
->cursor_position());
410 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
411 EXPECT_EQ(2U, render_text
->cursor_position());
413 // Test index conversion and cursor validity with a valid surrogate pair.
414 EXPECT_EQ(0U, render_text
->TextIndexToDisplayIndex(0U));
415 EXPECT_EQ(1U, render_text
->TextIndexToDisplayIndex(1U));
416 EXPECT_EQ(1U, render_text
->TextIndexToDisplayIndex(2U));
417 EXPECT_EQ(0U, render_text
->DisplayIndexToTextIndex(0U));
418 EXPECT_EQ(2U, render_text
->DisplayIndexToTextIndex(1U));
419 EXPECT_TRUE(render_text
->IsValidCursorIndex(0U));
420 EXPECT_FALSE(render_text
->IsValidCursorIndex(1U));
421 EXPECT_TRUE(render_text
->IsValidCursorIndex(2U));
423 // FindCursorPosition() should not return positions between a surrogate pair.
424 render_text
->SetDisplayRect(Rect(0, 0, 20, 20));
425 EXPECT_EQ(render_text
->FindCursorPosition(Point(0, 0)).caret_pos(), 0U);
426 EXPECT_EQ(render_text
->FindCursorPosition(Point(20, 0)).caret_pos(), 2U);
427 for (int x
= -1; x
<= 20; ++x
) {
428 SelectionModel selection
= render_text
->FindCursorPosition(Point(x
, 0));
429 EXPECT_TRUE(selection
.caret_pos() == 0U || selection
.caret_pos() == 2U);
432 // GetGlyphBounds() should yield the entire string bounds for text index 0.
433 EXPECT_EQ(render_text
->GetStringSize().width(),
434 static_cast<int>(render_text
->GetGlyphBounds(0U).length()));
436 // Cursoring is independent of underlying characters when text is obscured.
437 const wchar_t* const texts
[] = {
438 kWeak
, kLtr
, kLtrRtl
, kLtrRtlLtr
, kRtl
, kRtlLtr
, kRtlLtrRtl
,
439 L
"hop on pop", // Check LTR word boundaries.
440 L
"\x05d0\x05d1 \x05d0\x05d2 \x05d1\x05d2", // Check RTL word boundaries.
442 for (size_t i
= 0; i
< arraysize(texts
); ++i
) {
443 base::string16 text
= WideToUTF16(texts
[i
]);
444 TestVisualCursorMotionInObscuredField(render_text
.get(), text
, false);
445 TestVisualCursorMotionInObscuredField(render_text
.get(), text
, true);
449 TEST_F(RenderTextTest
, RevealObscuredText
) {
450 const base::string16 seuss
= ASCIIToUTF16("hop on pop");
451 const base::string16 no_seuss
= ASCIIToUTF16("**********");
452 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
454 render_text
->SetText(seuss
);
455 render_text
->SetObscured(true);
456 EXPECT_EQ(seuss
, render_text
->text());
457 EXPECT_EQ(no_seuss
, render_text
->GetDisplayText());
459 // Valid reveal index and new revealed index clears previous one.
460 render_text
->RenderText::SetObscuredRevealIndex(0);
461 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text
->GetDisplayText());
462 render_text
->RenderText::SetObscuredRevealIndex(1);
463 EXPECT_EQ(ASCIIToUTF16("*o********"), render_text
->GetDisplayText());
464 render_text
->RenderText::SetObscuredRevealIndex(2);
465 EXPECT_EQ(ASCIIToUTF16("**p*******"), render_text
->GetDisplayText());
467 // Invalid reveal index.
468 render_text
->RenderText::SetObscuredRevealIndex(-1);
469 EXPECT_EQ(no_seuss
, render_text
->GetDisplayText());
470 render_text
->RenderText::SetObscuredRevealIndex(seuss
.length() + 1);
471 EXPECT_EQ(no_seuss
, render_text
->GetDisplayText());
473 // SetObscured clears the revealed index.
474 render_text
->RenderText::SetObscuredRevealIndex(0);
475 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text
->GetDisplayText());
476 render_text
->SetObscured(false);
477 EXPECT_EQ(seuss
, render_text
->GetDisplayText());
478 render_text
->SetObscured(true);
479 EXPECT_EQ(no_seuss
, render_text
->GetDisplayText());
481 // SetText clears the revealed index.
482 render_text
->SetText(ASCIIToUTF16("new"));
483 EXPECT_EQ(ASCIIToUTF16("***"), render_text
->GetDisplayText());
484 render_text
->RenderText::SetObscuredRevealIndex(2);
485 EXPECT_EQ(ASCIIToUTF16("**w"), render_text
->GetDisplayText());
486 render_text
->SetText(ASCIIToUTF16("new longer"));
487 EXPECT_EQ(ASCIIToUTF16("**********"), render_text
->GetDisplayText());
489 // Text with invalid surrogates.
490 const base::char16 invalid_surrogates
[] = {0xDC00, 0xD800, 'h', 'o', 'p', 0};
491 render_text
->SetText(invalid_surrogates
);
492 EXPECT_EQ(ASCIIToUTF16("*****"), render_text
->GetDisplayText());
493 render_text
->RenderText::SetObscuredRevealIndex(0);
494 const base::char16 invalid_expect_0
[] = {0xDC00, '*', '*', '*', '*', 0};
495 EXPECT_EQ(invalid_expect_0
, render_text
->GetDisplayText());
496 render_text
->RenderText::SetObscuredRevealIndex(1);
497 const base::char16 invalid_expect_1
[] = {'*', 0xD800, '*', '*', '*', 0};
498 EXPECT_EQ(invalid_expect_1
, render_text
->GetDisplayText());
499 render_text
->RenderText::SetObscuredRevealIndex(2);
500 EXPECT_EQ(ASCIIToUTF16("**h**"), render_text
->GetDisplayText());
502 // Text with valid surrogates before and after the reveal index.
503 const base::char16 valid_surrogates
[] =
504 {0xD800, 0xDC00, 'h', 'o', 'p', 0xD800, 0xDC00, 0};
505 render_text
->SetText(valid_surrogates
);
506 EXPECT_EQ(ASCIIToUTF16("*****"), render_text
->GetDisplayText());
507 render_text
->RenderText::SetObscuredRevealIndex(0);
508 const base::char16 valid_expect_0_and_1
[] =
509 {0xD800, 0xDC00, '*', '*', '*', '*', 0};
510 EXPECT_EQ(valid_expect_0_and_1
, render_text
->GetDisplayText());
511 render_text
->RenderText::SetObscuredRevealIndex(1);
512 EXPECT_EQ(valid_expect_0_and_1
, render_text
->GetDisplayText());
513 render_text
->RenderText::SetObscuredRevealIndex(2);
514 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text
->GetDisplayText());
515 render_text
->RenderText::SetObscuredRevealIndex(5);
516 const base::char16 valid_expect_5_and_6
[] =
517 {'*', '*', '*', '*', 0xD800, 0xDC00, 0};
518 EXPECT_EQ(valid_expect_5_and_6
, render_text
->GetDisplayText());
519 render_text
->RenderText::SetObscuredRevealIndex(6);
520 EXPECT_EQ(valid_expect_5_and_6
, render_text
->GetDisplayText());
523 TEST_F(RenderTextTest
, ElidedText
) {
524 // TODO(skanuj) : Add more test cases for following
525 // - RenderText styles.
526 // - Cross interaction of truncate, elide and obscure.
527 // - ElideText tests from text_elider.cc.
530 const wchar_t* display_text
;
531 const bool elision_expected
;
533 // Strings shorter than the elision width should be laid out in full.
534 { L
"", L
"" , false },
535 { L
"M", L
"" , false },
536 { L
" . ", L
" . " , false },
537 { kWeak
, kWeak
, false },
538 { kLtr
, kLtr
, false },
539 { kLtrRtl
, kLtrRtl
, false },
540 { kLtrRtlLtr
, kLtrRtlLtr
, false },
541 { kRtl
, kRtl
, false },
542 { kRtlLtr
, kRtlLtr
, false },
543 { kRtlLtrRtl
, kRtlLtrRtl
, false },
544 // Strings as long as the elision width should be laid out in full.
545 { L
"012ab", L
"012ab" , false },
546 // Long strings should be elided with an ellipsis appended at the end.
547 { L
"012abc", L
"012a\x2026", true },
548 { L
"012ab" L
"\x5d0\x5d1", L
"012a\x2026", true },
549 { L
"012a" L
"\x5d1" L
"b", L
"012a\x2026", true },
550 // No RLM marker added as digits (012) have weak directionality.
551 { L
"01" L
"\x5d0\x5d1\x5d2", L
"01\x5d0\x5d1\x2026", true },
552 // RLM marker added as "ab" have strong LTR directionality.
553 { L
"ab" L
"\x5d0\x5d1\x5d2", L
"ab\x5d0\x5d1\x2026\x200f", true },
554 // Complex script is not handled. In this example, the "\x0915\x093f" is a
555 // compound glyph, but only half of it is elided.
556 { L
"0123\x0915\x093f", L
"0123\x0915\x2026", true },
557 // Surrogate pairs should be elided reasonably enough.
558 { L
"0\x05e9\x05bc\x05c1\x05b8", L
"0\x05e9\x05bc\x05c1\x05b8", false },
559 { L
"0\x05e9\x05bc\x05c1\x05b8", L
"0\x05e9\x05bc\x2026" , true },
560 { L
"01\x05e9\x05bc\x05c1\x05b8", L
"01\x05e9\x2026" , true },
561 { L
"012\x05e9\x05bc\x05c1\x05b8", L
"012\x2026\x200E" , true },
562 { L
"012\xF0\x9D\x84\x9E", L
"012\xF0\x2026" , true },
565 scoped_ptr
<RenderText
> expected_render_text(RenderText::CreateInstance());
566 expected_render_text
->SetFontList(FontList("serif, Sans serif, 12px"));
567 expected_render_text
->SetDisplayRect(Rect(0, 0, 9999, 100));
569 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
570 render_text
->SetFontList(FontList("serif, Sans serif, 12px"));
571 render_text
->SetElideBehavior(ELIDE_TAIL
);
573 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
574 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS
"] '%ls'", i
,
577 // Compute expected width
578 expected_render_text
->SetText(WideToUTF16(cases
[i
].display_text
));
579 int expected_width
= expected_render_text
->GetContentWidth();
581 base::string16 input
= WideToUTF16(cases
[i
].text
);
582 // Extend the input text to ensure that it is wider than the display_text,
583 // and so it will get elided.
584 if (cases
[i
].elision_expected
)
585 input
.append(WideToUTF16(L
" MMMMMMMMMMM"));
586 render_text
->SetText(input
);
587 render_text
->SetDisplayRect(Rect(0, 0, expected_width
, 100));
588 EXPECT_EQ(input
, render_text
->text());
589 EXPECT_EQ(WideToUTF16(cases
[i
].display_text
),
590 render_text
->GetDisplayText());
591 expected_render_text
->SetText(base::string16());
595 TEST_F(RenderTextTest
, ElidedObscuredText
) {
596 scoped_ptr
<RenderText
> expected_render_text(RenderText::CreateInstance());
597 expected_render_text
->SetFontList(FontList("serif, Sans serif, 12px"));
598 expected_render_text
->SetDisplayRect(Rect(0, 0, 9999, 100));
599 expected_render_text
->SetText(WideToUTF16(L
"**\x2026"));
601 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
602 render_text
->SetFontList(FontList("serif, Sans serif, 12px"));
603 render_text
->SetElideBehavior(ELIDE_TAIL
);
604 render_text
->SetDisplayRect(
605 Rect(0, 0, expected_render_text
->GetContentWidth(), 100));
606 render_text
->SetObscured(true);
607 render_text
->SetText(WideToUTF16(L
"abcdef"));
608 EXPECT_EQ(WideToUTF16(L
"abcdef"), render_text
->text());
609 EXPECT_EQ(WideToUTF16(L
"**\x2026"), render_text
->GetDisplayText());
612 TEST_F(RenderTextTest
, TruncatedText
) {
615 const wchar_t* display_text
;
617 // Strings shorter than the truncation length should be laid out in full.
621 { kLtrRtl
, kLtrRtl
},
622 { kLtrRtlLtr
, kLtrRtlLtr
},
624 { kRtlLtr
, kRtlLtr
},
625 { kRtlLtrRtl
, kRtlLtrRtl
},
626 // Strings as long as the truncation length should be laid out in full.
627 { L
"01234", L
"01234" },
628 // Long strings should be truncated with an ellipsis appended at the end.
629 { L
"012345", L
"0123\x2026" },
630 { L
"012" L
" . ", L
"012 \x2026" },
631 { L
"012" L
"abc", L
"012a\x2026" },
632 { L
"012" L
"a" L
"\x5d0\x5d1", L
"012a\x2026" },
633 { L
"012" L
"a" L
"\x5d1" L
"b", L
"012a\x2026" },
634 { L
"012" L
"\x5d0\x5d1\x5d2", L
"012\x5d0\x2026" },
635 { L
"012" L
"\x5d0\x5d1" L
"a", L
"012\x5d0\x2026" },
636 { L
"012" L
"\x5d0" L
"a" L
"\x5d1", L
"012\x5d0\x2026" },
637 // Surrogate pairs should be truncated reasonably enough.
638 { L
"0123\x0915\x093f", L
"0123\x2026" },
639 { L
"0\x05e9\x05bc\x05c1\x05b8", L
"0\x05e9\x05bc\x05c1\x05b8" },
640 { L
"01\x05e9\x05bc\x05c1\x05b8", L
"01\x05e9\x05bc\x2026" },
641 { L
"012\x05e9\x05bc\x05c1\x05b8", L
"012\x05e9\x2026" },
642 { L
"0123\x05e9\x05bc\x05c1\x05b8", L
"0123\x2026" },
643 { L
"01234\x05e9\x05bc\x05c1\x05b8", L
"0123\x2026" },
644 { L
"012\xF0\x9D\x84\x9E", L
"012\xF0\x2026" },
647 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
648 render_text
->set_truncate_length(5);
649 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
650 render_text
->SetText(WideToUTF16(cases
[i
].text
));
651 EXPECT_EQ(WideToUTF16(cases
[i
].text
), render_text
->text());
652 EXPECT_EQ(WideToUTF16(cases
[i
].display_text
), render_text
->GetDisplayText())
653 << "For case " << i
<< ": " << cases
[i
].text
;
657 TEST_F(RenderTextTest
, TruncatedObscuredText
) {
658 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
659 render_text
->set_truncate_length(3);
660 render_text
->SetObscured(true);
661 render_text
->SetText(WideToUTF16(L
"abcdef"));
662 EXPECT_EQ(WideToUTF16(L
"abcdef"), render_text
->text());
663 EXPECT_EQ(WideToUTF16(L
"**\x2026"), render_text
->GetDisplayText());
666 TEST_F(RenderTextTest
, TruncatedCursorMovementLTR
) {
667 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
668 render_text
->set_truncate_length(2);
669 render_text
->SetText(WideToUTF16(L
"abcd"));
671 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
672 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
673 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD
), render_text
->selection_model());
674 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
675 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
677 std::vector
<SelectionModel
> expected
;
678 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
679 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
680 // The cursor hops over the ellipsis and elided text to the line end.
681 expected
.push_back(SelectionModel(4, CURSOR_BACKWARD
));
682 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
683 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
686 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
687 // The cursor hops over the elided text to preceeding text.
688 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
689 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
690 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
691 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
694 TEST_F(RenderTextTest
, TruncatedCursorMovementRTL
) {
695 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
696 render_text
->set_truncate_length(2);
697 render_text
->SetText(WideToUTF16(L
"\x5d0\x5d1\x5d2\x5d3"));
699 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
700 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
701 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD
), render_text
->selection_model());
702 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
703 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD
), render_text
->selection_model());
705 std::vector
<SelectionModel
> expected
;
706 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
707 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
708 // The cursor hops over the ellipsis and elided text to the line end.
709 expected
.push_back(SelectionModel(4, CURSOR_BACKWARD
));
710 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
711 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
714 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
715 // The cursor hops over the elided text to preceeding text.
716 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
717 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
718 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
719 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
722 TEST_F(RenderTextTest
, GetDisplayTextDirection
) {
725 const base::i18n::TextDirection text_direction
;
727 // Blank strings and those with no/weak directionality default to LTR.
728 { L
"", base::i18n::LEFT_TO_RIGHT
},
729 { kWeak
, base::i18n::LEFT_TO_RIGHT
},
730 // Strings that begin with strong LTR characters.
731 { kLtr
, base::i18n::LEFT_TO_RIGHT
},
732 { kLtrRtl
, base::i18n::LEFT_TO_RIGHT
},
733 { kLtrRtlLtr
, base::i18n::LEFT_TO_RIGHT
},
734 // Strings that begin with strong RTL characters.
735 { kRtl
, base::i18n::RIGHT_TO_LEFT
},
736 { kRtlLtr
, base::i18n::RIGHT_TO_LEFT
},
737 { kRtlLtrRtl
, base::i18n::RIGHT_TO_LEFT
},
740 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
741 const bool was_rtl
= base::i18n::IsRTL();
743 for (size_t i
= 0; i
< 2; ++i
) {
744 // Toggle the application default text direction (to try each direction).
745 SetRTL(!base::i18n::IsRTL());
746 const base::i18n::TextDirection ui_direction
= base::i18n::IsRTL() ?
747 base::i18n::RIGHT_TO_LEFT
: base::i18n::LEFT_TO_RIGHT
;
749 // Ensure that directionality modes yield the correct text directions.
750 for (size_t j
= 0; j
< arraysize(cases
); j
++) {
751 render_text
->SetText(WideToUTF16(cases
[j
].text
));
752 render_text
->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT
);
753 EXPECT_EQ(render_text
->GetDisplayTextDirection(),cases
[j
].text_direction
);
754 render_text
->SetDirectionalityMode(DIRECTIONALITY_FROM_UI
);
755 EXPECT_EQ(render_text
->GetDisplayTextDirection(), ui_direction
);
756 render_text
->SetDirectionalityMode(DIRECTIONALITY_FORCE_LTR
);
757 EXPECT_EQ(render_text
->GetDisplayTextDirection(),
758 base::i18n::LEFT_TO_RIGHT
);
759 render_text
->SetDirectionalityMode(DIRECTIONALITY_FORCE_RTL
);
760 EXPECT_EQ(render_text
->GetDisplayTextDirection(),
761 base::i18n::RIGHT_TO_LEFT
);
765 EXPECT_EQ(was_rtl
, base::i18n::IsRTL());
767 // Ensure that text changes update the direction for DIRECTIONALITY_FROM_TEXT.
768 render_text
->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT
);
769 render_text
->SetText(WideToUTF16(kLtr
));
770 EXPECT_EQ(render_text
->GetDisplayTextDirection(), base::i18n::LEFT_TO_RIGHT
);
771 render_text
->SetText(WideToUTF16(kRtl
));
772 EXPECT_EQ(render_text
->GetDisplayTextDirection(), base::i18n::RIGHT_TO_LEFT
);
775 TEST_F(RenderTextTest
, MoveCursorLeftRightInLtr
) {
776 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
779 render_text
->SetText(ASCIIToUTF16("abc"));
780 // |expected| saves the expected SelectionModel when moving cursor from left
782 std::vector
<SelectionModel
> expected
;
783 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
784 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
785 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
786 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
787 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
788 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
791 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
792 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
793 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
794 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
795 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
796 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
799 TEST_F(RenderTextTest
, MoveCursorLeftRightInLtrRtl
) {
800 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
802 render_text
->SetText(WideToUTF16(L
"abc\x05d0\x05d1\x05d2"));
803 // The last one is the expected END position.
804 std::vector
<SelectionModel
> expected
;
805 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
806 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
807 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
808 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
809 expected
.push_back(SelectionModel(5, CURSOR_FORWARD
));
810 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
811 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
812 expected
.push_back(SelectionModel(6, CURSOR_FORWARD
));
813 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
816 expected
.push_back(SelectionModel(6, CURSOR_FORWARD
));
817 expected
.push_back(SelectionModel(4, CURSOR_BACKWARD
));
818 expected
.push_back(SelectionModel(5, CURSOR_BACKWARD
));
819 expected
.push_back(SelectionModel(6, CURSOR_BACKWARD
));
820 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
821 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
822 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
823 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
824 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
827 TEST_F(RenderTextTest
, MoveCursorLeftRightInLtrRtlLtr
) {
828 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
830 render_text
->SetText(WideToUTF16(L
"a" L
"\x05d1" L
"b"));
831 std::vector
<SelectionModel
> expected
;
832 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
833 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
834 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
835 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
836 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
837 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
840 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
841 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
842 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
843 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
844 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
845 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
848 TEST_F(RenderTextTest
, MoveCursorLeftRightInRtl
) {
849 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
851 render_text
->SetText(WideToUTF16(L
"\x05d0\x05d1\x05d2"));
852 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
853 std::vector
<SelectionModel
> expected
;
855 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
856 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
857 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
858 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
859 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
860 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
864 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
865 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
866 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
867 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
868 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
869 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
872 TEST_F(RenderTextTest
, MoveCursorLeftRightInRtlLtr
) {
873 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
875 render_text
->SetText(WideToUTF16(L
"\x05d0\x05d1\x05d2" L
"abc"));
876 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
877 std::vector
<SelectionModel
> expected
;
878 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
879 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
880 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
881 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
882 expected
.push_back(SelectionModel(5, CURSOR_FORWARD
));
883 expected
.push_back(SelectionModel(4, CURSOR_FORWARD
));
884 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
885 expected
.push_back(SelectionModel(6, CURSOR_FORWARD
));
886 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
889 expected
.push_back(SelectionModel(6, CURSOR_FORWARD
));
890 expected
.push_back(SelectionModel(4, CURSOR_BACKWARD
));
891 expected
.push_back(SelectionModel(5, CURSOR_BACKWARD
));
892 expected
.push_back(SelectionModel(6, CURSOR_BACKWARD
));
893 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
894 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
895 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
896 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
897 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
900 TEST_F(RenderTextTest
, MoveCursorLeftRightInRtlLtrRtl
) {
901 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
903 render_text
->SetText(WideToUTF16(L
"\x05d0" L
"a" L
"\x05d1"));
904 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
905 std::vector
<SelectionModel
> expected
;
906 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
907 expected
.push_back(SelectionModel(1, CURSOR_BACKWARD
));
908 expected
.push_back(SelectionModel(1, CURSOR_FORWARD
));
909 expected
.push_back(SelectionModel(3, CURSOR_BACKWARD
));
910 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
911 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_LEFT
);
914 expected
.push_back(SelectionModel(3, CURSOR_FORWARD
));
915 expected
.push_back(SelectionModel(2, CURSOR_FORWARD
));
916 expected
.push_back(SelectionModel(2, CURSOR_BACKWARD
));
917 expected
.push_back(SelectionModel(0, CURSOR_FORWARD
));
918 expected
.push_back(SelectionModel(0, CURSOR_BACKWARD
));
919 RunMoveCursorLeftRightTest(render_text
.get(), expected
, CURSOR_RIGHT
);
922 // TODO(xji): temporarily disable in platform Win since the complex script
923 // characters turned into empty square due to font regression. So, not able
924 // to test 2 characters belong to the same grapheme.
925 #if defined(OS_LINUX)
926 TEST_F(RenderTextTest
, MoveCursorLeftRight_ComplexScript
) {
927 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
929 render_text
->SetText(WideToUTF16(L
"\x0915\x093f\x0915\x094d\x0915"));
930 EXPECT_EQ(0U, render_text
->cursor_position());
931 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
932 EXPECT_EQ(2U, render_text
->cursor_position());
933 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
934 EXPECT_EQ(4U, render_text
->cursor_position());
935 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
936 EXPECT_EQ(5U, render_text
->cursor_position());
937 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
938 EXPECT_EQ(5U, render_text
->cursor_position());
940 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
941 EXPECT_EQ(4U, render_text
->cursor_position());
942 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
943 EXPECT_EQ(2U, render_text
->cursor_position());
944 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
945 EXPECT_EQ(0U, render_text
->cursor_position());
946 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
947 EXPECT_EQ(0U, render_text
->cursor_position());
951 // TODO(ckocagil): Enable for RenderTextHarfBuzz. http://crbug.com/383265
952 #if defined(OS_MACOSX)
953 TEST_F(RenderTextTest
, MoveCursorLeftRight_MeiryoUILigatures
) {
954 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
955 // Meiryo UI uses single-glyph ligatures for 'ff' and 'ffi', but each letter
956 // (code point) has unique bounds, so mid-glyph cursoring should be possible.
957 render_text
->SetFontList(FontList("Meiryo UI, 12px"));
958 render_text
->SetText(WideToUTF16(L
"ff ffi"));
959 EXPECT_EQ(0U, render_text
->cursor_position());
960 for (size_t i
= 0; i
< render_text
->text().length(); ++i
) {
961 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
962 EXPECT_EQ(i
+ 1, render_text
->cursor_position());
964 EXPECT_EQ(6U, render_text
->cursor_position());
966 #endif // defined(OS_MACOSX)
968 TEST_F(RenderTextTest
, GraphemePositions
) {
969 // LTR 2-character grapheme, LTR abc, LTR 2-character grapheme.
970 const base::string16 kText1
=
971 WideToUTF16(L
"\x0915\x093f" L
"abc" L
"\x0915\x093f");
973 // LTR ab, LTR 2-character grapheme, LTR cd.
974 const base::string16 kText2
= WideToUTF16(L
"ab" L
"\x0915\x093f" L
"cd");
976 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
977 // two characters forming the surrogate pair 0x0001D11E.
978 const std::string kSurrogate
= "\xF0\x9D\x84\x9E";
980 // LTR ab, UTF16 surrogate pair, LTR cd.
981 const base::string16 kText3
= UTF8ToUTF16("ab" + kSurrogate
+ "cd");
986 size_t expected_previous
;
987 size_t expected_next
;
989 { base::string16(), 0, 0, 0 },
990 { base::string16(), 1, 0, 0 },
991 { base::string16(), 50, 0, 0 },
1000 { kText1
, 8, 7, 7 },
1001 { kText1
, 50, 7, 7 },
1002 { kText2
, 0, 0, 1 },
1003 { kText2
, 1, 0, 2 },
1004 { kText2
, 2, 1, 4 },
1005 { kText2
, 3, 2, 4 },
1006 { kText2
, 4, 2, 5 },
1007 { kText2
, 5, 4, 6 },
1008 { kText2
, 6, 5, 6 },
1009 { kText2
, 7, 6, 6 },
1010 { kText2
, 50, 6, 6 },
1011 { kText3
, 0, 0, 1 },
1012 { kText3
, 1, 0, 2 },
1013 { kText3
, 2, 1, 4 },
1014 { kText3
, 3, 2, 4 },
1015 { kText3
, 4, 2, 5 },
1016 { kText3
, 5, 4, 6 },
1017 { kText3
, 6, 5, 6 },
1018 { kText3
, 7, 6, 6 },
1019 { kText3
, 50, 6, 6 },
1023 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1024 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
1028 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1029 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
1030 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS
"]", i
));
1031 render_text
->SetText(cases
[i
].text
);
1033 size_t next
= render_text
->IndexOfAdjacentGrapheme(cases
[i
].index
,
1035 EXPECT_EQ(cases
[i
].expected_next
, next
);
1036 EXPECT_TRUE(render_text
->IsValidCursorIndex(next
));
1038 size_t previous
= render_text
->IndexOfAdjacentGrapheme(cases
[i
].index
,
1040 EXPECT_EQ(cases
[i
].expected_previous
, previous
);
1041 EXPECT_TRUE(render_text
->IsValidCursorIndex(previous
));
1045 TEST_F(RenderTextTest
, MidGraphemeSelectionBounds
) {
1047 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1048 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
1052 // Test that selection bounds may be set amid multi-character graphemes.
1053 const base::string16 kHindi
= WideToUTF16(L
"\x0915\x093f");
1054 const base::string16 kThai
= WideToUTF16(L
"\x0e08\x0e33");
1055 const base::string16 cases
[] = { kHindi
, kThai
};
1057 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1058 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
1059 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS
"]", i
));
1060 render_text
->SetText(cases
[i
]);
1061 EXPECT_TRUE(render_text
->IsValidLogicalIndex(1));
1062 #if !defined(OS_MACOSX)
1063 EXPECT_FALSE(render_text
->IsValidCursorIndex(1));
1065 EXPECT_TRUE(render_text
->SelectRange(Range(2, 1)));
1066 EXPECT_EQ(Range(2, 1), render_text
->selection());
1067 EXPECT_EQ(1U, render_text
->cursor_position());
1068 // Although selection bounds may be set within a multi-character grapheme,
1069 // cursor movement (e.g. via arrow key) should avoid those indices.
1070 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1071 EXPECT_EQ(0U, render_text
->cursor_position());
1072 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1073 EXPECT_EQ(2U, render_text
->cursor_position());
1077 TEST_F(RenderTextTest
, FindCursorPosition
) {
1078 const wchar_t* kTestStrings
[] = { kLtrRtl
, kLtrRtlLtr
, kRtlLtr
, kRtlLtrRtl
};
1079 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1080 render_text
->SetDisplayRect(Rect(0, 0, 100, 20));
1081 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
1082 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS
"]", i
));
1083 render_text
->SetText(WideToUTF16(kTestStrings
[i
]));
1084 for(size_t j
= 0; j
< render_text
->text().length(); ++j
) {
1085 const Range
range(render_text
->GetGlyphBounds(j
));
1086 // Test a point just inside the leading edge of the glyph bounds.
1087 int x
= range
.is_reversed() ? range
.GetMax() - 1 : range
.GetMin() + 1;
1088 EXPECT_EQ(j
, render_text
->FindCursorPosition(Point(x
, 0)).caret_pos());
1093 TEST_F(RenderTextTest
, EdgeSelectionModels
) {
1094 // Simple Latin text.
1095 const base::string16 kLatin
= WideToUTF16(L
"abc");
1096 // LTR 2-character grapheme.
1097 const base::string16 kLTRGrapheme
= WideToUTF16(L
"\x0915\x093f");
1098 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
1099 const base::string16 kHindiLatin
=
1100 WideToUTF16(L
"\x0915\x093f" L
"a" L
"\x0915\x093f");
1101 // RTL 2-character grapheme.
1102 const base::string16 kRTLGrapheme
= WideToUTF16(L
"\x05e0\x05b8");
1103 // RTL 2-character grapheme, LTR a, RTL 2-character grapheme.
1104 const base::string16 kHebrewLatin
=
1105 WideToUTF16(L
"\x05e0\x05b8" L
"a" L
"\x05e0\x05b8");
1108 base::string16 text
;
1109 base::i18n::TextDirection expected_text_direction
;
1111 { base::string16(), base::i18n::LEFT_TO_RIGHT
},
1112 { kLatin
, base::i18n::LEFT_TO_RIGHT
},
1113 { kLTRGrapheme
, base::i18n::LEFT_TO_RIGHT
},
1114 { kHindiLatin
, base::i18n::LEFT_TO_RIGHT
},
1115 { kRTLGrapheme
, base::i18n::RIGHT_TO_LEFT
},
1116 { kHebrewLatin
, base::i18n::RIGHT_TO_LEFT
},
1120 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1121 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
1125 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1126 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
1127 render_text
->SetText(cases
[i
].text
);
1128 bool ltr
= (cases
[i
].expected_text_direction
== base::i18n::LEFT_TO_RIGHT
);
1130 SelectionModel start_edge
=
1131 render_text
->EdgeSelectionModel(ltr
? CURSOR_LEFT
: CURSOR_RIGHT
);
1132 EXPECT_EQ(start_edge
, SelectionModel(0, CURSOR_BACKWARD
));
1134 SelectionModel end_edge
=
1135 render_text
->EdgeSelectionModel(ltr
? CURSOR_RIGHT
: CURSOR_LEFT
);
1136 EXPECT_EQ(end_edge
, SelectionModel(cases
[i
].text
.length(), CURSOR_FORWARD
));
1140 TEST_F(RenderTextTest
, SelectAll
) {
1141 const wchar_t* const cases
[] =
1142 { kWeak
, kLtr
, kLtrRtl
, kLtrRtlLtr
, kRtl
, kRtlLtr
, kRtlLtrRtl
};
1144 // Ensure that SelectAll respects the |reversed| argument regardless of
1145 // application locale and text content directionality.
1146 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1147 const SelectionModel
expected_reversed(Range(3, 0), CURSOR_FORWARD
);
1148 const SelectionModel
expected_forwards(Range(0, 3), CURSOR_BACKWARD
);
1149 const bool was_rtl
= base::i18n::IsRTL();
1151 for (size_t i
= 0; i
< 2; ++i
) {
1152 SetRTL(!base::i18n::IsRTL());
1153 // Test that an empty string produces an empty selection model.
1154 render_text
->SetText(base::string16());
1155 EXPECT_EQ(render_text
->selection_model(), SelectionModel());
1157 // Test the weak, LTR, RTL, and Bidi string cases.
1158 for (size_t j
= 0; j
< arraysize(cases
); j
++) {
1159 render_text
->SetText(WideToUTF16(cases
[j
]));
1160 render_text
->SelectAll(false);
1161 EXPECT_EQ(render_text
->selection_model(), expected_forwards
);
1162 render_text
->SelectAll(true);
1163 EXPECT_EQ(render_text
->selection_model(), expected_reversed
);
1167 EXPECT_EQ(was_rtl
, base::i18n::IsRTL());
1170 TEST_F(RenderTextTest
, MoveCursorLeftRightWithSelection
) {
1171 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1172 render_text
->SetText(WideToUTF16(L
"abc\x05d0\x05d1\x05d2"));
1173 // Left arrow on select ranging (6, 4).
1174 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
1175 EXPECT_EQ(Range(6), render_text
->selection());
1176 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1177 EXPECT_EQ(Range(4), render_text
->selection());
1178 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1179 EXPECT_EQ(Range(5), render_text
->selection());
1180 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1181 EXPECT_EQ(Range(6), render_text
->selection());
1182 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, true);
1183 EXPECT_EQ(Range(6, 5), render_text
->selection());
1184 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, true);
1185 EXPECT_EQ(Range(6, 4), render_text
->selection());
1186 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1187 EXPECT_EQ(Range(6), render_text
->selection());
1189 // Right arrow on select ranging (4, 6).
1190 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
1191 EXPECT_EQ(Range(0), render_text
->selection());
1192 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1193 EXPECT_EQ(Range(1), render_text
->selection());
1194 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1195 EXPECT_EQ(Range(2), render_text
->selection());
1196 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1197 EXPECT_EQ(Range(3), render_text
->selection());
1198 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1199 EXPECT_EQ(Range(5), render_text
->selection());
1200 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1201 EXPECT_EQ(Range(4), render_text
->selection());
1202 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, true);
1203 EXPECT_EQ(Range(4, 5), render_text
->selection());
1204 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, true);
1205 EXPECT_EQ(Range(4, 6), render_text
->selection());
1206 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1207 EXPECT_EQ(Range(4), render_text
->selection());
1210 TEST_F(RenderTextTest
, CenteredDisplayOffset
) {
1211 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1212 render_text
->SetText(ASCIIToUTF16("abcdefghij"));
1213 render_text
->SetHorizontalAlignment(ALIGN_CENTER
);
1215 const int kEnlargement
= 10;
1216 const int content_width
= render_text
->GetContentWidth();
1217 Rect
display_rect(0, 0, content_width
/ 2,
1218 render_text
->font_list().GetHeight());
1219 render_text
->SetDisplayRect(display_rect
);
1221 // Move the cursor to the beginning of the text and, by checking the cursor
1222 // bounds, make sure no empty space is to the left of the text.
1223 render_text
->SetCursorPosition(0);
1224 EXPECT_EQ(display_rect
.x(), render_text
->GetUpdatedCursorBounds().x());
1226 // Widen the display rect and, by checking the cursor bounds, make sure no
1227 // empty space is introduced to the left of the text.
1228 display_rect
.Inset(0, 0, -kEnlargement
, 0);
1229 render_text
->SetDisplayRect(display_rect
);
1230 EXPECT_EQ(display_rect
.x(), render_text
->GetUpdatedCursorBounds().x());
1232 // Move the cursor to the end of the text and, by checking the cursor bounds,
1233 // make sure no empty space is to the right of the text.
1234 render_text
->SetCursorPosition(render_text
->text().length());
1235 EXPECT_EQ(display_rect
.right(),
1236 render_text
->GetUpdatedCursorBounds().right());
1238 // Widen the display rect and, by checking the cursor bounds, make sure no
1239 // empty space is introduced to the right of the text.
1240 display_rect
.Inset(0, 0, -kEnlargement
, 0);
1241 render_text
->SetDisplayRect(display_rect
);
1242 EXPECT_EQ(display_rect
.right(),
1243 render_text
->GetUpdatedCursorBounds().right());
1245 #endif // !defined(OS_MACOSX)
1247 // TODO(xji): Make these work on Windows.
1248 #if defined(OS_LINUX)
1249 void MoveLeftRightByWordVerifier(RenderText
* render_text
,
1250 const wchar_t* str
) {
1251 render_text
->SetText(WideToUTF16(str
));
1253 // Test moving by word from left ro right.
1254 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
1255 bool first_word
= true;
1257 // First, test moving by word from a word break position, such as from
1258 // "|abc def" to "abc| def".
1259 SelectionModel start
= render_text
->selection_model();
1260 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1261 SelectionModel end
= render_text
->selection_model();
1262 if (end
== start
) // reach the end.
1265 // For testing simplicity, each word is a 3-character word.
1266 int num_of_character_moves
= first_word
? 3 : 4;
1268 render_text
->MoveCursorTo(start
);
1269 for (int j
= 0; j
< num_of_character_moves
; ++j
)
1270 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1271 EXPECT_EQ(end
, render_text
->selection_model());
1273 // Then, test moving by word from positions inside the word, such as from
1274 // "a|bc def" to "abc| def", and from "ab|c def" to "abc| def".
1275 for (int j
= 1; j
< num_of_character_moves
; ++j
) {
1276 render_text
->MoveCursorTo(start
);
1277 for (int k
= 0; k
< j
; ++k
)
1278 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_RIGHT
, false);
1279 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1280 EXPECT_EQ(end
, render_text
->selection_model());
1284 // Test moving by word from right to left.
1285 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
1288 SelectionModel start
= render_text
->selection_model();
1289 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, false);
1290 SelectionModel end
= render_text
->selection_model();
1291 if (end
== start
) // reach the end.
1294 int num_of_character_moves
= first_word
? 3 : 4;
1296 render_text
->MoveCursorTo(start
);
1297 for (int j
= 0; j
< num_of_character_moves
; ++j
)
1298 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1299 EXPECT_EQ(end
, render_text
->selection_model());
1301 for (int j
= 1; j
< num_of_character_moves
; ++j
) {
1302 render_text
->MoveCursorTo(start
);
1303 for (int k
= 0; k
< j
; ++k
)
1304 render_text
->MoveCursor(CHARACTER_BREAK
, CURSOR_LEFT
, false);
1305 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, false);
1306 EXPECT_EQ(end
, render_text
->selection_model());
1311 TEST_F(RenderTextTest
, MoveLeftRightByWordInBidiText
) {
1312 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1314 // For testing simplicity, each word is a 3-character word.
1315 std::vector
<const wchar_t*> test
;
1316 test
.push_back(L
"abc");
1317 test
.push_back(L
"abc def");
1318 test
.push_back(L
"\x05E1\x05E2\x05E3");
1319 test
.push_back(L
"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1320 test
.push_back(L
"abc \x05E1\x05E2\x05E3");
1321 test
.push_back(L
"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1322 test
.push_back(L
"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1323 L
" \x05E7\x05E8\x05E9");
1325 test
.push_back(L
"abc \x05E1\x05E2\x05E3 hij");
1326 test
.push_back(L
"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 hij opq");
1327 test
.push_back(L
"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1328 L
" \x05E7\x05E8\x05E9" L
" opq rst uvw");
1330 test
.push_back(L
"\x05E1\x05E2\x05E3 abc");
1331 test
.push_back(L
"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 abc def");
1332 test
.push_back(L
"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 \x05E7\x05E8\x05E9"
1335 test
.push_back(L
"\x05D1\x05D2\x05D3 abc \x05E1\x05E2\x05E3");
1336 test
.push_back(L
"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 abc def"
1337 L
" \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1338 test
.push_back(L
"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 \x05D7\x05D8\x05D9"
1339 L
" abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1340 L
" \x05E7\x05E8\x05E9");
1342 for (size_t i
= 0; i
< test
.size(); ++i
)
1343 MoveLeftRightByWordVerifier(render_text
.get(), test
[i
]);
1346 TEST_F(RenderTextTest
, MoveLeftRightByWordInBidiText_TestEndOfText
) {
1347 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1349 render_text
->SetText(WideToUTF16(L
"ab\x05E1"));
1350 // Moving the cursor by word from "abC|" to the left should return "|abC".
1351 // But since end of text is always treated as a word break, it returns
1353 // TODO(xji): Need to make it work as expected.
1354 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
1355 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, false);
1356 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1358 // Moving the cursor by word from "|abC" to the right returns "abC|".
1359 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
1360 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1361 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD
), render_text
->selection_model());
1363 render_text
->SetText(WideToUTF16(L
"\x05E1\x05E2" L
"a"));
1364 // For logical text "BCa", moving the cursor by word from "aCB|" to the left
1366 render_text
->MoveCursor(LINE_BREAK
, CURSOR_RIGHT
, false);
1367 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, false);
1368 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD
), render_text
->selection_model());
1370 // Moving the cursor by word from "|aCB" to the right should return "aCB|".
1371 // But since end of text is always treated as a word break, it returns
1373 // TODO(xji): Need to make it work as expected.
1374 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
1375 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1376 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1379 TEST_F(RenderTextTest
, MoveLeftRightByWordInTextWithMultiSpaces
) {
1380 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1381 render_text
->SetText(WideToUTF16(L
"abc def"));
1382 render_text
->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD
));
1383 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1384 EXPECT_EQ(11U, render_text
->cursor_position());
1386 render_text
->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD
));
1387 render_text
->MoveCursor(WORD_BREAK
, CURSOR_LEFT
, false);
1388 EXPECT_EQ(0U, render_text
->cursor_position());
1391 TEST_F(RenderTextTest
, MoveLeftRightByWordInChineseText
) {
1392 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1393 render_text
->SetText(WideToUTF16(L
"\x6211\x4EEC\x53BB\x516C\x56ED\x73A9"));
1394 render_text
->MoveCursor(LINE_BREAK
, CURSOR_LEFT
, false);
1395 EXPECT_EQ(0U, render_text
->cursor_position());
1396 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1397 EXPECT_EQ(2U, render_text
->cursor_position());
1398 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1399 EXPECT_EQ(3U, render_text
->cursor_position());
1400 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1401 EXPECT_EQ(5U, render_text
->cursor_position());
1402 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1403 EXPECT_EQ(6U, render_text
->cursor_position());
1404 render_text
->MoveCursor(WORD_BREAK
, CURSOR_RIGHT
, false);
1405 EXPECT_EQ(6U, render_text
->cursor_position());
1409 TEST_F(RenderTextTest
, StringSizeSanity
) {
1410 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1411 render_text
->SetText(UTF8ToUTF16("Hello World"));
1412 const Size string_size
= render_text
->GetStringSize();
1413 EXPECT_GT(string_size
.width(), 0);
1414 EXPECT_GT(string_size
.height(), 0);
1417 TEST_F(RenderTextTest
, StringSizeLongStrings
) {
1418 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1419 Size previous_string_size
;
1420 for (size_t length
= 10; length
< 1000000; length
*= 10) {
1421 render_text
->SetText(base::string16(length
, 'a'));
1422 const Size string_size
= render_text
->GetStringSize();
1423 EXPECT_GT(string_size
.width(), previous_string_size
.width());
1424 EXPECT_GT(string_size
.height(), 0);
1425 previous_string_size
= string_size
;
1429 // TODO(asvitkine): This test fails because PlatformFontMac uses point font
1430 // sizes instead of pixel sizes like other implementations.
1431 #if !defined(OS_MACOSX)
1432 TEST_F(RenderTextTest
, StringSizeEmptyString
) {
1433 // Ascent and descent of Arial and Symbol are different on most platforms.
1434 const FontList
font_list("Arial,Symbol, 16px");
1435 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1436 render_text
->SetFontList(font_list
);
1437 render_text
->SetDisplayRect(Rect(0, 0, 0, font_list
.GetHeight()));
1439 // The empty string respects FontList metrics for non-zero height
1441 render_text
->SetText(base::string16());
1442 EXPECT_EQ(font_list
.GetHeight(), render_text
->GetStringSize().height());
1443 EXPECT_EQ(0, render_text
->GetStringSize().width());
1444 EXPECT_EQ(font_list
.GetBaseline(), render_text
->GetBaseline());
1446 render_text
->SetText(UTF8ToUTF16(" "));
1447 EXPECT_EQ(font_list
.GetHeight(), render_text
->GetStringSize().height());
1448 EXPECT_EQ(font_list
.GetBaseline(), render_text
->GetBaseline());
1450 #endif // !defined(OS_MACOSX)
1452 TEST_F(RenderTextTest
, StringSizeRespectsFontListMetrics
) {
1453 // Check that Arial and Symbol have different font metrics.
1454 Font
arial_font("Arial", 16);
1456 base::StringToLowerASCII(arial_font
.GetActualFontNameForTesting()));
1457 Font
symbol_font("Symbol", 16);
1459 base::StringToLowerASCII(
1460 symbol_font
.GetActualFontNameForTesting()));
1461 EXPECT_NE(arial_font
.GetHeight(), symbol_font
.GetHeight());
1462 EXPECT_NE(arial_font
.GetBaseline(), symbol_font
.GetBaseline());
1463 // "a" should be rendered with Arial, not with Symbol.
1464 const char* arial_font_text
= "a";
1465 // "®" (registered trademark symbol) should be rendered with Symbol,
1467 const char* symbol_font_text
= "\xC2\xAE";
1469 Font smaller_font
= arial_font
;
1470 Font larger_font
= symbol_font
;
1471 const char* smaller_font_text
= arial_font_text
;
1472 const char* larger_font_text
= symbol_font_text
;
1473 if (symbol_font
.GetHeight() < arial_font
.GetHeight() &&
1474 symbol_font
.GetBaseline() < arial_font
.GetBaseline()) {
1475 std::swap(smaller_font
, larger_font
);
1476 std::swap(smaller_font_text
, larger_font_text
);
1478 ASSERT_LT(smaller_font
.GetHeight(), larger_font
.GetHeight());
1479 ASSERT_LT(smaller_font
.GetBaseline(), larger_font
.GetBaseline());
1481 // Check |smaller_font_text| is rendered with the smaller font.
1482 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1483 render_text
->SetText(UTF8ToUTF16(smaller_font_text
));
1484 render_text
->SetFontList(FontList(smaller_font
));
1485 render_text
->SetDisplayRect(Rect(0, 0, 0,
1486 render_text
->font_list().GetHeight()));
1487 EXPECT_EQ(smaller_font
.GetHeight(), render_text
->GetStringSize().height());
1488 EXPECT_EQ(smaller_font
.GetBaseline(), render_text
->GetBaseline());
1490 // Layout the same text with mixed fonts. The text should be rendered with
1491 // the smaller font, but the height and baseline are determined with the
1492 // metrics of the font list, which is equal to the larger font.
1493 std::vector
<Font
> fonts
;
1494 fonts
.push_back(smaller_font
); // The primary font is the smaller font.
1495 fonts
.push_back(larger_font
);
1496 const FontList
font_list(fonts
);
1497 render_text
->SetFontList(font_list
);
1498 render_text
->SetDisplayRect(Rect(0, 0, 0,
1499 render_text
->font_list().GetHeight()));
1500 EXPECT_LT(smaller_font
.GetHeight(), render_text
->GetStringSize().height());
1501 EXPECT_LT(smaller_font
.GetBaseline(), render_text
->GetBaseline());
1502 EXPECT_EQ(font_list
.GetHeight(), render_text
->GetStringSize().height());
1503 EXPECT_EQ(font_list
.GetBaseline(), render_text
->GetBaseline());
1506 TEST_F(RenderTextTest
, MinLineHeight
) {
1507 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1509 render_text
->SetText(ASCIIToUTF16("Hello!"));
1510 SizeF default_size
= render_text
->GetStringSizeF();
1511 ASSERT_NE(0, default_size
.height());
1512 ASSERT_NE(0, default_size
.width());
1514 render_text
->SetMinLineHeight(default_size
.height() / 2);
1515 EXPECT_EQ(default_size
.ToString(), render_text
->GetStringSizeF().ToString());
1517 render_text
->SetMinLineHeight(default_size
.height() * 2);
1518 SizeF taller_size
= render_text
->GetStringSizeF();
1519 EXPECT_EQ(default_size
.height() * 2, taller_size
.height());
1520 EXPECT_EQ(default_size
.width(), taller_size
.width());
1523 TEST_F(RenderTextTest
, SetFontList
) {
1524 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1525 render_text
->SetFontList(FontList("Arial,Symbol, 13px"));
1526 const std::vector
<Font
>& fonts
= render_text
->font_list().GetFonts();
1527 ASSERT_EQ(2U, fonts
.size());
1528 EXPECT_EQ("Arial", fonts
[0].GetFontName());
1529 EXPECT_EQ("Symbol", fonts
[1].GetFontName());
1530 EXPECT_EQ(13, render_text
->font_list().GetFontSize());
1533 TEST_F(RenderTextTest
, StringSizeBoldWidth
) {
1534 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1535 render_text
->SetText(UTF8ToUTF16("Hello World"));
1537 const int plain_width
= render_text
->GetStringSize().width();
1538 EXPECT_GT(plain_width
, 0);
1540 // Apply a bold style and check that the new width is greater.
1541 render_text
->SetStyle(BOLD
, true);
1542 const int bold_width
= render_text
->GetStringSize().width();
1543 EXPECT_GT(bold_width
, plain_width
);
1545 // Now, apply a plain style over the first word only.
1546 render_text
->ApplyStyle(BOLD
, false, Range(0, 5));
1547 const int plain_bold_width
= render_text
->GetStringSize().width();
1548 EXPECT_GT(plain_bold_width
, plain_width
);
1549 EXPECT_LT(plain_bold_width
, bold_width
);
1552 TEST_F(RenderTextTest
, StringSizeHeight
) {
1553 base::string16 cases
[] = {
1554 WideToUTF16(L
"Hello World!"), // English
1555 WideToUTF16(L
"\x6328\x62f6"), // Japanese
1556 WideToUTF16(L
"\x0915\x093f"), // Hindi
1557 WideToUTF16(L
"\x05e0\x05b8"), // Hebrew
1560 const FontList default_font_list
;
1561 const FontList
& larger_font_list
= default_font_list
.DeriveWithSizeDelta(24);
1562 EXPECT_GT(larger_font_list
.GetHeight(), default_font_list
.GetHeight());
1564 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
1565 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1566 render_text
->SetFontList(default_font_list
);
1567 render_text
->SetText(cases
[i
]);
1569 const int height1
= render_text
->GetStringSize().height();
1570 EXPECT_GT(height1
, 0);
1572 // Check that setting the larger font increases the height.
1573 render_text
->SetFontList(larger_font_list
);
1574 const int height2
= render_text
->GetStringSize().height();
1575 EXPECT_GT(height2
, height1
);
1579 TEST_F(RenderTextTest
, GetBaselineSanity
) {
1580 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1581 render_text
->SetText(UTF8ToUTF16("Hello World"));
1582 const int baseline
= render_text
->GetBaseline();
1583 EXPECT_GT(baseline
, 0);
1586 TEST_F(RenderTextTest
, CursorBoundsInReplacementMode
) {
1587 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1588 render_text
->SetText(ASCIIToUTF16("abcdefg"));
1589 render_text
->SetDisplayRect(Rect(100, 17));
1590 SelectionModel
sel_b(1, CURSOR_FORWARD
);
1591 SelectionModel
sel_c(2, CURSOR_FORWARD
);
1592 Rect cursor_around_b
= render_text
->GetCursorBounds(sel_b
, false);
1593 Rect cursor_before_b
= render_text
->GetCursorBounds(sel_b
, true);
1594 Rect cursor_before_c
= render_text
->GetCursorBounds(sel_c
, true);
1595 EXPECT_EQ(cursor_around_b
.x(), cursor_before_b
.x());
1596 EXPECT_EQ(cursor_around_b
.right(), cursor_before_c
.x());
1599 TEST_F(RenderTextTest
, GetTextOffset
) {
1600 // The default horizontal text offset differs for LTR and RTL, and is only set
1601 // when the RenderText object is created. This test will check the default in
1602 // LTR mode, and the next test will check the RTL default.
1603 const bool was_rtl
= base::i18n::IsRTL();
1605 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1606 render_text
->SetText(ASCIIToUTF16("abcdefg"));
1607 render_text
->SetFontList(FontList("Arial, 13px"));
1609 // Set display area's size equal to the font size.
1610 const Size
font_size(render_text
->GetContentWidth(),
1611 render_text
->font_list().GetHeight());
1612 Rect
display_rect(font_size
);
1613 render_text
->SetDisplayRect(display_rect
);
1615 Vector2d offset
= render_text
->GetLineOffset(0);
1616 EXPECT_TRUE(offset
.IsZero());
1618 const int kEnlargementX
= 2;
1619 display_rect
.Inset(0, 0, -kEnlargementX
, 0);
1620 render_text
->SetDisplayRect(display_rect
);
1622 // Check the default horizontal alignment.
1623 offset
= render_text
->GetLineOffset(0);
1624 EXPECT_EQ(0, offset
.x());
1626 // Check explicitly setting the horizontal alignment.
1627 render_text
->SetHorizontalAlignment(ALIGN_LEFT
);
1628 offset
= render_text
->GetLineOffset(0);
1629 EXPECT_EQ(0, offset
.x());
1630 render_text
->SetHorizontalAlignment(ALIGN_CENTER
);
1631 offset
= render_text
->GetLineOffset(0);
1632 EXPECT_EQ(kEnlargementX
/ 2, offset
.x());
1633 render_text
->SetHorizontalAlignment(ALIGN_RIGHT
);
1634 offset
= render_text
->GetLineOffset(0);
1635 EXPECT_EQ(kEnlargementX
, offset
.x());
1637 // Check that text is vertically centered within taller display rects.
1638 const int kEnlargementY
= display_rect
.height();
1639 display_rect
.Inset(0, 0, 0, -kEnlargementY
);
1640 render_text
->SetDisplayRect(display_rect
);
1641 const Vector2d prev_offset
= render_text
->GetLineOffset(0);
1642 display_rect
.Inset(0, 0, 0, -2 * kEnlargementY
);
1643 render_text
->SetDisplayRect(display_rect
);
1644 offset
= render_text
->GetLineOffset(0);
1645 EXPECT_EQ(prev_offset
.y() + kEnlargementY
, offset
.y());
1650 TEST_F(RenderTextTest
, GetTextOffsetHorizontalDefaultInRTL
) {
1651 // This only checks the default horizontal alignment in RTL mode; all other
1652 // GetLineOffset(0) attributes are checked by the test above.
1653 const bool was_rtl
= base::i18n::IsRTL();
1655 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1656 render_text
->SetText(ASCIIToUTF16("abcdefg"));
1657 render_text
->SetFontList(FontList("Arial, 13px"));
1658 const int kEnlargement
= 2;
1659 const Size
font_size(render_text
->GetContentWidth() + kEnlargement
,
1660 render_text
->GetStringSize().height());
1661 Rect
display_rect(font_size
);
1662 render_text
->SetDisplayRect(display_rect
);
1663 Vector2d offset
= render_text
->GetLineOffset(0);
1664 EXPECT_EQ(kEnlargement
, offset
.x());
1668 TEST_F(RenderTextTest
, SetDisplayOffset
) {
1669 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1670 render_text
->SetText(ASCIIToUTF16("abcdefg"));
1671 render_text
->SetFontList(FontList("Arial, 13px"));
1673 const Size
font_size(render_text
->GetContentWidth(),
1674 render_text
->font_list().GetHeight());
1675 const int kEnlargement
= 10;
1677 // Set display width |kEnlargement| pixels greater than content width and test
1678 // different possible situations. In this case the only possible display
1680 Rect
display_rect(font_size
);
1681 display_rect
.Inset(0, 0, -kEnlargement
, 0);
1682 render_text
->SetDisplayRect(display_rect
);
1685 HorizontalAlignment alignment
;
1687 } small_content_cases
[] = {
1688 { ALIGN_LEFT
, -kEnlargement
},
1690 { ALIGN_LEFT
, kEnlargement
},
1691 { ALIGN_RIGHT
, -kEnlargement
},
1693 { ALIGN_RIGHT
, kEnlargement
},
1694 { ALIGN_CENTER
, -kEnlargement
},
1695 { ALIGN_CENTER
, 0 },
1696 { ALIGN_CENTER
, kEnlargement
},
1699 for (size_t i
= 0; i
< arraysize(small_content_cases
); i
++) {
1700 render_text
->SetHorizontalAlignment(small_content_cases
[i
].alignment
);
1701 render_text
->SetDisplayOffset(small_content_cases
[i
].offset
);
1702 EXPECT_EQ(0, render_text
->GetUpdatedDisplayOffset().x());
1705 // Set display width |kEnlargement| pixels less than content width and test
1706 // different possible situations.
1707 display_rect
= Rect(font_size
);
1708 display_rect
.Inset(0, 0, kEnlargement
, 0);
1709 render_text
->SetDisplayRect(display_rect
);
1712 HorizontalAlignment alignment
;
1714 int expected_offset
;
1715 } large_content_cases
[] = {
1716 // When text is left-aligned, display offset can be in range
1717 // [-kEnlargement, 0].
1718 { ALIGN_LEFT
, -2 * kEnlargement
, -kEnlargement
},
1719 { ALIGN_LEFT
, -kEnlargement
/ 2, -kEnlargement
/ 2 },
1720 { ALIGN_LEFT
, kEnlargement
, 0 },
1721 // When text is right-aligned, display offset can be in range
1722 // [0, kEnlargement].
1723 { ALIGN_RIGHT
, -kEnlargement
, 0 },
1724 { ALIGN_RIGHT
, kEnlargement
/ 2, kEnlargement
/ 2 },
1725 { ALIGN_RIGHT
, 2 * kEnlargement
, kEnlargement
},
1726 // When text is center-aligned, display offset can be in range
1727 // [-kEnlargement / 2 - 1, (kEnlargement - 1) / 2].
1728 { ALIGN_CENTER
, -kEnlargement
, -kEnlargement
/ 2 - 1 },
1729 { ALIGN_CENTER
, -kEnlargement
/ 4, -kEnlargement
/ 4 },
1730 { ALIGN_CENTER
, kEnlargement
/ 4, kEnlargement
/ 4 },
1731 { ALIGN_CENTER
, kEnlargement
, (kEnlargement
- 1) / 2 },
1734 for (size_t i
= 0; i
< arraysize(large_content_cases
); i
++) {
1735 render_text
->SetHorizontalAlignment(large_content_cases
[i
].alignment
);
1736 render_text
->SetDisplayOffset(large_content_cases
[i
].offset
);
1737 EXPECT_EQ(large_content_cases
[i
].expected_offset
,
1738 render_text
->GetUpdatedDisplayOffset().x());
1742 TEST_F(RenderTextTest
, SameFontForParentheses
) {
1744 const base::char16 left_char
;
1745 const base::char16 right_char
;
1746 } punctuation_pairs
[] = {
1752 base::string16 text
;
1755 { WideToUTF16(L
"Hello World(a)") },
1756 // English(English)English
1757 { WideToUTF16(L
"Hello World(a)Hello World") },
1759 // Japanese(English)
1760 { WideToUTF16(L
"\x6328\x62f6(a)") },
1761 // Japanese(English)Japanese
1762 { WideToUTF16(L
"\x6328\x62f6(a)\x6328\x62f6") },
1763 // English(Japanese)English
1764 { WideToUTF16(L
"Hello World(\x6328\x62f6)Hello World") },
1767 { WideToUTF16(L
"\x0915\x093f(a)") },
1768 // Hindi(English)Hindi
1769 { WideToUTF16(L
"\x0915\x093f(a)\x0915\x093f") },
1770 // English(Hindi)English
1771 { WideToUTF16(L
"Hello World(\x0915\x093f)Hello World") },
1774 { WideToUTF16(L
"\x05e0\x05b8(a)") },
1775 // Hebrew(English)Hebrew
1776 { WideToUTF16(L
"\x05e0\x05b8(a)\x05e0\x05b8") },
1777 // English(Hebrew)English
1778 { WideToUTF16(L
"Hello World(\x05e0\x05b8)Hello World") },
1781 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1782 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
1783 base::string16 text
= cases
[i
].text
;
1784 const size_t start_paren_char_index
= text
.find('(');
1785 ASSERT_NE(base::string16::npos
, start_paren_char_index
);
1786 const size_t end_paren_char_index
= text
.find(')');
1787 ASSERT_NE(base::string16::npos
, end_paren_char_index
);
1789 for (size_t j
= 0; j
< arraysize(punctuation_pairs
); ++j
) {
1790 text
[start_paren_char_index
] = punctuation_pairs
[j
].left_char
;
1791 text
[end_paren_char_index
] = punctuation_pairs
[j
].right_char
;
1792 render_text
->SetText(text
);
1794 const std::vector
<RenderText::FontSpan
> spans
=
1795 render_text
->GetFontSpansForTesting();
1797 int start_paren_span_index
= -1;
1798 int end_paren_span_index
= -1;
1799 for (size_t k
= 0; k
< spans
.size(); ++k
) {
1800 if (IndexInRange(spans
[k
].second
, start_paren_char_index
))
1801 start_paren_span_index
= k
;
1802 if (IndexInRange(spans
[k
].second
, end_paren_char_index
))
1803 end_paren_span_index
= k
;
1805 ASSERT_NE(-1, start_paren_span_index
);
1806 ASSERT_NE(-1, end_paren_span_index
);
1808 const Font
& start_font
= spans
[start_paren_span_index
].first
;
1809 const Font
& end_font
= spans
[end_paren_span_index
].first
;
1810 EXPECT_EQ(start_font
.GetFontName(), end_font
.GetFontName());
1811 EXPECT_EQ(start_font
.GetFontSize(), end_font
.GetFontSize());
1812 EXPECT_EQ(start_font
.GetStyle(), end_font
.GetStyle());
1817 // Make sure the caret width is always >=1 so that the correct
1818 // caret is drawn at high DPI. crbug.com/164100.
1819 TEST_F(RenderTextTest
, CaretWidth
) {
1820 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1821 render_text
->SetText(ASCIIToUTF16("abcdefg"));
1822 EXPECT_GE(render_text
->GetUpdatedCursorBounds().width(), 1);
1825 TEST_F(RenderTextTest
, SelectWord
) {
1826 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1827 render_text
->SetText(ASCIIToUTF16(" foo a.bc.d bar"));
1831 size_t selection_start
;
1832 size_t selection_end
;
1853 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
1854 render_text
->SetCursorPosition(cases
[i
].cursor
);
1855 render_text
->SelectWord();
1856 EXPECT_EQ(Range(cases
[i
].selection_start
, cases
[i
].selection_end
),
1857 render_text
->selection());
1861 // Make sure the last word is selected when the cursor is at text.length().
1862 TEST_F(RenderTextTest
, LastWordSelected
) {
1863 const std::string kTestURL1
= "http://www.google.com";
1864 const std::string kTestURL2
= "http://www.google.com/something/";
1866 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1868 render_text
->SetText(ASCIIToUTF16(kTestURL1
));
1869 render_text
->SetCursorPosition(kTestURL1
.length());
1870 render_text
->SelectWord();
1871 EXPECT_EQ(ASCIIToUTF16("com"), GetSelectedText(render_text
.get()));
1872 EXPECT_FALSE(render_text
->selection().is_reversed());
1874 render_text
->SetText(ASCIIToUTF16(kTestURL2
));
1875 render_text
->SetCursorPosition(kTestURL2
.length());
1876 render_text
->SelectWord();
1877 EXPECT_EQ(ASCIIToUTF16("/"), GetSelectedText(render_text
.get()));
1878 EXPECT_FALSE(render_text
->selection().is_reversed());
1881 // When given a non-empty selection, SelectWord should expand the selection to
1882 // nearest word boundaries.
1883 TEST_F(RenderTextTest
, SelectMultipleWords
) {
1884 const std::string kTestURL
= "http://www.google.com";
1886 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1888 render_text
->SetText(ASCIIToUTF16(kTestURL
));
1889 render_text
->SelectRange(Range(16, 20));
1890 render_text
->SelectWord();
1891 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text
.get()));
1892 EXPECT_FALSE(render_text
->selection().is_reversed());
1894 // SelectWord should preserve the selection direction.
1895 render_text
->SelectRange(Range(20, 16));
1896 render_text
->SelectWord();
1897 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text
.get()));
1898 EXPECT_TRUE(render_text
->selection().is_reversed());
1901 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
1902 // does not implement this yet. http://crbug.com/131618
1903 #if !defined(OS_MACOSX)
1904 TEST_F(RenderTextTest
, DisplayRectShowsCursorLTR
) {
1905 ASSERT_FALSE(base::i18n::IsRTL());
1906 ASSERT_FALSE(base::i18n::ICUIsRTL());
1908 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1909 render_text
->SetText(WideToUTF16(L
"abcdefghijklmnopqrstuvwxzyabcdefg"));
1910 render_text
->MoveCursorTo(SelectionModel(render_text
->text().length(),
1912 int width
= render_text
->GetStringSize().width();
1913 ASSERT_GT(width
, 10);
1915 // Ensure that the cursor is placed at the width of its preceding text.
1916 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1917 EXPECT_EQ(width
, render_text
->GetUpdatedCursorBounds().x());
1919 // Ensure that shrinking the display rectangle keeps the cursor in view.
1920 render_text
->SetDisplayRect(Rect(width
- 10, 1));
1921 EXPECT_EQ(render_text
->display_rect().width(),
1922 render_text
->GetUpdatedCursorBounds().right());
1924 // Ensure that the text will pan to fill its expanding display rectangle.
1925 render_text
->SetDisplayRect(Rect(width
- 5, 1));
1926 EXPECT_EQ(render_text
->display_rect().width(),
1927 render_text
->GetUpdatedCursorBounds().right());
1929 // Ensure that a sufficiently large display rectangle shows all the text.
1930 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1931 EXPECT_EQ(width
, render_text
->GetUpdatedCursorBounds().x());
1933 // Repeat the test with RTL text.
1934 render_text
->SetText(WideToUTF16(L
"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1935 L
"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1936 render_text
->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD
));
1937 width
= render_text
->GetStringSize().width();
1938 ASSERT_GT(width
, 10);
1940 // Ensure that the cursor is placed at the width of its preceding text.
1941 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1942 EXPECT_EQ(width
, render_text
->GetUpdatedCursorBounds().x());
1944 // Ensure that shrinking the display rectangle keeps the cursor in view.
1945 render_text
->SetDisplayRect(Rect(width
- 10, 1));
1946 EXPECT_EQ(render_text
->display_rect().width(),
1947 render_text
->GetUpdatedCursorBounds().right());
1949 // Ensure that the text will pan to fill its expanding display rectangle.
1950 render_text
->SetDisplayRect(Rect(width
- 5, 1));
1951 EXPECT_EQ(render_text
->display_rect().width(),
1952 render_text
->GetUpdatedCursorBounds().right());
1954 // Ensure that a sufficiently large display rectangle shows all the text.
1955 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1956 EXPECT_EQ(width
, render_text
->GetUpdatedCursorBounds().x());
1959 TEST_F(RenderTextTest
, DisplayRectShowsCursorRTL
) {
1960 // Set the application default text direction to RTL.
1961 const bool was_rtl
= base::i18n::IsRTL();
1964 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
1965 render_text
->SetText(WideToUTF16(L
"abcdefghijklmnopqrstuvwxzyabcdefg"));
1966 render_text
->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD
));
1967 int width
= render_text
->GetStringSize().width();
1968 ASSERT_GT(width
, 10);
1970 // Ensure that the cursor is placed at the width of its preceding text.
1971 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1972 EXPECT_EQ(render_text
->display_rect().width() - width
- 1,
1973 render_text
->GetUpdatedCursorBounds().x());
1975 // Ensure that shrinking the display rectangle keeps the cursor in view.
1976 render_text
->SetDisplayRect(Rect(width
- 10, 1));
1977 EXPECT_EQ(0, render_text
->GetUpdatedCursorBounds().x());
1979 // Ensure that the text will pan to fill its expanding display rectangle.
1980 render_text
->SetDisplayRect(Rect(width
- 5, 1));
1981 EXPECT_EQ(0, render_text
->GetUpdatedCursorBounds().x());
1983 // Ensure that a sufficiently large display rectangle shows all the text.
1984 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1985 EXPECT_EQ(render_text
->display_rect().width() - width
- 1,
1986 render_text
->GetUpdatedCursorBounds().x());
1988 // Repeat the test with RTL text.
1989 render_text
->SetText(WideToUTF16(L
"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1990 L
"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1991 render_text
->MoveCursorTo(SelectionModel(render_text
->text().length(),
1993 width
= render_text
->GetStringSize().width();
1994 ASSERT_GT(width
, 10);
1996 // Ensure that the cursor is placed at the width of its preceding text.
1997 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
1998 EXPECT_EQ(render_text
->display_rect().width() - width
- 1,
1999 render_text
->GetUpdatedCursorBounds().x());
2001 // Ensure that shrinking the display rectangle keeps the cursor in view.
2002 render_text
->SetDisplayRect(Rect(width
- 10, 1));
2003 EXPECT_EQ(0, render_text
->GetUpdatedCursorBounds().x());
2005 // Ensure that the text will pan to fill its expanding display rectangle.
2006 render_text
->SetDisplayRect(Rect(width
- 5, 1));
2007 EXPECT_EQ(0, render_text
->GetUpdatedCursorBounds().x());
2009 // Ensure that a sufficiently large display rectangle shows all the text.
2010 render_text
->SetDisplayRect(Rect(width
+ 10, 1));
2011 EXPECT_EQ(render_text
->display_rect().width() - width
- 1,
2012 render_text
->GetUpdatedCursorBounds().x());
2014 // Reset the application default text direction to LTR.
2016 EXPECT_EQ(was_rtl
, base::i18n::IsRTL());
2018 #endif // !defined(OS_MACOSX)
2020 // Changing colors between or inside ligated glyphs should not break shaping.
2021 TEST_F(RenderTextTest
, SelectionKeepsLigatures
) {
2022 const wchar_t* kTestStrings
[] = { L
"\x644\x623", L
"\x633\x627" };
2023 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
2024 render_text
->set_selection_color(SK_ColorRED
);
2027 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2028 render_text
->SetText(WideToUTF16(kTestStrings
[i
]));
2029 const int expected_width
= render_text
->GetStringSize().width();
2030 render_text
->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD
));
2031 EXPECT_EQ(expected_width
, render_text
->GetStringSize().width());
2032 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119
2033 render_text
->Draw(&canvas
);
2034 render_text
->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD
));
2038 // Ensure strings wrap onto multiple lines for a small available width.
2039 TEST_F(RenderTextTest
, Multiline_MinWidth
) {
2040 const wchar_t* kTestStrings
[] = { kWeak
, kLtr
, kLtrRtl
, kLtrRtlLtr
, kRtl
,
2041 kRtlLtr
, kRtlLtrRtl
};
2043 RenderTextHarfBuzz render_text
;
2044 render_text
.SetDisplayRect(Rect(1, 1000));
2045 render_text
.SetMultiline(true);
2048 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2049 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"]", i
));
2050 render_text
.SetText(WideToUTF16(kTestStrings
[i
]));
2051 render_text
.Draw(&canvas
);
2052 EXPECT_GT(render_text
.lines_
.size(), 1U);
2056 // Ensure strings wrap onto multiple lines for a normal available width.
2057 TEST_F(RenderTextTest
, Multiline_NormalWidth
) {
2059 const wchar_t* const text
;
2060 const Range first_line_char_range
;
2061 const Range second_line_char_range
;
2063 } kTestStrings
[] = {
2064 { L
"abc defg hijkl", Range(0, 9), Range(9, 14), true },
2065 { L
"qwertyzxcvbn", Range(0, 10), Range(10, 12), true },
2066 { L
"\x0627\x0644\x0644\x063A\x0629 "
2067 L
"\x0627\x0644\x0639\x0631\x0628\x064A\x0629",
2068 Range(0, 6), Range(6, 13), false },
2069 { L
"\x062A\x0641\x0627\x062D\x05EA\x05E4\x05D5\x05D6\x05D9"
2070 L
"\x05DA\x05DB\x05DD", Range(0, 4), Range(4, 12), false }
2073 RenderTextHarfBuzz render_text
;
2074 // Specify the fixed width for characters to suppress the possible variations
2075 // of linebreak results.
2076 render_text
.set_glyph_width_for_test(5);
2077 render_text
.SetDisplayRect(Rect(50, 1000));
2078 render_text
.SetMultiline(true);
2079 render_text
.SetHorizontalAlignment(ALIGN_TO_HEAD
);
2082 TestSkiaTextRenderer
renderer(&canvas
);
2084 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2085 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"]", i
));
2086 render_text
.SetText(WideToUTF16(kTestStrings
[i
].text
));
2087 render_text
.EnsureLayout();
2088 render_text
.DrawVisualTextInternal(&renderer
);
2090 ASSERT_EQ(2U, render_text
.lines_
.size());
2091 ASSERT_EQ(1U, render_text
.lines_
[0].segments
.size());
2092 EXPECT_EQ(kTestStrings
[i
].first_line_char_range
,
2093 render_text
.lines_
[0].segments
[0].char_range
);
2094 ASSERT_EQ(1U, render_text
.lines_
[1].segments
.size());
2095 EXPECT_EQ(kTestStrings
[i
].second_line_char_range
,
2096 render_text
.lines_
[1].segments
[0].char_range
);
2098 std::vector
<TestSkiaTextRenderer::TextLog
> text_log
;
2099 renderer
.GetTextLogAndReset(&text_log
);
2100 ASSERT_EQ(2U, text_log
.size());
2101 // NOTE: this expectation compares the character length and glyph counts,
2102 // which isn't always equal. This is okay only because all the test
2103 // strings are simple (like, no compound characters nor UTF16-surrogate
2104 // pairs). Be careful in case more complicated test strings are added.
2105 EXPECT_EQ(kTestStrings
[i
].first_line_char_range
.length(),
2106 text_log
[0].glyph_count
);
2107 EXPECT_EQ(kTestStrings
[i
].second_line_char_range
.length(),
2108 text_log
[1].glyph_count
);
2109 EXPECT_LT(text_log
[0].origin
.y(), text_log
[1].origin
.y());
2110 if (kTestStrings
[i
].is_ltr
) {
2111 EXPECT_EQ(0, text_log
[0].origin
.x());
2112 EXPECT_EQ(0, text_log
[1].origin
.x());
2114 EXPECT_LT(0, text_log
[0].origin
.x());
2115 EXPECT_LT(0, text_log
[1].origin
.x());
2120 // Ensure strings don't wrap onto multiple lines for a sufficient available
2122 TEST_F(RenderTextTest
, Multiline_SufficientWidth
) {
2123 const wchar_t* kTestStrings
[] = { L
"", L
" ", L
".", L
" . ", L
"abc", L
"a b c",
2124 L
"\x62E\x628\x632", L
"\x62E \x628 \x632" };
2126 RenderTextHarfBuzz render_text
;
2127 render_text
.SetDisplayRect(Rect(1000, 1000));
2128 render_text
.SetMultiline(true);
2131 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2132 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"]", i
));
2133 render_text
.SetText(WideToUTF16(kTestStrings
[i
]));
2134 render_text
.Draw(&canvas
);
2135 EXPECT_EQ(1U, render_text
.lines_
.size());
2139 TEST_F(RenderTextTest
, Multiline_Newline
) {
2141 const wchar_t* const text
;
2142 const size_t lines_count
;
2143 // Ranges of the characters on each line preceding the newline.
2144 const Range line_char_ranges
[3];
2145 } kTestStrings
[] = {
2146 {L
"abc\ndef", 2ul, { Range(0, 3), Range(4, 7), Range::InvalidRange() } },
2147 {L
"a \n b ", 2ul, { Range(0, 2), Range(3, 6), Range::InvalidRange() } },
2148 {L
"ab\n", 2ul, { Range(0, 2), Range(), Range::InvalidRange() } },
2149 {L
"a\n\nb", 3ul, { Range(0, 1), Range(), Range(3, 4) } },
2150 {L
"\nab", 2ul, { Range(), Range(1, 3), Range::InvalidRange() } },
2151 {L
"\n", 2ul, { Range(), Range(), Range::InvalidRange() } },
2154 RenderTextHarfBuzz render_text
;
2155 render_text
.SetDisplayRect(Rect(200, 1000));
2156 render_text
.SetMultiline(true);
2159 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2160 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"]", i
));
2161 render_text
.SetText(WideToUTF16(kTestStrings
[i
].text
));
2162 render_text
.Draw(&canvas
);
2163 EXPECT_EQ(kTestStrings
[i
].lines_count
, render_text
.lines_
.size());
2164 if (kTestStrings
[i
].lines_count
!= render_text
.lines_
.size())
2167 for (size_t j
= 0; j
< kTestStrings
[i
].lines_count
; ++j
) {
2168 SCOPED_TRACE(base::StringPrintf("Line %" PRIuS
"", j
));
2169 VerifyLineSegments(kTestStrings
[i
].line_char_ranges
[j
],
2170 render_text
.lines_
[j
].segments
);
2175 // Make sure that multiline mode ignores elide behavior.
2176 TEST_F(RenderTextTest
, Multiline_IgnoreElide
) {
2177 const wchar_t kTestString
[] =
2178 L
"very very very long string xxxxxxxxxxxxxxxxxxxxxxxxxx";
2179 const wchar_t kEllipsis
[] = L
"\x2026";
2181 RenderTextHarfBuzz render_text
;
2182 render_text
.SetElideBehavior(ELIDE_TAIL
);
2183 render_text
.SetDisplayRect(Rect(20, 1000));
2184 render_text
.SetText(base::WideToUTF16(kTestString
));
2185 EXPECT_NE(base::string16::npos
,
2186 render_text
.GetDisplayText().find(base::WideToUTF16(kEllipsis
)));
2188 render_text
.SetMultiline(true);
2189 EXPECT_EQ(base::string16::npos
,
2190 render_text
.GetDisplayText().find(base::WideToUTF16(kEllipsis
)));
2193 TEST_F(RenderTextTest
, Multiline_NewlineCharacterReplacement
) {
2194 const wchar_t* kTestStrings
[] = {
2195 L
"abc\ndef", L
"a \n b ", L
"ab\n", L
"a\n\nb", L
"\nab", L
"\n",
2198 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2199 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"]", i
));
2200 RenderTextHarfBuzz render_text
;
2201 render_text
.SetDisplayRect(Rect(200, 1000));
2202 render_text
.SetText(WideToUTF16(kTestStrings
[i
]));
2204 base::string16 display_text
= render_text
.GetDisplayText();
2205 // If RenderText is not multiline, the newline characters are replaced
2206 // by symbols, therefore the character should be changed.
2207 EXPECT_NE(WideToUTF16(kTestStrings
[i
]), render_text
.GetDisplayText());
2209 // Setting multiline will fix this, the newline characters will be back
2210 // to the original text.
2211 render_text
.SetMultiline(true);
2212 EXPECT_EQ(WideToUTF16(kTestStrings
[i
]), render_text
.GetDisplayText());
2216 // Ensure horizontal alignment works in multiline mode.
2217 TEST_F(RenderTextTest
, Multiline_HorizontalAlignment
) {
2219 const wchar_t* const text
;
2220 const gfx::HorizontalAlignment alignment
;
2221 } kTestStrings
[] = {
2222 { L
"abcdefghij\nhijkl", gfx::ALIGN_LEFT
},
2223 { L
"nhijkl\nabcdefghij", gfx::ALIGN_LEFT
},
2224 // hebrew, 2nd line shorter
2225 { L
"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7\n\x5d0\x5d1\x5d2\x5d3",
2227 // hebrew, 2nd line longer
2228 { L
"\x5d0\x5d1\x5d2\x5d3\n\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7",
2230 // arabic, 2nd line shorter
2231 { L
"\x62a\x62b\x62c\x62d\x62e\x62f\x630\n\x660\x661\x662\x663\x664",
2233 // arabic, 2nd line longer
2234 { L
"\x660\x661\x662\x663\x664\n\x62a\x62b\x62c\x62d\x62e\x62f\x630",
2237 const int kGlyphSize
= 5;
2238 RenderTextHarfBuzz render_text
;
2239 render_text
.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD
);
2240 render_text
.set_glyph_width_for_test(kGlyphSize
);
2241 render_text
.SetDisplayRect(Rect(100, 1000));
2242 render_text
.SetMultiline(true);
2245 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2246 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"] %ls", i
,
2247 kTestStrings
[i
].text
));
2248 render_text
.SetText(WideToUTF16(kTestStrings
[i
].text
));
2249 render_text
.Draw(&canvas
);
2250 ASSERT_LE(2u, render_text
.lines().size());
2251 if (kTestStrings
[i
].alignment
== gfx::ALIGN_LEFT
) {
2252 EXPECT_EQ(0, render_text
.GetAlignmentOffset(0).x());
2253 EXPECT_EQ(0, render_text
.GetAlignmentOffset(1).x());
2255 std::vector
<base::string16
> lines
;
2256 base::SplitString(base::WideToUTF16(kTestStrings
[i
].text
), '\n', &lines
);
2257 ASSERT_EQ(2u, lines
.size());
2258 int difference
= (lines
[0].length() - lines
[1].length()) * kGlyphSize
;
2259 EXPECT_EQ(render_text
.GetAlignmentOffset(0).x() + difference
,
2260 render_text
.GetAlignmentOffset(1).x());
2265 TEST_F(RenderTextTest
, NewlineWithoutMultilineFlag
) {
2266 const wchar_t* kTestStrings
[] = {
2267 L
"abc\ndef", L
"a \n b ", L
"ab\n", L
"a\n\nb", L
"\nab", L
"\n",
2270 RenderTextHarfBuzz render_text
;
2271 render_text
.SetDisplayRect(Rect(200, 1000));
2274 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2275 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"]", i
));
2276 render_text
.SetText(WideToUTF16(kTestStrings
[i
]));
2277 render_text
.Draw(&canvas
);
2279 EXPECT_EQ(1U, render_text
.lines_
.size());
2283 // Make sure the horizontal positions of runs in a line (left-to-right for
2284 // LTR languages and right-to-left for RTL languages).
2285 TEST_F(RenderTextTest
, HarfBuzz_HorizontalPositions
) {
2287 const wchar_t* const text
;
2288 const Range first_run_char_range
;
2289 const Range second_run_char_range
;
2291 } kTestStrings
[] = {
2292 { L
"abc\x3042\x3044\x3046\x3048\x304A", Range(0, 3), Range(3, 8), false },
2293 { L
"\x062A\x0641\x0627\x062D"
2294 L
"\x05EA\x05E4\x05D5\x05D6\x05D9\x05DA\x05DB\x05DD",
2295 Range(0, 4), Range(4, 12), true },
2298 RenderTextHarfBuzz render_text
;
2300 TestSkiaTextRenderer
renderer(&canvas
);
2302 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2303 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS
"]", i
));
2304 render_text
.SetText(WideToUTF16(kTestStrings
[i
].text
));
2306 render_text
.EnsureLayout();
2307 const internal::TextRunList
* run_list
= render_text
.GetRunList();
2308 ASSERT_EQ(2U, run_list
->runs().size());
2309 EXPECT_EQ(kTestStrings
[i
].first_run_char_range
, run_list
->runs()[0]->range
);
2310 EXPECT_EQ(kTestStrings
[i
].second_run_char_range
,
2311 run_list
->runs()[1]->range
);
2312 // If it's RTL, the visual order is reversed.
2313 if (kTestStrings
[i
].is_rtl
) {
2314 EXPECT_EQ(1U, run_list
->logical_to_visual(0));
2315 EXPECT_EQ(0U, run_list
->logical_to_visual(1));
2317 EXPECT_EQ(0U, run_list
->logical_to_visual(0));
2318 EXPECT_EQ(1U, run_list
->logical_to_visual(1));
2321 render_text
.DrawVisualTextInternal(&renderer
);
2323 std::vector
<TestSkiaTextRenderer::TextLog
> text_log
;
2324 renderer
.GetTextLogAndReset(&text_log
);
2326 EXPECT_EQ(2U, text_log
.size());
2328 // Verifies the DrawText happens in the visual order and left-to-right.
2329 // If the text is RTL, the logically first run should be drawn at last.
2330 EXPECT_EQ(run_list
->runs()[run_list
->logical_to_visual(0)]->glyph_count
,
2331 text_log
[0].glyph_count
);
2332 EXPECT_EQ(run_list
->runs()[run_list
->logical_to_visual(1)]->glyph_count
,
2333 text_log
[1].glyph_count
);
2334 EXPECT_LT(text_log
[0].origin
.x(), text_log
[1].origin
.x());
2338 // Test TextRunHarfBuzz's cluster finding logic.
2339 TEST_F(RenderTextTest
, HarfBuzz_Clusters
) {
2341 uint32 glyph_to_char
[4];
2346 { // From string "A B C D" to glyphs "a b c d".
2348 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2349 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2352 { // From string "A B C D" to glyphs "d c b a".
2354 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2355 { Range(3, 4), Range(2, 3), Range(1, 2), Range(0, 1) },
2358 { // From string "A B C D" to glyphs "ab c c d".
2360 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2361 { Range(0, 1), Range(0, 1), Range(1, 3), Range(3, 4) },
2364 { // From string "A B C D" to glyphs "d c c ba".
2366 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2367 { Range(3, 4), Range(3, 4), Range(1, 3), Range(0, 1) },
2372 internal::TextRunHarfBuzz run
;
2373 run
.range
= Range(0, 4);
2374 run
.glyph_count
= 4;
2375 run
.glyph_to_char
.resize(4);
2377 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
2378 std::copy(cases
[i
].glyph_to_char
, cases
[i
].glyph_to_char
+ 4,
2379 run
.glyph_to_char
.begin());
2380 run
.is_rtl
= cases
[i
].is_rtl
;
2382 for (size_t j
= 0; j
< 4; ++j
) {
2383 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS
", char %" PRIuS
, i
, j
));
2386 run
.GetClusterAt(j
, &chars
, &glyphs
);
2387 EXPECT_EQ(cases
[i
].chars
[j
], chars
);
2388 EXPECT_EQ(cases
[i
].glyphs
[j
], glyphs
);
2389 EXPECT_EQ(cases
[i
].glyphs
[j
], run
.CharRangeToGlyphRange(chars
));
2394 // Ensure that graphemes with multiple code points do not get split.
2395 TEST_F(RenderTextTest
, HarfBuzz_SubglyphGraphemeCases
) {
2396 const wchar_t* cases
[] = {
2397 // "A" with a combining umlaut, followed by a "B".
2399 // Devanagari biconsonantal conjunct "ki", followed by an "a".
2400 L
"\x0915\x093f\x0905",
2401 // Thai consonant and vowel pair "cho chan" + "sara am", followed by Thai
2403 L
"\x0e08\x0e33\x0E50",
2406 RenderTextHarfBuzz render_text
;
2408 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
2409 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS
, i
));
2411 base::string16 text
= WideToUTF16(cases
[i
]);
2412 render_text
.SetText(text
);
2413 render_text
.EnsureLayout();
2414 internal::TextRunList
* run_list
= render_text
.GetRunList();
2415 ASSERT_EQ(1U, run_list
->size());
2416 internal::TextRunHarfBuzz
* run
= run_list
->runs()[0];
2418 base::i18n::BreakIterator
* iter
= render_text
.grapheme_iterator_
.get();
2419 auto first_grapheme_bounds
= run
->GetGraphemeBounds(iter
, 0);
2420 EXPECT_EQ(first_grapheme_bounds
, run
->GetGraphemeBounds(iter
, 1));
2421 auto second_grapheme_bounds
= run
->GetGraphemeBounds(iter
, 2);
2422 EXPECT_EQ(first_grapheme_bounds
.end(), second_grapheme_bounds
.start());
2426 // Test the partition of a multi-grapheme cluster into grapheme ranges.
2427 TEST_F(RenderTextTest
, HarfBuzz_SubglyphGraphemePartition
) {
2429 uint32 glyph_to_char
[2];
2433 { // From string "A B C D" to glyphs "a bcd".
2435 { Range(0, 10), Range(10, 13), Range(13, 17), Range(17, 20) },
2438 { // From string "A B C D" to glyphs "ab cd".
2440 { Range(0, 5), Range(5, 10), Range(10, 15), Range(15, 20) },
2443 { // From string "A B C D" to glyphs "dcb a".
2445 { Range(10, 20), Range(7, 10), Range(3, 7), Range(0, 3) },
2448 { // From string "A B C D" to glyphs "dc ba".
2450 { Range(15, 20), Range(10, 15), Range(5, 10), Range(0, 5) },
2455 internal::TextRunHarfBuzz run
;
2456 run
.range
= Range(0, 4);
2457 run
.glyph_count
= 2;
2458 run
.glyph_to_char
.resize(2);
2459 run
.positions
.reset(new SkPoint
[4]);
2462 const base::string16 kString
= ASCIIToUTF16("abcd");
2463 scoped_ptr
<base::i18n::BreakIterator
> iter(new base::i18n::BreakIterator(
2464 kString
, base::i18n::BreakIterator::BREAK_CHARACTER
));
2465 ASSERT_TRUE(iter
->Init());
2467 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
2468 std::copy(cases
[i
].glyph_to_char
, cases
[i
].glyph_to_char
+ 2,
2469 run
.glyph_to_char
.begin());
2470 run
.is_rtl
= cases
[i
].is_rtl
;
2471 for (int j
= 0; j
< 2; ++j
)
2472 run
.positions
[j
].set(j
* 10, 0);
2474 for (size_t j
= 0; j
< 4; ++j
) {
2475 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS
", char %" PRIuS
, i
, j
));
2476 EXPECT_EQ(cases
[i
].bounds
[j
],
2477 internal::RoundRangeF(run
.GetGraphemeBounds(iter
.get(), j
)));
2482 TEST_F(RenderTextTest
, HarfBuzz_RunDirection
) {
2483 RenderTextHarfBuzz render_text
;
2484 const base::string16 mixed
=
2485 WideToUTF16(L
"\x05D0\x05D1" L
"1234" L
"\x05D2\x05D3");
2486 render_text
.SetText(mixed
);
2487 render_text
.EnsureLayout();
2488 internal::TextRunList
* run_list
= render_text
.GetRunList();
2489 ASSERT_EQ(3U, run_list
->size());
2490 EXPECT_TRUE(run_list
->runs()[0]->is_rtl
);
2491 EXPECT_FALSE(run_list
->runs()[1]->is_rtl
);
2492 EXPECT_TRUE(run_list
->runs()[2]->is_rtl
);
2495 TEST_F(RenderTextTest
, HarfBuzz_BreakRunsByUnicodeBlocks
) {
2496 RenderTextHarfBuzz render_text
;
2498 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2499 render_text
.SetText(WideToUTF16(L
"x\x25B6y"));
2500 render_text
.EnsureLayout();
2501 internal::TextRunList
* run_list
= render_text
.GetRunList();
2502 ASSERT_EQ(3U, run_list
->size());
2503 EXPECT_EQ(Range(0, 1), run_list
->runs()[0]->range
);
2504 EXPECT_EQ(Range(1, 2), run_list
->runs()[1]->range
);
2505 EXPECT_EQ(Range(2, 3), run_list
->runs()[2]->range
);
2507 render_text
.SetText(WideToUTF16(L
"x \x25B6 y"));
2508 render_text
.EnsureLayout();
2509 run_list
= render_text
.GetRunList();
2510 ASSERT_EQ(3U, run_list
->size());
2511 EXPECT_EQ(Range(0, 2), run_list
->runs()[0]->range
);
2512 EXPECT_EQ(Range(2, 3), run_list
->runs()[1]->range
);
2513 EXPECT_EQ(Range(3, 5), run_list
->runs()[2]->range
);
2516 TEST_F(RenderTextTest
, HarfBuzz_BreakRunsByEmoji
) {
2517 RenderTextHarfBuzz render_text
;
2519 // \xF0\x9F\x98\x81 (U+1F601) is smile icon emoji. \xE2\x9C\xA8 (U+2728) is
2520 // a sparkle icon. Both can be drawn with color emoji fonts, so runs should be
2521 // separated. See crbug.com/448909
2522 render_text
.SetText(UTF8ToUTF16("x\xF0\x9F\x98\x81y\xE2\x9C\xA8"));
2523 render_text
.EnsureLayout();
2524 internal::TextRunList
* run_list
= render_text
.GetRunList();
2525 ASSERT_EQ(4U, run_list
->size());
2526 EXPECT_EQ(Range(0, 1), run_list
->runs()[0]->range
);
2527 // The length is 2 since U+1F601 is represented as a surrogate pair in UTF16.
2528 EXPECT_EQ(Range(1, 3), run_list
->runs()[1]->range
);
2529 EXPECT_EQ(Range(3, 4), run_list
->runs()[2]->range
);
2530 EXPECT_EQ(Range(4, 5), run_list
->runs()[3]->range
);
2533 TEST_F(RenderTextTest
, GlyphBounds
) {
2534 const wchar_t* kTestStrings
[] = {
2535 L
"asdf 1234 qwer", L
"\x0647\x0654", L
"\x0645\x0631\x062D\x0628\x0627"
2537 scoped_ptr
<RenderText
> render_text(new RenderTextHarfBuzz
);
2539 for (size_t i
= 0; i
< arraysize(kTestStrings
); ++i
) {
2540 render_text
->SetText(WideToUTF16(kTestStrings
[i
]));
2541 render_text
->EnsureLayout();
2543 for (size_t j
= 0; j
< render_text
->text().length(); ++j
)
2544 EXPECT_FALSE(render_text
->GetGlyphBounds(j
).is_empty());
2548 // Ensure that shaping with a non-existent font does not cause a crash.
2549 TEST_F(RenderTextTest
, HarfBuzz_NonExistentFont
) {
2550 RenderTextHarfBuzz render_text
;
2551 render_text
.SetText(ASCIIToUTF16("test"));
2552 render_text
.EnsureLayout();
2553 internal::TextRunList
* run_list
= render_text
.GetRunList();
2554 ASSERT_EQ(1U, run_list
->size());
2555 internal::TextRunHarfBuzz
* run
= run_list
->runs()[0];
2556 render_text
.ShapeRunWithFont(
2557 render_text
.text(), "TheFontThatDoesntExist", FontRenderParams(), run
);
2560 // Ensure an empty run returns sane values to queries.
2561 TEST_F(RenderTextTest
, HarfBuzz_EmptyRun
) {
2562 internal::TextRunHarfBuzz run
;
2563 const base::string16 kString
= ASCIIToUTF16("abcdefgh");
2564 scoped_ptr
<base::i18n::BreakIterator
> iter(new base::i18n::BreakIterator(
2565 kString
, base::i18n::BreakIterator::BREAK_CHARACTER
));
2566 ASSERT_TRUE(iter
->Init());
2568 run
.range
= Range(3, 8);
2569 run
.glyph_count
= 0;
2570 EXPECT_EQ(Range(0, 0), run
.CharRangeToGlyphRange(Range(4, 5)));
2571 EXPECT_EQ(Range(0, 0),
2572 internal::RoundRangeF(run
.GetGraphemeBounds(iter
.get(), 4)));
2575 run
.GetClusterAt(4, &chars
, &glyphs
);
2576 EXPECT_EQ(Range(3, 8), chars
);
2577 EXPECT_EQ(Range(0, 0), glyphs
);
2580 // Ensure a string fits in a display rect with a width equal to the string's.
2581 TEST_F(RenderTextTest
, StringFitsOwnWidth
) {
2582 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
2583 const base::string16 kString
= ASCIIToUTF16("www.example.com");
2585 render_text
->SetText(kString
);
2586 render_text
->ApplyStyle(BOLD
, true, Range(0, 3));
2587 render_text
->SetElideBehavior(ELIDE_TAIL
);
2589 render_text
->SetDisplayRect(Rect(0, 0, 500, 100));
2590 EXPECT_EQ(kString
, render_text
->GetDisplayText());
2591 render_text
->SetDisplayRect(Rect(0, 0, render_text
->GetContentWidth(), 100));
2592 EXPECT_EQ(kString
, render_text
->GetDisplayText());
2595 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184
2596 #if !defined(OS_WIN)
2597 // Ensure that RenderText examines all of the fonts in its FontList before
2598 // falling back to other fonts.
2599 TEST_F(RenderTextTest
, HarfBuzz_FontListFallback
) {
2600 // Double-check that the requested fonts are present.
2601 FontList
font_list("Arial, Symbol, 12px");
2602 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
2603 ASSERT_EQ(2u, fonts
.size());
2605 base::StringToLowerASCII(fonts
[0].GetActualFontNameForTesting()));
2607 base::StringToLowerASCII(fonts
[1].GetActualFontNameForTesting()));
2609 // "⊕" (CIRCLED PLUS) should be rendered with Symbol rather than falling back
2610 // to some other font that's present on the system.
2611 RenderTextHarfBuzz render_text
;
2612 render_text
.SetFontList(font_list
);
2613 render_text
.SetText(UTF8ToUTF16("\xE2\x8A\x95"));
2614 const std::vector
<RenderText::FontSpan
> spans
=
2615 render_text
.GetFontSpansForTesting();
2616 ASSERT_EQ(static_cast<size_t>(1), spans
.size());
2617 EXPECT_EQ("Symbol", spans
[0].first
.GetFontName());
2619 #endif // !defined(OS_WIN)
2621 // Ensure that the fallback fonts of the Uniscribe font are tried for shaping.
2623 TEST_F(RenderTextTest
, HarfBuzz_UniscribeFallback
) {
2624 RenderTextHarfBuzz render_text
;
2625 PlatformFontWin
* font_win
= new PlatformFontWin("Meiryo", 12);
2626 // Japanese name for Meiryo. This name won't be found in the system's linked
2627 // fonts, forcing RTHB to try the Uniscribe font and its fallbacks.
2628 font_win
->font_ref_
->font_name_
= WideToUTF8(L
"\x30e1\x30a4\x30ea\x30aa");
2629 FontList
font_list((Font(font_win
)));
2631 render_text
.SetFontList(font_list
);
2632 // Korean character "han".
2633 render_text
.SetText(WideToUTF16(L
"\xd55c"));
2634 render_text
.EnsureLayout();
2635 internal::TextRunList
* run_list
= render_text
.GetRunList();
2636 ASSERT_EQ(1U, run_list
->size());
2637 EXPECT_EQ(0U, run_list
->runs()[0]->CountMissingGlyphs());
2639 #endif // defined(OS_WIN)
2641 // Ensure that the fallback fonts offered by gfx::GetFallbackFontFamilies() are
2642 // tried. Note this test assumes the font "Arial" doesn't provide a unicode
2643 // glyph for a particular character, and that there exists a system fallback
2645 #if defined(OS_WIN) || defined(OS_MACOSX)
2646 TEST_F(RenderTextTest
, HarfBuzz_UnicodeFallback
) {
2647 RenderTextHarfBuzz render_text
;
2648 render_text
.SetFontList(FontList("Arial, 12px"));
2650 // Korean character "han".
2651 render_text
.SetText(WideToUTF16(L
"\xd55c"));
2652 render_text
.EnsureLayout();
2653 internal::TextRunList
* run_list
= render_text
.GetRunList();
2654 ASSERT_EQ(1U, run_list
->size());
2655 EXPECT_EQ(0U, run_list
->runs()[0]->CountMissingGlyphs());
2657 #endif // defined(OS_WIN) || defined(OS_MACOSX)
2659 // Ensure that the width reported by RenderText is sufficient for drawing. Draws
2660 // to a canvas and checks if any pixel beyond the bounding rectangle is colored.
2661 TEST_F(RenderTextTest
, TextDoesntClip
) {
2662 const wchar_t* kTestStrings
[] = {
2664 // TODO(dschuyler): Underscores draw outside GetStringSize;
2665 // crbug.com/459812. This appears to be a preexisting issue that wasn't
2666 // revealed by the prior unit tests.
2671 // TODO(dschuyler): A-Ring draws outside GetStringSize; crbug.com/459812.
2672 // L"g\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5",
2673 L
"\x0647\x0654\x0647\x0654\x0647\x0654\x0647\x0654\x0645\x0631\x062D"
2675 const Size
kCanvasSize(300, 50);
2676 const int kTestSize
= 10;
2678 skia::RefPtr
<SkSurface
> surface
= skia::AdoptRef(
2679 SkSurface::NewRasterN32Premul(kCanvasSize
.width(), kCanvasSize
.height()));
2680 scoped_ptr
<Canvas
> canvas(
2681 Canvas::CreateCanvasWithoutScaling(surface
->getCanvas(), 1.0f
));
2682 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
2683 render_text
->SetHorizontalAlignment(ALIGN_LEFT
);
2684 render_text
->SetColor(SK_ColorBLACK
);
2686 for (auto string
: kTestStrings
) {
2687 surface
->getCanvas()->clear(SK_ColorWHITE
);
2688 render_text
->SetText(WideToUTF16(string
));
2689 const Size string_size
= render_text
->GetStringSize();
2690 render_text
->ApplyBaselineStyle(SUPERSCRIPT
, Range(1, 2));
2691 render_text
->ApplyBaselineStyle(SUPERIOR
, Range(3, 4));
2692 render_text
->ApplyBaselineStyle(INFERIOR
, Range(5, 6));
2693 render_text
->ApplyBaselineStyle(SUBSCRIPT
, Range(7, 8));
2694 render_text
->SetStyle(BOLD
, true);
2695 render_text
->SetDisplayRect(
2696 Rect(kTestSize
, kTestSize
, string_size
.width(), string_size
.height()));
2697 // Allow the RenderText to paint outside of its display rect.
2698 render_text
->set_clip_to_display_rect(false);
2699 ASSERT_LE(string_size
.width() + kTestSize
* 2, kCanvasSize
.width());
2701 render_text
->Draw(canvas
.get());
2702 ASSERT_LT(string_size
.width() + kTestSize
, kCanvasSize
.width());
2703 const uint32
* buffer
=
2704 static_cast<const uint32
*>(surface
->peekPixels(nullptr, nullptr));
2705 ASSERT_NE(nullptr, buffer
);
2706 TestRectangleBuffer
rect_buffer(string
, buffer
, kCanvasSize
.width(),
2707 kCanvasSize
.height());
2709 #if !defined(OS_CHROMEOS)
2710 // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect.
2711 SCOPED_TRACE("TextDoesntClip Top Side");
2712 rect_buffer
.EnsureSolidRect(SK_ColorWHITE
, 0, 0, kCanvasSize
.width(),
2717 SCOPED_TRACE("TextDoesntClip Bottom Side");
2718 rect_buffer
.EnsureSolidRect(SK_ColorWHITE
, 0,
2719 kTestSize
+ string_size
.height(),
2720 kCanvasSize
.width(), kTestSize
);
2723 SCOPED_TRACE("TextDoesntClip Left Side");
2725 // TODO(dschuyler): On Windows XP the Unicode test draws to the left edge
2726 // as if it is ignoring the SetDisplayRect shift by kTestSize. This
2727 // appears to be a preexisting issue that wasn't revealed by the prior
2729 #elif defined(OS_MACOSX)
2730 // TODO(dschuyler): On Windows (non-XP) and Mac smoothing draws left of
2731 // text. This appears to be a preexisting issue that wasn't revealed by
2732 // the prior unit tests.
2733 rect_buffer
.EnsureSolidRect(SK_ColorWHITE
, 0, kTestSize
, kTestSize
- 1,
2734 string_size
.height());
2736 rect_buffer
.EnsureSolidRect(SK_ColorWHITE
, 0, kTestSize
, kTestSize
,
2737 string_size
.height());
2741 SCOPED_TRACE("TextDoesntClip Right Side");
2742 #if !defined(OS_MACOSX)
2743 // TODO(dschuyler): On Mac text draws to right of GetStringSize. This
2744 // appears to be a preexisting issue that wasn't revealed by the prior
2746 rect_buffer
.EnsureSolidRect(SK_ColorWHITE
,
2747 kTestSize
+ string_size
.width(), kTestSize
,
2748 kTestSize
, string_size
.height());
2754 // Ensure that the text will clip to the display rect. Draws to a canvas and
2755 // checks whether any pixel beyond the bounding rectangle is colored.
2756 TEST_F(RenderTextTest
, TextDoesClip
) {
2757 const wchar_t* kTestStrings
[] = {L
"TEST", L
"W", L
"WWWW", L
"gAXAXWWWW"};
2758 const Size
kCanvasSize(300, 50);
2759 const int kTestSize
= 10;
2761 skia::RefPtr
<SkSurface
> surface
= skia::AdoptRef(
2762 SkSurface::NewRasterN32Premul(kCanvasSize
.width(), kCanvasSize
.height()));
2763 scoped_ptr
<Canvas
> canvas(
2764 Canvas::CreateCanvasWithoutScaling(surface
->getCanvas(), 1.0f
));
2765 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
2766 render_text
->SetHorizontalAlignment(ALIGN_LEFT
);
2767 render_text
->SetColor(SK_ColorBLACK
);
2769 for (auto string
: kTestStrings
) {
2770 surface
->getCanvas()->clear(SK_ColorWHITE
);
2771 render_text
->SetText(WideToUTF16(string
));
2772 const Size string_size
= render_text
->GetStringSize();
2773 int fake_width
= string_size
.width() / 2;
2774 int fake_height
= string_size
.height() / 2;
2775 render_text
->SetDisplayRect(
2776 Rect(kTestSize
, kTestSize
, fake_width
, fake_height
));
2777 render_text
->set_clip_to_display_rect(true);
2778 render_text
->Draw(canvas
.get());
2779 ASSERT_LT(string_size
.width() + kTestSize
, kCanvasSize
.width());
2780 const uint32
* buffer
=
2781 static_cast<const uint32
*>(surface
->peekPixels(nullptr, nullptr));
2782 ASSERT_NE(nullptr, buffer
);
2783 TestRectangleBuffer
rect_buffer(string
, buffer
, kCanvasSize
.width(),
2784 kCanvasSize
.height());
2786 SCOPED_TRACE("TextDoesClip Top Side");
2787 rect_buffer
.EnsureSolidRect(SK_ColorWHITE
, 0, 0, kCanvasSize
.width(),
2792 SCOPED_TRACE("TextDoesClip Bottom Side");
2793 rect_buffer
.EnsureSolidRect(SK_ColorWHITE
, 0, kTestSize
+ fake_height
,
2794 kCanvasSize
.width(), kTestSize
);
2797 SCOPED_TRACE("TextDoesClip Left Side");
2798 rect_buffer
.EnsureSolidRect(SK_ColorWHITE
, 0, kTestSize
, kTestSize
,
2802 SCOPED_TRACE("TextDoesClip Right Side");
2803 rect_buffer
.EnsureSolidRect(SK_ColorWHITE
, kTestSize
+ fake_width
,
2804 kTestSize
, kTestSize
, fake_height
);
2809 #if defined(OS_MACOSX)
2810 TEST_F(RenderTextTest
, Mac_ElidedText
) {
2811 RenderTextMac render_text
;
2812 base::string16
text(ASCIIToUTF16("This is an example."));
2813 render_text
.SetText(text
);
2814 gfx::Size string_size
= render_text
.GetStringSize();
2815 render_text
.SetDisplayRect(gfx::Rect(string_size
));
2816 render_text
.EnsureLayout();
2817 // NOTE: Character and glyph counts are only comparable for simple text.
2818 EXPECT_EQ(text
.size(),
2819 static_cast<size_t>(CTLineGetGlyphCount(render_text
.line_
)));
2821 render_text
.SetElideBehavior(ELIDE_TAIL
);
2822 string_size
.set_width(string_size
.width() / 2);
2823 render_text
.SetDisplayRect(gfx::Rect(string_size
));
2824 render_text
.EnsureLayout();
2825 CFIndex glyph_count
= CTLineGetGlyphCount(render_text
.line_
);
2826 EXPECT_GT(text
.size(), static_cast<size_t>(glyph_count
));
2827 EXPECT_NE(0, glyph_count
);