2011-02-23 04:45:13 +01:00
|
|
|
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2011-12-23 18:36:30 +01:00
|
|
|
#include "include/cef_dom.h"
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "tests/unittests/test_handler.h"
|
2011-02-23 04:45:13 +01:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
class TestDOMHandler : public TestHandler {
|
|
|
|
public:
|
|
|
|
class Visitor : public CefDOMVisitor {
|
|
|
|
public:
|
|
|
|
explicit Visitor(TestDOMHandler* handler) : handler_(handler) {}
|
2011-02-23 04:45:13 +01:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void TestHeadNodeStructure(CefRefPtr<CefDOMNode> headNode) {
|
2011-02-23 04:45:13 +01:00
|
|
|
EXPECT_TRUE(headNode.get());
|
|
|
|
EXPECT_TRUE(headNode->IsElement());
|
|
|
|
EXPECT_FALSE(headNode->IsText());
|
|
|
|
EXPECT_EQ(headNode->GetName(), "HEAD");
|
|
|
|
EXPECT_EQ(headNode->GetElementTagName(), "HEAD");
|
|
|
|
|
|
|
|
EXPECT_TRUE(headNode->HasChildren());
|
|
|
|
EXPECT_FALSE(headNode->HasElementAttributes());
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-02-23 04:45:13 +01:00
|
|
|
CefRefPtr<CefDOMNode> titleNode = headNode->GetFirstChild();
|
|
|
|
EXPECT_TRUE(titleNode.get());
|
|
|
|
EXPECT_TRUE(titleNode->IsElement());
|
|
|
|
EXPECT_FALSE(titleNode->IsText());
|
|
|
|
EXPECT_EQ(titleNode->GetName(), "TITLE");
|
|
|
|
EXPECT_EQ(titleNode->GetElementTagName(), "TITLE");
|
2011-05-10 16:17:32 +02:00
|
|
|
EXPECT_TRUE(titleNode->GetParent()->IsSame(headNode));
|
2011-02-23 04:45:13 +01:00
|
|
|
|
|
|
|
EXPECT_FALSE(titleNode->GetNextSibling().get());
|
|
|
|
EXPECT_FALSE(titleNode->GetPreviousSibling().get());
|
|
|
|
EXPECT_TRUE(titleNode->HasChildren());
|
|
|
|
EXPECT_FALSE(titleNode->HasElementAttributes());
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> textNode = titleNode->GetFirstChild();
|
|
|
|
EXPECT_TRUE(textNode.get());
|
|
|
|
EXPECT_FALSE(textNode->IsElement());
|
|
|
|
EXPECT_TRUE(textNode->IsText());
|
|
|
|
EXPECT_EQ(textNode->GetValue(), "The Title");
|
2011-05-10 16:17:32 +02:00
|
|
|
EXPECT_TRUE(textNode->GetParent()->IsSame(titleNode));
|
2011-02-23 04:45:13 +01:00
|
|
|
|
|
|
|
EXPECT_FALSE(textNode->GetNextSibling().get());
|
|
|
|
EXPECT_FALSE(textNode->GetPreviousSibling().get());
|
|
|
|
EXPECT_FALSE(textNode->HasChildren());
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
|
|
|
|
void TestBodyNodeStructure(CefRefPtr<CefDOMNode> bodyNode) {
|
2011-02-23 04:45:13 +01:00
|
|
|
EXPECT_TRUE(bodyNode.get());
|
|
|
|
EXPECT_TRUE(bodyNode->IsElement());
|
|
|
|
EXPECT_FALSE(bodyNode->IsText());
|
|
|
|
EXPECT_EQ(bodyNode->GetName(), "BODY");
|
|
|
|
EXPECT_EQ(bodyNode->GetElementTagName(), "BODY");
|
|
|
|
|
|
|
|
EXPECT_TRUE(bodyNode->HasChildren());
|
|
|
|
EXPECT_FALSE(bodyNode->HasElementAttributes());
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> h1Node = bodyNode->GetFirstChild();
|
|
|
|
EXPECT_TRUE(h1Node.get());
|
|
|
|
EXPECT_TRUE(h1Node->IsElement());
|
|
|
|
EXPECT_FALSE(h1Node->IsText());
|
|
|
|
EXPECT_EQ(h1Node->GetName(), "H1");
|
|
|
|
EXPECT_EQ(h1Node->GetElementTagName(), "H1");
|
|
|
|
|
|
|
|
EXPECT_FALSE(h1Node->GetNextSibling().get());
|
|
|
|
EXPECT_FALSE(h1Node->GetPreviousSibling().get());
|
|
|
|
EXPECT_TRUE(h1Node->HasChildren());
|
|
|
|
EXPECT_FALSE(h1Node->HasElementAttributes());
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> textNode = h1Node->GetFirstChild();
|
|
|
|
EXPECT_TRUE(textNode.get());
|
|
|
|
EXPECT_FALSE(textNode->IsElement());
|
|
|
|
EXPECT_TRUE(textNode->IsText());
|
|
|
|
EXPECT_EQ(textNode->GetValue(), "Hello From");
|
|
|
|
|
|
|
|
EXPECT_FALSE(textNode->GetPreviousSibling().get());
|
|
|
|
EXPECT_FALSE(textNode->HasChildren());
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> brNode = textNode->GetNextSibling();
|
|
|
|
EXPECT_TRUE(brNode.get());
|
|
|
|
EXPECT_TRUE(brNode->IsElement());
|
|
|
|
EXPECT_FALSE(brNode->IsText());
|
|
|
|
EXPECT_EQ(brNode->GetName(), "BR");
|
|
|
|
EXPECT_EQ(brNode->GetElementTagName(), "BR");
|
|
|
|
|
|
|
|
EXPECT_FALSE(brNode->HasChildren());
|
|
|
|
|
|
|
|
EXPECT_TRUE(brNode->HasElementAttributes());
|
|
|
|
EXPECT_TRUE(brNode->HasElementAttribute("class"));
|
|
|
|
EXPECT_EQ(brNode->GetElementAttribute("class"), "some_class");
|
|
|
|
EXPECT_TRUE(brNode->HasElementAttribute("id"));
|
|
|
|
EXPECT_EQ(brNode->GetElementAttribute("id"), "some_id");
|
|
|
|
EXPECT_FALSE(brNode->HasElementAttribute("no_existing"));
|
|
|
|
|
|
|
|
CefDOMNode::AttributeMap map;
|
|
|
|
brNode->GetElementAttributes(map);
|
|
|
|
ASSERT_EQ(map.size(), (size_t)2);
|
|
|
|
EXPECT_EQ(map["class"], "some_class");
|
|
|
|
EXPECT_EQ(map["id"], "some_id");
|
|
|
|
|
|
|
|
// Can also retrieve by ID.
|
|
|
|
brNode = bodyNode->GetDocument()->GetElementById("some_id");
|
|
|
|
EXPECT_TRUE(brNode.get());
|
|
|
|
EXPECT_TRUE(brNode->IsElement());
|
|
|
|
EXPECT_FALSE(brNode->IsText());
|
|
|
|
EXPECT_EQ(brNode->GetName(), "BR");
|
|
|
|
EXPECT_EQ(brNode->GetElementTagName(), "BR");
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-02-23 04:45:13 +01:00
|
|
|
textNode = brNode->GetNextSibling();
|
|
|
|
EXPECT_TRUE(textNode.get());
|
|
|
|
EXPECT_FALSE(textNode->IsElement());
|
|
|
|
EXPECT_TRUE(textNode->IsText());
|
|
|
|
EXPECT_EQ(textNode->GetValue(), "Main Frame");
|
|
|
|
|
|
|
|
EXPECT_FALSE(textNode->GetNextSibling().get());
|
|
|
|
EXPECT_FALSE(textNode->HasChildren());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test document structure by iterating through the DOM tree.
|
2012-01-10 00:46:23 +01:00
|
|
|
void TestStructure(CefRefPtr<CefDOMDocument> document) {
|
2011-02-23 04:45:13 +01:00
|
|
|
EXPECT_EQ(document->GetTitle(), "The Title");
|
|
|
|
EXPECT_EQ(document->GetBaseURL(), "http://tests/main.html");
|
|
|
|
EXPECT_EQ(document->GetCompleteURL("foo.html"), "http://tests/foo.html");
|
|
|
|
|
|
|
|
// Navigate the complete document structure.
|
|
|
|
CefRefPtr<CefDOMNode> docNode = document->GetDocument();
|
|
|
|
EXPECT_TRUE(docNode.get());
|
|
|
|
EXPECT_FALSE(docNode->IsElement());
|
|
|
|
EXPECT_FALSE(docNode->IsText());
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> htmlNode = docNode->GetFirstChild();
|
|
|
|
EXPECT_TRUE(htmlNode.get());
|
|
|
|
EXPECT_TRUE(htmlNode->IsElement());
|
|
|
|
EXPECT_FALSE(htmlNode->IsText());
|
|
|
|
EXPECT_EQ(htmlNode->GetName(), "HTML");
|
|
|
|
EXPECT_EQ(htmlNode->GetElementTagName(), "HTML");
|
|
|
|
|
|
|
|
EXPECT_TRUE(htmlNode->HasChildren());
|
|
|
|
EXPECT_FALSE(htmlNode->HasElementAttributes());
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> headNode = htmlNode->GetFirstChild();
|
|
|
|
TestHeadNodeStructure(headNode);
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> bodyNode = headNode->GetNextSibling();
|
|
|
|
TestBodyNodeStructure(bodyNode);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-02-23 04:45:13 +01:00
|
|
|
// Retrieve the head node directly.
|
|
|
|
headNode = document->GetHead();
|
|
|
|
TestHeadNodeStructure(headNode);
|
|
|
|
|
|
|
|
// Retrieve the body node directly.
|
|
|
|
bodyNode = document->GetBody();
|
|
|
|
TestBodyNodeStructure(bodyNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test document modification by changing the H1 tag.
|
2012-01-10 00:46:23 +01:00
|
|
|
void TestModify(CefRefPtr<CefDOMDocument> document) {
|
2011-02-23 04:45:13 +01:00
|
|
|
CefRefPtr<CefDOMNode> bodyNode = document->GetBody();
|
|
|
|
CefRefPtr<CefDOMNode> h1Node = bodyNode->GetFirstChild();
|
|
|
|
|
|
|
|
ASSERT_EQ(h1Node->GetAsMarkup(),
|
2012-01-10 00:46:23 +01:00
|
|
|
"<h1>Hello From<br class=\"some_class\" id=\"some_id\">"
|
|
|
|
"Main Frame</h1>");
|
|
|
|
|
2011-02-23 04:45:13 +01:00
|
|
|
CefRefPtr<CefDOMNode> textNode = h1Node->GetFirstChild();
|
|
|
|
ASSERT_EQ(textNode->GetValue(), "Hello From");
|
|
|
|
ASSERT_TRUE(textNode->SetValue("A Different Message From"));
|
|
|
|
ASSERT_EQ(textNode->GetValue(), "A Different Message From");
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> brNode = textNode->GetNextSibling();
|
|
|
|
EXPECT_EQ(brNode->GetElementAttribute("class"), "some_class");
|
|
|
|
EXPECT_TRUE(brNode->SetElementAttribute("class", "a_different_class"));
|
|
|
|
EXPECT_EQ(brNode->GetElementAttribute("class"), "a_different_class");
|
|
|
|
|
|
|
|
ASSERT_EQ(h1Node->GetAsMarkup(),
|
2012-01-10 00:46:23 +01:00
|
|
|
"<h1>A Different Message From<br class=\"a_different_class\" "
|
|
|
|
"id=\"some_id\">Main Frame</h1>");
|
2011-02-23 04:45:13 +01:00
|
|
|
|
|
|
|
ASSERT_FALSE(h1Node->SetValue("Something Different"));
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
virtual void Visit(CefRefPtr<CefDOMDocument> document) {
|
2011-02-23 04:45:13 +01:00
|
|
|
handler_->got_visitor_called_.yes();
|
|
|
|
|
|
|
|
if (handler_->test_type_ == STRUCTURE)
|
|
|
|
TestStructure(document);
|
|
|
|
else if (handler_->test_type_ == MODIFY)
|
|
|
|
TestModify(document);
|
|
|
|
|
|
|
|
handler_->DestroyTest();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
protected:
|
2011-02-23 04:45:13 +01:00
|
|
|
TestDOMHandler* handler_;
|
2011-05-20 16:42:25 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(Visitor);
|
2011-02-23 04:45:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum TestType {
|
|
|
|
STRUCTURE,
|
|
|
|
MODIFY,
|
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
explicit TestDOMHandler(TestType test) : test_type_(test) {
|
2011-02-23 04:45:13 +01:00
|
|
|
visitor_ = new Visitor(this);
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
|
|
|
|
virtual void RunTest() OVERRIDE {
|
2011-02-23 04:45:13 +01:00
|
|
|
std::stringstream mainHtml;
|
|
|
|
mainHtml <<
|
2012-01-10 00:46:23 +01:00
|
|
|
"<html>"
|
|
|
|
"<head><title>The Title</title></head>"
|
|
|
|
"<body>"
|
|
|
|
"<h1>Hello From<br class=\"some_class\"/ id=\"some_id\"/>"
|
|
|
|
"Main Frame</h1>"
|
|
|
|
"</body>"
|
|
|
|
"</html>";
|
|
|
|
|
2011-02-23 04:45:13 +01:00
|
|
|
AddResource("http://tests/main.html", mainHtml.str(), "text/html");
|
|
|
|
CreateBrowser("http://tests/main.html");
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2012-01-10 00:46:23 +01:00
|
|
|
int httpStatusCode) OVERRIDE {
|
|
|
|
if (frame->IsMain()) {
|
2011-02-23 04:45:13 +01:00
|
|
|
// The page is done loading so visit the DOM.
|
|
|
|
browser->GetMainFrame()->VisitDOM(visitor_.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<Visitor> visitor_;
|
|
|
|
TestType test_type_;
|
|
|
|
|
|
|
|
TrackCallback got_visitor_called_;
|
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
} // namespace
|
2011-02-23 04:45:13 +01:00
|
|
|
|
|
|
|
// Test DOM structure reading.
|
2012-01-10 00:46:23 +01:00
|
|
|
TEST(DOMTest, Read) {
|
2011-02-23 04:45:13 +01:00
|
|
|
CefRefPtr<TestDOMHandler> handler =
|
|
|
|
new TestDOMHandler(TestDOMHandler::STRUCTURE);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_visitor_called_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test DOM modifications.
|
2012-01-10 00:46:23 +01:00
|
|
|
TEST(DOMTest, Modify) {
|
2011-02-23 04:45:13 +01:00
|
|
|
CefRefPtr<TestDOMHandler> handler =
|
|
|
|
new TestDOMHandler(TestDOMHandler::MODIFY);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_visitor_called_);
|
|
|
|
}
|