| OLD | NEW |
| 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/CSSParser.h" | 9 #include "core/css/parser/CSSParser.h" |
| 9 #include "core/css/parser/CSSParserMode.h" | 10 #include "core/css/parser/CSSParserMode.h" |
| 10 #include "core/frame/FrameHost.h" | 11 #include "core/frame/FrameHost.h" |
| 11 #include "core/testing/DummyPageHolder.h" | 12 #include "core/testing/DummyPageHolder.h" |
| 12 #include "platform/heap/Heap.h" | 13 #include "platform/heap/Heap.h" |
| 14 #include "platform/testing/HistogramTester.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "wtf/text/WTFString.h" | 16 #include "wtf/text/WTFString.h" |
| 15 | 17 |
| 16 namespace blink { | 18 namespace blink { |
| 17 | 19 |
| 18 class CSSLazyParsingTest : public testing::Test { | 20 class CSSLazyParsingTest : public testing::Test { |
| 19 public: | 21 public: |
| 20 bool hasParsedProperties(StyleRule* rule) { | 22 bool hasParsedProperties(StyleRule* rule) { |
| 21 return rule->hasParsedProperties(); | 23 return rule->hasParsedProperties(); |
| 22 } | 24 } |
| 23 | 25 |
| 24 StyleRule* ruleAt(StyleSheetContents* sheet, size_t index) { | 26 StyleRule* ruleAt(StyleSheetContents* sheet, size_t index) { |
| 25 return toStyleRule(sheet->childRules()[index]); | 27 return toStyleRule(sheet->childRules()[index]); |
| 26 } | 28 } |
| 29 |
| 30 protected: |
| 31 HistogramTester m_histogramTester; |
| 27 }; | 32 }; |
| 28 | 33 |
| 29 TEST_F(CSSLazyParsingTest, Simple) { | 34 TEST_F(CSSLazyParsingTest, Simple) { |
| 30 CSSParserContext context(HTMLStandardMode, nullptr); | 35 CSSParserContext context(HTMLStandardMode, nullptr); |
| 31 StyleSheetContents* styleSheet = StyleSheetContents::create(context); | 36 StyleSheetContents* styleSheet = StyleSheetContents::create(context); |
| 32 | 37 |
| 33 String sheetText = "body { background-color: red; }"; | 38 String sheetText = "body { background-color: red; }"; |
| 34 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */); | 39 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */); |
| 35 StyleRule* rule = ruleAt(styleSheet, 0); | 40 StyleRule* rule = ruleAt(styleSheet, 0); |
| 36 EXPECT_FALSE(hasParsedProperties(rule)); | 41 EXPECT_FALSE(hasParsedProperties(rule)); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 EXPECT_TRUE(hasParsedProperties(rule2)); | 150 EXPECT_TRUE(hasParsedProperties(rule2)); |
| 146 | 151 |
| 147 UseCounter& useCounter1 = dummyHolder->document().frameHost()->useCounter(); | 152 UseCounter& useCounter1 = dummyHolder->document().frameHost()->useCounter(); |
| 148 UseCounter& useCounter2 = dummyHolder2->document().frameHost()->useCounter(); | 153 UseCounter& useCounter2 = dummyHolder2->document().frameHost()->useCounter(); |
| 149 EXPECT_TRUE(useCounter1.isCounted(CSSPropertyBackgroundColor)); | 154 EXPECT_TRUE(useCounter1.isCounted(CSSPropertyBackgroundColor)); |
| 150 EXPECT_TRUE(useCounter2.isCounted(CSSPropertyColor)); | 155 EXPECT_TRUE(useCounter2.isCounted(CSSPropertyColor)); |
| 151 EXPECT_FALSE(useCounter2.isCounted(CSSPropertyBackgroundColor)); | 156 EXPECT_FALSE(useCounter2.isCounted(CSSPropertyBackgroundColor)); |
| 152 EXPECT_FALSE(useCounter1.isCounted(CSSPropertyColor)); | 157 EXPECT_FALSE(useCounter1.isCounted(CSSPropertyColor)); |
| 153 } | 158 } |
| 154 | 159 |
| 160 TEST_F(CSSLazyParsingTest, SimpleRuleUsagePercent) { |
| 161 CSSParserContext context(HTMLStandardMode, nullptr); |
| 162 StyleSheetContents* styleSheet = StyleSheetContents::create(context); |
| 163 |
| 164 std::string metricName = "Style.LazyUsage.Percent"; |
| 165 m_histogramTester.expectTotalCount(metricName, 0); |
| 166 |
| 167 String sheetText = |
| 168 "body { background-color: red; }" |
| 169 "p { color: blue; }" |
| 170 "a { color: yellow; }" |
| 171 "#id { color: blue; }" |
| 172 "div { color: grey; }"; |
| 173 CSSParser::parseSheet(context, styleSheet, sheetText, true /* lazy parse */); |
| 174 |
| 175 m_histogramTester.expectTotalCount(metricName, 1); |
| 176 m_histogramTester.expectUniqueSample(metricName, |
| 177 CSSLazyParsingState::UsageGe0, 1); |
| 178 |
| 179 ruleAt(styleSheet, 0)->properties(); |
| 180 m_histogramTester.expectTotalCount(metricName, 2); |
| 181 m_histogramTester.expectBucketCount(metricName, |
| 182 CSSLazyParsingState::UsageGt10, 1); |
| 183 |
| 184 ruleAt(styleSheet, 1)->properties(); |
| 185 m_histogramTester.expectTotalCount(metricName, 3); |
| 186 m_histogramTester.expectBucketCount(metricName, |
| 187 CSSLazyParsingState::UsageGt25, 1); |
| 188 |
| 189 ruleAt(styleSheet, 2)->properties(); |
| 190 m_histogramTester.expectTotalCount(metricName, 4); |
| 191 m_histogramTester.expectBucketCount(metricName, |
| 192 CSSLazyParsingState::UsageGt50, 1); |
| 193 |
| 194 ruleAt(styleSheet, 3)->properties(); |
| 195 m_histogramTester.expectTotalCount(metricName, 5); |
| 196 m_histogramTester.expectBucketCount(metricName, |
| 197 CSSLazyParsingState::UsageGt75, 1); |
| 198 |
| 199 // Parsing the last rule bumps both Gt90 and All buckets. |
| 200 ruleAt(styleSheet, 4)->properties(); |
| 201 m_histogramTester.expectTotalCount(metricName, 7); |
| 202 m_histogramTester.expectBucketCount(metricName, |
| 203 CSSLazyParsingState::UsageGt90, 1); |
| 204 m_histogramTester.expectBucketCount(metricName, CSSLazyParsingState::UsageAll, |
| 205 1); |
| 206 } |
| 207 |
| 155 } // namespace blink | 208 } // namespace blink |
| OLD | NEW |