Fix search results being clipped in app list.
[chromium-blink-merge.git] / ui / gfx / render_text_unittest.cc
blob75d2b1dc4949fa7a6baeddc88952a5f26beeee22
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 #if !defined(OS_MACOSX)
74 // Ensure cursor movement in the specified |direction| yields |expected| values.
75 void RunMoveCursorLeftRightTest(RenderText* render_text,
76 const std::vector<SelectionModel>& expected,
77 VisualCursorDirection direction) {
78 for (size_t i = 0; i < expected.size(); ++i) {
79 SCOPED_TRACE(base::StringPrintf("Going %s; expected value index %d.",
80 direction == CURSOR_LEFT ? "left" : "right", static_cast<int>(i)));
81 EXPECT_EQ(expected[i], render_text->selection_model());
82 render_text->MoveCursor(CHARACTER_BREAK, direction, false);
84 // Check that cursoring is clamped at the line edge.
85 EXPECT_EQ(expected.back(), render_text->selection_model());
86 // Check that it is the line edge.
87 render_text->MoveCursor(LINE_BREAK, direction, false);
88 EXPECT_EQ(expected.back(), render_text->selection_model());
90 #endif // !defined(OS_MACOSX)
92 // Test utility for Multiline_Newline test case. Empty |expected_range| means
93 // the blank line which has no segments. Otherwise |segments| should contain
94 // exactly one line segment whose range equals to |expected_range|.
95 void VerifyLineSegments(const Range& expected_range,
96 const std::vector<internal::LineSegment>& segments) {
97 EXPECT_EQ(expected_range.is_empty() ? 0ul : 1ul, segments.size());
98 if (!expected_range.is_empty())
99 EXPECT_EQ(expected_range, segments[0].char_range);
102 // The class which records the drawing operations so that the test case can
103 // verify where exactly the glyphs are drawn.
104 class TestSkiaTextRenderer : public internal::SkiaTextRenderer {
105 public:
106 struct TextLog {
107 TextLog() : glyph_count(0u) {}
108 PointF origin;
109 size_t glyph_count;
112 struct DecorationLog {
113 DecorationLog(int x, int y, int width, bool underline, bool strike,
114 bool diagonal_strike)
115 : x(x), y(y), width(width), underline(underline), strike(strike),
116 diagonal_strike(diagonal_strike) {}
117 int x;
118 int y;
119 int width;
120 bool underline;
121 bool strike;
122 bool diagonal_strike;
125 explicit TestSkiaTextRenderer(Canvas* canvas)
126 : internal::SkiaTextRenderer(canvas) {}
127 ~TestSkiaTextRenderer() override {}
129 void GetTextLogAndReset(std::vector<TextLog>* text_log) {
130 text_log_.swap(*text_log);
131 text_log_.clear();
134 void GetDecorationLogAndReset(std::vector<DecorationLog>* decoration_log) {
135 decoration_log_.swap(*decoration_log);
136 decoration_log_.clear();
139 private:
140 // internal::SkiaTextRenderer:
141 void DrawPosText(const SkPoint* pos,
142 const uint16* glyphs,
143 size_t glyph_count) override {
144 TextLog log_entry;
145 log_entry.glyph_count = glyph_count;
146 if (glyph_count > 0) {
147 log_entry.origin =
148 PointF(SkScalarToFloat(pos[0].x()), SkScalarToFloat(pos[0].y()));
149 for (size_t i = 1U; i < glyph_count; ++i) {
150 log_entry.origin.SetToMin(
151 PointF(SkScalarToFloat(pos[i].x()), SkScalarToFloat(pos[i].y())));
154 text_log_.push_back(log_entry);
155 internal::SkiaTextRenderer::DrawPosText(pos, glyphs, glyph_count);
158 void DrawDecorations(int x, int y, int width, bool underline, bool strike,
159 bool diagonal_strike) override {
160 decoration_log_.push_back(
161 DecorationLog(x, y, width, underline, strike, diagonal_strike));
162 internal::SkiaTextRenderer::DrawDecorations(
163 x, y, width, underline, strike, diagonal_strike);
166 std::vector<TextLog> text_log_;
167 std::vector<DecorationLog> decoration_log_;
169 DISALLOW_COPY_AND_ASSIGN(TestSkiaTextRenderer);
172 // Given a buffer to test against, this can be used to test various areas of the
173 // rectangular buffer against a specific color value.
174 class TestRectangleBuffer {
175 public:
176 TestRectangleBuffer(const wchar_t* string,
177 const SkColor* buffer,
178 uint32_t stride,
179 uint32_t row_count)
180 : string_(string),
181 buffer_(buffer),
182 stride_(stride),
183 row_count_(row_count) {}
185 // Test if any values in the rectangular area are anything other than |color|.
186 void EnsureSolidRect(SkColor color,
187 int left,
188 int top,
189 int width,
190 int height) const {
191 ASSERT_LT(top, row_count_) << string_;
192 ASSERT_LE(top + height, row_count_) << string_;
193 ASSERT_LT(left, stride_) << string_;
194 ASSERT_LE(left + width, stride_) << string_ << ", left " << left
195 << ", width " << width << ", stride_ "
196 << stride_;
197 for (int y = top; y < top + height; ++y) {
198 for (int x = left; x < left + width; ++x) {
199 SkColor buffer_color = buffer_[x + y * stride_];
200 EXPECT_EQ(color, buffer_color) << string_ << " at " << x << ", " << y;
205 private:
206 const wchar_t* string_;
207 const SkColor* buffer_;
208 int stride_;
209 int row_count_;
211 DISALLOW_COPY_AND_ASSIGN(TestRectangleBuffer);
214 } // namespace
216 class RenderTextTest : public testing::Test {
219 TEST_F(RenderTextTest, DefaultStyles) {
220 // Check the default styles applied to new instances and adjusted text.
221 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
222 EXPECT_TRUE(render_text->text().empty());
223 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
224 for (size_t i = 0; i < arraysize(cases); ++i) {
225 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLACK));
226 EXPECT_TRUE(
227 render_text->baselines().EqualsValueForTesting(NORMAL_BASELINE));
228 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
229 EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false));
230 render_text->SetText(WideToUTF16(cases[i]));
234 TEST_F(RenderTextTest, SetStyles) {
235 // Ensure custom default styles persist across setting and clearing text.
236 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
237 const SkColor color = SK_ColorRED;
238 render_text->SetColor(color);
239 render_text->SetBaselineStyle(SUPERSCRIPT);
240 render_text->SetStyle(BOLD, true);
241 render_text->SetStyle(UNDERLINE, false);
242 const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
243 for (size_t i = 0; i < arraysize(cases); ++i) {
244 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color));
245 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUPERSCRIPT));
246 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true));
247 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false));
248 render_text->SetText(WideToUTF16(cases[i]));
250 // Ensure custom default styles can be applied after text has been set.
251 if (i == 1)
252 render_text->SetStyle(STRIKE, true);
253 if (i >= 1)
254 EXPECT_TRUE(render_text->styles()[STRIKE].EqualsValueForTesting(true));
258 TEST_F(RenderTextTest, ApplyStyles) {
259 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
260 render_text->SetText(ASCIIToUTF16("012345678"));
262 // Apply a ranged color and style and check the resulting breaks.
263 render_text->ApplyColor(SK_ColorRED, Range(1, 4));
264 render_text->ApplyBaselineStyle(SUPERIOR, Range(2, 4));
265 render_text->ApplyStyle(BOLD, true, Range(2, 5));
266 std::vector<std::pair<size_t, SkColor> > expected_color;
267 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK));
268 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED));
269 expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK));
270 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
271 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_style;
272 expected_baseline_style.push_back(
273 std::pair<size_t, BaselineStyle>(0, NORMAL_BASELINE));
274 expected_baseline_style.push_back(
275 std::pair<size_t, BaselineStyle>(2, SUPERIOR));
276 expected_baseline_style.push_back(
277 std::pair<size_t, BaselineStyle>(4, NORMAL_BASELINE));
278 EXPECT_TRUE(
279 render_text->baselines().EqualsForTesting(expected_baseline_style));
280 std::vector<std::pair<size_t, bool> > expected_style;
281 expected_style.push_back(std::pair<size_t, bool>(0, false));
282 expected_style.push_back(std::pair<size_t, bool>(2, true));
283 expected_style.push_back(std::pair<size_t, bool>(5, false));
284 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style));
286 // Ensure that setting a value overrides the ranged values.
287 render_text->SetColor(SK_ColorBLUE);
288 EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE));
289 render_text->SetBaselineStyle(SUBSCRIPT);
290 EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUBSCRIPT));
291 render_text->SetStyle(BOLD, false);
292 EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false));
294 // Apply a value over the text end and check the resulting breaks (INT_MAX
295 // should be used instead of the text length for the range end)
296 const size_t text_length = render_text->text().length();
297 render_text->ApplyColor(SK_ColorRED, Range(0, text_length));
298 render_text->ApplyBaselineStyle(SUPERIOR, Range(0, text_length));
299 render_text->ApplyStyle(BOLD, true, Range(2, text_length));
300 std::vector<std::pair<size_t, SkColor> > expected_color_end;
301 expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED));
302 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end));
303 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_end;
304 expected_baseline_end.push_back(
305 std::pair<size_t, BaselineStyle>(0, SUPERIOR));
306 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline_end));
307 std::vector<std::pair<size_t, bool> > expected_style_end;
308 expected_style_end.push_back(std::pair<size_t, bool>(0, false));
309 expected_style_end.push_back(std::pair<size_t, bool>(2, true));
310 EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style_end));
312 // Ensure ranged values adjust to accommodate text length changes.
313 render_text->ApplyStyle(ITALIC, true, Range(0, 2));
314 render_text->ApplyStyle(ITALIC, true, Range(3, 6));
315 render_text->ApplyStyle(ITALIC, true, Range(7, text_length));
316 std::vector<std::pair<size_t, bool> > expected_italic;
317 expected_italic.push_back(std::pair<size_t, bool>(0, true));
318 expected_italic.push_back(std::pair<size_t, bool>(2, false));
319 expected_italic.push_back(std::pair<size_t, bool>(3, true));
320 expected_italic.push_back(std::pair<size_t, bool>(6, false));
321 expected_italic.push_back(std::pair<size_t, bool>(7, true));
322 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
324 // Changing the text should clear any breaks except for the first one.
325 render_text->SetText(ASCIIToUTF16("0123456"));
326 expected_italic.resize(1);
327 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
328 render_text->ApplyStyle(ITALIC, false, Range(2, 4));
329 render_text->SetText(ASCIIToUTF16("012345678"));
330 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
331 render_text->ApplyStyle(ITALIC, false, Range(0, 1));
332 render_text->SetText(ASCIIToUTF16("0123456"));
333 expected_italic.begin()->second = false;
334 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
335 render_text->ApplyStyle(ITALIC, true, Range(2, 4));
336 render_text->SetText(ASCIIToUTF16("012345678"));
337 EXPECT_TRUE(render_text->styles()[ITALIC].EqualsForTesting(expected_italic));
339 // TODO(tmoniuszko): Enable when RenderTextMac::IsValidCursorIndex is
340 // implemented.
341 #if !defined(OS_MACOSX)
342 // Styles shouldn't be changed mid-grapheme.
343 render_text->SetText(WideToUTF16(
344 L"0" L"\x0915\x093f" L"1" L"\x0915\x093f" L"2"));
345 render_text->ApplyStyle(UNDERLINE, true, Range(2, 5));
346 std::vector<std::pair<size_t, bool> > expected_underline;
347 expected_underline.push_back(std::pair<size_t, bool>(0, false));
348 expected_underline.push_back(std::pair<size_t, bool>(1, true));
349 expected_underline.push_back(std::pair<size_t, bool>(6, false));
350 EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsForTesting(
351 expected_underline));
352 #endif // OS_MACOSX
355 TEST_F(RenderTextTest, AppendTextKeepsStyles) {
356 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
357 // Setup basic functionality.
358 render_text->SetText(ASCIIToUTF16("abc"));
359 render_text->ApplyColor(SK_ColorRED, Range(0, 1));
360 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2));
361 render_text->ApplyStyle(UNDERLINE, true, Range(2, 3));
362 // Verify basic functionality.
363 std::vector<std::pair<size_t, SkColor>> expected_color;
364 expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED));
365 expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorBLACK));
366 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
367 std::vector<std::pair<size_t, BaselineStyle>> expected_baseline;
368 expected_baseline.push_back(
369 std::pair<size_t, BaselineStyle>(0, NORMAL_BASELINE));
370 expected_baseline.push_back(std::pair<size_t, BaselineStyle>(1, SUPERSCRIPT));
371 expected_baseline.push_back(
372 std::pair<size_t, BaselineStyle>(2, NORMAL_BASELINE));
373 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline));
374 std::vector<std::pair<size_t, bool>> expected_style;
375 expected_style.push_back(std::pair<size_t, bool>(0, false));
376 expected_style.push_back(std::pair<size_t, bool>(2, true));
377 EXPECT_TRUE(
378 render_text->styles()[UNDERLINE].EqualsForTesting(expected_style));
379 // Ensure AppendText maintains current text styles.
380 render_text->AppendText(ASCIIToUTF16("def"));
381 EXPECT_EQ(render_text->GetDisplayText(), ASCIIToUTF16("abcdef"));
382 EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
383 EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline));
384 EXPECT_TRUE(
385 render_text->styles()[UNDERLINE].EqualsForTesting(expected_style));
388 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
389 // does not implement this yet. http://crbug.com/131618
390 #if !defined(OS_MACOSX)
391 void TestVisualCursorMotionInObscuredField(RenderText* render_text,
392 const base::string16& text,
393 bool select) {
394 ASSERT_TRUE(render_text->obscured());
395 render_text->SetText(text);
396 int len = text.length();
397 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, select);
398 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
399 render_text->selection_model());
400 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, select);
401 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
402 for (int j = 1; j <= len; ++j) {
403 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, select);
404 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_BACKWARD),
405 render_text->selection_model());
407 for (int j = len - 1; j >= 0; --j) {
408 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, select);
409 EXPECT_EQ(SelectionModel(Range(select ? 0 : j, j), CURSOR_FORWARD),
410 render_text->selection_model());
412 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, select);
413 EXPECT_EQ(SelectionModel(Range(select ? 0 : len, len), CURSOR_FORWARD),
414 render_text->selection_model());
415 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, select);
416 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
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);
482 TEST_F(RenderTextTest, RevealObscuredText) {
483 const base::string16 seuss = ASCIIToUTF16("hop on pop");
484 const base::string16 no_seuss = ASCIIToUTF16("**********");
485 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
487 render_text->SetText(seuss);
488 render_text->SetObscured(true);
489 EXPECT_EQ(seuss, render_text->text());
490 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
492 // Valid reveal index and new revealed index clears previous one.
493 render_text->RenderText::SetObscuredRevealIndex(0);
494 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetDisplayText());
495 render_text->RenderText::SetObscuredRevealIndex(1);
496 EXPECT_EQ(ASCIIToUTF16("*o********"), render_text->GetDisplayText());
497 render_text->RenderText::SetObscuredRevealIndex(2);
498 EXPECT_EQ(ASCIIToUTF16("**p*******"), render_text->GetDisplayText());
500 // Invalid reveal index.
501 render_text->RenderText::SetObscuredRevealIndex(-1);
502 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
503 render_text->RenderText::SetObscuredRevealIndex(seuss.length() + 1);
504 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
506 // SetObscured clears the revealed index.
507 render_text->RenderText::SetObscuredRevealIndex(0);
508 EXPECT_EQ(ASCIIToUTF16("h*********"), render_text->GetDisplayText());
509 render_text->SetObscured(false);
510 EXPECT_EQ(seuss, render_text->GetDisplayText());
511 render_text->SetObscured(true);
512 EXPECT_EQ(no_seuss, render_text->GetDisplayText());
514 // SetText clears the revealed index.
515 render_text->SetText(ASCIIToUTF16("new"));
516 EXPECT_EQ(ASCIIToUTF16("***"), render_text->GetDisplayText());
517 render_text->RenderText::SetObscuredRevealIndex(2);
518 EXPECT_EQ(ASCIIToUTF16("**w"), render_text->GetDisplayText());
519 render_text->SetText(ASCIIToUTF16("new longer"));
520 EXPECT_EQ(ASCIIToUTF16("**********"), render_text->GetDisplayText());
522 // Text with invalid surrogates.
523 const base::char16 invalid_surrogates[] = {0xDC00, 0xD800, 'h', 'o', 'p', 0};
524 render_text->SetText(invalid_surrogates);
525 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetDisplayText());
526 render_text->RenderText::SetObscuredRevealIndex(0);
527 const base::char16 invalid_expect_0[] = {0xDC00, '*', '*', '*', '*', 0};
528 EXPECT_EQ(invalid_expect_0, render_text->GetDisplayText());
529 render_text->RenderText::SetObscuredRevealIndex(1);
530 const base::char16 invalid_expect_1[] = {'*', 0xD800, '*', '*', '*', 0};
531 EXPECT_EQ(invalid_expect_1, render_text->GetDisplayText());
532 render_text->RenderText::SetObscuredRevealIndex(2);
533 EXPECT_EQ(ASCIIToUTF16("**h**"), render_text->GetDisplayText());
535 // Text with valid surrogates before and after the reveal index.
536 const base::char16 valid_surrogates[] =
537 {0xD800, 0xDC00, 'h', 'o', 'p', 0xD800, 0xDC00, 0};
538 render_text->SetText(valid_surrogates);
539 EXPECT_EQ(ASCIIToUTF16("*****"), render_text->GetDisplayText());
540 render_text->RenderText::SetObscuredRevealIndex(0);
541 const base::char16 valid_expect_0_and_1[] =
542 {0xD800, 0xDC00, '*', '*', '*', '*', 0};
543 EXPECT_EQ(valid_expect_0_and_1, render_text->GetDisplayText());
544 render_text->RenderText::SetObscuredRevealIndex(1);
545 EXPECT_EQ(valid_expect_0_and_1, render_text->GetDisplayText());
546 render_text->RenderText::SetObscuredRevealIndex(2);
547 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text->GetDisplayText());
548 render_text->RenderText::SetObscuredRevealIndex(5);
549 const base::char16 valid_expect_5_and_6[] =
550 {'*', '*', '*', '*', 0xD800, 0xDC00, 0};
551 EXPECT_EQ(valid_expect_5_and_6, render_text->GetDisplayText());
552 render_text->RenderText::SetObscuredRevealIndex(6);
553 EXPECT_EQ(valid_expect_5_and_6, render_text->GetDisplayText());
556 TEST_F(RenderTextTest, ElidedText) {
557 // TODO(skanuj) : Add more test cases for following
558 // - RenderText styles.
559 // - Cross interaction of truncate, elide and obscure.
560 // - ElideText tests from text_elider.cc.
561 struct {
562 const wchar_t* text;
563 const wchar_t* display_text;
564 const bool elision_expected;
565 } cases[] = {
566 // Strings shorter than the elision width should be laid out in full.
567 { L"", L"" , false },
568 { L"M", L"" , false },
569 { L" . ", L" . " , false },
570 { kWeak, kWeak , false },
571 { kLtr, kLtr , false },
572 { kLtrRtl, kLtrRtl , false },
573 { kLtrRtlLtr, kLtrRtlLtr, false },
574 { kRtl, kRtl , false },
575 { kRtlLtr, kRtlLtr , false },
576 { kRtlLtrRtl, kRtlLtrRtl, false },
577 // Strings as long as the elision width should be laid out in full.
578 { L"012ab", L"012ab" , false },
579 // Long strings should be elided with an ellipsis appended at the end.
580 { L"012abc", L"012a\x2026", true },
581 { L"012ab" L"\x5d0\x5d1", L"012a\x2026", true },
582 { L"012a" L"\x5d1" L"b", L"012a\x2026", true },
583 // No RLM marker added as digits (012) have weak directionality.
584 { L"01" L"\x5d0\x5d1\x5d2", L"01\x5d0\x5d1\x2026", true },
585 // RLM marker added as "ab" have strong LTR directionality.
586 { L"ab" L"\x5d0\x5d1\x5d2", L"ab\x5d0\x5d1\x2026\x200f", true },
587 // Complex script is not handled. In this example, the "\x0915\x093f" is a
588 // compound glyph, but only half of it is elided.
589 { L"0123\x0915\x093f", L"0123\x0915\x2026", true },
590 // Surrogate pairs should be elided reasonably enough.
591 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8", false },
592 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x2026" , true },
593 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x2026" , true },
594 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x2026\x200E" , true },
595 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" , true },
598 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
599 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
600 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
602 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
603 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
604 render_text->SetElideBehavior(ELIDE_TAIL);
606 for (size_t i = 0; i < arraysize(cases); i++) {
607 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "] '%ls'", i,
608 cases[i].text));
610 // Compute expected width
611 expected_render_text->SetText(WideToUTF16(cases[i].display_text));
612 int expected_width = expected_render_text->GetContentWidth();
614 base::string16 input = WideToUTF16(cases[i].text);
615 // Extend the input text to ensure that it is wider than the display_text,
616 // and so it will get elided.
617 if (cases[i].elision_expected)
618 input.append(WideToUTF16(L" MMMMMMMMMMM"));
619 render_text->SetText(input);
620 render_text->SetDisplayRect(Rect(0, 0, expected_width, 100));
621 EXPECT_EQ(input, render_text->text());
622 EXPECT_EQ(WideToUTF16(cases[i].display_text),
623 render_text->GetDisplayText());
624 expected_render_text->SetText(base::string16());
628 TEST_F(RenderTextTest, ElidedObscuredText) {
629 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance());
630 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px"));
631 expected_render_text->SetDisplayRect(Rect(0, 0, 9999, 100));
632 expected_render_text->SetText(WideToUTF16(L"**\x2026"));
634 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
635 render_text->SetFontList(FontList("serif, Sans serif, 12px"));
636 render_text->SetElideBehavior(ELIDE_TAIL);
637 render_text->SetDisplayRect(
638 Rect(0, 0, expected_render_text->GetContentWidth(), 100));
639 render_text->SetObscured(true);
640 render_text->SetText(WideToUTF16(L"abcdef"));
641 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
642 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetDisplayText());
645 TEST_F(RenderTextTest, ElidedEmail) {
646 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
647 render_text->SetText(ASCIIToUTF16("test@example.com"));
648 const gfx::Size size = render_text->GetStringSize();
650 const base::string16 long_email =
651 ASCIIToUTF16("longemailaddresstest@example.com");
652 render_text->SetText(long_email);
653 render_text->SetElideBehavior(ELIDE_EMAIL);
654 render_text->SetDisplayRect(gfx::Rect(size));
655 EXPECT_GE(size.width(), render_text->GetStringSize().width());
656 EXPECT_GT(long_email.size(), render_text->GetDisplayText().size());
659 TEST_F(RenderTextTest, TruncatedText) {
660 struct {
661 const wchar_t* text;
662 const wchar_t* display_text;
663 } cases[] = {
664 // Strings shorter than the truncation length should be laid out in full.
665 { L"", L"" },
666 { kWeak, kWeak },
667 { kLtr, kLtr },
668 { kLtrRtl, kLtrRtl },
669 { kLtrRtlLtr, kLtrRtlLtr },
670 { kRtl, kRtl },
671 { kRtlLtr, kRtlLtr },
672 { kRtlLtrRtl, kRtlLtrRtl },
673 // Strings as long as the truncation length should be laid out in full.
674 { L"01234", L"01234" },
675 // Long strings should be truncated with an ellipsis appended at the end.
676 { L"012345", L"0123\x2026" },
677 { L"012" L" . ", L"012 \x2026" },
678 { L"012" L"abc", L"012a\x2026" },
679 { L"012" L"a" L"\x5d0\x5d1", L"012a\x2026" },
680 { L"012" L"a" L"\x5d1" L"b", L"012a\x2026" },
681 { L"012" L"\x5d0\x5d1\x5d2", L"012\x5d0\x2026" },
682 { L"012" L"\x5d0\x5d1" L"a", L"012\x5d0\x2026" },
683 { L"012" L"\x5d0" L"a" L"\x5d1", L"012\x5d0\x2026" },
684 // Surrogate pairs should be truncated reasonably enough.
685 { L"0123\x0915\x093f", L"0123\x2026" },
686 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8" },
687 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x05bc\x2026" },
688 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x05e9\x2026" },
689 { L"0123\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
690 { L"01234\x05e9\x05bc\x05c1\x05b8", L"0123\x2026" },
691 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" },
694 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
695 render_text->set_truncate_length(5);
696 for (size_t i = 0; i < arraysize(cases); i++) {
697 render_text->SetText(WideToUTF16(cases[i].text));
698 EXPECT_EQ(WideToUTF16(cases[i].text), render_text->text());
699 EXPECT_EQ(WideToUTF16(cases[i].display_text), render_text->GetDisplayText())
700 << "For case " << i << ": " << cases[i].text;
704 TEST_F(RenderTextTest, TruncatedObscuredText) {
705 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
706 render_text->set_truncate_length(3);
707 render_text->SetObscured(true);
708 render_text->SetText(WideToUTF16(L"abcdef"));
709 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text());
710 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetDisplayText());
713 TEST_F(RenderTextTest, TruncatedCursorMovementLTR) {
714 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
715 render_text->set_truncate_length(2);
716 render_text->SetText(WideToUTF16(L"abcd"));
718 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
719 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
720 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
721 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
722 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
724 std::vector<SelectionModel> expected;
725 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
726 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
727 // The cursor hops over the ellipsis and elided text to the line end.
728 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
729 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
730 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
732 expected.clear();
733 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
734 // The cursor hops over the elided text to preceeding text.
735 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
736 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
737 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
738 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
741 TEST_F(RenderTextTest, TruncatedCursorMovementRTL) {
742 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
743 render_text->set_truncate_length(2);
744 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3"));
746 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
747 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
748 EXPECT_EQ(SelectionModel(4, CURSOR_FORWARD), render_text->selection_model());
749 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
750 EXPECT_EQ(SelectionModel(0, CURSOR_BACKWARD), render_text->selection_model());
752 std::vector<SelectionModel> expected;
753 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
754 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
755 // The cursor hops over the ellipsis and elided text to the line end.
756 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
757 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
758 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
760 expected.clear();
761 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
762 // The cursor hops over the elided text to preceeding text.
763 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
764 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
765 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
766 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
769 TEST_F(RenderTextTest, GetDisplayTextDirection) {
770 struct {
771 const wchar_t* text;
772 const base::i18n::TextDirection text_direction;
773 } cases[] = {
774 // Blank strings and those with no/weak directionality default to LTR.
775 { L"", base::i18n::LEFT_TO_RIGHT },
776 { kWeak, base::i18n::LEFT_TO_RIGHT },
777 // Strings that begin with strong LTR characters.
778 { kLtr, base::i18n::LEFT_TO_RIGHT },
779 { kLtrRtl, base::i18n::LEFT_TO_RIGHT },
780 { kLtrRtlLtr, base::i18n::LEFT_TO_RIGHT },
781 // Strings that begin with strong RTL characters.
782 { kRtl, base::i18n::RIGHT_TO_LEFT },
783 { kRtlLtr, base::i18n::RIGHT_TO_LEFT },
784 { kRtlLtrRtl, base::i18n::RIGHT_TO_LEFT },
787 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
788 const bool was_rtl = base::i18n::IsRTL();
790 for (size_t i = 0; i < 2; ++i) {
791 // Toggle the application default text direction (to try each direction).
792 SetRTL(!base::i18n::IsRTL());
793 const base::i18n::TextDirection ui_direction = base::i18n::IsRTL() ?
794 base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
796 // Ensure that directionality modes yield the correct text directions.
797 for (size_t j = 0; j < arraysize(cases); j++) {
798 render_text->SetText(WideToUTF16(cases[j].text));
799 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
800 EXPECT_EQ(render_text->GetDisplayTextDirection(),cases[j].text_direction);
801 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_UI);
802 EXPECT_EQ(render_text->GetDisplayTextDirection(), ui_direction);
803 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_LTR);
804 EXPECT_EQ(render_text->GetDisplayTextDirection(),
805 base::i18n::LEFT_TO_RIGHT);
806 render_text->SetDirectionalityMode(DIRECTIONALITY_FORCE_RTL);
807 EXPECT_EQ(render_text->GetDisplayTextDirection(),
808 base::i18n::RIGHT_TO_LEFT);
812 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
814 // Ensure that text changes update the direction for DIRECTIONALITY_FROM_TEXT.
815 render_text->SetDirectionalityMode(DIRECTIONALITY_FROM_TEXT);
816 render_text->SetText(WideToUTF16(kLtr));
817 EXPECT_EQ(render_text->GetDisplayTextDirection(), base::i18n::LEFT_TO_RIGHT);
818 render_text->SetText(WideToUTF16(kRtl));
819 EXPECT_EQ(render_text->GetDisplayTextDirection(), base::i18n::RIGHT_TO_LEFT);
822 TEST_F(RenderTextTest, MoveCursorLeftRightInLtr) {
823 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
825 // Pure LTR.
826 render_text->SetText(ASCIIToUTF16("abc"));
827 // |expected| saves the expected SelectionModel when moving cursor from left
828 // to right.
829 std::vector<SelectionModel> expected;
830 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
831 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
832 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
833 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
834 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
835 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
837 expected.clear();
838 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
839 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
840 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
841 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
842 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
843 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
846 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtl) {
847 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
848 // LTR-RTL
849 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
850 // The last one is the expected END position.
851 std::vector<SelectionModel> expected;
852 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
853 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
854 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
855 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
856 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
857 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
858 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
859 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
860 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
862 expected.clear();
863 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
864 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
865 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
866 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
867 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
868 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
869 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
870 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
871 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
874 TEST_F(RenderTextTest, MoveCursorLeftRightInLtrRtlLtr) {
875 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
876 // LTR-RTL-LTR.
877 render_text->SetText(WideToUTF16(L"a" L"\x05d1" L"b"));
878 std::vector<SelectionModel> expected;
879 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
880 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
881 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
882 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
883 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
884 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
886 expected.clear();
887 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
888 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
889 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
890 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
891 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
892 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
895 TEST_F(RenderTextTest, MoveCursorLeftRightInRtl) {
896 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
897 // Pure RTL.
898 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2"));
899 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
900 std::vector<SelectionModel> expected;
902 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
903 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
904 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
905 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
906 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
907 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
909 expected.clear();
911 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
912 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
913 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
914 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
915 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
916 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
919 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtr) {
920 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
921 // RTL-LTR
922 render_text->SetText(WideToUTF16(L"\x05d0\x05d1\x05d2" L"abc"));
923 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
924 std::vector<SelectionModel> expected;
925 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
926 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
927 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
928 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
929 expected.push_back(SelectionModel(5, CURSOR_FORWARD));
930 expected.push_back(SelectionModel(4, CURSOR_FORWARD));
931 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
932 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
933 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
935 expected.clear();
936 expected.push_back(SelectionModel(6, CURSOR_FORWARD));
937 expected.push_back(SelectionModel(4, CURSOR_BACKWARD));
938 expected.push_back(SelectionModel(5, CURSOR_BACKWARD));
939 expected.push_back(SelectionModel(6, CURSOR_BACKWARD));
940 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
941 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
942 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
943 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
944 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
947 TEST_F(RenderTextTest, MoveCursorLeftRightInRtlLtrRtl) {
948 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
949 // RTL-LTR-RTL.
950 render_text->SetText(WideToUTF16(L"\x05d0" L"a" L"\x05d1"));
951 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
952 std::vector<SelectionModel> expected;
953 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
954 expected.push_back(SelectionModel(1, CURSOR_BACKWARD));
955 expected.push_back(SelectionModel(1, CURSOR_FORWARD));
956 expected.push_back(SelectionModel(3, CURSOR_BACKWARD));
957 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
958 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_LEFT);
960 expected.clear();
961 expected.push_back(SelectionModel(3, CURSOR_FORWARD));
962 expected.push_back(SelectionModel(2, CURSOR_FORWARD));
963 expected.push_back(SelectionModel(2, CURSOR_BACKWARD));
964 expected.push_back(SelectionModel(0, CURSOR_FORWARD));
965 expected.push_back(SelectionModel(0, CURSOR_BACKWARD));
966 RunMoveCursorLeftRightTest(render_text.get(), expected, CURSOR_RIGHT);
969 // TODO(xji): temporarily disable in platform Win since the complex script
970 // characters turned into empty square due to font regression. So, not able
971 // to test 2 characters belong to the same grapheme.
972 #if defined(OS_LINUX)
973 TEST_F(RenderTextTest, MoveCursorLeftRight_ComplexScript) {
974 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
976 render_text->SetText(WideToUTF16(L"\x0915\x093f\x0915\x094d\x0915"));
977 EXPECT_EQ(0U, render_text->cursor_position());
978 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
979 EXPECT_EQ(2U, render_text->cursor_position());
980 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
981 EXPECT_EQ(4U, render_text->cursor_position());
982 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
983 EXPECT_EQ(5U, render_text->cursor_position());
984 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
985 EXPECT_EQ(5U, render_text->cursor_position());
987 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
988 EXPECT_EQ(4U, render_text->cursor_position());
989 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
990 EXPECT_EQ(2U, render_text->cursor_position());
991 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
992 EXPECT_EQ(0U, render_text->cursor_position());
993 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
994 EXPECT_EQ(0U, render_text->cursor_position());
996 #endif
998 // TODO(ckocagil): Enable for RenderTextHarfBuzz. http://crbug.com/383265
999 #if defined(OS_MACOSX)
1000 TEST_F(RenderTextTest, MoveCursorLeftRight_MeiryoUILigatures) {
1001 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1002 // Meiryo UI uses single-glyph ligatures for 'ff' and 'ffi', but each letter
1003 // (code point) has unique bounds, so mid-glyph cursoring should be possible.
1004 render_text->SetFontList(FontList("Meiryo UI, 12px"));
1005 render_text->SetText(WideToUTF16(L"ff ffi"));
1006 EXPECT_EQ(0U, render_text->cursor_position());
1007 for (size_t i = 0; i < render_text->text().length(); ++i) {
1008 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1009 EXPECT_EQ(i + 1, render_text->cursor_position());
1011 EXPECT_EQ(6U, render_text->cursor_position());
1013 #endif // defined(OS_MACOSX)
1015 TEST_F(RenderTextTest, GraphemePositions) {
1016 // LTR 2-character grapheme, LTR abc, LTR 2-character grapheme.
1017 const base::string16 kText1 =
1018 WideToUTF16(L"\x0915\x093f" L"abc" L"\x0915\x093f");
1020 // LTR ab, LTR 2-character grapheme, LTR cd.
1021 const base::string16 kText2 = WideToUTF16(L"ab" L"\x0915\x093f" L"cd");
1023 // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as
1024 // two characters forming the surrogate pair 0x0001D11E.
1025 const std::string kSurrogate = "\xF0\x9D\x84\x9E";
1027 // LTR ab, UTF16 surrogate pair, LTR cd.
1028 const base::string16 kText3 = UTF8ToUTF16("ab" + kSurrogate + "cd");
1030 struct {
1031 base::string16 text;
1032 size_t index;
1033 size_t expected_previous;
1034 size_t expected_next;
1035 } cases[] = {
1036 { base::string16(), 0, 0, 0 },
1037 { base::string16(), 1, 0, 0 },
1038 { base::string16(), 50, 0, 0 },
1039 { kText1, 0, 0, 2 },
1040 { kText1, 1, 0, 2 },
1041 { kText1, 2, 0, 3 },
1042 { kText1, 3, 2, 4 },
1043 { kText1, 4, 3, 5 },
1044 { kText1, 5, 4, 7 },
1045 { kText1, 6, 5, 7 },
1046 { kText1, 7, 5, 7 },
1047 { kText1, 8, 7, 7 },
1048 { kText1, 50, 7, 7 },
1049 { kText2, 0, 0, 1 },
1050 { kText2, 1, 0, 2 },
1051 { kText2, 2, 1, 4 },
1052 { kText2, 3, 2, 4 },
1053 { kText2, 4, 2, 5 },
1054 { kText2, 5, 4, 6 },
1055 { kText2, 6, 5, 6 },
1056 { kText2, 7, 6, 6 },
1057 { kText2, 50, 6, 6 },
1058 { kText3, 0, 0, 1 },
1059 { kText3, 1, 0, 2 },
1060 { kText3, 2, 1, 4 },
1061 { kText3, 3, 2, 4 },
1062 { kText3, 4, 2, 5 },
1063 { kText3, 5, 4, 6 },
1064 { kText3, 6, 5, 6 },
1065 { kText3, 7, 6, 6 },
1066 { kText3, 50, 6, 6 },
1069 #if defined(OS_WIN)
1070 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1071 if (base::win::GetVersion() < base::win::VERSION_VISTA)
1072 return;
1073 #endif
1075 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1076 for (size_t i = 0; i < arraysize(cases); i++) {
1077 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
1078 render_text->SetText(cases[i].text);
1080 size_t next = render_text->IndexOfAdjacentGrapheme(cases[i].index,
1081 CURSOR_FORWARD);
1082 EXPECT_EQ(cases[i].expected_next, next);
1083 EXPECT_TRUE(render_text->IsValidCursorIndex(next));
1085 size_t previous = render_text->IndexOfAdjacentGrapheme(cases[i].index,
1086 CURSOR_BACKWARD);
1087 EXPECT_EQ(cases[i].expected_previous, previous);
1088 EXPECT_TRUE(render_text->IsValidCursorIndex(previous));
1092 TEST_F(RenderTextTest, MidGraphemeSelectionBounds) {
1093 #if defined(OS_WIN)
1094 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1095 if (base::win::GetVersion() < base::win::VERSION_VISTA)
1096 return;
1097 #endif
1099 // Test that selection bounds may be set amid multi-character graphemes.
1100 const base::string16 kHindi = WideToUTF16(L"\x0915\x093f");
1101 const base::string16 kThai = WideToUTF16(L"\x0e08\x0e33");
1102 const base::string16 cases[] = { kHindi, kThai };
1104 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1105 for (size_t i = 0; i < arraysize(cases); i++) {
1106 SCOPED_TRACE(base::StringPrintf("Testing cases[%" PRIuS "]", i));
1107 render_text->SetText(cases[i]);
1108 EXPECT_TRUE(render_text->IsValidLogicalIndex(1));
1109 #if !defined(OS_MACOSX)
1110 EXPECT_FALSE(render_text->IsValidCursorIndex(1));
1111 #endif
1112 EXPECT_TRUE(render_text->SelectRange(Range(2, 1)));
1113 EXPECT_EQ(Range(2, 1), render_text->selection());
1114 EXPECT_EQ(1U, render_text->cursor_position());
1115 // Although selection bounds may be set within a multi-character grapheme,
1116 // cursor movement (e.g. via arrow key) should avoid those indices.
1117 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1118 EXPECT_EQ(0U, render_text->cursor_position());
1119 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1120 EXPECT_EQ(2U, render_text->cursor_position());
1124 TEST_F(RenderTextTest, FindCursorPosition) {
1125 const wchar_t* kTestStrings[] = { kLtrRtl, kLtrRtlLtr, kRtlLtr, kRtlLtrRtl };
1126 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1127 render_text->SetDisplayRect(Rect(0, 0, 100, 20));
1128 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1129 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS "]", i));
1130 render_text->SetText(WideToUTF16(kTestStrings[i]));
1131 for(size_t j = 0; j < render_text->text().length(); ++j) {
1132 const Range range(render_text->GetGlyphBounds(j));
1133 // Test a point just inside the leading edge of the glyph bounds.
1134 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1;
1135 EXPECT_EQ(j, render_text->FindCursorPosition(Point(x, 0)).caret_pos());
1140 TEST_F(RenderTextTest, EdgeSelectionModels) {
1141 // Simple Latin text.
1142 const base::string16 kLatin = WideToUTF16(L"abc");
1143 // LTR 2-character grapheme.
1144 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f");
1145 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
1146 const base::string16 kHindiLatin =
1147 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f");
1148 // RTL 2-character grapheme.
1149 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8");
1150 // RTL 2-character grapheme, LTR a, RTL 2-character grapheme.
1151 const base::string16 kHebrewLatin =
1152 WideToUTF16(L"\x05e0\x05b8" L"a" L"\x05e0\x05b8");
1154 struct {
1155 base::string16 text;
1156 base::i18n::TextDirection expected_text_direction;
1157 } cases[] = {
1158 { base::string16(), base::i18n::LEFT_TO_RIGHT },
1159 { kLatin, base::i18n::LEFT_TO_RIGHT },
1160 { kLTRGrapheme, base::i18n::LEFT_TO_RIGHT },
1161 { kHindiLatin, base::i18n::LEFT_TO_RIGHT },
1162 { kRTLGrapheme, base::i18n::RIGHT_TO_LEFT },
1163 { kHebrewLatin, base::i18n::RIGHT_TO_LEFT },
1166 #if defined(OS_WIN)
1167 // TODO(msw): XP fails due to lack of font support: http://crbug.com/106450
1168 if (base::win::GetVersion() < base::win::VERSION_VISTA)
1169 return;
1170 #endif
1172 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1173 for (size_t i = 0; i < arraysize(cases); i++) {
1174 render_text->SetText(cases[i].text);
1175 bool ltr = (cases[i].expected_text_direction == base::i18n::LEFT_TO_RIGHT);
1177 SelectionModel start_edge =
1178 render_text->EdgeSelectionModel(ltr ? CURSOR_LEFT : CURSOR_RIGHT);
1179 EXPECT_EQ(start_edge, SelectionModel(0, CURSOR_BACKWARD));
1181 SelectionModel end_edge =
1182 render_text->EdgeSelectionModel(ltr ? CURSOR_RIGHT : CURSOR_LEFT);
1183 EXPECT_EQ(end_edge, SelectionModel(cases[i].text.length(), CURSOR_FORWARD));
1187 TEST_F(RenderTextTest, SelectAll) {
1188 const wchar_t* const cases[] =
1189 { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl };
1191 // Ensure that SelectAll respects the |reversed| argument regardless of
1192 // application locale and text content directionality.
1193 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1194 const SelectionModel expected_reversed(Range(3, 0), CURSOR_FORWARD);
1195 const SelectionModel expected_forwards(Range(0, 3), CURSOR_BACKWARD);
1196 const bool was_rtl = base::i18n::IsRTL();
1198 for (size_t i = 0; i < 2; ++i) {
1199 SetRTL(!base::i18n::IsRTL());
1200 // Test that an empty string produces an empty selection model.
1201 render_text->SetText(base::string16());
1202 EXPECT_EQ(render_text->selection_model(), SelectionModel());
1204 // Test the weak, LTR, RTL, and Bidi string cases.
1205 for (size_t j = 0; j < arraysize(cases); j++) {
1206 render_text->SetText(WideToUTF16(cases[j]));
1207 render_text->SelectAll(false);
1208 EXPECT_EQ(render_text->selection_model(), expected_forwards);
1209 render_text->SelectAll(true);
1210 EXPECT_EQ(render_text->selection_model(), expected_reversed);
1214 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1217 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) {
1218 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1219 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2"));
1220 // Left arrow on select ranging (6, 4).
1221 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1222 EXPECT_EQ(Range(6), render_text->selection());
1223 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1224 EXPECT_EQ(Range(4), render_text->selection());
1225 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1226 EXPECT_EQ(Range(5), render_text->selection());
1227 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1228 EXPECT_EQ(Range(6), render_text->selection());
1229 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1230 EXPECT_EQ(Range(6, 5), render_text->selection());
1231 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, true);
1232 EXPECT_EQ(Range(6, 4), render_text->selection());
1233 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1234 EXPECT_EQ(Range(6), render_text->selection());
1236 // Right arrow on select ranging (4, 6).
1237 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1238 EXPECT_EQ(Range(0), render_text->selection());
1239 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1240 EXPECT_EQ(Range(1), render_text->selection());
1241 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1242 EXPECT_EQ(Range(2), render_text->selection());
1243 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1244 EXPECT_EQ(Range(3), render_text->selection());
1245 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1246 EXPECT_EQ(Range(5), render_text->selection());
1247 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1248 EXPECT_EQ(Range(4), render_text->selection());
1249 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1250 EXPECT_EQ(Range(4, 5), render_text->selection());
1251 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, true);
1252 EXPECT_EQ(Range(4, 6), render_text->selection());
1253 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1254 EXPECT_EQ(Range(4), render_text->selection());
1257 TEST_F(RenderTextTest, CenteredDisplayOffset) {
1258 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1259 render_text->SetText(ASCIIToUTF16("abcdefghij"));
1260 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1262 const int kEnlargement = 10;
1263 const int content_width = render_text->GetContentWidth();
1264 Rect display_rect(0, 0, content_width / 2,
1265 render_text->font_list().GetHeight());
1266 render_text->SetDisplayRect(display_rect);
1268 // Move the cursor to the beginning of the text and, by checking the cursor
1269 // bounds, make sure no empty space is to the left of the text.
1270 render_text->SetCursorPosition(0);
1271 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1273 // Widen the display rect and, by checking the cursor bounds, make sure no
1274 // empty space is introduced to the left of the text.
1275 display_rect.Inset(0, 0, -kEnlargement, 0);
1276 render_text->SetDisplayRect(display_rect);
1277 EXPECT_EQ(display_rect.x(), render_text->GetUpdatedCursorBounds().x());
1279 // Move the cursor to the end of the text and, by checking the cursor bounds,
1280 // make sure no empty space is to the right of the text.
1281 render_text->SetCursorPosition(render_text->text().length());
1282 EXPECT_EQ(display_rect.right(),
1283 render_text->GetUpdatedCursorBounds().right());
1285 // Widen the display rect and, by checking the cursor bounds, make sure no
1286 // empty space is introduced to the right of the text.
1287 display_rect.Inset(0, 0, -kEnlargement, 0);
1288 render_text->SetDisplayRect(display_rect);
1289 EXPECT_EQ(display_rect.right(),
1290 render_text->GetUpdatedCursorBounds().right());
1292 #endif // !defined(OS_MACOSX)
1294 // TODO(xji): Make these work on Windows.
1295 #if defined(OS_LINUX)
1296 void MoveLeftRightByWordVerifier(RenderText* render_text,
1297 const wchar_t* str) {
1298 render_text->SetText(WideToUTF16(str));
1300 // Test moving by word from left ro right.
1301 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1302 bool first_word = true;
1303 while (true) {
1304 // First, test moving by word from a word break position, such as from
1305 // "|abc def" to "abc| def".
1306 SelectionModel start = render_text->selection_model();
1307 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1308 SelectionModel end = render_text->selection_model();
1309 if (end == start) // reach the end.
1310 break;
1312 // For testing simplicity, each word is a 3-character word.
1313 int num_of_character_moves = first_word ? 3 : 4;
1314 first_word = false;
1315 render_text->MoveCursorTo(start);
1316 for (int j = 0; j < num_of_character_moves; ++j)
1317 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1318 EXPECT_EQ(end, render_text->selection_model());
1320 // Then, test moving by word from positions inside the word, such as from
1321 // "a|bc def" to "abc| def", and from "ab|c def" to "abc| def".
1322 for (int j = 1; j < num_of_character_moves; ++j) {
1323 render_text->MoveCursorTo(start);
1324 for (int k = 0; k < j; ++k)
1325 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false);
1326 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1327 EXPECT_EQ(end, render_text->selection_model());
1331 // Test moving by word from right to left.
1332 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1333 first_word = true;
1334 while (true) {
1335 SelectionModel start = render_text->selection_model();
1336 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1337 SelectionModel end = render_text->selection_model();
1338 if (end == start) // reach the end.
1339 break;
1341 int num_of_character_moves = first_word ? 3 : 4;
1342 first_word = false;
1343 render_text->MoveCursorTo(start);
1344 for (int j = 0; j < num_of_character_moves; ++j)
1345 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1346 EXPECT_EQ(end, render_text->selection_model());
1348 for (int j = 1; j < num_of_character_moves; ++j) {
1349 render_text->MoveCursorTo(start);
1350 for (int k = 0; k < j; ++k)
1351 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false);
1352 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1353 EXPECT_EQ(end, render_text->selection_model());
1358 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText) {
1359 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1361 // For testing simplicity, each word is a 3-character word.
1362 std::vector<const wchar_t*> test;
1363 test.push_back(L"abc");
1364 test.push_back(L"abc def");
1365 test.push_back(L"\x05E1\x05E2\x05E3");
1366 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1367 test.push_back(L"abc \x05E1\x05E2\x05E3");
1368 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1369 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1370 L" \x05E7\x05E8\x05E9");
1372 test.push_back(L"abc \x05E1\x05E2\x05E3 hij");
1373 test.push_back(L"abc def \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 hij opq");
1374 test.push_back(L"abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1375 L" \x05E7\x05E8\x05E9" L" opq rst uvw");
1377 test.push_back(L"\x05E1\x05E2\x05E3 abc");
1378 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 abc def");
1379 test.push_back(L"\x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6 \x05E7\x05E8\x05E9"
1380 L" abc def hij");
1382 test.push_back(L"\x05D1\x05D2\x05D3 abc \x05E1\x05E2\x05E3");
1383 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 abc def"
1384 L" \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6");
1385 test.push_back(L"\x05D1\x05D2\x05D3 \x05D4\x05D5\x05D6 \x05D7\x05D8\x05D9"
1386 L" abc def hij \x05E1\x05E2\x05E3 \x05E4\x05E5\x05E6"
1387 L" \x05E7\x05E8\x05E9");
1389 for (size_t i = 0; i < test.size(); ++i)
1390 MoveLeftRightByWordVerifier(render_text.get(), test[i]);
1393 TEST_F(RenderTextTest, MoveLeftRightByWordInBidiText_TestEndOfText) {
1394 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1396 render_text->SetText(WideToUTF16(L"ab\x05E1"));
1397 // Moving the cursor by word from "abC|" to the left should return "|abC".
1398 // But since end of text is always treated as a word break, it returns
1399 // position "ab|C".
1400 // TODO(xji): Need to make it work as expected.
1401 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1402 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1403 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1405 // Moving the cursor by word from "|abC" to the right returns "abC|".
1406 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1407 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1408 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1410 render_text->SetText(WideToUTF16(L"\x05E1\x05E2" L"a"));
1411 // For logical text "BCa", moving the cursor by word from "aCB|" to the left
1412 // returns "|aCB".
1413 render_text->MoveCursor(LINE_BREAK, CURSOR_RIGHT, false);
1414 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1415 EXPECT_EQ(SelectionModel(3, CURSOR_FORWARD), render_text->selection_model());
1417 // Moving the cursor by word from "|aCB" to the right should return "aCB|".
1418 // But since end of text is always treated as a word break, it returns
1419 // position "a|CB".
1420 // TODO(xji): Need to make it work as expected.
1421 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1422 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1423 // EXPECT_EQ(SelectionModel(), render_text->selection_model());
1426 TEST_F(RenderTextTest, MoveLeftRightByWordInTextWithMultiSpaces) {
1427 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1428 render_text->SetText(WideToUTF16(L"abc def"));
1429 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1430 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1431 EXPECT_EQ(11U, render_text->cursor_position());
1433 render_text->MoveCursorTo(SelectionModel(5, CURSOR_FORWARD));
1434 render_text->MoveCursor(WORD_BREAK, CURSOR_LEFT, false);
1435 EXPECT_EQ(0U, render_text->cursor_position());
1438 TEST_F(RenderTextTest, MoveLeftRightByWordInChineseText) {
1439 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1440 render_text->SetText(WideToUTF16(L"\x6211\x4EEC\x53BB\x516C\x56ED\x73A9"));
1441 render_text->MoveCursor(LINE_BREAK, CURSOR_LEFT, false);
1442 EXPECT_EQ(0U, render_text->cursor_position());
1443 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1444 EXPECT_EQ(2U, render_text->cursor_position());
1445 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1446 EXPECT_EQ(3U, render_text->cursor_position());
1447 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1448 EXPECT_EQ(5U, render_text->cursor_position());
1449 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1450 EXPECT_EQ(6U, render_text->cursor_position());
1451 render_text->MoveCursor(WORD_BREAK, CURSOR_RIGHT, false);
1452 EXPECT_EQ(6U, render_text->cursor_position());
1454 #endif
1456 TEST_F(RenderTextTest, StringSizeSanity) {
1457 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1458 render_text->SetText(UTF8ToUTF16("Hello World"));
1459 const Size string_size = render_text->GetStringSize();
1460 EXPECT_GT(string_size.width(), 0);
1461 EXPECT_GT(string_size.height(), 0);
1464 TEST_F(RenderTextTest, StringSizeLongStrings) {
1465 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1466 Size previous_string_size;
1467 for (size_t length = 10; length < 1000000; length *= 10) {
1468 render_text->SetText(base::string16(length, 'a'));
1469 const Size string_size = render_text->GetStringSize();
1470 EXPECT_GT(string_size.width(), previous_string_size.width());
1471 EXPECT_GT(string_size.height(), 0);
1472 previous_string_size = string_size;
1476 // TODO(asvitkine): This test fails because PlatformFontMac uses point font
1477 // sizes instead of pixel sizes like other implementations.
1478 #if !defined(OS_MACOSX)
1479 TEST_F(RenderTextTest, StringSizeEmptyString) {
1480 // Ascent and descent of Arial and Symbol are different on most platforms.
1481 const FontList font_list("Arial,Symbol, 16px");
1482 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1483 render_text->SetFontList(font_list);
1484 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight()));
1486 // The empty string respects FontList metrics for non-zero height
1487 // and baseline.
1488 render_text->SetText(base::string16());
1489 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1490 EXPECT_EQ(0, render_text->GetStringSize().width());
1491 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1493 render_text->SetText(UTF8ToUTF16(" "));
1494 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1495 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1497 #endif // !defined(OS_MACOSX)
1499 TEST_F(RenderTextTest, StringSizeRespectsFontListMetrics) {
1500 // Check that Arial and Symbol have different font metrics.
1501 Font arial_font("Arial", 16);
1502 ASSERT_EQ("arial",
1503 base::StringToLowerASCII(arial_font.GetActualFontNameForTesting()));
1504 Font symbol_font("Symbol", 16);
1505 ASSERT_EQ("symbol",
1506 base::StringToLowerASCII(
1507 symbol_font.GetActualFontNameForTesting()));
1508 EXPECT_NE(arial_font.GetHeight(), symbol_font.GetHeight());
1509 EXPECT_NE(arial_font.GetBaseline(), symbol_font.GetBaseline());
1510 // "a" should be rendered with Arial, not with Symbol.
1511 const char* arial_font_text = "a";
1512 // "®" (registered trademark symbol) should be rendered with Symbol,
1513 // not with Arial.
1514 const char* symbol_font_text = "\xC2\xAE";
1516 Font smaller_font = arial_font;
1517 Font larger_font = symbol_font;
1518 const char* smaller_font_text = arial_font_text;
1519 const char* larger_font_text = symbol_font_text;
1520 if (symbol_font.GetHeight() < arial_font.GetHeight() &&
1521 symbol_font.GetBaseline() < arial_font.GetBaseline()) {
1522 std::swap(smaller_font, larger_font);
1523 std::swap(smaller_font_text, larger_font_text);
1525 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight());
1526 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline());
1528 // Check |smaller_font_text| is rendered with the smaller font.
1529 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1530 render_text->SetText(UTF8ToUTF16(smaller_font_text));
1531 render_text->SetFontList(FontList(smaller_font));
1532 render_text->SetDisplayRect(Rect(0, 0, 0,
1533 render_text->font_list().GetHeight()));
1534 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height());
1535 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline());
1537 // Layout the same text with mixed fonts. The text should be rendered with
1538 // the smaller font, but the height and baseline are determined with the
1539 // metrics of the font list, which is equal to the larger font.
1540 std::vector<Font> fonts;
1541 fonts.push_back(smaller_font); // The primary font is the smaller font.
1542 fonts.push_back(larger_font);
1543 const FontList font_list(fonts);
1544 render_text->SetFontList(font_list);
1545 render_text->SetDisplayRect(Rect(0, 0, 0,
1546 render_text->font_list().GetHeight()));
1547 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height());
1548 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline());
1549 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1550 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1553 TEST_F(RenderTextTest, MinLineHeight) {
1554 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1556 render_text->SetText(ASCIIToUTF16("Hello!"));
1557 SizeF default_size = render_text->GetStringSizeF();
1558 ASSERT_NE(0, default_size.height());
1559 ASSERT_NE(0, default_size.width());
1561 render_text->SetMinLineHeight(default_size.height() / 2);
1562 EXPECT_EQ(default_size.ToString(), render_text->GetStringSizeF().ToString());
1564 render_text->SetMinLineHeight(default_size.height() * 2);
1565 SizeF taller_size = render_text->GetStringSizeF();
1566 EXPECT_EQ(default_size.height() * 2, taller_size.height());
1567 EXPECT_EQ(default_size.width(), taller_size.width());
1570 TEST_F(RenderTextTest, SetFontList) {
1571 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1572 render_text->SetFontList(FontList("Arial,Symbol, 13px"));
1573 const std::vector<Font>& fonts = render_text->font_list().GetFonts();
1574 ASSERT_EQ(2U, fonts.size());
1575 EXPECT_EQ("Arial", fonts[0].GetFontName());
1576 EXPECT_EQ("Symbol", fonts[1].GetFontName());
1577 EXPECT_EQ(13, render_text->font_list().GetFontSize());
1580 TEST_F(RenderTextTest, StringSizeBoldWidth) {
1581 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1582 render_text->SetText(UTF8ToUTF16("Hello World"));
1584 const int plain_width = render_text->GetStringSize().width();
1585 EXPECT_GT(plain_width, 0);
1587 // Apply a bold style and check that the new width is greater.
1588 render_text->SetStyle(BOLD, true);
1589 const int bold_width = render_text->GetStringSize().width();
1590 EXPECT_GT(bold_width, plain_width);
1592 // Now, apply a plain style over the first word only.
1593 render_text->ApplyStyle(BOLD, false, Range(0, 5));
1594 const int plain_bold_width = render_text->GetStringSize().width();
1595 EXPECT_GT(plain_bold_width, plain_width);
1596 EXPECT_LT(plain_bold_width, bold_width);
1599 TEST_F(RenderTextTest, StringSizeHeight) {
1600 base::string16 cases[] = {
1601 WideToUTF16(L"Hello World!"), // English
1602 WideToUTF16(L"\x6328\x62f6"), // Japanese
1603 WideToUTF16(L"\x0915\x093f"), // Hindi
1604 WideToUTF16(L"\x05e0\x05b8"), // Hebrew
1607 const FontList default_font_list;
1608 const FontList& larger_font_list = default_font_list.DeriveWithSizeDelta(24);
1609 EXPECT_GT(larger_font_list.GetHeight(), default_font_list.GetHeight());
1611 for (size_t i = 0; i < arraysize(cases); i++) {
1612 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1613 render_text->SetFontList(default_font_list);
1614 render_text->SetText(cases[i]);
1616 const int height1 = render_text->GetStringSize().height();
1617 EXPECT_GT(height1, 0);
1619 // Check that setting the larger font increases the height.
1620 render_text->SetFontList(larger_font_list);
1621 const int height2 = render_text->GetStringSize().height();
1622 EXPECT_GT(height2, height1);
1626 TEST_F(RenderTextTest, GetBaselineSanity) {
1627 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1628 render_text->SetText(UTF8ToUTF16("Hello World"));
1629 const int baseline = render_text->GetBaseline();
1630 EXPECT_GT(baseline, 0);
1633 TEST_F(RenderTextTest, CursorBoundsInReplacementMode) {
1634 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1635 render_text->SetText(ASCIIToUTF16("abcdefg"));
1636 render_text->SetDisplayRect(Rect(100, 17));
1637 SelectionModel sel_b(1, CURSOR_FORWARD);
1638 SelectionModel sel_c(2, CURSOR_FORWARD);
1639 Rect cursor_around_b = render_text->GetCursorBounds(sel_b, false);
1640 Rect cursor_before_b = render_text->GetCursorBounds(sel_b, true);
1641 Rect cursor_before_c = render_text->GetCursorBounds(sel_c, true);
1642 EXPECT_EQ(cursor_around_b.x(), cursor_before_b.x());
1643 EXPECT_EQ(cursor_around_b.right(), cursor_before_c.x());
1646 TEST_F(RenderTextTest, GetTextOffset) {
1647 // The default horizontal text offset differs for LTR and RTL, and is only set
1648 // when the RenderText object is created. This test will check the default in
1649 // LTR mode, and the next test will check the RTL default.
1650 const bool was_rtl = base::i18n::IsRTL();
1651 SetRTL(false);
1652 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1653 render_text->SetText(ASCIIToUTF16("abcdefg"));
1654 render_text->SetFontList(FontList("Arial, 13px"));
1656 // Set display area's size equal to the font size.
1657 const Size font_size(render_text->GetContentWidth(),
1658 render_text->font_list().GetHeight());
1659 Rect display_rect(font_size);
1660 render_text->SetDisplayRect(display_rect);
1662 Vector2d offset = render_text->GetLineOffset(0);
1663 EXPECT_TRUE(offset.IsZero());
1665 const int kEnlargementX = 2;
1666 display_rect.Inset(0, 0, -kEnlargementX, 0);
1667 render_text->SetDisplayRect(display_rect);
1669 // Check the default horizontal alignment.
1670 offset = render_text->GetLineOffset(0);
1671 EXPECT_EQ(0, offset.x());
1673 // Check explicitly setting the horizontal alignment.
1674 render_text->SetHorizontalAlignment(ALIGN_LEFT);
1675 offset = render_text->GetLineOffset(0);
1676 EXPECT_EQ(0, offset.x());
1677 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1678 offset = render_text->GetLineOffset(0);
1679 EXPECT_EQ(kEnlargementX / 2, offset.x());
1680 render_text->SetHorizontalAlignment(ALIGN_RIGHT);
1681 offset = render_text->GetLineOffset(0);
1682 EXPECT_EQ(kEnlargementX, offset.x());
1684 // Check that text is vertically centered within taller display rects.
1685 const int kEnlargementY = display_rect.height();
1686 display_rect.Inset(0, 0, 0, -kEnlargementY);
1687 render_text->SetDisplayRect(display_rect);
1688 const Vector2d prev_offset = render_text->GetLineOffset(0);
1689 display_rect.Inset(0, 0, 0, -2 * kEnlargementY);
1690 render_text->SetDisplayRect(display_rect);
1691 offset = render_text->GetLineOffset(0);
1692 EXPECT_EQ(prev_offset.y() + kEnlargementY, offset.y());
1694 SetRTL(was_rtl);
1697 TEST_F(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL) {
1698 // This only checks the default horizontal alignment in RTL mode; all other
1699 // GetLineOffset(0) attributes are checked by the test above.
1700 const bool was_rtl = base::i18n::IsRTL();
1701 SetRTL(true);
1702 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1703 render_text->SetText(ASCIIToUTF16("abcdefg"));
1704 render_text->SetFontList(FontList("Arial, 13px"));
1705 const int kEnlargement = 2;
1706 const Size font_size(render_text->GetContentWidth() + kEnlargement,
1707 render_text->GetStringSize().height());
1708 Rect display_rect(font_size);
1709 render_text->SetDisplayRect(display_rect);
1710 Vector2d offset = render_text->GetLineOffset(0);
1711 EXPECT_EQ(kEnlargement, offset.x());
1712 SetRTL(was_rtl);
1715 TEST_F(RenderTextTest, SetDisplayOffset) {
1716 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1717 render_text->SetText(ASCIIToUTF16("abcdefg"));
1718 render_text->SetFontList(FontList("Arial, 13px"));
1720 const Size font_size(render_text->GetContentWidth(),
1721 render_text->font_list().GetHeight());
1722 const int kEnlargement = 10;
1724 // Set display width |kEnlargement| pixels greater than content width and test
1725 // different possible situations. In this case the only possible display
1726 // offset is zero.
1727 Rect display_rect(font_size);
1728 display_rect.Inset(0, 0, -kEnlargement, 0);
1729 render_text->SetDisplayRect(display_rect);
1731 struct {
1732 HorizontalAlignment alignment;
1733 int offset;
1734 } small_content_cases[] = {
1735 { ALIGN_LEFT, -kEnlargement },
1736 { ALIGN_LEFT, 0 },
1737 { ALIGN_LEFT, kEnlargement },
1738 { ALIGN_RIGHT, -kEnlargement },
1739 { ALIGN_RIGHT, 0 },
1740 { ALIGN_RIGHT, kEnlargement },
1741 { ALIGN_CENTER, -kEnlargement },
1742 { ALIGN_CENTER, 0 },
1743 { ALIGN_CENTER, kEnlargement },
1746 for (size_t i = 0; i < arraysize(small_content_cases); i++) {
1747 render_text->SetHorizontalAlignment(small_content_cases[i].alignment);
1748 render_text->SetDisplayOffset(small_content_cases[i].offset);
1749 EXPECT_EQ(0, render_text->GetUpdatedDisplayOffset().x());
1752 // Set display width |kEnlargement| pixels less than content width and test
1753 // different possible situations.
1754 display_rect = Rect(font_size);
1755 display_rect.Inset(0, 0, kEnlargement, 0);
1756 render_text->SetDisplayRect(display_rect);
1758 struct {
1759 HorizontalAlignment alignment;
1760 int offset;
1761 int expected_offset;
1762 } large_content_cases[] = {
1763 // When text is left-aligned, display offset can be in range
1764 // [-kEnlargement, 0].
1765 { ALIGN_LEFT, -2 * kEnlargement, -kEnlargement },
1766 { ALIGN_LEFT, -kEnlargement / 2, -kEnlargement / 2 },
1767 { ALIGN_LEFT, kEnlargement, 0 },
1768 // When text is right-aligned, display offset can be in range
1769 // [0, kEnlargement].
1770 { ALIGN_RIGHT, -kEnlargement, 0 },
1771 { ALIGN_RIGHT, kEnlargement / 2, kEnlargement / 2 },
1772 { ALIGN_RIGHT, 2 * kEnlargement, kEnlargement },
1773 // When text is center-aligned, display offset can be in range
1774 // [-kEnlargement / 2 - 1, (kEnlargement - 1) / 2].
1775 { ALIGN_CENTER, -kEnlargement, -kEnlargement / 2 - 1 },
1776 { ALIGN_CENTER, -kEnlargement / 4, -kEnlargement / 4 },
1777 { ALIGN_CENTER, kEnlargement / 4, kEnlargement / 4 },
1778 { ALIGN_CENTER, kEnlargement, (kEnlargement - 1) / 2 },
1781 for (size_t i = 0; i < arraysize(large_content_cases); i++) {
1782 render_text->SetHorizontalAlignment(large_content_cases[i].alignment);
1783 render_text->SetDisplayOffset(large_content_cases[i].offset);
1784 EXPECT_EQ(large_content_cases[i].expected_offset,
1785 render_text->GetUpdatedDisplayOffset().x());
1789 TEST_F(RenderTextTest, SameFontForParentheses) {
1790 struct {
1791 const base::char16 left_char;
1792 const base::char16 right_char;
1793 } punctuation_pairs[] = {
1794 { '(', ')' },
1795 { '{', '}' },
1796 { '<', '>' },
1798 struct {
1799 base::string16 text;
1800 } cases[] = {
1801 // English(English)
1802 { WideToUTF16(L"Hello World(a)") },
1803 // English(English)English
1804 { WideToUTF16(L"Hello World(a)Hello World") },
1806 // Japanese(English)
1807 { WideToUTF16(L"\x6328\x62f6(a)") },
1808 // Japanese(English)Japanese
1809 { WideToUTF16(L"\x6328\x62f6(a)\x6328\x62f6") },
1810 // English(Japanese)English
1811 { WideToUTF16(L"Hello World(\x6328\x62f6)Hello World") },
1813 // Hindi(English)
1814 { WideToUTF16(L"\x0915\x093f(a)") },
1815 // Hindi(English)Hindi
1816 { WideToUTF16(L"\x0915\x093f(a)\x0915\x093f") },
1817 // English(Hindi)English
1818 { WideToUTF16(L"Hello World(\x0915\x093f)Hello World") },
1820 // Hebrew(English)
1821 { WideToUTF16(L"\x05e0\x05b8(a)") },
1822 // Hebrew(English)Hebrew
1823 { WideToUTF16(L"\x05e0\x05b8(a)\x05e0\x05b8") },
1824 // English(Hebrew)English
1825 { WideToUTF16(L"Hello World(\x05e0\x05b8)Hello World") },
1828 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1829 for (size_t i = 0; i < arraysize(cases); ++i) {
1830 base::string16 text = cases[i].text;
1831 const size_t start_paren_char_index = text.find('(');
1832 ASSERT_NE(base::string16::npos, start_paren_char_index);
1833 const size_t end_paren_char_index = text.find(')');
1834 ASSERT_NE(base::string16::npos, end_paren_char_index);
1836 for (size_t j = 0; j < arraysize(punctuation_pairs); ++j) {
1837 text[start_paren_char_index] = punctuation_pairs[j].left_char;
1838 text[end_paren_char_index] = punctuation_pairs[j].right_char;
1839 render_text->SetText(text);
1841 const std::vector<RenderText::FontSpan> spans =
1842 render_text->GetFontSpansForTesting();
1844 int start_paren_span_index = -1;
1845 int end_paren_span_index = -1;
1846 for (size_t k = 0; k < spans.size(); ++k) {
1847 if (IndexInRange(spans[k].second, start_paren_char_index))
1848 start_paren_span_index = k;
1849 if (IndexInRange(spans[k].second, end_paren_char_index))
1850 end_paren_span_index = k;
1852 ASSERT_NE(-1, start_paren_span_index);
1853 ASSERT_NE(-1, end_paren_span_index);
1855 const Font& start_font = spans[start_paren_span_index].first;
1856 const Font& end_font = spans[end_paren_span_index].first;
1857 EXPECT_EQ(start_font.GetFontName(), end_font.GetFontName());
1858 EXPECT_EQ(start_font.GetFontSize(), end_font.GetFontSize());
1859 EXPECT_EQ(start_font.GetStyle(), end_font.GetStyle());
1864 // Make sure the caret width is always >=1 so that the correct
1865 // caret is drawn at high DPI. crbug.com/164100.
1866 TEST_F(RenderTextTest, CaretWidth) {
1867 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1868 render_text->SetText(ASCIIToUTF16("abcdefg"));
1869 EXPECT_GE(render_text->GetUpdatedCursorBounds().width(), 1);
1872 TEST_F(RenderTextTest, SelectWord) {
1873 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1874 render_text->SetText(ASCIIToUTF16(" foo a.bc.d bar"));
1876 struct {
1877 size_t cursor;
1878 size_t selection_start;
1879 size_t selection_end;
1880 } cases[] = {
1881 { 0, 0, 1 },
1882 { 1, 1, 4 },
1883 { 2, 1, 4 },
1884 { 3, 1, 4 },
1885 { 4, 4, 6 },
1886 { 5, 4, 6 },
1887 { 6, 6, 7 },
1888 { 7, 7, 8 },
1889 { 8, 8, 10 },
1890 { 9, 8, 10 },
1891 { 10, 10, 11 },
1892 { 11, 11, 12 },
1893 { 12, 12, 13 },
1894 { 13, 13, 16 },
1895 { 14, 13, 16 },
1896 { 15, 13, 16 },
1897 { 16, 13, 16 },
1900 for (size_t i = 0; i < arraysize(cases); ++i) {
1901 render_text->SetCursorPosition(cases[i].cursor);
1902 render_text->SelectWord();
1903 EXPECT_EQ(Range(cases[i].selection_start, cases[i].selection_end),
1904 render_text->selection());
1908 // Make sure the last word is selected when the cursor is at text.length().
1909 TEST_F(RenderTextTest, LastWordSelected) {
1910 const std::string kTestURL1 = "http://www.google.com";
1911 const std::string kTestURL2 = "http://www.google.com/something/";
1913 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1915 render_text->SetText(ASCIIToUTF16(kTestURL1));
1916 render_text->SetCursorPosition(kTestURL1.length());
1917 render_text->SelectWord();
1918 EXPECT_EQ(ASCIIToUTF16("com"), GetSelectedText(render_text.get()));
1919 EXPECT_FALSE(render_text->selection().is_reversed());
1921 render_text->SetText(ASCIIToUTF16(kTestURL2));
1922 render_text->SetCursorPosition(kTestURL2.length());
1923 render_text->SelectWord();
1924 EXPECT_EQ(ASCIIToUTF16("/"), GetSelectedText(render_text.get()));
1925 EXPECT_FALSE(render_text->selection().is_reversed());
1928 // When given a non-empty selection, SelectWord should expand the selection to
1929 // nearest word boundaries.
1930 TEST_F(RenderTextTest, SelectMultipleWords) {
1931 const std::string kTestURL = "http://www.google.com";
1933 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1935 render_text->SetText(ASCIIToUTF16(kTestURL));
1936 render_text->SelectRange(Range(16, 20));
1937 render_text->SelectWord();
1938 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1939 EXPECT_FALSE(render_text->selection().is_reversed());
1941 // SelectWord should preserve the selection direction.
1942 render_text->SelectRange(Range(20, 16));
1943 render_text->SelectWord();
1944 EXPECT_EQ(ASCIIToUTF16("google.com"), GetSelectedText(render_text.get()));
1945 EXPECT_TRUE(render_text->selection().is_reversed());
1948 // TODO(asvitkine): Cursor movements tests disabled on Mac because RenderTextMac
1949 // does not implement this yet. http://crbug.com/131618
1950 #if !defined(OS_MACOSX)
1951 TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) {
1952 ASSERT_FALSE(base::i18n::IsRTL());
1953 ASSERT_FALSE(base::i18n::ICUIsRTL());
1955 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1956 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
1957 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
1958 CURSOR_FORWARD));
1959 int width = render_text->GetStringSize().width();
1960 ASSERT_GT(width, 10);
1962 // Ensure that the cursor is placed at the width of its preceding text.
1963 render_text->SetDisplayRect(Rect(width + 10, 1));
1964 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1966 // Ensure that shrinking the display rectangle keeps the cursor in view.
1967 render_text->SetDisplayRect(Rect(width - 10, 1));
1968 EXPECT_EQ(render_text->display_rect().width(),
1969 render_text->GetUpdatedCursorBounds().right());
1971 // Ensure that the text will pan to fill its expanding display rectangle.
1972 render_text->SetDisplayRect(Rect(width - 5, 1));
1973 EXPECT_EQ(render_text->display_rect().width(),
1974 render_text->GetUpdatedCursorBounds().right());
1976 // Ensure that a sufficiently large display rectangle shows all the text.
1977 render_text->SetDisplayRect(Rect(width + 10, 1));
1978 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1980 // Repeat the test with RTL text.
1981 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
1982 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
1983 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1984 width = render_text->GetStringSize().width();
1985 ASSERT_GT(width, 10);
1987 // Ensure that the cursor is placed at the width of its preceding text.
1988 render_text->SetDisplayRect(Rect(width + 10, 1));
1989 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
1991 // Ensure that shrinking the display rectangle keeps the cursor in view.
1992 render_text->SetDisplayRect(Rect(width - 10, 1));
1993 EXPECT_EQ(render_text->display_rect().width(),
1994 render_text->GetUpdatedCursorBounds().right());
1996 // Ensure that the text will pan to fill its expanding display rectangle.
1997 render_text->SetDisplayRect(Rect(width - 5, 1));
1998 EXPECT_EQ(render_text->display_rect().width(),
1999 render_text->GetUpdatedCursorBounds().right());
2001 // Ensure that a sufficiently large display rectangle shows all the text.
2002 render_text->SetDisplayRect(Rect(width + 10, 1));
2003 EXPECT_EQ(width, render_text->GetUpdatedCursorBounds().x());
2006 TEST_F(RenderTextTest, DisplayRectShowsCursorRTL) {
2007 // Set the application default text direction to RTL.
2008 const bool was_rtl = base::i18n::IsRTL();
2009 SetRTL(true);
2011 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2012 render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg"));
2013 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
2014 int width = render_text->GetStringSize().width();
2015 ASSERT_GT(width, 10);
2017 // Ensure that the cursor is placed at the width of its preceding text.
2018 render_text->SetDisplayRect(Rect(width + 10, 1));
2019 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2020 render_text->GetUpdatedCursorBounds().x());
2022 // Ensure that shrinking the display rectangle keeps the cursor in view.
2023 render_text->SetDisplayRect(Rect(width - 10, 1));
2024 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2026 // Ensure that the text will pan to fill its expanding display rectangle.
2027 render_text->SetDisplayRect(Rect(width - 5, 1));
2028 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2030 // Ensure that a sufficiently large display rectangle shows all the text.
2031 render_text->SetDisplayRect(Rect(width + 10, 1));
2032 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2033 render_text->GetUpdatedCursorBounds().x());
2035 // Repeat the test with RTL text.
2036 render_text->SetText(WideToUTF16(L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7"
2037 L"\x5d8\x5d9\x5da\x5db\x5dc\x5dd\x5de\x5df"));
2038 render_text->MoveCursorTo(SelectionModel(render_text->text().length(),
2039 CURSOR_FORWARD));
2040 width = render_text->GetStringSize().width();
2041 ASSERT_GT(width, 10);
2043 // Ensure that the cursor is placed at the width of its preceding text.
2044 render_text->SetDisplayRect(Rect(width + 10, 1));
2045 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2046 render_text->GetUpdatedCursorBounds().x());
2048 // Ensure that shrinking the display rectangle keeps the cursor in view.
2049 render_text->SetDisplayRect(Rect(width - 10, 1));
2050 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2052 // Ensure that the text will pan to fill its expanding display rectangle.
2053 render_text->SetDisplayRect(Rect(width - 5, 1));
2054 EXPECT_EQ(0, render_text->GetUpdatedCursorBounds().x());
2056 // Ensure that a sufficiently large display rectangle shows all the text.
2057 render_text->SetDisplayRect(Rect(width + 10, 1));
2058 EXPECT_EQ(render_text->display_rect().width() - width - 1,
2059 render_text->GetUpdatedCursorBounds().x());
2061 // Reset the application default text direction to LTR.
2062 SetRTL(was_rtl);
2063 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
2065 #endif // !defined(OS_MACOSX)
2067 // Changing colors between or inside ligated glyphs should not break shaping.
2068 TEST_F(RenderTextTest, SelectionKeepsLigatures) {
2069 const wchar_t* kTestStrings[] = { L"\x644\x623", L"\x633\x627" };
2070 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2071 render_text->set_selection_color(SK_ColorRED);
2072 Canvas canvas;
2074 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2075 render_text->SetText(WideToUTF16(kTestStrings[i]));
2076 const int expected_width = render_text->GetStringSize().width();
2077 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD));
2078 EXPECT_EQ(expected_width, render_text->GetStringSize().width());
2079 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119
2080 render_text->Draw(&canvas);
2081 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
2085 // Ensure strings wrap onto multiple lines for a small available width.
2086 TEST_F(RenderTextTest, Multiline_MinWidth) {
2087 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl,
2088 kRtlLtr, kRtlLtrRtl };
2090 RenderTextHarfBuzz render_text;
2091 render_text.SetDisplayRect(Rect(1, 1000));
2092 render_text.SetMultiline(true);
2093 render_text.SetWordWrapBehavior(WRAP_LONG_WORDS);
2094 Canvas canvas;
2096 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2097 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2098 render_text.SetText(WideToUTF16(kTestStrings[i]));
2099 render_text.Draw(&canvas);
2100 EXPECT_GT(render_text.lines_.size(), 1U);
2104 // Ensure strings wrap onto multiple lines for a normal available width.
2105 TEST_F(RenderTextTest, Multiline_NormalWidth) {
2106 const struct {
2107 const wchar_t* const text;
2108 const Range first_line_char_range;
2109 const Range second_line_char_range;
2110 bool is_ltr;
2111 } kTestStrings[] = {
2112 { L"abc defg hijkl", Range(0, 9), Range(9, 14), true },
2113 { L"qwertyzxcvbn", Range(0, 10), Range(10, 12), true },
2114 { L"\x0627\x0644\x0644\x063A\x0629 "
2115 L"\x0627\x0644\x0639\x0631\x0628\x064A\x0629",
2116 Range(0, 6), Range(6, 13), false },
2117 { L"\x062A\x0641\x0627\x062D\x05EA\x05E4\x05D5\x05D6\x05D9"
2118 L"\x05DA\x05DB\x05DD", Range(0, 4), Range(4, 12), false }
2121 RenderTextHarfBuzz render_text;
2122 // Specify the fixed width for characters to suppress the possible variations
2123 // of linebreak results.
2124 render_text.set_glyph_width_for_test(5);
2125 render_text.SetDisplayRect(Rect(50, 1000));
2126 render_text.SetMultiline(true);
2127 render_text.SetWordWrapBehavior(WRAP_LONG_WORDS);
2128 render_text.SetHorizontalAlignment(ALIGN_TO_HEAD);
2130 Canvas canvas;
2131 TestSkiaTextRenderer renderer(&canvas);
2133 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2134 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2135 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2136 render_text.EnsureLayout();
2137 render_text.DrawVisualTextInternal(&renderer);
2139 ASSERT_EQ(2U, render_text.lines_.size());
2140 ASSERT_EQ(1U, render_text.lines_[0].segments.size());
2141 EXPECT_EQ(kTestStrings[i].first_line_char_range,
2142 render_text.lines_[0].segments[0].char_range);
2143 ASSERT_EQ(1U, render_text.lines_[1].segments.size());
2144 EXPECT_EQ(kTestStrings[i].second_line_char_range,
2145 render_text.lines_[1].segments[0].char_range);
2147 std::vector<TestSkiaTextRenderer::TextLog> text_log;
2148 renderer.GetTextLogAndReset(&text_log);
2149 ASSERT_EQ(2U, text_log.size());
2150 // NOTE: this expectation compares the character length and glyph counts,
2151 // which isn't always equal. This is okay only because all the test
2152 // strings are simple (like, no compound characters nor UTF16-surrogate
2153 // pairs). Be careful in case more complicated test strings are added.
2154 EXPECT_EQ(kTestStrings[i].first_line_char_range.length(),
2155 text_log[0].glyph_count);
2156 EXPECT_EQ(kTestStrings[i].second_line_char_range.length(),
2157 text_log[1].glyph_count);
2158 EXPECT_LT(text_log[0].origin.y(), text_log[1].origin.y());
2159 if (kTestStrings[i].is_ltr) {
2160 EXPECT_EQ(0, text_log[0].origin.x());
2161 EXPECT_EQ(0, text_log[1].origin.x());
2162 } else {
2163 EXPECT_LT(0, text_log[0].origin.x());
2164 EXPECT_LT(0, text_log[1].origin.x());
2169 // Ensure strings don't wrap onto multiple lines for a sufficient available
2170 // width.
2171 TEST_F(RenderTextTest, Multiline_SufficientWidth) {
2172 const wchar_t* kTestStrings[] = { L"", L" ", L".", L" . ", L"abc", L"a b c",
2173 L"\x62E\x628\x632", L"\x62E \x628 \x632" };
2175 RenderTextHarfBuzz render_text;
2176 render_text.SetDisplayRect(Rect(1000, 1000));
2177 render_text.SetMultiline(true);
2178 Canvas canvas;
2180 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2181 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2182 render_text.SetText(WideToUTF16(kTestStrings[i]));
2183 render_text.Draw(&canvas);
2184 EXPECT_EQ(1U, render_text.lines_.size());
2188 TEST_F(RenderTextTest, Multiline_Newline) {
2189 const struct {
2190 const wchar_t* const text;
2191 const size_t lines_count;
2192 // Ranges of the characters on each line preceding the newline.
2193 const Range line_char_ranges[3];
2194 } kTestStrings[] = {
2195 {L"abc\ndef", 2ul, { Range(0, 3), Range(4, 7), Range::InvalidRange() } },
2196 {L"a \n b ", 2ul, { Range(0, 2), Range(3, 6), Range::InvalidRange() } },
2197 {L"ab\n", 2ul, { Range(0, 2), Range(), Range::InvalidRange() } },
2198 {L"a\n\nb", 3ul, { Range(0, 1), Range(), Range(3, 4) } },
2199 {L"\nab", 2ul, { Range(), Range(1, 3), Range::InvalidRange() } },
2200 {L"\n", 2ul, { Range(), Range(), Range::InvalidRange() } },
2203 RenderTextHarfBuzz render_text;
2204 render_text.SetDisplayRect(Rect(200, 1000));
2205 render_text.SetMultiline(true);
2206 Canvas canvas;
2208 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2209 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2210 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2211 render_text.Draw(&canvas);
2212 EXPECT_EQ(kTestStrings[i].lines_count, render_text.lines_.size());
2213 if (kTestStrings[i].lines_count != render_text.lines_.size())
2214 continue;
2216 for (size_t j = 0; j < kTestStrings[i].lines_count; ++j) {
2217 SCOPED_TRACE(base::StringPrintf("Line %" PRIuS "", j));
2218 VerifyLineSegments(kTestStrings[i].line_char_ranges[j],
2219 render_text.lines_[j].segments);
2224 // Make sure that multiline mode ignores elide behavior.
2225 TEST_F(RenderTextTest, Multiline_IgnoreElide) {
2226 const wchar_t kTestString[] =
2227 L"very very very long string xxxxxxxxxxxxxxxxxxxxxxxxxx";
2228 const wchar_t kEllipsis[] = L"\x2026";
2230 RenderTextHarfBuzz render_text;
2231 render_text.SetElideBehavior(ELIDE_TAIL);
2232 render_text.SetDisplayRect(Rect(20, 1000));
2233 render_text.SetText(base::WideToUTF16(kTestString));
2234 EXPECT_NE(base::string16::npos,
2235 render_text.GetDisplayText().find(base::WideToUTF16(kEllipsis)));
2237 render_text.SetMultiline(true);
2238 EXPECT_EQ(base::string16::npos,
2239 render_text.GetDisplayText().find(base::WideToUTF16(kEllipsis)));
2242 TEST_F(RenderTextTest, Multiline_NewlineCharacterReplacement) {
2243 const wchar_t* kTestStrings[] = {
2244 L"abc\ndef", L"a \n b ", L"ab\n", L"a\n\nb", L"\nab", L"\n",
2247 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2248 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2249 RenderTextHarfBuzz render_text;
2250 render_text.SetDisplayRect(Rect(200, 1000));
2251 render_text.SetText(WideToUTF16(kTestStrings[i]));
2253 base::string16 display_text = render_text.GetDisplayText();
2254 // If RenderText is not multiline, the newline characters are replaced
2255 // by symbols, therefore the character should be changed.
2256 EXPECT_NE(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText());
2258 // Setting multiline will fix this, the newline characters will be back
2259 // to the original text.
2260 render_text.SetMultiline(true);
2261 EXPECT_EQ(WideToUTF16(kTestStrings[i]), render_text.GetDisplayText());
2265 // Ensure horizontal alignment works in multiline mode.
2266 TEST_F(RenderTextTest, Multiline_HorizontalAlignment) {
2267 const struct {
2268 const wchar_t* const text;
2269 const gfx::HorizontalAlignment alignment;
2270 } kTestStrings[] = {
2271 { L"abcdefghij\nhijkl", gfx::ALIGN_LEFT },
2272 { L"nhijkl\nabcdefghij", gfx::ALIGN_LEFT },
2273 // hebrew, 2nd line shorter
2274 { L"\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7\n\x5d0\x5d1\x5d2\x5d3",
2275 gfx::ALIGN_RIGHT },
2276 // hebrew, 2nd line longer
2277 { L"\x5d0\x5d1\x5d2\x5d3\n\x5d0\x5d1\x5d2\x5d3\x5d4\x5d5\x5d6\x5d7",
2278 gfx::ALIGN_RIGHT },
2279 // arabic, 2nd line shorter
2280 { L"\x62a\x62b\x62c\x62d\x62e\x62f\x630\n\x660\x661\x662\x663\x664",
2281 gfx::ALIGN_RIGHT },
2282 // arabic, 2nd line longer
2283 { L"\x660\x661\x662\x663\x664\n\x62a\x62b\x62c\x62d\x62e\x62f\x630",
2284 gfx::ALIGN_RIGHT },
2286 const int kGlyphSize = 5;
2287 RenderTextHarfBuzz render_text;
2288 render_text.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
2289 render_text.set_glyph_width_for_test(kGlyphSize);
2290 render_text.SetDisplayRect(Rect(100, 1000));
2291 render_text.SetMultiline(true);
2293 Canvas canvas;
2294 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2295 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "] %ls", i,
2296 kTestStrings[i].text));
2297 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2298 render_text.Draw(&canvas);
2299 ASSERT_LE(2u, render_text.lines().size());
2300 if (kTestStrings[i].alignment == gfx::ALIGN_LEFT) {
2301 EXPECT_EQ(0, render_text.GetAlignmentOffset(0).x());
2302 EXPECT_EQ(0, render_text.GetAlignmentOffset(1).x());
2303 } else {
2304 std::vector<base::string16> lines;
2305 base::SplitString(base::WideToUTF16(kTestStrings[i].text), '\n', &lines);
2306 ASSERT_EQ(2u, lines.size());
2307 int difference = (lines[0].length() - lines[1].length()) * kGlyphSize;
2308 EXPECT_EQ(render_text.GetAlignmentOffset(0).x() + difference,
2309 render_text.GetAlignmentOffset(1).x());
2314 TEST_F(RenderTextTest, Multiline_WordWrapBehavior) {
2315 const int kGlyphSize = 5;
2316 const struct {
2317 const WordWrapBehavior behavior;
2318 const size_t num_lines;
2319 const Range char_ranges[4];
2320 } kTestScenarios[] = {
2321 { IGNORE_LONG_WORDS, 3u,
2322 { Range(0, 4), Range(4, 11), Range(11, 14), Range::InvalidRange() } },
2323 { TRUNCATE_LONG_WORDS, 3u,
2324 { Range(0, 4), Range(4, 8), Range(11, 14), Range::InvalidRange() } },
2325 { WRAP_LONG_WORDS, 4u,
2326 { Range(0, 4), Range(4, 8), Range(8, 11), Range(11, 14) } },
2327 // TODO(mukai): implement ELIDE_LONG_WORDS. It's not used right now.
2329 RenderTextHarfBuzz render_text;
2330 render_text.SetMultiline(true);
2331 render_text.SetText(ASCIIToUTF16("foo fooooo foo"));
2332 render_text.set_glyph_width_for_test(kGlyphSize);
2333 render_text.SetDisplayRect(Rect(0, 0, kGlyphSize * 4, 0));
2335 Canvas canvas;
2337 for (size_t i = 0; i < arraysize(kTestScenarios); ++i) {
2338 SCOPED_TRACE(base::StringPrintf(
2339 "kTestScenarios[%" PRIuS "] %d", i, kTestScenarios[i].behavior));
2340 render_text.SetWordWrapBehavior(kTestScenarios[i].behavior);
2341 render_text.Draw(&canvas);
2343 ASSERT_EQ(kTestScenarios[i].num_lines, render_text.lines().size());
2344 for (size_t j = 0; j < render_text.lines().size(); ++j) {
2345 SCOPED_TRACE(base::StringPrintf("%" PRIuS "-th line", j));
2346 EXPECT_EQ(kTestScenarios[i].char_ranges[j],
2347 render_text.lines()[j].segments[0].char_range);
2348 EXPECT_EQ(kTestScenarios[i].char_ranges[j].length() * kGlyphSize,
2349 render_text.lines()[j].size.width());
2354 TEST_F(RenderTextTest, NewlineWithoutMultilineFlag) {
2355 const wchar_t* kTestStrings[] = {
2356 L"abc\ndef", L"a \n b ", L"ab\n", L"a\n\nb", L"\nab", L"\n",
2359 RenderTextHarfBuzz render_text;
2360 render_text.SetDisplayRect(Rect(200, 1000));
2361 Canvas canvas;
2363 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2364 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2365 render_text.SetText(WideToUTF16(kTestStrings[i]));
2366 render_text.Draw(&canvas);
2368 EXPECT_EQ(1U, render_text.lines_.size());
2372 // Make sure the horizontal positions of runs in a line (left-to-right for
2373 // LTR languages and right-to-left for RTL languages).
2374 TEST_F(RenderTextTest, HarfBuzz_HorizontalPositions) {
2375 const struct {
2376 const wchar_t* const text;
2377 const Range first_run_char_range;
2378 const Range second_run_char_range;
2379 bool is_rtl;
2380 } kTestStrings[] = {
2381 { L"abc\x3042\x3044\x3046\x3048\x304A", Range(0, 3), Range(3, 8), false },
2382 { L"\x062A\x0641\x0627\x062D"
2383 L"\x05EA\x05E4\x05D5\x05D6\x05D9\x05DA\x05DB\x05DD",
2384 Range(0, 4), Range(4, 12), true },
2387 RenderTextHarfBuzz render_text;
2388 Canvas canvas;
2389 TestSkiaTextRenderer renderer(&canvas);
2391 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2392 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2393 render_text.SetText(WideToUTF16(kTestStrings[i].text));
2395 render_text.EnsureLayout();
2396 const internal::TextRunList* run_list = render_text.GetRunList();
2397 ASSERT_EQ(2U, run_list->runs().size());
2398 EXPECT_EQ(kTestStrings[i].first_run_char_range, run_list->runs()[0]->range);
2399 EXPECT_EQ(kTestStrings[i].second_run_char_range,
2400 run_list->runs()[1]->range);
2401 // If it's RTL, the visual order is reversed.
2402 if (kTestStrings[i].is_rtl) {
2403 EXPECT_EQ(1U, run_list->logical_to_visual(0));
2404 EXPECT_EQ(0U, run_list->logical_to_visual(1));
2405 } else {
2406 EXPECT_EQ(0U, run_list->logical_to_visual(0));
2407 EXPECT_EQ(1U, run_list->logical_to_visual(1));
2410 render_text.DrawVisualTextInternal(&renderer);
2412 std::vector<TestSkiaTextRenderer::TextLog> text_log;
2413 renderer.GetTextLogAndReset(&text_log);
2415 EXPECT_EQ(2U, text_log.size());
2417 // Verifies the DrawText happens in the visual order and left-to-right.
2418 // If the text is RTL, the logically first run should be drawn at last.
2419 EXPECT_EQ(run_list->runs()[run_list->logical_to_visual(0)]->glyph_count,
2420 text_log[0].glyph_count);
2421 EXPECT_EQ(run_list->runs()[run_list->logical_to_visual(1)]->glyph_count,
2422 text_log[1].glyph_count);
2423 EXPECT_LT(text_log[0].origin.x(), text_log[1].origin.x());
2427 // Test TextRunHarfBuzz's cluster finding logic.
2428 TEST_F(RenderTextTest, HarfBuzz_Clusters) {
2429 struct {
2430 uint32 glyph_to_char[4];
2431 Range chars[4];
2432 Range glyphs[4];
2433 bool is_rtl;
2434 } cases[] = {
2435 { // From string "A B C D" to glyphs "a b c d".
2436 { 0, 1, 2, 3 },
2437 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2438 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2439 false
2441 { // From string "A B C D" to glyphs "d c b a".
2442 { 3, 2, 1, 0 },
2443 { Range(0, 1), Range(1, 2), Range(2, 3), Range(3, 4) },
2444 { Range(3, 4), Range(2, 3), Range(1, 2), Range(0, 1) },
2445 true
2447 { // From string "A B C D" to glyphs "ab c c d".
2448 { 0, 2, 2, 3 },
2449 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2450 { Range(0, 1), Range(0, 1), Range(1, 3), Range(3, 4) },
2451 false
2453 { // From string "A B C D" to glyphs "d c c ba".
2454 { 3, 2, 2, 0 },
2455 { Range(0, 2), Range(0, 2), Range(2, 3), Range(3, 4) },
2456 { Range(3, 4), Range(3, 4), Range(1, 3), Range(0, 1) },
2457 true
2461 internal::TextRunHarfBuzz run;
2462 run.range = Range(0, 4);
2463 run.glyph_count = 4;
2464 run.glyph_to_char.resize(4);
2466 for (size_t i = 0; i < arraysize(cases); ++i) {
2467 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 4,
2468 run.glyph_to_char.begin());
2469 run.is_rtl = cases[i].is_rtl;
2471 for (size_t j = 0; j < 4; ++j) {
2472 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2473 Range chars;
2474 Range glyphs;
2475 run.GetClusterAt(j, &chars, &glyphs);
2476 EXPECT_EQ(cases[i].chars[j], chars);
2477 EXPECT_EQ(cases[i].glyphs[j], glyphs);
2478 EXPECT_EQ(cases[i].glyphs[j], run.CharRangeToGlyphRange(chars));
2483 // Ensure that graphemes with multiple code points do not get split.
2484 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemeCases) {
2485 const wchar_t* cases[] = {
2486 // "A" with a combining umlaut, followed by a "B".
2487 L"A\x0308" L"B",
2488 // Devanagari biconsonantal conjunct "ki", followed by an "a".
2489 L"\x0915\x093f\x0905",
2490 // Thai consonant and vowel pair "cho chan" + "sara am", followed by Thai
2491 // digit 0.
2492 L"\x0e08\x0e33\x0E50",
2495 RenderTextHarfBuzz render_text;
2497 for (size_t i = 0; i < arraysize(cases); ++i) {
2498 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS, i));
2500 base::string16 text = WideToUTF16(cases[i]);
2501 render_text.SetText(text);
2502 render_text.EnsureLayout();
2503 internal::TextRunList* run_list = render_text.GetRunList();
2504 ASSERT_EQ(1U, run_list->size());
2505 internal::TextRunHarfBuzz* run = run_list->runs()[0];
2507 base::i18n::BreakIterator* iter = render_text.grapheme_iterator_.get();
2508 auto first_grapheme_bounds = run->GetGraphemeBounds(iter, 0);
2509 EXPECT_EQ(first_grapheme_bounds, run->GetGraphemeBounds(iter, 1));
2510 auto second_grapheme_bounds = run->GetGraphemeBounds(iter, 2);
2511 EXPECT_EQ(first_grapheme_bounds.end(), second_grapheme_bounds.start());
2515 // Test the partition of a multi-grapheme cluster into grapheme ranges.
2516 TEST_F(RenderTextTest, HarfBuzz_SubglyphGraphemePartition) {
2517 struct {
2518 uint32 glyph_to_char[2];
2519 Range bounds[4];
2520 bool is_rtl;
2521 } cases[] = {
2522 { // From string "A B C D" to glyphs "a bcd".
2523 { 0, 1 },
2524 { Range(0, 10), Range(10, 13), Range(13, 17), Range(17, 20) },
2525 false
2527 { // From string "A B C D" to glyphs "ab cd".
2528 { 0, 2 },
2529 { Range(0, 5), Range(5, 10), Range(10, 15), Range(15, 20) },
2530 false
2532 { // From string "A B C D" to glyphs "dcb a".
2533 { 1, 0 },
2534 { Range(10, 20), Range(7, 10), Range(3, 7), Range(0, 3) },
2535 true
2537 { // From string "A B C D" to glyphs "dc ba".
2538 { 2, 0 },
2539 { Range(15, 20), Range(10, 15), Range(5, 10), Range(0, 5) },
2540 true
2544 internal::TextRunHarfBuzz run;
2545 run.range = Range(0, 4);
2546 run.glyph_count = 2;
2547 run.glyph_to_char.resize(2);
2548 run.positions.reset(new SkPoint[4]);
2549 run.width = 20;
2551 const base::string16 kString = ASCIIToUTF16("abcd");
2552 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2553 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2554 ASSERT_TRUE(iter->Init());
2556 for (size_t i = 0; i < arraysize(cases); ++i) {
2557 std::copy(cases[i].glyph_to_char, cases[i].glyph_to_char + 2,
2558 run.glyph_to_char.begin());
2559 run.is_rtl = cases[i].is_rtl;
2560 for (int j = 0; j < 2; ++j)
2561 run.positions[j].set(j * 10, 0);
2563 for (size_t j = 0; j < 4; ++j) {
2564 SCOPED_TRACE(base::StringPrintf("Case %" PRIuS ", char %" PRIuS, i, j));
2565 EXPECT_EQ(cases[i].bounds[j],
2566 internal::RoundRangeF(run.GetGraphemeBounds(iter.get(), j)));
2571 TEST_F(RenderTextTest, HarfBuzz_RunDirection) {
2572 RenderTextHarfBuzz render_text;
2573 const base::string16 mixed =
2574 WideToUTF16(L"\x05D0\x05D1" L"1234" L"\x05D2\x05D3");
2575 render_text.SetText(mixed);
2576 render_text.EnsureLayout();
2577 internal::TextRunList* run_list = render_text.GetRunList();
2578 ASSERT_EQ(3U, run_list->size());
2579 EXPECT_TRUE(run_list->runs()[0]->is_rtl);
2580 EXPECT_FALSE(run_list->runs()[1]->is_rtl);
2581 EXPECT_TRUE(run_list->runs()[2]->is_rtl);
2584 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByUnicodeBlocks) {
2585 RenderTextHarfBuzz render_text;
2587 // The '\x25B6' "play character" should break runs. http://crbug.com/278913
2588 render_text.SetText(WideToUTF16(L"x\x25B6y"));
2589 render_text.EnsureLayout();
2590 internal::TextRunList* run_list = render_text.GetRunList();
2591 ASSERT_EQ(3U, run_list->size());
2592 EXPECT_EQ(Range(0, 1), run_list->runs()[0]->range);
2593 EXPECT_EQ(Range(1, 2), run_list->runs()[1]->range);
2594 EXPECT_EQ(Range(2, 3), run_list->runs()[2]->range);
2596 render_text.SetText(WideToUTF16(L"x \x25B6 y"));
2597 render_text.EnsureLayout();
2598 run_list = render_text.GetRunList();
2599 ASSERT_EQ(3U, run_list->size());
2600 EXPECT_EQ(Range(0, 2), run_list->runs()[0]->range);
2601 EXPECT_EQ(Range(2, 3), run_list->runs()[1]->range);
2602 EXPECT_EQ(Range(3, 5), run_list->runs()[2]->range);
2605 TEST_F(RenderTextTest, HarfBuzz_BreakRunsByEmoji) {
2606 RenderTextHarfBuzz render_text;
2608 // \xF0\x9F\x98\x81 (U+1F601) is smile icon emoji. \xE2\x9C\xA8 (U+2728) is
2609 // a sparkle icon. Both can be drawn with color emoji fonts, so runs should be
2610 // separated. See crbug.com/448909
2611 render_text.SetText(UTF8ToUTF16("x\xF0\x9F\x98\x81y\xE2\x9C\xA8"));
2612 render_text.EnsureLayout();
2613 internal::TextRunList* run_list = render_text.GetRunList();
2614 ASSERT_EQ(4U, run_list->size());
2615 EXPECT_EQ(Range(0, 1), run_list->runs()[0]->range);
2616 // The length is 2 since U+1F601 is represented as a surrogate pair in UTF16.
2617 EXPECT_EQ(Range(1, 3), run_list->runs()[1]->range);
2618 EXPECT_EQ(Range(3, 4), run_list->runs()[2]->range);
2619 EXPECT_EQ(Range(4, 5), run_list->runs()[3]->range);
2622 TEST_F(RenderTextTest, GlyphBounds) {
2623 const wchar_t* kTestStrings[] = {
2624 L"asdf 1234 qwer", L"\x0647\x0654", L"\x0645\x0631\x062D\x0628\x0627"
2626 scoped_ptr<RenderText> render_text(new RenderTextHarfBuzz);
2628 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2629 render_text->SetText(WideToUTF16(kTestStrings[i]));
2630 render_text->EnsureLayout();
2632 for (size_t j = 0; j < render_text->text().length(); ++j)
2633 EXPECT_FALSE(render_text->GetGlyphBounds(j).is_empty());
2637 // Ensure that shaping with a non-existent font does not cause a crash.
2638 TEST_F(RenderTextTest, HarfBuzz_NonExistentFont) {
2639 RenderTextHarfBuzz render_text;
2640 render_text.SetText(ASCIIToUTF16("test"));
2641 render_text.EnsureLayout();
2642 internal::TextRunList* run_list = render_text.GetRunList();
2643 ASSERT_EQ(1U, run_list->size());
2644 internal::TextRunHarfBuzz* run = run_list->runs()[0];
2645 render_text.ShapeRunWithFont(
2646 render_text.text(), "TheFontThatDoesntExist", FontRenderParams(), run);
2649 // Ensure an empty run returns sane values to queries.
2650 TEST_F(RenderTextTest, HarfBuzz_EmptyRun) {
2651 internal::TextRunHarfBuzz run;
2652 const base::string16 kString = ASCIIToUTF16("abcdefgh");
2653 scoped_ptr<base::i18n::BreakIterator> iter(new base::i18n::BreakIterator(
2654 kString, base::i18n::BreakIterator::BREAK_CHARACTER));
2655 ASSERT_TRUE(iter->Init());
2657 run.range = Range(3, 8);
2658 run.glyph_count = 0;
2659 EXPECT_EQ(Range(0, 0), run.CharRangeToGlyphRange(Range(4, 5)));
2660 EXPECT_EQ(Range(0, 0),
2661 internal::RoundRangeF(run.GetGraphemeBounds(iter.get(), 4)));
2662 Range chars;
2663 Range glyphs;
2664 run.GetClusterAt(4, &chars, &glyphs);
2665 EXPECT_EQ(Range(3, 8), chars);
2666 EXPECT_EQ(Range(0, 0), glyphs);
2669 // Ensure the line breaker doesn't compute the word's width bigger than the
2670 // actual size. See http://crbug.com/470073
2671 TEST_F(RenderTextTest, HarfBuzz_WordWidthWithDiacritics) {
2672 RenderTextHarfBuzz render_text;
2673 const base::string16 kWord = WideToUTF16(L"\u0906\u092A\u0915\u0947 ");
2674 render_text.SetText(kWord);
2675 const gfx::SizeF text_size = render_text.GetStringSizeF();
2677 render_text.SetText(kWord + kWord);
2678 render_text.SetMultiline(true);
2679 EXPECT_EQ(text_size.width() * 2, render_text.GetStringSizeF().width());
2680 EXPECT_EQ(text_size.height(), render_text.GetStringSizeF().height());
2681 render_text.SetDisplayRect(gfx::Rect(0, 0, std::ceil(text_size.width()), 0));
2682 EXPECT_NEAR(text_size.width(), render_text.GetStringSizeF().width(), 1.0f);
2683 EXPECT_EQ(text_size.height() * 2, render_text.GetStringSizeF().height());
2686 // Ensure a string fits in a display rect with a width equal to the string's.
2687 TEST_F(RenderTextTest, StringFitsOwnWidth) {
2688 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2689 const base::string16 kString = ASCIIToUTF16("www.example.com");
2691 render_text->SetText(kString);
2692 render_text->ApplyStyle(BOLD, true, Range(0, 3));
2693 render_text->SetElideBehavior(ELIDE_TAIL);
2695 render_text->SetDisplayRect(Rect(0, 0, 500, 100));
2696 EXPECT_EQ(kString, render_text->GetDisplayText());
2697 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100));
2698 EXPECT_EQ(kString, render_text->GetDisplayText());
2701 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184
2702 #if !defined(OS_WIN)
2703 // Ensure that RenderText examines all of the fonts in its FontList before
2704 // falling back to other fonts.
2705 TEST_F(RenderTextTest, HarfBuzz_FontListFallback) {
2706 // Double-check that the requested fonts are present.
2707 FontList font_list("Arial, Symbol, 12px");
2708 const std::vector<Font>& fonts = font_list.GetFonts();
2709 ASSERT_EQ(2u, fonts.size());
2710 ASSERT_EQ("arial",
2711 base::StringToLowerASCII(fonts[0].GetActualFontNameForTesting()));
2712 ASSERT_EQ("symbol",
2713 base::StringToLowerASCII(fonts[1].GetActualFontNameForTesting()));
2715 // "⊕" (CIRCLED PLUS) should be rendered with Symbol rather than falling back
2716 // to some other font that's present on the system.
2717 RenderTextHarfBuzz render_text;
2718 render_text.SetFontList(font_list);
2719 render_text.SetText(UTF8ToUTF16("\xE2\x8A\x95"));
2720 const std::vector<RenderText::FontSpan> spans =
2721 render_text.GetFontSpansForTesting();
2722 ASSERT_EQ(static_cast<size_t>(1), spans.size());
2723 EXPECT_EQ("Symbol", spans[0].first.GetFontName());
2725 #endif // !defined(OS_WIN)
2727 // Ensure that the fallback fonts of the Uniscribe font are tried for shaping.
2728 #if defined(OS_WIN)
2729 TEST_F(RenderTextTest, HarfBuzz_UniscribeFallback) {
2730 RenderTextHarfBuzz render_text;
2731 PlatformFontWin* font_win = new PlatformFontWin("Meiryo", 12);
2732 // Japanese name for Meiryo. This name won't be found in the system's linked
2733 // fonts, forcing RTHB to try the Uniscribe font and its fallbacks.
2734 font_win->font_ref_->font_name_ = WideToUTF8(L"\x30e1\x30a4\x30ea\x30aa");
2735 FontList font_list((Font(font_win)));
2737 render_text.SetFontList(font_list);
2738 // Korean character "han".
2739 render_text.SetText(WideToUTF16(L"\xd55c"));
2740 render_text.EnsureLayout();
2741 internal::TextRunList* run_list = render_text.GetRunList();
2742 ASSERT_EQ(1U, run_list->size());
2743 EXPECT_EQ(0U, run_list->runs()[0]->CountMissingGlyphs());
2745 #endif // defined(OS_WIN)
2747 // Ensure that the fallback fonts offered by gfx::GetFallbackFontFamilies() are
2748 // tried. Note this test assumes the font "Arial" doesn't provide a unicode
2749 // glyph for a particular character, and that there exists a system fallback
2750 // font which does.
2751 #if defined(OS_WIN) || defined(OS_MACOSX)
2752 TEST_F(RenderTextTest, HarfBuzz_UnicodeFallback) {
2753 RenderTextHarfBuzz render_text;
2754 render_text.SetFontList(FontList("Arial, 12px"));
2756 // Korean character "han".
2757 render_text.SetText(WideToUTF16(L"\xd55c"));
2758 render_text.EnsureLayout();
2759 internal::TextRunList* run_list = render_text.GetRunList();
2760 ASSERT_EQ(1U, run_list->size());
2761 EXPECT_EQ(0U, run_list->runs()[0]->CountMissingGlyphs());
2763 #endif // defined(OS_WIN) || defined(OS_MACOSX)
2765 // Ensure that the width reported by RenderText is sufficient for drawing. Draws
2766 // to a canvas and checks if any pixel beyond the bounding rectangle is colored.
2767 TEST_F(RenderTextTest, TextDoesntClip) {
2768 const wchar_t* kTestStrings[] = {
2769 L" ",
2770 // TODO(dschuyler): Underscores draw outside GetStringSize;
2771 // crbug.com/459812. This appears to be a preexisting issue that wasn't
2772 // revealed by the prior unit tests.
2773 // L"TEST_______",
2774 L"TEST some stuff",
2775 L"WWWWWWWWWW",
2776 L"gAXAXAXAXAXAXA",
2777 // TODO(dschuyler): A-Ring draws outside GetStringSize; crbug.com/459812.
2778 // L"g\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5",
2779 L"\x0647\x0654\x0647\x0654\x0647\x0654\x0647\x0654\x0645\x0631\x062D"
2780 L"\x0628\x0627"};
2781 const Size kCanvasSize(300, 50);
2782 const int kTestSize = 10;
2784 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
2785 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2786 scoped_ptr<Canvas> canvas(
2787 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
2788 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2789 render_text->SetHorizontalAlignment(ALIGN_LEFT);
2790 render_text->SetColor(SK_ColorBLACK);
2792 for (auto string : kTestStrings) {
2793 surface->getCanvas()->clear(SK_ColorWHITE);
2794 render_text->SetText(WideToUTF16(string));
2795 const Size string_size = render_text->GetStringSize();
2796 render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2));
2797 render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4));
2798 render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6));
2799 render_text->ApplyBaselineStyle(SUBSCRIPT, Range(7, 8));
2800 render_text->SetStyle(BOLD, true);
2801 render_text->SetDisplayRect(
2802 Rect(kTestSize, kTestSize, string_size.width(), string_size.height()));
2803 // Allow the RenderText to paint outside of its display rect.
2804 render_text->set_clip_to_display_rect(false);
2805 ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width());
2807 render_text->Draw(canvas.get());
2808 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width());
2809 const uint32* buffer =
2810 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
2811 ASSERT_NE(nullptr, buffer);
2812 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
2813 kCanvasSize.height());
2815 #if !defined(OS_CHROMEOS)
2816 // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect.
2817 SCOPED_TRACE("TextDoesntClip Top Side");
2818 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(),
2819 kTestSize);
2820 #endif
2823 SCOPED_TRACE("TextDoesntClip Bottom Side");
2824 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0,
2825 kTestSize + string_size.height(),
2826 kCanvasSize.width(), kTestSize);
2829 SCOPED_TRACE("TextDoesntClip Left Side");
2830 #if defined(OS_WIN)
2831 // TODO(dschuyler): On Windows XP the Unicode test draws to the left edge
2832 // as if it is ignoring the SetDisplayRect shift by kTestSize. This
2833 // appears to be a preexisting issue that wasn't revealed by the prior
2834 // unit tests.
2835 #elif defined(OS_MACOSX) || defined(OS_CHROMEOS)
2836 // TODO(dschuyler): On Windows (non-XP), Chrome OS and Mac smoothing draws
2837 // left of text. This appears to be a preexisting issue that wasn't
2838 // revealed by the prior unit tests.
2839 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize - 1,
2840 string_size.height());
2841 #else
2842 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize,
2843 string_size.height());
2844 #endif
2847 SCOPED_TRACE("TextDoesntClip Right Side");
2848 #if !defined(OS_MACOSX)
2849 // TODO(dschuyler): On Mac text draws to right of GetStringSize. This
2850 // appears to be a preexisting issue that wasn't revealed by the prior
2851 // unit tests.
2852 rect_buffer.EnsureSolidRect(SK_ColorWHITE,
2853 kTestSize + string_size.width(), kTestSize,
2854 kTestSize, string_size.height());
2855 #endif
2860 // Ensure that the text will clip to the display rect. Draws to a canvas and
2861 // checks whether any pixel beyond the bounding rectangle is colored.
2862 TEST_F(RenderTextTest, TextDoesClip) {
2863 const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"};
2864 const Size kCanvasSize(300, 50);
2865 const int kTestSize = 10;
2867 skia::RefPtr<SkSurface> surface = skia::AdoptRef(
2868 SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
2869 scoped_ptr<Canvas> canvas(
2870 Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
2871 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
2872 render_text->SetHorizontalAlignment(ALIGN_LEFT);
2873 render_text->SetColor(SK_ColorBLACK);
2875 for (auto string : kTestStrings) {
2876 surface->getCanvas()->clear(SK_ColorWHITE);
2877 render_text->SetText(WideToUTF16(string));
2878 const Size string_size = render_text->GetStringSize();
2879 int fake_width = string_size.width() / 2;
2880 int fake_height = string_size.height() / 2;
2881 render_text->SetDisplayRect(
2882 Rect(kTestSize, kTestSize, fake_width, fake_height));
2883 render_text->set_clip_to_display_rect(true);
2884 render_text->Draw(canvas.get());
2885 ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width());
2886 const uint32* buffer =
2887 static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr));
2888 ASSERT_NE(nullptr, buffer);
2889 TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(),
2890 kCanvasSize.height());
2892 SCOPED_TRACE("TextDoesClip Top Side");
2893 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(),
2894 kTestSize);
2898 SCOPED_TRACE("TextDoesClip Bottom Side");
2899 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize + fake_height,
2900 kCanvasSize.width(), kTestSize);
2903 SCOPED_TRACE("TextDoesClip Left Side");
2904 rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize,
2905 fake_height);
2908 SCOPED_TRACE("TextDoesClip Right Side");
2909 rect_buffer.EnsureSolidRect(SK_ColorWHITE, kTestSize + fake_width,
2910 kTestSize, kTestSize, fake_height);
2915 #if defined(OS_MACOSX)
2916 TEST_F(RenderTextTest, Mac_ElidedText) {
2917 RenderTextMac render_text;
2918 base::string16 text(ASCIIToUTF16("This is an example."));
2919 render_text.SetText(text);
2920 gfx::Size string_size = render_text.GetStringSize();
2921 render_text.SetDisplayRect(gfx::Rect(string_size));
2922 render_text.EnsureLayout();
2923 // NOTE: Character and glyph counts are only comparable for simple text.
2924 EXPECT_EQ(text.size(),
2925 static_cast<size_t>(CTLineGetGlyphCount(render_text.line_)));
2927 render_text.SetElideBehavior(ELIDE_TAIL);
2928 string_size.set_width(string_size.width() / 2);
2929 render_text.SetDisplayRect(gfx::Rect(string_size));
2930 render_text.EnsureLayout();
2931 CFIndex glyph_count = CTLineGetGlyphCount(render_text.line_);
2932 EXPECT_GT(text.size(), static_cast<size_t>(glyph_count));
2933 EXPECT_NE(0, glyph_count);
2935 #endif
2937 } // namespace gfx