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