OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/css/RuleFeature.h" |
| 7 |
| 8 #include "core/css/CSSSelectorList.h" |
| 9 #include "core/css/RuleSet.h" |
| 10 #include "core/css/StylePropertySet.h" |
| 11 #include "core/css/StyleRule.h" |
| 12 #include "core/css/invalidation/InvalidationSet.h" |
| 13 #include "core/css/parser/CSSParser.h" |
| 14 #include "core/dom/ElementTraversal.h" |
| 15 #include "core/html/HTMLBodyElement.h" |
| 16 #include "core/html/HTMLDocument.h" |
| 17 #include "core/html/HTMLElement.h" |
| 18 #include "core/html/HTMLHtmlElement.h" |
| 19 #include <gtest/gtest.h> |
| 20 |
| 21 namespace blink { |
| 22 |
| 23 class RuleFeatureSetTest : public ::testing::Test { |
| 24 public: |
| 25 RuleFeatureSetTest() |
| 26 { |
| 27 } |
| 28 |
| 29 void SetUp() |
| 30 { |
| 31 m_document = HTMLDocument::create(); |
| 32 RefPtrWillBeRawPtr<HTMLHtmlElement> html = HTMLHtmlElement::create(*m_do
cument); |
| 33 html->appendChild(HTMLBodyElement::create(*m_document)); |
| 34 m_document->appendChild(html.release()); |
| 35 |
| 36 m_document->body()->setInnerHTML("<b><i></i></b>", ASSERT_NO_EXCEPTION); |
| 37 } |
| 38 |
| 39 void updateInvalidationSets(const String& selectorText) |
| 40 { |
| 41 CSSSelectorList selectorList; |
| 42 CSSParser::parseSelector(strictCSSParserContext(), selectorText, selecto
rList); |
| 43 |
| 44 RefPtrWillBeRawPtr<StyleRule> styleRule = StyleRule::create(selectorList
, MutableStylePropertySet::create(HTMLStandardMode)); |
| 45 RuleData ruleData(styleRule.get(), 0, 0, RuleHasNoSpecialState); |
| 46 m_ruleFeatureSet.updateInvalidationSets(ruleData); |
| 47 } |
| 48 |
| 49 void collectInvalidationSetsForClass(InvalidationLists& invalidationLists, c
onst AtomicString& className) const |
| 50 { |
| 51 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); |
| 52 m_ruleFeatureSet.collectInvalidationSetsForClass(invalidationLists, *ele
ment, className); |
| 53 } |
| 54 |
| 55 void collectInvalidationSetsForId(InvalidationLists& invalidationLists, cons
t AtomicString& id) const |
| 56 { |
| 57 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); |
| 58 m_ruleFeatureSet.collectInvalidationSetsForId(invalidationLists, *elemen
t, id); |
| 59 } |
| 60 |
| 61 void collectInvalidationSetsForAttribute(InvalidationLists& invalidationList
s, const QualifiedName& attributeName) const |
| 62 { |
| 63 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); |
| 64 m_ruleFeatureSet.collectInvalidationSetsForAttribute(invalidationLists,
*element, attributeName); |
| 65 } |
| 66 |
| 67 void collectInvalidationSetsForPseudoClass(InvalidationLists& invalidationLi
sts, CSSSelector::PseudoType pseudo) const |
| 68 { |
| 69 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); |
| 70 m_ruleFeatureSet.collectInvalidationSetsForPseudoClass(invalidationLists
, *element, pseudo); |
| 71 } |
| 72 |
| 73 const HashSet<AtomicString>& classSet(const InvalidationSet& invalidationSet
) |
| 74 { |
| 75 return invalidationSet.classSetForTesting(); |
| 76 } |
| 77 |
| 78 const HashSet<AtomicString>& idSet(const InvalidationSet& invalidationSet) |
| 79 { |
| 80 return invalidationSet.idSetForTesting(); |
| 81 } |
| 82 |
| 83 const HashSet<AtomicString>& tagNameSet(const InvalidationSet& invalidationS
et) |
| 84 { |
| 85 return invalidationSet.tagNameSetForTesting(); |
| 86 } |
| 87 |
| 88 const HashSet<AtomicString>& attributeSet(const InvalidationSet& invalidatio
nSet) |
| 89 { |
| 90 return invalidationSet.attributeSetForTesting(); |
| 91 } |
| 92 |
| 93 void expectNoInvalidation(InvalidationSetVector& invalidationSets) |
| 94 { |
| 95 EXPECT_EQ(0u, invalidationSets.size()); |
| 96 } |
| 97 |
| 98 void expectSelfInvalidation(InvalidationSetVector& invalidationSets) |
| 99 { |
| 100 EXPECT_EQ(1u, invalidationSets.size()); |
| 101 EXPECT_TRUE(invalidationSets[0]->invalidatesSelf()); |
| 102 } |
| 103 |
| 104 void expectClassInvalidation(const AtomicString& className, InvalidationSetV
ector& invalidationSets) |
| 105 { |
| 106 EXPECT_EQ(1u, invalidationSets.size()); |
| 107 HashSet<AtomicString> classes = classSet(*invalidationSets[0]); |
| 108 EXPECT_EQ(1u, classes.size()); |
| 109 EXPECT_TRUE(classes.contains(className)); |
| 110 } |
| 111 |
| 112 void expectSiblingInvalidation(unsigned maxDirectAdjacentSelectors, const At
omicString& siblingName, InvalidationSetVector& invalidationSets) |
| 113 { |
| 114 EXPECT_EQ(1u, invalidationSets.size()); |
| 115 const SiblingInvalidationSet& siblingInvalidationSet = toSiblingInvalida
tionSet(*invalidationSets[0]); |
| 116 HashSet<AtomicString> classes = classSet(siblingInvalidationSet); |
| 117 EXPECT_EQ(1u, classes.size()); |
| 118 EXPECT_TRUE(classes.contains(siblingName)); |
| 119 EXPECT_EQ(maxDirectAdjacentSelectors, siblingInvalidationSet.maxDirectAd
jacentSelectors()); |
| 120 } |
| 121 |
| 122 void expectSiblingDescendantInvalidation(unsigned maxDirectAdjacentSelectors
, const AtomicString& siblingName, const AtomicString& descendantName, Invalidat
ionSetVector& invalidationSets) |
| 123 { |
| 124 EXPECT_EQ(1u, invalidationSets.size()); |
| 125 const SiblingInvalidationSet& siblingInvalidationSet = toSiblingInvalida
tionSet(*invalidationSets[0]); |
| 126 HashSet<AtomicString> classes = classSet(siblingInvalidationSet); |
| 127 EXPECT_EQ(1u, classes.size()); |
| 128 EXPECT_TRUE(classes.contains(siblingName)); |
| 129 EXPECT_EQ(maxDirectAdjacentSelectors, siblingInvalidationSet.maxDirectAd
jacentSelectors()); |
| 130 |
| 131 HashSet<AtomicString> descendantClasses = classSet(siblingInvalidationSe
t.descendants()); |
| 132 EXPECT_EQ(1u, descendantClasses.size()); |
| 133 EXPECT_TRUE(descendantClasses.contains(descendantName)); |
| 134 } |
| 135 |
| 136 void expectClassesInvalidation(const AtomicString& firstClassName, const Ato
micString& secondClassName, InvalidationSetVector& invalidationSets) |
| 137 { |
| 138 EXPECT_EQ(1u, invalidationSets.size()); |
| 139 HashSet<AtomicString> classes = classSet(*invalidationSets[0]); |
| 140 EXPECT_EQ(2u, classes.size()); |
| 141 EXPECT_TRUE(classes.contains(firstClassName)); |
| 142 EXPECT_TRUE(classes.contains(secondClassName)); |
| 143 } |
| 144 |
| 145 void expectIdInvalidation(const AtomicString& id, InvalidationSetVector& inv
alidationSets) |
| 146 { |
| 147 EXPECT_EQ(1u, invalidationSets.size()); |
| 148 HashSet<AtomicString> ids = idSet(*invalidationSets[0]); |
| 149 EXPECT_EQ(1u, ids.size()); |
| 150 EXPECT_TRUE(ids.contains(id)); |
| 151 } |
| 152 |
| 153 void expectTagNameInvalidation(const AtomicString& tagName, InvalidationSetV
ector& invalidationSets) |
| 154 { |
| 155 EXPECT_EQ(1u, invalidationSets.size()); |
| 156 HashSet<AtomicString> tagNames = tagNameSet(*invalidationSets[0]); |
| 157 EXPECT_EQ(1u, tagNames.size()); |
| 158 EXPECT_TRUE(tagNames.contains(tagName)); |
| 159 } |
| 160 |
| 161 void expectAttributeInvalidation(const AtomicString& attribute, Invalidation
SetVector& invalidationSets) |
| 162 { |
| 163 EXPECT_EQ(1u, invalidationSets.size()); |
| 164 HashSet<AtomicString> attributes = attributeSet(*invalidationSets[0]); |
| 165 EXPECT_EQ(1u, attributes.size()); |
| 166 EXPECT_TRUE(attributes.contains(attribute)); |
| 167 } |
| 168 |
| 169 DEFINE_INLINE_TRACE() |
| 170 { |
| 171 #if ENABLE(OILPAN) |
| 172 visitor->trace(m_ruleFeatureSet); |
| 173 visitor->trace(m_document); |
| 174 #endif |
| 175 } |
| 176 |
| 177 private: |
| 178 RuleFeatureSet m_ruleFeatureSet; |
| 179 RefPtrWillBePersistent<Document> m_document; |
| 180 }; |
| 181 |
| 182 TEST_F(RuleFeatureSetTest, interleavedDescendantSibling1) |
| 183 { |
| 184 updateInvalidationSets(".p"); |
| 185 |
| 186 InvalidationLists invalidationLists; |
| 187 collectInvalidationSetsForClass(invalidationLists, "p"); |
| 188 expectSelfInvalidation(invalidationLists.descendants); |
| 189 expectNoInvalidation(invalidationLists.siblings); |
| 190 } |
| 191 |
| 192 TEST_F(RuleFeatureSetTest, interleavedDescendantSibling2) |
| 193 { |
| 194 updateInvalidationSets(".o + .p"); |
| 195 |
| 196 InvalidationLists invalidationLists; |
| 197 collectInvalidationSetsForClass(invalidationLists, "o"); |
| 198 expectNoInvalidation(invalidationLists.descendants); |
| 199 expectSiblingInvalidation(1, "p", invalidationLists.siblings); |
| 200 } |
| 201 |
| 202 TEST_F(RuleFeatureSetTest, interleavedDescendantSibling3) |
| 203 { |
| 204 updateInvalidationSets(".n .o + .p"); |
| 205 |
| 206 InvalidationLists invalidationLists; |
| 207 collectInvalidationSetsForClass(invalidationLists, "n"); |
| 208 expectClassInvalidation("p", invalidationLists.descendants); |
| 209 expectNoInvalidation(invalidationLists.siblings); |
| 210 } |
| 211 |
| 212 TEST_F(RuleFeatureSetTest, interleavedDescendantSibling4) |
| 213 { |
| 214 updateInvalidationSets(".m + .n .o + .p"); |
| 215 |
| 216 InvalidationLists invalidationLists; |
| 217 collectInvalidationSetsForClass(invalidationLists, "m"); |
| 218 expectNoInvalidation(invalidationLists.descendants); |
| 219 expectSiblingDescendantInvalidation(1, "n", "p", invalidationLists.siblings)
; |
| 220 } |
| 221 |
| 222 TEST_F(RuleFeatureSetTest, interleavedDescendantSibling5) |
| 223 { |
| 224 updateInvalidationSets(".l ~ .m + .n .o + .p"); |
| 225 |
| 226 InvalidationLists invalidationLists; |
| 227 collectInvalidationSetsForClass(invalidationLists, "l"); |
| 228 expectNoInvalidation(invalidationLists.descendants); |
| 229 expectSiblingDescendantInvalidation(std::numeric_limits<unsigned>::max(), "n
", "p", invalidationLists.siblings); |
| 230 } |
| 231 |
| 232 TEST_F(RuleFeatureSetTest, interleavedDescendantSibling6) |
| 233 { |
| 234 updateInvalidationSets(".k > .l ~ .m + .n .o + .p"); |
| 235 |
| 236 InvalidationLists invalidationLists; |
| 237 collectInvalidationSetsForClass(invalidationLists, "k"); |
| 238 expectClassInvalidation("p", invalidationLists.descendants); |
| 239 expectNoInvalidation(invalidationLists.siblings); |
| 240 } |
| 241 |
| 242 |
| 243 TEST_F(RuleFeatureSetTest, anySibling) |
| 244 { |
| 245 updateInvalidationSets(":-webkit-any(.q, .r) ~ .s .t"); |
| 246 |
| 247 InvalidationLists invalidationLists; |
| 248 collectInvalidationSetsForClass(invalidationLists, "q"); |
| 249 expectNoInvalidation(invalidationLists.descendants); |
| 250 expectSiblingDescendantInvalidation(std::numeric_limits<unsigned>::max(), "s
", "t", invalidationLists.siblings); |
| 251 } |
| 252 |
| 253 TEST_F(RuleFeatureSetTest, any) |
| 254 { |
| 255 updateInvalidationSets(":-webkit-any(.w, .x)"); |
| 256 |
| 257 InvalidationLists invalidationLists; |
| 258 collectInvalidationSetsForClass(invalidationLists, "w"); |
| 259 expectSelfInvalidation(invalidationLists.descendants); |
| 260 expectNoInvalidation(invalidationLists.siblings); |
| 261 } |
| 262 |
| 263 TEST_F(RuleFeatureSetTest, siblingAny) |
| 264 { |
| 265 updateInvalidationSets(".v ~ :-webkit-any(.w, .x)"); |
| 266 |
| 267 InvalidationLists invalidationLists; |
| 268 collectInvalidationSetsForClass(invalidationLists, "v"); |
| 269 expectNoInvalidation(invalidationLists.descendants); |
| 270 expectClassesInvalidation("w", "x", invalidationLists.siblings); |
| 271 } |
| 272 |
| 273 TEST_F(RuleFeatureSetTest, descendantSiblingAny) |
| 274 { |
| 275 updateInvalidationSets(".u .v ~ :-webkit-any(.w, .x)"); |
| 276 |
| 277 InvalidationLists invalidationLists; |
| 278 collectInvalidationSetsForClass(invalidationLists, "u"); |
| 279 expectClassesInvalidation("w", "x", invalidationLists.descendants); |
| 280 expectNoInvalidation(invalidationLists.siblings); |
| 281 } |
| 282 |
| 283 TEST_F(RuleFeatureSetTest, id) |
| 284 { |
| 285 updateInvalidationSets("#a #b"); |
| 286 |
| 287 InvalidationLists invalidationLists; |
| 288 collectInvalidationSetsForId(invalidationLists, "a"); |
| 289 expectIdInvalidation("b", invalidationLists.descendants); |
| 290 } |
| 291 |
| 292 TEST_F(RuleFeatureSetTest, attribute) |
| 293 { |
| 294 updateInvalidationSets("[c] [d]"); |
| 295 |
| 296 InvalidationLists invalidationLists; |
| 297 collectInvalidationSetsForAttribute(invalidationLists, QualifiedName("", "c"
, "")); |
| 298 expectAttributeInvalidation("d", invalidationLists.descendants); |
| 299 } |
| 300 |
| 301 TEST_F(RuleFeatureSetTest, pseudoClass) |
| 302 { |
| 303 updateInvalidationSets(":valid"); |
| 304 |
| 305 InvalidationLists invalidationLists; |
| 306 collectInvalidationSetsForPseudoClass(invalidationLists, CSSSelector::Pseudo
Valid); |
| 307 expectSelfInvalidation(invalidationLists.descendants); |
| 308 } |
| 309 |
| 310 TEST_F(RuleFeatureSetTest, tagName) |
| 311 { |
| 312 updateInvalidationSets(":valid e"); |
| 313 |
| 314 InvalidationLists invalidationLists; |
| 315 collectInvalidationSetsForPseudoClass(invalidationLists, CSSSelector::Pseudo
Valid); |
| 316 expectTagNameInvalidation("e", invalidationLists.descendants); |
| 317 } |
| 318 |
| 319 } // namespace blink |
OLD | NEW |