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 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 render_text->RenderText::SetObscuredRevealIndex(2); | 400 render_text->RenderText::SetObscuredRevealIndex(2); |
401 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text->GetLayoutText()); | 401 EXPECT_EQ(ASCIIToUTF16("*h***"), render_text->GetLayoutText()); |
402 render_text->RenderText::SetObscuredRevealIndex(5); | 402 render_text->RenderText::SetObscuredRevealIndex(5); |
403 const base::char16 valid_expect_5_and_6[] = | 403 const base::char16 valid_expect_5_and_6[] = |
404 {'*', '*', '*', '*', 0xD800, 0xDC00, 0}; | 404 {'*', '*', '*', '*', 0xD800, 0xDC00, 0}; |
405 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText()); | 405 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText()); |
406 render_text->RenderText::SetObscuredRevealIndex(6); | 406 render_text->RenderText::SetObscuredRevealIndex(6); |
407 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText()); | 407 EXPECT_EQ(valid_expect_5_and_6, render_text->GetLayoutText()); |
408 } | 408 } |
409 | 409 |
| 410 TEST_F(RenderTextTest, ElidedText) { |
| 411 // TODO(skanuj) : Add more test cases for following |
| 412 // - RenderText styles. |
| 413 // - Cross interaction of truncate, elide and obscure. |
| 414 // - ElideText tests from text_elider.cc. |
| 415 struct { |
| 416 const wchar_t* text; |
| 417 const wchar_t* layout_text; |
| 418 const bool elision_expected; |
| 419 } cases[] = { |
| 420 // Strings shorter than the elision width should be laid out in full. |
| 421 { L"", L"" , false }, |
| 422 { kWeak, kWeak , false }, |
| 423 { kLtr, kLtr , false }, |
| 424 { kLtrRtl, kLtrRtl , false }, |
| 425 { kLtrRtlLtr, kLtrRtlLtr, false }, |
| 426 { kRtl, kRtl , false }, |
| 427 { kRtlLtr, kRtlLtr , false }, |
| 428 { kRtlLtrRtl, kRtlLtrRtl, false }, |
| 429 // Strings as long as the elision width should be laid out in full. |
| 430 { L"012ab", L"012ab" , false }, |
| 431 // Long strings should be elided with an ellipsis appended at the end. |
| 432 { L"012abc", L"012a\x2026", true }, |
| 433 { L"012ab" L"\x5d0\x5d1", L"012a\x2026", true }, |
| 434 { L"012a" L"\x5d1" L"b", L"012a\x2026", true }, |
| 435 // No RLM marker added as digits (012) have weak directionality. |
| 436 { L"01" L"\x5d0\x5d1\x5d2", L"01\x5d0\x5d1\x2026", true }, |
| 437 // RLM marker added as "ab" have strong LTR directionality. |
| 438 { L"ab" L"\x5d0\x5d1\x5d2", L"ab\x5d0\x5d1\x2026\x200f", true }, |
| 439 // Complex script is not handled. In this example, the "\x0915\x093f" is a |
| 440 // compound glyph, but only half of it is elided. |
| 441 { L"0123\x0915\x093f", L"0123\x0915\x2026", true }, |
| 442 // Surrogate pairs should be elided reasonably enough. |
| 443 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8", false }, |
| 444 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x2026" , true }, |
| 445 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x2026" , true }, |
| 446 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x2026" , true }, |
| 447 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" , true }, |
| 448 }; |
| 449 |
| 450 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance()); |
| 451 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px")); |
| 452 expected_render_text->SetDisplayRect(gfx::Rect(0, 0, 9999, 100)); |
| 453 |
| 454 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 455 render_text->SetFontList(FontList("serif, Sans serif, 12px")); |
| 456 render_text->SetElideBehavior(gfx::ELIDE_AT_END); |
| 457 |
| 458 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
| 459 // Compute expected width |
| 460 expected_render_text->SetText(WideToUTF16(cases[i].layout_text)); |
| 461 int expected_width = expected_render_text->GetContentWidth(); |
| 462 |
| 463 base::string16 input = WideToUTF16(cases[i].text); |
| 464 // Extend the input text to ensure that it is wider than the layout_text, |
| 465 // and so it will get elided. |
| 466 if (cases[i].elision_expected) |
| 467 input.append(WideToUTF16(L" MMMMMMMMMMM")); |
| 468 |
| 469 render_text->SetText(input); |
| 470 render_text->SetDisplayRect(gfx::Rect(0, 0, expected_width, 100)); |
| 471 EXPECT_EQ(input, render_text->text()); |
| 472 EXPECT_EQ(WideToUTF16(cases[i].layout_text), render_text->GetLayoutText()) |
| 473 << "->For case " << i << ": " << cases[i].text << "\n"; |
| 474 expected_render_text->SetText(base::string16()); |
| 475 } |
| 476 } |
| 477 |
| 478 TEST_F(RenderTextTest, ElidedObscuredText) { |
| 479 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance()); |
| 480 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px")); |
| 481 expected_render_text->SetDisplayRect(gfx::Rect(0, 0, 9999, 100)); |
| 482 expected_render_text->SetText(WideToUTF16(L"**\x2026")); |
| 483 |
| 484 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 485 render_text->SetFontList(FontList("serif, Sans serif, 12px")); |
| 486 render_text->SetElideBehavior(gfx::ELIDE_AT_END); |
| 487 render_text->SetDisplayRect( |
| 488 gfx::Rect(0, 0, expected_render_text->GetContentWidth(), 100)); |
| 489 render_text->SetObscured(true); |
| 490 render_text->SetText(WideToUTF16(L"abcdef")); |
| 491 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text()); |
| 492 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetLayoutText()); |
| 493 } |
| 494 |
410 TEST_F(RenderTextTest, TruncatedText) { | 495 TEST_F(RenderTextTest, TruncatedText) { |
411 struct { | 496 struct { |
412 const wchar_t* text; | 497 const wchar_t* text; |
413 const wchar_t* layout_text; | 498 const wchar_t* layout_text; |
414 } cases[] = { | 499 } cases[] = { |
415 // Strings shorter than the truncation length should be laid out in full. | 500 // Strings shorter than the truncation length should be laid out in full. |
416 { L"", L"" }, | 501 { L"", L"" }, |
417 { kWeak, kWeak }, | 502 { kWeak, kWeak }, |
418 { kLtr, kLtr }, | 503 { kLtr, kLtr }, |
419 { kLtrRtl, kLtrRtl }, | 504 { kLtrRtl, kLtrRtl }, |
(...skipping 1378 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 |