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

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

Issue 2302313002: Configure font font names in GFX unittests (Closed)
Patch Set: Use Heiti SC font, namespace, comment, hex escapes Created 4 years, 3 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
« ui/gfx/font_names_testing.cc ('K') | « ui/gfx/font_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <limits.h> 7 #include <limits.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 11 matching lines...) Expand all
22 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
23 #include "build/build_config.h" 23 #include "build/build_config.h"
24 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/skia/include/core/SkRefCnt.h" 25 #include "third_party/skia/include/core/SkRefCnt.h"
26 #include "third_party/skia/include/core/SkSurface.h" 26 #include "third_party/skia/include/core/SkSurface.h"
27 #include "third_party/skia/include/core/SkTypeface.h" 27 #include "third_party/skia/include/core/SkTypeface.h"
28 #include "ui/gfx/break_list.h" 28 #include "ui/gfx/break_list.h"
29 #include "ui/gfx/canvas.h" 29 #include "ui/gfx/canvas.h"
30 #include "ui/gfx/color_utils.h" 30 #include "ui/gfx/color_utils.h"
31 #include "ui/gfx/font.h" 31 #include "ui/gfx/font.h"
32 #include "ui/gfx/font_names_testing.h"
32 #include "ui/gfx/geometry/point.h" 33 #include "ui/gfx/geometry/point.h"
33 #include "ui/gfx/geometry/point_f.h" 34 #include "ui/gfx/geometry/point_f.h"
34 #include "ui/gfx/range/range.h" 35 #include "ui/gfx/range/range.h"
35 #include "ui/gfx/range/range_f.h" 36 #include "ui/gfx/range/range_f.h"
36 #include "ui/gfx/render_text_harfbuzz.h" 37 #include "ui/gfx/render_text_harfbuzz.h"
37 #include "ui/gfx/text_utils.h" 38 #include "ui/gfx/text_utils.h"
38 39
39 #if defined(OS_WIN) 40 #if defined(OS_WIN)
40 #include "base/win/windows_version.h" 41 #include "base/win/windows_version.h"
41 #include "ui/gfx/platform_font_win.h" 42 #include "ui/gfx/platform_font_win.h"
(...skipping 1917 matching lines...) Expand 10 before | Expand all | Expand 10 after
1959 render_text->SetText(base::string16(length, 'a')); 1960 render_text->SetText(base::string16(length, 'a'));
1960 const Size string_size = render_text->GetStringSize(); 1961 const Size string_size = render_text->GetStringSize();
1961 EXPECT_GT(string_size.width(), previous_string_size.width()); 1962 EXPECT_GT(string_size.width(), previous_string_size.width());
1962 EXPECT_GT(string_size.height(), 0); 1963 EXPECT_GT(string_size.height(), 0);
1963 previous_string_size = string_size; 1964 previous_string_size = string_size;
1964 } 1965 }
1965 } 1966 }
1966 1967
1967 TEST_P(RenderTextTest, StringSizeEmptyString) { 1968 TEST_P(RenderTextTest, StringSizeEmptyString) {
1968 // Ascent and descent of Arial and Symbol are different on most platforms. 1969 // Ascent and descent of Arial and Symbol are different on most platforms.
1969 const FontList font_list("Arial,Symbol, 16px"); 1970 const FontList font_list(
1971 base::StringPrintf("Arial,%s, 16px", kSymbolFontName));
1970 RenderText* render_text = GetRenderText(); 1972 RenderText* render_text = GetRenderText();
1971 render_text->SetFontList(font_list); 1973 render_text->SetFontList(font_list);
1972 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight())); 1974 render_text->SetDisplayRect(Rect(0, 0, 0, font_list.GetHeight()));
1973 1975
1974 // The empty string respects FontList metrics for non-zero height 1976 // The empty string respects FontList metrics for non-zero height
1975 // and baseline. 1977 // and baseline.
1976 render_text->SetText(base::string16()); 1978 render_text->SetText(base::string16());
1977 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); 1979 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1978 EXPECT_EQ(0, render_text->GetStringSize().width()); 1980 EXPECT_EQ(0, render_text->GetStringSize().width());
1979 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline()); 1981 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1980 1982
1981 render_text->SetText(UTF8ToUTF16(" ")); 1983 render_text->SetText(UTF8ToUTF16(" "));
1982 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); 1984 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
1983 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline()); 1985 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
1984 } 1986 }
1985 1987
1986 TEST_P(RenderTextTest, StringSizeRespectsFontListMetrics) { 1988 TEST_P(RenderTextTest, StringSizeRespectsFontListMetrics) {
1987 // Check that Arial and Symbol have different font metrics. 1989 // Check that Arial and the CJK font have different font metrics.
1988 Font arial_font("Arial", 16); 1990 Font arial_font("Arial", 16);
1989 ASSERT_EQ("arial", 1991 ASSERT_EQ("arial",
1990 base::ToLowerASCII(arial_font.GetActualFontNameForTesting())); 1992 base::ToLowerASCII(arial_font.GetActualFontNameForTesting()));
1991 Font symbol_font("Symbol", 16); 1993 Font cjk_font(kCJKFontName, 16);
1992 ASSERT_EQ("symbol", 1994 ASSERT_EQ(base::ToLowerASCII(kCJKFontName),
1993 base::ToLowerASCII(symbol_font.GetActualFontNameForTesting())); 1995 base::ToLowerASCII(cjk_font.GetActualFontNameForTesting()));
1994 EXPECT_NE(arial_font.GetHeight(), symbol_font.GetHeight()); 1996 EXPECT_NE(arial_font.GetHeight(), cjk_font.GetHeight());
1995 EXPECT_NE(arial_font.GetBaseline(), symbol_font.GetBaseline()); 1997 EXPECT_NE(arial_font.GetBaseline(), cjk_font.GetBaseline());
1996 // "a" should be rendered with Arial, not with Symbol. 1998 // "a" should be rendered with Arial, not with the CJK font.
1997 const char* arial_font_text = "a"; 1999 const char* arial_font_text = "a";
1998 // "®" (registered trademark symbol) should be rendered with Symbol, 2000 // "" CJK UNIFIED IDEOGRAPH-5186, aka the Yen sign should be rendered with
1999 // not with Arial. 2001 // the CJK font, not with Arial.
2000 const char* symbol_font_text = "\xC2\xAE"; 2002 const char* cjk_font_text = "\xE5\x86\x86";
2001
2002 Font smaller_font = arial_font; 2003 Font smaller_font = arial_font;
2003 Font larger_font = symbol_font; 2004 Font larger_font = cjk_font;
2004 const char* smaller_font_text = arial_font_text; 2005 const char* smaller_font_text = arial_font_text;
2005 const char* larger_font_text = symbol_font_text; 2006 const char* larger_font_text = cjk_font_text;
2006 if (symbol_font.GetHeight() < arial_font.GetHeight() && 2007 if (cjk_font.GetHeight() < arial_font.GetHeight() &&
2007 symbol_font.GetBaseline() < arial_font.GetBaseline()) { 2008 cjk_font.GetBaseline() < arial_font.GetBaseline()) {
2008 std::swap(smaller_font, larger_font); 2009 std::swap(smaller_font, larger_font);
2009 std::swap(smaller_font_text, larger_font_text); 2010 std::swap(smaller_font_text, larger_font_text);
2010 } 2011 }
2011 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight()); 2012 ASSERT_LT(smaller_font.GetHeight(), larger_font.GetHeight());
2012 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline()); 2013 ASSERT_LT(smaller_font.GetBaseline(), larger_font.GetBaseline());
2013 2014
2014 // Check |smaller_font_text| is rendered with the smaller font. 2015 // Check |smaller_font_text| is rendered with the smaller font.
2015 RenderText* render_text = GetRenderText(); 2016 RenderText* render_text = GetRenderText();
2016 render_text->SetText(UTF8ToUTF16(smaller_font_text)); 2017 render_text->SetText(UTF8ToUTF16(smaller_font_text));
2017 render_text->SetFontList(FontList(smaller_font)); 2018 render_text->SetFontList(FontList(smaller_font));
2018 render_text->SetDisplayRect(Rect(0, 0, 0, 2019 render_text->SetDisplayRect(Rect(0, 0, 0,
2019 render_text->font_list().GetHeight())); 2020 render_text->font_list().GetHeight()));
2020 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height()); 2021 EXPECT_EQ(smaller_font.GetHeight(), render_text->GetStringSize().height());
2021 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline()); 2022 EXPECT_EQ(smaller_font.GetBaseline(), render_text->GetBaseline());
2023 EXPECT_STRCASEEQ(
2024 render_text->GetFontSpansForTesting()[0].first.GetFontName().c_str(),
2025 smaller_font.GetFontName().c_str());
2022 2026
2023 // Layout the same text with mixed fonts. The text should be rendered with 2027 // Layout the same text with mixed fonts. The text should be rendered with
2024 // the smaller font, but the height and baseline are determined with the 2028 // the smaller font, but the height and baseline are determined with the
2025 // metrics of the font list, which is equal to the larger font. 2029 // metrics of the font list, which is equal to the larger font.
2026 std::vector<Font> fonts; 2030 std::vector<Font> fonts;
2027 fonts.push_back(smaller_font); // The primary font is the smaller font. 2031 fonts.push_back(smaller_font); // The primary font is the smaller font.
2028 fonts.push_back(larger_font); 2032 fonts.push_back(larger_font);
2029 const FontList font_list(fonts); 2033 const FontList font_list(fonts);
2030 render_text->SetFontList(font_list); 2034 render_text->SetFontList(font_list);
2031 render_text->SetDisplayRect(Rect(0, 0, 0, 2035 render_text->SetDisplayRect(Rect(0, 0, 0,
2032 render_text->font_list().GetHeight())); 2036 render_text->font_list().GetHeight()));
2037 EXPECT_STRCASEEQ(
2038 render_text->GetFontSpansForTesting()[0].first.GetFontName().c_str(),
2039 smaller_font.GetFontName().c_str());
2033 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height()); 2040 EXPECT_LT(smaller_font.GetHeight(), render_text->GetStringSize().height());
2034 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline()); 2041 EXPECT_LT(smaller_font.GetBaseline(), render_text->GetBaseline());
2035 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height()); 2042 EXPECT_EQ(font_list.GetHeight(), render_text->GetStringSize().height());
2036 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline()); 2043 EXPECT_EQ(font_list.GetBaseline(), render_text->GetBaseline());
2037 } 2044 }
2038 2045
2039 TEST_P(RenderTextTest, MinLineHeight) { 2046 TEST_P(RenderTextTest, MinLineHeight) {
2040 RenderText* render_text = GetRenderText(); 2047 RenderText* render_text = GetRenderText();
2041 render_text->SetText(ASCIIToUTF16("Hello!")); 2048 render_text->SetText(ASCIIToUTF16("Hello!"));
2042 SizeF default_size = render_text->GetStringSizeF(); 2049 SizeF default_size = render_text->GetStringSizeF();
2043 ASSERT_NE(0, default_size.height()); 2050 ASSERT_NE(0, default_size.height());
2044 ASSERT_NE(0, default_size.width()); 2051 ASSERT_NE(0, default_size.width());
2045 2052
2046 render_text->SetMinLineHeight(default_size.height() / 2); 2053 render_text->SetMinLineHeight(default_size.height() / 2);
2047 EXPECT_EQ(default_size.ToString(), render_text->GetStringSizeF().ToString()); 2054 EXPECT_EQ(default_size.ToString(), render_text->GetStringSizeF().ToString());
2048 2055
2049 render_text->SetMinLineHeight(default_size.height() * 2); 2056 render_text->SetMinLineHeight(default_size.height() * 2);
2050 SizeF taller_size = render_text->GetStringSizeF(); 2057 SizeF taller_size = render_text->GetStringSizeF();
2051 EXPECT_EQ(default_size.height() * 2, taller_size.height()); 2058 EXPECT_EQ(default_size.height() * 2, taller_size.height());
2052 EXPECT_EQ(default_size.width(), taller_size.width()); 2059 EXPECT_EQ(default_size.width(), taller_size.width());
2053 } 2060 }
2054 2061
2055 TEST_P(RenderTextTest, SetFontList) { 2062 TEST_P(RenderTextTest, SetFontList) {
2056 RenderText* render_text = GetRenderText(); 2063 RenderText* render_text = GetRenderText();
2057 render_text->SetFontList(FontList("Arial,Symbol, 13px")); 2064 render_text->SetFontList(
2065 FontList(base::StringPrintf("Arial,%s, 13px", kSymbolFontName)));
2058 const std::vector<Font>& fonts = render_text->font_list().GetFonts(); 2066 const std::vector<Font>& fonts = render_text->font_list().GetFonts();
2059 ASSERT_EQ(2U, fonts.size()); 2067 ASSERT_EQ(2U, fonts.size());
2060 EXPECT_EQ("Arial", fonts[0].GetFontName()); 2068 EXPECT_EQ("Arial", fonts[0].GetFontName());
2061 EXPECT_EQ("Symbol", fonts[1].GetFontName()); 2069 EXPECT_EQ(kSymbolFontName, fonts[1].GetFontName());
2062 EXPECT_EQ(13, render_text->font_list().GetFontSize()); 2070 EXPECT_EQ(13, render_text->font_list().GetFontSize());
2063 } 2071 }
2064 2072
2065 // http://crbug/624513 2073 // http://crbug/624513
2066 #if !defined(OS_WIN) 2074 #if !defined(OS_WIN)
2067 TEST_P(RenderTextTest, StringSizeBoldWidth) { 2075 TEST_P(RenderTextTest, StringSizeBoldWidth) {
2068 // TODO(mboc): Add some unittests for other weights (currently not 2076 // TODO(mboc): Add some unittests for other weights (currently not
2069 // implemented because of test system font configuration). 2077 // implemented because of test system font configuration).
2070 RenderText* render_text = GetRenderText(); 2078 RenderText* render_text = GetRenderText();
2071 render_text->SetText(UTF8ToUTF16("Hello World")); 2079 render_text->SetText(UTF8ToUTF16("Hello World"));
(...skipping 1315 matching lines...) Expand 10 before | Expand all | Expand 10 after
3387 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100)); 3395 render_text->SetDisplayRect(Rect(0, 0, render_text->GetContentWidth(), 100));
3388 EXPECT_EQ(kString, render_text->GetDisplayText()); 3396 EXPECT_EQ(kString, render_text->GetDisplayText());
3389 } 3397 }
3390 3398
3391 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184 3399 // TODO(derat): Figure out why this fails on Windows: http://crbug.com/427184
3392 #if !defined(OS_WIN) 3400 #if !defined(OS_WIN)
3393 // Ensure that RenderText examines all of the fonts in its FontList before 3401 // Ensure that RenderText examines all of the fonts in its FontList before
3394 // falling back to other fonts. 3402 // falling back to other fonts.
3395 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_FontListFallback) { 3403 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_FontListFallback) {
3396 // Double-check that the requested fonts are present. 3404 // Double-check that the requested fonts are present.
3397 FontList font_list("Arial, Symbol, 12px"); 3405 FontList font_list(base::StringPrintf("Arial, %s, 12px", kSymbolFontName));
3398 const std::vector<Font>& fonts = font_list.GetFonts(); 3406 const std::vector<Font>& fonts = font_list.GetFonts();
3399 ASSERT_EQ(2u, fonts.size()); 3407 ASSERT_EQ(2u, fonts.size());
3400 ASSERT_EQ("arial", 3408 ASSERT_EQ("arial",
3401 base::ToLowerASCII(fonts[0].GetActualFontNameForTesting())); 3409 base::ToLowerASCII(fonts[0].GetActualFontNameForTesting()));
3402 ASSERT_EQ("symbol", 3410 ASSERT_EQ(base::ToLowerASCII(kSymbolFontName),
3403 base::ToLowerASCII(fonts[1].GetActualFontNameForTesting())); 3411 base::ToLowerASCII(fonts[1].GetActualFontNameForTesting()));
3404 3412
3405 // "⊕" (CIRCLED PLUS) should be rendered with Symbol rather than falling back 3413 // "⊕" (CIRCLED PLUS) should be rendered with Symbol rather than falling back
3406 // to some other font that's present on the system. 3414 // to some other font that's present on the system.
3407 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz(); 3415 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz();
3408 render_text->SetFontList(font_list); 3416 render_text->SetFontList(font_list);
3409 render_text->SetText(UTF8ToUTF16("\xE2\x8A\x95")); 3417 render_text->SetText(UTF8ToUTF16("\xE2\x8A\x95"));
3410 const std::vector<RenderText::FontSpan> spans = 3418 const std::vector<RenderText::FontSpan> spans =
3411 render_text->GetFontSpansForTesting(); 3419 render_text->GetFontSpansForTesting();
3412 ASSERT_EQ(static_cast<size_t>(1), spans.size()); 3420 ASSERT_EQ(static_cast<size_t>(1), spans.size());
3413 EXPECT_EQ("Symbol", spans[0].first.GetFontName()); 3421 EXPECT_STRCASEEQ(kSymbolFontName, spans[0].first.GetFontName().c_str());
3414 } 3422 }
3415 #endif // !defined(OS_WIN) 3423 #endif // !defined(OS_WIN)
3416 3424
3417 // Ensure that the fallback fonts of the Uniscribe font are tried for shaping. 3425 // Ensure that the fallback fonts of the Uniscribe font are tried for shaping.
3418 #if defined(OS_WIN) 3426 #if defined(OS_WIN)
3419 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_UniscribeFallback) { 3427 TEST_P(RenderTextHarfBuzzTest, HarfBuzz_UniscribeFallback) {
3420 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz(); 3428 RenderTextHarfBuzz* render_text = GetRenderTextHarfBuzz();
3421 PlatformFontWin* font_win = new PlatformFontWin("Meiryo", 12); 3429 PlatformFontWin* font_win = new PlatformFontWin("Meiryo", 12);
3422 // Japanese name for Meiryo. This name won't be found in the system's linked 3430 // Japanese name for Meiryo. This name won't be found in the system's linked
3423 // fonts, forcing RTHB to try the Uniscribe font and its fallbacks. 3431 // fonts, forcing RTHB to try the Uniscribe font and its fallbacks.
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
3738 ::testing::Values(RENDER_TEXT_HARFBUZZ), 3746 ::testing::Values(RENDER_TEXT_HARFBUZZ),
3739 PrintRenderTextBackend()); 3747 PrintRenderTextBackend());
3740 #endif 3748 #endif
3741 3749
3742 INSTANTIATE_TEST_CASE_P(, 3750 INSTANTIATE_TEST_CASE_P(,
3743 RenderTextHarfBuzzTest, 3751 RenderTextHarfBuzzTest,
3744 ::testing::Values(RENDER_TEXT_HARFBUZZ), 3752 ::testing::Values(RENDER_TEXT_HARFBUZZ),
3745 PrintRenderTextBackend()); 3753 PrintRenderTextBackend());
3746 3754
3747 } // namespace gfx 3755 } // namespace gfx
OLDNEW
« ui/gfx/font_names_testing.cc ('K') | « ui/gfx/font_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698