Apply tizen coding rule
[platform/core/multimedia/libmedia-service.git] / test / media-service-test.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <media-svc.h>
25 #include <media-svc-noti.h>
26
27 #define SAFE_FREE(src)  { if (src) { free(src); src = NULL; } }
28
29 GMainLoop *g_loop = NULL;
30 MediaSvcHandle *g_db_handle = NULL;
31
32 void _noti_cb(int pid,
33                 media_item_type_e update_item,
34                 media_item_update_type_e update_type,
35                 char *path,
36                 char *uuid,
37                 media_type_e content_type,
38                 char *mime_type,
39                 void *user_data)
40 {
41         media_svc_debug("Noti from PID(%d)", pid);
42
43         if (update_item == MS_MEDIA_ITEM_FILE) {
44                 media_svc_debug("Noti item : MS_MEDIA_ITEM_FILE");
45         } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
46                 media_svc_debug("Noti item : MS_MEDIA_ITEM_DIRECTORY");
47         }
48
49         if (update_type == MS_MEDIA_ITEM_INSERT) {
50                 media_svc_debug("Noti type : MS_MEDIA_ITEM_INSERT");
51         } else if (update_type == MS_MEDIA_ITEM_DELETE) {
52                 media_svc_debug("Noti type : MS_MEDIA_ITEM_DELETE");
53         } else if (update_type == MS_MEDIA_ITEM_UPDATE) {
54                 media_svc_debug("Noti type : MS_MEDIA_ITEM_UPDATE");
55         }
56
57         /*media_svc_debug("content type : %d", content_type); */
58         printf("content type : %d\n", content_type);
59
60         if (path)
61                 printf("path : %s\n", path);
62         else
63                 printf("path not");
64
65         if (mime_type)
66                 printf("mime_type : %s", mime_type);
67         else
68                 printf("mime not");
69
70         if (user_data) printf("String : %s\n", (char *)user_data);
71         else
72                 printf("user not");
73
74         return;
75 }
76
77 #if 1
78 gboolean _send_noti_batch_operations(gpointer data)
79 {
80         int ret = MS_MEDIA_ERR_NONE;
81
82         /* First of all, noti subscription */
83         char *user_str = strdup("hi");
84         media_db_update_subscribe(_noti_cb, (void *)user_str);
85
86         /* 1. media_svc_insert_item_immediately */
87         char *path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");
88
89         media_svc_storage_type_e storage_type;
90
91         ret = media_svc_get_storage_type(path, &storage_type, tzplatform_getuid(TZ_USER_NAME));
92         if (ret != MS_MEDIA_ERR_NONE) {
93                 media_svc_error("media_svc_get_storage_type failed : %d (%s)", ret, path);
94                 SAFE_FREE(user_str);
95                 return FALSE;
96         }
97
98         int idx = 0;
99         char *file_list[10];
100
101         ret = media_svc_insert_item_begin(g_db_handle, 100, TRUE, getpid());
102         /*ret = media_svc_insert_item_begin(g_db_handle, 100); */
103         for (idx = 0; idx < 10; idx++) {
104                 char filepath[255] = {0, };
105                 snprintf(filepath, sizeof(filepath), "%s%d.jpg", tzplatform_mkpath(TZ_USER_CONTENT, "test/image"), idx + 1);
106                 media_svc_debug("File : %s\n", filepath);
107                 file_list[idx] = strdup(filepath);
108                 ret = media_svc_insert_item_bulk(g_db_handle, storage_type, file_list[idx], FALSE);
109                 if (ret != 0) {
110                         media_svc_error("media_svc_insert_item_bulk[%d] failed", idx);
111                 } else {
112                         media_svc_debug("media_svc_insert_item_bulk[%d] success", idx);
113                 }
114         }
115
116         ret = media_svc_insert_item_end(g_db_handle);
117
118         SAFE_FREE(user_str);
119         return FALSE;
120 }
121 #endif
122
123 gboolean _send_noti_operations(gpointer data)
124 {
125         int ret = MS_MEDIA_ERR_NONE;
126
127         /* First of all, noti subscription */
128         char *user_str = strdup("hi");
129         media_db_update_subscribe(_noti_cb, (void *)user_str);
130
131         /* 1. media_svc_insert_item_immediately */
132         char *path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");
133         media_svc_storage_type_e storage_type;
134
135         ret = media_svc_get_storage_type(path, &storage_type, tzplatform_getuid(TZ_USER_NAME));
136         if (ret != MS_MEDIA_ERR_NONE) {
137                 media_svc_error("media_svc_get_storage_type failed : %d (%s)", ret, path);
138                 SAFE_FREE(user_str);
139                 return FALSE;
140         }
141
142         ret = media_svc_insert_item_immediately(g_db_handle, storage_type, path);
143         if (ret != MS_MEDIA_ERR_NONE) {
144                 media_svc_error("media_svc_insert_item_immediately failed : %d", ret);
145                 SAFE_FREE(user_str);
146                 return FALSE;
147         }
148
149         media_svc_debug("media_svc_insert_item_immediately success");
150
151         /* 2. media_svc_refresh_item */
152         ret = media_svc_refresh_item(g_db_handle, storage_type, path);
153         if (ret != MS_MEDIA_ERR_NONE) {
154                 media_svc_error("media_svc_refresh_item failed : %d", ret);
155                 return FALSE;
156         }
157         media_svc_debug("media_svc_refresh_item success");
158
159         /* 2. media_svc_move_item */
160         const char *dst_path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image11.jpg");
161         ret = media_svc_move_item(g_db_handle, storage_type, path, storage_type, dst_path);
162         if (ret != MS_MEDIA_ERR_NONE) {
163                 media_svc_error("media_svc_move_item failed : %d", ret);
164                 return FALSE;
165         }
166         media_svc_debug("media_svc_move_item success");
167
168         ret = media_svc_move_item(g_db_handle, storage_type, dst_path, storage_type, path);
169         if (ret != MS_MEDIA_ERR_NONE) {
170                 media_svc_error("media_svc_move_item failed : %d", ret);
171                 return FALSE;
172         }
173         media_svc_debug("media_svc_move_item success");
174
175         /* 4. media_svc_delete_item_by_path */
176         ret = media_svc_delete_item_by_path(g_db_handle, path);
177         if (ret != MS_MEDIA_ERR_NONE) {
178                 media_svc_error("media_svc_delete_item_by_path failed : %d", ret);
179                 return FALSE;
180         }
181         media_svc_debug("media_svc_delete_item_by_path success");
182
183         /* Rename folder */
184         const char *src_folder_path = tzplatform_mkpath(TZ_USER_CONTENT, "test");
185         const char *dst_folder_path = tzplatform_mkpath(TZ_USER_CONTENT, "test_test");
186         ret = media_svc_rename_folder(g_db_handle, src_folder_path, dst_folder_path);
187         if (ret != MS_MEDIA_ERR_NONE) {
188                 media_svc_error("media_svc_rename_folder failed : %d", ret);
189                 return FALSE;
190         }
191         media_svc_debug("media_svc_rename_folder success");
192
193         /* Rename folder again */
194         ret = media_svc_rename_folder(g_db_handle, dst_folder_path, src_folder_path);
195         if (ret != MS_MEDIA_ERR_NONE) {
196                 media_svc_error("media_svc_rename_folder failed : %d", ret);
197                 return FALSE;
198         }
199         media_svc_debug("media_svc_rename_folder success");
200
201         return FALSE;
202 }
203
204 int test_noti()
205 {
206         GSource *source = NULL;
207         GMainContext *context = NULL;
208
209         g_loop = g_main_loop_new(NULL, FALSE);
210         context = g_main_loop_get_context(g_loop);
211         source = g_idle_source_new();
212 #if 0
213         g_source_set_callback(source, _send_noti_operations, NULL, NULL);
214 #else
215         g_source_set_callback(source, _send_noti_batch_operations, NULL, NULL);
216 #endif
217         g_source_attach(source, context);
218
219         g_main_loop_run(g_loop);
220
221         g_main_loop_unref(g_loop);
222         media_db_update_unsubscribe();
223
224         return MS_MEDIA_ERR_NONE;
225 }
226
227 int main()
228 {
229         int ret = MS_MEDIA_ERR_NONE;
230         ret = media_svc_connect(&g_db_handle, tzplatform_getuid(TZ_USER_NAME), true);
231         if (ret != MS_MEDIA_ERR_NONE) {
232                 media_svc_error("media_svc_connect failed : %d", ret);
233         } else {
234                 media_svc_debug("media_svc_connect success");
235         }
236
237         ret = test_noti();
238         if (ret < MS_MEDIA_ERR_NONE) {
239                 media_svc_error("test_noti failed : %d", ret);
240         } else {
241                 media_svc_debug("test_noti success");
242         }
243
244         media_svc_disconnect(g_db_handle);
245         return ret;
246 }
247