Back to index

lightning-sunbird  0.9+nobinonly
nsiHistory.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 nsiHistory 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 "nsihistory.h"
00053 #include <stdio.h>
00054 
00055 
00057 // CNsIHistory
00058 
00059 CNsIHistory::CNsIHistory(nsIWebNavigation *mWebNav)
00060 {
00061        //qaTests = mTests;
00062        qaWebNav = mWebNav ;
00063 }
00064 
00065 
00066 CNsIHistory::~CNsIHistory()
00067 {
00068 }
00069 
00070 
00071 
00073 // CNsIHistory message handlers
00074 // ***********************************************************************
00075 // ***********************************************************************
00076 //  nsIHistory iface
00077 
00078 // ***********************************************************************
00079 // ***********************************************************************
00080 // nsISHistory & nsIHistoryEntry ifaces:
00081 
00082 void CNsIHistory::OnStartTests(UINT nMenuID)
00083 {
00084 
00085        // Calls  all or indivdual test cases on the basis of the 
00086        // option selected from menu.
00087    PRInt32 numEntries = 5;
00088    PRInt32 theIndex;
00089    PRInt32 theMaxLength = 100;
00090 
00091    CString shString;
00092 
00093    nsCOMPtr<nsISHistory> theSessionHistory;
00094    nsCOMPtr<nsIHistoryEntry> theHistoryEntry;
00095    nsCOMPtr<nsISimpleEnumerator> theSimpleEnum;
00096 
00097 
00098    //nsCOMPtr<nsIURI> theURI;
00099    // do_QueryInterface
00100    // NS_HISTORYENTRY_CONTRACTID
00101    // NS_SHISTORYLISTENER_CONTRACTID
00102 
00103        // get Session History through web nav iface
00104    if (qaWebNav) {
00105          rv = qaWebNav->GetSessionHistory(getter_AddRefs(theSessionHistory));
00106          RvTestResult(rv, "GetSessionHistory() object", 1);
00107          RvTestResultDlg(rv, "GetSessionHistory() object", true);
00108    }
00109 
00110    if (!theSessionHistory)
00111    {
00112           QAOutput("theSessionHistory object wasn't created. No session history tests performed.", 2);
00113           return;
00114    }
00115    else {
00116           QAOutput("theSessionHistory object was created.", 1);
00117           theSessionHistory->GetCount(&numEntries);
00118    }
00119 
00120        theSessionHistory->GetEntryAtIndex(0, PR_FALSE, getter_AddRefs(theHistoryEntry));
00121        if (!theHistoryEntry)
00122               QAOutput("We didn't get the History Entry object.", 1);
00123        else 
00124               QAOutput("We have the History Entry object!", 1);       
00125 
00126 
00127 
00128        switch(nMenuID)
00129        {
00130               case ID_INTERFACES_NSISHISTORY_RUNALLTESTS :
00131                      QAOutput("Begin nsISHistory tests.", 1);
00132                      RunAllTests();
00133                      QAOutput("End nsISHistory tests.", 1);
00134                      break ;
00135               case ID_INTERFACES_NSISHISTORY_GETCOUNT :
00136                      GetCountTest(theSessionHistory, &numEntries, 2);
00137                      break ;
00138               case ID_INTERFACES_NSISHISTORY_GETINDEX :
00139                      GetIndexTest(theSessionHistory, &theIndex, 2);
00140                      break ;
00141               case ID_INTERFACES_NSISHISTORY_GETMAXLENGTH :
00142                      GetMaxLengthTest(theSessionHistory, &theMaxLength, 2);
00143                      break ;
00144               case ID_INTERFACES_NSISHISTORY_SETMAXLENGTH :
00145                      SetMaxLengthTest(theSessionHistory, theMaxLength, 2);
00146                      break ;
00147               case ID_INTERFACES_NSISHISTORY_GETENTRYATINDEX :
00148                      //GetEntryAtIndex(0, PR_FALSE, getter_AddRefs(theHistoryEntry));
00149                      for (theIndex = 0; theIndex < numEntries; theIndex++)
00150                      { 
00151                             FormatAndPrintOutput("the index = ", theIndex, 1); 
00152 
00153                             rv = theSessionHistory->GetEntryAtIndex(theIndex, PR_FALSE, getter_AddRefs(theHistoryEntry));
00154                             RvTestResult(rv, "GetEntryAtIndex() test", 1);
00155                             RvTestResultDlg(rv, "GetEntryAtIndex() test");
00156                             if (!theHistoryEntry)
00157                             {
00158                                    QAOutput("We didn't get the History Entry object. No more tests performed.", 1);
00159                                    return;
00160                             }
00161                             GetURIHistTest(theHistoryEntry, 2);
00162                             GetTitleHistTest(theHistoryEntry, 2);
00163                             GetIsSubFrameTest(theHistoryEntry, 2);
00164                      }      // end for loop
00165 
00166                      break ;
00167               case ID_INTERFACES_NSISHISTORY_PURGEHISTORY :
00168                      PurgeHistoryTest(theSessionHistory, numEntries, 2);
00169                      break ;
00170               case ID_INTERFACES_NSISHISTORY_GETSHISTORYENUMERATOR :
00171                      //GetSHistoryEnumerator(getter_AddRefs(theSimpleEnum));
00172 
00173                      rv = theSessionHistory->GetSHistoryEnumerator(getter_AddRefs(theSimpleEnum));
00174                      RvTestResult(rv, "GetSHistoryEnumerator() (SHistoryEnumerator attribute) test", 1);
00175                      RvTestResultDlg(rv, "GetSHistoryEnumerator() test");
00176 
00177                      if (!theSimpleEnum) {
00178                         QAOutput("theSimpleEnum for GetSHistoryEnumerator() invalid. Test failed.", 1);
00179                         return;
00180                      }
00181                      SimpleEnumTest(theSimpleEnum, 2);
00182                      break ;
00183 
00184               case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_RUNALLTESTS :
00185                      RunAllHistoryEntryTests(2);
00186                      break ;
00187               case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_GETURI :
00188                      GetURIHistTest(theHistoryEntry, 2);
00189                      break ;
00190               case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_GETTITLE :
00191                      GetTitleHistTest(theHistoryEntry, 2);
00192                      break ;
00193               case ID_INTERFACES_NSISHISTORY_NSIHISTORYENTRY_GETISSUBFRAME :
00194                      GetIsSubFrameTest(theHistoryEntry, 2);
00195                      break ;
00196        }
00197 
00198 }
00199 
00200 void CNsIHistory::RunAllTests() 
00201 {
00202    //nsresult rv;  
00203 
00204    PRInt32 numEntries = 5;
00205    PRInt32 theIndex;
00206    PRInt32 theMaxLength = 100;
00207 
00208    CString shString;
00209 
00210    nsCOMPtr<nsISHistory> theSessionHistory;
00211    nsCOMPtr<nsIHistoryEntry> theHistoryEntry;
00212 
00213        // get Session History through web nav iface
00214    if (qaWebNav)
00215               qaWebNav->GetSessionHistory( getter_AddRefs(theSessionHistory));
00216 
00217    if (!theSessionHistory)
00218    {
00219           QAOutput("theSessionHistory object wasn't created. No session history tests performed.", 2);
00220           return;
00221    }
00222    else {
00223           QAOutput("theSessionHistory object was created.", 1);
00224           theSessionHistory->GetCount(&numEntries);
00225    }
00226 
00227               // test count attribute in nsISHistory.idl
00228    GetCountTest(theSessionHistory, &numEntries, 1);
00229 
00230               // test index attribute in nsISHistory.idl
00231    GetIndexTest(theSessionHistory, &theIndex, 1);
00232 
00233               // test maxLength attribute in nsISHistory.idl
00234    SetMaxLengthTest(theSessionHistory, theMaxLength, 1);
00235    GetMaxLengthTest(theSessionHistory, &theMaxLength, 1);
00236 
00237        QAOutput("Start nsiHistoryEntry tests.", 1); 
00238 
00239               // get theHistoryEntry object
00240        rv = theSessionHistory->GetEntryAtIndex(0, PR_FALSE, getter_AddRefs(theHistoryEntry));
00241        RvTestResult(rv, "GetEntryAtIndex() test", 1);
00242        RvTestResultDlg(rv, "GetEntryAtIndex() test");
00243 
00244        if (!theHistoryEntry)
00245               QAOutput("We didn't get the History Entry object.", 1);
00246        else 
00247        {
00248               QAOutput("We have the History Entry object!", 1);       
00249 
00250                          // getEntryAtIndex() tests
00251               if (theMaxLength < numEntries)
00252               {
00253                      QAOutput("Setting number of entries to maximum length!", 1);
00254                      numEntries = theMaxLength;
00255               }
00256 
00257               for (theIndex = 0; theIndex < numEntries; theIndex++)
00258               { 
00259                      FormatAndPrintOutput("the index = ", theIndex, 1); 
00260 
00261                      //GetEntryAtIndexTest(theSessionHistory,theHistoryEntry, theIndex);
00262 
00263                      theSessionHistory->GetEntryAtIndex(theIndex, PR_FALSE, getter_AddRefs(theHistoryEntry));
00264                      if (!theHistoryEntry)
00265                      {
00266                             QAOutput("We didn't get the History Entry object. No more tests performed.", 1);
00267                             return;
00268                      }
00269                      // nsiHistoryEntry.idl tests       
00270 
00271                      // test URI attribute in nsIHistoryEntry.idl
00272                      GetURIHistTest(theHistoryEntry, 1);
00273 
00274                      // test title attribute in nsIHistoryEntry.idl
00275                      GetTitleHistTest(theHistoryEntry, 1);
00276 
00277                      // test isSubFrame attribute in nsIHistoryEntry.idl
00278                      GetIsSubFrameTest(theHistoryEntry, 1);
00279 
00280               }      // end for loop
00281        }             // end outer else
00282 
00283 
00284        // test SHistoryEnumerator attribute in nsISHistory.idl
00285        nsCOMPtr<nsISimpleEnumerator> theSimpleEnum;
00286 
00287 //     GetSHEnumTest(theSessionHistory, theSimpleEnum);
00288 
00289        rv = theSessionHistory->GetSHistoryEnumerator(getter_AddRefs(theSimpleEnum));
00290        RvTestResult(rv, "GetSHistoryEnumerator() (SHistoryEnumerator attribute) test", 1);
00291        RvTestResultDlg(rv, "GetSHistoryEnumerator() (SHistoryEnumerator attribute) test");
00292 
00293        if (!theSimpleEnum)
00294           QAOutput("theSimpleEnum for GetSHistoryEnumerator() invalid. Test failed.", 1);
00295        else
00296               SimpleEnumTest(theSimpleEnum, 1);
00297 
00298        // PurgeHistory() test
00299 
00300        PurgeHistoryTest(theSessionHistory, numEntries, 1);
00301 }
00302 
00303 // ***********************************************************************
00304 // Individual nsISHistory tests
00305 
00306 void CNsIHistory::GetCountTest(nsISHistory *theSessionHistory, PRInt32 *numEntries,
00307                                                     PRInt16 displayMode)
00308 {
00309     rv = theSessionHistory->GetCount(numEntries);
00310        if (*numEntries < 0) 
00311               QAOutput("numEntries for GetCount() < 0. Test failed.", 1);
00312        else {
00313               FormatAndPrintOutput("GetCount():number of entries = ", *numEntries, displayMode);
00314               RvTestResult(rv, "GetCount() (count attribute) test", 1);
00315               RvTestResultDlg(rv, "GetCount() (count attribute) test");
00316        }
00317 }
00318 
00319 void CNsIHistory::GetIndexTest(nsISHistory *theSessionHistory, PRInt32 *theIndex,
00320                                                     PRInt16 displayMode)
00321 {
00322        rv = theSessionHistory->GetIndex(theIndex);
00323        if (*theIndex <0) 
00324               QAOutput("theIndex for GetIndex() < 0. Test failed.", 1);
00325        else {
00326               FormatAndPrintOutput("GetIndex():the index = ", *theIndex, displayMode);
00327               RvTestResult(rv, "GetIndex() (index attribute) test", 1);
00328               RvTestResultDlg(rv, "GetIndex() (index attribute) test");
00329        }
00330 }
00331 
00332 void CNsIHistory::SetMaxLengthTest(nsISHistory *theSessionHistory, PRInt32 theMaxLength,
00333                                                            PRInt16 displayMode)
00334 {
00335        rv = theSessionHistory->SetMaxLength(theMaxLength);
00336        if (theMaxLength < 0)
00337               QAOutput("theMaxLength for SetMaxLength() < 0. Test failed.", 1);
00338        else {
00339               RvTestResult(rv, "SetMaxLength() (MaxLength attribute) test", displayMode);
00340               RvTestResultDlg(rv, "SetMaxLength() (MaxLength attribute) test");
00341        }
00342 }
00343 
00344 void CNsIHistory::GetMaxLengthTest(nsISHistory *theSessionHistory, PRInt32 *theMaxLength,
00345                                                            PRInt16 displayMode)
00346 {
00347        rv = theSessionHistory->GetMaxLength(theMaxLength);
00348        if (*theMaxLength<0)
00349               QAOutput("theMaxLength for GetMaxLength() < 0. Test failed.", 1);
00350        else {
00351               FormatAndPrintOutput("GetMaxLength():theMaxLength = ", *theMaxLength, displayMode); 
00352               RvTestResult(rv, "GetMaxLength() (MaxLength attribute) test", 1);
00353               RvTestResultDlg(rv, "GetMaxLength() (MaxLength attribute) test");
00354        }
00355 }
00356 
00357 
00358 /*void CNsIHistory::GetEntryAtIndexTest(nsISHistory *theSessionHistory, 
00359                                                          nsIHistoryEntry *theHistoryEntry, 
00360                                                          PRInt32 theIndex)
00361 {
00362        theSessionHistory->GetEntryAtIndex(theIndex, PR_FALSE,&theHistoryEntry);
00363 
00364        // test URI attribute in nsIHistoryEntry.idl
00365        //GetURIHistTest(theHistoryEntry);
00366 
00367        // test title attribute in nsIHistoryEntry.idl
00368        //GetTitleHistTest(theHistoryEntry);
00369 
00370        // test isSubFrame attribute in nsIHistoryEntry.idl
00371        //GetIsSubFrameTest(theHistoryEntry);
00372 
00373 }*/
00374 
00375 
00376 void CNsIHistory::GetURIHistTest(nsIHistoryEntry* theHistoryEntry,
00377                                                          PRInt16 displayMode)
00378 {
00379        rv = theHistoryEntry->GetURI(getter_AddRefs(theURI));
00380        RvTestResult(rv, "GetURI() (URI attribute) test", 1);
00381        RvTestResultDlg(rv, "GetURI() (URI attribute) test");
00382        if (!theURI)
00383               QAOutput("theURI for GetURI() invalid. Test failed.", 1);
00384        else
00385        {
00386               nsCAutoString uriString;
00387               rv = theURI->GetSpec(uriString);
00388               if (NS_FAILED(rv))
00389                      QAOutput("We didn't get the uriString.", 1);
00390               else
00391                      FormatAndPrintOutput("The SH Url = ", uriString, displayMode);
00392        }
00393 }
00394 
00395 void CNsIHistory::GetTitleHistTest(nsIHistoryEntry* theHistoryEntry,
00396                                                            PRInt16 displayMode)
00397 {
00398    nsXPIDLString theTitle;
00399 
00400        rv = theHistoryEntry->GetTitle(getter_Copies(theTitle));
00401        RvTestResult(rv, "GetTitle() (title attribute) test", 1);
00402        RvTestResultDlg(rv, "GetTitle() (title attribute) test");
00403        if (!theTitle) {
00404               QAOutput("theTitle for GetTitle() is blank. Test failed.", 1);
00405               return;
00406        }
00407 
00408        FormatAndPrintOutput("The title = ",
00409                         NS_ConvertUCS2toUTF8(theTitle).get(), displayMode);
00410 }
00411 
00412 void CNsIHistory::GetIsSubFrameTest(nsIHistoryEntry* theHistoryEntry,
00413                                                                PRInt16 displayMode)
00414 {
00415        PRBool isSubFrame;
00416 
00417        rv = theHistoryEntry->GetIsSubFrame(&isSubFrame);
00418        
00419        RvTestResult(rv, "GetIsSubFrame() (isSubFrame attribute) test", 1);
00420        RvTestResultDlg(rv, "GetIsSubFrame() (isSubFrame attribute) test");
00421        FormatAndPrintOutput("The subFrame boolean value = ", isSubFrame, displayMode);
00422 }
00423 
00424 /*
00425 void CNsIHistory::GetSHEnumTest(nsISHistory *theSessionHistory,
00426                                              nsISimpleEnumerator *theSimpleEnum)
00427 {
00428        rv = theSessionHistory->GetSHistoryEnumerator(getter_AddRefs(theSimpleEnum));
00429        if (!theSimpleEnum)
00430           QAOutput("theSimpleEnum for GetSHistoryEnumerator() invalid. Test failed.", 1);
00431        else
00432           RvTestResult(rv, "GetSHistoryEnumerator() (SHistoryEnumerator attribute) test", 2);
00433 }
00434 */
00435 
00436 void CNsIHistory::SimpleEnumTest(nsISimpleEnumerator *theSimpleEnum,
00437                                                          PRInt16 displayMode)
00438 {
00439   PRBool bMore = PR_FALSE;
00440   nsCOMPtr<nsISupports> nextObj;
00441   nsCOMPtr<nsIHistoryEntry> nextHistoryEntry;
00442 
00443   while (NS_SUCCEEDED(theSimpleEnum->HasMoreElements(&bMore)) && bMore)
00444   {
00445         theSimpleEnum->GetNext(getter_AddRefs(nextObj));
00446         if (!nextObj)
00447               continue;
00448         nextHistoryEntry = do_QueryInterface(nextObj);
00449         if (!nextHistoryEntry)
00450               continue;
00451         rv = nextHistoryEntry->GetURI(getter_AddRefs(theURI));
00452         RvTestResult(rv, "theSimpleEnum nsIHistoryEntry->GetURI() test", 1);
00453         RvTestResultDlg(rv, "theSimpleEnum nsIHistoryEntry->GetURI() test");
00454         nsCAutoString uriString;
00455         rv = theURI->GetSpec(uriString);
00456         if (NS_FAILED(rv))
00457               QAOutput("uriString for GetSpec() invalid. Test failed.", 1);
00458         else
00459               FormatAndPrintOutput("The SimpleEnum URL = ", uriString, displayMode);               
00460   } 
00461 }
00462 
00463 void CNsIHistory::PurgeHistoryTest(nsISHistory* theSessionHistory, PRInt32 numEntries,
00464                                                            PRInt16 displayMode)
00465 {
00466    rv = theSessionHistory->PurgeHistory(numEntries);
00467    RvTestResult(rv, "PurgeHistory() test", 1);
00468    RvTestResultDlg(rv, "PurgeHistory() test");
00469    FormatAndPrintOutput("PurgeHistory(): num requested entries for removal = ", numEntries, 1);           
00470 }
00471 
00472 void CNsIHistory::RunAllHistoryEntryTests(PRInt16 displayMode) 
00473 {
00474    PRInt32 numEntries = 5;
00475    PRInt32 theIndex;
00476    PRInt32 theMaxLength = 100;
00477 
00478    CString shString;
00479 
00480    nsCOMPtr<nsISHistory> theSessionHistory;
00481    nsCOMPtr<nsIHistoryEntry> theHistoryEntry;
00482 
00483        // get Session History through web nav iface
00484    if (qaWebNav)
00485               qaWebNav->GetSessionHistory( getter_AddRefs(theSessionHistory));
00486 
00487    if (!theSessionHistory)
00488    {
00489           QAOutput("theSessionHistory object wasn't created. No session history tests performed.", displayMode);
00490           return;
00491    }
00492    else
00493           QAOutput("theSessionHistory object was created.", 1);
00494 
00495        theSessionHistory->GetEntryAtIndex(0, PR_FALSE, getter_AddRefs(theHistoryEntry));
00496        if (!theHistoryEntry)
00497               QAOutput("We didn't get the History Entry object.", 1);
00498        else 
00499        {
00500               QAOutput("We have the History Entry object!", 1);       
00501 
00502                          // getEntryAtIndex() tests
00503               if (theMaxLength < numEntries)
00504               {
00505                      QAOutput("Setting number of entries to maximum length!", 1);
00506                      numEntries = theMaxLength;
00507               }
00508 
00509               for (theIndex = 0; theIndex < numEntries; theIndex++)
00510               { 
00511                      FormatAndPrintOutput("the index = ", theIndex, displayMode); 
00512 
00513                      //GetEntryAtIndexTest(theSessionHistory,theHistoryEntry, theIndex);
00514 
00515                      theSessionHistory->GetEntryAtIndex(theIndex, PR_FALSE, getter_AddRefs(theHistoryEntry));
00516                      if (!theHistoryEntry)
00517                      {
00518                             QAOutput("We didn't get the History Entry object. No more tests performed.", 1);
00519                             return;
00520                      }
00521                      // nsiHistoryEntry.idl tests       
00522 
00523                      // test URI attribute in nsIHistoryEntry.idl
00524                      GetURIHistTest(theHistoryEntry, displayMode);
00525 
00526                      // test title attribute in nsIHistoryEntry.idl
00527                      GetTitleHistTest(theHistoryEntry, displayMode);
00528 
00529                      // test isSubFrame attribute in nsIHistoryEntry.idl
00530                      GetIsSubFrameTest(theHistoryEntry, displayMode);
00531 
00532               }      // end for loop
00533        }             // end outer else
00534 }