This source file includes following definitions.
- document
- SetUp
- setHtmlInnerHTML
- checkElements
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "config.h"
#include "HTMLNames.h"
#include "core/dom/Element.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/NodeRenderStyle.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 WebCore;
using namespace HTMLNames;
namespace {
class AffectedByFocusTest : public ::testing::Test {
protected:
struct ElementResult {
const WebCore::QualifiedName tag;
bool affectedBy;
bool childrenAffectedBy;
};
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;
Element* elm = document().body();
for (; elm && i < expectedCount; elm = ElementTraversal::next(*elm), ++i) {
ASSERT_TRUE(elm->hasTagName(expected[i].tag));
ASSERT(elm->renderStyle());
ASSERT_EQ(expected[i].affectedBy, elm->renderStyle()->affectedByFocus());
ASSERT_EQ(expected[i].childrenAffectedBy, elm->childrenAffectedByFocus());
}
ASSERT(!elm && i == expectedCount);
}
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));
}
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));
}
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));
}
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));
}
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)
{
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, ChildrenAffectedByFocusUpdate)
{
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);
}
}