summaryrefslogtreecommitdiff
path: root/test/rua-test.c
blob: 5aa36198226be293af78a9562b2ffb7c22ae1335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <getopt.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <glib.h>
#include <pthread.h>

/* For multi-user support */
#include <tzplatform_config.h>

#include "rua.h"
#include "rua_internal.h"
#include "rua_stat.h"
#include "rua_stat_internal.h"

static GMainLoop *mainloop = NULL;
static pthread_t g_thread;

static int callback_id_table[10] = { 0, };

static gboolean run_test(int selected_number);
static void *__print_menu(void *arg)
{
	int test_num = 0;
	int run_next = 1;

	while(run_next) {
		printf("==========================================\n");
		printf("    Basic test menu \n");
		printf("==========================================\n");
		printf(" 0.  EXIT\n");
		printf(" 1.  Add rua history to DEFAULT USER(5001)\n");
		printf(" 2.  Delete history with pkgname\n");
		printf(" 3.  Load RUA history\n");
		printf(" 4.  Update RUA stat\n");
		printf(" 5.  Get RUA stat tags\n");
		printf(" 6.  Register update callback (MAX 10)\n");
		printf(" 7.  Unregister update callback\n");
		printf("------------------------------------------\n");
		int ret = scanf("%d", &test_num);
		if (ret < 0) {
			printf("scanf fail %d", ret);
			break;
		}

		run_next = run_test(test_num);
	}

	return NULL;
}

static void __update_cb(char **table, int rows, int cols, void *user_data)
{
	struct rua_rec record;

	printf("update_cb");
	int row;
	for (row = 0; row < rows; ++row) {
		rua_history_get_rec(&record, table, rows, cols, row);
		printf("pkgname : %s, time : %d \n", record.pkg_name, (int)record.launch_time);
	}

}

static int __add_history()
{
	int ret;
	char *app_path;
	char *pkgname = "org.tizen.ruatester";

	app_path = (char *)tzplatform_mkpath(TZ_SYS_RW_APP, pkgname);
	ret = rua_add_history_for_uid(pkgname, app_path, NULL, 5001);
	return ret;
}

static int __delete_history_with_pkgname()
{
	int ret;
	char *pkgname = "org.tizen.ruatester";

	ret = rua_delete_history_with_pkgname_for_uid(pkgname, 5001);
	return ret;
}

static int __load_rua_history()
{
	char **table = NULL;
	int rows = 0;
	int cols = 0;
	struct rua_rec record;

	if (rua_history_load_db_for_uid(&table, &rows, &cols, 5001) || !table) {
		printf("fail to load rua history \n");
		return -1;
	}

	int row;
	for (row = 0; row < rows; ++row) {
		rua_history_get_rec(&record, table, rows, cols, row);
		printf("pkgname : %s, time : %d \n", record.pkg_name, (int)record.launch_time);
	}

	rua_history_unload_db(&table);
	return 0;
}

static int __update_stat()
{
	int ret;

	ret = rua_stat_update_for_uid("ruacaller", "org.tizen.ruatester", 5001);
	return ret;
}

static int __rua_stat_tag_iter_cb(const char *rua_stat_tag, void *data)
{
	printf("rua_stat_tag : %s \n", rua_stat_tag);

	return 0;
}

static int __get_stat_tags()
{
	int ret;
	ret = rua_stat_get_stat_tags_for_uid("ruacaller", __rua_stat_tag_iter_cb, NULL, 5001);
	return ret;
}

static int __register_callback()
{
	int ret;

	int i = 0;
	for (i = 0; i < 10; i++) {
		if (callback_id_table[i] == 0) {
			int output = 0;
			ret = rua_register_update_cb(__update_cb, NULL, &output);
			if (ret == -1) {
				printf("__register_callback error : %d \n", ret);
				return -1;
			}

			printf("callback id : %d \n", output);
			callback_id_table[i] = output;

			return 0;
		}
	}

	return -1;
}

static int __unregister_callback()
{
	int ret;

	int i = 0;
	int id = 0;
	printf("Enter callback_id (The number from register_callback) : ");
	ret = scanf("%d", &id);
	if (ret < 0) {
		printf("scanf fail %d", ret);
		return -1;
	}


	for (i = 0; i < 10; i++) {
		if (callback_id_table[i] == id) {
			ret = rua_unregister_update_cb(callback_id_table[i]);
			if (ret == -1)
				printf("__unregister_callback : %d \n", ret);

			callback_id_table[i] = 0;
			return 0;
		}
	}

	return -1;
}


static gboolean run_test(int selected_number)
{
	int r = 0;
	gboolean loop = TRUE;
	switch (selected_number) {
	case 0:
		g_main_loop_quit(mainloop);
		loop = FALSE;
		break;

	case 1:
		__add_history();
		break;

	case 2:
		__delete_history_with_pkgname();
		break;

	case 3:
		__load_rua_history();
		break;

	case 4:
		__update_stat();
		break;

	case 5:
		__get_stat_tags();
		break;

	case 6:
		r = __register_callback();
		if (r == -1)
			printf("table is full");
		break;

	case 7:
		r = __unregister_callback();
		if (r == -1)
			printf("id is wrong");

		break;

	default:
		break;
	}



	return loop;
}

int main()
{
	int ret = 0;

	mainloop = g_main_loop_new(NULL, FALSE);

	int result = pthread_create(&g_thread, NULL, __print_menu, (void *)NULL);
	if (result < 0)
	{
		printf("pthread_create failed in initialize\n");
		return 0;
	}

	g_main_loop_run(mainloop);
	g_main_loop_unref(mainloop);
	mainloop = NULL;

	return ret;
}