1 // Copyright 2014 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.
6 #include "public/web/WebNode.h"
8 #include "core/dom/Document.h"
9 #include "core/dom/Element.h"
10 #include "core/testing/DummyPageHolder.h"
11 #include "public/web/WebElement.h"
12 #include "public/web/WebElementCollection.h"
13 #include <gtest/gtest.h>
17 class WebNodeTest
: public testing::Test
{
21 return m_pageHolder
->document();
24 void setInnerHTML(const String
& html
)
26 document().documentElement()->setInnerHTML(html
, ASSERT_NO_EXCEPTION
);
31 return WebNode(document().documentElement());
35 void SetUp() override
;
37 OwnPtr
<DummyPageHolder
> m_pageHolder
;
40 void WebNodeTest::SetUp()
42 m_pageHolder
= DummyPageHolder::create(IntSize(800, 600));
45 TEST_F(WebNodeTest
, QuerySelectorMatches
)
47 setInnerHTML("<div id=x><span class=a></span></div>");
49 WebElement element
= root().querySelector(".a", ec
);
51 EXPECT_FALSE(element
.isNull());
52 EXPECT_TRUE(element
.hasHTMLTagName("span"));
55 TEST_F(WebNodeTest
, QuerySelectorDoesNotMatch
)
57 setInnerHTML("<div id=x><span class=a></span></div>");
59 WebElement element
= root().querySelector("section", ec
);
61 EXPECT_TRUE(element
.isNull());
64 TEST_F(WebNodeTest
, QuerySelectorError
)
66 setInnerHTML("<div></div>");
68 WebElement element
= root().querySelector("@invalid-selector", ec
);
70 EXPECT_TRUE(element
.isNull());
73 TEST_F(WebNodeTest
, QuerySelectorAllMatches
)
75 setInnerHTML("<div id=x><span class=a></span></div>");
77 WebVector
<WebElement
> results
;
78 root().querySelectorAll(".a, #x", results
, ec
);
80 EXPECT_EQ(2u, results
.size());
81 EXPECT_TRUE(results
[0].hasHTMLTagName("div"));
82 EXPECT_TRUE(results
[1].hasHTMLTagName("span"));
85 TEST_F(WebNodeTest
, QuerySelectorAllDoesNotMatch
)
87 setInnerHTML("<div id=x><span class=a></span></div>");
89 WebVector
<WebElement
> results
;
90 root().querySelectorAll(".bar, #foo", results
, ec
);
92 EXPECT_TRUE(results
.isEmpty());
95 TEST_F(WebNodeTest
, QuerySelectorAllError
)
97 setInnerHTML("<div></div>");
99 WebVector
<WebElement
> results
;
100 root().querySelectorAll("@invalid-selector", results
, ec
);
102 EXPECT_TRUE(results
.isEmpty());
105 TEST_F(WebNodeTest
, GetElementsByHTMLTagName
)
107 setInnerHTML("<body><LABEL></LABEL><svg xmlns='http://www.w3.org/2000/svg'><label></label></svg></body>");
108 // WebNode::getElementsByHTMLTagName returns only HTML elements.
109 WebElementCollection collection
= root().getElementsByHTMLTagName("label");
110 EXPECT_EQ(1u, collection
.length());
111 EXPECT_TRUE(collection
.firstItem().hasHTMLTagName("label"));
112 // The argument should be lower-case.
113 collection
= root().getElementsByHTMLTagName("LABEL");
114 EXPECT_EQ(0u, collection
.length());