Chromium Code Reviews| 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 { L" . ", L" . " , false }, | |
|
msw
2013/12/27 22:54:33
nit: this case is equivalent to kWeak, right? Remo
Anuj
2013/12/30 22:46:58
Done.
| |
| 417 { kWeak, kWeak , false }, | |
| 418 { kLtr, kLtr , false }, | |
| 419 { kLtrRtl, kLtrRtl , false }, | |
| 420 { kLtrRtlLtr, kLtrRtlLtr, false }, | |
| 421 { kRtl, kRtl , false }, | |
| 422 { kRtlLtr, kRtlLtr , false }, | |
| 423 { kRtlLtrRtl, kRtlLtrRtl, false }, | |
| 424 // Strings as long as the elision width should be laid out in full. | |
| 425 { L"012ab", L"012ab" , false }, | |
| 426 // Long strings should be elided with an ellipsis appended at the end. | |
| 427 { L"012abc", L"012a\x2026", true }, | |
| 428 { L"012ab" L"\x5d0\x5d1", L"012a\x2026", true }, | |
| 429 { L"012a" L"\x5d1" L"b", L"012a\x2026", true }, | |
| 430 // No RLM marker added as digits (012) have weak directionality. | |
| 431 { L"01" L"\x5d0\x5d1\x5d2", L"01\x5d0\x5d1\x2026", true }, | |
| 432 // RLM marker added as "ab" have strong LTR directionality. | |
| 433 { L"ab" L"\x5d0\x5d1\x5d2", L"ab\x5d0\x5d1\x2026\x200f", true }, | |
| 434 // Complex script is not handled. In this example, the "\x0915\x093f" is a | |
| 435 // compound glyph, but only half of it is elided. | |
| 436 { L"0123\x0915\x093f", L"0123\x0915\x2026", true }, | |
| 437 // Surrogate pairs should be elided reasonably enough. | |
| 438 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x05c1\x05b8", false }, | |
| 439 { L"0\x05e9\x05bc\x05c1\x05b8", L"0\x05e9\x05bc\x2026" , true }, | |
| 440 { L"01\x05e9\x05bc\x05c1\x05b8", L"01\x05e9\x2026" , true }, | |
| 441 { L"012\x05e9\x05bc\x05c1\x05b8", L"012\x2026" , true }, | |
| 442 { L"012\xF0\x9D\x84\x9E", L"012\xF0\x2026" , true }, | |
| 443 }; | |
| 444 | |
| 445 scoped_ptr<RenderText> expected_render_text(RenderText::CreateInstance()); | |
| 446 expected_render_text->SetFontList(FontList("serif, Sans serif, 12px")); | |
| 447 expected_render_text->SetDisplayRect(gfx::Rect(0, 0, 9999, 100)); | |
| 448 | |
| 449 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | |
| 450 render_text->SetFontList(FontList("serif, Sans serif, 12px")); | |
| 451 render_text->SetElideBehavior(gfx::ELIDE_AT_END); | |
| 452 | |
| 453 for (size_t i = 1; i < ARRAYSIZE_UNSAFE(cases); i++) { | |
|
msw
2013/12/27 22:54:33
Why do you skip the first test case of an empty st
Anuj
2013/12/30 22:46:58
Fixed. I was skipping it for testing the testcases
| |
| 454 // Compute expected width | |
| 455 expected_render_text->SetText(WideToUTF16(cases[i].layout_text)); | |
| 456 int expected_width = expected_render_text->GetContentWidth(); | |
| 457 | |
| 458 base::string16 input = WideToUTF16(cases[i].text); | |
| 459 // Extend input to always be wide enough to be elided. | |
|
msw
2013/12/27 22:54:33
nit: add to this comment ", otherwise the layout t
Anuj
2013/12/30 22:46:58
Reworded the comment. I think this is a reasonable
| |
| 460 if (cases[i].elision_expected) | |
| 461 input += WideToUTF16(L" MMMMMMMMMMM"); | |
|
msw
2013/12/27 22:54:33
Use append, and shouldn't this use characters with
Anuj
2013/12/30 22:46:58
I am curious about preference for append? The two
msw
2013/12/30 23:17:59
Don't you check the trailing directionality of the
Anuj
2013/12/31 00:04:03
I do. But the string which remains matters. This p
msw
2013/12/31 00:24:52
Ah, good point, thanks for clarifying.
Anuj
2013/12/31 00:42:03
Done.
| |
| 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 |