Misc test additions
[platform/upstream/glib.git] / gio / tests / appinfo.c
1
2 #include <locale.h>
3
4 #include <gio/gio.h>
5 #include <gio/gdesktopappinfo.h>
6
7 static void
8 test_launch (void)
9 {
10   GAppInfo *appinfo;
11   GError *error;
12
13   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
14   g_assert (appinfo != NULL);
15
16   error = NULL;
17   g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
18   g_assert_no_error (error);
19
20   g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
21   g_assert_no_error (error);
22 }
23
24 static void
25 test_locale (const char *locale)
26 {
27   GAppInfo *appinfo;
28   const gchar *orig;
29
30   orig = setlocale (LC_ALL, NULL);
31   g_setenv ("LANGUAGE", locale, TRUE);
32   setlocale (LC_ALL, "");
33
34   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
35
36   if (g_strcmp0 (locale, "C") == 0)
37     {
38       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
39       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
40       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
41     }
42   else if (g_str_has_prefix (locale, "en"))
43     {
44       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
45       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
46       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
47     }
48   else if (g_str_has_prefix (locale, "de"))
49     {
50       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
51       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
52       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
53     }
54
55   g_object_unref (appinfo);
56
57   g_setenv ("LANGUAGE", orig, TRUE);
58   setlocale (LC_ALL, "");
59 }
60
61 static void
62 test_text (void)
63 {
64   test_locale ("C");
65   test_locale ("en_US");
66   test_locale ("de");
67   test_locale ("de_DE.UTF-8");
68 }
69
70 static void
71 test_basic (void)
72 {
73   GAppInfo *appinfo;
74   GAppInfo *appinfo2;
75   GIcon *icon, *icon2;
76
77   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
78
79   g_assert (g_app_info_get_id (appinfo) == NULL);
80   g_assert_cmpstr (g_app_info_get_executable (appinfo), ==, "./appinfo-test");
81   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option");
82
83   icon = g_app_info_get_icon (appinfo);
84   g_assert (G_IS_THEMED_ICON (icon));
85   icon2 = g_themed_icon_new ("testicon");
86   g_assert (g_icon_equal (icon, icon2));
87   g_object_unref (icon2);
88
89   appinfo2 = g_app_info_dup (appinfo);
90   g_assert (g_app_info_get_id (appinfo) == g_app_info_get_id (appinfo2));
91   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
92
93   g_object_unref (appinfo);
94   g_object_unref (appinfo2);
95 }
96
97 static void
98 test_show_in (void)
99 {
100   GAppInfo *appinfo;
101
102   g_desktop_app_info_set_desktop_env ("GNOME");
103
104   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
105   g_assert (g_app_info_should_show (appinfo));
106   g_object_unref (appinfo);
107
108   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-gnome.desktop");
109   g_assert (g_app_info_should_show (appinfo));
110   g_object_unref (appinfo);
111
112   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-notgnome.desktop");
113   g_assert (!g_app_info_should_show (appinfo));
114   g_object_unref (appinfo);
115 }
116
117 static void
118 test_commandline (void)
119 {
120   GAppInfo *appinfo;
121   GError *error;
122
123   error = NULL;
124   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
125                                                 "cmdline-app-test",
126                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
127                                                 &error);
128   g_assert (appinfo != NULL);
129   g_assert_no_error (error);
130   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
131   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %u");
132   g_assert (g_app_info_supports_uris (appinfo));
133   g_assert (!g_app_info_supports_files (appinfo));
134
135   g_object_unref (appinfo);
136
137   error = NULL;
138   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
139                                                 "cmdline-app-test",
140                                                 G_APP_INFO_CREATE_NONE,
141                                                 &error);
142   g_assert (appinfo != NULL);
143   g_assert_no_error (error);
144   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
145   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %f");
146   g_assert (!g_app_info_supports_uris (appinfo));
147   g_assert (g_app_info_supports_files (appinfo));
148
149   g_object_unref (appinfo);
150 }
151
152 static void
153 test_launch_context (void)
154 {
155   GAppLaunchContext *context;
156   GAppInfo *appinfo;
157   gchar *str;
158
159   context = g_app_launch_context_new ();
160   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
161                                                 "cmdline-app-test",
162                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
163                                                 NULL);
164
165   str = g_app_launch_context_get_display (context, appinfo, NULL);
166   g_assert (str == NULL);
167
168   str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
169   g_assert (str == NULL);
170
171   g_object_unref (appinfo);
172   g_object_unref (context);
173 }
174
175 static void
176 test_tryexec (void)
177 {
178   GAppInfo *appinfo;
179
180   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test2.desktop");
181
182   g_assert (appinfo == NULL);
183 }
184
185 int
186 main (int argc, char *argv[])
187 {
188   g_type_init ();
189   g_test_init (&argc, &argv, NULL);
190
191   g_test_add_func ("/appinfo/basic", test_basic);
192   g_test_add_func ("/appinfo/text", test_text);
193   g_test_add_func ("/appinfo/launch", test_launch);
194   g_test_add_func ("/appinfo/show-in", test_show_in);
195   g_test_add_func ("/appinfo/commandline", test_commandline);
196   g_test_add_func ("/appinfo/launch-context", test_launch_context);
197   g_test_add_func ("/appinfo/tryexec", test_tryexec);
198
199   return g_test_run ();
200 }
201