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/>.
29 static const gchar
*URLs
[] =
31 "http://youtube.com/watch?v=0QRO3gKj3qw",
32 "http://youtube.com/watch?v=ntLPcVAyNPE",
36 static void _t_chk_val(quvi_subtitle_type_t qst
,
37 const QuviSubtitleTypeProperty p
,
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
,
54 quvi_subtitle_lang_get(qsl
, p
, &s
);
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
)
71 t_chk_val(QUVI_SUBTITLE_TYPE_PROPERTY_FORMAT
);
72 t_chk_val(QUVI_SUBTITLE_TYPE_PROPERTY_TYPE
);
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
;
110 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
115 g_assert_cmpint(qerr(q
), ==, QUVI_OK
);
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
);
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
;
173 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
178 g_assert_cmpint(qerr(q
), ==, QUVI_OK
);
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
);
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 */
207 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
212 g_assert_cmpint(qerr(q
), ==, QUVI_OK
);
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. */
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
);
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
);
278 quvi_subtitle_free(qsub
);
282 static void test_subtitle_nosupport()
284 static const gchar URL
[] = "http://example.com";
286 quvi_subtitle_t qsub
;
289 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
294 g_assert_cmpint(qerr(q
), ==, QUVI_OK
);
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
);
307 static void test_subtitle_sameq()
309 quvi_subtitle_type_t qst
;
310 quvi_subtitle_lang_t qsl
;
311 quvi_subtitle_t qsub
;
315 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
320 g_assert_cmpint(qerr(q
), ==, QUVI_OK
);
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
);
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: */