Fix wrong function name
[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 #define SAFE_FREE(src)          { if (src) { free(src); src = NULL; } }
36
37 static void get_audio_meta(media_info_h media)
38 {
39         audio_meta_h audio = NULL;
40         char *c_value = NULL;
41
42         content_debug("=== audio meta ===");
43         if (media_info_get_audio(media, &audio) != MEDIA_CONTENT_ERROR_NONE)
44                 return;
45
46         if (audio_meta_get_media_id(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
47                 content_debug("audio_id : [%s]", c_value);
48                 SAFE_FREE(c_value);
49         }
50
51         if (audio_meta_get_album(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
52                 content_debug("album : [%s]", c_value);
53                 SAFE_FREE(c_value);
54         }
55
56         if (audio_meta_get_artist(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
57                 content_debug("artist : [%s]", c_value);
58                 SAFE_FREE(c_value);
59         }
60
61         if (audio_meta_get_album_artist(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
62                 content_debug("album artist : [%s]", c_value);
63                 SAFE_FREE(c_value);
64         }
65
66         if (audio_meta_get_genre(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
67                 content_debug("genre : [%s]", c_value);
68                 SAFE_FREE(c_value);
69         }
70
71         if (audio_meta_get_year(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
72                 content_debug("year : [%s]", c_value);
73                 SAFE_FREE(c_value);
74         }
75
76         if (audio_meta_get_track_num(audio, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
77                 content_debug("track number : [%s]", c_value);
78                 SAFE_FREE(c_value);
79         }
80
81         audio_meta_destroy(audio);
82 }
83
84 static void get_image_meta(media_info_h media)
85 {
86         image_meta_h image = NULL;
87         char *c_value = NULL;
88         int i_value = 0;
89         media_content_orientation_e orientation;
90
91         content_debug("=== image meta ===");
92         if (media_info_get_image(media, &image) != MEDIA_CONTENT_ERROR_NONE)
93                 return;
94
95         if (image_meta_get_width(image, &i_value) == MEDIA_CONTENT_ERROR_NONE)
96                 content_debug("width : [%d]", i_value);
97
98         if (image_meta_get_height(image, &i_value) == MEDIA_CONTENT_ERROR_NONE)
99                 content_debug("height : [%d]", i_value);
100
101         if (image_meta_get_orientation(image, &orientation) == MEDIA_CONTENT_ERROR_NONE)
102                 content_debug("orientation : [%d]", orientation);
103
104         if (image_meta_get_date_taken(image, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
105                 content_debug("datetaken : [%s]", c_value);
106                 SAFE_FREE(c_value);
107         }
108
109         image_meta_destroy(image);
110 }
111
112 static void get_book_meta(media_info_h media)
113 {
114         book_meta_h book = NULL;
115         char *c_value = NULL;
116
117         content_debug("=== book meta ===");
118         if (media_info_get_book(media, &book) != MEDIA_CONTENT_ERROR_NONE)
119                 return;
120
121         if (book_meta_get_media_id(book, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
122                 content_debug("media_id : [%s]", c_value);
123                 SAFE_FREE(c_value);
124         }
125
126         if (book_meta_get_author(book, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
127                 content_debug("author : [%s]", c_value);
128                 SAFE_FREE(c_value);
129         }
130
131         if (book_meta_get_subject(book, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
132                 content_debug("subject : [%s]", c_value);
133                 SAFE_FREE(c_value);
134         }
135
136         book_meta_destroy(book);
137 }
138
139 static void get_album_meta(media_album_h album)
140 {
141         char *c_value = NULL;
142         int i_value = 0;
143
144         content_debug("=== album meta ===");
145         if (media_album_get_album_id(album, &i_value) == MEDIA_CONTENT_ERROR_NONE)
146                 content_debug("album id : [%d]", i_value);
147
148         if (media_album_get_name(album, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
149                 content_debug("album name : [%s]", c_value);
150                 SAFE_FREE(c_value);
151         }
152
153         if (media_album_get_artist(album, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
154                 content_debug("album artist : [%s]", c_value);
155                 SAFE_FREE(c_value);
156         }
157 }
158
159 static void get_folder_meta(media_folder_h folder, char **folder_id)
160 {
161         char *c_value = NULL;
162
163         content_debug("=== folder meta ===");
164         if (media_folder_get_folder_id(folder, &c_value) != MEDIA_CONTENT_ERROR_NONE) {
165                 content_debug("folder_id : %s", c_value);
166                 *folder_id = c_value;
167         }
168
169         if (media_folder_get_path(folder, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
170                 content_debug("folder_path : %s", c_value);
171                 SAFE_FREE(c_value);
172         }
173
174         if (media_folder_get_name(folder, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
175                 content_debug("folder_name : %s", c_value);
176                 SAFE_FREE(c_value);
177         }
178 }
179
180 static void get_media_meta(media_info_h media)
181 {
182         char *c_value = NULL;
183         time_t t_value = 0;
184         unsigned long long size = 0;
185         media_content_type_e media_type = 0;
186 #ifdef _USE_TVPD_MODE
187         int i_value = 0;
188 #endif
189
190         content_debug("=== media meta ===");
191         if (media_info_get_media_type(media, &media_type) == MEDIA_CONTENT_ERROR_NONE)
192                 content_debug("media_type : [%d]", media_type);
193
194         if (media_info_get_media_id(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
195                 content_debug("media_id : [%s]", c_value);
196                 SAFE_FREE(c_value);
197         }
198
199         if (media_info_get_file_path(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
200                 content_debug("file_path : [%s]", c_value);
201                 SAFE_FREE(c_value);
202         }
203
204         if (media_info_get_display_name(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
205                 content_debug("display_name : [%s]", c_value);
206                 SAFE_FREE(c_value);
207         }
208
209         if (media_info_get_mime_type(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
210                 content_debug("mime_type : [%s]", c_value);
211                 SAFE_FREE(c_value);
212         }
213
214         if (media_info_get_thumbnail_path(media, &c_value) == MEDIA_CONTENT_ERROR_NONE) {
215                 content_debug("thumbnail_path : [%s]", c_value);
216                 SAFE_FREE(c_value);
217         }
218
219         if (media_info_get_size(media, &size) == MEDIA_CONTENT_ERROR_NONE)
220                 content_debug("size : [%lld]", size);
221
222         if (media_info_get_added_time(media, &t_value) == MEDIA_CONTENT_ERROR_NONE)
223                 content_debug("added_time : [%ld]", t_value);
224
225         if (media_info_get_modified_time(media, &t_value) == MEDIA_CONTENT_ERROR_NONE)
226                 content_debug("modified_time : [%ld]", t_value);
227
228 #ifdef _USE_TVPD_MODE
229         if (media_info_get_stitched_state(media, &i_value) == MEDIA_CONTENT_ERROR_NONE)
230                 content_debug("360 stitched : [%d]", i_value);
231 #endif
232
233         if (media_type == MEDIA_CONTENT_TYPE_MUSIC)
234                 get_audio_meta(media);
235
236         if (media_type == MEDIA_CONTENT_TYPE_IMAGE)
237                 get_image_meta(media);
238
239         if (media_type == MEDIA_CONTENT_TYPE_BOOK)
240                 get_book_meta(media);
241
242         if (media_type == MEDIA_CONTENT_TYPE_OTHERS)
243                 content_debug("Other type");
244 }
245
246 bool media_item_cb(media_info_h media, void *user_data)
247 {
248         if (!media) {
249                 content_debug("NO Item");
250                 return true;
251         }
252
253         get_media_meta(media);
254
255         return true;
256 }
257
258 bool gallery_folder_list_cb(media_folder_h folder, void *user_data)
259 {
260         media_folder_h new_folder = NULL;
261         media_folder_clone(&new_folder, folder);
262
263         GList **list = (GList**)user_data;
264         *list = g_list_append(*list, new_folder);
265
266         return true;
267 }
268
269 bool gallery_media_item_cb(media_info_h media, void *user_data)
270 {
271         media_info_h new_media = NULL;
272         int ret = MEDIA_CONTENT_ERROR_NONE;
273
274         ret = media_info_clone(&new_media, media);
275
276         if (ret != MEDIA_CONTENT_ERROR_NONE) {
277                 GList **list = (GList**)user_data;
278                 *list = g_list_append(*list, new_media);
279         }
280
281         return true;
282 }
283
284 bool folder_list_cb(media_folder_h folder, void *user_data)
285 {
286         int item_count = 0;
287         char *folder_id = NULL;
288         media_folder_h *_folder = (media_folder_h*)user_data;
289
290         content_debug("===========================");
291         if (folder != NULL) {
292                 if (_folder != NULL)
293                         media_folder_clone(_folder, folder);
294
295                 get_folder_meta(folder, &folder_id);
296
297                 if (media_folder_get_media_count_from_db(folder_id, g_filter, &item_count) != MEDIA_CONTENT_ERROR_NONE) {
298                         SAFE_FREE(folder_id);
299                         content_error("[ERROR] media_folder_get_media_count_from_db is failed");
300                         return false;
301                 }
302
303                 if (media_folder_foreach_media_from_db(folder_id, g_filter, media_item_cb, NULL) != MEDIA_CONTENT_ERROR_NONE) {
304                         SAFE_FREE(folder_id);
305                         content_error("[ERROR] media_folder_foreach_media_from_db is failed");
306                         return false;
307                 }
308
309                 SAFE_FREE(folder_id);
310                 return true;
311         } else {
312                 return false;
313         }
314
315 }
316
317 bool album_list_cb(media_album_h album, void *user_data)
318 {
319         int album_id = 0;
320         int media_count = 0;
321         int ret = MEDIA_CONTENT_ERROR_NONE;
322
323         if (album != NULL) {
324                 get_album_meta(album);
325                 media_album_get_album_id(album, &album_id);
326
327                 ret = media_album_get_media_count_from_db(album_id, NULL, &media_count);
328                 if (ret != MEDIA_CONTENT_ERROR_NONE)
329                         content_error("error media_album_get_media_count_from_db : [%d]", ret);
330
331                 content_debug("media_count : [%d]", media_count);
332
333                 ret = media_album_foreach_media_from_db(album_id, NULL, media_item_cb, NULL);
334                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
335                         content_error("error media_album_foreach_media_from_db : [%d]", ret);
336                         return false;
337                 }
338
339         } else {
340                 content_error("album item not Found!!");
341         }
342
343         return true;
344 }
345
346 bool group_list_cb(const char *group_name, void *user_data)
347 {
348         int media_count = 0;
349         int *idx = user_data;
350
351         content_debug("group item : [%s] [%d]", group_name, *idx);
352
353         if (media_group_get_media_count_from_db(group_name, *idx, g_filter, &media_count) != MEDIA_CONTENT_ERROR_NONE)
354                 return false;
355
356         content_debug("media_count : [%d]", media_count);
357
358         if (media_group_foreach_media_from_db(group_name, *idx, g_filter, media_item_cb, NULL) != MEDIA_CONTENT_ERROR_NONE)
359                 return false;
360
361         return true;
362 }
363
364 int test_filter_create(void)
365 {
366         content_debug("\n============Filter Create============\n\n");
367
368         int ret = MEDIA_CONTENT_ERROR_NONE;
369
370         /* Filter for media */
371         const char *condition = "MEDIA_TYPE=3"; /*MEDIA_TYPE 0-image, 1-video, 2-sound, 3-music, 4-other*/
372
373         ret = media_filter_create(&g_filter);
374
375         /* Set condition and collate
376          * Condition string : You can make where statement of sql.
377          * Colation : You can use collation when comparing.
378          * Ex) In case of FILE_NAME='Samsung' as condition string,
379          *      if you want to compare with NOCASE collation,
380          *      call media_filter_set_condition(g_filter, condition, MEDIA_CONTENT_COLLATE_NOCASE);
381          *      if you want to compare in case-sensitive,
382          *      call media_filter_set_condition(g_filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
383          */
384         ret = media_filter_set_condition(g_filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
385
386         return ret;
387 }
388
389 int test_filter_destroy(void)
390 {
391         content_debug("\n============Filter Create============\n\n");
392
393         int ret = MEDIA_CONTENT_ERROR_NONE;
394
395         ret = media_filter_destroy(g_filter);
396
397         return ret;
398 }
399
400 int test_connect_database(void)
401 {
402         int ret = MEDIA_CONTENT_ERROR_NONE;
403
404         content_debug("\n============DB Connection Test============\n\n");
405
406         ret = media_content_connect();
407
408         if (ret == MEDIA_CONTENT_ERROR_NONE)
409                 content_debug("connection is success\n\n");
410         else
411                 content_error("connection is failed\n\n");
412
413         return ret;
414 }
415
416 int test_gallery_scenario(void)
417 {
418         int ret = MEDIA_CONTENT_ERROR_NONE;
419         unsigned int i = 0;
420         filter_h filter = NULL;
421
422         int count;
423         GList *folder_list = NULL;
424         media_folder_h folder_handle = NULL;
425
426         /* First, Get folder list */
427         ret = media_folder_foreach_folder_from_db(filter, gallery_folder_list_cb, &folder_list);
428         if (ret != MEDIA_CONTENT_ERROR_NONE) {
429                 content_error("media_folder_foreach_folder_from_db failed: %d", ret);
430                 return -1;
431         } else {
432                 content_debug("media_folder_foreach_folder_from_db success!!");
433                 char *folder_id = NULL;
434
435                 for (i = 0; i < g_list_length(folder_list); i++) {
436                         folder_handle = (media_folder_h)g_list_nth_data(folder_list, i);
437
438                         get_folder_meta(folder_handle, &folder_id);
439
440                         ret = media_folder_get_media_count_from_db(folder_id, filter, &count);
441                         SAFE_FREE(folder_id);
442                         if (ret != MEDIA_CONTENT_ERROR_NONE) {
443                                 content_error("media_folder_get_media_count_from_db failed: %d", ret);
444                                 return -1;
445                         } else {
446                                 content_debug("media count [%d] : %d", i, count);
447                         }
448                 }
449         }
450
451         /* To check performance */
452         struct timeval start, end;
453         gettimeofday(&start, NULL);
454
455         /* Second, Get all item list */
456         media_info_h media_handle = NULL;
457         GList *all_item_list = NULL;
458
459         media_content_collation_e collate_type = MEDIA_CONTENT_COLLATE_NOCASE;
460         media_content_order_e order_type = MEDIA_CONTENT_ORDER_DESC;
461         ret = media_filter_create(&filter);
462         if (ret != MEDIA_CONTENT_ERROR_NONE) {
463                 content_error("Fail to create filter");
464                 return ret;
465         }
466         ret = media_filter_set_condition(filter, "MEDIA_TYPE = 0", collate_type);
467         if (ret != MEDIA_CONTENT_ERROR_NONE) {
468                 media_filter_destroy(filter);
469                 content_error("Fail to set condition");
470                 return ret;
471         }
472         ret = media_filter_set_order(filter, order_type, MEDIA_DISPLAY_NAME, collate_type);
473         if (ret != MEDIA_CONTENT_ERROR_NONE) {
474                 media_filter_destroy(filter);
475                 content_error("Fail to set order");
476                 return ret;
477         }
478
479         ret = media_info_foreach_media_from_db(filter, gallery_media_item_cb, &all_item_list);
480         if (ret != MEDIA_CONTENT_ERROR_NONE) {
481                 content_error("media_info_foreach_media_from_db failed: %d", ret);
482                 media_filter_destroy(filter);
483                 return -1;
484         } else {
485                 content_debug("media_info_foreach_media_from_db success");
486
487                 for (i = 0; i < g_list_length(all_item_list); i++) {
488                         media_handle = (media_info_h)g_list_nth_data(all_item_list, i);
489                         get_media_meta(media_handle);
490                 }
491         }
492
493         media_filter_destroy(filter);
494         filter = NULL;
495
496         /* To check performance */
497         gettimeofday(&end, NULL);
498         long time = (end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec);
499         content_debug("Time : %ld\n", time);
500
501         /* Third, Get item list of a folder */
502         GList *item_list = NULL;
503
504         for (i = 0; i < g_list_length(folder_list); i++) {
505                 unsigned int j = 0;
506                 char *folder_id = NULL;
507                 folder_handle = (media_folder_h)g_list_nth_data(folder_list, i);
508
509                 get_folder_meta(folder_handle, &folder_id);
510
511                 ret = media_folder_foreach_media_from_db(folder_id, filter, gallery_media_item_cb, &item_list);
512                 SAFE_FREE(folder_id);
513
514                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
515                         content_error("media_folder_foreach_media_from_db failed: %d", ret);
516                         return -1;
517                 } else {
518                         content_error("media_folder_foreach_media_from_db success!");
519
520                         for (j = 0; j < g_list_length(item_list); j++) {
521                                 media_handle = (media_info_h)g_list_nth_data(item_list, j);
522                                 get_media_meta(media_handle);
523                         }
524                 }
525         }
526
527         /* Remove folder list */
528         if (folder_list) {
529                 for (i = 0; i < g_list_length(folder_list); i++) {
530                         folder_handle = (media_folder_h)g_list_nth_data(folder_list, i);
531                         media_folder_destroy(folder_handle);
532                 }
533
534                 g_list_free(folder_list);
535         }
536
537         /* Remove all items list */
538         if (all_item_list) {
539                 for (i = 0; i < g_list_length(all_item_list); i++) {
540                         media_handle = (media_info_h)g_list_nth_data(all_item_list, i);
541                         ret = media_info_destroy(media_handle);
542                         if (ret != MEDIA_CONTENT_ERROR_NONE)
543                                 content_error("media_info_destroy failed: %d", ret);
544                 }
545
546                 g_list_free(all_item_list);
547         }
548
549         /* Remove items list */
550         if (item_list) {
551                 for (i = 0; i < g_list_length(item_list); i++) {
552                         media_handle = (media_info_h)g_list_nth_data(item_list, i);
553                         ret = media_info_destroy(media_handle);
554                         if (ret != MEDIA_CONTENT_ERROR_NONE)
555                                 content_error("media_info_destroy failed: %d", ret);
556                 }
557
558                 g_list_free(item_list);
559         }
560
561         return MEDIA_CONTENT_ERROR_NONE;
562 }
563
564 /*Get All Music file. sort by Title and not case sensitive*/
565 int test_get_all_music_files(void)
566 {
567         int ret = MEDIA_CONTENT_ERROR_NONE;
568         int media_count = 0;
569         filter_h filter;
570
571         /*Set Filter*/
572         const char *condition = "MEDIA_TYPE=3"; /*0-image, 1-video, 2-sound, 3-music, 4-other*/
573
574         ret = media_filter_create(&filter);
575         if (ret != MEDIA_CONTENT_ERROR_NONE) {
576                 content_error("Fail to create filter");
577                 return ret;
578         }
579         ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_LOCALIZED);
580         if (ret != MEDIA_CONTENT_ERROR_NONE) {
581                 media_filter_destroy(filter);
582                 content_error("Fail to set condition");
583                 return ret;
584         }
585         ret = media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_TITLE, MEDIA_CONTENT_COLLATE_LOCALIZED);
586         if (ret != MEDIA_CONTENT_ERROR_NONE) {
587                 media_filter_destroy(filter);
588                 content_error("Fail to set order");
589                 return ret;
590         }
591
592         /*Get Media Count*/
593         ret = media_info_get_media_count_from_db(filter, &media_count);
594         if (ret != MEDIA_CONTENT_ERROR_NONE) {
595                 media_filter_destroy(filter);
596                 content_error("Fail to get media count");
597                 return ret;
598         }
599
600         content_debug("media_count : [%d]", media_count);
601
602         ret = media_info_foreach_media_from_db(filter, media_item_cb, NULL);
603         if (ret != MEDIA_CONTENT_ERROR_NONE) {
604                 media_filter_destroy(filter);
605                 content_error("Fail to get media");
606                 return ret;
607         }
608
609         ret = media_filter_destroy(filter);
610
611         return ret;
612 }
613
614 int test_media_info_operation(void)
615 {
616         int ret = MEDIA_CONTENT_ERROR_NONE;
617         int media_count = 0;
618
619         content_debug("\n============Media info Test============\n\n");
620
621         test_filter_create();
622
623         ret = media_info_get_media_count_from_db(g_filter, &media_count);
624         if (ret != MEDIA_CONTENT_ERROR_NONE)
625                 content_error("media_info_get_media_count_from_db failed: %d", ret);
626         else
627                 content_debug("media_count : [%d]", media_count);
628
629         ret = media_info_foreach_media_from_db(g_filter, media_item_cb, NULL);
630         if (ret == MEDIA_CONTENT_ERROR_NONE)
631                 content_debug("media_info_foreach_media_from_db is success");
632         else
633                 content_error("media_info_foreach_media_from_db is failed");
634
635         test_filter_destroy();
636
637         return ret;
638 }
639
640 int test_folder_operation(void)
641 {
642         int ret = MEDIA_CONTENT_ERROR_NONE;
643         filter_h filter = NULL;
644         media_folder_h folder = NULL;
645         char *folder_id = NULL;
646         int count = 0;
647
648         content_debug("\n============Folder Test============\n\n");
649
650         test_filter_create();
651
652         ret = media_filter_create(&filter);
653         if (ret != MEDIA_CONTENT_ERROR_NONE) {
654                 content_error("[ERROR] media_folder_filter_create is failed");
655                 return ret;
656         }
657
658         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*/
659         media_filter_set_offset(filter, 0, 5);
660         media_filter_set_order(filter, MEDIA_CONTENT_ORDER_DESC, MEDIA_PATH, MEDIA_CONTENT_COLLATE_NOCASE);
661
662         ret = media_folder_get_folder_count_from_db(filter, &count);
663         content_debug("Folder count : [%d]", count);
664
665         ret = media_folder_foreach_folder_from_db(filter, folder_list_cb, &folder);
666
667         filter_h m_filter = NULL;
668
669         ret = media_filter_create(&m_filter);
670         if (ret != MEDIA_CONTENT_ERROR_NONE) {
671                 test_filter_destroy();
672                 media_filter_destroy(filter);
673                 content_error("[ERROR] media_info_filter_create is failed");
674                 return ret;
675         }
676
677         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*/
678         media_filter_set_offset(m_filter, 0, 5);
679         media_filter_set_order(m_filter, MEDIA_CONTENT_ORDER_DESC, MEDIA_PATH, MEDIA_CONTENT_COLLATE_NOCASE);
680
681         ret = media_folder_foreach_media_from_db(folder_id, m_filter, media_item_cb, NULL);
682         if (ret != MEDIA_CONTENT_ERROR_NONE)
683                 content_error("[ERROR] media_folder_foreach_media_from_db is failed, error code : %d", ret);
684
685         media_filter_destroy(filter);
686         media_filter_destroy(m_filter);
687
688         test_filter_destroy();
689
690         /* fix prevent: Resource Leak */
691         SAFE_FREE(folder_id);
692
693         return ret;
694 }
695
696 int test_album_list(void)
697 {
698         content_debug("\n============Album Test============\n\n");
699
700         int ret = MEDIA_CONTENT_ERROR_NONE;
701         int album_count = 0;
702         filter_h filter;
703
704         /*Set Filter*/
705         const char *condition = "MEDIA_TYPE=3"; /*0-image, 1-video, 2-sound, 3-music, 4-other*/
706
707         ret = media_filter_create(&filter);
708         if (ret != MEDIA_CONTENT_ERROR_NONE) {
709                 content_error("Fail to create filter");
710                 return ret;
711         }
712         ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
713         if (ret != MEDIA_CONTENT_ERROR_NONE) {
714                 media_filter_destroy(filter);
715                 content_error("Fail to set condition");
716                 return ret;
717         }
718         ret = media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_ALBUM, MEDIA_CONTENT_COLLATE_NOCASE);
719         if (ret != MEDIA_CONTENT_ERROR_NONE) {
720                 media_filter_destroy(filter);
721                 content_error("Fail to set order");
722                 return ret;
723         }
724
725         ret = media_album_get_album_count_from_db(filter, &album_count);
726         if (ret != MEDIA_CONTENT_ERROR_NONE) {
727                 media_filter_destroy(filter);
728                 return ret;
729         } else {
730                 content_debug("album_count [%d]", album_count);
731         }
732
733         ret = media_album_foreach_album_from_db(filter, album_list_cb, NULL);
734         if (ret != MEDIA_CONTENT_ERROR_NONE)
735                 content_error("error media_album_foreach_album_from_db : [%d]", ret);
736
737         ret = media_filter_destroy(filter);
738         if (ret != MEDIA_CONTENT_ERROR_NONE)
739                 content_error("error media_filter_destroy : [%d]", ret);
740
741         return ret;
742 }
743
744 int test_group_operation(void)
745 {
746         content_debug("\n============Group Test============\n\n");
747
748         int ret = MEDIA_CONTENT_ERROR_NONE;
749         int group_count = 0;
750         int idx = 0;
751
752         ret = test_filter_create();
753         if (ret != MEDIA_CONTENT_ERROR_NONE) {
754                 content_error("[error(0x%08x)", ret);
755                 return ret;
756         }
757
758         for (idx = 0; idx < MEDIA_CONTENT_GROUP_MAX; idx++) {
759                 ret = media_group_get_group_count_from_db(g_filter, idx, &group_count);
760                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
761                         test_filter_destroy();
762                         content_error("media_group_get_group_count_from_db fail. ret=[%d] idx=[%d]", ret, idx);
763                         return ret;
764                 } else {
765                         content_debug("[%2d]group_count [%d]", idx, group_count);
766                 }
767
768                 ret = media_group_foreach_group_from_db(g_filter, idx, group_list_cb, &idx);
769                 if (ret != MEDIA_CONTENT_ERROR_NONE)
770                         content_error("media_group_foreach_group_from_db failed: %d", ret);
771         }
772         ret = test_filter_destroy();
773
774         return ret;
775 }
776
777 int test_update_operation()
778 {
779         int ret = MEDIA_CONTENT_ERROR_NONE;
780         unsigned int i = 0;
781         media_info_h media_handle = NULL;
782         GList *all_item_list = NULL;
783
784         /* Get all item list */
785         ret = media_info_foreach_media_from_db(NULL, gallery_media_item_cb, &all_item_list);
786         if (ret != MEDIA_CONTENT_ERROR_NONE) {
787                 content_error("media_info_foreach_media_from_db failed: %d", ret);
788                 return -1;
789         } else {
790                 content_debug("media_info_foreach_media_from_db success");
791
792                 for (i = 0; i < g_list_length(all_item_list); i++) {
793                         media_handle = (media_info_h)g_list_nth_data(all_item_list, i);
794                         get_media_meta(media_handle);
795                 }
796         }
797
798         return MEDIA_CONTENT_ERROR_NONE;
799 }
800
801 int test_insert(void)
802 {
803         int ret = MEDIA_CONTENT_ERROR_NONE;
804         const char *path = tzplatform_mkpath(TZ_USER_IMAGES, "Default.jpg");
805 #if 0
806         const char *path = tzplatform_mkpath(TZ_USER_DOCUMENTS, "other.txt"));
807         char *path = NULL;
808 #endif
809         media_info_h media_item = NULL;
810         content_debug("\n============DB Insert Test============\n\n");
811
812         ret = media_info_insert_to_db(path, &media_item);
813
814         if ((ret == MEDIA_CONTENT_ERROR_NONE) && (media_item != NULL)) {
815                 content_debug("Insertion is success");
816         } else {
817                 content_error("Insertion is failed");
818                 ret = media_info_destroy(media_item);
819                 return ret;
820         }
821
822         char *media_id = NULL;
823
824         ret = media_info_get_media_id(media_item, &media_id);
825         if (ret != MEDIA_CONTENT_ERROR_NONE)
826                 content_error("media_info_get_media_id failed: %d", ret);
827         else
828                 content_debug("Media ID: %s", media_id);
829
830         SAFE_FREE(media_id);
831
832         ret = media_info_destroy(media_item);
833         if (ret != MEDIA_CONTENT_ERROR_NONE)
834                 content_error("media_info_destroy failed: %d", ret);
835
836         return ret;
837 }
838
839 int test_move(void)
840 {
841         int ret = MEDIA_CONTENT_ERROR_NONE;
842         const char *move_media_id = "60aea677-4742-408e-b5f7-f2628062d06d";
843         const char *dst_path = tzplatform_mkpath(TZ_USER_IMAGES, "XX/Default1.jpg");
844         media_info_h move_media = NULL;
845
846         ret = media_info_get_media_from_db(move_media_id, &move_media);
847         if (ret == MEDIA_CONTENT_ERROR_NONE)
848                 content_debug("media_info_get_media_from_db success");
849         else
850                 content_error("media_info_get_media_from_db failed: %d", ret);
851
852         content_debug("\n============DB Move Test============\n\n");
853
854         if (move_media) {
855                 ret = media_info_move_to_db(move_media, dst_path);
856
857                 if (ret == MEDIA_CONTENT_ERROR_NONE)
858                         content_debug("Move is success");
859                 else
860                         content_error("Move is failed");
861
862                 ret = media_info_destroy(move_media);
863         } else {
864                 content_debug("There is no item : %s", move_media_id);
865         }
866
867         return ret;
868 }
869
870 int test_create_thumbnail(const char *path)
871 {
872         int ret = MEDIA_CONTENT_ERROR_NONE;
873         media_info_h media;
874         char *thumb_path = NULL;
875
876         ret = media_info_insert_to_db(path, &media);
877         content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, ret, "media_info_insert_to_db failed [%d]", ret);
878
879         ret = media_info_generate_thumbnail(media);
880         if (ret != MEDIA_CONTENT_ERROR_NONE) {
881                 content_error("media_info_generate_thumbnail failed [%d]", ret);
882                 goto FINALIZE;
883         }
884
885         ret = media_info_get_thumbnail_path(media, &thumb_path);
886         if (ret != MEDIA_CONTENT_ERROR_NONE) {
887                 content_error("media_info_get_thumbnail_path failed [%d]", ret);
888                 goto FINALIZE;
889         }
890
891         content_debug("Thumbnail Path [%s]", thumb_path);
892         if (thumb_path)
893                 free(thumb_path);
894
895 FINALIZE:
896         media_info_destroy(media);
897         return ret;
898 }
899
900 int test_ebook_text_finder(const char *keyword)
901 {
902         int ret = MEDIA_CONTENT_ERROR_NONE;
903         char **book_path_list = NULL;
904         unsigned int book_path_len = 0;
905         unsigned int i = 0;
906         media_info_h media = NULL;
907         book_meta_h book = NULL;
908         char *s_value = NULL;
909         long long ms_time = 0;
910         struct timeval start_time;
911         struct timeval end_time;
912
913         gettimeofday(&start_time, NULL);
914
915         ret = book_meta_get_path_with_keyword(keyword, &book_path_list, &book_path_len);
916         if (ret == MEDIA_CONTENT_ERROR_NONE)
917                 content_debug("book_meta_get_path_with_keyword is success");
918         else
919                 content_error("book_meta_get_path_with_keyword is failed");
920
921         gettimeofday(&end_time, NULL);
922
923         for (i = 0; i < book_path_len; i++) {
924                 ret = media_info_get_media_from_db_by_path(book_path_list[i], &media);
925                 if (ret != MEDIA_CONTENT_ERROR_NONE)
926                         continue;
927
928                 content_debug("===============================");
929                 content_debug("[%d] %s", i, book_path_list[i]);
930                 content_debug("===============================");
931                 media_info_get_title(media, &s_value);
932                 content_debug("Title  : %s", s_value);
933                 g_free(s_value);
934                 s_value = NULL;
935
936                 ret = media_info_get_book(media, &book);
937                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
938                         media_info_destroy(media);
939                         continue;
940                 }
941
942                 ret = book_meta_get_author(book, &s_value);
943                 if (ret == MEDIA_CONTENT_ERROR_NONE && s_value) {
944                         content_debug("Author : %s", s_value);
945                         g_free(s_value);
946                         s_value = NULL;
947                 }
948
949                 ret = book_meta_get_date(book, &s_value);
950                 if (ret == MEDIA_CONTENT_ERROR_NONE && s_value) {
951                         content_debug("Date   : %s", s_value);
952                         g_free(s_value);
953                         s_value = NULL;
954                 }
955
956                 content_debug("===============================");
957                 ret = book_meta_destroy(book);
958                 if (ret != MEDIA_CONTENT_ERROR_NONE)
959                         content_error("book_meta_destroy failed");
960
961                 ret = media_info_destroy(media);
962                 if (ret != MEDIA_CONTENT_ERROR_NONE)
963                         content_error("media_info_destroy failed");
964
965                 g_free(book_path_list[i]);
966         }
967
968         ms_time = (end_time.tv_sec * 1000LL + end_time.tv_usec / 1000) - (start_time.tv_sec * 1000LL + start_time.tv_usec/ 1000);
969         content_debug("Search Time [%lld]", ms_time);
970
971         g_free(book_path_list);
972
973         return ret;
974 }
975 int test_disconnect_database(void)
976 {
977         int ret = MEDIA_CONTENT_ERROR_NONE;
978         content_debug("\n============DB Disconnection Test============\n\n");
979
980         ret = media_content_disconnect();
981
982         if (ret == MEDIA_CONTENT_ERROR_NONE)
983                 content_debug("disconnection is success");
984         else
985                 content_error("disconnection is failed");
986
987         return ret;
988 }
989
990 static int g_total_photo_size = 0;
991 static int g_total_video_size = 0;
992 static int g_total_mp3_size = 0;
993 static int g_total_voice_memo_size = 0;
994
995 bool dft_cb(media_info_h media, void *user_data)
996 {
997         unsigned long long file_size = 0;
998         media_content_type_e media_type = -1;
999         char *mime_type = NULL;
1000         int ret = MEDIA_CONTENT_ERROR_NONE;
1001
1002         if (media == NULL)
1003                 return true;
1004
1005         ret = media_info_get_media_type(media, &media_type);
1006         if (ret != MEDIA_CONTENT_ERROR_NONE)
1007                 content_error("media_info_get_media_type failed: %d", ret);
1008         ret = media_info_get_size(media, &file_size);
1009         if (ret != MEDIA_CONTENT_ERROR_NONE)
1010                 content_error("media_info_get_size failed: %d", ret);
1011         ret = media_info_get_mime_type(media, &mime_type);
1012         if (ret != MEDIA_CONTENT_ERROR_NONE)
1013                 content_error("media_info_get_mime_type failed: %d", ret);
1014
1015         if (media_type == MEDIA_CONTENT_TYPE_IMAGE)
1016                 g_total_photo_size += file_size;
1017         else if (media_type == MEDIA_CONTENT_TYPE_VIDEO)
1018                 g_total_video_size += file_size;
1019         else if (media_type == MEDIA_CONTENT_TYPE_SOUND)
1020                 g_total_voice_memo_size += file_size;
1021         else if (media_type == MEDIA_CONTENT_TYPE_MUSIC) {
1022                 if ((mime_type != NULL) && (!strcmp("audio/mpeg", mime_type)))
1023                         g_total_mp3_size += file_size;
1024                 else
1025                         g_total_voice_memo_size += file_size;
1026         } else
1027                 content_debug("invalid media_type");
1028
1029         SAFE_FREE(mime_type);
1030
1031         return true;
1032
1033 }
1034
1035 int DFT_test(void)
1036 {
1037         int ret = MEDIA_CONTENT_ERROR_NONE;
1038         content_debug("\n============DFT_test============\n\n");
1039
1040         filter_h filter = NULL;
1041         int media_cnt = 0;
1042
1043         /*MEDIA_TYPE 0-image, 1-video, 2-sound, 3-music, 4-other*/
1044
1045         ret = media_filter_create(&filter);
1046
1047 /*Internal Memory*/
1048         content_debug("[Internal Memory]\n");
1049         /*1. Photo ============================================================*/
1050         ret = media_filter_set_condition(filter, "MEDIA_TYPE=0", MEDIA_CONTENT_COLLATE_DEFAULT);
1051
1052         /*Get Photo Count*/
1053         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1054         content_debug("Photo count = [%d]\n", media_cnt);
1055
1056         /*Get Photo Size*/
1057         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1058         content_debug("Photo size = [%d]\n", g_total_photo_size);
1059
1060         /*2. Video ============================================================*/
1061         ret = media_filter_set_condition(filter, "MEDIA_TYPE=1", MEDIA_CONTENT_COLLATE_DEFAULT);
1062
1063         /*Get Video Count*/
1064         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1065         content_debug("Video count = [%d]\n", media_cnt);
1066
1067         /*Get Video Size*/
1068         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1069         content_debug("Video size = [%d]\n", g_total_video_size);
1070
1071         /*3. MP3 ============================================================*/
1072         ret = media_filter_set_condition(filter, "MEDIA_TYPE=3 AND MEDIA_MIME_TYPE=\"audio/mpeg\"", MEDIA_CONTENT_COLLATE_DEFAULT);
1073
1074         /*Get MP3 Count*/
1075         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1076         content_debug("MP3 count = [%d]\n", media_cnt);
1077
1078         /*Get MP3 Size*/
1079         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1080         content_debug("MP3 size = [%d]\n", g_total_mp3_size);
1081
1082         /*4. Voice Memo ============================================================*/
1083         ret = media_filter_set_condition(filter, "(MEDIA_MIME_TYPE=\"audio/AMR\" OR MEDIA_MIME_TYPE=\"audio/mp4\")", MEDIA_CONTENT_COLLATE_DEFAULT);
1084
1085         /*Get Voice Memo Count*/
1086         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1087         content_debug("Voice Memo count = [%d]\n", media_cnt);
1088
1089         /*Get Voice Memo Size*/
1090         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1091         content_debug("Voice Memo size = [%d]\n", g_total_voice_memo_size);
1092
1093         g_total_photo_size = 0;
1094         g_total_video_size = 0;
1095         g_total_mp3_size = 0;
1096         g_total_voice_memo_size = 0;
1097
1098 /*External Memory*/
1099         content_debug("\n[External Memory]\n");
1100         /*1. Photo ============================================================*/
1101         ret = media_filter_set_condition(filter, "MEDIA_TYPE=0", MEDIA_CONTENT_COLLATE_DEFAULT);
1102
1103         /*Get Photo Count*/
1104         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1105         content_debug("Photo count = [%d]\n", media_cnt);
1106
1107         /*Get Photo Size*/
1108         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1109         content_debug("Photo size = [%d]\n", g_total_photo_size);
1110
1111         /*2. Video ============================================================*/
1112         ret = media_filter_set_condition(filter, "MEDIA_TYPE=1", MEDIA_CONTENT_COLLATE_DEFAULT);
1113
1114         /*Get Video Count*/
1115         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1116         content_debug("Video count = [%d]\n", media_cnt);
1117
1118         /*Get Video Size*/
1119         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1120         content_debug("Video size = [%d]\n", g_total_video_size);
1121
1122         /*3. MP3 ============================================================*/
1123         ret = media_filter_set_condition(filter, "MEDIA_TYPE=3 AND MEDIA_MIME_TYPE=\"audio/mpeg\"", MEDIA_CONTENT_COLLATE_DEFAULT);
1124
1125         /*Get MP3 Count*/
1126         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1127         content_debug("MP3 count = [%d]\n", media_cnt);
1128
1129         /*Get MP3 Size*/
1130         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1131         content_debug("MP3 size = [%d]\n", g_total_mp3_size);
1132
1133         /*4. Voice Memo ============================================================*/
1134         ret = media_filter_set_condition(filter, "(MEDIA_MIME_TYPE=\"audio/AMR\" OR MEDIA_MIME_TYPE=\"audio/mp4\")", MEDIA_CONTENT_COLLATE_DEFAULT);
1135
1136         /*Get Voice Memo Count*/
1137         ret = media_info_get_media_count_from_db(filter, &media_cnt);
1138         content_debug("Voice Memo count = [%d]\n", media_cnt);
1139
1140         /*Get Voice Memo Size*/
1141         ret = media_info_foreach_media_from_db(filter, dft_cb, NULL);
1142         content_debug("Voice Memo size = [%d]\n", g_total_voice_memo_size);
1143         ret = media_filter_destroy(filter);
1144
1145         return ret;
1146 }
1147
1148 void insert_batch_cb(media_content_error_e error, void * user_data)
1149 {
1150         content_debug("media_info_insert_batch_to_db completed!\n");
1151 }
1152
1153 int test_batch_operations()
1154 {
1155         int ret = -1;
1156         int i = 0;
1157         char *file_list[10];
1158
1159         for (i = 0; i < 10; i++) {
1160                 file_list[i] = g_strdup_printf("%s%d.jpg", tzplatform_mkpath(TZ_USER_CONTENT, "test/image"), i+1);
1161                 content_debug("File : %s\n", file_list[i]);
1162         }
1163
1164         ret = media_info_insert_batch_to_db((const char **)file_list, 10, insert_batch_cb, NULL);
1165         if (ret != MEDIA_CONTENT_ERROR_NONE)
1166                 content_error("media_info_insert_batch_to_db failed : %d\n", ret);
1167
1168         for (i = 0; i < 10; i++)
1169                 g_free(file_list[i]);
1170
1171         return ret;
1172 }
1173
1174 void _scan_cb(media_content_error_e err, void *user_data)
1175 {
1176         content_debug("scan callback is called : %d\n", err);
1177         g_main_loop_quit(g_loop);
1178
1179         return;
1180 }
1181
1182 int test_scan_file()
1183 {
1184         int ret = -1;
1185
1186         const char *file_path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");
1187         ret = media_content_scan_file(file_path);
1188         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1189                 content_error("Fail to media_content_scan_file : %d", ret);
1190                 return ret;
1191         }
1192
1193         return 0;
1194 }
1195
1196 gboolean test_scan_dir_start(gpointer data)
1197 {
1198         int ret = -1;
1199
1200         const char *dir_path = tzplatform_getenv(TZ_USER_CONTENT);
1201
1202         ret = media_content_scan_folder(dir_path, TRUE, _scan_cb, NULL);
1203
1204         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1205                 content_error("Fail to test_scan_dir_start : %d", ret);
1206                 return ret;
1207         }
1208
1209         return 0;
1210 }
1211
1212 gboolean cancel_scan_dir_start(gpointer data)
1213 {
1214         int ret = MEDIA_CONTENT_ERROR_NONE;
1215
1216         const char *dir_path = tzplatform_getenv(TZ_USER_IMAGES);
1217
1218         ret = media_content_cancel_scan_folder(dir_path);
1219
1220         if (ret == MEDIA_CONTENT_ERROR_NONE)
1221                 content_debug("media_content_cancel_scan_folder is success");
1222         else
1223                 content_error("media_content_cancel_scan_folder is failed");
1224
1225         return false;
1226 }
1227
1228 int test_scan_dir(int cancel)
1229 {
1230         GSource *source = NULL;
1231         GMainContext *context = NULL;
1232
1233         g_loop = g_main_loop_new(NULL, FALSE);
1234         context = g_main_loop_get_context(g_loop);
1235         source = g_idle_source_new();
1236         g_source_set_callback(source, test_scan_dir_start, NULL, NULL);
1237         g_source_attach(source, context);
1238
1239         if (cancel) {
1240                 GSource *cancel_src = NULL;
1241                 cancel_src = g_idle_source_new();
1242                 g_source_set_callback(cancel_src, cancel_scan_dir_start, NULL, NULL);
1243                 g_source_attach(cancel_src, context);
1244         }
1245
1246         g_main_loop_run(g_loop);
1247         g_main_loop_unref(g_loop);
1248
1249         return 0;
1250 }
1251
1252 void _noti_cb(media_content_error_e error,
1253                                 int pid,
1254                                 media_content_db_update_item_type_e update_item,
1255                                 media_content_db_update_type_e update_type,
1256                                 media_content_type_e media_type,
1257                                 char *uuid,
1258                                 char *path,
1259                                 char *mime_type,
1260                                 void *user_data)
1261 {
1262         if (error == 0)
1263                 content_debug("noti success! : %d\n", error);
1264         else
1265                 content_debug("error occurred! : %d\n", error);
1266
1267         content_debug("Noti from PID(%d)\n", pid);
1268
1269         if (update_item == MEDIA_ITEM_FILE)
1270                 content_debug("Noti item : MEDIA_ITEM_FILE\n");
1271         else if (update_item == MEDIA_ITEM_DIRECTORY)
1272                 content_debug("Noti item : MEDIA_ITEM_DIRECTORY\n");
1273
1274         if (update_type == MEDIA_CONTENT_INSERT)
1275                 content_debug("Noti type : MEDIA_CONTENT_INSERT\n");
1276         else if (update_type == MEDIA_CONTENT_DELETE)
1277                 content_debug("Noti type : MEDIA_CONTENT_DELETE\n");
1278         else if (update_type == MEDIA_CONTENT_UPDATE)
1279                 content_debug("Noti type : MEDIA_CONTENT_UPDATE\n");
1280
1281         content_debug("content type : %d\n", media_type);
1282
1283         if (path)
1284                 content_debug("path : %s\n", path);
1285         else
1286                 content_debug("path not\n");
1287
1288         if (uuid)
1289                 content_debug("uuid : %s\n", uuid);
1290         else
1291                 content_debug("uuid not\n");
1292
1293         if (mime_type)
1294                 content_debug("mime_type : %s\n", mime_type);
1295         else
1296                 content_debug("mime not\n");
1297
1298         if (user_data) content_debug("String : %s\n", (char *)user_data);
1299
1300         return;
1301 }
1302
1303 void _noti_cb_2(media_content_error_e error,
1304                                 int pid,
1305                                 media_content_db_update_item_type_e update_item,
1306                                 media_content_db_update_type_e update_type,
1307                                 media_content_type_e media_type,
1308                                 char *uuid,
1309                                 char *path,
1310                                 char *mime_type,
1311                                 void *user_data)
1312 {
1313         if (error == 0)
1314                 content_debug("noti_2 success! : %d\n", error);
1315         else
1316                 content_debug("error occurred! : %d\n", error);
1317
1318         content_debug("Noti_2 from PID(%d)\n", pid);
1319
1320         g_main_loop_quit(g_loop);
1321         return;
1322 }
1323
1324 static media_content_noti_h noti_h;
1325 static media_content_noti_h noti_h_2;
1326
1327 gboolean _send_noti_operations(gpointer data)
1328 {
1329         int ret = MEDIA_CONTENT_ERROR_NONE;
1330
1331         /* First of all, noti subscription */
1332         char *user_str = strdup("hi");
1333         media_content_add_db_updated_cb(_noti_cb, (void*)user_str, &noti_h);
1334         media_content_add_db_updated_cb(_noti_cb_2, (void*)user_str, &noti_h_2);
1335
1336         /* media_info_insert_to_db */
1337         media_info_h media_item = NULL;
1338         const char *path = tzplatform_mkpath(TZ_USER_CONTENT, "test/image1.jpg");
1339
1340         ret = media_info_insert_to_db(path, &media_item);
1341         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1342                 content_error("media_info_insert_to_db failed : %d", ret);
1343                 media_info_destroy(media_item);
1344                 return FALSE;
1345         }
1346
1347         content_debug("media_info_insert_to_db success");
1348
1349         media_content_remove_db_updated_cb(noti_h);
1350
1351         media_info_destroy(media_item);
1352
1353         return FALSE;
1354 }
1355
1356 int test_noti()
1357 {
1358         int ret = MEDIA_CONTENT_ERROR_NONE;
1359         GSource *source = NULL;
1360         GMainContext *context = NULL;
1361
1362         g_loop = g_main_loop_new(NULL, FALSE);
1363         context = g_main_loop_get_context(g_loop);
1364         source = g_idle_source_new();
1365         g_source_set_callback(source, _send_noti_operations, NULL, NULL);
1366         g_source_attach(source, context);
1367
1368         g_main_loop_run(g_loop);
1369         g_main_loop_unref(g_loop);
1370
1371         test_filter_destroy();
1372         media_content_remove_db_updated_cb(noti_h_2);
1373
1374         return ret;
1375 }
1376 #ifdef _USE_TVPD_MODE
1377 filter_h g_tv_filter = NULL;
1378
1379 int test_tv_filter_create(void)
1380 {
1381         content_debug("\n============Filter Create============\n\n");
1382
1383         int ret = MEDIA_CONTENT_ERROR_NONE;
1384
1385         ret = media_filter_create(&g_tv_filter);
1386
1387         ret = media_filter_set_storage(g_tv_filter, "0a22a163-e634-4a2e-ba14-0469a969eea0");
1388
1389         ret = media_filter_set_order(g_tv_filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_TITLE, MEDIA_CONTENT_COLLATE_DEFAULT);
1390
1391         return ret;
1392 }
1393
1394 int test_tv_filter_destroy(void)
1395 {
1396         content_debug("\n============Filter Destroy============\n\n");
1397
1398         int ret = MEDIA_CONTENT_ERROR_NONE;
1399
1400         ret = media_filter_destroy(g_tv_filter);
1401
1402         return ret;
1403 }
1404
1405 bool pvr_item_cb(media_pvr_h pvr, void *user_data)
1406 {
1407         int ret = MEDIA_CONTENT_ERROR_NONE;
1408         char *c_value = NULL;
1409         int i_value = 0;
1410         bool b_value = false;
1411         unsigned long long l_value = 0;
1412
1413         if (pvr == NULL) {
1414                 content_debug("NO Item");
1415                 return true;
1416         }
1417
1418         ret = media_pvr_get_media_id(pvr, &c_value);
1419         if (ret != MEDIA_CONTENT_ERROR_NONE)
1420                 content_error("Fail to media_pvr_get_media_id");
1421         content_debug("media_id [%s]", c_value);
1422         SAFE_FREE(c_value);
1423
1424         ret = media_pvr_get_channel_name(pvr, &c_value);
1425         if (ret != MEDIA_CONTENT_ERROR_NONE)
1426                 content_error("Fail to media_pvr_get_channel_name");
1427         content_debug("channel_name [%s]", c_value);
1428         SAFE_FREE(c_value);
1429
1430         ret = media_pvr_get_program_title(pvr, &c_value);
1431         if (ret != MEDIA_CONTENT_ERROR_NONE)
1432                 content_error("Fail to media_pvr_get_program_title");
1433         content_debug("program_title [%s]", c_value);
1434         SAFE_FREE(c_value);
1435
1436         ret = media_pvr_get_program_crid(pvr, &c_value);
1437         if (ret != MEDIA_CONTENT_ERROR_NONE)
1438                 content_error("Fail to media_pvr_get_program_crid");
1439         content_debug("program_crid [%s]", c_value);
1440         SAFE_FREE(c_value);
1441
1442         ret = media_pvr_get_guidance(pvr, &c_value);
1443         if (ret != MEDIA_CONTENT_ERROR_NONE)
1444                 content_error("Fail to media_pvr_get_guidance");
1445         content_debug("guidance [%s]", c_value);
1446         SAFE_FREE(c_value);
1447
1448         ret = media_pvr_get_synopsis(pvr, &c_value);
1449         if (ret != MEDIA_CONTENT_ERROR_NONE)
1450                 content_error("Fail to media_pvr_get_synopsis");
1451         content_debug("synopsis [%s]", c_value);
1452         SAFE_FREE(c_value);
1453
1454         ret = media_pvr_get_genre(pvr, &c_value);
1455         if (ret != MEDIA_CONTENT_ERROR_NONE)
1456                 content_error("Fail to media_pvr_get_synopsis");
1457         content_debug("genre [%s]", c_value);
1458         SAFE_FREE(c_value);
1459
1460         ret = media_pvr_get_language(pvr, &c_value);
1461         if (ret != MEDIA_CONTENT_ERROR_NONE)
1462                 content_error("Fail to media_pvr_get_language");
1463         content_debug("language [%s]", c_value);
1464         SAFE_FREE(c_value);
1465
1466         ret = media_pvr_get_path(pvr, &c_value);
1467         if (ret != MEDIA_CONTENT_ERROR_NONE)
1468                 content_error("Fail to media_pvr_get_path");
1469         content_debug("path [%s]", c_value);
1470         SAFE_FREE(c_value);
1471
1472         ret = media_pvr_get_storage_id(pvr, &c_value);
1473         if (ret != MEDIA_CONTENT_ERROR_NONE)
1474                 content_error("Fail to media_pvr_get_storage_id");
1475         content_debug("storage_id [%s]", c_value);
1476         SAFE_FREE(c_value);
1477
1478         ret = media_pvr_get_size(pvr, &l_value);
1479         if (ret != MEDIA_CONTENT_ERROR_NONE)
1480                 content_error("Fail to media_pvr_get_size");
1481         content_debug("size [%lld]", l_value);
1482
1483         ret = media_pvr_get_timezone(pvr, &i_value);
1484         if (ret != MEDIA_CONTENT_ERROR_NONE)
1485                 content_error("Fail to media_pvr_get_timezone");
1486         content_debug("timezone [%d]", i_value);
1487
1488         ret = media_pvr_get_ptc(pvr, &i_value);
1489         if (ret != MEDIA_CONTENT_ERROR_NONE)
1490                 content_error("Fail to media_pvr_get_ptc");
1491         content_debug("ptc [%d]", i_value);
1492
1493         ret = media_pvr_get_major(pvr, &i_value);
1494         if (ret != MEDIA_CONTENT_ERROR_NONE)
1495                 content_error("Fail to media_pvr_get_major");
1496         content_debug("[%d]", i_value);
1497
1498         ret = media_pvr_get_minor(pvr, &i_value);
1499         if (ret != MEDIA_CONTENT_ERROR_NONE)
1500                 content_error("Fail to media_pvr_get_minor");
1501         content_debug("minor [%d]", i_value);
1502
1503         ret = media_pvr_get_channel_type(pvr, &i_value);
1504         if (ret != MEDIA_CONTENT_ERROR_NONE)
1505                 content_error("Fail to media_pvr_get_channel_type");
1506         content_debug("channel_type [%d]", i_value);
1507
1508         ret = media_pvr_get_program_num(pvr, &i_value);
1509         if (ret != MEDIA_CONTENT_ERROR_NONE)
1510                 content_error("Fail to media_pvr_get_program_num");
1511         content_debug("program_num [%d]", i_value);
1512
1513         unsigned int ui_value = 0;
1514         ret = media_pvr_get_service_profile(pvr, &ui_value);
1515         if (ret != MEDIA_CONTENT_ERROR_NONE)
1516                 content_error("Fail to media_pvr_get_service_profile");
1517         content_debug("service_profile [%u]", ui_value);
1518
1519         ret = media_pvr_get_duration(pvr, &i_value);
1520         if (ret != MEDIA_CONTENT_ERROR_NONE)
1521                 content_error("Fail to media_pvr_get_duration");
1522         content_debug("duration [%d]", i_value);
1523
1524         ret = media_pvr_get_embargo_time(pvr, &i_value);
1525         if (ret != MEDIA_CONTENT_ERROR_NONE)
1526                 content_error("Fail to media_pvr_get_embargo_time");
1527         content_debug("embargo_time [%d]", i_value);
1528
1529         ret = media_pvr_get_expiry_time(pvr, &i_value);
1530         if (ret != MEDIA_CONTENT_ERROR_NONE)
1531                 content_error("Fail to media_pvr_get_expiry_time");
1532         content_debug("expiry_time [%d]", i_value);
1533
1534         ret = media_pvr_get_parental_rating(pvr, &i_value);
1535         if (ret != MEDIA_CONTENT_ERROR_NONE)
1536                 content_error("Fail to media_pvr_get_parental_rating");
1537         content_debug("parental_rating [%d]", i_value);
1538
1539         ret = media_pvr_get_start_time(pvr, &i_value);
1540         if (ret != MEDIA_CONTENT_ERROR_NONE)
1541                 content_error("Fail to media_pvr_get_start_time");
1542         content_debug("start_time [%d]", i_value);
1543
1544         ret = media_pvr_get_program_start_time(pvr, &i_value);
1545         if (ret != MEDIA_CONTENT_ERROR_NONE)
1546                 content_error("Fail to media_pvr_get_program_start_time");
1547         content_debug("program_start_time [%d]", i_value);
1548
1549         ret = media_pvr_get_program_end_time(pvr, &i_value);
1550         if (ret != MEDIA_CONTENT_ERROR_NONE)
1551                 content_error("Fail to media_pvr_get_program_end_time");
1552         content_debug("program_end_time [%d]", i_value);
1553
1554         ret = media_pvr_get_program_date(pvr, &i_value);
1555         if (ret != MEDIA_CONTENT_ERROR_NONE)
1556                 content_error("Fail to media_pvr_get_program_date");
1557         content_debug("program_date [%d]", i_value);
1558
1559         ret = media_pvr_get_timer_record(pvr, &b_value);
1560         if (ret != MEDIA_CONTENT_ERROR_NONE)
1561                 content_error("Fail to media_pvr_get_timer_record");
1562         content_debug("timer_record [%d]", b_value);
1563
1564         ret = media_pvr_get_series_record(pvr, &b_value);
1565         if (ret != MEDIA_CONTENT_ERROR_NONE)
1566                 content_error("Fail to media_pvr_get_series_record");
1567         content_debug("series_record [%d]", b_value);
1568
1569         ret = media_pvr_get_hd(pvr, &i_value);
1570         if (ret != MEDIA_CONTENT_ERROR_NONE)
1571                 content_error("Fail to media_pvr_get_hd");
1572         content_debug("hd [%d]", i_value);
1573
1574         ret = media_pvr_get_subtitle(pvr, &b_value);
1575         if (ret != MEDIA_CONTENT_ERROR_NONE)
1576                 content_error("Fail to media_pvr_get_subtitle");
1577         content_debug("subtitle [%d]", b_value);
1578
1579         ret = media_pvr_get_ttx(pvr, &b_value);
1580         if (ret != MEDIA_CONTENT_ERROR_NONE)
1581                 content_error("Fail to media_pvr_get_ttx");
1582         content_debug("ttx [%d]", b_value);
1583
1584         ret = media_pvr_get_ad(pvr, &b_value);
1585         if (ret != MEDIA_CONTENT_ERROR_NONE)
1586                 content_error("Fail to media_pvr_get_ad");
1587         content_debug("ad [%d]", b_value);
1588
1589         ret = media_pvr_get_hard_of_hearing_radio(pvr, &b_value);
1590         if (ret != MEDIA_CONTENT_ERROR_NONE)
1591                 content_error("Fail to media_pvr_get_hard_of_hearing_radio");
1592         content_debug("hard_of_hearing_radio [%d]", b_value);
1593
1594         ret = media_pvr_get_data_service(pvr, &b_value);
1595         if (ret != MEDIA_CONTENT_ERROR_NONE)
1596                 content_error("Fail to media_pvr_get_data_service");
1597         content_debug("data_service [%d]", b_value);
1598
1599         ret = media_pvr_get_content_lock(pvr, &b_value);
1600         if (ret != MEDIA_CONTENT_ERROR_NONE)
1601                 content_error("Fail to media_pvr_get_content_lock");
1602         content_debug("content_lock [%d]", b_value);
1603
1604         ret = media_pvr_get_content_watch(pvr, &b_value);
1605         if (ret != MEDIA_CONTENT_ERROR_NONE)
1606                 content_error("Fail to media_pvr_get_content_watch");
1607         content_debug("content_watch [%d]", b_value);
1608
1609         ret = media_pvr_get_has_audio_only(pvr, &b_value);
1610         if (ret != MEDIA_CONTENT_ERROR_NONE)
1611                 content_error("Fail to media_pvr_get_has_audio_only");
1612         content_debug("has_audio_only [%d]", b_value);
1613
1614         ret = media_pvr_get_is_local_record(pvr, &b_value);
1615         if (ret != MEDIA_CONTENT_ERROR_NONE)
1616                 content_error("Fail to media_pvr_get_is_local_record");
1617         content_debug("is_local_record [%d]", b_value);
1618
1619         ret = media_pvr_get_resolution(pvr, (media_pvr_resolution_e*)&i_value);
1620         if (ret != MEDIA_CONTENT_ERROR_NONE)
1621                 content_error("Fail to media_pvr_get_resolution");
1622         content_debug("resolution [%d]", i_value);
1623
1624         ret = media_pvr_get_aspectratio(pvr, (media_pvr_aspectratio_e*)&i_value);
1625         if (ret != MEDIA_CONTENT_ERROR_NONE)
1626                 content_error("Fail to media_pvr_get_aspectratio");
1627         content_debug("aspectratio [%d]", i_value);
1628
1629         return TRUE;
1630 }
1631
1632 int test_pvr()
1633 {
1634         int ret = MEDIA_CONTENT_ERROR_NONE;
1635
1636         content_debug("\n============PVR Test============\n\n");
1637
1638         test_tv_filter_create();
1639
1640         ret = media_pvr_foreach_media_from_db(g_tv_filter, pvr_item_cb, NULL);
1641         if (ret != MEDIA_CONTENT_ERROR_NONE)
1642                 content_error("media_pvr_foreach_media_from_db is failed");
1643
1644         test_tv_filter_destroy();
1645
1646         return ret;
1647 }
1648
1649 int test_pvr_update_db(void)
1650 {
1651         int ret = MEDIA_CONTENT_ERROR_NONE;
1652         media_pvr_h pvr = NULL;
1653
1654         ret = media_pvr_get_pvr_from_db("ff9b5a9a-a7b4-47f4-8255-84e007e25f13", &pvr);
1655         if (ret != MEDIA_CONTENT_ERROR_NONE)
1656                 content_error("media_pvr_get_pvr_from_db failed: %d", ret);
1657
1658         ret = media_pvr_set_content_lock(pvr, TRUE);
1659         if (ret != MEDIA_CONTENT_ERROR_NONE)
1660                 content_error("Fail to media_pvr_set_content_lock");
1661
1662         ret = media_pvr_set_content_watch(pvr, TRUE);
1663         if (ret != MEDIA_CONTENT_ERROR_NONE)
1664                 content_error("Fail to media_pvr_set_content_watch");
1665
1666         ret = media_pvr_set_program_title(pvr, "TEST TITLE");
1667         if (ret != MEDIA_CONTENT_ERROR_NONE)
1668                 content_error("Fail to media_pvr_set_program_title");
1669
1670         ret = media_pvr_update_to_db(pvr);
1671         if (ret != MEDIA_CONTENT_ERROR_NONE)
1672                 content_error("Fail to media_pvr_update_to_db");
1673
1674         if (pvr != NULL)
1675                 media_pvr_destroy(pvr);
1676
1677         return ret;
1678 }
1679 #endif
1680
1681 int main(int argc, char *argv[])
1682 {
1683         int ret = MEDIA_CONTENT_ERROR_NONE;
1684
1685         content_debug("--- content manager test start ---\n\n");
1686
1687         ret = test_connect_database();
1688         if (ret != MEDIA_CONTENT_ERROR_NONE)
1689                 return MEDIA_CONTENT_ERROR_NONE;
1690 #ifdef _USE_TVPD_MODE
1691         test_pvr();
1692
1693         test_pvr_update_db();
1694
1695         test_pvr();
1696 #endif
1697
1698 #if 0
1699         if (argc == 2) {
1700                 ret = test_ebook_text_finder(argv[1]);
1701                 if (ret != MEDIA_CONTENT_ERROR_NONE)
1702                         return ret;
1703         }
1704
1705         ret = test_move();
1706         if (ret != MEDIA_CONTENT_ERROR_NONE)
1707                 return ret;
1708
1709         ret = test_gallery_scenario();
1710         if (ret != MEDIA_CONTENT_ERROR_NONE)
1711                 return ret;
1712
1713         ret = test_get_all_music_files();
1714         if (ret != MEDIA_CONTENT_ERROR_NONE)
1715                 return ret;
1716
1717         ret = test_media_info_operation();
1718         if (ret != MEDIA_CONTENT_ERROR_NONE)
1719                 return ret;
1720
1721         ret = test_folder_operation();
1722         if (ret != MEDIA_CONTENT_ERROR_NONE)
1723                 return ret;
1724
1725         ret = test_album_list();
1726         if (ret != MEDIA_CONTENT_ERROR_NONE)
1727                 return ret;
1728
1729         ret = test_group_operation();
1730         if (ret != MEDIA_CONTENT_ERROR_NONE)
1731                 return ret;
1732
1733         ret = test_update_operation();
1734         if (ret != MEDIA_CONTENT_ERROR_NONE)
1735                 return ret;
1736
1737         ret = test_insert();
1738         if (ret != MEDIA_CONTENT_ERROR_NONE)
1739                 return ret;
1740
1741         ret = test_move();
1742         if (ret != MEDIA_CONTENT_ERROR_NONE)
1743                 return ret;
1744
1745         if (argc == 2) {
1746                 ret = test_create_thumbnail(argv[1]);
1747                 if (ret != MEDIA_CONTENT_ERROR_NONE)
1748                         return ret;
1749         }
1750
1751         ret = DFT_test();
1752         if (ret != MEDIA_CONTENT_ERROR_NONE)
1753                 return ret;
1754
1755         ret = test_batch_operations();
1756         if (ret != MEDIA_CONTENT_ERROR_NONE)
1757                 return MEDIA_CONTENT_ERROR_NONE;
1758
1759         ret = test_scan_file();
1760         if (ret != MEDIA_CONTENT_ERROR_NONE)
1761                 return MEDIA_CONTENT_ERROR_NONE;
1762
1763         ret = test_scan_dir(true);
1764         if (ret != MEDIA_CONTENT_ERROR_NONE)
1765                 return MEDIA_CONTENT_ERROR_NONE;
1766
1767         ret = test_noti();
1768         if (ret != MEDIA_CONTENT_ERROR_NONE)
1769                 return MEDIA_CONTENT_ERROR_NONE;
1770 #endif
1771
1772         ret = test_disconnect_database();
1773         if (ret != MEDIA_CONTENT_ERROR_NONE)
1774                 return ret;
1775
1776         content_debug("--- content manager test end ---\n");
1777
1778         return ret;
1779 }