1 // Copyright 2014 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 "chrome/browser/font_family_cache.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/test/base/testing_pref_service_syncable.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace {
13
14 class TestingFontFamilyCache : public FontFamilyCache {
15 public:
TestingFontFamilyCache(Profile * profile)16 explicit TestingFontFamilyCache(Profile* profile)
17 : FontFamilyCache(profile), fetch_font_count_(0) {}
~TestingFontFamilyCache()18 virtual ~TestingFontFamilyCache() {}
FetchFont(const char * script,const char * map_name)19 virtual base::string16 FetchFont(const char* script,
20 const char* map_name) OVERRIDE {
21 ++fetch_font_count_;
22 return FontFamilyCache::FetchFont(script, map_name);
23 }
24
25 int fetch_font_count_;
26
27 private:
28 DISALLOW_COPY_AND_ASSIGN(TestingFontFamilyCache);
29 };
30
31 } // namespace
32
33 // Tests that the cache is correctly set and cleared.
TEST(FontFamilyCacheTest,Caching)34 TEST(FontFamilyCacheTest, Caching) {
35 TestingProfile profile;
36 TestingFontFamilyCache cache(&profile);
37 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
38
39 std::string font1("font 1");
40 std::string font2("font 2");
41 std::string map_name("webkit.webprefs.fonts.pictograph");
42 std::string script("Zzyxca");
43 std::string pref_name(map_name + '.' + script);
44 std::string pref_name2(map_name + '.' + "adsf");
45
46 // Registers 2 preferences, and sets the first one.
47 prefs->registry()->RegisterStringPref(
48 pref_name.c_str(),
49 std::string(),
50 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
51 prefs->registry()->RegisterStringPref(
52 pref_name2.c_str(),
53 std::string(),
54 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
55 prefs->SetString(pref_name.c_str(), font1.c_str());
56
57 // Check that the right preference is returned.
58 std::string result = base::UTF16ToUTF8(
59 cache.FetchAndCacheFont(script.c_str(), map_name.c_str()));
60 EXPECT_EQ(font1, result);
61 EXPECT_EQ(1, cache.fetch_font_count_);
62
63 // Check that the second access uses the cache.
64 result = base::UTF16ToUTF8(
65 cache.FetchAndCacheFont(script.c_str(), map_name.c_str()));
66 EXPECT_EQ(font1, result);
67 EXPECT_EQ(1, cache.fetch_font_count_);
68
69 // Changing another preference should have no effect.
70 prefs->SetString(pref_name2.c_str(), "katy perry");
71 result = base::UTF16ToUTF8(
72 cache.FetchAndCacheFont(script.c_str(), map_name.c_str()));
73 EXPECT_EQ(font1, result);
74 EXPECT_EQ(1, cache.fetch_font_count_);
75
76 // Changing the preferences invalidates the cache.
77 prefs->SetString(pref_name.c_str(), font2.c_str());
78 result = base::UTF16ToUTF8(
79 cache.FetchAndCacheFont(script.c_str(), map_name.c_str()));
80 EXPECT_EQ(font2, result);
81 EXPECT_EQ(2, cache.fetch_font_count_);
82 }
83