blob: 0bcc1e64bee4406a8a10605158486967ca65bb37 [file] [log] [blame]
[email protected]354de9e2014-08-07 03:27:191// 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
avi6846aef2015-12-26 01:09:387#include "base/macros.h"
[email protected]354de9e2014-08-07 03:27:198#include "base/strings/utf_string_conversions.h"
[email protected]354de9e2014-08-07 03:27:199#include "chrome/test/base/testing_profile.h"
maxbogueea16ff412016-10-28 16:35:2910#include "components/sync_preferences/testing_pref_service_syncable.h"
Gabriel Charettec7108742019-08-23 03:31:4011#include "content/public/test/browser_task_environment.h"
[email protected]354de9e2014-08-07 03:27:1912#include "testing/gtest/include/gtest/gtest.h"
13
14namespace {
15
16class TestingFontFamilyCache : public FontFamilyCache {
17 public:
18 explicit TestingFontFamilyCache(Profile* profile)
19 : FontFamilyCache(profile), fetch_font_count_(0) {}
Daniel Chenga542fca2014-10-21 09:51:2920 ~TestingFontFamilyCache() override {}
21 base::string16 FetchFont(const char* script, const char* map_name) override {
[email protected]354de9e2014-08-07 03:27:1922 ++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.
35TEST(FontFamilyCacheTest, Caching) {
Gabriel Charette798fde72019-08-20 22:24:0436 content::BrowserTaskEnvironment task_environment_;
[email protected]354de9e2014-08-07 03:27:1937 TestingProfile profile;
38 TestingFontFamilyCache cache(&profile);
maxbogueea16ff412016-10-28 16:35:2939 sync_preferences::TestingPrefServiceSyncable* prefs =
sdefresne50c1e522015-09-18 09:47:5140 profile.GetTestingPrefService();
[email protected]354de9e2014-08-07 03:27:1941
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.
raymesaa608722015-04-27 03:00:2550 prefs->registry()->RegisterStringPref(pref_name.c_str(), std::string());
51 prefs->registry()->RegisterStringPref(pref_name2.c_str(), std::string());
[email protected]354de9e2014-08-07 03:27:1952 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}