Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / font_family_cache_unittest.cc
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:
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);
22   }
23
24   int fetch_font_count_;
25
26  private:
27   DISALLOW_COPY_AND_ASSIGN(TestingFontFamilyCache);
28 };
29
30 }  // namespace
31
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();
37
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");
44
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());
55
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_);
61
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_);
67
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_);
74
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_);
81 }