Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: Source/web/WebNodeTest.cpp

Issue 1345423002: Introduce WebNode::querySelectorAll. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/web/WebNode.cpp ('k') | public/web/WebNode.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "public/web/WebNode.h" 6 #include "public/web/WebNode.h"
7 7
8 #include "core/dom/Document.h" 8 #include "core/dom/Document.h"
9 #include "core/dom/Element.h" 9 #include "core/dom/Element.h"
10 #include "core/testing/DummyPageHolder.h" 10 #include "core/testing/DummyPageHolder.h"
11 #include "public/web/WebElement.h" 11 #include "public/web/WebElement.h"
12 #include "public/web/WebElementCollection.h" 12 #include "public/web/WebElementCollection.h"
13 #include <gtest/gtest.h> 13 #include <gtest/gtest.h>
14 14
15 namespace blink { 15 namespace blink {
16 16
17 class WebNodeTest : public testing::Test { 17 class WebNodeTest : public testing::Test {
18 protected: 18 protected:
19 Document& document() { return m_pageHolder->document(); } 19 Document& document()
20 {
21 return m_pageHolder->document();
22 }
23
24 void setInnerHTML(const String& html)
25 {
26 document().documentElement()->setInnerHTML(html, ASSERT_NO_EXCEPTION);
27 }
28
29 WebNode root()
30 {
31 return WebNode(document().documentElement());
32 }
20 33
21 private: 34 private:
22 void SetUp() override; 35 void SetUp() override;
23 36
24 OwnPtr<DummyPageHolder> m_pageHolder; 37 OwnPtr<DummyPageHolder> m_pageHolder;
25 }; 38 };
26 39
27 void WebNodeTest::SetUp() 40 void WebNodeTest::SetUp()
28 { 41 {
29 m_pageHolder = DummyPageHolder::create(IntSize(800, 600)); 42 m_pageHolder = DummyPageHolder::create(IntSize(800, 600));
30 } 43 }
31 44
45 TEST_F(WebNodeTest, QuerySelectorMatches)
46 {
47 setInnerHTML("<div id=x><span class=a></span></div>");
48 WebExceptionCode ec;
49 WebElement element = root().querySelector(".a", ec);
50 EXPECT_EQ(0, ec);
51 EXPECT_FALSE(element.isNull());
52 EXPECT_TRUE(element.hasHTMLTagName("span"));
53 }
54
55 TEST_F(WebNodeTest, QuerySelectorDoesNotMatch)
56 {
57 setInnerHTML("<div id=x><span class=a></span></div>");
58 WebExceptionCode ec;
59 WebElement element = root().querySelector("section", ec);
60 EXPECT_EQ(0, ec);
61 EXPECT_TRUE(element.isNull());
62 }
63
64 TEST_F(WebNodeTest, QuerySelectorError)
65 {
66 setInnerHTML("<div></div>");
67 WebExceptionCode ec;
68 WebElement element = root().querySelector("@invalid-selector", ec);
69 EXPECT_NE(0, ec);
70 EXPECT_TRUE(element.isNull());
71 }
72
73 TEST_F(WebNodeTest, QuerySelectorAllMatches)
74 {
75 setInnerHTML("<div id=x><span class=a></span></div>");
76 WebExceptionCode ec;
77 WebVector<WebElement> results;
78 root().querySelectorAll(".a, #x", ec, results);
79 EXPECT_EQ(0, ec);
80 EXPECT_EQ(2u, results.size());
81 EXPECT_TRUE(results[0].hasHTMLTagName("div"));
82 EXPECT_TRUE(results[1].hasHTMLTagName("span"));
83 }
84
85 TEST_F(WebNodeTest, QuerySelectorAllDoesNotMatch)
86 {
87 setInnerHTML("<div id=x><span class=a></span></div>");
88 WebExceptionCode ec;
89 WebVector<WebElement> results;
90 root().querySelectorAll(".bar, #foo", ec, results);
91 EXPECT_EQ(0, ec);
92 EXPECT_TRUE(results.isEmpty());
93 }
94
95 TEST_F(WebNodeTest, QuerySelectorAllError)
96 {
97 setInnerHTML("<div></div>");
98 WebExceptionCode ec;
99 WebVector<WebElement> results;
100 root().querySelectorAll("@invalid-selector", ec, results);
101 EXPECT_NE(0, ec);
102 EXPECT_TRUE(results.isEmpty());
103 }
104
32 TEST_F(WebNodeTest, GetElementsByHTMLTagName) 105 TEST_F(WebNodeTest, GetElementsByHTMLTagName)
33 { 106 {
34 document().documentElement()->setInnerHTML("<body><LABEL></LABEL><svg xmlns= 'http://www.w3.org/2000/svg'><label></label></svg></body>", ASSERT_NO_EXCEPTION) ; 107 setInnerHTML("<body><LABEL></LABEL><svg xmlns='http://www.w3.org/2000/svg'>< label></label></svg></body>");
35 WebNode node(document().documentElement());
36 // WebNode::getElementsByHTMLTagName returns only HTML elements. 108 // WebNode::getElementsByHTMLTagName returns only HTML elements.
37 WebElementCollection collection = node.getElementsByHTMLTagName("label"); 109 WebElementCollection collection = root().getElementsByHTMLTagName("label");
38 EXPECT_EQ(1u, collection.length()); 110 EXPECT_EQ(1u, collection.length());
39 EXPECT_TRUE(collection.firstItem().hasHTMLTagName("label")); 111 EXPECT_TRUE(collection.firstItem().hasHTMLTagName("label"));
40 // The argument should be lower-case. 112 // The argument should be lower-case.
41 collection = node.getElementsByHTMLTagName("LABEL"); 113 collection = root().getElementsByHTMLTagName("LABEL");
42 EXPECT_EQ(0u, collection.length()); 114 EXPECT_EQ(0u, collection.length());
43 } 115 }
44 116
45 } // namespace blink 117 } // namespace blink
OLDNEW
« no previous file with comments | « Source/web/WebNode.cpp ('k') | public/web/WebNode.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698