test_media_multi: Replace test URL
[libquvi.git] / tests / media.c
blobe61b9634c9305b495847caa1f5b5a9a19237f6ef
1 /* libquvi
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
27 #include "config.h"
29 #include <string.h>
30 #include <glib.h>
31 #include <quvi.h>
33 #include "tests.h"
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",
39 NULL
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",
46 NULL
49 #define chk_len(p) \
50 do {\
51 gchar *s = NULL; \
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); \
56 } while (0)
58 #define chk_val(p) \
59 do {\
60 gdouble n = -1; \
61 quvi_media_get(qm, p, &n); \
62 g_test_message("%s=%f", #p, n); \
63 g_assert_cmpfloat(n, >, -1);\
64 } while (0)
66 static void test_media_core()
68 quvi_media_t qm;
69 quvi_t q;
70 gchar *s;
72 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
73 return;
75 q = quvi_new();
76 g_assert(q != NULL);
77 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
79 chk_verbose(q);
81 qm = quvi_media_new(q, URLs[0]);
82 g_assert_cmpint(qerr_m(q, URLs[0]), ==, QUVI_OK);
83 g_assert(qm != NULL);
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);
93 /* string */
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);
100 /* double */
101 chk_val(QUVI_MEDIA_PROPERTY_START_TIME_MS);
102 chk_val(QUVI_MEDIA_PROPERTY_DURATION_MS);
104 quvi_media_free(qm);
105 quvi_free(q);
108 static void test_media_multi()
110 static const gchar URL[] = "http://www.youtube.com/watch?v=G4evlxq34og";
112 quvi_media_t qm;
113 GString *b;
114 gchar *s;
115 quvi_t q;
116 gint i;
118 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
119 return;
121 q = quvi_new();
122 g_assert(q != NULL);
123 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
125 chk_verbose(q);
127 qm = quvi_media_new(q, URL);
128 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
129 g_assert(qm != NULL);
131 /* string */
132 chk_len(QUVI_MEDIA_PROPERTY_THUMBNAIL_URL);
133 chk_len(QUVI_MEDIA_PROPERTY_TITLE);
134 chk_len(QUVI_MEDIA_PROPERTY_ID);
136 /* double */
137 chk_val(QUVI_MEDIA_PROPERTY_START_TIME_MS);
138 chk_val(QUVI_MEDIA_PROPERTY_DURATION_MS);
140 b = g_string_new(NULL);
141 i = 0;
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);
164 ++i;
166 g_assert_cmpint(i, >, 1);
168 g_string_free(b, TRUE);
169 b = NULL;
171 quvi_media_free(qm);
172 quvi_free(q);
175 static void test_media_select()
177 static const gchar URL[] =
178 "http://www.spiegel.de/video/64-jaehrige-schwimmt-von-kuba-richtung-florida-video-1293301.html";
180 quvi_media_t qm;
181 gchar *s, *best;
182 GSList *curr;
183 GSList *ids;
184 quvi_t q;
186 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
187 return;
189 q = quvi_new();
190 g_assert(q != NULL);
191 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
193 chk_verbose(q);
195 qm = quvi_media_new(q, URL);
196 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
197 g_assert(qm != NULL);
199 ids = NULL;
200 while (quvi_media_stream_next(qm) == QUVI_TRUE)
202 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
203 ids = g_slist_prepend(ids, g_strdup(s));
205 ids = g_slist_reverse(ids);
208 * Default stream. This should also advance the current list pointer,
209 * so that when quvi_media_stream_next is called the next time, it
210 * should return the 2nd stream in the list, not the first one.
213 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
214 curr = g_slist_nth(ids, 0);
215 g_assert_cmpstr(curr->data, ==, s);
217 quvi_media_stream_next(qm);
218 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
219 curr = g_slist_nth(ids, 1);
220 g_assert_cmpstr(curr->data, ==, s);
222 /* Reset. */
224 quvi_media_stream_reset(qm);
225 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
226 curr = g_slist_nth(ids, 0);
227 g_assert_cmpstr(curr->data, ==, s);
229 /* The best stream -- anything but the default (first) stream. */
231 quvi_media_stream_choose_best(qm);
232 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
233 curr = g_slist_nth(ids, 0);
234 g_assert_cmpstr(curr->data, !=, s);
235 best = g_strdup(s);
237 /* Select. */
239 quvi_media_stream_select(qm, "foo,bar,baz,best,croak");
240 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
241 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
242 g_assert_cmpstr(s, ==, best);
243 g_free(best);
245 quvi_media_stream_select(qm, "foo,bar,baz,croak");
246 g_assert_cmpint(quvi_errcode(q), ==, QUVI_ERROR_KEYWORD_CROAK);
247 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
248 curr = g_slist_nth(ids, 0);
249 g_assert_cmpstr(curr->data, ==, s);
251 quvi_media_stream_select(qm, "foo,bar,baz");
252 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
253 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
254 curr = g_slist_nth(ids, 0); /* Should be the default stream (first) */
255 g_assert_cmpstr(curr->data, ==, s);
257 quvi_media_stream_select(qm, "^\\w\\w\\d_\\w+_40\\dk_\\d20p$,bar,baz,croak");
258 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
259 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
260 g_assert_cmpstr(s, ==, "mp4_mpeg4_404k_320p");
262 quvi_media_stream_select(qm, "foo,^\\w\\w\\d_\\w+_14\\d+k_\\d+6p$,baz,croak");
263 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
264 quvi_media_get(qm, QUVI_MEDIA_STREAM_PROPERTY_ID, &s);
265 g_assert_cmpstr(s, ==, "mp4_h264_1400k_576p");
267 quvi_media_free(qm);
268 quvi_free(q);
271 static void test_media_short()
273 static const gchar URL[] = "http://tinyurl.com/d8s3y54";
275 quvi_media_t qm;
276 quvi_t q;
277 gchar *s;
279 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
280 return;
282 q = quvi_new();
283 g_assert(q != NULL);
284 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
286 chk_verbose(q);
288 qm = quvi_media_new(q, URL);
289 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
290 g_assert(qm != NULL);
292 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
293 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
294 g_assert_cmpstr(s, ==, TITLEs[1]);
296 quvi_media_free(qm);
297 quvi_free(q);
300 static void test_media_starttime()
302 static const gchar *URL =
303 "http://www.youtube.com/watch?v=LWxTGJ3TK1U#t=2m22s";
305 quvi_media_t qm;
306 gdouble st = 0;
307 quvi_t q;
309 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
310 return;
312 q = quvi_new();
313 g_assert(q != NULL);
314 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
316 chk_verbose(q);
318 qm = quvi_media_new(q, URL);
319 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
320 g_assert(qm != NULL);
322 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_START_TIME_MS, &st);
323 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
324 g_assert_cmpint(st, ==, 142000);
326 /* YouTube videos should give us this data. */
327 chk_len(QUVI_MEDIA_STREAM_PROPERTY_AUDIO_ENCODING);
329 quvi_media_free(qm);
330 quvi_free(q);
333 static void test_media_escaped_url()
335 static gchar URL[] =
336 "http%3A//www.liveleak.com/view%3Fi%3D77f_1378178228";
338 quvi_media_t qm;
339 quvi_t q;
341 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
342 return;
344 q = quvi_new();
345 g_assert(q != NULL);
346 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
348 chk_verbose(q);
350 qm = quvi_media_new(q, URL);
351 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_OK);
352 g_assert(qm != NULL);
354 quvi_media_free(qm);
355 quvi_free(q);
358 static void test_media_nosupport()
360 static const gchar URL[] = "http://example.com";
362 quvi_media_t qm;
363 quvi_t q;
364 gchar *s;
366 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
367 return;
369 q = quvi_new();
370 g_assert(q != NULL);
371 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
373 chk_verbose(q);
375 qm = quvi_media_new(q, URL);
376 g_assert_cmpint(qerr_m(q, URL), ==, QUVI_ERROR_NO_SUPPORT);
377 g_assert(qm != NULL);
379 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
380 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
381 g_assert_cmpstr(s, ==, "");
383 quvi_media_free(qm);
384 quvi_free(q);
387 static void test_media_same_q()
389 quvi_media_t qm;
390 gint i = 0;
391 quvi_t q;
393 if (chk_internet() == FALSE || chk_skip(__func__) == TRUE)
394 return;
396 q = quvi_new();
397 g_assert(q != NULL);
398 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
400 chk_verbose(q);
402 for (; URLs[i] != NULL; ++i)
404 gchar *s = NULL;
406 qm = quvi_media_new(q, URLs[i]);
407 g_assert_cmpint(qerr_m(q, URLs[i]), ==, QUVI_OK);
408 g_assert(qm != NULL);
410 quvi_media_get(qm, QUVI_MEDIA_PROPERTY_TITLE, &s);
411 g_assert_cmpint(quvi_errcode(q), ==, QUVI_OK);
412 g_assert_cmpstr(s, ==, TITLEs[i]);
414 quvi_media_free(qm);
416 quvi_free(q);
419 gint main(gint argc, gchar **argv)
421 g_test_init(&argc, &argv, NULL);
422 g_test_add_func("/quvi/media (core)", test_media_core);
423 g_test_add_func("/quvi/media (>1 streams)", test_media_multi);
424 g_test_add_func("/quvi/media (select)", test_media_select);
425 g_test_add_func("/quvi/media (short)", test_media_short);
426 g_test_add_func("/quvi/media (escaped URL)", test_media_escaped_url);
427 g_test_add_func("/quvi/media (nosupport)", test_media_nosupport);
428 g_test_add_func("/quvi/media (start_time)", test_media_starttime);
429 g_test_add_func("/quvi/media (same handle)", test_media_same_q);
430 return (g_test_run());
433 /* vim: set ts=2 sw=2 tw=72 expandtab: */