quvi_supports: Handle QUVI_SUPPORTS_TYPE_SUBTITLE
[libquvi.git] / tests / subtitle.c
blob34377915410e9cf3470b053ccbe6d729e9603bd0
1 /* libquvi
2 * Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvu <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (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 Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <string.h>
24 #include <glib.h>
25 #include <quvi.h>
27 #include "tests.h"
29 static const gchar *URLs[] =
31 "http://youtube.com/watch?v=0QRO3gKj3qw",
32 "http://youtube.com/watch?v=ntLPcVAyNPE",
33 NULL
36 static void _t_chk_val(quvi_subtitle_type_t qst,
37 const QuviSubtitleTypeProperty p,
38 const gchar *n)
40 gdouble d = -1;
41 quvi_subtitle_type_get(qst, p, &d);
42 g_assert_cmpint(d, !=, -1);
43 g_test_message("%s=%g", n, d);
46 #define t_chk_val(p) \
47 do { _t_chk_val(qst, p, #p); } while (0)
49 static void _l_chk_len(quvi_subtitle_lang_t qsl,
50 const QuviSubtitleLangProperty p,
51 const gchar *n)
53 gchar *s = NULL;
54 quvi_subtitle_lang_get(qsl, p, &s);
55 g_assert(s != NULL);
56 g_test_message("%s=%s", n, s);
57 g_assert_cmpint(strlen(s), >, 1);
60 #define l_chk_len(p) \
61 do { _l_chk_len(qsl, p, #p); } while (0)
63 static void _chk_shared_properties(quvi_t q, quvi_subtitle_t qsub,
64 quvi_subtitle_type_t qst,
65 quvi_subtitle_lang_t qsl)
67 gdouble v;
69 /* type */
71 t_chk_val(QUVI_SUBTITLE_TYPE_PROPERTY_FORMAT);
72 t_chk_val(QUVI_SUBTITLE_TYPE_PROPERTY_TYPE);
74 /* lang */
76 l_chk_len(QUVI_SUBTITLE_LANG_PROPERTY_URL);
77 l_chk_len(QUVI_SUBTITLE_LANG_PROPERTY_ID);
79 /* These are currently provided for (YouTube) CCs only. */
81 qst = quvi_subtitle_type_next(qsub);
82 g_assert(qst != NULL); /* Advance to the CC which should be next. */
84 quvi_subtitle_type_get(qst, QUVI_SUBTITLE_TYPE_PROPERTY_FORMAT, &v);
85 g_assert_cmpint(qerr(q), ==, QUVI_OK);
86 g_assert_cmpint(v, ==, QUVI_SUBTITLE_FORMAT_TT);
88 quvi_subtitle_type_get(qst, QUVI_SUBTITLE_TYPE_PROPERTY_TYPE, &v);
89 g_assert_cmpint(qerr(q), ==, QUVI_OK);
90 g_assert_cmpint(v, ==, QUVI_SUBTITLE_TYPE_CC);
92 qsl = quvi_subtitle_lang_next(qst);
93 g_assert(qsl != NULL);
95 l_chk_len(QUVI_SUBTITLE_LANG_PROPERTY_TRANSLATED);
96 l_chk_len(QUVI_SUBTITLE_LANG_PROPERTY_ORIGINAL);
97 l_chk_len(QUVI_SUBTITLE_LANG_PROPERTY_CODE);
100 static void test_subtitle_core()
102 quvi_subtitle_type_t qst;
103 quvi_subtitle_lang_t qsl;
104 quvi_subtitle_t qsub;
105 gdouble v;
106 quvi_t q;
107 gchar *s;
108 gint i,j;
110 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
111 return;
113 q = quvi_new();
114 g_assert(q != NULL);
115 g_assert_cmpint(qerr(q), ==, QUVI_OK);
117 chk_verbose(q);
119 qsub = quvi_subtitle_new(q, URLs[0]);
120 g_assert_cmpint(qerr_m(q, URLs[0]), ==, QUVI_OK);
121 g_assert(qsub != NULL);
123 /* The first subtitle type. */
124 qst = quvi_subtitle_type_next(qsub);
125 g_assert(qst != NULL);
127 /* type: Boundary check: the first -1 */
128 quvi_subtitle_type_get(qst, QUVI_SUBTITLE_TYPE_PROPERTY_FORMAT-1, &v);
129 g_assert_cmpint(qerr(q), ==, QUVI_ERROR_INVALID_ARG);
131 /* type: Boundary check: the last +1 */
132 quvi_subtitle_type_get(qst, QUVI_SUBTITLE_TYPE_PROPERTY_TYPE+1, &v);
133 g_assert_cmpint(qerr(q), ==, QUVI_ERROR_INVALID_ARG);
135 /* The first language of the first language. */
136 qsl = quvi_subtitle_lang_next(qst);
137 g_assert(qsl != NULL);
139 /* lang: Boundary check: the first -1 */
140 quvi_subtitle_lang_get(qst, QUVI_SUBTITLE_LANG_PROPERTY_TRANSLATED-1, &s);
141 g_assert_cmpint(qerr(q), ==, QUVI_ERROR_INVALID_ARG);
143 /* lang: Boundary check: the last +1 */
144 quvi_subtitle_lang_get(qst, QUVI_SUBTITLE_LANG_PROPERTY_ID+1, &v);
145 g_assert_cmpint(qerr(q), ==, QUVI_ERROR_INVALID_ARG);
147 _chk_shared_properties(q, qsub, qst, qsl);
149 quvi_subtitle_lang_reset(qst);
150 quvi_subtitle_type_reset(qsub);
152 for (i=0, j=0; (qst = quvi_subtitle_type_next(qsub)) != NULL; ++i)
154 for (; (qsl = quvi_subtitle_lang_next(qst)) != NULL; ++j);
157 quvi_subtitle_free(qsub);
158 quvi_free(q);
160 g_assert_cmpint(i, ==, 2);
161 g_assert_cmpint(j, >, 12);
164 static void test_subtitle_short()
166 static const gchar *URL = "http://is.gd/DIVaRF";
168 quvi_subtitle_type_t qst;
169 quvi_subtitle_lang_t qsl;
170 quvi_subtitle_t qsub;
171 quvi_t q;
173 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
174 return;
176 q = quvi_new();
177 g_assert(q != NULL);
178 g_assert_cmpint(qerr(q), ==, QUVI_OK);
180 chk_verbose(q);
182 qsub = quvi_subtitle_new(q, URL);
183 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
184 g_assert(qsub != NULL);
186 qst = quvi_subtitle_type_next(qsub);
187 g_assert(qst != NULL);
189 qsl = quvi_subtitle_lang_next(qst);
190 g_assert(qsl != NULL);
192 _chk_shared_properties(q, qsub, qst, qsl);
194 quvi_subtitle_free(qsub);
195 quvi_free(q);
198 static void test_subtitle_select()
200 quvi_subtitle_type_t qst;
201 quvi_subtitle_lang_t qsl;
202 quvi_subtitle_t qsub;
203 GSList *curr, *ids; /* language IDs */
204 quvi_t q;
205 gchar *s;
207 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
208 return;
210 q = quvi_new();
211 g_assert(q != NULL);
212 g_assert_cmpint(qerr(q), ==, QUVI_OK);
214 chk_verbose(q);
216 qsub = quvi_subtitle_new(q, URLs[0]);
217 g_assert_cmpint(qerr_m(q, URLs[0]), ==, QUVI_OK);
218 g_assert(qsub != NULL);
220 /* Construct the lookup list. */
221 ids = NULL;
222 while ( (qst = quvi_subtitle_type_next(qsub)) != NULL)
224 while ( (qsl = quvi_subtitle_lang_next(qst)) != NULL)
226 quvi_subtitle_lang_get(qsl, QUVI_SUBTITLE_LANG_PROPERTY_ID, &s);
227 ids = g_slist_prepend(ids, g_strdup(s));
230 ids = g_slist_reverse(ids);
232 /* Should croak. */
234 qsl = quvi_subtitle_select(qsub, "foo,bar,baz,croak,cc_en");
235 g_assert_cmpint(qerr(q), ==, QUVI_ERROR_KEYWORD_CROAK);
236 g_assert(qsl == NULL);
238 /* Should return cc_en. */
240 qsl = quvi_subtitle_select(qsub, "foo,bar,baz,cc_en");
241 g_assert_cmpint(qerr(q), ==, QUVI_OK);
242 g_assert(qsl != NULL);
244 quvi_subtitle_lang_get(qsl, QUVI_SUBTITLE_LANG_PROPERTY_TRANSLATED, &s);
245 g_assert_cmpint(qerr(q), ==, QUVI_OK);
246 g_assert_cmpstr(s, ==, "English");
248 quvi_subtitle_lang_get(qsl, QUVI_SUBTITLE_LANG_PROPERTY_ORIGINAL, &s);
249 g_assert_cmpint(qerr(q), ==, QUVI_OK);
250 g_assert_cmpstr(s, ==, "English");
252 quvi_subtitle_lang_get(qsl, QUVI_SUBTITLE_LANG_PROPERTY_CODE, &s);
253 g_assert_cmpint(qerr(q), ==, QUVI_OK);
254 g_assert_cmpstr(s, ==, "en");
256 quvi_subtitle_lang_get(qsl, QUVI_SUBTITLE_LANG_PROPERTY_URL, &s);
257 g_assert_cmpint(qerr(q), ==, QUVI_OK);
258 g_assert_cmpint(strlen(s), >, 1);
260 quvi_subtitle_lang_get(qsl, QUVI_SUBTITLE_LANG_PROPERTY_ID, &s);
261 g_assert_cmpint(qerr(q), ==, QUVI_OK);
262 g_assert_cmpstr(s, ==, "cc_en");
264 /* Should return the default language (the first). */
266 qsl = quvi_subtitle_select(qsub, "foo,bar,baz");
267 g_assert_cmpint(qerr(q), ==, QUVI_OK);
268 g_assert(qsl != NULL);
270 quvi_subtitle_lang_get(qsl, QUVI_SUBTITLE_LANG_PROPERTY_ID, &s);
271 g_assert_cmpint(qerr(q), ==, QUVI_OK);
272 curr = g_slist_nth(ids, 0);
273 g_assert_cmpstr(curr->data, ==, s);
275 g_slist_foreach(ids, (GFunc) g_free, NULL);
276 g_slist_free(ids);
278 quvi_subtitle_free(qsub);
279 quvi_free(q);
282 static void test_subtitle_nosupport()
284 static const gchar URL[] = "http://example.com";
286 quvi_subtitle_t qsub;
287 quvi_t q;
289 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
290 return;
292 q = quvi_new();
293 g_assert(q != NULL);
294 g_assert_cmpint(qerr(q), ==, QUVI_OK);
296 chk_verbose(q);
298 qsub = quvi_subtitle_new(q, URL);
299 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_ERROR_NO_SUPPORT);
300 g_assert(qsub != NULL);
301 g_assert(quvi_subtitle_type_next(qsub) == NULL);
303 quvi_subtitle_free(qsub);
304 quvi_free(q);
307 static void test_subtitle_sameq()
309 quvi_subtitle_type_t qst;
310 quvi_subtitle_lang_t qsl;
311 quvi_subtitle_t qsub;
312 quvi_t q;
313 gint i;
315 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
316 return;
318 q = quvi_new();
319 g_assert(q != NULL);
320 g_assert_cmpint(qerr(q), ==, QUVI_OK);
322 chk_verbose(q);
324 i = 0;
325 while (URLs[i] != NULL)
327 qsub = quvi_subtitle_new(q, URLs[i]);
328 g_assert_cmpint(qerr_m(q, URLs[i]), ==, QUVI_OK);
329 g_assert(qsub != NULL);
331 qst = quvi_subtitle_type_next(qsub);
332 g_assert(qst != NULL);
333 t_chk_val(QUVI_SUBTITLE_TYPE_PROPERTY_TYPE);
335 qsl = quvi_subtitle_lang_next(qst);
336 g_assert(qsl != NULL);
337 l_chk_len(QUVI_SUBTITLE_LANG_PROPERTY_ID);
339 quvi_subtitle_free(qsub);
340 ++i;
342 quvi_free(q);
345 gint main(gint argc, gchar **argv)
347 g_test_init(&argc, &argv, NULL);
348 g_test_add_func("/quvi/subtitle (core)", test_subtitle_core);
349 g_test_add_func("/quvi/subtitle (short)", test_subtitle_short);
350 g_test_add_func("/quvi/subtitle (select)", test_subtitle_select);
351 g_test_add_func("/quvi/subtitle (nosupport)", test_subtitle_nosupport);
352 g_test_add_func("/quvi/subtitle (sameq)", test_subtitle_sameq);
353 return (g_test_run());
356 /* vim: set ts=2 sw=2 tw=72 expandtab: */