OLD | NEW |
---|---|
(Empty) | |
1 | |
esprehn
2015/09/10 08:54:53
missing copyright
Eric Willigers
2015/09/14 07:20:23
Done.
| |
2 #include "config.h" | |
3 #include "core/css/RuleFeature.h" | |
4 | |
5 #include "core/css/CSSSelectorList.h" | |
6 #include "core/css/RuleSet.h" | |
7 #include "core/css/StylePropertySet.h" | |
8 #include "core/css/StyleRule.h" | |
9 #include "core/css/parser/CSSParser.h" | |
10 #include "core/dom/ElementTraversal.h" | |
11 #include "core/html/HTMLBodyElement.h" | |
12 #include "core/html/HTMLDocument.h" | |
13 #include "core/html/HTMLElement.h" | |
14 #include "core/html/HTMLHtmlElement.h" | |
15 #include <gtest/gtest.h> | |
16 | |
17 namespace blink { | |
18 | |
19 class RuleFeatureSetTest : public ::testing::Test { | |
20 public: | |
21 RuleFeatureSetTest() | |
22 { | |
23 } | |
24 | |
25 void SetUp() | |
26 { | |
27 m_document = HTMLDocument::create(); | |
28 RefPtrWillBeRawPtr<HTMLHtmlElement> html = HTMLHtmlElement::create(*m_do cument); | |
29 html->appendChild(HTMLBodyElement::create(*m_document)); | |
30 m_document->appendChild(html.release()); | |
31 | |
32 m_document->body()->setInnerHTML("<b><i></i></b>", ASSERT_NO_EXCEPTION); | |
33 } | |
34 | |
35 void updateInvalidationSets(const String& selectorText) | |
36 { | |
37 CSSSelectorList selectorList; | |
38 CSSParser::parseSelector(strictCSSParserContext(), selectorText, selecto rList); | |
39 | |
40 RefPtrWillBeRawPtr<StyleRule> styleRule = StyleRule::create(selectorList , MutableStylePropertySet::create(HTMLStandardMode)); | |
41 RuleData ruleData(styleRule.get(), 0, 0, RuleHasNoSpecialState); | |
42 m_ruleFeatureSet.updateInvalidationSets(ruleData); | |
43 } | |
44 | |
45 void collectInvalidationSetsForClass(InvalidationSetVector& descendant, Inva lidationSetVector& sibling, const AtomicString& className) const | |
46 { | |
47 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle ment>::firstChild(*m_document->body())); | |
48 m_ruleFeatureSet.collectInvalidationSetsForClass(descendant, sibling, *e lement, className); | |
49 } | |
50 | |
51 WillBeHeapHashSet<AtomicString>& ensureClassSet(InvalidationSet& invalidatio nSet) | |
52 { | |
53 return invalidationSet.ensureClassSet(); | |
54 } | |
55 | |
56 void expectNoInvalidation(InvalidationSetVector& invalidationSets) | |
57 { | |
58 EXPECT_EQ(0u, invalidationSets.size()); | |
59 } | |
60 | |
61 void expectSelfInvalidation(InvalidationSetVector& invalidationSets) | |
62 { | |
63 EXPECT_EQ(1u, invalidationSets.size()); | |
64 EXPECT_TRUE(invalidationSets[0]->isEmpty()); | |
65 } | |
66 | |
67 void expectClassInvalidation(const AtomicString& className, InvalidationSetV ector& invalidationSets) | |
68 { | |
69 EXPECT_EQ(1u, invalidationSets.size()); | |
70 WillBeHeapHashSet<AtomicString> classes = ensureClassSet(*invalidationSe ts[0]); | |
71 EXPECT_EQ(1u, classes.size()); | |
72 EXPECT_TRUE(classes.contains(className)); | |
73 } | |
74 | |
75 void expectSiblingInvalidation(unsigned maxDirectAdjacentSelectors, const At omicString& siblingName, InvalidationSetVector& invalidationSets) | |
76 { | |
77 EXPECT_EQ(1u, invalidationSets.size()); | |
78 WillBeHeapHashSet<AtomicString> classes = ensureClassSet(*invalidationSe ts[0]); | |
79 EXPECT_EQ(1u, classes.size()); | |
80 EXPECT_TRUE(classes.contains(siblingName)); | |
81 EXPECT_EQ(maxDirectAdjacentSelectors, invalidationSets[0]->maxDirectAdja centSelectors()); | |
82 } | |
83 | |
84 void expectSiblingDescendantInvalidation(unsigned maxDirectAdjacentSelectors , const AtomicString& siblingName, const AtomicString& descendantName, Invalidat ionSetVector& invalidationSets) | |
85 { | |
86 EXPECT_EQ(1u, invalidationSets.size()); | |
87 WillBeHeapHashSet<AtomicString> classes = ensureClassSet(*invalidationSe ts[0]); | |
88 EXPECT_EQ(1u, classes.size()); | |
89 EXPECT_TRUE(classes.contains(siblingName)); | |
90 EXPECT_EQ(maxDirectAdjacentSelectors, invalidationSets[0]->maxDirectAdja centSelectors()); | |
91 EXPECT_FALSE(invalidationSets[0]->appliesDirectly()); | |
92 | |
93 WillBeHeapHashSet<AtomicString> descendantClasses = ensureClassSet(inval idationSets[0]->ensureInvalidationSet()); | |
94 EXPECT_EQ(1u, descendantClasses.size()); | |
95 EXPECT_TRUE(descendantClasses.contains(descendantName)); | |
96 } | |
97 | |
98 void expectClassesInvalidation(const AtomicString& firstClassName, const Ato micString& secondClassName, InvalidationSetVector& invalidationSets) | |
99 { | |
100 EXPECT_EQ(1u, invalidationSets.size()); | |
101 WillBeHeapHashSet<AtomicString> classes = ensureClassSet(*invalidationSe ts[0]); | |
102 EXPECT_EQ(2u, classes.size()); | |
103 EXPECT_TRUE(classes.contains(firstClassName)); | |
104 EXPECT_TRUE(classes.contains(secondClassName)); | |
105 } | |
106 | |
107 DEFINE_INLINE_TRACE() | |
108 { | |
109 #if ENABLE(OILPAN) | |
110 visitor->trace(m_ruleFeatureSet); | |
111 visitor->trace(m_document); | |
112 #endif | |
113 } | |
114 | |
115 private: | |
116 RuleFeatureSet m_ruleFeatureSet; | |
117 RefPtrWillBePersistent<Document> m_document; | |
118 }; | |
119 | |
120 TEST_F(RuleFeatureSetTest, interleavedDescendantSibling) | |
121 { | |
122 updateInvalidationSets(".k > .l ~ .m + .n .o + .p"); | |
123 { | |
124 InvalidationSetVector descendant; | |
125 InvalidationSetVector sibling; | |
126 collectInvalidationSetsForClass(descendant, sibling, "k"); | |
127 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.
| |
128 expectNoInvalidation(sibling); | |
129 } | |
130 { | |
131 InvalidationSetVector descendant; | |
132 InvalidationSetVector sibling; | |
133 collectInvalidationSetsForClass(descendant, sibling, "l"); | |
134 expectNoInvalidation(descendant); | |
135 expectSiblingDescendantInvalidation(std::numeric_limits<unsigned>::max() , "n", "p", sibling); | |
136 } | |
137 { | |
138 InvalidationSetVector descendant; | |
139 InvalidationSetVector sibling; | |
140 collectInvalidationSetsForClass(descendant, sibling, "m"); | |
141 expectNoInvalidation(descendant); | |
142 expectSiblingDescendantInvalidation(1, "n", "p", sibling); | |
143 } | |
144 { | |
145 InvalidationSetVector descendant; | |
146 InvalidationSetVector sibling; | |
147 collectInvalidationSetsForClass(descendant, sibling, "n"); | |
148 expectClassInvalidation("p", descendant); | |
149 expectNoInvalidation(sibling); | |
150 } | |
151 { | |
152 InvalidationSetVector descendant; | |
153 InvalidationSetVector sibling; | |
154 collectInvalidationSetsForClass(descendant, sibling, "o"); | |
155 expectNoInvalidation(descendant); | |
156 expectSiblingInvalidation(1, "p", sibling); | |
157 } | |
158 { | |
159 InvalidationSetVector descendant; | |
160 InvalidationSetVector sibling; | |
161 collectInvalidationSetsForClass(descendant, sibling, "p"); | |
162 expectSelfInvalidation(descendant); | |
163 expectNoInvalidation(sibling); | |
164 } | |
165 } | |
166 | |
167 TEST_F(RuleFeatureSetTest, anySibling) | |
168 { | |
169 updateInvalidationSets(":-webkit-any(.q, .r) ~ .s .t"); | |
170 { | |
171 InvalidationSetVector descendant; | |
172 InvalidationSetVector sibling; | |
173 collectInvalidationSetsForClass(descendant, sibling, "q"); | |
174 expectNoInvalidation(descendant); | |
175 expectSiblingDescendantInvalidation(std::numeric_limits<unsigned>::max() , "s", "t", sibling); | |
176 } | |
177 { | |
178 InvalidationSetVector descendant; | |
179 InvalidationSetVector sibling; | |
180 collectInvalidationSetsForClass(descendant, sibling, "r"); | |
181 expectNoInvalidation(descendant); | |
182 expectSiblingDescendantInvalidation(std::numeric_limits<unsigned>::max() , "s", "t", sibling); | |
183 } | |
184 { | |
185 InvalidationSetVector descendant; | |
186 InvalidationSetVector sibling; | |
187 collectInvalidationSetsForClass(descendant, sibling, "s"); | |
188 expectClassInvalidation("t", descendant); | |
189 expectNoInvalidation(sibling); | |
190 } | |
191 { | |
192 InvalidationSetVector descendant; | |
193 InvalidationSetVector sibling; | |
194 collectInvalidationSetsForClass(descendant, sibling, "t"); | |
195 expectSelfInvalidation(descendant); | |
196 expectNoInvalidation(sibling); | |
197 } | |
198 } | |
199 | |
200 TEST_F(RuleFeatureSetTest, siblingAny) | |
201 { | |
202 updateInvalidationSets(".u .v ~ :-webkit-any(.w, .x)"); | |
203 { | |
204 InvalidationSetVector descendant; | |
205 InvalidationSetVector sibling; | |
206 collectInvalidationSetsForClass(descendant, sibling, "u"); | |
207 expectClassesInvalidation("w", "x", descendant); | |
esprehn
2015/09/10 08:54:53
please write small tests
Eric Willigers
2015/09/15 05:39:17
Done.
| |
208 expectNoInvalidation(sibling); | |
209 } | |
210 { | |
211 InvalidationSetVector descendant; | |
212 InvalidationSetVector sibling; | |
213 collectInvalidationSetsForClass(descendant, sibling, "v"); | |
214 expectNoInvalidation(descendant); | |
215 expectClassesInvalidation("w", "x", sibling); | |
216 } | |
217 { | |
218 InvalidationSetVector descendant; | |
219 InvalidationSetVector sibling; | |
220 collectInvalidationSetsForClass(descendant, sibling, "w"); | |
221 expectSelfInvalidation(descendant); | |
222 expectNoInvalidation(sibling); | |
223 } | |
224 { | |
225 InvalidationSetVector descendant; | |
226 InvalidationSetVector sibling; | |
227 collectInvalidationSetsForClass(descendant, sibling, "x"); | |
228 expectSelfInvalidation(descendant); | |
229 expectNoInvalidation(sibling); | |
230 } | |
231 } | |
232 | |
233 TEST_F(RuleFeatureSetTest, adjacentUniversal) | |
234 { | |
235 updateInvalidationSets(".a + *"); | |
236 { | |
237 InvalidationSetVector descendant; | |
238 InvalidationSetVector sibling; | |
239 collectInvalidationSetsForClass(descendant, sibling, "a"); | |
240 expectNoInvalidation(descendant); | |
241 EXPECT_EQ(1u, sibling.size()); | |
242 InvalidationSet& invalidationSet = *sibling[0]; | |
243 EXPECT_EQ(1U, invalidationSet.maxDirectAdjacentSelectors()); | |
244 EXPECT_TRUE(invalidationSet.appliesDirectly()); | |
245 EXPECT_TRUE(invalidationSet.wholeSubtreeInvalid()); | |
246 EXPECT_EQ(nullptr, invalidationSet.descendants()); | |
247 } | |
248 } | |
249 | |
250 TEST_F(RuleFeatureSetTest, siblingDescendantNegation) | |
251 { | |
252 updateInvalidationSets(".a ~ .b :not(.c)"); | |
253 { | |
254 InvalidationSetVector descendant; | |
255 InvalidationSetVector sibling; | |
256 collectInvalidationSetsForClass(descendant, sibling, "a"); | |
257 expectNoInvalidation(descendant); | |
258 expectSiblingInvalidation(std::numeric_limits<unsigned>::max(), "b", sib ling); | |
259 | |
260 InvalidationSet& siblingInvalidationSet = *sibling[0]; | |
261 | |
262 EXPECT_FALSE(siblingInvalidationSet.appliesDirectly()); | |
263 EXPECT_NE(nullptr, siblingInvalidationSet.descendants()); | |
264 const InvalidationSet& siblingDescendant = *siblingInvalidationSet.desce ndants(); | |
265 EXPECT_TRUE(siblingDescendant.wholeSubtreeInvalid()); | |
266 } | |
267 } | |
268 | |
269 } // namespace blink | |
OLD | NEW |