Move internal API set to internal header file.
[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 #include <notification_setting.h>
38 #include <notification_setting_internal.h>
39
40 /*-----------------------------------------------------------------------------------------*/
41 /* types */
42 typedef enum testapp_menu_type {
43         TESTAPP_MENU_TYPE_MAIN_MENU = 1,
44         TESTAPP_MENU_TYPE_BASIC_TEST_MENU = 2,
45         TESTAPP_MENU_TYPE_SETTING_TEST_MENU = 3
46 } testapp_menu_type_e;
47
48 /*-----------------------------------------------------------------------------------------*/
49 /* function prototypes */
50 static void testapp_system_signal_handler (int signal_number);
51 void testapp_show_prompt (testapp_menu_type_e menu);
52
53 /*-----------------------------------------------------------------------------------------*/
54 /* implementation */
55 void testapp_print (char *fmt, ...)
56 {
57         va_list args = {0};
58         va_start(args, fmt);
59         vfprintf (stdout, fmt, args);
60         va_end (args);
61         fflush (stdout);
62 }
63
64 static gboolean testapp_initialize_testing ()
65 {
66         struct timeval tv_1, tv_2;
67         int interval;
68
69         /* register signal handler */
70         if ( signal (SIGINT, testapp_system_signal_handler) == SIG_ERR ) {
71                 testapp_print ("register signal handler fail\n");
72                 return FALSE;
73         }
74
75         if ( signal (SIGQUIT, testapp_system_signal_handler) == SIG_ERR ) {
76                 testapp_print ("register signal handler fail\n");
77                 return FALSE;
78         }
79
80         if ( signal (SIGTSTP, testapp_system_signal_handler) == SIG_ERR ) {
81                 testapp_print ("register signal handler fail\n");
82                 return FALSE;
83         }
84
85         if ( signal (SIGTERM, testapp_system_signal_handler) == SIG_ERR ) {
86                 testapp_print ("register signal handler fail\n");
87                 return FALSE;
88         }
89
90
91         gettimeofday(&tv_1, NULL);
92
93         /* TODO : initializing notification */
94
95         gettimeofday(&tv_2, NULL);
96         interval = tv_2.tv_usec - tv_1.tv_usec;
97         testapp_print("\t Initializing Proceed time %d us\n",interval);
98
99
100         return TRUE;
101 }
102
103 static gboolean testapp_finalize_testing ()
104 {
105         /* TODO : finalizing notification */
106
107         return TRUE;
108 }
109
110 static void testapp_system_signal_handler (int signal_number)
111 {
112         testapp_print ("signal:%d\n", signal_number);
113         switch (signal_number) {
114         case SIGQUIT:
115         case SIGINT:
116         case SIGTSTP:
117         case SIGTERM:
118                 testapp_finalize_testing();
119                 break;
120
121         default:
122                 testapp_print ("unhandled signal:%d\n", signal_number);
123                 break;
124         }
125         exit(0);
126 }
127
128
129
130
131 void testapp_show_menu (testapp_menu_type_e menu)
132 {
133         switch (menu) {
134         case TESTAPP_MENU_TYPE_MAIN_MENU:
135                 testapp_print ("==========================================\n");
136                 testapp_print ("    Notification test application \n");
137                 testapp_print ("==========================================\n");
138                 testapp_print ("1. Basic Test\n");
139                 testapp_print ("2. Setting Test\n");
140                 testapp_print ("0. Exit \n");
141                 testapp_print ("------------------------------------------\n");
142                 break;
143         case TESTAPP_MENU_TYPE_BASIC_TEST_MENU:
144                 testapp_print ("==========================================\n");
145                 testapp_print ("    Basic test menu \n");
146                 testapp_print ("==========================================\n");
147                 testapp_print (" 1.  Post a simple notification\n");
148                 testapp_print (" 2.  Post simple notifications repeatedly\n");
149                 testapp_print (" 3.  Post a notification on indicator\n");
150                 testapp_print (" 4.  Post status status message\n");
151                 testapp_print (" 5.  Delete all notification\n");
152                 testapp_print (" 6.  Post a heads notification with a button\n");
153                 testapp_print ("------------------------------------------\n");
154                 break;
155         case TESTAPP_MENU_TYPE_SETTING_TEST_MENU:
156                 testapp_print ("==========================================\n");
157                 testapp_print ("    Setting test menu \n");
158                 testapp_print ("==========================================\n");
159                 testapp_print (" 1.  Get setting list\n");
160                 testapp_print (" 2.  Update setting\n");
161                 testapp_print (" 3.  Update system setting\n");
162                 testapp_print ("------------------------------------------\n");
163                 break;
164         default:
165                 break;
166         }
167 }
168 /* Common { ------------------------------------------------------------------*/
169
170 static int testapp_add_a_notification()
171 {
172         notification_h noti_handle = NULL;
173         int noti_err = NOTIFICATION_ERROR_NONE;
174
175         noti_handle = notification_create(NOTIFICATION_TYPE_NOTI);
176
177         if (noti_handle == NULL) {
178                 testapp_print("notification_create failed");
179                 goto FINISH_OFF;
180         }
181
182         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_TITLE, "I'm Title", "TITLE", NOTIFICATION_VARIABLE_TYPE_NONE);
183         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_CONTENT, "I'm Content", "CONTENT", NOTIFICATION_VARIABLE_TYPE_NONE);
184         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, "3", "3", NOTIFICATION_VARIABLE_TYPE_NONE);
185         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_INFO_1, "I'm Info 1", "INFO_1", NOTIFICATION_VARIABLE_TYPE_NONE);
186         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);
187         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_INFO_2, "I'm Info 2", "INFO_2", NOTIFICATION_VARIABLE_TYPE_NONE);
188         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);
189         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_INFO_3, "I'm Info 3", "INFO_3", NOTIFICATION_VARIABLE_TYPE_NONE);
190         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);
191
192         noti_err  = notification_post(noti_handle);
193
194         if (noti_err != NOTIFICATION_ERROR_NONE) {
195                 testapp_print("notification_post failed[%d]", noti_err);
196                 goto FINISH_OFF;
197         }
198
199         FINISH_OFF:
200         if (noti_handle)
201                 notification_free(noti_handle);
202
203         return noti_err;
204 }
205
206 /* Common } ------------------------------------------------------------------*/
207
208 /* Basic Test { --------------------------------------------------------------*/
209 static int testapp_test_post_notification()
210 {
211         int err = NOTIFICATION_ERROR_NONE;
212
213         if ((err = testapp_add_a_notification()) != NOTIFICATION_ERROR_NONE) {
214                 testapp_print("testapp_add_a_notification failed[%d]", err);
215                 goto FINISH_OFF;
216         }
217
218
219         FINISH_OFF:
220
221         return err;
222 }
223
224 static int testapp_test_post_notifications()
225 {
226         int err = NOTIFICATION_ERROR_NONE;
227         int repeat_count = 0;
228         int i = 0;
229
230         testapp_print("Input count : ");
231
232         if (0 >= scanf("%d", &repeat_count))
233                 testapp_print("Invalid input");
234
235         for (i = 0; i < repeat_count; i++) {
236                 if ((err = testapp_add_a_notification()) != NOTIFICATION_ERROR_NONE) {
237                         testapp_print("testapp_add_a_notification failed[%d]", err);
238                         goto FINISH_OFF;
239                 }
240         }
241
242         FINISH_OFF:
243         return err;
244 }
245
246 static int testapp_test_post_notification_on_indicator()
247 {
248         notification_h noti_handle = NULL;
249         int noti_err = NOTIFICATION_ERROR_NONE;
250
251         noti_handle = notification_create(NOTIFICATION_TYPE_NOTI);
252
253         if (noti_handle == NULL) {
254                 testapp_print("notification_create failed");
255                 goto FINISH_OFF;
256         }
257
258         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_TITLE, "I'm Title", "TITLE", NOTIFICATION_VARIABLE_TYPE_NONE);
259         noti_err  = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_CONTENT, "I'm Content", "CONTENT", NOTIFICATION_VARIABLE_TYPE_NONE);
260
261         noti_err  = notification_set_display_applist(noti_handle, NOTIFICATION_DISPLAY_APP_INDICATOR);
262
263         if(noti_err != NOTIFICATION_ERROR_NONE) {
264                 testapp_print("notification_set_display_applist failed[%d]", noti_err);
265                 goto FINISH_OFF;
266         }
267
268         noti_err  = notification_post(noti_handle);
269
270         if (noti_err != NOTIFICATION_ERROR_NONE) {
271                 testapp_print("notification_post failed[%d]", noti_err);
272                 goto FINISH_OFF;
273         }
274
275         FINISH_OFF:
276         if (noti_handle)
277                 notification_free(noti_handle);
278
279         return noti_err;
280 }
281
282 static int testapp_test_post_status_message()
283 {
284         int noti_err = NOTIFICATION_ERROR_NONE;
285
286
287         noti_err = notification_status_message_post("This is only a test");
288
289         if(noti_err != NOTIFICATION_ERROR_NONE) {
290                 testapp_print("notification_status_message_post failed[%d]", noti_err);
291         }
292
293         return noti_err;
294 }
295
296 static int testapp_test_delete_all_notifications()
297 {
298         int noti_err = NOTIFICATION_ERROR_NONE;
299
300         noti_err = notification_delete_all(NOTIFICATION_TYPE_NOTI);
301
302         testapp_print("notification_delete_all returns[%d]", noti_err);
303
304         return noti_err;
305 }
306
307 static int testapp_test_post_heads_up_notification_with_button()
308 {
309         notification_h noti_handle = NULL;
310         int noti_err = NOTIFICATION_ERROR_NONE;
311         int app_control_err = APP_CONTROL_ERROR_NONE;
312         int priv_id = 0;
313         int group_id = 0;
314         char *app_id = NULL;
315         app_control_h app_control = NULL;
316         time_t result = time(NULL);
317         char tag[100] = { 0, };
318
319         noti_handle = notification_create(NOTIFICATION_TYPE_NOTI);
320
321         if (noti_handle == NULL) {
322                 testapp_print("notification_create failed");
323                 goto FINISH_OFF;
324         }
325
326         noti_err = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_TITLE, "I'm Title", "TITLE", NOTIFICATION_VARIABLE_TYPE_NONE);
327         noti_err = notification_set_text(noti_handle, NOTIFICATION_TEXT_TYPE_CONTENT, "I'm Content", "CONTENT", NOTIFICATION_VARIABLE_TYPE_NONE);
328
329         noti_err = notification_set_display_applist(noti_handle, NOTIFICATION_DISPLAY_APP_HEADS_UP | NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
330
331         snprintf(tag, 100, "%d", result);
332
333         noti_err = notification_set_tag(noti_handle, tag);
334
335         if (noti_err != NOTIFICATION_ERROR_NONE) {
336                 testapp_print("notification_set_display_applist failed[%d]\n", noti_err);
337                 goto FINISH_OFF;
338         }
339
340         app_control_err = app_control_create(&app_control);
341         if (app_control_err != APP_CONTROL_ERROR_NONE ) {
342                 testapp_print("app_control_create failed[%d]\n", app_control_err);
343                 goto FINISH_OFF;
344         }
345
346         app_control_err = app_control_set_app_id(app_control, "org.tizen.quickpanel");
347         if (app_control_err != APP_CONTROL_ERROR_NONE ) {
348                 testapp_print("app_control_set_app_id failed[%d]\n", app_control_err);
349                 goto FINISH_OFF;
350         }
351
352         noti_err = notification_set_event_handler(noti_handle, NOTIFICATION_EVENT_TYPE_CLICK_ON_BUTTON_1, app_control);
353
354         if (noti_err != NOTIFICATION_ERROR_NONE) {
355                 testapp_print("notification_set_event_handler failed[%d]", noti_err);
356                 goto FINISH_OFF;
357         }
358
359         app_control_destroy(app_control);
360         app_control = NULL;
361
362         noti_err = notification_post(noti_handle);
363
364         if (noti_err != NOTIFICATION_ERROR_NONE) {
365                 testapp_print("notification_post failed[%d]", noti_err);
366                 goto FINISH_OFF;
367         }
368
369         noti_err = notification_get_id(noti_handle, &group_id, &priv_id);
370
371         if (noti_err != NOTIFICATION_ERROR_NONE) {
372                 testapp_print("notification_get_id failed[%d]", noti_err);
373                 goto FINISH_OFF;
374         }
375
376         if (noti_handle)
377                 notification_free(noti_handle);
378         noti_handle = NULL;
379
380         noti_handle = notification_load(NULL, priv_id);
381
382         if (noti_handle == NULL) {
383                 testapp_print("notification_load failed");
384                 goto FINISH_OFF;
385         }
386
387         noti_err = notification_get_event_handler(noti_handle, NOTIFICATION_EVENT_TYPE_CLICK_ON_BUTTON_1, &app_control);
388
389         if (noti_err != NOTIFICATION_ERROR_NONE ||app_control == NULL) {
390                 testapp_print("notification_get_event_handler failed[%d]", noti_err);
391                 goto FINISH_OFF;
392         }
393
394         app_control_get_app_id(app_control, &app_id);
395
396         if (app_id) {
397                 testapp_print("result app_id [%s]\n", app_id);
398                 free(app_id);
399         }
400
401 FINISH_OFF:
402
403         if (app_control)
404                 app_control_destroy(app_control);
405
406         if (noti_handle)
407                 notification_free(noti_handle);
408
409         return noti_err;
410 }
411
412 static gboolean testapp_interpret_command_basic_test (int selected_number)
413 {
414         gboolean go_to_loop = TRUE;
415
416         switch (selected_number) {
417         case 1:
418                 testapp_test_post_notification();
419                 break;
420
421         case 2:
422                 testapp_test_post_notifications();
423                 break;
424
425         case 3:
426                 testapp_test_post_notification_on_indicator();
427                 break;
428
429         case 4:
430                 testapp_test_post_status_message();
431                 break;
432
433         case 5:
434                 testapp_test_delete_all_notifications();
435                 break;
436
437         case 6:
438                 testapp_test_post_heads_up_notification_with_button();
439                 break;
440
441         case 0:
442                 go_to_loop = FALSE;
443                 break;
444
445         default:
446                 break;
447         }
448
449         return go_to_loop;
450
451 }
452
453 void testapp_notification_main ()
454 {
455         gboolean go_to_loop = TRUE;
456         int menu_number = 0;
457
458         while (go_to_loop) {
459                 testapp_show_menu (TESTAPP_MENU_TYPE_BASIC_TEST_MENU);
460                 testapp_show_prompt (TESTAPP_MENU_TYPE_BASIC_TEST_MENU);
461
462                 if (0 >= scanf("%d", &menu_number))
463                         testapp_print("Invalid input");
464
465                 go_to_loop = testapp_interpret_command_basic_test(menu_number);
466         }
467 }
468 /* Basic Test } ---------------------------------------------------------------*/
469
470 /* Setting Test } ---------------------------------------------------------------*/
471
472 static int testapp_test_get_setting_list()
473 {
474         int err = NOTIFICATION_ERROR_NONE;
475         int i = 0;
476         int count = 0;
477         char *package_name = NULL;
478         bool  allow_to_notify = false;
479         bool  do_not_disturb_except = false;
480         bool  visibility_class = false;
481         notification_setting_h setting_array = NULL;
482
483         notification_setting_get_setting_array(&setting_array, &count);
484
485         testapp_print("count [%d]\n", count);
486
487         for (i = 0; i < count; i++) {
488                 notification_setting_get_package_name(setting_array + i, &package_name);
489                 notification_setting_get_allow_to_notify(setting_array + i, &allow_to_notify);
490                 notification_setting_get_do_not_disturb_except(setting_array + i, &do_not_disturb_except);
491                 notification_setting_get_visibility_class(setting_array + i, &visibility_class);
492
493                 testapp_print("[%d] : package_name[%s], allow_to_notify[%d], do_not_disturb_except[%d], visibility_class[%d]\n"
494                                 ,i, package_name, allow_to_notify, do_not_disturb_except, visibility_class);
495                 free(package_name);
496                 notification_setting_free_notification(setting_array );
497         }
498
499         if (setting_array)
500                 free(setting_array);
501
502         return err;
503 }
504
505 static int testapp_test_update_setting()
506 {
507         int err = NOTIFICATION_ERROR_NONE;
508         notification_setting_h setting = NULL;
509
510         err = notification_setting_get_setting_by_package_name("org.tizen.internet", &setting);
511
512         if (err != NOTIFICATION_ERROR_NONE || setting == NULL) {
513                 testapp_print("notification_setting_get_setting_by_package_name failed [%d]", err);
514         }
515         else {
516                 notification_setting_set_allow_to_notify(setting, 0);
517                 notification_setting_update_setting(setting);
518         }
519
520         return err;
521 }
522
523 static int testapp_test_update_system_setting()
524 {
525         int err = NOTIFICATION_ERROR_NONE;
526         bool do_not_disturb;
527         int visibility_class;
528         notification_system_setting_h system_setting = NULL;
529
530
531         err = notification_system_setting_load_system_setting(&system_setting);
532
533         if (err != NOTIFICATION_ERROR_NONE || system_setting == NULL) {
534                 testapp_print("notification_system_setting_load_system_setting failed [%d]\n", err);
535                 goto out;
536         }
537
538         notification_system_setting_get_do_not_disturb(system_setting, &do_not_disturb);
539         testapp_print("do_not_disturb [%d]\n", do_not_disturb);
540         do_not_disturb = !do_not_disturb;
541         notification_system_setting_set_do_not_disturb(system_setting, do_not_disturb);
542
543         notification_system_setting_get_visibility_class(system_setting, &visibility_class);
544         testapp_print("visibility_class [%d]\n", visibility_class);
545         visibility_class = !visibility_class;
546         notification_system_setting_set_visibility_class(system_setting, visibility_class);
547
548         err = notification_system_setting_update_system_setting(system_setting);
549
550         if (err != NOTIFICATION_ERROR_NONE || system_setting == NULL) {
551                 testapp_print("notification_system_setting_update_system_setting failed [%d]\n", err);
552                 goto out;
553         }
554
555 out:
556         if (system_setting)
557                 notification_system_setting_free_system_setting(system_setting);
558
559         return err;
560 }
561
562 static gboolean testapp_interpret_command_setting_test (int selected_number)
563 {
564         gboolean go_to_loop = TRUE;
565
566         switch (selected_number) {
567         case 1:
568                 testapp_test_get_setting_list();
569                 break;
570
571         case 2:
572                 testapp_test_update_setting();
573                 break;
574
575         case 3:
576                 testapp_test_update_system_setting();
577                 break;
578
579         case 0:
580                 go_to_loop = FALSE;
581                 break;
582
583         default:
584                 break;
585         }
586
587         return go_to_loop;
588
589 }
590
591 void testapp_setting_main()
592 {
593         gboolean go_to_loop = TRUE;
594         int menu_number = 0;
595
596         while (go_to_loop) {
597                 testapp_show_menu (TESTAPP_MENU_TYPE_SETTING_TEST_MENU);
598                 testapp_show_prompt (TESTAPP_MENU_TYPE_SETTING_TEST_MENU);
599
600                 if (0 >= scanf("%d", &menu_number))
601                         testapp_print("Invalid input");
602
603                 go_to_loop = testapp_interpret_command_setting_test(menu_number);
604         }
605 }
606 /* Setting Test } ---------------------------------------------------------------*/
607
608 /* Main { ---------------------------------------------------------------------*/
609 static gboolean testapp_interpret_command (int menu_number)
610 {
611         gboolean go_to_loop = TRUE;
612
613         switch (menu_number) {
614         case 1:
615                 testapp_notification_main();
616                 break;
617
618         case 2:
619                 testapp_setting_main();
620                 break;
621
622         case 0:
623                 go_to_loop = FALSE;
624                 break;
625
626         default:
627                 break;
628         }
629
630         return go_to_loop;
631 }
632
633 void testapp_show_prompt (testapp_menu_type_e menu)
634 {
635         switch (menu) {
636         case TESTAPP_MENU_TYPE_MAIN_MENU:
637                 testapp_print ("[MAIN]# ");
638                 break;
639
640         case TESTAPP_MENU_TYPE_BASIC_TEST_MENU:
641                 testapp_print ("[BASIC_TEST]# ");
642                 break;
643
644         case TESTAPP_MENU_TYPE_SETTING_TEST_MENU:
645                 testapp_print ("[SETTING_TEST]# ");
646                 break;
647         }
648 }
649
650 int main (int argc, char *argv[])
651 {
652         gboolean go_to_loop = TRUE;
653         int menu_number = 0;
654
655         if ( testapp_initialize_testing() == FALSE ) {
656                 testapp_print ("Initializing failed.\n");
657                 exit(0);
658         }
659
660         while (go_to_loop) {
661                 testapp_show_menu (TESTAPP_MENU_TYPE_MAIN_MENU);
662                 testapp_show_prompt (TESTAPP_MENU_TYPE_MAIN_MENU);
663                 if (0 >= scanf ("%d", &menu_number))
664                         testapp_print("Invalid input");
665                 go_to_loop = testapp_interpret_command (menu_number);
666         }
667
668         testapp_finalize_testing();
669
670         exit(0);
671 }
672 /* Main } ---------------------------------------------------------------------*/
673