1 /* EasyTAG - tag editor for audio files
2  * Copyright (C) 2014-2015 David King <amigadave@amigadave.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "picture.h"
20 
21 #include <gtk/gtk.h>
22 #include <string.h>
23 
24 GtkWidget *MainWindow;
25 GSettings *MainSettings;
26 
27 static void
picture_copy(void)28 picture_copy (void)
29 {
30     GBytes *bytes;
31     EtPicture *pic1;
32     EtPicture *pic2;
33     EtPicture *pic3;
34     EtPicture *pic4;
35     EtPicture *pic1_copy;
36     EtPicture *pic2_copy;
37     const EtPicture *pic3_copy;
38     EtPicture *pic4_copy;
39 
40     bytes = g_bytes_new_static ("foobar", 6);
41     pic1 = et_picture_new (ET_PICTURE_TYPE_LEAFLET_PAGE, "foobar.png", 640,
42                            480, bytes);
43     g_bytes_unref (bytes);
44 
45     pic2 = et_picture_copy_all (pic1);
46 
47     g_assert (!et_picture_detect_difference (pic1, pic2));
48     g_assert (pic2->next == NULL);
49 
50     bytes = g_bytes_new_static ("foo", 3);
51     pic3 = et_picture_new (ET_PICTURE_TYPE_ILLUSTRATION, "bash.jpg", 320, 240,
52                            bytes);
53     g_bytes_unref (bytes);
54 
55     pic1->next = pic2;
56     pic2->next = pic3;
57 
58     pic2_copy = et_picture_copy_single (pic2);
59 
60     g_assert (!et_picture_detect_difference (pic2, pic2_copy));
61     g_assert (pic2_copy->next == NULL);
62 
63     pic1_copy = et_picture_copy_all (pic1);
64 
65     g_assert (pic1_copy->next != NULL);
66     g_assert (pic1_copy->next->next != NULL);
67     g_assert (pic1_copy->next->next->next == NULL);
68 
69     pic3_copy = pic1_copy->next->next;
70 
71     g_assert (!et_picture_detect_difference (pic3, pic3_copy));
72 
73     bytes = g_bytes_new_static ("foobarbaz", 9);
74     pic4 = et_picture_new (ET_PICTURE_TYPE_MEDIA, "baz.gif", 800, 600, bytes);
75     g_bytes_unref (bytes);
76 
77     pic4_copy = g_boxed_copy (ET_TYPE_PICTURE, pic4);
78 
79     g_assert (!et_picture_detect_difference (pic4, pic4_copy));
80 
81     et_picture_free (pic1_copy);
82     et_picture_free (pic2_copy);
83     g_boxed_free (ET_TYPE_PICTURE, pic4_copy);
84     et_picture_free (pic1);
85     et_picture_free (pic4);
86 }
87 
88 static void
picture_difference(void)89 picture_difference (void)
90 {
91     GBytes *bytes;
92     EtPicture *pic1;
93     EtPicture *pic2;
94 
95     pic1 = NULL;
96     pic2 = NULL;
97 
98     g_assert (!et_picture_detect_difference (pic1, pic2));
99 
100     bytes = g_bytes_new_static ("foobar", 6);
101     pic1 = et_picture_new (ET_PICTURE_TYPE_LEAFLET_PAGE, "foobar.png", 640,
102                            480, bytes);
103     g_bytes_unref (bytes);
104 
105     g_assert (et_picture_detect_difference (pic1, pic2));
106     g_assert (et_picture_detect_difference (pic2, pic1));
107 
108     pic2 = et_picture_new (ET_PICTURE_TYPE_ILLUSTRATION, "foobar.png", 640,
109                            480, pic1->bytes);
110 
111     g_assert (et_picture_detect_difference (pic1, pic2));
112 
113     et_picture_free (pic2);
114     pic2 = et_picture_new (ET_PICTURE_TYPE_LEAFLET_PAGE, "foobar.png", 480,
115                            640, pic1->bytes);
116 
117     g_assert (et_picture_detect_difference (pic1, pic2));
118 
119     et_picture_free (pic2);
120     pic2 = et_picture_new (ET_PICTURE_TYPE_LEAFLET_PAGE, "foobar.png", 640,
121                            640, pic1->bytes);
122 
123     g_assert (et_picture_detect_difference (pic1, pic2));
124 
125     et_picture_free (pic2);
126     pic2 = et_picture_new (ET_PICTURE_TYPE_LEAFLET_PAGE, "foobar.png", 480,
127                            480, pic1->bytes);
128 
129     g_assert (et_picture_detect_difference (pic1, pic2));
130 
131     et_picture_free (pic2);
132     pic2 = et_picture_new (ET_PICTURE_TYPE_LEAFLET_PAGE, "baz.gif", 640,
133                            480, pic1->bytes);
134 
135     g_assert (et_picture_detect_difference (pic1, pic2));
136 
137     et_picture_free (pic2);
138     bytes = g_bytes_new_static ("baz", 3);
139     pic2 = et_picture_new (ET_PICTURE_TYPE_LEAFLET_PAGE, "foobar.png", 640,
140                            480, bytes);
141     g_bytes_unref (bytes);
142 
143     g_assert (et_picture_detect_difference (pic1, pic2));
144 
145     et_picture_free (pic2);
146     pic2 = et_picture_copy_single (pic1);
147     pic1->next = pic2;
148 
149     /* et_picture_detect_difference() does not examine the next pointer. */
150     g_assert (!et_picture_detect_difference (pic1, pic2));
151 
152     et_picture_free (pic1);
153 }
154 
155 static void
picture_type_from_filename(void)156 picture_type_from_filename (void)
157 {
158     gsize i;
159 
160     static const struct
161     {
162         const gchar *filename;
163         EtPictureType type;
164     } pictures[] =
165     {
166         { "no clues here", ET_PICTURE_TYPE_FRONT_COVER },
167         { "cover.jpg", ET_PICTURE_TYPE_FRONT_COVER },
168         { "inside cover.png", ET_PICTURE_TYPE_LEAFLET_PAGE },
169         { "acdc", ET_PICTURE_TYPE_MEDIA },
170         { "ACDC", ET_PICTURE_TYPE_MEDIA },
171         { "aCdC", ET_PICTURE_TYPE_MEDIA },
172         { "aC dC", ET_PICTURE_TYPE_FRONT_COVER },
173         { "back in black", ET_PICTURE_TYPE_BACK_COVER },
174         { "illustrations of grandeur", ET_PICTURE_TYPE_ILLUSTRATION },
175         { "inside outside", ET_PICTURE_TYPE_LEAFLET_PAGE },
176         { "front to back", ET_PICTURE_TYPE_FRONT_COVER },
177         { "back to front", ET_PICTURE_TYPE_FRONT_COVER },
178         { "inlay", ET_PICTURE_TYPE_LEAFLET_PAGE },
179         { "leaflet", ET_PICTURE_TYPE_LEAFLET_PAGE },
180         { "page", ET_PICTURE_TYPE_LEAFLET_PAGE },
181         { "multimedia", ET_PICTURE_TYPE_MEDIA },
182         { "artist band", ET_PICTURE_TYPE_ARTIST_PERFORMER },
183         { "band", ET_PICTURE_TYPE_BAND_ORCHESTRA },
184         { "orchestra", ET_PICTURE_TYPE_BAND_ORCHESTRA },
185         { "performer", ET_PICTURE_TYPE_ARTIST_PERFORMER },
186         { "composer", ET_PICTURE_TYPE_COMPOSER },
187         { "lyricist", ET_PICTURE_TYPE_LYRICIST_TEXT_WRITER },
188         { "writer", ET_PICTURE_TYPE_FRONT_COVER },
189         { "publisher", ET_PICTURE_TYPE_PUBLISHER_STUDIO_LOGOTYPE },
190         { "studio", ET_PICTURE_TYPE_FRONT_COVER }
191     };
192 
193     for (i = 0; i < G_N_ELEMENTS (pictures); i++)
194     {
195         g_assert_cmpint (pictures[i].type, ==,
196                          et_picture_type_from_filename (pictures[i].filename));
197     }
198 }
199 
200 static void
picture_format_from_data(void)201 picture_format_from_data (void)
202 {
203     gsize i;
204 
205     static const struct
206     {
207         const gchar *data;
208         Picture_Format format;
209     } pictures[] =
210     {
211         { "\xff\xd8", PICTURE_FORMAT_UNKNOWN },
212         { "\xff\xd8\xff", PICTURE_FORMAT_JPEG },
213         { "\x89PNG\x0d\x0a\x1a\x0a", PICTURE_FORMAT_PNG },
214         { "GIF87a", PICTURE_FORMAT_GIF },
215         { "GIF89a", PICTURE_FORMAT_GIF },
216         { "GIF900", PICTURE_FORMAT_UNKNOWN }
217     };
218 
219     for (i = 0; i < G_N_ELEMENTS (pictures); i++)
220     {
221         GBytes *bytes;
222         EtPicture *pic;
223 
224         bytes = g_bytes_new_static (pictures[i].data,
225                                     strlen (pictures[i].data) + 1);
226         pic = et_picture_new (ET_PICTURE_TYPE_FRONT_COVER, "", 0, 0, bytes);
227         g_bytes_unref (bytes);
228 
229         g_assert_cmpint (pictures[i].format, ==,
230                          Picture_Format_From_Data (pic));
231 
232         et_picture_free (pic);
233     }
234 }
235 
236 int
main(int argc,char ** argv)237 main (int argc, char** argv)
238 {
239     g_test_init (&argc, &argv, NULL);
240 
241     g_test_add_func ("/picture/copy", picture_copy);
242     g_test_add_func ("/picture/difference", picture_difference);
243     g_test_add_func ("/picture/format-from-data", picture_format_from_data);
244     g_test_add_func ("/picture/type-from-filename",
245                      picture_type_from_filename);
246 
247     return g_test_run ();
248 }
249