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_fallback_win.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 
      8 namespace gfx {
      9 
     10 namespace {
     11 
     12 // Subclass of LinkedFontsIterator for testing that allows mocking the linked
     13 // fonts vector.
     14 class TestLinkedFontsIterator : public LinkedFontsIterator {
     15  public:
     16   explicit TestLinkedFontsIterator(Font font) : LinkedFontsIterator(font) {
     17   }
     18 
     19   virtual ~TestLinkedFontsIterator() {
     20   }
     21 
     22   // Add a linked font to the mocked vector of linked fonts.
     23   void AddLinkedFontForTesting(Font font) {
     24     test_linked_fonts.push_back(font);
     25   }
     26 
     27   virtual const std::vector<Font>* GetLinkedFonts() const OVERRIDE {
     28     return &test_linked_fonts;
     29   }
     30 
     31  private:
     32   std::vector<Font> test_linked_fonts;
     33 
     34   DISALLOW_COPY_AND_ASSIGN(TestLinkedFontsIterator);
     35 };
     36 
     37 }  // namespace
     38 
     39 TEST(FontFallbackWinTest, ParseFontLinkEntry) {
     40   std::string file;
     41   std::string font;
     42 
     43   internal::ParseFontLinkEntry("TAHOMA.TTF", &file, &font);
     44   EXPECT_EQ("TAHOMA.TTF", file);
     45   EXPECT_EQ("", font);
     46 
     47   internal::ParseFontLinkEntry("MSGOTHIC.TTC,MS UI Gothic", &file, &font);
     48   EXPECT_EQ("MSGOTHIC.TTC", file);
     49   EXPECT_EQ("MS UI Gothic", font);
     50 
     51   internal::ParseFontLinkEntry("MALGUN.TTF,128,96", &file, &font);
     52   EXPECT_EQ("MALGUN.TTF", file);
     53   EXPECT_EQ("", font);
     54 
     55   internal::ParseFontLinkEntry("MEIRYO.TTC,Meiryo,128,85", &file, &font);
     56   EXPECT_EQ("MEIRYO.TTC", file);
     57   EXPECT_EQ("Meiryo", font);
     58 }
     59 
     60 TEST(FontFallbackWinTest, ParseFontFamilyString) {
     61   std::vector<std::string> font_names;
     62 
     63   internal::ParseFontFamilyString("Times New Roman (TrueType)", &font_names);
     64   ASSERT_EQ(1U, font_names.size());
     65   EXPECT_EQ("Times New Roman", font_names[0]);
     66   font_names.clear();
     67 
     68   internal::ParseFontFamilyString("Cambria & Cambria Math (TrueType)",
     69                                   &font_names);
     70   ASSERT_EQ(2U, font_names.size());
     71   EXPECT_EQ("Cambria", font_names[0]);
     72   EXPECT_EQ("Cambria Math", font_names[1]);
     73   font_names.clear();
     74 
     75   internal::ParseFontFamilyString(
     76       "Meiryo & Meiryo Italic & Meiryo UI & Meiryo UI Italic (TrueType)",
     77       &font_names);
     78   ASSERT_EQ(4U, font_names.size());
     79   EXPECT_EQ("Meiryo", font_names[0]);
     80   EXPECT_EQ("Meiryo Italic", font_names[1]);
     81   EXPECT_EQ("Meiryo UI", font_names[2]);
     82   EXPECT_EQ("Meiryo UI Italic", font_names[3]);
     83 }
     84 
     85 TEST(FontFallbackWinTest, LinkedFontsIterator) {
     86   TestLinkedFontsIterator iterator(Font("Arial", 16));
     87   iterator.AddLinkedFontForTesting(Font("Times New Roman", 16));
     88 
     89   Font font;
     90   EXPECT_TRUE(iterator.NextFont(&font));
     91   ASSERT_EQ("Arial", font.GetFontName());
     92 
     93   EXPECT_TRUE(iterator.NextFont(&font));
     94   ASSERT_EQ("Times New Roman", font.GetFontName());
     95 
     96   EXPECT_FALSE(iterator.NextFont(&font));
     97 }
     98 
     99 TEST(FontFallbackWinTest, LinkedFontsIteratorSetNextFont) {
    100   TestLinkedFontsIterator iterator(Font("Arial", 16));
    101   iterator.AddLinkedFontForTesting(Font("Times New Roman", 16));
    102 
    103   Font font;
    104   EXPECT_TRUE(iterator.NextFont(&font));
    105   ASSERT_EQ("Arial", font.GetFontName());
    106 
    107   iterator.SetNextFont(Font("Tahoma", 16));
    108   EXPECT_TRUE(iterator.NextFont(&font));
    109   ASSERT_EQ("Tahoma", font.GetFontName());
    110 
    111   EXPECT_TRUE(iterator.NextFont(&font));
    112   ASSERT_EQ("Times New Roman", font.GetFontName());
    113 
    114   EXPECT_FALSE(iterator.NextFont(&font));
    115 }
    116 
    117 }  // namespace gfx
    118