1*c2c66affSColin Finck /**
2*c2c66affSColin Finck  * Test case for PostThreadMessage
3*c2c66affSColin Finck  * (C) 2003 ReactOS
4*c2c66affSColin Finck  * License: LGPL
5*c2c66affSColin Finck  * See: LGPL.txt in top directory.
6*c2c66affSColin Finck  * Author: arty
7*c2c66affSColin Finck  *
8*c2c66affSColin Finck  * Windows thread message queue test case.
9*c2c66affSColin Finck  * Derived from ../event/event.c in part.
10*c2c66affSColin Finck  */
11*c2c66affSColin Finck 
12*c2c66affSColin Finck #include <windows.h>
13*c2c66affSColin Finck #include <stdio.h>
14*c2c66affSColin Finck #include <assert.h>
15*c2c66affSColin Finck 
16*c2c66affSColin Finck HANDLE hWaitForFailure;
17*c2c66affSColin Finck HANDLE hOkToPostThreadMessage;
18*c2c66affSColin Finck HANDLE hOkToTerminate;
19*c2c66affSColin Finck 
thread(LPVOID crap)20*c2c66affSColin Finck DWORD WINAPI thread( LPVOID crap )
21*c2c66affSColin Finck {
22*c2c66affSColin Finck 	MSG msg;
23*c2c66affSColin Finck 
24*c2c66affSColin Finck 	/* Failure case ... Wait for the parent to try to post a message
25*c2c66affSColin Finck 	   before queue creation */
26*c2c66affSColin Finck 	printf( "Waiting to create the message queue.\n" );
27*c2c66affSColin Finck 
28*c2c66affSColin Finck         WaitForSingleObject(hWaitForFailure,INFINITE);
29*c2c66affSColin Finck 
30*c2c66affSColin Finck 	printf( "Creating message queue.\n" );
31*c2c66affSColin Finck 
32*c2c66affSColin Finck 	/* "Create" a message queue */
33*c2c66affSColin Finck 	PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE );
34*c2c66affSColin Finck 
35*c2c66affSColin Finck 	printf( "Signalling the parent that we're ready.\n" );
36*c2c66affSColin Finck 
37*c2c66affSColin Finck 	/* Signal that it's ok to post */
38*c2c66affSColin Finck 	SetEvent( hOkToPostThreadMessage );
39*c2c66affSColin Finck 
40*c2c66affSColin Finck 	printf( "Listening messages.\n" );
41*c2c66affSColin Finck 
42*c2c66affSColin Finck 	/* Now read some messages */
43*c2c66affSColin Finck 	while( GetMessage( &msg, 0,0,0 ) ) {
44*c2c66affSColin Finck 		printf( "Received message: %04x %04x %08lx\n",
45*c2c66affSColin Finck 		        (msg.message & 0xffff),
46*c2c66affSColin Finck 			(msg.wParam & 0xffff),
47*c2c66affSColin Finck 			msg.lParam );
48*c2c66affSColin Finck 		assert( !msg.hwnd );
49*c2c66affSColin Finck 	}
50*c2c66affSColin Finck 
51*c2c66affSColin Finck 	printf( "Finished receiving messages.\n" );
52*c2c66affSColin Finck 	SetEvent( hOkToTerminate );
53*c2c66affSColin Finck 
54*c2c66affSColin Finck 	return 0;
55*c2c66affSColin Finck }
56*c2c66affSColin Finck 
main(int argc,char ** argv)57*c2c66affSColin Finck int main( int argc, char **argv )
58*c2c66affSColin Finck {
59*c2c66affSColin Finck 	DWORD id;
60*c2c66affSColin Finck 
61*c2c66affSColin Finck 	printf( "Creating events\n" );
62*c2c66affSColin Finck 
63*c2c66affSColin Finck 	hOkToPostThreadMessage = CreateEvent( NULL, FALSE, FALSE, NULL );
64*c2c66affSColin Finck 	hOkToTerminate = CreateEvent( NULL, FALSE, FALSE, NULL );
65*c2c66affSColin Finck         hWaitForFailure = CreateEvent( NULL, FALSE, FALSE, NULL );
66*c2c66affSColin Finck 
67*c2c66affSColin Finck 	printf( "Created events\n" );
68*c2c66affSColin Finck 
69*c2c66affSColin Finck 	if( CreateThread( 0, 0, thread, 0, 0, &id ) == NULL ) {
70*c2c66affSColin Finck 		printf( "Couldn't create one thread.\n" );
71*c2c66affSColin Finck 		return 0;
72*c2c66affSColin Finck         }
73*c2c66affSColin Finck 
74*c2c66affSColin Finck         printf( "Posting to non-existent queue\n" );
75*c2c66affSColin Finck 
76*c2c66affSColin Finck 	/* Check failure case */
77*c2c66affSColin Finck 	assert( PostThreadMessage( id, WM_USER + 0, 1, 2 ) == FALSE );
78*c2c66affSColin Finck 
79*c2c66affSColin Finck 	printf( "Signalling thread to advance.\n" );
80*c2c66affSColin Finck 
81*c2c66affSColin Finck         SetEvent( hWaitForFailure );
82*c2c66affSColin Finck 
83*c2c66affSColin Finck 	printf( "Waiting for signal from thread.\n" );
84*c2c66affSColin Finck 	WaitForSingleObject( hOkToPostThreadMessage, INFINITE );
85*c2c66affSColin Finck 
86*c2c66affSColin Finck 	printf( "Sending three messages, then quit.\n" );
87*c2c66affSColin Finck 	assert( PostThreadMessage( id, WM_USER + 0, 1, 2 ) );
88*c2c66affSColin Finck 	assert( PostThreadMessage( id, WM_USER + 1, 3, 4 ) );
89*c2c66affSColin Finck 	Sleep( 500 ); /* Sleep a bit, so that the queue is empty for a bit. */
90*c2c66affSColin Finck 	assert( PostThreadMessage( id, WM_USER + 2, 5, 6 ) );
91*c2c66affSColin Finck 	assert( PostThreadMessage( id, WM_QUIT, 0,0 ) );
92*c2c66affSColin Finck 
93*c2c66affSColin Finck 	WaitForSingleObject( hOkToTerminate, INFINITE );
94*c2c66affSColin Finck 	printf( "Test complete.\n" );
95*c2c66affSColin Finck 
96*c2c66affSColin Finck 	return 0;
97*c2c66affSColin Finck }
98