]> git.0d.be Git - empathy.git/blob - tests/check-empathy-helpers.c
automatically save the chatroom mgr XML file when one chatroom is modified
[empathy.git] / tests / check-empathy-helpers.c
1 /*
2  * check-empathy-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 #include "check-empathy-helpers.h"
29
30 gchar *
31 get_xml_file (const gchar *filename)
32 {
33   return g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "tests", "xml",
34       filename, NULL);
35 }
36
37 gchar *
38 get_user_xml_file (const gchar *filename)
39 {
40   return g_build_filename (g_get_tmp_dir (), filename, NULL);
41 }
42
43 void
44 copy_xml_file (const gchar *orig,
45                const gchar *dest)
46 {
47   gboolean result;
48   gchar *buffer;
49   gsize length;
50   gchar *sample;
51   gchar *file;
52
53   sample = get_xml_file (orig);
54   result = g_file_get_contents (sample, &buffer, &length, NULL);
55   fail_if (!result);
56
57   file = get_user_xml_file (dest);
58   result = g_file_set_contents (file, buffer, length, NULL);
59   fail_if (!result);
60
61   g_free (sample);
62   g_free (file);
63   g_free (buffer);
64 }
65
66 McAccount *
67 create_test_account (void)
68 {
69   McProfile *profile;
70   McAccount *account;
71
72   profile = mc_profile_lookup ("test");
73   account = mc_account_create (profile);
74
75   g_object_unref (profile);
76
77   return account;
78 }
79
80 void
81 destroy_test_account (McAccount *account)
82 {
83   GConfClient *client;
84   gchar *path;
85   GError *error = NULL;
86   GSList *entries = NULL, *l;
87
88   client = gconf_client_get_default ();
89   path = g_strdup_printf ("/apps/telepathy/mc/accounts/%s",
90       mc_account_get_unique_name (account));
91
92   entries = gconf_client_all_entries (client, path, &error);
93   if (error != NULL)
94     {
95       g_print ("failed to list entries in %s: %s\n", path, error->message);
96       g_error_free (error);
97       error = NULL;
98     }
99
100   for (l = entries; l != NULL; l = g_slist_next (l))
101     {
102       GConfEntry *entry = l->data;
103
104       if (g_str_has_suffix (entry->key, "data_dir"))
105         {
106           gchar *dir;
107
108           dir = gconf_client_get_string (client, entry->key, &error);
109           if (error != NULL)
110             {
111               g_print ("get data_dir string failed: %s\n", entry->key);
112               g_error_free (error);
113               error = NULL;
114             }
115           else
116             {
117               if (g_rmdir (dir) != 0)
118                 g_print ("can't remove %s\n", dir);
119             }
120
121           g_free (dir);
122         }
123
124       /* FIXME: this doesn't remove the key */
125       gconf_client_unset (client, entry->key, &error);
126       if (error != NULL)
127         {
128           g_print ("unset of %s failed: %s\n", path, error->message);
129           g_error_free (error);
130           error = NULL;
131         }
132
133       gconf_entry_free (entry);
134     }
135
136   g_slist_free (entries);
137
138   g_object_unref (client);
139   g_free (path);
140
141   mc_account_delete (account);
142   g_object_unref (account);
143 }