diff options
author | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-12 00:14:21 +0000 |
---|---|---|
committer | asvitkine@chromium.org <asvitkine@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-12 00:14:21 +0000 |
commit | 588de5bdf333b29748abc98412e64017b252632d (patch) | |
tree | d4586ac993d0c7fd4eaa24dd2373c6d45b8883e8 /ui | |
parent | 0078ab5fed5004af0c4c5be7091c64a923f4a4f9 (diff) | |
download | chromium_src-588de5bdf333b29748abc98412e64017b252632d.zip chromium_src-588de5bdf333b29748abc98412e64017b252632d.tar.gz chromium_src-588de5bdf333b29748abc98412e64017b252632d.tar.bz2 |
Add a RenderText test that checks that matching parentheses use the same font.
This is a test for http://crbug.com/125792, which I had fixed earlier.
BUG=125792, 105550
TEST=This is one. Checked that this test fails without the fix for
http://crbug.com/125792.
Review URL: https://chromiumcodereview.appspot.com/10388092
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@136720 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/gfx/render_text.h | 8 | ||||
-rw-r--r-- | ui/gfx/render_text_linux.cc | 19 | ||||
-rw-r--r-- | ui/gfx/render_text_linux.h | 1 | ||||
-rw-r--r-- | ui/gfx/render_text_unittest.cc | 86 | ||||
-rw-r--r-- | ui/gfx/render_text_win.cc | 10 | ||||
-rw-r--r-- | ui/gfx/render_text_win.h | 1 |
6 files changed, 125 insertions, 0 deletions
diff --git a/ui/gfx/render_text.h b/ui/gfx/render_text.h index 4c16fe5..69860df 100644 --- a/ui/gfx/render_text.h +++ b/ui/gfx/render_text.h @@ -9,6 +9,7 @@ #include <algorithm> #include <cstring> #include <string> +#include <utility> #include <vector> #include "base/gtest_prod_util.h" @@ -261,6 +262,13 @@ class UI_EXPORT RenderText { // Sets shadows to drawn with text. void SetTextShadows(const std::vector<ShadowValue>& shadows); + typedef std::pair<Font, ui::Range> FontSpan; + // For testing purposes, returns which fonts were chosen for which parts of + // the text by returning a vector of Font and Range pairs, where each range + // specifies the character range for which the corresponding font has been + // chosen. + virtual std::vector<FontSpan> GetFontSpansForTesting() = 0; + protected: RenderText(); diff --git a/ui/gfx/render_text_linux.cc b/ui/gfx/render_text_linux.cc index 69fb4bf..d24f8be 100644 --- a/ui/gfx/render_text_linux.cc +++ b/ui/gfx/render_text_linux.cc @@ -162,6 +162,25 @@ size_t RenderTextLinux::IndexOfAdjacentGrapheme( return ui::UTF16OffsetToIndex(text(), 0, char_offset); } +std::vector<RenderText::FontSpan> RenderTextLinux::GetFontSpansForTesting() { + EnsureLayout(); + + std::vector<RenderText::FontSpan> spans; + for (GSList* it = current_line_->runs; it; it = it->next) { + PangoItem* item = reinterpret_cast<PangoLayoutRun*>(it->data)->item; + const int start = LayoutIndexToTextIndex(item->offset); + const int end = LayoutIndexToTextIndex(item->offset + item->length); + const ui::Range range(start, end); + + PangoFontDescription* native_font = + pango_font_describe(item->analysis.font); + spans.push_back(RenderText::FontSpan(Font(native_font), range)); + pango_font_description_free(native_font); + } + + return spans; +} + SelectionModel RenderTextLinux::AdjacentCharSelectionModel( const SelectionModel& selection, VisualCursorDirection direction) { diff --git a/ui/gfx/render_text_linux.h b/ui/gfx/render_text_linux.h index cf82fa4..cd3bf9a 100644 --- a/ui/gfx/render_text_linux.h +++ b/ui/gfx/render_text_linux.h @@ -26,6 +26,7 @@ class RenderTextLinux : public RenderText { virtual size_t IndexOfAdjacentGrapheme( size_t index, LogicalCursorDirection direction) OVERRIDE; + virtual std::vector<FontSpan> GetFontSpansForTesting() OVERRIDE; protected: // Overridden from RenderText: diff --git a/ui/gfx/render_text_unittest.cc b/ui/gfx/render_text_unittest.cc index 607869c..f0802410 100644 --- a/ui/gfx/render_text_unittest.cc +++ b/ui/gfx/render_text_unittest.cc @@ -15,6 +15,17 @@ namespace gfx { +namespace { + +// Checks whether |range| contains |index|. This is not the same as calling +// |range.Contains(ui::Range(index))| - as that would return true when +// |index| == |range.end()|. +bool IndexInRange(const ui::Range& range, size_t index) { + return index >= range.start() && index < range.end(); +} + +} // namespace + class RenderTextTest : public testing::Test { }; @@ -1026,6 +1037,81 @@ TEST_F(RenderTextTest, OriginForDrawing) { EXPECT_EQ(origin.y(), 1); } +TEST_F(RenderTextTest, SameFontForParentheses) { + struct { + const char16 left_char; + const char16 right_char; + } punctuation_pairs[] = { + { '(', ')' }, + { '{', '}' }, + { '<', '>' }, + }; + struct { + string16 text; + } cases[] = { + // English(English) + { WideToUTF16(L"Hello World(a)") }, + // English(English)English + { WideToUTF16(L"Hello World(a)Hello World") }, + + // Japanese(English) + { WideToUTF16(L"\x6328\x62f6(a)") }, + // Japanese(English)Japanese + { WideToUTF16(L"\x6328\x62f6(a)\x6328\x62f6") }, + // English(Japanese)English + { WideToUTF16(L"Hello World(\x6328\x62f6)Hello World") }, + + // Hindi(English) + { WideToUTF16(L"\x0915\x093f(a)") }, + // Hindi(English)Hindi + { WideToUTF16(L"\x0915\x093f(a)\x0915\x093f") }, + // English(Hindi)English + { WideToUTF16(L"Hello World(\x0915\x093f)Hello World") }, + + // Hebrew(English) + { WideToUTF16(L"\x05e0\x05b8(a)") }, + // Hebrew(English)Hebrew + { WideToUTF16(L"\x05e0\x05b8(a)\x05e0\x05b8") }, + // English(Hebrew)English + { WideToUTF16(L"Hello World(\x05e0\x05b8)Hello World") }, + }; + + scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + string16 text = cases[i].text; + const size_t start_paren_char_index = text.find('('); + ASSERT_NE(string16::npos, start_paren_char_index); + const size_t end_paren_char_index = text.find(')'); + ASSERT_NE(string16::npos, end_paren_char_index); + + for (size_t j = 0; j < ARRAYSIZE_UNSAFE(punctuation_pairs); ++j) { + text[start_paren_char_index] = punctuation_pairs[j].left_char; + text[end_paren_char_index] = punctuation_pairs[j].right_char; + render_text->SetText(text); + + const std::vector<RenderText::FontSpan> spans = + render_text->GetFontSpansForTesting(); + + int start_paren_span_index = -1; + int end_paren_span_index = -1; + for (size_t k = 0; k < spans.size(); ++k) { + if (IndexInRange(spans[k].second, start_paren_char_index)) + start_paren_span_index = k; + if (IndexInRange(spans[k].second, end_paren_char_index)) + end_paren_span_index = k; + } + ASSERT_NE(-1, start_paren_span_index); + ASSERT_NE(-1, end_paren_span_index); + + const Font& start_font = spans[start_paren_span_index].first; + const Font& end_font = spans[end_paren_span_index].first; + EXPECT_EQ(start_font.GetFontName(), end_font.GetFontName()); + EXPECT_EQ(start_font.GetFontSize(), end_font.GetFontSize()); + EXPECT_EQ(start_font.GetStyle(), end_font.GetStyle()); + } + } +} + TEST_F(RenderTextTest, DisplayRectShowsCursorLTR) { scoped_ptr<RenderText> render_text(RenderText::CreateRenderText()); render_text->SetText(WideToUTF16(L"abcdefghijklmnopqrstuvwxzyabcdefg")); diff --git a/ui/gfx/render_text_win.cc b/ui/gfx/render_text_win.cc index cfc0b3b..b5cfec3 100644 --- a/ui/gfx/render_text_win.cc +++ b/ui/gfx/render_text_win.cc @@ -302,6 +302,16 @@ size_t RenderTextWin::IndexOfAdjacentGrapheme( return start + ch; } +std::vector<RenderText::FontSpan> RenderTextWin::GetFontSpansForTesting() { + EnsureLayout(); + + std::vector<RenderText::FontSpan> spans; + for (size_t i = 0; i < runs_.size(); ++i) + spans.push_back(RenderText::FontSpan(runs_[i]->font, runs_[i]->range)); + + return spans; +} + SelectionModel RenderTextWin::AdjacentCharSelectionModel( const SelectionModel& selection, VisualCursorDirection direction) { diff --git a/ui/gfx/render_text_win.h b/ui/gfx/render_text_win.h index 64d24c7..9a5ff65 100644 --- a/ui/gfx/render_text_win.h +++ b/ui/gfx/render_text_win.h @@ -72,6 +72,7 @@ class RenderTextWin : public RenderText { virtual size_t IndexOfAdjacentGrapheme( size_t index, LogicalCursorDirection direction) OVERRIDE; + virtual std::vector<FontSpan> GetFontSpansForTesting() OVERRIDE; protected: // Overridden from RenderText: |