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

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

Issue 276123003: Add a unit tests for RenderText::FindCursorPosition. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refine the test; cleanup debugging code. Created 6 years, 7 months 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 | Annotate | Revision Log
« no previous file with comments | « ui/gfx/render_text.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <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
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 render_text->EnsureLayout();
Alexei Svitkine (slow) 2014/05/12 15:23:27 GetGlyphBounds() and FindCursorPosition() are both
msw 2014/05/12 17:15:56 Done.
957 for(size_t j = 0; j < render_text->text().length(); ++j) {
958 const Range range(render_text->GetGlyphBounds(j));
959 // Test a point just inside the leading edge of the glyph bounds.
960 int x = range.is_reversed() ? range.GetMax() - 1 : range.GetMin() + 1;
961 EXPECT_EQ(j, render_text->FindCursorPosition(Point(x, 0)).caret_pos());
962 }
963 }
964 }
965
949 TEST_F(RenderTextTest, EdgeSelectionModels) { 966 TEST_F(RenderTextTest, EdgeSelectionModels) {
950 // Simple Latin text. 967 // Simple Latin text.
951 const base::string16 kLatin = WideToUTF16(L"abc"); 968 const base::string16 kLatin = WideToUTF16(L"abc");
952 // LTR 2-character grapheme. 969 // LTR 2-character grapheme.
953 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f"); 970 const base::string16 kLTRGrapheme = WideToUTF16(L"\x0915\x093f");
954 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme. 971 // LTR 2-character grapheme, LTR a, LTR 2-character grapheme.
955 const base::string16 kHindiLatin = 972 const base::string16 kHindiLatin =
956 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f"); 973 WideToUTF16(L"\x0915\x093f" L"a" L"\x0915\x093f");
957 // RTL 2-character grapheme. 974 // RTL 2-character grapheme.
958 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8"); 975 const base::string16 kRTLGrapheme = WideToUTF16(L"\x05e0\x05b8");
(...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after
1744 render_text->GetUpdatedCursorBounds().x()); 1761 render_text->GetUpdatedCursorBounds().x());
1745 1762
1746 // Reset the application default text direction to LTR. 1763 // Reset the application default text direction to LTR.
1747 SetRTL(was_rtl); 1764 SetRTL(was_rtl);
1748 EXPECT_EQ(was_rtl, base::i18n::IsRTL()); 1765 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
1749 } 1766 }
1750 #endif // !defined(OS_MACOSX) 1767 #endif // !defined(OS_MACOSX)
1751 1768
1752 // Changing colors between or inside ligated glyphs should not break shaping. 1769 // Changing colors between or inside ligated glyphs should not break shaping.
1753 TEST_F(RenderTextTest, SelectionKeepsLigatures) { 1770 TEST_F(RenderTextTest, SelectionKeepsLigatures) {
1754 const wchar_t* kTestStrings[] = { 1771 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()); 1772 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1760 render_text->set_selection_color(SK_ColorRED); 1773 render_text->set_selection_color(SK_ColorRED);
1761 Canvas canvas; 1774 Canvas canvas;
1762 1775
1763 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { 1776 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1764 render_text->SetText(WideToUTF16(kTestStrings[i])); 1777 render_text->SetText(WideToUTF16(kTestStrings[i]));
1765 const int expected_width = render_text->GetStringSize().width(); 1778 const int expected_width = render_text->GetStringSize().width();
1766 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD)); 1779 render_text->MoveCursorTo(SelectionModel(Range(0, 1), CURSOR_FORWARD));
1767 EXPECT_EQ(expected_width, render_text->GetStringSize().width()); 1780 EXPECT_EQ(expected_width, render_text->GetStringSize().width());
1768 // Draw the text. It shouldn't hit any DCHECKs or crash. 1781 // Drawing the text should not DCHECK or crash; see http://crbug.com/262119
1769 // See http://crbug.com/214150
1770 render_text->Draw(&canvas); 1782 render_text->Draw(&canvas);
1771 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD)); 1783 render_text->MoveCursorTo(SelectionModel(0, CURSOR_FORWARD));
1772 } 1784 }
1773 } 1785 }
1774 1786
1775 #if defined(OS_WIN) 1787 #if defined(OS_WIN)
1776 // Ensure strings wrap onto multiple lines for a small available width. 1788 // Ensure strings wrap onto multiple lines for a small available width.
1777 TEST_F(RenderTextTest, Multiline_MinWidth) { 1789 TEST_F(RenderTextTest, Multiline_MinWidth) {
1778 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl, 1790 const wchar_t* kTestStrings[] = { kWeak, kLtr, kLtrRtl, kLtrRtlLtr, kRtl,
1779 kRtlLtr, kRtlLtrRtl }; 1791 kRtlLtr, kRtlLtrRtl };
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1907 render_text->SetText(WideToUTF16(L"x \x25B6 y")); 1919 render_text->SetText(WideToUTF16(L"x \x25B6 y"));
1908 render_text->EnsureLayout(); 1920 render_text->EnsureLayout();
1909 ASSERT_EQ(3U, render_text->runs_.size()); 1921 ASSERT_EQ(3U, render_text->runs_.size());
1910 EXPECT_EQ(Range(0, 2), render_text->runs_[0]->range); 1922 EXPECT_EQ(Range(0, 2), render_text->runs_[0]->range);
1911 EXPECT_EQ(Range(2, 3), render_text->runs_[1]->range); 1923 EXPECT_EQ(Range(2, 3), render_text->runs_[1]->range);
1912 EXPECT_EQ(Range(3, 5), render_text->runs_[2]->range); 1924 EXPECT_EQ(Range(3, 5), render_text->runs_[2]->range);
1913 } 1925 }
1914 #endif // defined(OS_WIN) 1926 #endif // defined(OS_WIN)
1915 1927
1916 } // namespace gfx 1928 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698