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

Unified Diff: ui/gfx/render_text_unittest.cc

Issue 924543004: adding baseline options for super/sub scripting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added rough bounds checking that is not yet correct Created 5 years, 10 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
« no previous file with comments | « ui/gfx/render_text_mac.cc ('k') | ui/gfx/text_constants.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/render_text_unittest.cc
diff --git a/ui/gfx/render_text_unittest.cc b/ui/gfx/render_text_unittest.cc
index e7829f65d6b86089256f61d473a1303e14908990..5eb613d3020e5121e829c5f29cdb4c0ab49cae34 100644
--- a/ui/gfx/render_text_unittest.cc
+++ b/ui/gfx/render_text_unittest.cc
@@ -174,6 +174,8 @@ TEST_F(RenderTextTest, DefaultStyle) {
const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
for (size_t i = 0; i < arraysize(cases); ++i) {
EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLACK));
+ EXPECT_TRUE(
+ render_text->baselines().EqualsValueForTesting(NORMAL_BASELINE));
for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
EXPECT_TRUE(render_text->styles()[style].EqualsValueForTesting(false));
render_text->SetText(WideToUTF16(cases[i]));
@@ -185,11 +187,13 @@ TEST_F(RenderTextTest, SetColorAndStyle) {
scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
const SkColor color = SK_ColorRED;
render_text->SetColor(color);
+ render_text->SetBaselineStyle(SUPERSCRIPT);
render_text->SetStyle(BOLD, true);
render_text->SetStyle(UNDERLINE, false);
const wchar_t* const cases[] = { kWeak, kLtr, L"Hello", kRtl, L"", L"" };
for (size_t i = 0; i < arraysize(cases); ++i) {
EXPECT_TRUE(render_text->colors().EqualsValueForTesting(color));
+ EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUPERSCRIPT));
EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(true));
EXPECT_TRUE(render_text->styles()[UNDERLINE].EqualsValueForTesting(false));
render_text->SetText(WideToUTF16(cases[i]));
@@ -208,32 +212,51 @@ TEST_F(RenderTextTest, ApplyColorAndStyle) {
// Apply a ranged color and style and check the resulting breaks.
render_text->ApplyColor(SK_ColorRED, Range(1, 4));
+ render_text->ApplyBaselineStyle(SUPERIOR, Range(2, 4));
render_text->ApplyStyle(BOLD, true, Range(2, 5));
std::vector<std::pair<size_t, SkColor> > expected_color;
expected_color.push_back(std::pair<size_t, SkColor>(0, SK_ColorBLACK));
expected_color.push_back(std::pair<size_t, SkColor>(1, SK_ColorRED));
expected_color.push_back(std::pair<size_t, SkColor>(4, SK_ColorBLACK));
EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color));
+ std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_style;
+ expected_baseline_style.push_back(
+ std::pair<size_t, BaselineStyle>(0, NORMAL_BASELINE));
+ expected_baseline_style.push_back(
+ std::pair<size_t, BaselineStyle>(2, SUPERIOR));
+ expected_baseline_style.push_back(
+ std::pair<size_t, BaselineStyle>(4, NORMAL_BASELINE));
+ EXPECT_TRUE(
+ render_text->baselines().EqualsForTesting(expected_baseline_style));
std::vector<std::pair<size_t, bool> > expected_style;
expected_style.push_back(std::pair<size_t, bool>(0, false));
expected_style.push_back(std::pair<size_t, bool>(2, true));
expected_style.push_back(std::pair<size_t, bool>(5, false));
EXPECT_TRUE(render_text->styles()[BOLD].EqualsForTesting(expected_style));
- // Ensure setting a color and style overrides the ranged colors and styles.
+ // Ensure setting a color, baseline, and style overrides the ranged colors,
+ // baseline, and styles.
render_text->SetColor(SK_ColorBLUE);
EXPECT_TRUE(render_text->colors().EqualsValueForTesting(SK_ColorBLUE));
+ render_text->SetBaselineStyle(SUBSCRIPT);
+ EXPECT_TRUE(render_text->baselines().EqualsValueForTesting(SUBSCRIPT));
render_text->SetStyle(BOLD, false);
EXPECT_TRUE(render_text->styles()[BOLD].EqualsValueForTesting(false));
- // Apply a color and style over the text end and check the resulting breaks.
- // (INT_MAX should be used instead of the text length for the range end)
+ // Apply a color, baseline, and style over the text end and check the
+ // resulting breaks (INT_MAX should be used instead of the text length for
+ // the range end)
const size_t text_length = render_text->text().length();
render_text->ApplyColor(SK_ColorRED, Range(0, text_length));
+ render_text->ApplyBaselineStyle(SUPERIOR, Range(0, text_length));
render_text->ApplyStyle(BOLD, true, Range(2, text_length));
std::vector<std::pair<size_t, SkColor> > expected_color_end;
expected_color_end.push_back(std::pair<size_t, SkColor>(0, SK_ColorRED));
EXPECT_TRUE(render_text->colors().EqualsForTesting(expected_color_end));
+ std::vector<std::pair<size_t, BaselineStyle>> expected_baseline_end;
+ expected_baseline_end.push_back(
+ std::pair<size_t, BaselineStyle>(0, SUPERIOR));
+ EXPECT_TRUE(render_text->baselines().EqualsForTesting(expected_baseline_end));
std::vector<std::pair<size_t, bool> > expected_style_end;
expected_style_end.push_back(std::pair<size_t, bool>(0, false));
expected_style_end.push_back(std::pair<size_t, bool>(2, true));
@@ -2558,4 +2581,88 @@ TEST_F(RenderTextTest, TextDoesntClip) {
}
}
+// Ensure that the width reported by RenderText is sufficient for drawing. Draws
+// to a canvas and checks whether any pixel beyond the bounding rectangle is
+// colored.
+TEST_F(RenderTextTest, TextBaselineStyleDoesntClip) {
msw 2015/02/26 03:36:53 It might be nice to combine the two tests, or mayb
dschuyler 2015/02/26 23:15:55 I realized that TextDoesntClipt was a subset of Te
+ const wchar_t* kTestStrings[] = {
+ L"TEST_______",
+ L"WWWWWWWWWW",
+ L"gAXAXAXAXAXAXA",
+ L"g\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5X\x00C5",
+ L"\x0647\x0654\x0647\x0654\x0647\x0654\x0647\x0654\x0645\x0631\x062D"
+ L"\x0628\x0627"};
+ const Size kCanvasSize(300, 50);
+ const int kTestSize = 10;
+
+ skia::RefPtr<SkSurface> surface = skia::AdoptRef(
+ SkSurface::NewRasterN32Premul(kCanvasSize.width(), kCanvasSize.height()));
+ scoped_ptr<Canvas> canvas(
+ Canvas::CreateCanvasWithoutScaling(surface->getCanvas(), 1.0f));
+ scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
+ render_text->SetDisplayRect(Rect(kCanvasSize));
+ render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT);
msw 2015/02/26 03:36:53 nit: you can remove all gfx:: qualifiers in this t
dschuyler 2015/02/26 23:15:55 Done.
+ render_text->SetColor(SK_ColorBLACK);
+
+ for (auto string : kTestStrings) {
+ surface->getCanvas()->clear(SK_ColorWHITE);
+ render_text->SetText(WideToUTF16(string));
+ render_text->ApplyBaselineStyle(SUPERSCRIPT, gfx::Range(1, 2));
+ render_text->ApplyBaselineStyle(SUPERIOR, gfx::Range(3, 4));
+ render_text->ApplyBaselineStyle(INFERIOR, gfx::Range(5, 6));
+ render_text->ApplyBaselineStyle(SUBSCRIPT, gfx::Range(7, 8));
+ render_text->SetStyle(BOLD, true);
+ render_text->SetDisplayRect(
+ Rect(kTestSize, 0, kCanvasSize.width() - kTestSize,
msw 2015/02/26 03:36:54 Shouldn't the width be render_text->GetStringSize(
dschuyler 2015/02/26 23:15:55 If the intent were to clip to the rect, yes. The
+ render_text->GetStringSize().height() + kTestSize * 2));
+#if 1
+ // REMOVE(dschuyler): This should be removed because it creates a fake
+ // success for this test by clipping the text.
+ render_text->SetDisplayRect(Rect(kTestSize, kTestSize,
+ kCanvasSize.width() - kTestSize,
+ render_text->GetStringSize().height()));
+#endif
+ render_text->Draw(canvas.get());
+ int width = render_text->GetStringSize().width();
+ ASSERT_LT(width + kTestSize, kCanvasSize.width());
+ const uint32* buffer =
+ static_cast<const uint32*>(surface->peekPixels(NULL, NULL));
msw 2015/02/26 03:36:53 nit: nullptr
dschuyler 2015/02/26 23:15:55 Done.
+ ASSERT_NE(nullptr, buffer);
+ // Top side.
+ for (int y = 0; y < kTestSize; ++y) {
msw 2015/02/26 03:36:53 I don't think you can assume the text will be vert
dschuyler 2015/02/26 23:15:55 Thanks for pointing out the clip to display rect f
+ for (int x = 0; x < kCanvasSize.width(); ++x) {
+ SkColor color = buffer[x + y * kCanvasSize.width()];
+ EXPECT_EQ(SK_ColorWHITE, color) << string << " at " << x << ", " << y;
+ }
+ }
+ // Bottom side.
+ int underside = (kTestSize + render_text->GetStringSize().height());
+ for (int y = underside; y < underside + kTestSize; ++y) {
+ for (int x = 0; x < kCanvasSize.width(); ++x) {
+ SkColor color = buffer[x + y * kCanvasSize.width()];
+ EXPECT_EQ(SK_ColorWHITE, color) << string << " at " << x << ", " << y;
+ }
+ }
+ // Left side.
+ for (int y = 0; y < kCanvasSize.height(); ++y) {
+ int current_y = y * kCanvasSize.width();
+ SkColor color;
+ for (int x = 0; x < kTestSize; ++x) {
+ color = buffer[x + current_y];
+ EXPECT_EQ(SK_ColorWHITE, color) << string << " at " << x << ", " << y;
+ }
+ }
+ // Right side.
+ for (int y = 0; y < kCanvasSize.height(); ++y) {
+ // Allow one column of anti-aliased pixels past the expected width.
+ SkColor color = buffer[kTestSize + width + y * kCanvasSize.width()];
+ EXPECT_LT(220U, color_utils::GetLuminanceForColor(color)) << string;
+ for (int x = 1; x < kTestSize; ++x) {
+ color = buffer[kTestSize + width + x + y * kCanvasSize.width()];
+ EXPECT_EQ(SK_ColorWHITE, color) << string << " at " << x << ", " << y;
+ }
+ }
+ }
+}
+
} // namespace gfx
« no previous file with comments | « ui/gfx/render_text_mac.cc ('k') | ui/gfx/text_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698