1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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.
23 * David Epstein <depstein@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 // nsIChannelTests.h : implementation file for nsIChannel test cases
41 // nsIChannelTests.cpp : implementation file
45 #include "testembed.h"
46 #include "nsIChannelTests.h"
47 #include "BrowserImpl.h"
48 #include "BrowserFrm.h"
49 #include "UrlDialog.h"
50 #include "ProfileMgr.h"
51 #include "ProfilesDlg.h"
58 static char THIS_FILE
[] = __FILE__
;
61 /////////////////////////////////////////////////////////////////////////////
64 CnsIChannelTests::CnsIChannelTests(nsIWebBrowser
*mWebBrowser
,
65 CBrowserImpl
*mpBrowserImpl
)
67 qaWebBrowser
= mWebBrowser
;
68 qaBrowserImpl
= mpBrowserImpl
;
71 CnsIChannelTests::~CnsIChannelTests()
75 ChannelRow ChannelTable
[] = {
76 {"http://www.netscape.com/", "text/plain"},
77 {"https://www.sun.com/", "text/html"},
78 {"ftp://ftp.mozilla.org/", "image/gif"},
79 {"gopher://gopher.tc.umn.edu/", "application/vnd.mozilla.xul+xml"},
80 {"http://www.mozilla.org/projects/embedding", "text/plain"},
81 {"file://C|/Program Files", "image/jpeg"},
82 {"about:mozilla", ""},
83 {"javascript:document.write('hi')", ""},
84 {"data:text/plain;charset=iso-8859-7,%be%fg%be", ""},
85 {"jar:resource:///chrome/toolkit.jar!/content/global/plugins.html", ""},
88 /////////////////////////////////////////////////////////////////////////////
89 // nsIChannelTests message handlers
91 nsIChannel
* CnsIChannelTests::GetChannelObject(nsCAutoString theSpec
)
93 theURI
= GetURIObject(theSpec
);
96 QAOutput("Didn't get URI object. Test failed.", 2);
99 rv
= NS_NewChannel(getter_AddRefs(theChannel
), theURI
, nsnull
, nsnull
);
100 RvTestResult(rv
, "NS_NewChannel", 1);
103 QAOutput("Didn't get Channel object. GetChannelObject Test failed.", 2);
109 nsIURI
* CnsIChannelTests::GetURIObject(nsCAutoString theSpec
)
111 rv
= NS_NewURI(getter_AddRefs(theURI
), theSpec
);
112 RvTestResult(rv
, "NS_NewURI", 1);
115 QAOutput("Didn't get URI object. GetURIObject Test failed.", 2);
118 // nsIURI *retVal = theURI;
119 // NS_ADDREF(retVal);
124 void CnsIChannelTests::SetOriginalURITest(nsIChannel
*theChannel
, nsCAutoString theSpec
,
129 QAOutput("Didn't get Channel object. SetOriginalURITest failed.", 2);
132 theURI
= GetURIObject(theSpec
);
133 rv
= theChannel
->SetOriginalURI(theURI
);
134 RvTestResult(rv
, "SetOriginalURITest", displayMode
);
135 if (displayMode
== 1)
136 RvTestResultDlg(rv
, "SetOriginalURITest", true);
138 QAOutput("Didn't get URI object. SetOriginalURITest failed.", displayMode
);
141 void CnsIChannelTests::GetOriginalURITest(nsIChannel
*theChannel
, PRInt16 displayMode
)
145 QAOutput("Didn't get Channel object. GetOriginalURITest failed.", 2);
148 rv
= theChannel
->GetOriginalURI(getter_AddRefs(theURI
));
149 RvTestResult(rv
, "GetOriginalURITest", displayMode
);
150 if (displayMode
== 1)
151 RvTestResultDlg(rv
, "GetOriginalURITest");
154 QAOutput("Didn't get URI object. GetOriginalURITest failed.", displayMode
);
160 void CnsIChannelTests::GetURITest(nsIChannel
*theChannel
, PRInt16 displayMode
)
164 QAOutput("Didn't get Channel object. GetURITest failed.", 2);
167 rv
= theChannel
->GetURI(getter_AddRefs(theURI
));
168 RvTestResult(rv
, "GetURITest", displayMode
);
169 if (displayMode
== 1)
170 RvTestResultDlg(rv
, "GetURITest");
173 QAOutput("Didn't get URI object. GetURITest failed.", displayMode
);
179 void CnsIChannelTests::SetOwnerTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
183 QAOutput("Didn't get Channel object. SetOwnerTest failed.", 2);
186 theSupports
= do_QueryInterface(qaWebBrowser
);
187 rv
= theChannel
->SetOwner(theChannel
);
188 RvTestResult(rv
, "SetOwner", displayMode
);
189 if (displayMode
== 1)
190 RvTestResultDlg(rv
, "SetOwner");
192 QAOutput("Didn't get nsISupports object. SetOwnerTest failed.", displayMode
);
195 void CnsIChannelTests::GetOwnerTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
199 QAOutput("Didn't get Channel object. GetOwnerTest failed.", 2);
202 rv
= theChannel
->GetOwner(getter_AddRefs(theSupports
));
203 RvTestResult(rv
, "GetOwner", displayMode
);
204 if (displayMode
== 1)
205 RvTestResultDlg(rv
, "GetOwner");
207 QAOutput("Didn't get nsISupports object. GetOwnerTest failed.", displayMode
);
210 void CnsIChannelTests::SetNotificationsTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
214 QAOutput("Didn't get Channel object. SetNotificationsTest failed.", 2);
219 QAOutput("Didn't get nsIWebBrowser object. SetNotificationsTest failed.", displayMode
);
222 theIRequestor
= do_QueryInterface(qaWebBrowser
);
223 rv
= theChannel
->SetNotificationCallbacks(theIRequestor
);
224 RvTestResult(rv
, "SetNotificationCallbacks", displayMode
);
225 if (displayMode
== 1)
226 RvTestResultDlg(rv
, "SetNotificationCallbacks");
228 QAOutput("Didn't get nsIInterfaceRequestor object. SetNotificationsTest failed.", displayMode
);
231 void CnsIChannelTests::GetNotificationsTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
235 QAOutput("Didn't get Channel object. GetNotificationsTest failed.", 2);
238 rv
= theChannel
->GetNotificationCallbacks(getter_AddRefs(theIRequestor
));
239 RvTestResult(rv
, "GetNotificationCallbacks", displayMode
);
240 if (displayMode
== 1)
241 RvTestResultDlg(rv
, "GetNotificationCallbacks");
243 QAOutput("Didn't get nsIInterfaceRequestor object. GetNotificationsTest failed.", displayMode
);
246 void CnsIChannelTests::GetSecurityInfoTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
250 QAOutput("Didn't get nsIChannel object. GetSecurityInfoTest failed.", displayMode
);
253 theSupports
= do_QueryInterface(qaWebBrowser
);
254 rv
= theChannel
->GetSecurityInfo(getter_AddRefs(theSupports
));
255 RvTestResult(rv
, "GetSecurityInfo", displayMode
);
257 QAOutput("Didn't get nsISupports object for GetSecurityInfoTest.", displayMode
);
260 void CnsIChannelTests::SetContentTypeTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
264 QAOutput("Didn't get nsIChannel object. SetContentTypeTest failed.", displayMode
);
267 rv
= theChannel
->SetContentType(NS_LITERAL_CSTRING("text/plain"));
268 RvTestResult(rv
, "SetContentType", displayMode
);
271 void CnsIChannelTests::GetContentTypeTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
273 nsCAutoString contentType
;
277 QAOutput("Didn't get nsIChannel object. GetContentTypeTest failed.", displayMode
);
280 rv
= theChannel
->GetContentType(contentType
);
281 RvTestResult(rv
, "GetContentType", displayMode
);
282 FormatAndPrintOutput("the content type = ", contentType
, displayMode
);
285 void CnsIChannelTests::SetContentCharsetTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
287 nsCAutoString charsetType
;
291 QAOutput("Didn't get nsIChannel object. SetContentCharsetTest failed.", displayMode
);
294 rv
= theChannel
->SetContentCharset(NS_LITERAL_CSTRING("ISO-8859-1"));
295 RvTestResult(rv
, "SetContentCharset", displayMode
);
298 void CnsIChannelTests::GetContentCharsetTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
300 nsCAutoString charsetType
;
304 QAOutput("Didn't get nsIChannel object. GetContentCharsetTest failed.", displayMode
);
307 rv
= theChannel
->GetContentCharset(charsetType
);
308 RvTestResult(rv
, "GetContentCharset", displayMode
);
309 FormatAndPrintOutput("the charset type = ", charsetType
, displayMode
);
312 void CnsIChannelTests::SetContentLengthTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
314 PRInt32 contentLength
;
316 contentLength
= 10000;
320 QAOutput("Didn't get nsIChannel object. SetContentLengthTest failed.", displayMode
);
323 rv
= theChannel
->SetContentLength(contentLength
);
324 RvTestResult(rv
, "SetContentLength", displayMode
);
327 void CnsIChannelTests::GetContentLengthTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
329 PRInt32 contentLength
;
333 QAOutput("Didn't get nsIChannel object. GetContentLengthTest failed.", displayMode
);
336 rv
= theChannel
->GetContentLength(&contentLength
);
337 RvTestResult(rv
, "GetContentLength", displayMode
);
338 FormatAndPrintOutput("the content length = ", contentLength
, displayMode
);
341 void CnsIChannelTests::OpenTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
345 QAOutput("Didn't get nsIChannel object. OpenTest failed.", displayMode
);
348 rv
= theChannel
->Open(getter_AddRefs(theInputStream
));
349 RvTestResult(rv
, "OpenTest", displayMode
);
351 QAOutput("Didn't get theInputStream object. OpenTest failed.", displayMode
);
354 void CnsIChannelTests::AsyncOpenTest(nsIChannel
*theChannel
, PRInt16 displayMode
)
356 nsCOMPtr
<nsIStreamListener
> listener(static_cast<nsIStreamListener
*>(qaBrowserImpl
));
357 nsCOMPtr
<nsIWeakReference
> thisListener(do_GetWeakReference(listener
));
358 qaWebBrowser
->AddWebBrowserListener(thisListener
, NS_GET_IID(nsIStreamListener
));
361 QAOutput("Didn't get nsIChannel object. AsyncOpenTest failed.", displayMode
);
365 QAOutput("Didn't get the stream listener object. AsyncOpenTest failed.", displayMode
);
368 // this calls nsIStreamListener::OnDataAvailable()
369 theSupports
= do_QueryInterface(theChannel
);
371 QAOutput("Didn't get the nsISupports object. AsyncOpen() failed.", displayMode
);
373 SaveObject(theSupports
);
375 rv
= theChannel
->AsyncOpen(listener
, theSupports
);
376 RvTestResult(rv
, "AsyncOpen()", displayMode
);
377 if (displayMode
== 1)
378 RvTestResultDlg(rv
, "AsyncOpen()");
381 void CnsIChannelTests::PostAsyncOpenTests(nsIChannel
*theChannel
, PRInt16 displayMode
)
383 // These tests are run after the channel is opened (using AsyncOpen())
384 // To run them in TestEmbed, select Tests > asyncOpen menu and enter complete URL with protocol
385 GetOriginalURITest(theChannel
, displayMode
);
386 GetURITest(theChannel
, displayMode
);
387 SetOwnerTest(theChannel
, displayMode
);
388 GetOwnerTest(theChannel
, displayMode
);
389 SetNotificationsTest(theChannel
, displayMode
);
390 GetNotificationsTest(theChannel
, displayMode
);
391 GetSecurityInfoTest(theChannel
, displayMode
);
392 SetContentTypeTest(theChannel
, displayMode
);
393 GetContentTypeTest(theChannel
, displayMode
);
394 SetContentCharsetTest(theChannel
, displayMode
);
395 GetContentCharsetTest(theChannel
, displayMode
);
396 SetContentLengthTest(theChannel
, displayMode
);
397 GetContentLengthTest(theChannel
, displayMode
);
400 void CnsIChannelTests::OnStartTests(UINT nMenuID
)
402 theSpec
= "http://www.netscape.com";
403 theChannel
= GetChannelObject(theSpec
);
406 QAOutput("Didn't get nsIChannel object. Test not run.", 1);
411 case ID_INTERFACES_NSICHANNEL_RUNALLTESTS
:
414 case ID_INTERFACES_NSICHANNEL_SETORIGINALURI
:
415 SetOriginalURITest(theChannel
, theSpec
, 2);
417 case ID_INTERFACES_NSICHANNEL_GETORIGINALURI
:
418 GetOriginalURITest(theChannel
, 2);
420 case ID_INTERFACES_NSICHANNEL_GETURI
:
421 GetURITest(theChannel
, 2);
423 case ID_INTERFACES_NSICHANNEL_SETOWNER
:
424 SetOwnerTest(theChannel
, 2);
426 case ID_INTERFACES_NSICHANNEL_GETOWNER
:
427 GetOwnerTest(theChannel
, 2);
429 case ID_INTERFACES_NSICHANNEL_SETNOTIFICATIONS
:
430 SetNotificationsTest(theChannel
, 2);
432 case ID_INTERFACES_NSICHANNEL_GETNOTIFICATIONS
:
433 GetNotificationsTest(theChannel
, 2);
435 case ID_INTERFACES_NSICHANNEL_GETSECURITYINFO
:
436 AsyncOpenTest(theChannel
, 1);
437 GetSecurityInfoTest(theChannel
, 2);
439 case ID_INTERFACES_NSICHANNEL_SETCONTENTTYPE
:
440 AsyncOpenTest(theChannel
, 1);
441 SetContentTypeTest(theChannel
, 2);
443 case ID_INTERFACES_NSICHANNEL_GETCONTENTTYPE
:
444 AsyncOpenTest(theChannel
, 1);
445 GetContentTypeTest(theChannel
, 2);
447 case ID_INTERFACES_NSICHANNEL_SETCONTENTCHARSET
:
448 AsyncOpenTest(theChannel
, 1);
449 SetContentCharsetTest(theChannel
, 2);
451 case ID_INTERFACES_NSICHANNEL_GETCONTENTCHARSET
:
452 AsyncOpenTest(theChannel
, 1);
453 GetContentCharsetTest(theChannel
, 2);
455 case ID_INTERFACES_NSICHANNEL_SETCONTENTLENGTH
:
456 AsyncOpenTest(theChannel
, 1);
457 SetContentLengthTest(theChannel
, 2);
459 case ID_INTERFACES_NSICHANNEL_GETCONTENTLENGTH
:
460 AsyncOpenTest(theChannel
, 1);
461 GetContentLengthTest(theChannel
, 2);
463 case ID_INTERFACES_NSICHANNEL_OPEN
:
464 OpenTest(theChannel
, 2);
466 case ID_INTERFACES_NSICHANNEL_ASYNCOPEN
:
467 AsyncOpenTest(theChannel
, 2);
472 void CnsIChannelTests::RunAllTests()
478 theSpec
= ChannelTable
[i
].theURL
;
479 theChannel
= GetChannelObject(theSpec
);
482 QAOutput("Didn't get nsIChannel object. RunAllTests not run.", 2);
485 QAOutput("\nStart nsIChannel Tests: ");
486 SetOriginalURITest(theChannel
, theSpec
, 1);
487 GetOriginalURITest(theChannel
, 1);
488 GetURITest(theChannel
, 1);
489 SetOwnerTest(theChannel
, 1);
490 GetOwnerTest(theChannel
, 1);
491 SetNotificationsTest(theChannel
, 1);
492 GetNotificationsTest(theChannel
, 1);
493 AsyncOpenTest(theChannel
, 1);
494 // PostAsyncOpenTests() called from nsIRequestObserver::OnStartRequest (in BrowserImpl.cpp)