]> git.sesse.net Git - vlc/blob - src/control/testapi.c
416e5c8404312f12568d68f1dfa8f7947b422baf
[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
71 /* Tests implementations */
72 static void test_core (const char ** argv, int argc)
73 {
74     libvlc_instance_t *vlc;
75     int id;
76
77     log ("Testing core\n");
78
79     libvlc_exception_init (&ex);
80     vlc = libvlc_new (argc, argv, &ex);
81     catch ();
82
83     libvlc_playlist_clear (vlc, &ex);
84     catch ();
85
86     id = libvlc_playlist_add_extended (vlc, "/dev/null", "Test", 0, NULL,
87                                        &ex);
88     catch ();
89
90     libvlc_playlist_clear (vlc, &ex);
91     catch ();
92
93     libvlc_retain (vlc);
94     libvlc_release (vlc);
95     libvlc_release (vlc);
96 }
97
98 static void test_media_list (const char ** argv, int argc)
99 {
100     libvlc_instance_t *vlc;
101     libvlc_media_t *md1, *md2, *md3, *md4;
102     libvlc_media_list_t *ml;
103
104     log ("Testing media_list\n");
105
106     libvlc_exception_init (&ex);
107     vlc = libvlc_new (argc, argv, &ex);
108     catch ();
109
110     ml = libvlc_media_list_new (vlc, &ex);
111     catch ();
112
113     md1 = libvlc_media_new (vlc, "/dev/null", &ex);
114     catch ();
115     md2 = libvlc_media_new (vlc, "/dev/null", &ex);
116     catch ();
117     md3 = libvlc_media_new (vlc, "/dev/null", &ex);
118     catch ();
119
120     libvlc_media_list_add_media (ml, md1, &ex);
121     catch ();
122     libvlc_media_list_add_media (ml, md2, &ex);
123     catch ();
124
125     assert( libvlc_media_list_count (ml, &ex) == 2 );
126     catch ();
127
128     assert( libvlc_media_list_index_of_item (ml, md1, &ex) == 0 );
129     catch ();
130
131     assert( libvlc_media_list_index_of_item (ml, md2, &ex) == 1 );
132     catch ();
133
134     libvlc_media_list_remove_index (ml, 0, &ex);  /* removing first item */
135     catch ();
136
137     /* test if second item was moved on first place */
138     assert( libvlc_media_list_index_of_item (ml, md2, &ex) == 0 );
139     catch ();
140
141     libvlc_media_list_add_media (ml, md1, &ex); /* add 2 items */
142     catch ();
143     libvlc_media_list_add_media (ml, md1, &ex);
144     catch ();
145
146     /* there should be 3 pieces */
147     assert( libvlc_media_list_count (ml, &ex) == 3 );
148     catch ();
149
150     libvlc_media_list_insert_media (ml, md3, 2, &ex);
151     catch ();
152
153     /* there should be 4 pieces */
154     assert( libvlc_media_list_count (ml, &ex) == 4 );
155     catch ();
156
157     /* test inserting on right place */
158     assert( libvlc_media_list_index_of_item (ml, md3, &ex) == 2 );
159     catch ();
160
161     /* test right returning descriptor*/
162     assert ( libvlc_media_list_item_at_index (ml, 0, &ex) == md2 );
163     catch ();
164
165     assert ( libvlc_media_list_item_at_index (ml, 2, &ex) == md3 );
166     catch ();
167
168     /* test if give exceptions, when it should */
169     /* have 4 items, so index 4 should give exception */
170     libvlc_media_list_remove_index (ml, 4, &ex);
171     assert (have_exception ());
172
173     libvlc_media_list_remove_index (ml, 100, &ex);
174     assert (have_exception ());
175
176     libvlc_media_list_remove_index (ml, -1, &ex);
177     assert (have_exception ());
178
179     /* getting non valid items */
180     libvlc_media_t * p_non_exist =
181         libvlc_media_list_item_at_index (ml, 4, &ex);
182     assert (have_exception ());
183
184     p_non_exist = libvlc_media_list_item_at_index (ml, 100, &ex);
185     assert (have_exception ());
186
187     p_non_exist = libvlc_media_list_item_at_index (ml, -1, &ex);
188     assert (have_exception ());
189
190     md4 = libvlc_media_new (vlc, "/dev/dsp", &ex);
191     catch ();
192
193     /* try to find non inserted item */
194     int i_non_exist = 0;
195     i_non_exist = libvlc_media_list_index_of_item (ml, md4, &ex);
196     assert ( i_non_exist == -1 );
197
198     libvlc_media_release (md1);
199     libvlc_media_release (md2);
200     libvlc_media_release (md3);
201     libvlc_media_release (md4);
202
203     libvlc_media_list_release (ml);
204
205     libvlc_release (vlc);
206     catch ();
207 }
208
209 /* This one is an internal API. We use it here to run tests that
210  * don't depends on playback, and only test the event framework */
211 extern void libvlc_event_send( libvlc_event_manager_t *, libvlc_event_t *);
212
213 static void test_events_dummy_callback( const libvlc_event_t * event, void * user_data)
214 {
215     vlc_bool_t * callback_was_called = user_data;
216     *callback_was_called = VLC_TRUE;
217 }
218
219 static void test_events_callback_and_detach( const libvlc_event_t * event, void * user_data)
220 {
221     vlc_bool_t * callback_was_called = user_data;
222     libvlc_event_manager_t *em;
223
224     em = libvlc_media_player_event_manager (event->p_obj, &ex);
225     catch();
226
227     libvlc_event_detach (em, event->type, test_events_callback_and_detach, user_data, &ex);
228     *callback_was_called = VLC_TRUE;
229 }
230
231 static void test_event_type_reception( libvlc_event_manager_t * em, libvlc_event_type_t event_type, vlc_bool_t * callback_was_called )
232 {
233     libvlc_event_t event;
234     event.type = event_type;
235     *callback_was_called = VLC_FALSE;
236     libvlc_event_send (em, &event);
237     assert (*callback_was_called);
238 }
239
240 static void test_events (const char ** argv, int argc)
241 {
242     libvlc_instance_t *vlc;
243     libvlc_media_player_t *mi;
244     libvlc_event_manager_t *em;
245     vlc_bool_t callback_was_called;
246     libvlc_exception_t ex;
247     libvlc_event_type_t mi_events[] = {
248         libvlc_MediaPlayerPlayed,
249         libvlc_MediaPlayerPaused,
250         libvlc_MediaPlayerEndReached,
251         libvlc_MediaPlayerStopped,
252         libvlc_MediaPlayerEncounteredError,
253         libvlc_MediaPlayerTimeChanged,
254         libvlc_MediaPlayerPositionChanged,
255     };
256     int i, mi_events_len = sizeof(mi_events)/sizeof(*mi_events);
257     
258     log ("Testing events\n");
259
260     libvlc_exception_init (&ex);
261     vlc = libvlc_new (argc, argv, &ex);
262     catch ();
263
264     mi = libvlc_media_player_new (vlc, &ex);
265     catch ();
266
267     em = libvlc_media_player_event_manager (mi, &ex);
268
269     log ("+ Testing attaching to Media Instance\n");
270
271     for (i = 0; i < mi_events_len; i++) {
272         libvlc_event_attach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
273         catch ();
274     }
275
276     log ("+ Testing event reception\n");
277
278     for (i = 0; i < mi_events_len; i++)
279         test_event_type_reception (em, mi_events[i], &callback_was_called);
280
281     log ("+ Testing event detaching while in the event callback\n");
282
283     libvlc_event_t event;
284     event.type = mi_events[mi_events_len-1];
285     callback_was_called = VLC_FALSE;
286
287     libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_dummy_callback, &callback_was_called, &ex);
288     catch ();
289
290     libvlc_event_attach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
291     catch ();
292
293     libvlc_event_send (em, &event);
294     assert( callback_was_called );
295
296     callback_was_called = VLC_FALSE;
297     libvlc_event_send (em, &event);
298     assert( !callback_was_called );
299
300     libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
301     catch ();
302
303     log ("+ Testing regular detach()\n");
304
305     for (i = 0; i < mi_events_len - 1; i++) {
306         libvlc_event_detach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
307         catch ();
308     }
309
310     libvlc_media_player_release (mi);
311     catch ();
312
313     libvlc_release (vlc);
314     catch ();
315 }
316
317 static void test_media_player_play_stop(const char** argv, int argc)
318 {
319 #if 0
320     libvlc_instance_t *vlc;
321     libvlc_media_t *md;
322     libvlc_media_player_t *mi;
323     const char * file = "file://../bindings/java/core/src/test/resources/raffa_voice.ogg";
324
325     log ("Testing play and pause of %s\n", file);
326
327     libvlc_exception_init (&ex);
328     vlc = libvlc_new (argc, argv, &ex);
329     catch ();
330
331     md = libvlc_media_new (vlc, file, &ex);
332     catch ();
333
334     mi = libvlc_media_player_new_from_media (md, &ex);
335     catch ();
336     
337     libvlc_media_release (md);
338
339     libvlc_media_player_play (mi, &ex);
340     catch ();
341
342     /* FIXME: Do something clever */
343     sleep(1);
344
345     assert( libvlc_media_player_get_state (mi, &ex) != libvlc_Error );
346     catch ();
347
348     libvlc_media_player_stop (mi, &ex);
349     catch ();
350
351     libvlc_media_player_release (mi);
352     catch ();
353
354     libvlc_release (vlc);
355     catch ();
356 #endif
357 }
358
359 int main (int argc, char *argv[])
360 {
361     const char *args[argc + 5];
362     int nlibvlc_args = sizeof (args) / sizeof (args[0]);
363
364     alarm (50); /* Make sure "make check" does not get stuck */
365
366     args[0] = "-vvv";
367     args[1] = "-I";
368     args[2] = "dummy";
369     args[3] = "--plugin-path=../modules";
370     args[4] = "--vout=dummy";
371     args[5] = "--aout=dummy";
372     for (int i = 1; i < argc; i++)
373         args[i + 3] = argv[i];
374
375     test_core (args, nlibvlc_args);
376
377     test_events (args, nlibvlc_args);
378
379     test_media_list (args, nlibvlc_args);
380
381     test_media_player_play_stop(args, nlibvlc_args);
382
383     return 0;
384 }