1 #include "../src/icon.c"
2 #include "greatest.h"
3 
4 #define ICONPREFIX "/data/icons/path"
5 
6 /* As there are no hints to test if the loaded GdkPixbuf is
7  * read from a PNG or an SVG file, the sample icons in the
8  * test structure have different sizes
9  */
10 #define IS_ICON_PNG(pb)  4 == gdk_pixbuf_get_width(pb)
11 #define IS_ICON_SVG(pb) 16 == gdk_pixbuf_get_width(pb)
12 
13 extern const char *base;
14 
test_get_path_from_icon_null(void)15 TEST test_get_path_from_icon_null(void){
16     char *result = get_path_from_icon_name(NULL);
17     ASSERT_EQ(result, NULL);
18     PASS();
19 }
20 
test_get_path_from_icon_sorting(void)21 TEST test_get_path_from_icon_sorting(void)
22 {
23         const char *iconpath = ICONPREFIX;
24 
25         const char* icon_names[] = { "onlypng", "onlysvg", "icon1" };
26         const char* icon_paths[] = { "valid/onlypng.png", "valid/onlysvg.svg", "invalid/icon1.svg" };
27         ASSERT_EQm("Test is incorrect", G_N_ELEMENTS(icon_names), G_N_ELEMENTS(icon_paths));
28 
29         for (int i = 0; i < G_N_ELEMENTS(icon_names); i++){
30                 gchar *path = g_build_filename(base, iconpath, icon_paths[i], NULL);
31                 char *result = get_path_from_icon_name(icon_names[i]);
32                 ASSERT(result);
33                 ASSERT_EQ(strcmp(result, path), 0);
34                 g_free(path);
35                 g_free(result);
36         }
37 
38         PASS();
39 }
40 
test_get_path_from_icon_name(void)41 TEST test_get_path_from_icon_name(void)
42 {
43         const char *iconpath = ICONPREFIX;
44 
45         const char* icon_name = "onlypng";
46         const char* expected_suffix = ".png";
47         char* full_name = g_strconcat(icon_name, expected_suffix, NULL);
48 
49         gchar *path = g_build_filename(base, iconpath, "valid", full_name, NULL);
50         char *result = get_path_from_icon_name(icon_name);
51 
52         ASSERT(result);
53         ASSERT_EQ(strcmp(result, path), 0);
54 
55         g_free(full_name);
56         g_free(path);
57         g_free(result);
58         PASS();
59 }
60 
test_get_path_from_icon_name_full(void)61 TEST test_get_path_from_icon_name_full(void)
62 {
63         const char *iconpath = ICONPREFIX;
64 
65         gchar *path = g_build_filename(base, iconpath, "valid", "icon1.svg", NULL);
66 
67         char *result = get_path_from_icon_name(path);
68         ASSERT(result);
69         ASSERT_EQ(strcmp(result, path), 0);
70 
71         g_free(path);
72         g_free(result);
73         PASS();
74 }
75 
test_get_pixbuf_from_file_tilde(void)76 TEST test_get_pixbuf_from_file_tilde(void)
77 {
78         const char *home = g_get_home_dir();
79         const char *iconpath = ICONPREFIX;
80 
81         if (0 != strncmp(home, base, strlen(home))) {
82                 SKIPm("Current directory is not a subdirectory from user's home."
83                       " Cannot test iconpath tilde expansion.\n");
84         }
85 
86         gchar *path = g_build_filename(base, iconpath, "valid", "icon1.svg", NULL);
87         path = string_replace_at(path, 0, strlen(home), "~");
88 
89         GdkPixbuf *pixbuf = get_pixbuf_from_file(path);
90         g_clear_pointer(&path, g_free);
91 
92         ASSERT(pixbuf);
93         ASSERTm("The wrong pixbuf is loaded in the icon file.", IS_ICON_SVG(pixbuf));
94         g_clear_pointer(&pixbuf, g_object_unref);
95         PASS();
96 }
97 
test_get_pixbuf_from_file_absolute(void)98 TEST test_get_pixbuf_from_file_absolute(void)
99 {
100         const char *iconpath = ICONPREFIX;
101 
102         gchar *path = g_build_filename(base, iconpath, "valid", "icon1.svg", NULL);
103 
104         GdkPixbuf *pixbuf = get_pixbuf_from_file(path);
105         g_clear_pointer(&path, g_free);
106 
107         ASSERT(pixbuf);
108         ASSERTm("The wrong pixbuf is loaded in the icon file.", IS_ICON_SVG(pixbuf));
109         g_clear_pointer(&pixbuf, g_object_unref);
110 
111         PASS();
112 }
113 
test_get_pixbuf_from_icon_invalid(void)114 TEST test_get_pixbuf_from_icon_invalid(void)
115 {
116         GdkPixbuf *pixbuf = get_pixbuf_from_icon("invalid");
117         ASSERT(pixbuf == NULL);
118         g_clear_pointer(&pixbuf, g_object_unref);
119 
120         PASS();
121 }
122 
test_get_pixbuf_from_icon_both(void)123 TEST test_get_pixbuf_from_icon_both(void)
124 {
125         GdkPixbuf *pixbuf = get_pixbuf_from_icon("icon1");
126         // the first icon  found is invalid, so the pixbuf is empty
127         ASSERT(!pixbuf);
128         g_clear_pointer(&pixbuf, g_object_unref);
129 
130         PASS();
131 }
132 
test_get_pixbuf_from_icon_onlysvg(void)133 TEST test_get_pixbuf_from_icon_onlysvg(void)
134 {
135         GdkPixbuf *pixbuf = get_pixbuf_from_icon("onlysvg");
136         ASSERT(pixbuf);
137         ASSERTm("SVG pixbuf isn't loaded", IS_ICON_SVG(pixbuf));
138         g_clear_pointer(&pixbuf, g_object_unref);
139 
140         PASS();
141 }
142 
test_get_pixbuf_from_icon_onlypng(void)143 TEST test_get_pixbuf_from_icon_onlypng(void)
144 {
145         GdkPixbuf *pixbuf = get_pixbuf_from_icon("onlypng");
146         ASSERT(pixbuf);
147         ASSERTm("PNG pixbuf isn't loaded", IS_ICON_PNG(pixbuf));
148         g_clear_pointer(&pixbuf, g_object_unref);
149 
150         PASS();
151 }
152 
test_get_pixbuf_from_icon_filename(void)153 TEST test_get_pixbuf_from_icon_filename(void)
154 {
155         char *icon = g_strconcat(base, "/data/icons/valid.png", NULL);
156         GdkPixbuf *pixbuf = get_pixbuf_from_icon(icon);
157         ASSERT(pixbuf);
158         ASSERTm("PNG pixbuf isn't loaded", IS_ICON_PNG(pixbuf));
159         g_clear_pointer(&pixbuf, g_object_unref);
160 
161         g_free(icon);
162         PASS();
163 }
164 
test_get_pixbuf_from_icon_fileuri(void)165 TEST test_get_pixbuf_from_icon_fileuri(void)
166 {
167         char *icon = g_strconcat("file://", base, "/data/icons/valid.svg", NULL);
168         GdkPixbuf *pixbuf = get_pixbuf_from_icon(icon);
169         ASSERT(pixbuf);
170         ASSERTm("SVG pixbuf isn't loaded", IS_ICON_SVG(pixbuf));
171         g_clear_pointer(&pixbuf, g_object_unref);
172 
173         g_free(icon);
174         PASS();
175 }
176 
test_icon_size_clamp_too_small(void)177 TEST test_icon_size_clamp_too_small(void)
178 {
179         int w = 12, h = 24;
180         bool resized = icon_size_clamp(&w, &h);
181         ASSERT(resized);
182         ASSERT_EQ(w, 16);
183         ASSERT_EQ(h, 32);
184 
185         PASS();
186 }
187 
test_icon_size_clamp_not_necessary(void)188 TEST test_icon_size_clamp_not_necessary(void)
189 {
190         int w = 20, h = 30;
191         bool resized = icon_size_clamp(&w, &h);
192         ASSERT(!resized);
193         ASSERT_EQ(w, 20);
194         ASSERT_EQ(h, 30);
195 
196         PASS();
197 }
198 
test_icon_size_clamp_too_big(void)199 TEST test_icon_size_clamp_too_big(void)
200 {
201         int w = 75, h = 150;
202         bool resized = icon_size_clamp(&w, &h);
203         ASSERT(resized);
204         ASSERT_EQ(w, 50);
205         ASSERT_EQ(h, 100);
206 
207         PASS();
208 }
209 
test_icon_size_clamp_too_small_then_too_big(void)210 TEST test_icon_size_clamp_too_small_then_too_big(void)
211 {
212         int w = 8, h = 80;
213         bool resized = icon_size_clamp(&w, &h);
214         ASSERT(resized);
215         ASSERT_EQ(w, 10);
216         ASSERT_EQ(h, 100);
217 
218         PASS();
219 }
220 
test_get_pixbuf_from_icon_both_is_scaled(void)221 TEST test_get_pixbuf_from_icon_both_is_scaled(void)
222 {
223         GdkPixbuf *pixbuf = get_pixbuf_from_icon("onlypng");
224         ASSERT(pixbuf);
225         ASSERT_EQ(gdk_pixbuf_get_width(pixbuf), 16);
226         ASSERT_EQ(gdk_pixbuf_get_height(pixbuf), 16);
227         g_clear_pointer(&pixbuf, g_object_unref);
228 
229         PASS();
230 }
231 
SUITE(suite_icon)232 SUITE(suite_icon)
233 {
234         // set only valid icons in the path
235         settings.icon_path = g_strconcat(
236                      base, ICONPREFIX "/valid"
237                 ":", base, ICONPREFIX "/both",
238                 NULL);
239         RUN_TEST(test_get_path_from_icon_name);
240 
241         g_clear_pointer(&settings.icon_path, g_free);
242         settings.icon_path = g_strconcat(
243                      base, ICONPREFIX "/invalid"
244                 ":", base, ICONPREFIX "/valid"
245                 ":", base, ICONPREFIX "/both",
246                 NULL);
247 
248         RUN_TEST(test_get_path_from_icon_null);
249         RUN_TEST(test_get_path_from_icon_sorting);
250         RUN_TEST(test_get_path_from_icon_name_full);
251         RUN_TEST(test_get_pixbuf_from_file_tilde);
252         RUN_TEST(test_get_pixbuf_from_file_absolute);
253         RUN_TEST(test_get_pixbuf_from_icon_invalid);
254         RUN_TEST(test_get_pixbuf_from_icon_both);
255         RUN_TEST(test_get_pixbuf_from_icon_onlysvg);
256         RUN_TEST(test_get_pixbuf_from_icon_onlypng);
257         RUN_TEST(test_get_pixbuf_from_icon_filename);
258         RUN_TEST(test_get_pixbuf_from_icon_fileuri);
259         RUN_TEST(test_icon_size_clamp_not_necessary);
260 
261         settings.min_icon_size = 16;
262         settings.max_icon_size = 100;
263 
264         RUN_TEST(test_get_pixbuf_from_icon_both_is_scaled);
265         RUN_TEST(test_icon_size_clamp_too_small);
266         RUN_TEST(test_icon_size_clamp_not_necessary);
267         RUN_TEST(test_icon_size_clamp_too_big);
268         RUN_TEST(test_icon_size_clamp_too_small_then_too_big);
269 
270         settings.min_icon_size = 16;
271         settings.max_icon_size = 0;
272 
273         RUN_TEST(test_icon_size_clamp_too_small);
274         RUN_TEST(test_icon_size_clamp_not_necessary);
275 
276         settings.min_icon_size = 0;
277         settings.max_icon_size = 100;
278 
279         RUN_TEST(test_icon_size_clamp_not_necessary);
280         RUN_TEST(test_icon_size_clamp_too_big);
281 
282         settings.min_icon_size = 0;
283         settings.max_icon_size = 0;
284 
285         g_clear_pointer(&settings.icon_path, g_free);
286 }
287 /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */
288