home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / TestCaseManager.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.1 KB  |  306 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. // TestCaseManager.cpp : implementation file
  19. //
  20. #include "stdafx.h"
  21. #include "resource.h"
  22. //#include "TestCaseManager.h"
  23. #include "testcase.h"
  24. #include "qa.h"
  25.  
  26. #include <stdlib.h>
  27.  
  28. #ifdef _DEBUG
  29. #define new DEBUG_NEW
  30. #undef THIS_FILE
  31. static char THIS_FILE[] = __FILE__;
  32. #endif
  33.  
  34. // To be used by Par's code
  35. CTestCase QA_TestCase;
  36.  
  37. IMPLEMENT_SERIAL(CTCData, CObject, 0)
  38. IMPLEMENT_SERIAL(CTestCaseManager, CObject, 0)
  39.  
  40.  
  41.  
  42.  
  43. CTCData::CTCData()
  44. {
  45.     m_strName = "";
  46.     m_strDesc = "";
  47.     m_nID = 0;
  48. }
  49.  
  50. void CTCData::Serialize(CArchive& ar)
  51. {
  52.     
  53.     if (ar.IsStoring())
  54.     {
  55.         ar << m_strName;
  56.         ar << m_strDesc;
  57.         ar << (WORD)m_nID;
  58.     }
  59.     else
  60.     {
  61.         ar >> m_strName;
  62.         ar >> m_strDesc;
  63.         ar >> (WORD&)m_nID;
  64.     }
  65. }
  66.  
  67.  
  68.  
  69. CTestCaseManager::CTestCaseManager()
  70. {
  71.  
  72.     m_iNumTestCases = 0; 
  73.     m_iNumTestCasesToRun = 0; 
  74.     m_sDataFile = CString("");
  75.     m_sDataPath = CString("");
  76.     m_sLogFile = CString("");
  77. }
  78.  
  79. CTestCaseManager::~CTestCaseManager()
  80. {
  81.     DeleteData();
  82. }
  83.  
  84. void CTestCaseManager::Serialize(CArchive& ar)
  85. {
  86.     CTCData* pTestCase;
  87.     int nI;
  88.  
  89.     CObject::Serialize(ar);
  90.  
  91.     if ( ar.IsStoring() )
  92.     {
  93.         ar << (WORD)m_TCData.GetSize();
  94.         for (nI = 0;nI < m_TCData.GetSize();nI++)
  95.         {
  96.             pTestCase = (CTCData*)m_TCData[nI];
  97.             ar << pTestCase;
  98.         }
  99.     }
  100.     else
  101.     {
  102.         WORD wSize;
  103.         ar >> wSize;
  104.         m_TCData.SetSize(wSize);
  105.         for (nI = 0;nI < m_TCData.GetSize();nI++)
  106.         {
  107.             ar >> pTestCase;
  108.             m_TCData.SetAt(nI,pTestCase);
  109.         }
  110.     }
  111.  
  112. }
  113.  
  114. void CTestCaseManager::AddTestCase(CTCData* pTestCase)
  115. {
  116.     CTCData *pTCData;    
  117.     int nI;
  118.     // put it in its place (acsending sorted order)
  119.  
  120.     for (nI = 0;nI < m_TCData.GetSize();nI++)
  121.     {
  122.         pTCData = (CTCData*)m_TCData[nI];
  123.         if (pTCData->m_strName.Compare(pTestCase->m_strName) > 0)
  124.             break;
  125.     }
  126.     m_TCData.InsertAt(nI,pTestCase);
  127.  
  128. }
  129.  
  130. void CTestCaseManager::InitRun()
  131. {
  132.     
  133.     CFile fFile;
  134.  
  135.     
  136.     m_iNumPassed = 0;
  137.     m_iNumFailed = 0;
  138.     m_sLogFile = (CString)(m_sDataPath + "results.all");
  139.  
  140.     // Delete the .all file
  141.     if (FileExists(m_sLogFile))
  142.         fFile.Remove(m_sLogFile);    
  143.  
  144.     fFile.Open(m_sLogFile,CFile::modeCreate | CFile::shareDenyNone);
  145.     fFile.Close();
  146.     
  147.  
  148.  
  149. }
  150.  
  151. // appends [str] to the Log File
  152. void CTestCaseManager::PrintToFile(CString& str)
  153. {
  154.     CStdioFile fFile;
  155.  
  156.     if (fFile.Open(m_sLogFile,CFile::modeWrite | CFile::shareDenyNone))
  157.     {  // write to Testcase Log File
  158.         fFile.Seek(0, CFile::end);
  159.         fFile.WriteString(str + "\n");
  160.         fFile.Flush();
  161.         fFile.Close();
  162.     }
  163. }
  164. void CTestCaseManager::RemoveTestCases(LPINT m_TestCasesToRemove, int nNum)
  165. {
  166.     
  167.     CTCData* pTCData;
  168.     
  169.         
  170.     for (int nI = nNum-1 ; nI >= 0 ; nI--)
  171.     {
  172.         pTCData = (CTCData*)m_TCData[m_TestCasesToRemove[nI]];
  173.         delete pTCData;
  174.         m_TCData.RemoveAt(m_TestCasesToRemove[nI]);
  175.     }
  176. }
  177.  
  178. void CTestCaseManager::DeleteData()
  179. {
  180.     CTCData* pTCData;
  181.     int nI;
  182.  
  183.     for (nI = 0;nI < m_TCData.GetSize();nI++)
  184.     {
  185.         pTCData = (CTCData*)m_TCData[nI];
  186.         delete pTCData;
  187.     }
  188.     m_TCData.RemoveAll();
  189. }
  190.  
  191. void CTestCaseManager::SaveData() 
  192. {
  193.     CFile file;
  194.     
  195.  
  196.     if ( m_sDataFile == "" )
  197.     {
  198.         CFileDialog dlg(FALSE, NULL, NULL, NULL, "Testcase Data Files (*.dat)|*.dat|All Files (*.*)|*.*||");
  199.         if ( dlg.DoModal() == IDOK )
  200.         {
  201.             m_sDataFile = dlg.GetPathName();
  202.         
  203.             if ( !FileExists(m_sDataFile) )
  204.                 return;
  205.         }
  206.     }
  207.     if ( file.Open(m_sDataFile,CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone) )
  208.     {
  209.             CArchive saveArchive(&file, CArchive::store | CArchive::bNoFlushOnDelete);
  210.             Serialize(saveArchive);
  211.             saveArchive.Close();
  212.             file.Close();
  213.     }
  214. }
  215.  
  216. void CTestCaseManager::LoadData() 
  217. {
  218.     CFileDialog dlg(TRUE, NULL, NULL, NULL, "Testcase Data Files (*.dat)|*.dat|All Files (*.*)|*.*||");
  219.     CString sFile;
  220.     
  221.     CFile file;
  222.  
  223.     if ( dlg.DoModal() == IDOK )
  224.     {
  225.         
  226.         m_sDataFile = dlg.GetPathName();
  227.  
  228.         
  229.         if ( !FileExists(m_sDataFile) )
  230.             return;
  231.  
  232.         // Save the path
  233.         int iSlantPos = m_sDataFile.ReverseFind('\\');
  234.         m_sDataPath = m_sDataFile.Left(iSlantPos+1);  // get the slant too!
  235.  
  236.         if ( file.Open(m_sDataFile,CFile::modeRead | CFile::shareDenyNone) )
  237.         {
  238.             DeleteData(); // delete data first
  239.  
  240.             CArchive loadArchive(&file,CArchive::load | CArchive::bNoFlushOnDelete);
  241.             Serialize(loadArchive);
  242.             loadArchive.Close();
  243.             file.Close();
  244.         }
  245.     }
  246. }
  247.  
  248.  
  249. void CTestCaseManager::ExecuteTestCases(LPINT m_TestCasesToRun)
  250. {
  251.     int index;
  252.     CTCData* pTCData;
  253.         
  254.     CTestCaseManager *tc;
  255.     tc = this;
  256.  
  257.     InitRun();
  258.     while (*m_TestCasesToRun != -1)
  259.     {
  260.         
  261.         index = *m_TestCasesToRun;
  262.         pTCData = (CTCData*)m_TCData[index];
  263.         m_strName = pTCData->m_strName;
  264.  
  265.         // Examine the following very closely - not a cast but a constructor
  266.         QA_TestCase = (CTestCase)(this); // formerly tc
  267.         switch (pTCData->m_nID)
  268.         {
  269.             case TEST1 : WhiteBox_DeleteMessage1(this);  // formerly tc
  270.                          break;
  271.             case TEST2 : TrialTestCase2(tc);
  272.                          break;
  273.         }
  274.         while (QATestCaseDone == FALSE) {
  275.             FEU_StayingAlive();
  276.         }
  277.         Wrapup();  
  278.         m_TestCasesToRun++;
  279.  
  280.     }
  281.     DeleteData();
  282.     
  283. }
  284.  
  285. void CTestCaseManager::Wrapup()
  286. {
  287.     CFile fFile;
  288.  
  289.     // create the runflag file to talk to QAP
  290.     fFile.Open(m_sDataPath + "runflag.txt",CFile::modeCreate | CFile::shareDenyNone);
  291.     fFile.Close();
  292.  
  293.     QA_TestCase.WrapUp();    
  294. }
  295.  
  296. BOOL CTestCaseManager::FileExists(CString& strPath)
  297. {
  298.    CFileStatus status;
  299.    BOOL bFileExists = TRUE;
  300.  
  301.    if ( !CFile::GetStatus(strPath,status) ) // static member
  302.      bFileExists = FALSE;
  303.  
  304.    return bFileExists;
  305. }
  306.