]> git.sesse.net Git - vlc/blob - src/control/testapi.c
Add tests for media list.
[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_descriptor_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_descriptor_new (vlc, "/dev/null", &ex);
114     catch ();
115     md2 = libvlc_media_descriptor_new (vlc, "/dev/null", &ex);
116     catch ();
117     md3 = libvlc_media_descriptor_new (vlc, "/dev/null", &ex);
118     catch ();
119
120     libvlc_media_list_add_media_descriptor (ml, md1, &ex);
121     catch ();
122     libvlc_media_list_add_media_descriptor (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_descriptor (ml, md1, &ex); /* add 2 items */
142     catch ();
143     libvlc_media_list_add_media_descriptor (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_descriptor (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_descriptor_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_descriptor_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_descriptor_release (md1);
199     libvlc_media_descriptor_release (md2);
200     libvlc_media_descriptor_release (md3);
201     libvlc_media_descriptor_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_instance_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_instance_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_MediaInstancePlayed,
249         libvlc_MediaInstancePaused,
250         libvlc_MediaInstanceReachedEnd,
251         libvlc_MediaInstanceEncounteredError,
252         libvlc_MediaInstanceTimeChanged,
253         libvlc_MediaInstancePositionChanged,
254     };
255     int i, mi_events_len = sizeof(mi_events)/sizeof(*mi_events);
256     
257     log ("Testing events\n");
258
259     libvlc_exception_init (&ex);
260     vlc = libvlc_new (argc, argv, &ex);
261     catch ();
262
263     mi = libvlc_media_instance_new (vlc, &ex);
264     catch ();
265
266     em = libvlc_media_instance_event_manager (mi, &ex);
267
268     log ("+ Testing attaching to Media Instance\n");
269
270     for (i = 0; i < mi_events_len; i++) {
271         libvlc_event_attach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
272         catch ();
273     }
274
275     log ("+ Testing event reception\n");
276
277     for (i = 0; i < mi_events_len; i++)
278         test_event_type_reception (em, mi_events[i], &callback_was_called);
279
280     log ("+ Testing event detaching while in the event callback\n");
281
282     libvlc_event_t event;
283     event.type = mi_events[mi_events_len-1];
284     callback_was_called = VLC_FALSE;
285
286     libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_dummy_callback, &callback_was_called, &ex);
287     catch ();
288
289     libvlc_event_attach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
290     catch ();
291
292     libvlc_event_send (em, &event);
293     assert( callback_was_called );
294
295     callback_was_called = VLC_FALSE;
296     libvlc_event_send (em, &event);
297     assert( !callback_was_called );
298
299     libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
300     catch ();
301
302     log ("+ Testing regular detach()\n");
303
304     for (i = 0; i < mi_events_len - 1; i++) {
305         libvlc_event_detach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
306         catch ();
307     }
308
309     libvlc_media_instance_release (mi);
310     catch ();
311
312     libvlc_release (vlc);
313     catch ();
314 }
315
316 static void test_media_player_play_stop(const char** argv, int argc)
317 {
318     libvlc_instance_t *vlc;
319     libvlc_media_descriptor_t *md;
320     libvlc_media_instance_t *mi;
321     const char * file = "file://../bindings/java/core/src/test/resources/raffa_voice.ogg";
322
323     log ("Testing play and pause of %s\n", file);
324
325     libvlc_exception_init (&ex);
326     vlc = libvlc_new (argc, argv, &ex);
327     catch ();
328
329     md = libvlc_media_descriptor_new (vlc, file, &ex);
330     catch ();
331
332     mi = libvlc_media_instance_new_from_media_descriptor (md, &ex);
333     catch ();
334     
335     libvlc_media_descriptor_release (md);
336
337     libvlc_media_instance_play (mi, &ex);
338     catch ();
339
340     /* FIXME: Do something clever */
341     sleep(1);
342
343     assert( libvlc_media_instance_get_state (mi, &ex) != libvlc_Error );
344     catch ();
345
346     libvlc_media_instance_stop (mi, &ex);
347     catch ();
348
349     libvlc_media_instance_release (mi);
350     catch ();
351
352     libvlc_release (vlc);
353     catch ();
354 }
355
356 int main (int argc, char *argv[])
357 {
358     const char *args[argc + 5];
359     int nlibvlc_args = sizeof (args) / sizeof (args[0]);
360
361     alarm (50); /* Make sure "make check" does not get stuck */
362
363     args[0] = "-vvv";
364     args[1] = "-I";
365     args[2] = "dummy";
366     args[3] = "--plugin-path=../modules";
367     args[4] = "--vout=dummy";
368     args[5] = "--aout=dummy";
369     for (int i = 1; i < argc; i++)
370         args[i + 3] = argv[i];
371
372     test_core (args, nlibvlc_args);
373
374     test_events (args, nlibvlc_args);
375
376     test_media_list (args, nlibvlc_args);
377
378     test_media_player_play_stop(args, nlibvlc_args);
379
380     return 0;
381 }