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

Unified Diff: Source/core/css/RuleFeatureSetTest.cpp

Issue 1317533002: Sibling invalidation sets (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebase Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: Source/core/css/RuleFeatureSetTest.cpp
diff --git a/Source/core/css/RuleFeatureSetTest.cpp b/Source/core/css/RuleFeatureSetTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..a2573323b8f0206963fff1589fe0187fdad47735
--- /dev/null
+++ b/Source/core/css/RuleFeatureSetTest.cpp
@@ -0,0 +1,269 @@
+
esprehn 2015/09/10 08:54:53 missing copyright
Eric Willigers 2015/09/14 07:20:23 Done.
+#include "config.h"
+#include "core/css/RuleFeature.h"
+
+#include "core/css/CSSSelectorList.h"
+#include "core/css/RuleSet.h"
+#include "core/css/StylePropertySet.h"
+#include "core/css/StyleRule.h"
+#include "core/css/parser/CSSParser.h"
+#include "core/dom/ElementTraversal.h"
+#include "core/html/HTMLBodyElement.h"
+#include "core/html/HTMLDocument.h"
+#include "core/html/HTMLElement.h"
+#include "core/html/HTMLHtmlElement.h"
+#include <gtest/gtest.h>
+
+namespace blink {
+
+class RuleFeatureSetTest : public ::testing::Test {
+public:
+ RuleFeatureSetTest()
+ {
+ }
+
+ void SetUp()
+ {
+ m_document = HTMLDocument::create();
+ RefPtrWillBeRawPtr<HTMLHtmlElement> html = HTMLHtmlElement::create(*m_document);
+ html->appendChild(HTMLBodyElement::create(*m_document));
+ m_document->appendChild(html.release());
+
+ m_document->body()->setInnerHTML("<b><i></i></b>", ASSERT_NO_EXCEPTION);
+ }
+
+ void updateInvalidationSets(const String& selectorText)
+ {
+ CSSSelectorList selectorList;
+ CSSParser::parseSelector(strictCSSParserContext(), selectorText, selectorList);
+
+ RefPtrWillBeRawPtr<StyleRule> styleRule = StyleRule::create(selectorList, MutableStylePropertySet::create(HTMLStandardMode));
+ RuleData ruleData(styleRule.get(), 0, 0, RuleHasNoSpecialState);
+ m_ruleFeatureSet.updateInvalidationSets(ruleData);
+ }
+
+ void collectInvalidationSetsForClass(InvalidationSetVector& descendant, InvalidationSetVector& sibling, const AtomicString& className) const
+ {
+ Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLElement>::firstChild(*m_document->body()));
+ m_ruleFeatureSet.collectInvalidationSetsForClass(descendant, sibling, *element, className);
+ }
+
+ WillBeHeapHashSet<AtomicString>& ensureClassSet(InvalidationSet& invalidationSet)
+ {
+ return invalidationSet.ensureClassSet();
+ }
+
+ void expectNoInvalidation(InvalidationSetVector& invalidationSets)
+ {
+ EXPECT_EQ(0u, invalidationSets.size());
+ }
+
+ void expectSelfInvalidation(InvalidationSetVector& invalidationSets)
+ {
+ EXPECT_EQ(1u, invalidationSets.size());
+ EXPECT_TRUE(invalidationSets[0]->isEmpty());
+ }
+
+ void expectClassInvalidation(const AtomicString& className, InvalidationSetVector& invalidationSets)
+ {
+ EXPECT_EQ(1u, invalidationSets.size());
+ WillBeHeapHashSet<AtomicString> classes = ensureClassSet(*invalidationSets[0]);
+ EXPECT_EQ(1u, classes.size());
+ EXPECT_TRUE(classes.contains(className));
+ }
+
+ void expectSiblingInvalidation(unsigned maxDirectAdjacentSelectors, const AtomicString& siblingName, InvalidationSetVector& invalidationSets)
+ {
+ EXPECT_EQ(1u, invalidationSets.size());
+ WillBeHeapHashSet<AtomicString> classes = ensureClassSet(*invalidationSets[0]);
+ EXPECT_EQ(1u, classes.size());
+ EXPECT_TRUE(classes.contains(siblingName));
+ EXPECT_EQ(maxDirectAdjacentSelectors, invalidationSets[0]->maxDirectAdjacentSelectors());
+ }
+
+ void expectSiblingDescendantInvalidation(unsigned maxDirectAdjacentSelectors, const AtomicString& siblingName, const AtomicString& descendantName, InvalidationSetVector& invalidationSets)
+ {
+ EXPECT_EQ(1u, invalidationSets.size());
+ WillBeHeapHashSet<AtomicString> classes = ensureClassSet(*invalidationSets[0]);
+ EXPECT_EQ(1u, classes.size());
+ EXPECT_TRUE(classes.contains(siblingName));
+ EXPECT_EQ(maxDirectAdjacentSelectors, invalidationSets[0]->maxDirectAdjacentSelectors());
+ EXPECT_FALSE(invalidationSets[0]->appliesDirectly());
+
+ WillBeHeapHashSet<AtomicString> descendantClasses = ensureClassSet(invalidationSets[0]->ensureInvalidationSet());
+ EXPECT_EQ(1u, descendantClasses.size());
+ EXPECT_TRUE(descendantClasses.contains(descendantName));
+ }
+
+ void expectClassesInvalidation(const AtomicString& firstClassName, const AtomicString& secondClassName, InvalidationSetVector& invalidationSets)
+ {
+ EXPECT_EQ(1u, invalidationSets.size());
+ WillBeHeapHashSet<AtomicString> classes = ensureClassSet(*invalidationSets[0]);
+ EXPECT_EQ(2u, classes.size());
+ EXPECT_TRUE(classes.contains(firstClassName));
+ EXPECT_TRUE(classes.contains(secondClassName));
+ }
+
+ DEFINE_INLINE_TRACE()
+ {
+#if ENABLE(OILPAN)
+ visitor->trace(m_ruleFeatureSet);
+ visitor->trace(m_document);
+#endif
+ }
+
+private:
+ RuleFeatureSet m_ruleFeatureSet;
+ RefPtrWillBePersistent<Document> m_document;
+};
+
+TEST_F(RuleFeatureSetTest, interleavedDescendantSibling)
+{
+ updateInvalidationSets(".k > .l ~ .m + .n .o + .p");
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "k");
+ expectClassInvalidation("p", descendant);
esprehn 2015/09/10 08:54:53 I think you want more than one test, having one me
Eric Willigers 2015/09/15 05:39:17 Done.
+ expectNoInvalidation(sibling);
+ }
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "l");
+ expectNoInvalidation(descendant);
+ expectSiblingDescendantInvalidation(std::numeric_limits<unsigned>::max(), "n", "p", sibling);
+ }
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "m");
+ expectNoInvalidation(descendant);
+ expectSiblingDescendantInvalidation(1, "n", "p", sibling);
+ }
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "n");
+ expectClassInvalidation("p", descendant);
+ expectNoInvalidation(sibling);
+ }
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "o");
+ expectNoInvalidation(descendant);
+ expectSiblingInvalidation(1, "p", sibling);
+ }
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "p");
+ expectSelfInvalidation(descendant);
+ expectNoInvalidation(sibling);
+ }
+}
+
+TEST_F(RuleFeatureSetTest, anySibling)
+{
+ updateInvalidationSets(":-webkit-any(.q, .r) ~ .s .t");
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "q");
+ expectNoInvalidation(descendant);
+ expectSiblingDescendantInvalidation(std::numeric_limits<unsigned>::max(), "s", "t", sibling);
+ }
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "r");
+ expectNoInvalidation(descendant);
+ expectSiblingDescendantInvalidation(std::numeric_limits<unsigned>::max(), "s", "t", sibling);
+ }
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "s");
+ expectClassInvalidation("t", descendant);
+ expectNoInvalidation(sibling);
+ }
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "t");
+ expectSelfInvalidation(descendant);
+ expectNoInvalidation(sibling);
+ }
+}
+
+TEST_F(RuleFeatureSetTest, siblingAny)
+{
+ updateInvalidationSets(".u .v ~ :-webkit-any(.w, .x)");
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "u");
+ expectClassesInvalidation("w", "x", descendant);
esprehn 2015/09/10 08:54:53 please write small tests
Eric Willigers 2015/09/15 05:39:17 Done.
+ expectNoInvalidation(sibling);
+ }
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "v");
+ expectNoInvalidation(descendant);
+ expectClassesInvalidation("w", "x", sibling);
+ }
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "w");
+ expectSelfInvalidation(descendant);
+ expectNoInvalidation(sibling);
+ }
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "x");
+ expectSelfInvalidation(descendant);
+ expectNoInvalidation(sibling);
+ }
+}
+
+TEST_F(RuleFeatureSetTest, adjacentUniversal)
+{
+ updateInvalidationSets(".a + *");
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "a");
+ expectNoInvalidation(descendant);
+ EXPECT_EQ(1u, sibling.size());
+ InvalidationSet& invalidationSet = *sibling[0];
+ EXPECT_EQ(1U, invalidationSet.maxDirectAdjacentSelectors());
+ EXPECT_TRUE(invalidationSet.appliesDirectly());
+ EXPECT_TRUE(invalidationSet.wholeSubtreeInvalid());
+ EXPECT_EQ(nullptr, invalidationSet.descendants());
+ }
+}
+
+TEST_F(RuleFeatureSetTest, siblingDescendantNegation)
+{
+ updateInvalidationSets(".a ~ .b :not(.c)");
+ {
+ InvalidationSetVector descendant;
+ InvalidationSetVector sibling;
+ collectInvalidationSetsForClass(descendant, sibling, "a");
+ expectNoInvalidation(descendant);
+ expectSiblingInvalidation(std::numeric_limits<unsigned>::max(), "b", sibling);
+
+ InvalidationSet& siblingInvalidationSet = *sibling[0];
+
+ EXPECT_FALSE(siblingInvalidationSet.appliesDirectly());
+ EXPECT_NE(nullptr, siblingInvalidationSet.descendants());
+ const InvalidationSet& siblingDescendant = *siblingInvalidationSet.descendants();
+ EXPECT_TRUE(siblingDescendant.wholeSubtreeInvalid());
+ }
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698