Back to index

lightning-sunbird  0.9+nobinonly
Tests.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "stdafx.h"
00039 
00040 #include "resource.h"
00041 #include "Cbrowse.h"
00042 #include "CBrowseDlg.h"
00043 #include "TestScriptHelper.h"
00044 
00045 #define BEGIN_TESTSET() \
00046     TestSet aTestSets[] = {
00047 #define DECL_SET(name, descr) \
00048     { _T(#name), _T(descr), sizeof(a##name)/sizeof(a##name[0]), a##name, NULL },
00049 #define DECL_SET_DYNAMIC(name, descr, pop) \
00050     { _T(#name), _T(descr), 0, NULL, pop },
00051 #define END_TESTSET() \
00052     { NULL, NULL, 0, NULL } }; \
00053     int nTestSets = sizeof(aTestSets) / sizeof(aTestSets[0]) - 1;
00054 
00056 
00057 void BrowserInfo::OutputString(const TCHAR *szMessage, ...)
00058 {
00059        TCHAR szBuffer[256];
00060 
00061        va_list cArgs;
00062        va_start(cArgs, szMessage);
00063        _vstprintf(szBuffer, szMessage, cArgs);
00064        va_end(cArgs);
00065 
00066        CString szOutput;
00067        szOutput.Format(_T("  Test: %s"), szBuffer);
00068 
00069        pBrowseDlg->OutputString(szOutput);
00070 }
00071 
00072 
00073 HRESULT BrowserInfo::GetWebBrowser(IWebBrowserApp **pWebBrowser)
00074 {
00075        if (pIUnknown == NULL)
00076        {
00077               return E_FAIL;
00078        }
00079        return pIUnknown->QueryInterface(IID_IWebBrowserApp, (void **) pWebBrowser);
00080 }
00081 
00082 
00083 HRESULT BrowserInfo::GetDocument(IHTMLDocument2 **pDocument)
00084 {
00085        CIPtr(IWebBrowserApp) cpWebBrowser;
00086        if (FAILED(GetWebBrowser(&cpWebBrowser)))
00087        {
00088               return E_FAIL;
00089        }
00090 
00091        CIPtr(IDispatch) cpDispDocument;
00092        cpWebBrowser->get_Document(&cpDispDocument);
00093        if (cpDispDocument == NULL)
00094        {
00095               return E_FAIL;
00096        }
00097 
00098        return cpDispDocument->QueryInterface(IID_IHTMLDocument2, (void **) pDocument);
00099 }
00100 
00102 
00103 struct InterfaceInfo
00104 {
00105        const IID *piid;
00106        const TCHAR *szName;
00107 };
00108 
00109 
00110 static InterfaceInfo aDocIIDs[] = 
00111 {
00112        { &IID_IOleCommandTarget,          _T("IOleCommandTarget") },
00113        { &IID_IHTMLDocument,                     _T("IHTMLDocument") },
00114        { &IID_IHTMLDocument2,                    _T("IHTMLDocument2") },
00115        { &IID_IHTMLElementCollection,     _T("IHTMLElementCollection") },
00116        { &IID_IHTMLElement,               _T("IHTMLElement") }
00117 };
00118 
00119 
00120 TestResult __cdecl tstDocument(BrowserInfo &cInfo)
00121 {
00122        CIPtr(IHTMLDocument2) cpDocElement;
00123        cInfo.GetDocument(&cpDocElement);
00124        if (cpDocElement == NULL)
00125        {
00126               cInfo.OutputString(_T("Error: No document"));
00127               return trFailed;
00128        }
00129 
00130        // Dump out all the interfaces supported by the document element
00131        for (int i = 0; i < sizeof(aDocIIDs) / sizeof(aDocIIDs[0]); i++)
00132        {
00133               IUnknown *pUnkI = NULL;
00134               if (SUCCEEDED(cpDocElement->QueryInterface(*(aDocIIDs[i].piid), (void **) &pUnkI)))
00135               {
00136                      cInfo.OutputString(_T("Info: Document supports interface %s"), aDocIIDs[i].szName);
00137                      pUnkI->Release();
00138               }
00139               else
00140               {
00141                      cInfo.OutputString(_T("Info: Document doesn't support interface %s"), aDocIIDs[i].szName);
00142               }
00143        }
00144 
00145        return trPassed;
00146 }
00147 
00148 
00149 TestResult __cdecl tstBody(BrowserInfo &cInfo)
00150 {
00151        CIPtr(IHTMLDocument2) cpDocElement;
00152        cInfo.GetDocument(&cpDocElement);
00153        if (cpDocElement == NULL)
00154        {
00155               cInfo.OutputString(_T("Error: No document"));
00156               return trFailed;
00157        }
00158 
00159     CIPtr(IHTMLElement) cpBody;
00160     cpDocElement->get_body(&cpBody);
00161     if (cpBody == NULL)
00162     {
00163         cInfo.OutputString(_T("Error: No Body"));
00164         return trFailed;
00165     }
00166 
00167        return trPassed;
00168 }
00169 
00170 TestResult __cdecl tstPutInnerHTML(BrowserInfo &cInfo)
00171 {
00172        CIPtr(IHTMLDocument2) cpDocElement;
00173        cInfo.GetDocument(&cpDocElement);
00174        if (cpDocElement == NULL)
00175        {
00176               cInfo.OutputString(_T("Error: No document"));
00177               return trFailed;
00178        }
00179 
00180     CIPtr(IHTMLElement) cpBody;
00181     cpDocElement->get_body(&cpBody);
00182     if (cpBody == NULL)
00183     {
00184         cInfo.OutputString(_T("Error: No Body"));
00185         return trFailed;
00186     }
00187 
00188     CComBSTR bstr("<p>Hello world!<p><a href=\"http://www.mozilla.org\">Click here to visit our sponsor</a>");
00189     if (FAILED(cpBody->put_innerHTML(bstr)))
00190     {
00191         cInfo.OutputString(_T("Error: put_innerHTML returned error"));
00192         return trFailed;
00193     }
00194 
00195        return trPassed;
00196 }
00197 
00198 TestResult __cdecl tstGetInnerHTML(BrowserInfo &cInfo)
00199 {
00200        CIPtr(IHTMLDocument2) cpDocElement;
00201        cInfo.GetDocument(&cpDocElement);
00202        if (cpDocElement == NULL)
00203        {
00204               cInfo.OutputString(_T("Error: No document"));
00205               return trFailed;
00206        }
00207        
00208        CIPtr(IHTMLElement) cpBody;
00209        HRESULT hr = cpDocElement->get_body( &cpBody );
00210        if (hr == S_OK)
00211        {
00212         CComBSTR html;
00213         if (FAILED(cpBody->get_innerHTML(&html)))
00214         {
00215             cInfo.OutputString(_T("Error: get_innerHTML returned error"));
00216             return trFailed;
00217         }
00218        }
00219 
00220        return trPassed;
00221 }
00222 
00223 TestResult __cdecl tstSetBgColor(BrowserInfo &cInfo)
00224 {
00225        CIPtr(IHTMLDocument2) cpDocElement;
00226        cInfo.GetDocument(&cpDocElement);
00227        if (cpDocElement == NULL)
00228        {
00229               cInfo.OutputString(_T("Error: No document"));
00230               return trFailed;
00231        }
00232 
00233     CComVariant v("red");
00234     if (FAILED(cpDocElement->put_bgColor(v)))
00235     {
00236               cInfo.OutputString(_T("Error: put_bgColor returned an error"));
00237         return trFailed;
00238     }
00239 
00240        return trPassed;
00241 }
00242 
00243 TestResult __cdecl tstCollectionEnum(BrowserInfo &cInfo)
00244 {
00245        CIPtr(IHTMLDocument2) cpDocElement;
00246        cInfo.GetDocument(&cpDocElement);
00247        if (cpDocElement == NULL)
00248        {
00249               cInfo.OutputString(_T("Error: No document"));
00250               return trFailed;
00251        }
00252        
00253        CIPtr(IHTMLElementCollection) cpColl;
00254        HRESULT hr = cpDocElement->get_all( &cpColl );
00255        if (hr == S_OK)
00256        {
00257               CComPtr<IUnknown> cpUnkEnum;
00258               cpColl->get__newEnum(&cpUnkEnum);
00259               if (cpUnkEnum == NULL)
00260               {
00261                      cInfo.OutputString(_T("Error: No collection"));
00262                      return trFailed;
00263               }
00264 
00265               CIPtr(IEnumVARIANT) cpEnumVARIANT = cpUnkEnum;
00266               if (cpEnumVARIANT)
00267               {
00268                      cInfo.OutputString(_T("Collection has IEnumVARIANT"));
00269               }
00270               CIPtr(IEnumUnknown) cpEnumUnknown = cpUnkEnum;
00271               if (cpEnumUnknown)
00272               {
00273                      cInfo.OutputString(_T("Collection has IEnumUnknown"));
00274               }
00275        }
00276        else
00277        {
00278               cInfo.OutputString(_T("Error: No collection from document"));
00279               return trFailed;
00280        }
00281 
00282        return trPassed;
00283 }
00284 
00285 
00286 void tstDrillerLevel(BrowserInfo &cInfo, IHTMLElementCollection *pCollection, int nLevel)
00287 {
00288        if (pCollection == NULL)
00289        {
00290               return;
00291        }
00292 
00293        LONG nElements = 0;;
00294        HRESULT hr = pCollection->get_length( &nElements );
00295        if (FAILED(hr))
00296        {
00297               cInfo.OutputString(_T("Error: Collection failed to return number of elements!"));
00298               return;
00299        }
00300 
00301        USES_CONVERSION;
00302        char szLevel[256];
00303        memset(szLevel, 0, sizeof(szLevel));
00304        memset(szLevel, ' ', nLevel);
00305        TCHAR *szIndent = A2T(szLevel);
00306 
00307        cInfo.OutputString(_T("%sParsing collection..."), szIndent);
00308        cInfo.OutputString(_T("%sCollection with %d elements"), szIndent, (int) nElements);
00309 
00310        for ( int i=0; i< nElements; i++ )
00311        {
00312               VARIANT varIndex;
00313               varIndex.vt = VT_UINT;
00314               varIndex.lVal = i;
00315               
00316               VARIANT var2;
00317               VariantInit( &var2 );
00318               CIPtr(IDispatch) cpDisp; 
00319 
00320               hr = pCollection->item( varIndex, var2, &cpDisp );
00321               if ( hr != S_OK )
00322               {
00323                      continue;
00324               }
00325 
00326               CIPtr(IHTMLElement) cpElem;
00327 
00328               hr = cpDisp->QueryInterface( IID_IHTMLElement, (void **)&cpElem );
00329               if ( hr == S_OK )
00330               {
00331 
00332                      BSTR bstrTagName = NULL;
00333                      hr = cpElem->get_tagName(&bstrTagName);
00334                      CString szTagName = bstrTagName;
00335                      SysFreeString(bstrTagName);
00336 
00337                      BSTR bstrID = NULL;
00338                      hr = cpElem->get_id(&bstrID);
00339                      CString szID = bstrID;
00340                      SysFreeString(bstrID);
00341 
00342                      BSTR bstrClassName = NULL;
00343                      hr = cpElem->get_className(&bstrClassName);
00344                      CString szClassName = bstrClassName;
00345                      SysFreeString(bstrClassName);
00346                      
00347                      cInfo.OutputString(_T("%sElement at %d is %s"), szIndent, i, szTagName);
00348                      cInfo.OutputString(_T("%s  id=%s"), szIndent, szID);
00349                      cInfo.OutputString(_T("%s  classname=%s"), szIndent, szClassName);
00350 
00351                      CIPtr(IHTMLImgElement) cpImgElem;
00352                      hr = cpDisp->QueryInterface( IID_IHTMLImgElement, (void **)&cpImgElem );
00353                      if ( hr == S_OK )
00354                      {
00355 //                          cpImgElem->get_href(&bstr);
00356                      }
00357                      else
00358                      {
00359                             CIPtr(IHTMLAnchorElement) cpAnchElem;
00360                             hr = cpDisp->QueryInterface( IID_IHTMLAnchorElement, (void **)&cpAnchElem );
00361                             if ( hr == S_OK )
00362                             {
00363 //                                 cpAnchElem->get_href(&bstr);
00364                             }
00365                      }
00366 
00367                      CIPtr(IDispatch) cpDispColl;
00368                      hr = cpElem->get_children(&cpDispColl);
00369                      if (hr == S_OK)
00370                      {
00371                             CIPtr(IHTMLElementCollection) cpColl = cpDispColl;
00372                             tstDrillerLevel(cInfo, cpColl, nLevel + 1);
00373                      }
00374               }
00375        }
00376 
00377        cInfo.OutputString(_T("%sEnd collection"), szIndent);
00378 }
00379 
00380 
00381 TestResult __cdecl tstDriller(BrowserInfo &cInfo)
00382 {
00383        CIPtr(IHTMLDocument2) cpDocElement;
00384        cInfo.GetDocument(&cpDocElement);
00385        if (cpDocElement == NULL)
00386        {
00387               cInfo.OutputString(_T("Error: No document"));
00388               return trFailed;
00389        }
00390        
00391        CIPtr(IHTMLElementCollection) cpColl;
00392        HRESULT hr = cpDocElement->get_all( &cpColl );
00393        if (hr == S_OK)
00394        {
00395               tstDrillerLevel(cInfo, cpColl, 0);
00396        }
00397 
00398        return trPassed;
00399 }
00400 
00401 
00402 TestResult __cdecl tstTesters(BrowserInfo &cInfo)
00403 {
00404        cInfo.OutputString("Test architecture is reasonably sane!");
00405        return trPassed;
00406 }
00407 
00408 
00409 TestResult __cdecl tstControlActive(BrowserInfo &cInfo)
00410 {
00411        CBrowserCtlSiteInstance *pControlSite = cInfo.pControlSite;
00412        if (pControlSite == NULL)
00413        {
00414               cInfo.OutputString(_T("Error: No control site"));
00415               return trFailed;
00416        }
00417 
00418        if (!pControlSite->IsInPlaceActive())
00419        {
00420               cInfo.OutputString(_T("Error: Control is not in-place active"));
00421               return trFailed;
00422        }
00423 
00424        return trPassed;
00425 }
00426 
00427 
00428 TestResult __cdecl tstIWebBrowser(BrowserInfo &cInfo)
00429 {
00430        if (cInfo.pIUnknown == NULL)
00431        {
00432               cInfo.OutputString(_T("Error: No control"));
00433               return trFailed;
00434        }
00435 
00436        CIPtr(IWebBrowser) cpIWebBrowser = cInfo.pIUnknown;
00437        if (cpIWebBrowser)
00438        {
00439               return trPassed;
00440        }
00441 
00442        cInfo.OutputString(_T("Error: No IWebBrowser"));
00443        return trFailed;
00444 }
00445 
00446 
00447 TestResult __cdecl tstIWebBrowser2(BrowserInfo &cInfo)
00448 {
00449        if (cInfo.pIUnknown == NULL)
00450        {
00451               cInfo.OutputString(_T("Error: No control"));
00452               return trFailed;
00453        }
00454        CIPtr(IWebBrowser2) cpIWebBrowser = cInfo.pIUnknown;
00455        if (cpIWebBrowser)
00456        {
00457               return trPassed;
00458        }
00459 
00460        cInfo.OutputString(_T("Error: No IWebBrowser2"));
00461        return trFailed;
00462 }
00463 
00464 
00465 TestResult __cdecl tstIWebBrowserApp(BrowserInfo &cInfo)
00466 {
00467        if (cInfo.pIUnknown == NULL)
00468        {
00469               cInfo.OutputString(_T("Error: No control"));
00470               return trFailed;
00471        }
00472 
00473        CIPtr(IWebBrowserApp) cpIWebBrowser = cInfo.pIUnknown;
00474        if (cpIWebBrowser)
00475        {
00476               return trPassed;
00477        }
00478 
00479        cInfo.OutputString(_T("Error: No IWebBrowserApp"));
00480        return trFailed;
00481 }
00482 
00483 
00484 TestResult __cdecl tstNavigate2(BrowserInfo &cInfo)
00485 {
00486        return trFailed;
00487 }
00488 
00489 
00490 TestResult __cdecl tstScriptTest(BrowserInfo &cInfo)
00491 {
00492        cInfo.nResult = trFailed;
00493 
00494        CTestScriptHelperInstance *pHelper = NULL;
00495        CTestScriptHelperInstance::CreateInstance(&pHelper);
00496        if (pHelper)
00497        {
00498               pHelper->m_pBrowserInfo = &cInfo;
00499 
00500               CActiveScriptSiteInstance *pSite = NULL;
00501               CActiveScriptSiteInstance::CreateInstance(&pSite);
00502               if (pSite)
00503               {
00504                      // TODO read from registry
00505                      CString szScript;
00506                      szScript.Format(_T("Scripts\\%s"), cInfo.pTest->szName);
00507 
00508                      pSite->AddRef();
00509                      pSite->AttachVBScript();
00510                      pSite->AddNamedObject(_T("BrowserInfo"), pHelper, TRUE);
00511                      pSite->ParseScriptFile(szScript);
00512                      pSite->PlayScript();
00513                      pSite->Detach();
00514                      pSite->Release();
00515               }
00516        }
00517 
00518        return cInfo.nResult;
00519 }
00520 
00521 
00522 Test aScripts[] =
00523 {
00524        { _T("Script test"), _T("Test that the scripting engine is sane"), tstScriptTest }
00525 };
00526 
00527 
00528 void __cdecl ScriptSetPopulator(TestSet *pTestSet)
00529 {
00530        // TODO read from registry
00531        CString szTestDir(_T("Scripts"));
00532 
00533        CStringList cStringList;
00534        CFileFind cFinder;
00535        CString szPattern;
00536 
00537 
00538        szPattern.Format(_T("%s\\*.vbs"), szTestDir);
00539        BOOL bWorking = cFinder.FindFile(szPattern);
00540        while (bWorking)
00541        {
00542               bWorking = cFinder.FindNextFile();
00543               cStringList.AddTail(cFinder.GetFileName());
00544        }
00545        
00546        szPattern.Format(_T("%s\\*.js"), szTestDir);
00547        bWorking = cFinder.FindFile(szPattern);
00548        while (bWorking)
00549        {
00550               bWorking = cFinder.FindNextFile();
00551               cStringList.AddTail(cFinder.GetFileName());
00552        }
00553 
00554        // Create a set of tests from the scripts found
00555        Test *pTests = (Test *) malloc(sizeof(Test) * cStringList.GetCount());
00556        for (int i = 0; i < cStringList.GetCount(); i++)
00557        {
00558               CString szScript = cStringList.GetAt(cStringList.FindIndex(i));
00559               _tcscpy(pTests[i].szName, szScript);
00560               _tcscpy(pTests[i].szDesc, _T("Run the specified script"));
00561               pTests[i].pfn = tstScriptTest;
00562        }
00563 
00564        pTestSet->nTests = cStringList.GetCount();
00565        pTestSet->aTests = pTests;
00566 }
00567 
00569 
00570 Test aBasic[] =
00571 {
00572        { _T("Test Tester"), _T("Ensure that the testing architecture is working"), tstTesters, trNotRun },
00573        { _T("Control basics"), _T("Ensure that the browser control is active"), tstControlActive, trNotRun },
00574        { _T("IWebBrowser"), _T("Test if control has an IWebBrowser interface"), tstIWebBrowser, trNotRun },
00575        { _T("IWebBrowser2"), _T("Test if control has an IWebBrowser2 interface"), tstIWebBrowser2, trNotRun },
00576        { _T("IWebBrowserApp"), _T("Test if control has an IWebBrowserApp interface"), tstIWebBrowserApp, trNotRun }
00577 };
00578 
00579 
00580 Test aBrowsing[] =
00581 {
00582        { _T("IWebBrowser2::Navigate2"), _T("Test if browser can navigate to the test URL"), NULL }
00583 };
00584 
00585 
00586 Test aDHTML[] =
00587 {
00588        { _T("IWebBrowser::get_Document"), _T("Test if browser has a top level element"), tstDocument },
00589     { _T("IHTMLDocument::get_Body"), _T("Test if the body element can be retrieved"), tstBody},
00590     { _T("IHTMLElement::put_innerHTML"), _T("Test if the innerHTML attribute works"), tstPutInnerHTML },
00591     { _T("IHTMLElement::get_innerHTML"), _T("Dump the innerHTML for the BODY element"), tstGetInnerHTML },
00592     { _T("IHTMLElement::put_bgColor"), _T("Set the document background color to red"), tstSetBgColor },
00593        { _T("IHTMLElementCollection::get__newEnum"), _T("Test if element collections return enumerations"), tstCollectionEnum },
00594        { _T("Parse DOM"), _T("Parse the document DOM"), tstDriller }
00595 };
00596 
00597 
00598 Test aOther[] =
00599 {
00600        { _T("Print Page"), _T("Print the test URL page"), NULL }
00601 };
00602 
00603 BEGIN_TESTSET()
00604     DECL_SET(Basic, "Basic sanity tests")
00605     DECL_SET(Browsing, "Browsing and navigation tests")
00606     DECL_SET(DHTML, "Test the DOM")
00607     DECL_SET(Other, "Other tests")
00608     DECL_SET_DYNAMIC(Scripts, "Script tests", ScriptSetPopulator)
00609 END_TESTSET()
00610