[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 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 | |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 7 | #include "base/macros.h" |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 9 | #include "chrome/test/base/testing_profile.h" |
maxbogue | ea16ff41 | 2016-10-28 16:35:29 | [diff] [blame] | 10 | #include "components/sync_preferences/testing_pref_service_syncable.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 11 | #include "content/public/test/browser_task_environment.h" |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | class TestingFontFamilyCache : public FontFamilyCache { |
| 17 | public: |
| 18 | explicit TestingFontFamilyCache(Profile* profile) |
| 19 | : FontFamilyCache(profile), fetch_font_count_(0) {} |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 20 | ~TestingFontFamilyCache() override {} |
| 21 | base::string16 FetchFont(const char* script, const char* map_name) override { |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 22 | ++fetch_font_count_; |
| 23 | return FontFamilyCache::FetchFont(script, map_name); |
| 24 | } |
| 25 | |
| 26 | int fetch_font_count_; |
| 27 | |
| 28 | private: |
| 29 | DISALLOW_COPY_AND_ASSIGN(TestingFontFamilyCache); |
| 30 | }; |
| 31 | |
| 32 | } // namespace |
| 33 | |
| 34 | // Tests that the cache is correctly set and cleared. |
| 35 | TEST(FontFamilyCacheTest, Caching) { |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 36 | content::BrowserTaskEnvironment task_environment_; |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 37 | TestingProfile profile; |
| 38 | TestingFontFamilyCache cache(&profile); |
maxbogue | ea16ff41 | 2016-10-28 16:35:29 | [diff] [blame] | 39 | sync_preferences::TestingPrefServiceSyncable* prefs = |
sdefresne | 50c1e52 | 2015-09-18 09:47:51 | [diff] [blame] | 40 | profile.GetTestingPrefService(); |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 41 | |
| 42 | std::string font1("font 1"); |
| 43 | std::string font2("font 2"); |
| 44 | std::string map_name("webkit.webprefs.fonts.pictograph"); |
| 45 | std::string script("Zzyxca"); |
| 46 | std::string pref_name(map_name + '.' + script); |
| 47 | std::string pref_name2(map_name + '.' + "adsf"); |
| 48 | |
| 49 | // Registers 2 preferences, and sets the first one. |
raymes | aa60872 | 2015-04-27 03:00:25 | [diff] [blame] | 50 | prefs->registry()->RegisterStringPref(pref_name.c_str(), std::string()); |
| 51 | prefs->registry()->RegisterStringPref(pref_name2.c_str(), std::string()); |
[email protected] | 354de9e | 2014-08-07 03:27:19 | [diff] [blame] | 52 | prefs->SetString(pref_name.c_str(), font1.c_str()); |
| 53 | |
| 54 | // Check that the right preference is returned. |
| 55 | std::string result = base::UTF16ToUTF8( |
| 56 | cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); |
| 57 | EXPECT_EQ(font1, result); |
| 58 | EXPECT_EQ(1, cache.fetch_font_count_); |
| 59 | |
| 60 | // Check that the second access uses the cache. |
| 61 | result = base::UTF16ToUTF8( |
| 62 | cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); |
| 63 | EXPECT_EQ(font1, result); |
| 64 | EXPECT_EQ(1, cache.fetch_font_count_); |
| 65 | |
| 66 | // Changing another preference should have no effect. |
| 67 | prefs->SetString(pref_name2.c_str(), "katy perry"); |
| 68 | result = base::UTF16ToUTF8( |
| 69 | cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); |
| 70 | EXPECT_EQ(font1, result); |
| 71 | EXPECT_EQ(1, cache.fetch_font_count_); |
| 72 | |
| 73 | // Changing the preferences invalidates the cache. |
| 74 | prefs->SetString(pref_name.c_str(), font2.c_str()); |
| 75 | result = base::UTF16ToUTF8( |
| 76 | cache.FetchAndCacheFont(script.c_str(), map_name.c_str())); |
| 77 | EXPECT_EQ(font2, result); |
| 78 | EXPECT_EQ(2, cache.fetch_font_count_); |
| 79 | } |