tizen 2.4 release
[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[1024] = { 0, };
241         int data = 0;
242
243         if (fgets(input_str, 1024, stdin) == NULL) {
244                 printf("Input buffer overflow....\n");
245                 return -1;
246         }
247
248         if (sscanf(input_str, "%4d", &data) != 1) {
249                 printf("Input only integer option....\n");
250                 return -1;
251         }
252
253         return data;
254 }
255
256 /*Function to get alarm persistance type*/
257 static int __get_alarm_persistance_type(void)
258 {
259         int type = ALARM_TYPE_VOLATILE;
260         int alarm_type = 0;
261         printf("Enter Type\n");
262         printf("1. ALARM_TYPE_DEFAULT \n");
263         printf("2. ALARM_TYPE_VOLATILE\n");
264         printf("Enter Choice between [1-2]....\n");
265         alarm_type = __get_integer_input_data();
266
267         switch (alarm_type) {
268         case USER_INPUT_DEFAULT:
269                 {
270                         type = ALARM_TYPE_DEFAULT;
271                         break;
272                 }
273         case USER_INPUT_VOLATILE:
274                 {
275                         type = ALARM_TYPE_VOLATILE;
276                         break;
277                 }
278         default:
279                 {
280                         printf("Invalid option....Try Again\n");
281                         exit(-1);
282                 }
283
284         }
285         return type;
286 }
287
288 static char *__get_alarm_destination(void)
289 {
290         int alarm_type = 0;
291         char *destination = NULL;
292         printf("[Optional]: Do you want to provide Destination?\n");
293         printf("1. YES\n");
294         printf("2. NO \n");
295         printf("Enter Choice between [1-2]....\n");
296         alarm_type = __get_integer_input_data();
297         switch (alarm_type) {
298         case USER_INPUT_YES:
299                 {
300
301                         destination = (char *)malloc(1024);
302                         if (NULL == destination) {
303                                 return NULL;
304                         }
305                         printf
306                             ("Enter destination: [Ex:com.samsung.alarm-test "
307                              "or com.samsung.gallery...]\n");
308                         if (fgets(destination, 1024, stdin) == NULL) {
309                                 printf("Input buffer overflow....,try again\n");
310                                 exit(-1);
311                         }
312
313                         destination[strlen(destination) - 1] = '\0';
314
315                         break;
316                 }
317         case USER_INPUT_NO:
318                 {
319                         break;
320                 }
321         default:
322                 {
323                         printf("Invalid option....Try Again\n");
324                         exit(-1);
325                 }
326         }
327
328         return destination;
329 }
330
331 static int __get_alarm_repeat_mode(void)
332 {
333         int repeat_mode = 0;
334         printf("Enter Repeat Mode:\n");
335         printf("0. ALARM_REPEAT_MODE_ONCE\n");
336         printf("1. ALARM_REPEAT_MODE_REPEAT\n");
337         printf("2. ALARM_REPEAT_MODE_WEEKLY\n");
338         printf("3. ALARM_REPEAT_MODE_MONTHLY\n");
339         printf("4. ALARM_REPEAT_MODE_ANNUALLY\n");
340         printf("5. ALARM_REPEAT_MODE_MAX\n");
341         printf("Enter Choice between [0-5]....\n");
342         repeat_mode = __get_integer_input_data();
343         if (repeat_mode > 5 || repeat_mode < 0) {
344                 printf("Invalid option....Try Again\n");
345                 exit(-1);
346         }
347         return repeat_mode;
348 }
349
350 static void __test_alarm_manager_set_rtc_wakeup_phone(void){
351
352         time_t current_time;
353         alarm_date_t time1={0,};
354         int ret = 0;
355
356         time(&current_time);
357         printf("current time: %s\n", ctime(&current_time));
358
359         printf("Enter Year: ");
360         time1.year = __get_integer_input_data();
361
362         printf("Enter month: ");
363         time1.month = __get_integer_input_data();
364
365         printf("Enter day: ");
366         time1.day = __get_integer_input_data();
367
368         printf("Enter hour: ");
369         time1.hour = __get_integer_input_data();
370
371         printf("Enter minute: ");
372         time1.min = __get_integer_input_data();
373
374         printf("Enter second: ");
375         time1.sec = __get_integer_input_data();
376
377         ret = alarmmgr_set_rtc_time(&time1);
378
379         __print_api_return_result(ret, "alarmmgr_set_rtc_time");
380
381         return;
382
383 }
384
385 /*Function to create alarm in the calender input format*/
386 static void __create_calendar_alarm(void)
387 {
388         char *destination = NULL;
389         time_t current_time;
390         struct tm current_tm;
391         alarm_entry_t *alarm_info = NULL;
392         int result = 0;
393         alarm_date_t test_time;
394         int type = 0;
395         int year, month, day, hour, min, sec;
396         int repeat_mode = 0;
397
398         year = month = day = hour = min = sec = 0;
399
400         time(&current_time);
401         printf("current time: %s\n", ctime(&current_time));
402
403         localtime_r(&current_time, &current_tm);
404         alarm_info = alarmmgr_create_alarm();
405         assert(alarm_info != NULL);
406
407 #if AUTO_TEST
408         test_time.year = current_tm.tm_year;
409         test_time.month = current_tm.tm_mon;
410         test_time.day = current_tm.tm_mday;
411
412         test_time.hour = current_tm.tm_hour;
413         test_time.min = current_tm.tm_min + 1;
414         test_time.sec = 5;
415 #else
416
417         printf("Enter Year of Alarm Expiry: ");
418         year = __get_integer_input_data();
419
420         printf("Enter month of Alarm Expiry: ");
421         month = __get_integer_input_data();
422
423         printf("Enter day of Alarm Expiry: ");
424         day = __get_integer_input_data();
425
426         printf("Enter hour of Alarm Expiry: ");
427         hour = __get_integer_input_data();
428
429         printf("Enter minute of Alarm Expiry: ");
430         min = __get_integer_input_data();
431
432         printf("Enter second Alarm Expiry: ");
433         sec = __get_integer_input_data();
434
435         test_time.year = year;
436         test_time.month = month;
437         test_time.day = day;
438         test_time.hour = hour;
439         test_time.min = min;
440         test_time.sec = sec;
441         printf
442             ("Note: Test Program will exit After [Year:%d Month:%d Day:%d "
443              "at Hour:%d Min:%d Sec:%d]. Please wait....\n\n",
444              year, month, day, hour, min, sec);
445 #endif
446
447         result = alarmmgr_set_time(alarm_info, test_time);
448         __print_api_return_result(result, "alarmmgr_set_time");
449
450 #if AUTO_TEST
451         repeat_mode = ALARM_REPEAT_MODE_WEEKLY;
452 #else
453         repeat_mode = __get_alarm_repeat_mode();
454 #endif
455         result = alarmmgr_set_repeat_mode(alarm_info, repeat_mode,
456                                           ALARM_WDAY_SUNDAY | ALARM_WDAY_MONDAY
457                                           | ALARM_WDAY_TUESDAY |
458                                           ALARM_WDAY_WEDNESDAY |
459                                           ALARM_WDAY_THURSDAY |
460                                           ALARM_WDAY_FRIDAY |
461                                           ALARM_WDAY_SATURDAY);
462         __print_api_return_result(result, "alarmmgr_set_repeat_mode");
463
464 #if AUTO_TEST
465         type = (random() % 2) + 1;
466 #else
467         type = __get_alarm_persistance_type();
468 #endif
469         result = alarmmgr_set_type(alarm_info, type);
470         __print_api_return_result(result, "alarmmgr_set_type");
471
472 #if AUTO_TEST
473         result =
474             alarmmgr_add_alarm_with_localtime(alarm_info, destination,
475                                               &alarm_id);
476 #else
477         destination = __get_alarm_destination();
478         result =
479             alarmmgr_add_alarm_with_localtime(alarm_info, destination,
480                                               &alarm_id);
481         __print_api_return_result(result, "alarmmgr_add_alarm_with_localtime");
482         printf("Created Alarm with Alarm ID %d\n", alarm_id);
483 #endif
484
485         cleanup_req = true;
486
487         pgm_exit_time = 70000;
488         printf
489             ("Note: Test Program will exit After %f seconds..."
490              "Please wait....\n\n", (float)pgm_exit_time / 1000);
491
492         return;
493 }
494
495 /*Function to create alarm with a delay for first expiry and
496 alarm expiry inteval for subsequent alarm*/
497 static void __create_interval_alarm(void)
498 {
499         time_t current_time = { 0, };
500         int result = 0;
501         int delay = 0;
502         int interval = 0;
503         int type = 0;
504
505         char *destination = NULL;
506
507         time(&current_time);
508 #if AUTO_TEST
509         int i = 0;
510         delay = 1;
511 #else
512         printf("current time: %s\n", ctime(&current_time));
513         printf("Enter First Delay (in seconds) for alarm: \n");
514         delay = __get_integer_input_data();
515 #endif
516
517 #if AUTO_TEST
518         interval = 1;
519 #else
520         printf("Enter Alarm repeat Interval in seconds (if 0, no repeat): \n");
521         interval = __get_integer_input_data();
522 #endif
523
524 #if AUTO_TEST
525         type = (random() % 2) + 1;
526 #else
527         type = __get_alarm_persistance_type();
528 #endif
529
530 #if AUTO_TEST
531         multiple_alarm = true;
532         for (i = 0; i < ITER_COUNT; i++) {
533                 interval++;
534                 delay++;
535                 result =
536                     alarmmgr_add_alarm(type, delay, interval,
537                                        destination, &alarm_id_arr[i]);
538                 __print_api_return_result(result, "alarmmgr_add_alarm");
539         }
540 #else
541         destination = __get_alarm_destination();
542         result =
543             alarmmgr_add_alarm(type, delay, interval, destination, &alarm_id);
544         __print_api_return_result(result, "alarmmgr_add_alarm");
545         printf("Created Alarm with Alarm ID %d\n", alarm_id);
546 #endif
547
548         if (interval > 0) {
549                 cleanup_req = true;
550         }
551         pgm_exit_time = ((delay + interval) * 1000) + 100;
552         printf
553             ("Note: Test Program will exit After %f seconds..."
554              "Please wait....\n\n", (float)pgm_exit_time / 1000);
555
556         return;
557 }
558
559 /*Function to create alarm (with app-svc information)
560 in the calender input format */
561 static void __create_app_svc_calendar_alarm(void)
562 {
563         time_t current_time;
564         struct tm current_tm;
565         alarm_entry_t *alarm_info = NULL;
566         int result;
567         alarm_date_t test_time;
568         int type = 0;
569         bundle *b = NULL;
570         int op_type = 0;
571         int repeat_mode = 0;
572
573         char pkgname[1024] = { 0, };
574
575         memset(pkgname, '\0', 1024);
576
577         int year, month, day, hour, min, sec;
578         year = month = day = hour = min = sec = 0;
579
580         time(&current_time);
581         printf("current time: %s\n", ctime(&current_time));
582
583         localtime_r(&current_time, &current_tm);
584         alarm_info = alarmmgr_create_alarm();
585         assert(alarm_info != NULL);
586
587 #if AUTO_TEST
588         test_time.year = current_tm.tm_year;
589         test_time.month = current_tm.tm_mon;
590         test_time.day = current_tm.tm_mday;
591
592         test_time.hour = current_tm.tm_hour;
593         test_time.min = current_tm.tm_min + 1;
594         test_time.sec = 5;
595 #else
596         printf("Enter Year of Alarm Expiry: ");
597         year = __get_integer_input_data();
598
599         printf("Enter month of Alarm Expiry: ");
600         month = __get_integer_input_data();
601
602         printf("Enter day of Alarm Expiry: ");
603         day = __get_integer_input_data();
604
605         printf("Enter hour of Alarm Expiry: ");
606         hour = __get_integer_input_data();
607
608         printf("Enter minute of Alarm Expiry: ");
609         min = __get_integer_input_data();
610
611         printf("Enter second Alarm Expiry: ");
612         sec = __get_integer_input_data();
613
614         test_time.year = year;
615         test_time.month = month;
616         test_time.day = day;
617         test_time.hour = hour;
618         test_time.min = min;
619         test_time.sec = sec;
620
621         printf
622             ("Note: Test Program will exit After "
623              "[Year:%d Month:%d Day:%d at Hour:%d Min:%d Sec:%d]."
624              " Please wait....\n\n", year, month, day, hour, min, sec);
625 #endif
626         result = alarmmgr_set_time(alarm_info, test_time);
627         __print_api_return_result(result, "alarmmgr_set_time");
628
629 #if AUTO_TEST
630         repeat_mode = ALARM_REPEAT_MODE_WEEKLY;
631 #else
632         repeat_mode = __get_alarm_repeat_mode();
633 #endif
634         result = alarmmgr_set_repeat_mode(alarm_info, repeat_mode,
635                                           ALARM_WDAY_SUNDAY | ALARM_WDAY_MONDAY
636                                           | ALARM_WDAY_TUESDAY |
637                                           ALARM_WDAY_WEDNESDAY |
638                                           ALARM_WDAY_THURSDAY |
639                                           ALARM_WDAY_FRIDAY |
640                                           ALARM_WDAY_SATURDAY);
641         __print_api_return_result(result, "alarmmgr_set_repeat_mode");
642
643 #if AUTO_TEST
644         type = (random() % 2) + 1;
645 #else
646         type = __get_alarm_persistance_type();
647 #endif
648         result = alarmmgr_set_type(alarm_info, type);
649         __print_api_return_result(result, "alarmmgr_set_type");
650
651         b = bundle_create();
652
653         assert(b != NULL);
654
655 #if AUTO_TEST
656         op_type = 1;
657 #else
658         printf("Enter APPSVC Operation\n");
659         printf("1. APPSVC_OPERATION_DEFAULT \n");
660         printf("2. APPSVC_OPERATION_SEARCH\n");
661         printf("3. No operation\n");
662         op_type = __get_integer_input_data();
663 #endif
664         switch (op_type) {
665         case 1:{
666                         appsvc_set_operation(b, APPSVC_OPERATION_DEFAULT);
667                         break;
668                 }
669         case 2:{
670                         appsvc_set_operation(b, APPSVC_OPERATION_SEARCH);
671                         break;
672                 }
673         case 3:
674                 {
675                         printf("NO APPSVC OPERATION!!!\n");
676                         break;
677                 }
678         default:{
679                         printf("Invalid option....Try Again\n");
680                         exit(-1);
681                 }
682
683         }
684
685 #if AUTO_TEST
686         sprintf(pkgname, "com.samsung.gallery");
687 #else
688
689         printf
690             ("Enter Pkgname: [Ex:com.samsung.alarm-test "
691              "or com.samsung.gallery...]\n");
692         if (fgets(pkgname, 1024, stdin) == NULL) {
693                 printf("Input buffer overflow....,try again\n");
694                 exit(-1);
695         }
696
697         pkgname[strlen(pkgname) - 1] = '\0';
698 #endif
699         appsvc_set_pkgname(b, pkgname);
700
701         result =
702             alarmmgr_add_alarm_appsvc_with_localtime(alarm_info, (void *)b,
703                                                      &alarm_id);
704         __print_api_return_result(result,
705                                   "alarmmgr_add_alarm_appsvc_with_localtime");
706
707         printf("Alarm created with alarm id %d\n", alarm_id);
708
709         cleanup_req = true;
710
711         pgm_exit_time = 70000;
712
713         printf
714             ("Note: Test Program will exit After %f "
715              "seconds...Please wait....\n\n", (float)pgm_exit_time / 1000);
716         printf
717             ("Upon Alarm Expiry, %s application will be "
718              "launched (purpose:testing)\n", pkgname);
719
720 }
721
722 /*Function to create alarm with a delay for first expiry and
723 alarm expiry inteval for subsequent alarm (with app-svc information)*/
724 static void __create_app_svc_interval_alarm(void)
725 {
726         time_t current_time = { 0, };
727         int result = 0;
728         int delay = 0;
729         int interval = 0;
730         int type = 0;
731         bundle *b = NULL;
732         int op_type = 0;
733
734         char pkgname[1024] = { 0, };
735
736         memset(pkgname, '\0', 1024);
737
738         time(&current_time);
739 #if AUTO_TEST
740         int i = 0;
741         delay = (random() % 9) + 1;
742         interval = (random() % 9);
743 #else
744         printf("current time: %s\n", ctime(&current_time));
745         printf("Enter First Delay (in seconds) for alarm: \n");
746         delay = __get_integer_input_data();
747         printf("Enter Alarm repeat Interval in seconds (if 0, no repeat): \n");
748         interval = __get_integer_input_data();
749 #endif
750         if (interval > 0) {
751                 cleanup_req = true;
752         }
753         pgm_exit_time = ((delay + interval) * 1000) + 100;
754         printf
755             ("Note: Test Program will exit After %f "
756              "seconds...Please wait....\n\n", (float)pgm_exit_time / 1000);
757
758 #if AUTO_TEST
759         type = (random() % 2) + 1;
760 #else
761         type = __get_alarm_persistance_type();
762 #endif
763
764         b = bundle_create();
765
766         assert(b != NULL);
767
768 #if AUTO_TEST
769         op_type = 1;
770 #else
771         printf("Enter APPSVC Operation\n");
772         printf("1. APPSVC_OPERATION_DEFAULT \n");
773         printf("2. APPSVC_OPERATION_SEARCH\n");
774         printf("3. No operation\n");
775         op_type = __get_integer_input_data();
776 #endif
777
778         switch (op_type) {
779         case 1:{
780                         appsvc_set_operation(b, APPSVC_OPERATION_DEFAULT);
781                         break;
782                 }
783         case 2:{
784                         appsvc_set_operation(b, APPSVC_OPERATION_SEARCH);
785                         break;
786                 }
787         case 3:
788                 {
789                         printf("NO APPSVC OPERATION!!!\n");
790                         break;
791                 }
792         default:{
793                         printf("Invalid option....Try Again\n");
794                         exit(-1);
795                 }
796
797         }
798
799
800 #if AUTO_TEST
801         sprintf(pkgname, "com.samsung.gallery");
802 #else
803         printf
804             ("Enter Pkgname: [Ex:com.samsung.alarm-test "
805              "or com.samsung.gallery...]\n");
806         if (fgets(pkgname, 1024, stdin) == NULL) {
807                 printf("Input buffer overflow...Try Again.\n");
808                 exit(-1);
809         }
810         pkgname[strlen(pkgname) - 1] = '\0';
811 #endif
812         appsvc_set_pkgname(b, pkgname);
813
814 #if AUTO_TEST
815         multiple_alarm = true;
816         for (i = 0; i < ITER_COUNT; i++) {
817                 interval++;
818                 delay++;
819                 result =
820                     alarmmgr_add_alarm_appsvc(type, delay, interval, (void *)b,
821                                               &alarm_id_arr[i]);
822                 __print_api_return_result(result, "alarmmgr_add_alarm");
823         }
824 #else
825         result =
826             alarmmgr_add_alarm_appsvc(type, delay, interval, (void *)b,
827                                       &alarm_id);
828         __print_api_return_result(result, "alarmmgr_add_alarm_appsvc");
829         printf("Created Alarm with Alarm ID %d\n", alarm_id);
830 #endif
831
832         if (interval > 0) {
833                 cleanup_req = true;
834         }
835         pgm_exit_time = ((delay + interval) * 1000) + 100;
836         printf
837             ("Note: Test Program will exit After %f seconds..."
838              "Please wait....\n\n", (float)pgm_exit_time / 1000);
839
840         return;
841 }
842
843 /*This function will be executed before program exit (if required)*/
844 static void __test_pgm_clean_up(void)
845 {
846         int result = 0;
847         printf("Cleaning up test program\n");
848 #if AUTO_TEST
849         int i = 0;
850         if (multiple_alarm == true) {
851                 for (i = 0; i < ITER_COUNT; i++) {
852                         result = alarmmgr_remove_alarm(alarm_id_arr[i]);
853                         __print_api_return_result(result,
854                                                   "alarmmgr_remove_alarm");
855                 }
856                 multiple_alarm = false;
857         } else {
858                 result = alarmmgr_remove_alarm(alarm_id);
859                 __print_api_return_result(result, "alarmmgr_remove_alarm");
860         }
861 #else
862         result = alarmmgr_remove_alarm(alarm_id);
863         __print_api_return_result(result, "alarmmgr_remove_alarm");
864 #endif
865         return;
866 }
867
868 /*Function to exit gmainloop*/
869 static gboolean __timout_handler(gpointer user_data)
870 {
871         if (mainloop)
872                 g_main_loop_quit(mainloop);
873
874         return false;
875 }
876
877 /*Main alarm create function which provides options
878  for different types of alarm creation*/
879 static void __test_alarm_manager_callback(void)
880 {
881         printf("Inside Alarm Callback test\n");
882         int result;
883
884         mainloop = g_main_loop_new(NULL, FALSE);
885         assert(mainloop != NULL);
886
887         result = alarmmgr_init("com.samsung.alarm-test");
888         __print_api_return_result(result, "alarmmgr_init");
889
890         result = alarmmgr_set_cb(__alarm_callback, NULL);
891         __print_api_return_result(result, "alarmmgr_set_cb");
892         result = alarmmgr_set_cb(__alarm_callback1, NULL);
893         __print_api_return_result(result, "alarmmgr_set_cb");
894
895         __create_interval_alarm();
896
897         g_timeout_add(pgm_exit_time, __timout_handler, NULL);
898
899         g_main_loop_run(mainloop);
900
901         if (cleanup_req) {
902                 __test_pgm_clean_up();
903         }
904
905         return;
906 }
907
908 /*Main alarm create function which provides options
909  for different types of alarm creation*/
910 static void __test_alarm_manager_create_alarm(void)
911 {
912         int result = 0;
913         int alarm_type = 0;
914
915 #if AUTO_TEST
916         alarm_type = sub_type;
917 #else
918         char destination[1024] = { 0, };
919
920         printf("Enter Alarm Type\n");
921         printf("1. Interval with initial delay\n");
922         printf("2. App-svc Interval alarm with initial delay\n");
923         printf("3. Calendar Alarm\n");
924         printf("4. App-svc Calendar Alarm\n");
925         printf("Enter Choice between [1-4]....\n");
926         alarm_type = __get_integer_input_data();
927 #endif
928         mainloop = g_main_loop_new(NULL, FALSE);
929         assert(mainloop != NULL);
930
931         switch (alarm_type) {
932         case 1:
933                 {
934 #if AUTO_TEST
935                         result = alarmmgr_init("com.samsung.1000");
936 #else
937                         printf
938                             ("Enter pkgname (used to generate service name)"
939                              "[Ex:com.samsung.alarm-test or "
940                              "com.samsung.1000 etc]\n");
941                         if (fgets(destination, 1024, stdin) == NULL) {
942                                 printf("Input buffer overflow....,try again\n");
943                                 exit(-1);
944                         }
945
946                         destination[strlen(destination) - 1] = '\0';
947
948                         result = alarmmgr_init(destination);
949 #endif
950                         __print_api_return_result(result, "alarmmgr_init");
951
952                         result = alarmmgr_set_cb(__alarm_callback, NULL);
953                         __print_api_return_result(result, "alarmmgr_set_cb");
954
955                         __create_interval_alarm();
956
957                         break;
958                 }
959         case 2:
960                 {
961                         printf("Inside APPSVC Interval Alarm test\n");
962                         __create_app_svc_interval_alarm();
963                         break;
964                 }
965         case 3:
966                 {
967                         printf("Inside Calendar Alarm test\n");
968 #if AUTO_TEST
969                         result = alarmmgr_init("com.samsung.1000");
970 #else
971                         printf
972                             ("Enter pkgname (used to generate service name)"
973                              "[Ex:com.samsung.alarm-test or "
974                              "com.samsung.1000 etc]\n");
975                         if (fgets(destination, 1024, stdin) == NULL) {
976                                 printf("Input buffer overflow....,try again\n");
977                                 exit(-1);
978                         }
979
980                         destination[strlen(destination) - 1] = '\0';
981
982                         result = alarmmgr_init(destination);
983 #endif
984                         __print_api_return_result(result, "alarmmgr_init");
985
986                         result = alarmmgr_set_cb(__alarm_callback, NULL);
987                         __print_api_return_result(result, "alarmmgr_set_cb");
988
989                         __create_calendar_alarm();
990                         break;
991                 }
992         case 4:
993                 {
994                         printf("Inside APPSVC Calendar Alarm test\n");
995                         __create_app_svc_calendar_alarm();
996                         break;
997                 }
998         default:
999                 {
1000                         printf("Invalid option....Try Again\n");
1001                         return;
1002                 }
1003         }
1004
1005         g_timeout_add(pgm_exit_time, __timout_handler, NULL);
1006
1007         g_main_loop_run(mainloop);
1008
1009         if (cleanup_req) {
1010                 __test_pgm_clean_up();
1011         }
1012
1013         return;
1014 }
1015
1016 /*Main alarm get information function which provides
1017  different options of getting alarm information*/
1018 static void __test_alarm_manager_get_information(void)
1019 {
1020
1021         printf("Inside Get Info Interval Alarm test\n");
1022         int option = 0;
1023         int result = 0;
1024         alarm_entry_t *alarm_info;
1025         int type = 0;
1026
1027         alarm_date_t time = { 0, };
1028
1029 #if AUTO_TEST
1030         option = 2;
1031 #else
1032         printf("1. Get Existing Alarm's Information\n");
1033         printf("2. Create Alarm & Get Information\n");
1034         option = __get_integer_input_data();
1035 #endif
1036         alarm_info = alarmmgr_create_alarm();
1037         assert(alarm_info != NULL);
1038
1039         switch (option) {
1040         case 1:
1041                 {
1042                         printf("Enter Alarm Id: \n");
1043                         alarm_id = __get_integer_input_data();
1044                         result = alarmmgr_get_info(alarm_id, alarm_info);
1045                         __print_api_return_result(result, "alarmmgr_get_info");
1046                         result = alarmmgr_get_time(alarm_info, &time);
1047                         __print_api_return_result(result, "alarmmgr_get_time");
1048                         printf("day is %d\n", time.day);
1049                         printf("Alarm Get Info Success\n");
1050                         break;
1051                 }
1052         case 2:
1053                 {
1054                         result = alarmmgr_init("com.samsung.10000");
1055                         __print_api_return_result(result, "alarmmgr_init");
1056
1057 #if AUTO_TEST
1058                         type = (random() % 2) + 1;
1059 #else
1060                         type = __get_alarm_persistance_type();
1061 #endif
1062
1063                         result =
1064                             alarmmgr_add_alarm(type, 2, 0, NULL, &alarm_id);
1065                         __print_api_return_result(result, "alarmmgr_add_alarm");
1066                         printf("Created Alarm with Alarm ID %d\n", alarm_id);
1067
1068                         pgm_exit_time = 3000;
1069                         g_timeout_add(pgm_exit_time, __timout_handler, NULL);
1070
1071                         result = alarmmgr_get_info(alarm_id, alarm_info);
1072                         __print_api_return_result(result, "alarmmgr_get_info");
1073
1074                         alarm_date_t time = { 0, };
1075                         result = alarmmgr_get_time(alarm_info, &time);
1076                         __print_api_return_result(result, "alarmmgr_get_time");
1077
1078                         printf("day is %d\n", time.day);
1079                         printf("Alarm Get Info Success\n");
1080
1081                         __test_pgm_clean_up();
1082
1083                         break;
1084                 }
1085         default:
1086                 {
1087                         printf("Invalid option\n");
1088                         exit(-1);
1089                 }
1090         }
1091         return;
1092 }
1093
1094 static void __test_alarm_manager_get_appsvc_information(void)
1095 {
1096
1097
1098         printf("Inside Get Info Interval Alarm test\n");
1099         int option = 0;
1100         int result = 0;
1101         alarm_entry_t *alarm_info;
1102         bundle *b = NULL;
1103
1104 #if AUTO_TEST
1105         option = 2;
1106 #else
1107         printf("1. Get Existing Alarm's Information\n");
1108         printf("2. Create Alarm & Get Information\n");
1109         option = __get_integer_input_data();
1110 #endif
1111         alarm_info = alarmmgr_create_alarm();
1112         assert(alarm_info != NULL);
1113
1114         switch (option) {
1115         case 1:
1116                 {
1117                         printf("Enter Alarm ID:\n");
1118                         alarm_id = __get_integer_input_data();
1119
1120                         b = alarmmgr_get_alarm_appsvc_info(alarm_id, &result);
1121
1122                         if (b)
1123                                 bundle_iterate(b, __print_appsvc_bundle_data, NULL);
1124
1125                         __print_api_return_result(result, "alarmmgr_get_alarm_appsvc_info");
1126
1127                         break;
1128                 }
1129         case 2:
1130                 {
1131                         mainloop = g_main_loop_new(NULL, FALSE);
1132                         assert(mainloop != NULL);
1133
1134                         __create_app_svc_interval_alarm();
1135
1136                         b = alarmmgr_get_alarm_appsvc_info(alarm_id, &result);
1137                         __print_api_return_result(result, "alarmmgr_get_alarm_appsvc_info");
1138                         if (b)
1139                                 bundle_iterate(b, __print_appsvc_bundle_data, NULL);
1140
1141                         g_timeout_add(2000, __timout_handler, NULL);
1142
1143                         g_main_loop_run(mainloop);
1144
1145                         if (cleanup_req) {
1146                                 __test_pgm_clean_up();
1147
1148                         break;
1149                 }
1150         default:
1151                 {
1152                         printf("Invalid option\n");
1153                         exit(-1);
1154                 }
1155         }
1156         }
1157
1158         return;
1159 }
1160
1161 static int __alarm_id_retrive_callback(alarm_id_t id, void *user_param)
1162 {
1163         int *n = NULL;
1164         n = (int *)user_param;
1165         if (n == NULL) {
1166                 printf("Invalid Parameter in test program");
1167                 return -1;
1168         }
1169         printf("[%d]Alarm ID is %d\n", *n, id);
1170         (*n)++;
1171
1172         return 0;
1173 }
1174
1175 static void __test_alarm_manager_get_alarm_ids(void)
1176 {
1177         printf("Inside Get Ids Alarm test\n");
1178         int option = 0;
1179         int result = 0;
1180         int n = 1;
1181         int type = 0;
1182 #if AUTO_TEST
1183         option = 2;
1184 #else
1185         printf("1. Get Existing Alarm's ID\n");
1186         printf("2. Create Alarm & Get ID\n");
1187
1188         option = __get_integer_input_data();
1189 #endif
1190         switch (option) {
1191         case 1:
1192                 {
1193                         result = alarmmgr_init("com.samsung.1000");
1194                         __print_api_return_result(result, "alarmmgr_init");
1195                         result =
1196                             alarmmgr_enum_alarm_ids(__alarm_id_retrive_callback,
1197                                                     (void *)&n);
1198                         __print_api_return_result(result,
1199                                                   "alarmmgr_enum_alarm_ids");
1200                         break;
1201                 }
1202         case 2:
1203                 {
1204                         result = alarmmgr_init("com.samsung.test");
1205                         __print_api_return_result(result, "alarmmgr_init");
1206
1207 #if AUTO_TEST
1208                         type = (random() % 2) + 1;
1209 #else
1210                         type = __get_alarm_persistance_type();
1211 #endif
1212                         result = alarmmgr_add_alarm(type, 2, 0,
1213                                                     NULL, &alarm_id);
1214                         printf("Created Alarm with Alarm ID %d\n", alarm_id);
1215
1216                         __print_api_return_result(result, "alarmmgr_add_alarm");
1217
1218                         result = alarmmgr_add_alarm(type, 3, 0,
1219                                                     NULL, &alarm_id);
1220                         __print_api_return_result(result, "alarmmgr_add_alarm");
1221                         printf("Created Alarm with Alarm ID %d\n", alarm_id);
1222
1223                         pgm_exit_time = 5100;
1224                         g_timeout_add(pgm_exit_time, __timout_handler, NULL);
1225
1226                         result =
1227                             alarmmgr_enum_alarm_ids(__alarm_id_retrive_callback,
1228                                                     (void *)&n);
1229                         __print_api_return_result(result,
1230                                                   "alarmmgr_enum_alarm_ids");
1231
1232                         break;
1233                 }
1234         default:
1235                 {
1236                         printf("Invalid option\n");
1237                         exit(-1);
1238                 }
1239         }
1240         return;
1241 }
1242
1243 /*Main alarm delete test function which provides options
1244  for different types of alarm deletion*/
1245 static void __test_alarm_manager_delete_alarm(void)
1246 {
1247         int option = 0;
1248         int result = 0;
1249         int type = 0;
1250         printf("Inside Delete Alarm test\n");
1251
1252 #if AUTO_TEST
1253         option = 2;
1254 #else
1255         printf("1. Delete Existing Alarm\n");
1256         printf("2. Create Alarm & Delete\n");
1257
1258         option = __get_integer_input_data();
1259 #endif
1260         switch (option) {
1261         case 1:
1262                 {
1263                         printf("Enter Alarm Id: \n");
1264                         option = __get_integer_input_data();
1265
1266                         result = alarmmgr_remove_alarm(option);
1267                         __print_api_return_result(result,
1268                                                   "alarmmgr_remove_alarm");
1269                         break;
1270                 }
1271         case 2:
1272                 {
1273                         result = alarmmgr_init("com.samsung.test");
1274                         __print_api_return_result(result, "alarmmgr_init");
1275
1276 #if AUTO_TEST
1277                         type = (random() % 2) + 1;
1278 #else
1279                         type = __get_alarm_persistance_type();
1280 #endif
1281                         result = alarmmgr_add_alarm(type, 2, 0,
1282                                                     NULL, &alarm_id);
1283                         __print_api_return_result(result, "alarmmgr_add_alamr");
1284                         printf("Created Alarm with Alarm ID %d\n", alarm_id);
1285
1286                         result = alarmmgr_remove_alarm(alarm_id);
1287                         __print_api_return_result(result,
1288                                                   "alarmmgr_remove_alarm");
1289
1290                         break;
1291                 }
1292         default:
1293                 {
1294                         printf("Invalid Option\n");
1295                         break;
1296                 }
1297         }
1298         return;
1299 }
1300
1301 /*Entry point of the test program*/
1302 int main(int argc, char **argv)
1303 {
1304 #if AUTO_TEST
1305         int i = 0;
1306         int k = 0;
1307         int iteration = 0;
1308
1309         int fd = 0;
1310
1311         /*test result will be written into below file */
1312         fd = open("/var/alarm_test.log", O_CREAT | O_RDWR | O_APPEND, 0666);
1313         if (fd < 0) {
1314                 printf("Unable to create log file\n");
1315                 return -1;
1316         }
1317         if (argc < 3) {
1318                 printf("Insufficient arguments\n");
1319                 printf
1320                     ("Example: ./test_alarmmgr.exe {all|create_interval|"
1321                      "create_svc_interval|create_calendar|create_svc_calendar|"
1322                      "delete|get_id|callback} <iteration>");
1323                 return -1;
1324         }
1325
1326         close(1);
1327         close(2);
1328         dup(fd);
1329         dup(fd);
1330
1331         char *test_job = NULL;
1332
1333         test_job = argv[1];
1334         iteration = atoi(argv[2]);
1335
1336         printf("##################Alarm Test for %s Starts##################\n",
1337                test_job);
1338         g_type_init();
1339
1340         for (i = 0; i < iteration; i++) {
1341                 printf("########ITERATION %d #############\n", i);
1342                 if (strncmp(test_job, "create_interval", 15) == 0) {
1343                         sub_type = INTERVAL;
1344                         __test_alarm_manager_create_alarm();
1345                 } else if (strncmp(test_job, "create_svc_interval", 19) == 0) {
1346                         sub_type = INTERVAL_APPSVC;
1347                         __test_alarm_manager_create_alarm();
1348                 } else if (strncmp(test_job, "create_calendar", 15) == 0) {
1349                         sub_type = CALENDAR;
1350                         __test_alarm_manager_create_alarm();
1351                 } else if (strncmp(test_job, "create_svc_calendar", 19) == 0) {
1352                         sub_type = CALENDAR_APPSVC;
1353                         __test_alarm_manager_create_alarm();
1354                 } else if (strncmp(test_job, "delete", 6) == 0) {
1355                         __test_alarm_manager_delete_alarm();
1356                 } else if (strncmp(test_job, "get_info", 8) == 0) {
1357                         __test_alarm_manager_get_information();
1358                 } else if (strncmp(test_job, "get_id", 6) == 0) {
1359                         __test_alarm_manager_get_alarm_ids();
1360                 } else if (strncmp(test_job, "callback", 8) == 0) {
1361                         __test_alarm_manager_callback();
1362                 } else if (strncmp(test_job, "all", 3) == 0) {
1363                         for (k = INTERVAL; k <= CALENDAR_APPSVC; k++) {
1364                                 sub_type = k;
1365                                 __test_alarm_manager_create_alarm();
1366                         }
1367                         __test_alarm_manager_delete_alarm();
1368                         __test_alarm_manager_get_information();
1369                         __test_alarm_manager_get_alarm_ids();
1370                         __test_alarm_manager_get_alarm_ids();
1371                         __test_alarm_manager_callback();
1372
1373                 } else {
1374                         printf("Invalid Option\n");
1375                         return -1;
1376                 }
1377         }
1378         printf("##################Alarm Test for %s Ends##################\n\n",
1379                test_job);
1380         close(fd);
1381
1382 #else
1383         /*Compile with disabling AUTO_TEST flag for individual testing */
1384         int option = 0;
1385
1386         g_type_init();
1387
1388         printf("*********ALARM MANAGER TESTING************\n");
1389         printf("1. Test Alarm-Manager: Create Alarm\n");
1390         printf("2. Test Alarm-Manager: Delete Alarm\n");
1391         printf("3. Test Alarm-Manager: Get Alarm Information\n");
1392         printf("4. Test Alarm-Manager: Get Alarm ID\n");
1393         printf("5. Test Alarm-Manager: Callback\n");
1394         printf("6. Test Alarm-Manager: Get Alarm app-svc(bundle) Information\n");
1395         printf("7. Test Alarm-Manager: Set RTC Wakeup\n");
1396         printf("Enter Choice between [1-7]....\n");
1397
1398         option = __get_integer_input_data();
1399         switch (option) {
1400         case 1:
1401                 {
1402                         __test_alarm_manager_create_alarm();
1403                         break;
1404                 }
1405         case 2:
1406                 {
1407                         __test_alarm_manager_delete_alarm();
1408                         break;
1409                 }
1410         case 3:
1411                 {
1412                         __test_alarm_manager_get_information();
1413                         break;
1414                 }
1415         case 4:
1416                 {
1417                         __test_alarm_manager_get_alarm_ids();
1418                         break;
1419                 }
1420         case 5:
1421                 {
1422                         __test_alarm_manager_callback();
1423                         break;
1424                 }
1425         case 6:
1426                 {
1427                         __test_alarm_manager_get_appsvc_information();
1428                         break;
1429                 }
1430         case 7:
1431                 {
1432                         __test_alarm_manager_set_rtc_wakeup_phone();
1433                         break;
1434                 }
1435         default:
1436                 {
1437                         printf("Invalid Input!!! Retry again\n");
1438                 }
1439
1440         }
1441         printf("****************************************\n");
1442 #endif
1443         return 0;
1444 }