Back to index

lightning-sunbird  0.9+nobinonly
QaUtils.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 // These are utilities to help with QA tasks.
00043 
00044 // Includes routine to post results to a QA log file.
00045 
00046 #include "stdafx.h"
00047 #include "TestEmbed.h"
00048 #include "BrowserView.h"
00049 #include "BrowserImpl.h"
00050 #include "BrowserFrm.h"
00051 #include "QAUtils.h"
00052 
00053 
00054 #ifdef _DEBUG
00055 #define new DEBUG_NEW
00056 #undef THIS_FILE
00057 static char THIS_FILE[] = __FILE__;
00058 #endif
00059 
00060 storage getSupportObj;
00061 
00062 void RvTestResultDlg(nsresult rv, CString pLine,BOOL bClearList)
00063 {
00064        static CShowTestResults dlgResult ;
00065        if (!dlgResult)
00066               dlgResult.Create(IDD_RUNTESTSDLG);
00067 
00068        
00069        if (bClearList)
00070        {
00071               dlgResult.m_ListResults.DeleteAllItems();
00072               dlgResult.ShowWindow(true);
00073        }
00074 
00075        if (NS_FAILED(rv))
00076           dlgResult.AddItemToList(pLine,false);
00077        else
00078           dlgResult.AddItemToList(pLine,true);
00079 
00080 }
00081 
00082 void RvTestResult(nsresult rv, const char *pLine, int displayMethod)
00083 {
00084        // note: default displayMethod = 1 in .h file
00085 
00086        CString strLine = pLine;
00087        char theOutputLine[200];
00088 
00089        if (NS_FAILED(rv))
00090           strLine += " failed.";
00091        else
00092           strLine += " passed.";
00093 
00094        strcpy(theOutputLine, strLine);
00095        QAOutput(theOutputLine, displayMethod);
00096 }
00097 
00098 void WriteToOutputFile(const char *pLine) 
00099 { 
00100     CStdioFile myFile; 
00101     CFileException e; 
00102     CString strFileName = "c:\\temp\\TestOutput.txt"; 
00103 
00104     if(! myFile.Open( strFileName, CStdioFile::modeCreate | CStdioFile::modeWrite
00105                                                         | CStdioFile::modeNoTruncate, &e ) ) 
00106     { 
00107         CString failCause = "Unable to open file. Reason : "; 
00108         switch (e.m_cause) {
00109         case CFileException::none:
00110             failCause += "No error occurred.";
00111             break;
00112 
00113         case CFileException::generic:
00114             failCause += "An unspecified error occurred.";
00115             break;
00116 
00117         case CFileException::fileNotFound:
00118             failCause += "The file could not be located.";
00119             break;
00120 
00121         case CFileException::badPath:
00122             failCause += "All or part of the path is invalid.";
00123             break;
00124 
00125         case CFileException::tooManyOpenFiles:
00126             failCause += "The permitted number of open files was exceeded.";
00127             break;
00128 
00129         case CFileException::accessDenied:
00130             failCause += "The file could not be accessed.";
00131             break;
00132 
00133         case CFileException::invalidFile:
00134             failCause += "There was an attempt to use an invalid file handle.";
00135             break;
00136 
00137         case CFileException::removeCurrentDir:
00138             failCause += "The current working directory cannot be removed.";
00139             break;
00140 
00141         case CFileException::directoryFull:
00142             failCause += "There are no more directory entries.";
00143             break;
00144 
00145         case CFileException::badSeek:
00146             failCause += "There was an error trying to set the file pointer.";
00147             break;
00148 
00149         case CFileException::hardIO:
00150             failCause += "There was a hardware error.";
00151             break;
00152 
00153         case CFileException::sharingViolation:
00154             failCause += "SHARE.EXE was not loaded, or a shared region was locked.";
00155             break;
00156 
00157         case CFileException::lockViolation:
00158             failCause += "There was an attempt to lock a region that was already locked.";
00159             break;
00160 
00161         case CFileException::diskFull:
00162             failCause += "The disk is full.";
00163             break;
00164 
00165         case CFileException::endOfFile:
00166             failCause += "The end of file was reached.";
00167             break;
00168 
00169         default:
00170             failCause += "Some reason not documented in <http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cfileexception.3a3a.m_cause.asp>.";
00171             break;
00172         }
00173         AfxMessageBox(failCause);
00174     } 
00175     else 
00176     { 
00177               myFile.SeekToEnd();
00178         CString strLine = pLine; 
00179         strLine += "\r\n"; 
00180 
00181         myFile.WriteString(strLine); 
00182 
00183         myFile.Close(); 
00184     } 
00185 } 
00186 
00187 void QAOutput(const char *pLine, int displayMethod)
00188 {
00189        // note: default displayMethod = 1 in .h file
00190        // displayMethod 0 = mfc dialog; 1 = output log file; 2 = both
00191 //#if 0
00192    CString strLine = pLine;
00193 
00194    if (displayMethod == 0)
00195           AfxMessageBox(strLine);
00196    else if (displayMethod == 1)
00197           WriteToOutputFile(pLine);
00198    else 
00199    {
00200        WriteToOutputFile(pLine);
00201           AfxMessageBox(strLine);
00202    }
00203 //#endif
00204 }
00205 
00206 void FormatAndPrintOutput(const char *theInput, const char *theVar, int outputMode)
00207 {
00208        nsCString outStr;
00209        CString strMsg;
00210 
00211        outStr = theInput;
00212        outStr += theVar;
00213 
00214        strMsg = outStr.get();
00215 
00216        switch (outputMode)
00217        {
00218        case 0:
00219               AfxMessageBox(strMsg); 
00220               break;
00221        case 1:
00222               WriteToOutputFile(outStr.get());
00223               break;
00224        case 2:
00225               WriteToOutputFile(outStr.get());
00226               AfxMessageBox(strMsg); 
00227               break;
00228        }
00229 }
00230 
00231 void FormatAndPrintOutput(const char *theInput, nsCAutoString theVar, int outputMode)
00232 {
00233        nsCString outStr;
00234        CString strMsg;
00235 
00236        outStr = theInput;
00237        outStr += theVar;
00238 
00239        strMsg = outStr.get();
00240 
00241        switch (outputMode)
00242        {
00243        case 0:
00244               AfxMessageBox(strMsg); 
00245               break;
00246        case 1:
00247               WriteToOutputFile(outStr.get());
00248               break;
00249        case 2:
00250               WriteToOutputFile(outStr.get());
00251               AfxMessageBox(strMsg); 
00252               break;
00253        }
00254 }
00255 
00256 void FormatAndPrintOutput(const char *theInput, int theVar, int outputMode) 
00257 {
00258        nsCString outStr;
00259        CString strMsg;
00260 
00261        outStr = theInput;
00262        outStr.AppendInt(theVar);
00263 
00264        strMsg = outStr.get();
00265 
00266        switch (outputMode)
00267        {
00268        case 0:
00269               AfxMessageBox(strMsg); 
00270               break;
00271        case 1:
00272               WriteToOutputFile(outStr.get());
00273               break;
00274        case 2:
00275               WriteToOutputFile(outStr.get());
00276               AfxMessageBox(strMsg); 
00277               break;
00278        }
00279 }
00280 
00281 void FormatAndPrintOutput(const char *theInput, double theVar, int outputMode) 
00282 {
00283        nsCString outStr;
00284        CString strMsg;
00285 
00286        outStr = theInput;
00287        outStr.AppendFloat(theVar);
00288 
00289        strMsg = outStr.get();
00290 
00291        switch (outputMode)
00292        {
00293        case 0:
00294               AfxMessageBox(strMsg); 
00295               break;
00296        case 1:
00297               WriteToOutputFile(outStr.get());
00298               break;
00299        case 2:
00300               WriteToOutputFile(outStr.get());
00301               AfxMessageBox(strMsg); 
00302               break;
00303        }
00304 }
00305 
00306 void FormatAndPrintOutput(const char *theInput, PRUint32 theVar, int outputMode) 
00307 {
00308        nsCString outStr;
00309        CString strMsg;
00310 
00311        outStr = theInput;
00312        outStr.AppendFloat(theVar);
00313 
00314        strMsg = outStr.get();
00315 
00316        switch (outputMode)
00317        {
00318        case 0:
00319               AfxMessageBox(strMsg); 
00320               break;
00321        case 1:
00322               WriteToOutputFile(outStr.get());
00323               break;
00324        case 2:
00325               WriteToOutputFile(outStr.get());
00326               AfxMessageBox(strMsg); 
00327               break;
00328        }
00329 }
00330 
00331 // stringMsg is returned in case embeddor wishes to use it in the calling method.
00332 void RequestName(nsIRequest *request, nsCString &stringMsg, int displayMethod)
00333 {
00334        nsresult rv;
00335 
00336        if (!request) {
00337               QAOutput("ERROR. nsIRequest object is Null. RequestName test fails.", displayMethod);
00338               return;
00339        }
00340        else
00341               rv = request->GetName(stringMsg);
00342 
00343        if(NS_SUCCEEDED(rv))                                                                                                          
00344               FormatAndPrintOutput("nsIRequest: The request name = ", stringMsg.get(), displayMethod);
00345        else
00346               QAOutput("nsIRequest: We didn't get the request name.", displayMethod);
00347 }
00348 
00349 void WebProgDOMWindowTest(nsIWebProgress *progress, const char *inString,
00350                                                           int displayMethod)
00351 {
00352        nsresult rv;
00353        nsCString totalStr1, totalStr2;
00354        nsCOMPtr<nsIDOMWindow> theDOMWindow;
00355 
00356        totalStr1 = inString;
00357        totalStr1 += ": Didn't get the DOMWindow. Test failed.";
00358 
00359        totalStr2 = inString;
00360        totalStr2 += ": nsIWebProgress:DOMWindow attribute test";
00361 
00362        if (!progress) {
00363               QAOutput("ERROR. nsIWebProgress object is Null. WebProgDOMWindowTest fails.", displayMethod);
00364               return;
00365        }
00366        else
00367               rv = progress->GetDOMWindow(getter_AddRefs(theDOMWindow));
00368        if (!theDOMWindow)
00369               QAOutput(totalStr1.get(), displayMethod);
00370        else
00371               RvTestResult(rv, totalStr2.get(), displayMethod);
00372 }
00373 
00374 void WebProgIsDocLoadingTest(nsIWebProgress *progress, const char *inString,
00375                                                           int displayMethod)
00376 {
00377        nsresult rv;
00378        PRBool docLoading;
00379        nsCString totalStr;
00380 
00381        totalStr = inString;
00382        totalStr += ": nsIWebProgress:IsDocumentLoading attribute test";
00383 
00384        if (!progress) {
00385               QAOutput("ERROR. nsIWebProgress object is Null. WebProgIsDocLoadingTest fails.", displayMethod);
00386               return;
00387        }
00388        else
00389        rv = progress->GetIsLoadingDocument(&docLoading);
00390        RvTestResult(rv, totalStr.get(), displayMethod);
00391        FormatAndPrintOutput("nsIWebProgress: isDocumentLoading return value = ", docLoading, displayMethod);
00392 }
00393 
00394 nsIDOMWindow * GetTheDOMWindow(nsIWebBrowser *webBrowser)
00395 {
00396        nsCOMPtr<nsIDOMWindow> theDOMWindow;
00397 
00398     webBrowser->GetContentDOMWindow(getter_AddRefs(theDOMWindow));
00399     if (!theDOMWindow) {
00400         QAOutput("Didn't get a DOM Window.");
00401               return nsnull;
00402        }
00403        return (theDOMWindow);
00404 }
00405 
00406 nsCAutoString GetTheURI(nsIURI *theURI, int displayMethod)
00407 {
00408        nsresult rv;
00409        nsCAutoString uriString;
00410 
00411        if (!theURI) {
00412         QAOutput("nsIURI object is null. return failure.");
00413               return uriString;  //dep 3/30
00414        }
00415        rv = theURI->GetSpec(uriString);
00416     RvTestResult(rv, "nsIURI::GetSpec() test", displayMethod);
00417     FormatAndPrintOutput("the uri = ", uriString, displayMethod);
00418 
00419        return uriString;
00420 }
00421 
00422 // used for web progress listener in BrowserImplWebPrgrsLstnr.cpp
00423 void onStateChangeString(char *theStateType, char *theDocType, 
00424                                            nsCString stringMsg, PRUint32 status, int displayMode)
00425 {
00426        nsCString totalMsg;
00427 
00428        totalMsg = "OnStateChange(): ";
00429        totalMsg += theStateType;
00430        totalMsg += ", ";
00431        totalMsg += theDocType;
00432        totalMsg += ", ";
00433        totalMsg += stringMsg;
00434        totalMsg += ", status (hex) = ";
00435        totalMsg.AppendInt(status, 16);
00436        QAOutput(totalMsg.get(), displayMode);
00437 }
00438 
00439 void SaveObject(nsISupports *theSupports)
00440 {
00441        getSupportObj.sup = theSupports;
00442 }
00443 
00445 // CShowTestResults dialog
00446 
00447 
00448 CShowTestResults::CShowTestResults(CWnd* pParent /*=NULL*/)
00449        : CDialog(CShowTestResults::IDD, pParent)
00450 {
00451 
00452        //{{AFX_DATA_INIT(CShowTestResults)
00453               // NOTE: the ClassWizard will add member initialization here
00454        //}}AFX_DATA_INIT
00455 
00456 }
00457 
00458 
00459 void CShowTestResults::DoDataExchange(CDataExchange* pDX)
00460 {
00461        CDialog::DoDataExchange(pDX);
00462        //{{AFX_DATA_MAP(CShowTestResults)
00463        DDX_Control(pDX, IDC_LIST1, m_ListResults);
00464        //}}AFX_DATA_MAP
00465 }
00466 
00467 
00468 BEGIN_MESSAGE_MAP(CShowTestResults, CDialog)
00469        //{{AFX_MSG_MAP(CShowTestResults)
00470        //}}AFX_MSG_MAP
00471 END_MESSAGE_MAP()
00472 
00474 // CShowTestResults message handlers
00475 
00476 BOOL CShowTestResults::OnInitDialog() 
00477 {
00478        
00479        CDialog::OnInitDialog();
00480        
00481        m_ListResults.InsertColumn(0,"Test Case",LVCFMT_LEFT,360);
00482        m_ListResults.InsertColumn(1,"Result",LVCFMT_LEFT,100);
00483        
00484        return TRUE;  // return TRUE unless you set the focus to a control
00485                      // EXCEPTION: OCX Property Pages should return FALSE
00486 }
00487 
00488 
00489 void CShowTestResults::AddItemToList(LPCTSTR szTestCaseName, BOOL bResult) 
00490 {
00491        LV_ITEM lvitem ;
00492               
00493        lvitem.mask = LVIF_TEXT;
00494        lvitem.iItem = m_ListResults.GetItemCount();
00495        lvitem.iSubItem = 0;
00496        lvitem.pszText = (LPTSTR)szTestCaseName ;
00497        //Insert the main item
00498        m_ListResults.InsertItem(&lvitem);
00499 
00500        if (bResult)
00501        m_ListResults.SetItemText(m_ListResults.GetItemCount()-1,1,"Passed");
00502        else
00503        m_ListResults.SetItemText(m_ListResults.GetItemCount()-1,1,"Failed");
00504 
00505        //Insert the sub item
00506        //m_ListResults.InsertItem(&lvitem);
00507 
00508 }