mac: Add the flag "-gline-tables-only" to reduce dSYM size. (attempt #2)
[chromium-blink-merge.git] / ui / gfx / render_text_unittest.cc
blob1ceec5171d0afdd4d9c7d87f514ef8c2a22d17a6
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/render_text.h"
7 #include <algorithm>
9 #include "base/format_macros.h"
10 #include "base/i18n/break_iterator.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/core/SkSurface.h"
18 #include "ui/gfx/break_list.h"
19 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/color_utils.h"
21 #include "ui/gfx/font.h"
22 #include "ui/gfx/range/range.h"
23 #include "ui/gfx/range/range_f.h"
24 #include "ui/gfx/render_text_harfbuzz.h"
26 #if defined(OS_WIN)
27 #include "base/win/windows_version.h"
28 #include "ui/gfx/platform_font_win.h"
29 #endif
31 #if defined(OS_MACOSX)
32 #include <ApplicationServices/ApplicationServices.h>
34 #include "ui/gfx/render_text_mac.h"
35 #endif
37 using base::ASCIIToUTF16;
38 using base::UTF8ToUTF16;
39 using base::WideToUTF16;
40 using base::WideToUTF8;
42 namespace gfx {
44 namespace {
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 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
74 #if !defined(OS_MACOSX)
75 // Ensure cursor movement in the specified |direction| yields |expected| values.
76 void RunMoveCursorLeftRightTest(RenderText* render_text,
77 const std::vector<SelectionModel>& expected,
78 VisualCursorDirection direction) {
79 for (size_t i = 0; i < expected.size(); ++i) {
80 SCOPED_TRACE(base::StringPrintf("Going %s; expected value index %d.",
81 direction == CURSOR_LEFT ? "left" : "right", static_cast<int>(i)));
82 EXPECT_EQ(expected[i], render_text->selection_model());
83 render_text->MoveCursor(CHARACTER_BREAK, direction, false);
85 // Check that cursoring is clamped at the line edge.
86 EXPECT_EQ(expected.back(), render_text->selection_model());
87 // Check that it is the line edge.
88 render_text->MoveCursor(LINE_BREAK, direction, false);
89 EXPECT_EQ(expected.back(), render_text->selection_model());
91 #endif // !defined(OS_MACOSX)
93 // Test utility for Multiline_Newline test case. Empty |expected_range| means
94 // the blank line which has no segments. Otherwise |segments| should contain
95 // exactly one line segment whose range equals to |expected_range|.
96 void VerifyLineSegments(const Range& expected_range,
97 const std::vector<internal::LineSegment>& segments) {
98 EXPECT_EQ(expected_range.is_empty() ? 0ul : 1ul, segments.size());
99 if (!expected_range.is_empty())
100 EXPECT_EQ(expected_range, segments[0].char_range);
103 // The class which records the drawing operations so that the test case can
104 // verify where exactly the glyphs are drawn.
105 class TestSkiaTextRenderer : public internal::SkiaTextRenderer {
106 public:
107 struct TextLog {
108 TextLog() : glyph_count(0u) {}
109 PointF origin;
110 size_t glyph_count;
113 struct DecorationLog {
114 DecorationLog(int x, int y, int width, bool underline, bool strike,
115 bool diagonal_strike)
116 : x(x), y(y), width(width), underline(underline), strike(strike),
117 diagonal_strike(diagonal_strike) {}
118 int x;
119 int y;
120 int width;
121 bool underline;
122 bool strike;
123 bool diagonal_strike;
126 explicit TestSkiaTextRenderer(Canvas* canvas)
127 : internal::SkiaTextRenderer(canvas) {}
128 ~TestSkiaTextRenderer() override {}
130 void GetTextLogAndReset(std::vector<TextLog>* text_log) {
131 text_log_.swap(*text_log);
132 text_log_.clear();
135 void GetDecorationLogAndReset(std::vector<DecorationLog>* decoration_log) {
136 decoration_log_.swap(*decoration_log);
137 decoration_log_.clear();
140 private:
141 // internal::SkiaTextRenderer:
142 void DrawPosText(const SkPoint* pos,
143 const uint16* glyphs,
144 size_t glyph_count) override {
145 TextLog log_entry;
146 log_entry.glyph_count = glyph_count;
147 if (glyph_count > 0) {
148 log_entry.origin =
149 PointF(SkScalarToFloat(pos[0].x()), SkScalarToFloat(pos[0].y()));
150 for (size_t i = 1U; i < glyph_count; ++i) {
151 log_entry.origin.SetToMin(
152 PointF(SkScalarToFloat(pos[i].x()), SkScalarToFloat(pos[i].y())));
155 text_log_.push_back(log_entry);
156 internal::SkiaTextRenderer::DrawPosText(pos, glyphs, glyph_count);
159 void DrawDecorations(int x, int y, int width, bool underline, bool strike,
160 bool diagonal_strike) override {
161 decoration_log_.push_back(
162 DecorationLog(x, y, width, underline, strike, diagonal_strike));
163 internal::SkiaTextRenderer::DrawDecorations(
164 x, y, width, underline, strike, diagonal_strike);
167 std::vector<TextLog> text_log_;
168 std::vector<DecorationLog> decoration_log_;
170 DISALLOW_COPY_AND_ASSIGN(TestSkiaTextRenderer);
173 // Given a buffer to test against, this can be used to test various areas of the
174 // rectangular buffer against a specific color value.
175 class TestRectangleBuffer {
176 public:
177 TestRectangleBuffer(const wchar_t* string,
178 const SkColor* buffer,
179 uint32_t stride,
180 uint32_t row_count)
181 : string_(string),
182 buffer_(buffer),
183 stride_(stride),
184 row_count_(row_count) {}
186 // Test if any values in the rectangular area are anything other than |color|.
187 void EnsureSolidRect(SkColor color,
188 int left,
189 int top,
190 int width,
191 int height) const {
192 ASSERT_LT(top, row_count_) << string_;
193 ASSERT_LE(top + height, row_count_) << string_;
194 ASSERT_LT(left, stride_) << string_;
195 ASSERT_LE(left + width, stride_) << string_ << ", left " << left
196 << ", width " << width << ", stride_ "
197 << stride_;
198 for (int y = top; y < top + height; ++y) {
199 for (int x = left; x < left + width; ++x) {
200 SkColor buffer_color = buffer_[x + y * stride_];
201 EXPECT_EQ(color, buffer_color) << string_ << " at " << x << ", " << y;
206 private:
207 const wchar_t* string_;
208 const SkColor* buffer_;
209 int stride_;
210 int row_count_;
212 DISALLOW_COPY_AND_ASSIGN(TestRectangleBuffer);
215 } // namespace
217 class RenderTextTest : public testing::Test {
220 TEST_F(RenderTextTest, DefaultStyles) {
221 // Check the default styles applied to new instances and adjusted text.
222 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
223 EXPECT_TRUE(render_text->text().empty());
224 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
225 for (size_t i = 0; i < arraysize(cases); ++i) {
226 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLACK));
227 EXPECT_TRUE(
228 render_text->baselines().EqualsValueForTesting(NORMAL_BASELINE));
229 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
230 EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false));
231 render_text->SetText(WideToUTF16(cases[i]));
235 TEST_F(RenderTextTest, SetStyles) {
236 // Ensure custom default styles persist across setting and clearing text.
237 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
238 const SkColor color = SK_ColorRED;
239 render_text->SetColor(color);
240 render_text->SetBaselineStyle(SUPERSCRIPT);
241 render_text->SetStyle(BOLD, true);
242 render_text->SetStyle(UNDERLINE, false);
243 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
244 for (size_t i = 0; i < arraysize(cases); ++i) {
245 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color));
246 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUPERSCRIPT));
247 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true));
248 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false));
249 render_text->SetText(WideToUTF16(cases[i]));
251 // Ensure custom default styles can be applied after text has been set.
252 if (i == 1)
253 render_text->SetStyle(STRIKE, true);
254 if (i >= 1)
255 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true));
259 TEST_F(RenderTextTest, ApplyStyles) {
260 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
261 render_text->SetText(ASCIIToUTF16("012345678"));
263 // Apply a ranged color and style and check the resulting breaks.
264 render_text->ApplyColor(SK_ColorRED, Range(1, 4));
265 render_text->ApplyBaselineStyle(SUPERIOR, Range(2, 4));
266 render_text->ApplyStyle(BOLD, true, Range(2, 5));
267 std::vector<std::pair<size_t, SkColor> > expected_color;
268 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK));
269 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED));
270 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK));
271 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
272 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_style;
273 expected_baseline_style.push_back(
274 std::pair<size_t, BaselineStyle>(0, NORMAL_BASELINE));
275 expected_baseline_style.push_back(
276 std::pair<size_t, BaselineStyle>(2, SUPERIOR));
277 expected_baseline_style.push_back(
278 std::pair<size_t, BaselineStyle>(4, NORMAL_BASELINE));
279 EXPECT_TRUE(
280 render_text->baselines().EqualsForTesting(expected_baseline_style));
281 std::vector<std::pair<size_t, bool> > expected_style;
282 expected_style.push_back(std::pair<size_t, bool>(0, false));
283 expected_style.push_back(std::pair<size_t, bool>(2, true));
284 expected_style.push_back(std::pair<size_t, bool>(5, false));
285 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style));
287 // Ensure that setting a value overrides the ranged values.
288 render_text->SetColor(SK_ColorBLUE);
289 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE));
290 render_text->SetBaselineStyle(SUBSCRIPT);
291 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUBSCRIPT));
292 render_text->SetStyle(BOLD, false);
293 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false));
295 // Apply a value over the text end and check the resulting breaks (INT_MAX
296 // should be used instead of the text length for the range end)
297 const size_t text_length = render_text->text().length();
298 render_text->ApplyColor(SK_ColorRED, Range(0, text_length));
299 render_text->ApplyBaselineStyle(SUPERIOR, Range(0, text_length));
300 render_text->ApplyStyle(BOLD, true, Range(2, text_length));
301 std::vector<std::pair<size_t, SkColor> > expected_color_end;
302 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED));
303 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end));
304 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_end;
305 expected_baseline_end.push_back(
306 std::pair<size_t, BaselineStyle>(0, SUPERIOR));
307 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline_end));
308 std::vector<std::pair<size_t, bool> > expected_style_end;
309 expected_style_end.push_back(std::pair<size_t, bool>(0, false));
310 expected_style_end.push_back(std::pair<size_t, bool>(2, true));
311 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end));
313 // Ensure ranged values adjust to accommodate text length changes.
314 render_text->ApplyStyle(ITALIC, true, Range(0, 2));
315 render_text->ApplyStyle(ITALIC, true, Range(3, 6));
316 render_text->ApplyStyle(ITALIC, true, Range(7, text_length));
317 std::vector<std::pair<size_t, bool> > expected_italic;
318 expected_italic.push_back(std::pair<size_t, bool>(0, true));
319 expected_italic.push_back(std::pair<size_t, bool>(2, false));
320 expected_italic.push_back(std::pair<size_t, bool>(3, true));
321 expected_italic.push_back(std::pair<size_t, bool>(6, false));
322 expected_italic.push_back(std::pair<size_t, bool>(7, true));
323 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
325 // Changing the text should clear any breaks except for the first one.
326 render_text->SetText(ASCIIToUTF16("0123456"));
327 expected_italic.resize(1);
328 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
329 render_text->ApplyStyle(ITALIC, false, Range(2, 4));
330 render_text->SetText(ASCIIToUTF16("012345678"));
331 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
332 render_text->ApplyStyle(ITALIC, false, Range(0, 1));
333 render_text->SetText(ASCIIToUTF16("0123456"));
334 expected_italic.begin()->second = false;
335 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
336 render_text->ApplyStyle(ITALIC, true, Range(2, 4));
337 render_text->SetText(ASCIIToUTF16("012345678"));
338 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
340 // TODO(tmoniuszko): Enable when RenderTextMac::IsValidCursorIndex is
341 // implemented.
342 #if !defined(OS_MACOSX)
343 // Styles shouldn't be changed mid-grapheme.
344 render_text->SetText(WideToUTF16(
345 L"0" L"\x0915\x093f" L"1" L"\x0915\x093f" L"2"));
346 render_text->ApplyStyle(UNDERLINE, true, Range(2, 5));
347 std::vector<std::pair<size_t, bool> > expected_underline;
348 expected_underline.push_back(std::pair<size_t, bool>(0, false));
349 expected_underline.push_back(std::pair<size_t, bool>(1, true));
350 expected_underline.push_back(std::pair<size_t, bool>(6, false));
351 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsForTesting(
352 expected_underline));
353 #endif // !defined(OS_MACOSX)
356 TEST_F(RenderTextTest, AppendTextKeepsStyles) {
357 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
358 // Setup basic functionality.
359 render_text->SetText(ASCIIToUTF16("abc"));
360 render_text->ApplyColor(SK_ColorRED, Range(0, 1));
361 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2));
362 render_text->ApplyStyle(UNDERLINE, true, Range(2, 3));
363 // Verify basic functionality.
364 std::vector<std::pair<size_t, SkColor>> expected_color;
365 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED));
366 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorBLACK));
367 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
368 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline;
369 expected_baseline.push_back(
370 std::pair<size_t, BaselineStyle>(0, NORMAL_BASELINE));
371 expected_baseline.push_back(std::pair<size_t, BaselineStyle>(1, SUPERSCRIPT));
372 expected_baseline.push_back(
373 std::pair<size_t, BaselineStyle>(2, NORMAL_BASELINE));
374 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline));
375 std::vector<std::pair<size_t, bool>> expected_style;
376 expected_style.push_back(std::pair<size_t, bool>(0, false));
377 expected_style.push_back(std::pair<size_t, bool>(2, true));
378 EXPECT_TRUE(
379 render_text->styles()[UNDERLINE].EqualsForTesting(expected_style));
380 // Ensure AppendText maintains current text styles.
381 render_text->AppendText(ASCIIToUTF16("def"));
382 EXPECT_EQ(render_text->GetDisplayText(), ASCIIToUTF16("abcdef"));
383 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
384 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline));
385 EXPECT_TRUE(
386 render_text->styles()[UNDERLINE].EqualsForTesting(expected_style));
389 void TestVisualCursorMotionInObscuredField(RenderText* render_text,
390 const base::string16& text,
391 bool select) {
392 ASSERT_TRUE(render_text->obscured());
393 render_text->SetText(text);
394 int len = text.length();
395 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, select);
396 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
397 render_text->selection_model());
398 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, select);
399 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
400 for (int j = 1; j <= len; ++j) {
401 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, select);
402 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_BACKWARD),
403 render_text->selection_model());
405 for (int j = len - 1; j >= 0; --j) {
406 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, select);
407 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_FORWARD),
408 render_text->selection_model());
410 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, select);
411 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
412 render_text->selection_model());
413 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, select);
414 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
417 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
418 #if !defined(OS_MACOSX)
419 TEST_F(RenderTextTest, ObscuredText) {
420 const base::string16 seuss = ASCIIToUTF16("hop on pop");
421 const base::string16 no_seuss = ASCIIToUTF16("**********");
422 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
424 // GetLayoutText() returns asterisks when the obscured bit is set.
425 render_text->SetText(seuss);
426 render_text->SetObscured(true);
427 EXPECT_EQ(seuss, render_text->text());
428 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
429 render_text->SetObscured(false);
430 EXPECT_EQ(seuss, render_text->text());
431 EXPECT_EQ(seuss, render_text->GetDisplayText());
433 render_text->SetObscured(true);
435 // Surrogate pairs are counted as one code point.
436 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 0};
437 render_text->SetText(invalid_surrogates);
438 EXPECT_EQ(ASCIIToUTF16("**"), render_text->GetDisplayText());
439 const base::char16 valid_surrogates[] = {0xD800, 0xDC00, 0};
440 render_text->SetText(valid_surrogates);
441 EXPECT_EQ(ASCIIToUTF16("*"), render_text->GetDisplayText());
442 EXPECT_EQ(0U, render_text->cursor_position());
443 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
444 EXPECT_EQ(2U, render_text->cursor_position());
446 // Test index conversion and cursor validity with a valid surrogate pair.
447 EXPECT_EQ(0U, render_text->TextIndexToDisplayIndex(0U));
448 EXPECT_EQ(1U, render_text->TextIndexToDisplayIndex(1U));
449 EXPECT_EQ(1U, render_text->TextIndexToDisplayIndex(2U));
450 EXPECT_EQ(0U, render_text->DisplayIndexToTextIndex(0U));
451 EXPECT_EQ(2U, render_text->DisplayIndexToTextIndex(1U));
452 EXPECT_TRUE(render_text->IsValidCursorIndex(0U));
453 EXPECT_FALSE(render_text->IsValidCursorIndex(1U));
454 EXPECT_TRUE(render_text->IsValidCursorIndex(2U));
456 // FindCursorPosition() should not return positions between a surrogate pair.
457 render_text->SetDisplayRect(Rect(0, 0, 20, 20));
458 EXPECT_EQ(render_text->FindCursorPosition(Point(0, 0)).caret_pos(), 0U);
459 EXPECT_EQ(render_text->FindCursorPosition(Point(20, 0)).caret_pos(), 2U);
460 for (int x = -1; x <= 20; ++x) {
461 SelectionModel selection = render_text->FindCursorPosition(Point(x, 0));
462 EXPECT_TRUE(selection.caret_pos() == 0U || selection.caret_pos() == 2U);
465 // GetGlyphBounds() should yield the entire string bounds for text index 0.
466 EXPECT_EQ(render_text->GetStringSize().width(),
467 static_cast<int>(render_text->GetGlyphBounds(0U).length()));
469 // Cursoring is independent of underlying characters when text is obscured.
470 const wchar_t* const texts[] = {
471 kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl,
472 L"hop on pop", // Check LTR word boundaries.
473 L"\x05d0\x05d1 \x05d0\x05d2 \x05d1\x05d2", // Check RTL word boundaries.
475 for (size_t i = 0; i < arraysize(texts); ++i) {
476 base::string16 text = WideToUTF16(texts[i]);
477 TestVisualCursorMotionInObscuredField(render_text.get(), text, false);
478 TestVisualCursorMotionInObscuredField(render_text.get(), text, true);
481 #endif // !defined(OS_MACOSX)
483 TEST_F(RenderTextTest, RevealObscuredText) {
484 const base::string16 seuss = ASCIIToUTF16("hop on pop");
485 const base::string16 no_seuss = ASCIIToUTF16("**********");
486 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
488 render_text->SetText(seuss);
489 render_text->SetObscured(true);
490 EXPECT_EQ(seuss, render_text->text());
491 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
493 // Valid reveal index and new revealed index clears previous one.
494 render_text->RenderText::SetObscuredRevealIndex(0);
495 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetDisplayText());
496 render_text->RenderText::SetObscuredRevealIndex(1);
497 EXPECT_EQ(ASCIIToUTF16("*o********"), render_text->GetDisplayText());
498 render_text->RenderText::SetObscuredRevealIndex(2);
499 EXPECT_EQ(ASCIIToUTF16("**p*******"), render_text->GetDisplayText());
501 // Invalid reveal index.
502 render_text->RenderText::SetObscuredRevealIndex(-1);
503 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
504 render_text->RenderText::SetObscuredRevealIndex(seuss.length() + 1);
505 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
507 // SetObscured clears the revealed index.
508 render_text->RenderText::SetObscuredRevealIndex(0);
509 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetDisplayText());
510 render_text->SetObscured(false);
511 EXPECT_EQ(seuss, render_text->GetDisplayText());
512 render_text->SetObscured(true);
513 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
515 // SetText clears the revealed index.
516 render_text->SetText(ASCIIToUTF16("new"));
517 EXPECT_EQ(ASCIIToUTF16("***"), render_text->GetDisplayText());
518 render_text->RenderText::SetObscuredRevealIndex(2);
519 EXPECT_EQ(ASCIIToUTF16("**w"), render_text->GetDisplayText());
520 render_text->SetText(ASCIIToUTF16("new longer"));
521 EXPECT_EQ(ASCIIToUTF16("**********"), render_text->GetDisplayText());
523 // Text with invalid surrogates.
524 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 'h', 'o', 'p', 0};
525 render_text->SetText(invalid_surrogates);
526 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetDisplayText());
527 render_text->RenderText::SetObscuredRevealIndex(0);
528 const base::char16 invalid_expect_0[] = {0xDC00, '*', '*', '*', '*', 0};
529 EXPECT_EQ(invalid_expect_0, render_text->GetDisplayText());
530 render_text->RenderText::SetObscuredRevealIndex(1);
531 const base::char16 invalid_expect_1[] = {'*', 0xD800, '*', '*', '*', 0};
532 EXPECT_EQ(invalid_expect_1, render_text->GetDisplayText());
533 render_text->RenderText::SetObscuredRevealIndex(2);
534 EXPECT_EQ(ASCIIToUTF16("**h**"), render_text->GetDisplayText());
536 // Text with valid surrogates before and after the reveal index.
537 const base::char16 valid_surrogates[] =
538 {0xD800, 0xDC00, 'h', 'o', 'p', 0xD800, 0xDC00, 0};
539 render_text->SetText(valid_surrogates);
540 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetDisplayText());
541 render_text->RenderText::SetObscuredRevealIndex(0);
542 const base::char16 valid_expect_0_and_1[] =
543 {0xD800, 0xDC00, '*', '*', '*', '*', 0};
544 EXPECT_EQ(valid_expect_0_and_1, render_text->GetDisplayText());
545 render_text->RenderText::SetObscuredRevealIndex(1);
546 EXPECT_EQ(valid_expect_0_and_1, render_text->GetDisplayText());
547 render_text->RenderText::SetObscuredRevealIndex(2);
548 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text->GetDisplayText());
549 render_text->RenderText::SetObscuredRevealIndex(5);
550 const base::char16 valid_expect_5_and_6[] =
551 {'*', '*', '*', '*', 0xD800, 0xDC00, 0};
552 EXPECT_EQ(valid_expect_5_and_6, render_text->GetDisplayText());
553 render_text->RenderText::SetObscuredRevealIndex(6);
554 EXPECT_EQ(valid_expect_5_and_6, render_text->GetDisplayText());
557 // TODO(PORT): Fails for RenderTextMac.
558 #if !defined(OS_MACOSX)
559 TEST_F(RenderTextTest, ElidedText) {
560 // TODO(skanuj) : Add more test cases for following
561 // - RenderText styles.
562 // - Cross interaction of truncate, elide and obscure.
563 // - ElideText tests from text_elider.cc.
564 struct {
565 const wchar_t* text;
566 const wchar_t* display_text;
567 const bool elision_expected;
568 } cases[] = {
569 // Strings shorter than the elision width should be laid out in full.
570 { L"", L"" , false },
571 { L"M", L"" , false },
572 { L" . ", L" . " , false },
573 { kWeak, kWeak , false },
574 { kLtr, kLtr , false },
575 { kLtrRtl, kLtrRtl , false },
576 { kLtrRtlLtr, kLtrRtlLtr, false },
577 { kRtl, kRtl , false },
578 { kRtlLtr, kRtlLtr , false },
579 { kRtlLtrRtl, kRtlLtrRtl, false },
580 // Strings as long as the elision width should be laid out in full.
581 { L"012ab", L"012ab" , false },
582 // Long strings should be elided with an ellipsis appended at the end.
583 { L"012abc", L"012a\x2026", true },
584 { L"012ab" L"\x5d0\x5d1", L"012a\x2026", true },
585 { L"012a" L"\x5d1" L"b", L"012a\x2026", true },
586 // No RLM marker added as digits (012) have weak directionality.
587 { L"01" L"\x5d0\x5d1\x5d2", L"01\x5d0\x5d1\x2026", true },
588 // RLM marker added as "ab" have strong LTR directionality.
589 { L"ab" L"\x5d0\x5d1\x5d2", L"ab\x5d0\x5d1\x2026\x200f", true },
590 // Test surrogate pairs. \xd834\xdd1e forms a single code point U+1D11E;
591 // \xd834\xdd22 forms a second code point U+1D122. The first should be kept;
592 // the second removed (no surrogate pair should be partially elided).
593 { L"0123\xd834\xdd1e\xd834\xdd22", L"0123\xd834\xdd1e\x2026", true },
594 // Test combining character sequences. U+0915 U+093F forms a compound glyph;
595 // U+0915 U+0942 forms a second compound glyph. The first should be kept;
596 // the second removed (no combining sequence should be partially elided).
597 { L"0123\x0915\x093f\x0915\x0942", L"0123\x0915\x093f\x2026", true },
598 // U+05E9 U+05BC U+05C1 U+05B8 forms a four-character compound glyph. Again,
599 // it should be either fully elided, or not elided at all. If completely
600 // elided, an LTR Mark (U+200E) should be added.
601 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8", false },
602 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x2026\x200E" , true },
603 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x2026\x200E" , true },
604 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x2026\x200E" , true },
605 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" , true },
608 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
609 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
610 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
612 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
613 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
614 render_text->SetElideBehavior(ELIDE_TAIL);
616 for (size_t i = 0; i < arraysize(cases); i++) {
617 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "] '%ls'", i,
618 cases[i].text));
620 // Compute expected width
621 expected_render_text->SetText(WideToUTF16(cases[i].display_text));
622 int expected_width = expected_render_text->GetContentWidth();
624 base::string16 input = WideToUTF16(cases[i].text);
625 // Extend the input text to ensure that it is wider than the display_text,
626 // and so it will get elided.
627 if (cases[i].elision_expected)
628 input.append(WideToUTF16(L" MMMMMMMMMMM"));
629 render_text->SetText(input);
630 render_text->SetDisplayRect(Rect(0, 0, expected_width, 100));
631 EXPECT_EQ(input, render_text->text());
632 EXPECT_EQ(WideToUTF16(cases[i].display_text),
633 render_text->GetDisplayText());
634 expected_render_text->SetText(base::string16());
638 TEST_F(RenderTextTest, ElidedObscuredText) {
639 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
640 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
641 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
642 expected_render_text->SetText(WideToUTF16(L"**\x2026"));
644 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
645 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
646 render_text->SetElideBehavior(ELIDE_TAIL);
647 render_text->SetDisplayRect(
648 Rect(0, 0, expected_render_text->GetContentWidth(), 100));
649 render_text->SetObscured(true);
650 render_text->SetText(WideToUTF16(L"abcdef"));
651 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
652 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetDisplayText());
654 #endif // !defined(OS_MACOSX)
656 TEST_F(RenderTextTest, ElidedEmail) {
657 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
658 render_text->SetText(ASCIIToUTF16("test@example.com"));
659 const gfx::Size size = render_text->GetStringSize();
661 const base::string16 long_email =
662 ASCIIToUTF16("longemailaddresstest@example.com");
663 render_text->SetText(long_email);
664 render_text->SetElideBehavior(ELIDE_EMAIL);
665 render_text->SetDisplayRect(gfx::Rect(size));
666 EXPECT_GE(size.width(), render_text->GetStringSize().width());
667 EXPECT_GT(long_email.size(), render_text->GetDisplayText().size());
670 TEST_F(RenderTextTest, TruncatedText) {
671 struct {
672 const wchar_t* text;
673 const wchar_t* display_text;
674 } cases[] = {
675 // Strings shorter than the truncation length should be laid out in full.
676 { L"", L"" },
677 { kWeak, kWeak },
678 { kLtr, kLtr },
679 { kLtrRtl, kLtrRtl },
680 { kLtrRtlLtr, kLtrRtlLtr },
681 { kRtl, kRtl },
682 { kRtlLtr, kRtlLtr },
683 { kRtlLtrRtl, kRtlLtrRtl },
684 // Strings as long as the truncation length should be laid out in full.
685 { L"01234", L"01234" },
686 // Long strings should be truncated with an ellipsis appended at the end.
687 { L"012345", L"0123\x2026" },
688 { L"012" L" . ", L"012 \x2026" },
689 { L"012" L"abc", L"012a\x2026" },
690 { L"012" L"a" L"\x5d0\x5d1", L"012a\x2026" },
691 { L"012" L"a" L"\x5d1" L"b", L"012a\x2026" },
692 { L"012" L"\x5d0\x5d1\x5d2", L"012\x5d0\x2026" },
693 { L"012" L"\x5d0\x5d1" L"a", L"012\x5d0\x2026" },
694 { L"012" L"\x5d0" L"a" L"\x5d1", L"012\x5d0\x2026" },
695 // Surrogate pairs should be truncated reasonably enough.
696 { L"0123\x0915\x093f", L"0123\x2026" },
697 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8" },
698 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x05bc\x2026" },
699 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x05e9\x2026" },
700 { L"0123\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
701 { L"01234\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
702 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" },
705 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
706 render_text->set_truncate_length(5);
707 for (size_t i = 0; i < arraysize(cases); i++) {
708 render_text->SetText(WideToUTF16(cases[i].text));
709 EXPECT_EQ(WideToUTF16(cases[i].text), render_text->text());
710 EXPECT_EQ(WideToUTF16(cases[i].display_text), render_text->GetDisplayText())
711 << "For case " << i << ": " << cases[i].text;
715 TEST_F(RenderTextTest, TruncatedObscuredText) {
716 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
717 render_text->set_truncate_length(3);
718 render_text->SetObscured(true);
719 render_text->SetText(WideToUTF16(L"abcdef"));
720 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
721 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetDisplayText());
724 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
725 #if !defined(OS_MACOSX)
726 TEST_F(RenderTextTest, TruncatedCursorMovementLTR) {
727 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
728 render_text->set_truncate_length(2);
729 render_text->SetText(WideToUTF16(L"abcd"));
731 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
732 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
733 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
734 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
735 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
737 std::vector<SelectionModel> expected;
738 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
739 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
740 // The cursor hops over the ellipsis and elided text to the line end.
741 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
742 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
743 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
745 expected.clear();
746 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
747 // The cursor hops over the elided text to preceeding text.
748 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
749 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
750 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
751 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
754 TEST_F(RenderTextTest, TruncatedCursorMovementRTL) {
755 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
756 render_text->set_truncate_length(2);
757 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3"));
759 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
760 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
761 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
762 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
763 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
765 std::vector<SelectionModel> expected;
766 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
767 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
768 // The cursor hops over the ellipsis and elided text to the line end.
769 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
770 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
771 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
773 expected.clear();
774 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
775 // The cursor hops over the elided text to preceeding text.
776 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
777 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
778 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
779 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
781 #endif // !defined(OS_MACOSX)
783 TEST_F(RenderTextTest, GetDisplayTextDirection) {
784 struct {
785 const wchar_t* text;
786 const base::i18n::TextDirection text_direction;
787 } cases[] = {
788 // Blank strings and those with no/weak directionality default to LTR.
789 { L"", base::i18n::LEFT_TO_RIGHT },
790 { kWeak, base::i18n::LEFT_TO_RIGHT },
791 // Strings that begin with strong LTR characters.
792 { kLtr, base::i18n::LEFT_TO_RIGHT },
793 { kLtrRtl, base::i18n::LEFT_TO_RIGHT },
794 { kLtrRtlLtr, base::i18n::LEFT_TO_RIGHT },
795 // Strings that begin with strong RTL characters.
796 { kRtl, base::i18n::RIGHT_TO_LEFT },
797 { kRtlLtr, base::i18n::RIGHT_TO_LEFT },
798 { kRtlLtrRtl, base::i18n::RIGHT_TO_LEFT },
801 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
802 const bool was_rtl = base::i18n::IsRTL();
804 for (size_t i = 0; i < 2; ++i) {
805 // Toggle the application default text direction (to try each direction).
806 SetRTL(!base::i18n::IsRTL());
807 const base::i18n::TextDirection ui_direction = base::i18n::IsRTL() ?
808 base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
810 // Ensure that directionality modes yield the correct text directions.
811 for (size_t j = 0; j < arraysize(cases); j++) {
812 render_text->SetText(WideToUTF16(cases[j].text));
813 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
814 EXPECT_EQ(render_text->GetDisplayTextDirection(),cases[j].text_direction);
815 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_UI);
816 EXPECT_EQ(render_text->GetDisplayTextDirection(), ui_direction);
817 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_LTR);
818 EXPECT_EQ(render_text->GetDisplayTextDirection(),
819 base::i18n::LEFT_TO_RIGHT);
820 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_RTL);
821 EXPECT_EQ(render_text->GetDisplayTextDirection(),
822 base::i18n::RIGHT_TO_LEFT);
826 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
828 // Ensure that text changes update the direction for DIRECTIONALITY_FROM_TEXT.
829 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
830 render_text->SetText(WideToUTF16(kLtr));
831 EXPECT_EQ(render_text->GetDisplayTextDirection(), base::i18n::LEFT_TO_RIGHT);
832 render_text->SetText(WideToUTF16(kRtl));
833 EXPECT_EQ(render_text->GetDisplayTextDirection(), base::i18n::RIGHT_TO_LEFT);
836 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
837 #if !defined(OS_MACOSX)
838 TEST_F(RenderTextTest, MoveCursorLeftRightInLtr) {
839 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
841 // Pure LTR.
842 render_text->SetText(ASCIIToUTF16("abc"));
843 // |expected| saves the expected SelectionModel when moving cursor from left
844 // to right.
845 std::vector<SelectionModel> expected;
846 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
847 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
848 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
849 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
850 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
851 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
853 expected.clear();
854 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
855 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
856 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
857 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
858 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
859 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
862 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtl) {
863 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
864 // LTR-RTL
865 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
866 // The last one is the expected END position.
867 std::vector<SelectionModel> expected;
868 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
869 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
870 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
871 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
872 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
873 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
874 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
875 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
876 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
878 expected.clear();
879 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
880 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
881 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
882 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
883 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
884 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
885 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
886 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
887 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
890 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtlLtr) {
891 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
892 // LTR-RTL-LTR.
893 render_text->SetText(WideToUTF16(L"a" L"\x05d1" L"b"));
894 std::vector<SelectionModel> expected;
895 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
896 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
897 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
898 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
899 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
900 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
902 expected.clear();
903 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
904 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
905 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
906 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
907 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
908 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
911 TEST_F(RenderTextTest, MoveCursorLeftRightInRtl) {
912 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
913 // Pure RTL.
914 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2"));
915 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
916 std::vector<SelectionModel> expected;
918 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
919 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
920 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
921 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
922 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
923 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
925 expected.clear();
927 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
928 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
929 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
930 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
931 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
932 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
935 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtr) {
936 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
937 // RTL-LTR
938 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2" L"abc"));
939 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
940 std::vector<SelectionModel> expected;
941 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
942 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
943 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
944 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
945 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
946 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
947 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
948 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
949 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
951 expected.clear();
952 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
953 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
954 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
955 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
956 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
957 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
958 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
959 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
960 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
963 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtrRtl) {
964 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
965 // RTL-LTR-RTL.
966 render_text->SetText(WideToUTF16(L"\x05d0" L"a" L"\x05d1"));
967 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
968 std::vector<SelectionModel> expected;
969 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
970 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
971 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
972 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
973 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
974 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
976 expected.clear();
977 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
978 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
979 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
980 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
981 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
982 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
985 TEST_F(RenderTextTest, MoveCursorLeftRight_ComplexScript) {
986 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
988 render_text->SetText(WideToUTF16(L"\x0915\x093f\x0915\x094d\x0915"));
989 EXPECT_EQ(0U, render_text->cursor_position());
990 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
991 EXPECT_EQ(2U, render_text->cursor_position());
992 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
993 EXPECT_EQ(4U, render_text->cursor_position());
994 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
995 EXPECT_EQ(5U, render_text->cursor_position());
996 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
997 EXPECT_EQ(5U, render_text->cursor_position());
999 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1000 EXPECT_EQ(4U, render_text->cursor_position());
1001 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1002 EXPECT_EQ(2U, render_text->cursor_position());
1003 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1004 EXPECT_EQ(0U, render_text->cursor_position());
1005 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1006 EXPECT_EQ(0U, render_text->cursor_position());
1009 TEST_F(RenderTextTest, MoveCursorLeftRight_MeiryoUILigatures) {
1010 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1011 // Meiryo UI uses single-glyph ligatures for 'ff' and 'ffi', but each letter
1012 // (code point) has unique bounds, so mid-glyph cursoring should be possible.
1013 render_text->SetFontList(FontList("Meiryo UI, 12px"));
1014 render_text->SetText(WideToUTF16(L"ff ffi"));
1015 EXPECT_EQ(0U, render_text->cursor_position());
1016 for (size_t i = 0; i < render_text->text().length(); ++i) {
1017 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1018 EXPECT_EQ(i + 1, render_text->cursor_position());
1020 EXPECT_EQ(6U, render_text->cursor_position());
1023 TEST_F(RenderTextTest, GraphemePositions) {
1024 // LTR 2-character grapheme, LTR abc, LTR 2-character grapheme.
1025 const base::string16 kText1 =
1026 WideToUTF16(L"\x0915\x093f" L"abc" L"\x0915\x093f");
1028 // LTR ab, LTR 2-character grapheme, LTR cd.
1029 const base::string16 kText2 = WideToUTF16(L"ab" L"\x0915\x093f" L"cd");
1031 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
1032 // two characters forming the surrogate pair 0x0001D11E.
1033 const std::string kSurrogate = "\xF0\x9D\x84\x9E";
1035 // LTR ab, UTF16 surrogate pair, LTR cd.
1036 const base::string16 kText3 = UTF8ToUTF16("ab" + kSurrogate + "cd");
1038 struct {
1039 base::string16 text;
1040 size_t index;
1041 size_t expected_previous;
1042 size_t expected_next;
1043 } cases[] = {
1044 { base::string16(), 0, 0, 0 },
1045 { base::string16(), 1, 0, 0 },
1046 { base::string16(), 50, 0, 0 },
1047 { kText1, 0, 0, 2 },
1048 { kText1, 1, 0, 2 },
1049 { kText1, 2, 0, 3 },
1050 { kText1, 3, 2, 4 },
1051 { kText1, 4, 3, 5 },
1052 { kText1, 5, 4, 7 },
1053 { kText1, 6, 5, 7 },
1054 { kText1, 7, 5, 7 },
1055 { kText1, 8, 7, 7 },
1056 { kText1, 50, 7, 7 },
1057 { kText2, 0, 0, 1 },
1058 { kText2, 1, 0, 2 },
1059 { kText2, 2, 1, 4 },
1060 { kText2, 3, 2, 4 },
1061 { kText2, 4, 2, 5 },
1062 { kText2, 5, 4, 6 },
1063 { kText2, 6, 5, 6 },
1064 { kText2, 7, 6, 6 },
1065 { kText2, 50, 6, 6 },
1066 { kText3, 0, 0, 1 },
1067 { kText3, 1, 0, 2 },
1068 { kText3, 2, 1, 4 },
1069 { kText3, 3, 2, 4 },
1070 { kText3, 4, 2, 5 },
1071 { kText3, 5, 4, 6 },
1072 { kText3, 6, 5, 6 },
1073 { kText3, 7, 6, 6 },
1074 { kText3, 50, 6, 6 },
1077 #if defined(OS_WIN)
1078 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1079 if (base::win::GetVersion() < base::win::VERSION_VISTA)
1080 return;
1081 #endif
1083 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1084 for (size_t i = 0; i < arraysize(cases); i++) {
1085 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
1086 render_text->SetText(cases[i].text);
1088 size_t next = render_text->IndexOfAdjacentGrapheme(cases[i].index,
1089 CURSOR_FORWARD);
1090 EXPECT_EQ(cases[i].expected_next, next);
1091 EXPECT_TRUE(render_text->IsValidCursorIndex(next));
1093 size_t previous = render_text->IndexOfAdjacentGrapheme(cases[i].index,
1094 CURSOR_BACKWARD);
1095 EXPECT_EQ(cases[i].expected_previous, previous);
1096 EXPECT_TRUE(render_text->IsValidCursorIndex(previous));
1100 TEST_F(RenderTextTest, MidGraphemeSelectionBounds) {
1101 #if defined(OS_WIN)
1102 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1103 if (base::win::GetVersion() < base::win::VERSION_VISTA)
1104 return;
1105 #endif
1107 // Test that selection bounds may be set amid multi-character graphemes.
1108 const base::string16 kHindi = WideToUTF16(L"\x0915\x093f");
1109 const base::string16 kThai = WideToUTF16(L"\x0e08\x0e33");
1110 const base::string16 cases[] = { kHindi, kThai };
1112 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1113 for (size_t i = 0; i < arraysize(cases); i++) {
1114 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
1115 render_text->SetText(cases[i]);
1116 EXPECT_TRUE(render_text->IsValidLogicalIndex(1));
1117 EXPECT_FALSE(render_text->IsValidCursorIndex(1));
1118 EXPECT_TRUE(render_text->SelectRange(Range(2, 1)));
1119 EXPECT_EQ(Range(2, 1), render_text->selection());
1120 EXPECT_EQ(1U, render_text->cursor_position());
1121 // Although selection bounds may be set within a multi-character grapheme,
1122 // cursor movement (e.g. via arrow key) should avoid those indices.
1123 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1124 EXPECT_EQ(0U, render_text->cursor_position());
1125 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1126 EXPECT_EQ(2U, render_text->cursor_position());
1130 TEST_F(RenderTextTest, FindCursorPosition) {
1131 const wchar_t* kTestStrings[] = { kLtrRtl, kLtrRtlLtr, kRtlLtr, kRtlLtrRtl };
1132 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1133 render_text->SetDisplayRect(Rect(0, 0, 100, 20));
1134 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1135 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS "]", i));
1136 render_text->SetText(WideToUTF16(kTestStrings[i]));
1137 for(size_t j = 0; j < render_text->text().length(); ++j) {
1138 const Range range(render_text->GetGlyphBounds(j));
1139 // Test a point just inside the leading edge of the glyph bounds.
1140 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1;
1141 EXPECT_EQ(j, render_text->FindCursorPosition(Point(x, 0)).caret_pos());
1145 #endif // !defined(OS_MACOSX)
1147 TEST_F(RenderTextTest, EdgeSelectionModels) {
1148 // Simple Latin text.
1149 const base::string16 kLatin = WideToUTF16(L"abc");
1150 // LTR 2-character grapheme.
1151 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f");
1152 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
1153 const base::string16 kHindiLatin =
1154 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f");
1155 // RTL 2-character grapheme.
1156 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8");
1157 // RTL 2-character grapheme, LTR a, RTL 2-character grapheme.
1158 const base::string16 kHebrewLatin =
1159 WideToUTF16(L"\x05e0\x05b8" L"a" L"\x05e0\x05b8");
1161 struct {
1162 base::string16 text;
1163 base::i18n::TextDirection expected_text_direction;
1164 } cases[] = {
1165 { base::string16(), base::i18n::LEFT_TO_RIGHT },
1166 { kLatin, base::i18n::LEFT_TO_RIGHT },
1167 { kLTRGrapheme, base::i18n::LEFT_TO_RIGHT },
1168 { kHindiLatin, base::i18n::LEFT_TO_RIGHT },
1169 { kRTLGrapheme, base::i18n::RIGHT_TO_LEFT },
1170 { kHebrewLatin, base::i18n::RIGHT_TO_LEFT },
1173 #if defined(OS_WIN)
1174 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1175 if (base::win::GetVersion() < base::win::VERSION_VISTA)
1176 return;
1177 #endif
1179 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1180 for (size_t i = 0; i < arraysize(cases); i++) {
1181 render_text->SetText(cases[i].text);
1182 bool ltr = (cases[i].expected_text_direction == base::i18n::LEFT_TO_RIGHT);
1184 SelectionModel start_edge =
1185 render_text->EdgeSelectionModel(ltr ? CURSOR_LEFT : CURSOR_RIGHT);
1186 EXPECT_EQ(start_edge, SelectionModel(0, CURSOR_BACKWARD));
1188 SelectionModel end_edge =
1189 render_text->EdgeSelectionModel(ltr ? CURSOR_RIGHT : CURSOR_LEFT);
1190 EXPECT_EQ(end_edge, SelectionModel(cases[i].text.length(), CURSOR_FORWARD));
1194 TEST_F(RenderTextTest, SelectAll) {
1195 const wchar_t* const cases[] =
1196 { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl };
1198 // Ensure that SelectAll respects the |reversed| argument regardless of
1199 // application locale and text content directionality.
1200 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1201 const SelectionModel expected_reversed(Range(3, 0), CURSOR_FORWARD);
1202 const SelectionModel expected_forwards(Range(0, 3), CURSOR_BACKWARD);
1203 const bool was_rtl = base::i18n::IsRTL();
1205 for (size_t i = 0; i < 2; ++i) {
1206 SetRTL(!base::i18n::IsRTL());
1207 // Test that an empty string produces an empty selection model.
1208 render_text->SetText(base::string16());
1209 EXPECT_EQ(render_text->selection_model(), SelectionModel());
1211 // Test the weak, LTR, RTL, and Bidi string cases.
1212 for (size_t j = 0; j < arraysize(cases); j++) {
1213 render_text->SetText(WideToUTF16(cases[j]));
1214 render_text->SelectAll(false);
1215 EXPECT_EQ(render_text->selection_model(), expected_forwards);
1216 render_text->SelectAll(true);
1217 EXPECT_EQ(render_text->selection_model(), expected_reversed);
1221 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1224 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
1225 #if !defined(OS_MACOSX)
1226 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) {
1227 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1228 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
1229 // Left arrow on select ranging (6, 4).
1230 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1231 EXPECT_EQ(Range(6), render_text->selection());
1232 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1233 EXPECT_EQ(Range(4), render_text->selection());
1234 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1235 EXPECT_EQ(Range(5), render_text->selection());
1236 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1237 EXPECT_EQ(Range(6), render_text->selection());
1238 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1239 EXPECT_EQ(Range(6, 5), render_text->selection());
1240 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1241 EXPECT_EQ(Range(6, 4), render_text->selection());
1242 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1243 EXPECT_EQ(Range(6), render_text->selection());
1245 // Right arrow on select ranging (4, 6).
1246 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1247 EXPECT_EQ(Range(0), render_text->selection());
1248 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1249 EXPECT_EQ(Range(1), render_text->selection());
1250 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1251 EXPECT_EQ(Range(2), render_text->selection());
1252 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1253 EXPECT_EQ(Range(3), render_text->selection());
1254 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1255 EXPECT_EQ(Range(5), render_text->selection());
1256 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1257 EXPECT_EQ(Range(4), render_text->selection());
1258 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1259 EXPECT_EQ(Range(4, 5), render_text->selection());
1260 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1261 EXPECT_EQ(Range(4, 6), render_text->selection());
1262 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1263 EXPECT_EQ(Range(4), render_text->selection());
1265 #endif // !defined(OS_MACOSX)
1267 TEST_F(RenderTextTest, CenteredDisplayOffset) {
1268 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1269 render_text->SetText(ASCIIToUTF16("abcdefghij"));
1270 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1272 const int kEnlargement = 10;
1273 const int content_width = render_text->GetContentWidth();
1274 Rect display_rect(0, 0, content_width / 2,
1275 render_text->font_list().GetHeight());
1276 render_text->SetDisplayRect(display_rect);
1278 // Move the cursor to the beginning of the text and, by checking the cursor
1279 // bounds, make sure no empty space is to the left of the text.
1280 render_text->SetCursorPosition(0);
1281 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1283 // Widen the display rect and, by checking the cursor bounds, make sure no
1284 // empty space is introduced to the left of the text.
1285 display_rect.Inset(0, 0, -kEnlargement, 0);
1286 render_text->SetDisplayRect(display_rect);
1287 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1289 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
1290 #if !defined(OS_MACOSX)
1291 // Move the cursor to the end of the text and, by checking the cursor bounds,
1292 // make sure no empty space is to the right of the text.
1293 render_text->SetCursorPosition(render_text->text().length());
1294 EXPECT_EQ(display_rect.right(),
1295 render_text->GetUpdatedCursorBounds().right());
1297 // Widen the display rect and, by checking the cursor bounds, make sure no
1298 // empty space is introduced to the right of the text.
1299 display_rect.Inset(0, 0, -kEnlargement, 0);
1300 render_text->SetDisplayRect(display_rect);
1301 EXPECT_EQ(display_rect.right(),
1302 render_text->GetUpdatedCursorBounds().right());
1303 #endif // !defined(OS_MACOSX)
1306 void MoveLeftRightByWordVerifier(RenderText* render_text,
1307 const wchar_t* str) {
1308 render_text->SetText(WideToUTF16(str));
1310 // Test moving by word from left ro right.
1311 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1312 bool first_word = true;
1313 while (true) {
1314 // First, test moving by word from a word break position, such as from
1315 // "|abc def" to "abc| def".
1316 SelectionModel start = render_text->selection_model();
1317 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1318 SelectionModel end = render_text->selection_model();
1319 if (end == start) // reach the end.
1320 break;
1322 // For testing simplicity, each word is a 3-character word.
1323 int num_of_character_moves = first_word ? 3 : 4;
1324 first_word = false;
1325 render_text->MoveCursorTo(start);
1326 for (int j = 0; j < num_of_character_moves; ++j)
1327 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1328 EXPECT_EQ(end, render_text->selection_model());
1330 // Then, test moving by word from positions inside the word, such as from
1331 // "a|bc def" to "abc| def", and from "ab|c def" to "abc| def".
1332 for (int j = 1; j < num_of_character_moves; ++j) {
1333 render_text->MoveCursorTo(start);
1334 for (int k = 0; k < j; ++k)
1335 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1336 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1337 EXPECT_EQ(end, render_text->selection_model());
1341 // Test moving by word from right to left.
1342 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1343 first_word = true;
1344 while (true) {
1345 SelectionModel start = render_text->selection_model();
1346 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1347 SelectionModel end = render_text->selection_model();
1348 if (end == start) // reach the end.
1349 break;
1351 int num_of_character_moves = first_word ? 3 : 4;
1352 first_word = false;
1353 render_text->MoveCursorTo(start);
1354 for (int j = 0; j < num_of_character_moves; ++j)
1355 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1356 EXPECT_EQ(end, render_text->selection_model());
1358 for (int j = 1; j < num_of_character_moves; ++j) {
1359 render_text->MoveCursorTo(start);
1360 for (int k = 0; k < j; ++k)
1361 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1362 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1363 EXPECT_EQ(end, render_text->selection_model());
1368 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
1369 #if !defined(OS_MACOSX)
1370 // TODO(msw): Make these work on Windows.
1371 #if !defined(OS_WIN)
1372 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText) {
1373 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1375 // For testing simplicity, each word is a 3-character word.
1376 std::vector<const wchar_t*> test;
1377 test.push_back(L"abc");
1378 test.push_back(L"abc def");
1379 test.push_back(L"\x05E1\x05E2\x05E3");
1380 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1381 test.push_back(L"abc \x05E1\x05E2\x05E3");
1382 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1383 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1384 L" \x05E7\x05E8\x05E9");
1386 test.push_back(L"abc \x05E1\x05E2\x05E3 hij");
1387 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 hij opq");
1388 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1389 L" \x05E7\x05E8\x05E9" L" opq rst uvw");
1391 test.push_back(L"\x05E1\x05E2\x05E3 abc");
1392 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 abc def");
1393 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 \x05E7\x05E8\x05E9"
1394 L" abc def hij");
1396 test.push_back(L"\x05D1\x05D2\x05D3 abc \x05E1\x05E2\x05E3");
1397 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 abc def"
1398 L" \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1399 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 \x05D7\x05D8\x05D9"
1400 L" abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1401 L" \x05E7\x05E8\x05E9");
1403 for (size_t i = 0; i < test.size(); ++i)
1404 MoveLeftRightByWordVerifier(render_text.get(), test[i]);
1407 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText_TestEndOfText) {
1408 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1410 render_text->SetText(WideToUTF16(L"ab\x05E1"));
1411 // Moving the cursor by word from "abC|" to the left should return "|abC".
1412 // But since end of text is always treated as a word break, it returns
1413 // position "ab|C".
1414 // TODO(xji): Need to make it work as expected.
1415 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1416 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1417 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1419 // Moving the cursor by word from "|abC" to the right returns "abC|".
1420 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1421 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1422 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1424 render_text->SetText(WideToUTF16(L"\x05E1\x05E2" L"a"));
1425 // For logical text "BCa", moving the cursor by word from "aCB|" to the left
1426 // returns "|aCB".
1427 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1428 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1429 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1431 // Moving the cursor by word from "|aCB" to the right should return "aCB|".
1432 // But since end of text is always treated as a word break, it returns
1433 // position "a|CB".
1434 // TODO(xji): Need to make it work as expected.
1435 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1436 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1437 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1440 TEST_F(RenderTextTest, MoveLeftRightByWordInTextWithMultiSpaces) {
1441 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1442 render_text->SetText(WideToUTF16(L"abc def"));
1443 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1444 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1445 EXPECT_EQ(11U, render_text->cursor_position());
1447 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1448 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1449 EXPECT_EQ(0U, render_text->cursor_position());
1451 #endif // !defined(OS_WIN)
1453 TEST_F(RenderTextTest, MoveLeftRightByWordInChineseText) {
1454 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1455 render_text->SetText(WideToUTF16(L"\x6211\x4EEC\x53BB\x516C\x56ED\x73A9"));
1456 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1457 EXPECT_EQ(0U, render_text->cursor_position());
1458 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1459 EXPECT_EQ(2U, render_text->cursor_position());
1460 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1461 EXPECT_EQ(3U, render_text->cursor_position());
1462 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1463 EXPECT_EQ(5U, render_text->cursor_position());
1464 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1465 EXPECT_EQ(6U, render_text->cursor_position());
1466 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1467 EXPECT_EQ(6U, render_text->cursor_position());
1469 #endif // !defined(OS_MACOSX)
1471 TEST_F(RenderTextTest, StringSizeSanity) {
1472 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1473 render_text->SetText(UTF8ToUTF16("Hello World"));
1474 const Size string_size = render_text->GetStringSize();
1475 EXPECT_GT(string_size.width(), 0);
1476 EXPECT_GT(string_size.height(), 0);
1479 TEST_F(RenderTextTest, StringSizeLongStrings) {
1480 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1481 Size previous_string_size;
1482 for (size_t length = 10; length < 1000000; length *= 10) {
1483 render_text->SetText(base::string16(length, 'a'));
1484 const Size string_size = render_text->GetStringSize();
1485 EXPECT_GT(string_size.width(), previous_string_size.width());
1486 EXPECT_GT(string_size.height(), 0);
1487 previous_string_size = string_size;
1491 // TODO(asvitkine): This test fails because PlatformFontMac uses point font
1492 // sizes instead of pixel sizes like other implementations.
1493 #if !defined(OS_MACOSX)
1494 TEST_F(RenderTextTest, StringSizeEmptyString) {
1495 // Ascent and descent of Arial and Symbol are different on most platforms.
1496 const FontList font_list("Arial,Symbol, 16px");
1497 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1498 render_text->SetFontList(font_list);
1499 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight()));
1501 // The empty string respects FontList metrics for non-zero height
1502 // and baseline.
1503 render_text->SetText(base::string16());
1504 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1505 EXPECT_EQ(0, render_text->GetStringSize().width());
1506 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1508 render_text->SetText(UTF8ToUTF16(" "));
1509 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1510 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1512 #endif // !defined(OS_MACOSX)
1514 TEST_F(RenderTextTest, StringSizeRespectsFontListMetrics) {
1515 // Check that Arial and Symbol have different font metrics.
1516 Font arial_font("Arial", 16);
1517 ASSERT_EQ("arial",
1518 base::StringToLowerASCII(arial_font.GetActualFontNameForTesting()));
1519 Font symbol_font("Symbol", 16);
1520 ASSERT_EQ("symbol",
1521 base::StringToLowerASCII(
1522 symbol_font.GetActualFontNameForTesting()));
1523 EXPECT_NE(arial_font.GetHeight(), symbol_font.GetHeight());
1524 EXPECT_NE(arial_font.GetBaseline(), symbol_font.GetBaseline());
1525 // "a" should be rendered with Arial, not with Symbol.
1526 const char* arial_font_text = "a";
1527 // "®" (registered trademark symbol) should be rendered with Symbol,
1528 // not with Arial.
1529 const char* symbol_font_text = "\xC2\xAE";
1531 Font smaller_font = arial_font;
1532 Font larger_font = symbol_font;
1533 const char* smaller_font_text = arial_font_text;
1534 const char* larger_font_text = symbol_font_text;
1535 if (symbol_font.GetHeight() < arial_font.GetHeight() &&
1536 symbol_font.GetBaseline() < arial_font.GetBaseline()) {
1537 std::swap(smaller_font, larger_font);
1538 std::swap(smaller_font_text, larger_font_text);
1540 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight());
1541 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline());
1543 // Check |smaller_font_text| is rendered with the smaller font.
1544 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1545 render_text->SetText(UTF8ToUTF16(smaller_font_text));
1546 render_text->SetFontList(FontList(smaller_font));
1547 render_text->SetDisplayRect(Rect(0, 0, 0,
1548 render_text->font_list().GetHeight()));
1549 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height());
1550 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline());
1552 // Layout the same text with mixed fonts. The text should be rendered with
1553 // the smaller font, but the height and baseline are determined with the
1554 // metrics of the font list, which is equal to the larger font.
1555 std::vector<Font> fonts;
1556 fonts.push_back(smaller_font); // The primary font is the smaller font.
1557 fonts.push_back(larger_font);
1558 const FontList font_list(fonts);
1559 render_text->SetFontList(font_list);
1560 render_text->SetDisplayRect(Rect(0, 0, 0,
1561 render_text->font_list().GetHeight()));
1562 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height());
1563 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline());
1564 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1565 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1568 TEST_F(RenderTextTest, MinLineHeight) {
1569 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1571 render_text->SetText(ASCIIToUTF16("Hello!"));
1572 SizeF default_size = render_text->GetStringSizeF();
1573 ASSERT_NE(0, default_size.height());
1574 ASSERT_NE(0, default_size.width());
1576 render_text->SetMinLineHeight(default_size.height() / 2);
1577 EXPECT_EQ(default_size.ToString(), render_text->GetStringSizeF().ToString());
1579 render_text->SetMinLineHeight(default_size.height() * 2);
1580 SizeF taller_size = render_text->GetStringSizeF();
1581 EXPECT_EQ(default_size.height() * 2, taller_size.height());
1582 EXPECT_EQ(default_size.width(), taller_size.width());
1585 TEST_F(RenderTextTest, SetFontList) {
1586 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1587 render_text->SetFontList(FontList("Arial,Symbol, 13px"));
1588 const std::vector<Font>& fonts = render_text->font_list().GetFonts();
1589 ASSERT_EQ(2U, fonts.size());
1590 EXPECT_EQ("Arial", fonts[0].GetFontName());
1591 EXPECT_EQ("Symbol", fonts[1].GetFontName());
1592 EXPECT_EQ(13, render_text->font_list().GetFontSize());
1595 TEST_F(RenderTextTest, StringSizeBoldWidth) {
1596 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1597 render_text->SetText(UTF8ToUTF16("Hello World"));
1599 const int plain_width = render_text->GetStringSize().width();
1600 EXPECT_GT(plain_width, 0);
1602 // Apply a bold style and check that the new width is greater.
1603 render_text->SetStyle(BOLD, true);
1604 const int bold_width = render_text->GetStringSize().width();
1605 EXPECT_GT(bold_width, plain_width);
1607 // Now, apply a plain style over the first word only.
1608 render_text->ApplyStyle(BOLD, false, Range(0, 5));
1609 const int plain_bold_width = render_text->GetStringSize().width();
1610 EXPECT_GT(plain_bold_width, plain_width);
1611 EXPECT_LT(plain_bold_width, bold_width);
1614 TEST_F(RenderTextTest, StringSizeHeight) {
1615 base::string16 cases[] = {
1616 WideToUTF16(L"Hello World!"), // English
1617 WideToUTF16(L"\x6328\x62f6"), // Japanese
1618 WideToUTF16(L"\x0915\x093f"), // Hindi
1619 WideToUTF16(L"\x05e0\x05b8"), // Hebrew
1622 const FontList default_font_list;
1623 const FontList& larger_font_list = default_font_list.DeriveWithSizeDelta(24);
1624 EXPECT_GT(larger_font_list.GetHeight(), default_font_list.GetHeight());
1626 for (size_t i = 0; i < arraysize(cases); i++) {
1627 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1628 render_text->SetFontList(default_font_list);
1629 render_text->SetText(cases[i]);
1631 const int height1 = render_text->GetStringSize().height();
1632 EXPECT_GT(height1, 0);
1634 // Check that setting the larger font increases the height.
1635 render_text->SetFontList(larger_font_list);
1636 const int height2 = render_text->GetStringSize().height();
1637 EXPECT_GT(height2, height1);
1641 TEST_F(RenderTextTest, GetBaselineSanity) {
1642 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1643 render_text->SetText(UTF8ToUTF16("Hello World"));
1644 const int baseline = render_text->GetBaseline();
1645 EXPECT_GT(baseline, 0);
1648 TEST_F(RenderTextTest, CursorBoundsInReplacementMode) {
1649 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1650 render_text->SetText(ASCIIToUTF16("abcdefg"));
1651 render_text->SetDisplayRect(Rect(100, 17));
1652 SelectionModel sel_b(1, CURSOR_FORWARD);
1653 SelectionModel sel_c(2, CURSOR_FORWARD);
1654 Rect cursor_around_b = render_text->GetCursorBounds(sel_b, false);
1655 Rect cursor_before_b = render_text->GetCursorBounds(sel_b, true);
1656 Rect cursor_before_c = render_text->GetCursorBounds(sel_c, true);
1657 EXPECT_EQ(cursor_around_b.x(), cursor_before_b.x());
1658 EXPECT_EQ(cursor_around_b.right(), cursor_before_c.x());
1661 TEST_F(RenderTextTest, GetTextOffset) {
1662 // The default horizontal text offset differs for LTR and RTL, and is only set
1663 // when the RenderText object is created. This test will check the default in
1664 // LTR mode, and the next test will check the RTL default.
1665 const bool was_rtl = base::i18n::IsRTL();
1666 SetRTL(false);
1667 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1668 render_text->SetText(ASCIIToUTF16("abcdefg"));
1669 render_text->SetFontList(FontList("Arial, 13px"));
1671 // Set display area's size equal to the font size.
1672 const Size font_size(render_text->GetContentWidth(),
1673 render_text->font_list().GetHeight());
1674 Rect display_rect(font_size);
1675 render_text->SetDisplayRect(display_rect);
1677 Vector2d offset = render_text->GetLineOffset(0);
1678 EXPECT_TRUE(offset.IsZero());
1680 const int kEnlargementX = 2;
1681 display_rect.Inset(0, 0, -kEnlargementX, 0);
1682 render_text->SetDisplayRect(display_rect);
1684 // Check the default horizontal alignment.
1685 offset = render_text->GetLineOffset(0);
1686 EXPECT_EQ(0, offset.x());
1688 // Check explicitly setting the horizontal alignment.
1689 render_text->SetHorizontalAlignment(ALIGN_LEFT);
1690 offset = render_text->GetLineOffset(0);
1691 EXPECT_EQ(0, offset.x());
1692 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1693 offset = render_text->GetLineOffset(0);
1694 EXPECT_EQ(kEnlargementX / 2, offset.x());
1695 render_text->SetHorizontalAlignment(ALIGN_RIGHT);
1696 offset = render_text->GetLineOffset(0);
1697 EXPECT_EQ(kEnlargementX, offset.x());
1699 // Check that text is vertically centered within taller display rects.
1700 const int kEnlargementY = display_rect.height();
1701 display_rect.Inset(0, 0, 0, -kEnlargementY);
1702 render_text->SetDisplayRect(display_rect);
1703 const Vector2d prev_offset = render_text->GetLineOffset(0);
1704 display_rect.Inset(0, 0, 0, -2 * kEnlargementY);
1705 render_text->SetDisplayRect(display_rect);
1706 offset = render_text->GetLineOffset(0);
1707 EXPECT_EQ(prev_offset.y() + kEnlargementY, offset.y());
1709 SetRTL(was_rtl);
1712 TEST_F(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL) {
1713 // This only checks the default horizontal alignment in RTL mode; all other
1714 // GetLineOffset(0) attributes are checked by the test above.
1715 const bool was_rtl = base::i18n::IsRTL();
1716 SetRTL(true);
1717 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1718 render_text->SetText(ASCIIToUTF16("abcdefg"));
1719 render_text->SetFontList(FontList("Arial, 13px"));
1720 const int kEnlargement = 2;
1721 const Size font_size(render_text->GetContentWidth() + kEnlargement,
1722 render_text->GetStringSize().height());
1723 Rect display_rect(font_size);
1724 render_text->SetDisplayRect(display_rect);
1725 Vector2d offset = render_text->GetLineOffset(0);
1726 EXPECT_EQ(kEnlargement, offset.x());
1727 SetRTL(was_rtl);
1730 TEST_F(RenderTextTest, SetDisplayOffset) {
1731 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1732 render_text->SetText(ASCIIToUTF16("abcdefg"));
1733 render_text->SetFontList(FontList("Arial, 13px"));
1735 const Size font_size(render_text->GetContentWidth(),
1736 render_text->font_list().GetHeight());
1737 const int kEnlargement = 10;
1739 // Set display width |kEnlargement| pixels greater than content width and test
1740 // different possible situations. In this case the only possible display
1741 // offset is zero.
1742 Rect display_rect(font_size);
1743 display_rect.Inset(0, 0, -kEnlargement, 0);
1744 render_text->SetDisplayRect(display_rect);
1746 struct {
1747 HorizontalAlignment alignment;
1748 int offset;
1749 } small_content_cases[] = {
1750 { ALIGN_LEFT, -kEnlargement },
1751 { ALIGN_LEFT, 0 },
1752 { ALIGN_LEFT, kEnlargement },
1753 { ALIGN_RIGHT, -kEnlargement },
1754 { ALIGN_RIGHT, 0 },
1755 { ALIGN_RIGHT, kEnlargement },
1756 { ALIGN_CENTER, -kEnlargement },
1757 { ALIGN_CENTER, 0 },
1758 { ALIGN_CENTER, kEnlargement },
1761 for (size_t i = 0; i < arraysize(small_content_cases); i++) {
1762 render_text->SetHorizontalAlignment(small_content_cases[i].alignment);
1763 render_text->SetDisplayOffset(small_content_cases[i].offset);
1764 EXPECT_EQ(0, render_text->GetUpdatedDisplayOffset().x());
1767 // Set display width |kEnlargement| pixels less than content width and test
1768 // different possible situations.
1769 display_rect = Rect(font_size);
1770 display_rect.Inset(0, 0, kEnlargement, 0);
1771 render_text->SetDisplayRect(display_rect);
1773 struct {
1774 HorizontalAlignment alignment;
1775 int offset;
1776 int expected_offset;
1777 } large_content_cases[] = {
1778 // When text is left-aligned, display offset can be in range
1779 // [-kEnlargement, 0].
1780 { ALIGN_LEFT, -2 * kEnlargement, -kEnlargement },
1781 { ALIGN_LEFT, -kEnlargement / 2, -kEnlargement / 2 },
1782 { ALIGN_LEFT, kEnlargement, 0 },
1783 // When text is right-aligned, display offset can be in range
1784 // [0, kEnlargement].
1785 { ALIGN_RIGHT, -kEnlargement, 0 },
1786 { ALIGN_RIGHT, kEnlargement / 2, kEnlargement / 2 },
1787 { ALIGN_RIGHT, 2 * kEnlargement, kEnlargement },
1788 // When text is center-aligned, display offset can be in range
1789 // [-kEnlargement / 2 - 1, (kEnlargement - 1) / 2].
1790 { ALIGN_CENTER, -kEnlargement, -kEnlargement / 2 - 1 },
1791 { ALIGN_CENTER, -kEnlargement / 4, -kEnlargement / 4 },
1792 { ALIGN_CENTER, kEnlargement / 4, kEnlargement / 4 },
1793 { ALIGN_CENTER, kEnlargement, (kEnlargement - 1) / 2 },
1796 for (size_t i = 0; i < arraysize(large_content_cases); i++) {
1797 render_text->SetHorizontalAlignment(large_content_cases[i].alignment);
1798 render_text->SetDisplayOffset(large_content_cases[i].offset);
1799 EXPECT_EQ(large_content_cases[i].expected_offset,
1800 render_text->GetUpdatedDisplayOffset().x());
1804 TEST_F(RenderTextTest, SameFontForParentheses) {
1805 struct {
1806 const base::char16 left_char;
1807 const base::char16 right_char;
1808 } punctuation_pairs[] = {
1809 { '(', ')' },
1810 { '{', '}' },
1811 { '<', '>' },
1813 struct {
1814 base::string16 text;
1815 } cases[] = {
1816 // English(English)
1817 { WideToUTF16(L"Hello World(a)") },
1818 // English(English)English
1819 { WideToUTF16(L"Hello World(a)Hello World") },
1821 // Japanese(English)
1822 { WideToUTF16(L"\x6328\x62f6(a)") },
1823 // Japanese(English)Japanese
1824 { WideToUTF16(L"\x6328\x62f6(a)\x6328\x62f6") },
1825 // English(Japanese)English
1826 { WideToUTF16(L"Hello World(\x6328\x62f6)Hello World") },
1828 // Hindi(English)
1829 { WideToUTF16(L"\x0915\x093f(a)") },
1830 // Hindi(English)Hindi
1831 { WideToUTF16(L"\x0915\x093f(a)\x0915\x093f") },
1832 // English(Hindi)English
1833 { WideToUTF16(L"Hello World(\x0915\x093f)Hello World") },
1835 // Hebrew(English)
1836 { WideToUTF16(L"\x05e0\x05b8(a)") },
1837 // Hebrew(English)Hebrew
1838 { WideToUTF16(L"\x05e0\x05b8(a)\x05e0\x05b8") },
1839 // English(Hebrew)English
1840 { WideToUTF16(L"Hello World(\x05e0\x05b8)Hello World") },
1843 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1844 for (size_t i = 0; i < arraysize(cases); ++i) {
1845 base::string16 text = cases[i].text;
1846 const size_t start_paren_char_index = text.find('(');
1847 ASSERT_NE(base::string16::npos, start_paren_char_index);
1848 const size_t end_paren_char_index = text.find(')');
1849 ASSERT_NE(base::string16::npos, end_paren_char_index);
1851 for (size_t j = 0; j < arraysize(punctuation_pairs); ++j) {
1852 text[start_paren_char_index] = punctuation_pairs[j].left_char;
1853 text[end_paren_char_index] = punctuation_pairs[j].right_char;
1854 render_text->SetText(text);
1856 const std::vector<RenderText::FontSpan> spans =
1857 render_text->GetFontSpansForTesting();
1859 int start_paren_span_index = -1;
1860 int end_paren_span_index = -1;
1861 for (size_t k = 0; k < spans.size(); ++k) {
1862 if (IndexInRange(spans[k].second, start_paren_char_index))
1863 start_paren_span_index = k;
1864 if (IndexInRange(spans[k].second, end_paren_char_index))
1865 end_paren_span_index = k;
1867 ASSERT_NE(-1, start_paren_span_index);
1868 ASSERT_NE(-1, end_paren_span_index);
1870 const Font& start_font = spans[start_paren_span_index].first;
1871 const Font& end_font = spans[end_paren_span_index].first;
1872 EXPECT_EQ(start_font.GetFontName(), end_font.GetFontName());
1873 EXPECT_EQ(start_font.GetFontSize(), end_font.GetFontSize());
1874 EXPECT_EQ(start_font.GetStyle(), end_font.GetStyle());
1879 // Make sure the caret width is always >=1 so that the correct
1880 // caret is drawn at high DPI. crbug.com/164100.
1881 TEST_F(RenderTextTest, CaretWidth) {
1882 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1883 render_text->SetText(ASCIIToUTF16("abcdefg"));
1884 EXPECT_GE(render_text->GetUpdatedCursorBounds().width(), 1);
1887 TEST_F(RenderTextTest, SelectWord) {
1888 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1889 render_text->SetText(ASCIIToUTF16(" foo a.bc.d bar"));
1891 struct {
1892 size_t cursor;
1893 size_t selection_start;
1894 size_t selection_end;
1895 } cases[] = {
1896 { 0, 0, 1 },
1897 { 1, 1, 4 },
1898 { 2, 1, 4 },
1899 { 3, 1, 4 },
1900 { 4, 4, 6 },
1901 { 5, 4, 6 },
1902 { 6, 6, 7 },
1903 { 7, 7, 8 },
1904 { 8, 8, 10 },
1905 { 9, 8, 10 },
1906 { 10, 10, 11 },
1907 { 11, 11, 12 },
1908 { 12, 12, 13 },
1909 { 13, 13, 16 },
1910 { 14, 13, 16 },
1911 { 15, 13, 16 },
1912 { 16, 13, 16 },
1915 for (size_t i = 0; i < arraysize(cases); ++i) {
1916 render_text->SetCursorPosition(cases[i].cursor);
1917 render_text->SelectWord();
1918 EXPECT_EQ(Range(cases[i].selection_start, cases[i].selection_end),
1919 render_text->selection());
1923 // Make sure the last word is selected when the cursor is at text.length().
1924 TEST_F(RenderTextTest, LastWordSelected) {
1925 const std::string kTestURL1 = "http://www.google.com";
1926 const std::string kTestURL2 = "http://www.google.com/something/";
1928 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1930 render_text->SetText(ASCIIToUTF16(kTestURL1));
1931 render_text->SetCursorPosition(kTestURL1.length());
1932 render_text->SelectWord();
1933 EXPECT_EQ(ASCIIToUTF16("com"), GetSelectedText(render_text.get()));
1934 EXPECT_FALSE(render_text->selection().is_reversed());
1936 render_text->SetText(ASCIIToUTF16(kTestURL2));
1937 render_text->SetCursorPosition(kTestURL2.length());
1938 render_text->SelectWord();
1939 EXPECT_EQ(ASCIIToUTF16("/"), GetSelectedText(render_text.get()));
1940 EXPECT_FALSE(render_text->selection().is_reversed());
1943 // When given a non-empty selection, SelectWord should expand the selection to
1944 // nearest word boundaries.
1945 TEST_F(RenderTextTest, SelectMultipleWords) {
1946 const std::string kTestURL = "http://www.google.com";
1948 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1950 render_text->SetText(ASCIIToUTF16(kTestURL));
1951 render_text->SelectRange(Range(16, 20));
1952 render_text->SelectWord();
1953 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1954 EXPECT_FALSE(render_text->selection().is_reversed());
1956 // SelectWord should preserve the selection direction.
1957 render_text->SelectRange(Range(20, 16));
1958 render_text->SelectWord();
1959 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1960 EXPECT_TRUE(render_text->selection().is_reversed());
1963 // TODO(asvitkine): RenderTextMac cursor movements. http://crbug.com/131618
1964 #if !defined(OS_MACOSX)
1965 TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) {
1966 ASSERT_FALSE(base::i18n::IsRTL());
1967 ASSERT_FALSE(base::i18n::ICUIsRTL());
1969 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1970 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1971 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1972 CURSOR_FORWARD));
1973 int width = render_text->GetStringSize().width();
1974 ASSERT_GT(width, 10);
1976 // Ensure that the cursor is placed at the width of its preceding text.
1977 render_text->SetDisplayRect(Rect(width + 10, 1));
1978 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1980 // Ensure that shrinking the display rectangle keeps the cursor in view.
1981 render_text->SetDisplayRect(Rect(width - 10, 1));
1982 EXPECT_EQ(render_text->display_rect().width(),
1983 render_text->GetUpdatedCursorBounds().right());
1985 // Ensure that the text will pan to fill its expanding display rectangle.
1986 render_text->SetDisplayRect(Rect(width - 5, 1));
1987 EXPECT_EQ(render_text->display_rect().width(),
1988 render_text->GetUpdatedCursorBounds().right());
1990 // Ensure that a sufficiently large display rectangle shows all the text.
1991 render_text->SetDisplayRect(Rect(width + 10, 1));
1992 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1994 // Repeat the test with RTL text.
1995 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1996 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1997 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1998 width = render_text->GetStringSize().width();
1999 ASSERT_GT(width, 10);
2001 // Ensure that the cursor is placed at the width of its preceding text.
2002 render_text->SetDisplayRect(Rect(width + 10, 1));
2003 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
2005 // Ensure that shrinking the display rectangle keeps the cursor in view.
2006 render_text->SetDisplayRect(Rect(width - 10, 1));
2007 EXPECT_EQ(render_text->display_rect().width(),
2008 render_text->GetUpdatedCursorBounds().right());
2010 // Ensure that the text will pan to fill its expanding display rectangle.
2011 render_text->SetDisplayRect(Rect(width - 5, 1));
2012 EXPECT_EQ(render_text->display_rect().width(),
2013 render_text->GetUpdatedCursorBounds().right());
2015 // Ensure that a sufficiently large display rectangle shows all the text.
2016 render_text->SetDisplayRect(Rect(width + 10, 1));
2017 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
2019 #endif // !defined(OS_MACOSX)
2021 TEST_F(RenderTextTest, DisplayRectShowsCursorRTL) {
2022 // Set the application default text direction to RTL.
2023 const bool was_rtl = base::i18n::IsRTL();
2024 SetRTL(true);
2026 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2027 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
2028 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
2029 int width = render_text->GetStringSize().width();
2030 ASSERT_GT(width, 10);
2032 // Ensure that the cursor is placed at the width of its preceding text.
2033 render_text->SetDisplayRect(Rect(width + 10, 1));
2034 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2035 render_text->GetUpdatedCursorBounds().x());
2037 // Ensure that shrinking the display rectangle keeps the cursor in view.
2038 render_text->SetDisplayRect(Rect(width - 10, 1));
2039 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2041 // Ensure that the text will pan to fill its expanding display rectangle.
2042 render_text->SetDisplayRect(Rect(width - 5, 1));
2043 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2045 // Ensure that a sufficiently large display rectangle shows all the text.
2046 render_text->SetDisplayRect(Rect(width + 10, 1));
2047 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2048 render_text->GetUpdatedCursorBounds().x());
2050 // Repeat the test with RTL text.
2051 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
2052 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
2053 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
2054 CURSOR_FORWARD));
2055 width = render_text->GetStringSize().width();
2056 ASSERT_GT(width, 10);
2058 // Ensure that the cursor is placed at the width of its preceding text.
2059 render_text->SetDisplayRect(Rect(width + 10, 1));
2060 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2061 render_text->GetUpdatedCursorBounds().x());
2063 // Ensure that shrinking the display rectangle keeps the cursor in view.
2064 render_text->SetDisplayRect(Rect(width - 10, 1));
2065 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2067 // Ensure that the text will pan to fill its expanding display rectangle.
2068 render_text->SetDisplayRect(Rect(width - 5, 1));
2069 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2071 // Ensure that a sufficiently large display rectangle shows all the text.
2072 render_text->SetDisplayRect(Rect(width + 10, 1));
2073 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2074 render_text->GetUpdatedCursorBounds().x());
2076 // Reset the application default text direction to LTR.
2077 SetRTL(was_rtl);
2078 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
2081 // Changing colors between or inside ligated glyphs should not break shaping.
2082 TEST_F(RenderTextTest, SelectionKeepsLigatures) {
2083 const wchar_t* kTestStrings[] = { L"\x644\x623", L"\x633\x627" };
2084 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2085 render_text->set_selection_color(SK_ColorRED);
2086 Canvas canvas;
2088 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2089 render_text->SetText(WideToUTF16(kTestStrings[i]));
2090 const int expected_width = render_text->GetStringSize().width();
2091 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD));
2092 EXPECT_EQ(expected_width, render_text->GetStringSize().width());
2093 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119
2094 render_text->Draw(&canvas);
2095 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
2099 // Ensure strings wrap onto multiple lines for a small available width.
2100 TEST_F(RenderTextTest, Multiline_MinWidth) {
2101 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl,
2102 kRtlLtr, kRtlLtrRtl };
2104 RenderTextHarfBuzz render_text;
2105 render_text.SetDisplayRect(Rect(1, 1000));
2106 render_text.SetMultiline(true);
2107 render_text.SetWordWrapBehavior(WRAP_LONG_WORDS);
2108 Canvas canvas;
2110 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2111 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2112 render_text.SetText(WideToUTF16(kTestStrings[i]));
2113 render_text.Draw(&canvas);
2114 EXPECT_GT(render_text.lines_.size(), 1U);
2118 // Ensure strings wrap onto multiple lines for a normal available width.
2119 TEST_F(RenderTextTest, Multiline_NormalWidth) {
2120 const struct {
2121 const wchar_t* const text;
2122 const Range first_line_char_range;
2123 const Range second_line_char_range;
2124 bool is_ltr;
2125 } kTestStrings[] = {
2126 { L"abc defg hijkl", Range(0, 9), Range(9, 14), true },
2127 { L"qwertyzxcvbn", Range(0, 10), Range(10, 12), true },
2128 { L"\x0627\x0644\x0644\x063A\x0629 "
2129 L"\x0627\x0644\x0639\x0631\x0628\x064A\x0629",
2130 Range(0, 6), Range(6, 13), false },
2131 { L"\x062A\x0641\x0627\x062D\x05EA\x05E4\x05D5\x05D6\x05D9"
2132 L"\x05DA\x05DB\x05DD", Range(0, 4), Range(4, 12), false }
2135 RenderTextHarfBuzz render_text;
2136 // Specify the fixed width for characters to suppress the possible variations
2137 // of linebreak results.
2138 render_text.set_glyph_width_for_test(5);
2139 render_text.SetDisplayRect(Rect(50, 1000));
2140 render_text.SetMultiline(true);
2141 render_text.SetWordWrapBehavior(WRAP_LONG_WORDS);
2142 render_text.SetHorizontalAlignment(ALIGN_TO_HEAD);
2144 Canvas canvas;
2145 TestSkiaTextRenderer renderer(&canvas);
2147 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2148 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2149 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2150 render_text.EnsureLayout();
2151 render_text.DrawVisualTextInternal(&renderer);
2153 ASSERT_EQ(2U, render_text.lines_.size());
2154 ASSERT_EQ(1U, render_text.lines_[0].segments.size());
2155 EXPECT_EQ(kTestStrings[i].first_line_char_range,
2156 render_text.lines_[0].segments[0].char_range);
2157 ASSERT_EQ(1U, render_text.lines_[1].segments.size());
2158 EXPECT_EQ(kTestStrings[i].second_line_char_range,
2159 render_text.lines_[1].segments[0].char_range);
2161 std::vector<TestSkiaTextRenderer::TextLog> text_log;
2162 renderer.GetTextLogAndReset(&text_log);
2163 ASSERT_EQ(2U, text_log.size());
2164 // NOTE: this expectation compares the character length and glyph counts,
2165 // which isn't always equal. This is okay only because all the test
2166 // strings are simple (like, no compound characters nor UTF16-surrogate
2167 // pairs). Be careful in case more complicated test strings are added.
2168 EXPECT_EQ(kTestStrings[i].first_line_char_range.length(),
2169 text_log[0].glyph_count);
2170 EXPECT_EQ(kTestStrings[i].second_line_char_range.length(),
2171 text_log[1].glyph_count);
2172 EXPECT_LT(text_log[0].origin.y(), text_log[1].origin.y());
2173 if (kTestStrings[i].is_ltr) {
2174 EXPECT_EQ(0, text_log[0].origin.x());
2175 EXPECT_EQ(0, text_log[1].origin.x());
2176 } else {
2177 EXPECT_LT(0, text_log[0].origin.x());
2178 EXPECT_LT(0, text_log[1].origin.x());
2183 // Ensure strings don't wrap onto multiple lines for a sufficient available
2184 // width.
2185 TEST_F(RenderTextTest, Multiline_SufficientWidth) {
2186 const wchar_t* kTestStrings[] = { L"", L" ", L".", L" . ", L"abc", L"a b c",
2187 L"\x62E\x628\x632", L"\x62E \x628 \x632" };
2189 RenderTextHarfBuzz render_text;
2190 render_text.SetDisplayRect(Rect(1000, 1000));
2191 render_text.SetMultiline(true);
2192 Canvas canvas;
2194 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2195 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2196 render_text.SetText(WideToUTF16(kTestStrings[i]));
2197 render_text.Draw(&canvas);
2198 EXPECT_EQ(1U, render_text.lines_.size());
2202 TEST_F(RenderTextTest, Multiline_Newline) {
2203 const struct {
2204 const wchar_t* const text;
2205 const size_t lines_count;
2206 // Ranges of the characters on each line preceding the newline.
2207 const Range line_char_ranges[3];
2208 } kTestStrings[] = {
2209 {L"abc\ndef", 2ul, { Range(0, 3), Range(4, 7), Range::InvalidRange() } },
2210 {L"a \n b ", 2ul, { Range(0, 2), Range(3, 6), Range::InvalidRange() } },
2211 {L"ab\n", 2ul, { Range(0, 2), Range(), Range::InvalidRange() } },
2212 {L"a\n\nb", 3ul, { Range(0, 1), Range(), Range(3, 4) } },
2213 {L"\nab", 2ul, { Range(), Range(1, 3), Range::InvalidRange() } },
2214 {L"\n", 2ul, { Range(), Range(), Range::InvalidRange() } },
2217 RenderTextHarfBuzz render_text;
2218 render_text.SetDisplayRect(Rect(200, 1000));
2219 render_text.SetMultiline(true);
2220 Canvas canvas;
2222 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2223 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2224 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2225 render_text.Draw(&canvas);
2226 EXPECT_EQ(kTestStrings[i].lines_count, render_text.lines_.size());
2227 if (kTestStrings[i].lines_count != render_text.lines_.size())
2228 continue;
2230 for (size_t j = 0; j < kTestStrings[i].lines_count; ++j) {
2231 SCOPED_TRACE(base::StringPrintf("Line %" PRIuS "", j));
2232 VerifyLineSegments(kTestStrings[i].line_char_ranges[j],
2233 render_text.lines_[j].segments);
2238 // Make sure that multiline mode ignores elide behavior.
2239 TEST_F(RenderTextTest, Multiline_IgnoreElide) {
2240 const wchar_t kTestString[] =
2241 L"very very very long string xxxxxxxxxxxxxxxxxxxxxxxxxx";
2242 const wchar_t kEllipsis[] = L"\x2026";
2244 RenderTextHarfBuzz render_text;
2245 render_text.SetElideBehavior(ELIDE_TAIL);
2246 render_text.SetDisplayRect(Rect(20, 1000));
2247 render_text.SetText(base::WideToUTF16(kTestString));
2248 EXPECT_NE(base::string16::npos,
2249 render_text.GetDisplayText().find(base::WideToUTF16(kEllipsis)));
2251 render_text.SetMultiline(true);
2252 EXPECT_EQ(base::string16::npos,
2253 render_text.GetDisplayText().find(base::WideToUTF16(kEllipsis)));
2256 TEST_F(RenderTextTest, Multiline_NewlineCharacterReplacement) {
2257 const wchar_t* kTestStrings[] = {
2258 L"abc\ndef", L"a \n b ", L"ab\n", L"a\n\nb", L"\nab", L"\n",
2261 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2262 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2263 RenderTextHarfBuzz render_text;
2264 render_text.SetDisplayRect(Rect(200, 1000));
2265 render_text.SetText(WideToUTF16(kTestStrings[i]));
2267 base::string16 display_text = render_text.GetDisplayText();
2268 // If RenderText is not multiline, the newline characters are replaced
2269 // by symbols, therefore the character should be changed.
2270 EXPECT_NE(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText());
2272 // Setting multiline will fix this, the newline characters will be back
2273 // to the original text.
2274 render_text.SetMultiline(true);
2275 EXPECT_EQ(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText());
2279 // Ensure horizontal alignment works in multiline mode.
2280 TEST_F(RenderTextTest, Multiline_HorizontalAlignment) {
2281 const struct {
2282 const wchar_t* const text;
2283 const gfx::HorizontalAlignment alignment;
2284 } kTestStrings[] = {
2285 { L"abcdefghij\nhijkl", gfx::ALIGN_LEFT },
2286 { L"nhijkl\nabcdefghij", gfx::ALIGN_LEFT },
2287 // hebrew, 2nd line shorter
2288 { L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7\n\x5d0\x5d1\x5d2\x5d3",
2289 gfx::ALIGN_RIGHT },
2290 // hebrew, 2nd line longer
2291 { L"\x5d0\x5d1\x5d2\x5d3\n\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7",
2292 gfx::ALIGN_RIGHT },
2293 // arabic, 2nd line shorter
2294 { L"\x62a\x62b\x62c\x62d\x62e\x62f\x630\n\x660\x661\x662\x663\x664",
2295 gfx::ALIGN_RIGHT },
2296 // arabic, 2nd line longer
2297 { L"\x660\x661\x662\x663\x664\n\x62a\x62b\x62c\x62d\x62e\x62f\x630",
2298 gfx::ALIGN_RIGHT },
2300 const int kGlyphSize = 5;
2301 RenderTextHarfBuzz render_text;
2302 render_text.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
2303 render_text.set_glyph_width_for_test(kGlyphSize);
2304 render_text.SetDisplayRect(Rect(100, 1000));
2305 render_text.SetMultiline(true);
2307 Canvas canvas;
2308 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2309 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "] %ls", i,
2310 kTestStrings[i].text));
2311 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2312 render_text.Draw(&canvas);
2313 ASSERT_LE(2u, render_text.lines().size());
2314 if (kTestStrings[i].alignment == gfx::ALIGN_LEFT) {
2315 EXPECT_EQ(0, render_text.GetAlignmentOffset(0).x());
2316 EXPECT_EQ(0, render_text.GetAlignmentOffset(1).x());
2317 } else {
2318 std::vector<base::string16> lines;
2319 base::SplitString(base::WideToUTF16(kTestStrings[i].text), '\n', &lines);
2320 ASSERT_EQ(2u, lines.size());
2321 int difference = (lines[0].length() - lines[1].length()) * kGlyphSize;
2322 EXPECT_EQ(render_text.GetAlignmentOffset(0).x() + difference,
2323 render_text.GetAlignmentOffset(1).x());
2328 TEST_F(RenderTextTest, Multiline_WordWrapBehavior) {
2329 const int kGlyphSize = 5;
2330 const struct {
2331 const WordWrapBehavior behavior;
2332 const size_t num_lines;
2333 const Range char_ranges[4];
2334 } kTestScenarios[] = {
2335 { IGNORE_LONG_WORDS, 3u,
2336 { Range(0, 4), Range(4, 11), Range(11, 14), Range::InvalidRange() } },
2337 { TRUNCATE_LONG_WORDS, 3u,
2338 { Range(0, 4), Range(4, 8), Range(11, 14), Range::InvalidRange() } },
2339 { WRAP_LONG_WORDS, 4u,
2340 { Range(0, 4), Range(4, 8), Range(8, 11), Range(11, 14) } },
2341 // TODO(mukai): implement ELIDE_LONG_WORDS. It's not used right now.
2343 RenderTextHarfBuzz render_text;
2344 render_text.SetMultiline(true);
2345 render_text.SetText(ASCIIToUTF16("foo fooooo foo"));
2346 render_text.set_glyph_width_for_test(kGlyphSize);
2347 render_text.SetDisplayRect(Rect(0, 0, kGlyphSize * 4, 0));
2349 Canvas canvas;
2351 for (size_t i = 0; i < arraysize(kTestScenarios); ++i) {
2352 SCOPED_TRACE(base::StringPrintf(
2353 "kTestScenarios[%" PRIuS "] %d", i, kTestScenarios[i].behavior));
2354 render_text.SetWordWrapBehavior(kTestScenarios[i].behavior);
2355 render_text.Draw(&canvas);
2357 ASSERT_EQ(kTestScenarios[i].num_lines, render_text.lines().size());
2358 for (size_t j = 0; j < render_text.lines().size(); ++j) {
2359 SCOPED_TRACE(base::StringPrintf("%" PRIuS "-th line", j));
2360 EXPECT_EQ(kTestScenarios[i].char_ranges[j],
2361 render_text.lines()[j].segments[0].char_range);
2362 EXPECT_EQ(kTestScenarios[i].char_ranges[j].length() * kGlyphSize,
2363 render_text.lines()[j].size.width());
2368 TEST_F(RenderTextTest, NewlineWithoutMultilineFlag) {
2369 const wchar_t* kTestStrings[] = {
2370 L"abc\ndef", L"a \n b ", L"ab\n", L"a\n\nb", L"\nab", L"\n",
2373 RenderTextHarfBuzz render_text;
2374 render_text.SetDisplayRect(Rect(200, 1000));
2375 Canvas canvas;
2377 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2378 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2379 render_text.SetText(WideToUTF16(kTestStrings[i]));
2380 render_text.Draw(&canvas);
2382 EXPECT_EQ(1U, render_text.lines_.size());
2386 // Make sure the horizontal positions of runs in a line (left-to-right for
2387 // LTR languages and right-to-left for RTL languages).
2388 TEST_F(RenderTextTest, HarfBuzz_HorizontalPositions) {
2389 const struct {
2390 const wchar_t* const text;
2391 const Range first_run_char_range;
2392 const Range second_run_char_range;
2393 bool is_rtl;
2394 } kTestStrings[] = {
2395 { L"abc\x3042\x3044\x3046\x3048\x304A", Range(0, 3), Range(3, 8), false },
2396 { L"\x062A\x0641\x0627\x062D"
2397 L"\x05EA\x05E4\x05D5\x05D6\x05D9\x05DA\x05DB\x05DD",
2398 Range(0, 4), Range(4, 12), true },
2401 RenderTextHarfBuzz render_text;
2402 Canvas canvas;
2403 TestSkiaTextRenderer renderer(&canvas);
2405 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2406 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2407 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2409 render_text.EnsureLayout();
2410 const internal::TextRunList* run_list = render_text.GetRunList();
2411 ASSERT_EQ(2U, run_list->runs().size());
2412 EXPECT_EQ(kTestStrings[i].first_run_char_range, run_list->runs()[0]->range);
2413 EXPECT_EQ(kTestStrings[i].second_run_char_range,
2414 run_list->runs()[1]->range);
2415 // If it's RTL, the visual order is reversed.
2416 if (kTestStrings[i].is_rtl) {
2417 EXPECT_EQ(1U, run_list->logical_to_visual(0));
2418 EXPECT_EQ(0U, run_list->logical_to_visual(1));
2419 } else {
2420 EXPECT_EQ(0U, run_list->logical_to_visual(0));
2421 EXPECT_EQ(1U, run_list->logical_to_visual(1));
2424 render_text.DrawVisualTextInternal(&renderer);
2426 std::vector<TestSkiaTextRenderer::TextLog> text_log;
2427 renderer.GetTextLogAndReset(&text_log);
2429 EXPECT_EQ(2U, text_log.size());
2431 // Verifies the DrawText happens in the visual order and left-to-right.
2432 // If the text is RTL, the logically first run should be drawn at last.
2433 EXPECT_EQ(run_list->runs()[run_list->logical_to_visual(0)]->glyph_count,
2434 text_log[0].glyph_count);
2435 EXPECT_EQ(run_list->runs()[run_list->logical_to_visual(1)]->glyph_count,
2436 text_log[1].glyph_count);
2437 EXPECT_LT(text_log[0].origin.x(), text_log[1].origin.x());
2441 // Test TextRunHarfBuzz's cluster finding logic.
2442 TEST_F(RenderTextTest, HarfBuzz_Clusters) {
2443 struct {
2444 uint32 glyph_to_char[4];
2445 Range chars[4];
2446 Range glyphs[4];
2447 bool is_rtl;
2448 } cases[] = {
2449 { // From string "A B C D" to glyphs "a b c d".
2450 { 0, 1, 2, 3 },
2451 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2452 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2453 false
2455 { // From string "A B C D" to glyphs "d c b a".
2456 { 3, 2, 1, 0 },
2457 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2458 { Range(3, 4), Range(2, 3), Range(1, 2), Range(0, 1) },
2459 true
2461 { // From string "A B C D" to glyphs "ab c c d".
2462 { 0, 2, 2, 3 },
2463 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2464 { Range(0, 1), Range(0, 1), Range(1, 3), Range(3, 4) },
2465 false
2467 { // From string "A B C D" to glyphs "d c c ba".
2468 { 3, 2, 2, 0 },
2469 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2470 { Range(3, 4), Range(3, 4), Range(1, 3), Range(0, 1) },
2471 true
2475 internal::TextRunHarfBuzz run;
2476 run.range = Range(0, 4);
2477 run.glyph_count = 4;
2478 run.glyph_to_char.resize(4);
2480 for (size_t i = 0; i < arraysize(cases); ++i) {
2481 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 4,
2482 run.glyph_to_char.begin());
2483 run.is_rtl = cases[i].is_rtl;
2485 for (size_t j = 0; j < 4; ++j) {
2486 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2487 Range chars;
2488 Range glyphs;
2489 run.GetClusterAt(j, &chars, &glyphs);
2490 EXPECT_EQ(cases[i].chars[j], chars);
2491 EXPECT_EQ(cases[i].glyphs[j], glyphs);
2492 EXPECT_EQ(cases[i].glyphs[j], run.CharRangeToGlyphRange(chars));
2497 // Ensure that graphemes with multiple code points do not get split.
2498 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemeCases) {
2499 const wchar_t* cases[] = {
2500 // "A" with a combining umlaut, followed by a "B".
2501 L"A\x0308" L"B",
2502 // Devanagari biconsonantal conjunct "ki", followed by an "a".
2503 L"\x0915\x093f\x0905",
2504 // Thai consonant and vowel pair "cho chan" + "sara am", followed by Thai
2505 // digit 0.
2506 L"\x0e08\x0e33\x0E50",
2509 RenderTextHarfBuzz render_text;
2511 for (size_t i = 0; i < arraysize(cases); ++i) {
2512 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS, i));
2514 base::string16 text = WideToUTF16(cases[i]);
2515 render_text.SetText(text);
2516 render_text.EnsureLayout();
2517 internal::TextRunList* run_list = render_text.GetRunList();
2518 ASSERT_EQ(1U, run_list->size());
2519 internal::TextRunHarfBuzz* run = run_list->runs()[0];
2521 base::i18n::BreakIterator* iter = render_text.grapheme_iterator_.get();
2522 auto first_grapheme_bounds = run->GetGraphemeBounds(iter, 0);
2523 EXPECT_EQ(first_grapheme_bounds, run->GetGraphemeBounds(iter, 1));
2524 auto second_grapheme_bounds = run->GetGraphemeBounds(iter, 2);
2525 EXPECT_EQ(first_grapheme_bounds.end(), second_grapheme_bounds.start());
2529 // Test the partition of a multi-grapheme cluster into grapheme ranges.
2530 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemePartition) {
2531 struct {
2532 uint32 glyph_to_char[2];
2533 Range bounds[4];
2534 bool is_rtl;
2535 } cases[] = {
2536 { // From string "A B C D" to glyphs "a bcd".
2537 { 0, 1 },
2538 { Range(0, 10), Range(10, 13), Range(13, 17), Range(17, 20) },
2539 false
2541 { // From string "A B C D" to glyphs "ab cd".
2542 { 0, 2 },
2543 { Range(0, 5), Range(5, 10), Range(10, 15), Range(15, 20) },
2544 false
2546 { // From string "A B C D" to glyphs "dcb a".
2547 { 1, 0 },
2548 { Range(10, 20), Range(7, 10), Range(3, 7), Range(0, 3) },
2549 true
2551 { // From string "A B C D" to glyphs "dc ba".
2552 { 2, 0 },
2553 { Range(15, 20), Range(10, 15), Range(5, 10), Range(0, 5) },
2554 true
2558 internal::TextRunHarfBuzz run;
2559 run.range = Range(0, 4);
2560 run.glyph_count = 2;
2561 run.glyph_to_char.resize(2);
2562 run.positions.reset(new SkPoint[4]);
2563 run.width = 20;
2565 const base::string16 kString = ASCIIToUTF16("abcd");
2566 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2567 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2568 ASSERT_TRUE(iter->Init());
2570 for (size_t i = 0; i < arraysize(cases); ++i) {
2571 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 2,
2572 run.glyph_to_char.begin());
2573 run.is_rtl = cases[i].is_rtl;
2574 for (int j = 0; j < 2; ++j)
2575 run.positions[j].set(j * 10, 0);
2577 for (size_t j = 0; j < 4; ++j) {
2578 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2579 EXPECT_EQ(cases[i].bounds[j],
2580 internal::RoundRangeF(run.GetGraphemeBounds(iter.get(), j)));
2585 TEST_F(RenderTextTest, HarfBuzz_RunDirection) {
2586 RenderTextHarfBuzz render_text;
2587 const base::string16 mixed =
2588 WideToUTF16(L"\x05D0\x05D1" L"1234" L"\x05D2\x05D3");
2589 render_text.SetText(mixed);
2590 render_text.EnsureLayout();
2591 internal::TextRunList* run_list = render_text.GetRunList();
2592 ASSERT_EQ(3U, run_list->size());
2593 EXPECT_TRUE(run_list->runs()[0]->is_rtl);
2594 EXPECT_FALSE(run_list->runs()[1]->is_rtl);
2595 EXPECT_TRUE(run_list->runs()[2]->is_rtl);
2598 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks) {
2599 RenderTextHarfBuzz render_text;
2601 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2602 render_text.SetText(WideToUTF16(L"x\x25B6y"));
2603 render_text.EnsureLayout();
2604 internal::TextRunList* run_list = render_text.GetRunList();
2605 ASSERT_EQ(3U, run_list->size());
2606 EXPECT_EQ(Range(0, 1), run_list->runs()[0]->range);
2607 EXPECT_EQ(Range(1, 2), run_list->runs()[1]->range);
2608 EXPECT_EQ(Range(2, 3), run_list->runs()[2]->range);
2610 render_text.SetText(WideToUTF16(L"x \x25B6 y"));
2611 render_text.EnsureLayout();
2612 run_list = render_text.GetRunList();
2613 ASSERT_EQ(3U, run_list->size());
2614 EXPECT_EQ(Range(0, 2), run_list->runs()[0]->range);
2615 EXPECT_EQ(Range(2, 3), run_list->runs()[1]->range);
2616 EXPECT_EQ(Range(3, 5), run_list->runs()[2]->range);
2619 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByEmoji) {
2620 RenderTextHarfBuzz render_text;
2622 // \xF0\x9F\x98\x81 (U+1F601) is smile icon emoji. \xE2\x9C\xA8 (U+2728) is
2623 // a sparkle icon. Both can be drawn with color emoji fonts, so runs should be
2624 // separated. See crbug.com/448909
2625 render_text.SetText(UTF8ToUTF16("x\xF0\x9F\x98\x81y\xE2\x9C\xA8"));
2626 render_text.EnsureLayout();
2627 internal::TextRunList* run_list = render_text.GetRunList();
2628 ASSERT_EQ(4U, run_list->size());
2629 EXPECT_EQ(Range(0, 1), run_list->runs()[0]->range);
2630 // The length is 2 since U+1F601 is represented as a surrogate pair in UTF16.
2631 EXPECT_EQ(Range(1, 3), run_list->runs()[1]->range);
2632 EXPECT_EQ(Range(3, 4), run_list->runs()[2]->range);
2633 EXPECT_EQ(Range(4, 5), run_list->runs()[3]->range);
2636 TEST_F(RenderTextTest, GlyphBounds) {
2637 const wchar_t* kTestStrings[] = {
2638 L"asdf 1234 qwer", L"\x0647\x0654", L"\x0645\x0631\x062D\x0628\x0627"
2640 scoped_ptr<RenderText> render_text(new RenderTextHarfBuzz);
2642 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2643 render_text->SetText(WideToUTF16(kTestStrings[i]));
2644 render_text->EnsureLayout();
2646 for (size_t j = 0; j < render_text->text().length(); ++j)
2647 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2651 // Ensure that shaping with a non-existent font does not cause a crash.
2652 TEST_F(RenderTextTest, HarfBuzz_NonExistentFont) {
2653 RenderTextHarfBuzz render_text;
2654 render_text.SetText(ASCIIToUTF16("test"));
2655 render_text.EnsureLayout();
2656 internal::TextRunList* run_list = render_text.GetRunList();
2657 ASSERT_EQ(1U, run_list->size());
2658 internal::TextRunHarfBuzz* run = run_list->runs()[0];
2659 render_text.ShapeRunWithFont(
2660 render_text.text(), "TheFontThatDoesntExist", FontRenderParams(), run);
2663 // Ensure an empty run returns sane values to queries.
2664 TEST_F(RenderTextTest, HarfBuzz_EmptyRun) {
2665 internal::TextRunHarfBuzz run;
2666 const base::string16 kString = ASCIIToUTF16("abcdefgh");
2667 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2668 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2669 ASSERT_TRUE(iter->Init());
2671 run.range = Range(3, 8);
2672 run.glyph_count = 0;
2673 EXPECT_EQ(Range(0, 0), run.CharRangeToGlyphRange(Range(4, 5)));
2674 EXPECT_EQ(Range(0, 0),
2675 internal::RoundRangeF(run.GetGraphemeBounds(iter.get(), 4)));
2676 Range chars;
2677 Range glyphs;
2678 run.GetClusterAt(4, &chars, &glyphs);
2679 EXPECT_EQ(Range(3, 8), chars);
2680 EXPECT_EQ(Range(0, 0), glyphs);
2683 // Ensure the line breaker doesn't compute the word's width bigger than the
2684 // actual size. See http://crbug.com/470073
2685 TEST_F(RenderTextTest, HarfBuzz_WordWidthWithDiacritics) {
2686 RenderTextHarfBuzz render_text;
2687 const base::string16 kWord = WideToUTF16(L"\u0906\u092A\u0915\u0947 ");
2688 render_text.SetText(kWord);
2689 const gfx::SizeF text_size = render_text.GetStringSizeF();
2691 render_text.SetText(kWord + kWord);
2692 render_text.SetMultiline(true);
2693 EXPECT_EQ(text_size.width() * 2, render_text.GetStringSizeF().width());
2694 EXPECT_EQ(text_size.height(), render_text.GetStringSizeF().height());
2695 render_text.SetDisplayRect(gfx::Rect(0, 0, std::ceil(text_size.width()), 0));
2696 EXPECT_NEAR(text_size.width(), render_text.GetStringSizeF().width(), 1.0f);
2697 EXPECT_EQ(text_size.height() * 2, render_text.GetStringSizeF().height());
2700 // Ensure a string fits in a display rect with a width equal to the string's.
2701 TEST_F(RenderTextTest, StringFitsOwnWidth) {
2702 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2703 const base::string16 kString = ASCIIToUTF16("www.example.com");
2705 render_text->SetText(kString);
2706 render_text->ApplyStyle(BOLD, true, Range(0, 3));
2707 render_text->SetElideBehavior(ELIDE_TAIL);
2709 render_text->SetDisplayRect(Rect(0, 0, 500, 100));
2710 EXPECT_EQ(kString, render_text->GetDisplayText());
2711 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100));
2712 EXPECT_EQ(kString, render_text->GetDisplayText());
2715 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184
2716 #if !defined(OS_WIN)
2717 // Ensure that RenderText examines all of the fonts in its FontList before
2718 // falling back to other fonts.
2719 TEST_F(RenderTextTest, HarfBuzz_FontListFallback) {
2720 // Double-check that the requested fonts are present.
2721 FontList font_list("Arial, Symbol, 12px");
2722 const std::vector<Font>& fonts = font_list.GetFonts();
2723 ASSERT_EQ(2u, fonts.size());
2724 ASSERT_EQ("arial",
2725 base::StringToLowerASCII(fonts[0].GetActualFontNameForTesting()));
2726 ASSERT_EQ("symbol",
2727 base::StringToLowerASCII(fonts[1].GetActualFontNameForTesting()));
2729 // "⊕" (CIRCLED PLUS) should be rendered with Symbol rather than falling back
2730 // to some other font that's present on the system.
2731 RenderTextHarfBuzz render_text;
2732 render_text.SetFontList(font_list);
2733 render_text.SetText(UTF8ToUTF16("\xE2\x8A\x95"));
2734 const std::vector<RenderText::FontSpan> spans =
2735 render_text.GetFontSpansForTesting();
2736 ASSERT_EQ(static_cast<size_t>(1), spans.size());
2737 EXPECT_EQ("Symbol", spans[0].first.GetFontName());
2739 #endif // !defined(OS_WIN)
2741 // Ensure that the fallback fonts of the Uniscribe font are tried for shaping.
2742 #if defined(OS_WIN)
2743 TEST_F(RenderTextTest, HarfBuzz_UniscribeFallback) {
2744 RenderTextHarfBuzz render_text;
2745 PlatformFontWin* font_win = new PlatformFontWin("Meiryo", 12);
2746 // Japanese name for Meiryo. This name won't be found in the system's linked
2747 // fonts, forcing RTHB to try the Uniscribe font and its fallbacks.
2748 font_win->font_ref_->font_name_ = WideToUTF8(L"\x30e1\x30a4\x30ea\x30aa");
2749 FontList font_list((Font(font_win)));
2751 render_text.SetFontList(font_list);
2752 // Korean character "han".
2753 render_text.SetText(WideToUTF16(L"\xd55c"));
2754 render_text.EnsureLayout();
2755 internal::TextRunList* run_list = render_text.GetRunList();
2756 ASSERT_EQ(1U, run_list->size());
2757 EXPECT_EQ(0U, run_list->runs()[0]->CountMissingGlyphs());
2759 #endif // defined(OS_WIN)
2761 // Ensure that the fallback fonts offered by gfx::GetFallbackFontFamilies() are
2762 // tried. Note this test assumes the font "Arial" doesn't provide a unicode
2763 // glyph for a particular character, and that there exists a system fallback
2764 // font which does.
2765 // TODO(msw): Fallback doesn't find a glyph on Linux.
2766 #if !defined(OS_LINUX)
2767 TEST_F(RenderTextTest, HarfBuzz_UnicodeFallback) {
2768 RenderTextHarfBuzz render_text;
2769 render_text.SetFontList(FontList("Arial, 12px"));
2771 // Korean character "han".
2772 render_text.SetText(WideToUTF16(L"\xd55c"));
2773 render_text.EnsureLayout();
2774 internal::TextRunList* run_list = render_text.GetRunList();
2775 ASSERT_EQ(1U, run_list->size());
2776 EXPECT_EQ(0U, run_list->runs()[0]->CountMissingGlyphs());
2778 #endif // !defined(OS_LINUX)
2780 // Ensure that the width reported by RenderText is sufficient for drawing. Draws
2781 // to a canvas and checks if any pixel beyond the bounding rectangle is colored.
2782 TEST_F(RenderTextTest, TextDoesntClip) {
2783 const wchar_t* kTestStrings[] = {
2784 L" ",
2785 // TODO(dschuyler): Underscores draw outside GetStringSize;
2786 // crbug.com/459812. This appears to be a preexisting issue that wasn't
2787 // revealed by the prior unit tests.
2788 // L"TEST_______",
2789 L"TEST some stuff",
2790 L"WWWWWWWWWW",
2791 L"gAXAXAXAXAXAXA",
2792 // TODO(dschuyler): A-Ring draws outside GetStringSize; crbug.com/459812.
2793 // L"g\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5",
2794 L"\x0647\x0654\x0647\x0654\x0647\x0654\x0647\x0654\x0645\x0631\x062D"
2795 L"\x0628\x0627"};
2796 const Size kCanvasSize(300, 50);
2797 const int kTestSize = 10;
2799 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
2800 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2801 scoped_ptr<Canvas> canvas(
2802 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
2803 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2804 render_text->SetHorizontalAlignment(ALIGN_LEFT);
2805 render_text->SetColor(SK_ColorBLACK);
2807 for (auto string : kTestStrings) {
2808 surface->getCanvas()->clear(SK_ColorWHITE);
2809 render_text->SetText(WideToUTF16(string));
2810 const Size string_size = render_text->GetStringSize();
2811 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2));
2812 render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4));
2813 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6));
2814 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8));
2815 render_text->SetStyle(BOLD, true);
2816 render_text->SetDisplayRect(
2817 Rect(kTestSize, kTestSize, string_size.width(), string_size.height()));
2818 // Allow the RenderText to paint outside of its display rect.
2819 render_text->set_clip_to_display_rect(false);
2820 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width());
2822 render_text->Draw(canvas.get());
2823 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width());
2824 const uint32* buffer =
2825 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
2826 ASSERT_NE(nullptr, buffer);
2827 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
2828 kCanvasSize.height());
2830 #if !defined(OS_CHROMEOS)
2831 // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect.
2832 SCOPED_TRACE("TextDoesntClip Top Side");
2833 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(),
2834 kTestSize);
2835 #endif
2838 SCOPED_TRACE("TextDoesntClip Bottom Side");
2839 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0,
2840 kTestSize + string_size.height(),
2841 kCanvasSize.width(), kTestSize);
2844 SCOPED_TRACE("TextDoesntClip Left Side");
2845 #if defined(OS_WIN)
2846 // TODO(dschuyler): On Windows XP the Unicode test draws to the left edge
2847 // as if it is ignoring the SetDisplayRect shift by kTestSize. This
2848 // appears to be a preexisting issue that wasn't revealed by the prior
2849 // unit tests.
2850 #elif defined(OS_MACOSX) || defined(OS_CHROMEOS)
2851 // TODO(dschuyler): On Windows (non-XP), Chrome OS and Mac smoothing draws
2852 // left of text. This appears to be a preexisting issue that wasn't
2853 // revealed by the prior unit tests.
2854 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize - 1,
2855 string_size.height());
2856 #else
2857 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize,
2858 string_size.height());
2859 #endif
2862 SCOPED_TRACE("TextDoesntClip Right Side");
2863 #if !defined(OS_MACOSX)
2864 // TODO(dschuyler): On Mac text draws to right of GetStringSize. This
2865 // appears to be a preexisting issue that wasn't revealed by the prior
2866 // unit tests.
2867 rect_buffer.EnsureSolidRect(SK_ColorWHITE,
2868 kTestSize + string_size.width(), kTestSize,
2869 kTestSize, string_size.height());
2870 #endif
2875 // Ensure that the text will clip to the display rect. Draws to a canvas and
2876 // checks whether any pixel beyond the bounding rectangle is colored.
2877 TEST_F(RenderTextTest, TextDoesClip) {
2878 const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"};
2879 const Size kCanvasSize(300, 50);
2880 const int kTestSize = 10;
2882 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
2883 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2884 scoped_ptr<Canvas> canvas(
2885 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
2886 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2887 render_text->SetHorizontalAlignment(ALIGN_LEFT);
2888 render_text->SetColor(SK_ColorBLACK);
2890 for (auto string : kTestStrings) {
2891 surface->getCanvas()->clear(SK_ColorWHITE);
2892 render_text->SetText(WideToUTF16(string));
2893 const Size string_size = render_text->GetStringSize();
2894 int fake_width = string_size.width() / 2;
2895 int fake_height = string_size.height() / 2;
2896 render_text->SetDisplayRect(
2897 Rect(kTestSize, kTestSize, fake_width, fake_height));
2898 render_text->set_clip_to_display_rect(true);
2899 render_text->Draw(canvas.get());
2900 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width());
2901 const uint32* buffer =
2902 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
2903 ASSERT_NE(nullptr, buffer);
2904 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
2905 kCanvasSize.height());
2907 SCOPED_TRACE("TextDoesClip Top Side");
2908 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(),
2909 kTestSize);
2913 SCOPED_TRACE("TextDoesClip Bottom Side");
2914 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize + fake_height,
2915 kCanvasSize.width(), kTestSize);
2918 SCOPED_TRACE("TextDoesClip Left Side");
2919 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize,
2920 fake_height);
2923 SCOPED_TRACE("TextDoesClip Right Side");
2924 rect_buffer.EnsureSolidRect(SK_ColorWHITE, kTestSize + fake_width,
2925 kTestSize, kTestSize, fake_height);
2930 #if defined(OS_MACOSX)
2931 TEST_F(RenderTextTest, Mac_ElidedText) {
2932 RenderTextMac render_text;
2933 base::string16 text(ASCIIToUTF16("This is an example."));
2934 render_text.SetText(text);
2935 gfx::Size string_size = render_text.GetStringSize();
2936 render_text.SetDisplayRect(gfx::Rect(string_size));
2937 render_text.EnsureLayout();
2938 // NOTE: Character and glyph counts are only comparable for simple text.
2939 EXPECT_EQ(text.size(),
2940 static_cast<size_t>(CTLineGetGlyphCount(render_text.line_)));
2942 render_text.SetElideBehavior(ELIDE_TAIL);
2943 string_size.set_width(string_size.width() / 2);
2944 render_text.SetDisplayRect(gfx::Rect(string_size));
2945 render_text.EnsureLayout();
2946 CFIndex glyph_count = CTLineGetGlyphCount(render_text.line_);
2947 EXPECT_GT(text.size(), static_cast<size_t>(glyph_count));
2948 EXPECT_NE(0, glyph_count);
2950 #endif
2952 } // namespace gfx