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 <msgtrace.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 
get_msg_name(UINT msg)22 static char* get_msg_name(UINT msg)
23 {
24     switch(msg)
25     {
26         case WM_CREATE: return "WM_CREATE";
27         case WM_NCCREATE: return "WM_NCCREATE";
28         case WM_PARENTNOTIFY: return "WM_PARENTNOTIFY";
29         case WM_DESTROY: return "WM_DESTROY";
30         case WM_NCDESTROY: return "WM_NCDESTROY";
31         case WM_CHILDACTIVATE: return "WM_CHILDACTIVATE";
32         case WM_NCACTIVATE: return "WM_NCACTIVATE";
33         case WM_ACTIVATE: return "WM_ACTIVATE";
34         case WM_ACTIVATEAPP: return "WM_ACTIVATEAPP";
35         case WM_WINDOWPOSCHANGING: return "WM_WINDOWPOSCHANGING";
36         case WM_WINDOWPOSCHANGED: return "WM_WINDOWPOSCHANGED";
37         case WM_SETFOCUS: return "WM_SETFOCUS";
38         case WM_KILLFOCUS: return "WM_KILLFOCUS";
39         case WM_NCPAINT: return "WM_NCPAINT";
40         case WM_PAINT: return "WM_PAINT";
41         case WM_ERASEBKGND: return "WM_ERASEBKGND";
42         case WM_SIZE: return "WM_SIZE";
43         case WM_MOVE: return "WM_MOVE";
44         case WM_SHOWWINDOW: return "WM_SHOWWINDOW";
45         case WM_QUERYNEWPALETTE: return "WM_QUERYNEWPALETTE";
46         case WM_MOUSELEAVE: return "WM_MOUSELEAVE";
47         case WM_MOUSEHOVER: return "WM_MOUSEHOVER";
48         case WM_NCMOUSELEAVE: return "WM_NCMOUSELEAVE";
49         case WM_NCMOUSEHOVER: return "WM_NCMOUSEHOVER";
50         case WM_NCHITTEST: return "WM_NCHITTEST";
51         case WM_SETCURSOR: return "WM_SETCURSOR";
52         case WM_MOUSEMOVE: return "WM_MOUSEMOVE";
53         case WM_SYSTIMER: return "WM_SYSTIMER";
54         case WM_GETMINMAXINFO: return "WM_GETMINMAXINFO";
55         case WM_NCCALCSIZE: return "WM_NCCALCSIZE";
56         case WM_SETTINGCHANGE: return "WM_SETTINGCHANGE";
57         case WM_GETICON: return "WM_GETICON";
58         case WM_SETICON: return "WM_SETICON";
59         case WM_KEYDOWN: return "WM_KEYDOWN";
60         case WM_KEYUP: return "WM_KEYUP";
61         case WM_NOTIFY: return "WM_NOTIFY";
62         case WM_COMMAND: return "WM_COMMAND";
63         case WM_PRINTCLIENT: return "WM_PRINTCLIENT";
64         case WM_CTLCOLORSTATIC: return "WM_CTLCOLORSTATIC";
65         case WM_STYLECHANGING: return "WM_STYLECHANGING";
66         case WM_STYLECHANGED: return "WM_STYLECHANGED";
67         default: return NULL;
68     }
69 }
70 
get_hook_name(UINT id)71 static char* get_hook_name(UINT id)
72 {
73     switch (id)
74     {
75         case WH_KEYBOARD: return "WH_KEYBOARD";
76         case WH_KEYBOARD_LL: return "WH_KEYBOARD_LL";
77         case WH_MOUSE: return "WH_MOUSE";
78         case WH_MOUSE_LL: return "WH_MOUSE_LL";
79         default: return NULL;
80     }
81 }
82 
empty_message_cache(MSG_CACHE * cache)83 void empty_message_cache(MSG_CACHE* cache)
84 {
85     memset(cache, 0, sizeof(MSG_CACHE));
86 }
87 
sprintf_msg_entry(char * buffer,MSG_ENTRY * msg)88 void sprintf_msg_entry(char* buffer, MSG_ENTRY* msg)
89 {
90     if(!msg->iwnd && !msg->msg)
91     {
92         sprintf(buffer, "nothing");
93     }
94     else
95     {
96         char* msgName;
97         char* msgType;
98 
99         switch (msg->type)
100         {
101         case POST:
102         case MARKER:
103             msgName = get_msg_name(msg->msg);
104             msgType = msg->type == POST ? "post msg" : "marker";
105             break;
106         case SENT:
107         case SENT_RET:
108             msgName = get_msg_name(msg->msg);
109             msgType = msg->type == SENT ? "sent msg" : "sent_ret msg";
110             break;
111         case HOOK:
112             msgName = get_hook_name(msg->msg);
113             msgType = "hook";
114             break;
115         case EVENT:
116             msgName = NULL;
117             msgType = "event";
118             break;
119         default:
120             return;
121         }
122 
123         if(msgName)
124             sprintf(buffer, "hwnd%d %s %s %d %d", msg->iwnd, msgType, msgName, msg->param1, msg->param2);
125         else
126             sprintf(buffer, "hwnd%d %s %d %d %d", msg->iwnd, msgType, msg->msg, msg->param1, msg->param2);
127     }
128 }
129 
trace_cache(MSG_CACHE * cache,const char * file,int line)130 void trace_cache(MSG_CACHE* cache, const char* file, int line)
131 {
132     int i;
133     char buff[100];
134 
135     for (i=0; i < cache->count; i++)
136     {
137         sprintf_msg_entry(buff, &cache->message_cache[i]);
138         trace_(file,line)("%d: %s\n", i, buff);
139     }
140     trace_(file,line)("\n");
141 }
142 
compare_cache(MSG_CACHE * cache,const char * file,int line,MSG_ENTRY * msg_chain)143 void compare_cache(MSG_CACHE* cache, const char* file, int line, MSG_ENTRY *msg_chain)
144 {
145     int i = 0;
146     char buffGot[100], buffExp[100];
147     BOOL got_error = FALSE;
148 
149     while(1)
150     {
151         BOOL same = !memcmp(&cache->message_cache[i],msg_chain, sizeof(MSG_ENTRY));
152 
153         sprintf_msg_entry(buffGot, &cache->message_cache[i]);
154         sprintf_msg_entry(buffExp, msg_chain);
155         ok_(file,line)(same,"%d: got %s, expected %s\n",i, buffGot, buffExp);
156 
157         if(!got_error && !same)
158             got_error = TRUE;
159 
160         if(msg_chain->msg !=0 || msg_chain->iwnd != 0)
161             msg_chain++;
162         else
163         {
164             if(i > cache->count)
165                 break;
166         }
167         i++;
168     }
169 
170     if(got_error )
171     {
172         trace_(file,line)("The complete list of messages got is:\n");
173         trace_cache(cache, file,line);
174     }
175 
176     empty_message_cache(cache);
177 }
178 
record_message(MSG_CACHE * cache,int iwnd,UINT message,MSG_TYPE type,int param1,int param2)179 void record_message(MSG_CACHE* cache, int iwnd, UINT message, MSG_TYPE type, int param1,int param2)
180 {
181     if(cache->count >= 100)
182     {
183         return;
184     }
185 
186     /* do not report a post message a second time */
187     if(type == SENT &&
188        cache->last_post_message.iwnd == iwnd &&
189        cache->last_post_message.msg == message &&
190        cache->last_post_message.param1 == param1 &&
191        cache->last_post_message.param2 == param2)
192     {
193         memset(&cache->last_post_message, 0, sizeof(MSG_ENTRY));
194         return;
195     }
196 
197     cache->message_cache[cache->count].iwnd = iwnd;
198     cache->message_cache[cache->count].msg = message;
199     cache->message_cache[cache->count].type = type;
200     cache->message_cache[cache->count].param1 = param1;
201     cache->message_cache[cache->count].param2 = param2;
202 
203     if(cache->message_cache[cache->count].type == POST)
204     {
205         cache->last_post_message = cache->message_cache[cache->count];
206     }
207     else
208     {
209         memset(&cache->last_post_message, 0, sizeof(MSG_ENTRY));
210     }
211 
212     cache->count++;
213 }
214