2 * Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <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/>.
22 * TODO: When a suitable test URL is found
23 * - Add check for QUVI_MEDIA_STREAM_PROPERTY_VIDEO_BITRATE_KBIT_S
24 * - Add check for QUVI_MEDIA_STREAM_PROPERTY_AUDIO_BITRATE_KBIT_S
35 static gchar
*URLs
[] =
37 "http://www.gaskrank.tv/tv/motorrad-oldtimer/1928-henderson-deluxe-alt-und--19115.htm",
38 "http://www.gaskrank.tv/tv/motorrad-oldtimer/1912-harley-davidson-superscho-9481.htm",
42 static gchar
*TITLEs
[] =
44 "1928 Henderson Deluxe - alt und mit der Patina des Alters aber läuft",
45 "1912 Harley Davidson - superschön restauriert",
52 quvi_media_get(qm, p, &s); \
53 g_assert(s != NULL); \
54 g_test_message("%s=%s", #p, s); \
55 g_assert_cmpint(strlen(s), >, 1); \
61 quvi_media_get(qm, p, &n); \
62 g_test_message("%s=%f", #p, n); \
63 g_assert_cmpfloat(n, >, -1);\
66 static void test_media_core()
72 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
77 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
81 qm
= quvi_media_new(q
, URLs
[0]);
82 g_assert_cmpint(qerr_m(q
, URLs
[0]), ==, QUVI_OK
);
85 /* Boundary check: the first -1 */
86 quvi_media_get(qm
, QUVI_MEDIA_PROPERTY_THUMBNAIL_URL
-1, &s
);
87 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_ERROR_INVALID_ARG
);
89 /* Boundary check: the last +1 */
90 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_VIDEO_WIDTH
+1, &s
);
91 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_ERROR_INVALID_ARG
);
94 chk_len(QUVI_MEDIA_PROPERTY_THUMBNAIL_URL
);
95 chk_len(QUVI_MEDIA_PROPERTY_TITLE
);
96 chk_len(QUVI_MEDIA_PROPERTY_ID
);
98 chk_len(QUVI_MEDIA_STREAM_PROPERTY_URL
);
101 chk_val(QUVI_MEDIA_PROPERTY_START_TIME_MS
);
102 chk_val(QUVI_MEDIA_PROPERTY_DURATION_MS
);
108 static void test_media_multi()
110 static const gchar URL
[] = "http://www.youtube.com/watch?v=G4evlxq34og";
118 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
123 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
127 qm
= quvi_media_new(q
, URL
);
128 g_assert_cmpint(qerr_m(q
, URL
), ==, QUVI_OK
);
129 g_assert(qm
!= NULL
);
132 chk_len(QUVI_MEDIA_PROPERTY_THUMBNAIL_URL
);
133 chk_len(QUVI_MEDIA_PROPERTY_TITLE
);
134 chk_len(QUVI_MEDIA_PROPERTY_ID
);
137 chk_val(QUVI_MEDIA_PROPERTY_START_TIME_MS
);
138 chk_val(QUVI_MEDIA_PROPERTY_DURATION_MS
);
140 b
= g_string_new(NULL
);
143 while (quvi_media_stream_next(qm
) == QUVI_TRUE
)
145 chk_len(QUVI_MEDIA_STREAM_PROPERTY_URL
);
146 chk_len(QUVI_MEDIA_STREAM_PROPERTY_ID
);
148 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_URL
, &s
);
149 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
151 if (b
->len
>0) /* Make sure each media stream URL is unique */
152 g_assert_cmpstr(b
->str
, !=, s
);
153 g_string_assign(b
, s
);
155 chk_len(QUVI_MEDIA_STREAM_PROPERTY_CONTAINER
);
156 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_CONTAINER
, &s
);
157 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
159 if (strstr(s
, "flv") ==NULL
) /* FLVs do not come with these */
161 chk_len(QUVI_MEDIA_STREAM_PROPERTY_VIDEO_ENCODING
);
162 chk_len(QUVI_MEDIA_STREAM_PROPERTY_AUDIO_ENCODING
);
166 g_assert_cmpint(i
, >, 1);
168 g_string_free(b
, TRUE
);
175 static void test_media_select()
177 static const gchar URL
[] = "http://www.youtube.com/watch?v=G4evlxq34og";
185 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
190 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
194 qm
= quvi_media_new(q
, URL
);
195 g_assert_cmpint(qerr_m(q
, URL
), ==, QUVI_OK
);
196 g_assert(qm
!= NULL
);
199 while (quvi_media_stream_next(qm
) == QUVI_TRUE
)
201 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_ID
, &s
);
202 ids
= g_slist_prepend(ids
, g_strdup(s
));
204 ids
= g_slist_reverse(ids
);
207 * Default stream. This should also advance the current list pointer,
208 * so that when quvi_media_stream_next is called the next time, it
209 * should return the 2nd stream in the list, not the first one.
212 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_ID
, &s
);
213 curr
= g_slist_nth(ids
, 0);
214 g_assert_cmpstr(curr
->data
, ==, s
);
216 quvi_media_stream_next(qm
);
217 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_ID
, &s
);
218 curr
= g_slist_nth(ids
, 1);
219 g_assert_cmpstr(curr
->data
, ==, s
);
223 quvi_media_stream_reset(qm
);
224 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_ID
, &s
);
225 curr
= g_slist_nth(ids
, 0);
226 g_assert_cmpstr(curr
->data
, ==, s
);
228 /* The best stream, same as the first stream. */
230 quvi_media_stream_choose_best(qm
);
231 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_ID
, &s
);
232 curr
= g_slist_nth(ids
, 0);
233 g_assert_cmpstr(curr
->data
, ==, s
);
238 quvi_media_stream_select(qm
, "foo,bar,baz,best,croak");
239 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
240 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_ID
, &s
);
241 g_assert_cmpstr(s
, ==, best
);
244 quvi_media_stream_select(qm
, "foo,bar,baz,croak");
245 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_ERROR_KEYWORD_CROAK
);
246 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_ID
, &s
);
247 curr
= g_slist_nth(ids
, 0);
248 g_assert_cmpstr(curr
->data
, ==, s
);
250 quvi_media_stream_select(qm
, "foo,bar,baz");
251 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
252 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_ID
, &s
);
253 curr
= g_slist_nth(ids
, 0); /* Should be the default stream (first) */
254 g_assert_cmpstr(curr
->data
, ==, s
);
256 quvi_media_stream_select(qm
, "^\\w+_\\w+_i\\d+_\\d40p$,bar,baz,croak");
257 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
258 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_ID
, &s
);
259 g_assert_cmpstr(s
, ==, "medium_webm_i43_240p");
261 quvi_media_stream_select(qm
, "foo,144p$,baz,croak");
262 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
263 quvi_media_get(qm
, QUVI_MEDIA_STREAM_PROPERTY_ID
, &s
);
264 g_assert_cmpstr(s
, ==, "small_3gpp_i17_144p");
270 static void test_media_short()
272 static const gchar URL
[] = "http://tinyurl.com/d8s3y54";
278 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
283 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
287 qm
= quvi_media_new(q
, URL
);
288 g_assert_cmpint(qerr_m(q
, URL
), ==, QUVI_OK
);
289 g_assert(qm
!= NULL
);
291 quvi_media_get(qm
, QUVI_MEDIA_PROPERTY_TITLE
, &s
);
292 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
293 g_assert_cmpstr(s
, ==, TITLEs
[1]);
299 static void test_media_starttime()
301 static const gchar
*URL
=
302 "http://www.youtube.com/watch?v=LWxTGJ3TK1U#t=2m22s";
308 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
313 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
317 qm
= quvi_media_new(q
, URL
);
318 g_assert_cmpint(qerr_m(q
, URL
), ==, QUVI_OK
);
319 g_assert(qm
!= NULL
);
321 quvi_media_get(qm
, QUVI_MEDIA_PROPERTY_START_TIME_MS
, &st
);
322 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
323 g_assert_cmpint(st
, ==, 142000);
325 /* YouTube videos should give us this data. */
326 chk_len(QUVI_MEDIA_STREAM_PROPERTY_AUDIO_ENCODING
);
332 static void test_media_escaped_url()
335 "http%3A//www.youtube.com/watch%3Fv%3DLWxTGJ3TK1U%23t%3D2m22s";
340 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
345 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
349 qm
= quvi_media_new(q
, URL
);
350 g_assert_cmpint(qerr_m(q
, URL
), ==, QUVI_OK
);
351 g_assert(qm
!= NULL
);
357 static void test_media_escaped_utf8_url()
360 "http://flix.tapuz.co.il/v/watch-4323227-%D7%9C%D7%A2%D7%A7%D7%95%D7%A8%20%D7%A9%D7%9F%20%D7%9C%D7%98%D7%99%D7%92%D7%A8%D7%99%D7%A1.html";
365 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
370 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
374 qm
= quvi_media_new(q
, URL
);
375 g_assert_cmpint(qerr_m(q
, URL
), ==, QUVI_OK
);
376 g_assert(qm
!= NULL
);
382 static void test_media_nosupport()
384 static const gchar URL
[] = "http://example.com";
390 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
395 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
399 qm
= quvi_media_new(q
, URL
);
400 g_assert_cmpint(qerr_m(q
, URL
), ==, QUVI_ERROR_NO_SUPPORT
);
401 g_assert(qm
!= NULL
);
403 quvi_media_get(qm
, QUVI_MEDIA_PROPERTY_TITLE
, &s
);
404 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
405 g_assert_cmpstr(s
, ==, "");
411 static void test_media_same_q()
417 if (chk_internet() == FALSE
|| chk_skip(__func__
) == TRUE
)
422 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
426 for (; URLs
[i
] != NULL
; ++i
)
430 qm
= quvi_media_new(q
, URLs
[i
]);
431 g_assert_cmpint(qerr_m(q
, URLs
[i
]), ==, QUVI_OK
);
432 g_assert(qm
!= NULL
);
434 quvi_media_get(qm
, QUVI_MEDIA_PROPERTY_TITLE
, &s
);
435 g_assert_cmpint(quvi_errcode(q
), ==, QUVI_OK
);
436 g_assert_cmpstr(s
, ==, TITLEs
[i
]);
443 gint
main(gint argc
, gchar
**argv
)
445 g_test_init(&argc
, &argv
, NULL
);
446 g_test_add_func("/quvi/media (core)", test_media_core
);
447 g_test_add_func("/quvi/media (>1 streams)", test_media_multi
);
448 g_test_add_func("/quvi/media (select)", test_media_select
);
449 g_test_add_func("/quvi/media (short)", test_media_short
);
450 g_test_add_func("/quvi/media (escaped URL)", test_media_escaped_url
);
451 g_test_add_func("/quvi/media (escaped UTF8 URL)",
452 test_media_escaped_utf8_url
);
453 g_test_add_func("/quvi/media (nosupport)", test_media_nosupport
);
454 g_test_add_func("/quvi/media (start_time)", test_media_starttime
);
455 g_test_add_func("/quvi/media (same handle)", test_media_same_q
);
456 return (g_test_run());
459 /* vim: set ts=2 sw=2 tw=72 expandtab: */