source: trunk/MFCtooling/replay/LogParser.cpp @ 163

Last change on this file since 163 was 163, checked in by sherbold, 13 years ago
  • optional parameter -p now sets the working directory of the application under test
File size: 8.0 KB
Line 
1#include "StdAfx.h"
2#include "LogParser.h"
3
4#include <iostream>
5
6#include "WindowFinder.h"
7
8LogParser::LogParser(_TCHAR* runCommand, unsigned int startupTime, TestResults * results, bool useDefaultDelay) : runCommand(runCommand), startupTime(startupTime), results(results), useDefaultDelay(useDefaultDelay)
9{
10        workingPath = NULL;
11}
12
13LogParser::~LogParser(void)
14{
15       
16}
17
18
19
20HRESULT STDMETHODCALLTYPE LogParser::startElement(
21                        wchar_t __RPC_FAR *pwchNamespaceUri,
22                        int cchNamespaceUri,
23                        wchar_t __RPC_FAR *pwchLocalName,
24                        int cchLocalName,
25                        wchar_t __RPC_FAR *pwchRawName,
26                        int cchRawName,
27                        MSXML2::ISAXAttributes __RPC_FAR *pAttributes)
28{
29        std::wstring localName(pwchLocalName);
30        if( localName.compare(L"session")==0 ) {
31                sessionId = GetAttributeValue(pAttributes, L"id", L"");
32                std::wcout << L"================================================" << std::endl;
33                std::wcout << L"starting session " << sessionId << std::endl;
34                result.sessionPass = true;
35                result.errorMessage = L"";
36                result.msgNumber = 0;
37                currentMessage = 0;
38                std::wcout << L"executing " << runCommand << std::endl;
39                PROCESS_INFORMATION pi;
40                STARTUPINFO si;
41                ZeroMemory(&pi, sizeof(pi));
42                ZeroMemory(&si, sizeof(si));
43
44                CreateProcess(NULL, runCommand, NULL, NULL, FALSE, 0, NULL, workingPath, &si, &pi);
45
46                hProcess = pi.hProcess;
47                CloseHandle(pi.hThread);
48               
49                std::wcout << L"waiting " << startupTime << L" ms for application under test to intialize" << std::endl;
50                Sleep(startupTime);
51                std::wcout << L"replay starting..." << std::endl;
52        }
53        else if( localName.compare(L"msg")==0 ) {
54                std::wstring type = GetAttributeValue(pAttributes, L"type", L"");
55                msgType = _wtoi(type.c_str());
56                std::wstring lParamStr = GetAttributeValue(pAttributes, L"LPARAM", L"");
57                lParam = _wtoi(lParamStr.c_str());
58                std::wstring wParamStr = GetAttributeValue(pAttributes, L"WPARAM", L"");
59                wParam = _wtoi(wParamStr.c_str());
60                std::wstring delayStr = GetAttributeValue(pAttributes, L"delay", L"");
61                delay = _wtoi(delayStr.c_str());
62                currentWindow = NULL;
63                currentParent = NULL;
64        }
65        else if( localName.compare(L"window")==0 ) {
66                WindowData * winData = new WindowData;
67                winData->name = GetAttributeValue(pAttributes, L"name", L"");
68                winData->className = GetAttributeValue(pAttributes, L"class", L"");
69                std::wstring resourceIdStr = GetAttributeValue(pAttributes, L"resourceId", L"");
70                winData->resourceId = _wtoi(resourceIdStr.c_str());
71                std::wstring isModalStr = GetAttributeValue(pAttributes, L"isModal", L"");
72                if( isModalStr.compare(L"true")==0 ) {
73                        winData->isModal = true;
74                } else {
75                        winData->isModal = false;
76                }
77                winData->child = NULL;
78                if( currentWindow==NULL ) {
79                        currentWindow = winData;
80                } else {
81                        currentParent->child = winData;
82                }
83                currentParent = winData;
84        }
85        else if( localName.compare(L"textEquals")==0 ) {
86                expectedValue = GetAttributeValue(pAttributes, L"expectedValue", L"");
87        }
88        else if( localName.compare(L"fileEquals")==0) {
89                actualFile = GetAttributeValue(pAttributes, L"actualFile", L"");
90                expectedFile = GetAttributeValue(pAttributes, L"expectedFile", L"");
91        }
92
93        return S_OK;
94}
95
96
97HRESULT STDMETHODCALLTYPE LogParser::endElement(
98                        wchar_t __RPC_FAR *pwchNamespaceUri,
99                        int cchNamespaceUri,
100                        wchar_t __RPC_FAR *pwchLocalName,
101                        int cchLocalName,
102                        wchar_t __RPC_FAR *pwchRawName,
103                        int cchRawName)
104{
105        std::wstring localName(pwchLocalName);
106        if( localName.compare(L"session")==0 ) {
107                std::wcout << L"session completed" << std::endl;
108                results->addResult(sessionId, result);
109                BOOL retVal = TerminateProcess(hProcess, 0);
110                if( retVal!=0 ) {
111                        std::wcout << L"application terminated" << std::endl;
112                }
113                CloseHandle(hProcess);
114        }
115        else if( localName.compare(L"msg")==0 ) {
116                currentMessage++;
117                WindowFinder finder;
118                HWND hwnd = finder.find(currentWindow);
119                // check if window was found, if not test has failed
120                if( result.sessionPass ) {
121                        result.sessionPass = false;
122                        result.errorMessage = finder.getErrorMessage();
123                        result.msgNumber = currentMessage;
124                }
125
126                sendMessage(hwnd);
127                deleteWindowData(currentWindow);
128                currentWindow = NULL;
129        }
130        else if( localName.compare(L"LPARAM")==0 ) {
131                WindowFinder finder;
132                HWND hwnd = finder.find(currentWindow);
133                lParam = (LPARAM) hwnd;
134                deleteWindowData(currentWindow);
135                currentWindow = NULL;
136        }
137        else if( localName.compare(L"WPARAM")==0 ) {
138                WindowFinder finder;
139                HWND hwnd = finder.find(currentWindow);
140                wParam = (WPARAM) hwnd;
141                deleteWindowData(currentWindow);
142                currentWindow = NULL;
143        }
144        else if( localName.compare(L"textEquals")==0 ) {
145                WindowFinder finder;
146                HWND hwnd = finder.find(currentWindow);
147
148                wchar_t* wstr = new wchar_t[256];
149                wstr[0] = '\0';
150               
151                //GetWindowText is not working properly for EditControls in other applications -> send WM_GETTEXT instead
152                SendMessage(hwnd, WM_GETTEXT, 255, (LPARAM)wstr);
153                std::wstring windowText = wstr;
154
155                if(expectedValue == windowText) std::wcout << std::endl << L"textEquals passed (expected value: " << expectedValue.c_str() << ")" << std::endl << std::endl;
156                else std::wcout << std::endl << L"textEquals failed (expected value: " << expectedValue.c_str() << ", windowText: " << windowText << ")" << std::endl << std::endl;
157                deleteWindowData(currentWindow);
158                currentWindow = NULL;
159        }
160        else if( localName.compare(L"fileEquals")==0) {
161                std::ifstream f1(actualFile.c_str(), std::ios_base::in | std::ios_base::binary);
162                if(f1 == NULL) {
163
164                        std::wcout << std::endl << L"fileEquals failed because " << actualFile << " is not available!"<< std::endl << std::endl;
165                        return S_OK;
166                }
167
168                std::ifstream f2(expectedFile.c_str(), std::ios_base::in | std::ios_base::binary);
169                if(f2 == NULL) {
170                        std::wcout << std::endl << L"fileEquals failed because " << expectedFile << " is not available!" << std::endl << std::endl;
171                        return S_OK;
172                }
173
174                f1.seekg(0, std::ios_base::end);
175                f2.seekg(0, std::ios_base::end);
176                std::streamsize length1 = f1.tellg();
177                std::streamsize length2 = f2.tellg();
178
179                bool passed = true;
180
181                if (length1 != length2) { // Non equal length -> files differ
182                        passed = false;
183                }
184                else {
185                        f1.seekg(0);
186                        f2.seekg(0);
187
188                        const std::streamsize blocksize = 4096;
189                        char block1[blocksize], block2[blocksize];
190
191                        for (std::streamsize counter=length1; counter > 0; counter -= blocksize)
192                        {                               
193                                f1.read(block1, blocksize);
194                                f2.read(block2, blocksize);
195
196                                if(memcmp(block1, block2, blocksize) != 0) { //block are non equal -> files differ
197                                        passed = false;
198                                        break;
199                                }
200                        }
201                }
202
203                if(passed) {
204                        std::wcout << std::endl << L"FileEquals passed" << std::endl << std::endl;
205                }
206                else {
207                        std::wcout << std::endl << L"fileEquals failed (expectedFile: " << expectedFile.c_str() << ", actualFile: " << actualFile << ")" << std::endl << std::endl;
208                }
209               
210                f1.close();
211                f2.close();
212        }
213
214        return S_OK;
215}
216
217std::wstring LogParser::GetAttributeValue(MSXML2::ISAXAttributes __RPC_FAR *pAttributes,
218                                                           std::wstring name, std::wstring defvalue)
219{
220        // get the number of attributes
221        int length = 0;
222        pAttributes->getLength(&length);
223
224        // enumerate over all attributes
225        for ( int i=0; i<length; i++ )
226        {
227                wchar_t *attrname = NULL, * attrvalue = NULL;
228                int namelen = 0, valuelen = 0;
229
230                // get the local name of the current attribute
231                pAttributes->getLocalName(i,&attrname,&namelen);
232                // get the value of the current attribute
233                pAttributes->getValue(i,&attrvalue,&valuelen);
234                // if current attribute is the one needed return its value
235                if(name.compare(std::wstring(attrname,namelen)) == 0)
236                        return std::wstring(attrvalue, valuelen);
237        }
238
239        // attribute not found; return the default value
240        return defvalue;
241}
242
243void LogParser::sendMessage(HWND hwnd) {
244        std::wcout << L"  Sending " << msgType << L" to " << hwnd << "L - LPARAM: " << lParam << L" - WPARAM: " << wParam << std::endl;
245        PostMessage(hwnd, msgType, wParam, lParam);
246        if( useDefaultDelay ) {
247                Sleep(defaultMsgDelay);
248        } else {
249                Sleep(delay);
250        }
251}
252
253void LogParser::setWorkingPath(TCHAR * workingPath) {
254        this->workingPath = workingPath;
255}
Note: See TracBrowser for help on using the repository browser.