wmp: Add seeking and duration.
[wine.git] / dlls / wmp / tests / media.c
blob2372763d25428d0b2ae70b476c902c525d54fd72
1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 #define WIN32_LEAN_AND_MEAN
18 #define COBJMACROS
19 #include <wmp.h>
20 #include <olectl.h>
21 #include <nserror.h>
22 #include <wmpids.h>
23 #include <math.h>
24 #include <assert.h>
26 #include "wine/test.h"
28 #define DEFINE_EXPECT(kind) \
29 static DWORD expect_ ## kind = 0, called_ ## kind = 0
31 #define SET_EXPECT(kind, index) \
32 do { \
33 assert(index < 8 * sizeof(expect_ ## kind)); \
34 expect_ ## kind |= (1 << index); \
35 }while(0)
37 #define CHECK_EXPECT(kind, index) \
38 do { \
39 ok(expect_ ##kind & (1 << index), "unexpected event for " #kind ", index:%d\n", index); \
40 called_ ## kind |= (1 << index); \
41 }while(0)
43 #define CLEAR_CALLED(kind, index) \
44 do { \
45 expect_ ## kind &= ~(1 << index); \
46 called_ ## kind &= ~(1 << index); \
47 }while(0)
49 #define CHECK_CALLED(kind, index) \
50 do { \
51 ok(called_ ## kind & (1 << index), "expected " #kind ", %d\n", index); \
52 expect_ ## kind &= ~(1 << index); \
53 called_ ## kind &= ~(1 << index); \
54 }while(0)
56 #define CHECK_NOT_CALLED(kind, index) \
57 do { \
58 ok(!(called_ ## kind & (1 << index)), "not expected " #kind ", %d\n", index); \
59 expect_ ## kind &= ~(1 << index); \
60 called_ ## kind &= ~(1 << index); \
61 }while(0)
63 DEFINE_EXPECT(PLAYSTATE);
64 DEFINE_EXPECT(OPENSTATE);
66 static HANDLE playing_event;
67 static HANDLE completed_event;
68 static DWORD main_thread_id;
70 static const WCHAR mp3file[] = {'t','e','s','t','.','m','p','3',0};
71 static const WCHAR mp3file1s[] = {'t','e','s','t','1','s','.','m','p','3',0};
72 static inline WCHAR *load_resource(const WCHAR *name)
74 static WCHAR pathW[MAX_PATH];
75 DWORD written;
76 HANDLE file;
77 HRSRC res;
78 void *ptr;
80 GetTempPathW(sizeof(pathW)/sizeof(WCHAR), pathW);
81 lstrcatW(pathW, name);
83 file = CreateFileW(pathW, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
84 ok(file != INVALID_HANDLE_VALUE, "file creation failed, at %s, error %d\n", wine_dbgstr_w(pathW),
85 GetLastError());
87 res = FindResourceW(NULL, name, (LPCWSTR)RT_RCDATA);
88 ok( res != 0, "couldn't find resource\n" );
89 ptr = LockResource( LoadResource( GetModuleHandleA(NULL), res ));
90 WriteFile( file, ptr, SizeofResource( GetModuleHandleA(NULL), res ), &written, NULL );
91 ok( written == SizeofResource( GetModuleHandleA(NULL), res ), "couldn't write resource\n" );
92 CloseHandle( file );
94 return pathW;
97 static ULONG WINAPI Dispatch_AddRef(IDispatch *iface)
99 return 2;
102 static ULONG WINAPI Dispatch_Release(IDispatch *iface)
104 return 1;
107 static HRESULT WINAPI Dispatch_GetTypeInfoCount(IDispatch *iface, UINT *pctinfo)
109 ok(0, "unexpected call\n");
110 return E_NOTIMPL;
113 static HRESULT WINAPI Dispatch_GetTypeInfo(IDispatch *iface, UINT iTInfo, LCID lcid,
114 ITypeInfo **ppTInfo)
116 ok(0, "unexpected call\n");
117 return E_NOTIMPL;
120 static HRESULT WINAPI Dispatch_GetIDsOfNames(IDispatch *iface, REFIID riid, LPOLESTR *rgszNames,
121 UINT cNames, LCID lcid, DISPID *rgDispId)
123 ok(0, "unexpected call\n");
124 return E_NOTIMPL;
127 static HRESULT WINAPI WMPOCXEvents_QueryInterface(IDispatch *iface, REFIID riid, void **ppv)
129 *ppv = NULL;
131 if(IsEqualGUID(&IID__WMPOCXEvents, riid) || IsEqualGUID(&IID_IDispatch, riid)) {
132 *ppv = iface;
133 return S_OK;
136 ok(0, "unexpected riid %s\n", wine_dbgstr_guid(riid));
137 return E_NOINTERFACE;
140 static HRESULT WINAPI WMPOCXEvents_Invoke(IDispatch *iface, DISPID dispIdMember, REFIID riid,
141 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
142 EXCEPINFO *pExcepInfo, UINT *puArgErr)
144 ok(main_thread_id == GetCurrentThreadId(), "Got notification outside of main thread!\n");
145 switch(dispIdMember) {
146 case DISPID_WMPCOREEVENT_OPENSTATECHANGE:
147 CHECK_EXPECT(OPENSTATE, V_UI4(pDispParams->rgvarg));
148 if (winetest_debug > 1)
149 trace("DISPID_WMPCOREEVENT_OPENSTATECHANGE, %d\n", V_UI4(pDispParams->rgvarg));
150 break;
151 case DISPID_WMPCOREEVENT_PLAYSTATECHANGE:
152 CHECK_EXPECT(PLAYSTATE, V_UI4(pDispParams->rgvarg));
153 if (V_UI4(pDispParams->rgvarg) == wmppsPlaying) {
154 SetEvent(playing_event);
155 } else if (V_UI4(pDispParams->rgvarg) == wmppsMediaEnded) {
156 SetEvent(completed_event);
158 if (winetest_debug > 1)
159 trace("DISPID_WMPCOREEVENT_PLAYSTATECHANGE, %d\n", V_UI4(pDispParams->rgvarg));
160 break;
161 case DISPID_WMPCOREEVENT_MEDIACHANGE:
162 if (winetest_debug > 1)
163 trace("DISPID_WMPCOREEVENT_MEDIACHANGE\n");
164 break;
165 case DISPID_WMPCOREEVENT_CURRENTITEMCHANGE:
166 if (winetest_debug > 1)
167 trace("DISPID_WMPCOREEVENT_CURRENTITEMCHANGE\n");
168 break;
169 case DISPID_WMPCOREEVENT_STATUSCHANGE:
170 if (winetest_debug > 1)
171 trace("DISPID_WMPCOREEVENT_STATUSCHANGE\n");
172 break;
173 default:
174 if (winetest_debug > 1)
175 trace("event: %d\n", dispIdMember);
176 break;
179 return E_NOTIMPL;
182 static IDispatchVtbl WMPOcxEventsVtbl = {
183 WMPOCXEvents_QueryInterface,
184 Dispatch_AddRef,
185 Dispatch_Release,
186 Dispatch_GetTypeInfoCount,
187 Dispatch_GetTypeInfo,
188 Dispatch_GetIDsOfNames,
189 WMPOCXEvents_Invoke,
192 static IDispatch WMPOCXEvents = { &WMPOcxEventsVtbl };
194 static HRESULT pump_messages(DWORD timeout, DWORD count, const HANDLE *handles) {
195 MSG msg;
196 HRESULT res;
197 DWORD start_time = GetTickCount();
198 do {
199 DWORD now = GetTickCount();
200 res = MsgWaitForMultipleObjectsEx(count, handles, start_time + timeout - now,
201 QS_ALLINPUT ,MWMO_ALERTABLE | MWMO_INPUTAVAILABLE);
202 if (res == WAIT_OBJECT_0 + 1) {
203 GetMessageW(&msg, 0, 0, 0);
204 if (winetest_debug > 1)
205 trace("Dispatching %d\n", msg.message);
206 TranslateMessage(&msg);
207 DispatchMessageW(&msg);
210 while (res == WAIT_OBJECT_0 + 1);
211 return res;
214 static void test_completion_event(void)
216 DWORD res = 0;
217 IWMPPlayer4 *player4;
218 HRESULT hres;
219 BSTR filename;
220 IConnectionPointContainer *container;
221 IConnectionPoint *point;
222 IOleObject *oleobj;
223 static DWORD dw = 100;
225 hres = CoCreateInstance(&CLSID_WindowsMediaPlayer, NULL, CLSCTX_INPROC_SERVER, &IID_IOleObject, (void**)&oleobj);
226 if(hres == REGDB_E_CLASSNOTREG) {
227 win_skip("CLSID_WindowsMediaPlayer not registered\n");
228 return;
230 ok(hres == S_OK, "Could not create CLSID_WindowsMediaPlayer instance: %08x\n", hres);
232 hres = IOleObject_QueryInterface(oleobj, &IID_IConnectionPointContainer, (void**)&container);
233 ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
234 if(FAILED(hres))
235 return;
237 hres = IConnectionPointContainer_FindConnectionPoint(container, &IID__WMPOCXEvents, &point);
238 IConnectionPointContainer_Release(container);
239 ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
241 hres = IConnectionPoint_Advise(point, (IUnknown*)&WMPOCXEvents, &dw);
242 ok(hres == S_OK, "Advise failed: %08x\n", hres);
244 hres = IOleObject_QueryInterface(oleobj, &IID_IWMPPlayer4, (void**)&player4);
245 ok(hres == S_OK, "Could not get IWMPPlayer4 iface: %08x\n", hres);
247 filename = SysAllocString(load_resource(mp3file1s));
249 SET_EXPECT(OPENSTATE, wmposPlaylistChanging);
250 SET_EXPECT(OPENSTATE, wmposPlaylistOpenNoMedia);
251 SET_EXPECT(OPENSTATE, wmposPlaylistChanged);
252 SET_EXPECT(OPENSTATE, wmposOpeningUnknownURL);
253 SET_EXPECT(OPENSTATE, wmposMediaOpen);
254 SET_EXPECT(OPENSTATE, wmposMediaOpening);
255 SET_EXPECT(PLAYSTATE, wmppsPlaying);
256 SET_EXPECT(PLAYSTATE, wmppsMediaEnded);
257 SET_EXPECT(PLAYSTATE, wmppsStopped);
258 SET_EXPECT(PLAYSTATE, wmppsTransitioning);
259 /* following two are sent on vistau64 vms only */
260 SET_EXPECT(OPENSTATE, wmposMediaChanging);
261 SET_EXPECT(PLAYSTATE, wmppsReady);
262 hres = IWMPPlayer4_put_URL(player4, filename);
263 ok(hres == S_OK, "IWMPPlayer4_put_URL failed: %08x\n", hres);
264 res = pump_messages(3000, 1, &completed_event);
265 ok(res == WAIT_OBJECT_0, "Timed out while waiting for media to complete\n");
267 /* following two are sent on vistau64 vms only */
268 CLEAR_CALLED(OPENSTATE, wmposMediaChanging);
269 CLEAR_CALLED(PLAYSTATE, wmppsReady);
271 CHECK_CALLED(OPENSTATE, wmposPlaylistChanging);
272 CHECK_CALLED(OPENSTATE, wmposPlaylistChanged);
273 CHECK_CALLED(OPENSTATE, wmposPlaylistOpenNoMedia);
274 CHECK_CALLED(PLAYSTATE, wmppsTransitioning);
275 CHECK_CALLED(OPENSTATE, wmposOpeningUnknownURL);
276 CHECK_CALLED(OPENSTATE, wmposMediaOpen);
277 CHECK_CALLED(PLAYSTATE, wmppsPlaying);
278 CHECK_CALLED(PLAYSTATE, wmppsMediaEnded);
279 CHECK_CALLED(PLAYSTATE, wmppsStopped);
280 /* MediaOpening happens only on xp, 2003 */
281 CLEAR_CALLED(OPENSTATE, wmposMediaOpening);
283 hres = IConnectionPoint_Unadvise(point, dw);
284 ok(hres == S_OK, "Unadvise failed: %08x\n", hres);
286 IConnectionPoint_Release(point);
287 IWMPPlayer4_Release(player4);
288 IOleObject_Release(oleobj);
289 DeleteFileW(filename);
290 SysFreeString(filename);
293 static BOOL test_wmp(void)
295 DWORD res = 0;
296 IWMPPlayer4 *player4;
297 IWMPControls *controls;
298 HRESULT hres;
299 BSTR filename;
300 IConnectionPointContainer *container;
301 IConnectionPoint *point;
302 IOleObject *oleobj;
303 static DWORD dw = 100;
304 IWMPSettings *settings;
305 BOOL test_ran = TRUE;
306 DOUBLE duration;
307 VARIANT_BOOL vbool;
308 IWMPMedia *media;
309 static const WCHAR currentPosition[] = {'c','u','r','r','e','n','t','P','o','s','i','t','i','o','n',0};
310 BSTR bstrcurrentPosition = SysAllocString(currentPosition);
312 hres = CoCreateInstance(&CLSID_WindowsMediaPlayer, NULL, CLSCTX_INPROC_SERVER, &IID_IOleObject, (void**)&oleobj);
313 if(hres == REGDB_E_CLASSNOTREG) {
314 win_skip("CLSID_WindowsMediaPlayer not registered\n");
315 return FALSE;
317 ok(hres == S_OK, "Could not create CLSID_WindowsMediaPlayer instance: %08x\n", hres);
319 hres = IOleObject_QueryInterface(oleobj, &IID_IConnectionPointContainer, (void**)&container);
320 ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
322 hres = IConnectionPointContainer_FindConnectionPoint(container, &IID__WMPOCXEvents, &point);
323 IConnectionPointContainer_Release(container);
324 ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
326 hres = IConnectionPoint_Advise(point, (IUnknown*)&WMPOCXEvents, &dw);
327 ok(hres == S_OK, "Advise failed: %08x\n", hres);
329 hres = IOleObject_QueryInterface(oleobj, &IID_IWMPPlayer4, (void**)&player4);
330 ok(hres == S_OK, "Could not get IWMPPlayer4 iface: %08x\n", hres);
332 settings = NULL;
333 hres = IWMPPlayer4_get_settings(player4, &settings);
334 ok(hres == S_OK, "get_settings failed: %08x\n", hres);
335 ok(settings != NULL, "settings = NULL\n");
337 hres = IWMPSettings_put_autoStart(settings, VARIANT_FALSE);
338 ok(hres == S_OK, "Could not put autoStart in IWMPSettings: %08x\n", hres);
339 IWMPSettings_Release(settings);
341 controls = NULL;
342 hres = IWMPPlayer4_get_controls(player4, &controls);
343 ok(hres == S_OK, "get_controls failed: %08x\n", hres);
344 ok(controls != NULL, "controls = NULL\n");
346 hres = IWMPControls_get_isAvailable(controls, bstrcurrentPosition, &vbool);
347 ok(hres == S_OK, "IWMPControls_get_isAvailable failed: %08x\n", hres);
348 ok(vbool == VARIANT_FALSE, "unexpected value\n");
350 hres = IWMPControls_play(controls);
351 ok(hres == NS_S_WMPCORE_COMMAND_NOT_AVAILABLE, "IWMPControls_play is available: %08x\n", hres);
353 filename = SysAllocString(load_resource(mp3file));
355 SET_EXPECT(OPENSTATE, wmposPlaylistChanging);
356 SET_EXPECT(OPENSTATE, wmposPlaylistOpenNoMedia);
357 SET_EXPECT(OPENSTATE, wmposPlaylistChanged);
358 SET_EXPECT(PLAYSTATE, wmppsTransitioning);
359 SET_EXPECT(PLAYSTATE, wmppsReady);
360 hres = IWMPPlayer4_put_URL(player4, filename);
361 ok(hres == S_OK, "IWMPPlayer4_put_URL failed: %08x\n", hres);
362 CHECK_CALLED(OPENSTATE, wmposPlaylistChanging);
363 CHECK_CALLED(OPENSTATE, wmposPlaylistChanged);
364 CHECK_CALLED(OPENSTATE, wmposPlaylistOpenNoMedia);
365 CHECK_CALLED(PLAYSTATE, wmppsTransitioning);
366 CHECK_CALLED(PLAYSTATE, wmppsReady);
368 SET_EXPECT(OPENSTATE, wmposOpeningUnknownURL);
369 SET_EXPECT(OPENSTATE, wmposMediaOpen);
370 SET_EXPECT(PLAYSTATE, wmppsPlaying);
371 SET_EXPECT(PLAYSTATE, wmppsTransitioning);
372 /* MediaOpening happens only on xp, 2003 */
373 SET_EXPECT(OPENSTATE, wmposMediaOpening);
374 hres = IWMPControls_play(controls);
375 ok(hres == S_OK, "IWMPControls_play failed: %08x\n", hres);
376 res = pump_messages(1000, 1, &playing_event);
377 ok(res == WAIT_OBJECT_0 || broken(res == WAIT_TIMEOUT), "Timed out while waiting for media to become ready\n");
378 if (res == WAIT_TIMEOUT) {
379 /* This happens on Vista Ultimate 64 vms
380 * I have been unable to find out source of this behaviour */
381 win_skip("Failed to transition media to playing state.\n");
382 test_ran = FALSE;
383 goto playback_skip;
385 CHECK_CALLED(OPENSTATE, wmposOpeningUnknownURL);
386 CHECK_CALLED(OPENSTATE, wmposMediaOpen);
387 CHECK_CALLED(PLAYSTATE, wmppsPlaying);
388 CHECK_CALLED(PLAYSTATE, wmppsTransitioning);
389 /* MediaOpening happens only on xp, 2003 */
390 CLEAR_CALLED(OPENSTATE, wmposMediaOpening);
392 hres = IWMPControls_get_isAvailable(controls, bstrcurrentPosition, &vbool);
393 ok(hres == S_OK, "IWMPControls_get_isAvailable failed: %08x\n", hres);
394 ok(vbool == VARIANT_TRUE, "unexpected value\n");
396 duration = 0.0;
397 hres = IWMPControls_get_currentPosition(controls, &duration);
398 ok(hres == S_OK, "IWMPControls_get_currentPosition failed: %08x\n", hres);
399 ok((int)duration == 0, "unexpected value %f\n", duration);
401 duration = 1.1;
402 hres = IWMPControls_put_currentPosition(controls, duration);
403 ok(hres == S_OK, "IWMPControls_put_currentPosition failed: %08x\n", hres);
405 duration = 0.0;
406 hres = IWMPControls_get_currentPosition(controls, &duration);
407 ok(hres == S_OK, "IWMPControls_get_currentPosition failed: %08x\n", hres);
408 /* builtin quartz does not handle this currently and resets to 0.0, works
409 * with native quartz */
410 todo_wine ok(duration >= 1.05 /* save some fp errors */, "unexpected value %f\n", duration);
412 hres = IWMPPlayer4_get_currentMedia(player4, &media);
413 ok(hres == S_OK, "IWMPPlayer4_get_currentMedia failed: %08x\n", hres);
414 hres = IWMPMedia_get_duration(media, &duration);
415 ok(hres == S_OK, "IWMPMedia_get_duration failed: %08x\n", hres);
416 ok(round(duration) == 3, "unexpected value: %f\n", duration);
417 IWMPMedia_Release(media);
419 SET_EXPECT(PLAYSTATE, wmppsStopped);
420 /* The following happens on wine only since we close media on stop */
421 SET_EXPECT(OPENSTATE, wmposPlaylistOpenNoMedia);
422 hres = IWMPControls_stop(controls);
423 ok(hres == S_OK, "IWMPControls_stop failed: %08x\n", hres);
424 CHECK_CALLED(PLAYSTATE, wmppsStopped);
425 todo_wine CHECK_NOT_CALLED(OPENSTATE, wmposPlaylistOpenNoMedia);
427 /* Already Stopped */
428 hres = IWMPControls_stop(controls);
429 ok(hres == NS_S_WMPCORE_COMMAND_NOT_AVAILABLE, "IWMPControls_stop is available: %08x\n", hres);
431 SET_EXPECT(PLAYSTATE, wmppsPlaying);
432 /* The following happens on wine only since we close media on stop */
433 SET_EXPECT(OPENSTATE, wmposOpeningUnknownURL);
434 SET_EXPECT(OPENSTATE, wmposMediaOpen);
435 SET_EXPECT(PLAYSTATE, wmppsTransitioning);
436 hres = IWMPControls_play(controls);
437 ok(hres == S_OK, "IWMPControls_play failed: %08x\n", hres);
438 CHECK_CALLED(PLAYSTATE, wmppsPlaying);
439 todo_wine CHECK_NOT_CALLED(OPENSTATE, wmposOpeningUnknownURL);
440 todo_wine CHECK_NOT_CALLED(OPENSTATE, wmposMediaOpen);
441 todo_wine CHECK_NOT_CALLED(PLAYSTATE, wmppsTransitioning);
443 playback_skip:
444 hres = IConnectionPoint_Unadvise(point, dw);
445 ok(hres == S_OK, "Unadvise failed: %08x\n", hres);
447 IConnectionPoint_Release(point);
448 IWMPControls_Release(controls);
449 IWMPPlayer4_Release(player4);
450 IOleObject_Release(oleobj);
451 DeleteFileW(filename);
452 SysFreeString(filename);
453 SysFreeString(bstrcurrentPosition);
455 return test_ran;
458 START_TEST(media)
460 CoInitialize(NULL);
462 main_thread_id = GetCurrentThreadId();
463 playing_event = CreateEventW(NULL, FALSE, FALSE, NULL);
464 completed_event = CreateEventW(NULL, FALSE, FALSE, NULL);
465 if (test_wmp()) {
466 test_completion_event();
467 } else {
468 win_skip("Failed to play media\n");
471 CloseHandle(playing_event);
472 CloseHandle(completed_event);
474 CoUninitialize();