5410f219fabb46dc86a67b5c8fe47f0b2a51c27b
[platform/core/multimedia/libmedia-service.git] / test / media-service-test.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <media-svc.h>
4 #include <media-svc-noti.h>
5
6 GMainLoop *g_loop = NULL;
7 MediaSvcHandle *g_db_handle = NULL;
8
9 void _noti_cb(int pid,
10                 media_item_type_e update_item,
11                 media_item_update_type_e update_type,
12                 char *path,
13                 char *uuid,
14                 media_type_e content_type,
15                 char *mime_type,
16                 void *user_data)
17 {
18         media_svc_debug("Noti from PID(%d)", pid);
19
20         if (update_item == MS_MEDIA_ITEM_FILE) {
21                 media_svc_debug("Noti item : MS_MEDIA_ITEM_FILE");
22         } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
23                 media_svc_debug("Noti item : MS_MEDIA_ITEM_DIRECTORY");
24         }
25
26         if (update_type == MS_MEDIA_ITEM_INSERT) {
27                 media_svc_debug("Noti type : MS_MEDIA_ITEM_INSERT");
28         } else if (update_type == MS_MEDIA_ITEM_DELETE) {
29                 media_svc_debug("Noti type : MS_MEDIA_ITEM_DELETE");
30         } else if (update_type == MS_MEDIA_ITEM_UPDATE) {
31                 media_svc_debug("Noti type : MS_MEDIA_ITEM_UPDATE");
32         }
33
34         //media_svc_debug("content type : %d", content_type);
35         printf("content type : %d\n", content_type);
36
37         if (path)
38                 printf("path : %s\n", path);
39         else
40                 printf("path not");
41
42         if (mime_type)
43                 printf("mime_type : %s", mime_type);
44         else
45                 printf("mime not");
46
47     if (user_data) printf("String : %s\n", (char *)user_data);
48         else
49                 printf("user not");
50
51     return;
52 }
53
54 #if 1
55 gboolean _send_noti_batch_operations(gpointer data)
56 {
57     int ret = MEDIA_INFO_ERROR_NONE;
58
59     /* First of all, noti subscription */
60     char *user_str = strdup("hi");
61     media_db_update_subscribe(_noti_cb, (void*)user_str);
62
63     /* 1. media_svc_insert_item_immediately */
64     char *path = "/opt/usr/media/test/image1.jpg";
65
66         media_svc_media_type_e media_type;
67         media_svc_storage_type_e storage_type;
68         char mime_type[255] = {0, };
69
70         ret = media_svc_get_storage_type(path, &storage_type);
71         if (ret < MEDIA_INFO_ERROR_NONE) {
72                 media_svc_error("media_svc_get_storage_type failed : %d (%s)", ret, path);
73         return FALSE;
74         }
75
76         ret = media_svc_get_mime_type(path, mime_type);
77         if (ret < MEDIA_INFO_ERROR_NONE) {
78                 media_svc_error("media_svc_get_mime_type failed : %d (%s)", ret, path);
79         return FALSE;
80         }
81
82         ret = media_svc_get_media_type(path, mime_type, &media_type);
83         if (ret < MEDIA_INFO_ERROR_NONE) {
84                 media_svc_error("media_svc_get_media_type failed : %d (%s)", ret, path);
85         return FALSE;
86         }
87
88         int i;
89         char *file_list[10];
90
91         ret = media_svc_insert_item_begin(g_db_handle, 100, TRUE, getpid());
92         //ret = media_svc_insert_item_begin(g_db_handle, 100);
93         for (i = 0; i < 16; i++) {
94                 char filepath[255] = {0,};
95                 snprintf(filepath, sizeof(filepath), "%s%d.jpg", "/opt/usr/media/test/image", i+1);
96                 media_svc_debug("File : %s\n", filepath);
97                 file_list[i] = strdup(filepath);
98                 ret = media_svc_insert_item_bulk(g_db_handle, storage_type, file_list[i], mime_type, media_type, FALSE);
99                 if (ret != 0) {
100                         media_svc_error("media_svc_insert_item_bulk[%d] failed", i);
101                 } else {
102                         media_svc_debug("media_svc_insert_item_bulk[%d] success", i);
103                 }
104         }
105
106         ret = media_svc_insert_item_end(g_db_handle);
107
108         return FALSE;
109 }
110 #endif
111
112 gboolean _send_noti_operations(gpointer data)
113 {
114     int ret = MEDIA_INFO_ERROR_NONE;
115
116     /* First of all, noti subscription */
117     char *user_str = strdup("hi");
118     media_db_update_subscribe(_noti_cb, (void*)user_str);
119
120     /* 1. media_svc_insert_item_immediately */
121     char *path = "/opt/usr/media/test/image1.jpg";
122
123         media_svc_media_type_e media_type;
124         media_svc_storage_type_e storage_type;
125         char mime_type[255] = {0, };
126
127         ret = media_svc_get_storage_type(path, &storage_type);
128         if (ret < MEDIA_INFO_ERROR_NONE) {
129                 media_svc_error("media_svc_get_storage_type failed : %d (%s)", ret, path);
130         return FALSE;
131         }
132
133         ret = media_svc_get_mime_type(path, mime_type);
134         if (ret < MEDIA_INFO_ERROR_NONE) {
135                 media_svc_error("media_svc_get_mime_type failed : %d (%s)", ret, path);
136         return FALSE;
137         }
138
139         ret = media_svc_get_media_type(path, mime_type, &media_type);
140         if (ret < MEDIA_INFO_ERROR_NONE) {
141                 media_svc_error("media_svc_get_media_type failed : %d (%s)", ret, path);
142         return FALSE;
143         }
144
145     ret = media_svc_insert_item_immediately(g_db_handle, storage_type, path, mime_type, media_type);
146     if (ret < MEDIA_INFO_ERROR_NONE) {
147         media_svc_error("media_svc_insert_item_immediately failed : %d", ret);
148         return FALSE;
149     }
150
151     media_svc_debug("media_svc_insert_item_immediately success");
152
153         /* 2. media_svc_refresh_item */
154     ret = media_svc_refresh_item(g_db_handle, storage_type, path, media_type);
155     if (ret < MEDIA_INFO_ERROR_NONE) {
156         media_svc_error("media_svc_refresh_item failed : %d", ret);
157         return FALSE;
158     }
159     media_svc_debug("media_svc_refresh_item success");
160
161         /* 2. media_svc_move_item */
162         const char *dst_path = "/opt/usr/media/test/image11.jpg";
163     ret = media_svc_move_item(g_db_handle, storage_type, path, storage_type, dst_path);
164     if (ret < MEDIA_INFO_ERROR_NONE) {
165         media_svc_error("media_svc_move_item failed : %d", ret);
166         return FALSE;
167     }
168     media_svc_debug("media_svc_move_item success");
169
170     ret = media_svc_move_item(g_db_handle, storage_type, dst_path, storage_type, path);
171     if (ret < MEDIA_INFO_ERROR_NONE) {
172         media_svc_error("media_svc_move_item failed : %d", ret);
173         return FALSE;
174     }
175     media_svc_debug("media_svc_move_item success");
176
177         /* 4. media_svc_delete_item_by_path */
178         ret = media_svc_delete_item_by_path(g_db_handle, path);
179     if (ret < MEDIA_INFO_ERROR_NONE) {
180         media_svc_error("media_svc_delete_item_by_path failed : %d", ret);
181         return FALSE;
182     }
183     media_svc_debug("media_svc_delete_item_by_path success");
184
185         /* Rename folder */
186         const char *src_folder_path = "/opt/usr/media/test";
187         const char *dst_folder_path = "/opt/usr/media/test_test";
188         ret = media_svc_rename_folder(g_db_handle, src_folder_path, dst_folder_path);
189     if (ret < MEDIA_INFO_ERROR_NONE) {
190         media_svc_error("media_svc_rename_folder failed : %d", ret);
191         return FALSE;
192     }
193     media_svc_debug("media_svc_rename_folder success");
194
195         /* Rename folder again */
196         ret = media_svc_rename_folder(g_db_handle, dst_folder_path, src_folder_path);
197     if (ret < MEDIA_INFO_ERROR_NONE) {
198         media_svc_error("media_svc_rename_folder failed : %d", ret);
199         return FALSE;
200     }
201     media_svc_debug("media_svc_rename_folder success");
202
203     return FALSE;
204 }
205
206 int test_noti()
207 {
208         GSource *source = NULL;
209         GMainContext *context = NULL;
210
211         g_loop = g_main_loop_new(NULL, FALSE);
212         context = g_main_loop_get_context(g_loop);
213         source = g_idle_source_new();
214 #if 0
215         g_source_set_callback (source, _send_noti_operations, NULL, NULL);
216 #else
217         g_source_set_callback (source, _send_noti_batch_operations, NULL, NULL);
218 #endif
219         g_source_attach (source, context);
220
221         g_main_loop_run(g_loop);
222
223         g_main_loop_unref(g_loop);
224     media_db_update_unsubscribe();
225
226         return MEDIA_INFO_ERROR_NONE;
227 }
228
229 int main()
230 {
231         int ret = MEDIA_INFO_ERROR_NONE;
232         ret = media_svc_connect(&g_db_handle);
233         if (ret < MEDIA_INFO_ERROR_NONE) {
234                 media_svc_error("media_svc_connect failed : %d", ret);
235         } else {
236                 media_svc_debug("media_svc_connect success");
237         }
238
239         ret = test_noti();
240         if (ret < MEDIA_INFO_ERROR_NONE) {
241                 media_svc_error("test_noti failed : %d", ret);
242         } else {
243                 media_svc_debug("test_noti success");
244         }
245
246         media_svc_disconnect(g_db_handle);
247         return ret;
248 }
249