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(quvi_errcode(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(quvi_errcode(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_export_t qse
;
103 quvi_subtitle_type_t qst
;
104 quvi_subtitle_lang_t qsl
;
105 quvi_subtitle_t qsub
;
111 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
116 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
120 qsub
= quvi_subtitle_new(q
, URLs
[0]);
121 g_assert_cmpint(qerr_m(q
, URLs
[0]), ==, QUVI_OK
);
122 g_assert(qsub
!= NULL
);
124 /* The first subtitle type. */
125 qst
= quvi_subtitle_type_next(qsub
);
126 g_assert(qst
!= NULL
);
128 /* type: Boundary check: the first -1 */
129 quvi_subtitle_type_get(qst
, QUVI_SUBTITLE_TYPE_PROPERTY_FORMAT
-1, &v
);
130 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_ERROR_INVALID_ARG
);
132 /* type: Boundary check: the last +1 */
133 quvi_subtitle_type_get(qst
, QUVI_SUBTITLE_TYPE_PROPERTY_TYPE
+1, &v
);
134 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_ERROR_INVALID_ARG
);
136 /* The first language of the first language. */
137 qsl
= quvi_subtitle_lang_next(qst
);
138 g_assert(qsl
!= NULL
);
140 /* lang: Boundary check: the first -1 */
141 quvi_subtitle_lang_get(qst
, QUVI_SUBTITLE_LANG_PROPERTY_TRANSLATED
-1, &s
);
142 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_ERROR_INVALID_ARG
);
144 /* lang: Boundary check: the last +1 */
145 quvi_subtitle_lang_get(qst
, QUVI_SUBTITLE_LANG_PROPERTY_ID
+1, &v
);
146 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_ERROR_INVALID_ARG
);
148 _chk_shared_properties(q
, qsub
, qst
, qsl
);
150 quvi_subtitle_lang_reset(qst
);
151 quvi_subtitle_type_reset(qsub
);
153 for (i
=0, j
=0; (qst
= quvi_subtitle_type_next(qsub
)) != NULL
; ++i
)
155 for (; (qsl
= quvi_subtitle_lang_next(qst
)) != NULL
; ++j
);
158 g_assert_cmpint(i
, ==, 2);
159 g_assert_cmpint(j
, >, 12);
163 qsl
= quvi_subtitle_select(qsub
, "foo"); /* use the first available */
164 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
166 qse
= quvi_subtitle_export_new(qsl
, "srt");
167 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
168 g_assert_cmpint(strlen(quvi_subtitle_export_data(qse
)), >, 0);
169 quvi_subtitle_export_free(qse
);
171 qse
= quvi_subtitle_export_new(qsl
, "foo");
172 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_ERROR_NO_SUPPORT
);
173 quvi_subtitle_export_free(qse
);
175 quvi_subtitle_free(qsub
);
179 static void test_subtitle_short()
181 static const gchar
*URL
= "http://is.gd/DIVaRF";
183 quvi_subtitle_type_t qst
;
184 quvi_subtitle_lang_t qsl
;
185 quvi_subtitle_t qsub
;
188 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
193 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
197 qsub
= quvi_subtitle_new(q
, URL
);
198 g_assert_cmpint(qerr_m(q
, URL
), ==, QUVI_OK
);
199 g_assert(qsub
!= NULL
);
201 qst
= quvi_subtitle_type_next(qsub
);
202 g_assert(qst
!= NULL
);
204 qsl
= quvi_subtitle_lang_next(qst
);
205 g_assert(qsl
!= NULL
);
207 _chk_shared_properties(q
, qsub
, qst
, qsl
);
209 quvi_subtitle_free(qsub
);
213 static void test_subtitle_select()
215 quvi_subtitle_type_t qst
;
216 quvi_subtitle_lang_t qsl
;
217 quvi_subtitle_t qsub
;
218 GSList
*curr
, *ids
; /* language IDs */
222 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
227 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
231 qsub
= quvi_subtitle_new(q
, URLs
[0]);
232 g_assert_cmpint(qerr_m(q
, URLs
[0]), ==, QUVI_OK
);
233 g_assert(qsub
!= NULL
);
235 /* Construct the lookup list. */
237 while ( (qst
= quvi_subtitle_type_next(qsub
)) != NULL
)
239 while ( (qsl
= quvi_subtitle_lang_next(qst
)) != NULL
)
241 quvi_subtitle_lang_get(qsl
, QUVI_SUBTITLE_LANG_PROPERTY_ID
, &s
);
242 ids
= g_slist_prepend(ids
, g_strdup(s
));
245 ids
= g_slist_reverse(ids
);
249 qsl
= quvi_subtitle_select(qsub
, "foo,bar,baz,croak,cc_en");
250 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_ERROR_KEYWORD_CROAK
);
251 g_assert(qsl
== NULL
);
253 /* Should return cc_en. */
255 qsl
= quvi_subtitle_select(qsub
, "foo,bar,baz,cc_en");
256 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
257 g_assert(qsl
!= NULL
);
259 quvi_subtitle_lang_get(qsl
, QUVI_SUBTITLE_LANG_PROPERTY_TRANSLATED
, &s
);
260 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
261 g_assert_cmpstr(s
, ==, "English");
263 quvi_subtitle_lang_get(qsl
, QUVI_SUBTITLE_LANG_PROPERTY_ORIGINAL
, &s
);
264 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
265 g_assert_cmpstr(s
, ==, "English");
267 quvi_subtitle_lang_get(qsl
, QUVI_SUBTITLE_LANG_PROPERTY_CODE
, &s
);
268 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
269 g_assert_cmpstr(s
, ==, "en");
271 quvi_subtitle_lang_get(qsl
, QUVI_SUBTITLE_LANG_PROPERTY_URL
, &s
);
272 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
273 g_assert_cmpint(strlen(s
), >, 1);
275 quvi_subtitle_lang_get(qsl
, QUVI_SUBTITLE_LANG_PROPERTY_ID
, &s
);
276 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
277 g_assert_cmpstr(s
, ==, "cc_en");
279 /* Should return the default language (the first). */
281 qsl
= quvi_subtitle_select(qsub
, "foo,bar,baz");
282 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
283 g_assert(qsl
!= NULL
);
285 quvi_subtitle_lang_get(qsl
, QUVI_SUBTITLE_LANG_PROPERTY_ID
, &s
);
286 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
287 curr
= g_slist_nth(ids
, 0);
288 g_assert_cmpstr(curr
->data
, ==, s
);
290 g_slist_foreach(ids
, (GFunc
) g_free
, NULL
);
293 quvi_subtitle_free(qsub
);
297 static void test_subtitle_nosupport()
299 static const gchar URL
[] = "http://example.com";
301 quvi_subtitle_t qsub
;
304 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
309 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
313 qsub
= quvi_subtitle_new(q
, URL
);
314 g_assert_cmpint(qerr_m(q
, URL
), ==, QUVI_ERROR_NO_SUPPORT
);
315 g_assert(qsub
!= NULL
);
316 g_assert(quvi_subtitle_type_next(qsub
) == NULL
);
318 quvi_subtitle_free(qsub
);
322 static void test_subtitle_sameq()
324 quvi_subtitle_type_t qst
;
325 quvi_subtitle_lang_t qsl
;
326 quvi_subtitle_t qsub
;
330 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
335 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
340 while (URLs
[i
] != NULL
)
342 qsub
= quvi_subtitle_new(q
, URLs
[i
]);
343 g_assert_cmpint(qerr_m(q
, URLs
[i
]), ==, QUVI_OK
);
344 g_assert(qsub
!= NULL
);
346 qst
= quvi_subtitle_type_next(qsub
);
347 g_assert(qst
!= NULL
);
348 t_chk_val(QUVI_SUBTITLE_TYPE_PROPERTY_TYPE
);
350 qsl
= quvi_subtitle_lang_next(qst
);
351 g_assert(qsl
!= NULL
);
352 l_chk_len(QUVI_SUBTITLE_LANG_PROPERTY_ID
);
354 quvi_subtitle_free(qsub
);
360 gint
main(gint argc
, gchar
**argv
)
362 g_test_init(&argc
, &argv
, NULL
);
363 g_test_add_func("/quvi/subtitle (core)", test_subtitle_core
);
364 g_test_add_func("/quvi/subtitle (short)", test_subtitle_short
);
365 g_test_add_func("/quvi/subtitle (select)", test_subtitle_select
);
366 g_test_add_func("/quvi/subtitle (nosupport)", test_subtitle_nosupport
);
367 g_test_add_func("/quvi/subtitle (sameq)", test_subtitle_sameq
);
368 return (g_test_run());
371 /* vim: set ts=2 sw=2 tw=72 expandtab: */