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

Side by Side Diff: third_party/WebKit/Source/core/css/parser/CSSLazyParsingTest.cpp

Issue 2616093003: Make CSSParserContext be garbage collected. (Closed)
Patch Set: fix fuzzer compile again Created 3 years, 11 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/css/CSSStyleSheet.h" 5 #include "core/css/CSSStyleSheet.h"
6 #include "core/css/StyleRule.h" 6 #include "core/css/StyleRule.h"
7 #include "core/css/StyleSheetContents.h" 7 #include "core/css/StyleSheetContents.h"
8 #include "core/css/parser/CSSLazyParsingState.h" 8 #include "core/css/parser/CSSLazyParsingState.h"
9 #include "core/css/parser/CSSParser.h" 9 #include "core/css/parser/CSSParser.h"
10 #include "core/css/parser/CSSParserContext.h" 10 #include "core/css/parser/CSSParserContext.h"
(...skipping 15 matching lines...) Expand all
26 StyleRule* ruleAt(StyleSheetContents* sheet, size_t index) { 26 StyleRule* ruleAt(StyleSheetContents* sheet, size_t index) {
27 return toStyleRule(sheet->childRules()[index]); 27 return toStyleRule(sheet->childRules()[index]);
28 } 28 }
29 29
30 protected: 30 protected:
31 HistogramTester m_histogramTester; 31 HistogramTester m_histogramTester;
32 Persistent<StyleSheetContents> m_cachedContents; 32 Persistent<StyleSheetContents> m_cachedContents;
33 }; 33 };
34 34
35 TEST_F(CSSLazyParsingTest, Simple) { 35 TEST_F(CSSLazyParsingTest, Simple) {
36 CSSParserContext context(HTMLStandardMode, nullptr); 36 CSSParserContext* context = CSSParserContext::create(HTMLStandardMode);
37 StyleSheetContents* styleSheet = StyleSheetContents::create(context); 37 StyleSheetContents* styleSheet = StyleSheetContents::create(context);
38 38
39 String sheetText = "body { background-color: red; }"; 39 String sheetText = "body { background-color: red; }";
40 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */); 40 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */);
41 StyleRule* rule = ruleAt(styleSheet, 0); 41 StyleRule* rule = ruleAt(styleSheet, 0);
42 EXPECT_FALSE(hasParsedProperties(rule)); 42 EXPECT_FALSE(hasParsedProperties(rule));
43 rule->properties(); 43 rule->properties();
44 EXPECT_TRUE(hasParsedProperties(rule)); 44 EXPECT_TRUE(hasParsedProperties(rule));
45 } 45 }
46 46
47 // Avoiding lazy parsing for trivially empty blocks helps us perform the 47 // Avoiding lazy parsing for trivially empty blocks helps us perform the
48 // shouldConsiderForMatchingRules optimization. 48 // shouldConsiderForMatchingRules optimization.
49 TEST_F(CSSLazyParsingTest, DontLazyParseEmpty) { 49 TEST_F(CSSLazyParsingTest, DontLazyParseEmpty) {
50 CSSParserContext context(HTMLStandardMode, nullptr); 50 CSSParserContext* context = CSSParserContext::create(HTMLStandardMode);
51 StyleSheetContents* styleSheet = StyleSheetContents::create(context); 51 StyleSheetContents* styleSheet = StyleSheetContents::create(context);
52 52
53 String sheetText = "body { }"; 53 String sheetText = "body { }";
54 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */); 54 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */);
55 StyleRule* rule = ruleAt(styleSheet, 0); 55 StyleRule* rule = ruleAt(styleSheet, 0);
56 EXPECT_TRUE(hasParsedProperties(rule)); 56 EXPECT_TRUE(hasParsedProperties(rule));
57 EXPECT_FALSE( 57 EXPECT_FALSE(
58 rule->shouldConsiderForMatchingRules(false /* includeEmptyRules */)); 58 rule->shouldConsiderForMatchingRules(false /* includeEmptyRules */));
59 } 59 }
60 60
61 // Avoid parsing rules with ::before or ::after to avoid causing 61 // Avoid parsing rules with ::before or ::after to avoid causing
62 // collectFeatures() when we trigger parsing for attr(); 62 // collectFeatures() when we trigger parsing for attr();
63 TEST_F(CSSLazyParsingTest, DontLazyParseBeforeAfter) { 63 TEST_F(CSSLazyParsingTest, DontLazyParseBeforeAfter) {
64 CSSParserContext context(HTMLStandardMode, nullptr); 64 CSSParserContext* context = CSSParserContext::create(HTMLStandardMode);
65 StyleSheetContents* styleSheet = StyleSheetContents::create(context); 65 StyleSheetContents* styleSheet = StyleSheetContents::create(context);
66 66
67 String sheetText = 67 String sheetText =
68 "p::before { content: 'foo' } p .class::after { content: 'bar' } "; 68 "p::before { content: 'foo' } p .class::after { content: 'bar' } ";
69 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */); 69 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */);
70 70
71 EXPECT_TRUE(hasParsedProperties(ruleAt(styleSheet, 0))); 71 EXPECT_TRUE(hasParsedProperties(ruleAt(styleSheet, 0)));
72 EXPECT_TRUE(hasParsedProperties(ruleAt(styleSheet, 1))); 72 EXPECT_TRUE(hasParsedProperties(ruleAt(styleSheet, 1)));
73 } 73 }
74 74
75 // Test for crbug.com/664115 where |shouldConsiderForMatchingRules| would flip 75 // Test for crbug.com/664115 where |shouldConsiderForMatchingRules| would flip
76 // from returning false to true if the lazy property was parsed. This is a 76 // from returning false to true if the lazy property was parsed. This is a
77 // dangerous API because callers will expect the set of matching rules to be 77 // dangerous API because callers will expect the set of matching rules to be
78 // identical if the stylesheet is not mutated. 78 // identical if the stylesheet is not mutated.
79 TEST_F(CSSLazyParsingTest, ShouldConsiderForMatchingRulesDoesntChange1) { 79 TEST_F(CSSLazyParsingTest, ShouldConsiderForMatchingRulesDoesntChange1) {
80 CSSParserContext context(HTMLStandardMode, nullptr); 80 CSSParserContext* context = CSSParserContext::create(HTMLStandardMode);
81 StyleSheetContents* styleSheet = StyleSheetContents::create(context); 81 StyleSheetContents* styleSheet = StyleSheetContents::create(context);
82 82
83 String sheetText = "p::first-letter { ,badness, } "; 83 String sheetText = "p::first-letter { ,badness, } ";
84 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */); 84 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */);
85 85
86 StyleRule* rule = ruleAt(styleSheet, 0); 86 StyleRule* rule = ruleAt(styleSheet, 0);
87 EXPECT_FALSE(hasParsedProperties(rule)); 87 EXPECT_FALSE(hasParsedProperties(rule));
88 EXPECT_TRUE( 88 EXPECT_TRUE(
89 rule->shouldConsiderForMatchingRules(false /* includeEmptyRules */)); 89 rule->shouldConsiderForMatchingRules(false /* includeEmptyRules */));
90 90
91 // Parse the rule. 91 // Parse the rule.
92 rule->properties(); 92 rule->properties();
93 93
94 // Now, we should still consider this for matching rules even if it is empty. 94 // Now, we should still consider this for matching rules even if it is empty.
95 EXPECT_TRUE(hasParsedProperties(rule)); 95 EXPECT_TRUE(hasParsedProperties(rule));
96 EXPECT_TRUE( 96 EXPECT_TRUE(
97 rule->shouldConsiderForMatchingRules(false /* includeEmptyRules */)); 97 rule->shouldConsiderForMatchingRules(false /* includeEmptyRules */));
98 } 98 }
99 99
100 // Test the same thing as above, with a property that does not get lazy parsed, 100 // Test the same thing as above, with a property that does not get lazy parsed,
101 // to ensure that we perform the optimization where possible. 101 // to ensure that we perform the optimization where possible.
102 TEST_F(CSSLazyParsingTest, ShouldConsiderForMatchingRulesSimple) { 102 TEST_F(CSSLazyParsingTest, ShouldConsiderForMatchingRulesSimple) {
103 CSSParserContext context(HTMLStandardMode, nullptr); 103 CSSParserContext* context = CSSParserContext::create(HTMLStandardMode);
104 StyleSheetContents* styleSheet = StyleSheetContents::create(context); 104 StyleSheetContents* styleSheet = StyleSheetContents::create(context);
105 105
106 String sheetText = "p::before { ,badness, } "; 106 String sheetText = "p::before { ,badness, } ";
107 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */); 107 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */);
108 108
109 StyleRule* rule = ruleAt(styleSheet, 0); 109 StyleRule* rule = ruleAt(styleSheet, 0);
110 EXPECT_TRUE(hasParsedProperties(rule)); 110 EXPECT_TRUE(hasParsedProperties(rule));
111 EXPECT_FALSE( 111 EXPECT_FALSE(
112 rule->shouldConsiderForMatchingRules(false /* includeEmptyRules */)); 112 rule->shouldConsiderForMatchingRules(false /* includeEmptyRules */));
113 } 113 }
114 114
115 // Regression test for crbug.com/660290 where we change the underlying owning 115 // Regression test for crbug.com/660290 where we change the underlying owning
116 // document from the StyleSheetContents without changing the UseCounter. This 116 // document from the StyleSheetContents without changing the UseCounter. This
117 // test ensures that the new UseCounter is used when doing new parsing work. 117 // test ensures that the new UseCounter is used when doing new parsing work.
118 TEST_F(CSSLazyParsingTest, ChangeDocuments) { 118 TEST_F(CSSLazyParsingTest, ChangeDocuments) {
119 std::unique_ptr<DummyPageHolder> dummyHolder = 119 std::unique_ptr<DummyPageHolder> dummyHolder =
120 DummyPageHolder::create(IntSize(500, 500)); 120 DummyPageHolder::create(IntSize(500, 500));
121 CSSParserContext context(HTMLStandardMode, 121 CSSParserContext* context = CSSParserContext::create(
122 UseCounter::getFrom(&dummyHolder->document())); 122 HTMLStandardMode, CSSParserContext::DynamicProfile,
123 UseCounter::getFrom(&dummyHolder->document()));
123 m_cachedContents = StyleSheetContents::create(context); 124 m_cachedContents = StyleSheetContents::create(context);
124 { 125 {
125 CSSStyleSheet* sheet = 126 CSSStyleSheet* sheet =
126 CSSStyleSheet::create(m_cachedContents, dummyHolder->document()); 127 CSSStyleSheet::create(m_cachedContents, dummyHolder->document());
127 DCHECK(sheet); 128 DCHECK(sheet);
128 129
129 String sheetText = "body { background-color: red; } p { color: orange; }"; 130 String sheetText = "body { background-color: red; } p { color: orange; }";
130 CSSParser::parseSheet(context, m_cachedContents, sheetText, 131 CSSParser::parseSheet(context, m_cachedContents, sheetText,
131 true /* lazy parse */); 132 true /* lazy parse */);
132 133
(...skipping 29 matching lines...) Expand all
162 rule2->properties(); 163 rule2->properties();
163 EXPECT_TRUE(hasParsedProperties(rule2)); 164 EXPECT_TRUE(hasParsedProperties(rule2));
164 165
165 UseCounter& useCounter2 = dummyHolder2->document().frameHost()->useCounter(); 166 UseCounter& useCounter2 = dummyHolder2->document().frameHost()->useCounter();
166 EXPECT_TRUE(sheet2); 167 EXPECT_TRUE(sheet2);
167 EXPECT_TRUE(useCounter2.isCounted(CSSPropertyColor)); 168 EXPECT_TRUE(useCounter2.isCounted(CSSPropertyColor));
168 EXPECT_FALSE(useCounter2.isCounted(CSSPropertyBackgroundColor)); 169 EXPECT_FALSE(useCounter2.isCounted(CSSPropertyBackgroundColor));
169 } 170 }
170 171
171 TEST_F(CSSLazyParsingTest, SimpleRuleUsagePercent) { 172 TEST_F(CSSLazyParsingTest, SimpleRuleUsagePercent) {
172 CSSParserContext context(HTMLStandardMode, nullptr); 173 CSSParserContext* context = CSSParserContext::create(HTMLStandardMode);
173 StyleSheetContents* styleSheet = StyleSheetContents::create(context); 174 StyleSheetContents* styleSheet = StyleSheetContents::create(context);
174 175
175 std::string metricName = "Style.LazyUsage.Percent"; 176 std::string metricName = "Style.LazyUsage.Percent";
176 m_histogramTester.expectTotalCount(metricName, 0); 177 m_histogramTester.expectTotalCount(metricName, 0);
177 178
178 String sheetText = 179 String sheetText =
179 "body { background-color: red; }" 180 "body { background-color: red; }"
180 "p { color: blue; }" 181 "p { color: blue; }"
181 "a { color: yellow; }" 182 "a { color: yellow; }"
182 "#id { color: blue; }" 183 "#id { color: blue; }"
(...skipping 27 matching lines...) Expand all
210 // Parsing the last rule bumps both Gt90 and All buckets. 211 // Parsing the last rule bumps both Gt90 and All buckets.
211 ruleAt(styleSheet, 4)->properties(); 212 ruleAt(styleSheet, 4)->properties();
212 m_histogramTester.expectTotalCount(metricName, 7); 213 m_histogramTester.expectTotalCount(metricName, 7);
213 m_histogramTester.expectBucketCount(metricName, 214 m_histogramTester.expectBucketCount(metricName,
214 CSSLazyParsingState::UsageGt90, 1); 215 CSSLazyParsingState::UsageGt90, 1);
215 m_histogramTester.expectBucketCount(metricName, CSSLazyParsingState::UsageAll, 216 m_histogramTester.expectBucketCount(metricName, CSSLazyParsingState::UsageAll,
216 1); 217 1);
217 } 218 }
218 219
219 } // namespace blink 220 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698