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         if (update_type == MS_MEDIA_ITEM_INSERT)
49                 media_svc_debug("Noti type : MS_MEDIA_ITEM_INSERT");
50         else if (update_type == MS_MEDIA_ITEM_DELETE)
51                 media_svc_debug("Noti type : MS_MEDIA_ITEM_DELETE");
52         else if (update_type == MS_MEDIA_ITEM_UPDATE)
53                 media_svc_debug("Noti type : MS_MEDIA_ITEM_UPDATE");
54
55         /*media_svc_debug("content type : %d", content_type); */
56         printf("content type : %d\n", content_type);
57
58         if (path)
59                 printf("path : %s\n", path);
60         else
61                 printf("path not");
62
63         if (mime_type)
64                 printf("mime_type : %s", mime_type);
65         else
66                 printf("mime not");
67
68         if (user_data) printf("String : %s\n", (char *)user_data);
69         else
70                 printf("user not");
71
72         return;
73 }
74
75 #if 1
76 gboolean _send_noti_batch_operations(gpointer data)
77 {
78         int ret = MS_MEDIA_ERR_NONE;
79
80         /* First of all, noti subscription */
81         char *user_str = strdup("hi");
82         media_db_update_subscribe(_noti_cb, (void *)user_str);
83
84         /* 1. media_svc_insert_item_immediately */
85         char *path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");
86
87         media_svc_storage_type_e storage_type;
88
89         ret = media_svc_get_storage_type(path, &storage_type, tzplatform_getuid(TZ_USER_NAME));
90         if (ret != MS_MEDIA_ERR_NONE) {
91                 media_svc_error("media_svc_get_storage_type failed : %d (%s)", ret, path);
92                 SAFE_FREE(user_str);
93                 return FALSE;
94         }
95
96         int idx = 0;
97         char *file_list[10];
98
99         ret = media_svc_insert_item_begin(100, TRUE, getpid());
100         /*ret = media_svc_insert_item_begin(g_db_handle, 100); */
101         for (idx = 0; idx < 10; idx++) {
102                 char filepath[255] = {0, };
103                 snprintf(filepath, sizeof(filepath), "%s%d.jpg", tzplatform_mkpath(TZ_USER_CONTENT, "test/image"), idx + 1);
104                 media_svc_debug("File : %s\n", filepath);
105                 file_list[idx] = strdup(filepath);
106                 ret = media_svc_insert_item_bulk(g_db_handle, storage_type, file_list[idx], FALSE);
107                 if (ret != 0)
108                         media_svc_error("media_svc_insert_item_bulk[%d] failed", idx);
109                 else
110                         media_svc_debug("media_svc_insert_item_bulk[%d] success", idx);
111         }
112
113         ret = media_svc_insert_item_end(tzplatform_getuid(TZ_USER_NAME));
114
115         SAFE_FREE(user_str);
116         return FALSE;
117 }
118 #endif
119
120 gboolean _send_noti_operations(gpointer data)
121 {
122         int ret = MS_MEDIA_ERR_NONE;
123
124         /* First of all, noti subscription */
125         char *user_str = strdup("hi");
126         media_db_update_subscribe(_noti_cb, (void *)user_str);
127
128         /* 1. media_svc_insert_item_immediately */
129         char *path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");
130         media_svc_storage_type_e storage_type;
131
132         ret = media_svc_get_storage_type(path, &storage_type, tzplatform_getuid(TZ_USER_NAME));
133         if (ret != MS_MEDIA_ERR_NONE) {
134                 media_svc_error("media_svc_get_storage_type failed : %d (%s)", ret, path);
135                 SAFE_FREE(user_str);
136                 return FALSE;
137         }
138
139         ret = media_svc_insert_item_immediately(g_db_handle, storage_type, path);
140         if (ret != MS_MEDIA_ERR_NONE) {
141                 media_svc_error("media_svc_insert_item_immediately failed : %d", ret);
142                 SAFE_FREE(user_str);
143                 return FALSE;
144         }
145
146         media_svc_debug("media_svc_insert_item_immediately success");
147
148         /* 2. media_svc_refresh_item */
149         ret = media_svc_refresh_item(g_db_handle, storage_type, path);
150         if (ret != MS_MEDIA_ERR_NONE) {
151                 media_svc_error("media_svc_refresh_item failed : %d", ret);
152                 return FALSE;
153         }
154         media_svc_debug("media_svc_refresh_item success");
155
156         /* 2. media_svc_move_item */
157         const char *dst_path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image11.jpg");
158         ret = media_svc_move_item(g_db_handle, storage_type, path, storage_type, dst_path);
159         if (ret != MS_MEDIA_ERR_NONE) {
160                 media_svc_error("media_svc_move_item failed : %d", ret);
161                 return FALSE;
162         }
163         media_svc_debug("media_svc_move_item success");
164
165         ret = media_svc_move_item(g_db_handle, storage_type, dst_path, storage_type, path);
166         if (ret != MS_MEDIA_ERR_NONE) {
167                 media_svc_error("media_svc_move_item failed : %d", ret);
168                 return FALSE;
169         }
170         media_svc_debug("media_svc_move_item success");
171
172         /* 4. media_svc_delete_item_by_path */
173         ret = media_svc_delete_item_by_path(g_db_handle, path);
174         if (ret != MS_MEDIA_ERR_NONE) {
175                 media_svc_error("media_svc_delete_item_by_path failed : %d", ret);
176                 return FALSE;
177         }
178         media_svc_debug("media_svc_delete_item_by_path success");
179
180         /* Rename folder */
181         const char *src_folder_path = tzplatform_mkpath(TZ_USER_CONTENT, "test");
182         const char *dst_folder_path = tzplatform_mkpath(TZ_USER_CONTENT, "test_test");
183         ret = media_svc_rename_folder(g_db_handle, src_folder_path, dst_folder_path);
184         if (ret != MS_MEDIA_ERR_NONE) {
185                 media_svc_error("media_svc_rename_folder failed : %d", ret);
186                 return FALSE;
187         }
188         media_svc_debug("media_svc_rename_folder success");
189
190         /* Rename folder again */
191         ret = media_svc_rename_folder(g_db_handle, dst_folder_path, src_folder_path);
192         if (ret != MS_MEDIA_ERR_NONE) {
193                 media_svc_error("media_svc_rename_folder failed : %d", ret);
194                 return FALSE;
195         }
196         media_svc_debug("media_svc_rename_folder success");
197
198         return FALSE;
199 }
200
201 int test_noti()
202 {
203         GSource *source = NULL;
204         GMainContext *context = NULL;
205
206         g_loop = g_main_loop_new(NULL, FALSE);
207         context = g_main_loop_get_context(g_loop);
208         source = g_idle_source_new();
209 #if 0
210         g_source_set_callback(source, _send_noti_operations, NULL, NULL);
211 #else
212         g_source_set_callback(source, _send_noti_batch_operations, NULL, NULL);
213 #endif
214         g_source_attach(source, context);
215
216         g_main_loop_run(g_loop);
217
218         g_main_loop_unref(g_loop);
219         media_db_update_unsubscribe();
220
221         return MS_MEDIA_ERR_NONE;
222 }
223
224 int main()
225 {
226         int ret = MS_MEDIA_ERR_NONE;
227         ret = media_svc_connect(&g_db_handle, tzplatform_getuid(TZ_USER_NAME), true);
228         if (ret != MS_MEDIA_ERR_NONE)
229                 media_svc_error("media_svc_connect failed : %d", ret);
230         else
231                 media_svc_debug("media_svc_connect success");
232
233         ret = test_noti();
234         if (ret < MS_MEDIA_ERR_NONE)
235                 media_svc_error("test_noti failed : %d", ret);
236         else
237                 media_svc_debug("test_noti success");
238
239         media_svc_disconnect(g_db_handle);
240         return ret;
241 }
242