Tizen 2.0 Release
[platform/core/messaging/email-service.git] / utilities / test-application / main.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23
24 /* common header */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <sys/time.h>
29 #include <unistd.h>
30
31 /* open header */
32 #include <glib.h>
33
34 #include "email-api-init.h"
35
36 /* internal header */
37 #include "testapp-utility.h"
38 #include "testapp-account.h"
39 #include "testapp-mail.h"
40 #include "testapp-mailbox.h"
41 #include "testapp-rule.h"
42 #include "testapp-thread.h"
43 #include "testapp-others.h"
44 #include "db-util.h"
45
46 /* function prototype */
47 static void testapp_system_signal_handler (int signal_number);
48
49
50 /* implementation */
51 static gboolean testapp_initialize_testing ()
52 {
53         struct timeval tv_1, tv_2;
54         int interval;
55         int error;
56         
57         /* register signal handler */
58         if ( signal (SIGINT, testapp_system_signal_handler) == SIG_ERR ) {
59                 testapp_print ("register signal handler fail\n");
60                 return FALSE;
61         }
62
63         if ( signal (SIGQUIT, testapp_system_signal_handler) == SIG_ERR ) {
64                 testapp_print ("register signal handler fail\n");
65                 return FALSE;
66         }
67
68         if ( signal (SIGTSTP, testapp_system_signal_handler) == SIG_ERR ) {
69                 testapp_print ("register signal handler fail\n");
70                 return FALSE;
71         }
72
73         if ( signal (SIGTERM, testapp_system_signal_handler) == SIG_ERR ) {
74                 testapp_print ("register signal handler fail\n");
75                 return FALSE;
76         }
77
78         
79         gettimeofday(&tv_1, NULL);
80         
81         if ( email_service_begin() != EMAIL_ERROR_NONE ) {
82                 testapp_print ("unexpected error: opening email service fail\n");
83                 return FALSE;
84         }
85         gettimeofday(&tv_2, NULL);
86         interval = tv_2.tv_usec - tv_1.tv_usec;
87         testapp_print("\t email_service_begin Proceed time %d us\n",interval);
88
89         gettimeofday(&tv_1, NULL);
90         if ( (error = email_open_db()) != EMAIL_ERROR_NONE) {
91                 testapp_print("email_open_db failed [%d]\n", error);
92         }
93         gettimeofday(&tv_2, NULL);
94         interval = tv_2.tv_usec - tv_1.tv_usec;
95         testapp_print("\t email_open_db Proceed time %d us\n",interval);
96
97         return TRUE;
98 }
99
100 static gboolean testapp_finalize_testing ()
101 {
102         int error;
103
104         if ( (error = email_close_db()) != EMAIL_ERROR_NONE) {
105                 testapp_print("email_close_db failed [%d]\n", error);
106         }       
107
108         if ( email_service_end() != EMAIL_ERROR_NONE) {
109                 testapp_print ("unexpected error: closing email service fail \n");
110         }
111
112         return TRUE;
113 }
114
115 static void testapp_system_signal_handler (int signal_number)
116 {
117         testapp_print ("signal:%d\n", signal_number);
118         switch (signal_number) {
119                 case SIGQUIT:
120                 case SIGINT:
121                 case SIGTSTP:
122                 case SIGTERM:
123                         testapp_finalize_testing();
124                         break;
125
126                 default:
127                         testapp_print ("unhandled signal:%d\n", signal_number);
128                         break;
129         }
130         exit(0);
131 }
132
133
134 static gboolean testapp_interpret_command (int menu_number)
135 {
136         gboolean go_to_loop = TRUE;
137
138         switch (menu_number) {
139                 case 1:
140                         testapp_account_main();
141                         break;
142                         
143                 case 2:
144                         testapp_mail_main();
145                         break;
146                         
147                 case 3:
148                         email_test_mailbox_main();
149                         break;
150                         
151                 case 4:
152                         break;
153                         
154                 case 5:
155                         email_test_rule_main();
156                         break;
157                         
158                 case 6:
159                         testapp_thread_main();
160                         break;
161                         
162                 case 7:
163                         testapp_others_main();
164                         break;
165                 case 0:
166                         go_to_loop = FALSE;
167                         break;
168                 default:
169                         break;
170         }
171
172         return go_to_loop;
173 }
174
175 int main (int argc, char *argv[])
176 {
177         gboolean go_to_loop = TRUE;
178         int menu_number = 0;
179         int result_from_scanf = 0;
180
181         if ( testapp_initialize_testing() == FALSE ) {
182                 testapp_print ("email-serivce is not ready\n");
183                 exit(0);
184         }
185
186         while (go_to_loop) {
187                 testapp_show_menu (EMAIL_MAIN_MENU);
188                 testapp_show_prompt (EMAIL_MAIN_MENU);
189
190                 result_from_scanf = scanf ("%d", &menu_number);
191
192                 go_to_loop = testapp_interpret_command (menu_number);
193         }
194
195         testapp_finalize_testing();
196
197         exit(0);
198 }
199
200