OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/render_text.h" | 5 #include "ui/gfx/render_text.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 EXPECT_EQ(valid_expect_0_and_1, render_text->GetLayoutText()); | 394 EXPECT_EQ(valid_expect_0_and_1, render_text->GetLayoutText()); |
395 render_text->RenderText::SetObscuredRevealIndex(2); | 395 render_text->RenderText::SetObscuredRevealIndex(2); |
396 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text->GetLayoutText()); | 396 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text->GetLayoutText()); |
397 render_text->RenderText::SetObscuredRevealIndex(5); | 397 render_text->RenderText::SetObscuredRevealIndex(5); |
398 const char16 valid_expect_5_and_6[] = {'*', '*', '*', '*', 0xD800, 0xDC00, 0}; | 398 const char16 valid_expect_5_and_6[] = {'*', '*', '*', '*', 0xD800, 0xDC00, 0}; |
399 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText()); | 399 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText()); |
400 render_text->RenderText::SetObscuredRevealIndex(6); | 400 render_text->RenderText::SetObscuredRevealIndex(6); |
401 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText()); | 401 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText()); |
402 } | 402 } |
403 | 403 |
| 404 TEST_F(RenderTextTest, ElidedText) { |
| 405 // TODO(skanuj) : Add more test cases for following |
| 406 // - RenderText styles. |
| 407 // - Cross interaction of truncate, elide and obscure. |
| 408 // - ElideText tests from text_elider.cc. |
| 409 struct { |
| 410 const wchar_t* text; |
| 411 const wchar_t* layout_text; |
| 412 const bool elision_expected; |
| 413 } cases[] = { |
| 414 // Strings shorter than the elision width should be laid out in full. |
| 415 { L"", L"" , false }, |
| 416 { kWeak, kWeak , false }, |
| 417 { kLtr, kLtr , false }, |
| 418 { kLtrRtl, kLtrRtl , false }, |
| 419 { kLtrRtlLtr, kLtrRtlLtr, false }, |
| 420 { kRtl, kRtl , false }, |
| 421 { kRtlLtr, kRtlLtr , false }, |
| 422 { kRtlLtrRtl, kRtlLtrRtl, false }, |
| 423 // Strings as long as the elision width should be laid out in full. |
| 424 { L"012ab", L"012ab" , false }, |
| 425 // Long strings should be elided with an ellipsis appended at the end. |
| 426 { L"012abc", L"012a\x2026", true }, |
| 427 { L"012ab" L"\x5d0\x5d1", L"012a\x2026", true }, |
| 428 { L"012a" L"\x5d1" L"b", L"012a\x2026", true }, |
| 429 // No RLM marker added as digits (012) have weak directionality. |
| 430 { L"01" L"\x5d0\x5d1\x5d2", L"01\x5d0\x5d1\x2026", true }, |
| 431 // RLM marker added as "ab" have strong LTR directionality. |
| 432 { L"ab" L"\x5d0\x5d1\x5d2", L"ab\x5d0\x5d1\x2026\x200f", true }, |
| 433 // Complex script is not handled. In this example, the "\x0915\x093f" is a |
| 434 // compound glyph, but only half of it is elided. |
| 435 { L"0123\x0915\x093f", L"0123\x0915\x2026", true }, |
| 436 // Surrogate pairs should be elided reasonably enough. |
| 437 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8", false }, |
| 438 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x2026" , true }, |
| 439 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x2026" , true }, |
| 440 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x2026" , true }, |
| 441 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" , true }, |
| 442 }; |
| 443 |
| 444 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance()); |
| 445 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px")); |
| 446 expected_render_text->SetDisplayRect(gfx::Rect(0, 0, 9999, 100)); |
| 447 |
| 448 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 449 render_text->SetFontList(FontList("serif, Sans serif, 12px")); |
| 450 render_text->SetElideBehavior(gfx::ELIDE_AT_END); |
| 451 |
| 452 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
| 453 // Compute expected width |
| 454 expected_render_text->SetText(WideToUTF16(cases[i].layout_text)); |
| 455 int expected_width = expected_render_text->GetContentWidth(); |
| 456 |
| 457 base::string16 input = WideToUTF16(cases[i].text); |
| 458 // Extend the input text to ensure that it is wider than the layout_text, |
| 459 // and so it will get elided. |
| 460 if (cases[i].elision_expected) |
| 461 input.append(WideToUTF16(L" MMMMMMMMMMM")); |
| 462 |
| 463 render_text->SetText(input); |
| 464 render_text->SetDisplayRect(gfx::Rect(0, 0, expected_width, 100)); |
| 465 EXPECT_EQ(input, render_text->text()); |
| 466 EXPECT_EQ(WideToUTF16(cases[i].layout_text), render_text->GetLayoutText()) |
| 467 << "->For case " << i << ": " << cases[i].text << "\n"; |
| 468 expected_render_text->SetText(base::string16()); |
| 469 } |
| 470 } |
| 471 |
| 472 TEST_F(RenderTextTest, ElidedObscuredText) { |
| 473 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance()); |
| 474 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px")); |
| 475 expected_render_text->SetDisplayRect(gfx::Rect(0, 0, 9999, 100)); |
| 476 expected_render_text->SetText(WideToUTF16(L"**\x2026")); |
| 477 |
| 478 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 479 render_text->SetFontList(FontList("serif, Sans serif, 12px")); |
| 480 render_text->SetElideBehavior(gfx::ELIDE_AT_END); |
| 481 render_text->SetDisplayRect( |
| 482 gfx::Rect(0, 0, expected_render_text->GetContentWidth(), 100)); |
| 483 render_text->SetObscured(true); |
| 484 render_text->SetText(WideToUTF16(L"abcdef")); |
| 485 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text()); |
| 486 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetLayoutText()); |
| 487 } |
| 488 |
404 TEST_F(RenderTextTest, TruncatedText) { | 489 TEST_F(RenderTextTest, TruncatedText) { |
405 struct { | 490 struct { |
406 const wchar_t* text; | 491 const wchar_t* text; |
407 const wchar_t* layout_text; | 492 const wchar_t* layout_text; |
408 } cases[] = { | 493 } cases[] = { |
409 // Strings shorter than the truncation length should be laid out in full. | 494 // Strings shorter than the truncation length should be laid out in full. |
410 { L"", L"" }, | 495 { L"", L"" }, |
411 { kWeak, kWeak }, | 496 { kWeak, kWeak }, |
412 { kLtr, kLtr }, | 497 { kLtr, kLtr }, |
413 { kLtrRtl, kLtrRtl }, | 498 { kLtrRtl, kLtrRtl }, |
(...skipping 1384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1798 render_text->SetText(WideToUTF16(L"x \x25B6 y")); | 1883 render_text->SetText(WideToUTF16(L"x \x25B6 y")); |
1799 render_text->EnsureLayout(); | 1884 render_text->EnsureLayout(); |
1800 ASSERT_EQ(3U, render_text->runs_.size()); | 1885 ASSERT_EQ(3U, render_text->runs_.size()); |
1801 EXPECT_EQ(Range(0, 2), render_text->runs_[0]->range); | 1886 EXPECT_EQ(Range(0, 2), render_text->runs_[0]->range); |
1802 EXPECT_EQ(Range(2, 3), render_text->runs_[1]->range); | 1887 EXPECT_EQ(Range(2, 3), render_text->runs_[1]->range); |
1803 EXPECT_EQ(Range(3, 5), render_text->runs_[2]->range); | 1888 EXPECT_EQ(Range(3, 5), render_text->runs_[2]->range); |
1804 } | 1889 } |
1805 #endif // defined(OS_WIN) | 1890 #endif // defined(OS_WIN) |
1806 | 1891 |
1807 } // namespace gfx | 1892 } // namespace gfx |
OLD | NEW |