]> git.sesse.net Git - vlc/blob - src/control/testapi.c
new (failing) test for libvlc
[vlc] / src / control / testapi.c
1 /*
2  * testapi.c - libvlc smoke test
3  *
4  * $Id$
5  */
6
7 /**********************************************************************
8  *  Copyright (C) 2007 RĂ©mi Denis-Courmont.                           *
9  *  This program is free software; you can redistribute and/or modify *
10  *  it under the terms of the GNU General Public License as published *
11  *  by the Free Software Foundation; version 2 of the license, or (at *
12  *  your option) any later version.                                   *
13  *                                                                    *
14  *  This program is distributed in the hope that it will be useful,   *
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of    *
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              *
17  *  See the GNU General Public License for more details.              *
18  *                                                                    *
19  *  You should have received a copy of the GNU General Public License *
20  *  along with this program; if not, you can get it from:             *
21  *  http://www.gnu.org/copyleft/gpl.html                              *
22  **********************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <vlc/libvlc.h>
28
29 #undef NDEBUG
30 #include <assert.h>
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35
36 static libvlc_exception_t ex;
37
38 #define log( ... ) printf( "testapi: " __VA_ARGS__ );
39
40 /* test if we have exception */
41 static bool have_exception (void)
42 {
43     if (libvlc_exception_raised (&ex))
44     {
45         libvlc_exception_clear (&ex);
46         return true;
47     }
48     else
49         return false;
50 }
51
52 static void catch (void)
53 {
54     if (libvlc_exception_raised (&ex))
55     {
56          fprintf (stderr, "Exception: %s\n",
57                   libvlc_exception_get_message (&ex));
58          abort ();
59     }
60
61     assert (libvlc_exception_get_message (&ex) == NULL);
62     libvlc_exception_clear (&ex);
63 }
64
65 /* Test we have */
66 static void test_core (const char ** argv, int argc);
67 static void test_media_list (const char ** argv, int argc);
68 static void test_events (const char ** argv, int argc);
69 static void test_media_player_play_stop(const char** argv, int argc);
70 static void test_media_player_pause_stop(const char** argv, int argc);
71 static void test_media_list_player_pause_stop(const char** argv, int argc);
72
73 /* Tests implementations */
74 static void test_core (const char ** argv, int argc)
75 {
76     libvlc_instance_t *vlc;
77     int id;
78
79     log ("Testing core\n");
80
81     libvlc_exception_init (&ex);
82     vlc = libvlc_new (argc, argv, &ex);
83     catch ();
84
85     libvlc_playlist_clear (vlc, &ex);
86     catch ();
87
88     id = libvlc_playlist_add_extended (vlc, "/dev/null", "Test", 0, NULL,
89                                        &ex);
90     catch ();
91
92     libvlc_playlist_clear (vlc, &ex);
93     catch ();
94
95     libvlc_retain (vlc);
96     libvlc_release (vlc);
97     libvlc_release (vlc);
98 }
99
100 static void test_media_list (const char ** argv, int argc)
101 {
102     libvlc_instance_t *vlc;
103     libvlc_media_t *md1, *md2, *md3, *md4;
104     libvlc_media_list_t *ml;
105
106     log ("Testing media_list\n");
107
108     libvlc_exception_init (&ex);
109     vlc = libvlc_new (argc, argv, &ex);
110     catch ();
111
112     ml = libvlc_media_list_new (vlc, &ex);
113     catch ();
114
115     md1 = libvlc_media_new (vlc, "/dev/null", &ex);
116     catch ();
117     md2 = libvlc_media_new (vlc, "/dev/null", &ex);
118     catch ();
119     md3 = libvlc_media_new (vlc, "/dev/null", &ex);
120     catch ();
121
122     libvlc_media_list_add_media (ml, md1, &ex);
123     catch ();
124     libvlc_media_list_add_media (ml, md2, &ex);
125     catch ();
126
127     assert( libvlc_media_list_count (ml, &ex) == 2 );
128     catch ();
129
130     assert( libvlc_media_list_index_of_item (ml, md1, &ex) == 0 );
131     catch ();
132
133     assert( libvlc_media_list_index_of_item (ml, md2, &ex) == 1 );
134     catch ();
135
136     libvlc_media_list_remove_index (ml, 0, &ex);  /* removing first item */
137     catch ();
138
139     /* test if second item was moved on first place */
140     assert( libvlc_media_list_index_of_item (ml, md2, &ex) == 0 );
141     catch ();
142
143     libvlc_media_list_add_media (ml, md1, &ex); /* add 2 items */
144     catch ();
145     libvlc_media_list_add_media (ml, md1, &ex);
146     catch ();
147
148     /* there should be 3 pieces */
149     assert( libvlc_media_list_count (ml, &ex) == 3 );
150     catch ();
151
152     libvlc_media_list_insert_media (ml, md3, 2, &ex);
153     catch ();
154
155     /* there should be 4 pieces */
156     assert( libvlc_media_list_count (ml, &ex) == 4 );
157     catch ();
158
159     /* test inserting on right place */
160     assert( libvlc_media_list_index_of_item (ml, md3, &ex) == 2 );
161     catch ();
162
163     /* test right returning descriptor*/
164     assert ( libvlc_media_list_item_at_index (ml, 0, &ex) == md2 );
165     catch ();
166
167     assert ( libvlc_media_list_item_at_index (ml, 2, &ex) == md3 );
168     catch ();
169
170     /* test if give exceptions, when it should */
171     /* have 4 items, so index 4 should give exception */
172     libvlc_media_list_remove_index (ml, 4, &ex);
173     assert (have_exception ());
174
175     libvlc_media_list_remove_index (ml, 100, &ex);
176     assert (have_exception ());
177
178     libvlc_media_list_remove_index (ml, -1, &ex);
179     assert (have_exception ());
180
181     /* getting non valid items */
182     libvlc_media_t * p_non_exist =
183         libvlc_media_list_item_at_index (ml, 4, &ex);
184     assert (have_exception ());
185
186     p_non_exist = libvlc_media_list_item_at_index (ml, 100, &ex);
187     assert (have_exception ());
188
189     p_non_exist = libvlc_media_list_item_at_index (ml, -1, &ex);
190     assert (have_exception ());
191
192     md4 = libvlc_media_new (vlc, "/dev/dsp", &ex);
193     catch ();
194
195     /* try to find non inserted item */
196     int i_non_exist = 0;
197     i_non_exist = libvlc_media_list_index_of_item (ml, md4, &ex);
198     assert ( i_non_exist == -1 );
199
200     libvlc_media_release (md1);
201     libvlc_media_release (md2);
202     libvlc_media_release (md3);
203     libvlc_media_release (md4);
204
205     libvlc_media_list_release (ml);
206
207     libvlc_release (vlc);
208     catch ();
209 }
210
211 /* This one is an internal API. We use it here to run tests that
212  * don't depends on playback, and only test the event framework */
213 extern void libvlc_event_send( libvlc_event_manager_t *, libvlc_event_t *);
214
215 static void test_events_dummy_callback( const libvlc_event_t * event, void * user_data)
216 {
217     vlc_bool_t * callback_was_called = user_data;
218     *callback_was_called = VLC_TRUE;
219 }
220
221 static void test_events_callback_and_detach( const libvlc_event_t * event, void * user_data)
222 {
223     vlc_bool_t * callback_was_called = user_data;
224     libvlc_event_manager_t *em;
225
226     em = libvlc_media_player_event_manager (event->p_obj, &ex);
227     catch();
228
229     libvlc_event_detach (em, event->type, test_events_callback_and_detach, user_data, &ex);
230     *callback_was_called = VLC_TRUE;
231 }
232
233 static void test_event_type_reception( libvlc_event_manager_t * em, libvlc_event_type_t event_type, vlc_bool_t * callback_was_called )
234 {
235     libvlc_event_t event;
236     event.type = event_type;
237     *callback_was_called = VLC_FALSE;
238     libvlc_event_send (em, &event);
239     assert (*callback_was_called);
240 }
241
242 static void test_events (const char ** argv, int argc)
243 {
244     libvlc_instance_t *vlc;
245     libvlc_media_player_t *mi;
246     libvlc_event_manager_t *em;
247     vlc_bool_t callback_was_called;
248     libvlc_exception_t ex;
249     libvlc_event_type_t mi_events[] = {
250         libvlc_MediaPlayerPlayed,
251         libvlc_MediaPlayerPaused,
252         libvlc_MediaPlayerEndReached,
253         libvlc_MediaPlayerStopped,
254         libvlc_MediaPlayerEncounteredError,
255         libvlc_MediaPlayerTimeChanged,
256         libvlc_MediaPlayerPositionChanged,
257     };
258     int i, mi_events_len = sizeof(mi_events)/sizeof(*mi_events);
259     
260     log ("Testing events\n");
261
262     libvlc_exception_init (&ex);
263     vlc = libvlc_new (argc, argv, &ex);
264     catch ();
265
266     mi = libvlc_media_player_new (vlc, &ex);
267     catch ();
268
269     em = libvlc_media_player_event_manager (mi, &ex);
270
271     log ("+ Testing attaching to Media Instance\n");
272
273     for (i = 0; i < mi_events_len; i++) {
274         libvlc_event_attach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
275         catch ();
276     }
277
278     log ("+ Testing event reception\n");
279
280     for (i = 0; i < mi_events_len; i++)
281         test_event_type_reception (em, mi_events[i], &callback_was_called);
282
283     log ("+ Testing event detaching while in the event callback\n");
284
285     libvlc_event_t event;
286     event.type = mi_events[mi_events_len-1];
287     callback_was_called = VLC_FALSE;
288
289     libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_dummy_callback, &callback_was_called, &ex);
290     catch ();
291
292     libvlc_event_attach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
293     catch ();
294
295     libvlc_event_send (em, &event);
296     assert( callback_was_called );
297
298     callback_was_called = VLC_FALSE;
299     libvlc_event_send (em, &event);
300     assert( !callback_was_called );
301
302     libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
303     catch ();
304
305     log ("+ Testing regular detach()\n");
306
307     for (i = 0; i < mi_events_len - 1; i++) {
308         libvlc_event_detach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
309         catch ();
310     }
311
312     libvlc_media_player_release (mi);
313     catch ();
314
315     libvlc_release (vlc);
316     catch ();
317 }
318
319 static void test_media_player_play_stop(const char** argv, int argc)
320 {
321 #if 0
322     libvlc_instance_t *vlc;
323     libvlc_media_t *md;
324     libvlc_media_player_t *mi;
325     const char * file = "file://../bindings/java/core/src/test/resources/raffa_voice.ogg";
326
327     log ("Testing play and pause of %s\n", file);
328
329     libvlc_exception_init (&ex);
330     vlc = libvlc_new (argc, argv, &ex);
331     catch ();
332
333     md = libvlc_media_new (vlc, file, &ex);
334     catch ();
335
336     mi = libvlc_media_player_new_from_media (md, &ex);
337     catch ();
338     
339     libvlc_media_release (md);
340
341     libvlc_media_player_play (mi, &ex);
342     catch ();
343
344     /* FIXME: Do something clever */
345     sleep(1);
346
347     assert( libvlc_media_player_get_state (mi, &ex) != libvlc_Error );
348     catch ();
349
350     libvlc_media_player_stop (mi, &ex);
351     catch ();
352
353     libvlc_media_player_release (mi);
354     catch ();
355
356     libvlc_release (vlc);
357     catch ();
358 #endif
359 }
360
361 static void test_media_player_pause_stop(const char** argv, int argc)
362 {
363     libvlc_instance_t *vlc;
364     libvlc_media_t *md;
365     libvlc_media_player_t *mi;
366     const char * file = "file://../bindings/java/core/src/test/resources/raffa_voice.ogg";
367
368     log ("Testing play and pause of %s\n", file);
369
370     libvlc_exception_init (&ex);
371     vlc = libvlc_new (argc, argv, &ex);
372     catch ();
373
374     md = libvlc_media_new (vlc, file, &ex);
375     catch ();
376
377     mi = libvlc_media_player_new_from_media (md, &ex);
378     catch ();
379     
380     libvlc_media_release (md);
381
382     libvlc_media_player_play (mi, &ex);
383     catch ();
384
385     /* FIXME: Do something clever */
386     sleep(1);
387
388     assert( libvlc_media_player_get_state (mi, &ex) == libvlc_Playing );
389     catch ();
390
391     libvlc_media_player_pause (mi, &ex);
392     assert( libvlc_media_player_get_state (mi, &ex) == libvlc_Paused );
393     catch();
394
395     libvlc_media_player_stop (mi, &ex);
396     catch ();
397
398     libvlc_media_player_release (mi);
399     catch ();
400
401     libvlc_release (vlc);
402     catch ();
403 }
404
405 static void test_media_list_player_pause_stop(const char** argv, int argc)
406 {
407     libvlc_instance_t *vlc;
408     libvlc_media_t *md;
409     libvlc_media_player_t *mi;
410     libvlc_media_list_t *ml;
411     libvlc_media_list_player_t *mlp;
412     
413     const char * file = "file://../bindings/java/core/src/test/resources/raffa_voice.ogg";
414
415     log ("Testing play and pause of %s using the media list.\n", file);
416
417     libvlc_exception_init (&ex);
418     vlc = libvlc_new (argc, argv, &ex);
419     catch ();
420
421     md = libvlc_media_new (vlc, file, &ex);
422     catch ();
423
424     ml = libvlc_media_list_new (vlc, &ex);
425     catch ();
426     
427     mlp = libvlc_media_list_player_new (vlc, &ex);
428
429     libvlc_media_list_add_media( ml, md, &ex );
430     catch ();
431
432     libvlc_media_list_player_set_media_list( mlp, ml, &ex );
433
434     libvlc_media_list_player_play_item( mlp, md, &ex );
435
436     /* FIXME: Do something clever */
437     sleep(1);
438
439     assert( libvlc_media_player_get_state (mi, &ex) == libvlc_Playing );
440     catch ();
441
442     libvlc_media_player_pause (mi, &ex);
443     assert( libvlc_media_player_get_state (mi, &ex) == libvlc_Paused );
444     catch();
445
446     libvlc_media_player_stop (mi, &ex);
447     catch ();
448
449     libvlc_media_player_release (mi);
450     catch ();
451
452     libvlc_release (vlc);
453     catch ();
454 }
455
456
457
458 int main (int argc, char *argv[])
459 {
460     const char *args[argc + 5];
461     int nlibvlc_args = sizeof (args) / sizeof (args[0]);
462
463     alarm (50); /* Make sure "make check" does not get stuck */
464
465     args[0] = "-vvv";
466     args[1] = "-I";
467     args[2] = "dummy";
468     args[3] = "--plugin-path=../modules";
469     args[4] = "--vout=dummy";
470     args[5] = "--aout=dummy";
471     for (int i = 1; i < argc; i++)
472         args[i + 3] = argv[i];
473
474     test_core (args, nlibvlc_args);
475
476     test_events (args, nlibvlc_args);
477
478     test_media_list (args, nlibvlc_args);
479
480     test_media_player_play_stop(args, nlibvlc_args);
481
482     test_media_player_pause_stop(args, nlibvlc_args);
483
484     test_media_list_player_pause_stop(args, nlibvlc_args);
485     
486     return 0;
487 }