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.
5 #include "chrome/browser/font_family_cache.h"
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 "content/public/test/test_browser_thread_bundle.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class TestingFontFamilyCache
: public FontFamilyCache
{
17 explicit TestingFontFamilyCache(Profile
* profile
)
18 : FontFamilyCache(profile
), fetch_font_count_(0) {}
19 ~TestingFontFamilyCache() override
{}
20 base::string16
FetchFont(const char* script
, const char* map_name
) override
{
22 return FontFamilyCache::FetchFont(script
, map_name
);
25 int fetch_font_count_
;
28 DISALLOW_COPY_AND_ASSIGN(TestingFontFamilyCache
);
33 // Tests that the cache is correctly set and cleared.
34 TEST(FontFamilyCacheTest
, Caching
) {
35 content::TestBrowserThreadBundle thread_bundle_
;
36 TestingProfile profile
;
37 TestingFontFamilyCache
cache(&profile
);
38 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
40 std::string
font1("font 1");
41 std::string
font2("font 2");
42 std::string
map_name("webkit.webprefs.fonts.pictograph");
43 std::string
script("Zzyxca");
44 std::string
pref_name(map_name
+ '.' + script
);
45 std::string
pref_name2(map_name
+ '.' + "adsf");
47 // Registers 2 preferences, and sets the first one.
48 prefs
->registry()->RegisterStringPref(pref_name
.c_str(), std::string());
49 prefs
->registry()->RegisterStringPref(pref_name2
.c_str(), std::string());
50 prefs
->SetString(pref_name
.c_str(), font1
.c_str());
52 // Check that the right preference is returned.
53 std::string result
= base::UTF16ToUTF8(
54 cache
.FetchAndCacheFont(script
.c_str(), map_name
.c_str()));
55 EXPECT_EQ(font1
, result
);
56 EXPECT_EQ(1, cache
.fetch_font_count_
);
58 // Check that the second access uses the cache.
59 result
= base::UTF16ToUTF8(
60 cache
.FetchAndCacheFont(script
.c_str(), map_name
.c_str()));
61 EXPECT_EQ(font1
, result
);
62 EXPECT_EQ(1, cache
.fetch_font_count_
);
64 // Changing another preference should have no effect.
65 prefs
->SetString(pref_name2
.c_str(), "katy perry");
66 result
= base::UTF16ToUTF8(
67 cache
.FetchAndCacheFont(script
.c_str(), map_name
.c_str()));
68 EXPECT_EQ(font1
, result
);
69 EXPECT_EQ(1, cache
.fetch_font_count_
);
71 // Changing the preferences invalidates the cache.
72 prefs
->SetString(pref_name
.c_str(), font2
.c_str());
73 result
= base::UTF16ToUTF8(
74 cache
.FetchAndCacheFont(script
.c_str(), map_name
.c_str()));
75 EXPECT_EQ(font2
, result
);
76 EXPECT_EQ(2, cache
.fetch_font_count_
);