Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: ui/gfx/render_text_unittest.cc

Issue 2541313002: RenderTextHarfBuzz: Add support for multi line text selection. (Closed)
Patch Set: -- Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <limits.h> 7 #include <limits.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 return test::RenderTextTestApi::GetRendererPaint(renderer()); 446 return test::RenderTextTestApi::GetRendererPaint(renderer());
447 } 447 }
448 448
449 void DrawVisualText() { test_api_->DrawVisualText(renderer()); } 449 void DrawVisualText() { test_api_->DrawVisualText(renderer()); }
450 450
451 internal::TextRunList* GetHarfBuzzRunList() { 451 internal::TextRunList* GetHarfBuzzRunList() {
452 DCHECK_EQ(RENDER_TEXT_HARFBUZZ, GetParam()); 452 DCHECK_EQ(RENDER_TEXT_HARFBUZZ, GetParam());
453 return test_api_->GetHarfBuzzRunList(); 453 return test_api_->GetHarfBuzzRunList();
454 } 454 }
455 455
456 int GetCursorYForTesting(int line_num = 0) {
457 const internal::Line& line = test_api_->lines()[line_num];
458 return render_text_->GetLineOffset(line_num).y() + line.size.height() / 2;
459 }
460
456 void ResetRenderTextInstance() { 461 void ResetRenderTextInstance() {
457 render_text_ = CreateRenderTextInstance(); 462 render_text_ = CreateRenderTextInstance();
458 test_api_.reset(new test::RenderTextTestApi(GetRenderText())); 463 test_api_.reset(new test::RenderTextTestApi(GetRenderText()));
459 } 464 }
460 465
461 RenderText* GetRenderText() { return render_text_.get(); }; 466 RenderText* GetRenderText() { return render_text_.get(); };
462 467
463 RenderTextHarfBuzz* GetRenderTextHarfBuzz() { 468 RenderTextHarfBuzz* GetRenderTextHarfBuzz() {
464 DCHECK_EQ(RENDER_TEXT_HARFBUZZ, GetParam()); 469 DCHECK_EQ(RENDER_TEXT_HARFBUZZ, GetParam());
465 return static_cast<RenderTextHarfBuzz*>(GetRenderText()); 470 return static_cast<RenderTextHarfBuzz*>(GetRenderText());
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 EXPECT_EQ(1U, test_api()->TextIndexToDisplayIndex(1U)); 786 EXPECT_EQ(1U, test_api()->TextIndexToDisplayIndex(1U));
782 EXPECT_EQ(1U, test_api()->TextIndexToDisplayIndex(2U)); 787 EXPECT_EQ(1U, test_api()->TextIndexToDisplayIndex(2U));
783 EXPECT_EQ(0U, test_api()->DisplayIndexToTextIndex(0U)); 788 EXPECT_EQ(0U, test_api()->DisplayIndexToTextIndex(0U));
784 EXPECT_EQ(2U, test_api()->DisplayIndexToTextIndex(1U)); 789 EXPECT_EQ(2U, test_api()->DisplayIndexToTextIndex(1U));
785 EXPECT_TRUE(render_text->IsValidCursorIndex(0U)); 790 EXPECT_TRUE(render_text->IsValidCursorIndex(0U));
786 EXPECT_FALSE(render_text->IsValidCursorIndex(1U)); 791 EXPECT_FALSE(render_text->IsValidCursorIndex(1U));
787 EXPECT_TRUE(render_text->IsValidCursorIndex(2U)); 792 EXPECT_TRUE(render_text->IsValidCursorIndex(2U));
788 793
789 // FindCursorPosition() should not return positions between a surrogate pair. 794 // FindCursorPosition() should not return positions between a surrogate pair.
790 render_text->SetDisplayRect(Rect(0, 0, 20, 20)); 795 render_text->SetDisplayRect(Rect(0, 0, 20, 20));
791 EXPECT_EQ(render_text->FindCursorPosition(Point(0, 0)).caret_pos(), 0U); 796 const int cursor_y = GetCursorYForTesting();
792 EXPECT_EQ(render_text->FindCursorPosition(Point(20, 0)).caret_pos(), 2U); 797 EXPECT_EQ(render_text->FindCursorPosition(Point(0, cursor_y)).caret_pos(),
798 0U);
799 EXPECT_EQ(render_text->FindCursorPosition(Point(20, cursor_y)).caret_pos(),
800 2U);
793 for (int x = -1; x <= 20; ++x) { 801 for (int x = -1; x <= 20; ++x) {
794 SelectionModel selection = render_text->FindCursorPosition(Point(x, 0)); 802 SelectionModel selection =
803 render_text->FindCursorPosition(Point(x, cursor_y));
795 EXPECT_TRUE(selection.caret_pos() == 0U || selection.caret_pos() == 2U); 804 EXPECT_TRUE(selection.caret_pos() == 0U || selection.caret_pos() == 2U);
796 } 805 }
797 806
798 // GetGlyphBounds() should yield the entire string bounds for text index 0. 807 // GetGlyphBounds() should yield the entire string bounds for text index 0.
799 EXPECT_EQ(render_text->GetStringSize().width(), 808 EXPECT_EQ(render_text->GetStringSize().width(),
800 static_cast<int>(render_text->GetGlyphBounds(0U).length())); 809 static_cast<int>(render_text->GetGlyphBounds(0U).length()));
801 810
802 // Cursoring is independent of underlying characters when text is obscured. 811 // Cursoring is independent of underlying characters when text is obscured.
803 const wchar_t* const texts[] = { 812 const wchar_t* const texts[] = {
804 kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl, 813 kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, kRtlLtr, kRtlLtrRtl,
(...skipping 971 matching lines...) Expand 10 before | Expand all | Expand 10 after
1776 const wchar_t* kTestStrings[] = { kLtrRtl, kLtrRtlLtr, kRtlLtr, kRtlLtrRtl }; 1785 const wchar_t* kTestStrings[] = { kLtrRtl, kLtrRtlLtr, kRtlLtr, kRtlLtrRtl };
1777 RenderText* render_text = GetRenderText(); 1786 RenderText* render_text = GetRenderText();
1778 render_text->SetDisplayRect(Rect(0, 0, 100, 20)); 1787 render_text->SetDisplayRect(Rect(0, 0, 100, 20));
1779 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { 1788 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1780 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS "]", i)); 1789 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS "]", i));
1781 render_text->SetText(WideToUTF16(kTestStrings[i])); 1790 render_text->SetText(WideToUTF16(kTestStrings[i]));
1782 for (size_t j = 0; j < render_text->text().length(); ++j) { 1791 for (size_t j = 0; j < render_text->text().length(); ++j) {
1783 const Range range(render_text->GetGlyphBounds(j)); 1792 const Range range(render_text->GetGlyphBounds(j));
1784 // Test a point just inside the leading edge of the glyph bounds. 1793 // Test a point just inside the leading edge of the glyph bounds.
1785 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1; 1794 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1;
1786 EXPECT_EQ(j, render_text->FindCursorPosition(Point(x, 0)).caret_pos()); 1795 EXPECT_EQ(
1796 j, render_text->FindCursorPosition(Point(x, GetCursorYForTesting()))
1797 .caret_pos());
1787 } 1798 }
1788 } 1799 }
1789 } 1800 }
1790 1801
1791 TEST_P(RenderTextTest, EdgeSelectionModels) { 1802 TEST_P(RenderTextTest, EdgeSelectionModels) {
1792 // Simple Latin text. 1803 // Simple Latin text.
1793 const base::string16 kLatin = WideToUTF16(L"abc"); 1804 const base::string16 kLatin = WideToUTF16(L"abc");
1794 // LTR 2-character grapheme. 1805 // LTR 2-character grapheme.
1795 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f"); 1806 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f");
1796 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme. 1807 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
(...skipping 1059 matching lines...) Expand 10 before | Expand all | Expand 10 after
2856 } 2867 }
2857 } 2868 }
2858 2869
2859 TEST_P(RenderTextHarfBuzzTest, Multiline_Newline) { 2870 TEST_P(RenderTextHarfBuzzTest, Multiline_Newline) {
2860 const struct { 2871 const struct {
2861 const wchar_t* const text; 2872 const wchar_t* const text;
2862 const size_t lines_count; 2873 const size_t lines_count;
2863 // Ranges of the characters on each line preceding the newline. 2874 // Ranges of the characters on each line preceding the newline.
2864 const Range line_char_ranges[3]; 2875 const Range line_char_ranges[3];
2865 } kTestStrings[] = { 2876 } kTestStrings[] = {
2866 {L"abc\ndef", 2ul, { Range(0, 3), Range(4, 7), Range::InvalidRange() } }, 2877 {L"abc\ndef", 2ul, {Range(0, 3), Range(4, 7), Range::InvalidRange()}},
2867 {L"a \n b ", 2ul, { Range(0, 2), Range(3, 6), Range::InvalidRange() } }, 2878 {L"a \n b ", 2ul, {Range(0, 2), Range(3, 6), Range::InvalidRange()}},
2868 {L"ab\n", 2ul, { Range(0, 2), Range(), Range::InvalidRange() } }, 2879 {L"ab\n", 2ul, {Range(0, 2), Range(), Range::InvalidRange()}},
2869 {L"a\n\nb", 3ul, { Range(0, 1), Range(), Range(3, 4) } }, 2880 {L"a\n\nb", 3ul, {Range(0, 1), Range(2, 3), Range(3, 4)}},
2870 {L"\nab", 2ul, { Range(), Range(1, 3), Range::InvalidRange() } }, 2881 {L"\nab", 2ul, {Range(0, 1), Range(1, 3), Range::InvalidRange()}},
2871 {L"\n", 2ul, { Range(), Range(), Range::InvalidRange() } }, 2882 {L"\n", 2ul, {Range(0, 1), Range(), Range::InvalidRange()}},
2872 }; 2883 };
2873 2884
2874 RenderText* render_text = GetRenderText(); 2885 RenderText* render_text = GetRenderText();
2875 render_text->SetDisplayRect(Rect(200, 1000)); 2886 render_text->SetDisplayRect(Rect(200, 1000));
2876 render_text->SetMultiline(true); 2887 render_text->SetMultiline(true);
2877 2888
2878 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { 2889 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
2879 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i)); 2890 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
2880 render_text->SetText(WideToUTF16(kTestStrings[i].text)); 2891 render_text->SetText(WideToUTF16(kTestStrings[i].text));
2881 render_text->Draw(canvas()); 2892 render_text->Draw(canvas());
(...skipping 1009 matching lines...) Expand 10 before | Expand all | Expand 10 after
3891 const int kWordTwoStartIndex = 6; 3902 const int kWordTwoStartIndex = 6;
3892 3903
3893 RenderText* render_text = GetRenderText(); 3904 RenderText* render_text = GetRenderText();
3894 render_text->SetDisplayRect(Rect(100, 30)); 3905 render_text->SetDisplayRect(Rect(100, 30));
3895 render_text->SetText(ltr); 3906 render_text->SetText(ltr);
3896 render_text->ApplyWeight(Font::Weight::SEMIBOLD, Range(0, 3)); 3907 render_text->ApplyWeight(Font::Weight::SEMIBOLD, Range(0, 3));
3897 render_text->ApplyStyle(UNDERLINE, true, Range(1, 5)); 3908 render_text->ApplyStyle(UNDERLINE, true, Range(1, 5));
3898 render_text->ApplyStyle(ITALIC, true, Range(3, 8)); 3909 render_text->ApplyStyle(ITALIC, true, Range(3, 8));
3899 render_text->ApplyStyle(DIAGONAL_STRIKE, true, Range(5, 7)); 3910 render_text->ApplyStyle(DIAGONAL_STRIKE, true, Range(5, 7));
3900 render_text->ApplyStyle(STRIKE, true, Range(1, 7)); 3911 render_text->ApplyStyle(STRIKE, true, Range(1, 7));
3912 const int cursor_y = GetCursorYForTesting();
3901 3913
3902 const std::vector<RenderText::FontSpan> font_spans = 3914 const std::vector<RenderText::FontSpan> font_spans =
3903 render_text->GetFontSpansForTesting(); 3915 render_text->GetFontSpansForTesting();
3904 3916
3905 // Create expected decorated text instances. 3917 // Create expected decorated text instances.
3906 DecoratedText expected_word_1; 3918 DecoratedText expected_word_1;
3907 expected_word_1.text = ASCIIToUTF16("ab"); 3919 expected_word_1.text = ASCIIToUTF16("ab");
3908 // Attributes for the characters 'a' and 'b' at logical indices 2 and 3 3920 // Attributes for the characters 'a' and 'b' at logical indices 2 and 3
3909 // respectively. 3921 // respectively.
3910 expected_word_1.attributes.push_back(CreateRangedAttribute( 3922 expected_word_1.attributes.push_back(CreateRangedAttribute(
(...skipping 13 matching lines...) Expand all
3924 ITALIC_MASK | DIAGONAL_STRIKE_MASK | STRIKE_MASK)); 3936 ITALIC_MASK | DIAGONAL_STRIKE_MASK | STRIKE_MASK));
3925 const Rect left_glyph_word_2 = render_text->GetCursorBounds( 3937 const Rect left_glyph_word_2 = render_text->GetCursorBounds(
3926 SelectionModel(kWordTwoStartIndex, CURSOR_FORWARD), false); 3938 SelectionModel(kWordTwoStartIndex, CURSOR_FORWARD), false);
3927 3939
3928 DecoratedText decorated_word; 3940 DecoratedText decorated_word;
3929 Point baseline_point; 3941 Point baseline_point;
3930 3942
3931 { 3943 {
3932 SCOPED_TRACE(base::StringPrintf("Query to the left of text bounds")); 3944 SCOPED_TRACE(base::StringPrintf("Query to the left of text bounds"));
3933 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint( 3945 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
3934 Point(-5, 5), &decorated_word, &baseline_point)); 3946 Point(-5, cursor_y), &decorated_word, &baseline_point));
3935 VerifyDecoratedWordsAreEqual(expected_word_1, decorated_word); 3947 VerifyDecoratedWordsAreEqual(expected_word_1, decorated_word);
3936 EXPECT_TRUE(left_glyph_word_1.Contains(baseline_point)); 3948 EXPECT_TRUE(left_glyph_word_1.Contains(baseline_point));
3937 } 3949 }
3938 { 3950 {
3939 SCOPED_TRACE(base::StringPrintf("Query to the right of text bounds")); 3951 SCOPED_TRACE(base::StringPrintf("Query to the right of text bounds"));
3940 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint( 3952 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
3941 Point(105, 5), &decorated_word, &baseline_point)); 3953 Point(105, cursor_y), &decorated_word, &baseline_point));
3942 VerifyDecoratedWordsAreEqual(expected_word_2, decorated_word); 3954 VerifyDecoratedWordsAreEqual(expected_word_2, decorated_word);
3943 EXPECT_TRUE(left_glyph_word_2.Contains(baseline_point)); 3955 EXPECT_TRUE(left_glyph_word_2.Contains(baseline_point));
3944 } 3956 }
3945 3957
3946 for (size_t i = 0; i < render_text->text().length(); i++) { 3958 for (size_t i = 0; i < render_text->text().length(); i++) {
3947 SCOPED_TRACE(base::StringPrintf("Case[%" PRIuS "]", i)); 3959 SCOPED_TRACE(base::StringPrintf("Case[%" PRIuS "]", i));
3948 // Query the decorated word using the origin of the i'th glyph's bounds. 3960 // Query the decorated word using the origin of the i'th glyph's bounds.
3949 const Point query = 3961 const Point query =
3950 render_text->GetCursorBounds(SelectionModel(i, CURSOR_FORWARD), false) 3962 render_text->GetCursorBounds(SelectionModel(i, CURSOR_FORWARD), false)
3951 .origin(); 3963 .origin();
(...skipping 23 matching lines...) Expand all
3975 const int kWordTwoStartIndex = 5; 3987 const int kWordTwoStartIndex = 5;
3976 3988
3977 RenderText* render_text = GetRenderText(); 3989 RenderText* render_text = GetRenderText();
3978 render_text->SetDisplayRect(Rect(100, 30)); 3990 render_text->SetDisplayRect(Rect(100, 30));
3979 render_text->SetText(rtl); 3991 render_text->SetText(rtl);
3980 render_text->ApplyWeight(Font::Weight::SEMIBOLD, Range(2, 3)); 3992 render_text->ApplyWeight(Font::Weight::SEMIBOLD, Range(2, 3));
3981 render_text->ApplyStyle(UNDERLINE, true, Range(3, 6)); 3993 render_text->ApplyStyle(UNDERLINE, true, Range(3, 6));
3982 render_text->ApplyStyle(ITALIC, true, Range(0, 3)); 3994 render_text->ApplyStyle(ITALIC, true, Range(0, 3));
3983 render_text->ApplyStyle(DIAGONAL_STRIKE, true, Range(0, 2)); 3995 render_text->ApplyStyle(DIAGONAL_STRIKE, true, Range(0, 2));
3984 render_text->ApplyStyle(STRIKE, true, Range(2, 5)); 3996 render_text->ApplyStyle(STRIKE, true, Range(2, 5));
3997 const int cursor_y = GetCursorYForTesting();
3985 3998
3986 const std::vector<RenderText::FontSpan> font_spans = 3999 const std::vector<RenderText::FontSpan> font_spans =
3987 render_text->GetFontSpansForTesting(); 4000 render_text->GetFontSpansForTesting();
3988 4001
3989 // Create expected decorated text instance. 4002 // Create expected decorated text instance.
3990 DecoratedText expected_word_1; 4003 DecoratedText expected_word_1;
3991 expected_word_1.text = WideToUTF16(L"\x0634\x0632"); 4004 expected_word_1.text = WideToUTF16(L"\x0634\x0632");
3992 // Attributes for characters at logical indices 1 and 2. 4005 // Attributes for characters at logical indices 1 and 2.
3993 expected_word_1.attributes.push_back(CreateRangedAttribute( 4006 expected_word_1.attributes.push_back(CreateRangedAttribute(
3994 font_spans, 0, kWordOneStartIndex, Font::Weight::NORMAL, 4007 font_spans, 0, kWordOneStartIndex, Font::Weight::NORMAL,
(...skipping 12 matching lines...) Expand all
4007 font_spans, 0, kWordTwoStartIndex, Font::Weight::NORMAL, UNDERLINE_MASK)); 4020 font_spans, 0, kWordTwoStartIndex, Font::Weight::NORMAL, UNDERLINE_MASK));
4008 const Rect left_glyph_word_2 = render_text->GetCursorBounds( 4021 const Rect left_glyph_word_2 = render_text->GetCursorBounds(
4009 SelectionModel(kWordTwoStartIndex, CURSOR_FORWARD), false); 4022 SelectionModel(kWordTwoStartIndex, CURSOR_FORWARD), false);
4010 4023
4011 DecoratedText decorated_word; 4024 DecoratedText decorated_word;
4012 Point baseline_point; 4025 Point baseline_point;
4013 4026
4014 { 4027 {
4015 SCOPED_TRACE(base::StringPrintf("Query to the left of text bounds")); 4028 SCOPED_TRACE(base::StringPrintf("Query to the left of text bounds"));
4016 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint( 4029 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
4017 Point(-5, 5), &decorated_word, &baseline_point)); 4030 Point(-5, cursor_y), &decorated_word, &baseline_point));
4018 VerifyDecoratedWordsAreEqual(expected_word_2, decorated_word); 4031 VerifyDecoratedWordsAreEqual(expected_word_2, decorated_word);
4019 EXPECT_TRUE(left_glyph_word_2.Contains(baseline_point)); 4032 EXPECT_TRUE(left_glyph_word_2.Contains(baseline_point));
4020 } 4033 }
4021 { 4034 {
4022 SCOPED_TRACE(base::StringPrintf("Query to the right of text bounds")); 4035 SCOPED_TRACE(base::StringPrintf("Query to the right of text bounds"));
4023 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint( 4036 EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
4024 Point(105, 5), &decorated_word, &baseline_point)); 4037 Point(105, cursor_y), &decorated_word, &baseline_point));
4025 VerifyDecoratedWordsAreEqual(expected_word_1, decorated_word); 4038 VerifyDecoratedWordsAreEqual(expected_word_1, decorated_word);
4026 EXPECT_TRUE(left_glyph_word_1.Contains(baseline_point)); 4039 EXPECT_TRUE(left_glyph_word_1.Contains(baseline_point));
4027 } 4040 }
4028 4041
4029 for (size_t i = 0; i < render_text->text().length(); i++) { 4042 for (size_t i = 0; i < render_text->text().length(); i++) {
4030 SCOPED_TRACE(base::StringPrintf("Case[%" PRIuS "]", i)); 4043 SCOPED_TRACE(base::StringPrintf("Case[%" PRIuS "]", i));
4031 4044
4032 // Query the decorated word using the top right point of the i'th glyph's 4045 // Query the decorated word using the top right point of the i'th glyph's
4033 // bounds. 4046 // bounds.
4034 const Point query = 4047 const Point query =
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
4094 ::testing::Values(RENDER_TEXT_HARFBUZZ), 4107 ::testing::Values(RENDER_TEXT_HARFBUZZ),
4095 PrintRenderTextBackend()); 4108 PrintRenderTextBackend());
4096 #endif 4109 #endif
4097 4110
4098 INSTANTIATE_TEST_CASE_P(, 4111 INSTANTIATE_TEST_CASE_P(,
4099 RenderTextHarfBuzzTest, 4112 RenderTextHarfBuzzTest,
4100 ::testing::Values(RENDER_TEXT_HARFBUZZ), 4113 ::testing::Values(RENDER_TEXT_HARFBUZZ),
4101 PrintRenderTextBackend()); 4114 PrintRenderTextBackend());
4102 4115
4103 } // namespace gfx 4116 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698