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