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

Last change on this file since 143 was 143, checked in by jhall, 13 years ago

added assertion handling for "fileEquals" and "textEquals", should work properly

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