]> git.0d.be Git - empathy.git/blob - tests/check-helpers.c
276cf11b28eadd533a02fbc5773b9a0b98ffa76b
[empathy.git] / tests / check-helpers.c
1 /*
2  * check-helpers.c - Source for some check helpers
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include <glib/gstdio.h>
24 #include <gconf/gconf.h>
25 #include <gconf/gconf-client.h>
26
27 #include "check-helpers.h"
28
29 static gboolean expecting_critical = FALSE;
30 static gboolean received_critical  = FALSE;
31
32 static void
33 check_helper_log_critical_func (const gchar *log_damain,
34                                 GLogLevelFlags log_level,
35                                 const gchar *message,
36                                 gpointer user_data)
37 {
38
39   if (!expecting_critical)
40     {
41       fail("Unexpected critical message: %s\n", message);
42     }
43
44   g_assert (log_level & G_LOG_LEVEL_CRITICAL);
45
46   received_critical = TRUE;
47 }
48
49 gboolean
50 got_critical (void)
51 {
52   return received_critical;
53 }
54
55 void
56 expect_critical (gboolean expected)
57 {
58   expecting_critical = expected;
59   received_critical = FALSE;
60 }
61
62 void
63 check_helpers_init (void)
64 {
65   g_log_set_handler (NULL, G_LOG_LEVEL_CRITICAL,
66       check_helper_log_critical_func, NULL);
67 }
68
69 gchar *
70 get_xml_file (const gchar *filename)
71 {
72   return g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "tests", "xml",
73       filename, NULL);
74 }
75
76 gchar *
77 get_user_xml_file (const gchar *filename)
78 {
79   return g_build_filename (g_get_tmp_dir (), filename, NULL);
80 }
81
82 void
83 copy_xml_file (const gchar *orig,
84                const gchar *dest)
85 {
86   gboolean result;
87   gchar *buffer;
88   gsize length;
89   gchar *sample;
90   gchar *file;
91
92   sample = get_xml_file (orig);
93   result = g_file_get_contents (sample, &buffer, &length, NULL);
94   fail_if (!result);
95
96   file = get_user_xml_file (dest);
97   result = g_file_set_contents (file, buffer, length, NULL);
98   fail_if (!result);
99
100   g_free (sample);
101   g_free (file);
102   g_free (buffer);
103 }
104
105 void
106 remove_account_from_gconf (McAccount *account)
107 {
108   GConfClient *client;
109   gchar *path;
110   GError *error = NULL;
111   GSList *entries = NULL, *l;
112
113   client = gconf_client_get_default ();
114   path = g_strdup_printf ("/apps/telepathy/mc/accounts/%s",
115       mc_account_get_unique_name (account));
116
117   entries = gconf_client_all_entries (client, path, &error);
118   if (error != NULL)
119     {
120       g_print ("failed to list entries in %s: %s\n", path, error->message);
121       g_error_free (error);
122       error = NULL;
123     }
124
125   for (l = entries; l != NULL; l = g_slist_next (l))
126     {
127       GConfEntry *entry = l->data;
128
129       if (g_str_has_suffix (entry->key, "data_dir"))
130         {
131           gchar *dir;
132
133           dir = gconf_client_get_string (client, entry->key, &error);
134           if (error != NULL)
135             {
136               g_print ("get data_dir string failed: %s\n", entry->key);
137               g_error_free (error);
138               error = NULL;
139             }
140           else
141             {
142               if (g_rmdir (dir) != 0)
143                 g_print ("can't remove %s\n", dir);
144             }
145         }
146
147       /* FIXME: this doesn't remove the key */
148       gconf_client_unset (client, entry->key, &error);
149       if (error != NULL)
150         {
151           g_print ("unset of %s failed: %s\n", path, error->message);
152           g_error_free (error);
153           error = NULL;
154         }
155
156       gconf_entry_free (entry);
157     }
158
159   g_slist_free (entries);
160
161   g_object_unref (client);
162   g_free (path);
163 }