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 928 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
939 EXPECT_EQ(1U, render_text->cursor_position()); | 939 EXPECT_EQ(1U, render_text->cursor_position()); |
940 // Although selection bounds may be set within a multi-character grapheme, | 940 // Although selection bounds may be set within a multi-character grapheme, |
941 // cursor movement (e.g. via arrow key) should avoid those indices. | 941 // cursor movement (e.g. via arrow key) should avoid those indices. |
942 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); | 942 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_LEFT, false); |
943 EXPECT_EQ(0U, render_text->cursor_position()); | 943 EXPECT_EQ(0U, render_text->cursor_position()); |
944 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); | 944 render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, false); |
945 EXPECT_EQ(2U, render_text->cursor_position()); | 945 EXPECT_EQ(2U, render_text->cursor_position()); |
946 } | 946 } |
947 } | 947 } |
948 | 948 |
| 949 TEST_F(RenderTextTest, FindCursorPosition) { |
| 950 const wchar_t* kTestStrings[] = { kLtrRtl, kLtrRtlLtr, kRtlLtr, kRtlLtrRtl }; |
| 951 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 952 render_text->SetDisplayRect(Rect(0, 0, 100, 20)); |
| 953 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { |
| 954 SCOPED_TRACE(base::StringPrintf("Testing case[%" PRIuS "]", i)); |
| 955 render_text->SetText(WideToUTF16(kTestStrings[i])); |
| 956 for(size_t j = 0; j < render_text->text().length(); ++j) { |
| 957 const Range range(render_text->GetGlyphBounds(j)); |
| 958 // Test a point just inside the leading edge of the glyph bounds. |
| 959 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1; |
| 960 EXPECT_EQ(j, render_text->FindCursorPosition(Point(x, 0)).caret_pos()); |
| 961 } |
| 962 } |
| 963 } |
| 964 |
949 TEST_F(RenderTextTest, EdgeSelectionModels) { | 965 TEST_F(RenderTextTest, EdgeSelectionModels) { |
950 // Simple Latin text. | 966 // Simple Latin text. |
951 const base::string16 kLatin = WideToUTF16(L"abc"); | 967 const base::string16 kLatin = WideToUTF16(L"abc"); |
952 // LTR 2-character grapheme. | 968 // LTR 2-character grapheme. |
953 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f"); | 969 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f"); |
954 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme. | 970 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme. |
955 const base::string16 kHindiLatin = | 971 const base::string16 kHindiLatin = |
956 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f"); | 972 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f"); |
957 // RTL 2-character grapheme. | 973 // RTL 2-character grapheme. |
958 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8"); | 974 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8"); |
(...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1744 render_text->GetUpdatedCursorBounds().x()); | 1760 render_text->GetUpdatedCursorBounds().x()); |
1745 | 1761 |
1746 // Reset the application default text direction to LTR. | 1762 // Reset the application default text direction to LTR. |
1747 SetRTL(was_rtl); | 1763 SetRTL(was_rtl); |
1748 EXPECT_EQ(was_rtl, base::i18n::IsRTL()); | 1764 EXPECT_EQ(was_rtl, base::i18n::IsRTL()); |
1749 } | 1765 } |
1750 #endif // !defined(OS_MACOSX) | 1766 #endif // !defined(OS_MACOSX) |
1751 | 1767 |
1752 // Changing colors between or inside ligated glyphs should not break shaping. | 1768 // Changing colors between or inside ligated glyphs should not break shaping. |
1753 TEST_F(RenderTextTest, SelectionKeepsLigatures) { | 1769 TEST_F(RenderTextTest, SelectionKeepsLigatures) { |
1754 const wchar_t* kTestStrings[] = { | 1770 const wchar_t* kTestStrings[] = { L"\x644\x623", L"\x633\x627" }; |
1755 L"\x644\x623", | |
1756 L"\x633\x627" | |
1757 }; | |
1758 | |
1759 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 1771 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
1760 render_text->set_selection_color(SK_ColorRED); | 1772 render_text->set_selection_color(SK_ColorRED); |
1761 Canvas canvas; | 1773 Canvas canvas; |
1762 | 1774 |
1763 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { | 1775 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { |
1764 render_text->SetText(WideToUTF16(kTestStrings[i])); | 1776 render_text->SetText(WideToUTF16(kTestStrings[i])); |
1765 const int expected_width = render_text->GetStringSize().width(); | 1777 const int expected_width = render_text->GetStringSize().width(); |
1766 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD)); | 1778 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD)); |
1767 EXPECT_EQ(expected_width, render_text->GetStringSize().width()); | 1779 EXPECT_EQ(expected_width, render_text->GetStringSize().width()); |
1768 // Draw the text. It shouldn't hit any DCHECKs or crash. | 1780 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119 |
1769 // See http://crbug.com/214150 | |
1770 render_text->Draw(&canvas); | 1781 render_text->Draw(&canvas); |
1771 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD)); | 1782 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD)); |
1772 } | 1783 } |
1773 } | 1784 } |
1774 | 1785 |
1775 #if defined(OS_WIN) | 1786 #if defined(OS_WIN) |
1776 // Ensure strings wrap onto multiple lines for a small available width. | 1787 // Ensure strings wrap onto multiple lines for a small available width. |
1777 TEST_F(RenderTextTest, Multiline_MinWidth) { | 1788 TEST_F(RenderTextTest, Multiline_MinWidth) { |
1778 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, | 1789 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, |
1779 kRtlLtr, kRtlLtrRtl }; | 1790 kRtlLtr, kRtlLtrRtl }; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1907 render_text->SetText(WideToUTF16(L"x \x25B6 y")); | 1918 render_text->SetText(WideToUTF16(L"x \x25B6 y")); |
1908 render_text->EnsureLayout(); | 1919 render_text->EnsureLayout(); |
1909 ASSERT_EQ(3U, render_text->runs_.size()); | 1920 ASSERT_EQ(3U, render_text->runs_.size()); |
1910 EXPECT_EQ(Range(0, 2), render_text->runs_[0]->range); | 1921 EXPECT_EQ(Range(0, 2), render_text->runs_[0]->range); |
1911 EXPECT_EQ(Range(2, 3), render_text->runs_[1]->range); | 1922 EXPECT_EQ(Range(2, 3), render_text->runs_[1]->range); |
1912 EXPECT_EQ(Range(3, 5), render_text->runs_[2]->range); | 1923 EXPECT_EQ(Range(3, 5), render_text->runs_[2]->range); |
1913 } | 1924 } |
1914 #endif // defined(OS_WIN) | 1925 #endif // defined(OS_WIN) |
1915 | 1926 |
1916 } // namespace gfx | 1927 } // namespace gfx |
OLD | NEW |