quartz: Fix IReferenceClock::GetTime() implementation for system clock.
[wine] / dlls / quartz / tests / referenceclock.c
1 /*
2  * Unit tests for Direct Show functions - IReferenceClock
3  *
4  * Copyright (C) 2007 Alex VillacĂ­s Lasso
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <assert.h>
22
23 #define COBJMACROS
24
25 #include "wine/test.h"
26 #include "uuids.h"
27 #include "dshow.h"
28 #include "control.h"
29
30 /* The following method expects a reference clock that will keep ticking for
31  * at least 5 seconds since its creation. This method assumes no other methods
32  * were called on the IReferenceClock interface since its creation.
33  */
34 static void test_IReferenceClock_methods(const char * clockdesc, IReferenceClock * pClock)
35 {
36     HRESULT hr;
37     REFERENCE_TIME time1;
38     REFERENCE_TIME time2;
39     signed long diff;
40
41     /* Test response from invalid (NULL) argument */
42     hr = IReferenceClock_GetTime(pClock, NULL);
43     ok (hr == E_POINTER, "%s - Expected E_POINTER (0x%08x), got 0x%08x\n", clockdesc, E_POINTER, hr);
44
45     /* Test response for valid value - try 1 */
46     /* TODO: test whether Windows actually returns S_FALSE in its first invocation */
47     time1 = (REFERENCE_TIME)0xdeadbeef;
48     hr = IReferenceClock_GetTime(pClock, &time1);
49     ok (hr == S_FALSE || hr == S_OK, "%s - Expected S_OK or S_FALSE, got 0x%08x\n", clockdesc, hr);
50     ok (time1 != 0xdeadbeef, "%s - value was NOT changed on return!\n", clockdesc);
51
52     /* Test response for valid value - try 2 */
53     time2 = (REFERENCE_TIME)0xdeadbeef;
54     hr = IReferenceClock_GetTime(pClock, &time2);
55     ok (hr == S_FALSE || hr == S_OK, "%s - Expected S_OK or S_FALSE, got 0x%08x\n", clockdesc, hr);
56     ok (time2 != 0xdeadbeef, "%s - value was NOT changed on return!\n", clockdesc);
57
58     /* In case the second invocation managed to return S_FALSE, MSDN says the
59        returned time is the same as the previous one. */
60     ok ((hr != S_FALSE || time1 == time2), "%s - returned S_FALSE, but values not equal!\n", clockdesc);
61
62     time1 = time2;
63     Sleep(1000); /* Sleep for at least 1 second */
64     hr = IReferenceClock_GetTime(pClock, &time2);
65     /* After a 1-second sleep, there is no excuse to get S_FALSE (see TODO above) */
66     ok (hr == S_OK, "%s - Expected S_OK, got 0x%08x\n", clockdesc, hr);
67
68     /* FIXME: How much deviation should be allowed after a sleep? */
69     diff = time2 - time1;
70     ok (9980000 <= diff && diff <= 10020000, "%s - Expected difference around 10000000, got %lu\n", clockdesc, diff);
71
72 }
73
74 static void test_IReferenceClock_SystemClock(void)
75 {
76     IReferenceClock * pReferenceClock;
77     HRESULT hr;
78
79     hr = CoCreateInstance(&CLSID_SystemClock, NULL, CLSCTX_INPROC_SERVER, &IID_IReferenceClock, (LPVOID*)&pReferenceClock);
80     ok(hr == S_OK, "Unable to create reference clock from system clock %x\n", hr);
81     if (hr == S_OK)
82     {
83         test_IReferenceClock_methods("SystemClock", pReferenceClock);
84         IReferenceClock_Release(pReferenceClock);
85     }
86 }
87
88 START_TEST(referenceclock)
89 {
90     CoInitialize(NULL);
91
92     test_IReferenceClock_SystemClock();
93 }