merged with private
[framework/appfw/alarm-manager.git] / test / alarmmgr_unit_ts.c
1 /*
2  *  alarm-manager
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Venkatesha Sarpangala <sarpangala.v@samsung.com>, Jayoun Lee <airjany@samsung.com>,
7  * Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <assert.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <sys/types.h>
30
31 #include <glib.h>
32 #include <glib-object.h>
33 #include <dlog/dlog.h>
34
35 #include <alarm.h>
36 #include <bundle.h>
37 #include <appsvc.h>
38 #include <dlog.h>
39
40 #ifdef LOG_TAG
41 #undef LOG_TAG
42 #endif
43 #define LOG_TAG "TEST_LOG"
44
45 #define TEST_APP_LOG_PRINT(FMT, ARG...) SLOGD(FMT, ##ARG);
46
47 #define AUTO_TEST 0             /*Flag to Enable or Disable auto testing */
48
49 #if AUTO_TEST
50
51 #define ITER_COUNT 200          /*Stress testing iteration count */
52
53 typedef enum {
54         INTERVAL = 1,
55         INTERVAL_APPSVC,
56         CALENDAR,
57         CALENDAR_APPSVC
58 } create_subtype;               /*alarm type */
59
60 int sub_type = 0;
61
62 /*Used to hold multiple alarm id created */
63 alarm_id_t alarm_id_arr[ITER_COUNT] = { 0, };
64
65 #endif
66
67 #define USER_INPUT_YES 1
68 #define USER_INPUT_NO 2
69
70 #define USER_INPUT_DEFAULT 1
71 #define USER_INPUT_VOLATILE 2
72
73 GMainLoop *mainloop = NULL;     /*gmain loop */
74
75 alarm_id_t alarm_id;            /*Alarm identificaiton no */
76
77 guint pgm_exit_time = 2000000;  /*gmainloop life in milli second for few scenarios */
78
79 bool cleanup_req = false;       /*tells whether test program cleanup required
80                                    or not before program exit */
81
82 bool multiple_alarm = false;
83
84 /* Local Functions*/
85 static int __alarm_callback(alarm_id_t alarm_id, void *user_param);
86
87 static int __alarm_callback1(alarm_id_t alarm_id, void *user_param);
88
89 static void __create_interval_alarm(void);
90
91 static void __create_app_svc_interval_alarm(void);
92
93 static void __create_calendar_alarm(void);
94
95 static void __create_app_svc_calendar_alarm(void);
96
97 static gboolean __timout_handler(gpointer user_data);
98
99 static void __test_pgm_clean_up(void);
100
101 static int __get_integer_input_data(void);
102
103 static void __test_alarm_manager_delete_alarm(void);
104
105 static void __test_alarm_manager_create_alarm(void);
106
107 static void __test_alarm_manager_get_information(void);
108
109 static void __test_alarm_manager_get_appsvc_information(void);
110
111 static void __test_alarm_manager_get_alarm_ids(void);
112
113 static void __print_api_return_result(int err_no, const char *api_name);
114
115 static void __test_alarm_manager_callback(void);
116
117 static int __get_alarm_persistance_type(void);
118
119 static char *__get_alarm_destination(void);
120
121 static int __get_alarm_repeat_mode(void);
122
123 static void __print_appsvc_bundle_data(const char *key, const char *val, void *data);
124
125
126 /*This function will be executed upon alarm expiry*/
127 static int __alarm_callback(alarm_id_t alarm_id_rec, void *user_param)
128 {
129         time_t current_time = { 0, };
130         time(&current_time);
131
132         TEST_APP_LOG_PRINT("Alarm[%d] has expired at %s\n", alarm_id_rec,
133                            ctime(&current_time));
134         printf("Alarm[%d] has expired at %s\n", alarm_id_rec,
135                ctime(&current_time));
136
137         return 0;
138
139 }
140
141 /*This function will be executed upon alarm expiry*/
142 static int __alarm_callback1(alarm_id_t alarm_id_rec, void *user_param)
143 {
144         time_t current_time = { 0, };
145         time(&current_time);
146         printf("Callback function 2 executed\n");
147
148         TEST_APP_LOG_PRINT("Alarm[%d] has expired at %s\n", alarm_id_rec,
149                            ctime(&current_time));
150         printf("Alarm[%d] has expired at %s\n", alarm_id_rec,
151                ctime(&current_time));
152
153         return 0;
154
155 }
156
157 /*This function prints the result of Alarm manager API call*/
158 static void __print_api_return_result(int err_no, const char *api_name)
159 {
160
161         printf("Alarm Manager API \"%s\" retuned with result: ", api_name);
162
163         switch (err_no) {
164         case ERR_ALARM_INVALID_PARAM:{
165                         printf
166                             ("Alarm Manager error: \"ERR_ALARM_INVALID_PARAM\"\n");
167                         break;
168                 }
169         case ERR_ALARM_INVALID_ID:{
170                         printf
171                             ("Alarm Manager error: \"ERR_ALARM_INVALID_ID\"\n");
172                         break;
173                 }
174         case ERR_ALARM_INVALID_REPEAT:{
175                         printf
176                             ("Alarm Manager error: \"ERR_ALARM_INVALID_REPEAT\"\n");
177                         break;
178                 }
179         case ERR_ALARM_INVALID_TIME:{
180                         printf
181                             ("Alarm Manager error: \"ERR_ALARM_INVALID_TIME\"\n");
182                         break;
183                 }
184         case ERR_ALARM_INVALID_DATE:{
185                         printf
186                             ("Alarm Manager error: \"ERR_ALARM_INVALID_DATE\"\n");
187                         break;
188                 }
189         case ERR_ALARM_NO_SERVICE_NAME:{
190                         printf
191                             ("Alarm Manager error: \"ERR_ALARM_NO_SERVICE_NAME\"\n");
192                         break;
193                 }
194         case ERR_ALARM_NO_PERMISSION:{
195                         printf
196                             ("Alarm Manager error: \"ERR_ALARM_NO_PERMISSION\"\n");
197                         break;
198                 }
199         case ERR_ALARM_INVALID_TYPE:{
200                         printf
201                             ("Alarm Manager error: \"ERR_ALARM_INVALID_TYPE\"\n");
202                         break;
203                 }
204         case ERR_ALARM_SYSTEM_FAIL:{
205                         printf
206                             ("Alarm Manager error: \"ERR_ALARM_SYSTEM_FAIL\"\n");
207                         break;
208                 }
209         case ALARMMGR_RESULT_SUCCESS:{
210                         printf
211                             ("Alarm API Success: \"ALARMMGR_RESULT_SUCCESS\"\n");
212                         break;
213                 }
214         default:{
215                         printf("Alarm Manager error: Unknow Error \n");
216                 }
217         }
218 #if AUTO_TEST
219         /*Depending upon the test scenario,
220            these errno are added to assert()! */
221         assert(err_no == ALARMMGR_RESULT_SUCCESS
222                || err_no == ERR_ALARM_INVALID_ID
223                || err_no == ERR_ALARM_INVALID_PARAM);
224 #else
225         assert(err_no == ALARMMGR_RESULT_SUCCESS);
226 #endif
227
228         return;
229 }
230
231
232 static void __print_appsvc_bundle_data(const char *key, const char *val, void *data){
233         printf("Key-->[%s], Val-->[%s]\n",key,val);
234         return;
235 }
236
237 /*This function is to take command line integer input from the user*/
238 static int __get_integer_input_data(void)
239 {
240         char input_str[32] = { 0, };
241         int data = 0;
242         fflush(stdin);
243
244         if (fgets(input_str, 1024, stdin) == NULL) {
245                 printf("Input buffer overflow....\n");
246                 return -1;
247         }
248
249         if (sscanf(input_str, "%d", &data) != 1) {
250                 printf("Input only integer option....\n");
251                 return -1;
252         }
253
254         return data;
255 }
256
257 /*Function to get alarm persistance type*/
258 static int __get_alarm_persistance_type(void)
259 {
260         int type = ALARM_TYPE_VOLATILE;
261         int alarm_type = 0;
262         printf("Enter Type\n");
263         printf("1. ALARM_TYPE_DEFAULT \n");
264         printf("2. ALARM_TYPE_VOLATILE\n");
265         printf("Enter Choice between [1-2]....\n");
266         alarm_type = __get_integer_input_data();
267
268         switch (alarm_type) {
269         case USER_INPUT_DEFAULT:
270                 {
271                         type = ALARM_TYPE_DEFAULT;
272                         break;
273                 }
274         case USER_INPUT_VOLATILE:
275                 {
276                         type = ALARM_TYPE_VOLATILE;
277                         break;
278                 }
279         default:
280                 {
281                         printf("Invalid option....Try Again\n");
282                         exit(-1);
283                 }
284
285         }
286         return type;
287 }
288
289 static char *__get_alarm_destination(void)
290 {
291         int alarm_type = 0;
292         char *destination = NULL;
293         printf("[Optional]: Do you want to provide Destination?\n");
294         printf("1. YES\n");
295         printf("2. NO \n");
296         printf("Enter Choice between [1-2]....\n");
297         alarm_type = __get_integer_input_data();
298         switch (alarm_type) {
299         case USER_INPUT_YES:
300                 {
301                         fflush(stdin);
302
303                         destination = (char *)malloc(1024);
304                         if (NULL == destination) {
305                                 return NULL;
306                         }
307                         printf
308                             ("Enter destination: [Ex:org.tizen.alarm-test "
309                              "or org.tizen.gallery...]\n");
310                         if (fgets(destination, 1024, stdin) == NULL) {
311                                 printf("Input buffer overflow....,try again\n");
312                                 exit(-1);
313                         }
314
315                         destination[strlen(destination) - 1] = '\0';
316
317                         break;
318                 }
319         case USER_INPUT_NO:
320                 {
321                         break;
322                 }
323         default:
324                 {
325                         printf("Invalid option....Try Again\n");
326                         exit(-1);
327                 }
328         }
329
330         return destination;
331 }
332
333 static int __get_alarm_repeat_mode(void)
334 {
335         int repeat_mode = 0;
336         printf("Enter Repeat Mode:\n");
337         printf("0. ALARM_REPEAT_MODE_ONCE\n");
338         printf("1. ALARM_REPEAT_MODE_REPEAT\n");
339         printf("2. ALARM_REPEAT_MODE_WEEKLY\n");
340         printf("3. ALARM_REPEAT_MODE_MONTHLY\n");
341         printf("4. ALARM_REPEAT_MODE_ANNUALLY\n");
342         printf("5. ALARM_REPEAT_MODE_MAX\n");
343         printf("Enter Choice between [0-5]....\n");
344         repeat_mode = __get_integer_input_data();
345         if (repeat_mode > 5 || repeat_mode < 0) {
346                 printf("Invalid option....Try Again\n");
347                 exit(-1);
348         }
349         return repeat_mode;
350 }
351
352 /*Function to create alarm in the calender input format*/
353 static void __create_calendar_alarm(void)
354 {
355         char *destination = NULL;
356         time_t current_time;
357         struct tm current_tm;
358         alarm_entry_t *alarm_info = NULL;
359         int result = 0;
360         alarm_date_t test_time;
361         int type = 0;
362         int year, month, day, hour, min, sec;
363         int repeat_mode = 0;
364
365         year = month = day = hour = min = sec = 0;
366
367         time(&current_time);
368         printf("current time: %s\n", ctime(&current_time));
369
370         localtime_r(&current_time, &current_tm);
371         alarm_info = alarmmgr_create_alarm();
372         assert(alarm_info != NULL);
373
374 #if AUTO_TEST
375         test_time.year = current_tm.tm_year;
376         test_time.month = current_tm.tm_mon;
377         test_time.day = current_tm.tm_mday;
378
379         test_time.hour = current_tm.tm_hour;
380         test_time.min = current_tm.tm_min + 1;
381         test_time.sec = 5;
382 #else
383
384         printf("Enter Year of Alarm Expiry: ");
385         year = __get_integer_input_data();
386
387         printf("Enter month of Alarm Expiry: ");
388         month = __get_integer_input_data();
389
390         printf("Enter day of Alarm Expiry: ");
391         day = __get_integer_input_data();
392
393         printf("Enter hour of Alarm Expiry: ");
394         hour = __get_integer_input_data();
395
396         printf("Enter minute of Alarm Expiry: ");
397         min = __get_integer_input_data();
398
399         printf("Enter second Alarm Expiry: ");
400         sec = __get_integer_input_data();
401
402         test_time.year = year;
403         test_time.month = month;
404         test_time.day = day;
405         test_time.hour = hour;
406         test_time.min = min;
407         test_time.sec = sec;
408         printf
409             ("Note: Test Program will exit After [Year:%d Month:%d Day:%d "
410              "at Hour:%d Min:%d Sec:%d]. Please wait....\n\n",
411              year, month, day, hour, min, sec);
412 #endif
413
414         result = alarmmgr_set_time(alarm_info, test_time);
415         __print_api_return_result(result, "alarmmgr_set_time");
416
417 #if AUTO_TEST
418         repeat_mode = ALARM_REPEAT_MODE_WEEKLY;
419 #else
420         repeat_mode = __get_alarm_repeat_mode();
421 #endif
422         result = alarmmgr_set_repeat_mode(alarm_info, repeat_mode,
423                                           ALARM_WDAY_SUNDAY | ALARM_WDAY_MONDAY
424                                           | ALARM_WDAY_TUESDAY |
425                                           ALARM_WDAY_WEDNESDAY |
426                                           ALARM_WDAY_THURSDAY |
427                                           ALARM_WDAY_FRIDAY |
428                                           ALARM_WDAY_SATURDAY);
429         __print_api_return_result(result, "alarmmgr_set_repeat_mode");
430
431 #if AUTO_TEST
432         type = (random() % 2) + 1;
433 #else
434         type = __get_alarm_persistance_type();
435 #endif
436         result = alarmmgr_set_type(alarm_info, type);
437         __print_api_return_result(result, "alarmmgr_set_type");
438
439 #if AUTO_TEST
440         result =
441             alarmmgr_add_alarm_with_localtime(alarm_info, destination,
442                                               &alarm_id);
443 #else
444         destination = __get_alarm_destination();
445         result =
446             alarmmgr_add_alarm_with_localtime(alarm_info, destination,
447                                               &alarm_id);
448         __print_api_return_result(result, "alarmmgr_add_alarm_with_localtime");
449         printf("Created Alarm with Alarm ID %d\n", alarm_id);
450 #endif
451
452         cleanup_req = true;
453
454         pgm_exit_time = 70000;
455         printf
456             ("Note: Test Program will exit After %f seconds..."
457              "Please wait....\n\n", (float)pgm_exit_time / 1000);
458
459         return;
460 }
461
462 /*Function to create alarm with a delay for first expiry and
463 alarm expiry inteval for subsequent alarm*/
464 static void __create_interval_alarm(void)
465 {
466         time_t current_time = { 0, };
467         int result = 0;
468         int delay = 0;
469         int interval = 0;
470         int type = 0;
471
472         char *destination = NULL;
473
474         time(&current_time);
475 #if AUTO_TEST
476         int i = 0;
477         delay = 1;
478 #else
479         printf("current time: %s\n", ctime(&current_time));
480         printf("Enter First Delay (in seconds) for alarm: \n");
481         delay = __get_integer_input_data();
482 #endif
483
484 #if AUTO_TEST
485         interval = 1;
486 #else
487         printf("Enter Alarm repeat Interval in seconds (if 0, no repeat): \n");
488         interval = __get_integer_input_data();
489 #endif
490
491 #if AUTO_TEST
492         type = (random() % 2) + 1;
493 #else
494         type = __get_alarm_persistance_type();
495 #endif
496
497 #if AUTO_TEST
498         multiple_alarm = true;
499         for (i = 0; i < ITER_COUNT; i++) {
500                 interval++;
501                 delay++;
502                 result =
503                     alarmmgr_add_alarm(type, delay, interval,
504                                        destination, &alarm_id_arr[i]);
505                 __print_api_return_result(result, "alarmmgr_add_alarm");
506         }
507 #else
508         destination = __get_alarm_destination();
509         result =
510             alarmmgr_add_alarm(type, delay, interval, destination, &alarm_id);
511         __print_api_return_result(result, "alarmmgr_add_alarm");
512         printf("Created Alarm with Alarm ID %d\n", alarm_id);
513 #endif
514
515         if (interval > 0) {
516                 cleanup_req = true;
517         }
518         pgm_exit_time = ((delay + interval) * 1000) + 100;
519         printf
520             ("Note: Test Program will exit After %f seconds..."
521              "Please wait....\n\n", (float)pgm_exit_time / 1000);
522
523         return;
524 }
525
526 /*Function to create alarm (with app-svc information)
527 in the calender input format */
528 static void __create_app_svc_calendar_alarm(void)
529 {
530         time_t current_time;
531         struct tm current_tm;
532         alarm_entry_t *alarm_info = NULL;
533         int result;
534         alarm_date_t test_time;
535         int type = 0;
536         bundle *b = NULL;
537         int op_type = 0;
538         int repeat_mode = 0;
539
540         char pkgname[1024] = { 0, };
541
542         memset(pkgname, '\0', 1024);
543
544         int year, month, day, hour, min, sec;
545         year = month = day = hour = min = sec = 0;
546
547         time(&current_time);
548         printf("current time: %s\n", ctime(&current_time));
549
550         localtime_r(&current_time, &current_tm);
551         alarm_info = alarmmgr_create_alarm();
552         assert(alarm_info != NULL);
553
554 #if AUTO_TEST
555         test_time.year = current_tm.tm_year;
556         test_time.month = current_tm.tm_mon;
557         test_time.day = current_tm.tm_mday;
558
559         test_time.hour = current_tm.tm_hour;
560         test_time.min = current_tm.tm_min + 1;
561         test_time.sec = 5;
562 #else
563         printf("Enter Year of Alarm Expiry: ");
564         year = __get_integer_input_data();
565
566         printf("Enter month of Alarm Expiry: ");
567         month = __get_integer_input_data();
568
569         printf("Enter day of Alarm Expiry: ");
570         day = __get_integer_input_data();
571
572         printf("Enter hour of Alarm Expiry: ");
573         hour = __get_integer_input_data();
574
575         printf("Enter minute of Alarm Expiry: ");
576         min = __get_integer_input_data();
577
578         printf("Enter second Alarm Expiry: ");
579         sec = __get_integer_input_data();
580
581         test_time.year = year;
582         test_time.month = month;
583         test_time.day = day;
584         test_time.hour = hour;
585         test_time.min = min;
586         test_time.sec = sec;
587
588         printf
589             ("Note: Test Program will exit After "
590              "[Year:%d Month:%d Day:%d at Hour:%d Min:%d Sec:%d]."
591              " Please wait....\n\n", year, month, day, hour, min, sec);
592 #endif
593         result = alarmmgr_set_time(alarm_info, test_time);
594         __print_api_return_result(result, "alarmmgr_set_time");
595
596 #if AUTO_TEST
597         repeat_mode = ALARM_REPEAT_MODE_WEEKLY;
598 #else
599         repeat_mode = __get_alarm_repeat_mode();
600 #endif
601         result = alarmmgr_set_repeat_mode(alarm_info, repeat_mode,
602                                           ALARM_WDAY_SUNDAY | ALARM_WDAY_MONDAY
603                                           | ALARM_WDAY_TUESDAY |
604                                           ALARM_WDAY_WEDNESDAY |
605                                           ALARM_WDAY_THURSDAY |
606                                           ALARM_WDAY_FRIDAY |
607                                           ALARM_WDAY_SATURDAY);
608         __print_api_return_result(result, "alarmmgr_set_repeat_mode");
609
610 #if AUTO_TEST
611         type = (random() % 2) + 1;
612 #else
613         type = __get_alarm_persistance_type();
614 #endif
615         result = alarmmgr_set_type(alarm_info, type);
616         __print_api_return_result(result, "alarmmgr_set_type");
617
618         b = bundle_create();
619
620         assert(b != NULL);
621
622 #if AUTO_TEST
623         op_type = 1;
624 #else
625         printf("Enter APPSVC Operation\n");
626         printf("1. APPSVC_OPERATION_DEFAULT \n");
627         printf("2. APPSVC_OPERATION_SEARCH\n");
628         printf("3. No operation\n");
629         op_type = __get_integer_input_data();
630 #endif
631         switch (op_type) {
632         case 1:{
633                         appsvc_set_operation(b, APPSVC_OPERATION_DEFAULT);
634                         break;
635                 }
636         case 2:{
637                         appsvc_set_operation(b, APPSVC_OPERATION_SEARCH);
638                         break;
639                 }
640         case 3:
641                 {
642                         printf("NO APPSVC OPERATION!!!\n");
643                         break;
644                 }
645         default:{
646                         printf("Invalid option....Try Again\n");
647                         exit(-1);
648                 }
649
650         }
651
652 #if AUTO_TEST
653         sprintf(pkgname, "org.tizen.gallery");
654 #else
655         fflush(stdin);
656
657         printf
658             ("Enter Pkgname: [Ex:org.tizen.alarm-test "
659              "or org.tizen.gallery...]\n");
660         if (fgets(pkgname, 1024, stdin) == NULL) {
661                 printf("Input buffer overflow....,try again\n");
662                 exit(-1);
663         }
664
665         pkgname[strlen(pkgname) - 1] = '\0';
666 #endif
667         appsvc_set_pkgname(b, pkgname);
668
669         result =
670             alarmmgr_add_alarm_appsvc_with_localtime(alarm_info, (void *)b,
671                                                      &alarm_id);
672         __print_api_return_result(result,
673                                   "alarmmgr_add_alarm_appsvc_with_localtime");
674
675         printf("Alarm created with alarm id %d\n", alarm_id);
676
677         cleanup_req = true;
678
679         pgm_exit_time = 70000;
680
681         printf
682             ("Note: Test Program will exit After %f "
683              "seconds...Please wait....\n\n", (float)pgm_exit_time / 1000);
684         printf
685             ("Upon Alarm Expiry, %s application will be "
686              "launched (purpose:testing)\n", pkgname);
687
688 }
689
690 /*Function to create alarm with a delay for first expiry and
691 alarm expiry inteval for subsequent alarm (with app-svc information)*/
692 static void __create_app_svc_interval_alarm(void)
693 {
694         time_t current_time = { 0, };
695         int result = 0;
696         int delay = 0;
697         int interval = 0;
698         int type = 0;
699         bundle *b = NULL;
700         int op_type = 0;
701
702         char pkgname[1024] = { 0, };
703
704         memset(pkgname, '\0', 1024);
705
706         time(&current_time);
707 #if AUTO_TEST
708         int i = 0;
709         delay = (random() % 9) + 1;
710         interval = (random() % 9);
711 #else
712         printf("current time: %s\n", ctime(&current_time));
713         printf("Enter First Delay (in seconds) for alarm: \n");
714         delay = __get_integer_input_data();
715         printf("Enter Alarm repeat Interval in seconds (if 0, no repeat): \n");
716         interval = __get_integer_input_data();
717 #endif
718         if (interval > 0) {
719                 cleanup_req = true;
720         }
721         pgm_exit_time = ((delay + interval) * 1000) + 100;
722         printf
723             ("Note: Test Program will exit After %f "
724              "seconds...Please wait....\n\n", (float)pgm_exit_time / 1000);
725
726 #if AUTO_TEST
727         type = (random() % 2) + 1;
728 #else
729         type = __get_alarm_persistance_type();
730 #endif
731
732         b = bundle_create();
733
734         assert(b != NULL);
735
736 #if AUTO_TEST
737         op_type = 1;
738 #else
739         printf("Enter APPSVC Operation\n");
740         printf("1. APPSVC_OPERATION_DEFAULT \n");
741         printf("2. APPSVC_OPERATION_SEARCH\n");
742         printf("3. No operation\n");
743         op_type = __get_integer_input_data();
744 #endif
745
746         switch (op_type) {
747         case 1:{
748                         appsvc_set_operation(b, APPSVC_OPERATION_DEFAULT);
749                         break;
750                 }
751         case 2:{
752                         appsvc_set_operation(b, APPSVC_OPERATION_SEARCH);
753                         break;
754                 }
755         case 3:
756                 {
757                         printf("NO APPSVC OPERATION!!!\n");
758                         break;
759                 }
760         default:{
761                         printf("Invalid option....Try Again\n");
762                         exit(-1);
763                 }
764
765         }
766
767         fflush(stdin);
768
769 #if AUTO_TEST
770         sprintf(pkgname, "org.tizen.gallery");
771 #else
772         printf
773             ("Enter Pkgname: [Ex:org.tizen.alarm-test "
774              "or org.tizen.gallery...]\n");
775         if (fgets(pkgname, 1024, stdin) == NULL) {
776                 printf("Input buffer overflow...Try Again.\n");
777                 exit(-1);
778         }
779         pkgname[strlen(pkgname) - 1] = '\0';
780 #endif
781         appsvc_set_pkgname(b, pkgname);
782
783 #if AUTO_TEST
784         multiple_alarm = true;
785         for (i = 0; i < ITER_COUNT; i++) {
786                 interval++;
787                 delay++;
788                 result =
789                     alarmmgr_add_alarm_appsvc(type, delay, interval, (void *)b,
790                                               &alarm_id_arr[i]);
791                 __print_api_return_result(result, "alarmmgr_add_alarm");
792         }
793 #else
794         result =
795             alarmmgr_add_alarm_appsvc(type, delay, interval, (void *)b,
796                                       &alarm_id);
797         __print_api_return_result(result, "alarmmgr_add_alarm_appsvc");
798         printf("Created Alarm with Alarm ID %d\n", alarm_id);
799 #endif
800
801         if (interval > 0) {
802                 cleanup_req = true;
803         }
804         pgm_exit_time = ((delay + interval) * 1000) + 100;
805         printf
806             ("Note: Test Program will exit After %f seconds..."
807              "Please wait....\n\n", (float)pgm_exit_time / 1000);
808
809         return;
810 }
811
812 /*This function will be executed before program exit (if required)*/
813 static void __test_pgm_clean_up(void)
814 {
815         int result = 0;
816         printf("Cleaning up test program\n");
817 #if AUTO_TEST
818         int i = 0;
819         if (multiple_alarm == true) {
820                 for (i = 0; i < ITER_COUNT; i++) {
821                         result = alarmmgr_remove_alarm(alarm_id_arr[i]);
822                         __print_api_return_result(result,
823                                                   "alarmmgr_remove_alarm");
824                 }
825                 multiple_alarm = false;
826         } else {
827                 result = alarmmgr_remove_alarm(alarm_id);
828                 __print_api_return_result(result, "alarmmgr_remove_alarm");
829         }
830 #else
831         result = alarmmgr_remove_alarm(alarm_id);
832         __print_api_return_result(result, "alarmmgr_remove_alarm");
833 #endif
834         return;
835 }
836
837 /*Function to exit gmainloop*/
838 static gboolean __timout_handler(gpointer user_data)
839 {
840         if (mainloop)
841                 g_main_loop_quit(mainloop);
842
843         return false;
844 }
845
846 /*Main alarm create function which provides options
847  for different types of alarm creation*/
848 static void __test_alarm_manager_callback(void)
849 {
850         printf("Inside Alarm Callback test\n");
851         int result;
852
853         mainloop = g_main_loop_new(NULL, FALSE);
854         assert(mainloop != NULL);
855
856         result = alarmmgr_init("org.tizen.alarm-test");
857         __print_api_return_result(result, "alarmmgr_init");
858
859         result = alarmmgr_set_cb(__alarm_callback, NULL);
860         __print_api_return_result(result, "alarmmgr_set_cb");
861         result = alarmmgr_set_cb(__alarm_callback1, NULL);
862         __print_api_return_result(result, "alarmmgr_set_cb");
863
864         __create_interval_alarm();
865
866         g_timeout_add(pgm_exit_time, __timout_handler, NULL);
867
868         g_main_loop_run(mainloop);
869
870         if (cleanup_req) {
871                 __test_pgm_clean_up();
872         }
873
874         return;
875 }
876
877 /*Main alarm create function which provides options
878  for different types of alarm creation*/
879 static void __test_alarm_manager_create_alarm(void)
880 {
881         int result = 0;
882         int alarm_type = 0;
883
884 #if AUTO_TEST
885         alarm_type = sub_type;
886 #else
887         char destination[1024] = { 0, };
888
889         printf("Enter Alarm Type\n");
890         printf("1. Interval with initial delay\n");
891         printf("2. App-svc Interval alarm with initial delay\n");
892         printf("3. Calendar Alarm\n");
893         printf("4. App-svc Calendar Alarm\n");
894         printf("Enter Choice between [1-4]....\n");
895         alarm_type = __get_integer_input_data();
896 #endif
897         mainloop = g_main_loop_new(NULL, FALSE);
898         assert(mainloop != NULL);
899
900         switch (alarm_type) {
901         case 1:
902                 {
903 #if AUTO_TEST
904                         result = alarmmgr_init("org.tizen.1000");
905 #else
906                         fflush(stdin);
907                         printf
908                             ("Enter pkgname (used to generate service name)"
909                              "[Ex:org.tizen.alarm-test or "
910                              "org.tizen.1000 etc]\n");
911                         if (fgets(destination, 1024, stdin) == NULL) {
912                                 printf("Input buffer overflow....,try again\n");
913                                 exit(-1);
914                         }
915
916                         destination[strlen(destination) - 1] = '\0';
917
918                         result = alarmmgr_init(destination);
919 #endif
920                         __print_api_return_result(result, "alarmmgr_init");
921
922                         result = alarmmgr_set_cb(__alarm_callback, NULL);
923                         __print_api_return_result(result, "alarmmgr_set_cb");
924
925                         __create_interval_alarm();
926
927                         break;
928                 }
929         case 2:
930                 {
931                         printf("Inside APPSVC Interval Alarm test\n");
932                         __create_app_svc_interval_alarm();
933                         break;
934                 }
935         case 3:
936                 {
937                         printf("Inside Calendar Alarm test\n");
938 #if AUTO_TEST
939                         result = alarmmgr_init("org.tizen.1000");
940 #else
941                         fflush(stdin);
942                         printf
943                             ("Enter pkgname (used to generate service name)"
944                              "[Ex:org.tizen.alarm-test or "
945                              "org.tizen.1000 etc]\n");
946                         if (fgets(destination, 1024, stdin) == NULL) {
947                                 printf("Input buffer overflow....,try again\n");
948                                 exit(-1);
949                         }
950
951                         destination[strlen(destination) - 1] = '\0';
952
953                         result = alarmmgr_init(destination);
954 #endif
955                         __print_api_return_result(result, "alarmmgr_init");
956
957                         result = alarmmgr_set_cb(__alarm_callback, NULL);
958                         __print_api_return_result(result, "alarmmgr_set_cb");
959
960                         __create_calendar_alarm();
961                         break;
962                 }
963         case 4:
964                 {
965                         printf("Inside APPSVC Calendar Alarm test\n");
966                         __create_app_svc_calendar_alarm();
967                         break;
968                 }
969         default:
970                 {
971                         printf("Invalid option....Try Again\n");
972                         return;
973                 }
974         }
975
976         g_timeout_add(pgm_exit_time, __timout_handler, NULL);
977
978         g_main_loop_run(mainloop);
979
980         if (cleanup_req) {
981                 __test_pgm_clean_up();
982         }
983
984         return;
985 }
986
987 /*Main alarm get information function which provides
988  different options of getting alarm information*/
989 static void __test_alarm_manager_get_information(void)
990 {
991
992         printf("Inside Get Info Interval Alarm test\n");
993         int option = 0;
994         int result = 0;
995         alarm_entry_t *alarm_info;
996         int type = 0;
997
998         alarm_date_t time = { 0, };
999
1000 #if AUTO_TEST
1001         option = 2;
1002 #else
1003         printf("1. Get Existing Alarm's Information\n");
1004         printf("2. Create Alarm & Get Information\n");
1005         option = __get_integer_input_data();
1006 #endif
1007         alarm_info = alarmmgr_create_alarm();
1008         assert(alarm_info != NULL);
1009
1010         switch (option) {
1011         case 1:
1012                 {
1013                         printf("Enter Alarm Id: \n");
1014                         alarm_id = __get_integer_input_data();
1015                         result = alarmmgr_get_info(alarm_id, alarm_info);
1016                         __print_api_return_result(result, "alarmmgr_get_info");
1017                         result = alarmmgr_get_time(alarm_info, &time);
1018                         __print_api_return_result(result, "alarmmgr_get_time");
1019                         printf("day is %d\n", time.day);
1020                         printf("Alarm Get Info Success\n");
1021                         break;
1022                 }
1023         case 2:
1024                 {
1025                         result = alarmmgr_init("org.tizen.10000");
1026                         __print_api_return_result(result, "alarmmgr_init");
1027
1028 #if AUTO_TEST
1029                         type = (random() % 2) + 1;
1030 #else
1031                         type = __get_alarm_persistance_type();
1032 #endif
1033
1034                         result =
1035                             alarmmgr_add_alarm(type, 2, 0, NULL, &alarm_id);
1036                         __print_api_return_result(result, "alarmmgr_add_alarm");
1037                         printf("Created Alarm with Alarm ID %d\n", alarm_id);
1038
1039                         pgm_exit_time = 3000;
1040                         g_timeout_add(pgm_exit_time, __timout_handler, NULL);
1041
1042                         result = alarmmgr_get_info(alarm_id, alarm_info);
1043                         __print_api_return_result(result, "alarmmgr_get_info");
1044
1045                         alarm_date_t time = { 0, };
1046                         result = alarmmgr_get_time(alarm_info, &time);
1047                         __print_api_return_result(result, "alarmmgr_get_time");
1048
1049                         printf("day is %d\n", time.day);
1050                         printf("Alarm Get Info Success\n");
1051
1052                         __test_pgm_clean_up();
1053
1054                         break;
1055                 }
1056         default:
1057                 {
1058                         printf("Invalid option\n");
1059                         exit(-1);
1060                 }
1061         }
1062         return;
1063 }
1064
1065 static void __test_alarm_manager_get_appsvc_information(void)
1066 {
1067
1068
1069         printf("Inside Get Info Interval Alarm test\n");
1070         int option = 0;
1071         int result = 0;
1072         alarm_entry_t *alarm_info;
1073         bundle *b = NULL;
1074
1075 #if AUTO_TEST
1076         option = 2;
1077 #else
1078         printf("1. Get Existing Alarm's Information\n");
1079         printf("2. Create Alarm & Get Information\n");
1080         option = __get_integer_input_data();
1081 #endif
1082         alarm_info = alarmmgr_create_alarm();
1083         assert(alarm_info != NULL);
1084
1085         switch (option) {
1086         case 1:
1087                 {
1088                         printf("Enter Alarm ID:\n");
1089                         alarm_id = __get_integer_input_data();
1090
1091                         b = alarmmgr_get_alarm_appsvc_info(alarm_id, &result);
1092
1093                         if (b)
1094                                 bundle_iterate(b, __print_appsvc_bundle_data, NULL);
1095
1096                         __print_api_return_result(result, "alarmmgr_get_alarm_appsvc_info");
1097
1098                         break;
1099                 }
1100         case 2:
1101                 {
1102                         mainloop = g_main_loop_new(NULL, FALSE);
1103                         assert(mainloop != NULL);
1104
1105                         __create_app_svc_interval_alarm();
1106
1107                         b = alarmmgr_get_alarm_appsvc_info(alarm_id, &result);
1108                         __print_api_return_result(result, "alarmmgr_get_alarm_appsvc_info");
1109                         if (b)
1110                                 bundle_iterate(b, __print_appsvc_bundle_data, NULL);
1111
1112                         g_timeout_add(2000, __timout_handler, NULL);
1113
1114                         g_main_loop_run(mainloop);
1115
1116                         if (cleanup_req) {
1117                                 __test_pgm_clean_up();
1118
1119                         break;
1120                 }
1121         default:
1122                 {
1123                         printf("Invalid option\n");
1124                         exit(-1);
1125                 }
1126         }
1127         }
1128
1129         return;
1130 }
1131
1132 static int __alarm_id_retrive_callback(alarm_id_t id, void *user_param)
1133 {
1134         int *n = NULL;
1135         n = (int *)user_param;
1136         if (n == NULL) {
1137                 printf("Invalid Parameter in test program");
1138                 return -1;
1139         }
1140         printf("[%d]Alarm ID is %d\n", *n, id);
1141         (*n)++;
1142
1143         return 0;
1144 }
1145
1146 static void __test_alarm_manager_get_alarm_ids(void)
1147 {
1148         printf("Inside Get Ids Alarm test\n");
1149         int option = 0;
1150         int result = 0;
1151         int n = 1;
1152         int type = 0;
1153 #if AUTO_TEST
1154         option = 2;
1155 #else
1156         printf("1. Get Existing Alarm's ID\n");
1157         printf("2. Create Alarm & Get ID\n");
1158
1159         option = __get_integer_input_data();
1160 #endif
1161         switch (option) {
1162         case 1:
1163                 {
1164                         result = alarmmgr_init("org.tizen.1000");
1165                         __print_api_return_result(result, "alarmmgr_init");
1166                         result =
1167                             alarmmgr_enum_alarm_ids(__alarm_id_retrive_callback,
1168                                                     (void *)&n);
1169                         __print_api_return_result(result,
1170                                                   "alarmmgr_enum_alarm_ids");
1171                         break;
1172                 }
1173         case 2:
1174                 {
1175                         result = alarmmgr_init("org.tizen.test");
1176                         __print_api_return_result(result, "alarmmgr_init");
1177
1178 #if AUTO_TEST
1179                         type = (random() % 2) + 1;
1180 #else
1181                         type = __get_alarm_persistance_type();
1182 #endif
1183                         result = alarmmgr_add_alarm(type, 2, 0,
1184                                                     NULL, &alarm_id);
1185                         printf("Created Alarm with Alarm ID %d\n", alarm_id);
1186
1187                         __print_api_return_result(result, "alarmmgr_add_alarm");
1188
1189                         result = alarmmgr_add_alarm(type, 3, 0,
1190                                                     NULL, &alarm_id);
1191                         __print_api_return_result(result, "alarmmgr_add_alarm");
1192                         printf("Created Alarm with Alarm ID %d\n", alarm_id);
1193
1194                         pgm_exit_time = 5100;
1195                         g_timeout_add(pgm_exit_time, __timout_handler, NULL);
1196
1197                         result =
1198                             alarmmgr_enum_alarm_ids(__alarm_id_retrive_callback,
1199                                                     (void *)&n);
1200                         __print_api_return_result(result,
1201                                                   "alarmmgr_enum_alarm_ids");
1202
1203                         break;
1204                 }
1205         default:
1206                 {
1207                         printf("Invalid option\n");
1208                         exit(-1);
1209                 }
1210         }
1211         return;
1212 }
1213
1214 /*Main alarm delete test function which provides options
1215  for different types of alarm deletion*/
1216 static void __test_alarm_manager_delete_alarm(void)
1217 {
1218         int option = 0;
1219         int result = 0;
1220         int type = 0;
1221         printf("Inside Delete Alarm test\n");
1222
1223 #if AUTO_TEST
1224         option = 2;
1225 #else
1226         printf("1. Delete Existing Alarm\n");
1227         printf("2. Create Alarm & Delete\n");
1228
1229         option = __get_integer_input_data();
1230 #endif
1231         switch (option) {
1232         case 1:
1233                 {
1234                         printf("Enter Alarm Id: \n");
1235                         option = __get_integer_input_data();
1236
1237                         result = alarmmgr_remove_alarm(option);
1238                         __print_api_return_result(result,
1239                                                   "alarmmgr_remove_alarm");
1240                         break;
1241                 }
1242         case 2:
1243                 {
1244                         result = alarmmgr_init("org.tizen.test");
1245                         __print_api_return_result(result, "alarmmgr_init");
1246
1247 #if AUTO_TEST
1248                         type = (random() % 2) + 1;
1249 #else
1250                         type = __get_alarm_persistance_type();
1251 #endif
1252                         result = alarmmgr_add_alarm(type, 2, 0,
1253                                                     NULL, &alarm_id);
1254                         __print_api_return_result(result, "alarmmgr_add_alamr");
1255                         printf("Created Alarm with Alarm ID %d\n", alarm_id);
1256
1257                         result = alarmmgr_remove_alarm(alarm_id);
1258                         __print_api_return_result(result,
1259                                                   "alarmmgr_remove_alarm");
1260
1261                         break;
1262                 }
1263         default:
1264                 {
1265                         printf("Invalid Option\n");
1266                         break;
1267                 }
1268         }
1269         return;
1270 }
1271
1272 /*Entry point of the test program*/
1273 int main(int argc, char **argv)
1274 {
1275 #if AUTO_TEST
1276         int i = 0;
1277         int k = 0;
1278         int iteration = 0;
1279
1280         int fd = 0;
1281
1282         /*test result will be written into below file */
1283         fd = open("/var/alarm_test.log", O_CREAT | O_RDWR | O_APPEND, 0666);
1284         if (fd < 0) {
1285                 printf("Unable to create log file\n");
1286                 return -1;
1287         }
1288         if (argc < 3) {
1289                 printf("Insufficient arguments\n");
1290                 printf
1291                     ("Example: ./test_alarmmgr.exe {all|create_interval|"
1292                      "create_svc_interval|create_calendar|create_svc_calendar|"
1293                      "delete|get_id|callback} <iteration>");
1294                 return -1;
1295         }
1296
1297         close(1);
1298         close(2);
1299         dup(fd);
1300         dup(fd);
1301
1302         char *test_job = NULL;
1303
1304         test_job = argv[1];
1305         iteration = atoi(argv[2]);
1306
1307         printf("##################Alarm Test for %s Starts##################\n",
1308                test_job);
1309         g_type_init();
1310
1311         for (i = 0; i < iteration; i++) {
1312                 printf("########ITERATION %d #############\n", i);
1313                 if (strncmp(test_job, "create_interval", 15) == 0) {
1314                         sub_type = INTERVAL;
1315                         __test_alarm_manager_create_alarm();
1316                 } else if (strncmp(test_job, "create_svc_interval", 19) == 0) {
1317                         sub_type = INTERVAL_APPSVC;
1318                         __test_alarm_manager_create_alarm();
1319                 } else if (strncmp(test_job, "create_calendar", 15) == 0) {
1320                         sub_type = CALENDAR;
1321                         __test_alarm_manager_create_alarm();
1322                 } else if (strncmp(test_job, "create_svc_calendar", 19) == 0) {
1323                         sub_type = CALENDAR_APPSVC;
1324                         __test_alarm_manager_create_alarm();
1325                 } else if (strncmp(test_job, "delete", 6) == 0) {
1326                         __test_alarm_manager_delete_alarm();
1327                 } else if (strncmp(test_job, "get_info", 8) == 0) {
1328                         __test_alarm_manager_get_information();
1329                 } else if (strncmp(test_job, "get_id", 6) == 0) {
1330                         __test_alarm_manager_get_alarm_ids();
1331                 } else if (strncmp(test_job, "callback", 8) == 0) {
1332                         __test_alarm_manager_callback();
1333                 } else if (strncmp(test_job, "all", 3) == 0) {
1334                         for (k = INTERVAL; k <= CALENDAR_APPSVC; k++) {
1335                                 sub_type = k;
1336                                 __test_alarm_manager_create_alarm();
1337                         }
1338                         __test_alarm_manager_delete_alarm();
1339                         __test_alarm_manager_get_information();
1340                         __test_alarm_manager_get_alarm_ids();
1341                         __test_alarm_manager_get_alarm_ids();
1342                         __test_alarm_manager_callback();
1343
1344                 } else {
1345                         printf("Invalid Option\n");
1346                         return -1;
1347                 }
1348         }
1349         printf("##################Alarm Test for %s Ends##################\n\n",
1350                test_job);
1351         close(fd);
1352
1353 #else
1354         /*Compile with disabling AUTO_TEST flag for individual testing */
1355         int option = 0;
1356
1357         g_type_init();
1358
1359         printf("*********ALARM MANAGER TESTING************\n");
1360         printf("1. Test Alarm-Manager: Create Alarm\n");
1361         printf("2. Test Alarm-Manager: Delete Alarm\n");
1362         printf("3. Test Alarm-Manager: Get Alarm Information\n");
1363         printf("4. Test Alarm-Manager: Get Alarm ID\n");
1364         printf("5. Test Alarm-Manager: Callback\n");
1365         printf("6. Test Alarm-Manager: Get Alarm app-svc(bundle) Information");
1366         printf("Enter Choice between [1-5]....\n");
1367
1368         option = __get_integer_input_data();
1369         switch (option) {
1370         case 1:
1371                 {
1372                         __test_alarm_manager_create_alarm();
1373                         break;
1374                 }
1375         case 2:
1376                 {
1377                         __test_alarm_manager_delete_alarm();
1378                         break;
1379                 }
1380         case 3:
1381                 {
1382                         __test_alarm_manager_get_information();
1383                         break;
1384                 }
1385         case 4:
1386                 {
1387                         __test_alarm_manager_get_alarm_ids();
1388                         break;
1389                 }
1390         case 5:
1391                 {
1392                         __test_alarm_manager_callback();
1393                         break;
1394                 }
1395         case 6:
1396                 {
1397                         __test_alarm_manager_get_appsvc_information();
1398                         break;
1399                 }
1400         default:
1401                 {
1402                         printf("Invalid Input!!! Retry again\n");
1403                 }
1404
1405         }
1406         printf("****************************************\n");
1407 #endif
1408         return 0;
1409 }