1 /*
2  * Copyright © 2010 Canonical Ltd.
3  *             By Michal Hruby <michal.mhr@gmail.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 3 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18 
19 #include <glib.h>
20 #include <glib-object.h>
21 #include <string.h>
22 #include "zeitgeist.h"
23 
24 typedef struct
25 {
26 } Fixture;
27 
28 static void setup    (Fixture *fix, gconstpointer data);
29 static void teardown (Fixture *fix, gconstpointer data);
30 
31 static void
setup(Fixture * fix,gconstpointer data)32 setup (Fixture *fix, gconstpointer data)
33 {
34 }
35 
36 static void
teardown(Fixture * fix,gconstpointer data)37 teardown (Fixture *fix, gconstpointer data)
38 {
39 }
40 
41 static void
test_null_symbols(Fixture * fix,gconstpointer data)42 test_null_symbols (Fixture *fix, gconstpointer data)
43 {
44   // shouldn't crash
45   zeitgeist_symbol_is_a (NULL, NULL);
46 }
47 
48 static void
test_null_first(Fixture * fix,gconstpointer data)49 test_null_first (Fixture *fix, gconstpointer data)
50 {
51   gboolean res = zeitgeist_symbol_is_a (NULL, ZEITGEIST_NFO_MEDIA);
52 
53   g_assert_cmpint (res, ==, FALSE);
54 }
55 
56 static void
test_null_second(Fixture * fix,gconstpointer data)57 test_null_second (Fixture *fix, gconstpointer data)
58 {
59   gboolean res = zeitgeist_symbol_is_a (ZEITGEIST_NFO_MEDIA, NULL);
60 
61   g_assert_cmpint (res, ==, FALSE);
62 }
63 
64 static void
test_not_uri(Fixture * fix,gconstpointer data)65 test_not_uri (Fixture *fix, gconstpointer data)
66 {
67   gboolean res = zeitgeist_symbol_is_a ("first", "second");
68 
69   g_assert_cmpint (res, ==, FALSE);
70 }
71 
72 static void
test_not_uri_equal(Fixture * fix,gconstpointer data)73 test_not_uri_equal (Fixture *fix, gconstpointer data)
74 {
75   gboolean res = zeitgeist_symbol_is_a ("something", "something");
76 
77   g_assert_cmpint (res, ==, FALSE);
78 }
79 
80 static void
test_uris_equal(Fixture * fix,gconstpointer data)81 test_uris_equal (Fixture *fix, gconstpointer data)
82 {
83   gboolean res = zeitgeist_symbol_is_a (ZEITGEIST_NFO_AUDIO,
84                                         ZEITGEIST_NFO_AUDIO);
85 
86   g_assert_cmpint (res, ==, TRUE);
87 }
88 
89 static void
test_vector_image_media(Fixture * fix,gconstpointer data)90 test_vector_image_media (Fixture *fix, gconstpointer data)
91 {
92   gboolean res = zeitgeist_symbol_is_a (ZEITGEIST_NFO_VECTOR_IMAGE,
93                                         ZEITGEIST_NFO_MEDIA);
94 
95   g_assert_cmpint (res, ==, TRUE);
96 }
97 
98 static void
test_media_vector_image(Fixture * fix,gconstpointer data)99 test_media_vector_image (Fixture *fix, gconstpointer data)
100 {
101   gboolean res = zeitgeist_symbol_is_a (ZEITGEIST_NFO_MEDIA,
102                                         ZEITGEIST_NFO_VECTOR_IMAGE);
103 
104   g_assert_cmpint (res, ==, FALSE);
105 }
106 
107 static void
test_media_software(Fixture * fix,gconstpointer data)108 test_media_software (Fixture *fix, gconstpointer data)
109 {
110   gboolean res = zeitgeist_symbol_is_a (ZEITGEIST_NFO_MEDIA,
111                                         ZEITGEIST_NFO_SOFTWARE);
112 
113   g_assert_cmpint (res, ==, FALSE);
114 }
115 
116 static void
is_uri_valid(gpointer data,gpointer unused)117 is_uri_valid (gpointer data, gpointer unused)
118 {
119   const gchar SEM_D_URI[] = "http://www.semanticdesktop.org/ontologies";
120   gchar *uri = (gchar*) data;
121   g_assert (uri != NULL && g_str_has_prefix (uri, SEM_D_URI));
122   gchar *str = g_strdup_printf ("%s", uri);
123 
124   g_free (str);
125 }
126 
127 static void
test_media_children(Fixture * fix,gconstpointer data)128 test_media_children (Fixture *fix, gconstpointer data)
129 {
130   GList* children = zeitgeist_symbol_get_children (ZEITGEIST_NFO_MEDIA);
131 
132   g_assert_cmpint (g_list_length (children), >, 0);
133   g_list_foreach (children, is_uri_valid, NULL);
134 
135   g_list_free (children);
136 }
137 
138 static void
test_media_all_children(Fixture * fix,gconstpointer data)139 test_media_all_children (Fixture *fix, gconstpointer data)
140 {
141   GList* children = zeitgeist_symbol_get_all_children (ZEITGEIST_NFO_MEDIA);
142 
143   g_assert_cmpint (g_list_length (children), >, 0);
144   g_list_foreach (children, is_uri_valid, NULL);
145 
146   g_list_free (children);
147 }
148 
149 static void
test_vector_image_parents(Fixture * fix,gconstpointer data)150 test_vector_image_parents (Fixture *fix, gconstpointer data)
151 {
152   GList* parents = zeitgeist_symbol_get_parents (ZEITGEIST_NFO_VECTOR_IMAGE);
153 
154   g_assert_cmpint (g_list_length (parents), >, 0);
155   g_list_foreach (parents, is_uri_valid, NULL);
156 
157   g_list_free (parents);
158 }
159 
160 static void
test_media_complex(Fixture * fix,gconstpointer data)161 test_media_complex (Fixture *fix, gconstpointer data)
162 {
163   GList* iter;
164   GList* children = zeitgeist_symbol_get_children (ZEITGEIST_NFO_MEDIA);
165   GList* all_ch = zeitgeist_symbol_get_all_children (ZEITGEIST_NFO_MEDIA);
166 
167   g_assert_cmpint (g_list_length (children), >, 0);
168   g_assert_cmpint (g_list_length (all_ch), >, g_list_length (children));
169 
170   for (iter = children; iter; iter = iter->next)
171   {
172     // check that it's also in all_children
173     g_assert (g_list_find_custom (all_ch, iter->data, (GCompareFunc) strcmp));
174   }
175 
176   g_list_free (all_ch);
177   g_list_free (children);
178 }
179 
180 int
main(int argc,char * argv[])181 main (int   argc,
182       char *argv[])
183 {
184   g_type_init();
185   g_test_init (&argc, &argv, NULL);
186 
187   g_test_add ("/Zeitgeist/Symbols/NullNull", Fixture, NULL,
188               setup, test_null_symbols, teardown);
189   g_test_add ("/Zeitgeist/Symbols/FirstNull", Fixture, NULL,
190               setup, test_null_first, teardown);
191   g_test_add ("/Zeitgeist/Symbols/SecondNull", Fixture, NULL,
192               setup, test_null_second, teardown);
193   g_test_add ("/Zeitgeist/Symbols/NotUris", Fixture, NULL,
194               setup, test_not_uri, teardown);
195   g_test_add ("/Zeitgeist/Symbols/NotUrisEqual", Fixture, NULL,
196               setup, test_not_uri_equal, teardown);
197   g_test_add ("/Zeitgeist/Symbols/EqualUris", Fixture, NULL,
198               setup, test_uris_equal, teardown);
199   g_test_add ("/Zeitgeist/Symbols/ValidParent", Fixture, NULL,
200               setup, test_vector_image_media, teardown);
201   g_test_add ("/Zeitgeist/Symbols/ValidChild", Fixture, NULL,
202               setup, test_media_vector_image, teardown);
203   g_test_add ("/Zeitgeist/Symbols/Unrelated", Fixture, NULL,
204               setup, test_media_software, teardown);
205   g_test_add ("/Zeitgeist/Symbols/GetChildren", Fixture, NULL,
206               setup, test_media_children, teardown);
207   g_test_add ("/Zeitgeist/Symbols/GetAllChildren", Fixture, NULL,
208               setup, test_media_all_children, teardown);
209   g_test_add ("/Zeitgeist/Symbols/GetParents", Fixture, NULL,
210               setup, test_vector_image_parents, teardown);
211   g_test_add ("/Zeitgeist/Symbols/SymbolInfo", Fixture, NULL,
212               setup, test_media_complex, teardown);
213 
214   return g_test_run();
215 }
216