Chromium Code Reviews| Index: ui/gfx/render_text_unittest.cc |
| diff --git a/ui/gfx/render_text_unittest.cc b/ui/gfx/render_text_unittest.cc |
| index b982f5bf898e0a9eada0f6ab551d775ac486d2fd..00f2554895649e445dc13c2f6d22fd9d02fa6b34 100644 |
| --- a/ui/gfx/render_text_unittest.cc |
| +++ b/ui/gfx/render_text_unittest.cc |
| @@ -194,14 +194,41 @@ class TestRectangleBuffer { |
| ASSERT_LE(left + width, stride_) << string_ << ", left " << left |
| << ", width " << width << ", stride_ " |
| << stride_; |
| + // Only report the first N failures. |
| + uint32_t fail_limit = 5; |
| for (int y = top; y < top + height; ++y) { |
| for (int x = left; x < left + width; ++x) { |
| SkColor buffer_color = buffer_[x + y * stride_]; |
| + if (color != buffer_color && !(--fail_limit)) { |
| + EXPECT_TRUE(fail_limit) << "fail_limit reached"; |
| + return; |
| + } |
| EXPECT_EQ(color, buffer_color) << string_ << " at " << x << ", " << y; |
| } |
| } |
| } |
| + void EnsureSolidBorder(SkColor color, const Rect& rect) const { |
| + { |
| + SCOPED_TRACE("EnsureSolidBorder Top Side"); |
| + EnsureSolidRect(SK_ColorWHITE, 0, 0, stride_, rect.y()); |
| + } |
| + { |
| + SCOPED_TRACE("EnsureSolidBorder Bottom Side"); |
| + EnsureSolidRect(SK_ColorWHITE, 0, rect.bottom(), stride_, |
| + row_count_ - rect.bottom()); |
| + } |
| + { |
| + SCOPED_TRACE("EnsureSolidBorder Left Side"); |
| + EnsureSolidRect(SK_ColorWHITE, 0, rect.y(), rect.x(), rect.height()); |
| + } |
| + { |
| + SCOPED_TRACE("EnsureSolidBorder Right Side"); |
| + EnsureSolidRect(SK_ColorWHITE, rect.right(), rect.y(), |
| + stride_ - rect.right(), rect.height()); |
| + } |
| + } |
| + |
| private: |
| const wchar_t* string_; |
| const SkColor* buffer_; |
| @@ -2656,6 +2683,80 @@ TEST_F(RenderTextTest, StringFitsOwnWidth) { |
| EXPECT_EQ(kString, render_text->GetDisplayText()); |
| } |
| +// TODO(dschuyler): Alignment tests disabled on Mac because RenderTextMac |
| +// does not implement this yet. |
| +#if !defined(OS_MACOSX) |
| +TEST_F(RenderTextTest, VerticalAlignment) { |
| + scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| + const wchar_t* string = L"g Hello g"; |
| + render_text->SetText(WideToUTF16(string)); |
| + render_text->ApplyStyle(BOLD, true, Range(0, 3)); |
| + const Size kCanvasSize(171, 50); |
| + const int kTestSize = 10; |
| + const Size string_size(render_text->GetContentWidth(), |
| + render_text->GetStringSize().height()); |
| + // The + 1 accounts for legacy behavior in GetAlignmentOffset(). |
| + const int kCenterBegin = (kCanvasSize.width() + 1 - string_size.width()) / 2; |
| + const int kRightBegin = kCanvasSize.width() - string_size.width() - kTestSize; |
| + // TODO(dschuyler): Determine where the need for the -1 below originates from. |
| + const int kMiddleBegin = |
| + ((kCanvasSize.height()) / 2) - 1 + |
| + (render_text->GetBaseline() - render_text->GetDisplayTextBaseline()); |
| + const int kBottomBegin = |
| + kCanvasSize.height() - string_size.height() - kTestSize; |
| + |
| + struct Tests { |
| + HorizontalAlignment horizontal; |
| + VerticalAlignment vertical; |
| + int x; |
| + int y; |
| + } const kTests[] = { |
| + {ALIGN_LEFT, VALIGN_TOP, kTestSize, kTestSize}, |
| + {ALIGN_LEFT, VALIGN_MIDDLE, kTestSize, kMiddleBegin}, |
| + {ALIGN_LEFT, VALIGN_BOTTOM, kTestSize, kBottomBegin}, |
| + {ALIGN_CENTER, VALIGN_TOP, kCenterBegin, kTestSize}, |
| + {ALIGN_CENTER, VALIGN_MIDDLE, kCenterBegin, kMiddleBegin}, |
| + {ALIGN_CENTER, VALIGN_BOTTOM, kCenterBegin, kBottomBegin}, |
| + {ALIGN_RIGHT, VALIGN_TOP, kRightBegin, kTestSize}, |
| + {ALIGN_RIGHT, VALIGN_MIDDLE, kRightBegin, kMiddleBegin}, |
| + {ALIGN_RIGHT, VALIGN_BOTTOM, kRightBegin, kBottomBegin}, |
| + }; |
| + |
| + skia::RefPtr<SkSurface> surface = skia::AdoptRef( |
| + SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height())); |
| + scoped_ptr<Canvas> canvas( |
| + Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f)); |
| + render_text->SetColor(SK_ColorBLACK); |
| + Rect bounds = Rect(kTestSize, kTestSize, kCanvasSize.width() - kTestSize * 2, |
| + kCanvasSize.height() - kTestSize * 2); |
| + render_text->SetDisplayRect(bounds); |
| + // Allow the RenderText to paint outside of its display rect. |
| + render_text->set_clip_to_display_rect(false); |
| + const uint32* buffer = |
| + static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); |
| + ASSERT_NE(nullptr, buffer); |
| + TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), |
| + kCanvasSize.height()); |
| + ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width()); |
| + ASSERT_LE(string_size.height() + kTestSize * 2, kCanvasSize.height()); |
| + for (const auto& test : kTests) { |
| + SCOPED_TRACE(base::StringPrintf("VerticalAlignement H %i, V %i", |
| + test.horizontal, test.vertical)); |
| + |
| + surface->getCanvas()->clear(SK_ColorWHITE); |
| + render_text->SetHorizontalAlignment(test.horizontal); |
| + render_text->SetVerticalAlignment(test.vertical); |
| + EXPECT_EQ(test.x - kTestSize, render_text->GetAlignmentOffset(0).x()); |
| + EXPECT_EQ(test.y - kTestSize, render_text->GetAlignmentOffset(0).y()); |
| + Rect expected_rect(test.x, test.y, string_size.width(), |
| + string_size.height()); |
| + expected_rect.Inset(-1, -1); |
| + render_text->Draw(canvas.get()); |
| + rect_buffer.EnsureSolidBorder(SK_ColorWHITE, expected_rect); |
| + } |
| +} |
| +#endif // !defined(OS_MACOSX) |
| + |
| // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184 |
| #if !defined(OS_WIN) |
| // Ensure that RenderText examines all of the fonts in its FontList before |
| @@ -2750,7 +2851,8 @@ TEST_F(RenderTextTest, TextDoesntClip) { |
| for (auto string : kTestStrings) { |
| surface->getCanvas()->clear(SK_ColorWHITE); |
| render_text->SetText(WideToUTF16(string)); |
| - const Size string_size = render_text->GetStringSize(); |
| + const Size string_size(render_text->GetContentWidth(), |
|
msw
2015/03/30 19:19:12
I don't think this is correct, the cursor shouldn'
dschuyler
2015/03/31 18:38:14
The value of cursor_enabled_ defaults to true. It
msw
2015/04/01 15:07:17
Ah, you should probably disable the cursor for the
|
| + render_text->GetStringSize().height()); |
| render_text->ApplyBaselineStyle(SUPERSCRIPT, Range(1, 2)); |
| render_text->ApplyBaselineStyle(SUPERIOR, Range(3, 4)); |
| render_text->ApplyBaselineStyle(INFERIOR, Range(5, 6)); |
| @@ -2761,14 +2863,16 @@ TEST_F(RenderTextTest, TextDoesntClip) { |
| // Allow the RenderText to paint outside of its display rect. |
| render_text->set_clip_to_display_rect(false); |
| ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width()); |
| + ASSERT_LE(string_size.height() + kTestSize * 2, kCanvasSize.height()); |
| render_text->Draw(canvas.get()); |
| - ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width()); |
| const uint32* buffer = |
| static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); |
| ASSERT_NE(nullptr, buffer); |
| TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), |
| kCanvasSize.height()); |
| + // TODO(dschuyler): After platform issues are resolved below, change to |
| + // using EnsureSolidBorder() rather than EnsureSolidRect(). |
| { |
| #if !defined(OS_CHROMEOS) |
| // TODO(dschuyler): On ChromeOS text draws above the GetStringSize rect. |
| @@ -2818,6 +2922,7 @@ TEST_F(RenderTextTest, TextDoesntClip) { |
| // Ensure that the text will clip to the display rect. Draws to a canvas and |
| // checks whether any pixel beyond the bounding rectangle is colored. |
| TEST_F(RenderTextTest, TextDoesClip) { |
| + SCOPED_TRACE("TextDoesClip"); |
|
msw
2015/03/30 19:19:12
Remove this scoped trace.
dschuyler
2015/03/31 18:38:14
Done.
|
| const wchar_t* kTestStrings[] = {L"TEST", L"W", L"WWWW", L"gAXAXWWWW"}; |
| const Size kCanvasSize(300, 50); |
| const int kTestSize = 10; |
| @@ -2833,40 +2938,22 @@ TEST_F(RenderTextTest, TextDoesClip) { |
| for (auto string : kTestStrings) { |
| surface->getCanvas()->clear(SK_ColorWHITE); |
| render_text->SetText(WideToUTF16(string)); |
| - const Size string_size = render_text->GetStringSize(); |
| + const Size string_size(render_text->GetContentWidth(), |
|
msw
2015/03/30 19:19:12
Ditto.
dschuyler
2015/03/31 18:38:14
Acknowledged.
|
| + render_text->GetStringSize().height()); |
| int fake_width = string_size.width() / 2; |
| int fake_height = string_size.height() / 2; |
| - render_text->SetDisplayRect( |
| - Rect(kTestSize, kTestSize, fake_width, fake_height)); |
| + const Rect bounds(kTestSize, kTestSize, fake_width, fake_height); |
| + render_text->SetDisplayRect(bounds); |
| render_text->set_clip_to_display_rect(true); |
| + ASSERT_LE(string_size.width() + kTestSize * 2, kCanvasSize.width()); |
| + ASSERT_LE(string_size.height() + kTestSize * 2, kCanvasSize.height()); |
| render_text->Draw(canvas.get()); |
| - ASSERT_LT(string_size.width() + kTestSize, kCanvasSize.width()); |
| const uint32* buffer = |
| static_cast<const uint32*>(surface->peekPixels(nullptr, nullptr)); |
| ASSERT_NE(nullptr, buffer); |
| TestRectangleBuffer rect_buffer(string, buffer, kCanvasSize.width(), |
| kCanvasSize.height()); |
| - { |
| - SCOPED_TRACE("TextDoesClip Top Side"); |
| - rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, 0, kCanvasSize.width(), |
| - kTestSize); |
| - } |
| - |
| - { |
| - SCOPED_TRACE("TextDoesClip Bottom Side"); |
| - rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize + fake_height, |
| - kCanvasSize.width(), kTestSize); |
| - } |
| - { |
| - SCOPED_TRACE("TextDoesClip Left Side"); |
| - rect_buffer.EnsureSolidRect(SK_ColorWHITE, 0, kTestSize, kTestSize, |
| - fake_height); |
| - } |
| - { |
| - SCOPED_TRACE("TextDoesClip Right Side"); |
| - rect_buffer.EnsureSolidRect(SK_ColorWHITE, kTestSize + fake_width, |
| - kTestSize, kTestSize, fake_height); |
| - } |
| + rect_buffer.EnsureSolidBorder(SK_ColorWHITE, bounds); |
| } |
| } |