Back to index

lightning-sunbird  0.9+nobinonly
TestAttributes.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include <stdio.h>
00038 #include "nscore.h"
00039 #include "nsIAtom.h"
00040 #include "nsCRT.h"
00041 #include "nsHTMLParts.h"
00042 #include "nsGenericHTMLElement.h"
00043 #include "nsITextContent.h"
00044 #include "nsString.h"
00045 #include "nsIDocument.h"
00046 #include "nsISupportsArray.h"
00047 #include "nsDocument.h"
00048 #include "nsIURL.h"
00049 #include "nsIDOMText.h"
00050 #include "nsINameSpaceManager.h"
00051 
00052 void testAttributes(nsGenericHTMLElement* content) {
00053   nsIAtom* sBORDER = NS_NewAtom("border");
00054   nsIAtom* sHEIGHT = NS_NewAtom("height");
00055   nsIAtom* sSRC = NS_NewAtom("src");
00056   nsIAtom* sBAD = NS_NewAtom("badattribute");
00057   nsString sempty;
00058   nsString sfoo_gif(NS_LITERAL_STRING("foo.gif"));
00059 
00060   content->SetAttr(kNameSpaceID_None, sBORDER, EmptyString(), PR_FALSE);
00061   content->SetAttribute(kNameSpaceID_None, sHEIGHT, sempty, PR_FALSE);
00062   content->SetAttribute(kNameSpaceID_None, sSRC, sfoo_gif, PR_FALSE);
00063 
00064   const nsAttrValue* attr;
00065   attr = content->GetParsedAttr(sBORDER);
00066   if (!attr || attr->Type() != nsAttrValue::eString) {
00067     printf("test 0 failed\n");
00068   }
00069 
00070   attr = content->GetParsedAttr(sBAD);
00071   if (attr) {
00072     printf("test 2 failed\n");
00073   }
00074 
00075   content->UnsetAttribute(kNameSpaceID_None, sWIDTH, PR_FALSE);
00076 
00077   nsISupportsArray* allNames;
00078   NS_NewISupportsArray(&allNames);
00079   if (nsnull == allNames) return;
00080 
00081   PRInt32 na;
00082   content->GetAttributeCount(na);
00083   if (na != 3) {
00084     printf("test 5 (unset attriubte) failed\n");
00085   }
00086   PRInt32 index;
00087   for (index = 0; index < na; index++) {
00088     nsIAtom* name, *prefix = nsnull;
00089     PRInt32 nameSpaceID;
00090     content->GetAttributeNameAt(index, nameSpaceID, name, prefix);
00091     allNames->AppendElement(name);
00092     NS_RELEASE(name);
00093     NS_IF_RELEASE(prefix);
00094   }
00095 
00096   PRBool borderFound = PR_FALSE,heightFound = PR_FALSE,srcFound = PR_FALSE;
00097   for (int n = 0; n < 3; n++) {
00098     const nsIAtom* ident = (const nsIAtom*)allNames->ElementAt(n);
00099     if (sBORDER == ident) {
00100       borderFound = PR_TRUE;
00101     }
00102     if (sHEIGHT == ident) {
00103       heightFound = PR_TRUE;
00104     }
00105     if (sSRC == ident) {
00106       srcFound = PR_TRUE;
00107     }
00108   }
00109   if (!(borderFound && heightFound && srcFound)) {
00110     printf("test 6 failed\n");
00111   }
00112 
00113   NS_RELEASE(allNames);
00114 
00115   NS_RELEASE(sBORDER);
00116   NS_RELEASE(sWIDTH);
00117   NS_RELEASE(sHEIGHT);
00118   NS_RELEASE(sSRC);
00119 }
00120 
00121 void testStrings(nsIDocument* aDoc) {
00122   printf("begin string tests\n");
00123 
00124   PRBool val;
00125   // regular Equals
00126   val = (NS_ConvertASCIItoUCS2("mrString")).EqualsLiteral("mrString"); // XXXjag
00127   if (PR_TRUE != val) {
00128     printf("test 0 failed\n");
00129   }
00130   val = (NS_ConvertASCIItoUCS2("mrString")).EqualsLiteral("MRString"); // XXXjag
00131   if (PR_FALSE != val) {
00132     printf("test 1 failed\n");
00133   }
00134   val = (NS_ConvertASCIItoUCS2("mrString")).EqualsLiteral("mrStri"); // XXXjag
00135   if (PR_FALSE != val) {
00136     printf("test 2 failed\n");
00137   }
00138   val = (NS_ConvertASCIItoUCS2("mrStri")).EqualsLiteral("mrString"); // XXXjag
00139   if (PR_FALSE != val) {
00140     printf("test 3 failed\n");
00141   }
00142   // EqualsIgnoreCase
00143   val = (NS_ConvertASCIItoUCS2("mrString")).LowerCaseEqualsLiteral("mrstring");
00144   if (PR_TRUE != val) {
00145     printf("test 4 failed\n");
00146   }
00147   val = (NS_ConvertASCIItoUCS2("mrString")).LowerCaseEqualsLiteral("mrstring");
00148   if (PR_TRUE != val) {
00149     printf("test 5 failed\n");
00150   }
00151   val = (NS_ConvertASCIItoUCS2("mrString")).LowerCaseEqualsLiteral("mrstri");
00152   if (PR_FALSE != val) {
00153     printf("test 6 failed\n");
00154   }
00155   val = (NS_ConvertASCIItoUCS2("mrStri")).LowerCaseEqualsLiteral("mrstring");
00156   if (PR_FALSE != val) {
00157     printf("test 7 failed\n");
00158   }
00159   // String vs. Ident
00160   val = (NS_ConvertASCIItoUCS2("mrString")).EqualsIgnoreCase(NS_NewAtom("mrString"));
00161   if (PR_TRUE != val) {
00162     printf("test 8 failed\n");
00163   }
00164   val = (NS_ConvertASCIItoUCS2("mrString")).EqualsIgnoreCase(NS_NewAtom("MRStrINg"));
00165   if (PR_TRUE != val) {
00166     printf("test 9 failed\n");
00167   }
00168   val = (NS_ConvertASCIItoUCS2("mrString")).EqualsIgnoreCase(NS_NewAtom("mrStri"));
00169   if (PR_FALSE != val) {
00170     printf("test 10 failed\n");
00171   }
00172   val = (NS_ConvertASCIItoUCS2("mrStri")).EqualsIgnoreCase(NS_NewAtom("mrString"));
00173   if (PR_FALSE != val) {
00174     printf("test 11 failed\n");
00175   }
00176 
00177   printf("string tests complete\n");
00178 }
00179 
00180 class MyDocument : public nsDocument {
00181 public:
00182   MyDocument();
00183   NS_IMETHOD StartDocumentLoad(const char* aCommand,
00184                                nsIChannel* aChannel,
00185                                nsILoadGroup* aLoadGroup,
00186                                nsISupports* aContainer,
00187                                nsIStreamListener **aDocListener)
00188   {
00189     return NS_OK;
00190   }
00191 
00192   NS_IMETHOD    ImportNode(nsIDOMNode* aImportedNode, PRBool aDeep, nsIDOMNode** aReturn) {
00193     return NS_OK;
00194   }
00195 
00196   NS_IMETHOD    CreateElementNS(const nsAString& aNamespaceURI, const nsAString& aQualifiedName, nsIDOMElement** aReturn) {
00197     return NS_OK;
00198   }
00199 
00200   NS_IMETHOD    CreateAttributeNS(const nsAString& aNamespaceURI, const nsAString& aQualifiedName, nsIDOMAttr** aReturn) {
00201     return NS_OK;
00202   }
00203 
00204   NS_IMETHOD    GetElementsByTagNameNS(const nsAString& aNamespaceURI, const nsAString& aLocalName, nsIDOMNodeList** aReturn) {
00205     return NS_OK;
00206   }
00207 
00208   NS_IMETHOD    GetElementById(const nsAString& aElementId, nsIDOMElement** aReturn) {
00209     return NS_OK;
00210   }
00211 
00212 protected:
00213   virtual ~MyDocument();
00214 };
00215 
00216 MyDocument::MyDocument()
00217 {
00218 }
00219 
00220 MyDocument::~MyDocument()
00221 {
00222 }
00223 
00224 int main(int argc, char** argv)
00225 {
00226   /* Create Byte2Unicode converter? Not anymore. The converters are not tested
00227   here, they have their own test code. And if you just want to use them, you 
00228   need a properly intialized xpcom system. This simple test program doesn't do
00229   that. */
00230 
00231   // Create a unicode string
00232   static const char* srcStr = "This is some meaningless text about nothing at all";
00233   nsresult rv;
00234   PRUint32 origSrcLen = nsCRT::strlen((char *)srcStr);
00235   const int BUFFER_LENGTH = 100;
00236   PRUnichar destStr[BUFFER_LENGTH];
00237   PRUint32 srcLen = origSrcLen;
00238   PRUint32 destLen = BUFFER_LENGTH;
00239   // hacky Ascii conversion to unicode, because we don't have a real Converter.
00240   for (PRUint32 i=0; i<srcLen; i++) destStr[i] = ((PRUint8)srcStr[i]);
00241 
00242   // Create test document.
00243   MyDocument *myDoc = new MyDocument();
00244   if (myDoc) {
00245     testStrings(myDoc);
00246   } else {
00247     printf("Out of memory trying to create document\n");
00248     return -1;
00249   }
00250 
00251 
00252   // Create a new text content object.
00253   nsIContent *text;
00254   rv = NS_NewTextNode(&text, myDoc->NodeInfoManager());
00255   if (NS_OK != rv) {
00256     printf("Could not create text content.\n");
00257     return -1;
00258   }
00259 
00260   nsIDOMText* txt = nsnull;
00261   text->QueryInterface(NS_GET_IID(nsIDOMText), (void**) &txt);
00262   nsAutoString tmp(destStr);
00263   txt->AppendData(tmp);
00264   NS_RELEASE(txt);
00265 
00266   rv = text->BindToTree(myDoc, nsnull, nsnull, PR_FALSE);
00267   if (NS_FAILED(rv)) {
00268     printf("Could not bind text content to tree.\n");
00269     text->UnbindFromTree();
00270     return -1;
00271   }
00272 
00273 #if 0
00274   // Query ITextContent interface
00275   nsITextContent* textContent;
00276   rv = text->QueryInterface(NS_GET_IID(nsITextContent),(void **)&textContent);
00277   if (NS_OK != rv) {
00278     printf("Created text content does not have the ITextContent interface.\n");
00279     return -1;
00280   }
00281 
00282   // Print the contents.
00283   nsAutoString stringBuf;
00284   textContent->GetText(stringBuf,0,textContent->GetLength());
00285   if (!stringBuf.Equals(nsString(destStr,destLen))) {
00286     printf("something wrong with the text in a text content\n");
00287   }
00288 #endif
00289 
00290   // Create a simple container.
00291   nsGenericHTMLElement* container;
00292   nsIAtom* li = NS_NewAtom("li");
00293 
00294   nsCOMPtr<nsINodeInfo> ni;
00295   myDoc->NodeInfoManager()->GetNodeInfo(li, nsnull, kNameSpaceID_None,
00296                                         getter_AddRefs(ni));
00297 
00298   rv = NS_NewHTMLLIElement(&container,ni);
00299   if (NS_OK != rv) {
00300     printf("Could not create container.\n");
00301     return -1;
00302   }
00303 
00304   container->AppendChildTo(text, PR_FALSE);
00305   PRInt32 nk;
00306   container->ChildCount(nk);
00307   if (nk != 1) {
00308     printf("Container has wrong number of children.");
00309   }
00310 
00311   printf("begin attribute tests\n");
00312   testAttributes(container);
00313   printf("attribute tests complete\n");
00314 
00315 
00316   // Clean up memory.
00317   text->Release(); // The textContent interface.
00318   delete container;
00319   delete text;
00320   myDoc->Release();
00321   return 0;
00322 }