Give detail license information
[platform/core/api/notification.git] / test-app / main.c
1 /*
2  *  Test application for notification API
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Kyuho Jo <kyuho.jo@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 /* notification header */
35 #include <notification.h>
36 #include <notification_status.h>
37
38 /*-----------------------------------------------------------------------------------------*/
39 /* types */
40 typedef enum testapp_menu_type {
41         TESTAPP_MENU_TYPE_MAIN_MENU = 1,
42         TESTAPP_MENU_TYPE_BASIC_TEST_MENU = 2,
43 } testapp_menu_type_e;
44
45 /*-----------------------------------------------------------------------------------------*/
46 /* function prototypes */
47 static void testapp_system_signal_handler (int signal_number);
48 void testapp_show_prompt (testapp_menu_type_e menu);
49
50 /*-----------------------------------------------------------------------------------------*/
51 /* implementation */
52 void testapp_print (char *fmt, ...)
53 {
54         va_list args = {0};
55         va_start(args, fmt);
56         vfprintf (stdout, fmt, args);
57         va_end (args);
58         fflush (stdout);
59 }
60
61 static gboolean testapp_initialize_testing ()
62 {
63         struct timeval tv_1, tv_2;
64         int interval;
65
66         /* register signal handler */
67         if ( signal (SIGINT, testapp_system_signal_handler) == SIG_ERR ) {
68                 testapp_print ("register signal handler fail\n");
69                 return FALSE;
70         }
71
72         if ( signal (SIGQUIT, testapp_system_signal_handler) == SIG_ERR ) {
73                 testapp_print ("register signal handler fail\n");
74                 return FALSE;
75         }
76
77         if ( signal (SIGTSTP, testapp_system_signal_handler) == SIG_ERR ) {
78                 testapp_print ("register signal handler fail\n");
79                 return FALSE;
80         }
81
82         if ( signal (SIGTERM, testapp_system_signal_handler) == SIG_ERR ) {
83                 testapp_print ("register signal handler fail\n");
84                 return FALSE;
85         }
86
87
88         gettimeofday(&tv_1, NULL);
89
90         /* TODO : initializing notification */
91
92         gettimeofday(&tv_2, NULL);
93         interval = tv_2.tv_usec - tv_1.tv_usec;
94         testapp_print("\t Initializing Proceed time %d us\n",interval);
95
96
97         return TRUE;
98 }
99
100 static gboolean testapp_finalize_testing ()
101 {
102         /* TODO : finalizing notification */
103
104         return TRUE;
105 }
106
107 static void testapp_system_signal_handler (int signal_number)
108 {
109         testapp_print ("signal:%d\n", signal_number);
110         switch (signal_number) {
111         case SIGQUIT:
112         case SIGINT:
113         case SIGTSTP:
114         case SIGTERM:
115                 testapp_finalize_testing();
116                 break;
117
118         default:
119                 testapp_print ("unhandled signal:%d\n", signal_number);
120                 break;
121         }
122         exit(0);
123 }
124
125
126
127
128 void testapp_show_menu (testapp_menu_type_e menu)
129 {
130         switch (menu) {
131         case TESTAPP_MENU_TYPE_MAIN_MENU:
132                 testapp_print ("==========================================\n");
133                 testapp_print ("    Notification test application \n");
134                 testapp_print ("==========================================\n");
135                 testapp_print ("1. Basic Test\n");
136                 testapp_print ("0. Exit \n");
137                 testapp_print ("------------------------------------------\n");
138                 break;
139         case TESTAPP_MENU_TYPE_BASIC_TEST_MENU:
140                 testapp_print ("==========================================\n");
141                 testapp_print ("    Basic test menu \n");
142                 testapp_print ("==========================================\n");
143                 testapp_print (" 1.  Post a simple notification\n");
144                 testapp_print (" 2.  Post simple notifications repeatedly\n");
145                 testapp_print (" 3.  Post a notification on indicator\n");
146                 testapp_print (" 4.  Post status status message\n");
147                 testapp_print (" 5.  Delete all notification\n");
148                 testapp_print ("------------------------------------------\n");
149                 break;
150         default:
151                 break;
152         }
153 }
154 /* Common { ------------------------------------------------------------------*/
155
156 static int testapp_add_a_notification()
157 {
158         notification_h noti_handle = NULL;
159         int noti_err = NOTIFICATION_ERROR_NONE;
160
161         noti_handle = notification_create(NOTIFICATION_TYPE_NOTI);
162
163         if (noti_handle == NULL) {
164                 testapp_print("notification_create failed");
165                 goto FINISH_OFF;
166         }
167
168         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_TITLE, "I'm Title", "TITLE", NOTIFICATION_VARIABLE_TYPE_NONE);
169         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_CONTENT, "I'm Content", "CONTENT", NOTIFICATION_VARIABLE_TYPE_NONE);
170         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, "3", "3", NOTIFICATION_VARIABLE_TYPE_NONE);
171         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_INFO_1, "I'm Info 1", "INFO_1", NOTIFICATION_VARIABLE_TYPE_NONE);
172         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, "I'm Info Sub 1", "INFO_SUB_1", NOTIFICATION_VARIABLE_TYPE_NONE);
173         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_INFO_2, "I'm Info 2", "INFO_2", NOTIFICATION_VARIABLE_TYPE_NONE);
174         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_INFO_SUB_2, "I'm Info Sub 2", "INFO_SUB_2", NOTIFICATION_VARIABLE_TYPE_NONE);
175         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_INFO_3, "I'm Info 3", "INFO_3", NOTIFICATION_VARIABLE_TYPE_NONE);
176         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_INFO_SUB_3, "I'm Info Sub 3", "INFO_SUB_3", NOTIFICATION_VARIABLE_TYPE_NONE);
177
178         noti_err  = notification_post(noti_handle);
179
180         if (noti_err != NOTIFICATION_ERROR_NONE) {
181                 testapp_print("notification_post failed[%d]", noti_err);
182                 goto FINISH_OFF;
183         }
184
185 FINISH_OFF:
186         if (noti_handle)
187                 notification_free(noti_handle);
188
189         return noti_err;
190 }
191
192 /* Common } ------------------------------------------------------------------*/
193
194 /* Basic Test { --------------------------------------------------------------*/
195 static int testapp_test_post_notification()
196 {
197         int err = NOTIFICATION_ERROR_NONE;
198
199         if ((err = testapp_add_a_notification()) != NOTIFICATION_ERROR_NONE) {
200                 testapp_print("testapp_add_a_notification failed[%d]", err);
201                 goto FINISH_OFF;
202         }
203
204
205 FINISH_OFF:
206
207         return err;
208 }
209
210 static int testapp_test_post_notifications()
211 {
212         int err = NOTIFICATION_ERROR_NONE;
213         int repeat_count = 0;
214         int i = 0;
215
216         testapp_print("Input count : ");
217
218         if (0 >= scanf("%d", &repeat_count))
219                 testapp_print("Invalid input");
220
221         for (i = 0; i < repeat_count; i++) {
222                 if ((err = testapp_add_a_notification()) != NOTIFICATION_ERROR_NONE) {
223                         testapp_print("testapp_add_a_notification failed[%d]", err);
224                         goto FINISH_OFF;
225                 }
226         }
227
228
229 FINISH_OFF:
230         return err;
231 }
232
233 static int testapp_test_post_notification_on_indicator()
234 {
235         notification_h noti_handle = NULL;
236         int noti_err = NOTIFICATION_ERROR_NONE;
237
238         noti_handle = notification_create(NOTIFICATION_TYPE_NOTI);
239
240         if (noti_handle == NULL) {
241                 testapp_print("notification_create failed");
242                 goto FINISH_OFF;
243         }
244
245         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_TITLE, "I'm Title", "TITLE", NOTIFICATION_VARIABLE_TYPE_NONE);
246         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_CONTENT, "I'm Content", "CONTENT", NOTIFICATION_VARIABLE_TYPE_NONE);
247
248         noti_err  = notification_set_display_applist(noti_handle, NOTIFICATION_DISPLAY_APP_INDICATOR);
249
250         if(noti_err != NOTIFICATION_ERROR_NONE) {
251                 testapp_print("notification_set_display_applist failed[%d]", noti_err);
252                 goto FINISH_OFF;
253         }
254
255         noti_err  = notification_post(noti_handle);
256
257         if (noti_err != NOTIFICATION_ERROR_NONE) {
258                 testapp_print("notification_post failed[%d]", noti_err);
259                 goto FINISH_OFF;
260         }
261
262 FINISH_OFF:
263         if (noti_handle)
264                 notification_free(noti_handle);
265
266         return noti_err;
267 }
268
269 static int testapp_test_post_status_message()
270 {
271         int noti_err = NOTIFICATION_ERROR_NONE;
272
273
274         noti_err = notification_status_message_post("This is only a test");
275
276         if(noti_err != NOTIFICATION_ERROR_NONE) {
277                 testapp_print("notification_status_message_post failed[%d]", noti_err);
278         }
279
280         return noti_err;
281 }
282
283 static int testapp_test_delete_all_notifications()
284 {
285         int noti_err = NOTIFICATION_ERROR_NONE;
286
287         noti_err = notification_delete_all(NOTIFICATION_TYPE_NOTI);
288
289         testapp_print("notification_delete_all returns[%d]", noti_err);
290
291         return noti_err;
292 }
293
294 static gboolean testapp_interpret_command_basic_test (int selected_number)
295 {
296     gboolean go_to_loop = TRUE;
297
298     switch (selected_number) {
299         case 1:
300             testapp_test_post_notification();
301             break;
302
303         case 2:
304                         testapp_test_post_notifications();
305                         break;
306
307         case 3:
308                         testapp_test_post_notification_on_indicator();
309                         break;
310
311         case 4:
312                         testapp_test_post_status_message();
313                         break;
314
315         case 5:
316                 testapp_test_delete_all_notifications();
317                 break;
318
319         case 0:
320                         go_to_loop = FALSE;
321                         break;
322
323         default:
324                         break;
325     }
326
327     return go_to_loop;
328
329 }
330
331 void testapp_notification_main ()
332 {
333     gboolean go_to_loop = TRUE;
334     int menu_number = 0;
335
336     while (go_to_loop) {
337         testapp_show_menu (TESTAPP_MENU_TYPE_BASIC_TEST_MENU);
338         testapp_show_prompt (TESTAPP_MENU_TYPE_BASIC_TEST_MENU);
339
340         if (0 >= scanf("%d", &menu_number))
341             testapp_print("Invalid input");
342
343         go_to_loop = testapp_interpret_command_basic_test(menu_number);
344     }
345 }
346 /* Basic Test } ---------------------------------------------------------------*/
347
348
349 /* Main { ---------------------------------------------------------------------*/
350 static gboolean testapp_interpret_command (int menu_number)
351 {
352         gboolean go_to_loop = TRUE;
353
354         switch (menu_number) {
355         case 1:
356                 testapp_notification_main();
357                 break;
358
359         case 0:
360                 go_to_loop = FALSE;
361                 break;
362
363         default:
364                 break;
365         }
366
367         return go_to_loop;
368 }
369
370 void testapp_show_prompt (testapp_menu_type_e menu)
371 {
372     switch (menu) {
373         case TESTAPP_MENU_TYPE_MAIN_MENU:
374             testapp_print ("[MAIN]# ");
375             break;
376
377         case TESTAPP_MENU_TYPE_BASIC_TEST_MENU:
378             testapp_print ("[BASIC_TEST]# ");
379             break;
380     }
381 }
382
383 int main (int argc, char *argv[])
384 {
385         gboolean go_to_loop = TRUE;
386         int menu_number = 0;
387
388         if ( testapp_initialize_testing() == FALSE ) {
389                 testapp_print ("Initializing failed.\n");
390                 exit(0);
391         }
392
393         while (go_to_loop) {
394                 testapp_show_menu (TESTAPP_MENU_TYPE_MAIN_MENU);
395                 testapp_show_prompt (TESTAPP_MENU_TYPE_MAIN_MENU);
396                 if (0 >= scanf ("%d", &menu_number))
397                         testapp_print("Invalid input");
398                 go_to_loop = testapp_interpret_command (menu_number);
399         }
400
401         testapp_finalize_testing();
402
403         exit(0);
404 }
405 /* Main } ---------------------------------------------------------------------*/
406