OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
| 11 #if defined(OS_WIN) |
| 12 #include "base/win/windows_version.h" |
| 13 #endif |
| 14 |
11 namespace gfx { | 15 namespace gfx { |
12 | 16 |
13 class RenderTextTest : public testing::Test { | 17 class RenderTextTest : public testing::Test { |
14 }; | 18 }; |
15 | 19 |
16 TEST_F(RenderTextTest, DefaultStyle) { | 20 TEST_F(RenderTextTest, DefaultStyle) { |
17 // Defaults to empty text with no styles. | 21 // Defaults to empty text with no styles. |
18 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | 22 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); |
19 EXPECT_TRUE(render_text->text().empty()); | 23 EXPECT_TRUE(render_text->text().empty()); |
20 EXPECT_TRUE(render_text->style_ranges().empty()); | 24 EXPECT_TRUE(render_text->style_ranges().empty()); |
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 EXPECT_EQ(4U, render_text->GetCursorPosition()); | 540 EXPECT_EQ(4U, render_text->GetCursorPosition()); |
537 render_text->MoveCursorLeft(CHARACTER_BREAK, false); | 541 render_text->MoveCursorLeft(CHARACTER_BREAK, false); |
538 EXPECT_EQ(2U, render_text->GetCursorPosition()); | 542 EXPECT_EQ(2U, render_text->GetCursorPosition()); |
539 render_text->MoveCursorLeft(CHARACTER_BREAK, false); | 543 render_text->MoveCursorLeft(CHARACTER_BREAK, false); |
540 EXPECT_EQ(0U, render_text->GetCursorPosition()); | 544 EXPECT_EQ(0U, render_text->GetCursorPosition()); |
541 render_text->MoveCursorLeft(CHARACTER_BREAK, false); | 545 render_text->MoveCursorLeft(CHARACTER_BREAK, false); |
542 EXPECT_EQ(0U, render_text->GetCursorPosition()); | 546 EXPECT_EQ(0U, render_text->GetCursorPosition()); |
543 } | 547 } |
544 #endif | 548 #endif |
545 | 549 |
| 550 TEST_F(RenderTextTest, GraphemePositions) { |
| 551 // LTR 2-character grapheme, LTR abc, LTR 2-character grapheme. |
| 552 const string16 kText1 = WideToUTF16(L"\x0915\x093f"L"abc"L"\x0915\x093f"); |
| 553 |
| 554 // LTR ab, LTR 2-character grapheme, LTR cd. |
| 555 const string16 kText2 = WideToUTF16(L"ab"L"\x0915\x093f"L"cd"); |
| 556 |
| 557 struct { |
| 558 string16 text; |
| 559 size_t index; |
| 560 size_t expected_previous; |
| 561 size_t expected_next; |
| 562 } cases[] = { |
| 563 { string16(), 0, 0, 0 }, |
| 564 { string16(), 1, 0, 0 }, |
| 565 { string16(), 50, 0, 0 }, |
| 566 { kText1, 0, 0, 2 }, |
| 567 { kText1, 1, 0, 2 }, |
| 568 { kText1, 2, 0, 3 }, |
| 569 { kText1, 3, 2, 4 }, |
| 570 { kText1, 4, 3, 5 }, |
| 571 { kText1, 5, 4, 7 }, |
| 572 { kText1, 6, 5, 7 }, |
| 573 { kText1, 7, 5, 7 }, |
| 574 { kText1, 8, 7, 7 }, |
| 575 { kText1, 50, 7, 7 }, |
| 576 { kText2, 0, 0, 1 }, |
| 577 { kText2, 1, 0, 2 }, |
| 578 { kText2, 2, 1, 4 }, |
| 579 { kText2, 3, 2, 4 }, |
| 580 { kText2, 4, 2, 5 }, |
| 581 { kText2, 5, 4, 6 }, |
| 582 { kText2, 6, 5, 6 }, |
| 583 { kText2, 7, 6, 6 }, |
| 584 { kText2, 50, 6, 6 }, |
| 585 }; |
| 586 |
| 587 // TODO(asvitkine): Disable tests that fail on XP bots due to lack of complete |
| 588 // font support for some scripts - http://crbug.com/106450 |
| 589 #if defined(OS_WIN) |
| 590 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| 591 return; |
| 592 #endif |
| 593 |
| 594 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); |
| 595 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
| 596 render_text->SetText(cases[i].text); |
| 597 |
| 598 size_t next = render_text->GetIndexOfNextGrapheme(cases[i].index); |
| 599 EXPECT_EQ(cases[i].expected_next, next); |
| 600 EXPECT_TRUE(render_text->IsCursorablePosition(next)); |
| 601 |
| 602 size_t previous = render_text->GetIndexOfPreviousGrapheme(cases[i].index); |
| 603 EXPECT_EQ(cases[i].expected_previous, previous); |
| 604 EXPECT_TRUE(render_text->IsCursorablePosition(previous)); |
| 605 } |
| 606 } |
| 607 |
| 608 TEST_F(RenderTextTest, SelectionModels) { |
| 609 // Simple Latin text. |
| 610 const string16 kLatin = WideToUTF16(L"abc"); |
| 611 // LTR 2-character grapheme. |
| 612 const string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f"); |
| 613 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme. |
| 614 const string16 kHindiLatin = WideToUTF16(L"\x0915\x093f"L"a"L"\x0915\x093f"); |
| 615 // RTL 2-character grapheme. |
| 616 const string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8"); |
| 617 // RTL 2-character grapheme, LTR a, RTL 2-character grapheme. |
| 618 const string16 kHebrewLatin = WideToUTF16(L"\x05e0\x05b8"L"a"L"\x05e0\x05b8"); |
| 619 |
| 620 struct { |
| 621 string16 text; |
| 622 size_t expected_left_end_caret; |
| 623 SelectionModel::CaretPlacement expected_left_end_placement; |
| 624 size_t expected_right_end_caret; |
| 625 SelectionModel::CaretPlacement expected_right_end_placement; |
| 626 } cases[] = { |
| 627 { string16(), 0, SelectionModel::LEADING, 0, SelectionModel::LEADING }, |
| 628 { kLatin, 0, SelectionModel::LEADING, 2, SelectionModel::TRAILING }, |
| 629 { kLTRGrapheme, 0, SelectionModel::LEADING, 0, SelectionModel::TRAILING }, |
| 630 { kHindiLatin, 0, SelectionModel::LEADING, 3, SelectionModel::TRAILING }, |
| 631 { kRTLGrapheme, 0, SelectionModel::TRAILING, 0, SelectionModel::LEADING }, |
| 632 #if defined(OS_LINUX) |
| 633 // On Linux, the whole string is displayed RTL, rather than individual runs. |
| 634 { kHebrewLatin, 3, SelectionModel::TRAILING, 0, SelectionModel::LEADING }, |
| 635 #else |
| 636 { kHebrewLatin, 0, SelectionModel::TRAILING, 3, SelectionModel::LEADING }, |
| 637 #endif |
| 638 }; |
| 639 |
| 640 // TODO(asvitkine): Disable tests that fail on XP bots due to lack of complete |
| 641 // font support for some scripts - http://crbug.com/106450 |
| 642 #if defined(OS_WIN) |
| 643 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| 644 return; |
| 645 #endif |
| 646 |
| 647 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); |
| 648 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
| 649 render_text->SetText(cases[i].text); |
| 650 |
| 651 SelectionModel model = render_text->LeftEndSelectionModel(); |
| 652 EXPECT_EQ(cases[i].expected_left_end_caret, model.caret_pos()); |
| 653 EXPECT_TRUE(render_text->IsCursorablePosition(model.caret_pos())); |
| 654 EXPECT_EQ(cases[i].expected_left_end_placement, model.caret_placement()); |
| 655 |
| 656 model = render_text->RightEndSelectionModel(); |
| 657 EXPECT_EQ(cases[i].expected_right_end_caret, model.caret_pos()); |
| 658 EXPECT_TRUE(render_text->IsCursorablePosition(model.caret_pos())); |
| 659 EXPECT_EQ(cases[i].expected_right_end_placement, model.caret_placement()); |
| 660 } |
| 661 } |
| 662 |
546 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) { | 663 TEST_F(RenderTextTest, MoveCursorLeftRightWithSelection) { |
547 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); | 664 scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); |
548 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); | 665 render_text->SetText(WideToUTF16(L"abc\x05d0\x05d1\x05d2")); |
549 // Left arrow on select ranging (6, 4). | 666 // Left arrow on select ranging (6, 4). |
550 render_text->MoveCursorRight(LINE_BREAK, false); | 667 render_text->MoveCursorRight(LINE_BREAK, false); |
551 EXPECT_EQ(6U, render_text->GetCursorPosition()); | 668 EXPECT_EQ(6U, render_text->GetCursorPosition()); |
552 render_text->MoveCursorLeft(CHARACTER_BREAK, false); | 669 render_text->MoveCursorLeft(CHARACTER_BREAK, false); |
553 EXPECT_EQ(4U, render_text->GetCursorPosition()); | 670 EXPECT_EQ(4U, render_text->GetCursorPosition()); |
554 render_text->MoveCursorLeft(CHARACTER_BREAK, false); | 671 render_text->MoveCursorLeft(CHARACTER_BREAK, false); |
555 EXPECT_EQ(5U, render_text->GetCursorPosition()); | 672 EXPECT_EQ(5U, render_text->GetCursorPosition()); |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
746 EXPECT_EQ(5U, render_text->GetCursorPosition()); | 863 EXPECT_EQ(5U, render_text->GetCursorPosition()); |
747 render_text->MoveCursorRight(WORD_BREAK, false); | 864 render_text->MoveCursorRight(WORD_BREAK, false); |
748 EXPECT_EQ(6U, render_text->GetCursorPosition()); | 865 EXPECT_EQ(6U, render_text->GetCursorPosition()); |
749 render_text->MoveCursorRight(WORD_BREAK, false); | 866 render_text->MoveCursorRight(WORD_BREAK, false); |
750 EXPECT_EQ(6U, render_text->GetCursorPosition()); | 867 EXPECT_EQ(6U, render_text->GetCursorPosition()); |
751 } | 868 } |
752 | 869 |
753 #endif | 870 #endif |
754 | 871 |
755 } // namespace gfx | 872 } // namespace gfx |
OLD | NEW |