• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium Authors
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 "net/http/webfonts_histogram.h"
6 
7 #include "base/strings/strcat.h"
8 #include "base/test/metrics/histogram_tester.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace net::web_fonts_histogram {
12 
13 namespace {
14 
15 const char kRobotoHistogramName[] = "WebFont.HttpCacheStatus_roboto";
16 const char kOpenSansHistogramName[] = "WebFont.HttpCacheStatus_opensans";
17 const char kOthersHistogramName[] = "WebFont.HttpCacheStatus_others";
18 
19 const char kHttps[] = "https://";
20 const char kHttp[] = "http://";
21 
22 const char kPattern1[] = "themes.googleusercontent.com/static/fonts/";
23 const char kPattern2[] = "ssl.gstatic.com/fonts/";
24 const char kPattern3[] = "fonts.gstatic.com/s/";
25 
26 const char kRoboto[] = "roboto";
27 const char kOpenSans[] = "opensans";
28 
29 }  // namespace
30 
TEST(WebfontsHistogramTest,EmptyKey_NoRecord)31 TEST(WebfontsHistogramTest, EmptyKey_NoRecord) {
32   base::HistogramTester histograms;
33   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_USED, "");
34   histograms.ExpectTotalCount(kRobotoHistogramName, 0);
35   histograms.ExpectTotalCount(kOpenSansHistogramName, 0);
36   histograms.ExpectTotalCount(kOthersHistogramName, 0);
37 }
38 
TEST(WebfontsHistogramTest,RecordRoboto)39 TEST(WebfontsHistogramTest, RecordRoboto) {
40   base::HistogramTester histograms;
41   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_USED,
42                          base::StrCat({kHttps, kPattern1, kRoboto}));
43   histograms.ExpectUniqueSample(kRobotoHistogramName, 3 /* ENTRY_USED */, 1);
44   histograms.ExpectTotalCount(kOpenSansHistogramName, 0);
45   histograms.ExpectTotalCount(kOthersHistogramName, 0);
46 }
47 
TEST(WebfontsHistogramTest,RecordOpenSans)48 TEST(WebfontsHistogramTest, RecordOpenSans) {
49   base::HistogramTester histograms;
50   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_NOT_IN_CACHE,
51                          base::StrCat({kHttp, kPattern2, kOpenSans}));
52   histograms.ExpectTotalCount(kRobotoHistogramName, 0);
53   histograms.ExpectUniqueSample(kOpenSansHistogramName,
54                                 2 /* ENTRY_NOT_IN_CACHE */, 1);
55   histograms.ExpectTotalCount(kOthersHistogramName, 0);
56 }
57 
TEST(WebfontsHistogramTest,EmptyFont_RecordOthers)58 TEST(WebfontsHistogramTest, EmptyFont_RecordOthers) {
59   base::HistogramTester histograms;
60   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_CANT_CONDITIONALIZE,
61                          base::StrCat({kHttps, kPattern3}));
62   histograms.ExpectTotalCount(kRobotoHistogramName, 0);
63   histograms.ExpectTotalCount(kOpenSansHistogramName, 0);
64   histograms.ExpectUniqueSample(kOthersHistogramName,
65                                 6 /* ENTRY_CANT_CONDITIONALIZE */, 1);
66 }
67 
TEST(WebfontsHistogramTest,ArbitraryFont_RecordOthers)68 TEST(WebfontsHistogramTest, ArbitraryFont_RecordOthers) {
69   base::HistogramTester histograms;
70   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_OTHER,
71                          base::StrCat({kHttps, kPattern2, "abc"}));
72   histograms.ExpectTotalCount(kRobotoHistogramName, 0);
73   histograms.ExpectTotalCount(kOpenSansHistogramName, 0);
74   histograms.ExpectUniqueSample(kOthersHistogramName, 1 /* ENTRY_OTHER */, 1);
75 }
76 
TEST(WebfontsHistogramTest,WithSuffix_Record)77 TEST(WebfontsHistogramTest, WithSuffix_Record) {
78   base::HistogramTester histograms;
79   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_USED,
80                          base::StrCat({kHttps, kPattern1, kRoboto, "abc"}));
81   histograms.ExpectUniqueSample(kRobotoHistogramName, 3 /* ENTRY_USED */, 1);
82   histograms.ExpectTotalCount(kOpenSansHistogramName, 0);
83   histograms.ExpectTotalCount(kOthersHistogramName, 0);
84 }
85 
TEST(WebfontsHistogramTest,WithPrefix_NoRecord)86 TEST(WebfontsHistogramTest, WithPrefix_NoRecord) {
87   base::HistogramTester histograms;
88   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_USED,
89                          base::StrCat({"abc", kHttps, kPattern1, kRoboto}));
90   histograms.ExpectTotalCount(kRobotoHistogramName, 0);
91   histograms.ExpectTotalCount(kOpenSansHistogramName, 0);
92   histograms.ExpectTotalCount(kOthersHistogramName, 0);
93 }
94 
TEST(WebfontsHistogramTest,OtherProtocol_NoRecord)95 TEST(WebfontsHistogramTest, OtherProtocol_NoRecord) {
96   base::HistogramTester histograms;
97   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_OTHER,
98                          base::StrCat({"ftp://", kPattern1, kRoboto}));
99   histograms.ExpectTotalCount(kRobotoHistogramName, 0);
100   histograms.ExpectTotalCount(kOpenSansHistogramName, 0);
101   histograms.ExpectTotalCount(kOthersHistogramName, 0);
102 }
103 
TEST(WebfontsHistogramTest,OtherPattern_NoRecord)104 TEST(WebfontsHistogramTest, OtherPattern_NoRecord) {
105   base::HistogramTester histograms;
106   MaybeRecordCacheStatus(
107       HttpResponseInfo::ENTRY_USED,
108       base::StrCat({kHttps, "fonts.gstatic.com//s/", kRoboto}));
109   histograms.ExpectTotalCount(kRobotoHistogramName, 0);
110   histograms.ExpectTotalCount(kOpenSansHistogramName, 0);
111   histograms.ExpectTotalCount(kOthersHistogramName, 0);
112 }
113 
TEST(WebfontsHistogramTest,TwoRobotoSameBucket_TwoOpenSansDifferentBucket)114 TEST(WebfontsHistogramTest, TwoRobotoSameBucket_TwoOpenSansDifferentBucket) {
115   base::HistogramTester histograms;
116 
117   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_USED,
118                          base::StrCat({kHttps, kPattern2, kRoboto}));
119   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_USED,
120                          base::StrCat({kHttp, kPattern3, kRoboto}));
121   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_USED,
122                          base::StrCat({kHttps, kPattern2, kOpenSans}));
123   MaybeRecordCacheStatus(HttpResponseInfo::ENTRY_OTHER,
124                          base::StrCat({kHttp, kPattern3, kOpenSans}));
125 
126   histograms.ExpectUniqueSample(kRobotoHistogramName, 3 /* ENTRY_USED */, 2);
127   histograms.ExpectTotalCount(kRobotoHistogramName, 2);
128 
129   histograms.ExpectBucketCount(kOpenSansHistogramName, 3 /* ENTRY_USED */, 1);
130   histograms.ExpectBucketCount(kOpenSansHistogramName, 1 /* ENTRY_OTHER */, 1);
131   histograms.ExpectTotalCount(kOpenSansHistogramName, 2);
132 
133   histograms.ExpectTotalCount(kOthersHistogramName, 0);
134 }
135 
136 }  // namespace net::web_fonts_histogram
137