Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / chrome / browser / font_family_cache_unittest.cc
blobb7f8925deb27039218b382cf2984b5aa38fe9830
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 "testing/gtest/include/gtest/gtest.h"
12 namespace {
14 class TestingFontFamilyCache : public FontFamilyCache {
15 public:
16 explicit TestingFontFamilyCache(Profile* profile)
17 : FontFamilyCache(profile), fetch_font_count_(0) {}
18 ~TestingFontFamilyCache() override {}
19 base::string16 FetchFont(const char* script, const char* map_name) override {
20 ++fetch_font_count_;
21 return FontFamilyCache::FetchFont(script, map_name);
24 int fetch_font_count_;
26 private:
27 DISALLOW_COPY_AND_ASSIGN(TestingFontFamilyCache);
30 } // namespace
32 // Tests that the cache is correctly set and cleared.
33 TEST(FontFamilyCacheTest, Caching) {
34 TestingProfile profile;
35 TestingFontFamilyCache cache(&profile);
36 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
38 std::string font1("font 1");
39 std::string font2("font 2");
40 std::string map_name("webkit.webprefs.fonts.pictograph");
41 std::string script("Zzyxca");
42 std::string pref_name(map_name + '.' + script);
43 std::string pref_name2(map_name + '.' + "adsf");
45 // Registers 2 preferences, and sets the first one.
46 prefs->registry()->RegisterStringPref(
47 pref_name.c_str(),
48 std::string(),
49 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
50 prefs->registry()->RegisterStringPref(
51 pref_name2.c_str(),
52 std::string(),
53 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
54 prefs->SetString(pref_name.c_str(), font1.c_str());
56 // Check that the right preference is returned.
57 std::string result = base::UTF16ToUTF8(
58 cache.FetchAndCacheFont(script.c_str(), map_name.c_str()));
59 EXPECT_EQ(font1, result);
60 EXPECT_EQ(1, cache.fetch_font_count_);
62 // Check that the second access uses the cache.
63 result = base::UTF16ToUTF8(
64 cache.FetchAndCacheFont(script.c_str(), map_name.c_str()));
65 EXPECT_EQ(font1, result);
66 EXPECT_EQ(1, cache.fetch_font_count_);
68 // Changing another preference should have no effect.
69 prefs->SetString(pref_name2.c_str(), "katy perry");
70 result = base::UTF16ToUTF8(
71 cache.FetchAndCacheFont(script.c_str(), map_name.c_str()));
72 EXPECT_EQ(font1, result);
73 EXPECT_EQ(1, cache.fetch_font_count_);
75 // Changing the preferences invalidates the cache.
76 prefs->SetString(pref_name.c_str(), font2.c_str());
77 result = base::UTF16ToUTF8(
78 cache.FetchAndCacheFont(script.c_str(), map_name.c_str()));
79 EXPECT_EQ(font2, result);
80 EXPECT_EQ(2, cache.fetch_font_count_);