0ebe37171adec5604b45e2f06233df4602e301c7
[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         noti_err = notification_post(noti_handle);
360
361         app_control_destroy(app_control);
362         app_control = NULL;
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         /*
377         if (noti_handle)
378                 notification_free(noti_handle);
379         noti_handle = NULL;
380         */
381
382         noti_handle = notification_load(NULL, priv_id);
383
384         if (noti_handle == NULL) {
385                 testapp_print("notification_load failed");
386                 goto FINISH_OFF;
387         }
388
389         noti_err = notification_get_event_handler(noti_handle, NOTIFICATION_EVENT_TYPE_CLICK_ON_BUTTON_1, &app_control);
390
391         if (noti_err != NOTIFICATION_ERROR_NONE ||app_control == NULL) {
392                 testapp_print("notification_get_event_handler failed[%d]", noti_err);
393                 goto FINISH_OFF;
394         }
395
396         app_control_get_app_id(app_control, &app_id);
397
398         if (app_id) {
399                 testapp_print("result app_id [%s]", app_id);
400                 free(app_id);
401         }
402
403 FINISH_OFF:
404
405         if (app_control)
406                 app_control_destroy(app_control);
407
408         if (noti_handle)
409                 notification_free(noti_handle);
410
411         return noti_err;
412 }
413
414 static gboolean testapp_interpret_command_basic_test (int selected_number)
415 {
416         gboolean go_to_loop = TRUE;
417
418         switch (selected_number) {
419         case 1:
420                 testapp_test_post_notification();
421                 break;
422
423         case 2:
424                 testapp_test_post_notifications();
425                 break;
426
427         case 3:
428                 testapp_test_post_notification_on_indicator();
429                 break;
430
431         case 4:
432                 testapp_test_post_status_message();
433                 break;
434
435         case 5:
436                 testapp_test_delete_all_notifications();
437                 break;
438
439         case 6:
440                 testapp_test_post_heads_up_notification_with_button();
441                 break;
442
443         case 0:
444                 go_to_loop = FALSE;
445                 break;
446
447         default:
448                 break;
449         }
450
451         return go_to_loop;
452
453 }
454
455 void testapp_notification_main ()
456 {
457         gboolean go_to_loop = TRUE;
458         int menu_number = 0;
459
460         while (go_to_loop) {
461                 testapp_show_menu (TESTAPP_MENU_TYPE_BASIC_TEST_MENU);
462                 testapp_show_prompt (TESTAPP_MENU_TYPE_BASIC_TEST_MENU);
463
464                 if (0 >= scanf("%d", &menu_number))
465                         testapp_print("Invalid input");
466
467                 go_to_loop = testapp_interpret_command_basic_test(menu_number);
468         }
469 }
470 /* Basic Test } ---------------------------------------------------------------*/
471
472 /* Setting Test } ---------------------------------------------------------------*/
473
474 static int testapp_test_get_setting_list()
475 {
476         int err = NOTIFICATION_ERROR_NONE;
477         int i = 0;
478         int count = 0;
479         char *package_name = NULL;
480         bool  allow_to_notify = false;
481         bool  do_not_disturb_except = false;
482         bool  visibility_class = false;
483         notification_setting_h setting_array = NULL;
484
485         notification_setting_get_setting_array(&setting_array, &count);
486
487         testapp_print("count [%d]\n", count);
488
489         for (i = 0; i < count; i++) {
490                 notification_setting_get_package_name(setting_array + i, &package_name);
491                 notification_setting_get_allow_to_notify(setting_array + i, &allow_to_notify);
492                 notification_setting_get_do_not_disturb_except(setting_array + i, &do_not_disturb_except);
493                 notification_setting_get_visibility_class(setting_array + i, &visibility_class);
494
495                 testapp_print("[%d] : package_name[%s], allow_to_notify[%d], do_not_disturb_except[%d], visibility_class[%d]\n"
496                                 ,i, package_name, allow_to_notify, do_not_disturb_except, visibility_class);
497                 free(package_name);
498                 notification_setting_free_notification(setting_array );
499         }
500
501         if (setting_array)
502                 free(setting_array);
503
504         return err;
505 }
506
507 static int testapp_test_update_setting()
508 {
509         int err = NOTIFICATION_ERROR_NONE;
510         notification_setting_h setting = NULL;
511
512         err = notification_setting_get_setting_by_package_name("org.tizen.internet", &setting);
513
514         if (err != NOTIFICATION_ERROR_NONE || setting == NULL) {
515                 testapp_print("notification_setting_get_setting_by_package_name failed [%d]", err);
516         }
517         else {
518                 notification_setting_set_allow_to_notify(setting, 0);
519                 notification_setting_update_setting(setting);
520         }
521
522         return err;
523 }
524
525 static int testapp_test_update_system_setting()
526 {
527         int err = NOTIFICATION_ERROR_NONE;
528         bool do_not_disturb;
529         int visibility_class;
530         notification_system_setting_h system_setting = NULL;
531
532
533         err = notification_system_setting_load_system_setting(&system_setting);
534
535         if (err != NOTIFICATION_ERROR_NONE || system_setting == NULL) {
536                 testapp_print("notification_system_setting_load_system_setting failed [%d]\n", err);
537                 goto out;
538         }
539
540         notification_system_setting_get_do_not_disturb(system_setting, &do_not_disturb);
541         testapp_print("do_not_disturb [%d]\n", do_not_disturb);
542         do_not_disturb = !do_not_disturb;
543         notification_system_setting_set_do_not_disturb(system_setting, do_not_disturb);
544
545         notification_system_setting_get_visibility_class(system_setting, &visibility_class);
546         testapp_print("visibility_class [%d]\n", visibility_class);
547         visibility_class = !visibility_class;
548         notification_system_setting_set_visibility_class(system_setting, visibility_class);
549
550         err = notification_system_setting_update_system_setting(system_setting);
551
552         if (err != NOTIFICATION_ERROR_NONE || system_setting == NULL) {
553                 testapp_print("notification_system_setting_update_system_setting failed [%d]\n", err);
554                 goto out;
555         }
556
557 out:
558         if (system_setting)
559                 notification_system_setting_free_system_setting(system_setting);
560
561         return err;
562 }
563
564 static gboolean testapp_interpret_command_setting_test (int selected_number)
565 {
566         gboolean go_to_loop = TRUE;
567
568         switch (selected_number) {
569         case 1:
570                 testapp_test_get_setting_list();
571                 break;
572
573         case 2:
574                 testapp_test_update_setting();
575                 break;
576
577         case 3:
578                 testapp_test_update_system_setting();
579                 break;
580
581         case 0:
582                 go_to_loop = FALSE;
583                 break;
584
585         default:
586                 break;
587         }
588
589         return go_to_loop;
590
591 }
592
593 void testapp_setting_main()
594 {
595         gboolean go_to_loop = TRUE;
596         int menu_number = 0;
597
598         while (go_to_loop) {
599                 testapp_show_menu (TESTAPP_MENU_TYPE_SETTING_TEST_MENU);
600                 testapp_show_prompt (TESTAPP_MENU_TYPE_SETTING_TEST_MENU);
601
602                 if (0 >= scanf("%d", &menu_number))
603                         testapp_print("Invalid input");
604
605                 go_to_loop = testapp_interpret_command_setting_test(menu_number);
606         }
607 }
608 /* Setting Test } ---------------------------------------------------------------*/
609
610 /* Main { ---------------------------------------------------------------------*/
611 static gboolean testapp_interpret_command (int menu_number)
612 {
613         gboolean go_to_loop = TRUE;
614
615         switch (menu_number) {
616         case 1:
617                 testapp_notification_main();
618                 break;
619
620         case 2:
621                 testapp_setting_main();
622                 break;
623
624         case 0:
625                 go_to_loop = FALSE;
626                 break;
627
628         default:
629                 break;
630         }
631
632         return go_to_loop;
633 }
634
635 void testapp_show_prompt (testapp_menu_type_e menu)
636 {
637         switch (menu) {
638         case TESTAPP_MENU_TYPE_MAIN_MENU:
639                 testapp_print ("[MAIN]# ");
640                 break;
641
642         case TESTAPP_MENU_TYPE_BASIC_TEST_MENU:
643                 testapp_print ("[BASIC_TEST]# ");
644                 break;
645
646         case TESTAPP_MENU_TYPE_SETTING_TEST_MENU:
647                 testapp_print ("[SETTING_TEST]# ");
648                 break;
649         }
650 }
651
652 int main (int argc, char *argv[])
653 {
654         gboolean go_to_loop = TRUE;
655         int menu_number = 0;
656
657         if ( testapp_initialize_testing() == FALSE ) {
658                 testapp_print ("Initializing failed.\n");
659                 exit(0);
660         }
661
662         while (go_to_loop) {
663                 testapp_show_menu (TESTAPP_MENU_TYPE_MAIN_MENU);
664                 testapp_show_prompt (TESTAPP_MENU_TYPE_MAIN_MENU);
665                 if (0 >= scanf ("%d", &menu_number))
666                         testapp_print("Invalid input");
667                 go_to_loop = testapp_interpret_command (menu_number);
668         }
669
670         testapp_finalize_testing();
671
672         exit(0);
673 }
674 /* Main } ---------------------------------------------------------------------*/
675