Back to index

lightning-sunbird  0.9+nobinonly
nsIWebNav.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 nsiWebNavigation Interface
00043 
00044 
00045 
00046 #include "stdafx.h"
00047 #include "testembed.h"
00048 #include "qautils.h"
00049 #include "nsiwebnav.h"
00050 #include "UrlDialog.h"
00051 
00052 #ifdef _DEBUG
00053 #define new DEBUG_NEW
00054 #undef THIS_FILE
00055 static char THIS_FILE[] = __FILE__;
00056 #endif
00057 
00059 // CNsIWebNav 
00060 
00061 CNsIWebNav::CNsIWebNav(nsIWebNavigation *mWebNav)
00062 {
00063        qaWebNav = mWebNav ;
00064 }
00065 
00066 CNsIWebNav::~CNsIWebNav()
00067 {
00068 }
00069 
00070 
00072 // CNsIWebNav message handlers
00073 // ***********************************************************************
00074 // nsIWebNavigation iface
00075 // ***********************************************************************
00076 
00077 // Url table for web navigation
00078 NavElement UrlTable[] = {
00079    {"http://www.intel.com/", nsIWebNavigation::LOAD_FLAGS_NONE},
00080    {"http://www.yahoo.com/", nsIWebNavigation::LOAD_FLAGS_MASK},
00081    {"http://www.oracle.com/", nsIWebNavigation::LOAD_FLAGS_IS_LINK},
00082    {"http://www.sun.com/", nsIWebNavigation::LOAD_FLAGS_IS_REFRESH},
00083    {"ftp://ftp.netscape.com", nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY},
00084    {"ftp://ftp.mozilla.org/", nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY},
00085    {"https://www.motorola.com/", nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE},
00086    {"https://www.amazon.com", nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY},
00087    {"about:plugins", nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE},
00088    {"javascript: document.write('Test!')", nsIWebNavigation::LOAD_FLAGS_NONE},
00089    {"file://C|/Program Files", nsIWebNavigation::LOAD_FLAGS_NONE}
00090 };
00091 
00092 
00093 void CNsIWebNav::OnStartTests(UINT nMenuID)
00094 {
00095        switch(nMenuID)
00096        {
00097               case ID_INTERFACES_NSIWEBNAV_RUNALLTESTS :
00098                      RunAllTests();
00099                      break ;
00100               case ID_INTERFACES_NSIWEBNAV_GETCANGOBACK :
00101                   CanGoBackTest(2);
00102                      break ;
00103               case ID_INTERFACES_NSIWEBNAV_GETCANGOFORWARD :
00104                      CanGoForwardTest(2);
00105                      break ;
00106               case ID_INTERFACES_NSIWEBNAV_GOBACK  :
00107                      GoBackTest(2);
00108                      break ;
00109               case ID_INTERFACES_NSIWEBNAV_GOFORWARD :
00110                      GoForwardTest(2);
00111                      break ;
00112               case ID_INTERFACES_NSIWEBNAV_GOTOINDEX :
00113                      GoToIndexTest(2);
00114                      break ;
00115               case ID_INTERFACES_NSIWEBNAV_LOADURI :
00116                      LoadURITest(nsnull, nsnull, 2, PR_FALSE);
00117                      break ;
00118               case ID_INTERFACES_NSIWEBNAV_RELOAD  :
00119                      ReloadTest(nsIWebNavigation::LOAD_FLAGS_NONE, 2);
00120                      break ;
00121               case ID_INTERFACES_NSIWEBNAV_STOP    :
00122                      StopURITest("file://C|/Program Files",
00123                                            nsIWebNavigation::STOP_CONTENT, 2);
00124                      break ;
00125               case ID_INTERFACES_NSIWEBNAV_GETDOCUMENT :
00126                      GetDocumentTest(2);
00127                      break ;
00128               case ID_INTERFACES_NSIWEBNAV_GETCURRENTURI :
00129                      GetCurrentURITest(2);
00130                      break ;
00131               case ID_INTERFACES_NSIWEBNAV_GETREFERRINGURI:
00132                      GetReferringURITest(2);
00133                      break;
00134               case ID_INTERFACES_NSIWEBNAV_GETSESSIONHISTORY :
00135                      GetSHTest(2);
00136                      break ;
00137               case ID_INTERFACES_NSIWEBNAV_SETSESSIONHISTORY :
00138                      SetSHTest(2);
00139                      break ;
00140        }
00141 }
00142 
00143 void CNsIWebNav::RunAllTests()
00144 {
00145    int i=0;
00146 
00147    if (qaWebNav)
00148           QAOutput("We have the web nav object.", 1);
00149    else {
00150           QAOutput("We don't have the web nav object. No tests performed.", 2);
00151           return;
00152    }
00153 
00154    // load a couple of URLs to get things going
00155        LoadURITest("http://www.cisco.com", nsIWebNavigation::LOAD_FLAGS_NONE, 2, PR_TRUE);
00156        LoadURITest("www.google.com", nsIWebNavigation::LOAD_FLAGS_NONE, 2, PR_TRUE);
00157        
00158    // canGoBack attribute test
00159    CanGoBackTest(1);
00160 
00161    // GoBack test
00162    GoBackTest(2);
00163 
00164    // canGoForward attribute test
00165    CanGoForwardTest(1);
00166 
00167    // GoForward test
00168    GoForwardTest(2);
00169 
00170    // GotoIndex test
00171    GoToIndexTest(2);
00172 
00173    // LoadURI() & reload tests
00174 
00175    QAOutput("Run a few LoadURI() tests.", 1);
00176 
00177        
00178    LoadUriandReload(11);
00179 
00180  
00181        // Stop() tests
00182    StopURITest("http://www.microsoft.com", nsIWebNavigation::STOP_ALL, 1);
00183    StopURITest("https://www.microsoft.com/", nsIWebNavigation::STOP_NETWORK, 1);
00184    StopURITest("ftp://ftp.microsoft.com/", nsIWebNavigation::STOP_CONTENT, 1);
00185 
00186    // document test
00187    GetDocumentTest(1);
00188    
00189    // uri test
00190    GetCurrentURITest(1);
00191    GetReferringURITest(1);
00192 
00193    // session history test
00194    SetSHTest(1);
00195    GetSHTest(1);
00196 }
00197 
00198 void CNsIWebNav::LoadUriandReload(int URItotal)
00199 {
00200    int i=0, j=0;
00201    // LoadURI() & reload tests
00202 
00203    QAOutput("Run a few LoadURI() and Reload() tests.", 1);
00204 
00205    for (j=0; j < 9; j++) 
00206    {
00207           for (i=0; i < URItotal; i++)
00208           {
00209                  LoadURITest(UrlTable[i].theURI, UrlTable[j].theFlag, 2, PR_TRUE);
00210                  switch (i)
00211                  {
00212                  case 0:
00213                         ReloadTest(nsIWebNavigation::LOAD_FLAGS_NONE, 1);
00214                         break;
00215                  case 1:
00216                         ReloadTest(nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE, 1);
00217                         break;
00218                  case 2:
00219                         ReloadTest(nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY, 1);
00220                         break;
00221                  // simulate shift-reload
00222                  case 3:
00223                         ReloadTest(nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE |
00224                                             nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY, 1);
00225                         break;
00226                  case 4:
00227                         ReloadTest(nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE, 1);
00228                         break;
00229                  case 5:
00230                         ReloadTest(nsIWebNavigation::LOAD_FLAGS_NONE, 1);
00231                         break;
00232                  } 
00233           }
00234    }
00235 }
00236 
00237 // ***********************************************************************
00238 // Individual nsIWebNavigation tests
00239 
00240 
00241 void CNsIWebNav::CanGoBackTest(PRInt16 displayMode)
00242 {
00243    PRBool canGoBack = PR_FALSE;
00244    rv =  qaWebNav->GetCanGoBack(&canGoBack);
00245    RvTestResult(rv, "GetCanGoBack() attribute test", displayMode);
00246    FormatAndPrintOutput("canGoBack value = ", canGoBack, displayMode);
00247 }
00248 
00249 void CNsIWebNav::GoBackTest(PRInt16 displayMode)
00250 {
00251    rv =  qaWebNav->GoBack();
00252    RvTestResult(rv, "GoBack() test", displayMode);
00253 }
00254 
00255 void CNsIWebNav::CanGoForwardTest(PRInt16 displayMode)
00256 {
00257    PRBool canGoForward = PR_FALSE;
00258    rv =  qaWebNav->GetCanGoForward(&canGoForward);
00259    RvTestResult(rv, "GetCanGoForward() attribute test", displayMode);
00260    FormatAndPrintOutput("canGoForward value = ", canGoForward, displayMode); 
00261 }
00262 
00263 void CNsIWebNav::GoForwardTest(PRInt16 displayMode)
00264 {
00265    rv =  qaWebNav->GoForward();
00266    RvTestResult(rv, "GoForward() test", displayMode);
00267 }
00268 
00269 void CNsIWebNav::GoToIndexTest(PRInt16 displayMode)
00270 {
00271    PRInt32 theIndex = 0;
00272 
00273    rv =  qaWebNav->GotoIndex(theIndex);
00274    RvTestResult(rv, "GotoIndex() test", displayMode);
00275 }
00276 
00277 void CNsIWebNav::LoadURITest(char *theUrl, PRUint32 theFlag,
00278                                                   PRInt16 displayMode, PRBool runAllTests)
00279 {
00280    char theTotalString[500];
00281    char theFlagName[200];
00282 
00283    if (runAllTests == PR_FALSE)    // load just one url from Url dialog
00284    {
00285          CUrlDialog myDialog;
00286       if (myDialog.DoModal() == IDOK)
00287          {
00288               QAOutput("Begin Change URL test.", 1);
00289               rv = qaWebNav->LoadURI(NS_ConvertASCIItoUCS2(myDialog.m_urlfield).get(),
00290                                                         myDialog.m_flagvalue, nsnull,nsnull, nsnull);
00291 
00292            RvTestResult(rv, "rv LoadURI() test", 1);
00293               FormatAndPrintOutput("The url = ", myDialog.m_urlfield, displayMode);
00294               FormatAndPrintOutput("The flag = ", myDialog.m_flagvalue, displayMode);
00295               QAOutput("End Change URL test.", 1);
00296          }
00297          return;
00298    }
00299 
00300    switch(theFlag)
00301    {
00302    case nsIWebNavigation::LOAD_FLAGS_NONE:
00303           strcpy(theFlagName, "LOAD_FLAGS_NONE");
00304           break;
00305    case nsIWebNavigation::LOAD_FLAGS_MASK:
00306           strcpy(theFlagName, "LOAD_FLAGS_MASK");
00307           break;
00308    case nsIWebNavigation::LOAD_FLAGS_IS_REFRESH:
00309           strcpy(theFlagName, "LOAD_FLAGS_IS_REFRESH");
00310           break;
00311    case nsIWebNavigation::LOAD_FLAGS_IS_LINK:
00312           strcpy(theFlagName, "LOAD_FLAGS_IS_LINK");
00313           break;
00314    case nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY:
00315           strcpy(theFlagName, "LOAD_FLAGS_BYPASS_HISTORY");
00316           break;
00317    case nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY:
00318           strcpy(theFlagName, "LOAD_FLAGS_REPLACE_HISTORY");
00319           break;
00320    case nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE:
00321           strcpy(theFlagName, "LOAD_FLAGS_BYPASS_CACHE");
00322           break;
00323    case nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY:
00324           strcpy(theFlagName, "LOAD_FLAGS_BYPASS_PROXY");
00325           break;
00326    case nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE:
00327           strcpy(theFlagName, "LOAD_FLAGS_CHARSET_CHANGE");
00328           break;
00329    }
00330 
00331    rv = qaWebNav->LoadURI(NS_ConvertASCIItoUCS2(theUrl).get(), 
00332                           theFlag,
00333                           nsnull,
00334                           nsnull,
00335                           nsnull);
00336    sprintf(theTotalString, "%s%s%s%s%s", "LoadURI(): ", theUrl, " w/ ", theFlagName, " test");
00337    RvTestResult(rv, theTotalString, displayMode);
00338 }
00339 
00340 void CNsIWebNav::ReloadTest(PRUint32 theFlag, PRInt16 displayMode)
00341 {
00342    char theTotalString[500];
00343    char theFlagName[200];
00344 
00345   switch(theFlag)
00346   {
00347   case nsIWebNavigation::LOAD_FLAGS_NONE:
00348       strcpy(theFlagName, "LOAD_FLAGS_NONE");
00349       break;
00350   case nsIWebNavigation::LOAD_FLAGS_MASK:
00351       strcpy(theFlagName, "LOAD_FLAGS_MASK");
00352       break;
00353   case nsIWebNavigation::LOAD_FLAGS_IS_REFRESH:
00354       strcpy(theFlagName, "LOAD_FLAGS_IS_REFRESH");
00355       break;
00356   case nsIWebNavigation::LOAD_FLAGS_IS_LINK:
00357       strcpy(theFlagName, "LOAD_FLAGS_IS_LINK");
00358       break;
00359   case nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY:
00360       strcpy(theFlagName, "LOAD_FLAGS_BYPASS_HISTORY");
00361       break;
00362   case nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY:
00363       strcpy(theFlagName, "LOAD_FLAGS_REPLACE_HISTORY");
00364       break;
00365   case nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE:
00366       strcpy(theFlagName, "LOAD_FLAGS_BYPASS_CACHE");
00367       break;
00368   case nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY:
00369       strcpy(theFlagName, "LOAD_FLAGS_BYPASS_PROXY");
00370       break;
00371   case nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE:
00372       strcpy(theFlagName, "LOAD_FLAGS_CHARSET_CHANGE");
00373       break;
00374   case nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY:
00375       strcpy(theFlagName, "cache & proxy");
00376       break;
00377   } 
00378 
00379    rv =  qaWebNav->Reload(theFlag);
00380    sprintf(theTotalString, "%s%s%s%s", "Reload(): ", " w/ ", theFlagName, " test");
00381    RvTestResult(rv, theTotalString, displayMode);
00382 }
00383 
00384 void CNsIWebNav::StopURITest(char *theUrl, PRUint32 theFlag,
00385                                                   PRInt16 displayMode)
00386 {
00387    char theTotalString[200];
00388    char flagString[100];
00389 
00390    if (theFlag == nsIWebNavigation::STOP_ALL)
00391           strcpy(flagString, "STOP_ALL");
00392    else if (theFlag == nsIWebNavigation::STOP_NETWORK)
00393           strcpy(flagString, "STOP_NETWORK");
00394    else
00395           strcpy(flagString, "STOP_CONTENT");
00396 
00397    qaWebNav->LoadURI(NS_ConvertASCIItoUCS2(theUrl).get(), 
00398                      nsIWebNavigation::LOAD_FLAGS_NONE,
00399                      nsnull,
00400                      nsnull,
00401                      nsnull);
00402 
00403    rv = qaWebNav->Stop(theFlag);
00404    sprintf(theTotalString, "%s%s%s%s", "Stop(): ", theUrl, " test: ", flagString);
00405    RvTestResult(rv, theTotalString, displayMode);
00406 }
00407 
00408 void CNsIWebNav::GetDocumentTest(PRInt16 displayMode)
00409 {
00410    nsCOMPtr<nsIDOMDocument> theDocument;
00411    nsCOMPtr<nsIDOMDocumentType> theDocType;
00412    
00413    rv =  qaWebNav->GetDocument(getter_AddRefs(theDocument));
00414    if (!theDocument) {
00415          QAOutput("We didn't get the document. Test failed.", 2);
00416          return;
00417    }
00418    else
00419          RvTestResult(rv, "GetDocument() test", displayMode);
00420 
00421    rv = theDocument->GetDoctype(getter_AddRefs(theDocType));
00422    RvTestResult(rv, "nsIDOMDocument::GetDoctype() for nsIWebNav test", displayMode);
00423 }
00424 
00425 void CNsIWebNav::GetCurrentURITest(PRInt16 displayMode)
00426 {
00427    nsCOMPtr<nsIURI> theURI;
00428 
00429    rv =  qaWebNav->GetCurrentURI(getter_AddRefs(theURI));
00430    if (!theURI) {
00431       QAOutput("We didn't get the URI. Test failed.", 2);
00432          return;
00433    }
00434    else
00435          RvTestResult(rv, "GetCurrentURI() test", displayMode);
00436 
00437    nsCAutoString uriString;
00438    rv = theURI->GetSpec(uriString);
00439    RvTestResult(rv, "nsIURI::GetSpec() for nsIWebNav test", 1);
00440 
00441    FormatAndPrintOutput("the nsIWebNav uri = ", uriString, displayMode);
00442 }
00443 
00444 void CNsIWebNav::GetReferringURITest(PRInt16 displayMode)
00445 {
00446    nsCOMPtr<nsIURI> theURI;
00447    nsCAutoString uriString;
00448    CUrlDialog myDialog;
00449    if (myDialog.DoModal() == IDOK)
00450    {
00451          uriString = myDialog.m_urlfield;
00452          rv = NS_NewURI(getter_AddRefs(theURI), uriString);
00453          if (theURI)
00454                QAOutput("We GOT the URI.", 1);
00455          else
00456                QAOutput("We DIDN'T GET the URI.", 1);
00457          rv = qaWebNav->GetReferringURI(getter_AddRefs(theURI));
00458          RvTestResult(rv, "GetReferringURI() test", displayMode);
00459 //       rv = qaWebNav->LoadURI(NS_ConvertASCIItoUCS2(myDialog.m_urlfield).get(),
00460 //                                                      myDialog.m_flagvalue, theURI, nsnull, nsnull);
00461    }
00462    if (!theURI) {
00463       QAOutput("We didn't get the URI. Test failed.", 2);
00464          return;
00465    }
00466 
00467    rv = theURI->GetSpec(uriString);
00468    RvTestResult(rv, "nsIURI::GetSpec() for nsIWebNav test", 1);
00469 
00470    FormatAndPrintOutput("the nsIWebNav uri = ", uriString, displayMode);
00471 }
00472 
00473 void CNsIWebNav::GetSHTest(PRInt16 displayMode)
00474 {
00475    PRInt32 numOfElements;
00476 
00477    nsCOMPtr<nsISHistory> theSessionHistory;
00478    rv =  qaWebNav->GetSessionHistory(getter_AddRefs(theSessionHistory));
00479    if (!theSessionHistory) {
00480       QAOutput("We didn't get the session history. Test failed.", 2);
00481          return;
00482    }
00483    else
00484          RvTestResult(rv, "GetSessionHistory() test", displayMode);
00485 
00486    rv = theSessionHistory->GetCount(&numOfElements);
00487    RvTestResult(rv, "nsISHistory::GetCount() for nsIWebNav test", 1);
00488  
00489    FormatAndPrintOutput("the sHist entry count = ", numOfElements, displayMode);
00490 }
00491 
00492 void CNsIWebNav::SetSHTest(PRInt16 displayMode)
00493 {
00494    nsCOMPtr<nsISHistory> theSessionHistory, tempSHObject;
00495    // we want to save the existing session history
00496    rv =  qaWebNav->GetSessionHistory(getter_AddRefs(theSessionHistory));
00497    // this will create the test session history object
00498    tempSHObject = do_CreateInstance(NS_SHISTORY_CONTRACTID);
00499    rv = qaWebNav->SetSessionHistory(tempSHObject);
00500    RvTestResult(rv, "SetSessionHistory() test", displayMode);
00501    if (!tempSHObject)
00502       QAOutput("We didn't get the session history test object. Test failed.", 2);
00503        // we now reset the previous session history
00504    rv =  qaWebNav->SetSessionHistory(theSessionHistory);
00505 }