1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
41 #include "nsHTMLParts.h"
42 #include "nsGenericHTMLElement.h"
44 #include "nsIDocument.h"
45 #include "nsISupportsArray.h"
46 #include "nsDocument.h"
48 #include "nsIDOMText.h"
49 #include "nsINameSpaceManager.h"
51 void testAttributes(nsGenericHTMLElement
* content
) {
52 nsIAtom
* sBORDER
= NS_NewAtom("border");
53 nsIAtom
* sHEIGHT
= NS_NewAtom("height");
54 nsIAtom
* sSRC
= NS_NewAtom("src");
55 nsIAtom
* sBAD
= NS_NewAtom("badattribute");
57 nsString
sfoo_gif(NS_LITERAL_STRING("foo.gif"));
59 content
->SetAttr(kNameSpaceID_None
, sBORDER
, EmptyString(), PR_FALSE
);
60 content
->SetAttribute(kNameSpaceID_None
, sHEIGHT
, sempty
, PR_FALSE
);
61 content
->SetAttribute(kNameSpaceID_None
, sSRC
, sfoo_gif
, PR_FALSE
);
63 const nsAttrValue
* attr
;
64 attr
= content
->GetParsedAttr(sBORDER
);
65 if (!attr
|| attr
->Type() != nsAttrValue::eString
) {
66 printf("test 0 failed\n");
69 attr
= content
->GetParsedAttr(sBAD
);
71 printf("test 2 failed\n");
74 content
->UnsetAttribute(kNameSpaceID_None
, sWIDTH
, PR_FALSE
);
76 nsISupportsArray
* allNames
;
77 NS_NewISupportsArray(&allNames
);
78 if (nsnull
== allNames
) return;
81 content
->GetAttributeCount(na
);
83 printf("test 5 (unset attriubte) failed\n");
86 for (index
= 0; index
< na
; index
++) {
87 nsIAtom
* name
, *prefix
= nsnull
;
89 content
->GetAttributeNameAt(index
, nameSpaceID
, name
, prefix
);
90 allNames
->AppendElement(name
);
92 NS_IF_RELEASE(prefix
);
95 PRBool borderFound
= PR_FALSE
,heightFound
= PR_FALSE
,srcFound
= PR_FALSE
;
96 for (int n
= 0; n
< 3; n
++) {
97 const nsIAtom
* ident
= (const nsIAtom
*)allNames
->ElementAt(n
);
98 if (sBORDER
== ident
) {
99 borderFound
= PR_TRUE
;
101 if (sHEIGHT
== ident
) {
102 heightFound
= PR_TRUE
;
108 if (!(borderFound
&& heightFound
&& srcFound
)) {
109 printf("test 6 failed\n");
112 NS_RELEASE(allNames
);
120 void testStrings(nsIDocument
* aDoc
) {
121 printf("begin string tests\n");
125 val
= (NS_ConvertASCIItoUTF16("mrString")).EqualsLiteral("mrString"); // XXXjag
126 if (PR_TRUE
!= val
) {
127 printf("test 0 failed\n");
129 val
= (NS_ConvertASCIItoUTF16("mrString")).EqualsLiteral("MRString"); // XXXjag
130 if (PR_FALSE
!= val
) {
131 printf("test 1 failed\n");
133 val
= (NS_ConvertASCIItoUTF16("mrString")).EqualsLiteral("mrStri"); // XXXjag
134 if (PR_FALSE
!= val
) {
135 printf("test 2 failed\n");
137 val
= (NS_ConvertASCIItoUTF16("mrStri")).EqualsLiteral("mrString"); // XXXjag
138 if (PR_FALSE
!= val
) {
139 printf("test 3 failed\n");
142 val
= (NS_ConvertASCIItoUTF16("mrString")).LowerCaseEqualsLiteral("mrstring");
143 if (PR_TRUE
!= val
) {
144 printf("test 4 failed\n");
146 val
= (NS_ConvertASCIItoUTF16("mrString")).LowerCaseEqualsLiteral("mrstring");
147 if (PR_TRUE
!= val
) {
148 printf("test 5 failed\n");
150 val
= (NS_ConvertASCIItoUTF16("mrString")).LowerCaseEqualsLiteral("mrstri");
151 if (PR_FALSE
!= val
) {
152 printf("test 6 failed\n");
154 val
= (NS_ConvertASCIItoUTF16("mrStri")).LowerCaseEqualsLiteral("mrstring");
155 if (PR_FALSE
!= val
) {
156 printf("test 7 failed\n");
159 val
= (NS_ConvertASCIItoUTF16("mrString")).EqualsIgnoreCase(NS_NewAtom("mrString"));
160 if (PR_TRUE
!= val
) {
161 printf("test 8 failed\n");
163 val
= (NS_ConvertASCIItoUTF16("mrString")).EqualsIgnoreCase(NS_NewAtom("MRStrINg"));
164 if (PR_TRUE
!= val
) {
165 printf("test 9 failed\n");
167 val
= (NS_ConvertASCIItoUTF16("mrString")).EqualsIgnoreCase(NS_NewAtom("mrStri"));
168 if (PR_FALSE
!= val
) {
169 printf("test 10 failed\n");
171 val
= (NS_ConvertASCIItoUTF16("mrStri")).EqualsIgnoreCase(NS_NewAtom("mrString"));
172 if (PR_FALSE
!= val
) {
173 printf("test 11 failed\n");
176 printf("string tests complete\n");
179 class MyDocument
: public nsDocument
{
182 NS_IMETHOD
StartDocumentLoad(const char* aCommand
,
183 nsIChannel
* aChannel
,
184 nsILoadGroup
* aLoadGroup
,
185 nsISupports
* aContainer
,
186 nsIStreamListener
**aDocListener
)
191 NS_IMETHOD
ImportNode(nsIDOMNode
* aImportedNode
, PRBool aDeep
, nsIDOMNode
** aReturn
) {
195 NS_IMETHOD
CreateElementNS(const nsAString
& aNamespaceURI
, const nsAString
& aQualifiedName
, nsIDOMElement
** aReturn
) {
199 NS_IMETHOD
CreateAttributeNS(const nsAString
& aNamespaceURI
, const nsAString
& aQualifiedName
, nsIDOMAttr
** aReturn
) {
203 NS_IMETHOD
GetElementsByTagNameNS(const nsAString
& aNamespaceURI
, const nsAString
& aLocalName
, nsIDOMNodeList
** aReturn
) {
207 NS_IMETHOD
GetElementById(const nsAString
& aElementId
, nsIDOMElement
** aReturn
) {
212 virtual ~MyDocument();
215 MyDocument::MyDocument()
219 MyDocument::~MyDocument()
223 int main(int argc
, char** argv
)
225 /* Create Byte2Unicode converter? Not anymore. The converters are not tested
226 here, they have their own test code. And if you just want to use them, you
227 need a properly intialized xpcom system. This simple test program doesn't do
230 // Create a unicode string
231 static const char* srcStr
= "This is some meaningless text about nothing at all";
233 PRUint32 origSrcLen
= nsCRT::strlen((char *)srcStr
);
234 const int BUFFER_LENGTH
= 100;
235 PRUnichar destStr
[BUFFER_LENGTH
];
236 PRUint32 srcLen
= origSrcLen
;
237 PRUint32 destLen
= BUFFER_LENGTH
;
238 // hacky Ascii conversion to unicode, because we don't have a real Converter.
239 for (PRUint32 i
=0; i
<srcLen
; i
++) destStr
[i
] = ((PRUint8
)srcStr
[i
]);
241 // Create test document.
242 MyDocument
*myDoc
= new MyDocument();
246 printf("Out of memory trying to create document\n");
251 // Create a new text content object.
253 rv
= NS_NewTextNode(&text
, myDoc
->NodeInfoManager());
255 printf("Could not create text content.\n");
259 nsIDOMText
* txt
= nsnull
;
260 text
->QueryInterface(NS_GET_IID(nsIDOMText
), (void**) &txt
);
261 nsAutoString
tmp(destStr
);
262 txt
->AppendData(tmp
);
265 rv
= text
->BindToTree(myDoc
, nsnull
, nsnull
, PR_FALSE
);
267 printf("Could not bind text content to tree.\n");
268 text
->UnbindFromTree();
273 // Query IContent interface
274 nsIContent
* textContent
;
275 rv
= text
->QueryInterface(NS_GET_IID(nsIContent
),(void **)&textContent
);
277 printf("Created text content does not have the IContent interface.\n");
281 // Print the contents.
282 nsAutoString stringBuf
;
283 textContent
->GetText(stringBuf
,0,textContent
->GetLength());
284 if (!stringBuf
.Equals(nsString(destStr
,destLen
))) {
285 printf("something wrong with the text in a text content\n");
289 // Create a simple container.
290 nsGenericHTMLElement
* container
;
291 nsIAtom
* li
= NS_NewAtom("li");
293 nsCOMPtr
<nsINodeInfo
> ni
;
294 myDoc
->NodeInfoManager()->GetNodeInfo(li
, nsnull
, kNameSpaceID_None
,
297 rv
= NS_NewHTMLLIElement(&container
,ni
);
299 printf("Could not create container.\n");
303 container
->AppendChildTo(text
, PR_FALSE
);
305 container
->ChildCount(nk
);
307 printf("Container has wrong number of children.");
310 printf("begin attribute tests\n");
311 testAttributes(container
);
312 printf("attribute tests complete\n");
316 text
->Release(); // The textContent interface.