Back to index

lightning-sunbird  0.9+nobinonly
nsirequest.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  *   Ashish Bhatt <ashishbhatt@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 // File Overview....
00041 //
00042 // Test cases for the nsiRequest Interface
00043 
00044 #include "stdafx.h"
00045 #include "TestEmbed.h"
00046 #include "BrowserImpl.h"
00047 #include "BrowserFrm.h"
00048 #include "UrlDialog.h"
00049 #include "ProfileMgr.h"
00050 #include "ProfilesDlg.h"
00051 #include "QaUtils.h"
00052 #include "nsirequest.h"
00053 #include <stdio.h>
00054 
00055 
00057 // CNsIRequest
00058 
00059 
00060 CNsIRequest::CNsIRequest(nsIWebBrowser* mWebBrowser,CBrowserImpl *mpBrowserImpl)
00061 {
00062        qaWebBrowser = mWebBrowser ;
00063        qaBrowserImpl = mpBrowserImpl ;
00064 }
00065 
00066 
00067 CNsIRequest::~CNsIRequest()
00068 {
00069 }
00070 
00071 
00073 // CNsIRequest message handlers
00074 // ***********************************************************************
00075 // ***********************************************************************
00076 //  nsIRequest iface
00077 
00078 //  table columns corrsp to: pending, status, suspend, resume, cancel,
00079 //  setLoadGroup & getLoadGroup tests respectively.
00080 
00081 Element ReqTable[] = {
00082        {"http://www.netscape.com/", 1, 1, 0, 0, 0, 1, 1},
00083        {"http://www.yahoo.com/",    0, 0, 1, 1, 0, 0, 0},
00084        {"http://www.cisco.com/",    0, 0, 0, 0, 1, 0, 0},
00085        {"http://www.sun.com/",      0, 0, 0, 0, 0, 1, 1},
00086        {"http://www.intel.com/",    1, 1, 1, 0, 0, 0, 0},
00087        {"http://www.aol.com/",      0, 1, 0, 0, 0, 1, 1},
00088        {"https://www.yahoo.com/",   1, 1, 1, 1, 0, 1, 1},
00089        {"data:text/plain;charset=iso-8859-7,%be%fg%be",
00090                                                          1, 1, 1, 1, 0, 1, 1},
00091        {"file://C|/Program Files/", 1, 1, 1, 1, 0, 1, 1},
00092        {"ftp://ftp.netscape.com/",  1, 1, 1, 1, 0, 1, 1},
00093        {"ftp://ftp.mozilla.org/",   0, 0, 0, 0, 1, 0, 0},
00094 };
00095 
00096 void CNsIRequest::OnStartTests(UINT nMenuID)
00097 {
00098        if (nMenuID == ID_INTERFACES_NSIREQUEST_RUNALLTESTS)
00099               RunAllTests(9);
00100        else
00101               RunIndividualTests(nMenuID, 9);
00102 }
00103  
00104 void CNsIRequest::RunIndividualTests(UINT nMenuID, int reqTotal)
00105 {
00106        nsCOMPtr<nsIChannel> theChannel;
00107        nsCOMPtr<nsILoadGroup> theLoadGroup(do_CreateInstance(NS_LOADGROUP_CONTRACTID));
00108        nsCOMPtr<nsIURI> theURI;
00109        if (!theLoadGroup)
00110        {
00111               QAOutput("We didn't get the Load Group. Test failed.", 2);
00112               return;
00113        }
00114        int i=0;
00115 
00116     QAOutput("Start selected nsIRequest test.", 2);     
00117 
00118        for (i=0; i<reqTotal; i++)
00119        {
00120               // nsIRequest individual tests
00121 
00122               QAOutput("********** Individual nsIRequest test begins. **********");
00123 
00124               theChannel = GetTheChannel(i, theLoadGroup);
00125               nsCOMPtr<nsIRequest> theRequest = do_QueryInterface(theChannel);
00126 
00127               switch(nMenuID)
00128               {
00129                      
00130               case ID_INTERFACES_NSIREQUEST_GETNAME :
00131                      break;
00132               case ID_INTERFACES_NSIREQUEST_ISPENDING :
00133                      IsPendingReqTest(theRequest);
00134                      break;
00135               case ID_INTERFACES_NSIREQUEST_GETSTATUS :
00136                      GetStatusReqTest(theRequest);
00137                      break;
00138               case ID_INTERFACES_NSIREQUEST_CANCEL :
00139                      CancelReqTest(theRequest);  
00140                      break;
00141               case ID_INTERFACES_NSIREQUEST_SUSPEND :
00142                      SuspendReqTest(theRequest); 
00143                      break;
00144               case ID_INTERFACES_NSIREQUEST_RESUME :
00145                      ResumeReqTest(theRequest);  
00146                      break;
00147               case ID_INTERFACES_NSIREQUEST_GETLOADGROUP :
00148                      GetLoadGroupTest(theRequest);
00149                      break;
00150               case ID_INTERFACES_NSIREQUEST_SETLOADGROUP :
00151                      SetLoadGroupTest(theRequest, theLoadGroup);      
00152                      break;
00153               case ID_INTERFACES_NSIREQUEST_GETLOADFLAGS :
00154                      break;
00155               case ID_INTERFACES_NSIREQUEST_SETLOADFLAGS :
00156                      break;
00157               }
00158 
00159        } // end for loop
00160 }
00161 
00162 
00163 void CNsIRequest::RunAllTests(int reqTotal) 
00164 {
00165        // note: nsIRequest tests are called:
00166        // 1) in BrowserImpl.cpp, nsIStreamListener::OnDataAvailable()
00167        // 2) as individual tests below
00168 
00169        nsCOMPtr<nsIChannel> theChannel;
00170        nsCOMPtr<nsILoadGroup> theLoadGroup(do_CreateInstance(NS_LOADGROUP_CONTRACTID));
00171        if (!theLoadGroup)
00172        {
00173               QAOutput("We didn't get the Load Group. Test failed.", 2);
00174               return;
00175        }
00176 
00177        int i=0;
00178 
00179     QAOutput("Start nsIRequest tests.", 1);      
00180 
00181        for (i=0; i<reqTotal; i++)
00182        {
00183               // nsIRequest individual tests
00184 
00185               QAOutput("********** Individual nsIRequest test begins. **********");
00186 
00187               theChannel = GetTheChannel(i, theLoadGroup);
00188               nsCOMPtr<nsIRequest> theRequest = do_QueryInterface(theChannel);
00189 
00190               if (ReqTable[i].reqPend == TRUE)
00191                      IsPendingReqTest(theRequest);
00192 
00193               if (ReqTable[i].reqStatus == TRUE)
00194                      GetStatusReqTest(theRequest);
00195 
00196               if (ReqTable[i].reqSuspend == TRUE)
00197                      SuspendReqTest(theRequest); 
00198 
00199               if (ReqTable[i].reqResume == TRUE)
00200                      ResumeReqTest(theRequest);  
00201 
00202               if (ReqTable[i].reqCancel == TRUE)
00203                      CancelReqTest(theRequest);  
00204 
00205               if (ReqTable[i].reqSetLoadGroup == TRUE)
00206                      SetLoadGroupTest(theRequest, theLoadGroup);      
00207 
00208               if (ReqTable[i].reqGetLoadGroup == TRUE)
00209                      GetLoadGroupTest(theRequest);
00210 
00211               QAOutput("- - - - - - - - - - - - - - - - - - - - -", 1);
00212        } // end for loop
00213     QAOutput("End nsIRequest tests.", 1);
00214 }
00215 
00216 
00217 nsIChannel * CNsIRequest::GetTheChannel(int i, nsILoadGroup *theLoadGroup)
00218 {
00219        nsCAutoString theSpec, retURI;
00220        nsCOMPtr<nsIURI> theURI;
00221        nsCOMPtr<nsIChannel> theChannel;
00222 
00223        theSpec = ReqTable[i].theUrl;
00224        FormatAndPrintOutput("the input uri = ", theSpec, 1);
00225 
00226        rv = NS_NewURI(getter_AddRefs(theURI), theSpec);
00227 
00228        if (!theURI)
00229        {
00230           QAOutput("We didn't get the URI. Test failed.", 1);
00231           return nsnull;
00232        }
00233        else {
00234           retURI = GetTheURI(theURI, 1);
00235           // simple string compare to see if input & output URLs match
00236           if (strcmp(ReqTable[i].theUrl, retURI.get()) == 0)
00237                 QAOutput("The in/out URIs MATCH.", 1);
00238           else
00239                 QAOutput("The in/out URIs don't MATCH.", 1);
00240           RvTestResult(rv, "NS_NewURI", 1);
00241           RvTestResultDlg(rv, "NS_NewURI", true);
00242        }
00243 
00244        rv = NS_NewChannel(getter_AddRefs(theChannel), theURI, nsnull, theLoadGroup);
00245        if (!theChannel)
00246        {
00247           QAOutput("We didn't get the Channel. Test failed.", 1);
00248           return nsnull;
00249        }
00250        else {
00251           RvTestResult(rv, "NS_NewChannel", 1);
00252           RvTestResultDlg(rv, "NS_NewChannel");
00253        }
00254 
00255        nsCOMPtr<nsIStreamListener> listener(NS_STATIC_CAST(nsIStreamListener*, qaBrowserImpl));
00256        nsCOMPtr<nsIWeakReference> thisListener(do_GetWeakReference(listener));
00257        qaWebBrowser->AddWebBrowserListener(thisListener, NS_GET_IID(nsIStreamListener));
00258 
00259        if (!listener) {
00260           QAOutput("We didn't get the listener for AsyncOpen(). Test failed.", 1);
00261           return nsnull;
00262        }
00263        // this calls nsIStreamListener::OnDataAvailable()
00264        rv = theChannel->AsyncOpen(listener, nsnull);
00265        RvTestResult(rv, "AsyncOpen()", 1);
00266        RvTestResultDlg(rv, "AsyncOpen()");
00267 
00268        return theChannel;
00269 }
00270 
00271 
00272 void CNsIRequest::IsPendingReqTest(nsIRequest *request)
00273 {
00274        PRBool   reqPending;
00275        nsresult rv;  
00276 
00277        rv = request->IsPending(&reqPending);
00278     RvTestResult(rv, "nsIRequest::IsPending() rv test", 1);
00279        RvTestResultDlg(rv, "nsIRequest::IsPending() rv test()");
00280 
00281        if (!reqPending)
00282               QAOutput("Pending request = false.", 1);
00283        else
00284               QAOutput("Pending request = true.", 1);
00285 }
00286 
00287 void CNsIRequest::GetStatusReqTest(nsIRequest *request)
00288 {
00289        nsresult      theStatusError;
00290        nsresult rv;
00291 
00292        rv = request->GetStatus(&theStatusError);
00293     RvTestResult(rv, "nsIRequest::GetStatus() test", 1);
00294        RvTestResultDlg(rv, "nsIRequest::GetStatus() test");
00295     RvTestResult(theStatusError, "the returned status error test", 1);
00296     RvTestResultDlg(theStatusError, "the returned status error test");
00297 } 
00298 
00299 void CNsIRequest::SuspendReqTest(nsIRequest *request)
00300 {
00301        nsresult      rv;
00302 
00303        rv = request->Suspend();
00304     RvTestResult(rv, "nsIRequest::Suspend() test", 1);
00305     RvTestResultDlg(rv, "nsIRequest::Suspend() test");
00306 }
00307 
00308 void CNsIRequest::ResumeReqTest(nsIRequest *request)
00309 {
00310        nsresult      rv;
00311 
00312        rv = request->Resume();
00313     RvTestResult(rv, "nsIRequest::Resume() test", 1);
00314     RvTestResultDlg(rv, "nsIRequest::Resume() test");
00315 }
00316 
00317 void CNsIRequest::CancelReqTest(nsIRequest *request)
00318 {
00319        nsresult      rv;
00320        nsresult      status = NS_BINDING_ABORTED;
00321 
00322        rv = request->Cancel(status);
00323     RvTestResult(rv, "nsIRequest::Cancel() rv test", 1);
00324     RvTestResultDlg(rv, "nsIRequest::Cancel() test");
00325     RvTestResult(status, "nsIRequest::Cancel() status test", 1);
00326     RvTestResultDlg(status, "nsIRequest::Cancel() status test");
00327 }
00328 
00329 void CNsIRequest::SetLoadGroupTest(nsIRequest *request,
00330                                                    nsILoadGroup *theLoadGroup)
00331 {
00332        nsresult      rv;
00333        nsCOMPtr<nsISimpleEnumerator> theSimpEnum;
00334 
00335        rv = request->SetLoadGroup(theLoadGroup);
00336     RvTestResult(rv, "nsIRequest::SetLoadGroup() rv test", 1);
00337     RvTestResultDlg(rv, "nsIRequest::SetLoadGroup() rv test");
00338 }
00339 
00340 void CNsIRequest::GetLoadGroupTest(nsIRequest *request)
00341 {
00342        nsCOMPtr<nsILoadGroup> theLoadGroup;
00343        nsresult      rv;
00344        nsCOMPtr<nsISimpleEnumerator> theSimpEnum;
00345 
00346        rv = request->GetLoadGroup(getter_AddRefs(theLoadGroup));
00347     RvTestResult(rv, "nsIRequest::GetLoadGroup() rv test", 1);
00348     RvTestResultDlg(rv, "nsIRequest::GetLoadGroup() rv test");
00349 
00350        rv = theLoadGroup->GetRequests(getter_AddRefs(theSimpEnum));
00351     RvTestResult(rv, "nsIRequest:: LoadGroups' GetRequests() rv test", 1);
00352     RvTestResultDlg(rv, "nsIRequest:: LoadGroups' GetRequests() rv test");
00353 }
00354