]> git.0d.be Git - empathy.git/blob - tests/test-helper.c
Updated Polish translation
[empathy.git] / tests / test-helper.c
1 /*
2  * test-helper.c - Source for some test helper functions
3  * Copyright (C) 2007-2009 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 <stdlib.h>
21 #include <glib.h>
22 #include <glib-object.h>
23 #include <gtk/gtk.h>
24
25 #include <libempathy-gtk/empathy-ui-utils.h>
26
27 #include "test-helper.h"
28
29 void
30 test_init (int argc,
31     char **argv)
32 {
33   g_test_init (&argc, &argv, NULL);
34   gtk_init (&argc, &argv);
35   empathy_gtk_init ();
36 }
37
38 void
39 test_deinit (void)
40 {
41   ;
42 }
43
44 gchar *
45 get_xml_file (const gchar *filename)
46 {
47   return g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "tests", "xml",
48       filename, NULL);
49 }
50
51 gchar *
52 get_user_xml_file (const gchar *filename)
53 {
54   return g_build_filename (g_get_tmp_dir (), filename, NULL);
55 }
56
57 void
58 copy_xml_file (const gchar *orig,
59                const gchar *dest)
60 {
61   gboolean result;
62   gchar *buffer;
63   gsize length;
64   gchar *sample;
65   gchar *file;
66
67   sample = get_xml_file (orig);
68   result = g_file_get_contents (sample, &buffer, &length, NULL);
69   g_assert (result);
70
71   file = get_user_xml_file (dest);
72   result = g_file_set_contents (file, buffer, length, NULL);
73   g_assert (result);
74
75   g_free (sample);
76   g_free (file);
77   g_free (buffer);
78 }
79
80 #if 0
81 EmpathyAccount *
82 get_test_account (void)
83 {
84   McProfile *profile;
85   EmpathyAccountManager *account_manager;
86   EmpathyAccount *account;
87   GList *accounts;
88
89   account_manager = empathy_account_manager_dup_singleton ();
90   profile = mc_profile_lookup ("test");
91   accounts = mc_accounts_list_by_profile (profile);
92   if (g_list_length (accounts) == 0)
93     {
94       /* need to create a test account */
95       account = empathy_account_manager_create_by_profile (account_manager,
96           profile);
97     }
98   else
99     {
100       /* reuse an existing test account */
101       McAccount *mc_account;
102       mc_account = accounts->data;
103       account = empathy_account_manager_lookup (account_manager,
104         mc_account_get_unique_name (mc_account));
105     }
106   g_object_unref (account_manager);
107
108   g_object_unref (profile);
109
110   return account;
111 }
112
113 /* Not used for now as there is no API to remove completely gconf keys.
114  * So we reuse existing accounts instead of creating new ones */
115 void
116 destroy_test_account (EmpathyAccount *account)
117 {
118   GConfClient *client;
119   gchar *path;
120   GError *error = NULL;
121   GSList *entries = NULL, *l;
122   EmpathyAccountManager *manager;
123
124   client = gconf_client_get_default ();
125   path = g_strdup_printf ("/apps/telepathy/mc/accounts/%s",
126       empathy_account_get_unique_name (account));
127
128   entries = gconf_client_all_entries (client, path, &error);
129   if (error != NULL)
130     {
131       g_print ("failed to list entries in %s: %s\n", path, error->message);
132       g_error_free (error);
133       error = NULL;
134     }
135
136   for (l = entries; l != NULL; l = g_slist_next (l))
137     {
138       GConfEntry *entry = l->data;
139
140       if (g_str_has_suffix (entry->key, "data_dir"))
141         {
142           gchar *dir;
143
144           dir = gconf_client_get_string (client, entry->key, &error);
145           if (error != NULL)
146             {
147               g_print ("get data_dir string failed: %s\n", entry->key);
148               g_error_free (error);
149               error = NULL;
150             }
151           else
152             {
153               if (g_rmdir (dir) != 0)
154                 g_print ("can't remove %s\n", dir);
155             }
156
157           g_free (dir);
158         }
159
160       /* FIXME: this doesn't remove the key */
161       gconf_client_unset (client, entry->key, &error);
162       if (error != NULL)
163         {
164           g_print ("unset of %s failed: %s\n", path, error->message);
165           g_error_free (error);
166           error = NULL;
167         }
168
169       gconf_entry_unref (entry);
170     }
171
172   g_slist_free (entries);
173
174   g_object_unref (client);
175   g_free (path);
176
177   manager = empathy_account_manager_dup_singleton ();
178   empathy_account_manager_remove (manager, account);
179   g_object_unref (account);
180   g_object_unref (manager);
181 }
182 #endif