OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/css/resolver/SharedStyleFinder.h" |
| 6 |
| 7 #include "core/css/RuleFeature.h" |
| 8 #include "core/css/RuleSet.h" |
| 9 #include "core/css/parser/CSSParser.h" |
| 10 #include "core/dom/Document.h" |
| 11 #include "core/frame/FrameView.h" |
| 12 #include "core/testing/DummyPageHolder.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include <memory> |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 class SharedStyleFinderTest : public ::testing::Test { |
| 19 protected: |
| 20 SharedStyleFinderTest() = default; |
| 21 ~SharedStyleFinderTest() override = default; |
| 22 |
| 23 Document& document() { return m_dummyPageHolder->document(); } |
| 24 |
| 25 void setBodyContent(const String& html) |
| 26 { |
| 27 document().body()->setInnerHTML(html, ASSERT_NO_EXCEPTION); |
| 28 document().view()->updateAllLifecyclePhases(); |
| 29 } |
| 30 |
| 31 void addSelector(const String& selector) |
| 32 { |
| 33 StyleRuleBase* newRule = CSSParser::parseRule(CSSParserContext(HTMLStand
ardMode, nullptr), nullptr, selector + "{color:pink}"); |
| 34 m_ruleSet->addStyleRule(static_cast<StyleRule*>(newRule), RuleHasNoSpeci
alState); |
| 35 } |
| 36 |
| 37 void finishAddingSelectors() |
| 38 { |
| 39 m_siblingRuleSet = makeRuleSet(m_ruleSet->features().siblingRules); |
| 40 m_uncommonAttributeRuleSet = makeRuleSet(m_ruleSet->features().uncommonA
ttributeRules); |
| 41 } |
| 42 |
| 43 bool matchesUncommonAttributeRuleSet(Element& element) |
| 44 { |
| 45 return matchesRuleSet(element, m_uncommonAttributeRuleSet); |
| 46 } |
| 47 |
| 48 private: |
| 49 void SetUp() override |
| 50 { |
| 51 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| 52 m_ruleSet = RuleSet::create(); |
| 53 } |
| 54 |
| 55 static RuleSet* makeRuleSet(const HeapVector<RuleFeature>& ruleFeatures) |
| 56 { |
| 57 if (ruleFeatures.isEmpty()) |
| 58 return nullptr; |
| 59 RuleSet* ruleSet = RuleSet::create(); |
| 60 for (auto ruleFeature : ruleFeatures) |
| 61 ruleSet->addRule(ruleFeature.rule, ruleFeature.selectorIndex, RuleHa
sNoSpecialState); |
| 62 return ruleSet; |
| 63 } |
| 64 |
| 65 bool matchesRuleSet(Element& element, RuleSet* ruleSet) |
| 66 { |
| 67 if (!ruleSet) |
| 68 return false; |
| 69 |
| 70 SharedStyleFinder finder(ElementResolveContext(element), |
| 71 m_ruleSet->features(), |
| 72 m_siblingRuleSet, |
| 73 m_uncommonAttributeRuleSet, |
| 74 document().ensureStyleResolver()); |
| 75 |
| 76 return finder.matchesRuleSet(ruleSet); |
| 77 } |
| 78 |
| 79 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; |
| 80 Persistent<RuleSet> m_ruleSet; |
| 81 Persistent<RuleSet> m_siblingRuleSet; |
| 82 Persistent<RuleSet> m_uncommonAttributeRuleSet; |
| 83 }; |
| 84 |
| 85 // Selectors which only fail matching :hover/:focus/:active/:-webkit-drag are |
| 86 // considered as matching for matchesRuleSet because affectedBy bits need to be |
| 87 // set correctly on ComputedStyle objects, hence ComputedStyle may not be shared |
| 88 // with elements which would not have reached the pseudo classes during |
| 89 // matching. |
| 90 |
| 91 TEST_F(SharedStyleFinderTest, AttributeAffectedByHover) |
| 92 { |
| 93 setBodyContent("<div id=a attr></div><div id=b></div>"); |
| 94 |
| 95 addSelector("[attr]:hover"); |
| 96 finishAddingSelectors(); |
| 97 |
| 98 Element* a = document().getElementById("a"); |
| 99 Element* b = document().getElementById("b"); |
| 100 |
| 101 ASSERT_TRUE(a); |
| 102 ASSERT_TRUE(b); |
| 103 |
| 104 EXPECT_FALSE(a->hovered()); |
| 105 EXPECT_FALSE(b->hovered()); |
| 106 |
| 107 EXPECT_TRUE(matchesUncommonAttributeRuleSet(*a)); |
| 108 EXPECT_FALSE(matchesUncommonAttributeRuleSet(*b)); |
| 109 } |
| 110 |
| 111 TEST_F(SharedStyleFinderTest, AttributeAffectedByHoverNegated) |
| 112 { |
| 113 setBodyContent("<div id=a attr></div><div id=b></div>"); |
| 114 |
| 115 addSelector("[attr]:not(:hover)"); |
| 116 finishAddingSelectors(); |
| 117 |
| 118 Element* a = document().getElementById("a"); |
| 119 Element* b = document().getElementById("b"); |
| 120 |
| 121 ASSERT_TRUE(a); |
| 122 ASSERT_TRUE(b); |
| 123 |
| 124 EXPECT_FALSE(a->hovered()); |
| 125 EXPECT_FALSE(b->hovered()); |
| 126 |
| 127 EXPECT_TRUE(matchesUncommonAttributeRuleSet(*a)); |
| 128 EXPECT_FALSE(matchesUncommonAttributeRuleSet(*b)); |
| 129 } |
| 130 |
| 131 TEST_F(SharedStyleFinderTest, AttributeAffectedByFocus) |
| 132 { |
| 133 setBodyContent("<div id=a attr></div><div id=b></div>"); |
| 134 |
| 135 addSelector("[attr]:focus"); |
| 136 finishAddingSelectors(); |
| 137 |
| 138 Element* a = document().getElementById("a"); |
| 139 Element* b = document().getElementById("b"); |
| 140 |
| 141 ASSERT_TRUE(a); |
| 142 ASSERT_TRUE(b); |
| 143 |
| 144 EXPECT_FALSE(a->focused()); |
| 145 EXPECT_FALSE(b->focused()); |
| 146 |
| 147 EXPECT_TRUE(matchesUncommonAttributeRuleSet(*a)); |
| 148 EXPECT_FALSE(matchesUncommonAttributeRuleSet(*b)); |
| 149 } |
| 150 |
| 151 TEST_F(SharedStyleFinderTest, AttributeAffectedByActive) |
| 152 { |
| 153 setBodyContent("<div id=a attr></div><div id=b></div>"); |
| 154 |
| 155 addSelector("[attr]:active"); |
| 156 finishAddingSelectors(); |
| 157 |
| 158 Element* a = document().getElementById("a"); |
| 159 Element* b = document().getElementById("b"); |
| 160 |
| 161 ASSERT_TRUE(a); |
| 162 ASSERT_TRUE(b); |
| 163 |
| 164 EXPECT_FALSE(a->active()); |
| 165 EXPECT_FALSE(b->active()); |
| 166 |
| 167 EXPECT_TRUE(matchesUncommonAttributeRuleSet(*a)); |
| 168 EXPECT_FALSE(matchesUncommonAttributeRuleSet(*b)); |
| 169 } |
| 170 |
| 171 TEST_F(SharedStyleFinderTest, AttributeAffectedByDrag) |
| 172 { |
| 173 setBodyContent("<div id=a attr></div><div id=b></div>"); |
| 174 |
| 175 addSelector("[attr]:-webkit-drag"); |
| 176 finishAddingSelectors(); |
| 177 |
| 178 Element* a = document().getElementById("a"); |
| 179 Element* b = document().getElementById("b"); |
| 180 |
| 181 ASSERT_TRUE(a); |
| 182 ASSERT_TRUE(b); |
| 183 |
| 184 EXPECT_FALSE(a->isDragged()); |
| 185 EXPECT_FALSE(b->isDragged()); |
| 186 |
| 187 EXPECT_TRUE(matchesUncommonAttributeRuleSet(*a)); |
| 188 EXPECT_FALSE(matchesUncommonAttributeRuleSet(*b)); |
| 189 } |
| 190 |
| 191 } // namespace blink |
OLD | NEW |