Support multiple instance launch
[platform/core/appfw/librua.git] / test / rua-test.c
1 /*
2  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <getopt.h>
19 #include <time.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <glib.h>
25
26 /* For multi-user support */
27 #include <tzplatform_config.h>
28
29 #include "rua.h"
30 #include "rua_internal.h"
31 #include "rua_stat.h"
32 #include "rua_stat_internal.h"
33
34 static int __add_history()
35 {
36         int ret;
37         char *app_path;
38         char *pkgname = "org.tizen.ruatester";
39
40         app_path = (char *)tzplatform_mkpath(TZ_SYS_RW_APP, pkgname);
41         ret = rua_add_history_for_uid(pkgname, app_path, NULL, 5001);
42         return ret;
43 }
44
45 static int __delete_history_with_pkgname()
46 {
47         int ret;
48         char *pkgname = "org.tizen.ruatester";
49
50         ret = rua_delete_history_with_pkgname_for_uid(pkgname, 5001);
51         return ret;
52 }
53
54 static int __load_rua_history()
55 {
56         char **table = NULL;
57         int rows = 0;
58         int cols = 0;
59         struct rua_rec record;
60
61         if (rua_history_load_db_for_uid(&table, &rows, &cols, 5001) || !table) {
62                 printf("fail to load rua history \n");
63                 return -1;
64         }
65
66         int row;
67         for (row = 0; row < rows; ++row) {
68                 rua_history_get_rec(&record, table, rows, cols, row);
69                 printf("pkgname : %s, time : %d \n", record.pkg_name, (int)record.launch_time);
70         }
71
72         rua_history_unload_db(&table);
73         return 0;
74 }
75
76 static int __update_stat()
77 {
78         int ret;
79
80         ret = rua_stat_update_for_uid("ruacaller", "org.tizen.ruatester", 5001);
81         return ret;
82 }
83
84 static int __rua_stat_tag_iter_cb(const char *rua_stat_tag, void *data)
85 {
86         printf("rua_stat_tag : %s \n", rua_stat_tag);
87
88         return 0;
89 }
90
91 static int __get_stat_tags()
92 {
93         int ret;
94         ret = rua_stat_get_stat_tags_for_uid("ruacaller", __rua_stat_tag_iter_cb, NULL, 5001);
95         return ret;
96 }
97
98 static gboolean run_test(int selected_number)
99 {
100         gboolean go_to_loop = TRUE;
101
102         switch (selected_number) {
103         case 0:
104                 go_to_loop = FALSE;
105                 break;
106
107         case 1:
108                 __add_history();
109                 break;
110
111         case 2:
112                 __delete_history_with_pkgname();
113                 break;
114
115         case 3:
116                 __load_rua_history();
117                 break;
118
119         case 4:
120                 __update_stat();
121                 break;
122
123         case 5:
124                 __get_stat_tags();
125                 break;
126
127         default:
128                 break;
129         }
130
131         return go_to_loop;
132
133 }
134
135 int main()
136 {
137         int ret = 0;
138         int test_num;
139         gboolean run_next = TRUE;
140
141         while(run_next) {
142                 printf("==========================================\n");
143                 printf("    Basic test menu \n");
144                 printf("==========================================\n");
145                 printf(" 0.  EXIT\n");
146                 printf(" 1.  Add rua history to DEFAULT USER(5001)\n");
147                 printf(" 2.  Delete history with pkgname\n");
148                 printf(" 3.  Load RUA history\n");
149                 printf(" 4.  Update RUA stat\n");
150                 printf(" 5.  Get RUA stat tags\n");
151                 printf("------------------------------------------\n");
152                 ret = scanf("%d", &test_num);
153                 if (ret < 0) {
154                         printf("scanf fail %d", ret);
155                         break;
156                 }
157
158                 run_next = run_test(test_num);
159         }
160         return ret;
161 }