| 1 |
/* |
| 2 |
* Copyright (C) 2016 Grilo Project |
| 3 |
* |
| 4 |
* Author: Victor Toso <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public License |
| 8 |
* as published by the Free Software Foundation; version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 |
* 02110-1301 USA |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <locale.h> |
| 24 |
#include "test_lua_factory_utils.h" |
| 25 |
|
| 26 |
#define ACOUSTID_ID "grl-acoustid" |
| 27 |
#define CHROMAPRINT_ID "grl-chromaprint" |
| 28 |
|
| 29 |
#define TEST_PLUGINS_PATH LUA_FACTORY_PLUGIN_PATH ";" CHROMAPRINT_PLUGIN_PATH |
| 30 |
#define TEST_PLUGINS_LOAD LUA_FACTORY_ID ":" CHROMAPRINT_ID |
| 31 |
|
| 32 |
#define ACOUSTID_OPS GRL_OP_RESOLVE |
| 33 |
|
| 34 |
#define GRESOURCE_PREFIX "resource:///org/gnome/grilo/plugins/test/acoustid/data/" |
| 35 |
|
| 36 |
#define FINGERPRINT_LUDOVICO_EI GRESOURCE_PREFIX "chromaprint_ludovico_einaudi_primavera.txt" |
| 37 |
#define FINGERPRINT_NORAH_JONES GRESOURCE_PREFIX "chromaprint_norah_jones_chasing_pirates.txt" |
| 38 |
#define FINGERPRINT_PHILIP_GLAS GRESOURCE_PREFIX "chromaprint_philip_glass_the_passion_of.txt" |
| 39 |
#define FINGERPRINT_TROMBONE_SH GRESOURCE_PREFIX "chromaprint_trombone_shorty_buckjump.txt" |
| 40 |
#define FINGERPRINT_RADIOHEAD_PA GRESOURCE_PREFIX "chromaprint_radiohead_paranoid_android.txt" |
| 41 |
|
| 42 |
static void |
| 43 |
test_query_by_fingerprint (void) |
| 44 |
{ |
| 45 |
GList *keys; |
| 46 |
GrlOperationOptions *options; |
| 47 |
GDateTime *date; |
| 48 |
gchar *fingerprint, *publication_date, *query; |
| 49 |
GrlMedia *media; |
| 50 |
GError *error = NULL; |
| 51 |
GList *results; |
| 52 |
GFile *file; |
| 53 |
gsize size; |
| 54 |
GrlSource *source; |
| 55 |
gint duration = 445; |
| 56 |
|
| 57 |
file = g_file_new_for_uri (FINGERPRINT_LUDOVICO_EI); |
| 58 |
g_file_load_contents (file, NULL, &fingerprint, &size, NULL, &error); |
| 59 |
|
| 60 |
query = g_strdup_printf("duration=%d&fingerprint=%s", duration, fingerprint); |
| 61 |
|
| 62 |
source = test_lua_factory_get_source (ACOUSTID_ID, ACOUSTID_OPS); |
| 63 |
|
| 64 |
keys = grl_metadata_key_list_new (GRL_METADATA_KEY_MB_ARTIST_ID, |
| 65 |
GRL_METADATA_KEY_ARTIST, |
| 66 |
GRL_METADATA_KEY_ALBUM_ARTIST, |
| 67 |
GRL_METADATA_KEY_MB_RELEASE_ID, |
| 68 |
GRL_METADATA_KEY_MB_RELEASE_GROUP_ID, |
| 69 |
GRL_METADATA_KEY_ALBUM, |
| 70 |
GRL_METADATA_KEY_MB_RECORDING_ID, |
| 71 |
GRL_METADATA_KEY_TITLE, |
| 72 |
GRL_METADATA_KEY_ALBUM_DISC_NUMBER, |
| 73 |
GRL_METADATA_KEY_PUBLICATION_DATE, |
| 74 |
GRL_METADATA_KEY_TRACK_NUMBER, |
| 75 |
NULL); |
| 76 |
options = grl_operation_options_new (NULL); |
| 77 |
grl_operation_options_set_resolution_flags (options, GRL_RESOLVE_NORMAL); |
| 78 |
|
| 79 |
results = grl_source_query_sync (source, |
| 80 |
query, |
| 81 |
keys, |
| 82 |
options, |
| 83 |
&error); |
| 84 |
g_assert_no_error (error); |
| 85 |
g_assert_cmpint (g_list_length (results), ==, 8); |
| 86 |
|
| 87 |
media = GRL_MEDIA (results->data); |
| 88 |
g_assert_cmpstr (grl_media_get_title (media), ==, "Primavera"); |
| 89 |
g_assert_cmpstr (grl_media_get_album (media), ==, "Classic FM Summer Guitar"); |
| 90 |
g_assert_cmpstr (grl_media_get_album_artist (media), ==, "Various Artists"); |
| 91 |
g_assert_cmpstr (grl_media_get_artist (media), ==, "Craig Ogden"); |
| 92 |
g_assert_cmpstr (grl_media_get_mb_recording_id (media), ==, "5d72b7d4-d0c4-4d0d-ab7f-3a737075e1c9"); |
| 93 |
g_assert_cmpstr (grl_media_get_mb_release_id (media), ==, "5fd10cc8-30e0-48aa-9ba1-19d05b871a75"); |
| 94 |
g_assert_cmpstr (grl_media_get_mb_release_group_id (media), ==, "2cde60bc-829c-49af-a62c-e20283167c30"); |
| 95 |
g_assert_cmpstr (grl_media_get_mb_artist_id (media), ==, "e7d8aea3-9c1d-4fe0-b93a-481d545296fc"); |
| 96 |
g_assert_cmpint (grl_media_get_album_disc_number (media), ==, 1); |
| 97 |
g_assert_cmpint (grl_media_get_track_number (media), ==, 8); |
| 98 |
date = grl_media_get_publication_date (media); |
| 99 |
publication_date = g_date_time_format(date , "%Y-%m-%d"); |
| 100 |
g_assert_cmpstr (publication_date, ==, "2009-01-01"); |
| 101 |
g_free (publication_date); |
| 102 |
|
| 103 |
media = GRL_MEDIA (results->next->next->data); |
| 104 |
g_assert_cmpstr (grl_media_get_title (media), ==, "Primavera"); |
| 105 |
g_assert_cmpstr (grl_media_get_album (media), ==, "Einaudi essentiel"); |
| 106 |
g_assert_cmpstr (grl_media_get_album_artist (media), ==, NULL); |
| 107 |
g_assert_cmpstr (grl_media_get_artist (media), ==, "Ludovico Einaudi"); |
| 108 |
g_assert_cmpstr (grl_media_get_mb_recording_id (media), ==, "75e72e13-cf02-47f8-a4a7-98b916b3b18c"); |
| 109 |
g_assert_cmpstr (grl_media_get_mb_release_id (media), ==, "7a06eb11-6013-4eb8-8aef-a2302ef4ed79"); |
| 110 |
g_assert_cmpstr (grl_media_get_mb_release_group_id (media), ==, "6c42f326-7c15-41d5-851d-e887b544e217"); |
| 111 |
g_assert_cmpstr (grl_media_get_mb_artist_id (media), ==, "fa34b363-79df-434f-a5b8-be4e6898543f"); |
| 112 |
g_assert_cmpint (grl_media_get_album_disc_number (media), ==, 1); |
| 113 |
g_assert_cmpint (grl_media_get_track_number (media), ==, 11); |
| 114 |
date = grl_media_get_publication_date (media); |
| 115 |
publication_date = g_date_time_format(date , "%Y-%m-%d"); |
| 116 |
g_assert_cmpstr (publication_date, ==, "2012-01-01"); |
| 117 |
g_free (publication_date); |
| 118 |
|
| 119 |
g_assert_no_error (error); |
| 120 |
g_list_free_full (results, g_object_unref); |
| 121 |
g_free (fingerprint); |
| 122 |
g_object_unref (file); |
| 123 |
g_object_unref (options); |
| 124 |
} |
| 125 |
|
| 126 |
static gchar * |
| 127 |
resolve (GrlSource *source, |
| 128 |
const gchar *fingerprint, |
| 129 |
gint duration, |
| 130 |
gchar **out_mb_artist_id, |
| 131 |
gchar **out_artist, |
| 132 |
gchar **out_mb_album_id, |
| 133 |
gchar **out_album, |
| 134 |
gchar **out_album_artist, |
| 135 |
gchar **out_mb_recording_id, |
| 136 |
gchar **out_title, |
| 137 |
gchar **out_mb_release_id, |
| 138 |
gchar **out_mb_release_group_id, |
| 139 |
gint *out_album_disc_number, |
| 140 |
gchar **out_publication_date, |
| 141 |
gchar **out_creation_date, |
| 142 |
gint *out_track_number) |
| 143 |
{ |
| 144 |
GList *keys; |
| 145 |
GrlMedia *audio; |
| 146 |
GrlOperationOptions *options; |
| 147 |
GrlRegistry *registry; |
| 148 |
GrlKeyID chromaprint_key; |
| 149 |
GDateTime *publication_date, *creation_date; |
| 150 |
GError *error = NULL; |
| 151 |
|
| 152 |
registry = grl_registry_get_default (); |
| 153 |
chromaprint_key = grl_registry_lookup_metadata_key (registry, "chromaprint"); |
| 154 |
g_assert_cmpint (chromaprint_key, !=, GRL_METADATA_KEY_INVALID); |
| 155 |
|
| 156 |
audio = grl_media_audio_new (); |
| 157 |
grl_data_set_string (GRL_DATA (audio), chromaprint_key, fingerprint); |
| 158 |
grl_media_set_duration (audio, duration); |
| 159 |
|
| 160 |
keys = grl_metadata_key_list_new (GRL_METADATA_KEY_MB_ARTIST_ID, |
| 161 |
GRL_METADATA_KEY_ARTIST, |
| 162 |
GRL_METADATA_KEY_ALBUM_ARTIST, |
| 163 |
GRL_METADATA_KEY_MB_ALBUM_ID, |
| 164 |
GRL_METADATA_KEY_MB_RELEASE_ID, |
| 165 |
GRL_METADATA_KEY_MB_RELEASE_GROUP_ID, |
| 166 |
GRL_METADATA_KEY_ALBUM, |
| 167 |
GRL_METADATA_KEY_MB_RECORDING_ID, |
| 168 |
GRL_METADATA_KEY_TITLE, |
| 169 |
GRL_METADATA_KEY_ALBUM_DISC_NUMBER, |
| 170 |
GRL_METADATA_KEY_PUBLICATION_DATE, |
| 171 |
GRL_METADATA_KEY_CREATION_DATE, |
| 172 |
GRL_METADATA_KEY_TRACK_NUMBER, |
| 173 |
NULL); |
| 174 |
options = grl_operation_options_new (NULL); |
| 175 |
grl_operation_options_set_resolution_flags (options, GRL_RESOLVE_NORMAL); |
| 176 |
|
| 177 |
grl_source_resolve_sync (source, |
| 178 |
GRL_MEDIA (audio), |
| 179 |
keys, |
| 180 |
options, |
| 181 |
&error); |
| 182 |
g_assert_no_error (error); |
| 183 |
|
| 184 |
*out_mb_artist_id = g_strdup (grl_media_get_mb_artist_id (audio)); |
| 185 |
*out_artist = g_strdup (grl_media_get_artist (audio)); |
| 186 |
*out_mb_album_id = g_strdup (grl_media_get_mb_album_id (audio)); |
| 187 |
*out_album = g_strdup (grl_media_get_album (audio)); |
| 188 |
*out_album_artist = g_strdup (grl_media_get_album_artist (audio)); |
| 189 |
*out_mb_recording_id = g_strdup (grl_media_get_mb_recording_id (audio)); |
| 190 |
*out_title = g_strdup (grl_media_get_title (audio)); |
| 191 |
*out_mb_release_id = g_strdup (grl_media_get_mb_release_id (audio)); |
| 192 |
*out_mb_release_group_id = g_strdup (grl_media_get_mb_release_group_id (audio)); |
| 193 |
*out_album_disc_number = grl_media_get_album_disc_number (audio); |
| 194 |
publication_date = grl_media_get_publication_date (audio); |
| 195 |
*out_publication_date = g_date_time_format (publication_date, "%Y-%m-%d"); |
| 196 |
creation_date = grl_media_get_creation_date (audio); |
| 197 |
*out_creation_date = g_date_time_format (creation_date, "%Y-%m-%d"); |
| 198 |
*out_track_number = grl_media_get_track_number (audio); |
| 199 |
|
| 200 |
g_list_free (keys); |
| 201 |
g_object_unref (options); |
| 202 |
g_object_unref (audio); |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
|
| 206 |
static void |
| 207 |
test_resolve_fingerprint (void) |
| 208 |
{ |
| 209 |
GrlSource *source; |
| 210 |
guint i; |
| 211 |
|
| 212 |
struct { |
| 213 |
gchar *fingerprint_file; |
| 214 |
gint duration; |
| 215 |
gchar *mb_artist_id; |
| 216 |
gchar *artist; |
| 217 |
gchar *mb_album_id; |
| 218 |
gchar *album; |
| 219 |
gchar *album_artist; |
| 220 |
gchar *mb_recording_id; |
| 221 |
gchar *title; |
| 222 |
gchar *mb_release_id; |
| 223 |
gchar *mb_release_group_id; |
| 224 |
gint album_disc_number; |
| 225 |
gchar *publication_date; |
| 226 |
gchar *creation_date; |
| 227 |
gint track_number; |
| 228 |
} audios[] = { |
| 229 |
{ FINGERPRINT_LUDOVICO_EI, 445, |
| 230 |
"fa34b363-79df-434f-a5b8-be4e6898543f", "Ludovico Einaudi", |
| 231 |
"ac615d26-df00-3053-9462-7636375603b4", "The Number One Classical Album 2008", |
| 232 |
"Various Artists", |
| 233 |
"8f6ac978-95cf-41e8-82fb-043624652e04", "Primavera", |
| 234 |
"d73e4041-375c-4b9f-8366-46089bca304c", "ac615d26-df00-3053-9462-7636375603b4", |
| 235 |
1, "2007-11-26", "2007-11-26", 19 }, |
| 236 |
{ FINGERPRINT_NORAH_JONES, 160, |
| 237 |
"985c709c-7771-4de3-9024-7bda29ebe3f9", "Norah Jones", |
| 238 |
"f5cffa96-262c-49af-9747-3f04a1d42c78", "\u00d63 Greatest Hits 49", |
| 239 |
"Various Artists", |
| 240 |
"6d8ba615-d8fe-4f99-b38f-0a17d657b1bb", "Chasing Pirates", |
| 241 |
"1ee64f6c-560f-421f-83dc-7fd34e5b0674", "f5cffa96-262c-49af-9747-3f04a1d42c78", |
| 242 |
1, "2010-03-12", "2010-03-12", 18 }, |
| 243 |
{ FINGERPRINT_TROMBONE_SH, 243, |
| 244 |
"cae4fd51-4d58-4d48-92c1-6198cc2e45ed", "Trombone Shorty", |
| 245 |
"c3418122-387b-4477-90cf-e5e6d110e054", "For True", |
| 246 |
NULL, |
| 247 |
"96483bdd-f219-4ae3-a94e-04feeeef22a4", "Buckjump", |
| 248 |
"567621e3-b80f-4c30-af5f-2ecf0882e94a", "c3418122-387b-4477-90cf-e5e6d110e054", |
| 249 |
1, "2011-01-01", "2011-01-01", 1 }, |
| 250 |
{ FINGERPRINT_PHILIP_GLAS, 601, |
| 251 |
"5ae54dee-4dba-49c0-802a-a3b3b3adfe9b", "Philip Glass", |
| 252 |
"52f1f9d5-5166-4ceb-9289-6fb1a87f367c", "The Passion of Ramakrishna", |
| 253 |
NULL, |
| 254 |
"298e15a1-b29b-4947-9dca-ec3634f9ebde", "Part 2", |
| 255 |
"2807def3-7873-4277-b079-c9a963d99993", "52f1f9d5-5166-4ceb-9289-6fb1a87f367c", |
| 256 |
1, "2012-01-01", "2012-01-01", 3 }, |
| 257 |
{ FINGERPRINT_RADIOHEAD_PA, 385, |
| 258 |
"a74b1b7f-71a5-4011-9441-d0b5e4122711", "Radiohead", |
| 259 |
"dd02a722-bb7f-4771-9a5b-681f5556ce71", "3X plus de bruit!", |
| 260 |
"Various Artists", |
| 261 |
"9f9cf187-d6f9-437f-9d98-d59cdbd52757", "Paranoid Android", |
| 262 |
"67016a2b-118d-483d-9e36-49080ae43df4", "dd02a722-bb7f-4771-9a5b-681f5556ce71", |
| 263 |
1, "1997-01-01", "1997-01-01", 4, |
| 264 |
}, |
| 265 |
}; |
| 266 |
|
| 267 |
source = test_lua_factory_get_source (ACOUSTID_ID, ACOUSTID_OPS); |
| 268 |
for (i = 0; i < G_N_ELEMENTS (audios); i++) { |
| 269 |
gchar *data; |
| 270 |
GFile *file; |
| 271 |
gsize size; |
| 272 |
GError *error = NULL; |
| 273 |
gchar *mb_artist_id, *artist, *mb_album_id, *album, *album_artist, |
| 274 |
*mb_recording_id, *title, *mb_release_id, *mb_release_group_id, |
| 275 |
*publication_date, *creation_date; |
| 276 |
gint album_disc_number, track_number; |
| 277 |
|
| 278 |
file = g_file_new_for_uri (audios[i].fingerprint_file); |
| 279 |
g_file_load_contents (file, NULL, &data, &size, NULL, &error); |
| 280 |
g_assert_no_error (error); |
| 281 |
g_clear_pointer (&file, g_object_unref); |
| 282 |
|
| 283 |
resolve (source, data, audios[i].duration, |
| 284 |
&mb_artist_id, &artist, &mb_album_id, &album, &album_artist, |
| 285 |
&mb_recording_id, &title, &mb_release_id, &mb_release_group_id, |
| 286 |
&album_disc_number, &publication_date, &creation_date, |
| 287 |
&track_number); |
| 288 |
g_free (data); |
| 289 |
|
| 290 |
g_assert_cmpstr (audios[i].title, ==, title); |
| 291 |
g_free (title); |
| 292 |
g_assert_cmpstr (audios[i].mb_artist_id , ==, mb_artist_id); |
| 293 |
g_free (mb_artist_id); |
| 294 |
g_assert_cmpstr (audios[i].artist, ==, artist); |
| 295 |
g_free (artist); |
| 296 |
g_assert_cmpstr (audios[i].mb_album_id, ==, mb_album_id); |
| 297 |
g_free (mb_album_id); |
| 298 |
g_assert_cmpstr (audios[i].album, ==, album); |
| 299 |
g_free (album); |
| 300 |
g_assert_cmpstr (audios[i].album_artist, ==, album_artist); |
| 301 |
g_free (album_artist); |
| 302 |
g_assert_cmpstr (audios[i].mb_recording_id, ==, mb_recording_id); |
| 303 |
g_free (mb_recording_id); |
| 304 |
g_assert_cmpstr (audios[i].mb_release_id, ==, mb_release_id); |
| 305 |
g_free (mb_release_id); |
| 306 |
g_assert_cmpstr (audios[i].mb_release_group_id, ==, mb_release_group_id); |
| 307 |
g_free (mb_release_group_id); |
| 308 |
g_assert_cmpint (audios[i].album_disc_number, ==, album_disc_number); |
| 309 |
g_assert_cmpstr (audios[i].publication_date, ==, publication_date); |
| 310 |
g_free (publication_date); |
| 311 |
g_assert_cmpstr (audios[i].creation_date, ==, creation_date); |
| 312 |
g_free (creation_date); |
| 313 |
g_assert_cmpint (audios[i].track_number, ==, track_number); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
static void |
| 318 |
test_acoustid_setup (gint *p_argc, |
| 319 |
gchar ***p_argv) |
| 320 |
{ |
| 321 |
GrlRegistry *registry; |
| 322 |
GrlConfig *config; |
| 323 |
GError *error = NULL; |
| 324 |
|
| 325 |
g_setenv ("GRL_PLUGIN_PATH", TEST_PLUGINS_PATH, TRUE); |
| 326 |
g_setenv ("GRL_PLUGIN_LIST", TEST_PLUGINS_LOAD, TRUE); |
| 327 |
g_setenv ("GRL_LUA_SOURCES_PATH", LUA_FACTORY_SOURCES_PATH, TRUE); |
| 328 |
g_setenv ("GRL_NET_MOCKED", LUA_FACTORY_SOURCES_DATA_PATH "config.ini", TRUE); |
| 329 |
|
| 330 |
grl_init (p_argc, p_argv); |
| 331 |
g_test_init (p_argc, p_argv, NULL); |
| 332 |
|
| 333 |
/* This test uses 'chromaprint' metadata-key which is created by |
| 334 |
* Chromaprint plugin. For that reason, we need to load and activate it. */ |
| 335 |
registry = grl_registry_get_default (); |
| 336 |
grl_registry_load_plugin (registry, CHROMAPRINT_PLUGIN_PATH "/libgrlchromaprint.so", &error); |
| 337 |
g_assert_no_error (error); |
| 338 |
grl_registry_activate_plugin_by_id (registry, CHROMAPRINT_ID, &error); |
| 339 |
g_assert_no_error (error); |
| 340 |
|
| 341 |
config = grl_config_new (LUA_FACTORY_ID, ACOUSTID_ID); |
| 342 |
grl_config_set_api_key (config, "ACOUSTID_TEST_MOCK_API_KEY"); |
| 343 |
test_lua_factory_setup (config); |
| 344 |
} |
| 345 |
|
| 346 |
gint |
| 347 |
main (gint argc, gchar **argv) |
| 348 |
{ |
| 349 |
setlocale (LC_ALL, ""); |
| 350 |
|
| 351 |
test_acoustid_setup (&argc, &argv); |
| 352 |
|
| 353 |
g_test_add_func ("/lua_factory/sources/acoustid/resolve/fingerprint", |
| 354 |
test_resolve_fingerprint); |
| 355 |
g_test_add_func ("/lua_factory/sources/acoustid/query/fingerprint", |
| 356 |
test_query_by_fingerprint); |
| 357 |
|
| 358 |
gint result = g_test_run (); |
| 359 |
|
| 360 |
test_lua_factory_shutdown (); |
| 361 |
test_lua_factory_deinit (); |
| 362 |
|
| 363 |
return result; |
| 364 |
} |