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

Side by Side Diff: ui/gfx/render_text_unittest.cc

Issue 25039002: Always aligns text at vertically center (Textfield, Label). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Worked on pkasting's minor comment. Created 7 years, 2 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/render_text.h" 5 #include "ui/gfx/render_text.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits>
8 9
9 #include "base/format_macros.h" 10 #include "base/format_macros.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gfx/break_list.h" 15 #include "ui/gfx/break_list.h"
15 #include "ui/gfx/canvas.h" 16 #include "ui/gfx/canvas.h"
16 17
17 #if defined(OS_WIN) 18 #if defined(OS_WIN)
(...skipping 1114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1132 } 1133 }
1133 1134
1134 // TODO(asvitkine): This test fails because PlatformFontMac uses point font 1135 // TODO(asvitkine): This test fails because PlatformFontMac uses point font
1135 // sizes instead of pixel sizes like other implementations. 1136 // sizes instead of pixel sizes like other implementations.
1136 #if !defined(OS_MACOSX) 1137 #if !defined(OS_MACOSX)
1137 TEST_F(RenderTextTest, StringSizeEmptyString) { 1138 TEST_F(RenderTextTest, StringSizeEmptyString) {
1138 // Ascent and descent of Arial and Symbol are different on most platforms. 1139 // Ascent and descent of Arial and Symbol are different on most platforms.
1139 const FontList font_list("Arial,Symbol, 16px"); 1140 const FontList font_list("Arial,Symbol, 16px");
1140 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 1141 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1141 render_text->SetFontList(font_list); 1142 render_text->SetFontList(font_list);
1143 render_text->SetDisplayRect(Rect(0, 0, std::numeric_limits<int>::max(),
msw 2013/10/23 01:18:00 Hmm, an int max width shouldn't break anything, bu
Yuki 2013/10/24 14:32:54 Done.
1144 font_list.GetHeight()));
1142 1145
1143 // The empty string respects FontList metrics for non-zero height 1146 // The empty string respects FontList metrics for non-zero height
1144 // and baseline. 1147 // and baseline.
1145 render_text->SetText(base::string16()); 1148 render_text->SetText(base::string16());
1146 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); 1149 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1147 EXPECT_EQ(0, render_text->GetStringSize().width()); 1150 EXPECT_EQ(0, render_text->GetStringSize().width());
1148 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline()); 1151 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1149 1152
1150 render_text->SetText(UTF8ToUTF16(" ")); 1153 render_text->SetText(UTF8ToUTF16(" "));
1151 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); 1154 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
(...skipping 21 matching lines...) Expand all
1173 symbol_font.GetBaseline() < arial_font.GetBaseline()) { 1176 symbol_font.GetBaseline() < arial_font.GetBaseline()) {
1174 std::swap(smaller_font, larger_font); 1177 std::swap(smaller_font, larger_font);
1175 std::swap(smaller_font_text, larger_font_text); 1178 std::swap(smaller_font_text, larger_font_text);
1176 } 1179 }
1177 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight()); 1180 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight());
1178 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline()); 1181 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline());
1179 1182
1180 // Check |smaller_font_text| is rendered with the smaller font. 1183 // Check |smaller_font_text| is rendered with the smaller font.
1181 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 1184 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1182 render_text->SetText(UTF8ToUTF16(smaller_font_text)); 1185 render_text->SetText(UTF8ToUTF16(smaller_font_text));
1183 render_text->SetFont(smaller_font); 1186 render_text->SetFontList(FontList(smaller_font));
1187 render_text->SetDisplayRect(Rect(0, 0, std::numeric_limits<int>::max(),
1188 render_text->font_list().GetHeight()));
1184 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height()); 1189 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height());
1185 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline()); 1190 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline());
1186 1191
1187 // Layout the same text with mixed fonts. The text should be rendered with 1192 // Layout the same text with mixed fonts. The text should be rendered with
1188 // the smaller font, but the height and baseline are determined with the 1193 // the smaller font, but the height and baseline are determined with the
1189 // metrics of the font list, which is equal to the larger font. 1194 // metrics of the font list, which is equal to the larger font.
1190 std::vector<Font> fonts; 1195 std::vector<Font> fonts;
1191 fonts.push_back(smaller_font); // The primary font is the smaller font. 1196 fonts.push_back(smaller_font); // The primary font is the smaller font.
1192 fonts.push_back(larger_font); 1197 fonts.push_back(larger_font);
1193 const FontList font_list(fonts); 1198 const FontList font_list(fonts);
1194 render_text->SetFontList(font_list); 1199 render_text->SetFontList(font_list);
1200 render_text->SetDisplayRect(Rect(0, 0, std::numeric_limits<int>::max(),
1201 render_text->font_list().GetHeight()));
1195 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height()); 1202 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height());
1196 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline()); 1203 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline());
1197 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); 1204 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1198 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline()); 1205 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1199 } 1206 }
1200 1207
1201 TEST_F(RenderTextTest, SetFont) { 1208 TEST_F(RenderTextTest, SetFont) {
1202 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 1209 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
1203 render_text->SetFont(Font("Arial", 12)); 1210 render_text->SetFont(Font("Arial", 12));
1204 EXPECT_EQ("Arial", render_text->GetPrimaryFont().GetFontName()); 1211 EXPECT_EQ("Arial", render_text->GetPrimaryFont().GetFontName());
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1293 1300
1294 // Set display area's size equal to the font size. 1301 // Set display area's size equal to the font size.
1295 const Size font_size(render_text->GetContentWidth(), 1302 const Size font_size(render_text->GetContentWidth(),
1296 render_text->GetStringSize().height()); 1303 render_text->GetStringSize().height());
1297 Rect display_rect(font_size); 1304 Rect display_rect(font_size);
1298 render_text->SetDisplayRect(display_rect); 1305 render_text->SetDisplayRect(display_rect);
1299 1306
1300 Vector2d offset = render_text->GetLineOffset(0); 1307 Vector2d offset = render_text->GetLineOffset(0);
1301 EXPECT_TRUE(offset.IsZero()); 1308 EXPECT_TRUE(offset.IsZero());
1302 1309
1303 // Set display area's size greater than font size. 1310 // Widen display area's size by kEnlargementX.
msw 2013/10/23 01:18:00 nit: "Widen the display area by |kEnlargementX|",
Yuki 2013/10/24 14:32:54 Done.
1304 const int kEnlargement = 2; 1311 const int kEnlargementX = 2;
1305 display_rect.Inset(0, 0, -kEnlargement, -kEnlargement); 1312 display_rect.Inset(0, 0, -kEnlargementX, 0);
1306 render_text->SetDisplayRect(display_rect); 1313 render_text->SetDisplayRect(display_rect);
1307 1314
1308 // Check the default horizontal and vertical alignment. 1315 // Check the default horizontal alignment.
1309 offset = render_text->GetLineOffset(0); 1316 offset = render_text->GetLineOffset(0);
1310 EXPECT_EQ(kEnlargement / 2, offset.y());
1311 EXPECT_EQ(0, offset.x()); 1317 EXPECT_EQ(0, offset.x());
1312 1318
1313 // Check explicitly setting the horizontal alignment. 1319 // Check explicitly setting the horizontal alignment.
1314 render_text->SetHorizontalAlignment(ALIGN_LEFT); 1320 render_text->SetHorizontalAlignment(ALIGN_LEFT);
1315 offset = render_text->GetLineOffset(0); 1321 offset = render_text->GetLineOffset(0);
1316 EXPECT_EQ(0, offset.x()); 1322 EXPECT_EQ(0, offset.x());
1317 render_text->SetHorizontalAlignment(ALIGN_CENTER); 1323 render_text->SetHorizontalAlignment(ALIGN_CENTER);
1318 offset = render_text->GetLineOffset(0); 1324 offset = render_text->GetLineOffset(0);
1319 EXPECT_EQ(kEnlargement / 2, offset.x()); 1325 EXPECT_EQ(kEnlargementX / 2, offset.x());
1320 render_text->SetHorizontalAlignment(ALIGN_RIGHT); 1326 render_text->SetHorizontalAlignment(ALIGN_RIGHT);
1321 offset = render_text->GetLineOffset(0); 1327 offset = render_text->GetLineOffset(0);
1322 EXPECT_EQ(kEnlargement, offset.x()); 1328 EXPECT_EQ(kEnlargementX, offset.x());
1323 1329
1324 // Check explicitly setting the vertical alignment. 1330 // Enlarge the display height twice. This allows the |render_text| puts the
msw 2013/10/23 01:18:00 nit: simplify this comment to "Check that text is
Yuki 2013/10/24 14:32:54 Done.
1325 render_text->SetVerticalAlignment(ALIGN_TOP); 1331 // text at vertical center respecting cap height and regardless of height
1332 // limit.
1333 const int kEnlargementY = display_rect.height();
1334 display_rect.Inset(0, 0, 0, -kEnlargementY);
1335 render_text->SetDisplayRect(display_rect);
1336 const Vector2d prev_offset = render_text->GetLineOffset(0);
1337 // Enlarge the display height more and test the y offset.
1338 display_rect.Inset(0, 0, 0, -2 * kEnlargementY);
1339 render_text->SetDisplayRect(display_rect);
1326 offset = render_text->GetLineOffset(0); 1340 offset = render_text->GetLineOffset(0);
1327 EXPECT_EQ(0, offset.y()); 1341 EXPECT_EQ(prev_offset.y() + kEnlargementY, offset.y());
1328 render_text->SetVerticalAlignment(ALIGN_VCENTER);
1329 offset = render_text->GetLineOffset(0);
1330 EXPECT_EQ(kEnlargement / 2, offset.y());
1331 render_text->SetVerticalAlignment(ALIGN_BOTTOM);
1332 offset = render_text->GetLineOffset(0);
1333 EXPECT_EQ(kEnlargement, offset.y());
1334 1342
1335 SetRTL(was_rtl); 1343 SetRTL(was_rtl);
1336 } 1344 }
1337 1345
1338 TEST_F(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL) { 1346 TEST_F(RenderTextTest, GetTextOffsetHorizontalDefaultInRTL) {
1339 // This only checks the default horizontal alignment in RTL mode; all other 1347 // This only checks the default horizontal alignment in RTL mode; all other
1340 // GetLineOffset(0) attributes are checked by the test above. 1348 // GetLineOffset(0) attributes are checked by the test above.
1341 const bool was_rtl = base::i18n::IsRTL(); 1349 const bool was_rtl = base::i18n::IsRTL();
1342 SetRTL(true); 1350 SetRTL(true);
1343 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); 1351 scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after
1722 for (size_t i = 0; i < arraysize(kTestStrings); ++i) { 1730 for (size_t i = 0; i < arraysize(kTestStrings); ++i) {
1723 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i)); 1731 SCOPED_TRACE(base::StringPrintf("kTestStrings[%" PRIuS "]", i));
1724 render_text->SetText(WideToUTF16(kTestStrings[i])); 1732 render_text->SetText(WideToUTF16(kTestStrings[i]));
1725 render_text->Draw(&canvas); 1733 render_text->Draw(&canvas);
1726 EXPECT_EQ(1U, render_text->lines_.size()); 1734 EXPECT_EQ(1U, render_text->lines_.size());
1727 } 1735 }
1728 } 1736 }
1729 #endif // defined(OS_WIN) 1737 #endif // defined(OS_WIN)
1730 1738
1731 } // namespace gfx 1739 } // namespace gfx
OLDNEW
« ui/gfx/render_text.cc ('K') | « ui/gfx/render_text_mac.cc ('k') | ui/gfx/render_text_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698