Remove deprecated APIs from test code
[platform/core/api/media-content.git] / test / media-content_test.c
1 /*
2 * Copyright (c) 2011 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 <sys/time.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <media_content.h>
22 #include <media_info_private.h>
23 #include <glib.h>
24 #include <tzplatform_config.h>
25 #include <media_content_internal.h>
26
27
28 static filter_h g_filter = NULL;
29
30 static GMainLoop *g_loop = NULL;
31
32 #define test_audio_id "3304285f-1070-41af-8b4e-f0086cc768f3"
33 #define test_video_id "53c43e7e-53d2-4194-80a6-55d5dcde0def"
34 #define test_image_id "db1c184c-6f31-43b4-b924-8c00ac5b6197"
35
36 static void get_audio_meta(media_info_h media)
37 {
38         audio_meta_h audio = NULL;
39         char *c_value = NULL;
40
41         content_debug("=== audio meta ===");
42         if (media_info_get_audio(media, &audio) != MEDIA_CONTENT_ERROR_NONE)
43                 return;
44
45         if (audio_meta_get_media_id(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
46                 content_debug("audio_id : [%s]", c_value);
47                 SAFE_FREE(c_value);
48         }
49
50         if (audio_meta_get_album(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
51                 content_debug("album : [%s]", c_value);
52                 SAFE_FREE(c_value);
53         }
54
55         if (audio_meta_get_artist(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
56                 content_debug("artist : [%s]", c_value);
57                 SAFE_FREE(c_value);
58         }
59
60         if (audio_meta_get_album_artist(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
61                 content_debug("album artist : [%s]", c_value);
62                 SAFE_FREE(c_value);
63         }
64
65         if (audio_meta_get_genre(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
66                 content_debug("genre : [%s]", c_value);
67                 SAFE_FREE(c_value);
68         }
69
70         if (audio_meta_get_year(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
71                 content_debug("year : [%s]", c_value);
72                 SAFE_FREE(c_value);
73         }
74
75         if (audio_meta_get_track_num(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
76                 content_debug("track number : [%s]", c_value);
77                 SAFE_FREE(c_value);
78         }
79
80         audio_meta_destroy(audio);
81 }
82
83 static void get_image_meta(media_info_h media)
84 {
85         image_meta_h image = NULL;
86         char *c_value = NULL;
87         int i_value = 0;
88         media_content_orientation_e orientation;
89
90         content_debug("=== image meta ===");
91         if (media_info_get_image(media, &image) != MEDIA_CONTENT_ERROR_NONE)
92                 return;
93
94         if (image_meta_get_width(image, &i_value) == MEDIA_CONTENT_ERROR_NONE)
95                 content_debug("width : [%d]", i_value);
96
97         if (image_meta_get_height(image, &i_value) == MEDIA_CONTENT_ERROR_NONE)
98                 content_debug("height : [%d]", i_value);
99
100         if (image_meta_get_orientation(image, &orientation) == MEDIA_CONTENT_ERROR_NONE)
101                 content_debug("orientation : [%d]", orientation);
102
103         if (image_meta_get_date_taken(image, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
104                 content_debug("datetaken : [%s]", c_value);
105                 SAFE_FREE(c_value);
106         }
107
108         image_meta_destroy(image);
109 }
110
111 static void get_book_meta(media_info_h media)
112 {
113         book_meta_h book = NULL;
114         char *c_value = NULL;
115
116         content_debug("=== book meta ===");
117         if (media_info_get_book(media, &book) != MEDIA_CONTENT_ERROR_NONE)
118                 return;
119
120         if (book_meta_get_media_id(book, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
121                 content_debug("media_id : [%s]", c_value);
122                 SAFE_FREE(c_value);
123         }
124
125         if (book_meta_get_author(book, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
126                 content_debug("author : [%s]", c_value);
127                 SAFE_FREE(c_value);
128         }
129
130         if (book_meta_get_subject(book, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
131                 content_debug("subject : [%s]", c_value);
132                 SAFE_FREE(c_value);
133         }
134
135         book_meta_destroy(book);
136 }
137
138 static void get_album_meta(media_album_h album)
139 {
140         char *c_value = NULL;
141         int i_value = 0;
142
143         content_debug("=== album meta ===");
144         if (media_album_get_album_id(album, &i_value) == MEDIA_CONTENT_ERROR_NONE)
145                 content_debug("album id : [%d]", i_value);
146
147         if (media_album_get_name(album, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
148                 content_debug("album name : [%s]", c_value);
149                 SAFE_FREE(c_value);
150         }
151
152         if (media_album_get_artist(album, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
153                 content_debug("album artist : [%s]", c_value);
154                 SAFE_FREE(c_value);
155         }
156 }
157
158 static void get_bookmark_meta(media_bookmark_h bookmark)
159 {
160         char *c_value = NULL;
161         int i_value = 0;
162         time_t time = 0;
163
164         content_debug("=== bookmark meta ===");
165         if (media_bookmark_get_bookmark_id(bookmark, &i_value) == MEDIA_CONTENT_ERROR_NONE)
166                 content_debug("bookmark_id : %d", i_value);
167
168         if (media_bookmark_get_thumbnail_path(bookmark, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
169                 content_debug("bookmark thumbnail_path : %s", c_value);
170                 SAFE_FREE(c_value);
171         }
172
173         if (media_bookmark_get_marked_time(bookmark, &time) == MEDIA_CONTENT_ERROR_NONE)
174                 content_debug("bookmark marked_time : %ld", time);
175
176         if (media_bookmark_get_name(bookmark, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
177                 content_debug("bookmark name : %s", c_value);
178                 SAFE_FREE(c_value);
179         }
180 }
181
182 static void get_folder_meta(media_folder_h folder, char **folder_id)
183 {
184         char *c_value = NULL;
185
186         content_debug("=== folder meta ===");
187         if (media_folder_get_folder_id(folder, &c_value) != MEDIA_CONTENT_ERROR_NONE) {
188                 content_debug("folder_id : %s", c_value);
189                 *folder_id = c_value;
190         }
191
192         if (media_folder_get_path(folder, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
193                 content_debug("folder_path : %s", c_value);
194                 SAFE_FREE(c_value);
195         }
196
197         if (media_folder_get_name(folder, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
198                 content_debug("folder_name : %s", c_value);
199                 SAFE_FREE(c_value);
200         }
201 }
202
203 static void get_media_meta(media_info_h media)
204 {
205         char *c_value = NULL;
206         time_t t_value = 0;
207         unsigned long long size = 0;
208         media_content_type_e media_type = 0;
209
210         content_debug("=== media meta ===");
211         if (media_info_get_media_type(media, &media_type) == MEDIA_CONTENT_ERROR_NONE)
212                 content_debug("media_type : [%d]", media_type);
213
214         if (media_info_get_media_id(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
215                 content_debug("media_id : [%s]", c_value);
216                 SAFE_FREE(c_value);
217         }
218
219         if (media_info_get_file_path(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
220                 content_debug("file_path : [%s]", c_value);
221                 SAFE_FREE(c_value);
222         }
223
224         if (media_info_get_display_name(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
225                 content_debug("display_name : [%s]", c_value);
226                 SAFE_FREE(c_value);
227         }
228
229         if (media_info_get_mime_type(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
230                 content_debug("mime_type : [%s]", c_value);
231                 SAFE_FREE(c_value);
232         }
233
234         if (media_info_get_thumbnail_path(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
235                 content_debug("thumbnail_path : [%s]", c_value);
236                 SAFE_FREE(c_value);
237         }
238
239         if (media_info_get_size(media, &size) == MEDIA_CONTENT_ERROR_NONE)
240                 content_debug("size : [%lld]", size);
241
242         if (media_info_get_added_time(media, &t_value) == MEDIA_CONTENT_ERROR_NONE)
243                 content_debug("added_time : [%ld]", t_value);
244
245         if (media_info_get_modified_time(media, &t_value) == MEDIA_CONTENT_ERROR_NONE)
246                 content_debug("modified_time : [%ld]", t_value);
247
248 #ifdef _USE_TVPD_MODE
249         if (media_info_get_stitched_state(media, &i_value) == MEDIA_CONTENT_ERROR_NONE)
250                 content_debug("360 stitched : [%d]", i_value);
251
252         if (media_info_get_stitched_engine(media, &i_value) == MEDIA_CONTENT_ERROR_NONE)
253                 content_debug("360 engine : [%d]", i_value);
254 #endif
255
256         if (media_type == MEDIA_CONTENT_TYPE_MUSIC)
257                 get_audio_meta(media);
258
259         if (media_type == MEDIA_CONTENT_TYPE_IMAGE)
260                 get_image_meta(media);
261
262         if (media_type == MEDIA_CONTENT_TYPE_BOOK)
263                 get_book_meta(media);
264
265         if (media_type == MEDIA_CONTENT_TYPE_OTHERS)
266                 content_debug("Other type");
267 }
268
269 bool media_item_cb(media_info_h media, void *user_data)
270 {
271         if (!media) {
272                 content_debug("NO Item");
273                 return true;
274         }
275
276         get_media_meta(media);
277
278         return true;
279 }
280
281 bool gallery_folder_list_cb(media_folder_h folder, void *user_data)
282 {
283         media_folder_h new_folder = NULL;
284         media_folder_clone(&new_folder, folder);
285
286         GList **list = (GList**)user_data;
287         *list = g_list_append(*list, new_folder);
288
289         return true;
290 }
291
292 bool gallery_media_item_cb(media_info_h media, void *user_data)
293 {
294         media_info_h new_media = NULL;
295         int ret = MEDIA_CONTENT_ERROR_NONE;
296
297         ret = media_info_clone(&new_media, media);
298
299         if (ret != MEDIA_CONTENT_ERROR_NONE) {
300                 GList **list = (GList**)user_data;
301                 *list = g_list_append(*list, new_media);
302         }
303
304         return true;
305 }
306
307 bool gallery_bookmarks_cb(media_bookmark_h bookmark, void *user_data)
308 {
309         media_bookmark_h new_bm = NULL;
310         int ret = MEDIA_CONTENT_ERROR_NONE;
311
312         ret = media_bookmark_clone(&new_bm, bookmark);
313         if (ret != MEDIA_CONTENT_ERROR_NONE)
314                 content_error("error media_bookmark_clone : [%d]", ret);
315
316         GList **list = (GList**)user_data;
317         *list = g_list_append(*list, new_bm);
318
319         return true;
320 }
321
322 bool folder_list_cb(media_folder_h folder, void *user_data)
323 {
324         int item_count = 0;
325         char *folder_id = NULL;
326         media_folder_h *_folder = (media_folder_h*)user_data;
327
328         content_debug("===========================");
329         if (folder != NULL) {
330                 if (_folder != NULL)
331                         media_folder_clone(_folder, folder);
332
333                 get_folder_meta(folder, &folder_id);
334
335                 if (media_folder_get_media_count_from_db(folder_id, g_filter, &item_count) != MEDIA_CONTENT_ERROR_NONE) {
336                         SAFE_FREE(folder_id);
337                         content_error("[ERROR] media_folder_get_media_count_from_db is failed");
338                         return false;
339                 }
340
341                 if (media_folder_foreach_media_from_db(folder_id, g_filter, media_item_cb, NULL) != MEDIA_CONTENT_ERROR_NONE) {
342                         SAFE_FREE(folder_id);
343                         content_error("[ERROR] media_folder_foreach_media_from_db is failed");
344                         return false;
345                 }
346
347                 SAFE_FREE(folder_id);
348                 return true;
349         } else {
350                 return false;
351         }
352
353 }
354
355 bool playlist_list_cb(media_playlist_h playlist, void *user_data)
356 {
357         int ret = MEDIA_CONTENT_ERROR_NONE;
358         int playlist_id = 0;
359         char *playlist_name = NULL;
360         media_playlist_h playlist_h;
361         char *playlist_thumbnail_path = NULL;
362
363         content_debug("playlist_list_cb ======");
364 #if 0
365         GList **list = (GList**)user_data;
366 #endif
367
368         if (playlist == NULL) {
369                 content_debug(" playlist handle is NULL");
370                 return false;
371         }
372
373         ret = media_playlist_get_playlist_id(playlist, &playlist_id);
374         if (ret != MEDIA_CONTENT_ERROR_NONE)
375                 content_error("error media_playlist_get_playlist_id : [%d]", ret);
376
377         content_debug("playlist_id : %d", playlist_id);
378         /* 64bit build issue */
379 #if 0
380         if (user_data != NULL)
381                 *list = g_list_append(*list, (gpointer)playlist_id);
382 #endif
383         ret = media_playlist_get_name(playlist, &playlist_name);
384         if (ret != MEDIA_CONTENT_ERROR_NONE)
385                 content_error("error media_playlist_get_name : [%d]", ret);
386
387         content_debug("playlist_name : %s", playlist_name);
388         SAFE_FREE(playlist_name);
389
390         ret = media_playlist_get_thumbnail_path(playlist, &playlist_thumbnail_path);
391         if (ret != MEDIA_CONTENT_ERROR_NONE)
392                 content_error("error media_playlist_get_thumbnail_path : [%d]", ret);
393
394         content_debug("playlist_thumbnail_path : %s", playlist_thumbnail_path);
395         SAFE_FREE(playlist_thumbnail_path);
396
397         ret = media_playlist_get_playlist_from_db(playlist_id, &playlist_h);
398         if (ret != MEDIA_CONTENT_ERROR_NONE)
399                 content_error("error media_playlist_get_playlist_from_db : [%d]", ret);
400
401         ret = media_playlist_destroy(playlist_h);
402         if (ret != MEDIA_CONTENT_ERROR_NONE)
403                 content_error("error media_playlist_destroy : [%d]", ret);
404
405         return true;
406 }
407
408 bool bookmarks_cb(media_bookmark_h bookmark, void *user_data)
409 {
410         if (bookmark != NULL && user_data != NULL) {
411                 media_bookmark_h new_bookmark;
412
413                 media_bookmark_clone(&new_bookmark, bookmark);
414                 GList **list = (GList**)user_data;
415                 *list = g_list_append(*list, new_bookmark);
416         }
417
418         get_bookmark_meta(bookmark);
419
420         return true;
421 }
422
423 bool album_list_cb(media_album_h album, void *user_data)
424 {
425         int album_id = 0;
426         int media_count = 0;
427         int ret = MEDIA_CONTENT_ERROR_NONE;
428
429         if (album != NULL) {
430                 get_album_meta(album);
431                 media_album_get_album_id(album, &album_id);
432
433                 ret = media_album_get_media_count_from_db(album_id, NULL, &media_count);
434                 if (ret != MEDIA_CONTENT_ERROR_NONE)
435                         content_error("error media_album_get_media_count_from_db : [%d]", ret);
436
437                 content_debug("media_count : [%d]", media_count);
438
439                 ret = media_album_foreach_media_from_db(album_id, NULL, media_item_cb, NULL);
440                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
441                         content_error("error media_album_foreach_media_from_db : [%d]", ret);
442                         return false;
443                 }
444
445         } else {
446                 content_error("album item not Found!!");
447         }
448
449         return true;
450 }
451
452 bool group_list_cb(const char *group_name, void *user_data)
453 {
454         int media_count = 0;
455         int *idx = user_data;
456
457         content_debug("group item : [%s] [%d]", group_name, *idx);
458
459         if (media_group_get_media_count_from_db(group_name, *idx, g_filter, &media_count) != MEDIA_CONTENT_ERROR_NONE)
460                 return false;
461
462         content_debug("media_count : [%d]", media_count);
463
464         if (media_group_foreach_media_from_db(group_name, *idx, g_filter, media_item_cb, NULL) != MEDIA_CONTENT_ERROR_NONE)
465                 return false;
466
467         return true;
468 }
469
470 bool playlist_item_cb(int playlist_member_id, media_info_h media, void *user_data)
471 {
472         content_debug("playlist_member_id : [%d]", playlist_member_id);
473         /* 64bit build issue */
474 #if 0
475         GList **list = (GList**)user_data;
476
477         *list = g_list_append(*list, (gpointer)playlist_member_id);
478 #endif
479         /*media_item_cb(media, user_data);*/
480
481         return true;
482 }
483
484 int test_filter_create(void)
485 {
486         content_debug("\n============Filter Create============\n\n");
487
488         int ret = MEDIA_CONTENT_ERROR_NONE;
489
490         /* Filter for media */
491         const char *condition = "MEDIA_TYPE=3"; /*MEDIA_TYPE 0-image, 1-video, 2-sound, 3-music, 4-other*/
492
493         ret = media_filter_create(&g_filter);
494
495         /* Set condition and collate
496          * Condition string : You can make where statement of sql.
497          * Colation : You can use collation when comparing.
498          * Ex) In case of FILE_NAME='Samsung' as condition string,
499          *      if you want to compare with NOCASE collation,
500          *      call media_filter_set_condition(g_filter, condition, MEDIA_CONTENT_COLLATE_NOCASE);
501          *      if you want to compare in case-sensitive,
502          *      call media_filter_set_condition(g_filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
503          */
504         ret = media_filter_set_condition(g_filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
505
506         return ret;
507 }
508
509 int test_filter_destroy(void)
510 {
511         content_debug("\n============Filter Create============\n\n");
512
513         int ret = MEDIA_CONTENT_ERROR_NONE;
514
515         ret = media_filter_destroy(g_filter);
516
517         return ret;
518 }
519
520 int test_connect_database(void)
521 {
522         int ret = MEDIA_CONTENT_ERROR_NONE;
523
524         content_debug("\n============DB Connection Test============\n\n");
525
526         ret = media_content_connect();
527
528         if (ret == MEDIA_CONTENT_ERROR_NONE)
529                 content_debug("connection is success\n\n");
530         else
531                 content_error("connection is failed\n\n");
532
533         return ret;
534 }
535
536 int test_gallery_scenario(void)
537 {
538         int ret = MEDIA_CONTENT_ERROR_NONE;
539         unsigned int i = 0;
540         filter_h filter = NULL;
541
542         int count;
543         GList *folder_list = NULL;
544         media_folder_h folder_handle = NULL;
545
546         /* First, Get folder list */
547         ret = media_folder_foreach_folder_from_db(filter, gallery_folder_list_cb, &folder_list);
548         if (ret != MEDIA_CONTENT_ERROR_NONE) {
549                 content_error("media_folder_foreach_folder_from_db failed: %d", ret);
550                 return -1;
551         } else {
552                 content_debug("media_folder_foreach_folder_from_db success!!");
553                 char *folder_id = NULL;
554
555                 for (i = 0; i < g_list_length(folder_list); i++) {
556                         folder_handle = (media_folder_h)g_list_nth_data(folder_list, i);
557
558                         get_folder_meta(folder_handle, &folder_id);
559
560                         ret = media_folder_get_media_count_from_db(folder_id, filter, &count);
561                         SAFE_FREE(folder_id);
562                         if (ret != MEDIA_CONTENT_ERROR_NONE) {
563                                 content_error("media_folder_get_media_count_from_db failed: %d", ret);
564                                 return -1;
565                         } else {
566                                 content_debug("media count [%d] : %d", i, count);
567                         }
568                 }
569         }
570
571         /* To check performance */
572         struct timeval start, end;
573         gettimeofday(&start, NULL);
574
575         /* Second, Get all item list */
576         media_info_h media_handle = NULL;
577         GList *all_item_list = NULL;
578
579         media_content_collation_e collate_type = MEDIA_CONTENT_COLLATE_NOCASE;
580         media_content_order_e order_type = MEDIA_CONTENT_ORDER_DESC;
581         ret = media_filter_create(&filter);
582         if (ret != MEDIA_CONTENT_ERROR_NONE) {
583                 content_error("Fail to create filter");
584                 return ret;
585         }
586         ret = media_filter_set_condition(filter, "MEDIA_TYPE = 0", collate_type);
587         if (ret != MEDIA_CONTENT_ERROR_NONE) {
588                 media_filter_destroy(filter);
589                 content_error("Fail to set condition");
590                 return ret;
591         }
592         ret = media_filter_set_order(filter, order_type, MEDIA_DISPLAY_NAME, collate_type);
593         if (ret != MEDIA_CONTENT_ERROR_NONE) {
594                 media_filter_destroy(filter);
595                 content_error("Fail to set order");
596                 return ret;
597         }
598
599         ret = media_info_foreach_media_from_db(filter, gallery_media_item_cb, &all_item_list);
600         if (ret != MEDIA_CONTENT_ERROR_NONE) {
601                 content_error("media_info_foreach_media_from_db failed: %d", ret);
602                 media_filter_destroy(filter);
603                 return -1;
604         } else {
605                 content_debug("media_info_foreach_media_from_db success");
606
607                 for (i = 0; i < g_list_length(all_item_list); i++) {
608                         media_handle = (media_info_h)g_list_nth_data(all_item_list, i);
609                         get_media_meta(media_handle);
610                 }
611         }
612
613         media_filter_destroy(filter);
614         filter = NULL;
615
616         /* To check performance */
617         gettimeofday(&end, NULL);
618         long time = (end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec);
619         content_debug("Time : %ld\n", time);
620
621         /* Third, Get item list of a folder */
622         GList *item_list = NULL;
623
624         for (i = 0; i < g_list_length(folder_list); i++) {
625                 unsigned int j = 0;
626                 char *folder_id = NULL;
627                 folder_handle = (media_folder_h)g_list_nth_data(folder_list, i);
628
629                 get_folder_meta(folder_handle, &folder_id);
630
631                 ret = media_folder_foreach_media_from_db(folder_id, filter, gallery_media_item_cb, &item_list);
632                 SAFE_FREE(folder_id);
633
634                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
635                         content_error("media_folder_foreach_media_from_db failed: %d", ret);
636                         return -1;
637                 } else {
638                         content_error("media_folder_foreach_media_from_db success!");
639
640                         for (j = 0; j < g_list_length(item_list); j++) {
641                                 media_handle = (media_info_h)g_list_nth_data(item_list, j);
642                                 get_media_meta(media_handle);
643                         }
644                 }
645         }
646
647         /* Remove folder list */
648         if (folder_list) {
649                 for (i = 0; i < g_list_length(folder_list); i++) {
650                         folder_handle = (media_folder_h)g_list_nth_data(folder_list, i);
651                         media_folder_destroy(folder_handle);
652                 }
653
654                 g_list_free(folder_list);
655         }
656
657         /* Remove all items list */
658         if (all_item_list) {
659                 for (i = 0; i < g_list_length(all_item_list); i++) {
660                         media_handle = (media_info_h)g_list_nth_data(all_item_list, i);
661                         ret = media_info_destroy(media_handle);
662                         if (ret != MEDIA_CONTENT_ERROR_NONE)
663                                 content_error("media_info_destroy failed: %d", ret);
664                 }
665
666                 g_list_free(all_item_list);
667         }
668
669         /* Remove items list */
670         if (item_list) {
671                 for (i = 0; i < g_list_length(item_list); i++) {
672                         media_handle = (media_info_h)g_list_nth_data(item_list, i);
673                         ret = media_info_destroy(media_handle);
674                         if (ret != MEDIA_CONTENT_ERROR_NONE)
675                                 content_error("media_info_destroy failed: %d", ret);
676                 }
677
678                 g_list_free(item_list);
679         }
680
681         return MEDIA_CONTENT_ERROR_NONE;
682 }
683
684 /*Get All Music file. sort by Title and not case sensitive*/
685 int test_get_all_music_files(void)
686 {
687         int ret = MEDIA_CONTENT_ERROR_NONE;
688         int media_count = 0;
689         filter_h filter;
690
691         /*Set Filter*/
692         const char *condition = "MEDIA_TYPE=3"; /*0-image, 1-video, 2-sound, 3-music, 4-other*/
693
694         ret = media_filter_create(&filter);
695         if (ret != MEDIA_CONTENT_ERROR_NONE) {
696                 content_error("Fail to create filter");
697                 return ret;
698         }
699         ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_LOCALIZED);
700         if (ret != MEDIA_CONTENT_ERROR_NONE) {
701                 media_filter_destroy(filter);
702                 content_error("Fail to set condition");
703                 return ret;
704         }
705         ret = media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_TITLE, MEDIA_CONTENT_COLLATE_LOCALIZED);
706         if (ret != MEDIA_CONTENT_ERROR_NONE) {
707                 media_filter_destroy(filter);
708                 content_error("Fail to set order");
709                 return ret;
710         }
711
712         /*Get Media Count*/
713         ret = media_info_get_media_count_from_db(filter, &media_count);
714         if (ret != MEDIA_CONTENT_ERROR_NONE) {
715                 media_filter_destroy(filter);
716                 content_error("Fail to get media count");
717                 return ret;
718         }
719
720         content_debug("media_count : [%d]", media_count);
721
722         ret = media_info_foreach_media_from_db(filter, media_item_cb, NULL);
723         if (ret != MEDIA_CONTENT_ERROR_NONE) {
724                 media_filter_destroy(filter);
725                 content_error("Fail to get media");
726                 return ret;
727         }
728
729         ret = media_filter_destroy(filter);
730
731         return ret;
732 }
733
734 int test_media_info_operation(void)
735 {
736         int ret = MEDIA_CONTENT_ERROR_NONE;
737         int media_count = 0;
738
739         content_debug("\n============Media info Test============\n\n");
740
741         test_filter_create();
742
743         ret = media_info_get_media_count_from_db(g_filter, &media_count);
744         if (ret != MEDIA_CONTENT_ERROR_NONE)
745                 content_error("media_info_get_media_count_from_db failed: %d", ret);
746         else
747                 content_debug("media_count : [%d]", media_count);
748
749         ret = media_info_foreach_media_from_db(g_filter, media_item_cb, NULL);
750         if (ret == MEDIA_CONTENT_ERROR_NONE)
751                 content_debug("media_info_foreach_media_from_db is success");
752         else
753                 content_error("media_info_foreach_media_from_db is failed");
754
755         test_filter_destroy();
756
757         return ret;
758 }
759
760 int test_folder_operation(void)
761 {
762         int ret = MEDIA_CONTENT_ERROR_NONE;
763         filter_h filter = NULL;
764         media_folder_h folder = NULL;
765         char *folder_id = NULL;
766         int count = 0;
767
768         content_debug("\n============Folder Test============\n\n");
769
770         test_filter_create();
771
772         ret = media_filter_create(&filter);
773         if (ret != MEDIA_CONTENT_ERROR_NONE) {
774                 content_error("[ERROR] media_folder_filter_create is failed");
775                 return ret;
776         }
777
778         media_filter_set_condition(filter, "MEDIA_TYPE = 0 or MEDIA_TYPE = 1", MEDIA_CONTENT_COLLATE_DEFAULT);  /*MEDIA_TYPE 0-image, 1-video, 2-sound, 3-music, 4-other*/
779         media_filter_set_offset(filter, 0, 5);
780         media_filter_set_order(filter, MEDIA_CONTENT_ORDER_DESC, MEDIA_PATH, MEDIA_CONTENT_COLLATE_NOCASE);
781
782         ret = media_folder_get_folder_count_from_db(filter, &count);
783         content_debug("Folder count : [%d]", count);
784
785         ret = media_folder_foreach_folder_from_db(filter, folder_list_cb, &folder);
786
787         filter_h m_filter = NULL;
788
789         ret = media_filter_create(&m_filter);
790         if (ret != MEDIA_CONTENT_ERROR_NONE) {
791                 test_filter_destroy();
792                 media_filter_destroy(filter);
793                 content_error("[ERROR] media_info_filter_create is failed");
794                 return ret;
795         }
796
797         media_filter_set_condition(m_filter, "MEDIA_TYPE=1", MEDIA_CONTENT_COLLATE_DEFAULT);    /*MEDIA_TYPE 0-image, 1-video, 2-sound, 3-music, 4-other*/
798         media_filter_set_offset(m_filter, 0, 5);
799         media_filter_set_order(m_filter, MEDIA_CONTENT_ORDER_DESC, MEDIA_PATH, MEDIA_CONTENT_COLLATE_NOCASE);
800
801         ret = media_folder_foreach_media_from_db(folder_id, m_filter, media_item_cb, NULL);
802         if (ret != MEDIA_CONTENT_ERROR_NONE)
803                 content_error("[ERROR] media_folder_foreach_media_from_db is failed, error code : %d", ret);
804
805         media_filter_destroy(filter);
806         media_filter_destroy(m_filter);
807
808         test_filter_destroy();
809
810         /* fix prevent: Resource Leak */
811         SAFE_FREE(folder_id);
812
813         return ret;
814 }
815
816 int test_playlist_operation(void)
817 {
818         int ret = MEDIA_CONTENT_ERROR_NONE;
819         media_playlist_h playlist_1 = NULL;
820         media_playlist_h playlist_2 = NULL;
821         media_playlist_h playlist_3 = NULL;
822         media_playlist_h playlist_4 = NULL;
823         int playlist_id_1 = 0;
824         int playlist_id_2 = 0;
825         int playlist_id_3 = 0;
826         const char *playlist_name_1 = "myPlaylist_1";
827         const char *playlist_name_2 = "myPlaylist_2";
828         const char *playlist_name_3 = "myPlaylist_3";
829         int playlist_count = 0;
830         int media_count = 0;
831         int order_1 = 0;
832         int order_2 = 0;
833         int order_3 = 0;
834         int order_4 = 0;
835         int order_5 = 0;
836         filter_h filter = NULL;
837         filter_h m_filter = NULL;
838
839         content_debug("\n============Playlist Test============\n\n");
840
841         /* Filter for playlist */
842
843         const char *condition = "(MEDIA_TYPE=1 or MEDIA_TYPE=3)";       /*0-image, 1-video, 2-sound, 3-music, 4-other*/
844
845         ret = media_filter_create(&filter);
846         ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_NOCASE);
847         ret = media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, PLAYLIST_NAME, MEDIA_CONTENT_COLLATE_NOCASE);
848
849         /* Create Playlist */
850         media_playlist_insert_to_db(playlist_name_1, &playlist_1);
851         media_playlist_insert_to_db(playlist_name_2, &playlist_2);
852         media_playlist_insert_to_db(playlist_name_3, &playlist_3);
853
854         if (playlist_1 != NULL) {
855                 /* Add media to Playlist */
856                 media_playlist_add_media(playlist_1, test_audio_id);
857                 media_playlist_add_media(playlist_1, test_audio_id);
858                 media_playlist_add_media(playlist_1, test_video_id);
859
860                 #if 0
861                 char *playlist_thumb_path = tzplatform_mkpath(TZ_USER_IMAGES, "Default.jpg"));
862                 media_playlist_set_thumbnail_path(playlist_1, playlist_thumb_path);
863                 #endif
864
865                 media_playlist_update_to_db(playlist_1);
866         }
867
868         if (playlist_2 != NULL) {
869                 media_playlist_add_media(playlist_2, test_audio_id);
870                 media_playlist_add_media(playlist_2, test_audio_id);
871                 media_playlist_update_to_db(playlist_2);
872         }
873
874         /* Get Playlist Count*/
875         ret = media_playlist_get_playlist_count_from_db(filter, &playlist_count);
876         if (ret == 0)
877                 content_debug("playlist_count [%d]", playlist_count);
878
879         /* Get Playlist*/
880         GList *playlist_id_list = NULL;
881         media_playlist_foreach_playlist_from_db(filter, playlist_list_cb, &playlist_id_list);
882         /* 64bit build issue */
883 #if 0
884         /* Get Playlist id*/
885         playlist_id_1 = (int)g_list_nth_data(playlist_id_list, 0);
886         playlist_id_2 = (int)g_list_nth_data(playlist_id_list, 1);
887         playlist_id_3 = (int)g_list_nth_data(playlist_id_list, 2);
888 #endif
889         content_debug("playlist_id_1 [%d]", playlist_id_1);
890         content_debug("playlist_id_2 [%d]", playlist_id_2);
891         content_debug("playlist_id_3 [%d]", playlist_id_3);
892
893         /* Export and import playlist */
894         media_playlist_export_to_file(playlist_3, tzplatform_mkpath(TZ_USER_MUSIC, "playlist.m3u"));
895         media_playlist_import_from_file(tzplatform_mkpath(TZ_USER_MUSIC, "playlist.m3u"), "playlist_4", &playlist_4);
896
897         /* Filter for media*/
898         ret = media_filter_create(&m_filter);
899
900         ret = media_filter_set_condition(m_filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
901
902         ret = media_filter_set_order(m_filter, MEDIA_CONTENT_ORDER_ASC, PLAYLIST_MEMBER_ORDER, MEDIA_CONTENT_COLLATE_DEFAULT);
903
904         /* Get media count */
905         media_playlist_get_media_count_from_db(playlist_id_1, m_filter, &media_count);
906         content_debug("playlist_1_media_count [%d]", media_count);
907
908         media_playlist_get_media_count_from_db(playlist_id_2, m_filter, &media_count);
909         content_debug("playlist_2_media_count [%d]", media_count);
910
911         media_playlist_get_media_count_from_db(playlist_id_3, m_filter, &media_count);
912         content_debug("playlist_3_media_count [%d]", media_count);
913
914         /* Get media of playlist */
915         GList *playlist_member_id_list = NULL;
916         GList *playlist_member_id_list_1 = NULL;
917
918         media_playlist_foreach_media_from_db(playlist_id_1, m_filter, playlist_item_cb, &playlist_member_id_list);
919         media_playlist_foreach_media_from_db(playlist_id_2, m_filter, playlist_item_cb, &playlist_member_id_list_1);
920
921         int playlist_member_id_1_1 = 0;
922         int playlist_member_id_1_2 = 0;
923         int playlist_member_id_1_3 = 0;
924         int playlist_member_id_2_1 = 0;
925         int playlist_member_id_2_2 = 0;
926         /* 64bit build issue */
927 #if 0
928         playlist_member_id_1_1 = (int)g_list_nth_data(playlist_member_id_list, 0);
929         playlist_member_id_1_2 = (int)g_list_nth_data(playlist_member_id_list, 1);
930         playlist_member_id_1_3 = (int)g_list_nth_data(playlist_member_id_list, 2);
931         playlist_member_id_2_1 = (int)g_list_nth_data(playlist_member_id_list_1, 0);
932         playlist_member_id_2_2 = (int)g_list_nth_data(playlist_member_id_list_1, 1);
933 #endif
934         content_debug("playlist_member_id_1_1 [%d]", playlist_member_id_1_1);
935         content_debug("playlist_member_id_1_2 [%d]", playlist_member_id_1_2);
936         content_debug("playlist_member_id_1_3 [%d]", playlist_member_id_1_3);
937         content_debug("playlist_member_id_2_1 [%d]", playlist_member_id_2_1);
938         content_debug("playlist_member_id_2_2 [%d]", playlist_member_id_2_2);
939
940         media_playlist_get_play_order(playlist_1, playlist_member_id_1_1, &order_1);
941         media_playlist_get_play_order(playlist_1, playlist_member_id_1_2, &order_2);
942         media_playlist_get_play_order(playlist_1, playlist_member_id_1_3, &order_3);
943         media_playlist_get_play_order(playlist_2, playlist_member_id_2_1, &order_4);
944         media_playlist_get_play_order(playlist_2, playlist_member_id_2_2, &order_5);
945         content_debug("order_1 [%d] order_2 [%d] order_3 [%d] order_4 [%d] order_5 [%d]", order_1, order_2, order_3, order_4, order_5);
946
947         /* Update Playlist */
948         media_playlist_remove_media(playlist_2, playlist_member_id_2_1);
949         media_playlist_add_media(playlist_2, test_video_id);
950         media_playlist_set_name(playlist_2, "test_playlist");
951         media_playlist_set_play_order(playlist_2, playlist_member_id_2_2, order_5+100);
952         media_playlist_update_to_db(playlist_2);
953
954         /* Get Updated Playlist*/
955         media_playlist_foreach_playlist_from_db(filter, playlist_list_cb, NULL);
956
957         /* deletes the playlist */
958 #if 0
959         media_playlist_delete_from_db(playlist_id_1);
960         media_playlist_delete_from_db(playlist_id_2);
961 #endif
962
963         if (playlist_1 != NULL)
964                 media_playlist_destroy(playlist_1);
965         if (playlist_2 != NULL)
966                 media_playlist_destroy(playlist_2);
967         if (playlist_3 != NULL)
968                 media_playlist_destroy(playlist_3);
969         if (playlist_4 != NULL)
970                 media_playlist_destroy(playlist_4);
971
972         g_list_free(playlist_id_list);
973         g_list_free(playlist_member_id_list);
974         g_list_free(playlist_member_id_list_1);
975
976         if (filter != NULL)
977                 ret = media_filter_destroy(filter);
978         if (m_filter != NULL)
979                 ret = media_filter_destroy(m_filter);
980
981         return ret;
982 }
983
984 int test_playlist_operation_v2(void)
985 {
986         int ret = MEDIA_CONTENT_ERROR_NONE;
987         media_playlist_h playlist_1 = NULL;
988         const char *playlist_name_1 = "myPlaylist_1";
989         const char *playlist_thumb_path = tzplatform_mkpath(TZ_USER_IMAGES, "Default.jpg");
990         int playlist_id = 0;
991
992         content_debug("\n============Playlist Test V2============\n\n");
993
994         ret = media_playlist_create(&playlist_1);
995         if (ret != MEDIA_CONTENT_ERROR_NONE)
996                 content_error("error media_playlist_create : [%d]", ret);
997
998         ret = media_playlist_set_name(playlist_1, playlist_name_1);
999         if (ret != MEDIA_CONTENT_ERROR_NONE)
1000                 content_error("error media_playlist_set_name : [%d]", ret);
1001
1002         ret = media_playlist_set_thumbnail_path(playlist_1, playlist_thumb_path);
1003         if (ret != MEDIA_CONTENT_ERROR_NONE)
1004                 content_error("error media_playlist_set_thumbnail_path : [%d]", ret);
1005
1006         ret = media_playlist_insert_to_db_v2(playlist_1);
1007         if (ret != MEDIA_CONTENT_ERROR_NONE)
1008                 content_error("error media_playlist_insert_to_db_v2 : [%d]", ret);
1009
1010         ret = media_playlist_set_name(playlist_1, "myPlaylist_3");
1011         if (ret != MEDIA_CONTENT_ERROR_NONE)
1012                 content_error("error media_playlist_set_name : [%d]", ret);
1013
1014         ret = media_playlist_get_playlist_id(playlist_1, &playlist_id);
1015         if (ret != MEDIA_CONTENT_ERROR_NONE)
1016                 content_error("error media_playlist_get_playlist_id : [%d]", ret);
1017
1018         ret = media_playlist_update_to_db_v2(playlist_id, playlist_1);
1019         if (ret != MEDIA_CONTENT_ERROR_NONE)
1020                 content_error("error media_playlist_update_to_db_v2 : [%d]", ret);
1021
1022         ret = media_playlist_destroy(playlist_1);
1023         if (ret != MEDIA_CONTENT_ERROR_NONE)
1024                 content_error("error media_playlist_destroy : [%d]", ret);
1025
1026         return ret;
1027 }
1028
1029 static void __bookmark_handle_free(gpointer data)
1030 {
1031         media_bookmark_h handle = (media_bookmark_h) data;
1032         media_bookmark_destroy(handle);
1033 }
1034
1035 int test_bookmark_operation(void)
1036 {
1037         /* bookmark is only supported for video information. */
1038         int ret = MEDIA_CONTENT_ERROR_NONE;
1039         int bookmark_count = 0;
1040         filter_h filter;
1041         GList *all_item_list = NULL;
1042         int idx = 0;
1043
1044         content_debug("\n============Bookmark Test============\n\n");
1045
1046         const char *g_condition = "BOOKMARK_MARKED_TIME > 300";
1047
1048         ret = media_filter_create(&filter);
1049         if (ret != MEDIA_CONTENT_ERROR_NONE)
1050                 content_error("error media_filter_create : [%d]", ret);
1051
1052         ret = media_filter_set_condition(filter, g_condition, MEDIA_CONTENT_COLLATE_DEFAULT);
1053         if (ret != MEDIA_CONTENT_ERROR_NONE)
1054                 content_error("error media_filter_set_condition : [%d]", ret);
1055
1056         ret = media_filter_set_order(filter, MEDIA_CONTENT_ORDER_DESC, BOOKMARK_MARKED_TIME, MEDIA_CONTENT_COLLATE_DEFAULT);
1057         if (ret != MEDIA_CONTENT_ERROR_NONE)
1058                 content_error("error media_filter_set_order : [%d]", ret);
1059
1060         /* insert bookmark to video */
1061         const char *thumbnail_path1 = tzplatform_mkpath(TZ_USER_VIDEOS, "teat11.jpg");
1062         ret = media_bookmark_insert_to_db(test_video_id, 400, thumbnail_path1);
1063         if (ret != MEDIA_CONTENT_ERROR_NONE)
1064                 content_error("error media_bookmark_insert_to_db : [%d]", ret);
1065
1066         ret = media_bookmark_insert_to_db(test_video_id, 600, thumbnail_path1);
1067         if (ret != MEDIA_CONTENT_ERROR_NONE)
1068                 content_error("error media_bookmark_insert_to_db : [%d]", ret);
1069
1070         ret = media_bookmark_get_bookmark_count_from_db(filter, &bookmark_count);
1071         if (ret != MEDIA_CONTENT_ERROR_NONE)
1072                 content_error("error media_bookmark_get_bookmark_count_from_db : [%d]", ret);
1073         else
1074                 content_debug("bookmark_count = [%d]", bookmark_count);
1075
1076         ret = media_info_foreach_bookmark_from_db(test_video_id, NULL, bookmarks_cb, &all_item_list);
1077         if (ret != MEDIA_CONTENT_ERROR_NONE)
1078                 content_error("error media_info_foreach_bookmark_from_db : [%d]", ret);
1079
1080         for (idx = 0; idx < g_list_length(all_item_list); idx++) {
1081                 media_bookmark_h bookmark_handle;
1082                 bookmark_handle = (media_bookmark_h)g_list_nth_data(all_item_list, idx);
1083
1084                 ret = media_bookmark_set_name(bookmark_handle, "test 1");
1085                 if (ret != MEDIA_CONTENT_ERROR_NONE)
1086                         content_error("error media_bookmark_set_name : [%d]", ret);
1087
1088                 ret = media_bookmark_update_to_db(bookmark_handle);
1089                 if (ret != MEDIA_CONTENT_ERROR_NONE)
1090                         content_error("error media_bookmark_update_to_db : [%d]", ret);
1091         }
1092
1093         if (all_item_list)
1094                 g_list_free_full(all_item_list, __bookmark_handle_free);
1095
1096         ret = media_info_foreach_bookmark_from_db(test_video_id, NULL, bookmarks_cb, NULL);
1097         if (ret != MEDIA_CONTENT_ERROR_NONE)
1098                 content_error("error media_info_foreach_bookmark_from_db : [%d]", ret);
1099
1100         ret = media_filter_destroy(filter);
1101         if (ret != MEDIA_CONTENT_ERROR_NONE)
1102                 content_error("error media_filter_destroy : [%d]", ret);
1103
1104         return ret;
1105 }
1106
1107 int test_bookmark_operation_v2(void)
1108 {
1109         content_debug("\n============Bookmark Test V2============\n\n");
1110
1111         int ret = MEDIA_CONTENT_ERROR_NONE;
1112         media_bookmark_h bookmark = NULL;
1113         const char *thumbnail_path1 = tzplatform_mkpath(TZ_USER_VIDEOS, "teat11.jpg");
1114
1115         ret = media_bookmark_create(test_video_id, 400, &bookmark);
1116         if (ret != MEDIA_CONTENT_ERROR_NONE)
1117                 content_error("error media_bookmark_create : [%d]", ret);
1118
1119         ret = media_bookmark_set_name(bookmark, "test bookmark");
1120         if (ret != MEDIA_CONTENT_ERROR_NONE)
1121                 content_error("error media_bookmark_set_name : [%d]", ret);
1122
1123         ret = media_bookmark_set_thumbnail_path(bookmark, thumbnail_path1);
1124         if (ret != MEDIA_CONTENT_ERROR_NONE)
1125                 content_error("error media_bookmark_set_thumbnail_path : [%d]", ret);
1126
1127         ret = media_bookmark_insert_to_db_v2(bookmark);
1128         if (ret != MEDIA_CONTENT_ERROR_NONE)
1129                 content_error("error media_bookmark_insert_to_db_v2 : [%d]", ret);
1130
1131         ret = media_bookmark_set_name(bookmark, "test bookmark 2");
1132         if (ret != MEDIA_CONTENT_ERROR_NONE)
1133                 content_error("error media_bookmark_set_name : [%d]", ret);
1134
1135         ret = media_bookmark_update_to_db(bookmark);
1136         if (ret != MEDIA_CONTENT_ERROR_NONE)
1137                 content_error("error media_bookmark_update_to_db : [%d]", ret);
1138
1139         ret = media_bookmark_destroy(bookmark);
1140         if (ret != MEDIA_CONTENT_ERROR_NONE)
1141                 content_error("error media_bookmark_destroy : [%d]", ret);
1142
1143         return ret;
1144 }
1145
1146 int test_album_list(void)
1147 {
1148         content_debug("\n============Album Test============\n\n");
1149
1150         int ret = MEDIA_CONTENT_ERROR_NONE;
1151         int album_count = 0;
1152         filter_h filter;
1153
1154         /*Set Filter*/
1155         const char *condition = "MEDIA_TYPE=3"; /*0-image, 1-video, 2-sound, 3-music, 4-other*/
1156
1157         ret = media_filter_create(&filter);
1158         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1159                 content_error("Fail to create filter");
1160                 return ret;
1161         }
1162         ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
1163         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1164                 media_filter_destroy(filter);
1165                 content_error("Fail to set condition");
1166                 return ret;
1167         }
1168         ret = media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_ALBUM, MEDIA_CONTENT_COLLATE_NOCASE);
1169         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1170                 media_filter_destroy(filter);
1171                 content_error("Fail to set order");
1172                 return ret;
1173         }
1174
1175         ret = media_album_get_album_count_from_db(filter, &album_count);
1176         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1177                 media_filter_destroy(filter);
1178                 return ret;
1179         } else {
1180                 content_debug("album_count [%d]", album_count);
1181         }
1182
1183         ret = media_album_foreach_album_from_db(filter, album_list_cb, NULL);
1184         if (ret != MEDIA_CONTENT_ERROR_NONE)
1185                 content_error("error media_album_foreach_album_from_db : [%d]", ret);
1186
1187         ret = media_filter_destroy(filter);
1188         if (ret != MEDIA_CONTENT_ERROR_NONE)
1189                 content_error("error media_filter_destroy : [%d]", ret);
1190
1191         return ret;
1192 }
1193
1194 int test_group_operation(void)
1195 {
1196         content_debug("\n============Group Test============\n\n");
1197
1198         int ret = MEDIA_CONTENT_ERROR_NONE;
1199         int group_count = 0;
1200         int idx = 0;
1201
1202         ret = test_filter_create();
1203         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1204                 content_error("[error(0x%08x)", ret);
1205                 return ret;
1206         }
1207
1208         for (idx = 0; idx < MEDIA_CONTENT_GROUP_MAX; idx++) {
1209                 ret = media_group_get_group_count_from_db(g_filter, idx, &group_count);
1210                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1211                         test_filter_destroy();
1212                         content_error("media_group_get_group_count_from_db fail. ret=[%d] idx=[%d]", ret, idx);
1213                         return ret;
1214                 } else {
1215                         content_debug("[%2d]group_count [%d]", idx, group_count);
1216                 }
1217
1218                 ret = media_group_foreach_group_from_db(g_filter, idx, group_list_cb, &idx);
1219                 if (ret != MEDIA_CONTENT_ERROR_NONE)
1220                         content_error("media_group_foreach_group_from_db failed: %d", ret);
1221         }
1222         ret = test_filter_destroy();
1223
1224         return ret;
1225 }
1226
1227 int test_update_operation()
1228 {
1229         int ret = MEDIA_CONTENT_ERROR_NONE;
1230         unsigned int i = 0;
1231         media_info_h media_handle = NULL;
1232         GList *all_item_list = NULL;
1233
1234         /* Get all item list */
1235         ret = media_info_foreach_media_from_db(NULL, gallery_media_item_cb, &all_item_list);
1236         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1237                 content_error("media_info_foreach_media_from_db failed: %d", ret);
1238                 return -1;
1239         } else {
1240                 content_debug("media_info_foreach_media_from_db success");
1241
1242                 for (i = 0; i < g_list_length(all_item_list); i++) {
1243                         media_handle = (media_info_h)g_list_nth_data(all_item_list, i);
1244                         get_media_meta(media_handle);
1245                 }
1246         }
1247
1248         return MEDIA_CONTENT_ERROR_NONE;
1249 }
1250
1251 int test_insert(void)
1252 {
1253         int ret = MEDIA_CONTENT_ERROR_NONE;
1254         const char *path = tzplatform_mkpath(TZ_USER_IMAGES, "Default.jpg");
1255 #if 0
1256         const char *path = tzplatform_mkpath(TZ_USER_DOCUMENTS, "other.txt"));
1257         char *path = NULL;
1258 #endif
1259         media_info_h media_item = NULL;
1260         content_debug("\n============DB Insert Test============\n\n");
1261
1262         ret = media_info_insert_to_db(path, &media_item);
1263
1264         if ((ret == MEDIA_CONTENT_ERROR_NONE) && (media_item != NULL)) {
1265                 content_debug("Insertion is success");
1266         } else {
1267                 content_error("Insertion is failed");
1268                 ret = media_info_destroy(media_item);
1269                 return ret;
1270         }
1271
1272         char *media_id = NULL;
1273
1274         ret = media_info_get_media_id(media_item, &media_id);
1275         if (ret != MEDIA_CONTENT_ERROR_NONE)
1276                 content_error("media_info_get_media_id failed: %d", ret);
1277         else
1278                 content_debug("Media ID: %s", media_id);
1279
1280         SAFE_FREE(media_id);
1281
1282         ret = media_info_destroy(media_item);
1283         if (ret != MEDIA_CONTENT_ERROR_NONE)
1284                 content_error("media_info_destroy failed: %d", ret);
1285
1286         return ret;
1287 }
1288
1289 int test_move(void)
1290 {
1291         int ret = MEDIA_CONTENT_ERROR_NONE;
1292         const char *move_media_id = "60aea677-4742-408e-b5f7-f2628062d06d";
1293         const char *dst_path = tzplatform_mkpath(TZ_USER_IMAGES, "XX/Default1.jpg");
1294         media_info_h move_media = NULL;
1295
1296         ret = media_info_get_media_from_db(move_media_id, &move_media);
1297         if (ret == MEDIA_CONTENT_ERROR_NONE)
1298                 content_debug("media_info_get_media_from_db success");
1299         else
1300                 content_error("media_info_get_media_from_db failed: %d", ret);
1301
1302         content_debug("\n============DB Move Test============\n\n");
1303
1304         if (move_media) {
1305                 ret = media_info_move_to_db(move_media, dst_path);
1306
1307                 if (ret == MEDIA_CONTENT_ERROR_NONE)
1308                         content_debug("Move is success");
1309                 else
1310                         content_error("Move is failed");
1311
1312                 ret = media_info_destroy(move_media);
1313         } else {
1314                 content_debug("There is no item : %s", move_media_id);
1315         }
1316
1317         return ret;
1318 }
1319
1320 int test_create_thumbnail(const char *path)
1321 {
1322         int ret = MEDIA_CONTENT_ERROR_NONE;
1323         media_info_h media;
1324         char *thumb_path = NULL;
1325
1326         ret = media_info_insert_to_db(path, &media);
1327         content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, ret, "media_info_insert_to_db failed [%d]", ret);
1328
1329         ret = media_info_generate_thumbnail(media);
1330         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1331                 content_error("media_info_generate_thumbnail failed [%d]", ret);
1332                 goto FINALIZE;
1333         }
1334
1335         ret = media_info_get_thumbnail_path(media, &thumb_path);
1336         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1337                 content_error("media_info_get_thumbnail_path failed [%d]", ret);
1338                 goto FINALIZE;
1339         }
1340
1341         content_debug("Thumbnail Path [%s]", thumb_path);
1342         if (thumb_path)
1343                 free(thumb_path);
1344
1345 FINALIZE:
1346         media_info_destroy(media);
1347         return ret;
1348 }
1349
1350 int test_ebook_text_finder(const char *keyword)
1351 {
1352         int ret = MEDIA_CONTENT_ERROR_NONE;
1353         char **book_path_list = NULL;
1354         unsigned int book_path_len = 0;
1355         unsigned int i = 0;
1356         media_info_h media = NULL;
1357         book_meta_h book = NULL;
1358         char *s_value = NULL;
1359         long long ms_time = 0;
1360         struct timeval start_time;
1361         struct timeval end_time;
1362
1363         gettimeofday(&start_time, NULL);
1364
1365         ret = book_meta_get_path_with_keyword(keyword, &book_path_list, &book_path_len);
1366         if (ret == MEDIA_CONTENT_ERROR_NONE)
1367                 content_debug("book_meta_get_path_with_keyword is success");
1368         else
1369                 content_error("book_meta_get_path_with_keyword is failed");
1370
1371         gettimeofday(&end_time, NULL);
1372
1373         for (i = 0; i < book_path_len; i++) {
1374                 ret = media_info_get_media_from_db_by_path(book_path_list[i], &media);
1375                 if (ret != MEDIA_CONTENT_ERROR_NONE)
1376                         continue;
1377
1378                 content_debug("===============================");
1379                 content_debug("[%d] %s", i, book_path_list[i]);
1380                 content_debug("===============================");
1381                 media_info_get_title(media, &s_value);
1382                 content_debug("Title  : %s", s_value);
1383                 g_free(s_value);
1384                 s_value = NULL;
1385
1386                 ret = media_info_get_book(media, &book);
1387                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1388                         media_info_destroy(media);
1389                         continue;
1390                 }
1391
1392                 ret = book_meta_get_author(book, &s_value);
1393                 if (ret == MEDIA_CONTENT_ERROR_NONE && s_value) {
1394                         content_debug("Author : %s", s_value);
1395                         g_free(s_value);
1396                         s_value = NULL;
1397                 }
1398
1399                 ret = book_meta_get_date(book, &s_value);
1400                 if (ret == MEDIA_CONTENT_ERROR_NONE && s_value) {
1401                         content_debug("Date   : %s", s_value);
1402                         g_free(s_value);
1403                         s_value = NULL;
1404                 }
1405
1406                 content_debug("===============================");
1407                 ret = book_meta_destroy(book);
1408                 if (ret != MEDIA_CONTENT_ERROR_NONE)
1409                         content_error("book_meta_destroy failed");
1410
1411                 ret = media_info_destroy(media);
1412                 if (ret != MEDIA_CONTENT_ERROR_NONE)
1413                         content_error("media_info_destroy failed");
1414
1415                 g_free(book_path_list[i]);
1416         }
1417
1418         ms_time = (end_time.tv_sec * 1000LL + end_time.tv_usec / 1000) - (start_time.tv_sec * 1000LL + start_time.tv_usec/ 1000);
1419         content_debug("Search Time [%lld]", ms_time);
1420
1421         g_free(book_path_list);
1422
1423         return ret;
1424 }
1425 int test_disconnect_database(void)
1426 {
1427         int ret = MEDIA_CONTENT_ERROR_NONE;
1428         content_debug("\n============DB Disconnection Test============\n\n");
1429
1430         ret = media_content_disconnect();
1431
1432         if (ret == MEDIA_CONTENT_ERROR_NONE)
1433                 content_debug("disconnection is success");
1434         else
1435                 content_error("disconnection is failed");
1436
1437         return ret;
1438 }
1439
1440 static int g_total_photo_size = 0;
1441 static int g_total_video_size = 0;
1442 static int g_total_mp3_size = 0;
1443 static int g_total_voice_memo_size = 0;
1444
1445 bool dft_cb(media_info_h media, void *user_data)
1446 {
1447         unsigned long long file_size = 0;
1448         media_content_type_e media_type = -1;
1449         char *mime_type = NULL;
1450         int ret = MEDIA_CONTENT_ERROR_NONE;
1451
1452         if (media == NULL)
1453                 return true;
1454
1455         ret = media_info_get_media_type(media, &media_type);
1456         if (ret != MEDIA_CONTENT_ERROR_NONE)
1457                 content_error("media_info_get_media_type failed: %d", ret);
1458         ret = media_info_get_size(media, &file_size);
1459         if (ret != MEDIA_CONTENT_ERROR_NONE)
1460                 content_error("media_info_get_size failed: %d", ret);
1461         ret = media_info_get_mime_type(media, &mime_type);
1462         if (ret != MEDIA_CONTENT_ERROR_NONE)
1463                 content_error("media_info_get_mime_type failed: %d", ret);
1464
1465         if (media_type == MEDIA_CONTENT_TYPE_IMAGE)
1466                 g_total_photo_size += file_size;
1467         else if (media_type == MEDIA_CONTENT_TYPE_VIDEO)
1468                 g_total_video_size += file_size;
1469         else if (media_type == MEDIA_CONTENT_TYPE_SOUND)
1470                 g_total_voice_memo_size += file_size;
1471         else if (media_type == MEDIA_CONTENT_TYPE_MUSIC) {
1472                 if ((mime_type != NULL) && (!strcmp("audio/mpeg", mime_type)))
1473                         g_total_mp3_size += file_size;
1474                 else
1475                         g_total_voice_memo_size += file_size;
1476         } else
1477                 content_debug("invalid media_type");
1478
1479         SAFE_FREE(mime_type);
1480
1481         return true;
1482
1483 }
1484
1485 int DFT_test(void)
1486 {
1487         int ret = MEDIA_CONTENT_ERROR_NONE;
1488         content_debug("\n============DFT_test============\n\n");
1489
1490         filter_h filter = NULL;
1491         int media_cnt = 0;
1492
1493         /*MEDIA_TYPE 0-image, 1-video, 2-sound, 3-music, 4-other*/
1494
1495         ret = media_filter_create(&filter);
1496
1497 /*Internal Memory*/
1498         content_debug("[Internal Memory]\n");
1499         /*1. Photo ============================================================*/
1500         ret = media_filter_set_condition(filter, "MEDIA_TYPE=0", MEDIA_CONTENT_COLLATE_DEFAULT);
1501
1502         /*Get Photo Count*/
1503         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1504         content_debug("Photo count = [%d]\n", media_cnt);
1505
1506         /*Get Photo Size*/
1507         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1508         content_debug("Photo size = [%d]\n", g_total_photo_size);
1509
1510         /*2. Video ============================================================*/
1511         ret = media_filter_set_condition(filter, "MEDIA_TYPE=1", MEDIA_CONTENT_COLLATE_DEFAULT);
1512
1513         /*Get Video Count*/
1514         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1515         content_debug("Video count = [%d]\n", media_cnt);
1516
1517         /*Get Video Size*/
1518         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1519         content_debug("Video size = [%d]\n", g_total_video_size);
1520
1521         /*3. MP3 ============================================================*/
1522         ret = media_filter_set_condition(filter, "MEDIA_TYPE=3 AND MEDIA_MIME_TYPE=\"audio/mpeg\"", MEDIA_CONTENT_COLLATE_DEFAULT);
1523
1524         /*Get MP3 Count*/
1525         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1526         content_debug("MP3 count = [%d]\n", media_cnt);
1527
1528         /*Get MP3 Size*/
1529         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1530         content_debug("MP3 size = [%d]\n", g_total_mp3_size);
1531
1532         /*4. Voice Memo ============================================================*/
1533         ret = media_filter_set_condition(filter, "(MEDIA_MIME_TYPE=\"audio/AMR\" OR MEDIA_MIME_TYPE=\"audio/mp4\")", MEDIA_CONTENT_COLLATE_DEFAULT);
1534
1535         /*Get Voice Memo Count*/
1536         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1537         content_debug("Voice Memo count = [%d]\n", media_cnt);
1538
1539         /*Get Voice Memo Size*/
1540         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1541         content_debug("Voice Memo size = [%d]\n", g_total_voice_memo_size);
1542
1543         g_total_photo_size = 0;
1544         g_total_video_size = 0;
1545         g_total_mp3_size = 0;
1546         g_total_voice_memo_size = 0;
1547
1548 /*External Memory*/
1549         content_debug("\n[External Memory]\n");
1550         /*1. Photo ============================================================*/
1551         ret = media_filter_set_condition(filter, "MEDIA_TYPE=0", MEDIA_CONTENT_COLLATE_DEFAULT);
1552
1553         /*Get Photo Count*/
1554         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1555         content_debug("Photo count = [%d]\n", media_cnt);
1556
1557         /*Get Photo Size*/
1558         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1559         content_debug("Photo size = [%d]\n", g_total_photo_size);
1560
1561         /*2. Video ============================================================*/
1562         ret = media_filter_set_condition(filter, "MEDIA_TYPE=1", MEDIA_CONTENT_COLLATE_DEFAULT);
1563
1564         /*Get Video Count*/
1565         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1566         content_debug("Video count = [%d]\n", media_cnt);
1567
1568         /*Get Video Size*/
1569         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1570         content_debug("Video size = [%d]\n", g_total_video_size);
1571
1572         /*3. MP3 ============================================================*/
1573         ret = media_filter_set_condition(filter, "MEDIA_TYPE=3 AND MEDIA_MIME_TYPE=\"audio/mpeg\"", MEDIA_CONTENT_COLLATE_DEFAULT);
1574
1575         /*Get MP3 Count*/
1576         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1577         content_debug("MP3 count = [%d]\n", media_cnt);
1578
1579         /*Get MP3 Size*/
1580         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1581         content_debug("MP3 size = [%d]\n", g_total_mp3_size);
1582
1583         /*4. Voice Memo ============================================================*/
1584         ret = media_filter_set_condition(filter, "(MEDIA_MIME_TYPE=\"audio/AMR\" OR MEDIA_MIME_TYPE=\"audio/mp4\")", MEDIA_CONTENT_COLLATE_DEFAULT);
1585
1586         /*Get Voice Memo Count*/
1587         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1588         content_debug("Voice Memo count = [%d]\n", media_cnt);
1589
1590         /*Get Voice Memo Size*/
1591         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1592         content_debug("Voice Memo size = [%d]\n", g_total_voice_memo_size);
1593         ret = media_filter_destroy(filter);
1594
1595         return ret;
1596 }
1597
1598 void insert_batch_cb(media_content_error_e error, void * user_data)
1599 {
1600         content_debug("media_info_insert_batch_to_db completed!\n");
1601 }
1602
1603 int test_batch_operations()
1604 {
1605         int ret = -1;
1606         int i = 0;
1607         char *file_list[10];
1608
1609         for (i = 0; i < 10; i++) {
1610                 file_list[i] = g_strdup_printf("%s%d.jpg", tzplatform_mkpath(TZ_USER_CONTENT, "test/image"), i+1);
1611                 content_debug("File : %s\n", file_list[i]);
1612         }
1613
1614         ret = media_info_insert_batch_to_db((const char **)file_list, 10, insert_batch_cb, NULL);
1615         if (ret != MEDIA_CONTENT_ERROR_NONE)
1616                 content_error("media_info_insert_batch_to_db failed : %d\n", ret);
1617
1618         for (i = 0; i < 10; i++)
1619                 g_free(file_list[i]);
1620
1621         return ret;
1622 }
1623
1624 void _scan_cb(media_content_error_e err, void *user_data)
1625 {
1626         content_debug("scan callback is called : %d\n", err);
1627         g_main_loop_quit(g_loop);
1628
1629         return;
1630 }
1631
1632 int test_scan_file()
1633 {
1634         int ret = -1;
1635
1636         const char *file_path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");
1637         ret = media_content_scan_file(file_path);
1638         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1639                 content_error("Fail to media_content_scan_file : %d", ret);
1640                 return ret;
1641         }
1642
1643         return 0;
1644 }
1645
1646 gboolean test_scan_dir_start(gpointer data)
1647 {
1648         int ret = -1;
1649
1650         const char *dir_path = tzplatform_getenv(TZ_USER_CONTENT);
1651
1652         ret = media_content_scan_folder(dir_path, TRUE, _scan_cb, NULL);
1653
1654         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1655                 content_error("Fail to test_scan_dir_start : %d", ret);
1656                 return ret;
1657         }
1658
1659         return 0;
1660 }
1661
1662 gboolean cancel_scan_dir_start(gpointer data)
1663 {
1664         int ret = MEDIA_CONTENT_ERROR_NONE;
1665
1666         const char *dir_path = tzplatform_getenv(TZ_USER_IMAGES);
1667
1668         ret = media_content_cancel_scan_folder(dir_path);
1669
1670         if (ret == MEDIA_CONTENT_ERROR_NONE)
1671                 content_debug("media_content_cancel_scan_folder is success");
1672         else
1673                 content_error("media_content_cancel_scan_folder is failed");
1674
1675         return false;
1676 }
1677
1678 int test_scan_dir(int cancel)
1679 {
1680         GSource *source = NULL;
1681         GMainContext *context = NULL;
1682
1683         g_loop = g_main_loop_new(NULL, FALSE);
1684         context = g_main_loop_get_context(g_loop);
1685         source = g_idle_source_new();
1686         g_source_set_callback(source, test_scan_dir_start, NULL, NULL);
1687         g_source_attach(source, context);
1688
1689         if (cancel) {
1690                 GSource *cancel_src = NULL;
1691                 cancel_src = g_idle_source_new();
1692                 g_source_set_callback(cancel_src, cancel_scan_dir_start, NULL, NULL);
1693                 g_source_attach(cancel_src, context);
1694         }
1695
1696         g_main_loop_run(g_loop);
1697         g_main_loop_unref(g_loop);
1698
1699         return 0;
1700 }
1701
1702 void _noti_cb(media_content_error_e error,
1703                                 int pid,
1704                                 media_content_db_update_item_type_e update_item,
1705                                 media_content_db_update_type_e update_type,
1706                                 media_content_type_e media_type,
1707                                 char *uuid,
1708                                 char *path,
1709                                 char *mime_type,
1710                                 void *user_data)
1711 {
1712         if (error == 0)
1713                 content_debug("noti success! : %d\n", error);
1714         else
1715                 content_debug("error occurred! : %d\n", error);
1716
1717         content_debug("Noti from PID(%d)\n", pid);
1718
1719         if (update_item == MEDIA_ITEM_FILE)
1720                 content_debug("Noti item : MEDIA_ITEM_FILE\n");
1721         else if (update_item == MEDIA_ITEM_DIRECTORY)
1722                 content_debug("Noti item : MEDIA_ITEM_DIRECTORY\n");
1723
1724         if (update_type == MEDIA_CONTENT_INSERT)
1725                 content_debug("Noti type : MEDIA_CONTENT_INSERT\n");
1726         else if (update_type == MEDIA_CONTENT_DELETE)
1727                 content_debug("Noti type : MEDIA_CONTENT_DELETE\n");
1728         else if (update_type == MEDIA_CONTENT_UPDATE)
1729                 content_debug("Noti type : MEDIA_CONTENT_UPDATE\n");
1730
1731         content_debug("content type : %d\n", media_type);
1732
1733         if (path)
1734                 content_debug("path : %s\n", path);
1735         else
1736                 content_debug("path not\n");
1737
1738         if (uuid)
1739                 content_debug("uuid : %s\n", uuid);
1740         else
1741                 content_debug("uuid not\n");
1742
1743         if (mime_type)
1744                 content_debug("mime_type : %s\n", mime_type);
1745         else
1746                 content_debug("mime not\n");
1747
1748         if (user_data) content_debug("String : %s\n", (char *)user_data);
1749
1750         return;
1751 }
1752
1753 void _noti_cb_2(media_content_error_e error,
1754                                 int pid,
1755                                 media_content_db_update_item_type_e update_item,
1756                                 media_content_db_update_type_e update_type,
1757                                 media_content_type_e media_type,
1758                                 char *uuid,
1759                                 char *path,
1760                                 char *mime_type,
1761                                 void *user_data)
1762 {
1763         if (error == 0)
1764                 content_debug("noti_2 success! : %d\n", error);
1765         else
1766                 content_debug("error occurred! : %d\n", error);
1767
1768         content_debug("Noti_2 from PID(%d)\n", pid);
1769
1770         g_main_loop_quit(g_loop);
1771         return;
1772 }
1773
1774 static media_content_noti_h noti_h;
1775 static media_content_noti_h noti_h_2;
1776
1777 gboolean _send_noti_operations(gpointer data)
1778 {
1779         int ret = MEDIA_CONTENT_ERROR_NONE;
1780
1781         /* First of all, noti subscription */
1782         char *user_str = strdup("hi");
1783         media_content_add_db_updated_cb(_noti_cb, (void*)user_str, &noti_h);
1784         media_content_add_db_updated_cb(_noti_cb_2, (void*)user_str, &noti_h_2);
1785
1786         /* media_info_insert_to_db */
1787         media_info_h media_item = NULL;
1788         const char *path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");
1789
1790         ret = media_info_insert_to_db(path, &media_item);
1791         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1792                 content_error("media_info_insert_to_db failed : %d", ret);
1793                 media_info_destroy(media_item);
1794                 return FALSE;
1795         }
1796
1797         content_debug("media_info_insert_to_db success");
1798
1799         media_content_remove_db_updated_cb(noti_h);
1800
1801         media_info_destroy(media_item);
1802
1803         return FALSE;
1804 }
1805
1806 int test_noti()
1807 {
1808         int ret = MEDIA_CONTENT_ERROR_NONE;
1809         GSource *source = NULL;
1810         GMainContext *context = NULL;
1811
1812         g_loop = g_main_loop_new(NULL, FALSE);
1813         context = g_main_loop_get_context(g_loop);
1814         source = g_idle_source_new();
1815         g_source_set_callback(source, _send_noti_operations, NULL, NULL);
1816         g_source_attach(source, context);
1817
1818         g_main_loop_run(g_loop);
1819         g_main_loop_unref(g_loop);
1820
1821         test_filter_destroy();
1822         media_content_remove_db_updated_cb(noti_h_2);
1823
1824         return ret;
1825 }
1826 #ifdef _USE_TVPD_MODE
1827 filter_h g_tv_filter = NULL;
1828
1829 int test_tv_filter_create(void)
1830 {
1831         content_debug("\n============Filter Create============\n\n");
1832
1833         int ret = MEDIA_CONTENT_ERROR_NONE;
1834
1835         ret = media_filter_create(&g_tv_filter);
1836
1837         ret = media_filter_set_storage(g_tv_filter, "0a22a163-e634-4a2e-ba14-0469a969eea0");
1838
1839         ret = media_filter_set_order(g_tv_filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_TITLE, MEDIA_CONTENT_COLLATE_DEFAULT);
1840
1841         return ret;
1842 }
1843
1844 int test_tv_filter_destroy(void)
1845 {
1846         content_debug("\n============Filter Destroy============\n\n");
1847
1848         int ret = MEDIA_CONTENT_ERROR_NONE;
1849
1850         ret = media_filter_destroy(g_tv_filter);
1851
1852         return ret;
1853 }
1854
1855 bool pvr_item_cb(media_pvr_h pvr, void *user_data)
1856 {
1857         int ret = MEDIA_CONTENT_ERROR_NONE;
1858         char *c_value = NULL;
1859         int i_value = 0;
1860         bool b_value = false;
1861         unsigned long long l_value = 0;
1862
1863         if (pvr == NULL) {
1864                 content_debug("NO Item");
1865                 return true;
1866         }
1867
1868         ret = media_pvr_get_media_id(pvr, &c_value);
1869         if (ret != MEDIA_CONTENT_ERROR_NONE)
1870                 content_error("Fail to media_pvr_get_media_id");
1871         content_debug("media_id [%s]", c_value);
1872         SAFE_FREE(c_value);
1873
1874         ret = media_pvr_get_channel_name(pvr, &c_value);
1875         if (ret != MEDIA_CONTENT_ERROR_NONE)
1876                 content_error("Fail to media_pvr_get_channel_name");
1877         content_debug("channel_name [%s]", c_value);
1878         SAFE_FREE(c_value);
1879
1880         ret = media_pvr_get_program_title(pvr, &c_value);
1881         if (ret != MEDIA_CONTENT_ERROR_NONE)
1882                 content_error("Fail to media_pvr_get_program_title");
1883         content_debug("program_title [%s]", c_value);
1884         SAFE_FREE(c_value);
1885
1886         ret = media_pvr_get_program_crid(pvr, &c_value);
1887         if (ret != MEDIA_CONTENT_ERROR_NONE)
1888                 content_error("Fail to media_pvr_get_program_crid");
1889         content_debug("program_crid [%s]", c_value);
1890         SAFE_FREE(c_value);
1891
1892         ret = media_pvr_get_guidance(pvr, &c_value);
1893         if (ret != MEDIA_CONTENT_ERROR_NONE)
1894                 content_error("Fail to media_pvr_get_guidance");
1895         content_debug("guidance [%s]", c_value);
1896         SAFE_FREE(c_value);
1897
1898         ret = media_pvr_get_synopsis(pvr, &c_value);
1899         if (ret != MEDIA_CONTENT_ERROR_NONE)
1900                 content_error("Fail to media_pvr_get_synopsis");
1901         content_debug("synopsis [%s]", c_value);
1902         SAFE_FREE(c_value);
1903
1904         ret = media_pvr_get_genre(pvr, &c_value);
1905         if (ret != MEDIA_CONTENT_ERROR_NONE)
1906                 content_error("Fail to media_pvr_get_synopsis");
1907         content_debug("genre [%s]", c_value);
1908         SAFE_FREE(c_value);
1909
1910         ret = media_pvr_get_language(pvr, &c_value);
1911         if (ret != MEDIA_CONTENT_ERROR_NONE)
1912                 content_error("Fail to media_pvr_get_language");
1913         content_debug("language [%s]", c_value);
1914         SAFE_FREE(c_value);
1915
1916         ret = media_pvr_get_path(pvr, &c_value);
1917         if (ret != MEDIA_CONTENT_ERROR_NONE)
1918                 content_error("Fail to media_pvr_get_path");
1919         content_debug("path [%s]", c_value);
1920         SAFE_FREE(c_value);
1921
1922         ret = media_pvr_get_storage_id(pvr, &c_value);
1923         if (ret != MEDIA_CONTENT_ERROR_NONE)
1924                 content_error("Fail to media_pvr_get_storage_id");
1925         content_debug("storage_id [%s]", c_value);
1926         SAFE_FREE(c_value);
1927
1928         ret = media_pvr_get_size(pvr, &l_value);
1929         if (ret != MEDIA_CONTENT_ERROR_NONE)
1930                 content_error("Fail to media_pvr_get_size");
1931         content_debug("size [%lld]", l_value);
1932
1933         ret = media_pvr_get_timezone(pvr, &i_value);
1934         if (ret != MEDIA_CONTENT_ERROR_NONE)
1935                 content_error("Fail to media_pvr_get_timezone");
1936         content_debug("timezone [%d]", i_value);
1937
1938         ret = media_pvr_get_ptc(pvr, &i_value);
1939         if (ret != MEDIA_CONTENT_ERROR_NONE)
1940                 content_error("Fail to media_pvr_get_ptc");
1941         content_debug("ptc [%d]", i_value);
1942
1943         ret = media_pvr_get_major(pvr, &i_value);
1944         if (ret != MEDIA_CONTENT_ERROR_NONE)
1945                 content_error("Fail to media_pvr_get_major");
1946         content_debug("[%d]", i_value);
1947
1948         ret = media_pvr_get_minor(pvr, &i_value);
1949         if (ret != MEDIA_CONTENT_ERROR_NONE)
1950                 content_error("Fail to media_pvr_get_minor");
1951         content_debug("minor [%d]", i_value);
1952
1953         ret = media_pvr_get_channel_type(pvr, &i_value);
1954         if (ret != MEDIA_CONTENT_ERROR_NONE)
1955                 content_error("Fail to media_pvr_get_channel_type");
1956         content_debug("channel_type [%d]", i_value);
1957
1958         ret = media_pvr_get_program_num(pvr, &i_value);
1959         if (ret != MEDIA_CONTENT_ERROR_NONE)
1960                 content_error("Fail to media_pvr_get_program_num");
1961         content_debug("program_num [%d]", i_value);
1962
1963         unsigned int ui_value = 0;
1964         ret = media_pvr_get_service_profile(pvr, &ui_value);
1965         if (ret != MEDIA_CONTENT_ERROR_NONE)
1966                 content_error("Fail to media_pvr_get_service_profile");
1967         content_debug("service_profile [%u]", ui_value);
1968
1969         ret = media_pvr_get_duration(pvr, &i_value);
1970         if (ret != MEDIA_CONTENT_ERROR_NONE)
1971                 content_error("Fail to media_pvr_get_duration");
1972         content_debug("duration [%d]", i_value);
1973
1974         ret = media_pvr_get_embargo_time(pvr, &i_value);
1975         if (ret != MEDIA_CONTENT_ERROR_NONE)
1976                 content_error("Fail to media_pvr_get_embargo_time");
1977         content_debug("embargo_time [%d]", i_value);
1978
1979         ret = media_pvr_get_expiry_time(pvr, &i_value);
1980         if (ret != MEDIA_CONTENT_ERROR_NONE)
1981                 content_error("Fail to media_pvr_get_expiry_time");
1982         content_debug("expiry_time [%d]", i_value);
1983
1984         ret = media_pvr_get_parental_rating(pvr, &i_value);
1985         if (ret != MEDIA_CONTENT_ERROR_NONE)
1986                 content_error("Fail to media_pvr_get_parental_rating");
1987         content_debug("parental_rating [%d]", i_value);
1988
1989         ret = media_pvr_get_start_time(pvr, &i_value);
1990         if (ret != MEDIA_CONTENT_ERROR_NONE)
1991                 content_error("Fail to media_pvr_get_start_time");
1992         content_debug("start_time [%d]", i_value);
1993
1994         ret = media_pvr_get_program_start_time(pvr, &i_value);
1995         if (ret != MEDIA_CONTENT_ERROR_NONE)
1996                 content_error("Fail to media_pvr_get_program_start_time");
1997         content_debug("program_start_time [%d]", i_value);
1998
1999         ret = media_pvr_get_program_end_time(pvr, &i_value);
2000         if (ret != MEDIA_CONTENT_ERROR_NONE)
2001                 content_error("Fail to media_pvr_get_program_end_time");
2002         content_debug("program_end_time [%d]", i_value);
2003
2004         ret = media_pvr_get_program_date(pvr, &i_value);
2005         if (ret != MEDIA_CONTENT_ERROR_NONE)
2006                 content_error("Fail to media_pvr_get_program_date");
2007         content_debug("program_date [%d]", i_value);
2008
2009         ret = media_pvr_get_timer_record(pvr, &b_value);
2010         if (ret != MEDIA_CONTENT_ERROR_NONE)
2011                 content_error("Fail to media_pvr_get_timer_record");
2012         content_debug("timer_record [%d]", b_value);
2013
2014         ret = media_pvr_get_series_record(pvr, &b_value);
2015         if (ret != MEDIA_CONTENT_ERROR_NONE)
2016                 content_error("Fail to media_pvr_get_series_record");
2017         content_debug("series_record [%d]", b_value);
2018
2019         ret = media_pvr_get_hd(pvr, &i_value);
2020         if (ret != MEDIA_CONTENT_ERROR_NONE)
2021                 content_error("Fail to media_pvr_get_hd");
2022         content_debug("hd [%d]", i_value);
2023
2024         ret = media_pvr_get_subtitle(pvr, &b_value);
2025         if (ret != MEDIA_CONTENT_ERROR_NONE)
2026                 content_error("Fail to media_pvr_get_subtitle");
2027         content_debug("subtitle [%d]", b_value);
2028
2029         ret = media_pvr_get_ttx(pvr, &b_value);
2030         if (ret != MEDIA_CONTENT_ERROR_NONE)
2031                 content_error("Fail to media_pvr_get_ttx");
2032         content_debug("ttx [%d]", b_value);
2033
2034         ret = media_pvr_get_ad(pvr, &b_value);
2035         if (ret != MEDIA_CONTENT_ERROR_NONE)
2036                 content_error("Fail to media_pvr_get_ad");
2037         content_debug("ad [%d]", b_value);
2038
2039         ret = media_pvr_get_hard_of_hearing_radio(pvr, &b_value);
2040         if (ret != MEDIA_CONTENT_ERROR_NONE)
2041                 content_error("Fail to media_pvr_get_hard_of_hearing_radio");
2042         content_debug("hard_of_hearing_radio [%d]", b_value);
2043
2044         ret = media_pvr_get_data_service(pvr, &b_value);
2045         if (ret != MEDIA_CONTENT_ERROR_NONE)
2046                 content_error("Fail to media_pvr_get_data_service");
2047         content_debug("data_service [%d]", b_value);
2048
2049         ret = media_pvr_get_content_lock(pvr, &b_value);
2050         if (ret != MEDIA_CONTENT_ERROR_NONE)
2051                 content_error("Fail to media_pvr_get_content_lock");
2052         content_debug("content_lock [%d]", b_value);
2053
2054         ret = media_pvr_get_content_watch(pvr, &b_value);
2055         if (ret != MEDIA_CONTENT_ERROR_NONE)
2056                 content_error("Fail to media_pvr_get_content_watch");
2057         content_debug("content_watch [%d]", b_value);
2058
2059         ret = media_pvr_get_has_audio_only(pvr, &b_value);
2060         if (ret != MEDIA_CONTENT_ERROR_NONE)
2061                 content_error("Fail to media_pvr_get_has_audio_only");
2062         content_debug("has_audio_only [%d]", b_value);
2063
2064         ret = media_pvr_get_is_local_record(pvr, &b_value);
2065         if (ret != MEDIA_CONTENT_ERROR_NONE)
2066                 content_error("Fail to media_pvr_get_is_local_record");
2067         content_debug("is_local_record [%d]", b_value);
2068
2069         ret = media_pvr_get_resolution(pvr, (media_pvr_resolution_e*)&i_value);
2070         if (ret != MEDIA_CONTENT_ERROR_NONE)
2071                 content_error("Fail to media_pvr_get_resolution");
2072         content_debug("resolution [%d]", i_value);
2073
2074         ret = media_pvr_get_aspectratio(pvr, (media_pvr_aspectratio_e*)&i_value);
2075         if (ret != MEDIA_CONTENT_ERROR_NONE)
2076                 content_error("Fail to media_pvr_get_aspectratio");
2077         content_debug("aspectratio [%d]", i_value);
2078
2079         ret = media_pvr_get_highlight(pvr, &b_value);
2080         if (ret != MEDIA_CONTENT_ERROR_NONE)
2081                 content_error("Fail to media_pvr_get_highlight");
2082         content_debug("highlight [%d]", b_value);
2083
2084
2085         return TRUE;
2086 }
2087
2088 int test_pvr()
2089 {
2090         int ret = MEDIA_CONTENT_ERROR_NONE;
2091         int media_count = 0;
2092
2093         content_debug("\n============PVR Test============\n\n");
2094
2095         test_tv_filter_create();
2096
2097         ret = media_pvr_get_media_count_from_db(g_tv_filter, &media_count);
2098         if (ret != MEDIA_CONTENT_ERROR_NONE)
2099                 content_error("media_pvr_get_media_count_from_db failed: %d", ret);
2100         else
2101                 content_debug("media_count : [%d]", media_count);
2102
2103         ret = media_pvr_foreach_media_from_db(g_tv_filter, pvr_item_cb, NULL);
2104         if (ret != MEDIA_CONTENT_ERROR_NONE)
2105                 content_error("media_pvr_foreach_media_from_db is failed");
2106
2107         test_tv_filter_destroy();
2108
2109         return ret;
2110 }
2111
2112 int test_pvr_update_db(void)
2113 {
2114         int ret = MEDIA_CONTENT_ERROR_NONE;
2115         media_pvr_h pvr = NULL;
2116
2117         ret = media_pvr_get_pvr_from_db("ff9b5a9a-a7b4-47f4-8255-84e007e25f13", &pvr);
2118         if (ret != MEDIA_CONTENT_ERROR_NONE)
2119                 content_error("media_pvr_get_pvr_from_db failed: %d", ret);
2120
2121         ret = media_pvr_set_content_lock(pvr, TRUE);
2122         if (ret != MEDIA_CONTENT_ERROR_NONE)
2123                 content_error("Fail to media_pvr_set_content_lock");
2124
2125         ret = media_pvr_set_content_watch(pvr, TRUE);
2126         if (ret != MEDIA_CONTENT_ERROR_NONE)
2127                 content_error("Fail to media_pvr_set_content_watch");
2128
2129         ret = media_pvr_set_program_title(pvr, "TEST TITLE");
2130         if (ret != MEDIA_CONTENT_ERROR_NONE)
2131                 content_error("Fail to media_pvr_set_program_title");
2132
2133         ret = media_pvr_set_highlight(pvr, TRUE);
2134         if (ret != MEDIA_CONTENT_ERROR_NONE)
2135                 content_error("Fail to media_pvr_set_highlight");
2136
2137         ret = media_pvr_update_to_db(pvr);
2138         if (ret != MEDIA_CONTENT_ERROR_NONE)
2139                 content_error("Fail to media_pvr_update_to_db");
2140
2141         if (pvr != NULL)
2142                 media_pvr_destroy(pvr);
2143
2144         return ret;
2145 }
2146 #endif
2147
2148 int main(int argc, char *argv[])
2149 {
2150         int ret = MEDIA_CONTENT_ERROR_NONE;
2151
2152         content_debug("--- content manager test start ---\n\n");
2153
2154         ret = test_connect_database();
2155         if (ret != MEDIA_CONTENT_ERROR_NONE)
2156                 return MEDIA_CONTENT_ERROR_NONE;
2157 #ifdef _USE_TVPD_MODE
2158         test_pvr();
2159
2160         test_pvr_update_db();
2161
2162         test_pvr();
2163 #endif
2164
2165 #if 0
2166         if (argc == 2) {
2167                 ret = test_ebook_text_finder(argv[1]);
2168                 if (ret != MEDIA_CONTENT_ERROR_NONE)
2169                         return ret;
2170         }
2171
2172         ret = test_move();
2173         if (ret != MEDIA_CONTENT_ERROR_NONE)
2174                 return ret;
2175
2176         ret = test_gallery_scenario();
2177         if (ret != MEDIA_CONTENT_ERROR_NONE)
2178                 return ret;
2179
2180         ret = test_get_all_music_files();
2181         if (ret != MEDIA_CONTENT_ERROR_NONE)
2182                 return ret;
2183
2184         ret = test_media_info_operation();
2185         if (ret != MEDIA_CONTENT_ERROR_NONE)
2186                 return ret;
2187
2188         ret = test_folder_operation();
2189         if (ret != MEDIA_CONTENT_ERROR_NONE)
2190                 return ret;
2191
2192         ret = test_playlist_operation();
2193         if (ret != MEDIA_CONTENT_ERROR_NONE)
2194                 return ret;
2195
2196         ret = test_bookmark_operation();
2197         if (ret != MEDIA_CONTENT_ERROR_NONE)
2198                 return ret;
2199
2200         ret = test_album_list();
2201         if (ret != MEDIA_CONTENT_ERROR_NONE)
2202                 return ret;
2203
2204         ret = test_group_operation();
2205         if (ret != MEDIA_CONTENT_ERROR_NONE)
2206                 return ret;
2207
2208         ret = test_update_operation();
2209         if (ret != MEDIA_CONTENT_ERROR_NONE)
2210                 return ret;
2211
2212         ret = test_insert();
2213         if (ret != MEDIA_CONTENT_ERROR_NONE)
2214                 return ret;
2215
2216         ret = test_move();
2217         if (ret != MEDIA_CONTENT_ERROR_NONE)
2218                 return ret;
2219
2220         if (argc == 2) {
2221                 ret = test_create_thumbnail(argv[1]);
2222                 if (ret != MEDIA_CONTENT_ERROR_NONE)
2223                         return ret;
2224         }
2225
2226         ret = DFT_test();
2227         if (ret != MEDIA_CONTENT_ERROR_NONE)
2228                 return ret;
2229
2230         ret = test_batch_operations();
2231         if (ret != MEDIA_CONTENT_ERROR_NONE)
2232                 return MEDIA_CONTENT_ERROR_NONE;
2233
2234         ret = test_scan_file();
2235         if (ret != MEDIA_CONTENT_ERROR_NONE)
2236                 return MEDIA_CONTENT_ERROR_NONE;
2237
2238         ret = test_scan_dir(true);
2239         if (ret != MEDIA_CONTENT_ERROR_NONE)
2240                 return MEDIA_CONTENT_ERROR_NONE;
2241
2242         ret = test_noti();
2243         if (ret != MEDIA_CONTENT_ERROR_NONE)
2244                 return MEDIA_CONTENT_ERROR_NONE;
2245
2246         ret = test_playlist_operation_v2();
2247         if (ret != MEDIA_CONTENT_ERROR_NONE)
2248                 return MEDIA_CONTENT_ERROR_NONE;
2249
2250         ret = test_bookmark_operation_v2();
2251         if (ret != MEDIA_CONTENT_ERROR_NONE)
2252                 return MEDIA_CONTENT_ERROR_NONE;
2253 #endif
2254
2255         ret = test_disconnect_database();
2256         if (ret != MEDIA_CONTENT_ERROR_NONE)
2257                 return ret;
2258
2259         content_debug("--- content manager test end ---\n");
2260
2261         return ret;
2262 }