Commit 480004ff authored by Etienne Bergeron's avatar Etienne Bergeron Committed by Commit Bot

Add unittest for fixed text positioning

This CL is adding unittests for testing fractional text position. The
tests are fonts and platform-independant. The layout will be the same
on every system.

R=msw@chromium.org

Bug: 1111044
Change-Id: I3db022838866516501fbd0405a059a6a4b5aa3c7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2331332
Commit-Queue: Etienne Bergeron <etienneb@chromium.org>
Reviewed-by: default avatarMichael Wasserman <msw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#795138}
parent a4554aa1
...@@ -4539,6 +4539,49 @@ TEST_F(RenderTextTest, LineSizeMultiline) { ...@@ -4539,6 +4539,49 @@ TEST_F(RenderTextTest, LineSizeMultiline) {
render_text->GetLineSizeF(SelectionModel(10, CURSOR_FORWARD)).width()); render_text->GetLineSizeF(SelectionModel(10, CURSOR_FORWARD)).width());
} }
TEST_F(RenderTextTest, TextPosition) {
// Set a fractional glyph size to trigger floating rounding logic.
// This test sets a fixed fractional width and height for glyphs to ensure
// that computations are fixed (i.e. not font or system dependent).
const float kGlyphWidth = 5.4;
const float kGlyphHeight = 9.2;
SetGlyphWidth(kGlyphWidth);
SetGlyphHeight(kGlyphHeight);
const int kGlyphCount = 3;
RenderText* render_text = GetRenderText();
render_text->SetText(ASCIIToUTF16(std::string(kGlyphCount, 'x')));
render_text->SetDisplayRect(Rect(1, 1, 25, 12));
render_text->SetCursorEnabled(false);
render_text->SetVerticalAlignment(ALIGN_TOP);
// Content width is 16.2px. Extra space inside display rect is 8.8px
// (i.e. 25px - 16.2px) which is used for alignment.
const float expected_content_width = kGlyphCount * kGlyphWidth;
EXPECT_FLOAT_EQ(expected_content_width, render_text->GetContentWidthF());
EXPECT_FLOAT_EQ(expected_content_width, render_text->TotalLineWidth());
render_text->SetHorizontalAlignment(ALIGN_LEFT);
EXPECT_EQ(1, render_text->GetLineOffset(0).x());
EXPECT_EQ(Rect(1, 1, 6, 10), GetSubstringBoundsUnion(Range(0, 1)));
EXPECT_EQ(Rect(6, 1, 6, 10), GetSubstringBoundsUnion(Range(1, 2)));
EXPECT_EQ(Rect(11, 1, 7, 10), GetSubstringBoundsUnion(Range(2, 3)));
render_text->SetHorizontalAlignment(ALIGN_CENTER);
EXPECT_EQ(5, render_text->GetLineOffset(0).x());
EXPECT_EQ(Rect(5, 1, 6, 10), GetSubstringBoundsUnion(Range(0, 1)));
EXPECT_EQ(Rect(10, 1, 6, 10), GetSubstringBoundsUnion(Range(1, 2)));
EXPECT_EQ(Rect(15, 1, 7, 10), GetSubstringBoundsUnion(Range(2, 3)));
render_text->SetHorizontalAlignment(ALIGN_RIGHT);
EXPECT_EQ(9, render_text->GetLineOffset(0).x());
EXPECT_EQ(Rect(9, 1, 6, 10), GetSubstringBoundsUnion(Range(0, 1)));
EXPECT_EQ(Rect(14, 1, 6, 10), GetSubstringBoundsUnion(Range(1, 2)));
EXPECT_EQ(Rect(19, 1, 7, 10), GetSubstringBoundsUnion(Range(2, 3)));
}
TEST_F(RenderTextTest, SetFontList) { TEST_F(RenderTextTest, SetFontList) {
RenderText* render_text = GetRenderText(); RenderText* render_text = GetRenderText();
render_text->SetFontList( render_text->SetFontList(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment