home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / TEMPLATE / AUTOTEST.C < prev    next >
Text File  |  1993-12-01  |  15KB  |  446 lines

  1. /***************************************************************************
  2.  
  3.     PROGRAM: Autotest.c
  4.  
  5.     PURPOSE: Autotest is a capture/playback tool, implemented as a simple
  6.          Windows application.  The user records a sequence of keystrokes
  7.          or mouse messages by picking the Start Record menu option.
  8.          Autotest calls the Tester routine, in the TESTER.DLL to begin
  9.          recording.  Tester installs a WH_JOURNALRECORD hook and keyboard
  10.          and mouse events are saved in global memory.  Autotest calls the
  11.          Tester routine again, when the user picks the Start Playback
  12.          menu option.  Tester installs a WH_JOURNALPLAYBACK hook to
  13.          playback the previously recorded events.
  14.  
  15. ******************************************************************************/
  16.  
  17. #include "windows.h"
  18. #include "autotest.h"
  19. #include "tester.h"
  20.  
  21. HANDLE hInst;
  22. HWND   hWindow;
  23. HCURSOR hCursor;
  24.  
  25. static  char szFileName[128];
  26. char str[255];
  27.  
  28. HANDLE hHourGlass;                        /* handle to hourglass cursor    */
  29. HANDLE hSaveCursor;                       /* current cursor handle         */
  30. int hFile;                                /* file handle                   */
  31. OFSTRUCT OfStruct;                        /* information from OpenFile()   */
  32.  
  33. RECT Rect;                                /* dimension of the client window */
  34. HWND hWnd;
  35.  
  36. static PGMSTATUS       Autotest_status;
  37. static TESTRESULT      AutotestResult;
  38.  
  39. static BOOL            TraceOnFlag = FALSE;
  40. static GLOBALHANDLE    hMemTestEvents;
  41.  
  42. /****************************************************************************
  43.  
  44.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  45.  
  46.     PURPOSE: calls initialization function, processes message loop
  47.  
  48. ****************************************************************************/
  49.  
  50. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine,
  51.                    int nCmdShow)
  52. {
  53.     MSG msg;
  54.  
  55.     if (!hPrevInstance)
  56.        if (!InitApplication(hInstance))
  57.           return (FALSE);
  58.  
  59.     if (!InitInstance(hInstance, nCmdShow))
  60.        return (FALSE);
  61.  
  62.     while (GetMessage(&msg, NULL, NULL, NULL))
  63.        {
  64.        TranslateMessage(&msg);
  65.        DispatchMessage(&msg);
  66.        }
  67.     return (msg.wParam);
  68. }
  69.  
  70.  
  71. /****************************************************************************
  72.  
  73.     FUNCTION: InitApplication(HANDLE)
  74.  
  75.     PURPOSE: Initializes window data and registers window class
  76.  
  77. ****************************************************************************/
  78.  
  79. BOOL InitApplication(HANDLE hInstance)
  80. {
  81.     WNDCLASS  wc;
  82.  
  83.     wc.style         = CS_DBLCLKS;          /* double-click messages */
  84.     wc.lpfnWndProc   = MainWndProc;
  85.     wc.cbClsExtra    = 0;
  86.     wc.cbWndExtra    = 0;
  87.     wc.hInstance     = hInstance;
  88.     wc.hIcon         = LoadIcon(hInstance, "AutotestIcon");
  89.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  90.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  91.     wc.lpszMenuName  = "AutotestMenu";
  92.     wc.lpszClassName = "AutotestWClass";
  93.  
  94.     return (RegisterClass(&wc));
  95. }
  96.  
  97.  
  98. /****************************************************************************
  99.  
  100.     FUNCTION:  InitInstance(HANDLE, int)
  101.  
  102.     PURPOSE:  Saves instance handle and creates main window
  103.  
  104. ****************************************************************************/
  105.  
  106. BOOL InitInstance(HANDLE hInstance, int nCmdShow)
  107. {
  108.     HWND            hWnd;
  109.  
  110.  
  111.     hInst = hInstance;
  112.  
  113.  
  114.     hWnd = CreateWindow(
  115.               "AutotestWClass",
  116.               "AUTOTEST",
  117.               WS_OVERLAPPEDWINDOW,  /* horz & vert scroll bars */
  118.               (GetSystemMetrics(SM_CXSCREEN)/2),
  119.               0,
  120.               (GetSystemMetrics(SM_CXSCREEN)/2),
  121.               (GetSystemMetrics(SM_CYCAPTION)*3),
  122.               NULL,
  123.               NULL,
  124.               hInstance,
  125.               NULL
  126.               );
  127.  
  128.     if (!hWnd)
  129.        return (FALSE);
  130.  
  131.     szFileName[0] = '\0';
  132.  
  133.     ShowWindow(hWnd, nCmdShow);
  134.     UpdateWindow(hWnd);
  135.     return (TRUE);
  136.  
  137. }
  138.  
  139.  
  140. /****************************************************************************
  141.  
  142.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  143.  
  144.     PURPOSE:  Processes messages
  145.  
  146.     MESSAGES:
  147.  
  148.         WM_COMMAND  - application menu (About dialog box)
  149.         IDM_TRACEON - turns on trace mode, which causes the Tester
  150.                       routine to write out recorded event messages,
  151.                       and played-back event messages to trace files.
  152.        IDM_TRACEOFF - turns off trace mode.  No trace data is saved.
  153.         IDM_RECORD  - turns on record mode, causes the Tester routine
  154.                       to be invoked.  Tester installs a WH_JOURNALRECORD
  155.                       hook and records user event messages.
  156.         IDM_STOPREC - calls the Tester routine to turn off record mode
  157.                       and to de-install the WH_JOURNALRECORD hook.
  158.         IDM_REPLAY  - turns on playback mode, the Tester routine is
  159.                       invoked.  Tester installs a WH_JOURNALPLAYBACK hook
  160.                       which plays back previously recorded messages.
  161.  
  162.         WM_DESTROY  - destroy window
  163.  
  164.  
  165. ****************************************************************************/
  166.  
  167. long FAR PASCAL MainWndProc(HWND hWnd, unsigned message, WORD wParam,
  168.                             LONG lParam)
  169. {
  170.     FARPROC lpProcAbout;
  171.  
  172.     BOOL    RecordingFlag = FALSE;
  173.     BOOL    ReplayingFlag = FALSE;
  174.  
  175.  
  176.     switch (message)
  177.        {
  178.        case WM_INITMENU:
  179.           switch (Autotest_status)
  180.              {
  181.              case NOT_STARTED_RECORDING:
  182.                 EnableMenuItem(GetMenu(hWnd), IDM_RECORD,
  183.                                MF_BYCOMMAND | MF_ENABLED);
  184.                 EnableMenuItem(GetMenu(hWnd), IDM_STOPREC,
  185.                                MF_BYCOMMAND | MF_GRAYED);
  186.                 EnableMenuItem(GetMenu(hWnd), IDM_REPLAY,
  187.                                MF_BYCOMMAND | MF_GRAYED);
  188.                 break;
  189.  
  190.              case RECORDING_ON:
  191.                 EnableMenuItem(GetMenu(hWnd), IDM_RECORD,
  192.                                MF_BYCOMMAND | MF_GRAYED);
  193.                 EnableMenuItem(GetMenu(hWnd), IDM_STOPREC,
  194.                                MF_BYCOMMAND | MF_ENABLED);
  195.                 EnableMenuItem(GetMenu(hWnd), IDM_REPLAY,
  196.                                MF_BYCOMMAND | MF_GRAYED);
  197.                 break;
  198.  
  199.              case RECORDING_COMPLETED:
  200.                 EnableMenuItem(GetMenu(hWnd), IDM_RECORD,
  201.                                MF_BYCOMMAND | MF_GRAYED);
  202.                 EnableMenuItem(GetMenu(hWnd), IDM_STOPREC,
  203.                                MF_BYCOMMAND | MF_GRAYED);
  204.                 EnableMenuItem(GetMenu(hWnd), IDM_REPLAY,
  205.                                MF_BYCOMMAND | MF_ENABLED);
  206.                 break;
  207.  
  208.              case REPLAYING_ON:
  209.                 EnableMenuItem(GetMenu(hWnd), IDM_RECORD,
  210.                                MF_BYCOMMAND | MF_GRAYED);
  211.                 EnableMenuItem(GetMenu(hWnd), IDM_STOPREC,
  212.                                MF_BYCOMMAND | MF_GRAYED);
  213.                 EnableMenuItem(GetMenu(hWnd), IDM_REPLAY,
  214.                                MF_BYCOMMAND | MF_GRAYED);
  215.                 break;
  216.  
  217.              case REPLAYING_COMPLETED:
  218.                 EnableMenuItem(GetMenu(hWnd), IDM_RECORD,
  219.                                MF_BYCOMMAND | MF_ENABLED);
  220.                 EnableMenuItem(GetMenu(hWnd), IDM_STOPREC,
  221.                                MF_BYCOMMAND | MF_GRAYED);
  222.                 EnableMenuItem(GetMenu(hWnd), IDM_REPLAY,
  223.                                MF_BYCOMMAND | MF_ENABLED);
  224.                 break;
  225.              }
  226.  
  227.           if (!TraceOnFlag)
  228.              {
  229.              EnableMenuItem(GetMenu(hWnd), IDM_TRACEON,
  230.                             MF_BYCOMMAND | MF_ENABLED);
  231.              EnableMenuItem(GetMenu(hWnd), IDM_TRACEOFF,
  232.                             MF_BYCOMMAND | MF_GRAYED);
  233.              }
  234.           else
  235.              {
  236.              EnableMenuItem(GetMenu(hWnd), IDM_TRACEOFF,
  237.                             MF_BYCOMMAND | MF_ENABLED);
  238.              EnableMenuItem(GetMenu(hWnd), IDM_TRACEON,
  239.                             MF_BYCOMMAND | MF_GRAYED);
  240.              }
  241.           break;
  242.  
  243.        case WM_COMMAND:
  244.           hWindow = hWnd;
  245.           switch (wParam)
  246.              {
  247.              case IDM_TRACEON:
  248.                 GetProfileString("autotest","RecordTracefile","rectrace.txt",
  249.                                  szFileName, FILENAMELEN);
  250.                 if (-1 == (hFile = OpenFile(szFileName, (LPOFSTRUCT) &OfStruct,
  251.                                            OF_CREATE | OF_PROMPT | OF_CANCEL)))
  252.                    {
  253.                    TraceOnFlag = FALSE;
  254.                    wsprintf(str, "Cannot open trace file = %s", szFileName);
  255.                    MessageBox(hWnd, str, "ERROR", MB_APPLMODAL | MB_OK |
  256.                               MB_ICONHAND);
  257.                    }
  258.                 else
  259.                    {
  260.                    TraceOnFlag = TRUE;
  261.                    /* Check the filename for syntax errors Use default
  262.                       if errors exist  */
  263.                    _lclose(hFile);
  264.                    }
  265.                 break;
  266.  
  267.              case IDM_TRACEOFF:
  268.                 TraceOnFlag = FALSE;
  269.                 szFileName[0] = '\0';
  270.                 break;
  271.  
  272.              case IDM_ABOUT:
  273.                 lpProcAbout = MakeProcInstance(About, hInst);
  274.                 DialogBox(hInst,
  275.                          "AboutBox",
  276.                           hWnd,
  277.                           lpProcAbout);
  278.                 FreeProcInstance(lpProcAbout);
  279.                 break;
  280.  
  281.              case IDM_RECORD:
  282.                 if (Autotest_status != NOT_STARTED_RECORDING)
  283.                    {
  284.                    /*  Clear out the previous test script  */
  285.                    if (hMemTestEvents != NULL)
  286.                       {
  287.                       hMemTestEvents = GlobalFree(hMemTestEvents);
  288.                       }
  289.                    }
  290.                 Autotest_status = RECORDING_ON;
  291.                 AutotestResult = Tester(IDD_STARTRECORD, hMemTestEvents,
  292.                                         hWnd, IDD_HALTACTIVITY,
  293.                                         TraceOnFlag, (LPSTR)szFileName);
  294.                 break;
  295.  
  296.              case IDM_STOPREC:
  297.                 if (Autotest_status == RECORDING_ON)
  298.                    {
  299.                    hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  300.                    ShowCursor(TRUE);
  301.                    AutotestResult = Tester(IDD_STOPRECORD,
  302.                    hMemTestEvents, hWnd, IDD_HALTACTIVITY,
  303.                    TraceOnFlag, (LPSTR)szFileName);
  304.                    ShowCursor(FALSE);
  305.                    SetCursor(hCursor);
  306.                    }
  307.                 break;
  308.  
  309.              case IDM_REPLAY:
  310.                 if ((Autotest_status == RECORDING_COMPLETED) ||
  311.                     (Autotest_status == REPLAYING_COMPLETED))
  312.                    /* Read in replaying parameters from win.ini */
  313.                    {
  314.                    GetProfileString("autotest","PlaybackTracefile",
  315.                                     "plytrace.txt",szFileName, FILENAMELEN);
  316.                    if (TraceOnFlag)
  317.                       if (-1 == (hFile = OpenFile(szFileName,
  318.                                 (LPOFSTRUCT) &OfStruct,
  319.                                  OF_CREATE | OF_PROMPT | OF_CANCEL)))
  320.                          {
  321.                          TraceOnFlag = FALSE;
  322.                          wsprintf(str, "Cannot open trace file = %s",
  323.                                   szFileName);
  324.                          MessageBox(hWnd, str, "ERROR", MB_APPLMODAL | MB_OK |
  325.                                     MB_ICONHAND);
  326.                          }
  327.  
  328.                    if (hMemTestEvents != NULL)
  329.                       {
  330.                       /* Read in Timer values */
  331.                       Autotest_status = REPLAYING_ON;
  332.                       AutotestResult = Tester(IDD_STARTPLAY,
  333.                                        hMemTestEvents, hWnd, IDD_HALTACTIVITY,
  334.                                        TraceOnFlag, (LPSTR)szFileName);
  335.  
  336.                       }
  337.                    }
  338.                 break;
  339.  
  340.  
  341.              default:
  342.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  343.                 break;
  344.              }
  345.           ErrorMessageHandler(AutotestResult);
  346.           break;
  347.  
  348.        case IDD_HALTACTIVITY:
  349.           if (Autotest_status == RECORDING_ON)
  350.              Autotest_status = RECORDING_COMPLETED;
  351.           else
  352.              if (Autotest_status == REPLAYING_ON)
  353.                 Autotest_status = REPLAYING_COMPLETED;
  354.              else
  355.                 MessageBox(hWnd, "Incorrect Test Status", "Autotest",
  356.                            MB_SYSTEMMODAL | MB_OK);
  357.           hMemTestEvents = wParam;
  358.           ErrorMessageHandler((TESTRESULT)lParam);
  359.           break;
  360.  
  361.        case WM_DESTROY:
  362.           PostQuitMessage(0);
  363.           break;
  364.  
  365.        default:
  366.           return (DefWindowProc(hWnd, message, wParam, lParam));
  367.           break;
  368.        }
  369.     return (NULL);
  370. }
  371.  
  372.  
  373. /****************************************************************************
  374.  
  375.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  376.  
  377.     PURPOSE:  Processes messages for "About" dialog box
  378.  
  379.     MESSAGES:
  380.  
  381.        WM_INITDIALOG - initialize dialog box
  382.        WM_COMMAND    - input received
  383.  
  384. ****************************************************************************/
  385.  
  386. BOOL FAR PASCAL About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  387. {
  388.    switch (message)
  389.       {
  390.       case WM_INITDIALOG:
  391.          return (TRUE);
  392.  
  393.       case WM_COMMAND:
  394.          if (wParam == IDD_OK)
  395.             {
  396.             EndDialog(hDlg, TRUE);
  397.             return (TRUE);
  398.             }
  399.          break;
  400.       }
  401.  
  402.    return (FALSE);
  403. }
  404. /****************************************************************************
  405.  
  406.     FUNCTION: ErrorMessageHandler(WORD)
  407.  
  408.     PURPOSE:  Processes errors return codes from the Tester routine.
  409.               These return codes are passed through the lParam argument
  410.               of the SendMessage call.
  411.  
  412. ****************************************************************************/
  413. void ErrorMessageHandler(WORD wReturnCode)
  414. {
  415.    char *szMessage = NULL;
  416.  
  417.    switch (wReturnCode)
  418.       {
  419.       case TEST_RECORDACTIVE:
  420.          szMessage = "Tester already recording/playing.";
  421.          break;
  422.  
  423.       case TEST_INACTIVE:
  424.          szMessage = "Tester already stopped.";
  425.          break;
  426.  
  427.       case TEST_NOMEMORY:
  428.          szMessage = "Insufficient memory to begin recording.";
  429.          break;
  430.  
  431.       case TEST_NOEVENTS:
  432.          szMessage = "No events to playback.";
  433.          break;
  434.  
  435.       case TEST_TOOMANYEVENTS:
  436.          szMessage = "Too many events in event queue:  stop recording.";
  437.          break;
  438.  
  439.       }
  440.    if (szMessage != NULL)
  441.        MessageBox(hWindow, szMessage, "Autotest", MB_OK | MB_ICONINFORMATION);
  442.  
  443.    return;
  444. }
  445.  
  446.