| 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 } cases[] = { | |
| 413 // Strings shorter than the elision width should be laid out in full. | |
| 414 { L"" , L"" }, | |
| 415 { kWeak , kWeak }, | |
| 416 { kLtr , kLtr }, | |
| 417 { kLtrRtl , kLtrRtl }, | |
| 418 { kLtrRtlLtr , kLtrRtlLtr }, | |
| 419 { kRtl , kRtl }, | |
| 420 { kRtlLtr , kRtlLtr }, | |
| 421 { kRtlLtrRtl , kRtlLtrRtl }, | |
| 422 // Strings as long as the elision width should be laid out in full. | |
| 423 { L"012ab", L"012ab" }, | |
| 424 // Long strings should be elided with an ellipsis appended at the end. | |
| 425 { L"012" L"abc" , L"012a\x2026" }, | |
| 426 { L"012" L"ab" L"\x5d0\x5d1" , L"012a\x2026" }, | |
| 427 { L"012" L"a" L"\x5d1" L"b" , L"012a\x2026" }, | |
| 428 // No RLM marker added as digits (012) have weak directionality. | |
| 429 { L"01" L"\x5d0\x5d1\x5d2" L"ab" , L"01\x5d0\x5d1\x2026" }, | |
| 430 // RLM marker added as "ab" have strong LTR directionality. | |
| 431 { L"ab" L"\x5d0\x5d1\x5d2" L"ab" , L"ab\x5d0\x5d1\x2026\x200f" }, | |
| 432 // Complex script is not handled. In this example, the "\x0915\x093f" is a | |
| 433 // compound glyph, but only half of it is elided. | |
| 434 { L"0123\x0915\x093f" L"abcd" , L"0123\x0915\x2026" }, | |
| 435 // Surrogate pairs should be elided reasonably enough. | |
| 436 { L"0\x05e9\x05bc\x05c1\x05b8" , L"0\x05e9\x05bc\x05c1\x05b8" }, | |
| 437 { L"0\x05e9\x05bc\x05c1\x05b8" L"ab" , L"0\x05e9\x05bc\x2026" }, | |
| 438 { L"01\x05e9\x05bc\x05c1\x05b8" L"ab" , L"01\x05e9\x2026" }, | |
| 439 { L"012\x05e9\x05bc\x05c1\x05b8" L"ab" , L"012\x2026" }, | |
| 440 { L"012\xF0\x9D\x84\x9E" , L"012\xF0\x2026" }, | |
| 441 }; | |
| 442 | |
| 443 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance()); | |
| 444 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px")); | |
| 445 expected_render_text->SetDisplayRect(gfx::Rect(0, 0, 9999, 100)); | |
| 446 | |
| 447 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | |
| 448 render_text->SetFontList(FontList("serif, Sans serif, 12px")); | |
| 449 render_text->SetElideBehavior(gfx::ELIDE_AT_END); | |
| 450 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { | |
| 451 expected_render_text->SetText(WideToUTF16(cases[i].layout_text)); | |
| 452 int expected_width = expected_render_text->GetContentWidth(); | |
| 453 render_text->SetText(WideToUTF16(cases[i].text)); | |
| 454 #if defined(OS_WIN) | |
| 455 // TODO(skanuj): It seems in some cases (on Windows XP), the original text | |
| 456 // is shorter than the elided text. Those cases should be modified by | |
| 457 // adding extra chars in the end. Currently this problem happens for the | |
| 458 // following test case: | |
| 459 // { L"012\xF0\x9D\x84\x9E" , L"012\xF0\x2026" }, | |
| 460 int original_width = render_text->GetContentWidth(); | |
| 461 if ((base::win::GetVersion() < base::win::VERSION_VISTA) && | |
| 462 (expected_width >= original_width)) | |
| 463 continue; | |
| 464 #endif | |
| 465 render_text->SetDisplayRect(gfx::Rect(0, 0, expected_width, 100)); | |
| 466 EXPECT_EQ(WideToUTF16(cases[i].text), render_text->text()); | |
| 467 EXPECT_EQ(WideToUTF16(cases[i].layout_text), render_text->GetLayoutText()) | |
| 468 << "->For case " << i << ": " << cases[i].text << "\n"; | |
| 469 expected_render_text->SetText(base::string16()); | |
| 470 } | |
| 471 } | |
| 472 | |
| 473 TEST_F(RenderTextTest, ElidedObscuredText) { | |
| 474 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance()); | |
| 475 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px")); | |
| 476 expected_render_text->SetDisplayRect(gfx::Rect(0, 0, 9999, 100)); | |
| 477 expected_render_text->SetText(WideToUTF16(L"**\x2026")); | |
| 478 | |
| 479 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | |
| 480 render_text->SetFontList(FontList("serif, Sans serif, 12px")); | |
| 481 render_text->SetElideBehavior(gfx::ELIDE_AT_END); | |
| 482 render_text->SetDisplayRect( | |
| 483 gfx::Rect(0, 0, expected_render_text->GetContentWidth(), 100)); | |
| 484 render_text->SetObscured(true); | |
| 485 render_text->SetText(WideToUTF16(L"abcdef")); | |
| 486 EXPECT_EQ(WideToUTF16(L"abcdef"), render_text->text()); | |
| 487 EXPECT_EQ(WideToUTF16(L"**\x2026"), render_text->GetLayoutText()); | |
| 488 } | |
| 489 | |
| 490 TEST_F(RenderTextTest, TruncatedText) { | 404 TEST_F(RenderTextTest, TruncatedText) { |
| 491 struct { | 405 struct { |
| 492 const wchar_t* text; | 406 const wchar_t* text; |
| 493 const wchar_t* layout_text; | 407 const wchar_t* layout_text; |
| 494 } cases[] = { | 408 } cases[] = { |
| 495 // Strings shorter than the truncation length should be laid out in full. | 409 // Strings shorter than the truncation length should be laid out in full. |
| 496 { L"", L"" }, | 410 { L"", L"" }, |
| 497 { kWeak, kWeak }, | 411 { kWeak, kWeak }, |
| 498 { kLtr, kLtr }, | 412 { kLtr, kLtr }, |
| 499 { kLtrRtl, kLtrRtl }, | 413 { kLtrRtl, kLtrRtl }, |
| (...skipping 1384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1884 render_text->SetText(WideToUTF16(L"x \x25B6 y")); | 1798 render_text->SetText(WideToUTF16(L"x \x25B6 y")); |
| 1885 render_text->EnsureLayout(); | 1799 render_text->EnsureLayout(); |
| 1886 ASSERT_EQ(3U, render_text->runs_.size()); | 1800 ASSERT_EQ(3U, render_text->runs_.size()); |
| 1887 EXPECT_EQ(Range(0, 2), render_text->runs_[0]->range); | 1801 EXPECT_EQ(Range(0, 2), render_text->runs_[0]->range); |
| 1888 EXPECT_EQ(Range(2, 3), render_text->runs_[1]->range); | 1802 EXPECT_EQ(Range(2, 3), render_text->runs_[1]->range); |
| 1889 EXPECT_EQ(Range(3, 5), render_text->runs_[2]->range); | 1803 EXPECT_EQ(Range(3, 5), render_text->runs_[2]->range); |
| 1890 } | 1804 } |
| 1891 #endif // defined(OS_WIN) | 1805 #endif // defined(OS_WIN) |
| 1892 | 1806 |
| 1893 } // namespace gfx | 1807 } // namespace gfx |
| OLD | NEW |