[NETAPI32]
[reactos.git] / rostests / apitests / user32 / helper.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: helper functions
5 * PROGRAMMERS: Giannis Adamopoulos
6 */
7
8 #include <apitest.h>
9
10 #include <stdio.h>
11 #include <winuser.h>
12 #include "helper.h"
13 #include <undocuser.h>
14
15 MSG_CACHE default_cache = {
16 #ifdef _MSC_VER
17 0
18 #endif
19 };
20 MSG_ENTRY empty_chain[]= {{0,0}};
21
22 static char* get_msg_name(UINT msg)
23 {
24 switch(msg)
25 {
26 case WM_NCACTIVATE: return "WM_NCACTIVATE";
27 case WM_ACTIVATE: return "WM_ACTIVATE";
28 case WM_ACTIVATEAPP: return "WM_ACTIVATEAPP";
29 case WM_WINDOWPOSCHANGING: return "WM_WINDOWPOSCHANGING";
30 case WM_WINDOWPOSCHANGED: return "WM_WINDOWPOSCHANGED";
31 case WM_SETFOCUS: return "WM_SETFOCUS";
32 case WM_KILLFOCUS: return "WM_KILLFOCUS";
33 case WM_NCPAINT: return "WM_NCPAINT";
34 case WM_PAINT: return "WM_PAINT";
35 case WM_ERASEBKGND: return "WM_ERASEBKGND";
36 case WM_SIZE: return "WM_SIZE";
37 case WM_MOVE: return "WM_MOVE";
38 case WM_SHOWWINDOW: return "WM_SHOWWINDOW";
39 case WM_QUERYNEWPALETTE: return "WM_QUERYNEWPALETTE";
40 case WM_MOUSELEAVE: return "WM_MOUSELEAVE";
41 case WM_MOUSEHOVER: return "WM_MOUSEHOVER";
42 case WM_NCMOUSELEAVE: return "WM_NCMOUSELEAVE";
43 case WM_NCMOUSEHOVER: return "WM_NCMOUSEHOVER";
44 case WM_NCHITTEST: return "WM_NCHITTEST";
45 case WM_SETCURSOR: return "WM_SETCURSOR";
46 case WM_MOUSEMOVE: return "WM_MOUSEMOVE";
47 case WM_SYSTIMER: return "WM_SYSTIMER";
48 case WM_GETMINMAXINFO: return "WM_GETMINMAXINFO";
49 case WM_NCCALCSIZE: return "WM_NCCALCSIZE";
50 case WM_SETTINGCHANGE: return "WM_SETTINGCHANGE";
51 case WM_GETICON: return "WM_GETICON";
52 case WM_SETICON: return "WM_SETICON";
53 case WM_KEYDOWN: return "WM_KEYDOWN";
54 case WM_KEYUP: return "WM_KEYUP";
55 default: return NULL;
56 }
57 }
58
59 static char* get_hook_name(UINT id)
60 {
61 switch (id)
62 {
63 case WH_KEYBOARD: return "WH_KEYBOARD";
64 case WH_KEYBOARD_LL: return "WH_KEYBOARD_LL";
65 case WH_MOUSE: return "WH_MOUSE";
66 case WH_MOUSE_LL: return "WH_MOUSE_LL";
67 default: return NULL;
68 }
69 }
70
71 void empty_message_cache(MSG_CACHE* cache)
72 {
73 memset(cache, 0, sizeof(MSG_CACHE));
74 }
75
76 void sprintf_msg_entry(char* buffer, MSG_ENTRY* msg)
77 {
78 if(!msg->iwnd && !msg->msg)
79 {
80 sprintf(buffer, "nothing");
81 }
82 else
83 {
84 char* msgName;
85 char* msgType;
86
87 switch (msg->type)
88 {
89 case POST:
90 msgName = get_msg_name(msg->msg);
91 msgType = "post msg";
92 break;
93 case SENT:
94 msgName = get_msg_name(msg->msg);
95 msgType = "sent msg";
96 break;
97 case HOOK:
98 msgName = get_hook_name(msg->msg);
99 msgType = "hook";
100 break;
101 case EVENT:
102 msgName = NULL;
103 msgType = "event";
104 break;
105 default:
106 return;
107 }
108
109 if(msgName)
110 sprintf(buffer, "hwnd%d %s %s %d %d", msg->iwnd, msgType, msgName, msg->param1, msg->param2);
111 else
112 sprintf(buffer, "hwnd%d %s %d %d %d", msg->iwnd, msgType, msg->msg, msg->param1, msg->param2);
113 }
114 }
115
116 void trace_cache(MSG_CACHE* cache, const char* file, int line)
117 {
118 int i;
119 char buff[100];
120
121 for (i=0; i < cache->count; i++)
122 {
123 sprintf_msg_entry(buff, &cache->message_cache[i]);
124 trace_(file,line)("%d: %s\n", i, buff);
125 }
126 trace_(file,line)("\n");
127 }
128
129 void compare_cache(MSG_CACHE* cache, const char* file, int line, MSG_ENTRY *msg_chain)
130 {
131 int i = 0;
132 char buffGot[100], buffExp[100];
133 BOOL got_error = FALSE;
134
135 while(1)
136 {
137 BOOL same = !memcmp(&cache->message_cache[i],msg_chain, sizeof(MSG_ENTRY));
138
139 sprintf_msg_entry(buffGot, &cache->message_cache[i]);
140 sprintf_msg_entry(buffExp, msg_chain);
141 ok_(file,line)(same,"%d: got %s, expected %s\n",i, buffGot, buffExp);
142
143 if(!got_error && !same)
144 got_error = TRUE;
145
146 if(msg_chain->msg !=0 || msg_chain->iwnd != 0)
147 msg_chain++;
148 else
149 {
150 if(i > cache->count)
151 break;
152 }
153 i++;
154 }
155
156 if(got_error )
157 {
158 trace_(file,line)("The complete list of messages got is:\n");
159 trace_cache(cache, file,line);
160 }
161
162 empty_message_cache(cache);
163 }
164
165 void record_message(MSG_CACHE* cache, int iwnd, UINT message, MSG_TYPE type, int param1,int param2)
166 {
167 if(cache->count >= 100)
168 {
169 return;
170 }
171
172 /* do not report a post message a second time */
173 if(type == SENT &&
174 cache->last_post_message.iwnd == iwnd &&
175 cache->last_post_message.msg == message &&
176 cache->last_post_message.param1 == param1 &&
177 cache->last_post_message.param2 == param2)
178 {
179 memset(&cache->last_post_message, 0, sizeof(MSG_ENTRY));
180 return;
181 }
182
183 cache->message_cache[cache->count].iwnd = iwnd;
184 cache->message_cache[cache->count].msg = message;
185 cache->message_cache[cache->count].type = type;
186 cache->message_cache[cache->count].param1 = param1;
187 cache->message_cache[cache->count].param2 = param2;
188
189 if(cache->message_cache[cache->count].type == POST)
190 {
191 cache->last_post_message = cache->message_cache[cache->count];
192 }
193 else
194 {
195 memset(&cache->last_post_message, 0, sizeof(MSG_ENTRY));
196 }
197
198 cache->count++;
199 }
200
201 ATOM RegisterSimpleClass(WNDPROC lpfnWndProc, LPCWSTR lpszClassName)
202 {
203 WNDCLASSEXW wcex;
204
205 memset(&wcex, 0, sizeof(wcex));
206 wcex.cbSize = sizeof(WNDCLASSEX);
207 wcex.lpfnWndProc = lpfnWndProc;
208 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
209 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
210 wcex.lpszClassName = lpszClassName;
211 return RegisterClassExW(&wcex);
212 }