demux: mp4: check handler before dereferencing sample entry
[vlc.git] / test / src / interface / dialog.c
blobd47ea80e632b7c1dd08850be170d86dde9231104
1 /*****************************************************************************
2 * dialog.c: test VLC core dialogs
3 *****************************************************************************
4 * Copyright © 2016 VLC authors, VideoLAN and VideoLabs
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24 #include <vlc/vlc.h>
26 #include "../../../lib/libvlc_internal.h"
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_modules.h>
31 #include <vlc_dialog.h>
32 #include <vlc_interrupt.h>
33 #include <vlc_keystore.h>
35 #undef NDEBUG
36 #include <assert.h>
38 #define TITLE "VLC Dialogs test"
41 * Build and exec qt dialog test:
42 * $ cd vlc/test
43 * $ make test_src_interface_dialog
44 * $ ./test_src_interface_dialog -a
47 struct cb_answer
49 bool b_dismiss;
50 const char *psz_username;
51 int i_action;
56 static void
57 display_error_cb(void *p_data, const char *psz_title, const char *psz_text)
59 (void) p_data;
60 printf("error message: title: '%s', text: '%s'\n", psz_title, psz_text);
63 static void
64 display_login_cb(void *p_data, vlc_dialog_id *p_id, const char *psz_title,
65 const char *psz_text, const char *psz_default_username,
66 bool b_ask_store)
68 struct cb_answer *p_ans = p_data;
69 printf("login dialog: title: '%s', text: '%s', "
70 "default_username: '%s', b_ask_store: %d\n",
71 psz_title, psz_text, psz_default_username, b_ask_store);
73 if (p_ans->b_dismiss)
74 vlc_dialog_id_dismiss(p_id);
75 else if (p_ans->psz_username != NULL)
76 vlc_dialog_id_post_login(p_id, p_ans->psz_username, "", false);
79 static void
80 display_question_cb(void *p_data, vlc_dialog_id *p_id, const char *psz_title,
81 const char *psz_text, vlc_dialog_question_type i_type,
82 const char *psz_cancel, const char *psz_action1,
83 const char *psz_action2)
85 struct cb_answer *p_ans = p_data;
86 printf("question dialog: title: '%s', text: '%s', "
87 "type: %d, cancel: '%s', action1: '%s', action2: '%s'\n",
88 psz_title, psz_text, i_type, psz_cancel, psz_action1, psz_action2);
90 if (p_ans->b_dismiss)
91 vlc_dialog_id_dismiss(p_id);
92 else if (p_ans->i_action > 0)
93 vlc_dialog_id_post_action(p_id, p_ans->i_action);
96 static void
97 display_progress_cb(void *p_data, vlc_dialog_id *p_id, const char *psz_title,
98 const char *psz_text, bool b_indeterminate,
99 float f_position, const char *psz_cancel)
101 struct cb_answer *p_ans = p_data;
102 printf("progress dialog: title: '%s', text: '%s', "
103 "indeterminate: %d, position: %f, cancel: '%s'\n",
104 psz_title, psz_text, b_indeterminate, f_position, psz_cancel);
106 if (p_ans->b_dismiss)
107 vlc_dialog_id_dismiss(p_id);
110 static void cancel_cb(void *p_data, vlc_dialog_id *p_id)
112 (void) p_data;
113 vlc_dialog_id_dismiss(p_id);
116 static void update_progress_cb(void *p_data, vlc_dialog_id *p_id, float f_position,
117 const char *psz_text)
119 (void) p_id;
120 (void) p_data;
121 printf("update_progress: %f, text: %s\n", f_position, psz_text);
124 static inline void
125 set_answer(struct cb_answer *p_ans, bool b_dismiss, const char *psz_username,
126 int i_action)
128 if (p_ans != NULL)
130 p_ans->b_dismiss = b_dismiss;
131 p_ans->psz_username = psz_username;
132 p_ans->i_action = i_action;
136 static void
137 test_dialogs(vlc_object_t *p_obj, struct cb_answer *p_ans,
138 mtime_t i_dialog_wait)
140 set_answer(p_ans, false, NULL, 0);
141 int i_ret = vlc_dialog_display_error(p_obj, TITLE,
142 "%d/ Testing login...", 1);
143 assert(i_ret == VLC_SUCCESS);
145 vlc_dialog_id *p_id;
146 char *psz_user, *psz_passwd;
147 bool b_store;
148 set_answer(p_ans, false, "Click OK", 0);
149 i_ret = vlc_dialog_wait_login(p_obj, &psz_user, &psz_passwd,
150 &b_store, "Click OK", TITLE, "Click OK");
151 assert(i_ret == 1 && strcmp(psz_user, "Click OK") == 0);
152 free(psz_user);
153 free(psz_passwd);
155 set_answer(p_ans, true, NULL, 0);
156 i_ret = vlc_dialog_wait_login(p_obj, &psz_user, &psz_passwd,
157 &b_store, "Click Cancel", TITLE, "Click Cancel");
158 assert(i_ret == 0);
160 set_answer(p_ans, false, NULL, 0);
161 i_ret = vlc_dialog_display_error(p_obj, TITLE,
162 "%d/ Testing question...", 2);
163 assert(i_ret == VLC_SUCCESS);
165 set_answer(p_ans, false, NULL, 1);
166 i_ret = vlc_dialog_wait_question(p_obj,
167 VLC_DIALOG_QUESTION_NORMAL,
168 "Cancel", "Action1", "Action2", TITLE,
169 "Click Action1");
170 assert(i_ret == 1);
172 set_answer(p_ans, false, NULL, 2);
173 i_ret = vlc_dialog_wait_question(p_obj,
174 VLC_DIALOG_QUESTION_NORMAL,
175 "Cancel", "Action1", "Action2", TITLE,
176 "Click Action2");
177 assert(i_ret == 2);
179 set_answer(p_ans, true, NULL, 0);
180 i_ret = vlc_dialog_wait_question(p_obj,
181 VLC_DIALOG_QUESTION_NORMAL,
182 "Cancel", "Action1", "Action2", TITLE,
183 "Click Cancel");
184 assert(i_ret == 0);
186 set_answer(p_ans, false, NULL, 0);
187 i_ret = vlc_dialog_display_error(p_obj, TITLE,
188 "%d/ Testing critical waiting error...", 3);
189 assert(i_ret == VLC_SUCCESS);
191 set_answer(p_ans, true, NULL, 0);
192 i_ret = vlc_dialog_wait_question(p_obj,
193 VLC_DIALOG_QUESTION_CRITICAL,
194 "OK", NULL, NULL, TITLE,
195 "Error");
196 assert(i_ret == 0);
198 set_answer(p_ans, false, NULL, 0);
199 i_ret = vlc_dialog_display_error(p_obj, TITLE,
200 "%d/ Testing progress dialog...", 4);
201 assert(i_ret == VLC_SUCCESS);
203 set_answer(p_ans, false, NULL, 0);
204 p_id = vlc_dialog_display_progress(p_obj, true,
205 0.5f /* should be ignored */,
206 NULL, TITLE,
207 "Indeterminate non cancellable dialog "
208 "for %" PRId64 " us", i_dialog_wait);
209 assert(p_id != NULL);
210 msleep(i_dialog_wait);
211 vlc_dialog_release(p_obj, p_id);
212 assert(i_ret == VLC_SUCCESS);
214 set_answer(p_ans, true, NULL, 0);
215 float f_position = 0.5f;
216 p_id = vlc_dialog_display_progress(p_obj, true,
217 f_position /* should be ignored */,
218 "Cancel", TITLE,
219 "Indeterminate cancellable dialog.\n"
220 "Cancel It!");
221 assert(p_id != NULL);
222 while(!vlc_dialog_is_cancelled(p_obj, p_id))
223 msleep(i_dialog_wait / 30);
224 vlc_dialog_release(p_obj, p_id);
226 set_answer(p_ans, false, NULL, 0);
227 p_id = vlc_dialog_display_progress(p_obj, false, f_position, NULL, TITLE,
228 "Non cancellable dialog in progress");
229 assert(p_id != NULL);
230 while (f_position <= 1.0f)
232 msleep(i_dialog_wait / 30);
233 f_position += 0.02f;
234 i_ret = vlc_dialog_update_progress(p_obj, p_id, f_position);
235 assert(i_ret == VLC_SUCCESS);
237 vlc_dialog_release(p_obj, p_id);
239 f_position = 0.5f;
240 set_answer(p_ans, false, NULL, 0);
241 p_id = vlc_dialog_display_progress(p_obj, false, f_position, NULL, TITLE,
242 "Non cancellable dialog in progress.\n"
243 "float value: %f", f_position);
244 assert(p_id != NULL);
245 while (f_position <= 1.0f)
247 msleep(i_dialog_wait / 30);
248 f_position += 0.02f;
249 i_ret = vlc_dialog_update_progress_text(p_obj, p_id, f_position,
250 "Non cancellable dialog in progress.\n"
251 "float value: %f", f_position);
252 assert(i_ret == VLC_SUCCESS);
254 vlc_dialog_release(p_obj, p_id);
256 set_answer(p_ans, false, NULL, 0);
257 i_ret = vlc_dialog_display_error(p_obj, TITLE,
258 "%d/ Testing 2 modal dialogs at a time...", 5);
260 assert(i_ret == VLC_SUCCESS);
262 set_answer(p_ans, true, NULL, 0);
263 p_id = vlc_dialog_display_progress(p_obj, true,
264 f_position /* should be ignored */,
265 "Cancel", TITLE,
266 "Indeterminate cancellable dialog.\n"
267 "Cancel It!");
268 assert(p_id != NULL);
270 set_answer(p_ans, true, NULL, 0);
271 i_ret = vlc_dialog_wait_question(p_obj,
272 VLC_DIALOG_QUESTION_CRITICAL,
273 "OK", NULL, NULL, TITLE,
274 "Error");
275 assert(i_ret == 0);
276 while(!vlc_dialog_is_cancelled(p_obj, p_id))
277 msleep(i_dialog_wait / 30);
278 vlc_dialog_release(p_obj, p_id);
282 main(int i_argc, char *ppsz_argv[])
284 bool b_test_all = i_argc > 1 && strcmp(ppsz_argv[1], "-a") == 0;
286 if (!b_test_all)
287 alarm(10);
289 setenv("VLC_PLUGIN_PATH", "../modules", 1);
291 libvlc_instance_t *p_libvlc = libvlc_new(0, NULL);
292 assert(p_libvlc != NULL);
294 printf("testing dialog callbacks\n");
295 const vlc_dialog_cbs cbs = {
296 .pf_display_error = display_error_cb,
297 .pf_display_login = display_login_cb,
298 .pf_display_question = display_question_cb,
299 .pf_display_progress = display_progress_cb,
300 .pf_cancel = cancel_cb,
301 .pf_update_progress = update_progress_cb,
303 struct cb_answer ans = { 0 };
304 vlc_dialog_provider_set_callbacks(p_libvlc->p_libvlc_int, &cbs, &ans);
305 test_dialogs(VLC_OBJECT(p_libvlc->p_libvlc_int), &ans, 100000);
306 vlc_dialog_provider_set_callbacks(p_libvlc->p_libvlc_int, NULL, NULL);
308 libvlc_release(p_libvlc);
310 if (b_test_all)
312 printf("testing Qt dialog callbacks\n");
313 static const char *args[] = {
314 "--no-qt-privacy-ask", /* avoid dialog that ask for privacy */
316 libvlc_instance_t *p_libvlc = libvlc_new(1, args);
317 assert(p_libvlc != NULL);
319 int i_ret = libvlc_InternalAddIntf(p_libvlc->p_libvlc_int, "qt");
320 assert(i_ret == VLC_SUCCESS);
321 test_dialogs(VLC_OBJECT(p_libvlc->p_libvlc_int), NULL, 3000000);
323 libvlc_release(p_libvlc);
326 return 0;