2 * System clock unit tests
4 * Copyright (C) 2007 Alex VillacĂs Lasso
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
23 #include "wine/test.h"
25 static ULONGLONG (WINAPI
*pGetTickCount64
)(void);
27 static IReferenceClock
*create_system_clock(void)
29 IReferenceClock
*filter
= NULL
;
30 HRESULT hr
= CoCreateInstance(&CLSID_SystemClock
, NULL
, CLSCTX_INPROC_SERVER
,
31 &IID_IReferenceClock
, (void **)&filter
);
32 ok(hr
== S_OK
, "Got hr %#x.\n", hr
);
36 #define check_interface(a, b, c) check_interface_(__LINE__, a, b, c)
37 static void check_interface_(unsigned int line
, void *iface_ptr
, REFIID iid
, BOOL supported
)
39 IUnknown
*iface
= iface_ptr
;
40 HRESULT hr
, expected_hr
;
43 expected_hr
= supported
? S_OK
: E_NOINTERFACE
;
45 hr
= IUnknown_QueryInterface(iface
, iid
, (void **)&unk
);
46 ok_(__FILE__
, line
)(hr
== expected_hr
, "Got hr %#x, expected %#x.\n", hr
, expected_hr
);
48 IUnknown_Release(unk
);
51 static void test_interfaces(void)
53 IReferenceClock
*clock
= create_system_clock();
56 check_interface(clock
, &IID_IReferenceClock
, TRUE
);
57 check_interface(clock
, &IID_IUnknown
, TRUE
);
59 check_interface(clock
, &IID_IDirectDraw
, FALSE
);
61 ref
= IReferenceClock_Release(clock
);
62 ok(!ref
, "Got outstanding refcount %d.\n", ref
);
65 static void test_get_time(void)
67 IReferenceClock
*clock
= create_system_clock();
68 REFERENCE_TIME time1
, time2
;
72 hr
= IReferenceClock_GetTime(clock
, NULL
);
73 ok(hr
== E_POINTER
, "Got hr %#x.\n", hr
);
75 hr
= IReferenceClock_GetTime(clock
, &time1
);
77 time2
= pGetTickCount64() * 10000;
79 time2
= GetTickCount() * 10000;
80 ok(hr
== S_OK
, "Got hr %#x.\n", hr
);
81 ok(time1
% 10000 == 0, "Expected no less than 1ms coarseness, but got time %s.\n",
82 wine_dbgstr_longlong(time1
));
83 ok(abs(time1
- time2
) < 20 * 10000, "Expected about %s, got %s.\n",
84 wine_dbgstr_longlong(time2
), wine_dbgstr_longlong(time1
));
86 hr
= IReferenceClock_GetTime(clock
, &time2
);
87 ok(hr
== (time2
== time1
? S_FALSE
: S_OK
), "Got hr %#x.\n", hr
);
90 hr
= IReferenceClock_GetTime(clock
, &time2
);
91 ok(hr
== S_OK
, "Got hr %#x.\n", hr
);
92 ok(time2
- time1
> 80 * 10000, "Expected about %s, but got %s.\n",
93 wine_dbgstr_longlong(time1
+ 80 * 10000), wine_dbgstr_longlong(time2
));
95 ref
= IReferenceClock_Release(clock
);
96 ok(!ref
, "Got outstanding refcount %d.\n", ref
);
99 static void test_advise(void)
101 IReferenceClock
*clock
= create_system_clock();
102 HANDLE event
, semaphore
;
103 REFERENCE_TIME current
;
109 event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
110 semaphore
= CreateSemaphoreA(NULL
, 0, 10, NULL
);
112 hr
= IReferenceClock_GetTime(clock
, ¤t
);
113 ok(SUCCEEDED(hr
), "Got hr %#x.\n", hr
);
115 hr
= IReferenceClock_AdviseTime(clock
, current
, 500 * 10000, (HEVENT
)event
, NULL
);
116 ok(hr
== E_POINTER
, "Got hr %#x.\n", hr
);
118 hr
= IReferenceClock_AdviseTime(clock
, -1000 * 10000, 500 * 10000, (HEVENT
)event
, &cookie
);
119 ok(hr
== E_INVALIDARG
, "Got hr %#x.\n", hr
);
121 hr
= IReferenceClock_AdviseTime(clock
, current
, 500 * 10000, (HEVENT
)event
, &cookie
);
122 ok(hr
== S_OK
, "Got hr %#x.\n", hr
);
123 ok(WaitForSingleObject(event
, 480) == WAIT_TIMEOUT
, "Event should not be signaled.\n");
124 ok(!WaitForSingleObject(event
, 40), "Event should be signaled.\n");
126 hr
= IReferenceClock_Unadvise(clock
, cookie
);
127 ok(hr
== S_FALSE
, "Got hr %#x.\n", hr
);
130 hr
= IReferenceClock_GetTime(clock
, ¤t
);
131 ok(SUCCEEDED(hr
), "Got hr %#x.\n", hr
);
132 hr
= IReferenceClock_AdviseTime(clock
, current
, 500 * 10000, (HEVENT
)event
, &cookie
);
133 ok(hr
== S_OK
, "Got hr %#x.\n", hr
);
134 hr
= IReferenceClock_Unadvise(clock
, cookie
);
135 ok(hr
== S_OK
, "Got hr %#x.\n", hr
);
136 ok(WaitForSingleObject(event
, 520) == WAIT_TIMEOUT
, "Event should not be signaled.\n");
139 hr
= IReferenceClock_GetTime(clock
, ¤t
);
140 ok(SUCCEEDED(hr
), "Got hr %#x.\n", hr
);
141 hr
= IReferenceClock_AdviseTime(clock
, current
+ 500 * 10000, 0, (HEVENT
)event
, &cookie
);
142 ok(hr
== S_OK
, "Got hr %#x.\n", hr
);
143 ok(WaitForSingleObject(event
, 480) == WAIT_TIMEOUT
, "Event should not be signaled.\n");
144 ok(!WaitForSingleObject(event
, 40), "Event should be signaled.\n");
146 hr
= IReferenceClock_GetTime(clock
, ¤t
);
147 ok(SUCCEEDED(hr
), "Got hr %#x.\n", hr
);
149 hr
= IReferenceClock_AdvisePeriodic(clock
, current
, 500 * 10000, (HSEMAPHORE
)semaphore
, NULL
);
150 ok(hr
== E_POINTER
, "Got hr %#x.\n", hr
);
152 hr
= IReferenceClock_AdvisePeriodic(clock
, current
, 0, (HSEMAPHORE
)semaphore
, &cookie
);
153 ok(hr
== E_INVALIDARG
, "Got hr %#x.\n", hr
);
155 hr
= IReferenceClock_AdvisePeriodic(clock
, current
, -500 * 10000, (HSEMAPHORE
)semaphore
, &cookie
);
156 ok(hr
== E_INVALIDARG
, "Got hr %#x.\n", hr
);
158 hr
= IReferenceClock_AdvisePeriodic(clock
, -500 * 10000, 1000 * 10000, (HSEMAPHORE
)semaphore
, &cookie
);
159 ok(hr
== E_INVALIDARG
, "Got hr %#x.\n", hr
);
161 hr
= IReferenceClock_AdvisePeriodic(clock
, current
, 500 * 10000, (HSEMAPHORE
)semaphore
, &cookie
);
162 ok(hr
== S_OK
, "Got hr %#x.\n", hr
);
163 ok(!WaitForSingleObject(semaphore
, 20), "Semaphore should be signaled.\n");
164 for (i
= 0; i
< 5; ++i
)
166 ok(WaitForSingleObject(semaphore
, 460) == WAIT_TIMEOUT
, "Semaphore should not be signaled.\n");
167 ok(!WaitForSingleObject(semaphore
, 60), "Semaphore should be signaled.\n");
170 hr
= IReferenceClock_Unadvise(clock
, cookie
);
171 ok(hr
== S_OK
, "Got hr %#x.\n", hr
);
172 ok(WaitForSingleObject(semaphore
, 520) == WAIT_TIMEOUT
, "Semaphore should not be signaled.\n");
175 CloseHandle(semaphore
);
177 ref
= IReferenceClock_Release(clock
);
178 ok(!ref
, "Got outstanding refcount %d.\n", ref
);
181 START_TEST(systemclock
)
185 pGetTickCount64
= (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "GetTickCount64");