vconf cb debugging info add
[framework/appfw/vconf.git] / test / vconf_unit_test.c
1 /*
2  * vconf
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hakjoo ko <hakjoo.ko@samsung.com>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation; either version 2.1 of the License, or (at your option)
11  * any later version.
12  * 
13  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation, Inc., 51
20  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23
24 #include <stdio.h>
25 #include <vconf.h>
26 #include <Ecore.h>
27 #include <gconf/gconf-client.h>
28 #include <dirent.h>
29
30 const char *vconfkeys[5][5]={
31    {"db", "db/unittest/key1", "db/unittest/key2", "db/unittest/key3", "db/unittest/key4"},
32    {"file", "file/unittest/key1", "file/unittest/key2", "file/unittest/key3", "file/unittest/key4"},
33    {"memory", "memory/unittest/key1", "memory/unittest/key2", "memory/unittest/key3", "memory/unittest/key4"},
34    {"gconf_l", "gconf_l/unittest/key1", "gconf_l/unittest/key2", "gconf_l/unittest/key3", "gconf_l/unittest/key4"},
35    {"gconf_d", "gconf_d/unittest/key1", "gconf_d/unittest/key2", "gconf_d/unittest/key3", "gconf_d/unittest/key4"}
36 };
37
38 #define EPRINT(FMT, ARG...) \
39    fprintf(stderr, "\x1b[31m[VConf Unit Test]\x1b[0m" FMT "\n", ##ARG)
40
41 #define PRINT(FMT, ARG...) \
42    fprintf(stdout, "\x1b[33m[VConf Unit Test]\x1b[0m" FMT "\n", ##ARG)
43
44 static GMainLoop *event_loop;
45
46 void test_cb(keynode_t *key, void* data)
47 {
48    switch(vconf_keynode_get_type(key))
49    {
50       case VCONF_TYPE_INT:
51          printf("key = %s, value = %d(int)\n",
52                vconf_keynode_get_name(key), vconf_keynode_get_int(key));
53          break;
54       case VCONF_TYPE_BOOL:
55          printf("key = %s, value = %d(bool)\n",
56                vconf_keynode_get_name(key), vconf_keynode_get_bool(key));
57          break;
58       case VCONF_TYPE_DOUBLE:
59          printf("key = %s, value = %f(double)\n",
60                vconf_keynode_get_name(key), vconf_keynode_get_dbl(key));
61          break;
62       case VCONF_TYPE_STRING:
63          printf("key = %s, value = %s(string)\n",
64                vconf_keynode_get_name(key), vconf_keynode_get_str(key));
65          break;
66       case VCONF_TYPE_NONE:
67          printf("key = %s, deleted\n", vconf_keynode_get_name(key));
68          if(data == (void *)vconfkeys[4][0]) g_main_loop_quit(event_loop);
69          break;
70       default:
71          fprintf(stderr, "Unknown Type(%d)\n", vconf_keynode_get_type(key));
72          break;
73    }
74
75    PRINT("%s Notification OK", (char *)data);
76    return;
77 }
78
79 void set_operation(int i)
80 {
81    keylist_t *kl=NULL;
82
83    if(vconf_set_int(vconfkeys[i][1], 1))
84       EPRINT("vconf_set_int FAIL(%s)", vconfkeys[i][0]);
85    else
86       PRINT("vconf_set_int OK(%s)", vconfkeys[i][0]);
87       
88    if(vconf_set_dbl(vconfkeys[i][2], 0.1))
89       EPRINT("vconf_set_dbl FAIL(%s)", vconfkeys[i][0]);
90    else
91       PRINT("vconf_set_dbl OK(%s)", vconfkeys[i][0]);
92
93    if(vconf_set_bool(vconfkeys[i][3], 1))
94       EPRINT("vconf_set_bool FAIL(%s)", vconfkeys[i][0]);
95    else
96       PRINT("vconf_set_bool OK(%s)", vconfkeys[i][0]);
97
98    if(vconf_set_str(vconfkeys[i][4], "This is UNIT test"))
99       EPRINT("vconf_set_str FAIL(%s)", vconfkeys[i][0]);
100    else
101       PRINT("vconf_set_str OK(%s)", vconfkeys[i][0]);
102
103    kl = vconf_keylist_new();
104    vconf_keylist_add_dbl(kl, vconfkeys[i][1], 0.2);
105    vconf_keylist_add_bool(kl, vconfkeys[i][2], 0);
106    vconf_keylist_add_str(kl, vconfkeys[i][3], "This is UNIT test");
107    vconf_keylist_add_int(kl, vconfkeys[i][4], 2);
108    if(vconf_set(kl))
109       EPRINT("vconf_set FAIL(%s)", vconfkeys[i][0]);
110    else
111       PRINT("vconf_set OK(%s)", vconfkeys[i][0]);
112    vconf_keylist_free(kl);
113
114 }
115
116
117 void get_operation(int i, const char* parent_dir)
118 {
119    keylist_t *kl=NULL;
120    keynode_t *temp_node;
121    int get_int;
122    double get_dbl;
123    char *get_str;
124
125    if(vconf_get_dbl(vconfkeys[i][1], &get_dbl) || get_dbl != 0.2)
126       EPRINT("vconf_get_dbl FAIL(%s)", vconfkeys[i][0]);
127    else
128       PRINT("vconf_get_dbl OK(%s)", vconfkeys[i][0]);
129
130    if(vconf_get_bool(vconfkeys[i][2], &get_int) || get_int != 0)
131       EPRINT("vconf_get_bool FAIL(%s)", vconfkeys[i][0]);
132    else
133       PRINT("vconf_get_bool OK(%s)", vconfkeys[i][0]);
134
135    get_str=vconf_get_str(vconfkeys[i][3]);
136    if(get_str && !strcmp(get_str, "This is UNIT test"))
137       PRINT("vconf_get_str OK(%s)", vconfkeys[i][0]);
138    else
139       EPRINT("vconf_get_str FAIL(%s)", vconfkeys[i][0]);
140    if(get_str) free(get_str);
141
142    if(vconf_get_int(vconfkeys[i][4], &get_int) || get_int != 2)
143       EPRINT("vconf_get_int FAIL(%s)", vconfkeys[i][0]);
144    else
145       PRINT("vconf_get_int OK(%s)", vconfkeys[i][0]);
146
147    kl = vconf_keylist_new();
148    if(vconf_get(kl, parent_dir, 0))
149       EPRINT("vconf_get FAIL(%s)", vconfkeys[i][0]);
150    else
151       PRINT("vconf_get OK(%s)", vconfkeys[i][0]);
152
153    while((temp_node = vconf_keylist_nextnode(kl))) {
154       switch(vconf_keynode_get_type(temp_node)) {
155          case VCONF_TYPE_INT:
156             printf("key = %s, value = %d\n",
157                   vconf_keynode_get_name(temp_node), vconf_keynode_get_int(temp_node));
158             break;
159          case VCONF_TYPE_BOOL:
160             printf("key = %s, value = %d\n",
161                   vconf_keynode_get_name(temp_node), vconf_keynode_get_bool(temp_node));
162             break;
163          case VCONF_TYPE_DOUBLE:
164             printf("key = %s, value = %f\n",
165                   vconf_keynode_get_name(temp_node), vconf_keynode_get_dbl(temp_node));
166             break;
167          case VCONF_TYPE_STRING:
168             printf("key = %s, value = %s\n",
169                   vconf_keynode_get_name(temp_node), vconf_keynode_get_str(temp_node));
170             break;
171          default:
172             printf("Unknown Type\n");
173       }
174    }
175    vconf_keylist_free(kl);
176 }
177
178 void unset_operation(int i)
179 {
180    if(vconf_unset(vconfkeys[i][1]))
181       EPRINT("vconf_unset FAIL(%s)", vconfkeys[i][0]);
182    else
183       PRINT("vconf_unset OK(%s)", vconfkeys[i][0]);
184    if(vconf_unset(vconfkeys[i][2]))
185       EPRINT("vconf_unset FAIL(%s)", vconfkeys[i][0]);
186    else
187       PRINT("vconf_unset OK(%s)", vconfkeys[i][0]);
188    if(vconf_unset(vconfkeys[i][3]))
189       EPRINT("vconf_unset FAIL(%s)", vconfkeys[i][0]);
190    else
191       PRINT("vconf_unset OK(%s)", vconfkeys[i][0]);
192    if(vconf_unset(vconfkeys[i][4]))
193       EPRINT("vconf_unset FAIL(%s)", vconfkeys[i][0]);
194    else
195       PRINT("vconf_unset OK(%s)", vconfkeys[i][0]);
196 }
197
198 const char *test_dir[5]={
199    "/opt/var/kdb/db/unittest", "/opt/var/kdb/file/unittest", "/var/run/memory/unittest",
200    "/opt/var/gconf/local/unittest", "/opt/var/gconf/unittest"};
201
202 void recursive_unset_operation(void)
203 {
204    char parent[1024];
205    int i,j;
206    for(i=0;i<5;i++) {
207       for(j=1;j<5;j++)
208          vconf_set_int(vconfkeys[i][j],1);
209
210       sprintf(parent, "%s/unittest", vconfkeys[i][0]);
211       if(vconf_unset_recursive(parent))
212          EPRINT("vconf_unset_recursive FAIL(%s)", parent);
213       else {
214          if(opendir(test_dir[i]))
215             EPRINT("vconf_unset_recursive FAIL(%s)", parent);
216          else
217             PRINT("vconf_unset_recursive OK(%s)", parent);
218       }
219    }
220 }
221
222 void notify_operation(int i)
223 {
224    int j;
225    for(j=1;j<5;j++)
226    {
227       if(vconf_notify_key_changed(vconfkeys[i][j], test_cb, (void *)vconfkeys[i][0])) {
228          if(3==i)
229             PRINT("vconf_notify_key_changed OK(%s)", vconfkeys[i][j]);
230          else
231             EPRINT("vconf_notify_key_changed FAIL(%s)", vconfkeys[i][j]);
232       }else {
233          if(3==i)
234             EPRINT("vconf_notify_key_changed FAIL(%s)", vconfkeys[i][j]);
235          else               
236             PRINT("vconf_notify_key_changed OK(%s)", vconfkeys[i][j]);
237       }
238    }
239 }
240    
241 int main(int argc, char **argv)
242 {
243    g_type_init();
244
245    set_operation(0);
246    set_operation(1);
247    set_operation(2);
248    set_operation(3);
249    set_operation(4);
250    get_operation(0, "db/unittest");
251    get_operation(1, "file/unittest");
252    get_operation(2, "memory/unittest");
253    get_operation(3, "gconf_l/unittest");
254    get_operation(4, "gconf_d/unittest");
255    unset_operation(0);
256    unset_operation(1);
257    unset_operation(2);
258    unset_operation(3);
259    unset_operation(4);
260    recursive_unset_operation();
261    notify_operation(0);
262    notify_operation(1);
263    notify_operation(2);
264    notify_operation(3);
265    notify_operation(4);
266    printf("For Notification TEST, You have to excute below command\n");
267    printf("vconftool set -t int db/unittest/key1 1\n");
268    printf("vconftool set -t int file/unittest/key2 1\n");
269    printf("vconftool set -t int memory/unittest/key3 1\n");
270    printf("vconftool set -t int gconf_d/unittest/key4 1\n");
271    printf("vconftool unset db/unittest/key1\n");
272    printf("vconftool unset file/unittest/key2\n");
273    printf("vconftool unset memory/unittest/key3\n");
274    printf("vconftool unset gconf_d/unittest/key4\n");
275
276    //ecore_main_loop_begin();
277    event_loop = g_main_loop_new(NULL, FALSE);
278    g_main_loop_run(event_loop);
279
280    EPRINT("Did you get 8 Notifications\n");
281
282    return 0;
283 }