Fix memory leak (CID 95)
[vlc/vlc-skelet.git] / test / libvlc / events.c
blob199c61fc56c4dc1ca52f4732d24d3303defe310e
1 /*
2 * events.c - libvlc smoke test
4 * $Id$
5 */
7 /**********************************************************************
8 * Copyright (C) 2007 RĂ©mi Denis-Courmont. *
9 * Copyright (C) 2008 Pierre d'Herbemont. *
10 * This program is free software; you can redistribute and/or modify *
11 * it under the terms of the GNU General Public License as published *
12 * by the Free Software Foundation; version 2 of the license, or (at *
13 * your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
18 * See the GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, you can get it from: *
22 * http://www.gnu.org/copyleft/gpl.html *
23 **********************************************************************/
25 #include "test.h"
27 /* This one is an internal API. We use it here to run tests that
28 * don't depends on playback, and only test the event framework */
29 extern void libvlc_event_send( libvlc_event_manager_t *, libvlc_event_t *);
31 static void test_events_dummy_callback( const libvlc_event_t * event, void * user_data)
33 (void)event;
34 bool * callback_was_called = user_data;
35 *callback_was_called = true;
38 static void test_events_callback_and_detach( const libvlc_event_t * event, void * user_data)
40 bool * callback_was_called = user_data;
41 libvlc_event_manager_t *em;
43 em = libvlc_media_player_event_manager (event->p_obj, &ex);
44 catch();
46 libvlc_event_detach (em, event->type, test_events_callback_and_detach, user_data, &ex);
47 *callback_was_called = true;
50 static void test_event_type_reception( libvlc_event_manager_t * em, libvlc_event_type_t event_type, bool * callback_was_called )
52 libvlc_event_t event;
53 event.type = event_type;
54 *callback_was_called = false;
55 libvlc_event_send (em, &event);
56 assert (*callback_was_called);
59 static void test_events (const char ** argv, int argc)
61 libvlc_instance_t *vlc;
62 libvlc_media_player_t *mi;
63 libvlc_event_manager_t *em;
64 bool callback_was_called;
65 libvlc_exception_t ex;
66 libvlc_event_type_t mi_events[] = {
67 libvlc_MediaPlayerPlayed,
68 libvlc_MediaPlayerPaused,
69 libvlc_MediaPlayerEndReached,
70 libvlc_MediaPlayerStopped,
71 libvlc_MediaPlayerEncounteredError,
72 libvlc_MediaPlayerTimeChanged,
73 libvlc_MediaPlayerPositionChanged,
75 int i, mi_events_len = sizeof(mi_events)/sizeof(*mi_events);
77 log ("Testing events\n");
79 libvlc_exception_init (&ex);
80 vlc = libvlc_new (argc, argv, &ex);
81 catch ();
83 mi = libvlc_media_player_new (vlc, &ex);
84 catch ();
86 em = libvlc_media_player_event_manager (mi, &ex);
88 log ("+ Testing attaching to Media Instance\n");
90 for (i = 0; i < mi_events_len; i++) {
91 libvlc_event_attach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
92 catch ();
95 log ("+ Testing event reception\n");
97 for (i = 0; i < mi_events_len; i++)
98 test_event_type_reception (em, mi_events[i], &callback_was_called);
100 log ("+ Testing event detaching while in the event callback\n");
102 libvlc_event_t event;
103 event.type = mi_events[mi_events_len-1];
104 callback_was_called = false;
106 libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_dummy_callback, &callback_was_called, &ex);
107 catch ();
109 libvlc_event_attach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
110 catch ();
112 libvlc_event_send (em, &event);
113 assert( callback_was_called );
115 callback_was_called = false;
116 libvlc_event_send (em, &event);
117 assert( !callback_was_called );
119 libvlc_event_detach (em, mi_events[mi_events_len-1], test_events_callback_and_detach, &callback_was_called, &ex);
120 catch ();
122 log ("+ Testing regular detach()\n");
124 for (i = 0; i < mi_events_len - 1; i++) {
125 libvlc_event_detach (em, mi_events[i], test_events_dummy_callback, &callback_was_called, &ex);
126 catch ();
129 libvlc_media_player_release (mi);
130 catch ();
132 libvlc_release (vlc);
133 catch ();
136 int main (void)
138 test_init();
140 test_events (test_defaults_args, test_defaults_nargs);
142 return 0;