1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "core/HTMLNames.h"
#include "core/dom/Element.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/NodeRenderStyle.h"
#include "core/dom/StyleEngine.h"
#include "core/frame/FrameView.h"
#include "core/html/HTMLDocument.h"
#include "core/html/HTMLElement.h"
#include "core/testing/DummyPageHolder.h"
#include <gtest/gtest.h>
using namespace blink;
using namespace HTMLNames;
namespace {
class AffectedByFocusTest : public ::testing::Test {
protected:
struct ElementResult {
const blink::HTMLQualifiedName tag;
bool affectedBy;
bool childrenOrSiblingsAffectedBy;
};
virtual void SetUp() override;
HTMLDocument& document() const { return *m_document; }
void setHtmlInnerHTML(const char* htmlContent);
void checkElements(ElementResult expected[], unsigned expectedCount) const;
private:
OwnPtr<DummyPageHolder> m_dummyPageHolder;
HTMLDocument* m_document;
};
void AffectedByFocusTest::SetUp()
{
m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600));
m_document = toHTMLDocument(&m_dummyPageHolder->document());
ASSERT(m_document);
}
void AffectedByFocusTest::setHtmlInnerHTML(const char* htmlContent)
{
document().documentElement()->setInnerHTML(String::fromUTF8(htmlContent), ASSERT_NO_EXCEPTION);
document().view()->updateLayoutAndStyleIfNeededRecursive();
}
void AffectedByFocusTest::checkElements(ElementResult expected[], unsigned expectedCount) const
{
unsigned i = 0;
HTMLElement* element = document().body();
for (; element && i < expectedCount; element = Traversal<HTMLElement>::next(*element), ++i) {
ASSERT_TRUE(element->hasTagName(expected[i].tag));
ASSERT(element->renderStyle());
ASSERT_EQ(expected[i].affectedBy, element->renderStyle()->affectedByFocus());
ASSERT_EQ(expected[i].childrenOrSiblingsAffectedBy, element->childrenOrSiblingsAffectedByFocus());
}
ASSERT(!element && i == expectedCount);
}
// A global :focus rule in html.css currently causes every single element to be
// affectedByFocus. Check that all elements in a document with no :focus rules
// gets the affectedByFocus set on RenderStyle and not childrenOrSiblingsAffectedByFocus.
TEST_F(AffectedByFocusTest, UAUniversalFocusRule)
{
ElementResult expected[] = {
{ bodyTag, true, false },
{ divTag, true, false },
{ divTag, true, false },
{ divTag, true, false },
{ spanTag, true, false }
};
setHtmlInnerHTML("<body>"
"<div><div></div></div>"
"<div><span></span></div>"
"</body>");
checkElements(expected, sizeof(expected) / sizeof(ElementResult));
}
// ":focus div" will mark ascendants of all divs with childrenOrSiblingsAffectedByFocus.
TEST_F(AffectedByFocusTest, FocusedAscendant)
{
ElementResult expected[] = {
{ bodyTag, true, true },
{ divTag, true, true },
{ divTag, true, false },
{ divTag, true, false },
{ spanTag, true, false }
};
setHtmlInnerHTML("<head>"
"<style>:focus div { background-color: pink }</style>"
"</head>"
"<body>"
"<div><div></div></div>"
"<div><span></span></div>"
"</body>");
checkElements(expected, sizeof(expected) / sizeof(ElementResult));
}
// "body:focus div" will mark the body element with childrenOrSiblingsAffectedByFocus.
TEST_F(AffectedByFocusTest, FocusedAscendantWithType)
{
ElementResult expected[] = {
{ bodyTag, true, true },
{ divTag, true, false },
{ divTag, true, false },
{ divTag, true, false },
{ spanTag, true, false }
};
setHtmlInnerHTML("<head>"
"<style>body:focus div { background-color: pink }</style>"
"</head>"
"<body>"
"<div><div></div></div>"
"<div><span></span></div>"
"</body>");
checkElements(expected, sizeof(expected) / sizeof(ElementResult));
}
// ":not(body):focus div" should not mark the body element with childrenOrSiblingsAffectedByFocus.
// Note that currently ":focus:not(body)" does not do the same. Then the :focus is
// checked and the childrenOrSiblingsAffectedByFocus flag set before the negated type selector
// is found.
TEST_F(AffectedByFocusTest, FocusedAscendantWithNegatedType)
{
ElementResult expected[] = {
{ bodyTag, true, false },
{ divTag, true, true },
{ divTag, true, false },
{ divTag, true, false },
{ spanTag, true, false }
};
setHtmlInnerHTML("<head>"
"<style>:not(body):focus div { background-color: pink }</style>"
"</head>"
"<body>"
"<div><div></div></div>"
"<div><span></span></div>"
"</body>");
checkElements(expected, sizeof(expected) / sizeof(ElementResult));
}
// Checking current behavior for ":focus + div", but this is a BUG or at best
// sub-optimal. The focused element will also in this case get childrenOrSiblingsAffectedByFocus
// even if it's really a sibling. Effectively, the whole sub-tree of the focused
// element will have styles recalculated even though none of the children are
// affected. There are other mechanisms that makes sure the sibling also gets its
// styles recalculated.
TEST_F(AffectedByFocusTest, FocusedSibling)
{
ElementResult expected[] = {
{ bodyTag, true, false },
{ divTag, true, true },
{ spanTag, true, false },
{ divTag, true, false }
};
setHtmlInnerHTML("<head>"
"<style>:focus + div { background-color: pink }</style>"
"</head>"
"<body>"
"<div>"
" <span></span>"
"</div>"
"<div></div>"
"</body>");
checkElements(expected, sizeof(expected) / sizeof(ElementResult));
}
TEST_F(AffectedByFocusTest, AffectedByFocusUpdate)
{
// Check that when focussing the outer div in the document below, you only
// get a single element style recalc.
setHtmlInnerHTML("<style>:focus { border: 1px solid lime; }</style>"
"<div id=d tabIndex=1>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"</div>");
document().view()->updateLayoutAndStyleIfNeededRecursive();
unsigned startCount = document().styleEngine()->resolverAccessCount();
document().getElementById("d")->focus();
document().view()->updateLayoutAndStyleIfNeededRecursive();
unsigned accessCount = document().styleEngine()->resolverAccessCount() - startCount;
ASSERT_EQ(1U, accessCount);
}
TEST_F(AffectedByFocusTest, ChildrenOrSiblingsAffectedByFocusUpdate)
{
// Check that when focussing the outer div in the document below, you get a
// style recalc for the whole subtree.
setHtmlInnerHTML("<style>:focus div { border: 1px solid lime; }</style>"
"<div id=d tabIndex=1>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"</div>");
document().view()->updateLayoutAndStyleIfNeededRecursive();
unsigned startCount = document().styleEngine()->resolverAccessCount();
document().getElementById("d")->focus();
document().view()->updateLayoutAndStyleIfNeededRecursive();
unsigned accessCount = document().styleEngine()->resolverAccessCount() - startCount;
ASSERT_EQ(11U, accessCount);
}
TEST_F(AffectedByFocusTest, InvalidationSetFocusUpdate)
{
// Check that when focussing the outer div in the document below, you get a
// style recalc for the outer div and the class=a div only.
setHtmlInnerHTML("<style>:focus .a { border: 1px solid lime; }</style>"
"<div id=d tabIndex=1>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div class='a'></div>"
"</div>");
document().view()->updateLayoutAndStyleIfNeededRecursive();
unsigned startCount = document().styleEngine()->resolverAccessCount();
document().getElementById("d")->focus();
document().view()->updateLayoutAndStyleIfNeededRecursive();
unsigned accessCount = document().styleEngine()->resolverAccessCount() - startCount;
ASSERT_EQ(2U, accessCount);
}
TEST_F(AffectedByFocusTest, NoInvalidationSetFocusUpdate)
{
// Check that when focussing the outer div in the document below, you get a
// style recalc for the outer div only. The invalidation set for :focus will
// include 'a', but the id=d div should be affectedByFocus, not childrenOrSiblingsAffectedByFocus.
setHtmlInnerHTML("<style>#nomatch:focus .a { border: 1px solid lime; }</style>"
"<div id=d tabIndex=1>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div></div>"
"<div class='a'></div>"
"</div>");
document().view()->updateLayoutAndStyleIfNeededRecursive();
unsigned startCount = document().styleEngine()->resolverAccessCount();
document().getElementById("d")->focus();
document().view()->updateLayoutAndStyleIfNeededRecursive();
unsigned accessCount = document().styleEngine()->resolverAccessCount() - startCount;
ASSERT_EQ(1U, accessCount);
}
} // namespace
|