]> git.sesse.net Git - vlc/blob - test/src/misc/variables.c
Qt: avoid a crash with plugins dialog.
[vlc] / test / src / misc / variables.c
1 /*****************************************************************************
2  * variables.c: test for variables
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Rémi Duraffort <ivoire@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at 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.  See the
17  * 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, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <limits.h>
25
26 #include "../../libvlc/test.h"
27 #include <../src/control/libvlc_internal.h>
28
29 const char *psz_var_name[] = { "a", "abcdef", "abcdefg", "abc123", "abc-123", "é€!!" };
30 const int i_var_count = 6;
31 vlc_value_t var_value[6];
32
33
34 static void test_integer( libvlc_int_t *p_libvlc )
35 {
36     int i;
37     for( i = 0; i < i_var_count; i++ )
38         var_Create( p_libvlc, psz_var_name[i], VLC_VAR_INTEGER );
39
40     for( i = 0; i < i_var_count; i++ )
41     {
42         var_value[i].i_int = rand();
43         var_SetInteger( p_libvlc, psz_var_name[i], var_value[i].i_int );
44     }
45
46     for( i = 0; i < i_var_count; i++ )
47     {
48         assert( var_GetInteger( p_libvlc, psz_var_name[i] ) == var_value[i].i_int );
49         var_IncInteger( p_libvlc, psz_var_name[i] );
50         assert( var_GetInteger( p_libvlc, psz_var_name[i] ) == var_value[i].i_int + 1 );
51         var_DecInteger( p_libvlc, psz_var_name[i] );
52         assert( var_GetInteger( p_libvlc, psz_var_name[i] ) == var_value[i].i_int );
53     }
54
55     for( i = 0; i < i_var_count; i++ )
56         var_Destroy( p_libvlc, psz_var_name[i] );
57 }
58
59 static void test_booleans( libvlc_int_t *p_libvlc )
60 {
61     int i;
62     for( i = 0; i < i_var_count; i++ )
63          var_Create( p_libvlc, psz_var_name[i], VLC_VAR_BOOL );
64
65     for( i = 0; i < i_var_count; i++ )
66     {
67         var_value[i].b_bool = (rand() > RAND_MAX/2);
68         var_SetBool( p_libvlc, psz_var_name[i], var_value[i].b_bool );
69     }
70
71     for( i = 0; i < i_var_count; i++ )
72         assert( var_GetBool( p_libvlc, psz_var_name[i] ) == var_value[i].b_bool );
73
74     for( i = 0; i < i_var_count; i++ )
75         var_Destroy( p_libvlc, psz_var_name[i] );
76 }
77
78 static void test_times( libvlc_int_t *p_libvlc )
79 {
80     int i;
81     for( i = 0; i < i_var_count; i++ )
82          var_Create( p_libvlc, psz_var_name[i], VLC_VAR_TIME );
83
84     for( i = 0; i < i_var_count; i++ )
85     {
86         var_value[i].i_time = rand();
87         var_SetTime( p_libvlc, psz_var_name[i], var_value[i].i_time );
88     }
89
90     for( i = 0; i < i_var_count; i++ )
91         assert( var_GetTime( p_libvlc, psz_var_name[i] ) == var_value[i].i_time );
92
93     for( i = 0; i < i_var_count; i++ )
94         var_Destroy( p_libvlc, psz_var_name[i] );
95 }
96
97 static void test_floats( libvlc_int_t *p_libvlc )
98 {
99     int i;
100     for( i = 0; i < i_var_count; i++ )
101          var_Create( p_libvlc, psz_var_name[i], VLC_VAR_FLOAT );
102
103     for( i = 0; i < i_var_count; i++ )
104     {
105         var_value[i].f_float = rand();
106         var_SetFloat( p_libvlc, psz_var_name[i], var_value[i].f_float );
107     }
108
109     for( i = 0; i < i_var_count; i++ )
110         assert( var_GetFloat( p_libvlc, psz_var_name[i] ) == var_value[i].f_float );
111
112     for( i = 0; i < i_var_count; i++ )
113         var_Destroy( p_libvlc, psz_var_name[i] );
114 }
115
116 static void test_strings( libvlc_int_t *p_libvlc )
117 {
118     int i;
119     char *psz_tmp;
120     for( i = 0; i < i_var_count; i++ )
121          var_Create( p_libvlc, psz_var_name[i], VLC_VAR_STRING );
122
123     for( i = 0; i < i_var_count; i++ )
124         var_SetString( p_libvlc, psz_var_name[i], psz_var_name[i] );
125
126     for( i = 0; i < i_var_count; i++ )
127     {
128         psz_tmp = var_GetString( p_libvlc, psz_var_name[i] );
129         assert( !strcmp( psz_tmp, psz_var_name[i] ) );
130         free( psz_tmp );
131     }
132
133     for( i = 0; i < i_var_count; i++ )
134         var_Destroy( p_libvlc, psz_var_name[i] );
135
136
137     /* Some more test for strings */
138     var_Create( p_libvlc, "bla", VLC_VAR_STRING );
139     assert( var_GetNonEmptyString( p_libvlc, "bla" ) == NULL );
140     var_SetString( p_libvlc, "bla", "" );
141     assert( var_GetNonEmptyString( p_libvlc, "bla" ) == NULL );
142     var_SetString( p_libvlc, "bla", "test" );
143     psz_tmp = var_GetNonEmptyString( p_libvlc, "bla" );
144     assert( !strcmp( psz_tmp, "test" ) );
145     free( psz_tmp );
146     var_Destroy( p_libvlc, "bla" );
147 }
148
149 static void test_address( libvlc_int_t *p_libvlc )
150 {
151     int i;
152     for( i = 0; i < i_var_count; i++ )
153          var_Create( p_libvlc, psz_var_name[i], VLC_VAR_ADDRESS );
154
155     for( i = 0; i < i_var_count; i++ )
156     {
157         var_value[i].p_address = rand();
158         var_SetAddress( p_libvlc, psz_var_name[i], var_value[i].p_address );
159     }
160
161     for( i = 0; i < i_var_count; i++ )
162     {
163         vlc_value_t val;
164         var_Get( p_libvlc, psz_var_name[i], &val );
165         assert( val.p_address == var_value[i].p_address );
166     }
167
168     for( i = 0; i < i_var_count; i++ )
169         var_Destroy( p_libvlc, psz_var_name[i] );
170 }
171
172 static int callback( vlc_object_t* p_this, char const *psz_var,
173                      vlc_value_t oldval, vlc_value_t newval, void *p_data)
174 {
175     (void)p_this;    (void)oldval;
176     int i;
177
178     // Check the parameters
179     assert( p_data == psz_var_name );
180
181     // Find the variable
182     for( i = 0; i < i_var_count; i++ )
183     {
184         if( !strcmp( psz_var_name[i], psz_var ) )
185             break;
186     }
187     // Check the variable is known
188     assert( i < i_var_count );
189
190     var_value[i].i_int = newval.i_int;
191     return VLC_SUCCESS;
192 }
193
194 static void test_callbacks( libvlc_int_t *p_libvlc )
195 {
196     /* add the callbacks */
197     int i;
198     for( i = 0; i < i_var_count; i++ )
199     {
200         var_Create( p_libvlc, psz_var_name[i], VLC_VAR_INTEGER );
201         var_AddCallback( p_libvlc, psz_var_name[i], callback, psz_var_name );
202     }
203
204     /* Set the variables and trigger the callbacks */
205     for( i = 0; i < i_var_count; i++ )
206     {
207         int i_temp = rand();
208         var_SetInteger( p_libvlc, psz_var_name[i], i_temp );
209         assert( i_temp == var_value[i].i_int );
210         var_SetInteger( p_libvlc, psz_var_name[i], 0 );
211         assert( var_value[i].i_int == 0 );
212         var_value[i].i_int = 1;
213     }
214
215     /* Only trigger the callback: the value will be 0 again */
216     for( i = 0; i < i_var_count; i++ )
217     {
218         var_TriggerCallback( p_libvlc, psz_var_name[i] );
219         assert( var_value[i].i_int == 0 );
220     }
221
222     for( i = 0; i < i_var_count; i++ )
223         var_Destroy( p_libvlc, psz_var_name[i] );
224 }
225
226 static void test_limits( libvlc_int_t *p_libvlc )
227 {
228     vlc_value_t val;
229     val.i_int = 0;
230     var_Create( p_libvlc, "bla", VLC_VAR_INTEGER );
231
232     var_Change( p_libvlc, "bla", VLC_VAR_GETMIN, &val, NULL );
233     assert( val.i_int == 0 );
234
235     val.i_int = -1234;
236     var_Change( p_libvlc, "bla", VLC_VAR_SETMIN, &val, NULL );
237
238     val.i_int = 12345;
239     var_Change( p_libvlc, "bla", VLC_VAR_SETMAX, &val, NULL );
240
241     var_Change( p_libvlc, "bla", VLC_VAR_GETMIN, &val, NULL );
242     assert( val.i_int == -1234 );
243     var_Change( p_libvlc, "bla", VLC_VAR_GETMAX, &val, NULL );
244     assert( val.i_int == 12345 );
245
246     var_SetInteger( p_libvlc, "bla", -123456 );
247     assert( var_GetInteger( p_libvlc, "bla" ) == -1234 );
248     var_SetInteger( p_libvlc, "bla", 1234 );
249     assert( var_GetInteger( p_libvlc, "bla" ) == 1234 );
250     var_SetInteger( p_libvlc, "bla", 12346 );
251     assert( var_GetInteger( p_libvlc, "bla" ) == 12345 );
252
253     val.i_int = 42;
254     var_Change( p_libvlc, "bla", VLC_VAR_SETSTEP, &val, NULL );
255     var_SetInteger( p_libvlc, "bla", 20 );
256     val.i_int = 0;
257     var_Change( p_libvlc, "bla", VLC_VAR_GETSTEP, &val, NULL );
258     assert( val.i_int == 42 );
259
260     var_SetInteger( p_libvlc, "bla", 20 );
261     assert( var_GetInteger( p_libvlc, "bla" ) == 0 );
262
263     var_SetInteger( p_libvlc, "bla", 21 );
264     assert( var_GetInteger( p_libvlc, "bla" ) == 42 );
265
266     var_Destroy( p_libvlc, "bla" );
267 }
268
269 static void test_choices( libvlc_int_t *p_libvlc )
270 {
271     vlc_value_t val, val2;
272     var_Create( p_libvlc, "bla", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE |
273                                  VLC_VAR_ISCOMMAND );
274     val.i_int = 1;
275     val2.psz_string = (char*)"one";
276     var_Change( p_libvlc, "bla", VLC_VAR_ADDCHOICE, &val, &val2 );
277
278     val.i_int = 2;
279     val2.psz_string = (char*)"two";
280     var_Change( p_libvlc, "bla", VLC_VAR_ADDCHOICE, &val, &val2 );
281
282     assert( var_CountChoices( p_libvlc, "bla" ) == 2 );
283
284     var_Change( p_libvlc, "bla", VLC_VAR_DELCHOICE, &val, &val2 );
285     assert( var_CountChoices( p_libvlc, "bla" ) == 1 );
286
287     var_Change( p_libvlc, "bla", VLC_VAR_GETCHOICES, &val, &val2 );
288     assert( val.p_list->i_count == 1 && val.p_list->p_values[0].i_int == 1 &&
289             val2.p_list->i_count == 1 &&
290             !strcmp( val2.p_list->p_values[0].psz_string, "one" ) );
291     var_FreeList( &val, &val2 );
292
293     var_Destroy( p_libvlc, "bla" );
294 }
295
296 static void test_variables( libvlc_instance_t *p_vlc )
297 {
298     libvlc_int_t *p_libvlc = p_vlc->p_libvlc_int;
299     srand( time( NULL ) );
300
301     log( "Testing for integers\n" );
302     test_integer( p_libvlc );
303
304     log( "Testing for booleans\n" );
305     test_booleans( p_libvlc );
306
307     log( "Testing for times\n" );
308     test_times( p_libvlc );
309
310     log( "Testing for floats\n" );
311     test_floats( p_libvlc );
312
313     log( "Testing for strings\n" );
314     test_strings( p_libvlc );
315
316     log( "Testing for addresses\n" );
317     test_address( p_libvlc );
318
319     log( "Testing the callbacks\n" );
320     test_callbacks( p_libvlc );
321
322     log( "Testing the limits\n" );
323     test_limits( p_libvlc );
324
325     log( "Testing choices\n" );
326     test_choices( p_libvlc );
327 }
328
329
330 int main( void )
331 {
332     libvlc_instance_t *p_vlc;
333
334     test_init();
335
336     log( "Testing the core variables\n" );
337     libvlc_exception_init( &ex );
338     p_vlc = libvlc_new( test_defaults_nargs, test_defaults_args, &ex );
339     catch();
340
341     test_variables( p_vlc );
342
343     libvlc_release( p_vlc );
344
345     return 0;
346 }
347