winex11.drv: Map coordinates before calling send_mouse_input.
[wine/zf.git] / dlls / kernel32 / tests / timer.c
blob156f1acd7f1a1698185f0081ffc0552e70a90ae8
1 /*
2 * Unit test suite for timer functions
4 * Copyright 2004 Mike McCormack
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include "winternl.h"
24 #include "winuser.h"
27 static void test_timer(void)
29 HANDLE handle;
30 BOOL r;
31 LARGE_INTEGER due;
33 /* try once with a positive number */
34 handle = CreateWaitableTimerA( NULL, 0, NULL );
35 ok( handle != NULL, "failed to create waitable timer with no name\n" );
37 due.QuadPart = 10000;
38 r = SetWaitableTimer( handle, &due, 0x1f4, NULL, NULL, FALSE );
39 ok( r, "failed to set timer\n");
41 CloseHandle( handle );
43 /* try once with a negative number */
44 handle = CreateWaitableTimerA( NULL, 0, NULL );
45 ok( handle != NULL, "failed to create waitable timer with no name\n" );
47 due.QuadPart = -10000;
48 r = SetWaitableTimer( handle, &due, 0x1f4, NULL, NULL, FALSE );
49 ok( r, "failed to set timer\n");
51 CloseHandle( handle );
54 #define TICKSPERSEC 10000000
56 static BOOL adjust_system_time(int sec)
58 ULARGE_INTEGER uli;
59 SYSTEMTIME st;
60 FILETIME ft;
62 GetSystemTimeAsFileTime(&ft);
63 uli.u.LowPart = ft.dwLowDateTime;
64 uli.u.HighPart = ft.dwHighDateTime;
65 uli.QuadPart += (LONGLONG)sec * TICKSPERSEC;
66 ft.dwLowDateTime = uli.u.LowPart;
67 ft.dwHighDateTime = uli.u.HighPart;
68 if (!FileTimeToSystemTime(&ft, &st))
69 return FALSE;
70 return SetSystemTime(&st);
73 static DWORD WINAPI thread_WaitForSingleObject(void *arg)
75 HANDLE event;
76 DWORD t, r;
78 event = CreateEventW(NULL, FALSE, FALSE, NULL);
79 ok(event != NULL, "CreateEvent failed\n");
80 t = GetTickCount();
81 r = WaitForSingleObject(event, 3000);
82 ok(r == WAIT_TIMEOUT, "WiatForSingleObject returned %x\n", r);
83 CloseHandle(event);
84 t = GetTickCount() - t;
85 ok(t > 2000, "t = %d\n", t);
86 return 0;
89 static DWORD WINAPI thread_Sleep(void *arg)
91 DWORD t = GetTickCount();
93 Sleep(3000);
94 t = GetTickCount() - t;
95 ok(t > 2000, "t = %d\n", t);
96 return 0;
99 static DWORD WINAPI thread_SleepEx(void *arg)
101 DWORD t = GetTickCount();
103 SleepEx(3000, TRUE);
104 t = GetTickCount() - t;
105 ok(t > 2000, "t = %d\n", t);
106 return 0;
109 static DWORD WINAPI thread_WaitableTimer_rel(void *arg)
111 LARGE_INTEGER li;
112 HANDLE timer;
113 DWORD t, r;
115 li.QuadPart = -3 * TICKSPERSEC;
117 timer = CreateWaitableTimerA(NULL, TRUE, NULL);
118 ok(timer != NULL, "CreateWaitableTimer failed\n");
120 t = GetTickCount();
121 r = SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE);
122 ok(r, "SetWaitableTimer failed\n");
124 r = WaitForSingleObject(timer, INFINITE);
125 ok(r == WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", r);
126 CloseHandle(timer);
127 t = GetTickCount() - t;
128 ok(t > 2000, "t = %d\n", t);
129 return 0;
132 static DWORD WINAPI thread_WaitableTimer_abs(void *arg)
134 LARGE_INTEGER li;
135 HANDLE timer;
136 FILETIME ft;
137 DWORD t, r;
139 GetSystemTimeAsFileTime(&ft);
140 li.u.LowPart = ft.dwLowDateTime;
141 li.u.HighPart = ft.dwHighDateTime;
142 li.QuadPart += 3 * TICKSPERSEC;
144 timer = CreateWaitableTimerA(NULL, TRUE, NULL);
145 ok(timer != NULL, "CreateWaitableTimer failed\n");
147 t = GetTickCount();
148 r = SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE);
149 ok(r, "SetWaitableTimer failed\n");
151 r = WaitForSingleObject(timer, INFINITE);
152 ok(r == WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", r);
153 CloseHandle(timer);
154 t = GetTickCount() - t;
155 ok(t < 2000, "t = %d\n", t);
156 return 0;
159 static DWORD WINAPI thread_WaitableTimer_period(void *arg)
161 LARGE_INTEGER li;
162 HANDLE timer;
163 DWORD t, r;
165 li.QuadPart = -1;
167 timer = CreateWaitableTimerA(NULL, FALSE, NULL);
168 ok(timer != NULL, "CreateWaitableTimer failed\n");
170 t = GetTickCount();
171 r = SetWaitableTimer(timer, &li, 3000, NULL, NULL, FALSE);
172 ok(r, "SetWaitableTimer failed\n");
174 r = WaitForSingleObject(timer, INFINITE);
175 ok(r == WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", r);
177 r = WaitForSingleObject(timer, INFINITE);
178 ok(r == WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", r);
179 CloseHandle(timer);
180 t = GetTickCount() - t;
181 ok(t > 2000, "t = %d\n", t);
182 return 0;
185 static DWORD WINAPI thread_SetTimer(void *arg)
187 DWORD t = GetTickCount();
188 UINT_PTR timer;
189 MSG msg;
191 timer = SetTimer(NULL, 0, 3000, NULL);
192 ok(timer, "SetTimer failed (%d)\n", GetLastError());
194 while (GetMessageW(&msg, NULL, 0, 0))
196 DispatchMessageW(&msg);
197 if (msg.message == WM_TIMER) break;
200 t = GetTickCount() - t;
201 ok(t > 2000, "t = %d\n", t);
202 KillTimer(NULL, timer);
203 return 0;
206 static void test_timeouts(void)
208 HANDLE threads[7];
209 DWORD i;
211 if (!adjust_system_time(1))
213 skip("can't adjust system clock (%d)\n", GetLastError());
214 return;
218 threads[0] = CreateThread(NULL, 0, thread_WaitForSingleObject, NULL, 0, NULL);
219 threads[1] = CreateThread(NULL, 0, thread_Sleep, NULL, 0, NULL);
220 threads[2] = CreateThread(NULL, 0, thread_SleepEx, NULL, 0, NULL);
221 threads[3] = CreateThread(NULL, 0, thread_WaitableTimer_rel, NULL, 0, NULL);
222 threads[4] = CreateThread(NULL, 0, thread_WaitableTimer_abs, NULL, 0, NULL);
223 threads[5] = CreateThread(NULL, 0, thread_WaitableTimer_period, NULL, 0, NULL);
224 threads[6] = CreateThread(NULL, 0, thread_SetTimer, NULL, 0, NULL);
225 for(i=0; i<ARRAY_SIZE(threads); i++)
226 ok(threads[i] != NULL, "CreateThread failed\n");
228 Sleep(500);
229 adjust_system_time(10);
231 for (i=0; i<ARRAY_SIZE(threads); i++)
233 WaitForSingleObject(threads[i], INFINITE);
234 CloseHandle(threads[i]);
236 adjust_system_time(-11);
239 START_TEST(timer)
241 test_timer();
242 test_timeouts();