1 | #include "StdAfx.h"
|
---|
2 | #include "LogParser.h"
|
---|
3 |
|
---|
4 | #include <iostream>
|
---|
5 |
|
---|
6 | #include "WindowFinder.h"
|
---|
7 |
|
---|
8 | LogParser::LogParser(_TCHAR* runCommand, unsigned int startupTime, TestResults * results, bool useDefaultDelay) : runCommand(runCommand), startupTime(startupTime), results(results), useDefaultDelay(useDefaultDelay)
|
---|
9 | {
|
---|
10 |
|
---|
11 | }
|
---|
12 |
|
---|
13 | LogParser::~LogParser(void)
|
---|
14 | {
|
---|
15 |
|
---|
16 | }
|
---|
17 |
|
---|
18 |
|
---|
19 | HRESULT 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 | return S_OK;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | HRESULT STDMETHODCALLTYPE LogParser::endElement(
|
---|
86 | wchar_t __RPC_FAR *pwchNamespaceUri,
|
---|
87 | int cchNamespaceUri,
|
---|
88 | wchar_t __RPC_FAR *pwchLocalName,
|
---|
89 | int cchLocalName,
|
---|
90 | wchar_t __RPC_FAR *pwchRawName,
|
---|
91 | int cchRawName)
|
---|
92 | {
|
---|
93 | std::wstring localName(pwchLocalName);
|
---|
94 | if( localName.compare(L"session")==0 ) {
|
---|
95 | std::wcout << L"session completed" << std::endl;
|
---|
96 | results->addResult(sessionId, result);
|
---|
97 | BOOL retVal = TerminateProcess(hProcess, 0);
|
---|
98 | if( retVal!=0 ) {
|
---|
99 | std::wcout << L"application terminated" << std::endl;
|
---|
100 | }
|
---|
101 | CloseHandle(hProcess);
|
---|
102 | }
|
---|
103 | else if( localName.compare(L"msg")==0 ) {
|
---|
104 | currentMessage++;
|
---|
105 | WindowFinder finder;
|
---|
106 | HWND hwnd = finder.find(currentWindow);
|
---|
107 | // check if window was found, if not test has failed
|
---|
108 | if( result.sessionPass ) {
|
---|
109 | result.sessionPass = false;
|
---|
110 | result.errorMessage = finder.getErrorMessage();
|
---|
111 | result.msgNumber = currentMessage;
|
---|
112 | }
|
---|
113 |
|
---|
114 | sendMessage(hwnd);
|
---|
115 | deleteWindowData(currentWindow);
|
---|
116 | currentWindow = NULL;
|
---|
117 | }
|
---|
118 | else if( localName.compare(L"LPARAM")==0 ) {
|
---|
119 | WindowFinder finder;
|
---|
120 | HWND hwnd = finder.find(currentWindow);
|
---|
121 | lParam = (LPARAM) hwnd;
|
---|
122 | deleteWindowData(currentWindow);
|
---|
123 | currentWindow = NULL;
|
---|
124 | }
|
---|
125 | else if( localName.compare(L"WPARAM")==0 ) {
|
---|
126 | WindowFinder finder;
|
---|
127 | HWND hwnd = finder.find(currentWindow);
|
---|
128 | wParam = (WPARAM) hwnd;
|
---|
129 | deleteWindowData(currentWindow);
|
---|
130 | currentWindow = NULL;
|
---|
131 | }
|
---|
132 | return S_OK;
|
---|
133 | }
|
---|
134 |
|
---|
135 | std::wstring LogParser::GetAttributeValue(MSXML2::ISAXAttributes __RPC_FAR *pAttributes,
|
---|
136 | std::wstring name, std::wstring defvalue)
|
---|
137 | {
|
---|
138 | // get the number of attributes
|
---|
139 | int length = 0;
|
---|
140 | pAttributes->getLength(&length);
|
---|
141 |
|
---|
142 | // enumerate over all attributes
|
---|
143 | for ( int i=0; i<length; i++ )
|
---|
144 | {
|
---|
145 | wchar_t *attrname = NULL, * attrvalue = NULL;
|
---|
146 | int namelen = 0, valuelen = 0;
|
---|
147 |
|
---|
148 | // get the local name of the current attribute
|
---|
149 | pAttributes->getLocalName(i,&attrname,&namelen);
|
---|
150 | // get the value of the current attribute
|
---|
151 | pAttributes->getValue(i,&attrvalue,&valuelen);
|
---|
152 | // if current attribute is the one needed return its value
|
---|
153 | if(name.compare(std::wstring(attrname,namelen)) == 0)
|
---|
154 | return std::wstring(attrvalue, valuelen);
|
---|
155 | }
|
---|
156 |
|
---|
157 | // attribute not found; return the default value
|
---|
158 | return defvalue;
|
---|
159 | }
|
---|
160 |
|
---|
161 | void LogParser::sendMessage(HWND hwnd) {
|
---|
162 | std::wcout << L" Sending " << msgType << L" to " << hwnd << "L - LPARAM: " << lParam << L" - WPARAM: " << wParam << std::endl;
|
---|
163 | PostMessage(hwnd, msgType, wParam, lParam);
|
---|
164 | if( useDefaultDelay ) {
|
---|
165 | Sleep(defaultMsgDelay);
|
---|
166 | } else {
|
---|
167 | Sleep(delay);
|
---|
168 | }
|
---|
169 | } |
---|