Index: chrome/browser/font_family_cache_unittest.cc |
diff --git a/chrome/browser/font_family_cache_unittest.cc b/chrome/browser/font_family_cache_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6cfe5c3483ebe6197c588c6ed201bc86d3aa4875 |
--- /dev/null |
+++ b/chrome/browser/font_family_cache_unittest.cc |
@@ -0,0 +1,75 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/font_family_cache.h" |
+ |
+#include "base/prefs/pref_registry_simple.h" |
+#include "base/prefs/testing_pref_service.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+class TestingFontFamilyCache : public chrome::FontFamilyCache { |
+ public: |
+ explicit TestingFontFamilyCache(PrefService* prefs) |
+ : chrome::FontFamilyCache(prefs), fetch_font_count_(0) {} |
+ virtual ~TestingFontFamilyCache() {} |
+ virtual base::string16 FetchFont(const char* script, |
+ const char* map_name) OVERRIDE { |
+ ++fetch_font_count_; |
+ return chrome::FontFamilyCache::FetchFont(script, map_name); |
+ } |
+ |
+ int fetch_font_count_; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(TestingFontFamilyCache); |
+}; |
+ |
+} // namespace |
+ |
+// Tests that the cache is correctly set and cleared. |
+TEST(FontFamilyCacheTest, Caching) { |
+ TestingPrefServiceSimple prefs; |
+ TestingFontFamilyCache cache(&prefs); |
+ |
+ std::string font1("font 1"); |
+ std::string font2("font 2"); |
+ std::string map_name("webkit.webprefs.fonts.pictograph"); |
+ std::string script("Syrn"); |
+ std::string pref_name(map_name + '.' + script); |
+ std::string pref_name2(map_name + '.' + "adsf"); |
+ |
+ // Registers 2 preferences, and sets the first one. |
+ prefs.registry()->RegisterStringPref(pref_name.c_str(), std::string()); |
+ prefs.registry()->RegisterStringPref(pref_name2.c_str(), std::string()); |
+ prefs.SetString(pref_name.c_str(), font1.c_str()); |
+ |
+ // Check that the right preference is returned. |
+ std::string result = base::UTF16ToUTF8( |
+ cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); |
+ 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.
|
+ EXPECT_EQ(cache.fetch_font_count_, 1); |
+ |
+ // Check that the second access uses the cache. |
+ result = base::UTF16ToUTF8( |
+ cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); |
+ EXPECT_EQ(result, font1); |
+ EXPECT_EQ(cache.fetch_font_count_, 1); |
+ |
+ // Changing another preference should have no effect. |
+ prefs.SetString(pref_name2.c_str(), "katy perry"); |
+ result = base::UTF16ToUTF8( |
+ cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); |
+ EXPECT_EQ(result, font1); |
+ EXPECT_EQ(cache.fetch_font_count_, 1); |
+ |
+ // Changing the preferences invalidates the cache. |
+ prefs.SetString(pref_name.c_str(), font2.c_str()); |
+ result = base::UTF16ToUTF8( |
+ cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); |
+ EXPECT_EQ(result, font2); |
+ EXPECT_EQ(cache.fetch_font_count_, 2); |
+} |