* Fixes NB#105808, properly wakeup suspended messages when clicking on send&receive
[modest] / tests / check_modest-conf.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <check.h>
31 #include <modest-defs.h>
32 #include <modest-conf.h>
33 #include <gtk/gtk.h>
34 #include <string.h>
35 #include <modest-init.h>
36
37 static void
38 fx_setup_modest_conf ()
39 {
40         fail_unless (gtk_init_check (NULL, NULL));
41
42         fail_unless (g_setenv (MODEST_DIR_ENV, ".modesttest", TRUE));
43         fail_unless (g_setenv (MODEST_NAMESPACE_ENV, "/apps/modesttest", TRUE));
44
45         fail_unless (modest_init (0, NULL), "Failed running modest_init");
46
47 }
48
49 START_TEST (test_modest_conf_new)
50 {
51         ModestConf *conf = modest_conf_new ();
52         fail_unless (MODEST_IS_CONF(conf),
53                      "modest_conf_new should return a valid"
54                      " ModestConf instance");
55         g_object_unref (conf);
56 }
57 END_TEST
58
59
60 START_TEST (test_modest_conf_store_retrieve_string)
61 {
62         ModestConf *conf  = modest_conf_new ();
63         const gchar *key  =  modest_defs_namespace ("/teststring");
64         const gchar *key2 =  modest_defs_namespace ("/teststring2");
65
66         const gchar *data = "hello in Korean:  안녕하세요";
67         gchar *data2;
68         
69         fail_unless (MODEST_IS_CONF(conf),
70                      "modest_conf_new should return a valid"
71                      " ModestConf instance");
72
73         fail_unless (modest_conf_set_string (
74                              conf, key, data, NULL),
75                      "modest_conf_set_string should return TRUE");
76
77         fail_unless (modest_conf_key_exists(conf, key, NULL),
78                      "modest_conf_key_exists should return TRUE for <key>");
79         fail_unless (!modest_conf_key_exists(conf, key2, NULL),
80                      "modest_conf_key_exists should return FALSE for <key2>");
81         
82         data2 = modest_conf_get_string (conf, key, NULL);
83         fail_unless (data2 && strcmp (data2, data) == 0,
84                      "modest_conf_get_string should return what we put there");                                 
85         g_free (data2);
86         
87         fail_unless (modest_conf_remove_key (conf, key, NULL),
88                      "modest_conf_remove_key should return TRUE");
89         
90         fail_unless (!modest_conf_key_exists(conf, key, NULL),
91                      "modest_conf_key should return FALSE after we"
92                      "removed the key");
93         
94         g_object_unref (conf);
95 }
96 END_TEST
97
98
99
100 START_TEST (test_modest_conf_store_retrieve_bool)
101 {
102         ModestConf *conf  = modest_conf_new ();
103         const gchar *key  =  modest_defs_namespace ("/teststring");
104         const gchar *key2 =  modest_defs_namespace ("/teststring2");
105
106         gboolean data = TRUE, data2;
107         
108         fail_unless (MODEST_IS_CONF(conf),
109                      "modest_conf_new should return a valid"
110                      " ModestConf instance");
111
112         fail_unless (modest_conf_set_bool (conf, key, data, NULL),
113                      "modest_conf_set_bool should return TRUE");
114         
115         fail_unless (modest_conf_key_exists(conf, key, NULL),
116                      "modest_conf_key_exists should return TRUE for <key>");
117         fail_unless (!modest_conf_key_exists(conf, key2, NULL),
118                      "modest_conf_key_exists should return FALSE for <key2>");
119         
120         data2 = modest_conf_get_bool (conf, key, NULL);
121         fail_unless (data2 == data,
122                      "modest_conf_get_bool should return what we put there");                                   
123         fail_unless (modest_conf_remove_key (conf, key, NULL),
124                      "modest_conf_remove_key should return TRUE");
125         
126         fail_unless (!modest_conf_key_exists(conf, key, NULL),
127                      "modest_conf_key should return FALSE after we"
128                      "removed the key");
129
130         g_object_unref (conf);
131 }
132 END_TEST
133
134
135 START_TEST (test_modest_conf_store_retrieve_int)
136 {
137         ModestConf *conf  = modest_conf_new ();
138         const gchar *key  =  modest_defs_namespace ("/teststring");
139         const gchar *key2 =  modest_defs_namespace ("/teststring2");
140
141         gint data = 99, data2;
142         
143         fail_unless (MODEST_IS_CONF(conf),
144                      "modest_conf_new should return a valid"
145                      " ModestConf instance");
146
147         fail_unless (modest_conf_set_int (conf, key, data, NULL),
148                      "modest_conf_set_int should return TRUE");
149         
150         fail_unless (modest_conf_key_exists(conf, key, NULL),
151                      "modest_conf_key_exists should return TRUE for <key>");
152         fail_unless (!modest_conf_key_exists(conf, key2, NULL),
153                      "modest_conf_key_exists should return FALSE for <key2>");
154         
155         data2 = modest_conf_get_int (conf, key, NULL);
156         fail_unless (data2 == data,
157                      "modest_conf_get_int should return what we put there");                                    
158         fail_unless (modest_conf_remove_key (conf, key, NULL),
159                      "modest_conf_remove_key should return TRUE");
160         
161         fail_unless (!modest_conf_key_exists(conf, key, NULL),
162                      "modest_conf_key should return FALSE after we"
163                      "removed the key");
164
165         g_object_unref (conf);
166 }
167 END_TEST
168
169
170
171 static Suite*
172 modest_conf_suite (void)
173 {
174         Suite *suite = suite_create ("ModestConf");
175
176         TCase *tc_core = tcase_create ("core");
177         tcase_add_checked_fixture (tc_core,
178                                    fx_setup_modest_conf,
179                                    NULL);
180         tcase_add_test (tc_core, test_modest_conf_new);
181         tcase_add_test (tc_core, test_modest_conf_store_retrieve_string);
182         tcase_add_test (tc_core, test_modest_conf_store_retrieve_bool);
183         tcase_add_test (tc_core, test_modest_conf_store_retrieve_int);
184
185         suite_add_tcase (suite, tc_core);
186
187         return suite;
188 }
189
190
191 int
192 main ()
193 {
194         SRunner *srunner;
195         Suite   *suite;
196         int     failures;
197         
198         g_type_init();
199
200         suite   = modest_conf_suite ();
201         srunner = srunner_create (suite);
202
203         srunner_run_all (srunner, CK_ENV);
204         failures = srunner_ntests_failed (srunner);
205         srunner_free (srunner);
206         
207         return failures;
208 }