Home | History | Annotate | Download | only in gfx
      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
      3 // found in the LICENSE file.
      4 
      5 #include "ui/gfx/font.h"
      6 
      7 #include "base/strings/string16.h"
      8 #include "base/strings/utf_string_conversions.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
     12 #include <pango/pango.h>
     13 #elif defined(OS_WIN)
     14 #include "ui/gfx/platform_font_win.h"
     15 #endif
     16 
     17 namespace gfx {
     18 namespace {
     19 
     20 class FontTest : public testing::Test {
     21  public:
     22   // Fulfills the memory management contract as outlined by the comment at
     23   // gfx::Font::GetNativeFont().
     24   void FreeIfNecessary(NativeFont font) {
     25 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
     26     pango_font_description_free(font);
     27 #endif
     28   }
     29 };
     30 
     31 #if defined(OS_WIN)
     32 class ScopedMinimumFontSizeCallback {
     33  public:
     34   explicit ScopedMinimumFontSizeCallback(int minimum_size) {
     35     minimum_size_ = minimum_size;
     36     old_callback_ = PlatformFontWin::get_minimum_font_size_callback;
     37     PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize;
     38   }
     39 
     40   ~ScopedMinimumFontSizeCallback() {
     41     PlatformFontWin::get_minimum_font_size_callback = old_callback_;
     42   }
     43 
     44  private:
     45   static int GetMinimumFontSize() {
     46     return minimum_size_;
     47   }
     48 
     49   PlatformFontWin::GetMinimumFontSizeCallback old_callback_;
     50   static int minimum_size_;
     51 
     52   DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback);
     53 };
     54 
     55 int ScopedMinimumFontSizeCallback::minimum_size_ = 0;
     56 #endif  // defined(OS_WIN)
     57 
     58 
     59 TEST_F(FontTest, LoadArial) {
     60   Font cf("Arial", 16);
     61   NativeFont native = cf.GetNativeFont();
     62   ASSERT_TRUE(native);
     63   ASSERT_EQ(cf.GetStyle(), Font::NORMAL);
     64   ASSERT_EQ(cf.GetFontSize(), 16);
     65   ASSERT_EQ(cf.GetFontName(), "Arial");
     66   FreeIfNecessary(native);
     67 }
     68 
     69 TEST_F(FontTest, LoadArialBold) {
     70   Font cf("Arial", 16);
     71   Font bold(cf.DeriveFont(0, Font::BOLD));
     72   NativeFont native = bold.GetNativeFont();
     73   ASSERT_TRUE(native);
     74   ASSERT_EQ(bold.GetStyle(), Font::BOLD);
     75   FreeIfNecessary(native);
     76 }
     77 
     78 TEST_F(FontTest, Ascent) {
     79   Font cf("Arial", 16);
     80   ASSERT_GT(cf.GetBaseline(), 2);
     81   ASSERT_LE(cf.GetBaseline(), 22);
     82 }
     83 
     84 TEST_F(FontTest, Height) {
     85   Font cf("Arial", 16);
     86   ASSERT_GE(cf.GetHeight(), 16);
     87   // TODO(akalin): Figure out why height is so large on Linux.
     88   ASSERT_LE(cf.GetHeight(), 26);
     89 }
     90 
     91 TEST_F(FontTest, AvgWidths) {
     92   Font cf("Arial", 16);
     93   ASSERT_EQ(cf.GetExpectedTextWidth(0), 0);
     94   ASSERT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0));
     95   ASSERT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1));
     96   ASSERT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2));
     97 }
     98 
     99 TEST_F(FontTest, AvgCharWidth) {
    100   Font cf("Arial", 16);
    101   ASSERT_GT(cf.GetAverageCharacterWidth(), 0);
    102 }
    103 
    104 TEST_F(FontTest, Widths) {
    105   Font cf("Arial", 16);
    106   ASSERT_EQ(cf.GetStringWidth(base::string16()), 0);
    107   ASSERT_GT(cf.GetStringWidth(ASCIIToUTF16("a")),
    108             cf.GetStringWidth(base::string16()));
    109   ASSERT_GT(cf.GetStringWidth(ASCIIToUTF16("ab")),
    110             cf.GetStringWidth(ASCIIToUTF16("a")));
    111   ASSERT_GT(cf.GetStringWidth(ASCIIToUTF16("abc")),
    112             cf.GetStringWidth(ASCIIToUTF16("ab")));
    113 }
    114 
    115 #if defined(OS_WIN)
    116 TEST_F(FontTest, DeriveFontResizesIfSizeTooSmall) {
    117   Font cf("Arial", 8);
    118   // The minimum font size is set to 5 in browser_main.cc.
    119   ScopedMinimumFontSizeCallback minimum_size(5);
    120 
    121   Font derived_font = cf.DeriveFont(-4);
    122   EXPECT_EQ(5, derived_font.GetFontSize());
    123 }
    124 
    125 TEST_F(FontTest, DeriveFontKeepsOriginalSizeIfHeightOk) {
    126   Font cf("Arial", 8);
    127   // The minimum font size is set to 5 in browser_main.cc.
    128   ScopedMinimumFontSizeCallback minimum_size(5);
    129 
    130   Font derived_font = cf.DeriveFont(-2);
    131   EXPECT_EQ(6, derived_font.GetFontSize());
    132 }
    133 #endif  // defined(OS_WIN)
    134 
    135 }  // namespace
    136 }  // namespace gfx
    137