include/Tests/DOMTests.hpp

Go to the documentation of this file.
00001 #ifndef hpp_DOMTests_hpp
00002 #define hpp_DOMTests_hpp
00003 
00004 // We need DOM tree declaration
00005 #include "../HTMLParser/DOM.hpp"
00006 // We need test vectors
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             // Should be done in the constructor
00027             // Check it
00028             return memcmp(root->nodeName().getData(), "#document", 9) == 0 && root->parentNode() == 0;
00029         }
00030 
00032         bool checkMethods()
00033         {
00034             // Check implementation
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             // No value here expected
00043             if (root->nodeValue().getLength()) return false;
00044             // Try to set a value (should throw here, as no modification is allowed)
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             // Memory error ?
00051             if (!list) return false;
00052             // No child yet
00053             if (list->length) return false;
00054             // So no item should be set
00055             if (list->item(0)) return false;
00056             delete list;
00057 
00058             // Then check iterators
00059             if (root->firstChild() || root->lastChild() || root->previousSibling() || root->nextSibling()) return false;
00060             // Check no attributes are set 
00061             if (root->attributes()) return false;
00062             // Check the owner document (should be us)
00063             if (root->ownerDocument() != (const DOM::Document*)root) return false;
00064 
00065 
00066             // Ok, return
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             // Explicit reinsertion should remove the previous node
00086             if (mRoot->insertBefore(title, body) != title) return false;
00087 
00088             // Try to append a child at a impossible position
00089             try   { mRoot->insertBefore(body, (const DOM::Node *)0xDEADBABE);  }
00090             catch (DOM::Exceptions::NotFound)  { }
00091             catch (...)  { return false; }
00092             // And check the data are still valid
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             // Replace a child now 
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             // Insert the lost child
00106             if (mRoot->insertBefore(head, title) != head) return false;
00107             // Insert the other title too
00108             if (mRoot->insertBefore(anotherTitle, title) != anotherTitle) return false;
00109             // Insert the meta too
00110             if (mRoot->insertBefore(meta, body) != meta) return false;
00111             // Check insertion here
00112             if (root->getChildrenCount() != 5) return false;
00113 
00114             // Ok, start removing stuff now
00115             if (mRoot->removeChild(anotherTitle) != anotherTitle) return false;
00116             // Check removing here
00117             if (root->getChildrenCount() != 4) return false;
00118             // Try removing an invalid stuff
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         {   // It is REQUIRED to set the tree node containing any DOM::Node created, the DOM::Node can interact with its tree
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             // The DOM object allocator 
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 //            xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Testing tagging speed for the current algorithm" ), xPT, &ParserTest::checkTaggingSpeed));
00148 //          xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Testing parsing an example file" ), xPT, &ParserTest::parseDocHTMLFile));
00149 /*
00150             xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Checking node accessibility" ), xTT, &NTreeTest::countNodes, byRef(xTT.xTree)));
00151             xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Checking node creation count" ), xTT, &NTreeTest::refCheck, byRef(xTT.xTree)));
00152             xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Checking Node's access methods" ), xTT, &NTreeTest::testMethod1, byRef(xTT.xTree)));
00153             xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Checking Node's find and apply methods" ), xTT, &NTreeTest::testMethod2, byRef(xTT.xTree)));
00154             xVector.addUnitToTest(Test::MakeUnitTest(MakeTestName( "Checking Node deletion methods" ), xTT, &NTreeTest::testMethod3, byRef(xTT.xTree)));
00155             */
00156             xVector.testLastInsertedVectors(results);
00157         }
00158     }
00159 }
00160 
00161 #endif

(C) An X-Ryl669 project 2007

This document describes Unlimited Zooming Interface source code. UZI stands for Unlimited Zooming Interface, and source code license is