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

Unified Diff: ui/gfx/render_text_unittest.cc

Issue 2639493002: MacViews: Enable word lookup for selectable views::Labels and multi-line text. (Closed)
Patch Set: Correct baseline point logic. Add test. Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: ui/gfx/render_text_unittest.cc
diff --git a/ui/gfx/render_text_unittest.cc b/ui/gfx/render_text_unittest.cc
index 3657ef0353f9d09393b13f488032fa535cefc30a..94c72318c94523f321b28c0c875e79f367bdc3f0 100644
--- a/ui/gfx/render_text_unittest.cc
+++ b/ui/gfx/render_text_unittest.cc
@@ -473,9 +473,9 @@ class RenderTextTest : public testing::Test,
}
#endif
- Rect GetSelectionBoundsUnion() {
+ Rect GetSubstringBoundsUnionForRange(const Range& range) {
const std::vector<Rect> bounds =
- render_text_->GetSubstringBoundsForTesting(render_text_->selection());
+ render_text_->GetSubstringBoundsForTesting(range);
return std::accumulate(bounds.begin(), bounds.end(), Rect(), UnionRects);
}
@@ -1672,14 +1672,16 @@ TEST_P(RenderTextHarfBuzzTest, MoveCursorLeftRight_MeiryoUILigatures) {
test_api()->EnsureLayout();
EXPECT_EQ(0U, render_text->cursor_position());
- gfx::Rect last_selection_bounds = GetSelectionBoundsUnion();
+ gfx::Rect last_selection_bounds =
+ GetSubstringBoundsUnionForRange(render_text->selection());
for (size_t i = 0; i < render_text->text().length(); ++i) {
render_text->MoveCursor(CHARACTER_BREAK, CURSOR_RIGHT, SELECTION_RETAIN);
EXPECT_EQ(i + 1, render_text->cursor_position());
// Verify the selection bounds also increase and that the correct bounds are
// returned even when the grapheme boundary lies within a glyph.
- const gfx::Rect selection_bounds = GetSelectionBoundsUnion();
+ const gfx::Rect selection_bounds =
+ GetSubstringBoundsUnionForRange(render_text->selection());
EXPECT_GT(selection_bounds.right(), last_selection_bounds.right());
EXPECT_EQ(selection_bounds.x(), last_selection_bounds.x());
last_selection_bounds = selection_bounds;
@@ -1793,9 +1795,11 @@ TEST_P(RenderTextHarfBuzzTest, MidGraphemeSelectionBounds) {
// Verify that the selection bounds extend over the entire grapheme, even if
// the selection is set amid the grapheme.
test_api()->EnsureLayout();
- const gfx::Rect mid_grapheme_bounds = GetSelectionBoundsUnion();
+ const gfx::Rect mid_grapheme_bounds =
+ GetSubstringBoundsUnionForRange(render_text->selection());
render_text->SelectAll(false);
- EXPECT_EQ(GetSelectionBoundsUnion(), mid_grapheme_bounds);
+ EXPECT_EQ(GetSubstringBoundsUnionForRange(render_text->selection()),
+ mid_grapheme_bounds);
// Although selection bounds may be set within a multi-character grapheme,
// cursor movement (e.g. via arrow key) should avoid those indices.
@@ -1849,7 +1853,8 @@ TEST_P(RenderTextHarfBuzzTest, FindCursorPositionMultiline) {
render_text->SelectRange(Range(j, j + 1));
// Test a point inside the leading edge of the glyph bounds.
- const Rect bounds = GetSelectionBoundsUnion();
+ const Rect bounds =
+ GetSubstringBoundsUnionForRange(render_text->selection());
const Point cursor_position(is_ltr ? bounds.x() + 1 : bounds.right() - 1,
bounds.y() + 1);
@@ -4160,6 +4165,84 @@ TEST_P(RenderTextHarfBuzzTest, GetDecoratedWordAtPoint_RTL) {
}
}
+// Test that GetDecoratedWordAtPoint behaves correctly for multiline text.
+TEST_P(RenderTextHarfBuzzTest, GetDecoratedWordAtPoint_Multiline) {
+ const base::string16 text = ASCIIToUTF16("a b\n..\nc.");
+ const size_t kWordOneIndex = 0; // Index of character 'a'.
+ const size_t kWordTwoIndex = 2; // Index of character 'b'.
+ const size_t kWordThreeIndex = 7; // Index of character 'c'.
+
+ // Set up render text.
+ RenderText* render_text = GetRenderText();
+ render_text->SetMultiline(true);
+ render_text->SetDisplayRect(Rect(500, 500));
+ render_text->SetText(text);
+ render_text->ApplyWeight(Font::Weight::SEMIBOLD, Range(0, 3));
+ render_text->ApplyStyle(UNDERLINE, true, Range(1, 7));
+ render_text->ApplyStyle(STRIKE, true, Range(1, 8));
+ render_text->ApplyStyle(ITALIC, true, Range(5, 8));
+
+ // Set up test expectations.
+ const std::vector<RenderText::FontSpan> font_spans =
+ render_text->GetFontSpansForTesting();
+
+ DecoratedText expected_word_1;
+ expected_word_1.text = ASCIIToUTF16("a");
+ expected_word_1.attributes.push_back(CreateRangedAttribute(
+ font_spans, 0, kWordOneIndex, Font::Weight::SEMIBOLD, 0));
+ const Rect left_glyph_word_1 =
+ GetSubstringBoundsUnionForRange(Range(kWordOneIndex, kWordOneIndex + 1));
+
+ DecoratedText expected_word_2;
+ expected_word_2.text = ASCIIToUTF16("b");
+ expected_word_2.attributes.push_back(CreateRangedAttribute(
+ font_spans, 0, kWordTwoIndex, Font::Weight::SEMIBOLD,
+ UNDERLINE_MASK | STRIKE_MASK));
+ const Rect left_glyph_word_2 =
+ GetSubstringBoundsUnionForRange(Range(kWordTwoIndex, kWordTwoIndex + 1));
+
+ DecoratedText expected_word_3;
+ expected_word_3.text = ASCIIToUTF16("c");
+ expected_word_3.attributes.push_back(
+ CreateRangedAttribute(font_spans, 0, kWordThreeIndex,
+ Font::Weight::NORMAL, STRIKE_MASK | ITALIC_MASK));
+ const Rect left_glyph_word_3 = GetSubstringBoundsUnionForRange(
+ Range(kWordThreeIndex, kWordThreeIndex + 1));
+
+ DecoratedText decorated_word;
+ Point baseline_point;
+ {
+ SCOPED_TRACE(base::StringPrintf("Query to the left of first line."));
+ EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
+ Point(-5, GetCursorYForTesting(0)), &decorated_word, &baseline_point));
+ VerifyDecoratedWordsAreEqual(expected_word_1, decorated_word);
+ EXPECT_TRUE(left_glyph_word_1.Contains(baseline_point));
+ }
+ {
+ SCOPED_TRACE(base::StringPrintf("Query on the second line"));
+ EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
+ Point(5, GetCursorYForTesting(1)), &decorated_word, &baseline_point));
+ VerifyDecoratedWordsAreEqual(expected_word_2, decorated_word);
+ EXPECT_TRUE(left_glyph_word_2.Contains(baseline_point));
+ }
+ {
+ SCOPED_TRACE(base::StringPrintf("Query on the third line"));
+
+ // Query at the center point of the character 'c'.
+ EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
+ left_glyph_word_3.CenterPoint(), &decorated_word, &baseline_point));
+ VerifyDecoratedWordsAreEqual(expected_word_3, decorated_word);
+ EXPECT_TRUE(left_glyph_word_3.Contains(baseline_point));
+ }
+ {
+ SCOPED_TRACE(base::StringPrintf("Query to the right of the third line"));
+ EXPECT_TRUE(render_text->GetDecoratedWordAtPoint(
+ Point(505, GetCursorYForTesting(2)), &decorated_word, &baseline_point));
+ VerifyDecoratedWordsAreEqual(expected_word_3, decorated_word);
+ EXPECT_TRUE(left_glyph_word_3.Contains(baseline_point));
+ }
+}
+
// Verify the boolean return value of GetDecoratedWordAtPoint.
TEST_P(RenderTextHarfBuzzTest, GetDecoratedWordAtPoint_Return) {
RenderText* render_text = GetRenderText();
@@ -4257,12 +4340,14 @@ TEST_P(RenderTextHarfBuzzTest, GetSubstringBoundsMultiline) {
expected_total_bounds.Union(expected_line_bounds);
render_text->SelectRange(line_char_range[i]);
- EXPECT_EQ(expected_line_bounds, GetSelectionBoundsUnion());
+ EXPECT_EQ(expected_line_bounds,
+ GetSubstringBoundsUnionForRange(render_text->selection()));
}
// Test complete bounds.
render_text->SelectAll(false);
- EXPECT_EQ(expected_total_bounds, GetSelectionBoundsUnion());
+ EXPECT_EQ(expected_total_bounds,
+ GetSubstringBoundsUnionForRange(render_text->selection()));
}
// Prefix for test instantiations intentionally left blank since each test

Powered by Google App Engine
This is Rietveld 408576698