Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/prefs/pref_registry_simple.h" | |
| 8 #include "base/prefs/testing_pref_service.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 class TestingFontFamilyCache : public chrome::FontFamilyCache { | |
| 15 public: | |
| 16 explicit TestingFontFamilyCache(PrefService* prefs) | |
| 17 : chrome::FontFamilyCache(prefs), fetch_font_count_(0) {} | |
| 18 virtual ~TestingFontFamilyCache() {} | |
| 19 virtual base::string16 FetchFont(const char* script, | |
| 20 const char* map_name) OVERRIDE { | |
| 21 ++fetch_font_count_; | |
| 22 return chrome::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. | |
| 34 TEST(FontFamilyCacheTest, Caching) { | |
| 35 TestingPrefServiceSimple prefs; | |
| 36 TestingFontFamilyCache cache(&prefs); | |
| 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("Syrn"); | |
| 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(pref_name.c_str(), std::string()); | |
| 47 prefs.registry()->RegisterStringPref(pref_name2.c_str(), std::string()); | |
| 48 prefs.SetString(pref_name.c_str(), font1.c_str()); | |
| 49 | |
| 50 // Check that the right preference is returned. | |
| 51 std::string result = base::UTF16ToUTF8( | |
| 52 cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); | |
| 53 EXPECT_EQ(result, font1); | |
|
Lei Zhang
2014/08/05 19:26:49
nit: EXPECT_EQ(expected, actual)
Otherwise if thi
erikchen
2014/08/05 20:49:23
Done.
| |
| 54 EXPECT_EQ(cache.fetch_font_count_, 1); | |
| 55 | |
| 56 // Check that the second access uses the cache. | |
| 57 result = base::UTF16ToUTF8( | |
| 58 cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); | |
| 59 EXPECT_EQ(result, font1); | |
| 60 EXPECT_EQ(cache.fetch_font_count_, 1); | |
| 61 | |
| 62 // Changing another preference should have no effect. | |
| 63 prefs.SetString(pref_name2.c_str(), "katy perry"); | |
| 64 result = base::UTF16ToUTF8( | |
| 65 cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); | |
| 66 EXPECT_EQ(result, font1); | |
| 67 EXPECT_EQ(cache.fetch_font_count_, 1); | |
| 68 | |
| 69 // Changing the preferences invalidates the cache. | |
| 70 prefs.SetString(pref_name.c_str(), font2.c_str()); | |
| 71 result = base::UTF16ToUTF8( | |
| 72 cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); | |
| 73 EXPECT_EQ(result, font2); | |
| 74 EXPECT_EQ(cache.fetch_font_count_, 2); | |
| 75 } | |
| OLD | NEW |