summaryrefslogtreecommitdiff
path: root/tests/mediaarttest.c
blob: 967c911ac4e4d08e8c468c873566f7a12676103f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Copyright (C) 2011, Nokia <ivan.frade@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>

#include <glib-object.h>

#include <libmediaart/mediaart.h>

struct {
        const gchar *input;
        const gchar *expected_output;
} strip_test_cases [] = {
        { "nothing to strip here", "nothing to strip here" },
        { "Upper Case gOEs dOwN", "upper case goes down"},
        { "o", "o"},
        { "A", "a"},
        { "cool album (CD1)", "cool album"},
        { "cool album [CD1]", "cool album"},
        { "cool album {CD1}", "cool album"},
        { "cool album <CD1>", "cool album"},
        { " ", ""},
        { "     a     ", "a"},
        { "messy #title & stuff?", "messy title stuff"},
        { "Unbalanced [brackets", "unbalanced brackets" },
        { "Unbalanced (brackets", "unbalanced brackets" },
        { "Unbalanced <brackets", "unbalanced brackets" },
        { "Unbalanced brackets)", "unbalanced brackets" },
        { "Unbalanced brackets]", "unbalanced brackets" },
        { "Unbalanced brackets>", "unbalanced brackets" },
        { "Live at *WEMBLEY* dude!", "live at wembley dude" },
        { "met[xX[x]alli]ca", "metallica" },
        { NULL, NULL}
};

struct {
        const gchar *artist;
        const gchar *album;
        const gchar *filename;
} mediaart_test_cases [] = {
        {"Beatles", "Sgt. Pepper",
         "album-2a9ea35253dbec60e76166ec8420fbda-cfba4326a32b44b8760b3a2fc827a634.jpeg"},

        { "", "sgt. pepper",
          "album-d41d8cd98f00b204e9800998ecf8427e-cfba4326a32b44b8760b3a2fc827a634.jpeg"},

        { " ", "sgt. pepper",
          "album-d41d8cd98f00b204e9800998ecf8427e-cfba4326a32b44b8760b3a2fc827a634.jpeg"},

        { NULL, "sgt. pepper",
          "album-cfba4326a32b44b8760b3a2fc827a634-7215ee9c7d9dc229d2921a40e899ec5f.jpeg"}, 

        { "Beatles", NULL,
          "album-2a9ea35253dbec60e76166ec8420fbda-7215ee9c7d9dc229d2921a40e899ec5f.jpeg"},

        { NULL, NULL, NULL }
};

static void
test_mediaart_init (void)
{
	g_assert_true (media_art_init ());
}

static void
test_mediaart_stripping (void)
{
        gint i;
        gchar *result;

        for (i = 0; strip_test_cases[i].input != NULL; i++) {
                result = media_art_strip_invalid_entities (strip_test_cases[i].input);
                g_assert_cmpstr (result, ==, strip_test_cases[i].expected_output);
                g_free (result);
        }

        g_print ("(%d test cases) ", i);
}

static void
test_mediaart_stripping_null (void)
{
        // FIXME: Decide what is the expected behaviour here...
        //   a. Return NULL
        //   b. Return ""
        //g_assert (!mediaart_strip_invalid_entities (NULL));
}

static void
test_mediaart_location (void)
{
        gchar *path = NULL, *local_uri = NULL;
        gchar *expected;
        gint i;
     
        for (i = 0; mediaart_test_cases[i].filename != NULL; i++) {
                media_art_get_path (mediaart_test_cases[i].artist,
                                    mediaart_test_cases[i].album,
                                    "album",
                                    "file:///home/test/a.mp3",
                                    &path,
                                    &local_uri);
                expected = g_build_path (G_DIR_SEPARATOR_S,
                                         g_get_user_cache_dir (),
                                         "media-art",
                                         mediaart_test_cases[i].filename, 
                                         NULL);
                g_assert_cmpstr (path, ==, expected);
                
                g_free (expected);
                g_free (path);
                g_free (local_uri);
        }
        g_print ("(%d test cases) ", i);


}

static void
test_mediaart_location_null (void)
{
        gchar *path = NULL, *local_uri = NULL;

        /* NULL parameters */
        media_art_get_path (NULL, NULL, "album", "file:///a/b/c.mp3", &path, &local_uri);
        g_assert (!path && !local_uri);
}

static void
test_mediaart_location_path (void)
{
        gchar *path = NULL, *local_uri = NULL;
        gchar *expected;

        /* Use path instead of URI */
        media_art_get_path (mediaart_test_cases[0].artist,
                            mediaart_test_cases[0].album,
                            "album",
                            "/home/test/a.mp3",
                            &path,
                            &local_uri);
        expected = g_build_path (G_DIR_SEPARATOR_S, 
                                 g_get_user_cache_dir (),
                                 "media-art",
                                 mediaart_test_cases[0].filename, 
                                 NULL);
        g_assert_cmpstr (path, ==, expected);
                
        g_free (expected);
        g_free (path);
        g_free (local_uri);
}

static void
test_mediaart_embedded_mp3 (void)
{
	GError *error = NULL;
	GFile *file = NULL;
	gchar *dir, *path;
	gboolean retval;

	/* FIXME: Handle 'buffer' AND 'file/path', is broken currently */

	dir = g_get_current_dir ();
	path = g_build_filename (G_DIR_SEPARATOR_S, dir, "543249_King-Kilo---Radium.mp3", NULL);
	file = g_file_new_for_path (path);
	g_free (path);

	retval = media_art_process_file (NULL,
	                                 0,
	                                 "audio/mp3", /* mime */
	                                 MEDIA_ART_ALBUM,
	                                 "King Kilo", /* artist */
	                                 "Lanedo", /* title */
	                                 file);

	g_assert_true (retval);

	g_object_unref (file);
	g_free (dir);
}

static void
test_mediaart_png (void)
{
	GError *error = NULL;
	GFile *file = NULL;
	gchar *dir, *path;
	gchar *out_path = NULL;
	gchar *out_uri = NULL;
	gchar *expected;
	gboolean retval;

	dir = g_get_current_dir ();
	path = g_build_filename (G_DIR_SEPARATOR_S, dir, "LanedoIconHKS43-64².png", NULL);
	file = g_file_new_for_path (path);
	g_free (dir);

	/* Check data is not cached currently */
	media_art_get_path ("Lanedo", /* artist / title */
	                    NULL, /* album */
	                    NULL, /* prefix */
                            path,
                            &out_path,
                            &out_uri);
	g_assert (g_file_test (out_path, G_FILE_TEST_EXISTS) == FALSE);
	g_free (out_path);
	g_free (out_uri);

	/* Process data */
	retval = media_art_process_file (NULL,
	                                 0,
	                                 "image/png", /* mime */
	                                 MEDIA_ART_ALBUM,
	                                 NULL, /* album */
	                                 "Lanedo", /* title */
	                                 file);

	g_assert_true (retval);

	/* Check cache exists */
	media_art_get_path ("Lanedo", /* artist / title */
	                    NULL, /* album */
	                    NULL, /* prefix */
                            path,
                            &out_path,
                            &out_uri);

        expected = g_build_path (G_DIR_SEPARATOR_S,
                                 g_get_user_cache_dir (),
                                 "media-art",
                                 "album-be60c84852d9762b0a896ba9ba24245e-7215ee9c7d9dc229d2921a40e899ec5f.jpeg",
                                 NULL);
        g_assert_cmpstr (out_path, ==, expected);
        /* FIXME: Why is out_uri NULL? */
        /* FIXME: Why does this next test fail - i.e. file does not
         * exist if we've processed it?
         */
        /* g_assert (g_file_test (out_path, G_FILE_TEST_EXISTS) == TRUE); */

        g_free (out_path);
        g_free (out_uri);
        g_free (expected);

        /* Remove album art */
        retval = media_art_remove ("Lanedo", "");
        g_assert_true (retval);

        /* FIXME: This breaks, passing NULL for second param */
        /* retval = media_art_remove ("Lanedo", NULL); */

        g_object_unref (file);
        g_free (path);
}

int
main (int argc, char **argv)
{
	int success = EXIT_SUCCESS;

        g_test_init (&argc, &argv, NULL);

        g_test_add_func ("/mediaart/init",
                         test_mediaart_init);
        g_test_add_func ("/mediaart/stripping",
                         test_mediaart_stripping);
        g_test_add_func ("/mediaart/stripping_null",
                         test_mediaart_stripping_null);
        g_test_add_func ("/mediaart/location",
                         test_mediaart_location);
        g_test_add_func ("/mediaart/location_null",
                         test_mediaart_location_null);
        g_test_add_func ("/mediaart/location_path",
                         test_mediaart_location_path);
        g_test_add_func ("/mediaart/embedded_mp3",
                         test_mediaart_embedded_mp3);
        g_test_add_func ("/mediaart/png",
                         test_mediaart_png);

        success = g_test_run ();

        media_art_shutdown ();

        return success;
}