00001 #ifndef hpp_DOMTests_hpp
00002 #define hpp_DOMTests_hpp
00003
00004
00005 #include "../HTMLParser/DOM.hpp"
00006
00007 #include "TestVectors.hpp"
00008
00009 namespace Tests
00010 {
00014 struct DOMTest
00015 {
00017 HTML::DOMTree xTree;
00019 typedef HTML::DOMTree::Node DOMNode;
00021 const DOM::Node * root;
00022
00024 bool createRootNode()
00025 {
00026
00027
00028 return memcmp(root->nodeName().getData(), "#document", 9) == 0 && root->parentNode() == 0;
00029 }
00030
00032 bool checkMethods()
00033 {
00034
00035 if (!root->isSupported(DOM::DOMString("Core"), DOM::DOMString("2.0"))) return false;
00036 if (!root->isSupported(DOM::DOMString("Core"), DOM::DOMString("1.9"))) return false;
00037 if (root->isSupported(DOM::DOMString("Core"), DOM::DOMString("2.1"))) return false;
00038 if (!root->isSupported(DOM::DOMString("HTML"), DOM::DOMString("2.0"))) return false;
00039 if (root->isSupported(DOM::DOMString("Unknown"), DOM::DOMString("7.0"))) return false;
00040
00041
00042
00043 if (root->nodeValue().getLength()) return false;
00044
00045 try { const_cast<DOM::Node *>(root)->nodeValue(DOM::DOMString("Some value") ); }
00046 catch (DOM::Exceptions::NoModificationAllowed) { }
00047 catch (...) { return false; }
00048
00049 DOM::NodeList * list = root->childNodes();
00050
00051 if (!list) return false;
00052
00053 if (list->length) return false;
00054
00055 if (list->item(0)) return false;
00056 delete list;
00057
00058
00059 if (root->firstChild() || root->lastChild() || root->previousSibling() || root->nextSibling()) return false;
00060
00061 if (root->attributes()) return false;
00062
00063 if (root->ownerDocument() != (const DOM::Document*)root) return false;
00064
00065
00066
00067 return true;
00068 }
00069
00071 bool AppendSomeChildren()
00072 {
00073
00074
00075 DOM::Node * head = new DOM::Element(new HTML::Elements::Element(HTML::GenericElement::HEAD));
00076 DOM::Node * title = new DOM::Element(new HTML::Elements::Element(HTML::GenericElement::TITLE));
00077 DOM::Node * anotherTitle = new DOM::Element(new HTML::Elements::Element(HTML::GenericElement::TITLE));
00078 DOM::Node * meta = new DOM::Element(new HTML::Elements::Element(HTML::GenericElement::META));
00079 DOM::Node * body = new DOM::Element(new HTML::Elements::Element(HTML::GenericElement::BODY));
00080
00081 DOM::Node * mRoot = const_cast<DOM::Node *>(root);
00082 if (mRoot->insertBefore(body, 0) != body) return false;
00083 if (mRoot->insertBefore(title, body) != title) return false;
00084 if (mRoot->insertBefore(head, title) != head) return false;
00085
00086 if (mRoot->insertBefore(title, body) != title) return false;
00087
00088
00089 try { mRoot->insertBefore(body, (const DOM::Node *)0xDEADBABE); }
00090 catch (DOM::Exceptions::NotFound) { }
00091 catch (...) { return false; }
00092
00093 DOM::NodeList * list = root->childNodes();
00094 if (!list) return false;
00095 if (list->length != 3
00096 || (list->item(0)->nodeName() != DOM::DOMString("HEAD"))
00097 || (list->item(1)->nodeName() != DOM::DOMString("TITLE"))
00098 || (list->item(2)->nodeName() != DOM::DOMString("BODY"))) return false;
00099 delete list;
00100
00101
00102 if (mRoot->replaceChild(anotherTitle, title) != title) return false;
00103 if (mRoot->replaceChild(title, anotherTitle) != anotherTitle) return false;
00104 if (mRoot->replaceChild(title, head) != head) return false;
00105
00106 if (mRoot->insertBefore(head, title) != head) return false;
00107
00108 if (mRoot->insertBefore(anotherTitle, title) != anotherTitle) return false;
00109
00110 if (mRoot->insertBefore(meta, body) != meta) return false;
00111
00112 if (root->getChildrenCount() != 5) return false;
00113
00114
00115 if (mRoot->removeChild(anotherTitle) != anotherTitle) return false;
00116
00117 if (root->getChildrenCount() != 4) return false;
00118
00119 try { mRoot->removeChild((const DOM::Node *)0xDEADBABE); }
00120 catch (DOM::Exceptions::NotFound) { }
00121 catch (...) { return false; }
00122
00123 return true;
00124 }
00125
00130 DOMTest(HTML::Elements::Allocators::BaseAllocator * allocator) : xTree(DOM::getCurrentDOMImplementation(allocator).createDocument(DOM::DOMString("http://www.w3.org/1999/xhtml"), DOM::DOMString("html"), new DOM::DocumentType(DOM::DOMString("DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"")))), root(xTree.getRootNode()->getData())
00131 {
00132 root->setTreeNode(xTree.getRootNode());
00133 }
00134 };
00135
00136 void createDOMTests(Test::Vector & xVector, Bstrlib::String & results)
00137 {
00138 typedef Bstrlib::String String;
00139 xVector.addUnitToTest(Test::MakeLineDelimiter("DOM tree testing"));
00140 {
00141
00142 HTML::Elements::Allocators::SimpleHeap allocator;
00143 DOMTest xDT(&allocator);
00144 xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Creating a DOM tree root node" ), xDT, &DOMTest::createRootNode));
00145 xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Checking DOM Node interface for empty node" ), xDT, &DOMTest::checkMethods));
00146 xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Append some children using DOM interface" ), xDT, &DOMTest::AppendSomeChildren));
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 xVector.testLastInsertedVectors(results);
00157 }
00158 }
00159 }
00160
00161 #endif