Add some more appinfo tests
[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
12   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
13   g_assert (appinfo != NULL);
14
15   g_assert (g_app_info_launch (appinfo, NULL, NULL, NULL));
16 }
17
18 static void
19 test_locale (const char *locale)
20 {
21   GAppInfo *appinfo;
22   const gchar *orig;
23
24   orig = setlocale (LC_ALL, NULL);
25   g_setenv ("LANGUAGE", locale, TRUE);
26   setlocale (LC_ALL, "");
27
28   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
29
30   if (g_strcmp0 (locale, "C") == 0)
31     {
32       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
33       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
34       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
35     }
36   else if (g_str_has_prefix (locale, "en"))
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, "de"))
43     {
44       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
45       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
46       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
47     }
48
49   g_object_unref (appinfo);
50
51   g_setenv ("LANGUAGE", orig, TRUE);
52   setlocale (LC_ALL, "");
53 }
54
55 static void
56 test_text (void)
57 {
58   test_locale ("C");
59   test_locale ("en_US");
60   test_locale ("de");
61   test_locale ("de_DE.UTF-8");
62 }
63
64 static void
65 test_basic (void)
66 {
67   GAppInfo *appinfo;
68   GIcon *icon, *icon2;
69
70   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
71
72   g_assert (g_app_info_get_id (appinfo) == NULL);
73   g_assert_cmpstr (g_app_info_get_executable (appinfo), ==, "./appinfo-test");
74   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option");
75
76   icon = g_app_info_get_icon (appinfo);
77   g_assert (G_IS_THEMED_ICON (icon));
78   icon2 = g_themed_icon_new ("testicon");
79   g_assert (g_icon_equal (icon, icon2));
80   g_object_unref (icon2);
81
82   g_object_unref (appinfo);
83 }
84
85 static void
86 test_show_in (void)
87 {
88   GAppInfo *appinfo;
89
90   g_desktop_app_info_set_desktop_env ("GNOME");
91
92   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
93   g_assert (g_app_info_should_show (appinfo));
94   g_object_unref (appinfo);
95
96   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-gnome.desktop");
97   g_assert (g_app_info_should_show (appinfo));
98   g_object_unref (appinfo);
99
100   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-notgnome.desktop");
101   g_assert (!g_app_info_should_show (appinfo));
102   g_object_unref (appinfo);
103 }
104
105 static void
106 test_commandline (void)
107 {
108   GAppInfo *appinfo;
109   GError *error;
110
111   error = NULL;
112   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
113                                                 "cmdline-app-test",
114                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
115                                                 &error);
116   g_assert (appinfo != NULL);
117   g_assert_no_error (error);
118   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
119   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %u");
120   g_assert (g_app_info_supports_uris (appinfo));
121   g_assert (!g_app_info_supports_files (appinfo));
122
123   g_object_unref (appinfo);
124
125   error = NULL;
126   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
127                                                 "cmdline-app-test",
128                                                 G_APP_INFO_CREATE_NONE,
129                                                 &error);
130   g_assert (appinfo != NULL);
131   g_assert_no_error (error);
132   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
133   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %f");
134   g_assert (!g_app_info_supports_uris (appinfo));
135   g_assert (g_app_info_supports_files (appinfo));
136
137   g_object_unref (appinfo);
138 }
139
140 int
141 main (int argc, char *argv[])
142 {
143   g_type_init ();
144   g_test_init (&argc, &argv, NULL);
145
146   g_test_add_func ("/appinfo/basic", test_basic);
147   g_test_add_func ("/appinfo/text", test_text);
148   g_test_add_func ("/appinfo/launch", test_launch);
149   g_test_add_func ("/appinfo/show-in", test_show_in);
150   g_test_add_func ("/appinfo/commandline", test_commandline);
151
152   return g_test_run ();
153 }
154