Back to index

lightning-sunbird  0.9+nobinonly
nsichanneltests.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  *   David Epstein <depstein@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 // nsIChannelTests.h : implementation file for nsIChannel test cases
00040 
00041 // nsIChannelTests.cpp : implementation file
00042 //
00043 
00044 #include "stdafx.h"
00045 #include "testembed.h"
00046 #include "nsIChannelTests.h"
00047 #include "BrowserImpl.h"
00048 #include "BrowserFrm.h"
00049 #include "UrlDialog.h"
00050 #include "ProfileMgr.h"
00051 #include "ProfilesDlg.h"
00052 #include "QaUtils.h"
00053 #include "Tests.h"
00054 
00055 #ifdef _DEBUG
00056 #define new DEBUG_NEW
00057 #undef THIS_FILE
00058 static char THIS_FILE[] = __FILE__;
00059 #endif
00060 
00062 // nsIChannelTests
00063 
00064 CnsIChannelTests::CnsIChannelTests(nsIWebBrowser *mWebBrowser,
00065                                                            CBrowserImpl *mpBrowserImpl)
00066 {
00067        qaWebBrowser = mWebBrowser;
00068        qaBrowserImpl = mpBrowserImpl;
00069 }
00070 
00071 CnsIChannelTests::~CnsIChannelTests()
00072 {
00073 }
00074 
00075 ChannelRow ChannelTable[] = {
00076        {"http://www.netscape.com/", "text/plain"},
00077        {"https://www.sun.com/",    "text/html"},
00078        {"ftp://ftp.mozilla.org/",  "image/gif"},
00079        {"gopher://gopher.tc.umn.edu/", "application/vnd.mozilla.xul+xml"},
00080        {"http://www.mozilla.org/projects/embedding", "text/plain"},
00081        {"file://C|/Program Files",  "image/jpeg"},
00082        {"about:mozilla",   ""},
00083        {"javascript:document.write('hi')", ""},
00084        {"data:text/plain;charset=iso-8859-7,%be%fg%be", ""},
00085        {"jar:resource:///chrome/toolkit.jar!/content/global/plugins.html",  ""},
00086 }; 
00087 
00089 // nsIChannelTests message handlers
00090 
00091 nsIChannel * CnsIChannelTests::GetChannelObject(nsCAutoString theSpec)
00092 {
00093        theURI = GetURIObject(theSpec);
00094        if (!theURI)  
00095        {
00096           QAOutput("Didn't get URI object. Test failed.", 2);
00097           return nsnull;
00098        }
00099        rv = NS_NewChannel(getter_AddRefs(theChannel), theURI, nsnull, nsnull);
00100        RvTestResult(rv, "NS_NewChannel", 1);
00101        if (!theChannel)
00102        {
00103           QAOutput("Didn't get Channel object. GetChannelObject Test failed.", 2);
00104           return nsnull;
00105        }
00106        return theChannel;
00107 }
00108 
00109 nsIURI * CnsIChannelTests::GetURIObject(nsCAutoString theSpec)
00110 {
00111        rv = NS_NewURI(getter_AddRefs(theURI), theSpec);
00112        RvTestResult(rv, "NS_NewURI", 1);
00113        if (!theURI)
00114        {
00115           QAOutput("Didn't get URI object. GetURIObject Test failed.", 2);
00116           return nsnull;
00117        }
00118 //     nsIURI *retVal = theURI;
00119 //     NS_ADDREF(retVal);
00120 //     return retVal;
00121        return theURI;
00122 }
00123 
00124 void CnsIChannelTests::SetOriginalURITest(nsIChannel *theChannel, nsCAutoString theSpec,
00125                                                                         PRInt16 displayMode)
00126 {
00127        if (!theChannel)
00128        {
00129           QAOutput("Didn't get Channel object. SetOriginalURITest failed.", 2);
00130           return;
00131        }
00132        theURI = GetURIObject(theSpec);
00133        rv = theChannel->SetOriginalURI(theURI);
00134        RvTestResult(rv, "SetOriginalURITest", displayMode);
00135        if (displayMode == 1)
00136               RvTestResultDlg(rv, "SetOriginalURITest", true);
00137        if (!theURI)
00138           QAOutput("Didn't get URI object. SetOriginalURITest failed.", displayMode);
00139 }
00140 
00141 void CnsIChannelTests::GetOriginalURITest(nsIChannel *theChannel, PRInt16 displayMode)
00142 {
00143        if (!theChannel)
00144        {
00145           QAOutput("Didn't get Channel object. GetOriginalURITest failed.", 2);
00146           return;
00147        }
00148        rv = theChannel->GetOriginalURI(getter_AddRefs(theURI));
00149        RvTestResult(rv, "GetOriginalURITest", displayMode);
00150        if (displayMode == 1)
00151               RvTestResultDlg(rv, "GetOriginalURITest");
00152        if (!theURI)
00153        {
00154           QAOutput("Didn't get URI object. GetOriginalURITest failed.", displayMode);
00155           return;
00156        }
00157        GetTheURI(theURI);
00158 }
00159 
00160 void CnsIChannelTests::GetURITest(nsIChannel *theChannel, PRInt16 displayMode)
00161 {
00162        if (!theChannel)
00163        {
00164           QAOutput("Didn't get Channel object. GetURITest failed.", 2);
00165           return;
00166        }
00167        rv = theChannel->GetURI(getter_AddRefs(theURI));
00168        RvTestResult(rv, "GetURITest", displayMode);
00169        if (displayMode == 1)
00170               RvTestResultDlg(rv, "GetURITest");
00171        if (!theURI)
00172        {
00173           QAOutput("Didn't get URI object. GetURITest failed.", displayMode);
00174           return;
00175        }
00176        GetTheURI(theURI);
00177 }
00178 
00179 void CnsIChannelTests::SetOwnerTest(nsIChannel *theChannel, PRInt16 displayMode)
00180 {
00181        if (!theChannel)
00182        {
00183           QAOutput("Didn't get Channel object. SetOwnerTest failed.", 2);
00184           return;
00185        }
00186        theSupports = do_QueryInterface(qaWebBrowser);
00187        rv = theChannel->SetOwner(theChannel);
00188        RvTestResult(rv, "SetOwner", displayMode);
00189        if (displayMode == 1)
00190               RvTestResultDlg(rv, "SetOwner");
00191        if (!theSupports)
00192           QAOutput("Didn't get nsISupports object. SetOwnerTest failed.", displayMode);
00193 }
00194 
00195 void CnsIChannelTests::GetOwnerTest(nsIChannel *theChannel, PRInt16 displayMode)
00196 {
00197        if (!theChannel)
00198        {
00199           QAOutput("Didn't get Channel object. GetOwnerTest failed.", 2);
00200           return;
00201        }
00202        rv = theChannel->GetOwner(getter_AddRefs(theSupports));
00203        RvTestResult(rv, "GetOwner", displayMode);
00204        if (displayMode == 1)
00205               RvTestResultDlg(rv, "GetOwner");
00206        if (!theSupports)
00207           QAOutput("Didn't get nsISupports object. GetOwnerTest failed.", displayMode);
00208 }
00209 
00210 void CnsIChannelTests::SetNotificationsTest(nsIChannel *theChannel, PRInt16 displayMode)
00211 {
00212        if (!theChannel)
00213        {
00214           QAOutput("Didn't get Channel object. SetNotificationsTest failed.", 2);
00215           return;
00216        }
00217        if (!qaWebBrowser)
00218        {
00219           QAOutput("Didn't get nsIWebBrowser object. SetNotificationsTest failed.", displayMode);
00220           return;
00221        }
00222        theIRequestor = do_QueryInterface(qaWebBrowser);
00223        rv = theChannel->SetNotificationCallbacks(theIRequestor);
00224        RvTestResult(rv, "SetNotificationCallbacks", displayMode);
00225        if (displayMode == 1)
00226               RvTestResultDlg(rv, "SetNotificationCallbacks");
00227        if (!theIRequestor)
00228           QAOutput("Didn't get nsIInterfaceRequestor object. SetNotificationsTest failed.", displayMode);
00229 }
00230 
00231 void CnsIChannelTests::GetNotificationsTest(nsIChannel *theChannel, PRInt16 displayMode)
00232 {
00233        if (!theChannel)
00234        {
00235           QAOutput("Didn't get Channel object. GetNotificationsTest failed.", 2);
00236           return;
00237        }
00238        rv = theChannel->GetNotificationCallbacks(getter_AddRefs(theIRequestor));
00239        RvTestResult(rv, "GetNotificationCallbacks", displayMode);
00240        if (displayMode == 1)
00241               RvTestResultDlg(rv, "GetNotificationCallbacks");
00242        if(!theIRequestor)
00243           QAOutput("Didn't get nsIInterfaceRequestor object. GetNotificationsTest failed.", displayMode);
00244 }
00245 
00246 void CnsIChannelTests::GetSecurityInfoTest(nsIChannel *theChannel, PRInt16 displayMode)
00247 {
00248        if (!theChannel)
00249        {
00250           QAOutput("Didn't get nsIChannel object. GetSecurityInfoTest failed.", displayMode);
00251           return;
00252        }
00253        theSupports = do_QueryInterface(qaWebBrowser);
00254        rv = theChannel->GetSecurityInfo(getter_AddRefs(theSupports)); 
00255        RvTestResult(rv, "GetSecurityInfo", displayMode);
00256        if (!theSupports)
00257           QAOutput("Didn't get nsISupports object for GetSecurityInfoTest.", displayMode);
00258 }
00259 
00260 void CnsIChannelTests::SetContentTypeTest(nsIChannel *theChannel, PRInt16 displayMode)
00261 {
00262        if (!theChannel)
00263        {
00264           QAOutput("Didn't get nsIChannel object. SetContentTypeTest failed.", displayMode);
00265           return;
00266        }
00267        rv = theChannel->SetContentType(NS_LITERAL_CSTRING("text/plain"));
00268        RvTestResult(rv, "SetContentType", displayMode); 
00269 }
00270 
00271 void CnsIChannelTests::GetContentTypeTest(nsIChannel *theChannel, PRInt16 displayMode)
00272 {
00273        nsCAutoString contentType;
00274 
00275        if (!theChannel)
00276        {
00277           QAOutput("Didn't get nsIChannel object. GetContentTypeTest failed.", displayMode);
00278           return;
00279        }
00280        rv = theChannel->GetContentType(contentType);
00281        RvTestResult(rv, "GetContentType", displayMode);
00282        FormatAndPrintOutput("the content type = ", contentType, displayMode);
00283 }
00284 
00285 void CnsIChannelTests::SetContentCharsetTest(nsIChannel *theChannel, PRInt16 displayMode)
00286 {
00287        nsCAutoString charsetType;
00288 
00289        if (!theChannel)
00290        {
00291           QAOutput("Didn't get nsIChannel object. SetContentCharsetTest failed.", displayMode);
00292           return;
00293        }
00294        rv = theChannel->SetContentCharset(NS_LITERAL_CSTRING("ISO-8859-1"));
00295        RvTestResult(rv, "SetContentCharset", displayMode);
00296 }
00297 
00298 void CnsIChannelTests::GetContentCharsetTest(nsIChannel *theChannel, PRInt16 displayMode)
00299 {
00300        nsCAutoString charsetType;
00301 
00302        if (!theChannel)
00303        {
00304           QAOutput("Didn't get nsIChannel object. GetContentCharsetTest failed.", displayMode);
00305           return;
00306        }
00307        rv = theChannel->GetContentCharset(charsetType);
00308        RvTestResult(rv, "GetContentCharset", displayMode);
00309        FormatAndPrintOutput("the charset type = ", charsetType, displayMode);
00310 }
00311 
00312 void CnsIChannelTests::SetContentLengthTest(nsIChannel *theChannel, PRInt16 displayMode)
00313 {
00314        PRInt32 contentLength;
00315 
00316        contentLength = 10000;
00317 
00318        if (!theChannel)
00319        {
00320           QAOutput("Didn't get nsIChannel object. SetContentLengthTest failed.", displayMode);
00321           return;
00322        }
00323        rv = theChannel->SetContentLength(contentLength);
00324        RvTestResult(rv, "SetContentLength", displayMode);
00325 }
00326 
00327 void CnsIChannelTests::GetContentLengthTest(nsIChannel *theChannel, PRInt16 displayMode)
00328 {
00329        PRInt32 contentLength;
00330 
00331        if (!theChannel)
00332        {
00333           QAOutput("Didn't get nsIChannel object. GetContentLengthTest failed.", displayMode);
00334           return;
00335        }
00336        rv = theChannel->GetContentLength(&contentLength);
00337        RvTestResult(rv, "GetContentLength", displayMode);
00338        FormatAndPrintOutput("the content length = ", contentLength, displayMode);
00339 }
00340 
00341 void CnsIChannelTests::OpenTest(nsIChannel *theChannel, PRInt16 displayMode)
00342 {
00343        if (!theChannel)
00344        {
00345           QAOutput("Didn't get nsIChannel object. OpenTest failed.", displayMode);
00346           return;
00347        }
00348        rv =  theChannel->Open(getter_AddRefs(theInputStream));
00349        RvTestResult(rv, "OpenTest", displayMode);
00350        if (!theInputStream)
00351           QAOutput("Didn't get theInputStream object. OpenTest failed.", displayMode);
00352 }
00353 
00354 void CnsIChannelTests::AsyncOpenTest(nsIChannel *theChannel, PRInt16 displayMode)
00355 {
00356        nsCOMPtr<nsIStreamListener> listener(NS_STATIC_CAST(nsIStreamListener*, qaBrowserImpl));
00357        nsCOMPtr<nsIWeakReference> thisListener(do_GetWeakReference(listener));
00358        qaWebBrowser->AddWebBrowserListener(thisListener, NS_GET_IID(nsIStreamListener));
00359        if (!theChannel)
00360        {
00361           QAOutput("Didn't get nsIChannel object. AsyncOpenTest failed.", displayMode);
00362           return;
00363        }
00364        if (!listener) {
00365           QAOutput("Didn't get the stream listener object. AsyncOpenTest failed.", displayMode);
00366           return;
00367        }
00368        // this calls nsIStreamListener::OnDataAvailable()
00369        theSupports = do_QueryInterface(theChannel);
00370        if (!theSupports)
00371           QAOutput("Didn't get the nsISupports object. AsyncOpen() failed.", displayMode);
00372 
00373        SaveObject(theSupports);
00374 
00375        rv = theChannel->AsyncOpen(listener, theSupports);
00376        RvTestResult(rv, "AsyncOpen()", displayMode);
00377        if (displayMode == 1)
00378               RvTestResultDlg(rv, "AsyncOpen()");
00379 }
00380 
00381 void CnsIChannelTests::PostAsyncOpenTests(nsIChannel *theChannel, PRInt16 displayMode)
00382 {
00383        // These tests are run after the channel is opened (using AsyncOpen())
00384        // To run them in TestEmbed, select Tests > asyncOpen menu and enter complete URL with protocol
00385               GetOriginalURITest(theChannel, displayMode);
00386               GetURITest(theChannel, displayMode);
00387               SetOwnerTest(theChannel, displayMode);
00388               GetOwnerTest(theChannel, displayMode);
00389               SetNotificationsTest(theChannel, displayMode);
00390               GetNotificationsTest(theChannel, displayMode);
00391               GetSecurityInfoTest(theChannel, displayMode);
00392               SetContentTypeTest(theChannel, displayMode);
00393               GetContentTypeTest(theChannel, displayMode);
00394               SetContentCharsetTest(theChannel, displayMode);
00395               GetContentCharsetTest(theChannel, displayMode);
00396               SetContentLengthTest(theChannel, displayMode);
00397               GetContentLengthTest(theChannel, displayMode);
00398 }
00399 
00400 void CnsIChannelTests::OnStartTests(UINT nMenuID)
00401 {
00402        theSpec = "http://www.netscape.com";
00403        theChannel = GetChannelObject(theSpec);
00404        if (!theChannel)
00405        {
00406           QAOutput("Didn't get nsIChannel object. Test not run.", 1);
00407           return;
00408        }
00409        switch(nMenuID)
00410        {
00411               case ID_INTERFACES_NSICHANNEL_RUNALLTESTS :
00412                      RunAllTests();
00413                      break ;
00414               case ID_INTERFACES_NSICHANNEL_SETORIGINALURI :
00415                      SetOriginalURITest(theChannel, theSpec, 2);
00416                      break ;
00417               case ID_INTERFACES_NSICHANNEL_GETORIGINALURI :
00418                      GetOriginalURITest(theChannel, 2);
00419                      break ;
00420               case ID_INTERFACES_NSICHANNEL_GETURI  :
00421                      GetURITest(theChannel, 2);
00422                      break ;
00423               case ID_INTERFACES_NSICHANNEL_SETOWNER :
00424                      SetOwnerTest(theChannel, 2);
00425                      break ;
00426               case ID_INTERFACES_NSICHANNEL_GETOWNER :
00427                      GetOwnerTest(theChannel, 2);
00428                      break ;
00429               case ID_INTERFACES_NSICHANNEL_SETNOTIFICATIONS :
00430                      SetNotificationsTest(theChannel, 2);
00431                      break ;
00432               case ID_INTERFACES_NSICHANNEL_GETNOTIFICATIONS  :
00433                      GetNotificationsTest(theChannel, 2);
00434                      break ;
00435               case ID_INTERFACES_NSICHANNEL_GETSECURITYINFO  :
00436                      AsyncOpenTest(theChannel, 1);
00437                      GetSecurityInfoTest(theChannel, 2);
00438                      break ;
00439               case ID_INTERFACES_NSICHANNEL_SETCONTENTTYPE :
00440                      AsyncOpenTest(theChannel, 1);
00441                      SetContentTypeTest(theChannel, 2);
00442                      break ;
00443               case ID_INTERFACES_NSICHANNEL_GETCONTENTTYPE :
00444                      AsyncOpenTest(theChannel, 1);
00445                      GetContentTypeTest(theChannel, 2);
00446                      break ;
00447               case ID_INTERFACES_NSICHANNEL_SETCONTENTCHARSET  :
00448                      AsyncOpenTest(theChannel, 1);
00449                      SetContentCharsetTest(theChannel, 2);
00450                      break ;
00451               case ID_INTERFACES_NSICHANNEL_GETCONTENTCHARSET :
00452                      AsyncOpenTest(theChannel, 1);
00453                      GetContentCharsetTest(theChannel, 2);
00454                      break ;
00455               case ID_INTERFACES_NSICHANNEL_SETCONTENTLENGTH :
00456                      AsyncOpenTest(theChannel, 1);
00457                      SetContentLengthTest(theChannel, 2);
00458                      break ;
00459               case ID_INTERFACES_NSICHANNEL_GETCONTENTLENGTH :
00460                      AsyncOpenTest(theChannel, 1);
00461                      GetContentLengthTest(theChannel, 2);
00462                      break ;
00463               case ID_INTERFACES_NSICHANNEL_OPEN  :
00464                      OpenTest(theChannel, 2);
00465                      break ;
00466               case ID_INTERFACES_NSICHANNEL_ASYNCOPEN  :
00467                      AsyncOpenTest(theChannel, 2);
00468                      break ;
00469        }
00470 }
00471 
00472 void CnsIChannelTests::RunAllTests()
00473 {
00474        int i;
00475 
00476        for (i=0; i<10; i++)
00477        {
00478               theSpec = ChannelTable[i].theURL;
00479               theChannel = GetChannelObject(theSpec);
00480               if (!theChannel)
00481               {
00482                  QAOutput("Didn't get nsIChannel object. RunAllTests not run.", 2);
00483                  return;
00484               }
00485               QAOutput("\nStart nsIChannel Tests: ");
00486               SetOriginalURITest(theChannel, theSpec, 1);
00487               GetOriginalURITest(theChannel, 1);
00488               GetURITest(theChannel, 1);
00489               SetOwnerTest(theChannel, 1);
00490               GetOwnerTest(theChannel, 1);
00491               SetNotificationsTest(theChannel, 1);
00492               GetNotificationsTest(theChannel, 1);
00493               AsyncOpenTest(theChannel, 1);
00494         // PostAsyncOpenTests() called from nsIRequestObserver::OnStartRequest (in BrowserImpl.cpp)
00495               QAOutput("\n");
00496        }
00497 }