Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: third_party/WebKit/Source/core/frame/UseCounterTest.cpp

Issue 2796283005: Adding UseCounter specific for extensions (Closed)
Patch Set: Codereview: added tester ExpectTotalCountExcept to simplify UseCounter unit tests Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/frame/Deprecation.h" 5 #include "core/frame/Deprecation.h"
6 #include "core/frame/UseCounter.h" 6 #include "core/frame/UseCounter.h"
7 #include "core/page/Page.h" 7 #include "core/page/Page.h"
8 #include "core/testing/DummyPageHolder.h" 8 #include "core/testing/DummyPageHolder.h"
9 #include "platform/testing/HistogramTester.h" 9 #include "platform/testing/HistogramTester.h"
10 #include "platform/testing/URLTestHelpers.h" 10 #include "platform/testing/URLTestHelpers.h"
11 #include "platform/weborigin/KURL.h" 11 #include "platform/weborigin/KURL.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace { 14 namespace {
15 const char* const kFeaturesHistogramName = "Blink.UseCounter.Features"; 15 const char* const kFeaturesHistogramName = "Blink.UseCounter.Features";
16 const char* const kCSSHistogramName = "Blink.UseCounter.CSSProperties"; 16 const char* const kCSSHistogramName = "Blink.UseCounter.CSSProperties";
17 const char* const kAnimatedCSSHistogramName = 17 const char* const kAnimatedCSSHistogramName =
18 "Blink.UseCounter.AnimatedCSSProperties"; 18 "Blink.UseCounter.AnimatedCSSProperties";
19 const char* const kExtensionFeaturesHistogramName =
20 "Blink.UseCounter.Extensions.Features";
19 21
20 const char* const kSVGFeaturesHistogramName = 22 const char* const kSVGFeaturesHistogramName =
Alexei Svitkine (slow) 2017/05/10 15:17:30 Nit: Use const char kSVGFeaturesHistogramName[] =
Nico 2017/05/10 15:38:20 Oh, good catch. To elaborate a bit on this: const
lunalu1 2017/05/10 19:39:39 Makes sense! Thanks for the insight =) Although i
21 "Blink.UseCounter.SVGImage.Features"; 23 "Blink.UseCounter.SVGImage.Features";
22 const char* const kSVGCSSHistogramName = 24 const char* const kSVGCSSHistogramName =
23 "Blink.UseCounter.SVGImage.CSSProperties"; 25 "Blink.UseCounter.SVGImage.CSSProperties";
24 const char* const kSVGAnimatedCSSHistogramName = 26 const char* const kSVGAnimatedCSSHistogramName =
25 "Blink.UseCounter.SVGImage.AnimatedCSSProperties"; 27 "Blink.UseCounter.SVGImage.AnimatedCSSProperties";
26 28
27 const char* const kLegacyFeaturesHistogramName = "WebCore.FeatureObserver"; 29 const char* const kLegacyFeaturesHistogramName = "WebCore.FeatureObserver";
28 const char* const kLegacyCSSHistogramName = 30 const char* const kLegacyCSSHistogramName =
29 "WebCore.FeatureObserver.CSSProperties"; 31 "WebCore.FeatureObserver.CSSProperties";
32
33 // Sorted list of all histograms to be tested.
34 const std::vector<std::string>& all_histograms = {
Nico 2017/05/09 19:36:46 1) This is just a vector, why is it a reference? 2
lunalu1 2017/05/10 19:39:39 I see. Thanks!
35 kAnimatedCSSHistogramName, kCSSHistogramName,
36 kExtensionFeaturesHistogramName, kFeaturesHistogramName,
37 kSVGAnimatedCSSHistogramName, kSVGCSSHistogramName,
38 kSVGFeaturesHistogramName};
39
40 int GetPageVisitsBucketforHistogram(std::string histogram_name) {
Alexei Svitkine (slow) 2017/05/10 15:17:30 const std::string&
41 if (histogram_name.find("CSS") == std::string::npos)
42 return blink::UseCounter::kPageVisits;
43 // For CSS histograms, the page visits bucket should be 1.
44 return 1;
45 }
30 } 46 }
Alexei Svitkine (slow) 2017/05/10 15:17:30 Nit: " // namespace" Also maybe add inner empty
lunalu1 2017/05/10 19:39:39 Sorry my bad.
31 47
32 namespace blink { 48 namespace blink {
33 49
34 template <typename T> 50 template <typename T>
35 void HistogramBasicTest(const std::string& histogram, 51 void HistogramBasicTest(const std::string& histogram,
36 const std::string& legacy_histogram, 52 const std::string& legacy_histogram,
37 const std::vector<std::string>& unaffected_histograms,
38 T item, 53 T item,
39 T second_item, 54 T second_item,
40 std::function<bool(T)> counted, 55 std::function<bool(T)> counted,
41 std::function<void(T)> count, 56 std::function<void(T)> count,
42 std::function<int(T)> histogram_map, 57 std::function<int(T)> histogram_map,
43 std::function<void(KURL)> did_commit_load, 58 std::function<void(KURL)> did_commit_load,
44 const std::string& url, 59 const std::string& url) {
45 int page_visit_bucket) {
46 HistogramTester histogram_tester; 60 HistogramTester histogram_tester;
47 61
62 int page_visit_bucket = GetPageVisitsBucketforHistogram(histogram);
63
48 // Test recording a single (arbitrary) counter 64 // Test recording a single (arbitrary) counter
49 EXPECT_FALSE(counted(item)); 65 EXPECT_FALSE(counted(item));
50 count(item); 66 count(item);
51 EXPECT_TRUE(counted(item)); 67 EXPECT_TRUE(counted(item));
52 histogram_tester.ExpectUniqueSample(histogram, histogram_map(item), 1); 68 histogram_tester.ExpectUniqueSample(histogram, histogram_map(item), 1);
53 if (!legacy_histogram.empty()) { 69 if (!legacy_histogram.empty()) {
54 histogram_tester.ExpectTotalCount(legacy_histogram, 0); 70 histogram_tester.ExpectTotalCount(legacy_histogram, 0);
55 } 71 }
56 72
57 // Test that repeated measurements have no effect 73 // Test that repeated measurements have no effect
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 did_commit_load(URLTestHelpers::ToKURL(url)); 118 did_commit_load(URLTestHelpers::ToKURL(url));
103 if (!legacy_histogram.empty()) { 119 if (!legacy_histogram.empty()) {
104 histogram_tester.ExpectBucketCount(legacy_histogram, histogram_map(item), 120 histogram_tester.ExpectBucketCount(legacy_histogram, histogram_map(item),
105 2); 121 2);
106 histogram_tester.ExpectBucketCount(legacy_histogram, 122 histogram_tester.ExpectBucketCount(legacy_histogram,
107 histogram_map(second_item), 1); 123 histogram_map(second_item), 1);
108 histogram_tester.ExpectBucketCount(legacy_histogram, page_visit_bucket, 2); 124 histogram_tester.ExpectBucketCount(legacy_histogram, page_visit_bucket, 2);
109 histogram_tester.ExpectTotalCount(legacy_histogram, 5); 125 histogram_tester.ExpectTotalCount(legacy_histogram, 5);
110 } 126 }
111 127
112 for (size_t i = 0; i < unaffected_histograms.size(); ++i) { 128 for (const auto unaffected_histogram : all_histograms) {
Alexei Svitkine (slow) 2017/05/10 15:17:30 const auto& or const std::string& Otherwise, this
lunalu1 2017/05/10 19:39:39 Done.
113 histogram_tester.ExpectTotalCount(unaffected_histograms[i], 0); 129 if (unaffected_histogram == histogram)
130 continue;
131
132 histogram_tester.ExpectTotalCountExcept(
133 unaffected_histogram,
134 GetPageVisitsBucketforHistogram(unaffected_histogram), 0);
114 } 135 }
115 } 136 }
116 137
117 TEST(UseCounterTest, RecordingFeatures) { 138 TEST(UseCounterTest, RecordingFeatures) {
118 UseCounter use_counter; 139 UseCounter use_counter;
119 HistogramBasicTest<UseCounter::Feature>( 140 HistogramBasicTest<UseCounter::Feature>(
120 kFeaturesHistogramName, kLegacyFeaturesHistogramName, 141 kFeaturesHistogramName, kLegacyFeaturesHistogramName, UseCounter::kFetch,
121 {kSVGFeaturesHistogramName, kSVGCSSHistogramName, 142 UseCounter::kFetchBodyStream,
122 kSVGAnimatedCSSHistogramName},
123 UseCounter::kFetch, UseCounter::kFetchBodyStream,
124 [&](UseCounter::Feature feature) -> bool { 143 [&](UseCounter::Feature feature) -> bool {
125 return use_counter.HasRecordedMeasurement(feature); 144 return use_counter.HasRecordedMeasurement(feature);
126 }, 145 },
127 [&](UseCounter::Feature feature) { 146 [&](UseCounter::Feature feature) {
128 use_counter.RecordMeasurement(feature); 147 use_counter.RecordMeasurement(feature);
129 }, 148 },
130 [](UseCounter::Feature feature) -> int { return feature; }, 149 [](UseCounter::Feature feature) -> int { return feature; },
131 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); }, 150 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); },
132 "https://dummysite.com/", UseCounter::kPageVisits); 151 "https://dummysite.com/");
133 } 152 }
134 153
135 TEST(UseCounterTest, RecordingCSSProperties) { 154 TEST(UseCounterTest, RecordingCSSProperties) {
136 UseCounter use_counter; 155 UseCounter use_counter;
137 HistogramBasicTest<CSSPropertyID>( 156 HistogramBasicTest<CSSPropertyID>(
138 kCSSHistogramName, kLegacyCSSHistogramName, 157 kCSSHistogramName, kLegacyCSSHistogramName, CSSPropertyFont,
139 {kSVGFeaturesHistogramName, kSVGCSSHistogramName, 158 CSSPropertyZoom,
140 kSVGAnimatedCSSHistogramName},
141 CSSPropertyFont, CSSPropertyZoom,
142 [&](CSSPropertyID property) -> bool { 159 [&](CSSPropertyID property) -> bool {
143 return use_counter.IsCounted(property); 160 return use_counter.IsCounted(property);
144 }, 161 },
145 [&](CSSPropertyID property) { 162 [&](CSSPropertyID property) {
146 use_counter.Count(kHTMLStandardMode, property); 163 use_counter.Count(kHTMLStandardMode, property);
147 }, 164 },
148 [](CSSPropertyID property) -> int { 165 [](CSSPropertyID property) -> int {
149 return UseCounter::MapCSSPropertyIdToCSSSampleIdForHistogram(property); 166 return UseCounter::MapCSSPropertyIdToCSSSampleIdForHistogram(property);
150 }, 167 },
151 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); }, 168 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); },
152 "https://dummysite.com/", 1 /* page visit bucket */); 169 "https://dummysite.com/");
153 } 170 }
154 171
155 TEST(UseCounterTest, RecordingAnimatedCSSProperties) { 172 TEST(UseCounterTest, RecordingAnimatedCSSProperties) {
156 UseCounter use_counter; 173 UseCounter use_counter;
157 HistogramBasicTest<CSSPropertyID>( 174 HistogramBasicTest<CSSPropertyID>(
158 kAnimatedCSSHistogramName, "", 175 kAnimatedCSSHistogramName, "", CSSPropertyOpacity, CSSPropertyVariable,
159 {kSVGCSSHistogramName, kSVGAnimatedCSSHistogramName}, CSSPropertyOpacity,
160 CSSPropertyVariable,
161 [&](CSSPropertyID property) -> bool { 176 [&](CSSPropertyID property) -> bool {
162 return use_counter.IsCountedAnimatedCSS(property); 177 return use_counter.IsCountedAnimatedCSS(property);
163 }, 178 },
164 [&](CSSPropertyID property) { use_counter.CountAnimatedCSS(property); }, 179 [&](CSSPropertyID property) { use_counter.CountAnimatedCSS(property); },
165 [](CSSPropertyID property) -> int { 180 [](CSSPropertyID property) -> int {
166 return UseCounter::MapCSSPropertyIdToCSSSampleIdForHistogram(property); 181 return UseCounter::MapCSSPropertyIdToCSSSampleIdForHistogram(property);
167 }, 182 },
168 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); }, 183 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); },
169 "https://dummysite.com/", 1 /* page visit bucket */); 184 "https://dummysite.com/");
185 }
186
187 TEST(UseCounterTest, RecordingExtensions) {
188 UseCounter use_counter(UseCounter::kExtensionContext);
189 HistogramBasicTest<UseCounter::Feature>(
190 kExtensionFeaturesHistogramName, kLegacyFeaturesHistogramName,
191 UseCounter::kFetch, UseCounter::kFetchBodyStream,
192 [&](UseCounter::Feature feature) -> bool {
193 return use_counter.HasRecordedMeasurement(feature);
194 },
195 [&](UseCounter::Feature feature) {
196 use_counter.RecordMeasurement(feature);
197 },
198 [](UseCounter::Feature feature) -> int { return feature; },
199 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); },
200 "chrome-extension://dummysite");
170 } 201 }
171 202
172 TEST(UseCounterTest, SVGImageContextFeatures) { 203 TEST(UseCounterTest, SVGImageContextFeatures) {
173 UseCounter use_counter(UseCounter::kSVGImageContext); 204 UseCounter use_counter(UseCounter::kSVGImageContext);
174 HistogramBasicTest<UseCounter::Feature>( 205 HistogramBasicTest<UseCounter::Feature>(
175 kSVGFeaturesHistogramName, kLegacyFeaturesHistogramName, 206 kSVGFeaturesHistogramName, kLegacyFeaturesHistogramName,
176 {kFeaturesHistogramName, kCSSHistogramName, kAnimatedCSSHistogramName},
177 UseCounter::kSVGSMILAdditiveAnimation, 207 UseCounter::kSVGSMILAdditiveAnimation,
178 UseCounter::kSVGSMILAnimationElementTiming, 208 UseCounter::kSVGSMILAnimationElementTiming,
179 [&](UseCounter::Feature feature) -> bool { 209 [&](UseCounter::Feature feature) -> bool {
180 return use_counter.HasRecordedMeasurement(feature); 210 return use_counter.HasRecordedMeasurement(feature);
181 }, 211 },
182 [&](UseCounter::Feature feature) { 212 [&](UseCounter::Feature feature) {
183 use_counter.RecordMeasurement(feature); 213 use_counter.RecordMeasurement(feature);
184 }, 214 },
185 [](UseCounter::Feature feature) -> int { return feature; }, 215 [](UseCounter::Feature feature) -> int { return feature; },
186 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); }, "about:blank", 216 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); }, "about:blank");
187 // In practice SVGs always appear to be loaded with an about:blank URL 217 // In practice SVGs always appear to be loaded with an about:blank URL
Nico 2017/05/09 19:36:46 The comment looks strange here. Maybe do // In pr
lunalu1 2017/05/10 19:39:39 Done. I did the same for HttpsUrl and ExtensionUrl
188 UseCounter::kPageVisits);
189 } 218 }
190 219
191 TEST(UseCounterTest, SVGImageContextCSSProperties) { 220 TEST(UseCounterTest, SVGImageContextCSSProperties) {
192 UseCounter use_counter(UseCounter::kSVGImageContext); 221 UseCounter use_counter(UseCounter::kSVGImageContext);
193 HistogramBasicTest<CSSPropertyID>( 222 HistogramBasicTest<CSSPropertyID>(
194 kSVGCSSHistogramName, kLegacyCSSHistogramName, 223 kSVGCSSHistogramName, kLegacyCSSHistogramName, CSSPropertyFont,
195 {kFeaturesHistogramName, kCSSHistogramName, kAnimatedCSSHistogramName}, 224 CSSPropertyZoom,
196 CSSPropertyFont, CSSPropertyZoom,
197 [&](CSSPropertyID property) -> bool { 225 [&](CSSPropertyID property) -> bool {
198 return use_counter.IsCounted(property); 226 return use_counter.IsCounted(property);
199 }, 227 },
200 [&](CSSPropertyID property) { 228 [&](CSSPropertyID property) {
201 use_counter.Count(kHTMLStandardMode, property); 229 use_counter.Count(kHTMLStandardMode, property);
202 }, 230 },
203 [](CSSPropertyID property) -> int { 231 [](CSSPropertyID property) -> int {
204 return UseCounter::MapCSSPropertyIdToCSSSampleIdForHistogram(property); 232 return UseCounter::MapCSSPropertyIdToCSSSampleIdForHistogram(property);
205 }, 233 },
206 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); }, "about:blank", 234 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); }, "about:blank"
207 // In practice SVGs always appear to be loaded with an about:blank URL 235 // In practice SVGs always appear to be loaded with an about:blank URL
208 1 /* page visit bucket */); 236 );
209 } 237 }
210 238
211 TEST(UseCounterTest, SVGImageContextAnimatedCSSProperties) { 239 TEST(UseCounterTest, SVGImageContextAnimatedCSSProperties) {
212 UseCounter use_counter(UseCounter::kSVGImageContext); 240 UseCounter use_counter(UseCounter::kSVGImageContext);
213 HistogramBasicTest<CSSPropertyID>( 241 HistogramBasicTest<CSSPropertyID>(
214 kSVGAnimatedCSSHistogramName, "", 242 kSVGAnimatedCSSHistogramName, "", CSSPropertyOpacity, CSSPropertyVariable,
215 {kCSSHistogramName, kAnimatedCSSHistogramName}, CSSPropertyOpacity,
216 CSSPropertyVariable,
217 [&](CSSPropertyID property) -> bool { 243 [&](CSSPropertyID property) -> bool {
218 return use_counter.IsCountedAnimatedCSS(property); 244 return use_counter.IsCountedAnimatedCSS(property);
219 }, 245 },
220 [&](CSSPropertyID property) { use_counter.CountAnimatedCSS(property); }, 246 [&](CSSPropertyID property) { use_counter.CountAnimatedCSS(property); },
221 [](CSSPropertyID property) -> int { 247 [](CSSPropertyID property) -> int {
222 return UseCounter::MapCSSPropertyIdToCSSSampleIdForHistogram(property); 248 return UseCounter::MapCSSPropertyIdToCSSSampleIdForHistogram(property);
223 }, 249 },
224 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); }, "about:blank", 250 [&](KURL kurl) { use_counter.DidCommitLoad(kurl); }, "about:blank"
225 // In practice SVGs always appear to be loaded with an about:blank URL 251 // In practice SVGs always appear to be loaded with an about:blank URL
226 1 /* page visit bucket */); 252 );
227 } 253 }
228 254
229 TEST(UseCounterTest, InspectorDisablesMeasurement) { 255 TEST(UseCounterTest, InspectorDisablesMeasurement) {
230 UseCounter use_counter; 256 UseCounter use_counter;
231 HistogramTester histogram_tester; 257 HistogramTester histogram_tester;
232 258
233 // The specific feature we use here isn't important. 259 // The specific feature we use here isn't important.
234 UseCounter::Feature feature = UseCounter::Feature::kSVGSMILElementInDocument; 260 UseCounter::Feature feature = UseCounter::Feature::kSVGSMILElementInDocument;
235 CSSPropertyID property = CSSPropertyFontWeight; 261 CSSPropertyID property = CSSPropertyFontWeight;
236 CSSParserMode parser_mode = kHTMLStandardMode; 262 CSSParserMode parser_mode = kHTMLStandardMode;
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 446
421 deprecation_.UnmuteForInspector(); 447 deprecation_.UnmuteForInspector();
422 Deprecation::WarnOnDeprecatedProperties(GetFrame(), property); 448 Deprecation::WarnOnDeprecatedProperties(GetFrame(), property);
423 // TODO: use the actually deprecated property to get a deprecation message. 449 // TODO: use the actually deprecated property to get a deprecation message.
424 EXPECT_FALSE(deprecation_.IsSuppressed(property)); 450 EXPECT_FALSE(deprecation_.IsSuppressed(property));
425 Deprecation::CountDeprecation(GetFrame(), feature); 451 Deprecation::CountDeprecation(GetFrame(), feature);
426 EXPECT_TRUE(use_counter_.HasRecordedMeasurement(feature)); 452 EXPECT_TRUE(use_counter_.HasRecordedMeasurement(feature));
427 } 453 }
428 454
429 } // namespace blink 455 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698